cost_enc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Cost tables for level and modes
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./cost_enc.h"
  14. //------------------------------------------------------------------------------
  15. // Level cost tables
  16. // For each given level, the following table gives the pattern of contexts to
  17. // use for coding it (in [][0]) as well as the bit value to use for each
  18. // context (in [][1]).
  19. const uint16_t VP8LevelCodes[MAX_VARIABLE_LEVEL][2] = {
  20. {0x001, 0x000}, {0x007, 0x001}, {0x00f, 0x005},
  21. {0x00f, 0x00d}, {0x033, 0x003}, {0x033, 0x003}, {0x033, 0x023},
  22. {0x033, 0x023}, {0x033, 0x023}, {0x033, 0x023}, {0x0d3, 0x013},
  23. {0x0d3, 0x013}, {0x0d3, 0x013}, {0x0d3, 0x013}, {0x0d3, 0x013},
  24. {0x0d3, 0x013}, {0x0d3, 0x013}, {0x0d3, 0x013}, {0x0d3, 0x093},
  25. {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093},
  26. {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093},
  27. {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093},
  28. {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093}, {0x153, 0x053},
  29. {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
  30. {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
  31. {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
  32. {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
  33. {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
  34. {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
  35. {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
  36. {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x153}
  37. };
  38. static int VariableLevelCost(int level, const uint8_t probas[NUM_PROBAS]) {
  39. int pattern = VP8LevelCodes[level - 1][0];
  40. int bits = VP8LevelCodes[level - 1][1];
  41. int cost = 0;
  42. int i;
  43. for (i = 2; pattern; ++i) {
  44. if (pattern & 1) {
  45. cost += VP8BitCost(bits & 1, probas[i]);
  46. }
  47. bits >>= 1;
  48. pattern >>= 1;
  49. }
  50. return cost;
  51. }
  52. //------------------------------------------------------------------------------
  53. // Pre-calc level costs once for all
  54. void VP8CalculateLevelCosts(VP8EncProba* const proba) {
  55. int ctype, band, ctx;
  56. if (!proba->dirty_) return; // nothing to do.
  57. for (ctype = 0; ctype < NUM_TYPES; ++ctype) {
  58. int n;
  59. for (band = 0; band < NUM_BANDS; ++band) {
  60. for (ctx = 0; ctx < NUM_CTX; ++ctx) {
  61. const uint8_t* const p = proba->coeffs_[ctype][band][ctx];
  62. uint16_t* const table = proba->level_cost_[ctype][band][ctx];
  63. const int cost0 = (ctx > 0) ? VP8BitCost(1, p[0]) : 0;
  64. const int cost_base = VP8BitCost(1, p[1]) + cost0;
  65. int v;
  66. table[0] = VP8BitCost(0, p[1]) + cost0;
  67. for (v = 1; v <= MAX_VARIABLE_LEVEL; ++v) {
  68. table[v] = cost_base + VariableLevelCost(v, p);
  69. }
  70. // Starting at level 67 and up, the variable part of the cost is
  71. // actually constant.
  72. }
  73. }
  74. for (n = 0; n < 16; ++n) { // replicate bands. We don't need to sentinel.
  75. for (ctx = 0; ctx < NUM_CTX; ++ctx) {
  76. proba->remapped_costs_[ctype][n][ctx] =
  77. proba->level_cost_[ctype][VP8EncBands[n]][ctx];
  78. }
  79. }
  80. }
  81. proba->dirty_ = 0;
  82. }
  83. //------------------------------------------------------------------------------
  84. // Mode cost tables.
  85. // These are the fixed probabilities (in the coding trees) turned into bit-cost
  86. // by calling VP8BitCost().
  87. const uint16_t VP8FixedCostsUV[4] = { 302, 984, 439, 642 };
  88. // note: these values include the fixed VP8BitCost(1, 145) mode selection cost.
  89. const uint16_t VP8FixedCostsI16[4] = { 663, 919, 872, 919 };
  90. const uint16_t VP8FixedCostsI4[NUM_BMODES][NUM_BMODES][NUM_BMODES] = {
  91. { { 40, 1151, 1723, 1874, 2103, 2019, 1628, 1777, 2226, 2137 },
  92. { 192, 469, 1296, 1308, 1849, 1794, 1781, 1703, 1713, 1522 },
  93. { 142, 910, 762, 1684, 1849, 1576, 1460, 1305, 1801, 1657 },
  94. { 559, 641, 1370, 421, 1182, 1569, 1612, 1725, 863, 1007 },
  95. { 299, 1059, 1256, 1108, 636, 1068, 1581, 1883, 869, 1142 },
  96. { 277, 1111, 707, 1362, 1089, 672, 1603, 1541, 1545, 1291 },
  97. { 214, 781, 1609, 1303, 1632, 2229, 726, 1560, 1713, 918 },
  98. { 152, 1037, 1046, 1759, 1983, 2174, 1358, 742, 1740, 1390 },
  99. { 512, 1046, 1420, 753, 752, 1297, 1486, 1613, 460, 1207 },
  100. { 424, 827, 1362, 719, 1462, 1202, 1199, 1476, 1199, 538 } },
  101. { { 240, 402, 1134, 1491, 1659, 1505, 1517, 1555, 1979, 2099 },
  102. { 467, 242, 960, 1232, 1714, 1620, 1834, 1570, 1676, 1391 },
  103. { 500, 455, 463, 1507, 1699, 1282, 1564, 982, 2114, 2114 },
  104. { 672, 643, 1372, 331, 1589, 1667, 1453, 1938, 996, 876 },
  105. { 458, 783, 1037, 911, 738, 968, 1165, 1518, 859, 1033 },
  106. { 504, 815, 504, 1139, 1219, 719, 1506, 1085, 1268, 1268 },
  107. { 333, 630, 1445, 1239, 1883, 3672, 799, 1548, 1865, 598 },
  108. { 399, 644, 746, 1342, 1856, 1350, 1493, 613, 1855, 1015 },
  109. { 622, 749, 1205, 608, 1066, 1408, 1290, 1406, 546, 971 },
  110. { 500, 753, 1041, 668, 1230, 1617, 1297, 1425, 1383, 523 } },
  111. { { 394, 553, 523, 1502, 1536, 981, 1608, 1142, 1666, 2181 },
  112. { 655, 430, 375, 1411, 1861, 1220, 1677, 1135, 1978, 1553 },
  113. { 690, 640, 245, 1954, 2070, 1194, 1528, 982, 1972, 2232 },
  114. { 559, 834, 741, 867, 1131, 980, 1225, 852, 1092, 784 },
  115. { 690, 875, 516, 959, 673, 894, 1056, 1190, 1528, 1126 },
  116. { 740, 951, 384, 1277, 1177, 492, 1579, 1155, 1846, 1513 },
  117. { 323, 775, 1062, 1776, 3062, 1274, 813, 1188, 1372, 655 },
  118. { 488, 971, 484, 1767, 1515, 1775, 1115, 503, 1539, 1461 },
  119. { 740, 1006, 998, 709, 851, 1230, 1337, 788, 741, 721 },
  120. { 522, 1073, 573, 1045, 1346, 887, 1046, 1146, 1203, 697 } },
  121. { { 105, 864, 1442, 1009, 1934, 1840, 1519, 1920, 1673, 1579 },
  122. { 534, 305, 1193, 683, 1388, 2164, 1802, 1894, 1264, 1170 },
  123. { 305, 518, 877, 1108, 1426, 3215, 1425, 1064, 1320, 1242 },
  124. { 683, 732, 1927, 257, 1493, 2048, 1858, 1552, 1055, 947 },
  125. { 394, 814, 1024, 660, 959, 1556, 1282, 1289, 893, 1047 },
  126. { 528, 615, 996, 940, 1201, 635, 1094, 2515, 803, 1358 },
  127. { 347, 614, 1609, 1187, 3133, 1345, 1007, 1339, 1017, 667 },
  128. { 218, 740, 878, 1605, 3650, 3650, 1345, 758, 1357, 1617 },
  129. { 672, 750, 1541, 558, 1257, 1599, 1870, 2135, 402, 1087 },
  130. { 592, 684, 1161, 430, 1092, 1497, 1475, 1489, 1095, 822 } },
  131. { { 228, 1056, 1059, 1368, 752, 982, 1512, 1518, 987, 1782 },
  132. { 494, 514, 818, 942, 965, 892, 1610, 1356, 1048, 1363 },
  133. { 512, 648, 591, 1042, 761, 991, 1196, 1454, 1309, 1463 },
  134. { 683, 749, 1043, 676, 841, 1396, 1133, 1138, 654, 939 },
  135. { 622, 1101, 1126, 994, 361, 1077, 1203, 1318, 877, 1219 },
  136. { 631, 1068, 857, 1650, 651, 477, 1650, 1419, 828, 1170 },
  137. { 555, 727, 1068, 1335, 3127, 1339, 820, 1331, 1077, 429 },
  138. { 504, 879, 624, 1398, 889, 889, 1392, 808, 891, 1406 },
  139. { 683, 1602, 1289, 977, 578, 983, 1280, 1708, 406, 1122 },
  140. { 399, 865, 1433, 1070, 1072, 764, 968, 1477, 1223, 678 } },
  141. { { 333, 760, 935, 1638, 1010, 529, 1646, 1410, 1472, 2219 },
  142. { 512, 494, 750, 1160, 1215, 610, 1870, 1868, 1628, 1169 },
  143. { 572, 646, 492, 1934, 1208, 603, 1580, 1099, 1398, 1995 },
  144. { 786, 789, 942, 581, 1018, 951, 1599, 1207, 731, 768 },
  145. { 690, 1015, 672, 1078, 582, 504, 1693, 1438, 1108, 2897 },
  146. { 768, 1267, 571, 2005, 1243, 244, 2881, 1380, 1786, 1453 },
  147. { 452, 899, 1293, 903, 1311, 3100, 465, 1311, 1319, 813 },
  148. { 394, 927, 942, 1103, 1358, 1104, 946, 593, 1363, 1109 },
  149. { 559, 1005, 1007, 1016, 658, 1173, 1021, 1164, 623, 1028 },
  150. { 564, 796, 632, 1005, 1014, 863, 2316, 1268, 938, 764 } },
  151. { { 266, 606, 1098, 1228, 1497, 1243, 948, 1030, 1734, 1461 },
  152. { 366, 585, 901, 1060, 1407, 1247, 876, 1134, 1620, 1054 },
  153. { 452, 565, 542, 1729, 1479, 1479, 1016, 886, 2938, 1150 },
  154. { 555, 1088, 1533, 950, 1354, 895, 834, 1019, 1021, 496 },
  155. { 704, 815, 1193, 971, 973, 640, 1217, 2214, 832, 578 },
  156. { 672, 1245, 579, 871, 875, 774, 872, 1273, 1027, 949 },
  157. { 296, 1134, 2050, 1784, 1636, 3425, 442, 1550, 2076, 722 },
  158. { 342, 982, 1259, 1846, 1848, 1848, 622, 568, 1847, 1052 },
  159. { 555, 1064, 1304, 828, 746, 1343, 1075, 1329, 1078, 494 },
  160. { 288, 1167, 1285, 1174, 1639, 1639, 833, 2254, 1304, 509 } },
  161. { { 342, 719, 767, 1866, 1757, 1270, 1246, 550, 1746, 2151 },
  162. { 483, 653, 694, 1509, 1459, 1410, 1218, 507, 1914, 1266 },
  163. { 488, 757, 447, 2979, 1813, 1268, 1654, 539, 1849, 2109 },
  164. { 522, 1097, 1085, 851, 1365, 1111, 851, 901, 961, 605 },
  165. { 709, 716, 841, 728, 736, 945, 941, 862, 2845, 1057 },
  166. { 512, 1323, 500, 1336, 1083, 681, 1342, 717, 1604, 1350 },
  167. { 452, 1155, 1372, 1900, 1501, 3290, 311, 944, 1919, 922 },
  168. { 403, 1520, 977, 2132, 1733, 3522, 1076, 276, 3335, 1547 },
  169. { 559, 1374, 1101, 615, 673, 2462, 974, 795, 984, 984 },
  170. { 547, 1122, 1062, 812, 1410, 951, 1140, 622, 1268, 651 } },
  171. { { 165, 982, 1235, 938, 1334, 1366, 1659, 1578, 964, 1612 },
  172. { 592, 422, 925, 847, 1139, 1112, 1387, 2036, 861, 1041 },
  173. { 403, 837, 732, 770, 941, 1658, 1250, 809, 1407, 1407 },
  174. { 896, 874, 1071, 381, 1568, 1722, 1437, 2192, 480, 1035 },
  175. { 640, 1098, 1012, 1032, 684, 1382, 1581, 2106, 416, 865 },
  176. { 559, 1005, 819, 914, 710, 770, 1418, 920, 838, 1435 },
  177. { 415, 1258, 1245, 870, 1278, 3067, 770, 1021, 1287, 522 },
  178. { 406, 990, 601, 1009, 1265, 1265, 1267, 759, 1017, 1277 },
  179. { 968, 1182, 1329, 788, 1032, 1292, 1705, 1714, 203, 1403 },
  180. { 732, 877, 1279, 471, 901, 1161, 1545, 1294, 755, 755 } },
  181. { { 111, 931, 1378, 1185, 1933, 1648, 1148, 1714, 1873, 1307 },
  182. { 406, 414, 1030, 1023, 1910, 1404, 1313, 1647, 1509, 793 },
  183. { 342, 640, 575, 1088, 1241, 1349, 1161, 1350, 1756, 1502 },
  184. { 559, 766, 1185, 357, 1682, 1428, 1329, 1897, 1219, 802 },
  185. { 473, 909, 1164, 771, 719, 2508, 1427, 1432, 722, 782 },
  186. { 342, 892, 785, 1145, 1150, 794, 1296, 1550, 973, 1057 },
  187. { 208, 1036, 1326, 1343, 1606, 3395, 815, 1455, 1618, 712 },
  188. { 228, 928, 890, 1046, 3499, 1711, 994, 829, 1720, 1318 },
  189. { 768, 724, 1058, 636, 991, 1075, 1319, 1324, 616, 825 },
  190. { 305, 1167, 1358, 899, 1587, 1587, 987, 1988, 1332, 501 } }
  191. };
  192. //------------------------------------------------------------------------------
  193. // helper functions for residuals struct VP8Residual.
  194. void VP8InitResidual(int first, int coeff_type,
  195. VP8Encoder* const enc, VP8Residual* const res) {
  196. res->coeff_type = coeff_type;
  197. res->prob = enc->proba_.coeffs_[coeff_type];
  198. res->stats = enc->proba_.stats_[coeff_type];
  199. res->costs = enc->proba_.remapped_costs_[coeff_type];
  200. res->first = first;
  201. }
  202. //------------------------------------------------------------------------------
  203. // Mode costs
  204. int VP8GetCostLuma4(VP8EncIterator* const it, const int16_t levels[16]) {
  205. const int x = (it->i4_ & 3), y = (it->i4_ >> 2);
  206. VP8Residual res;
  207. VP8Encoder* const enc = it->enc_;
  208. int R = 0;
  209. int ctx;
  210. VP8InitResidual(0, 3, enc, &res);
  211. ctx = it->top_nz_[x] + it->left_nz_[y];
  212. VP8SetResidualCoeffs(levels, &res);
  213. R += VP8GetResidualCost(ctx, &res);
  214. return R;
  215. }
  216. int VP8GetCostLuma16(VP8EncIterator* const it, const VP8ModeScore* const rd) {
  217. VP8Residual res;
  218. VP8Encoder* const enc = it->enc_;
  219. int x, y;
  220. int R = 0;
  221. VP8IteratorNzToBytes(it); // re-import the non-zero context
  222. // DC
  223. VP8InitResidual(0, 1, enc, &res);
  224. VP8SetResidualCoeffs(rd->y_dc_levels, &res);
  225. R += VP8GetResidualCost(it->top_nz_[8] + it->left_nz_[8], &res);
  226. // AC
  227. VP8InitResidual(1, 0, enc, &res);
  228. for (y = 0; y < 4; ++y) {
  229. for (x = 0; x < 4; ++x) {
  230. const int ctx = it->top_nz_[x] + it->left_nz_[y];
  231. VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res);
  232. R += VP8GetResidualCost(ctx, &res);
  233. it->top_nz_[x] = it->left_nz_[y] = (res.last >= 0);
  234. }
  235. }
  236. return R;
  237. }
  238. int VP8GetCostUV(VP8EncIterator* const it, const VP8ModeScore* const rd) {
  239. VP8Residual res;
  240. VP8Encoder* const enc = it->enc_;
  241. int ch, x, y;
  242. int R = 0;
  243. VP8IteratorNzToBytes(it); // re-import the non-zero context
  244. VP8InitResidual(0, 2, enc, &res);
  245. for (ch = 0; ch <= 2; ch += 2) {
  246. for (y = 0; y < 2; ++y) {
  247. for (x = 0; x < 2; ++x) {
  248. const int ctx = it->top_nz_[4 + ch + x] + it->left_nz_[4 + ch + y];
  249. VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res);
  250. R += VP8GetResidualCost(ctx, &res);
  251. it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] = (res.last >= 0);
  252. }
  253. }
  254. }
  255. return R;
  256. }
  257. //------------------------------------------------------------------------------
  258. // Recording of token probabilities.
  259. // We keep the table-free variant around for reference, in case.
  260. #define USE_LEVEL_CODE_TABLE
  261. // Simulate block coding, but only record statistics.
  262. // Note: no need to record the fixed probas.
  263. int VP8RecordCoeffs(int ctx, const VP8Residual* const res) {
  264. int n = res->first;
  265. // should be stats[VP8EncBands[n]], but it's equivalent for n=0 or 1
  266. proba_t* s = res->stats[n][ctx];
  267. if (res->last < 0) {
  268. VP8RecordStats(0, s + 0);
  269. return 0;
  270. }
  271. while (n <= res->last) {
  272. int v;
  273. VP8RecordStats(1, s + 0); // order of record doesn't matter
  274. while ((v = res->coeffs[n++]) == 0) {
  275. VP8RecordStats(0, s + 1);
  276. s = res->stats[VP8EncBands[n]][0];
  277. }
  278. VP8RecordStats(1, s + 1);
  279. if (!VP8RecordStats(2u < (unsigned int)(v + 1), s + 2)) { // v = -1 or 1
  280. s = res->stats[VP8EncBands[n]][1];
  281. } else {
  282. v = abs(v);
  283. #if !defined(USE_LEVEL_CODE_TABLE)
  284. if (!VP8RecordStats(v > 4, s + 3)) {
  285. if (VP8RecordStats(v != 2, s + 4))
  286. VP8RecordStats(v == 4, s + 5);
  287. } else if (!VP8RecordStats(v > 10, s + 6)) {
  288. VP8RecordStats(v > 6, s + 7);
  289. } else if (!VP8RecordStats((v >= 3 + (8 << 2)), s + 8)) {
  290. VP8RecordStats((v >= 3 + (8 << 1)), s + 9);
  291. } else {
  292. VP8RecordStats((v >= 3 + (8 << 3)), s + 10);
  293. }
  294. #else
  295. if (v > MAX_VARIABLE_LEVEL) {
  296. v = MAX_VARIABLE_LEVEL;
  297. }
  298. {
  299. const int bits = VP8LevelCodes[v - 1][1];
  300. int pattern = VP8LevelCodes[v - 1][0];
  301. int i;
  302. for (i = 0; (pattern >>= 1) != 0; ++i) {
  303. const int mask = 2 << i;
  304. if (pattern & 1) VP8RecordStats(!!(bits & mask), s + 3 + i);
  305. }
  306. }
  307. #endif
  308. s = res->stats[VP8EncBands[n]][2];
  309. }
  310. }
  311. if (n < 16) VP8RecordStats(0, s + 0);
  312. return 1;
  313. }
  314. //------------------------------------------------------------------------------