c++parsingvideoh.264

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.


Solution

  • Exp.-Golomb codes of what order?

    If you need to parse H.264 bitstream (I mean transport layer) you can write a simple function to make an access to specified 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 implements 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 the first byte of each NAL Unit is a specified structure with forbidden bit, NAL reference, and NAL type.