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

📄 encputbits.c

📁 freescale i.mx31 BSP CE5.0全部源码
💻 C
字号:
/*------------------------------------------------------------------------------
--                                                                            --
--       This software is confidential and proprietary and may be used        --
--        only as expressly authorized by a licensing agreement from          --
--                                                                            --
--                            Hantro Products Oy.                             --
--                                                                            --
--      In the event of publication, the following notice is applicable:      --
--                                                                            --
--                   (C) COPYRIGHT 2004 HANTRO PRODUCTS OY                    --
--                            ALL RIGHTS RESERVED                             --
--                                                                            --
--          The entire notice above must be reproduced on all copies.         --
--                                                                            --
--------------------------------------------------------------------------------
--
--  Description : Encoder internal
--
-------------------------------------------------------------------------------*/


/*------------------------------------------------------------------------------

    Table of context

    1. Include headers
    2. External compiler flags
    3. Module defines
    4. Local function prototypes
    5. Functions
        5.1  EncPutBits
        5.2  EncNextStartCode
        5.3  EncNextByteAligned

------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------
    1. Include headers
------------------------------------------------------------------------------*/
#include "EncPutBits.h"
#include "EncTrace.h"

/*------------------------------------------------------------------------------
    2. External compiler flags
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
    3. Module defines
------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------------
    4. Local function prototypes
------------------------------------------------------------------------------*/
static bool_e BufferStatus(stream_s *stream);

/*------------------------------------------------------------------------------

	EncPutBits

	Write bits to stream. For example (value=2, number=4) write 0010 to the
	stream. Number of bits must be < 25, otherwise overflow occur.  Four
	bytes is maximum number of bytes to put stream and there should be at
	least 5 byte free space available because of byte buffer.
	stream[1] bits in byte buffer
	stream[0] byte buffer

	Input	stream	Pointer to the stream stucture
		value	Bit pattern
		number	Number of bits

------------------------------------------------------------------------------*/
void EncPutBits(stream_s *buffer, u32 value, u32 number)
{
	u32 bits;
	u32 byteBuffer;
	u8 *stream = buffer->stream;

	if (BufferStatus(buffer) != OK) {
		return;
	}

	/* Debug: value is too big */
	ASSERT(value < ((u32)1<<number));
	ASSERT(number < 25);

#ifdef TRACE_STREAM
	EncTraceStream(value, number);
#endif

	bits    = number + stream[1];
	value <<= (32 - bits);
	byteBuffer  = (((u32)stream[0])<<24) | value;

	while (bits > 7) {
		*stream  = (u8)(byteBuffer>>24);
		bits    -= 8;
		byteBuffer <<= 8;
		stream++;
		buffer->byteCnt++;
	}

	stream[0] = (u8)(byteBuffer>>24);
	stream[1] = (u8)bits;
	buffer->stream = stream;
	buffer->bitCnt += number;

	return;
}

/*------------------------------------------------------------------------------

	EncNextStartCode

	Function add codeword (0,01,011,...,01111111) needed for stuffing next
	byte aligned. Note that stream->stream[1] is bits in byte bufer.

	Input	stream	Pointer to the stream structure.

------------------------------------------------------------------------------*/
void EncNextStartCode(stream_s *stream)
{
	EncPutBits(stream, 127>>stream->stream[1], 8-stream->stream[1]);
	COMMENT("Next start Code");

	return;
}

/*------------------------------------------------------------------------------

	EncNextByteAligned

	Function add zero stuffing until next byte aligned if needed. Note that
	stream->stream[1] is bits in byte bufer.

	Input	stream	Pointer to the stream structure.

------------------------------------------------------------------------------*/
void EncNextByteAligned(stream_s *stream)
{
	if (stream->stream[1] > 0) {
		EncPutBits(stream, 0, 8-stream->stream[1]);
	}

	return;
}

/*------------------------------------------------------------------------------

	BufferStatus

	Check fullness of stream buffer.

	Input	stream	Pointer to the stream stucture.

	Return	OK	Buffer status is OK.
		NOK	Buffer overflow.

------------------------------------------------------------------------------*/
bool_e BufferStatus(stream_s *stream)
{
	if (stream->byteCnt+5 > stream->size) {
		stream->overflow = YES;
		COMMENT("Stream buffer is full");
		return NOK;
	}

	return OK;
}

/*------------------------------------------------------------------------------

	EncSetBuffer

	Set stream buffer.

	Input	buffer	Pointer to the stream_s structure.
		stream	Pointer to stream buffer.
		size	Size of stream buffer.

------------------------------------------------------------------------------*/
bool_e EncSetBuffer(stream_s *buffer, u8 *stream, u32 size)
{
	buffer->stream = stream;
	buffer->size = size;
	buffer->bitCnt = 0;
	buffer->byteCnt = 0;
	buffer->overflow = NO;

	if (BufferStatus(buffer) != OK) {
		return NOK;
	}
	buffer->stream[0] = 0;
	buffer->stream[1] = 0;

	return OK;
}

⌨️ 快捷键说明

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