vp9_tokenize.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <assert.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "vpx_mem/vpx_mem.h"
  15. #include "vp9/common/vp9_entropy.h"
  16. #include "vp9/common/vp9_pred_common.h"
  17. #include "vp9/common/vp9_scan.h"
  18. #include "vp9/common/vp9_seg_common.h"
  19. #include "vp9/encoder/vp9_cost.h"
  20. #include "vp9/encoder/vp9_encoder.h"
  21. #include "vp9/encoder/vp9_tokenize.h"
  22. static const TOKENVALUE dct_cat_lt_10_value_tokens[] = {
  23. {9, 63}, {9, 61}, {9, 59}, {9, 57}, {9, 55}, {9, 53}, {9, 51}, {9, 49},
  24. {9, 47}, {9, 45}, {9, 43}, {9, 41}, {9, 39}, {9, 37}, {9, 35}, {9, 33},
  25. {9, 31}, {9, 29}, {9, 27}, {9, 25}, {9, 23}, {9, 21}, {9, 19}, {9, 17},
  26. {9, 15}, {9, 13}, {9, 11}, {9, 9}, {9, 7}, {9, 5}, {9, 3}, {9, 1},
  27. {8, 31}, {8, 29}, {8, 27}, {8, 25}, {8, 23}, {8, 21},
  28. {8, 19}, {8, 17}, {8, 15}, {8, 13}, {8, 11}, {8, 9},
  29. {8, 7}, {8, 5}, {8, 3}, {8, 1},
  30. {7, 15}, {7, 13}, {7, 11}, {7, 9}, {7, 7}, {7, 5}, {7, 3}, {7, 1},
  31. {6, 7}, {6, 5}, {6, 3}, {6, 1}, {5, 3}, {5, 1},
  32. {4, 1}, {3, 1}, {2, 1}, {1, 1}, {0, 0},
  33. {1, 0}, {2, 0}, {3, 0}, {4, 0},
  34. {5, 0}, {5, 2}, {6, 0}, {6, 2}, {6, 4}, {6, 6},
  35. {7, 0}, {7, 2}, {7, 4}, {7, 6}, {7, 8}, {7, 10}, {7, 12}, {7, 14},
  36. {8, 0}, {8, 2}, {8, 4}, {8, 6}, {8, 8}, {8, 10}, {8, 12},
  37. {8, 14}, {8, 16}, {8, 18}, {8, 20}, {8, 22}, {8, 24},
  38. {8, 26}, {8, 28}, {8, 30}, {9, 0}, {9, 2},
  39. {9, 4}, {9, 6}, {9, 8}, {9, 10}, {9, 12}, {9, 14}, {9, 16},
  40. {9, 18}, {9, 20}, {9, 22}, {9, 24}, {9, 26}, {9, 28},
  41. {9, 30}, {9, 32}, {9, 34}, {9, 36}, {9, 38}, {9, 40},
  42. {9, 42}, {9, 44}, {9, 46}, {9, 48}, {9, 50}, {9, 52},
  43. {9, 54}, {9, 56}, {9, 58}, {9, 60}, {9, 62}
  44. };
  45. const TOKENVALUE *vp9_dct_cat_lt_10_value_tokens = dct_cat_lt_10_value_tokens +
  46. (sizeof(dct_cat_lt_10_value_tokens) / sizeof(*dct_cat_lt_10_value_tokens))
  47. / 2;
  48. // Array indices are identical to previously-existing CONTEXT_NODE indices
  49. const vp9_tree_index vp9_coef_tree[TREE_SIZE(ENTROPY_TOKENS)] = {
  50. -EOB_TOKEN, 2, // 0 = EOB
  51. -ZERO_TOKEN, 4, // 1 = ZERO
  52. -ONE_TOKEN, 6, // 2 = ONE
  53. 8, 12, // 3 = LOW_VAL
  54. -TWO_TOKEN, 10, // 4 = TWO
  55. -THREE_TOKEN, -FOUR_TOKEN, // 5 = THREE
  56. 14, 16, // 6 = HIGH_LOW
  57. -CATEGORY1_TOKEN, -CATEGORY2_TOKEN, // 7 = CAT_ONE
  58. 18, 20, // 8 = CAT_THREEFOUR
  59. -CATEGORY3_TOKEN, -CATEGORY4_TOKEN, // 9 = CAT_THREE
  60. -CATEGORY5_TOKEN, -CATEGORY6_TOKEN // 10 = CAT_FIVE
  61. };
  62. static const vp9_tree_index cat1[2] = {0, 0};
  63. static const vp9_tree_index cat2[4] = {2, 2, 0, 0};
  64. static const vp9_tree_index cat3[6] = {2, 2, 4, 4, 0, 0};
  65. static const vp9_tree_index cat4[8] = {2, 2, 4, 4, 6, 6, 0, 0};
  66. static const vp9_tree_index cat5[10] = {2, 2, 4, 4, 6, 6, 8, 8, 0, 0};
  67. static const vp9_tree_index cat6[28] = {2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12,
  68. 14, 14, 16, 16, 18, 18, 20, 20, 22, 22, 24, 24, 26, 26, 0, 0};
  69. static const int16_t zero_cost[] = {0};
  70. static const int16_t one_cost[] = {255, 257};
  71. static const int16_t two_cost[] = {255, 257};
  72. static const int16_t three_cost[] = {255, 257};
  73. static const int16_t four_cost[] = {255, 257};
  74. static const int16_t cat1_cost[] = {429, 431, 616, 618};
  75. static const int16_t cat2_cost[] = {624, 626, 727, 729, 848, 850, 951, 953};
  76. static const int16_t cat3_cost[] = {
  77. 820, 822, 893, 895, 940, 942, 1013, 1015, 1096, 1098, 1169, 1171, 1216, 1218,
  78. 1289, 1291
  79. };
  80. static const int16_t cat4_cost[] = {
  81. 1032, 1034, 1075, 1077, 1105, 1107, 1148, 1150, 1194, 1196, 1237, 1239,
  82. 1267, 1269, 1310, 1312, 1328, 1330, 1371, 1373, 1401, 1403, 1444, 1446,
  83. 1490, 1492, 1533, 1535, 1563, 1565, 1606, 1608
  84. };
  85. static const int16_t cat5_cost[] = {
  86. 1269, 1271, 1283, 1285, 1306, 1308, 1320,
  87. 1322, 1347, 1349, 1361, 1363, 1384, 1386, 1398, 1400, 1443, 1445, 1457,
  88. 1459, 1480, 1482, 1494, 1496, 1521, 1523, 1535, 1537, 1558, 1560, 1572,
  89. 1574, 1592, 1594, 1606, 1608, 1629, 1631, 1643, 1645, 1670, 1672, 1684,
  90. 1686, 1707, 1709, 1721, 1723, 1766, 1768, 1780, 1782, 1803, 1805, 1817,
  91. 1819, 1844, 1846, 1858, 1860, 1881, 1883, 1895, 1897
  92. };
  93. const int16_t vp9_cat6_low_cost[256] = {
  94. 1638, 1640, 1646, 1648, 1652, 1654, 1660, 1662,
  95. 1670, 1672, 1678, 1680, 1684, 1686, 1692, 1694, 1711, 1713, 1719, 1721,
  96. 1725, 1727, 1733, 1735, 1743, 1745, 1751, 1753, 1757, 1759, 1765, 1767,
  97. 1787, 1789, 1795, 1797, 1801, 1803, 1809, 1811, 1819, 1821, 1827, 1829,
  98. 1833, 1835, 1841, 1843, 1860, 1862, 1868, 1870, 1874, 1876, 1882, 1884,
  99. 1892, 1894, 1900, 1902, 1906, 1908, 1914, 1916, 1940, 1942, 1948, 1950,
  100. 1954, 1956, 1962, 1964, 1972, 1974, 1980, 1982, 1986, 1988, 1994, 1996,
  101. 2013, 2015, 2021, 2023, 2027, 2029, 2035, 2037, 2045, 2047, 2053, 2055,
  102. 2059, 2061, 2067, 2069, 2089, 2091, 2097, 2099, 2103, 2105, 2111, 2113,
  103. 2121, 2123, 2129, 2131, 2135, 2137, 2143, 2145, 2162, 2164, 2170, 2172,
  104. 2176, 2178, 2184, 2186, 2194, 2196, 2202, 2204, 2208, 2210, 2216, 2218,
  105. 2082, 2084, 2090, 2092, 2096, 2098, 2104, 2106, 2114, 2116, 2122, 2124,
  106. 2128, 2130, 2136, 2138, 2155, 2157, 2163, 2165, 2169, 2171, 2177, 2179,
  107. 2187, 2189, 2195, 2197, 2201, 2203, 2209, 2211, 2231, 2233, 2239, 2241,
  108. 2245, 2247, 2253, 2255, 2263, 2265, 2271, 2273, 2277, 2279, 2285, 2287,
  109. 2304, 2306, 2312, 2314, 2318, 2320, 2326, 2328, 2336, 2338, 2344, 2346,
  110. 2350, 2352, 2358, 2360, 2384, 2386, 2392, 2394, 2398, 2400, 2406, 2408,
  111. 2416, 2418, 2424, 2426, 2430, 2432, 2438, 2440, 2457, 2459, 2465, 2467,
  112. 2471, 2473, 2479, 2481, 2489, 2491, 2497, 2499, 2503, 2505, 2511, 2513,
  113. 2533, 2535, 2541, 2543, 2547, 2549, 2555, 2557, 2565, 2567, 2573, 2575,
  114. 2579, 2581, 2587, 2589, 2606, 2608, 2614, 2616, 2620, 2622, 2628, 2630,
  115. 2638, 2640, 2646, 2648, 2652, 2654, 2660, 2662
  116. };
  117. const int16_t vp9_cat6_high_cost[128] = {
  118. 72, 892, 1183, 2003, 1448, 2268, 2559, 3379,
  119. 1709, 2529, 2820, 3640, 3085, 3905, 4196, 5016, 2118, 2938, 3229, 4049,
  120. 3494, 4314, 4605, 5425, 3755, 4575, 4866, 5686, 5131, 5951, 6242, 7062,
  121. 2118, 2938, 3229, 4049, 3494, 4314, 4605, 5425, 3755, 4575, 4866, 5686,
  122. 5131, 5951, 6242, 7062, 4164, 4984, 5275, 6095, 5540, 6360, 6651, 7471,
  123. 5801, 6621, 6912, 7732, 7177, 7997, 8288, 9108, 2118, 2938, 3229, 4049,
  124. 3494, 4314, 4605, 5425, 3755, 4575, 4866, 5686, 5131, 5951, 6242, 7062,
  125. 4164, 4984, 5275, 6095, 5540, 6360, 6651, 7471, 5801, 6621, 6912, 7732,
  126. 7177, 7997, 8288, 9108, 4164, 4984, 5275, 6095, 5540, 6360, 6651, 7471,
  127. 5801, 6621, 6912, 7732, 7177, 7997, 8288, 9108, 6210, 7030, 7321, 8141,
  128. 7586, 8406, 8697, 9517, 7847, 8667, 8958, 9778, 9223, 10043, 10334, 11154
  129. };
  130. #if CONFIG_VP9_HIGHBITDEPTH
  131. const int16_t vp9_cat6_high10_high_cost[512] = {
  132. 74, 894, 1185, 2005, 1450, 2270, 2561,
  133. 3381, 1711, 2531, 2822, 3642, 3087, 3907, 4198, 5018, 2120, 2940, 3231,
  134. 4051, 3496, 4316, 4607, 5427, 3757, 4577, 4868, 5688, 5133, 5953, 6244,
  135. 7064, 2120, 2940, 3231, 4051, 3496, 4316, 4607, 5427, 3757, 4577, 4868,
  136. 5688, 5133, 5953, 6244, 7064, 4166, 4986, 5277, 6097, 5542, 6362, 6653,
  137. 7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290, 9110, 2120, 2940, 3231,
  138. 4051, 3496, 4316, 4607, 5427, 3757, 4577, 4868, 5688, 5133, 5953, 6244,
  139. 7064, 4166, 4986, 5277, 6097, 5542, 6362, 6653, 7473, 5803, 6623, 6914,
  140. 7734, 7179, 7999, 8290, 9110, 4166, 4986, 5277, 6097, 5542, 6362, 6653,
  141. 7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290, 9110, 6212, 7032, 7323,
  142. 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960, 9780, 9225, 10045, 10336,
  143. 11156, 2120, 2940, 3231, 4051, 3496, 4316, 4607, 5427, 3757, 4577, 4868,
  144. 5688, 5133, 5953, 6244, 7064, 4166, 4986, 5277, 6097, 5542, 6362, 6653,
  145. 7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290, 9110, 4166, 4986, 5277,
  146. 6097, 5542, 6362, 6653, 7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290,
  147. 9110, 6212, 7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960,
  148. 9780, 9225, 10045, 10336, 11156, 4166, 4986, 5277, 6097, 5542, 6362, 6653,
  149. 7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290, 9110, 6212, 7032, 7323,
  150. 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960, 9780, 9225, 10045, 10336,
  151. 11156, 6212, 7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960,
  152. 9780, 9225, 10045, 10336, 11156, 8258, 9078, 9369, 10189, 9634, 10454,
  153. 10745, 11565, 9895, 10715, 11006, 11826, 11271, 12091, 12382, 13202, 2120,
  154. 2940, 3231, 4051, 3496, 4316, 4607, 5427, 3757, 4577, 4868, 5688, 5133,
  155. 5953, 6244, 7064, 4166, 4986, 5277, 6097, 5542, 6362, 6653, 7473, 5803,
  156. 6623, 6914, 7734, 7179, 7999, 8290, 9110, 4166, 4986, 5277, 6097, 5542,
  157. 6362, 6653, 7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290, 9110, 6212,
  158. 7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960, 9780, 9225,
  159. 10045, 10336, 11156, 4166, 4986, 5277, 6097, 5542, 6362, 6653, 7473, 5803,
  160. 6623, 6914, 7734, 7179, 7999, 8290, 9110, 6212, 7032, 7323, 8143, 7588,
  161. 8408, 8699, 9519, 7849, 8669, 8960, 9780, 9225, 10045, 10336, 11156, 6212,
  162. 7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960, 9780, 9225,
  163. 10045, 10336, 11156, 8258, 9078, 9369, 10189, 9634, 10454, 10745, 11565,
  164. 9895, 10715, 11006, 11826, 11271, 12091, 12382, 13202, 4166, 4986, 5277,
  165. 6097, 5542, 6362, 6653, 7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290,
  166. 9110, 6212, 7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960,
  167. 9780, 9225, 10045, 10336, 11156, 6212, 7032, 7323, 8143, 7588, 8408, 8699,
  168. 9519, 7849, 8669, 8960, 9780, 9225, 10045, 10336, 11156, 8258, 9078, 9369,
  169. 10189, 9634, 10454, 10745, 11565, 9895, 10715, 11006, 11826, 11271, 12091,
  170. 12382, 13202, 6212, 7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669,
  171. 8960, 9780, 9225, 10045, 10336, 11156, 8258, 9078, 9369, 10189, 9634, 10454,
  172. 10745, 11565, 9895, 10715, 11006, 11826, 11271, 12091, 12382, 13202, 8258,
  173. 9078, 9369, 10189, 9634, 10454, 10745, 11565, 9895, 10715, 11006, 11826,
  174. 11271, 12091, 12382, 13202, 10304, 11124, 11415, 12235, 11680, 12500, 12791,
  175. 13611, 11941, 12761, 13052, 13872, 13317, 14137, 14428, 15248,
  176. };
  177. const int16_t vp9_cat6_high12_high_cost[2048] = {
  178. 76, 896, 1187, 2007, 1452, 2272, 2563,
  179. 3383, 1713, 2533, 2824, 3644, 3089, 3909, 4200, 5020, 2122, 2942, 3233,
  180. 4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870, 5690, 5135, 5955, 6246,
  181. 7066, 2122, 2942, 3233, 4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870,
  182. 5690, 5135, 5955, 6246, 7066, 4168, 4988, 5279, 6099, 5544, 6364, 6655,
  183. 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 2122, 2942, 3233,
  184. 4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870, 5690, 5135, 5955, 6246,
  185. 7066, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916,
  186. 7736, 7181, 8001, 8292, 9112, 4168, 4988, 5279, 6099, 5544, 6364, 6655,
  187. 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325,
  188. 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338,
  189. 11158, 2122, 2942, 3233, 4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870,
  190. 5690, 5135, 5955, 6246, 7066, 4168, 4988, 5279, 6099, 5544, 6364, 6655,
  191. 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 4168, 4988, 5279,
  192. 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292,
  193. 9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962,
  194. 9782, 9227, 10047, 10338, 11158, 4168, 4988, 5279, 6099, 5544, 6364, 6655,
  195. 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325,
  196. 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338,
  197. 11158, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962,
  198. 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456,
  199. 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 2122,
  200. 2942, 3233, 4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870, 5690, 5135,
  201. 5955, 6246, 7066, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805,
  202. 6625, 6916, 7736, 7181, 8001, 8292, 9112, 4168, 4988, 5279, 6099, 5544,
  203. 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214,
  204. 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227,
  205. 10047, 10338, 11158, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805,
  206. 6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145, 7590,
  207. 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 6214,
  208. 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227,
  209. 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567,
  210. 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 4168, 4988, 5279,
  211. 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292,
  212. 9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962,
  213. 9782, 9227, 10047, 10338, 11158, 6214, 7034, 7325, 8145, 7590, 8410, 8701,
  214. 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371,
  215. 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093,
  216. 12384, 13204, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671,
  217. 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456,
  218. 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 8260,
  219. 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828,
  220. 11273, 12093, 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793,
  221. 13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 2122, 2942,
  222. 3233, 4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870, 5690, 5135, 5955,
  223. 6246, 7066, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625,
  224. 6916, 7736, 7181, 8001, 8292, 9112, 4168, 4988, 5279, 6099, 5544, 6364,
  225. 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034,
  226. 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047,
  227. 10338, 11158, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625,
  228. 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145, 7590, 8410,
  229. 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 6214, 7034,
  230. 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047,
  231. 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897,
  232. 10717, 11008, 11828, 11273, 12093, 12384, 13204, 4168, 4988, 5279, 6099,
  233. 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112,
  234. 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782,
  235. 9227, 10047, 10338, 11158, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521,
  236. 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191,
  237. 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384,
  238. 13204, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962,
  239. 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456,
  240. 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 8260,
  241. 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828,
  242. 11273, 12093, 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793,
  243. 13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 4168, 4988,
  244. 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001,
  245. 8292, 9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671,
  246. 8962, 9782, 9227, 10047, 10338, 11158, 6214, 7034, 7325, 8145, 7590, 8410,
  247. 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080,
  248. 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273,
  249. 12093, 12384, 13204, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851,
  250. 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636,
  251. 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204,
  252. 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008,
  253. 11828, 11273, 12093, 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502,
  254. 12793, 13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 6214,
  255. 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227,
  256. 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567,
  257. 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 8260, 9080, 9371,
  258. 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093,
  259. 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943,
  260. 12763, 13054, 13874, 13319, 14139, 14430, 15250, 8260, 9080, 9371, 10191,
  261. 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384,
  262. 13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763,
  263. 13054, 13874, 13319, 14139, 14430, 15250, 10306, 11126, 11417, 12237, 11682,
  264. 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250,
  265. 12352, 13172, 13463, 14283, 13728, 14548, 14839, 15659, 13989, 14809, 15100,
  266. 15920, 15365, 16185, 16476, 17296, 2122, 2942, 3233, 4053, 3498, 4318, 4609,
  267. 5429, 3759, 4579, 4870, 5690, 5135, 5955, 6246, 7066, 4168, 4988, 5279,
  268. 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292,
  269. 9112, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916,
  270. 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701,
  271. 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 4168, 4988, 5279,
  272. 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292,
  273. 9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962,
  274. 9782, 9227, 10047, 10338, 11158, 6214, 7034, 7325, 8145, 7590, 8410, 8701,
  275. 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371,
  276. 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093,
  277. 12384, 13204, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625,
  278. 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145, 7590, 8410,
  279. 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 6214, 7034,
  280. 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047,
  281. 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897,
  282. 10717, 11008, 11828, 11273, 12093, 12384, 13204, 6214, 7034, 7325, 8145,
  283. 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158,
  284. 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008,
  285. 11828, 11273, 12093, 12384, 13204, 8260, 9080, 9371, 10191, 9636, 10456,
  286. 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306,
  287. 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874,
  288. 13319, 14139, 14430, 15250, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475,
  289. 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145,
  290. 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158,
  291. 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782,
  292. 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747,
  293. 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 6214, 7034,
  294. 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047,
  295. 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897,
  296. 10717, 11008, 11828, 11273, 12093, 12384, 13204, 8260, 9080, 9371, 10191,
  297. 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384,
  298. 13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763,
  299. 13054, 13874, 13319, 14139, 14430, 15250, 6214, 7034, 7325, 8145, 7590,
  300. 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260,
  301. 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828,
  302. 11273, 12093, 12384, 13204, 8260, 9080, 9371, 10191, 9636, 10456, 10747,
  303. 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306, 11126,
  304. 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319,
  305. 14139, 14430, 15250, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567,
  306. 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306, 11126, 11417,
  307. 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319, 14139,
  308. 14430, 15250, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943,
  309. 12763, 13054, 13874, 13319, 14139, 14430, 15250, 12352, 13172, 13463, 14283,
  310. 13728, 14548, 14839, 15659, 13989, 14809, 15100, 15920, 15365, 16185, 16476,
  311. 17296, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916,
  312. 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701,
  313. 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 6214, 7034, 7325,
  314. 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338,
  315. 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717,
  316. 11008, 11828, 11273, 12093, 12384, 13204, 6214, 7034, 7325, 8145, 7590,
  317. 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260,
  318. 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828,
  319. 11273, 12093, 12384, 13204, 8260, 9080, 9371, 10191, 9636, 10456, 10747,
  320. 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306, 11126,
  321. 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319,
  322. 14139, 14430, 15250, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851,
  323. 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636,
  324. 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204,
  325. 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008,
  326. 11828, 11273, 12093, 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502,
  327. 12793, 13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 8260,
  328. 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828,
  329. 11273, 12093, 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793,
  330. 13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 10306, 11126,
  331. 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319,
  332. 14139, 14430, 15250, 12352, 13172, 13463, 14283, 13728, 14548, 14839, 15659,
  333. 13989, 14809, 15100, 15920, 15365, 16185, 16476, 17296, 6214, 7034, 7325,
  334. 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338,
  335. 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717,
  336. 11008, 11828, 11273, 12093, 12384, 13204, 8260, 9080, 9371, 10191, 9636,
  337. 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204,
  338. 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054,
  339. 13874, 13319, 14139, 14430, 15250, 8260, 9080, 9371, 10191, 9636, 10456,
  340. 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306,
  341. 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874,
  342. 13319, 14139, 14430, 15250, 10306, 11126, 11417, 12237, 11682, 12502, 12793,
  343. 13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 12352, 13172,
  344. 13463, 14283, 13728, 14548, 14839, 15659, 13989, 14809, 15100, 15920, 15365,
  345. 16185, 16476, 17296, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567,
  346. 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306, 11126, 11417,
  347. 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319, 14139,
  348. 14430, 15250, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943,
  349. 12763, 13054, 13874, 13319, 14139, 14430, 15250, 12352, 13172, 13463, 14283,
  350. 13728, 14548, 14839, 15659, 13989, 14809, 15100, 15920, 15365, 16185, 16476,
  351. 17296, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763,
  352. 13054, 13874, 13319, 14139, 14430, 15250, 12352, 13172, 13463, 14283, 13728,
  353. 14548, 14839, 15659, 13989, 14809, 15100, 15920, 15365, 16185, 16476, 17296,
  354. 12352, 13172, 13463, 14283, 13728, 14548, 14839, 15659, 13989, 14809, 15100,
  355. 15920, 15365, 16185, 16476, 17296, 14398, 15218, 15509, 16329, 15774, 16594,
  356. 16885, 17705, 16035, 16855, 17146, 17966, 17411, 18231, 18522, 19342
  357. };
  358. #endif
  359. #if CONFIG_VP9_HIGHBITDEPTH
  360. static const vp9_tree_index cat1_high10[2] = {0, 0};
  361. static const vp9_tree_index cat2_high10[4] = {2, 2, 0, 0};
  362. static const vp9_tree_index cat3_high10[6] = {2, 2, 4, 4, 0, 0};
  363. static const vp9_tree_index cat4_high10[8] = {2, 2, 4, 4, 6, 6, 0, 0};
  364. static const vp9_tree_index cat5_high10[10] = {2, 2, 4, 4, 6, 6, 8, 8, 0, 0};
  365. static const vp9_tree_index cat6_high10[32] = {2, 2, 4, 4, 6, 6, 8, 8, 10, 10,
  366. 12, 12, 14, 14, 16, 16, 18, 18, 20, 20, 22, 22, 24, 24, 26, 26, 28, 28,
  367. 30, 30, 0, 0};
  368. static const vp9_tree_index cat1_high12[2] = {0, 0};
  369. static const vp9_tree_index cat2_high12[4] = {2, 2, 0, 0};
  370. static const vp9_tree_index cat3_high12[6] = {2, 2, 4, 4, 0, 0};
  371. static const vp9_tree_index cat4_high12[8] = {2, 2, 4, 4, 6, 6, 0, 0};
  372. static const vp9_tree_index cat5_high12[10] = {2, 2, 4, 4, 6, 6, 8, 8, 0, 0};
  373. static const vp9_tree_index cat6_high12[36] = {2, 2, 4, 4, 6, 6, 8, 8, 10, 10,
  374. 12, 12, 14, 14, 16, 16, 18, 18, 20, 20, 22, 22, 24, 24, 26, 26, 28, 28,
  375. 30, 30, 32, 32, 34, 34, 0, 0};
  376. #endif
  377. const vp9_extra_bit vp9_extra_bits[ENTROPY_TOKENS] = {
  378. {0, 0, 0, 0, zero_cost}, // ZERO_TOKEN
  379. {0, 0, 0, 1, one_cost}, // ONE_TOKEN
  380. {0, 0, 0, 2, two_cost}, // TWO_TOKEN
  381. {0, 0, 0, 3, three_cost}, // THREE_TOKEN
  382. {0, 0, 0, 4, four_cost}, // FOUR_TOKEN
  383. {cat1, vp9_cat1_prob, 1, CAT1_MIN_VAL, cat1_cost}, // CATEGORY1_TOKEN
  384. {cat2, vp9_cat2_prob, 2, CAT2_MIN_VAL, cat2_cost}, // CATEGORY2_TOKEN
  385. {cat3, vp9_cat3_prob, 3, CAT3_MIN_VAL, cat3_cost}, // CATEGORY3_TOKEN
  386. {cat4, vp9_cat4_prob, 4, CAT4_MIN_VAL, cat4_cost}, // CATEGORY4_TOKEN
  387. {cat5, vp9_cat5_prob, 5, CAT5_MIN_VAL, cat5_cost}, // CATEGORY5_TOKEN
  388. {cat6, vp9_cat6_prob, 14, CAT6_MIN_VAL, 0}, // CATEGORY6_TOKEN
  389. {0, 0, 0, 0, zero_cost} // EOB_TOKEN
  390. };
  391. #if CONFIG_VP9_HIGHBITDEPTH
  392. const vp9_extra_bit vp9_extra_bits_high10[ENTROPY_TOKENS] = {
  393. {0, 0, 0, 0, zero_cost}, // ZERO
  394. {0, 0, 0, 1, one_cost}, // ONE
  395. {0, 0, 0, 2, two_cost}, // TWO
  396. {0, 0, 0, 3, three_cost}, // THREE
  397. {0, 0, 0, 4, four_cost}, // FOUR
  398. {cat1_high10, vp9_cat1_prob_high10, 1, CAT1_MIN_VAL, cat1_cost}, // CAT1
  399. {cat2_high10, vp9_cat2_prob_high10, 2, CAT2_MIN_VAL, cat2_cost}, // CAT2
  400. {cat3_high10, vp9_cat3_prob_high10, 3, CAT3_MIN_VAL, cat3_cost}, // CAT3
  401. {cat4_high10, vp9_cat4_prob_high10, 4, CAT4_MIN_VAL, cat4_cost}, // CAT4
  402. {cat5_high10, vp9_cat5_prob_high10, 5, CAT5_MIN_VAL, cat5_cost}, // CAT5
  403. {cat6_high10, vp9_cat6_prob_high10, 16, CAT6_MIN_VAL, 0}, // CAT6
  404. {0, 0, 0, 0, zero_cost} // EOB
  405. };
  406. const vp9_extra_bit vp9_extra_bits_high12[ENTROPY_TOKENS] = {
  407. {0, 0, 0, 0, zero_cost}, // ZERO
  408. {0, 0, 0, 1, one_cost}, // ONE
  409. {0, 0, 0, 2, two_cost}, // TWO
  410. {0, 0, 0, 3, three_cost}, // THREE
  411. {0, 0, 0, 4, four_cost}, // FOUR
  412. {cat1_high12, vp9_cat1_prob_high12, 1, CAT1_MIN_VAL, cat1_cost}, // CAT1
  413. {cat2_high12, vp9_cat2_prob_high12, 2, CAT2_MIN_VAL, cat2_cost}, // CAT2
  414. {cat3_high12, vp9_cat3_prob_high12, 3, CAT3_MIN_VAL, cat3_cost}, // CAT3
  415. {cat4_high12, vp9_cat4_prob_high12, 4, CAT4_MIN_VAL, cat4_cost}, // CAT4
  416. {cat5_high12, vp9_cat5_prob_high12, 5, CAT5_MIN_VAL, cat5_cost}, // CAT5
  417. {cat6_high12, vp9_cat6_prob_high12, 18, CAT6_MIN_VAL, 0}, // CAT6
  418. {0, 0, 0, 0, zero_cost} // EOB
  419. };
  420. #endif
  421. const struct vp9_token vp9_coef_encodings[ENTROPY_TOKENS] = {
  422. {2, 2}, {6, 3}, {28, 5}, {58, 6}, {59, 6}, {60, 6}, {61, 6}, {124, 7},
  423. {125, 7}, {126, 7}, {127, 7}, {0, 1}
  424. };
  425. struct tokenize_b_args {
  426. VP9_COMP *cpi;
  427. ThreadData *td;
  428. TOKENEXTRA **tp;
  429. };
  430. static void set_entropy_context_b(int plane, int block, BLOCK_SIZE plane_bsize,
  431. TX_SIZE tx_size, void *arg) {
  432. struct tokenize_b_args* const args = arg;
  433. ThreadData *const td = args->td;
  434. MACROBLOCK *const x = &td->mb;
  435. MACROBLOCKD *const xd = &x->e_mbd;
  436. struct macroblock_plane *p = &x->plane[plane];
  437. struct macroblockd_plane *pd = &xd->plane[plane];
  438. int aoff, loff;
  439. txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);
  440. vp9_set_contexts(xd, pd, plane_bsize, tx_size, p->eobs[block] > 0,
  441. aoff, loff);
  442. }
  443. static INLINE void add_token(TOKENEXTRA **t, const vp9_prob *context_tree,
  444. int32_t extra, uint8_t token,
  445. uint8_t skip_eob_node,
  446. unsigned int *counts) {
  447. (*t)->token = token;
  448. (*t)->extra = extra;
  449. (*t)->context_tree = context_tree;
  450. (*t)->skip_eob_node = skip_eob_node;
  451. (*t)++;
  452. ++counts[token];
  453. }
  454. static INLINE void add_token_no_extra(TOKENEXTRA **t,
  455. const vp9_prob *context_tree,
  456. uint8_t token,
  457. uint8_t skip_eob_node,
  458. unsigned int *counts) {
  459. (*t)->token = token;
  460. (*t)->context_tree = context_tree;
  461. (*t)->skip_eob_node = skip_eob_node;
  462. (*t)++;
  463. ++counts[token];
  464. }
  465. static INLINE int get_tx_eob(const struct segmentation *seg, int segment_id,
  466. TX_SIZE tx_size) {
  467. const int eob_max = 16 << (tx_size << 1);
  468. return vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP) ? 0 : eob_max;
  469. }
  470. static void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize,
  471. TX_SIZE tx_size, void *arg) {
  472. struct tokenize_b_args* const args = arg;
  473. VP9_COMP *cpi = args->cpi;
  474. ThreadData *const td = args->td;
  475. MACROBLOCK *const x = &td->mb;
  476. MACROBLOCKD *const xd = &x->e_mbd;
  477. TOKENEXTRA **tp = args->tp;
  478. uint8_t token_cache[32 * 32];
  479. struct macroblock_plane *p = &x->plane[plane];
  480. struct macroblockd_plane *pd = &xd->plane[plane];
  481. MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
  482. int pt; /* near block/prev token context index */
  483. int c;
  484. TOKENEXTRA *t = *tp; /* store tokens starting here */
  485. int eob = p->eobs[block];
  486. const PLANE_TYPE type = pd->plane_type;
  487. const tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  488. const int segment_id = mbmi->segment_id;
  489. const int16_t *scan, *nb;
  490. const scan_order *so;
  491. const int ref = is_inter_block(mbmi);
  492. unsigned int (*const counts)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
  493. td->rd_counts.coef_counts[tx_size][type][ref];
  494. vp9_prob (*const coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
  495. cpi->common.fc->coef_probs[tx_size][type][ref];
  496. unsigned int (*const eob_branch)[COEFF_CONTEXTS] =
  497. td->counts->eob_branch[tx_size][type][ref];
  498. const uint8_t *const band = get_band_translate(tx_size);
  499. const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);
  500. int16_t token;
  501. EXTRABIT extra;
  502. int aoff, loff;
  503. txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);
  504. pt = get_entropy_context(tx_size, pd->above_context + aoff,
  505. pd->left_context + loff);
  506. so = get_scan(xd, tx_size, type, block);
  507. scan = so->scan;
  508. nb = so->neighbors;
  509. c = 0;
  510. while (c < eob) {
  511. int v = 0;
  512. int skip_eob = 0;
  513. v = qcoeff[scan[c]];
  514. while (!v) {
  515. add_token_no_extra(&t, coef_probs[band[c]][pt], ZERO_TOKEN, skip_eob,
  516. counts[band[c]][pt]);
  517. eob_branch[band[c]][pt] += !skip_eob;
  518. skip_eob = 1;
  519. token_cache[scan[c]] = 0;
  520. ++c;
  521. pt = get_coef_context(nb, token_cache, c);
  522. v = qcoeff[scan[c]];
  523. }
  524. vp9_get_token_extra(v, &token, &extra);
  525. add_token(&t, coef_probs[band[c]][pt], extra, (uint8_t)token,
  526. (uint8_t)skip_eob, counts[band[c]][pt]);
  527. eob_branch[band[c]][pt] += !skip_eob;
  528. token_cache[scan[c]] = vp9_pt_energy_class[token];
  529. ++c;
  530. pt = get_coef_context(nb, token_cache, c);
  531. }
  532. if (c < seg_eob) {
  533. add_token_no_extra(&t, coef_probs[band[c]][pt], EOB_TOKEN, 0,
  534. counts[band[c]][pt]);
  535. ++eob_branch[band[c]][pt];
  536. }
  537. *tp = t;
  538. vp9_set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff);
  539. }
  540. struct is_skippable_args {
  541. MACROBLOCK *x;
  542. int *skippable;
  543. };
  544. static void is_skippable(int plane, int block,
  545. BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
  546. void *argv) {
  547. struct is_skippable_args *args = argv;
  548. (void)plane_bsize;
  549. (void)tx_size;
  550. args->skippable[0] &= (!args->x->plane[plane].eobs[block]);
  551. }
  552. // TODO(yaowu): rewrite and optimize this function to remove the usage of
  553. // vp9_foreach_transform_block() and simplify is_skippable().
  554. int vp9_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
  555. int result = 1;
  556. struct is_skippable_args args = {x, &result};
  557. vp9_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane, is_skippable,
  558. &args);
  559. return result;
  560. }
  561. static void has_high_freq_coeff(int plane, int block,
  562. BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
  563. void *argv) {
  564. struct is_skippable_args *args = argv;
  565. int eobs = (tx_size == TX_4X4) ? 3 : 10;
  566. (void) plane_bsize;
  567. *(args->skippable) |= (args->x->plane[plane].eobs[block] > eobs);
  568. }
  569. int vp9_has_high_freq_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
  570. int result = 0;
  571. struct is_skippable_args args = {x, &result};
  572. vp9_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane,
  573. has_high_freq_coeff, &args);
  574. return result;
  575. }
  576. void vp9_tokenize_sb(VP9_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
  577. int dry_run, BLOCK_SIZE bsize) {
  578. VP9_COMMON *const cm = &cpi->common;
  579. MACROBLOCK *const x = &td->mb;
  580. MACROBLOCKD *const xd = &x->e_mbd;
  581. MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  582. const int ctx = vp9_get_skip_context(xd);
  583. const int skip_inc = !vp9_segfeature_active(&cm->seg, mbmi->segment_id,
  584. SEG_LVL_SKIP);
  585. struct tokenize_b_args arg = {cpi, td, t};
  586. if (mbmi->skip) {
  587. if (!dry_run)
  588. td->counts->skip[ctx][1] += skip_inc;
  589. reset_skip_context(xd, bsize);
  590. return;
  591. }
  592. if (!dry_run) {
  593. td->counts->skip[ctx][0] += skip_inc;
  594. vp9_foreach_transformed_block(xd, bsize, tokenize_b, &arg);
  595. } else {
  596. vp9_foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg);
  597. }
  598. }