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