diff mbox series

[FFmpeg-devel,12/35] avcodec/proresenc_anatoliy: shuffle encode_codeword() code to match Kostya encoder

Message ID 20231211014429.1841681-13-u@pkh.me
State Accepted
Commit 1832bd7838f7062201eacdbdda53821789d59c72
Headers show
Series [FFmpeg-devel,01/35] avcodec/proresenc_kostya: remove an unnecessary parenthesis level in MAKE_CODE() macro | expand

Commit Message

Clément Bœsch Dec. 11, 2023, 1:35 a.m. UTC
Code is functionally identical, it's just rename of variables, cosmetics
and branch logic shuffling.
---
 libavcodec/proresenc_anatoliy.c | 45 +++++++++++++++++----------------
 1 file changed, 23 insertions(+), 22 deletions(-)

Comments

Stefano Sabatini Dec. 18, 2023, 12:07 a.m. UTC | #1
On date Monday 2023-12-11 02:35:13 +0100, Clément Bœsch wrote:
> Code is functionally identical, it's just rename of variables, cosmetics
> and branch logic shuffling.
> ---
>  libavcodec/proresenc_anatoliy.c | 45 +++++++++++++++++----------------
>  1 file changed, 23 insertions(+), 22 deletions(-)
> 
> diff --git a/libavcodec/proresenc_anatoliy.c b/libavcodec/proresenc_anatoliy.c
> index 804ff6ddf7..c7e43fc88c 100644
> --- a/libavcodec/proresenc_anatoliy.c
> +++ b/libavcodec/proresenc_anatoliy.c
> @@ -226,31 +226,32 @@ static int int_from_list_or_default(void *ctx, const char *val_name, int val,
>      return default_value;
>  }
>  
> -static void encode_codeword(PutBitContext *pb, int val, int codebook)
> +static void encode_codeword(PutBitContext *pb, int val, unsigned codebook)
>  {
> -    unsigned int rice_order, exp_order, switch_bits, first_exp, exp, zeros;
> -
> -    /* number of bits to switch between rice and exp golomb */
> -    switch_bits = codebook & 3;
> -    rice_order  = codebook >> 5;
> -    exp_order   = (codebook >> 2) & 7;
> -
> -    first_exp = ((switch_bits + 1) << rice_order);
> -
> -    if (val >= first_exp) { /* exp golomb */
> -        val -= first_exp;
> -        val += (1 << exp_order);
> -        exp = av_log2(val);
> -        zeros = exp - exp_order + switch_bits + 1;
> -        put_bits(pb, zeros, 0);
> -        put_bits(pb, exp + 1, val);
> -    } else if (rice_order) {
> -        put_bits(pb, (val >> rice_order), 0);
> -        put_bits(pb, 1, 1);
> -        put_sbits(pb, rice_order, val);
> +    unsigned int rice_order, exp_order, switch_bits, switch_val;
> +    int exponent;
> +
> +    /* number of prefix bits to switch between Rice and expGolomb */
> +    switch_bits = (codebook & 3) + 1;
> +    rice_order  =  codebook >> 5;       /* rice code order */
> +    exp_order   = (codebook >> 2) & 7;  /* exp golomb code order */
> +
> +    switch_val  = switch_bits << rice_order;
> +
> +    if (val >= switch_val) {
> +        val -= switch_val - (1 << exp_order);
> +        exponent = av_log2(val);
> +
> +        put_bits(pb, exponent - exp_order + switch_bits, 0);
> +        put_bits(pb, exponent + 1, val);
>      } else {
> -        put_bits(pb, val, 0);
> +        exponent = val >> rice_order;
> +
> +        if (exponent)
> +            put_bits(pb, exponent, 0);
>          put_bits(pb, 1, 1);
> +        if (rice_order)
> +            put_sbits(pb, rice_order, val);
>      }
>  }
>  
> -- 
> 2.43.0

Should be equivalent to the original code (and with some small
optimizations), so it should be fine.
diff mbox series

Patch

diff --git a/libavcodec/proresenc_anatoliy.c b/libavcodec/proresenc_anatoliy.c
index 804ff6ddf7..c7e43fc88c 100644
--- a/libavcodec/proresenc_anatoliy.c
+++ b/libavcodec/proresenc_anatoliy.c
@@ -226,31 +226,32 @@  static int int_from_list_or_default(void *ctx, const char *val_name, int val,
     return default_value;
 }
 
-static void encode_codeword(PutBitContext *pb, int val, int codebook)
+static void encode_codeword(PutBitContext *pb, int val, unsigned codebook)
 {
-    unsigned int rice_order, exp_order, switch_bits, first_exp, exp, zeros;
-
-    /* number of bits to switch between rice and exp golomb */
-    switch_bits = codebook & 3;
-    rice_order  = codebook >> 5;
-    exp_order   = (codebook >> 2) & 7;
-
-    first_exp = ((switch_bits + 1) << rice_order);
-
-    if (val >= first_exp) { /* exp golomb */
-        val -= first_exp;
-        val += (1 << exp_order);
-        exp = av_log2(val);
-        zeros = exp - exp_order + switch_bits + 1;
-        put_bits(pb, zeros, 0);
-        put_bits(pb, exp + 1, val);
-    } else if (rice_order) {
-        put_bits(pb, (val >> rice_order), 0);
-        put_bits(pb, 1, 1);
-        put_sbits(pb, rice_order, val);
+    unsigned int rice_order, exp_order, switch_bits, switch_val;
+    int exponent;
+
+    /* number of prefix bits to switch between Rice and expGolomb */
+    switch_bits = (codebook & 3) + 1;
+    rice_order  =  codebook >> 5;       /* rice code order */
+    exp_order   = (codebook >> 2) & 7;  /* exp golomb code order */
+
+    switch_val  = switch_bits << rice_order;
+
+    if (val >= switch_val) {
+        val -= switch_val - (1 << exp_order);
+        exponent = av_log2(val);
+
+        put_bits(pb, exponent - exp_order + switch_bits, 0);
+        put_bits(pb, exponent + 1, val);
     } else {
-        put_bits(pb, val, 0);
+        exponent = val >> rice_order;
+
+        if (exponent)
+            put_bits(pb, exponent, 0);
         put_bits(pb, 1, 1);
+        if (rice_order)
+            put_sbits(pb, rice_order, val);
     }
 }