diff mbox series

[FFmpeg-devel,2/2] read_xbits: request fewer bits

Message ID 20230907192721.50459-3-christophe.gisquet@gmail.com
State New
Headers show
Series cached bistream: small improvements | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Christophe Gisquet Sept. 7, 2023, 7:27 p.m. UTC
This would have also helped a bitstream reader with a cache
of 32 bits.
---
 libavcodec/bitstream_template.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

Comments

Andreas Rheinhardt Sept. 7, 2023, 10:48 p.m. UTC | #1
Christophe Gisquet:
> This would have also helped a bitstream reader with a cache
> of 32 bits.
> ---
>  libavcodec/bitstream_template.h | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/libavcodec/bitstream_template.h b/libavcodec/bitstream_template.h
> index 3f90fc6a07..c27e8108b2 100644
> --- a/libavcodec/bitstream_template.h
> +++ b/libavcodec/bitstream_template.h
> @@ -423,8 +423,18 @@ static inline const uint8_t *BS_FUNC(align)(BSCTX *bc)
>   */
>  static inline int BS_FUNC(read_xbits)(BSCTX *bc, unsigned int n)
>  {
> -    int32_t cache = BS_FUNC(peek)(bc, 32);
> -    int sign = ~cache >> 31;
> +    int32_t cache;
> +    int sign;
> +
> +    if (n > bc->bits_valid)
> +        BS_FUNC(priv_refill_32)(bc);
> +
> +#if defined(BITSTREAM_READER_LE)
> +    cache = bc->bits & 0xFFFFFFFF;
> +#else
> +    cache = bc->bits >> 32;
> +#endif
> +    sign = ~cache >> 31;
>      BS_FUNC(skip_remaining)(bc, n);
>  
>      return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;

Great, this function has the same issue at the end of input as read_vlc.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/bitstream_template.h b/libavcodec/bitstream_template.h
index 3f90fc6a07..c27e8108b2 100644
--- a/libavcodec/bitstream_template.h
+++ b/libavcodec/bitstream_template.h
@@ -423,8 +423,18 @@  static inline const uint8_t *BS_FUNC(align)(BSCTX *bc)
  */
 static inline int BS_FUNC(read_xbits)(BSCTX *bc, unsigned int n)
 {
-    int32_t cache = BS_FUNC(peek)(bc, 32);
-    int sign = ~cache >> 31;
+    int32_t cache;
+    int sign;
+
+    if (n > bc->bits_valid)
+        BS_FUNC(priv_refill_32)(bc);
+
+#if defined(BITSTREAM_READER_LE)
+    cache = bc->bits & 0xFFFFFFFF;
+#else
+    cache = bc->bits >> 32;
+#endif
+    sign = ~cache >> 31;
     BS_FUNC(skip_remaining)(bc, n);
 
     return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;