From 50922e3dbd5d099a67d879c4ec1d7535ebfa30a8 Mon Sep 17 00:00:00 2001 From: Martin Storsjo Date: Wed, 26 Apr 2017 23:37:11 +0300 Subject: Try to properly handle the case when the bitstream reader runs out of bits to read --- libFDK/include/FDK_bitstream.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/libFDK/include/FDK_bitstream.h b/libFDK/include/FDK_bitstream.h index d2a7e7d..d47a750 100644 --- a/libFDK/include/FDK_bitstream.h +++ b/libFDK/include/FDK_bitstream.h @@ -223,7 +223,15 @@ FDK_INLINE UINT FDKreadBits(HANDLE_FDK_BITSTREAM hBitStream, else { hBitStream->CacheWord = FDK_get (&hBitStream->hBitBuf,validBits) ; - hBitStream->BitsInCache = validBits - missingBits; + if (validBits >= missingBits) + { + hBitStream->BitsInCache = validBits - missingBits; + } + else + { + hBitStream->BitsInCache = 0; + hBitStream->CacheWord <<= missingBits - validBits; + } } return ( bits | (hBitStream->CacheWord >> hBitStream->BitsInCache)) & BitMask[numberOfBits]; @@ -243,6 +251,12 @@ FDK_INLINE UINT FDKreadBits(HANDLE_FDK_BITSTREAM hBitStream, hBitStream->CacheWord = (hBitStream->CacheWord << bitsToRead) | FDK_get (&hBitStream->hBitBuf,bitsToRead) ; hBitStream->BitsInCache += bitsToRead ; + if (hBitStream->BitsInCache < numberOfBits) + { + hBitStream->CacheWord <<= numberOfBits - hBitStream->BitsInCache; + hBitStream->BitsInCache = 0; + return (hBitStream->CacheWord >> hBitStream->BitsInCache) & validMask ; + } } hBitStream->BitsInCache -= numberOfBits ; -- cgit v1.2.3