Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 0/1] avcodec/vpx_rac: Add VPX range encoder support
@ 2025-03-05 16:29 MihirGore
  2025-03-05 16:29 ` [FFmpeg-devel] [PATCH 1/1] Add vpx " MihirGore
  0 siblings, 1 reply; 3+ messages in thread
From: MihirGore @ 2025-03-05 16:29 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: MihirGore

This patch adds support for VPX range encoding alongside the existing VPX range decoder.
It extends the range coder structure to handle both encoding and decoding.
Key Changes:

   - Added vpx_rac_put_prob() and vpx_rac_put() for encoding
   - Added vpx_rac_flush() for proper bitstream finalization

MihirGore (1):
  Add vpx range encoder support

 libavcodec/vpx_rac.h | 75 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 59 insertions(+), 16 deletions(-)

-- 
2.43.0

_______________________________________________
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 1/1] Add vpx range encoder support
  2025-03-05 16:29 [FFmpeg-devel] [PATCH 0/1] avcodec/vpx_rac: Add VPX range encoder support MihirGore
@ 2025-03-05 16:29 ` MihirGore
  2025-03-05 16:51   ` Andreas Rheinhardt
  0 siblings, 1 reply; 3+ messages in thread
From: MihirGore @ 2025-03-05 16:29 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: MihirGore23

From: MihirGore23 <mihirvgore@outlook.com>

---
 libavcodec/vpx_rac.h | 75 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 59 insertions(+), 16 deletions(-)

diff --git a/libavcodec/vpx_rac.h b/libavcodec/vpx_rac.h
index b158cc0754..f01358f71f 100644
--- a/libavcodec/vpx_rac.h
+++ b/libavcodec/vpx_rac.h
@@ -20,7 +20,7 @@
 
 /**
  * @file
- * Common VP5-VP9 range decoder stuff
+ * Common VP5-VP9 range encoder and decoder functions
  */
 
 #ifndef AVCODEC_VPX_RAC_H
@@ -34,24 +34,25 @@
 
 typedef struct VPXRangeCoder {
     int high;
-    int bits; /* stored negated (i.e. negative "bits" is a positive number of
-                 bits left) in order to eliminate a negate in cache refilling */
+    int bits; 
     const uint8_t *buffer;
     const uint8_t *end;
     unsigned int code_word;
     int end_reached;
+    uint8_t *output_buffer; // Added for encoding
+    uint8_t *output_end;    // Added for encoding
 } VPXRangeCoder;
 
 extern const uint8_t ff_vpx_norm_shift[256];
+
+/*Decoder Functions */
+
 int ff_vpx_init_range_decoder(VPXRangeCoder *c, const uint8_t *buf, int buf_size);
 
-/**
- * returns 1 if the end of the stream has been reached, 0 otherwise.
- */
 static av_always_inline int vpx_rac_is_end(VPXRangeCoder *c)
 {
     if (c->end <= c->buffer && c->bits >= 0)
-        c->end_reached ++;
+        c->end_reached++;
     return c->end_reached > 10;
 }
 
@@ -64,7 +65,7 @@ static av_always_inline unsigned int vpx_rac_renorm(VPXRangeCoder *c)
     c->high   <<= shift;
     code_word <<= shift;
     bits       += shift;
-    if(bits >= 0 && c->buffer < c->end) {
+    if (bits >= 0 && c->buffer < c->end) {
         code_word |= bytestream_get_be16(&c->buffer) << bits;
         bits -= 16;
     }
@@ -72,12 +73,6 @@ static av_always_inline unsigned int vpx_rac_renorm(VPXRangeCoder *c)
     return code_word;
 }
 
-#if   ARCH_ARM
-#include "arm/vpx_arith.h"
-#elif ARCH_X86
-#include "x86/vpx_arith.h"
-#endif
-
 #ifndef vpx_rac_get_prob
 #define vpx_rac_get_prob vpx_rac_get_prob
 static av_always_inline int vpx_rac_get_prob(VPXRangeCoder *c, uint8_t prob)
@@ -95,7 +90,6 @@ static av_always_inline int vpx_rac_get_prob(VPXRangeCoder *c, uint8_t prob)
 #endif
 
 #ifndef vpx_rac_get_prob_branchy
-// branchy variant, to be used where there's a branch based on the bit decoded
 static av_always_inline int vpx_rac_get_prob_branchy(VPXRangeCoder *c, int prob)
 {
     unsigned long code_word = vpx_rac_renorm(c);
@@ -117,7 +111,6 @@ static av_always_inline int vpx_rac_get_prob_branchy(VPXRangeCoder *c, int prob)
 static av_always_inline int vpx_rac_get(VPXRangeCoder *c)
 {
     unsigned int code_word = vpx_rac_renorm(c);
-    /* equiprobable */
     int low = (c->high + 1) >> 1;
     unsigned int low_shift = low << 16;
     int bit = code_word >= low_shift;
@@ -132,4 +125,54 @@ static av_always_inline int vpx_rac_get(VPXRangeCoder *c)
     return bit;
 }
 
+//Encoder Functions
+
+int ff_vpx_init_range_encoder(VPXRangeCoder *c, uint8_t *buf, int buf_size);
+
+static av_always_inline void vpx_rac_renorm_encoder(VPXRangeCoder *c)
+{
+    while (c->high < 0x10000) {
+        if (c->output_buffer < c->output_end) {
+            *c->output_buffer++ = c->code_word >> 24;
+        }
+        c->code_word <<= 8;
+        c->high <<= 8;
+    }
+}
+
+static av_always_inline void vpx_rac_put_prob(VPXRangeCoder *c, int bit, uint8_t prob)
+{
+    unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
+    if (bit) {
+        c->code_word += low << 16;
+        c->high -= low;
+    } else {
+        c->high = low;
+    }
+    vpx_rac_renorm_encoder(c);
+}
+
+static av_always_inline void vpx_rac_put(VPXRangeCoder *c, int bit)
+{
+    unsigned int low = (c->high + 1) >> 1;
+    if (bit) {
+        c->code_word += low << 16;
+        c->high -= low;
+    } else {
+        c->high = low;
+    }
+    vpx_rac_renorm_encoder(c);
+}
+
+static av_always_inline void vpx_rac_flush(VPXRangeCoder *c)
+{
+    for (int i = 0; i < 4; i++) {
+        if (c->output_buffer < c->output_end) {
+            *c->output_buffer++ = c->code_word >> 24;
+        }
+        c->code_word <<= 8;
+    }
+}
+
 #endif /* AVCODEC_VPX_RAC_H */
+
-- 
2.43.0

_______________________________________________
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/1] Add vpx range encoder support
  2025-03-05 16:29 ` [FFmpeg-devel] [PATCH 1/1] Add vpx " MihirGore
@ 2025-03-05 16:51   ` Andreas Rheinhardt
  0 siblings, 0 replies; 3+ messages in thread
From: Andreas Rheinhardt @ 2025-03-05 16:51 UTC (permalink / raw)
  To: ffmpeg-devel

MihirGore:
> From: MihirGore23 <mihirvgore@outlook.com>
> 
> ---
>  libavcodec/vpx_rac.h | 75 ++++++++++++++++++++++++++++++++++----------
>  1 file changed, 59 insertions(+), 16 deletions(-)
> 
> diff --git a/libavcodec/vpx_rac.h b/libavcodec/vpx_rac.h
> index b158cc0754..f01358f71f 100644
> --- a/libavcodec/vpx_rac.h
> +++ b/libavcodec/vpx_rac.h
> @@ -20,7 +20,7 @@
>  
>  /**
>   * @file
> - * Common VP5-VP9 range decoder stuff
> + * Common VP5-VP9 range encoder and decoder functions
>   */
>  
>  #ifndef AVCODEC_VPX_RAC_H
> @@ -34,24 +34,25 @@
>  
>  typedef struct VPXRangeCoder {
>      int high;
> -    int bits; /* stored negated (i.e. negative "bits" is a positive number of
> -                 bits left) in order to eliminate a negate in cache refilling */
> +    int bits; 
>      const uint8_t *buffer;
>      const uint8_t *end;
>      unsigned int code_word;
>      int end_reached;
> +    uint8_t *output_buffer; // Added for encoding
> +    uint8_t *output_end;    // Added for encoding

What are the advantages of reusing the same structure etc. in contrast
to using dedicated ones?

>  } VPXRangeCoder;
>  
>  extern const uint8_t ff_vpx_norm_shift[256];
> +
> +/*Decoder Functions */
> +
>  int ff_vpx_init_range_decoder(VPXRangeCoder *c, const uint8_t *buf, int buf_size);
>  
> -/**
> - * returns 1 if the end of the stream has been reached, 0 otherwise.
> - */
>  static av_always_inline int vpx_rac_is_end(VPXRangeCoder *c)
>  {
>      if (c->end <= c->buffer && c->bits >= 0)
> -        c->end_reached ++;
> +        c->end_reached++;
>      return c->end_reached > 10;
>  }
>  
> @@ -64,7 +65,7 @@ static av_always_inline unsigned int vpx_rac_renorm(VPXRangeCoder *c)
>      c->high   <<= shift;
>      code_word <<= shift;
>      bits       += shift;
> -    if(bits >= 0 && c->buffer < c->end) {
> +    if (bits >= 0 && c->buffer < c->end) {

Cosmetics and functional changes should not be mixed.

>          code_word |= bytestream_get_be16(&c->buffer) << bits;
>          bits -= 16;
>      }
> @@ -72,12 +73,6 @@ static av_always_inline unsigned int vpx_rac_renorm(VPXRangeCoder *c)
>      return code_word;
>  }
>  
> -#if   ARCH_ARM
> -#include "arm/vpx_arith.h"
> -#elif ARCH_X86
> -#include "x86/vpx_arith.h"
> -#endif
> -

Why are you doing this?

>  #ifndef vpx_rac_get_prob
>  #define vpx_rac_get_prob vpx_rac_get_prob
>  static av_always_inline int vpx_rac_get_prob(VPXRangeCoder *c, uint8_t prob)
> @@ -95,7 +90,6 @@ static av_always_inline int vpx_rac_get_prob(VPXRangeCoder *c, uint8_t prob)
>  #endif
>  
>  #ifndef vpx_rac_get_prob_branchy
> -// branchy variant, to be used where there's a branch based on the bit decoded
>  static av_always_inline int vpx_rac_get_prob_branchy(VPXRangeCoder *c, int prob)
>  {
>      unsigned long code_word = vpx_rac_renorm(c);
> @@ -117,7 +111,6 @@ static av_always_inline int vpx_rac_get_prob_branchy(VPXRangeCoder *c, int prob)
>  static av_always_inline int vpx_rac_get(VPXRangeCoder *c)
>  {
>      unsigned int code_word = vpx_rac_renorm(c);
> -    /* equiprobable */
>      int low = (c->high + 1) >> 1;
>      unsigned int low_shift = low << 16;
>      int bit = code_word >= low_shift;
> @@ -132,4 +125,54 @@ static av_always_inline int vpx_rac_get(VPXRangeCoder *c)
>      return bit;
>  }
>  
> +//Encoder Functions
> +
> +int ff_vpx_init_range_encoder(VPXRangeCoder *c, uint8_t *buf, int buf_size);
> +
> +static av_always_inline void vpx_rac_renorm_encoder(VPXRangeCoder *c)
> +{
> +    while (c->high < 0x10000) {
> +        if (c->output_buffer < c->output_end) {
> +            *c->output_buffer++ = c->code_word >> 24;
> +        }
> +        c->code_word <<= 8;
> +        c->high <<= 8;
> +    }
> +}
> +
> +static av_always_inline void vpx_rac_put_prob(VPXRangeCoder *c, int bit, uint8_t prob)
> +{
> +    unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
> +    if (bit) {
> +        c->code_word += low << 16;
> +        c->high -= low;
> +    } else {
> +        c->high = low;
> +    }
> +    vpx_rac_renorm_encoder(c);
> +}
> +
> +static av_always_inline void vpx_rac_put(VPXRangeCoder *c, int bit)
> +{
> +    unsigned int low = (c->high + 1) >> 1;
> +    if (bit) {
> +        c->code_word += low << 16;
> +        c->high -= low;
> +    } else {
> +        c->high = low;
> +    }
> +    vpx_rac_renorm_encoder(c);
> +}
> +
> +static av_always_inline void vpx_rac_flush(VPXRangeCoder *c)
> +{
> +    for (int i = 0; i < 4; i++) {
> +        if (c->output_buffer < c->output_end) {
> +            *c->output_buffer++ = c->code_word >> 24;
> +        }
> +        c->code_word <<= 8;
> +    }
> +}
> +
>  #endif /* AVCODEC_VPX_RAC_H */
> +

_______________________________________________
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-05 16:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-05 16:29 [FFmpeg-devel] [PATCH 0/1] avcodec/vpx_rac: Add VPX range encoder support MihirGore
2025-03-05 16:29 ` [FFmpeg-devel] [PATCH 1/1] Add vpx " MihirGore
2025-03-05 16:51   ` Andreas Rheinhardt

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