From 4e50b2adb6de16754b629eaf18fc92d67ecdf1ca Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Sat, 8 Mar 2025 15:45:24 +0100 Subject: [PATCH] avcodec/vc2enc: Simplify writing dirac golomb codes The earlier code used a loop to determine the number of bits used and called ff_log2() on a power of two (and it would be easy to keep track of the exponent of said power-of-two); neither GCC nor Clang optimized the loop away or avoided the ff_log2(). This patch replaces the loop and the log2 with a single av_log2(). Signed-off-by: Andreas Rheinhardt --- libavcodec/vc2enc.c | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/libavcodec/vc2enc.c b/libavcodec/vc2enc.c index b82370a753..8e68d7a89d 100644 --- a/libavcodec/vc2enc.c +++ b/libavcodec/vc2enc.c @@ -189,23 +189,10 @@ typedef struct VC2EncContext { static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val) { int i; - int bits = 0; - unsigned topbit = 1, maxval = 1; + int bits = av_log2(++val); + unsigned topbit = 1 << bits; uint64_t pbits = 0; - if (!val++) { - put_bits(pb, 1, 1); - return; - } - - while (val > maxval) { - topbit <<= 1; - maxval <<= 1; - maxval |= 1; - } - - bits = ff_log2(topbit); - for (i = 0; i < bits; i++) { topbit >>= 1; av_assert2(pbits <= UINT64_MAX>>3); @@ -219,18 +206,7 @@ static av_always_inline void put_vc2_ue_uint(PutBitContext *pb, uint32_t val) static av_always_inline int count_vc2_ue_uint(uint32_t val) { - int topbit = 1, maxval = 1; - - if (!val++) - return 1; - - while (val > maxval) { - topbit <<= 1; - maxval <<= 1; - maxval |= 1; - } - - return ff_log2(topbit)*2 + 1; + return 2 * av_log2(val + 1) + 1; } /* VC-2 10.4 - parse_info() */ -- 2.45.2