📄 bitwriter.h
字号:
#ifndef _BITSTREAM_H_
#define _BITSTREAM_H_
#include <assert.h>
#include "../global.h"
#include "encoder.h"
#define VO_START_CODE 0x8
#define VOL_START_CODE 0x12
#define VOP_START_CODE 0x1b6
void BitstreamVolHeader(Encoder * const pEnc);
void BitstreamVopHeader(Encoder * const pEnc, uint32_t TRD, uint32_t TRB);
// initialise bitstream structure
static void __inline BitstreamInit(BitWriter * const bs,
void * const pointer)
{
bs->reg = 0;
bs->pos = 0;
bs->buf = (uint32_t *)pointer;
bs->buf_ptr = bs->buf;
}
// reset bitstream
static void __inline BitstreamReset(BitWriter * const bs)
{
bs->pos = 0;
bs->reg = 0;
bs->buf_ptr = bs->buf;
}
// bitstream length (unit bits)
static uint32_t __inline BsPos(const BitWriter * const bs)
{
return 8 * ((uint32_t)bs->buf_ptr - (uint32_t)bs->buf) + bs->pos;
}
//
// flush the bitsream & return length (unit bytes)
// NOTE: assumes no futher bitsream functions will be called.
//
static uint32_t __inline BitstreamLength(BitWriter * const bs)
{
uint32_t len = (uint32_t) bs->buf_ptr - (uint32_t) bs->buf;
if (bs->pos)
{
uint32_t b;
b = BSWAP(bs->reg);
*bs->buf_ptr = b;
len += ((bs->pos + 7) >> 3);
}
return len;
}
// move bitstream position forward by n bits
static void __inline BitstreamSkip(BitWriter * const bs, const uint32_t bits)
{
bs->pos += bits;
if (bs->pos >= 32)
{
uint32_t b;
b = BSWAP(bs->reg);
*bs->buf_ptr++ = b;
bs->reg = 0;
bs->pos -= 32;
}
}
// write single bit to bitstream
static void __inline BitstreamPutBit(BitWriter * const bs,
const uint32_t bit)
{
if (bit)
bs->reg |= (0x80000000 >> bs->pos);
bs->pos ++;
if (bs->pos > 31)
{
uint32_t b;
b = BSWAP(bs->reg);
*bs->buf_ptr++ = b;
bs->reg = 0;
bs->pos -= 32;
}
}
// write n bits to bitstream
static void __inline BitstreamPutBits(BitWriter * const bs,
const uint32_t value,
const uint32_t size)
{
if(size)
{
int32_t shift = 32 - bs->pos - size;
assert(size<=32);
if (shift>=0) {
bs->reg |= (value << shift);
BitstreamSkip(bs, size);
}
else
{
/* a smarter way to do it */
int32_t room = 32-bs->pos;
int32_t first_shift = size-room;
assert(room>0);
assert(first_shift>0);
bs->reg |= (value >> first_shift);
BitstreamSkip(bs, room);
room = 32-first_shift;
bs->reg |= (value << room);
BitstreamSkip(bs, first_shift);
}
}
}
// pad bitstream to the next byte boundary
static void __inline BitstreamPad(BitWriter * const bs)
{
uint32_t remainder = bs->pos & 7;
uint8_t padding_bits = 0xff;
if (remainder)
{
if(remainder < 8)
{
BitstreamPutBit(bs, 0);
remainder ++;
}
if(remainder < 8)
{
padding_bits >>= remainder;
BitstreamPutBits(bs, padding_bits, 8-remainder);
}
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -