⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitstream.c

📁 DM642的mpeg4编码
💻 C
字号:
#ifndef _BITSTREAM_H_
#define _BITSTREAM_H_

#    include <stdio.h>

#        define BITSWAP(a) \
	((a) = (((a) & 0xff) << 24)  | (((a) & 0xff00) << 8) | \
	 (((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff))

#define READ_MARKER()	StreamSkip(bs, 1)
#define WRITE_MARKER()	StreamPutBit(bs, 1)

typedef struct
{
	unsigned int buffer1;
	unsigned int buffer2;
	unsigned int buf;
	unsigned int position;
	unsigned int *end;
	unsigned int *begin;
	unsigned int length;
	unsigned int initpos;
}Stream;


void __inline StreamInitial(Stream * const bs,
						    void *const bitstream,
						    unsigned int length)
{
	unsigned int tmp;

	size_t position;
    unsigned int temp_stream = (unsigned int)bitstream;

	position = ((sizeof(unsigned int)-1) & (size_t)bitstream);
	temp_stream = temp_stream - position;
	bs->begin = bs->end = (unsigned int *) temp_stream;

	tmp = *bs->begin;
	BITSWAP(tmp);
	bs->buffer1 = tmp;

	tmp = *(bs->begin + 1);
	BITSWAP(tmp);
	bs->buffer2 = tmp;

	bs->buf = 0;
	bs->position = bs->initpos = position*8;
	bs->length = length;
}

unsigned int __inline GetStreamPosition(const Stream * const bs)
{
	return((unsigned int)(8*((unsigned int)bs->end - (unsigned int)bs->begin) + bs->position - bs->initpos));
}


unsigned int __inline ShowBits(Stream * const bs,
							   const unsigned int bits)
{
	int numberbit = (bits+bs->position) - 32;

	if (numberbit > 0) 
	{
		return ((bs->buffer1 & (0xffffffff >> bs->position)) << numberbit) | (bs->buffer2 >> (32 - numberbit));
	} 
	else
	{
		return (bs->buffer1 & (0xffffffff >> bs->position)) >> (32 - bs->position - bits);
	}
}


void __inline StreamSkip(Stream * const bs,
						const unsigned int bits)
{
	bs->position += bits;

	if (bs->position >= 32) 
	{
		unsigned int temp;

		bs->buffer1 = bs->buffer2;
		temp = *((unsigned int *) bs->end + 2);
		BITSWAP(temp);
		bs->buffer2 = temp;
		bs->end++;
		bs->position -= 32;
	}
}

unsigned int __inline NBitAlign(Stream *bs)
{
	unsigned int n = (32 - bs->position) % 8;
	return n == 0 ? 8 : n;
}


void __inline ForBit(Stream * const bs,
					 const unsigned int bits)
{
	bs->position += bits;

	if (bs->position >= 32) 
	{
		unsigned int b = bs->buf;
		BITSWAP(b);
		*bs->end++ = b;
		bs->buf = 0;
		bs->position -= 32;
	}
}


void __inline StreamPutBit(Stream * const bs,
						   const unsigned int bit)
{
	if (bit)
		bs->buf |= (0x80000000 >> bs->position);

	ForBit(bs, 1);
}


void __inline StreamPutBits(Stream * const bs,
							const unsigned int value,
				            const unsigned int size)
{
	unsigned int shift = 32 - bs->position - size; 

	if (shift <= 32) 
	{
		bs->buf |= value << shift;
		ForBit(bs, size);
	}
	else
	{
		unsigned int remainder;

		shift = size - (32 - bs->position);
		bs->buf |= value >> shift;
		ForBit(bs, size - shift);
		remainder = shift;

		shift = 32 - shift;

		bs->buf |= value << shift;
		ForBit(bs, remainder);
	}
}

unsigned int __inline GetStreamLength(Stream * const bs)
{
	unsigned int len = (unsigned int)((unsigned int)bs->end - (unsigned int)bs->begin);

	if (bs->position) 
	{
		unsigned int b = bs->buf;

		BITSWAP(b);

		*bs->end = b;

		len += (bs->position + 7) / 8;
	}

	if (bs->initpos)
		len -= bs->initpos/8;

	return len;
}

static const int stuffing_codes[8] =
{
	      
	0,		
	1,		
	3,		
	7,		
	0xf,	
	0x1f,	
	0x3f,   
	0x7f,	
};


void __inline StreamPad(Stream * const bs)
{
	int bits = 8 - (bs->position % 8);
	if (bits < 8)
		StreamPutBits(bs, stuffing_codes[bits - 1], bits);
}


void __inline PutBitsPad(Stream * const bs)
{
	int bits = 8 - (bs->position % 8);
	StreamPutBits(bs, stuffing_codes[bits - 1], bits);
}

#endif /* _BITSTREAM_H_ */






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -