x265-bitstream.cpp

本文介绍了Bitstream类的构造及关键方法实现,包括数据的写入与对齐操作。构造函数初始化数据缓冲区,`push_back`方法用于将字节追加到缓冲区,并在必要时调整缓冲区大小。`write`方法支持任意位数的数据写入,而`writeByte`、`writeAlignOne`和`writeAlignZero`则分别用于写入字节、1和0进行对齐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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的码字

表1. 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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值