[Scummvm-git-logs] scummvm master -> bd10131242210262eb23c5a62c223039cabf905c

wjp wjp at usecode.org
Thu Aug 24 19:51:14 CEST 2017


This automated email contains information about 10 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
dde259f068 COMMON: Remove BitStream base class to allow inlining calls
60fae8847e IMAGE: Inline indeo getVLC2
ac95030ef6 COMMON: Cache BitStream size
e7428bca2e COMMON: Keep track of bitstream position
0c8f95603f COMMON: Add BitStream classes for memory buffers
edfdbb9dd7 IMAGE: Use new BitStreamMemory class for indeo
e72f681ceb VIDEO: Use new BitStreamMemory class for SmackerDecoder
47539e1939 VIDEO: Use new BitStreamMemory class for PSXStreamDecoder
4278cff7f0 COMMON: Optimize BitStream::getBits
bd10131242 Merge pull request #975 from wjp/bitstream


Commit: dde259f06813534c7179a714403d8fd6fe918d09
    https://github.com/scummvm/scummvm/commit/dde259f06813534c7179a714403d8fd6fe918d09
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2017-08-24T19:46:59+02:00

Commit Message:
COMMON: Remove BitStream base class to allow inlining calls

All users of BitStream were in fact using a specific, hardcoded variant,
so we can hardcode that variant, removing the need for virtual calls,
and enabling inlining.

Changed paths:
    audio/decoders/qdm2.cpp
    common/bitstream.h
    common/huffman.cpp
    common/huffman.h
    engines/groovie/stuffit.cpp
    engines/macventure/container.h
    engines/macventure/image.cpp
    engines/macventure/image.h
    image/codecs/svq1.cpp
    image/codecs/svq1.h
    video/bink_decoder.h
    video/psx_decoder.cpp
    video/psx_decoder.h
    video/smk_decoder.cpp
    video/smk_decoder.h


diff --git a/audio/decoders/qdm2.cpp b/audio/decoders/qdm2.cpp
index a2f4326..b8d007d 100644
--- a/audio/decoders/qdm2.cpp
+++ b/audio/decoders/qdm2.cpp
@@ -213,9 +213,9 @@ private:
 	void fill_coding_method_array(sb_int8_array tone_level_idx, sb_int8_array tone_level_idx_temp,
 	                              sb_int8_array coding_method, int nb_channels,
 	                              int c, int superblocktype_2_3, int cm_table_select);
-	void synthfilt_build_sb_samples(Common::BitStream *gb, int length, int sb_min, int sb_max);
-	void init_quantized_coeffs_elem0(int8 *quantized_coeffs, Common::BitStream *gb, int length);
-	void init_tone_level_dequantization(Common::BitStream *gb, int length);
+	void synthfilt_build_sb_samples(Common::BitStream32LELSB *gb, int length, int sb_min, int sb_max);
+	void init_quantized_coeffs_elem0(int8 *quantized_coeffs, Common::BitStream32LELSB *gb, int length);
+	void init_tone_level_dequantization(Common::BitStream32LELSB *gb, int length);
 	void process_subpacket_9(QDM2SubPNode *node);
 	void process_subpacket_10(QDM2SubPNode *node, int length);
 	void process_subpacket_11(QDM2SubPNode *node, int length);
@@ -224,7 +224,7 @@ private:
 	void qdm2_decode_super_block(void);
 	void qdm2_fft_init_coefficient(int sub_packet, int offset, int duration,
 	                               int channel, int exp, int phase);
-	void qdm2_fft_decode_tones(int duration, Common::BitStream *gb, int b);
+	void qdm2_fft_decode_tones(int duration, Common::BitStream32LELSB *gb, int b);
 	void qdm2_decode_fft_packets(void);
 	void qdm2_fft_generate_tone(FFTTone *tone);
 	void qdm2_fft_tone_synthesizer(uint8 sub_packet);
@@ -677,7 +677,7 @@ void ff_mpa_synth_filter(int16 *synth_buf_ptr, int *synth_buf_offset,
  *                  read the longest vlc code
  *                  = (max_vlc_length + bits - 1) / bits
  */
-static int getVlc2(Common::BitStream *s, int16 (*table)[2], int bits, int maxDepth) {
+static int getVlc2(Common::BitStream32LELSB *s, int16 (*table)[2], int bits, int maxDepth) {
 	int index = s->peekBits(bits);
 	int code = table[index][0];
 	int n = table[index][1];
@@ -1227,7 +1227,7 @@ QDM2Stream::~QDM2Stream() {
 	delete[] _compressedData;
 }
 
-static int qdm2_get_vlc(Common::BitStream *gb, VLC *vlc, int flag, int depth) {
+static int qdm2_get_vlc(Common::BitStream32LELSB *gb, VLC *vlc, int flag, int depth) {
 	int value = getVlc2(gb, vlc->table, vlc->bits, depth);
 
 	// stage-2, 3 bits exponent escape sequence
@@ -1246,7 +1246,7 @@ static int qdm2_get_vlc(Common::BitStream *gb, VLC *vlc, int flag, int depth) {
 	return value;
 }
 
-static int qdm2_get_se_vlc(VLC *vlc, Common::BitStream *gb, int depth)
+static int qdm2_get_se_vlc(VLC *vlc, Common::BitStream32LELSB *gb, int depth)
 {
 	int value = qdm2_get_vlc(gb, vlc, 0, depth);
 
@@ -1612,7 +1612,7 @@ void QDM2Stream::fill_coding_method_array(sb_int8_array tone_level_idx, sb_int8_
  * @param sb_min    lower subband processed (sb_min included)
  * @param sb_max    higher subband processed (sb_max excluded)
  */
-void QDM2Stream::synthfilt_build_sb_samples(Common::BitStream *gb, int length, int sb_min, int sb_max) {
+void QDM2Stream::synthfilt_build_sb_samples(Common::BitStream32LELSB *gb, int length, int sb_min, int sb_max) {
 	int sb, j, k, n, ch, run, channels;
 	int joined_stereo, zero_encoding, chs;
 	int type34_first;
@@ -1792,7 +1792,7 @@ void QDM2Stream::synthfilt_build_sb_samples(Common::BitStream *gb, int length, i
  * @param gb        bitreader context
  * @param length    packet length in bits
  */
-void QDM2Stream::init_quantized_coeffs_elem0(int8 *quantized_coeffs, Common::BitStream *gb, int length) {
+void QDM2Stream::init_quantized_coeffs_elem0(int8 *quantized_coeffs, Common::BitStream32LELSB *gb, int length) {
 	int i, k, run, level, diff;
 
 	if ((length - gb->pos()) < 16)
@@ -1826,7 +1826,7 @@ void QDM2Stream::init_quantized_coeffs_elem0(int8 *quantized_coeffs, Common::Bit
  * @param gb        bitreader context
  * @param length    packet length in bits
  */
-void QDM2Stream::init_tone_level_dequantization(Common::BitStream *gb, int length) {
+void QDM2Stream::init_tone_level_dequantization(Common::BitStream32LELSB *gb, int length) {
 	int sb, j, k, n, ch;
 
 	for (ch = 0; ch < _channels; ch++) {
@@ -2021,7 +2021,7 @@ void QDM2Stream::qdm2_decode_super_block(void) {
 	average_quantized_coeffs(); // average elements in quantized_coeffs[max_ch][10][8]
 
 	Common::MemoryReadStream *d = new Common::MemoryReadStream(_compressedData, _packetSize*8);
-	Common::BitStream *gb = new Common::BitStream32LELSB(d);
+	Common::BitStream32LELSB *gb = new Common::BitStream32LELSB(d);
 	//qdm2_decode_sub_packet_header
 	header.type = gb->getBits(8);
 
@@ -2188,7 +2188,7 @@ void QDM2Stream::qdm2_fft_init_coefficient(int sub_packet, int offset, int durat
 	_fftCoefsIndex++;
 }
 
-void QDM2Stream::qdm2_fft_decode_tones(int duration, Common::BitStream *gb, int b) {
+void QDM2Stream::qdm2_fft_decode_tones(int duration, Common::BitStream32LELSB *gb, int b) {
 	int channel, stereo, phase, exp;
 	int local_int_4,  local_int_8,  stereo_phase,  local_int_10;
 	int local_int_14, stereo_exp, local_int_20, local_int_28;
diff --git a/common/bitstream.h b/common/bitstream.h
index 325118b..ae5677d 100644
--- a/common/bitstream.h
+++ b/common/bitstream.h
@@ -32,50 +32,6 @@
 
 namespace Common {
 
-/** A bit stream. */
-class BitStream {
-public:
-	virtual ~BitStream() {
-	}
-
-	/** Return the stream position in bits. */
-	virtual uint32 pos() const = 0;
-
-	/** Return the stream size in bits. */
-	virtual uint32 size() const = 0;
-
-	/** Has the end of the stream been reached? */
-	virtual bool eos() const = 0;
-
-	/** Rewind the bit stream back to the start. */
-	virtual void rewind() = 0;
-
-	/** Skip the specified amount of bits. */
-	virtual void skip(uint32 n) = 0;
-
-	/** Skip the bits to closest data value border. */
-	virtual void align() = 0;
-
-	/** Read a bit from the bit stream. */
-	virtual uint32 getBit() = 0;
-
-	/** Read a multi-bit value from the bit stream. */
-	virtual uint32 getBits(uint8 n) = 0;
-
-	/** Read a bit from the bit stream, without changing the stream's position. */
-	virtual uint32 peekBit() = 0;
-
-	/** Read a multi-bit value from the bit stream, without changing the stream's position. */
-	virtual uint32 peekBits(uint8 n) = 0;
-
-	/** Add a bit to the value x, making it an n+1-bit value. */
-	virtual void addBit(uint32 &x, uint32 n) = 0;
-
-protected:
-	BitStream() {
-	}
-};
-
 /**
  * A template implementing a bit stream for different data memory layouts.
  *
@@ -86,10 +42,10 @@ protected:
  * for valueBits, isLE and isMSB2LSB, reads 32bit little-endian values
  * from the data stream and hands out the bits in the order of LSB to MSB.
  */
-template<int valueBits, bool isLE, bool isMSB2LSB>
-class BitStreamImpl : public BitStream {
+template<class STREAM, int valueBits, bool isLE, bool isMSB2LSB>
+class BitStreamImpl {
 private:
-	SeekableReadStream *_stream;			///< The input stream.
+	STREAM *_stream;			///< The input stream.
 	DisposeAfterUse::Flag _disposeAfterUse; ///< Should we delete the stream on destruction?
 
 	uint32 _value;   ///< Current value.
@@ -133,7 +89,7 @@ private:
 
 public:
 	/** Create a bit stream using this input data stream and optionally delete it on destruction. */
-	BitStreamImpl(SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::NO) :
+	BitStreamImpl(STREAM *stream, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::NO) :
 		_stream(stream), _disposeAfterUse(disposeAfterUse), _value(0), _inValue(0) {
 
 		if ((valueBits != 8) && (valueBits != 16) && (valueBits != 32))
@@ -141,7 +97,7 @@ public:
 	}
 
 	/** Create a bit stream using this input data stream. */
-	BitStreamImpl(SeekableReadStream &stream) :
+	BitStreamImpl(STREAM &stream) :
 		_stream(&stream), _disposeAfterUse(DisposeAfterUse::NO), _value(0), _inValue(0) {
 
 		if ((valueBits != 8) && (valueBits != 16) && (valueBits != 32))
@@ -308,27 +264,27 @@ public:
 // typedefs for various memory layouts.
 
 /** 8-bit data, MSB to LSB. */
-typedef BitStreamImpl<8, false, true > BitStream8MSB;
+typedef BitStreamImpl<SeekableReadStream, 8, false, true > BitStream8MSB;
 /** 8-bit data, LSB to MSB. */
-typedef BitStreamImpl<8, false, false> BitStream8LSB;
+typedef BitStreamImpl<SeekableReadStream, 8, false, false> BitStream8LSB;
 
 /** 16-bit little-endian data, MSB to LSB. */
-typedef BitStreamImpl<16, true , true > BitStream16LEMSB;
+typedef BitStreamImpl<SeekableReadStream, 16, true , true > BitStream16LEMSB;
 /** 16-bit little-endian data, LSB to MSB. */
-typedef BitStreamImpl<16, true , false> BitStream16LELSB;
+typedef BitStreamImpl<SeekableReadStream, 16, true , false> BitStream16LELSB;
 /** 16-bit big-endian data, MSB to LSB. */
-typedef BitStreamImpl<16, false, true > BitStream16BEMSB;
+typedef BitStreamImpl<SeekableReadStream, 16, false, true > BitStream16BEMSB;
 /** 16-bit big-endian data, LSB to MSB. */
-typedef BitStreamImpl<16, false, false> BitStream16BELSB;
+typedef BitStreamImpl<SeekableReadStream, 16, false, false> BitStream16BELSB;
 
 /** 32-bit little-endian data, MSB to LSB. */
-typedef BitStreamImpl<32, true , true > BitStream32LEMSB;
+typedef BitStreamImpl<SeekableReadStream, 32, true , true > BitStream32LEMSB;
 /** 32-bit little-endian data, LSB to MSB. */
-typedef BitStreamImpl<32, true , false> BitStream32LELSB;
+typedef BitStreamImpl<SeekableReadStream, 32, true , false> BitStream32LELSB;
 /** 32-bit big-endian data, MSB to LSB. */
-typedef BitStreamImpl<32, false, true > BitStream32BEMSB;
+typedef BitStreamImpl<SeekableReadStream, 32, false, true > BitStream32BEMSB;
 /** 32-bit big-endian data, LSB to MSB. */
-typedef BitStreamImpl<32, false, false> BitStream32BELSB;
+typedef BitStreamImpl<SeekableReadStream, 32, false, false> BitStream32BELSB;
 
 } // End of namespace Common
 
diff --git a/common/huffman.cpp b/common/huffman.cpp
index afb4fa0..3268ddf 100644
--- a/common/huffman.cpp
+++ b/common/huffman.cpp
@@ -68,19 +68,4 @@ void Huffman::setSymbols(const uint32 *symbols) {
 		_symbols[i]->symbol = symbols ? *symbols++ : i;
 }
 
-uint32 Huffman::getSymbol(BitStream &bits) const {
-	uint32 code = 0;
-
-	for (uint32 i = 0; i < _codes.size(); i++) {
-		bits.addBit(code, i);
-
-		for (CodeList::const_iterator cCode = _codes[i].begin(); cCode != _codes[i].end(); ++cCode)
-			if (code == cCode->code)
-				return cCode->symbol;
-	}
-
-	error("Unknown Huffman code");
-	return 0;
-}
-
 } // End of namespace Common
diff --git a/common/huffman.h b/common/huffman.h
index 5ba7dec..f703d07 100644
--- a/common/huffman.h
+++ b/common/huffman.h
@@ -31,8 +31,6 @@
 
 namespace Common {
 
-class BitStream;
-
 /**
  * Huffman bitstream decoding
  *
@@ -56,7 +54,21 @@ public:
 	void setSymbols(const uint32 *symbols = 0);
 
 	/** Return the next symbol in the bitstream. */
-	uint32 getSymbol(BitStream &bits) const;
+	template<class BITSTREAM>
+	uint32 getSymbol(BITSTREAM &bits) const {
+		uint32 code = 0;
+
+		for (uint32 i = 0; i < _codes.size(); i++) {
+			bits.addBit(code, i);
+
+			for (CodeList::const_iterator cCode = _codes[i].begin(); cCode != _codes[i].end(); ++cCode)
+				if (code == cCode->code)
+					return cCode->symbol;
+		}
+
+		error("Unknown Huffman code");
+		return 0;
+	}
 
 private:
 	struct Symbol {
diff --git a/engines/groovie/stuffit.cpp b/engines/groovie/stuffit.cpp
index bbfcd3d..4543e37 100644
--- a/engines/groovie/stuffit.cpp
+++ b/engines/groovie/stuffit.cpp
@@ -71,7 +71,7 @@ private:
 
 	// Decompression Helpers
 	void update14(uint16 first, uint16 last, byte *code, uint16 *freq) const;
-	void readTree14(Common::BitStream *bits, SIT14Data *dat, uint16 codesize, uint16 *result) const;
+	void readTree14(Common::BitStream8LSB *bits, SIT14Data *dat, uint16 codesize, uint16 *result) const;
 };
 
 StuffItArchive::StuffItArchive() : Common::Archive() {
@@ -302,7 +302,7 @@ struct SIT14Data {
 	if (b->pos() & 7) \
 		b->skip(8 - (b->pos() & 7))
 
-void StuffItArchive::readTree14(Common::BitStream *bits, SIT14Data *dat, uint16 codesize, uint16 *result) const {
+void StuffItArchive::readTree14(Common::BitStream8LSB *bits, SIT14Data *dat, uint16 codesize, uint16 *result) const {
 	uint32 i, l, n;
 	uint32 k = bits->getBit();
 	uint32 j = bits->getBits(2) + 2;
@@ -429,7 +429,7 @@ Common::SeekableReadStream *StuffItArchive::decompress14(Common::SeekableReadStr
 	byte *dst = (byte *)malloc(uncompressedSize);
 	Common::MemoryWriteStream out(dst, uncompressedSize);
 
-	Common::BitStream *bits = new Common::BitStream8LSB(src);
+	Common::BitStream8LSB *bits = new Common::BitStream8LSB(src);
 
 	uint32 i, j, k, l, m, n;
 
diff --git a/engines/macventure/container.h b/engines/macventure/container.h
index ba1370d..bd8cd09 100644
--- a/engines/macventure/container.h
+++ b/engines/macventure/container.h
@@ -35,7 +35,6 @@
 
 #include "common/file.h"
 #include "common/fs.h"
-#include "common/bitstream.h"
 
 namespace MacVenture {
 
diff --git a/engines/macventure/image.cpp b/engines/macventure/image.cpp
index 7fccaa8..a899341 100644
--- a/engines/macventure/image.cpp
+++ b/engines/macventure/image.cpp
@@ -155,7 +155,7 @@ void ImageAsset::decodePPIC(ObjID id, Common::Array<byte> &data, uint &bitHeight
 	delete baseStream;
 }
 
-void ImageAsset::decodePPIC0(Common::BitStream &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
+void ImageAsset::decodePPIC0(Common::BitStream32BEMSB &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
 	uint words = bitWidth >> 4;
 	uint bytes = bitWidth & 0xF;
 	uint v = 0;
@@ -178,15 +178,15 @@ void ImageAsset::decodePPIC0(Common::BitStream &stream, Common::Array<byte> &dat
 
 }
 
-void ImageAsset::decodePPIC1(Common::BitStream &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
+void ImageAsset::decodePPIC1(Common::BitStream32BEMSB &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
 	decodeHuffGraphic(PPIC1Huff, stream, data, bitHeight, bitWidth, rowBytes);
 }
 
-void ImageAsset::decodePPIC2(Common::BitStream &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
+void ImageAsset::decodePPIC2(Common::BitStream32BEMSB &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
 	decodeHuffGraphic(PPIC2Huff, stream, data, bitHeight, bitWidth, rowBytes);
 }
 
-void ImageAsset::decodePPIC3(Common::BitStream &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
+void ImageAsset::decodePPIC3(Common::BitStream32BEMSB &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
 	// We need to load the huffman from the PPIC itself
 	PPICHuff huff;
 	uint16 v, bits;
@@ -238,7 +238,7 @@ void ImageAsset::decodePPIC3(Common::BitStream &stream, Common::Array<byte> &dat
 	decodeHuffGraphic(huff, stream, data, bitHeight, bitWidth, rowBytes);
 }
 
-void ImageAsset::decodeHuffGraphic(const PPICHuff &huff, Common::BitStream &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
+void ImageAsset::decodeHuffGraphic(const PPICHuff &huff, Common::BitStream32BEMSB &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes) {
 	byte flags = 0;
 	_walkRepeat = 0;
 	_walkLast = 0;
@@ -333,7 +333,7 @@ void ImageAsset::decodeHuffGraphic(const PPICHuff &huff, Common::BitStream &stre
 	}
 }
 
-byte ImageAsset::walkHuff(const PPICHuff &huff, Common::BitStream &stream) {
+byte ImageAsset::walkHuff(const PPICHuff &huff, Common::BitStream32BEMSB &stream) {
 	if (_walkRepeat) {
 		_walkRepeat--;
 		_walkLast = ((_walkLast << 8) & 0xFF00) | (_walkLast >> 8);
diff --git a/engines/macventure/image.h b/engines/macventure/image.h
index 87c5cec..9a3a2a1 100644
--- a/engines/macventure/image.h
+++ b/engines/macventure/image.h
@@ -33,6 +33,7 @@
 
 #include "macventure/macventure.h"
 #include "macventure/container.h"
+#include "common/bitstream.h"
 
 namespace MacVenture {
 
@@ -76,13 +77,13 @@ public:
 private:
 	void decodePPIC(ObjID id, Common::Array<byte> &data, uint &bitHeight, uint &bitWidth, uint &rowBytes);
 
-	void decodePPIC0(Common::BitStream &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
-	void decodePPIC1(Common::BitStream &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
-	void decodePPIC2(Common::BitStream &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
-	void decodePPIC3(Common::BitStream &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
+	void decodePPIC0(Common::BitStream32BEMSB &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
+	void decodePPIC1(Common::BitStream32BEMSB &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
+	void decodePPIC2(Common::BitStream32BEMSB &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
+	void decodePPIC3(Common::BitStream32BEMSB &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
 
-	void decodeHuffGraphic(const PPICHuff &huff, Common::BitStream &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
-	byte walkHuff(const PPICHuff &huff, Common::BitStream &stream);
+	void decodeHuffGraphic(const PPICHuff &huff, Common::BitStream32BEMSB &stream, Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
+	byte walkHuff(const PPICHuff &huff, Common::BitStream32BEMSB &stream);
 
 	void blitDirect(Graphics::ManagedSurface *target, int ox, int oy, const Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
 	void blitBIC(Graphics::ManagedSurface *target, int ox, int oy, const Common::Array<byte> &data, uint bitHeight, uint bitWidth, uint rowBytes);
diff --git a/image/codecs/svq1.cpp b/image/codecs/svq1.cpp
index 765d512..2d86070 100644
--- a/image/codecs/svq1.cpp
+++ b/image/codecs/svq1.cpp
@@ -282,7 +282,7 @@ const Graphics::Surface *SVQ1Decoder::decodeFrame(Common::SeekableReadStream &st
 	return _surface;
 }
 
-bool SVQ1Decoder::svq1DecodeBlockIntra(Common::BitStream *s, byte *pixels, int pitch) {
+bool SVQ1Decoder::svq1DecodeBlockIntra(Common::BitStream32BEMSB *s, byte *pixels, int pitch) {
 	// initialize list for breadth first processing of vectors
 	byte *list[63];
 	list[0] = pixels;
@@ -383,7 +383,7 @@ bool SVQ1Decoder::svq1DecodeBlockIntra(Common::BitStream *s, byte *pixels, int p
 	return true;
 }
 
-bool SVQ1Decoder::svq1DecodeBlockNonIntra(Common::BitStream *s, byte *pixels, int pitch) {
+bool SVQ1Decoder::svq1DecodeBlockNonIntra(Common::BitStream32BEMSB *s, byte *pixels, int pitch) {
 	// initialize list for breadth first processing of vectors
 	byte *list[63];
 	list[0] = pixels;
@@ -497,7 +497,7 @@ static inline int midPred(int a, int b, int c) {
 	return b;
 }
 
-bool SVQ1Decoder::svq1DecodeMotionVector(Common::BitStream *s, Common::Point *mv, Common::Point **pmv) {
+bool SVQ1Decoder::svq1DecodeMotionVector(Common::BitStream32BEMSB *s, Common::Point *mv, Common::Point **pmv) {
 	for (int i = 0; i < 2; i++) {
 		// get motion code
 		int diff = _motionComponent->getSymbol(*s);
@@ -611,7 +611,7 @@ void SVQ1Decoder::putPixels16XY2C(byte *block, const byte *pixels, int lineSize,
 	putPixels8XY2C(block + 8, pixels + 8, lineSize, h);
 }
 
-bool SVQ1Decoder::svq1MotionInterBlock(Common::BitStream *ss, byte *current, byte *previous, int pitch,
+bool SVQ1Decoder::svq1MotionInterBlock(Common::BitStream32BEMSB *ss, byte *current, byte *previous, int pitch,
 		Common::Point *motion, int x, int y) {
 
 	// predict and decode motion vector
@@ -662,7 +662,7 @@ bool SVQ1Decoder::svq1MotionInterBlock(Common::BitStream *ss, byte *current, byt
 	return true;
 }
 
-bool SVQ1Decoder::svq1MotionInter4vBlock(Common::BitStream *ss, byte *current, byte *previous, int pitch,
+bool SVQ1Decoder::svq1MotionInter4vBlock(Common::BitStream32BEMSB *ss, byte *current, byte *previous, int pitch,
 		Common::Point *motion, int x, int y) {
 	// predict and decode motion vector (0)
 	Common::Point *pmv[4];
@@ -749,7 +749,7 @@ bool SVQ1Decoder::svq1MotionInter4vBlock(Common::BitStream *ss, byte *current, b
 	return true;
 }
 
-bool SVQ1Decoder::svq1DecodeDeltaBlock(Common::BitStream *ss, byte *current, byte *previous, int pitch,
+bool SVQ1Decoder::svq1DecodeDeltaBlock(Common::BitStream32BEMSB *ss, byte *current, byte *previous, int pitch,
 		Common::Point *motion, int x, int y) {
 	// get block type
 	uint32 blockType = _blockType->getSymbol(*ss);
diff --git a/image/codecs/svq1.h b/image/codecs/svq1.h
index 236b810..148501d 100644
--- a/image/codecs/svq1.h
+++ b/image/codecs/svq1.h
@@ -23,10 +23,10 @@
 #ifndef IMAGE_CODECS_SVQ1_H
 #define IMAGE_CODECS_SVQ1_H
 
+#include "common/bitstream.h"
 #include "image/codecs/codec.h"
 
 namespace Common {
-class BitStream;
 class Huffman;
 struct Point;
 }
@@ -60,15 +60,15 @@ private:
 	Common::Huffman *_interMean;
 	Common::Huffman *_motionComponent;
 
-	bool svq1DecodeBlockIntra(Common::BitStream *s, byte *pixels, int pitch);
-	bool svq1DecodeBlockNonIntra(Common::BitStream *s, byte *pixels, int pitch);
-	bool svq1DecodeMotionVector(Common::BitStream *s, Common::Point *mv, Common::Point **pmv);
+	bool svq1DecodeBlockIntra(Common::BitStream32BEMSB *s, byte *pixels, int pitch);
+	bool svq1DecodeBlockNonIntra(Common::BitStream32BEMSB *s, byte *pixels, int pitch);
+	bool svq1DecodeMotionVector(Common::BitStream32BEMSB *s, Common::Point *mv, Common::Point **pmv);
 	void svq1SkipBlock(byte *current, byte *previous, int pitch, int x, int y);
-	bool svq1MotionInterBlock(Common::BitStream *ss, byte *current, byte *previous, int pitch,
+	bool svq1MotionInterBlock(Common::BitStream32BEMSB *ss, byte *current, byte *previous, int pitch,
 			Common::Point *motion, int x, int y);
-	bool svq1MotionInter4vBlock(Common::BitStream *ss, byte *current, byte *previous, int pitch,
+	bool svq1MotionInter4vBlock(Common::BitStream32BEMSB *ss, byte *current, byte *previous, int pitch,
 			Common::Point *motion, int x, int y);
-	bool svq1DecodeDeltaBlock(Common::BitStream *ss, byte *current, byte *previous, int pitch,
+	bool svq1DecodeDeltaBlock(Common::BitStream32BEMSB *ss, byte *current, byte *previous, int pitch,
 			Common::Point *motion, int x, int y);
 
 	void putPixels8C(byte *block, const byte *pixels, int lineSize, int h);
diff --git a/video/bink_decoder.h b/video/bink_decoder.h
index 6f18dd5..8a67913 100644
--- a/video/bink_decoder.h
+++ b/video/bink_decoder.h
@@ -32,6 +32,7 @@
 #define VIDEO_BINK_DECODER_H
 
 #include "common/array.h"
+#include "common/bitstream.h"
 #include "common/rational.h"
 
 #include "video/video_decoder.h"
@@ -45,7 +46,6 @@ class QueuingAudioStream;
 
 namespace Common {
 class SeekableReadStream;
-class BitStream;
 class Huffman;
 
 class RDFT;
@@ -100,7 +100,7 @@ private:
 
 		uint32 sampleCount;
 
-		Common::BitStream *bits;
+		Common::BitStream32LELSB *bits;
 
 		bool first;
 
@@ -133,7 +133,7 @@ private:
 		uint32 offset;
 		uint32 size;
 
-		Common::BitStream *bits;
+		Common::BitStream32LELSB *bits;
 
 		VideoFrame();
 		~VideoFrame();
diff --git a/video/psx_decoder.cpp b/video/psx_decoder.cpp
index 426f5eb..973803e 100644
--- a/video/psx_decoder.cpp
+++ b/video/psx_decoder.cpp
@@ -499,7 +499,7 @@ void PSXStreamDecoder::PSXVideoTrack::decodeFrame(Common::SeekableReadStream *fr
 	_nextFrameStartTime = _nextFrameStartTime.addFrames(sectorCount);
 }
 
-void PSXStreamDecoder::PSXVideoTrack::decodeMacroBlock(Common::BitStream *bits, int mbX, int mbY, uint16 scale, uint16 version) {
+void PSXStreamDecoder::PSXVideoTrack::decodeMacroBlock(Common::BitStream16LEMSB *bits, int mbX, int mbY, uint16 scale, uint16 version) {
 	int pitchY = _macroBlocksW * 16;
 	int pitchC = _macroBlocksW * 8;
 
@@ -546,7 +546,7 @@ void PSXStreamDecoder::PSXVideoTrack::dequantizeBlock(int *coefficients, float *
 	}
 }
 
-int PSXStreamDecoder::PSXVideoTrack::readDC(Common::BitStream *bits, uint16 version, PlaneType plane) {
+int PSXStreamDecoder::PSXVideoTrack::readDC(Common::BitStream16LEMSB *bits, uint16 version, PlaneType plane) {
 	// Version 2 just has its coefficient as 10-bits
 	if (version == 2)
 		return readSignedCoefficient(bits);
@@ -576,7 +576,7 @@ int PSXStreamDecoder::PSXVideoTrack::readDC(Common::BitStream *bits, uint16 vers
 	if (count > 63) \
 		error("PSXStreamDecoder::readAC(): Too many coefficients")
 
-void PSXStreamDecoder::PSXVideoTrack::readAC(Common::BitStream *bits, int *block) {
+void PSXStreamDecoder::PSXVideoTrack::readAC(Common::BitStream16LEMSB *bits, int *block) {
 	// Clear the block first
 	for (int i = 0; i < 63; i++)
 		block[i] = 0;
@@ -611,7 +611,7 @@ void PSXStreamDecoder::PSXVideoTrack::readAC(Common::BitStream *bits, int *block
 	}
 }
 
-int PSXStreamDecoder::PSXVideoTrack::readSignedCoefficient(Common::BitStream *bits) {
+int PSXStreamDecoder::PSXVideoTrack::readSignedCoefficient(Common::BitStream16LEMSB *bits) {
 	uint val = bits->getBits(10);
 
 	// extend the sign
@@ -672,7 +672,7 @@ void PSXStreamDecoder::PSXVideoTrack::idct(float *dequantData, float *result) {
 	}
 }
 
-void PSXStreamDecoder::PSXVideoTrack::decodeBlock(Common::BitStream *bits, byte *block, int pitch, uint16 scale, uint16 version, PlaneType plane) {
+void PSXStreamDecoder::PSXVideoTrack::decodeBlock(Common::BitStream16LEMSB *bits, byte *block, int pitch, uint16 scale, uint16 version, PlaneType plane) {
 	// Version 2 just has signed 10 bits for DC
 	// Version 3 has them huffman coded
 	int coefficients[8 * 8];
diff --git a/video/psx_decoder.h b/video/psx_decoder.h
index b19bd72..fa36af1 100644
--- a/video/psx_decoder.h
+++ b/video/psx_decoder.h
@@ -23,6 +23,7 @@
 #ifndef VIDEO_PSX_DECODER_H
 #define VIDEO_PSX_DECODER_H
 
+#include "common/bitstream.h"
 #include "common/endian.h"
 #include "common/rational.h"
 #include "common/rect.h"
@@ -35,7 +36,6 @@ class QueuingAudioStream;
 }
 
 namespace Common {
-class BitStream;
 class Huffman;
 class SeekableReadStream;
 }
@@ -108,19 +108,19 @@ private:
 
 		uint16 _macroBlocksW, _macroBlocksH;
 		byte *_yBuffer, *_cbBuffer, *_crBuffer;
-		void decodeMacroBlock(Common::BitStream *bits, int mbX, int mbY, uint16 scale, uint16 version);
-		void decodeBlock(Common::BitStream *bits, byte *block, int pitch, uint16 scale, uint16 version, PlaneType plane);
+		void decodeMacroBlock(Common::BitStream16LEMSB *bits, int mbX, int mbY, uint16 scale, uint16 version);
+		void decodeBlock(Common::BitStream16LEMSB *bits, byte *block, int pitch, uint16 scale, uint16 version, PlaneType plane);
 
-		void readAC(Common::BitStream *bits, int *block);
+		void readAC(Common::BitStream16LEMSB *bits, int *block);
 		Common::Huffman *_acHuffman;
 
-		int readDC(Common::BitStream *bits, uint16 version, PlaneType plane);
+		int readDC(Common::BitStream16LEMSB *bits, uint16 version, PlaneType plane);
 		Common::Huffman *_dcHuffmanLuma, *_dcHuffmanChroma;
 		int _lastDC[3];
 
 		void dequantizeBlock(int *coefficients, float *block, uint16 scale);
 		void idct(float *dequantData, float *result);
-		int readSignedCoefficient(Common::BitStream *bits);
+		int readSignedCoefficient(Common::BitStream16LEMSB *bits);
 	};
 
 	class PSXAudioTrack : public AudioTrack {
diff --git a/video/smk_decoder.cpp b/video/smk_decoder.cpp
index d913707..d8057ac 100644
--- a/video/smk_decoder.cpp
+++ b/video/smk_decoder.cpp
@@ -54,9 +54,9 @@ enum SmkBlockTypes {
 
 class SmallHuffmanTree {
 public:
-	SmallHuffmanTree(Common::BitStream &bs);
+	SmallHuffmanTree(Common::BitStream8LSB &bs);
 
-	uint16 getCode(Common::BitStream &bs);
+	uint16 getCode(Common::BitStream8LSB &bs);
 private:
 	enum {
 		SMK_NODE = 0x8000
@@ -70,10 +70,10 @@ private:
 	uint16 _prefixtree[256];
 	byte _prefixlength[256];
 
-	Common::BitStream &_bs;
+	Common::BitStream8LSB &_bs;
 };
 
-SmallHuffmanTree::SmallHuffmanTree(Common::BitStream &bs)
+SmallHuffmanTree::SmallHuffmanTree(Common::BitStream8LSB &bs)
 	: _treeSize(0), _bs(bs) {
 	uint32 bit = _bs.getBit();
 	assert(bit);
@@ -118,7 +118,7 @@ uint16 SmallHuffmanTree::decodeTree(uint32 prefix, int length) {
 	return r1+r2+1;
 }
 
-uint16 SmallHuffmanTree::getCode(Common::BitStream &bs) {
+uint16 SmallHuffmanTree::getCode(Common::BitStream8LSB &bs) {
 	byte peek = bs.peekBits(MIN<uint32>(bs.size() - bs.pos(), 8));
 	uint16 *p = &_tree[_prefixtree[peek]];
 	bs.skip(_prefixlength[peek]);
@@ -139,11 +139,11 @@ uint16 SmallHuffmanTree::getCode(Common::BitStream &bs) {
 
 class BigHuffmanTree {
 public:
-	BigHuffmanTree(Common::BitStream &bs, int allocSize);
+	BigHuffmanTree(Common::BitStream8LSB &bs, int allocSize);
 	~BigHuffmanTree();
 
 	void reset();
-	uint32 getCode(Common::BitStream &bs);
+	uint32 getCode(Common::BitStream8LSB &bs);
 private:
 	enum {
 		SMK_NODE = 0x80000000
@@ -159,13 +159,13 @@ private:
 	byte _prefixlength[256];
 
 	/* Used during construction */
-	Common::BitStream &_bs;
+	Common::BitStream8LSB &_bs;
 	uint32 _markers[3];
 	SmallHuffmanTree *_loBytes;
 	SmallHuffmanTree *_hiBytes;
 };
 
-BigHuffmanTree::BigHuffmanTree(Common::BitStream &bs, int allocSize)
+BigHuffmanTree::BigHuffmanTree(Common::BitStream8LSB &bs, int allocSize)
 	: _bs(bs) {
 	uint32 bit = _bs.getBit();
 	if (!bit) {
@@ -256,7 +256,7 @@ uint32 BigHuffmanTree::decodeTree(uint32 prefix, int length) {
 	return r1+r2+1;
 }
 
-uint32 BigHuffmanTree::getCode(Common::BitStream &bs) {
+uint32 BigHuffmanTree::getCode(Common::BitStream8LSB &bs) {
 	byte peek = bs.peekBits(MIN<uint32>(bs.size() - bs.pos(), 8));
 	uint32 *p = &_tree[_prefixtree[peek]];
 	bs.skip(_prefixlength[peek]);
@@ -553,14 +553,14 @@ Graphics::PixelFormat SmackerDecoder::SmackerVideoTrack::getPixelFormat() const
 	return _surface->format;
 }
 
-void SmackerDecoder::SmackerVideoTrack::readTrees(Common::BitStream &bs, uint32 mMapSize, uint32 mClrSize, uint32 fullSize, uint32 typeSize) {
+void SmackerDecoder::SmackerVideoTrack::readTrees(Common::BitStream8LSB &bs, uint32 mMapSize, uint32 mClrSize, uint32 fullSize, uint32 typeSize) {
 	_MMapTree = new BigHuffmanTree(bs, mMapSize);
 	_MClrTree = new BigHuffmanTree(bs, mClrSize);
 	_FullTree = new BigHuffmanTree(bs, fullSize);
 	_TypeTree = new BigHuffmanTree(bs, typeSize);
 }
 
-void SmackerDecoder::SmackerVideoTrack::decodeFrame(Common::BitStream &bs) {
+void SmackerDecoder::SmackerVideoTrack::decodeFrame(Common::BitStream8LSB &bs) {
 	_MMapTree->reset();
 	_MClrTree->reset();
 	_FullTree->reset();
diff --git a/video/smk_decoder.h b/video/smk_decoder.h
index bc04a68..e0ea600 100644
--- a/video/smk_decoder.h
+++ b/video/smk_decoder.h
@@ -23,6 +23,7 @@
 #ifndef VIDEO_SMK_PLAYER_H
 #define VIDEO_SMK_PLAYER_H
 
+#include "common/bitstream.h"
 #include "common/rational.h"
 #include "graphics/pixelformat.h"
 #include "graphics/surface.h"
@@ -34,7 +35,6 @@ class QueuingAudioStream;
 }
 
 namespace Common {
-class BitStream;
 class SeekableReadStream;
 }
 
@@ -91,9 +91,9 @@ protected:
 		const byte *getPalette() const { _dirtyPalette = false; return _palette; }
 		bool hasDirtyPalette() const { return _dirtyPalette; }
 
-		void readTrees(Common::BitStream &bs, uint32 mMapSize, uint32 mClrSize, uint32 fullSize, uint32 typeSize);
+		void readTrees(Common::BitStream8LSB &bs, uint32 mMapSize, uint32 mClrSize, uint32 fullSize, uint32 typeSize);
 		void increaseCurFrame() { _curFrame++; }
-		void decodeFrame(Common::BitStream &bs);
+		void decodeFrame(Common::BitStream8LSB &bs);
 		void unpackPalette(Common::SeekableReadStream *stream);
 
 	protected:


Commit: 60fae8847eeef85b6cda99cc1d690c59e292ecf7
    https://github.com/scummvm/scummvm/commit/60fae8847eeef85b6cda99cc1d690c59e292ecf7
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2017-08-24T19:46:59+02:00

Commit Message:
IMAGE: Inline indeo getVLC2

Changed paths:
  R image/codecs/indeo/get_bits.cpp
    image/codecs/indeo/get_bits.h
    image/codecs/indeo/indeo.cpp
    image/codecs/indeo4.cpp
    image/codecs/indeo5.cpp
    image/module.mk


diff --git a/image/codecs/indeo/get_bits.cpp b/image/codecs/indeo/get_bits.cpp
deleted file mode 100644
index f808a96..0000000
--- a/image/codecs/indeo/get_bits.cpp
+++ /dev/null
@@ -1,60 +0,0 @@
-/* ScummVM - Graphic Adventure Engine
- *
- * ScummVM is the legal property of its developers, whose names
- * are too numerous to list here. Please refer to the COPYRIGHT
- * file distributed with this source distribution.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- */
-
-#include "image/codecs/indeo/get_bits.h"
-
-namespace Image {
-namespace Indeo {
-
-int GetBits::getVLC2(int16 (*table)[2], int bits, int maxDepth) {
-	int code;
-	int n, nbBits;
-	unsigned int index;
-
-	index = peekBits(bits);
-	code  = table[index][0];
-	n     = table[index][1];
-
-	if (maxDepth > 1 && n < 0) {
-		skip(bits);
-		nbBits = -n;
-
-		index = peekBits(nbBits) + code;
-		code = table[index][0];
-		n = table[index][1];
-
-		if (maxDepth > 2 && n < 0) {
-			skip(nbBits);
-			nbBits = -n;
-
-			index = peekBits(nbBits) + code;
-			code = table[index][0];
-			n = table[index][1];
-		}
-	}
-
-	skip(n);
-	return code;
-}
-
-} // End of namespace Indeo
-} // End of namespace Image
diff --git a/image/codecs/indeo/get_bits.h b/image/codecs/indeo/get_bits.h
index 359d8fc..c2d593b 100644
--- a/image/codecs/indeo/get_bits.h
+++ b/image/codecs/indeo/get_bits.h
@@ -54,7 +54,37 @@ public:
 	 *                  read the longest vlc code
 	 *                  = (max_vlc_length + bits - 1) / bits
 	 */
-	int getVLC2(int16 (*table)[2], int bits, int maxDepth);
+	template <int maxDepth>
+	int getVLC2(int16 (*table)[2], int bits) {
+		int code;
+		int n, nbBits;
+		unsigned int index;
+
+		index = peekBits(bits);
+		code  = table[index][0];
+		n     = table[index][1];
+
+		if (maxDepth > 1 && n < 0) {
+			skip(bits);
+			nbBits = -n;
+
+			index = peekBits(nbBits) + code;
+			code = table[index][0];
+			n = table[index][1];
+
+			if (maxDepth > 2 && n < 0) {
+				skip(nbBits);
+				nbBits = -n;
+
+				index = peekBits(nbBits) + code;
+				code = table[index][0];
+				n = table[index][1];
+			}
+		}
+
+		skip(n);
+		return code;
+	}
 };
 
 } // End of namespace Indeo
diff --git a/image/codecs/indeo/indeo.cpp b/image/codecs/indeo/indeo.cpp
index 769790b..f420069 100644
--- a/image/codecs/indeo/indeo.cpp
+++ b/image/codecs/indeo/indeo.cpp
@@ -1258,16 +1258,15 @@ int IndeoDecoderBase::decodeCodedBlocks(GetBits *gb, IVIBandDesc *band,
 	// zero column flags
 	memset(colFlags, 0, sizeof(colFlags));
 	while (scanPos <= numCoeffs) {
-		sym = gb->getVLC2(band->_blkVlc._tab->_table,
-			IVI_VLC_BITS, 1);
+		sym = gb->getVLC2<1>(band->_blkVlc._tab->_table, IVI_VLC_BITS);
 		if (sym == rvmap->_eobSym)
 			break; // End of block
 
 		// Escape - run/val explicitly coded using 3 vlc codes
 		if (sym == rvmap->_escSym) {
-			run = gb->getVLC2(band->_blkVlc._tab->_table, IVI_VLC_BITS, 1) + 1;
-			lo = gb->getVLC2(band->_blkVlc._tab->_table, IVI_VLC_BITS, 1);
-			hi = gb->getVLC2(band->_blkVlc._tab->_table, IVI_VLC_BITS, 1);
+			run = gb->getVLC2<1>(band->_blkVlc._tab->_table, IVI_VLC_BITS) + 1;
+			lo = gb->getVLC2<1>(band->_blkVlc._tab->_table, IVI_VLC_BITS);
+			hi = gb->getVLC2<1>(band->_blkVlc._tab->_table, IVI_VLC_BITS);
 			// merge them and convert into signed val
 			val = IVI_TOSIGNED((hi << 6) | lo);
 		} else {
diff --git a/image/codecs/indeo4.cpp b/image/codecs/indeo4.cpp
index b6ac088..4658068 100644
--- a/image/codecs/indeo4.cpp
+++ b/image/codecs/indeo4.cpp
@@ -484,8 +484,8 @@ int Indeo4Decoder::decodeMbInfo(IVIBandDesc *band, IVITile *tile) {
 
 				mb->_qDelta = 0;
 				if (!band->_plane && !band->_bandNum && _ctx._inQ) {
-					mb->_qDelta = _ctx._gb->getVLC2(_ctx._mbVlc._tab->_table,
-						IVI_VLC_BITS, 1);
+					mb->_qDelta = _ctx._gb->getVLC2<1>(_ctx._mbVlc._tab->_table,
+						IVI_VLC_BITS);
 					mb->_qDelta = IVI_TOSIGNED(mb->_qDelta);
 				}
 
@@ -522,8 +522,8 @@ int Indeo4Decoder::decodeMbInfo(IVIBandDesc *band, IVITile *tile) {
 					if (refMb) mb->_qDelta = refMb->_qDelta;
 				} else if (mb->_cbp || (!band->_plane && !band->_bandNum &&
 					_ctx._inQ)) {
-					mb->_qDelta = _ctx._gb->getVLC2(_ctx._mbVlc._tab->_table,
-						IVI_VLC_BITS, 1);
+					mb->_qDelta = _ctx._gb->getVLC2<1>(_ctx._mbVlc._tab->_table,
+						IVI_VLC_BITS);
 					mb->_qDelta = IVI_TOSIGNED(mb->_qDelta);
 				}
 
@@ -543,22 +543,22 @@ int Indeo4Decoder::decodeMbInfo(IVIBandDesc *band, IVITile *tile) {
 						}
 					} else {
 						// decode motion vector deltas
-						mvDelta = _ctx._gb->getVLC2(_ctx._mbVlc._tab->_table,
-							IVI_VLC_BITS, 1);
+						mvDelta = _ctx._gb->getVLC2<1>(_ctx._mbVlc._tab->_table,
+							IVI_VLC_BITS);
 						mvY += IVI_TOSIGNED(mvDelta);
-						mvDelta = _ctx._gb->getVLC2(_ctx._mbVlc._tab->_table,
-							IVI_VLC_BITS, 1);
+						mvDelta = _ctx._gb->getVLC2<1>(_ctx._mbVlc._tab->_table,
+							IVI_VLC_BITS);
 						mvX += IVI_TOSIGNED(mvDelta);
 						mb->_mvX = mvX;
 						mb->_mvY = mvY;
 						if (mb->_type == 3) {
-							mvDelta = _ctx._gb->getVLC2(
+							mvDelta = _ctx._gb->getVLC2<1>(
 								_ctx._mbVlc._tab->_table,
-								IVI_VLC_BITS, 1);
+								IVI_VLC_BITS);
 							mvY += IVI_TOSIGNED(mvDelta);
-							mvDelta = _ctx._gb->getVLC2(
+							mvDelta = _ctx._gb->getVLC2<1>(
 								_ctx._mbVlc._tab->_table,
-								IVI_VLC_BITS, 1);
+								IVI_VLC_BITS);
 							mvX += IVI_TOSIGNED(mvDelta);
 							mb->_bMvX = -mvX;
 							mb->_bMvY = -mvY;
diff --git a/image/codecs/indeo5.cpp b/image/codecs/indeo5.cpp
index aca49c3..858c634 100644
--- a/image/codecs/indeo5.cpp
+++ b/image/codecs/indeo5.cpp
@@ -299,7 +299,7 @@ int Indeo5Decoder::decodeMbInfo(IVIBandDesc *band, IVITile *tile) {
 
 				mb->_qDelta = 0;
 				if (!band->_plane && !band->_bandNum && (_ctx._frameFlags & 8)) {
-					mb->_qDelta = _ctx._gb->getVLC2(_ctx._mbVlc._tab->_table, IVI_VLC_BITS, 1);
+					mb->_qDelta = _ctx._gb->getVLC2<1>(_ctx._mbVlc._tab->_table, IVI_VLC_BITS);
 					mb->_qDelta = IVI_TOSIGNED(mb->_qDelta);
 				}
 
@@ -332,7 +332,7 @@ int Indeo5Decoder::decodeMbInfo(IVIBandDesc *band, IVITile *tile) {
 						if (refMb) mb->_qDelta = refMb->_qDelta;
 					} else if (mb->_cbp || (!band->_plane && !band->_bandNum &&
 						(_ctx._frameFlags & 8))) {
-						mb->_qDelta = _ctx._gb->getVLC2(_ctx._mbVlc._tab->_table, IVI_VLC_BITS, 1);
+						mb->_qDelta = _ctx._gb->getVLC2<1>(_ctx._mbVlc._tab->_table, IVI_VLC_BITS);
 						mb->_qDelta = IVI_TOSIGNED(mb->_qDelta);
 					}
 				}
@@ -351,9 +351,9 @@ int Indeo5Decoder::decodeMbInfo(IVIBandDesc *band, IVITile *tile) {
 						}
 					} else {
 						// decode motion vector deltas
-						mvDelta = _ctx._gb->getVLC2(_ctx._mbVlc._tab->_table, IVI_VLC_BITS, 1);
+						mvDelta = _ctx._gb->getVLC2<1>(_ctx._mbVlc._tab->_table, IVI_VLC_BITS);
 						mvY += IVI_TOSIGNED(mvDelta);
-						mvDelta = _ctx._gb->getVLC2(_ctx._mbVlc._tab->_table, IVI_VLC_BITS, 1);
+						mvDelta = _ctx._gb->getVLC2<1>(_ctx._mbVlc._tab->_table, IVI_VLC_BITS);
 						mvX += IVI_TOSIGNED(mvDelta);
 						mb->_mvX = mvX;
 						mb->_mvY = mvY;
diff --git a/image/module.mk b/image/module.mk
index 2ede7c3..be049eb 100644
--- a/image/module.mk
+++ b/image/module.mk
@@ -24,7 +24,6 @@ MODULE_OBJS := \
 	codecs/smc.o \
 	codecs/svq1.o \
 	codecs/truemotion1.o \
-	codecs/indeo/get_bits.o \
 	codecs/indeo/indeo.o \
 	codecs/indeo/indeo_dsp.o \
 	codecs/indeo/mem.o \


Commit: ac95030ef65de5e5ae6e313fa28ced6b1d2eaf14
    https://github.com/scummvm/scummvm/commit/ac95030ef65de5e5ae6e313fa28ced6b1d2eaf14
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2017-08-24T19:46:59+02:00

Commit Message:
COMMON: Cache BitStream size

Changed paths:
    common/bitstream.h


diff --git a/common/bitstream.h b/common/bitstream.h
index ae5677d..6b9ab97 100644
--- a/common/bitstream.h
+++ b/common/bitstream.h
@@ -50,6 +50,7 @@ private:
 
 	uint32 _value;   ///< Current value.
 	uint8  _inValue; ///< Position within the current value.
+	uint32 _size;
 
 	/** Read a data value. */
 	inline uint32 readData() {
@@ -75,7 +76,7 @@ private:
 
 	/** Read the next data value. */
 	inline void readValue() {
-		if ((size() - pos()) < valueBits)
+		if ((_size - pos()) < valueBits)
 			error("BitStreamImpl::readValue(): End of bit stream reached");
 
 		_value = readData();
@@ -94,6 +95,8 @@ public:
 
 		if ((valueBits != 8) && (valueBits != 16) && (valueBits != 32))
 			error("BitStreamImpl: Invalid memory layout %d, %d, %d", valueBits, isLE, isMSB2LSB);
+
+		_size = (_stream->size() & ~((uint32) ((valueBits >> 3) - 1))) * 8;
 	}
 
 	/** Create a bit stream using this input data stream. */
@@ -102,6 +105,8 @@ public:
 
 		if ((valueBits != 8) && (valueBits != 16) && (valueBits != 32))
 			error("BitStreamImpl: Invalid memory layout %d, %d, %d", valueBits, isLE, isMSB2LSB);
+
+		_size = (_stream->size() & ~((uint32) ((valueBits >> 3) - 1))) * 8;
 	}
 
 	~BitStreamImpl() {
@@ -253,11 +258,11 @@ public:
 
 	/** Return the stream size in bits. */
 	uint32 size() const {
-		return (_stream->size() & ~((uint32) ((valueBits >> 3) - 1))) * 8;
+		return _size;
 	}
 
 	bool eos() const {
-		return _stream->eos() || (pos() >= size());
+		return _stream->eos() || (pos() >= _size);
 	}
 };
 


Commit: e7428bca2e3e53a5d21355675a2849bee1fbf9a1
    https://github.com/scummvm/scummvm/commit/e7428bca2e3e53a5d21355675a2849bee1fbf9a1
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2017-08-24T19:46:59+02:00

Commit Message:
COMMON: Keep track of bitstream position

Changed paths:
    common/bitstream.h


diff --git a/common/bitstream.h b/common/bitstream.h
index 6b9ab97..c5226d5 100644
--- a/common/bitstream.h
+++ b/common/bitstream.h
@@ -50,7 +50,8 @@ private:
 
 	uint32 _value;   ///< Current value.
 	uint8  _inValue; ///< Position within the current value.
-	uint32 _size;
+	uint32 _size;    ///< Total bitstream size (in bits)
+	uint32 _pos;     ///< Current bitstream position (in bits)
 
 	/** Read a data value. */
 	inline uint32 readData() {
@@ -76,7 +77,7 @@ private:
 
 	/** Read the next data value. */
 	inline void readValue() {
-		if ((_size - pos()) < valueBits)
+		if (_size - _pos < valueBits)
 			error("BitStreamImpl::readValue(): End of bit stream reached");
 
 		_value = readData();
@@ -91,7 +92,7 @@ private:
 public:
 	/** Create a bit stream using this input data stream and optionally delete it on destruction. */
 	BitStreamImpl(STREAM *stream, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::NO) :
-		_stream(stream), _disposeAfterUse(disposeAfterUse), _value(0), _inValue(0) {
+		_stream(stream), _disposeAfterUse(disposeAfterUse), _value(0), _inValue(0), _pos(0) {
 
 		if ((valueBits != 8) && (valueBits != 16) && (valueBits != 32))
 			error("BitStreamImpl: Invalid memory layout %d, %d, %d", valueBits, isLE, isMSB2LSB);
@@ -101,7 +102,7 @@ public:
 
 	/** Create a bit stream using this input data stream. */
 	BitStreamImpl(STREAM &stream) :
-		_stream(&stream), _disposeAfterUse(DisposeAfterUse::NO), _value(0), _inValue(0) {
+		_stream(&stream), _disposeAfterUse(DisposeAfterUse::NO), _value(0), _inValue(0), _pos(0) {
 
 		if ((valueBits != 8) && (valueBits != 16) && (valueBits != 32))
 			error("BitStreamImpl: Invalid memory layout %d, %d, %d", valueBits, isLE, isMSB2LSB);
@@ -135,6 +136,7 @@ public:
 
 		// Increase the position within the current value
 		_inValue = (_inValue + 1) % valueBits;
+		_pos++;
 
 		return b;
 	}
@@ -176,11 +178,13 @@ public:
 	uint32 peekBit() {
 		uint32 value   = _value;
 		uint8  inValue = _inValue;
-		uint32 curPos  = _stream->pos();
+		uint32 curStreamPos  = _stream->pos();
+		uint32 curPos = _pos;
 
 		uint32 v = getBit();
 
-		_stream->seek(curPos);
+		_pos     = curPos;
+		_stream->seek(curStreamPos);
 		_inValue = inValue;
 		_value   = value;
 
@@ -195,11 +199,13 @@ public:
 	uint32 peekBits(uint8 n) {
 		uint32 value   = _value;
 		uint8  inValue = _inValue;
-		uint32 curPos  = _stream->pos();
+		uint32 curStreamPos  = _stream->pos();
+		uint32 curPos = _pos;
 
 		uint32 v = getBits(n);
 
-		_stream->seek(curPos);
+		_pos     = curPos;
+		_stream->seek(curStreamPos);
 		_inValue = inValue;
 		_value   = value;
 
@@ -233,6 +239,7 @@ public:
 
 		_value   = 0;
 		_inValue = 0;
+		_pos     = 0;
 	}
 
 	/** Skip the specified amount of bits. */
@@ -249,11 +256,7 @@ public:
 
 	/** Return the stream position in bits. */
 	uint32 pos() const {
-		if (_stream->pos() == 0)
-			return 0;
-
-		uint32 p = (_inValue == 0) ? _stream->pos() : ((_stream->pos() - 1) & ~((uint32) ((valueBits >> 3) - 1)));
-		return p * 8 + _inValue;
+		return _pos;
 	}
 
 	/** Return the stream size in bits. */
@@ -262,7 +265,7 @@ public:
 	}
 
 	bool eos() const {
-		return _stream->eos() || (pos() >= _size);
+		return _stream->eos() || (_pos >= _size);
 	}
 };
 


Commit: 0c8f95603faca0f5a185bd421b34684645ed8b4d
    https://github.com/scummvm/scummvm/commit/0c8f95603faca0f5a185bd421b34684645ed8b4d
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2017-08-24T19:46:59+02:00

Commit Message:
COMMON: Add BitStream classes for memory buffers

Changed paths:
    common/bitstream.h
    test/common/bitstream.h


diff --git a/common/bitstream.h b/common/bitstream.h
index c5226d5..e867d86 100644
--- a/common/bitstream.h
+++ b/common/bitstream.h
@@ -269,6 +269,152 @@ public:
 	}
 };
 
+
+
+/**
+ * A cut-down version of MemoryReadStream specifically for use with BitStream.
+ * It removes the virtual call overhead for reading bytes from a memory buffer,
+ * and allows directly inlining this access.
+ *
+ * The code duplication with MemoryReadStream is not ideal.
+ * It might be possible to avoid this by making this a final subclass of
+ * MemoryReadStream, but that is a C++11 feature.
+ */
+class BitStreamMemoryStream {
+private:
+	const byte * const _ptrOrig;
+	const byte *_ptr;
+	const uint32 _size;
+	uint32 _pos;
+	DisposeAfterUse::Flag _disposeMemory;
+	bool _eos;
+
+public:
+	BitStreamMemoryStream(const byte *dataPtr, uint32 dataSize, DisposeAfterUse::Flag disposeMemory = DisposeAfterUse::NO) :
+		_ptrOrig(dataPtr),
+		_ptr(dataPtr),
+		_size(dataSize),
+		_pos(0),
+		_disposeMemory(disposeMemory),
+		_eos(false) {}
+
+	~BitStreamMemoryStream() {
+		if (_disposeMemory)
+			free(const_cast<byte *>(_ptrOrig));
+	}
+
+	bool eos() const {
+		return _eos;
+	}
+
+	bool err() const {
+		return false;
+	}
+
+	int32 pos() const {
+		return _pos;
+	}
+
+	int32 size() const {
+		return _size;
+	}
+
+	bool seek(uint32 offset) {
+		assert(offset <= _size);
+
+		_eos = false;
+		_pos = offset;
+		_ptr = _ptrOrig + _pos;
+		return true;
+	}
+
+	byte readByte() {
+		if (_pos >= _size) {
+			_eos = true;
+			return 0;
+		}
+
+		_pos++;
+		return *_ptr++;
+	}
+
+	uint16 readUint16LE() {
+		if (_pos + 2 > _size) {
+			_eos = true;
+			if (_pos < _size) {
+				_pos++;
+				return *_ptr++;
+			} else {
+				return 0;
+			}
+		}
+
+		uint16 val = READ_LE_UINT16(_ptr);
+
+		_pos += 2;
+		_ptr += 2;
+
+		return val;
+	}
+
+	uint16 readUint16BE() {
+		if (_pos + 2 > _size) {
+			_eos = true;
+			if (_pos < _size) {
+				_pos++;
+				return (*_ptr++) << 8;
+			} else {
+				return 0;
+			}
+		}
+
+		uint16 val = READ_LE_UINT16(_ptr);
+
+		_pos += 2;
+		_ptr += 2;
+
+		return val;
+	}
+
+	uint32 readUint32LE() {
+		if (_pos + 4 > _size) {
+			uint32 val = readByte();
+			val |= (uint32)readByte() << 8;
+			val |= (uint32)readByte() << 16;
+			val |= (uint32)readByte() << 24;
+
+			return val;
+		}
+
+		uint32 val = READ_LE_UINT32(_ptr);
+
+		_pos += 4;
+		_ptr += 4;
+
+		return val;
+	}
+
+	uint32 readUint32BE() {
+		if (_pos + 4 > _size) {
+			uint32 val = (uint32)readByte() << 24;
+			val |= (uint32)readByte() << 16;
+			val |= (uint32)readByte() << 8;
+			val |= (uint32)readByte();
+
+			return val;
+		}
+
+		uint32 val = READ_BE_UINT32(_ptr);
+
+		_pos += 4;
+		_ptr += 4;
+
+		return val;
+	}
+
+};
+
+
 // typedefs for various memory layouts.
 
 /** 8-bit data, MSB to LSB. */
@@ -294,6 +440,32 @@ typedef BitStreamImpl<SeekableReadStream, 32, false, true > BitStream32BEMSB;
 /** 32-bit big-endian data, LSB to MSB. */
 typedef BitStreamImpl<SeekableReadStream, 32, false, false> BitStream32BELSB;
 
+
+
+/** 8-bit data, MSB to LSB. */
+typedef BitStreamImpl<BitStreamMemoryStream, 8, false, true > BitStreamMemory8MSB;
+/** 8-bit data, LSB to MSB. */
+typedef BitStreamImpl<BitStreamMemoryStream, 8, false, false> BitStreamMemory8LSB;
+
+/** 16-bit little-endian data, MSB to LSB. */
+typedef BitStreamImpl<BitStreamMemoryStream, 16, true , true > BitStreamMemory16LEMSB;
+/** 16-bit little-endian data, LSB to MSB. */
+typedef BitStreamImpl<BitStreamMemoryStream, 16, true , false> BitStreamMemory16LELSB;
+/** 16-bit big-endian data, MSB to LSB. */
+typedef BitStreamImpl<BitStreamMemoryStream, 16, false, true > BitStreamMemory16BEMSB;
+/** 16-bit big-endian data, LSB to MSB. */
+typedef BitStreamImpl<BitStreamMemoryStream, 16, false, false> BitStreamMemory16BELSB;
+
+/** 32-bit little-endian data, MSB to LSB. */
+typedef BitStreamImpl<BitStreamMemoryStream, 32, true , true > BitStreamMemory32LEMSB;
+/** 32-bit little-endian data, LSB to MSB. */
+typedef BitStreamImpl<BitStreamMemoryStream, 32, true , false> BitStreamMemory32LELSB;
+/** 32-bit big-endian data, MSB to LSB. */
+typedef BitStreamImpl<BitStreamMemoryStream, 32, false, true > BitStreamMemory32BEMSB;
+/** 32-bit big-endian data, LSB to MSB. */
+typedef BitStreamImpl<BitStreamMemoryStream, 32, false, false> BitStreamMemory32BELSB;
+
+
 } // End of namespace Common
 
 #endif // COMMON_BITSTREAM_H
diff --git a/test/common/bitstream.h b/test/common/bitstream.h
index 3f6a15f..0488169 100644
--- a/test/common/bitstream.h
+++ b/test/common/bitstream.h
@@ -5,13 +5,14 @@
 
 class BitStreamTestSuite : public CxxTest::TestSuite
 {
-	public:
-	void test_get_bit() {
+private:
+	template<class MS, class BS>
+	void tmpl_get_bit() {
 		byte contents[] = { 'a' };
 
-		Common::MemoryReadStream ms(contents, sizeof(contents));
+		MS ms(contents, sizeof(contents));
 
-		Common::BitStream8MSB bs(ms);
+		BS bs(ms);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
 		TS_ASSERT_EQUALS(bs.getBit(), 0u);
 		TS_ASSERT_EQUALS(bs.getBit(), 1u);
@@ -19,13 +20,20 @@ class BitStreamTestSuite : public CxxTest::TestSuite
 		TS_ASSERT_EQUALS(bs.pos(), 3u);
 		TS_ASSERT(!bs.eos());
 	}
+public:
+	void test_get_bit() {
+		tmpl_get_bit<Common::MemoryReadStream, Common::BitStream8MSB>();
+		tmpl_get_bit<Common::BitStreamMemoryStream, Common::BitStreamMemory8MSB>();
+	}
 
-	void test_get_bits() {
+private:
+	template<class MS, class BS>
+	void tmpl_get_bits() {
 		byte contents[] = { 'a', 'b' };
 
-		Common::MemoryReadStream ms(contents, sizeof(contents));
+		MS ms(contents, sizeof(contents));
 
-		Common::BitStream8MSB bs(ms);
+		BS bs(ms);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
 		TS_ASSERT_EQUALS(bs.getBits(3), 3u);
 		TS_ASSERT_EQUALS(bs.pos(), 3u);
@@ -33,13 +41,20 @@ class BitStreamTestSuite : public CxxTest::TestSuite
 		TS_ASSERT_EQUALS(bs.pos(), 11u);
 		TS_ASSERT(!bs.eos());
 	}
+public:
+	void test_get_bits() {
+		tmpl_get_bits<Common::MemoryReadStream, Common::BitStream8MSB>();
+		tmpl_get_bits<Common::BitStreamMemoryStream, Common::BitStreamMemory8MSB>();
+	}
 
-	void test_skip() {
+private:
+	template<class MS, class BS>
+	void tmpl_skip() {
 		byte contents[] = { 'a', 'b' };
 
-		Common::MemoryReadStream ms(contents, sizeof(contents));
+		MS ms(contents, sizeof(contents));
 
-		Common::BitStream8MSB bs(ms);
+		BS bs(ms);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
 		bs.skip(5);
 		TS_ASSERT_EQUALS(bs.pos(), 5u);
@@ -48,13 +63,20 @@ class BitStreamTestSuite : public CxxTest::TestSuite
 		TS_ASSERT_EQUALS(bs.getBits(3), 6u);
 		TS_ASSERT(!bs.eos());
 	}
+public:
+	void test_skip() {
+		tmpl_skip<Common::MemoryReadStream, Common::BitStream8MSB>();
+		tmpl_skip<Common::BitStreamMemoryStream, Common::BitStreamMemory8MSB>();
+	}
 
-	void test_rewind() {
+private:
+	template<class MS, class BS>
+	void tmpl_rewind() {
 		byte contents[] = { 'a' };
 
-		Common::MemoryReadStream ms(contents, sizeof(contents));
+		MS ms(contents, sizeof(contents));
 
-		Common::BitStream8MSB bs(ms);
+		BS bs(ms);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
 		bs.skip(5);
 		TS_ASSERT_EQUALS(bs.pos(), 5u);
@@ -65,13 +87,20 @@ class BitStreamTestSuite : public CxxTest::TestSuite
 
 		TS_ASSERT_EQUALS(bs.size(), 8u);
 	}
+public:
+	void test_rewind() {
+		tmpl_rewind<Common::MemoryReadStream, Common::BitStream8MSB>();
+		tmpl_rewind<Common::BitStreamMemoryStream, Common::BitStreamMemory8MSB>();
+	}
 
-	void test_peek_bit() {
+private:
+	template<class MS, class BS>
+	void tmpl_peek_bit() {
 		byte contents[] = { 'a' };
 
-		Common::MemoryReadStream ms(contents, sizeof(contents));
+		MS ms(contents, sizeof(contents));
 
-		Common::BitStream8MSB bs(ms);
+		BS bs(ms);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
 		TS_ASSERT_EQUALS(bs.peekBit(), 0u);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
@@ -81,13 +110,20 @@ class BitStreamTestSuite : public CxxTest::TestSuite
 		TS_ASSERT_EQUALS(bs.pos(), 1u);
 		TS_ASSERT(!bs.eos());
 	}
+public:
+	void test_peek_bit() {
+		tmpl_peek_bit<Common::MemoryReadStream, Common::BitStream8MSB>();
+		tmpl_peek_bit<Common::BitStreamMemoryStream, Common::BitStreamMemory8MSB>();
+	}
 
-	void test_peek_bits() {
+private:
+	template<class MS, class BS>
+	void tmpl_peek_bits() {
 		byte contents[] = { 'a', 'b' };
 
-		Common::MemoryReadStream ms(contents, sizeof(contents));
+		MS ms(contents, sizeof(contents));
 
-		Common::BitStream8MSB bs(ms);
+		BS bs(ms);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
 		TS_ASSERT_EQUALS(bs.peekBits(3), 3u);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
@@ -100,13 +136,20 @@ class BitStreamTestSuite : public CxxTest::TestSuite
 		TS_ASSERT_EQUALS(bs.peekBits(5), 2u);
 		TS_ASSERT(!bs.eos());
 	}
+public:
+	void test_peek_bits() {
+		tmpl_peek_bits<Common::MemoryReadStream, Common::BitStream8MSB>();
+		tmpl_peek_bits<Common::BitStreamMemoryStream, Common::BitStreamMemory8MSB>();
+	}
 
-	void test_eos() {
+private:
+	template<class MS, class BS>
+	void tmpl_eos() {
 		byte contents[] = { 'a', 'b' };
 
-		Common::MemoryReadStream ms(contents, sizeof(contents));
+		MS ms(contents, sizeof(contents));
 
-		Common::BitStream8MSB bs(ms);
+		BS bs(ms);
 		bs.skip(11);
 		TS_ASSERT_EQUALS(bs.pos(), 11u);
 		TS_ASSERT_EQUALS(bs.getBits(5), 2u);
@@ -116,13 +159,20 @@ class BitStreamTestSuite : public CxxTest::TestSuite
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
 		TS_ASSERT(!bs.eos());
 	}
+public:
+	void test_eos() {
+		tmpl_eos<Common::MemoryReadStream, Common::BitStream8MSB>();
+		tmpl_eos<Common::BitStreamMemoryStream, Common::BitStreamMemory8MSB>();
+	}
 
-	void test_get_bits_lsb() {
+private:
+	template<class MS, class BS>
+	void tmpl_get_bits_lsb() {
 		byte contents[] = { 'a', 'b' };
 
-		Common::MemoryReadStream ms(contents, sizeof(contents));
+		MS ms(contents, sizeof(contents));
 
-		Common::BitStream8LSB bs(ms);
+		BS bs(ms);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
 		TS_ASSERT_EQUALS(bs.getBits(3), 1u);
 		TS_ASSERT_EQUALS(bs.pos(), 3u);
@@ -130,13 +180,20 @@ class BitStreamTestSuite : public CxxTest::TestSuite
 		TS_ASSERT_EQUALS(bs.pos(), 11u);
 		TS_ASSERT(!bs.eos());
 	}
+public:
+	void test_get_bits_lsb() {
+		tmpl_get_bits_lsb<Common::MemoryReadStream, Common::BitStream8LSB>();
+		tmpl_get_bits_lsb<Common::BitStreamMemoryStream, Common::BitStreamMemory8LSB>();
+	}
 
-	void test_peek_bits_lsb() {
+private:
+	template<class MS, class BS>
+	void tmpl_peek_bits_lsb() {
 		byte contents[] = { 'a', 'b' };
 
-		Common::MemoryReadStream ms(contents, sizeof(contents));
+		MS ms(contents, sizeof(contents));
 
-		Common::BitStream8LSB bs(ms);
+		BS bs(ms);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
 		TS_ASSERT_EQUALS(bs.peekBits(3), 1u);
 		TS_ASSERT_EQUALS(bs.pos(), 0u);
@@ -149,4 +206,9 @@ class BitStreamTestSuite : public CxxTest::TestSuite
 		TS_ASSERT_EQUALS(bs.peekBits(5), 12u);
 		TS_ASSERT(!bs.eos());
 	}
+public:
+	void test_peek_bits_lsb() {
+		tmpl_peek_bits_lsb<Common::MemoryReadStream, Common::BitStream8LSB>();
+		tmpl_peek_bits_lsb<Common::BitStreamMemoryStream, Common::BitStreamMemory8LSB>();
+	}
 };


Commit: edfdbb9dd749cde96c1e155a1e5e5e00856adcc1
    https://github.com/scummvm/scummvm/commit/edfdbb9dd749cde96c1e155a1e5e5e00856adcc1
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2017-08-24T19:46:59+02:00

Commit Message:
IMAGE: Use new BitStreamMemory class for indeo

Changed paths:
    image/codecs/indeo/get_bits.h
    image/codecs/indeo4.cpp
    image/codecs/indeo5.cpp


diff --git a/image/codecs/indeo/get_bits.h b/image/codecs/indeo/get_bits.h
index c2d593b..f972e68 100644
--- a/image/codecs/indeo/get_bits.h
+++ b/image/codecs/indeo/get_bits.h
@@ -31,15 +31,14 @@ namespace Indeo {
 /**
  * Intel Indeo Bitstream reader
  */
-class GetBits : public Common::BitStream8LSB {
+class GetBits : public Common::BitStreamMemory8LSB {
 public:
 	/**
 	* Constructor
 	* @param stream				Source stream to reader from
 	* @param disposeAfterUse	Whether to destroy stream in destructor
 	*/
-	GetBits(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse
-		= DisposeAfterUse::YES) : Common::BitStream8LSB(stream, disposeAfterUse) {}
+	GetBits(const uint8 *ptr, uint32 size) : Common::BitStreamMemory8LSB(new Common::BitStreamMemoryStream(ptr, size), DisposeAfterUse::YES) {}
 
 	/**
 	 * The number of bits left
diff --git a/image/codecs/indeo4.cpp b/image/codecs/indeo4.cpp
index 4658068..ead1d3a 100644
--- a/image/codecs/indeo4.cpp
+++ b/image/codecs/indeo4.cpp
@@ -56,7 +56,7 @@ bool Indeo4Decoder::isIndeo4(Common::SeekableReadStream &stream) {
 	stream.seek(-16, SEEK_CUR);
 
 	// Validate the first 18-bit word has the correct identifier
-	Indeo::GetBits gb(new Common::MemoryReadStream(buffer, 16 * 8), DisposeAfterUse::YES);
+	Indeo::GetBits gb(buffer, 16 * 8);
 	bool isIndeo4 = gb.getBits(18) == 0x3FFF8;
 
 	return isIndeo4;
@@ -74,7 +74,7 @@ const Graphics::Surface *Indeo4Decoder::decodeFrame(Common::SeekableReadStream &
 	_ctx._frameSize = stream.size();
 
 	// Set up the GetBits instance for reading the data
-	_ctx._gb = new GetBits(new Common::MemoryReadStream(_ctx._frameData, _ctx._frameSize));
+	_ctx._gb = new GetBits(_ctx._frameData, _ctx._frameSize);
 
 	// Decode the frame
 	int err = decodeIndeoFrame();
diff --git a/image/codecs/indeo5.cpp b/image/codecs/indeo5.cpp
index 858c634..c4e98d4 100644
--- a/image/codecs/indeo5.cpp
+++ b/image/codecs/indeo5.cpp
@@ -67,7 +67,7 @@ bool Indeo5Decoder::isIndeo5(Common::SeekableReadStream &stream) {
 	stream.seek(-16, SEEK_CUR);
 
 	// Validate the first 5-bit word has the correct identifier
-	Indeo::GetBits gb(new Common::MemoryReadStream(buffer, 16 * 8));
+	Indeo::GetBits gb(buffer, 16 * 8);
 	bool isIndeo5 = gb.getBits(5) == 0x1F;
 
 	return isIndeo5;
@@ -85,7 +85,7 @@ const Graphics::Surface *Indeo5Decoder::decodeFrame(Common::SeekableReadStream &
 	_ctx._frameSize = stream.size();
 
 	// Set up the GetBits instance for reading the data
-	_ctx._gb = new GetBits(new Common::MemoryReadStream(_ctx._frameData, _ctx._frameSize));
+	_ctx._gb = new GetBits(_ctx._frameData, _ctx._frameSize);
 
 	// Decode the frame
 	int err = decodeIndeoFrame();


Commit: e72f681ceb6fe77fcd82d97360cc5f1e48f7490e
    https://github.com/scummvm/scummvm/commit/e72f681ceb6fe77fcd82d97360cc5f1e48f7490e
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2017-08-24T19:46:59+02:00

Commit Message:
VIDEO: Use new BitStreamMemory class for SmackerDecoder

Changed paths:
    video/smk_decoder.cpp
    video/smk_decoder.h


diff --git a/video/smk_decoder.cpp b/video/smk_decoder.cpp
index d8057ac..55b43bd 100644
--- a/video/smk_decoder.cpp
+++ b/video/smk_decoder.cpp
@@ -29,7 +29,6 @@
 #include "common/endian.h"
 #include "common/util.h"
 #include "common/stream.h"
-#include "common/memstream.h"
 #include "common/bitstream.h"
 #include "common/system.h"
 #include "common/textconsole.h"
@@ -54,9 +53,9 @@ enum SmkBlockTypes {
 
 class SmallHuffmanTree {
 public:
-	SmallHuffmanTree(Common::BitStream8LSB &bs);
+	SmallHuffmanTree(Common::BitStreamMemory8LSB &bs);
 
-	uint16 getCode(Common::BitStream8LSB &bs);
+	uint16 getCode(Common::BitStreamMemory8LSB &bs);
 private:
 	enum {
 		SMK_NODE = 0x8000
@@ -70,10 +69,10 @@ private:
 	uint16 _prefixtree[256];
 	byte _prefixlength[256];
 
-	Common::BitStream8LSB &_bs;
+	Common::BitStreamMemory8LSB &_bs;
 };
 
-SmallHuffmanTree::SmallHuffmanTree(Common::BitStream8LSB &bs)
+SmallHuffmanTree::SmallHuffmanTree(Common::BitStreamMemory8LSB &bs)
 	: _treeSize(0), _bs(bs) {
 	uint32 bit = _bs.getBit();
 	assert(bit);
@@ -118,7 +117,7 @@ uint16 SmallHuffmanTree::decodeTree(uint32 prefix, int length) {
 	return r1+r2+1;
 }
 
-uint16 SmallHuffmanTree::getCode(Common::BitStream8LSB &bs) {
+uint16 SmallHuffmanTree::getCode(Common::BitStreamMemory8LSB &bs) {
 	byte peek = bs.peekBits(MIN<uint32>(bs.size() - bs.pos(), 8));
 	uint16 *p = &_tree[_prefixtree[peek]];
 	bs.skip(_prefixlength[peek]);
@@ -139,11 +138,11 @@ uint16 SmallHuffmanTree::getCode(Common::BitStream8LSB &bs) {
 
 class BigHuffmanTree {
 public:
-	BigHuffmanTree(Common::BitStream8LSB &bs, int allocSize);
+	BigHuffmanTree(Common::BitStreamMemory8LSB &bs, int allocSize);
 	~BigHuffmanTree();
 
 	void reset();
-	uint32 getCode(Common::BitStream8LSB &bs);
+	uint32 getCode(Common::BitStreamMemory8LSB &bs);
 private:
 	enum {
 		SMK_NODE = 0x80000000
@@ -159,13 +158,13 @@ private:
 	byte _prefixlength[256];
 
 	/* Used during construction */
-	Common::BitStream8LSB &_bs;
+	Common::BitStreamMemory8LSB &_bs;
 	uint32 _markers[3];
 	SmallHuffmanTree *_loBytes;
 	SmallHuffmanTree *_hiBytes;
 };
 
-BigHuffmanTree::BigHuffmanTree(Common::BitStream8LSB &bs, int allocSize)
+BigHuffmanTree::BigHuffmanTree(Common::BitStreamMemory8LSB &bs, int allocSize)
 	: _bs(bs) {
 	uint32 bit = _bs.getBit();
 	if (!bit) {
@@ -256,7 +255,7 @@ uint32 BigHuffmanTree::decodeTree(uint32 prefix, int length) {
 	return r1+r2+1;
 }
 
-uint32 BigHuffmanTree::getCode(Common::BitStream8LSB &bs) {
+uint32 BigHuffmanTree::getCode(Common::BitStreamMemory8LSB &bs) {
 	byte peek = bs.peekBits(MIN<uint32>(bs.size() - bs.pos(), 8));
 	uint32 *p = &_tree[_prefixtree[peek]];
 	bs.skip(_prefixlength[peek]);
@@ -386,7 +385,7 @@ bool SmackerDecoder::loadStream(Common::SeekableReadStream *stream) {
 	byte *huffmanTrees = (byte *) malloc(_header.treesSize);
 	_fileStream->read(huffmanTrees, _header.treesSize);
 
-	Common::BitStream8LSB bs(new Common::MemoryReadStream(huffmanTrees, _header.treesSize, DisposeAfterUse::YES), DisposeAfterUse::YES);
+	Common::BitStreamMemory8LSB bs(new Common::BitStreamMemoryStream(huffmanTrees, _header.treesSize, DisposeAfterUse::YES), DisposeAfterUse::YES);
 	videoTrack->readTrees(bs, _header.mMapSize, _header.mClrSize, _header.fullSize, _header.typeSize);
 
 	_firstFrameStart = _fileStream->pos();
@@ -469,7 +468,7 @@ void SmackerDecoder::readNextPacket() {
 
 	_fileStream->read(frameData, frameDataSize);
 
-	Common::BitStream8LSB bs(new Common::MemoryReadStream(frameData, frameDataSize + 1, DisposeAfterUse::YES), DisposeAfterUse::YES);
+	Common::BitStreamMemory8LSB bs(new Common::BitStreamMemoryStream(frameData, frameDataSize + 1, DisposeAfterUse::YES), DisposeAfterUse::YES);
 	videoTrack->decodeFrame(bs);
 
 	_fileStream->seek(startPos + frameSize);
@@ -553,14 +552,14 @@ Graphics::PixelFormat SmackerDecoder::SmackerVideoTrack::getPixelFormat() const
 	return _surface->format;
 }
 
-void SmackerDecoder::SmackerVideoTrack::readTrees(Common::BitStream8LSB &bs, uint32 mMapSize, uint32 mClrSize, uint32 fullSize, uint32 typeSize) {
+void SmackerDecoder::SmackerVideoTrack::readTrees(Common::BitStreamMemory8LSB &bs, uint32 mMapSize, uint32 mClrSize, uint32 fullSize, uint32 typeSize) {
 	_MMapTree = new BigHuffmanTree(bs, mMapSize);
 	_MClrTree = new BigHuffmanTree(bs, mClrSize);
 	_FullTree = new BigHuffmanTree(bs, fullSize);
 	_TypeTree = new BigHuffmanTree(bs, typeSize);
 }
 
-void SmackerDecoder::SmackerVideoTrack::decodeFrame(Common::BitStream8LSB &bs) {
+void SmackerDecoder::SmackerVideoTrack::decodeFrame(Common::BitStreamMemory8LSB &bs) {
 	_MMapTree->reset();
 	_MClrTree->reset();
 	_FullTree->reset();
@@ -774,7 +773,7 @@ Audio::AudioStream *SmackerDecoder::SmackerAudioTrack::getAudioStream() const {
 }
 
 void SmackerDecoder::SmackerAudioTrack::queueCompressedBuffer(byte *buffer, uint32 bufferSize, uint32 unpackedSize) {
-	Common::BitStream8LSB audioBS(new Common::MemoryReadStream(buffer, bufferSize), DisposeAfterUse::YES);
+	Common::BitStreamMemory8LSB audioBS(new Common::BitStreamMemoryStream(buffer, bufferSize), DisposeAfterUse::YES);
 	bool dataPresent = audioBS.getBit();
 
 	if (!dataPresent)
diff --git a/video/smk_decoder.h b/video/smk_decoder.h
index e0ea600..d7a4459 100644
--- a/video/smk_decoder.h
+++ b/video/smk_decoder.h
@@ -91,9 +91,9 @@ protected:
 		const byte *getPalette() const { _dirtyPalette = false; return _palette; }
 		bool hasDirtyPalette() const { return _dirtyPalette; }
 
-		void readTrees(Common::BitStream8LSB &bs, uint32 mMapSize, uint32 mClrSize, uint32 fullSize, uint32 typeSize);
+		void readTrees(Common::BitStreamMemory8LSB &bs, uint32 mMapSize, uint32 mClrSize, uint32 fullSize, uint32 typeSize);
 		void increaseCurFrame() { _curFrame++; }
-		void decodeFrame(Common::BitStream8LSB &bs);
+		void decodeFrame(Common::BitStreamMemory8LSB &bs);
 		void unpackPalette(Common::SeekableReadStream *stream);
 
 	protected:


Commit: 47539e1939433de73a75a60b15924afc23237cde
    https://github.com/scummvm/scummvm/commit/47539e1939433de73a75a60b15924afc23237cde
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2017-08-24T19:46:59+02:00

Commit Message:
VIDEO: Use new BitStreamMemory class for PSXStreamDecoder

Changed paths:
    video/psx_decoder.cpp
    video/psx_decoder.h


diff --git a/video/psx_decoder.cpp b/video/psx_decoder.cpp
index 973803e..562da88 100644
--- a/video/psx_decoder.cpp
+++ b/video/psx_decoder.cpp
@@ -27,7 +27,6 @@
 #include "audio/decoders/raw.h"
 #include "common/bitstream.h"
 #include "common/huffman.h"
-#include "common/memstream.h"
 #include "common/stream.h"
 #include "common/system.h"
 #include "common/textconsole.h"
@@ -232,7 +231,7 @@ void PSXStreamDecoder::readNextPacket() {
 
 				if (curSector == sectorCount - 1) {
 					// Done assembling the frame
-					Common::SeekableReadStream *frame = new Common::MemoryReadStream(partialFrame, frameSize, DisposeAfterUse::YES);
+					Common::BitStreamMemoryStream *frame = new Common::BitStreamMemoryStream(partialFrame, frameSize, DisposeAfterUse::YES);
 
 					_videoTrack->decodeFrame(frame, sectorsRead);
 
@@ -464,10 +463,10 @@ const Graphics::Surface *PSXStreamDecoder::PSXVideoTrack::decodeNextFrame() {
 	return _surface;
 }
 
-void PSXStreamDecoder::PSXVideoTrack::decodeFrame(Common::SeekableReadStream *frame, uint sectorCount) {
+void PSXStreamDecoder::PSXVideoTrack::decodeFrame(Common::BitStreamMemoryStream *frame, uint sectorCount) {
 	// A frame is essentially an MPEG-1 intra frame
 
-	Common::BitStream16LEMSB bits(frame);
+	Common::BitStreamMemory16LEMSB bits(frame);
 
 	bits.skip(16); // unknown
 	bits.skip(16); // 0x3800
@@ -499,7 +498,7 @@ void PSXStreamDecoder::PSXVideoTrack::decodeFrame(Common::SeekableReadStream *fr
 	_nextFrameStartTime = _nextFrameStartTime.addFrames(sectorCount);
 }
 
-void PSXStreamDecoder::PSXVideoTrack::decodeMacroBlock(Common::BitStream16LEMSB *bits, int mbX, int mbY, uint16 scale, uint16 version) {
+void PSXStreamDecoder::PSXVideoTrack::decodeMacroBlock(Common::BitStreamMemory16LEMSB *bits, int mbX, int mbY, uint16 scale, uint16 version) {
 	int pitchY = _macroBlocksW * 16;
 	int pitchC = _macroBlocksW * 8;
 
@@ -546,7 +545,7 @@ void PSXStreamDecoder::PSXVideoTrack::dequantizeBlock(int *coefficients, float *
 	}
 }
 
-int PSXStreamDecoder::PSXVideoTrack::readDC(Common::BitStream16LEMSB *bits, uint16 version, PlaneType plane) {
+int PSXStreamDecoder::PSXVideoTrack::readDC(Common::BitStreamMemory16LEMSB *bits, uint16 version, PlaneType plane) {
 	// Version 2 just has its coefficient as 10-bits
 	if (version == 2)
 		return readSignedCoefficient(bits);
@@ -576,7 +575,7 @@ int PSXStreamDecoder::PSXVideoTrack::readDC(Common::BitStream16LEMSB *bits, uint
 	if (count > 63) \
 		error("PSXStreamDecoder::readAC(): Too many coefficients")
 
-void PSXStreamDecoder::PSXVideoTrack::readAC(Common::BitStream16LEMSB *bits, int *block) {
+void PSXStreamDecoder::PSXVideoTrack::readAC(Common::BitStreamMemory16LEMSB *bits, int *block) {
 	// Clear the block first
 	for (int i = 0; i < 63; i++)
 		block[i] = 0;
@@ -611,7 +610,7 @@ void PSXStreamDecoder::PSXVideoTrack::readAC(Common::BitStream16LEMSB *bits, int
 	}
 }
 
-int PSXStreamDecoder::PSXVideoTrack::readSignedCoefficient(Common::BitStream16LEMSB *bits) {
+int PSXStreamDecoder::PSXVideoTrack::readSignedCoefficient(Common::BitStreamMemory16LEMSB *bits) {
 	uint val = bits->getBits(10);
 
 	// extend the sign
@@ -672,7 +671,7 @@ void PSXStreamDecoder::PSXVideoTrack::idct(float *dequantData, float *result) {
 	}
 }
 
-void PSXStreamDecoder::PSXVideoTrack::decodeBlock(Common::BitStream16LEMSB *bits, byte *block, int pitch, uint16 scale, uint16 version, PlaneType plane) {
+void PSXStreamDecoder::PSXVideoTrack::decodeBlock(Common::BitStreamMemory16LEMSB *bits, byte *block, int pitch, uint16 scale, uint16 version, PlaneType plane) {
 	// Version 2 just has signed 10 bits for DC
 	// Version 3 has them huffman coded
 	int coefficients[8 * 8];
diff --git a/video/psx_decoder.h b/video/psx_decoder.h
index fa36af1..c966422 100644
--- a/video/psx_decoder.h
+++ b/video/psx_decoder.h
@@ -37,7 +37,6 @@ class QueuingAudioStream;
 
 namespace Common {
 class Huffman;
-class SeekableReadStream;
 }
 
 namespace Graphics {
@@ -91,7 +90,7 @@ private:
 		const Graphics::Surface *decodeNextFrame();
 
 		void setEndOfTrack() { _endOfTrack = true; }
-		void decodeFrame(Common::SeekableReadStream *frame, uint sectorCount);
+		void decodeFrame(Common::BitStreamMemoryStream *frame, uint sectorCount);
 
 	private:
 		Graphics::Surface *_surface;
@@ -108,19 +107,19 @@ private:
 
 		uint16 _macroBlocksW, _macroBlocksH;
 		byte *_yBuffer, *_cbBuffer, *_crBuffer;
-		void decodeMacroBlock(Common::BitStream16LEMSB *bits, int mbX, int mbY, uint16 scale, uint16 version);
-		void decodeBlock(Common::BitStream16LEMSB *bits, byte *block, int pitch, uint16 scale, uint16 version, PlaneType plane);
+		void decodeMacroBlock(Common::BitStreamMemory16LEMSB *bits, int mbX, int mbY, uint16 scale, uint16 version);
+		void decodeBlock(Common::BitStreamMemory16LEMSB *bits, byte *block, int pitch, uint16 scale, uint16 version, PlaneType plane);
 
-		void readAC(Common::BitStream16LEMSB *bits, int *block);
+		void readAC(Common::BitStreamMemory16LEMSB *bits, int *block);
 		Common::Huffman *_acHuffman;
 
-		int readDC(Common::BitStream16LEMSB *bits, uint16 version, PlaneType plane);
+		int readDC(Common::BitStreamMemory16LEMSB *bits, uint16 version, PlaneType plane);
 		Common::Huffman *_dcHuffmanLuma, *_dcHuffmanChroma;
 		int _lastDC[3];
 
 		void dequantizeBlock(int *coefficients, float *block, uint16 scale);
 		void idct(float *dequantData, float *result);
-		int readSignedCoefficient(Common::BitStream16LEMSB *bits);
+		int readSignedCoefficient(Common::BitStreamMemory16LEMSB *bits);
 	};
 
 	class PSXAudioTrack : public AudioTrack {


Commit: 4278cff7f014ba1f404b3cebfe20016b957fafbf
    https://github.com/scummvm/scummvm/commit/4278cff7f014ba1f404b3cebfe20016b957fafbf
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2017-08-24T19:46:59+02:00

Commit Message:
COMMON: Optimize BitStream::getBits

Changed paths:
    common/bitstream.h


diff --git a/common/bitstream.h b/common/bitstream.h
index e867d86..1fbc006 100644
--- a/common/bitstream.h
+++ b/common/bitstream.h
@@ -29,6 +29,7 @@
 #include "common/textconsole.h"
 #include "common/stream.h"
 #include "common/types.h"
+#include "common/util.h"
 
 namespace Common {
 
@@ -115,14 +116,10 @@ public:
 			delete _stream;
 	}
 
-	/** Read a bit from the bit stream. */
-	uint32 getBit() {
-		// Check if we need the next value
-		if (_inValue == 0)
-			readValue();
-
+private:
+	uint32 getBit_internal() {
 		// Get the current bit
-		int b = 0;
+		uint32 b = 0;
 		if (isMSB2LSB)
 			b = ((_value & 0x80000000) == 0) ? 0 : 1;
 		else
@@ -134,6 +131,18 @@ public:
 		else
 			_value >>= 1;
 
+		return b;
+	}
+
+public:
+	/** Read a bit from the bit stream. */
+	uint32 getBit() {
+		// Check if we need the next value
+		if (_inValue == 0)
+			readValue();
+
+		uint32 b = getBit_internal();
+
 		// Increase the position within the current value
 		_inValue = (_inValue + 1) % valueBits;
 		_pos++;
@@ -161,16 +170,42 @@ public:
 		// Read the number of bits
 		uint32 v = 0;
 
-		if (isMSB2LSB) {
-			while (n-- > 0)
-				v = (v << 1) | getBit();
-		} else {
-			for (uint32 i = 0; i < n; i++)
-				v = (v >> 1) | (((uint32) getBit()) << 31);
+		uint8 nOrig = n;
+		if (_inValue) {
+			int count = MIN((int)n, valueBits - _inValue);
+			for (int i = 0; i < count; ++i) {
+				if (isMSB2LSB) {
+					v = (v << 1) | getBit_internal();
+				} else {
+					v = (v >> 1) | (getBit_internal() << 31);
+				}
+			}
+
+			n -= count;
+		}
+
+		while (n > 0) {
+			// NB: readValue doesn't care that _inValue is incorrect here
+			readValue();
 
-			v >>= (32 - n);
+			int count = MIN((int)n, valueBits);
+			for (int i = 0; i < count; ++i) {
+				if (isMSB2LSB) {
+					v = (v << 1) | getBit_internal();
+				} else {
+					v = (v >> 1) | (getBit_internal() << 31);
+				}
+			}
+
+			n -= count;
 		}
 
+		_inValue = (_inValue + nOrig) % valueBits;
+		_pos += nOrig;
+
+		if (!isMSB2LSB)
+			v >>= (32 - nOrig);
+
 		return v;
 	}
 


Commit: bd10131242210262eb23c5a62c223039cabf905c
    https://github.com/scummvm/scummvm/commit/bd10131242210262eb23c5a62c223039cabf905c
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2017-08-24T19:51:06+02:00

Commit Message:
Merge pull request #975 from wjp/bitstream

Rework and optimize Common::BitStream

Changed paths:
  R image/codecs/indeo/get_bits.cpp
    audio/decoders/qdm2.cpp
    common/bitstream.h
    common/huffman.cpp
    common/huffman.h
    engines/groovie/stuffit.cpp
    engines/macventure/container.h
    engines/macventure/image.cpp
    engines/macventure/image.h
    image/codecs/indeo/get_bits.h
    image/codecs/indeo/indeo.cpp
    image/codecs/indeo4.cpp
    image/codecs/indeo5.cpp
    image/codecs/svq1.cpp
    image/codecs/svq1.h
    image/module.mk
    test/common/bitstream.h
    video/bink_decoder.h
    video/psx_decoder.cpp
    video/psx_decoder.h
    video/smk_decoder.cpp
    video/smk_decoder.h







More information about the Scummvm-git-logs mailing list