#define MIN_FIFO_SIZE 1000
Bitstream()
bitstream的构造函数
Bitstream::Bitstream()
{
m_fifo = X265_MALLOC(uint8_t, MIN_FIFO_SIZE);
m_byteAlloc = MIN_FIFO_SIZE;
resetBits();
}
Bitstream::push_back(uint8_t val)
把int值放到fifo中,如果空间不够,就重新分配;
void Bitstream::push_back(uint8_t val)
{
if (!m_fifo)
return;
if (m_byteOccupancy >= m_byteAlloc)
{
/** reallocate buffer with doubled size */
uint8_t *temp = X265_MALLOC(uint8_t, m_byteAlloc * 2);
if (temp)
{
memcpy(temp, m_fifo, m_byteOccupancy);
X265_FREE(m_fifo);
m_fifo = temp;
m_byteAlloc *= 2;
}
else
{
x265_log(NULL, X265_LOG_ERROR, "Unable to realloc bitstream buffer");
return;
}
}
m_fifo[m_byteOccupancy++] = val;
}
Bitstream::write(uint32_t val, uint32_t numBits)
参数val:要写入的值
参数numBits:要写入的位数
void Bitstream::write(uint32_t val, uint32_t numBits)
{
X265_CHECK(numBits <= 32, "numBits out of range\n");
X265_CHECK(numBits == 32 || ((val & (~0u << numBits)) == 0), "numBits & val out of range\n");
uint32_t totalPartialBits = m_partialByteBits + numBits;//总共要写入的位数
uint32_t nextPartialBits = totalPartialBits & 7;//计算末尾有多少位不足一个字节=8 bit
uint8_t nextHeldByte = val << (8 - nextPartialBits);//凑满整数个字节
uint32_t writeBytes = totalPartialBits >> 3;//计算要写入的字节数
if (writeBytes)
{
/* topword aligns m_partialByte with the msb of val */
uint32_t topword = (numBits - nextPartialBits) & ~7;//除去末尾的字节
#if USING_FTRAPV
uint32_t write_bits = (topword < 32 ? m_partialByte << topword : 0) | (val >> nextPartialBits);
#else
uint32_t write_bits = (m_partialByte << topword) | (val >> nextPartialBits);//m_partialByte是高位,以前遗留下的值
#endif
/*按字节写入*/
switch (writeBytes)
{
case 4: push_back(write_bits >> 24); // fall-through
case 3: push_back(write_bits >> 16); // fall-through
case 2: push_back(write_bits >> 8); // fall-through
case 1: push_back(write_bits);
}
m_partialByte = nextHeldByte;
m_partialByteBits = nextPartialBits;
}
else
{
m_partialByte |= nextHeldByte;
m_partialByteBits = nextPartialBits;
}
}
void Bitstream::writeByte(uint32_t val)
作用:写入一个字节的数据
void Bitstream::writeByte(uint32_t val)
{
// Only CABAC will call writeByte, the fifo must be byte aligned
X265_CHECK(!m_partialByteBits, "expecting m_partialByteBits = 0\n");
push_back(val);
}
void Bitstream::writeAlignOne()
作用:写入1来对齐
void Bitstream::writeAlignOne()
{
uint32_t numBits = (8 - m_partialByteBits) & 0x7;
write((1 << numBits) - 1, numBits);
}
void Bitstream::writeAlignZero()
作用:写入0来对齐
void Bitstream::writeAlignZero()
{
if (m_partialByteBits)
{
push_back(m_partialByte);
m_partialByte = 0;
m_partialByteBits = 0;
}
}
void Bitstream::writeByteAlignment()
{
write(1, 1);
writeAlignZero();
}
void SyntaxElementWriter::writeUvlc(uint32_t code)
作用:写入uvlc的码字
符号 | 码长 | 码字 |
0 | 1 | |
1 | 010 | |
2 | 011 | |
3 | 00100 | |
4 | 00101 | |
5 | 00110 | |
6 | 00111 |
void SyntaxElementWriter::writeUvlc(uint32_t code)
{
++code;
X265_CHECK(code, "writing -1 code, will cause infinite loop\n");
unsigned long idx;
CLZ(idx, code);
uint32_t length = (uint32_t)idx * 2 + 1;
// Take care of cases where length > 32
m_bitIf->write(0, length >> 1);
m_bitIf->write(code, (length + 1) >> 1);
}