Blame libfreerdp/codec/rfx_bitstream.h

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * RemoteFX Codec Library - Bit Stream
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 Vic Lee
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifndef FREERDP_LIB_CODEC_RFX_BITSTREAM_H
Packit 1fb8d4
#define FREERDP_LIB_CODEC_RFX_BITSTREAM_H
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/codec/rfx.h>
Packit 1fb8d4
Packit 1fb8d4
struct _RFX_BITSTREAM
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE* buffer;
Packit 1fb8d4
	int nbytes;
Packit 1fb8d4
	int byte_pos;
Packit 1fb8d4
	int bits_left;
Packit 1fb8d4
};
Packit 1fb8d4
typedef struct _RFX_BITSTREAM RFX_BITSTREAM;
Packit 1fb8d4
Packit Service 5a9772
#define rfx_bitstream_attach(bs, _buffer, _nbytes) \
Packit Service 5a9772
	do                                             \
Packit Service 5a9772
	{                                              \
Packit Service 5a9772
		bs->buffer = (BYTE*)(_buffer);             \
Packit Service 5a9772
		bs->nbytes = (_nbytes);                    \
Packit Service 5a9772
		bs->byte_pos = 0;                          \
Packit Service 5a9772
		bs->bits_left = 8;                         \
Packit Service 5a9772
	} while (0)
Packit 1fb8d4
Packit Service 5a9772
#define rfx_bitstream_get_bits(bs, _nbits, _r)                                       \
Packit Service 5a9772
	do                                                                               \
Packit Service 5a9772
	{                                                                                \
Packit Service 5a9772
		int nbits = _nbits;                                                          \
Packit Service 5a9772
		int b;                                                                       \
Packit Service 5a9772
		UINT16 n = 0;                                                                \
Packit Service 5a9772
		while (bs->byte_pos < bs->nbytes && nbits > 0)                               \
Packit Service 5a9772
		{                                                                            \
Packit Service 5a9772
			b = nbits;                                                               \
Packit Service 5a9772
			if (b > bs->bits_left)                                                   \
Packit Service 5a9772
				b = bs->bits_left;                                                   \
Packit Service 5a9772
			if (n)                                                                   \
Packit Service 5a9772
				n <<= b;                                                             \
Packit Service 5a9772
			n |= (bs->buffer[bs->byte_pos] >> (bs->bits_left - b)) & ((1 << b) - 1); \
Packit Service 5a9772
			bs->bits_left -= b;                                                      \
Packit Service 5a9772
			nbits -= b;                                                              \
Packit Service 5a9772
			if (bs->bits_left == 0)                                                  \
Packit Service 5a9772
			{                                                                        \
Packit Service 5a9772
				bs->bits_left = 8;                                                   \
Packit Service 5a9772
				bs->byte_pos++;                                                      \
Packit Service 5a9772
			}                                                                        \
Packit Service 5a9772
		}                                                                            \
Packit Service 5a9772
		_r = n;                                                                      \
Packit Service 5a9772
	} while (0)
Packit 1fb8d4
Packit Service 5a9772
#define rfx_bitstream_put_bits(bs, _bits, _nbits)                                \
Packit Service 5a9772
	do                                                                           \
Packit Service 5a9772
	{                                                                            \
Packit Service 5a9772
		UINT16 bits = (_bits);                                                   \
Packit Service 5a9772
		int nbits = (_nbits);                                                    \
Packit Service 5a9772
		int b;                                                                   \
Packit Service 5a9772
		while (bs->byte_pos < bs->nbytes && nbits > 0)                           \
Packit Service 5a9772
		{                                                                        \
Packit Service 5a9772
			b = nbits;                                                           \
Packit Service 5a9772
			if (b > bs->bits_left)                                               \
Packit Service 5a9772
				b = bs->bits_left;                                               \
Packit Service 5a9772
			bs->buffer[bs->byte_pos] |= ((bits >> (nbits - b)) & ((1 << b) - 1)) \
Packit Service 5a9772
			                            << (bs->bits_left - b);                  \
Packit Service 5a9772
			bs->bits_left -= b;                                                  \
Packit Service 5a9772
			nbits -= b;                                                          \
Packit Service 5a9772
			if (bs->bits_left == 0)                                              \
Packit Service 5a9772
			{                                                                    \
Packit Service 5a9772
				bs->bits_left = 8;                                               \
Packit Service 5a9772
				bs->byte_pos++;                                                  \
Packit Service 5a9772
			}                                                                    \
Packit Service 5a9772
		}                                                                        \
Packit Service 5a9772
	} while (0)
Packit Service 5a9772
#define rfx_bitstream_flush(bs)                    \
Packit Service 5a9772
	do                                             \
Packit Service 5a9772
	{                                              \
Packit Service 5a9772
		if (bs->bits_left != 8)                    \
Packit Service 5a9772
		{                                          \
Packit Service 5a9772
			int _nbits = 8 - bs->bits_left;        \
Packit Service 5a9772
			rfx_bitstream_put_bits(bs, 0, _nbits); \
Packit Service 5a9772
		}                                          \
Packit Service 5a9772
	} while (0)
Packit 1fb8d4
Packit 1fb8d4
#define rfx_bitstream_eos(_bs) ((_bs)->byte_pos >= (_bs)->nbytes)
Packit Service 5a9772
#define rfx_bitstream_left(_bs)       \
Packit Service 5a9772
	((_bs)->byte_pos >= (_bs)->nbytes \
Packit Service 5a9772
	     ? 0                          \
Packit Service 5a9772
	     : ((_bs)->nbytes - (_bs)->byte_pos - 1) * 8 + (_bs)->bits_left)
Packit Service 5a9772
#define rfx_bitstream_get_processed_bytes(_bs) \
Packit Service 5a9772
	((_bs)->bits_left < 8 ? (_bs)->byte_pos + 1 : (_bs)->byte_pos)
Packit 1fb8d4
Packit 1fb8d4
#endif /* FREERDP_LIB_CODEC_RFX_BITSTREAM_H */