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

Christophe Gisquet christophe.gisquet at gmail.com
Thu Sep 7 22:27:21 EEST 2023


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;
-- 
2.42.0



More information about the ffmpeg-devel mailing list