本节是基于指数哥伦布编码的理论知识
八、 熵编码算法(2):H.264中的熵编码基本方法、指数哥伦布编码
对一段数据{ 0xA6, 0x42, 0x98, 0xE2, 0x04, 0x8A }进行解析
可使用该代码对H.264编码数据中采用指数哥伦布编码的数据进行解析,例:SPS
代码实现
// ExpColum.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <assert.h>
#include "cstdlib"
typedef unsigned char UINT8;
/*
* 读取第bytePotion个字节的第bitPosition位的值
*/
static int get_bit_at_position(UINT8 *buf, UINT8 &bytePotion, UINT8 &bitPosition)
{
UINT8 mask = 0, val = 0;
mask = 1 << (7 - bitPosition);
val = ((buf[bytePotion] & mask) != 0);
if (++bitPosition > 7)
{
bytePotion++;
bitPosition = 0;
}
return val;
}
static int get_uev_code_num(UINT8 *buf, UINT8 &bytePotion, UINT8 &bitPosition)
{
assert(bitPosition < 8);
UINT8 val = 0, prefixZeroCount = 0;
int prefix = 0, surfix = 0;
while (true)
{
val = get_bit_at_position(buf, bytePotion, bitPosition);/* 返回每一位的值 */
if (val == 0)
{
prefixZeroCount++;
}
else
{
break;
}
}
prefix = (1 << prefixZeroCount) - 1; /* 计算前级的值 */
for (size_t i = 0; i < prefixZeroCount; i++)
{
val = get_bit_at_position(buf, bytePotion, bitPosition);
surfix += val * (1 << (prefixZeroCount - i - 1));
}
prefix += surfix;
return prefix;
}
int _tmain(int argc, _TCHAR* argv[])
{
UINT8 strArray[6] = { 0xA6, 0x42, 0x98, 0xE2, 0x04, 0x8A };
UINT8 bytePosition = 0, bitPosition = 0;
UINT8 dataLengthInBits = sizeof(strArray) * 8; /* 占据了多少位 */
int codeNum = 0;
while ((bytePosition * 8 + bitPosition) < dataLengthInBits) /* 判断不要超出了待解析的总位数 */
{
codeNum = get_uev_code_num(strArray, bytePosition, bitPosition);
printf("ExpoColumb codeNum = %d\n", codeNum);
}
system("pause");
return 0;
}