</pre><pre name="code" class="html">
</pre></div></div><div class="bct fc05 fc11 nbw-blog ztag" style="line-height:28px; word-wrap:break-word; margin:15px 0px; padding:5px 0px; overflow:hidden; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53"><ol style="font-size:14px; font-family:Arial; line-height:22px; margin-top:0.3em; margin-bottom:0px; margin-left:3.2em"><li style="margin-bottom:0.1em"><span style="font-family:SimSun; font-size:12px">将数字以<a target=_blank target="_blank" title="二进制" rel="nofollow" href="http://zh.wikipedia.org/wiki/%E4%BA%8C%E8%BF%9B%E5%88%B6" style="color:rgb(11,0,128); text-decoration:none">二进制</a>形式写出,去掉最低的k个比特位,之后加1</span></li><li style="margin-bottom:0.1em"><span style="font-family:SimSun; font-size:12px">计算留下的<a target=_blank target="_blank" title="比特数(页面不存在)" rel="nofollow" href="http://zh.wikipedia.org/w/index.php?title=%E6%AF%94%E7%89%B9%E6%95%B0&action=edit&redlink=1" style="color:rgb(165,88,88); text-decoration:none">比特数</a>,将此数减一,即是需要增加的前导零个数</span></li><li style="margin-bottom:0.1em"><span style="font-family:SimSun; font-size:12px">将第一步中去掉的最低k个比特位补回比特串尾部</span></li></ol><p style="margin-top:0.4em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px; font-family:Arial; line-height:22px"><span style="font-family:SimSun"><strong><span style="font-size:18px; color:#000099">一、k=0阶指数哥伦布码如下所示:</span></strong></span></p><p style="margin-top:0.4em; margin-bottom:0.5em; padding-top:0px; padding-bottom:0px; font-family:Arial; line-height:22px"><span style="font-family:SimSun"><strong><span style="font-size:18px; color:#000099"></span></strong></span></p><pre style="white-space:pre-wrap; word-wrap:break-word; font-size:14px; background-color:rgb(255,255,255); padding:1em; border:1px dashed rgb(47,111,171); line-height:1.3em"><span style="font-family:SimSun; font-size:12px"> 0 => 1 => 1 1 => 10 => 010 2 => 11 => 011 3 => 100 => 00100 4 => 101 => 00101 5 => 110 => 00110 6 => 111 => 00111 7 => 1000 => 0001000 8 => 1001 => 0001001</span>
具体过程如下所示
-----------------------------------------------------------------------------------------------------
比特串格式为“前缀1后缀”。
1)1后缀=codeNum+1,如codeNum = 3,则1后缀=4,即为100,后缀为00;
2)前缀与后缀的比特数相同,且前缀的各位比特为0,
下例为对不同codeNum进行的编码结果:
待编码数字 | 加1 | 加1后二进制表示 | 需补的前缀0 | 其它 |
0 | 1 | 00000001 | ||
1 | 2 | 10 | ||
2 | 3 | 11 | ||
3 | 4 | 100 | ||
4 | 5 | 101 | ||
5 | 6 | 110 | ||
6 | 7 | 111 | ||
7 | 8 | 1000 | ||
8 | 9 | 1001 | ||
9 | 10 | 1010 | ||
10 | 11 | 1011 | ||
11 | 12 | 1100 |
H.264的一个重要特色就是采用UVLC(Universal Variable Length coding, 统一可变长编码)编码,提高了编码效率。
UVLC编码中有一种编码方式就是指数哥伦布编码。程序实现该编码时需要计算编码长度len及其value,下面分别对无符号和有符号两种指数哥伦布编码进行分析。
设编码值为code_val。
static const int i_size0_255[256] =
{
1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
};
void bs_write( bs_t *s, int bit_len, int value);
1. ue(v)
{
int i_size = 0;
{
bs_write( s, 1, 1);
}
else
{
unsigned int tmp = ++code_val;
{
i_size += 16;
tmp >>= 16;
}
if( tmp >= 0x100 )
{
i_size += 8;
tmp >>= 8;
}
i_size += i_size0_255[tmp];
}
}
2. se(v)
{
ue_v( s, code_val <= 0 ? -code_val * 2 : code_val * 2 - 1);
}
如下示例:
二进制比特数
1001
001 1001
01 1010
010
000 1011