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