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
13 #define INLINE __inline
14
15 #include <stdint.h>
16 #include "vp9_tokenize.h"
17 #include "vp9_entropy.h"
18 #include "vp9_block.h"
19 #include "vp9_blockd.h"
20 #include "vp9_enums.h"
21 #include "vp9_scan.h"
22 #include "vp9_encoder.h"
23
24 static const TOKENVALUE dct_cat_lt_10_value_tokens[] = {
25 { 9, 63 }, { 9, 61 }, { 9, 59 }, { 9, 57 }, { 9, 55 }, { 9, 53 }, { 9, 51 },
26 { 9, 49 }, { 9, 47 }, { 9, 45 }, { 9, 43 }, { 9, 41 }, { 9, 39 }, { 9, 37 },
27 { 9, 35 }, { 9, 33 }, { 9, 31 }, { 9, 29 }, { 9, 27 }, { 9, 25 }, { 9, 23 },
28 { 9, 21 }, { 9, 19 }, { 9, 17 }, { 9, 15 }, { 9, 13 }, { 9, 11 }, { 9, 9 },
29 { 9, 7 }, { 9, 5 }, { 9, 3 }, { 9, 1 }, { 8, 31 }, { 8, 29 }, { 8, 27 },
30 { 8, 25 }, { 8, 23 }, { 8, 21 }, { 8, 19 }, { 8, 17 }, { 8, 15 }, { 8, 13 },
31 { 8, 11 }, { 8, 9 }, { 8, 7 }, { 8, 5 }, { 8, 3 }, { 8, 1 }, { 7, 15 },
32 { 7, 13 }, { 7, 11 }, { 7, 9 }, { 7, 7 }, { 7, 5 }, { 7, 3 }, { 7, 1 },
33 { 6, 7 }, { 6, 5 }, { 6, 3 }, { 6, 1 }, { 5, 3 }, { 5, 1 }, { 4, 1 },
34 { 3, 1 }, { 2, 1 }, { 1, 1 }, { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },
35 { 4, 0 }, { 5, 0 }, { 5, 2 }, { 6, 0 }, { 6, 2 }, { 6, 4 }, { 6, 6 },
36 { 7, 0 }, { 7, 2 }, { 7, 4 }, { 7, 6 }, { 7, 8 }, { 7, 10 }, { 7, 12 },
37 { 7, 14 }, { 8, 0 }, { 8, 2 }, { 8, 4 }, { 8, 6 }, { 8, 8 }, { 8, 10 },
38 { 8, 12 }, { 8, 14 }, { 8, 16 }, { 8, 18 }, { 8, 20 }, { 8, 22 }, { 8, 24 },
39 { 8, 26 }, { 8, 28 }, { 8, 30 }, { 9, 0 }, { 9, 2 }, { 9, 4 }, { 9, 6 },
40 { 9, 8 }, { 9, 10 }, { 9, 12 }, { 9, 14 }, { 9, 16 }, { 9, 18 }, { 9, 20 },
41 { 9, 22 }, { 9, 24 }, { 9, 26 }, { 9, 28 }, { 9, 30 }, { 9, 32 }, { 9, 34 },
42 { 9, 36 }, { 9, 38 }, { 9, 40 }, { 9, 42 }, { 9, 44 }, { 9, 46 }, { 9, 48 },
43 { 9, 50 }, { 9, 52 }, { 9, 54 }, { 9, 56 }, { 9, 58 }, { 9, 60 }, { 9, 62 }
44 };
45 const TOKENVALUE *eb_vp9_dct_cat_lt_10_value_tokens =
46 dct_cat_lt_10_value_tokens +
47 (sizeof(dct_cat_lt_10_value_tokens) / sizeof(*dct_cat_lt_10_value_tokens)) /
48 2;
49 // The corresponding costs of the extra_bits for the tokens in the above table
50 // are stored in the table below. The values are obtained from looking up the
51 // entry for the specified extra_bits in the table corresponding to the token
52 // (as defined in cost element eb_vp9_extra_bits)
53 // e.g. {9, 63} maps to cat5_cost[63 >> 1], {1, 1} maps to sign_cost[1 >> 1]
54 static const int dct_cat_lt_10_value_cost[] = {
55 3773, 3750, 3704, 3681, 3623, 3600, 3554, 3531, 3432, 3409, 3363, 3340, 3282,
56 3259, 3213, 3190, 3136, 3113, 3067, 3044, 2986, 2963, 2917, 2894, 2795, 2772,
57 2726, 2703, 2645, 2622, 2576, 2553, 3197, 3116, 3058, 2977, 2881, 2800, 2742,
58 2661, 2615, 2534, 2476, 2395, 2299, 2218, 2160, 2079, 2566, 2427, 2334, 2195,
59 2023, 1884, 1791, 1652, 1893, 1696, 1453, 1256, 1229, 864, 512, 512, 512,
60 512, 0, 512, 512, 512, 512, 864, 1229, 1256, 1453, 1696, 1893, 1652,
61 1791, 1884, 2023, 2195, 2334, 2427, 2566, 2079, 2160, 2218, 2299, 2395, 2476,
62 2534, 2615, 2661, 2742, 2800, 2881, 2977, 3058, 3116, 3197, 2553, 2576, 2622,
63 2645, 2703, 2726, 2772, 2795, 2894, 2917, 2963, 2986, 3044, 3067, 3113, 3136,
64 3190, 3213, 3259, 3282, 3340, 3363, 3409, 3432, 3531, 3554, 3600, 3623, 3681,
65 3704, 3750, 3773,
66 };
67 const int *eb_vp9_dct_cat_lt_10_value_cost =
68 dct_cat_lt_10_value_cost +
69 (sizeof(dct_cat_lt_10_value_cost) / sizeof(*dct_cat_lt_10_value_cost)) / 2;
70
71 // Array indices are identical to previously-existing CONTEXT_NODE indices
72 /* clang-format off */
73 const vpx_tree_index eb_vp9_coef_tree[TREE_SIZE(ENTROPY_TOKENS)] = {
74 -EOB_TOKEN, 2, // 0 = EOB
75 -ZERO_TOKEN, 4, // 1 = ZERO
76 -ONE_TOKEN, 6, // 2 = ONE
77 8, 12, // 3 = LOW_VAL
78 -TWO_TOKEN, 10, // 4 = TWO
79 -THREE_TOKEN, -FOUR_TOKEN, // 5 = THREE
80 14, 16, // 6 = HIGH_LOW
81 -CATEGORY1_TOKEN, -CATEGORY2_TOKEN, // 7 = CAT_ONE
82 18, 20, // 8 = CAT_THREEFOUR
83 -CATEGORY3_TOKEN, -CATEGORY4_TOKEN, // 9 = CAT_THREE
84 -CATEGORY5_TOKEN, -CATEGORY6_TOKEN // 10 = CAT_FIVE
85 };
86 /* clang-format on */
87
88 static const int16_t zero_cost[] = { 0 };
89 static const int16_t sign_cost[1] = { 512 };
90 static const int16_t cat1_cost[1 << 1] = { 864, 1229 };
91 static const int16_t cat2_cost[1 << 2] = { 1256, 1453, 1696, 1893 };
92 static const int16_t cat3_cost[1 << 3] = { 1652, 1791, 1884, 2023,
93 2195, 2334, 2427, 2566 };
94 static const int16_t cat4_cost[1 << 4] = { 2079, 2160, 2218, 2299, 2395, 2476,
95 2534, 2615, 2661, 2742, 2800, 2881,
96 2977, 3058, 3116, 3197 };
97 static const int16_t cat5_cost[1 << 5] = {
98 2553, 2576, 2622, 2645, 2703, 2726, 2772, 2795, 2894, 2917, 2963,
99 2986, 3044, 3067, 3113, 3136, 3190, 3213, 3259, 3282, 3340, 3363,
100 3409, 3432, 3531, 3554, 3600, 3623, 3681, 3704, 3750, 3773
101 };
102 const int16_t eb_vp9_cat6_low_cost[256] = {
103 3378, 3390, 3401, 3413, 3435, 3447, 3458, 3470, 3517, 3529, 3540, 3552, 3574,
104 3586, 3597, 3609, 3671, 3683, 3694, 3706, 3728, 3740, 3751, 3763, 3810, 3822,
105 3833, 3845, 3867, 3879, 3890, 3902, 3973, 3985, 3996, 4008, 4030, 4042, 4053,
106 4065, 4112, 4124, 4135, 4147, 4169, 4181, 4192, 4204, 4266, 4278, 4289, 4301,
107 4323, 4335, 4346, 4358, 4405, 4417, 4428, 4440, 4462, 4474, 4485, 4497, 4253,
108 4265, 4276, 4288, 4310, 4322, 4333, 4345, 4392, 4404, 4415, 4427, 4449, 4461,
109 4472, 4484, 4546, 4558, 4569, 4581, 4603, 4615, 4626, 4638, 4685, 4697, 4708,
110 4720, 4742, 4754, 4765, 4777, 4848, 4860, 4871, 4883, 4905, 4917, 4928, 4940,
111 4987, 4999, 5010, 5022, 5044, 5056, 5067, 5079, 5141, 5153, 5164, 5176, 5198,
112 5210, 5221, 5233, 5280, 5292, 5303, 5315, 5337, 5349, 5360, 5372, 4988, 5000,
113 5011, 5023, 5045, 5057, 5068, 5080, 5127, 5139, 5150, 5162, 5184, 5196, 5207,
114 5219, 5281, 5293, 5304, 5316, 5338, 5350, 5361, 5373, 5420, 5432, 5443, 5455,
115 5477, 5489, 5500, 5512, 5583, 5595, 5606, 5618, 5640, 5652, 5663, 5675, 5722,
116 5734, 5745, 5757, 5779, 5791, 5802, 5814, 5876, 5888, 5899, 5911, 5933, 5945,
117 5956, 5968, 6015, 6027, 6038, 6050, 6072, 6084, 6095, 6107, 5863, 5875, 5886,
118 5898, 5920, 5932, 5943, 5955, 6002, 6014, 6025, 6037, 6059, 6071, 6082, 6094,
119 6156, 6168, 6179, 6191, 6213, 6225, 6236, 6248, 6295, 6307, 6318, 6330, 6352,
120 6364, 6375, 6387, 6458, 6470, 6481, 6493, 6515, 6527, 6538, 6550, 6597, 6609,
121 6620, 6632, 6654, 6666, 6677, 6689, 6751, 6763, 6774, 6786, 6808, 6820, 6831,
122 6843, 6890, 6902, 6913, 6925, 6947, 6959, 6970, 6982
123 };
124 const uint16_t eb_vp9_cat6_high_cost[64] = {
125 88, 2251, 2727, 4890, 3148, 5311, 5787, 7950, 3666, 5829, 6305,
126 8468, 6726, 8889, 9365, 11528, 3666, 5829, 6305, 8468, 6726, 8889,
127 9365, 11528, 7244, 9407, 9883, 12046, 10304, 12467, 12943, 15106, 3666,
128 5829, 6305, 8468, 6726, 8889, 9365, 11528, 7244, 9407, 9883, 12046,
129 10304, 12467, 12943, 15106, 7244, 9407, 9883, 12046, 10304, 12467, 12943,
130 15106, 10822, 12985, 13461, 15624, 13882, 16045, 16521, 18684
131 };
132
133 #if CONFIG_VP9_HIGHBITDEPTH
134 const uint16_t vp9_cat6_high10_high_cost[256] = {
135 94, 2257, 2733, 4896, 3154, 5317, 5793, 7956, 3672, 5835, 6311,
136 8474, 6732, 8895, 9371, 11534, 3672, 5835, 6311, 8474, 6732, 8895,
137 9371, 11534, 7250, 9413, 9889, 12052, 10310, 12473, 12949, 15112, 3672,
138 5835, 6311, 8474, 6732, 8895, 9371, 11534, 7250, 9413, 9889, 12052,
139 10310, 12473, 12949, 15112, 7250, 9413, 9889, 12052, 10310, 12473, 12949,
140 15112, 10828, 12991, 13467, 15630, 13888, 16051, 16527, 18690, 4187, 6350,
141 6826, 8989, 7247, 9410, 9886, 12049, 7765, 9928, 10404, 12567, 10825,
142 12988, 13464, 15627, 7765, 9928, 10404, 12567, 10825, 12988, 13464, 15627,
143 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 7765, 9928, 10404,
144 12567, 10825, 12988, 13464, 15627, 11343, 13506, 13982, 16145, 14403, 16566,
145 17042, 19205, 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 14921,
146 17084, 17560, 19723, 17981, 20144, 20620, 22783, 4187, 6350, 6826, 8989,
147 7247, 9410, 9886, 12049, 7765, 9928, 10404, 12567, 10825, 12988, 13464,
148 15627, 7765, 9928, 10404, 12567, 10825, 12988, 13464, 15627, 11343, 13506,
149 13982, 16145, 14403, 16566, 17042, 19205, 7765, 9928, 10404, 12567, 10825,
150 12988, 13464, 15627, 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205,
151 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 14921, 17084, 17560,
152 19723, 17981, 20144, 20620, 22783, 8280, 10443, 10919, 13082, 11340, 13503,
153 13979, 16142, 11858, 14021, 14497, 16660, 14918, 17081, 17557, 19720, 11858,
154 14021, 14497, 16660, 14918, 17081, 17557, 19720, 15436, 17599, 18075, 20238,
155 18496, 20659, 21135, 23298, 11858, 14021, 14497, 16660, 14918, 17081, 17557,
156 19720, 15436, 17599, 18075, 20238, 18496, 20659, 21135, 23298, 15436, 17599,
157 18075, 20238, 18496, 20659, 21135, 23298, 19014, 21177, 21653, 23816, 22074,
158 24237, 24713, 26876
159 };
160 const uint16_t vp9_cat6_high12_high_cost[1024] = {
161 100, 2263, 2739, 4902, 3160, 5323, 5799, 7962, 3678, 5841, 6317,
162 8480, 6738, 8901, 9377, 11540, 3678, 5841, 6317, 8480, 6738, 8901,
163 9377, 11540, 7256, 9419, 9895, 12058, 10316, 12479, 12955, 15118, 3678,
164 5841, 6317, 8480, 6738, 8901, 9377, 11540, 7256, 9419, 9895, 12058,
165 10316, 12479, 12955, 15118, 7256, 9419, 9895, 12058, 10316, 12479, 12955,
166 15118, 10834, 12997, 13473, 15636, 13894, 16057, 16533, 18696, 4193, 6356,
167 6832, 8995, 7253, 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831,
168 12994, 13470, 15633, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633,
169 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410,
170 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572,
171 17048, 19211, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927,
172 17090, 17566, 19729, 17987, 20150, 20626, 22789, 4193, 6356, 6832, 8995,
173 7253, 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831, 12994, 13470,
174 15633, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512,
175 13988, 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410, 12573, 10831,
176 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211,
177 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090, 17566,
178 19729, 17987, 20150, 20626, 22789, 8286, 10449, 10925, 13088, 11346, 13509,
179 13985, 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864,
180 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244,
181 18502, 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563,
182 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605,
183 18081, 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080,
184 24243, 24719, 26882, 4193, 6356, 6832, 8995, 7253, 9416, 9892, 12055,
185 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 7771, 9934, 10410,
186 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572,
187 17048, 19211, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349,
188 13512, 13988, 16151, 14409, 16572, 17048, 19211, 11349, 13512, 13988, 16151,
189 14409, 16572, 17048, 19211, 14927, 17090, 17566, 19729, 17987, 20150, 20626,
190 22789, 8286, 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027,
191 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924,
192 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
193 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081,
194 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665,
195 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 8286,
196 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027, 14503, 16666,
197 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924, 17087, 17563,
198 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 11864, 14027,
199 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502,
200 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
201 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379, 14542, 15018,
202 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180,
203 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535,
204 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759,
205 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234,
206 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276,
207 25752, 27915, 26173, 28336, 28812, 30975, 4193, 6356, 6832, 8995, 7253,
208 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633,
209 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988,
210 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410, 12573, 10831, 12994,
211 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 11349,
212 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090, 17566, 19729,
213 17987, 20150, 20626, 22789, 8286, 10449, 10925, 13088, 11346, 13509, 13985,
214 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027,
215 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502,
216 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
217 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081,
218 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243,
219 24719, 26882, 8286, 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864,
220 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666,
221 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141,
222 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605,
223 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502,
224 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882,
225 12379, 14542, 15018, 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596,
226 20759, 19017, 21180, 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180,
227 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957,
228 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337,
229 22595, 24758, 25234, 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234,
230 27397, 23113, 25276, 25752, 27915, 26173, 28336, 28812, 30975, 8286, 10449,
231 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027, 14503, 16666, 14924,
232 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
233 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 11864, 14027, 14503,
234 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665,
235 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 19020,
236 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379, 14542, 15018, 17181,
237 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
238 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698,
239 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759, 19017,
240 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397,
241 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276, 25752,
242 27915, 26173, 28336, 28812, 30975, 12379, 14542, 15018, 17181, 15439, 17602,
243 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 15957,
244 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337,
245 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
246 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 19535, 21698,
247 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276, 25752, 27915, 26173,
248 28336, 28812, 30975, 16472, 18635, 19111, 21274, 19532, 21695, 22171, 24334,
249 20050, 22213, 22689, 24852, 23110, 25273, 25749, 27912, 20050, 22213, 22689,
250 24852, 23110, 25273, 25749, 27912, 23628, 25791, 26267, 28430, 26688, 28851,
251 29327, 31490, 20050, 22213, 22689, 24852, 23110, 25273, 25749, 27912, 23628,
252 25791, 26267, 28430, 26688, 28851, 29327, 31490, 23628, 25791, 26267, 28430,
253 26688, 28851, 29327, 31490, 27206, 29369, 29845, 32008, 30266, 32429, 32905,
254 35068
255 };
256 #endif
257
258 const vp9_extra_bit eb_vp9_extra_bits[ENTROPY_TOKENS] = {
259 { 0, 0, 0, zero_cost }, // ZERO_TOKEN
260 { 0, 0, 1, sign_cost }, // ONE_TOKEN
261 { 0, 0, 2, sign_cost }, // TWO_TOKEN
262 { 0, 0, 3, sign_cost }, // THREE_TOKEN
263 { 0, 0, 4, sign_cost }, // FOUR_TOKEN
264 { eb_vp9_cat1_prob, 1, CAT1_MIN_VAL, cat1_cost }, // CATEGORY1_TOKEN
265 { eb_vp9_cat2_prob, 2, CAT2_MIN_VAL, cat2_cost }, // CATEGORY2_TOKEN
266 { eb_vp9_cat3_prob, 3, CAT3_MIN_VAL, cat3_cost }, // CATEGORY3_TOKEN
267 { eb_vp9_cat4_prob, 4, CAT4_MIN_VAL, cat4_cost }, // CATEGORY4_TOKEN
268 { eb_vp9_cat5_prob, 5, CAT5_MIN_VAL, cat5_cost }, // CATEGORY5_TOKEN
269 { eb_vp9_cat6_prob, 14, CAT6_MIN_VAL, 0 }, // CATEGORY6_TOKEN
270 { 0, 0, 0, zero_cost } // EOB_TOKEN
271 };
272
273 #if CONFIG_VP9_HIGHBITDEPTH
274 const vp9_extra_bit eb_vp9_extra_bits_high10[ENTROPY_TOKENS] = {
275 { 0, 0, 0, zero_cost }, // ZERO
276 { 0, 0, 1, sign_cost }, // ONE
277 { 0, 0, 2, sign_cost }, // TWO
278 { 0, 0, 3, sign_cost }, // THREE
279 { 0, 0, 4, sign_cost }, // FOUR
280 { eb_vp9_cat1_prob, 1, CAT1_MIN_VAL, cat1_cost }, // CAT1
281 { eb_vp9_cat2_prob, 2, CAT2_MIN_VAL, cat2_cost }, // CAT2
282 { eb_vp9_cat3_prob, 3, CAT3_MIN_VAL, cat3_cost }, // CAT3
283 { eb_vp9_cat4_prob, 4, CAT4_MIN_VAL, cat4_cost }, // CAT4
284 { eb_vp9_cat5_prob, 5, CAT5_MIN_VAL, cat5_cost }, // CAT5
285 { eb_vp9_cat6_prob_high12 + 2, 16, CAT6_MIN_VAL, 0 }, // CAT6
286 { 0, 0, 0, zero_cost } // EOB
287 };
288 const vp9_extra_bit eb_vp9_extra_bits_high12[ENTROPY_TOKENS] = {
289 { 0, 0, 0, zero_cost }, // ZERO
290 { 0, 0, 1, sign_cost }, // ONE
291 { 0, 0, 2, sign_cost }, // TWO
292 { 0, 0, 3, sign_cost }, // THREE
293 { 0, 0, 4, sign_cost }, // FOUR
294 { eb_vp9_cat1_prob, 1, CAT1_MIN_VAL, cat1_cost }, // CAT1
295 { eb_vp9_cat2_prob, 2, CAT2_MIN_VAL, cat2_cost }, // CAT2
296 { eb_vp9_cat3_prob, 3, CAT3_MIN_VAL, cat3_cost }, // CAT3
297 { eb_vp9_cat4_prob, 4, CAT4_MIN_VAL, cat4_cost }, // CAT4
298 { eb_vp9_cat5_prob, 5, CAT5_MIN_VAL, cat5_cost }, // CAT5
299 { eb_vp9_cat6_prob_high12, 18, CAT6_MIN_VAL, 0 }, // CAT6
300 { 0, 0, 0, zero_cost } // EOB
301 };
302 #endif
303
304 const struct vp9_token eb_vp9_coef_encodings[ENTROPY_TOKENS] = {
305 { 2, 2 }, { 6, 3 }, { 28, 5 }, { 58, 6 }, { 59, 6 }, { 60, 6 },
306 { 61, 6 }, { 124, 7 }, { 125, 7 }, { 126, 7 }, { 127, 7 }, { 0, 1 }
307 };
308
309 typedef struct tokenize_b_args {
310 VP9_COMP *cpi;
311 ThreadData *td;
312 TOKENEXTRA **tp;
313 }tokenize_b_args;
314
set_entropy_context_b(MACROBLOCKD * const xd,int plane,int block,int row,int col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)315 static void set_entropy_context_b(MACROBLOCKD *const xd, int plane, int block, int row, int col,
316 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
317 void *arg) {
318 struct tokenize_b_args *const args = (tokenize_b_args *) arg;
319
320 ThreadData *const td = args->td;
321 MACROBLOCK *const x = &td->mb;
322 #if 0
323 MACROBLOCKD *const xd = &x->e_mbd;
324 #endif
325
326 struct macroblock_plane *p = &x->plane[plane];
327 struct macroblockd_plane *pd = &xd->plane[plane];
328 eb_vp9_set_contexts(xd, pd, plane_bsize, tx_size, p->eobs[block] > 0, col, row);
329 }
330
add_token(TOKENEXTRA ** t,const vpx_prob * context_tree,int16_t token,EXTRABIT extra,unsigned int * counts)331 static INLINE void add_token(TOKENEXTRA **t, const vpx_prob *context_tree,
332 int16_t token, EXTRABIT extra,
333 unsigned int *counts) {
334 (*t)->context_tree = context_tree;
335 (*t)->token = token;
336 (*t)->extra = extra;
337 (*t)++;
338 ++counts[token];
339 }
340
add_token_no_extra(TOKENEXTRA ** t,const vpx_prob * context_tree,int16_t token,unsigned int * counts)341 static INLINE void add_token_no_extra(TOKENEXTRA **t,
342 const vpx_prob *context_tree,
343 int16_t token, unsigned int *counts) {
344 (*t)->context_tree = context_tree;
345 (*t)->token = token;
346 (*t)++;
347 ++counts[token];
348 }
349
tokenize_b(MACROBLOCKD * xd,int plane,int block,int row,int col,BLOCK_SIZE plane_bsize,TX_SIZE tx_size,void * arg)350 static void tokenize_b(MACROBLOCKD *xd, int plane, int block, int row, int col,
351 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
352 struct tokenize_b_args *const args = (tokenize_b_args *) arg;
353 VP9_COMP *cpi = args->cpi;
354 ThreadData *const td = args->td;
355 MACROBLOCK *const x = &td->mb;
356 #if 0
357 MACROBLOCKD *const xd = &x->e_mbd;
358 #endif
359 TOKENEXTRA **tp = args->tp;
360 uint8_t token_cache[32 * 32];
361 struct macroblock_plane *p = &x->plane[plane];
362 struct macroblockd_plane *pd = &xd->plane[plane];
363 ModeInfo *mi = xd->mi[0];
364 int pt; /* near block/prev token context index */
365 int c;
366 TOKENEXTRA *t = *tp; /* store tokens starting here */
367 int eob = p->eobs[block];
368 const PLANE_TYPE type = get_plane_type(plane);
369 const tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
370 const int16_t *scan, *nb;
371 const scan_order *so;
372 const int ref = is_inter_block(mi);
373 unsigned int(*const counts)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
374 td->rd_counts.coef_counts[tx_size][type][ref];
375 vpx_prob(*const coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
376 cpi->common.fc->coef_probs[tx_size][type][ref];
377
378 #if 0 // Hsan: count-based probability update not yet supported (i.e. use default)
379 unsigned int(*const eob_branch)[COEFF_CONTEXTS] =
380 td->counts->eob_branch[tx_size][type][ref];
381 #endif
382
383 const uint8_t *const band = get_band_translate(tx_size);
384 const int tx_eob = 16 << (tx_size << 1);
385 int16_t token;
386 EXTRABIT extra;
387 pt = get_entropy_context(tx_size, pd->above_context + col,
388 pd->left_context + row);
389 so = get_scan(xd, tx_size, type, block);
390 scan = so->scan;
391 nb = so->neighbors;
392 c = 0;
393
394 while (c < eob) {
395 int v = 0;
396 v = qcoeff[scan[c]];
397 #if 0 // Hsan: count-based probability update not yet supported (i.e. use default)
398 ++eob_branch[band[c]][pt];
399 #endif
400 while (!v) {
401 add_token_no_extra(&t, coef_probs[band[c]][pt], ZERO_TOKEN,
402 counts[band[c]][pt]);
403
404 token_cache[scan[c]] = 0;
405 ++c;
406 pt = get_coef_context(nb, token_cache, c);
407 v = qcoeff[scan[c]];
408 }
409
410 vp9_get_token_extra(v, &token, &extra);
411
412 add_token(&t, coef_probs[band[c]][pt], token, extra, counts[band[c]][pt]);
413
414 token_cache[scan[c]] = eb_vp9_pt_energy_class[token];
415 ++c;
416 pt = get_coef_context(nb, token_cache, c);
417 }
418 if (c < tx_eob) {
419 #if 0 // Hsan: count-based probability update not yet supported (i.e. use default)
420 ++eob_branch[band[c]][pt];
421 #endif
422 add_token_no_extra(&t, coef_probs[band[c]][pt], EOB_TOKEN,
423 counts[band[c]][pt]);
424 }
425
426 *tp = t;
427
428 eb_vp9_set_contexts(xd, pd, plane_bsize, tx_size, c > 0, col, row);
429 }
430
431 struct is_skippable_args {
432 uint16_t *eobs;
433 int *skippable;
434 };
435 #if 0
436 static void is_skippable(int plane, int block, int row, int col,
437 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *argv) {
438 struct is_skippable_args *args = argv;
439 (void)plane;
440 (void)plane_bsize;
441 (void)tx_size;
442 (void)row;
443 (void)col;
444 args->skippable[0] &= (!args->eobs[block]);
445 }
446
447 // TODO(yaowu): rewrite and optimize this function to remove the usage of
448 // vp9_foreach_transform_block() and simplify is_skippable().
449 int vp9_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
450 int result = 1;
451 struct is_skippable_args args = { x->plane[plane].eobs, &result };
452 eb_vp9_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane, is_skippable,
453 &args);
454 return result;
455 }
456
457 static void has_high_freq_coeff(int plane, int block, int row, int col,
458 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
459 void *argv) {
460 struct is_skippable_args *args = argv;
461 int eobs = (tx_size == TX_4X4) ? 3 : 10;
462 (void)plane;
463 (void)plane_bsize;
464 (void)row;
465 (void)col;
466 *(args->skippable) |= (args->eobs[block] > eobs);
467 }
468
469 int vp9_has_high_freq_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
470 int result = 0;
471 struct is_skippable_args args = { x->plane[plane].eobs, &result };
472 eb_vp9_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane,
473 has_high_freq_coeff, &args);
474 return result;
475 }
476 #endif
eb_vp9_tokenize_sb(struct VP9_COMP * cpi,MACROBLOCKD * const xd,struct ThreadData * td,TOKENEXTRA ** t,int dry_run,int seg_skip,BLOCK_SIZE bsize)477 void eb_vp9_tokenize_sb(struct VP9_COMP *cpi, MACROBLOCKD *const xd, struct ThreadData *td,
478 TOKENEXTRA **t, int dry_run, int seg_skip,
479 BLOCK_SIZE bsize) {
480 #if 0
481 MACROBLOCK *const x = &td->mb;
482 MACROBLOCKD *const xd = &x->e_mbd;
483 #endif
484 ModeInfo *const mi = xd->mi[0];
485
486 #if 0 // Hsan: count-based probability update not yet supported (i.e. do not increment, and use default)
487 const int ctx = vp9_get_skip_context(xd);
488 #endif
489 struct tokenize_b_args arg = { cpi, td, t };
490
491 if (seg_skip) {
492 assert(mi->skip);
493 }
494
495 if (mi->skip) {
496 #if 0 // Hsan: count-based probability update not yet supported (i.e. do not increment, and use default)
497 if (!dry_run && !seg_skip) ++td->counts->skip[ctx][1];
498 #endif
499 reset_skip_context(xd, bsize);
500 return;
501 }
502
503 if (!dry_run) {
504 #if 0 // Hsan: count-based probability update not yet supported (i.e. do not increment, and use default)
505 ++td->counts->skip[ctx][0];
506 #endif
507 eb_vp9_foreach_transformed_block(xd, bsize, tokenize_b, &arg);
508 } else {
509 eb_vp9_foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg);
510 }
511 }
512