有没有人有一个简单的解决方案来使用C ++解析Exp-Golomb代码?

时间:2010-03-02 13:49:55

标签: c++ parsing video h.264

尝试解码H.264视频流的SDP sprop-parameter-sets值并发现访问某些值将涉及解析Exp-Golomb编码数据,而我的方法包含base64解码的sprop-parameter-在一个字节数组中设置数据,我现在进行比特步行,但已经达到了Exp-Golomb编码数据的第一部分,并寻找合适的代码提取来解析这些值。

4 个答案:

答案 0 :(得分:5)

Exp.-Golomb代码的顺序是什么? 如果您需要解析H.264比特流(我的意思是传输层),您可以编写一个简单的函数来访问无限比特流中的场景位。比特从左到右编制索引。

inline u_dword get_bit(const u_byte * const base, u_dword offset)
{
    return ((*(base + (offset >> 0x3))) >> (0x7 - (offset & 0x7))) & 0x1;
}

该函数实现零范围的exp-Golomb码的解码(用于H.264)。

u_dword DecodeUGolomb(const u_byte * const base, u_dword * const offset)
{
    u_dword zeros = 0;

    // calculate zero bits. Will be optimized.
    while (0 == get_bit(base, (*offset)++)) zeros++;

    // insert first 1 bit
    u_dword info = 1 << zeros;

    for (s_dword i = zeros - 1; i >= 0; i--)
    {
        info |= get_bit(base, (*offset)++) << i;
    }

    return (info - 1);

}

u_dword表示无符号4字节整数。 u_byte表示无符号1字节整数。

注意每个NAL单元的第一个字节是一个带有禁止位,NAL引用和NAL类型的指定结构。

答案 1 :(得分:2)

我写了一个使用golomb代码的c ++ jpeg-ls压缩库。我不知道Exp-Golomb代码是否完全相同。该库是开源的,可以在http://charls.codeplex.com找到。我使用查找表来解码长度为&lt; = 8位的golomb码。如果您在寻找解决方法时遇到问题,请与我们联系。

答案 2 :(得分:1)

接受的答案不是正确的实施。它输出错误。根据

中的伪代码正确实现
  

&#34; Sec 9.1 Exp-Golomb代码的解析过程&#34;规范T-REC-H.264-201304

int32_t getBitByPos(unsigned char *buffer, int32_t pos) {
    return (buffer[pos/8] >> (8 - pos%8) & 0x01);
}


uint32_t decodeGolomb(unsigned char *byteStream, uint32_t *index) {
    uint32_t leadingZeroBits = -1;
    uint32_t codeNum = 0;
    uint32_t pos = *index;

    if (byteStream == NULL || pos == 0 ) {
        printf("Invalid input\n");
        return 0;
    }

    for (int32_t b = 0; !b; leadingZeroBits++)
        b = getBitByPos(byteStream, pos++);

    for (int32_t b = leadingZeroBits; b > 0; b--)
        codeNum = codeNum | (getBitByPos(byteStream, pos++) << (b - 1));

    *index = pos;
    return ((1 << leadingZeroBits) - 1 + codeNum);
}

答案 3 :(得分:0)

修改了一个从流中获取N位的函数;解析H.264 NAL

inline uint32_t get_bit(const uint8_t * const base, uint32_t offset)
{
    return ((*(base + (offset >> 0x3))) >> (0x7 - (offset & 0x7))) & 0x1;
}

inline uint32_t get_bits(const uint8_t * const base, uint32_t * const offset, uint8_t bits)
{
    uint32_t value = 0;
    for (int i = 0; i < bits; i++)
    {
      value = (value << 1) | (get_bit(base, (*offset)++) ? 1 : 0);
    }
    return value;
}

// This function implement decoding of exp-Golomb codes of zero range (used in H.264).

uint32_t DecodeUGolomb(const uint8_t * const base, uint32_t * const offset)
{
    uint32_t zeros = 0;

    // calculate zero bits. Will be optimized.
    while (0 == get_bit(base, (*offset)++)) zeros++;

    // insert first 1 bit
    uint32_t info = 1 << zeros;

    for (int32_t i = zeros - 1; i >= 0; i--)
    {
        info |= get_bit(base, (*offset)++) << i;
    }

    return (info - 1);
}