5

Trying to decode the SDP sprop-parameter-sets values for an H.264 video stream and have found to access some of the values will involve parsing of Exp-Golomb encoded data and my method contains the base64 decoded sprop-parameter-sets data in a byte array which I now bit walking but have come up to the first part of Exp-Golomb encoded data and looking for a suitable code extract to parse these values.

4

4 回答 4

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 类型的指定结构。

于 2012-08-12T09:58:13.990 回答
3

接受的答案不是正确的实现。它给出了错误的输出。根据伪代码正确实现

“Sec 9.1 Exp-Golomb 代码的解析过程”规范 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);
}
于 2016-10-03T22:11:23.533 回答
2

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

于 2010-03-03T20:19:10.353 回答
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);
}
于 2014-04-30T22:00:58.517 回答