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 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 
131 #if CONFIG_VP9_HIGHBITDEPTH
132 const int16_t vp9_cat6_high10_high_cost[512] = {
133   74, 894, 1185, 2005, 1450, 2270, 2561,
134   3381, 1711, 2531, 2822, 3642, 3087, 3907, 4198, 5018, 2120, 2940, 3231,
135   4051, 3496, 4316, 4607, 5427, 3757, 4577, 4868, 5688, 5133, 5953, 6244,
136   7064, 2120, 2940, 3231, 4051, 3496, 4316, 4607, 5427, 3757, 4577, 4868,
137   5688, 5133, 5953, 6244, 7064, 4166, 4986, 5277, 6097, 5542, 6362, 6653,
138   7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290, 9110, 2120, 2940, 3231,
139   4051, 3496, 4316, 4607, 5427, 3757, 4577, 4868, 5688, 5133, 5953, 6244,
140   7064, 4166, 4986, 5277, 6097, 5542, 6362, 6653, 7473, 5803, 6623, 6914,
141   7734, 7179, 7999, 8290, 9110, 4166, 4986, 5277, 6097, 5542, 6362, 6653,
142   7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290, 9110, 6212, 7032, 7323,
143   8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960, 9780, 9225, 10045, 10336,
144   11156, 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, 4166, 4986, 5277,
147   6097, 5542, 6362, 6653, 7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290,
148   9110, 6212, 7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960,
149   9780, 9225, 10045, 10336, 11156, 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, 6212, 7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960,
153   9780, 9225, 10045, 10336, 11156, 8258, 9078, 9369, 10189, 9634, 10454,
154   10745, 11565, 9895, 10715, 11006, 11826, 11271, 12091, 12382, 13202, 2120,
155   2940, 3231, 4051, 3496, 4316, 4607, 5427, 3757, 4577, 4868, 5688, 5133,
156   5953, 6244, 7064, 4166, 4986, 5277, 6097, 5542, 6362, 6653, 7473, 5803,
157   6623, 6914, 7734, 7179, 7999, 8290, 9110, 4166, 4986, 5277, 6097, 5542,
158   6362, 6653, 7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290, 9110, 6212,
159   7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960, 9780, 9225,
160   10045, 10336, 11156, 4166, 4986, 5277, 6097, 5542, 6362, 6653, 7473, 5803,
161   6623, 6914, 7734, 7179, 7999, 8290, 9110, 6212, 7032, 7323, 8143, 7588,
162   8408, 8699, 9519, 7849, 8669, 8960, 9780, 9225, 10045, 10336, 11156, 6212,
163   7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960, 9780, 9225,
164   10045, 10336, 11156, 8258, 9078, 9369, 10189, 9634, 10454, 10745, 11565,
165   9895, 10715, 11006, 11826, 11271, 12091, 12382, 13202, 4166, 4986, 5277,
166   6097, 5542, 6362, 6653, 7473, 5803, 6623, 6914, 7734, 7179, 7999, 8290,
167   9110, 6212, 7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669, 8960,
168   9780, 9225, 10045, 10336, 11156, 6212, 7032, 7323, 8143, 7588, 8408, 8699,
169   9519, 7849, 8669, 8960, 9780, 9225, 10045, 10336, 11156, 8258, 9078, 9369,
170   10189, 9634, 10454, 10745, 11565, 9895, 10715, 11006, 11826, 11271, 12091,
171   12382, 13202, 6212, 7032, 7323, 8143, 7588, 8408, 8699, 9519, 7849, 8669,
172   8960, 9780, 9225, 10045, 10336, 11156, 8258, 9078, 9369, 10189, 9634, 10454,
173   10745, 11565, 9895, 10715, 11006, 11826, 11271, 12091, 12382, 13202, 8258,
174   9078, 9369, 10189, 9634, 10454, 10745, 11565, 9895, 10715, 11006, 11826,
175   11271, 12091, 12382, 13202, 10304, 11124, 11415, 12235, 11680, 12500, 12791,
176   13611, 11941, 12761, 13052, 13872, 13317, 14137, 14428, 15248,
177 };
178 const int16_t vp9_cat6_high12_high_cost[2048] = {
179   76, 896, 1187, 2007, 1452, 2272, 2563,
180   3383, 1713, 2533, 2824, 3644, 3089, 3909, 4200, 5020, 2122, 2942, 3233,
181   4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870, 5690, 5135, 5955, 6246,
182   7066, 2122, 2942, 3233, 4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870,
183   5690, 5135, 5955, 6246, 7066, 4168, 4988, 5279, 6099, 5544, 6364, 6655,
184   7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 2122, 2942, 3233,
185   4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870, 5690, 5135, 5955, 6246,
186   7066, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916,
187   7736, 7181, 8001, 8292, 9112, 4168, 4988, 5279, 6099, 5544, 6364, 6655,
188   7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325,
189   8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338,
190   11158, 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, 4168, 4988, 5279,
193   6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292,
194   9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962,
195   9782, 9227, 10047, 10338, 11158, 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, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962,
199   9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456,
200   10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 2122,
201   2942, 3233, 4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870, 5690, 5135,
202   5955, 6246, 7066, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805,
203   6625, 6916, 7736, 7181, 8001, 8292, 9112, 4168, 4988, 5279, 6099, 5544,
204   6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214,
205   7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227,
206   10047, 10338, 11158, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805,
207   6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145, 7590,
208   8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 6214,
209   7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227,
210   10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567,
211   9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 4168, 4988, 5279,
212   6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292,
213   9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962,
214   9782, 9227, 10047, 10338, 11158, 6214, 7034, 7325, 8145, 7590, 8410, 8701,
215   9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371,
216   10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093,
217   12384, 13204, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671,
218   8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456,
219   10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 8260,
220   9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828,
221   11273, 12093, 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793,
222   13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 2122, 2942,
223   3233, 4053, 3498, 4318, 4609, 5429, 3759, 4579, 4870, 5690, 5135, 5955,
224   6246, 7066, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625,
225   6916, 7736, 7181, 8001, 8292, 9112, 4168, 4988, 5279, 6099, 5544, 6364,
226   6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034,
227   7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047,
228   10338, 11158, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625,
229   6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145, 7590, 8410,
230   8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 6214, 7034,
231   7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047,
232   10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897,
233   10717, 11008, 11828, 11273, 12093, 12384, 13204, 4168, 4988, 5279, 6099,
234   5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112,
235   6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782,
236   9227, 10047, 10338, 11158, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521,
237   7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191,
238   9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384,
239   13204, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962,
240   9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456,
241   10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 8260,
242   9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828,
243   11273, 12093, 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793,
244   13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 4168, 4988,
245   5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001,
246   8292, 9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671,
247   8962, 9782, 9227, 10047, 10338, 11158, 6214, 7034, 7325, 8145, 7590, 8410,
248   8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080,
249   9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273,
250   12093, 12384, 13204, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851,
251   8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636,
252   10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204,
253   8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008,
254   11828, 11273, 12093, 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502,
255   12793, 13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 6214,
256   7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227,
257   10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567,
258   9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 8260, 9080, 9371,
259   10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093,
260   12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943,
261   12763, 13054, 13874, 13319, 14139, 14430, 15250, 8260, 9080, 9371, 10191,
262   9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384,
263   13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763,
264   13054, 13874, 13319, 14139, 14430, 15250, 10306, 11126, 11417, 12237, 11682,
265   12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250,
266   12352, 13172, 13463, 14283, 13728, 14548, 14839, 15659, 13989, 14809, 15100,
267   15920, 15365, 16185, 16476, 17296, 2122, 2942, 3233, 4053, 3498, 4318, 4609,
268   5429, 3759, 4579, 4870, 5690, 5135, 5955, 6246, 7066, 4168, 4988, 5279,
269   6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292,
270   9112, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916,
271   7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701,
272   9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 4168, 4988, 5279,
273   6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916, 7736, 7181, 8001, 8292,
274   9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962,
275   9782, 9227, 10047, 10338, 11158, 6214, 7034, 7325, 8145, 7590, 8410, 8701,
276   9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371,
277   10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093,
278   12384, 13204, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625,
279   6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145, 7590, 8410,
280   8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 6214, 7034,
281   7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047,
282   10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897,
283   10717, 11008, 11828, 11273, 12093, 12384, 13204, 6214, 7034, 7325, 8145,
284   7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158,
285   8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008,
286   11828, 11273, 12093, 12384, 13204, 8260, 9080, 9371, 10191, 9636, 10456,
287   10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306,
288   11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874,
289   13319, 14139, 14430, 15250, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475,
290   5805, 6625, 6916, 7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145,
291   7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158,
292   6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782,
293   9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747,
294   11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 6214, 7034,
295   7325, 8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047,
296   10338, 11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897,
297   10717, 11008, 11828, 11273, 12093, 12384, 13204, 8260, 9080, 9371, 10191,
298   9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384,
299   13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763,
300   13054, 13874, 13319, 14139, 14430, 15250, 6214, 7034, 7325, 8145, 7590,
301   8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260,
302   9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828,
303   11273, 12093, 12384, 13204, 8260, 9080, 9371, 10191, 9636, 10456, 10747,
304   11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306, 11126,
305   11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319,
306   14139, 14430, 15250, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567,
307   9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306, 11126, 11417,
308   12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319, 14139,
309   14430, 15250, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943,
310   12763, 13054, 13874, 13319, 14139, 14430, 15250, 12352, 13172, 13463, 14283,
311   13728, 14548, 14839, 15659, 13989, 14809, 15100, 15920, 15365, 16185, 16476,
312   17296, 4168, 4988, 5279, 6099, 5544, 6364, 6655, 7475, 5805, 6625, 6916,
313   7736, 7181, 8001, 8292, 9112, 6214, 7034, 7325, 8145, 7590, 8410, 8701,
314   9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 6214, 7034, 7325,
315   8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338,
316   11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717,
317   11008, 11828, 11273, 12093, 12384, 13204, 6214, 7034, 7325, 8145, 7590,
318   8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260,
319   9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828,
320   11273, 12093, 12384, 13204, 8260, 9080, 9371, 10191, 9636, 10456, 10747,
321   11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306, 11126,
322   11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319,
323   14139, 14430, 15250, 6214, 7034, 7325, 8145, 7590, 8410, 8701, 9521, 7851,
324   8671, 8962, 9782, 9227, 10047, 10338, 11158, 8260, 9080, 9371, 10191, 9636,
325   10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204,
326   8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008,
327   11828, 11273, 12093, 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502,
328   12793, 13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 8260,
329   9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717, 11008, 11828,
330   11273, 12093, 12384, 13204, 10306, 11126, 11417, 12237, 11682, 12502, 12793,
331   13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 10306, 11126,
332   11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319,
333   14139, 14430, 15250, 12352, 13172, 13463, 14283, 13728, 14548, 14839, 15659,
334   13989, 14809, 15100, 15920, 15365, 16185, 16476, 17296, 6214, 7034, 7325,
335   8145, 7590, 8410, 8701, 9521, 7851, 8671, 8962, 9782, 9227, 10047, 10338,
336   11158, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567, 9897, 10717,
337   11008, 11828, 11273, 12093, 12384, 13204, 8260, 9080, 9371, 10191, 9636,
338   10456, 10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204,
339   10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054,
340   13874, 13319, 14139, 14430, 15250, 8260, 9080, 9371, 10191, 9636, 10456,
341   10747, 11567, 9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306,
342   11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874,
343   13319, 14139, 14430, 15250, 10306, 11126, 11417, 12237, 11682, 12502, 12793,
344   13613, 11943, 12763, 13054, 13874, 13319, 14139, 14430, 15250, 12352, 13172,
345   13463, 14283, 13728, 14548, 14839, 15659, 13989, 14809, 15100, 15920, 15365,
346   16185, 16476, 17296, 8260, 9080, 9371, 10191, 9636, 10456, 10747, 11567,
347   9897, 10717, 11008, 11828, 11273, 12093, 12384, 13204, 10306, 11126, 11417,
348   12237, 11682, 12502, 12793, 13613, 11943, 12763, 13054, 13874, 13319, 14139,
349   14430, 15250, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943,
350   12763, 13054, 13874, 13319, 14139, 14430, 15250, 12352, 13172, 13463, 14283,
351   13728, 14548, 14839, 15659, 13989, 14809, 15100, 15920, 15365, 16185, 16476,
352   17296, 10306, 11126, 11417, 12237, 11682, 12502, 12793, 13613, 11943, 12763,
353   13054, 13874, 13319, 14139, 14430, 15250, 12352, 13172, 13463, 14283, 13728,
354   14548, 14839, 15659, 13989, 14809, 15100, 15920, 15365, 16185, 16476, 17296,
355   12352, 13172, 13463, 14283, 13728, 14548, 14839, 15659, 13989, 14809, 15100,
356   15920, 15365, 16185, 16476, 17296, 14398, 15218, 15509, 16329, 15774, 16594,
357   16885, 17705, 16035, 16855, 17146, 17966, 17411, 18231, 18522, 19342
358 };
359 #endif
360 
361 const vp9_extra_bit vp9_extra_bits[ENTROPY_TOKENS] = {
362   {0, 0, 0, zero_cost},                          // ZERO_TOKEN
363   {0, 0, 1, one_cost},                           // ONE_TOKEN
364   {0, 0, 2, two_cost},                           // TWO_TOKEN
365   {0, 0, 3, three_cost},                         // THREE_TOKEN
366   {0, 0, 4, four_cost},                          // FOUR_TOKEN
367   {vp9_cat1_prob, 1,  CAT1_MIN_VAL, cat1_cost},  // CATEGORY1_TOKEN
368   {vp9_cat2_prob, 2,  CAT2_MIN_VAL, cat2_cost},  // CATEGORY2_TOKEN
369   {vp9_cat3_prob, 3,  CAT3_MIN_VAL, cat3_cost},  // CATEGORY3_TOKEN
370   {vp9_cat4_prob, 4,  CAT4_MIN_VAL, cat4_cost},  // CATEGORY4_TOKEN
371   {vp9_cat5_prob, 5,  CAT5_MIN_VAL, cat5_cost},  // CATEGORY5_TOKEN
372   {vp9_cat6_prob, 14, CAT6_MIN_VAL, 0},          // CATEGORY6_TOKEN
373   {0, 0, 0, zero_cost}                           // EOB_TOKEN
374 };
375 
376 #if CONFIG_VP9_HIGHBITDEPTH
377 const vp9_extra_bit vp9_extra_bits_high10[ENTROPY_TOKENS] = {
378   {0, 0, 0, zero_cost},                                 // ZERO
379   {0, 0, 1, one_cost},                                  // ONE
380   {0, 0, 2, two_cost},                                  // TWO
381   {0, 0, 3, three_cost},                                // THREE
382   {0, 0, 4, four_cost},                                 // FOUR
383   {vp9_cat1_prob_high10, 1,  CAT1_MIN_VAL, cat1_cost},  // CAT1
384   {vp9_cat2_prob_high10, 2,  CAT2_MIN_VAL, cat2_cost},  // CAT2
385   {vp9_cat3_prob_high10, 3,  CAT3_MIN_VAL, cat3_cost},  // CAT3
386   {vp9_cat4_prob_high10, 4,  CAT4_MIN_VAL, cat4_cost},  // CAT4
387   {vp9_cat5_prob_high10, 5,  CAT5_MIN_VAL, cat5_cost},  // CAT5
388   {vp9_cat6_prob_high10, 16, CAT6_MIN_VAL, 0},          // CAT6
389   {0, 0, 0, zero_cost}                                  // EOB
390 };
391 const vp9_extra_bit vp9_extra_bits_high12[ENTROPY_TOKENS] = {
392   {0, 0, 0, zero_cost},                                 // ZERO
393   {0, 0, 1, one_cost},                                  // ONE
394   {0, 0, 2, two_cost},                                  // TWO
395   {0, 0, 3, three_cost},                                // THREE
396   {0, 0, 4, four_cost},                                 // FOUR
397   {vp9_cat1_prob_high12, 1,  CAT1_MIN_VAL, cat1_cost},  // CAT1
398   {vp9_cat2_prob_high12, 2,  CAT2_MIN_VAL, cat2_cost},  // CAT2
399   {vp9_cat3_prob_high12, 3,  CAT3_MIN_VAL, cat3_cost},  // CAT3
400   {vp9_cat4_prob_high12, 4,  CAT4_MIN_VAL, cat4_cost},  // CAT4
401   {vp9_cat5_prob_high12, 5,  CAT5_MIN_VAL, cat5_cost},  // CAT5
402   {vp9_cat6_prob_high12, 18, CAT6_MIN_VAL, 0},          // CAT6
403   {0, 0, 0, zero_cost}                                  // EOB
404 };
405 #endif
406 
407 const struct vp9_token vp9_coef_encodings[ENTROPY_TOKENS] = {
408   {2, 2}, {6, 3}, {28, 5}, {58, 6}, {59, 6}, {60, 6}, {61, 6}, {124, 7},
409   {125, 7}, {126, 7}, {127, 7}, {0, 1}
410 };
411 
412 
413 struct tokenize_b_args {
414   VP9_COMP *cpi;
415   ThreadData *td;
416   TOKENEXTRA **tp;
417 };
418 
set_entropy_context_b(int plane,int block,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)419 static void set_entropy_context_b(int plane, int block, BLOCK_SIZE plane_bsize,
420                                   TX_SIZE tx_size, void *arg) {
421   struct tokenize_b_args* const args = arg;
422   ThreadData *const td = args->td;
423   MACROBLOCK *const x = &td->mb;
424   MACROBLOCKD *const xd = &x->e_mbd;
425   struct macroblock_plane *p = &x->plane[plane];
426   struct macroblockd_plane *pd = &xd->plane[plane];
427   int aoff, loff;
428   txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);
429   vp9_set_contexts(xd, pd, plane_bsize, tx_size, p->eobs[block] > 0,
430                    aoff, loff);
431 }
432 
add_token(TOKENEXTRA ** t,const vpx_prob * context_tree,int32_t extra,uint8_t token,uint8_t skip_eob_node,unsigned int * counts)433 static INLINE void add_token(TOKENEXTRA **t, const vpx_prob *context_tree,
434                              int32_t extra, uint8_t token,
435                              uint8_t skip_eob_node,
436                              unsigned int *counts) {
437   (*t)->token = token;
438   (*t)->extra = extra;
439   (*t)->context_tree = context_tree;
440   (*t)->skip_eob_node = skip_eob_node;
441   (*t)++;
442   ++counts[token];
443 }
444 
add_token_no_extra(TOKENEXTRA ** t,const vpx_prob * context_tree,uint8_t token,uint8_t skip_eob_node,unsigned int * counts)445 static INLINE void add_token_no_extra(TOKENEXTRA **t,
446                                       const vpx_prob *context_tree,
447                                       uint8_t token,
448                                       uint8_t skip_eob_node,
449                                       unsigned int *counts) {
450   (*t)->token = token;
451   (*t)->context_tree = context_tree;
452   (*t)->skip_eob_node = skip_eob_node;
453   (*t)++;
454   ++counts[token];
455 }
456 
get_tx_eob(const struct segmentation * seg,int segment_id,TX_SIZE tx_size)457 static INLINE int get_tx_eob(const struct segmentation *seg, int segment_id,
458                              TX_SIZE tx_size) {
459   const int eob_max = 16 << (tx_size << 1);
460   return segfeature_active(seg, segment_id, SEG_LVL_SKIP) ? 0 : eob_max;
461 }
462 
tokenize_b(int plane,int block,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)463 static void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize,
464                        TX_SIZE tx_size, void *arg) {
465   struct tokenize_b_args* const args = arg;
466   VP9_COMP *cpi = args->cpi;
467   ThreadData *const td = args->td;
468   MACROBLOCK *const x = &td->mb;
469   MACROBLOCKD *const xd = &x->e_mbd;
470   TOKENEXTRA **tp = args->tp;
471   uint8_t token_cache[32 * 32];
472   struct macroblock_plane *p = &x->plane[plane];
473   struct macroblockd_plane *pd = &xd->plane[plane];
474   MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
475   int pt; /* near block/prev token context index */
476   int c;
477   TOKENEXTRA *t = *tp;        /* store tokens starting here */
478   int eob = p->eobs[block];
479   const PLANE_TYPE type = get_plane_type(plane);
480   const tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
481   const int segment_id = mbmi->segment_id;
482   const int16_t *scan, *nb;
483   const scan_order *so;
484   const int ref = is_inter_block(mbmi);
485   unsigned int (*const counts)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
486       td->rd_counts.coef_counts[tx_size][type][ref];
487   vpx_prob (*const coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
488       cpi->common.fc->coef_probs[tx_size][type][ref];
489   unsigned int (*const eob_branch)[COEFF_CONTEXTS] =
490       td->counts->eob_branch[tx_size][type][ref];
491   const uint8_t *const band = get_band_translate(tx_size);
492   const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);
493   int16_t token;
494   EXTRABIT extra;
495   int aoff, loff;
496   txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);
497 
498   pt = get_entropy_context(tx_size, pd->above_context + aoff,
499                            pd->left_context + loff);
500   so = get_scan(xd, tx_size, type, block);
501   scan = so->scan;
502   nb = so->neighbors;
503   c = 0;
504 
505   while (c < eob) {
506     int v = 0;
507     int skip_eob = 0;
508     v = qcoeff[scan[c]];
509 
510     while (!v) {
511       add_token_no_extra(&t, coef_probs[band[c]][pt], ZERO_TOKEN, skip_eob,
512                          counts[band[c]][pt]);
513       eob_branch[band[c]][pt] += !skip_eob;
514 
515       skip_eob = 1;
516       token_cache[scan[c]] = 0;
517       ++c;
518       pt = get_coef_context(nb, token_cache, c);
519       v = qcoeff[scan[c]];
520     }
521 
522     vp9_get_token_extra(v, &token, &extra);
523 
524     add_token(&t, coef_probs[band[c]][pt], extra, (uint8_t)token,
525               (uint8_t)skip_eob, counts[band[c]][pt]);
526     eob_branch[band[c]][pt] += !skip_eob;
527 
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 
538   *tp = t;
539 
540   vp9_set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff);
541 }
542 
543 struct is_skippable_args {
544   uint16_t *eobs;
545   int *skippable;
546 };
is_skippable(int plane,int block,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * argv)547 static void is_skippable(int plane, int block,
548                          BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
549                          void *argv) {
550   struct is_skippable_args *args = argv;
551   (void)plane;
552   (void)plane_bsize;
553   (void)tx_size;
554   args->skippable[0] &= (!args->eobs[block]);
555 }
556 
557 // TODO(yaowu): rewrite and optimize this function to remove the usage of
558 //              vp9_foreach_transform_block() and simplify is_skippable().
vp9_is_skippable_in_plane(MACROBLOCK * x,BLOCK_SIZE bsize,int plane)559 int vp9_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
560   int result = 1;
561   struct is_skippable_args args = {x->plane[plane].eobs, &result};
562   vp9_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane, is_skippable,
563                                          &args);
564   return result;
565 }
566 
has_high_freq_coeff(int plane,int block,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * argv)567 static void has_high_freq_coeff(int plane, int block,
568                                 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
569                                 void *argv) {
570   struct is_skippable_args *args = argv;
571   int eobs = (tx_size == TX_4X4) ? 3 : 10;
572   (void) plane;
573   (void) plane_bsize;
574 
575   *(args->skippable) |= (args->eobs[block] > eobs);
576 }
577 
vp9_has_high_freq_in_plane(MACROBLOCK * x,BLOCK_SIZE bsize,int plane)578 int vp9_has_high_freq_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
579   int result = 0;
580   struct is_skippable_args args = {x->plane[plane].eobs, &result};
581   vp9_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane,
582                                          has_high_freq_coeff, &args);
583   return result;
584 }
585 
vp9_tokenize_sb(VP9_COMP * cpi,ThreadData * td,TOKENEXTRA ** t,int dry_run,BLOCK_SIZE bsize)586 void vp9_tokenize_sb(VP9_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
587                      int dry_run, BLOCK_SIZE bsize) {
588   VP9_COMMON *const cm = &cpi->common;
589   MACROBLOCK *const x = &td->mb;
590   MACROBLOCKD *const xd = &x->e_mbd;
591   MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
592   const int ctx = vp9_get_skip_context(xd);
593   const int skip_inc = !segfeature_active(&cm->seg, mbmi->segment_id,
594                                           SEG_LVL_SKIP);
595   struct tokenize_b_args arg = {cpi, td, t};
596   if (mbmi->skip) {
597     if (!dry_run)
598       td->counts->skip[ctx][1] += skip_inc;
599     reset_skip_context(xd, bsize);
600     return;
601   }
602 
603   if (!dry_run) {
604     td->counts->skip[ctx][0] += skip_inc;
605     vp9_foreach_transformed_block(xd, bsize, tokenize_b, &arg);
606   } else {
607     vp9_foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg);
608   }
609 }
610