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;
}
Run Code Online (Sandbox Code Playgroud)
该函数实现零范围的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);
Run Code Online (Sandbox Code Playgroud)
}
u_dword表示无符号4字节整数.u_byte表示无符号1字节整数.
注意,每个NAL单元的第一个字节是具有禁止位,NAL引用和NAL类型的指定结构.
接受的答案不是正确的实现。它给出了错误的输出。根据伪代码正确实现
“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);
}
Run Code Online (Sandbox Code Playgroud)