Commit 1f57aa38 authored by Alex Converse's avatar Alex Converse
Browse files

Raise the probability resolution for rANS tokens to 10-bits per symbol

Change-Id: I397b5a9371c85d1df401d261143c985623e9def6
parent 8f2fa041
......@@ -148,23 +148,25 @@ bool check_vpxbool(const PvVec &pv_vec, uint8_t *buf) {
return okay;
}
// TODO(aconverse): replace this with a more representative distribution from
// the codec.
const rans_sym rans_sym_tab[] = {
{16, 0}, {100, 16}, {70, 116}, {70, 186},
{16 * 4, 0 * 4}, {100 * 4, 16 * 4}, {70 * 4, 116 *4}, {70 * 4, 186 *4},
};
const int kDistinctSyms = sizeof(rans_sym_tab) / sizeof(rans_sym_tab[0]);
std::vector<int> ans_encode_build_vals(const rans_sym *tab, int iters) {
std::vector<int> p_to_sym;
int i = 0;
while (p_to_sym.size() < 256) {
while (p_to_sym.size() < rans_precision) {
p_to_sym.insert(p_to_sym.end(), tab[i].prob, i);
++i;
}
assert(p_to_sym.size() == 256);
assert(p_to_sym.size() == rans_precision);
std::vector<int> ret;
libvpx_test::ACMRandom gen(18543637);
for (int i = 0; i < iters; ++i) {
int sym = p_to_sym[gen.Rand8()];
int sym = p_to_sym[gen.Rand8() * 4];
ret.push_back(sym);
}
return ret;
......@@ -173,7 +175,7 @@ std::vector<int> ans_encode_build_vals(const rans_sym *tab, int iters) {
void rans_build_dec_tab(const struct rans_sym sym_tab[],
rans_dec_lut dec_tab) {
dec_tab[0] = 0;
for (int i = 1; dec_tab[i - 1] < ans_p8_precision; ++i) {
for (int i = 1; dec_tab[i - 1] < rans_precision; ++i) {
dec_tab[i] = dec_tab[i - 1] + sym_tab[i - 1].prob;
}
}
......@@ -229,10 +231,10 @@ void build_tree(vpx_tree_index *tree, int num_syms) {
* -sym2 -sym3
*/
void tab2tree(const rans_sym *tab, int tab_size, vpx_prob *treep) {
const unsigned basep = 256;
const unsigned basep = rans_precision;
unsigned pleft = basep;
for (int i = 0; i < tab_size - 1; ++i) {
unsigned prob = (tab[i].prob * basep + (basep / 2)) / pleft;
unsigned prob = (tab[i].prob * basep + basep * 2) / (pleft * 4);
assert(prob > 0 && prob < 256);
treep[i] = prob;
pleft -= tab[i].prob;
......
......@@ -58,7 +58,12 @@ struct AnsDecoder {
typedef uint8_t AnsP8;
#define ans_p8_precision 256u
#define ans_p8_shift 8
#define l_base (ans_p8_precision * 4) // l_base % precision must be 0
typedef uint16_t AnsP10;
#define ans_p10_precision 1024u
#define rans_precision ans_p10_precision
#define l_base (ans_p10_precision * 4) // l_base % precision must be 0
#define io_base 256
// Range I = { l_base, l_base + 1, ..., l_base * io_base - 1 }
......@@ -75,14 +80,17 @@ static INLINE int ans_write_end(struct AnsCoder *const ans) {
assert(ans->state < l_base * io_base);
state = ans->state - l_base;
if (state < (1 << 6)) {
ans->buf[ans->buf_offset] = (0 << 6) + state;
ans->buf[ans->buf_offset] = (0x00 << 6) + state;
return ans->buf_offset + 1;
} else if (state < (1 << 14)) {
mem_put_le16(ans->buf + ans->buf_offset, (1 << 14) + state);
mem_put_le16(ans->buf + ans->buf_offset, (0x01 << 14) + state);
return ans->buf_offset + 2;
} else {
mem_put_le24(ans->buf + ans->buf_offset, (1 << 23) + state);
} else if (state < (1 << 22)) {
mem_put_le24(ans->buf + ans->buf_offset, (0x02 << 22) + state);
return ans->buf_offset + 3;
} else {
assert(0 && "State is too large to be serialized");
return ans->buf_offset;
}
}
......@@ -189,7 +197,7 @@ static INLINE int rabs_asc_read(struct AnsDecoder *ans, AnsP8 p0) {
static INLINE void uabs_write(struct AnsCoder *ans, int val, AnsP8 p0) {
AnsP8 p = ans_p8_precision - p0;
const unsigned l_s = val ? p : p0;
if (ans->state >= l_base / ans_p8_precision * io_base * l_s) {
while (ans->state >= l_base / ans_p8_precision * io_base * l_s) {
ans->buf[ans->buf_offset++] = ans->state % io_base;
ans->state /= io_base;
}
......@@ -205,7 +213,7 @@ static INLINE int uabs_read(struct AnsDecoder *ans, AnsP8 p0) {
// unsigned int xp1;
unsigned xp, sp;
unsigned state = ans->state;
if (state < l_base && ans->buf_offset > 0) {
while (state < l_base && ans->buf_offset > 0) {
state = state * io_base + ans->buf[--ans->buf_offset];
}
sp = state * p;
......@@ -223,7 +231,7 @@ static INLINE int uabs_read(struct AnsDecoder *ans, AnsP8 p0) {
static INLINE int uabs_read_bit(struct AnsDecoder *ans) {
int s;
unsigned state = ans->state;
if (state < l_base && ans->buf_offset > 0) {
while (state < l_base && ans->buf_offset > 0) {
state = state * io_base + ans->buf[--ans->buf_offset];
}
s = (int)(state & 1);
......@@ -256,31 +264,31 @@ static INLINE int uabs_read_tree(struct AnsDecoder *ans,
}
struct rans_sym {
AnsP8 prob;
AnsP8 cum_prob; // not-inclusive
AnsP10 prob;
AnsP10 cum_prob; // not-inclusive
};
struct rans_dec_sym {
uint8_t val;
AnsP8 prob;
AnsP8 cum_prob; // not-inclusive
AnsP10 prob;
AnsP10 cum_prob; // not-inclusive
};
// This is now just a boring cdf. It starts with an explicit zero.
// TODO(aconverse): Remove starting zero.
typedef uint16_t rans_dec_lut[16];
static INLINE void rans_build_cdf_from_pdf(const AnsP8 token_probs[],
static INLINE void rans_build_cdf_from_pdf(const AnsP10 token_probs[],
rans_dec_lut cdf_tab) {
int i;
cdf_tab[0] = 0;
for (i = 1; cdf_tab[i - 1] < ans_p8_precision; ++i) {
for (i = 1; cdf_tab[i - 1] < rans_precision; ++i) {
cdf_tab[i] = cdf_tab[i - 1] + token_probs[i - 1];
}
assert(cdf_tab[i - 1] == ans_p8_precision);
assert(cdf_tab[i - 1] == rans_precision);
}
static INLINE int ans_find_largest(const AnsP8 *const pdf_tab,
static INLINE int ans_find_largest(const AnsP10 *const pdf_tab,
int num_syms) {
int largest_idx = -1;
int largest_p = -1;
......@@ -295,22 +303,22 @@ static INLINE int ans_find_largest(const AnsP8 *const pdf_tab,
return largest_idx;
}
static INLINE void rans_merge_prob_pdf(AnsP8 *const out_pdf,
const AnsP8 node_prob,
const AnsP8 *const src_pdf,
int in_syms) {
static INLINE void rans_merge_prob8_pdf(AnsP10 *const out_pdf,
const AnsP8 node_prob,
const AnsP10 *const src_pdf,
int in_syms) {
int i;
int adjustment = ans_p8_precision;
int adjustment = rans_precision;
const int round_fact = ans_p8_precision >> 1;
const AnsP8 p1 = ans_p8_precision - node_prob;
const int out_syms = in_syms + 1;
assert(src_pdf != out_pdf);
out_pdf[0] = node_prob;
adjustment -= node_prob;
out_pdf[0] = node_prob << (10 - 8);
adjustment -= out_pdf[0];
for (i = 0; i < in_syms; ++i) {
int p = (p1 * src_pdf[i] + round_fact) >> ans_p8_shift;
p = VPXMIN(p, (int)ans_p8_precision - in_syms);
p = VPXMIN(p, (int)rans_precision - in_syms);
p = VPXMAX(p, 1);
out_pdf[i + 1] = p;
adjustment -= p;
......@@ -332,20 +340,20 @@ static INLINE void rans_merge_prob_pdf(AnsP8 *const out_pdf,
// rANS with normalization
// sym->prob takes the place of l_s from the paper
// ans_p8_precision is m
// ans_p10_precision is m
static INLINE void rans_write(struct AnsCoder *ans,
const struct rans_sym *const sym) {
const AnsP8 p = sym->prob;
if (ans->state >= l_base / ans_p8_precision * io_base * p) {
const AnsP10 p = sym->prob;
while (ans->state >= l_base / rans_precision * io_base * p) {
ans->buf[ans->buf_offset++] = ans->state % io_base;
ans->state /= io_base;
}
ans->state =
(ans->state / p) * ans_p8_precision + ans->state % p + sym->cum_prob;
(ans->state / p) * rans_precision + ans->state % p + sym->cum_prob;
}
static INLINE void fetch_sym(struct rans_dec_sym *out, const rans_dec_lut cdf,
AnsP8 rem) {
AnsP10 rem) {
int i = 0;
// TODO(skal): if critical, could be a binary search.
// Or, better, an O(1) alias-table.
......@@ -353,8 +361,8 @@ static INLINE void fetch_sym(struct rans_dec_sym *out, const rans_dec_lut cdf,
++i;
}
out->val = i - 1;
out->prob = (AnsP8)(cdf[i] - cdf[i - 1]);
out->cum_prob = (AnsP8)cdf[i - 1];
out->prob = (AnsP10)(cdf[i] - cdf[i - 1]);
out->cum_prob = (AnsP10)cdf[i - 1];
}
static INLINE int rans_read(struct AnsDecoder *ans,
......@@ -362,11 +370,11 @@ static INLINE int rans_read(struct AnsDecoder *ans,
unsigned rem;
unsigned quo;
struct rans_dec_sym sym;
if (ans->state < l_base && ans->buf_offset > 0) {
while (ans->state < l_base && ans->buf_offset > 0) {
ans->state = ans->state * io_base + ans->buf[--ans->buf_offset];
}
quo = ans->state / ans_p8_precision;
rem = ans->state % ans_p8_precision;
quo = ans->state / rans_precision;
rem = ans->state % rans_precision;
fetch_sym(&sym, tab, rem);
ans->state = quo * sym.prob + rem - sym.cum_prob;
return sym.val;
......
This diff is collapsed.
......@@ -176,7 +176,7 @@ static INLINE const uint8_t *get_band_translate(TX_SIZE tx_size) {
extern const vpx_tree_index vp10_coef_con_tree[TREE_SIZE(ENTROPY_TOKENS)];
extern const vpx_prob vp10_pareto8_full[COEFF_PROB_MODELS][MODEL_NODES];
#if CONFIG_ANS
extern const vpx_prob
extern const AnsP10
vp10_pareto8_token_probs[COEFF_PROB_MODELS][ENTROPY_TOKENS - 2];
typedef rans_dec_lut coeff_cdf_model[REF_TYPES][COEF_BANDS][COEFF_CONTEXTS];
......
......@@ -29,8 +29,8 @@ extern "C" {
struct buffered_ans_symbol {
uint8_t method; // one of ANS_METHOD_UABS or ANS_METHOD_RANS
// TODO(aconverse): Should be possible to write this interms of start for ABS
AnsP8 val_start; // Boolean value for ABS, start in symbol cycle for Rans
AnsP8 prob; // Probability of this symbol
AnsP10 val_start; // Boolean value for ABS, start in symbol cycle for Rans
AnsP10 prob; // Probability of this symbol
};
struct BufAnsCoder {
......
......@@ -41,6 +41,97 @@ const uint16_t vp10_prob_cost[256] = {
48, 45, 42, 38, 35, 32, 29, 26, 23, 20, 18, 15,
12, 9, 6, 3};
#if CONFIG_ANS
// round(-log2(i/1024.) * (1 << VP9_PROB_COST_SHIFT))
static const uint16_t vp10_prob_cost10[1024] = {
5120, 5120, 4608, 4308, 4096, 3931, 3796, 3683, 3584, 3497, 3419, 3349,
3284, 3225, 3171, 3120, 3072, 3027, 2985, 2945, 2907, 2871, 2837, 2804,
2772, 2742, 2713, 2685, 2659, 2633, 2608, 2583, 2560, 2537, 2515, 2494,
2473, 2453, 2433, 2414, 2395, 2377, 2359, 2342, 2325, 2308, 2292, 2276,
2260, 2245, 2230, 2216, 2201, 2187, 2173, 2160, 2147, 2134, 2121, 2108,
2096, 2083, 2071, 2060, 2048, 2037, 2025, 2014, 2003, 1992, 1982, 1971,
1961, 1951, 1941, 1931, 1921, 1911, 1902, 1892, 1883, 1874, 1865, 1856,
1847, 1838, 1830, 1821, 1813, 1804, 1796, 1788, 1780, 1772, 1764, 1756,
1748, 1741, 1733, 1726, 1718, 1711, 1704, 1697, 1689, 1682, 1675, 1668,
1661, 1655, 1648, 1641, 1635, 1628, 1622, 1615, 1609, 1602, 1596, 1590,
1584, 1578, 1571, 1565, 1559, 1554, 1548, 1542, 1536, 1530, 1525, 1519,
1513, 1508, 1502, 1497, 1491, 1486, 1480, 1475, 1470, 1465, 1459, 1454,
1449, 1444, 1439, 1434, 1429, 1424, 1419, 1414, 1409, 1404, 1399, 1395,
1390, 1385, 1380, 1376, 1371, 1367, 1362, 1357, 1353, 1348, 1344, 1340,
1335, 1331, 1326, 1322, 1318, 1313, 1309, 1305, 1301, 1297, 1292, 1288,
1284, 1280, 1276, 1272, 1268, 1264, 1260, 1256, 1252, 1248, 1244, 1240,
1236, 1233, 1229, 1225, 1221, 1218, 1214, 1210, 1206, 1203, 1199, 1195,
1192, 1188, 1185, 1181, 1177, 1174, 1170, 1167, 1163, 1160, 1156, 1153,
1149, 1146, 1143, 1139, 1136, 1133, 1129, 1126, 1123, 1119, 1116, 1113,
1110, 1106, 1103, 1100, 1097, 1094, 1090, 1087, 1084, 1081, 1078, 1075,
1072, 1069, 1066, 1062, 1059, 1056, 1053, 1050, 1047, 1044, 1042, 1039,
1036, 1033, 1030, 1027, 1024, 1021, 1018, 1015, 1013, 1010, 1007, 1004,
1001, 998, 996, 993, 990, 987, 985, 982, 979, 977, 974, 971,
968, 966, 963, 960, 958, 955, 953, 950, 947, 945, 942, 940,
937, 934, 932, 929, 927, 924, 922, 919, 917, 914, 912, 909,
907, 904, 902, 899, 897, 895, 892, 890, 887, 885, 883, 880,
878, 876, 873, 871, 868, 866, 864, 861, 859, 857, 855, 852,
850, 848, 845, 843, 841, 839, 836, 834, 832, 830, 828, 825,
823, 821, 819, 817, 814, 812, 810, 808, 806, 804, 801, 799,
797, 795, 793, 791, 789, 787, 785, 783, 780, 778, 776, 774,
772, 770, 768, 766, 764, 762, 760, 758, 756, 754, 752, 750,
748, 746, 744, 742, 740, 738, 736, 734, 732, 730, 728, 726,
724, 723, 721, 719, 717, 715, 713, 711, 709, 707, 706, 704,
702, 700, 698, 696, 694, 693, 691, 689, 687, 685, 683, 682,
680, 678, 676, 674, 673, 671, 669, 667, 665, 664, 662, 660,
658, 657, 655, 653, 651, 650, 648, 646, 644, 643, 641, 639,
637, 636, 634, 632, 631, 629, 627, 626, 624, 622, 621, 619,
617, 616, 614, 612, 611, 609, 607, 606, 604, 602, 601, 599,
598, 596, 594, 593, 591, 590, 588, 586, 585, 583, 582, 580,
578, 577, 575, 574, 572, 571, 569, 567, 566, 564, 563, 561,
560, 558, 557, 555, 554, 552, 550, 549, 547, 546, 544, 543,
541, 540, 538, 537, 535, 534, 532, 531, 530, 528, 527, 525,
524, 522, 521, 519, 518, 516, 515, 513, 512, 511, 509, 508,
506, 505, 503, 502, 501, 499, 498, 496, 495, 493, 492, 491,
489, 488, 486, 485, 484, 482, 481, 480, 478, 477, 475, 474,
473, 471, 470, 469, 467, 466, 465, 463, 462, 460, 459, 458,
456, 455, 454, 452, 451, 450, 448, 447, 446, 444, 443, 442,
441, 439, 438, 437, 435, 434, 433, 431, 430, 429, 428, 426,
425, 424, 422, 421, 420, 419, 417, 416, 415, 414, 412, 411,
410, 409, 407, 406, 405, 404, 402, 401, 400, 399, 397, 396,
395, 394, 392, 391, 390, 389, 387, 386, 385, 384, 383, 381,
380, 379, 378, 377, 375, 374, 373, 372, 371, 369, 368, 367,
366, 365, 364, 362, 361, 360, 359, 358, 356, 355, 354, 353,
352, 351, 349, 348, 347, 346, 345, 344, 343, 341, 340, 339,
338, 337, 336, 335, 333, 332, 331, 330, 329, 328, 327, 326,
324, 323, 322, 321, 320, 319, 318, 317, 316, 314, 313, 312,
311, 310, 309, 308, 307, 306, 305, 303, 302, 301, 300, 299,
298, 297, 296, 295, 294, 293, 292, 291, 289, 288, 287, 286,
285, 284, 283, 282, 281, 280, 279, 278, 277, 276, 275, 274,
273, 272, 271, 269, 268, 267, 266, 265, 264, 263, 262, 261,
260, 259, 258, 257, 256, 255, 254, 253, 252, 251, 250, 249,
248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 237,
236, 235, 234, 233, 232, 231, 230, 229, 228, 227, 226, 225,
224, 223, 222, 221, 220, 219, 218, 217, 216, 215, 214, 213,
212, 212, 211, 210, 209, 208, 207, 206, 205, 204, 203, 202,
201, 200, 199, 198, 197, 196, 195, 194, 194, 193, 192, 191,
190, 189, 188, 187, 186, 185, 184, 183, 182, 181, 181, 180,
179, 178, 177, 176, 175, 174, 173, 172, 171, 170, 170, 169,
168, 167, 166, 165, 164, 163, 162, 161, 161, 160, 159, 158,
157, 156, 155, 154, 153, 152, 152, 151, 150, 149, 148, 147,
146, 145, 145, 144, 143, 142, 141, 140, 139, 138, 138, 137,
136, 135, 134, 133, 132, 132, 131, 130, 129, 128, 127, 126,
125, 125, 124, 123, 122, 121, 120, 120, 119, 118, 117, 116,
115, 114, 114, 113, 112, 111, 110, 109, 109, 108, 107, 106,
105, 104, 104, 103, 102, 101, 100, 99, 99, 98, 97, 96,
95, 95, 94, 93, 92, 91, 90, 90, 89, 88, 87, 86,
86, 85, 84, 83, 82, 82, 81, 80, 79, 78, 78, 77,
76, 75, 74, 74, 73, 72, 71, 70, 70, 69, 68, 67,
66, 66, 65, 64, 63, 62, 62, 61, 60, 59, 59, 58,
57, 56, 55, 55, 54, 53, 52, 52, 51, 50, 49, 48,
48, 47, 46, 45, 45, 44, 43, 42, 42, 41, 40, 39,
38, 38, 37, 36, 35, 35, 34, 33, 32, 32, 31, 30,
29, 29, 28, 27, 26, 26, 25, 24, 23, 23, 22, 21,
20, 20, 19, 18, 18, 17, 16, 15, 15, 14, 13, 12,
12, 11, 10, 9, 9, 8, 7, 7, 6, 5, 4, 4,
3, 2, 1, 1};
#endif // CONFIG_ANS
static void cost(int *costs, vpx_tree tree, const vpx_prob *probs,
int i, int c) {
const vpx_prob prob = probs[i / 2];
......@@ -68,7 +159,7 @@ void vp10_cost_tokens_ans(int *costs, const vpx_prob *tree_probs,
c_tree = vp10_cost_bit(tree_probs[0], 1);
for (i = ZERO_TOKEN; i <= CATEGORY6_TOKEN; ++i) {
const int p = (*token_cdf)[i + 1] - (*token_cdf)[i];
costs[i] = c_tree + vp10_cost_bit(p, 0);
costs[i] = c_tree + vp10_prob_cost10[p];
}
}
#endif // CONFIG_ANS
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment