APE 编码中用到了Range Code技术,类似于算数编码,前者是整数,后者基于0(含)到1(不含)之间小数。
具体请参考wiki百科的这个词条:
https://en.wikipedia.org/wiki/Range_encoding
例子代码:
#include <stdio.h>
int low = 0;
int range = 100000;
void Encode(int start, int size, int total)
{
int low_t=low,range_t=range;
// adjust the range based on the symbol interval
range /= total;
low += start * range;
range *= size;
low_t += range_t * start/total;
range_t = range_t*size/total;
printf("[%d,%d)\n",low_t,low_t+range_t);
}
void Run()
{
Encode(0, 6, 10); // A
Encode(0, 6, 10); // A
Encode(6, 2, 10); // B
Encode(0, 6, 10); // A
Encode(8, 2, 10); // <EOM>
}
int main()
{
Run();
}
执行结果为:
[0,60000)
[0,36000)
[21600,28800)
[21600,25920)
[25056,25920)
更进一步的变化和原理,请参考这个博客:http://yeye55blog.blog.163.com/blog/static/197241021201110613847165/
题目是“区间编码算法的分析与实现”,作者 yeye55