diff mbox series

[FFmpeg-devel,5/7] get_bits: improve and fix get_bits_long for 32b

Message ID 20200414102503.7858-6-christophe.gisquet@gmail.com
State New
Headers show
Series Port cache bitstream reader to 32bits, and improve | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate fail Make fate failed

Commit Message

Christophe Gisquet April 14, 2020, 10:25 a.m. UTC
The new code is guaranteed to read at least 32bits, which is likely ok with
the usual case that get_bits without cache can read up to 25.
---
 libavcodec/get_bits.h | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

Comments

Michael Niedermayer April 14, 2020, 8:17 p.m. UTC | #1
On Tue, Apr 14, 2020 at 10:25:01AM +0000, Christophe Gisquet wrote:
> The new code is guaranteed to read at least 32bits, which is likely ok with
> the usual case that get_bits without cache can read up to 25.
> ---
>  libavcodec/get_bits.h | 29 ++++++++++++++++++++++++++---
>  1 file changed, 26 insertions(+), 3 deletions(-)
> 
> diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h
> index 4f75f9dd84..da054ebfcb 100644
> --- a/libavcodec/get_bits.h
> +++ b/libavcodec/get_bits.h
> @@ -608,21 +608,44 @@ static inline void skip_bits1(GetBitContext *s)
>   */
>  static inline unsigned int get_bits_long(GetBitContext *s, int n)
>  {
> +    unsigned ret = 0;

Dead assignment if CACHED_BITSTREAM_READER is not used


>      av_assert2(n>=0 && n<=32);
>      if (!n) {
>          return 0;
>  #if CACHED_BITSTREAM_READER
>      }
> -    return get_bits(s, n);
> +
> +# ifdef BITSTREAM_READER_LE
> +    unsigned left = 0;
> +# endif
> +    if (n > s->bits_left) {
> +        n -= s->bits_left;

> +# ifdef BITSTREAM_READER_LE
> +        left = s->bits_left;
> +        ret = get_val(s, s->bits_left, 1);
> +        refill_all(s, 1);
> +# else
> +        ret = get_val(s, s->bits_left, 0);
> +        refill_all(s, 0);
> +# endif

Its a pitty one cannot write the following

+# ifdef BITSTREAM_READER_LE
+        left = s->bits_left;
+# endif
+        ret = get_val(s, s->bits_left, BITSTREAM_READER_LE);
+        refill_all(s, BITSTREAM_READER_LE);

that would be nicer looking if it worked


[...]
diff mbox series

Patch

diff --git a/libavcodec/get_bits.h b/libavcodec/get_bits.h
index 4f75f9dd84..da054ebfcb 100644
--- a/libavcodec/get_bits.h
+++ b/libavcodec/get_bits.h
@@ -608,21 +608,44 @@  static inline void skip_bits1(GetBitContext *s)
  */
 static inline unsigned int get_bits_long(GetBitContext *s, int n)
 {
+    unsigned ret = 0;
     av_assert2(n>=0 && n<=32);
     if (!n) {
         return 0;
 #if CACHED_BITSTREAM_READER
     }
-    return get_bits(s, n);
+
+# ifdef BITSTREAM_READER_LE
+    unsigned left = 0;
+# endif
+    if (n > s->bits_left) {
+        n -= s->bits_left;
+# ifdef BITSTREAM_READER_LE
+        left = s->bits_left;
+        ret = get_val(s, s->bits_left, 1);
+        refill_all(s, 1);
+# else
+        ret = get_val(s, s->bits_left, 0);
+        refill_all(s, 0);
+# endif
+    }
+
+#ifdef BITSTREAM_READER_LE
+    ret = get_val(s, n, 1) << left | ret;
+#else
+    ret = get_val(s, n, 0) | ret << n;
+#endif
+
+    return ret;
 #else
     } else if (n <= MIN_CACHE_BITS) {
         return get_bits(s, n);
     } else {
 #ifdef BITSTREAM_READER_LE
-        unsigned ret = get_bits(s, 16);
+        ret = get_bits(s, 16);
         return ret | (get_bits(s, n - 16) << 16);
 #else
-        unsigned ret = get_bits(s, 16) << (n - 16);
+        ret = get_bits(s, 16) << (n - 16);
         return ret | get_bits(s, n - 16);
 #endif
     }