Jonathan Websdale Jonathan Websdale - 1 year ago 156
C++ Question

Does anyone have an easy solution to parsing Exp-Golomb codes using C++?

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.

Answer Source

Exp.-Golomb codes of what range ?? If it you need to parse H.264 bit stream (I mean transport layer) you can write a simple functions to make an access to scecified bits in the endless bit stream. Bits indexing from left to right.

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

This function implement decoding of exp-Golomb codes of zero range (used in 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 means unsigned 4 bytes integer. u_byte means unsigned 1 byte integer.

Note that first byte of each NAL Unit is a specified structure with forbidden bit, NAL reference, and NAL type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download