1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * V4L2 VP9 helpers.
4  *
5  * Copyright (C) 2021 Collabora, Ltd.
6  *
7  * Author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
8  */
9 
10 #include <linux/module.h>
11 
12 #include <media/v4l2-vp9.h>
13 
14 const u8 v4l2_vp9_kf_y_mode_prob[10][10][9] = {
15 	{
16 		/* above = dc */
17 		{ 137,  30,  42, 148, 151, 207,  70,  52,  91 }, /*left = dc  */
18 		{  92,  45, 102, 136, 116, 180,  74,  90, 100 }, /*left = v   */
19 		{  73,  32,  19, 187, 222, 215,  46,  34, 100 }, /*left = h   */
20 		{  91,  30,  32, 116, 121, 186,  93,  86,  94 }, /*left = d45 */
21 		{  72,  35,  36, 149,  68, 206,  68,  63, 105 }, /*left = d135*/
22 		{  73,  31,  28, 138,  57, 124,  55, 122, 151 }, /*left = d117*/
23 		{  67,  23,  21, 140, 126, 197,  40,  37, 171 }, /*left = d153*/
24 		{  86,  27,  28, 128, 154, 212,  45,  43,  53 }, /*left = d207*/
25 		{  74,  32,  27, 107,  86, 160,  63, 134, 102 }, /*left = d63 */
26 		{  59,  67,  44, 140, 161, 202,  78,  67, 119 }, /*left = tm  */
27 	}, {  /* above = v */
28 		{  63,  36, 126, 146, 123, 158,  60,  90,  96 }, /*left = dc  */
29 		{  43,  46, 168, 134, 107, 128,  69, 142,  92 }, /*left = v   */
30 		{  44,  29,  68, 159, 201, 177,  50,  57,  77 }, /*left = h   */
31 		{  58,  38,  76, 114,  97, 172,  78, 133,  92 }, /*left = d45 */
32 		{  46,  41,  76, 140,  63, 184,  69, 112,  57 }, /*left = d135*/
33 		{  38,  32,  85, 140,  46, 112,  54, 151, 133 }, /*left = d117*/
34 		{  39,  27,  61, 131, 110, 175,  44,  75, 136 }, /*left = d153*/
35 		{  52,  30,  74, 113, 130, 175,  51,  64,  58 }, /*left = d207*/
36 		{  47,  35,  80, 100,  74, 143,  64, 163,  74 }, /*left = d63 */
37 		{  36,  61, 116, 114, 128, 162,  80, 125,  82 }, /*left = tm  */
38 	}, {  /* above = h */
39 		{  82,  26,  26, 171, 208, 204,  44,  32, 105 }, /*left = dc  */
40 		{  55,  44,  68, 166, 179, 192,  57,  57, 108 }, /*left = v   */
41 		{  42,  26,  11, 199, 241, 228,  23,  15,  85 }, /*left = h   */
42 		{  68,  42,  19, 131, 160, 199,  55,  52,  83 }, /*left = d45 */
43 		{  58,  50,  25, 139, 115, 232,  39,  52, 118 }, /*left = d135*/
44 		{  50,  35,  33, 153, 104, 162,  64,  59, 131 }, /*left = d117*/
45 		{  44,  24,  16, 150, 177, 202,  33,  19, 156 }, /*left = d153*/
46 		{  55,  27,  12, 153, 203, 218,  26,  27,  49 }, /*left = d207*/
47 		{  53,  49,  21, 110, 116, 168,  59,  80,  76 }, /*left = d63 */
48 		{  38,  72,  19, 168, 203, 212,  50,  50, 107 }, /*left = tm  */
49 	}, {  /* above = d45 */
50 		{ 103,  26,  36, 129, 132, 201,  83,  80,  93 }, /*left = dc  */
51 		{  59,  38,  83, 112, 103, 162,  98, 136,  90 }, /*left = v   */
52 		{  62,  30,  23, 158, 200, 207,  59,  57,  50 }, /*left = h   */
53 		{  67,  30,  29,  84,  86, 191, 102,  91,  59 }, /*left = d45 */
54 		{  60,  32,  33, 112,  71, 220,  64,  89, 104 }, /*left = d135*/
55 		{  53,  26,  34, 130,  56, 149,  84, 120, 103 }, /*left = d117*/
56 		{  53,  21,  23, 133, 109, 210,  56,  77, 172 }, /*left = d153*/
57 		{  77,  19,  29, 112, 142, 228,  55,  66,  36 }, /*left = d207*/
58 		{  61,  29,  29,  93,  97, 165,  83, 175, 162 }, /*left = d63 */
59 		{  47,  47,  43, 114, 137, 181, 100,  99,  95 }, /*left = tm  */
60 	}, {  /* above = d135 */
61 		{  69,  23,  29, 128,  83, 199,  46,  44, 101 }, /*left = dc  */
62 		{  53,  40,  55, 139,  69, 183,  61,  80, 110 }, /*left = v   */
63 		{  40,  29,  19, 161, 180, 207,  43,  24,  91 }, /*left = h   */
64 		{  60,  34,  19, 105,  61, 198,  53,  64,  89 }, /*left = d45 */
65 		{  52,  31,  22, 158,  40, 209,  58,  62,  89 }, /*left = d135*/
66 		{  44,  31,  29, 147,  46, 158,  56, 102, 198 }, /*left = d117*/
67 		{  35,  19,  12, 135,  87, 209,  41,  45, 167 }, /*left = d153*/
68 		{  55,  25,  21, 118,  95, 215,  38,  39,  66 }, /*left = d207*/
69 		{  51,  38,  25, 113,  58, 164,  70,  93,  97 }, /*left = d63 */
70 		{  47,  54,  34, 146, 108, 203,  72, 103, 151 }, /*left = tm  */
71 	}, {  /* above = d117 */
72 		{  64,  19,  37, 156,  66, 138,  49,  95, 133 }, /*left = dc  */
73 		{  46,  27,  80, 150,  55, 124,  55, 121, 135 }, /*left = v   */
74 		{  36,  23,  27, 165, 149, 166,  54,  64, 118 }, /*left = h   */
75 		{  53,  21,  36, 131,  63, 163,  60, 109,  81 }, /*left = d45 */
76 		{  40,  26,  35, 154,  40, 185,  51,  97, 123 }, /*left = d135*/
77 		{  35,  19,  34, 179,  19,  97,  48, 129, 124 }, /*left = d117*/
78 		{  36,  20,  26, 136,  62, 164,  33,  77, 154 }, /*left = d153*/
79 		{  45,  18,  32, 130,  90, 157,  40,  79,  91 }, /*left = d207*/
80 		{  45,  26,  28, 129,  45, 129,  49, 147, 123 }, /*left = d63 */
81 		{  38,  44,  51, 136,  74, 162,  57,  97, 121 }, /*left = tm  */
82 	}, {  /* above = d153 */
83 		{  75,  17,  22, 136, 138, 185,  32,  34, 166 }, /*left = dc  */
84 		{  56,  39,  58, 133, 117, 173,  48,  53, 187 }, /*left = v   */
85 		{  35,  21,  12, 161, 212, 207,  20,  23, 145 }, /*left = h   */
86 		{  56,  29,  19, 117, 109, 181,  55,  68, 112 }, /*left = d45 */
87 		{  47,  29,  17, 153,  64, 220,  59,  51, 114 }, /*left = d135*/
88 		{  46,  16,  24, 136,  76, 147,  41,  64, 172 }, /*left = d117*/
89 		{  34,  17,  11, 108, 152, 187,  13,  15, 209 }, /*left = d153*/
90 		{  51,  24,  14, 115, 133, 209,  32,  26, 104 }, /*left = d207*/
91 		{  55,  30,  18, 122,  79, 179,  44,  88, 116 }, /*left = d63 */
92 		{  37,  49,  25, 129, 168, 164,  41,  54, 148 }, /*left = tm  */
93 	}, {  /* above = d207 */
94 		{  82,  22,  32, 127, 143, 213,  39,  41,  70 }, /*left = dc  */
95 		{  62,  44,  61, 123, 105, 189,  48,  57,  64 }, /*left = v   */
96 		{  47,  25,  17, 175, 222, 220,  24,  30,  86 }, /*left = h   */
97 		{  68,  36,  17, 106, 102, 206,  59,  74,  74 }, /*left = d45 */
98 		{  57,  39,  23, 151,  68, 216,  55,  63,  58 }, /*left = d135*/
99 		{  49,  30,  35, 141,  70, 168,  82,  40, 115 }, /*left = d117*/
100 		{  51,  25,  15, 136, 129, 202,  38,  35, 139 }, /*left = d153*/
101 		{  68,  26,  16, 111, 141, 215,  29,  28,  28 }, /*left = d207*/
102 		{  59,  39,  19, 114,  75, 180,  77, 104,  42 }, /*left = d63 */
103 		{  40,  61,  26, 126, 152, 206,  61,  59,  93 }, /*left = tm  */
104 	}, {  /* above = d63 */
105 		{  78,  23,  39, 111, 117, 170,  74, 124,  94 }, /*left = dc  */
106 		{  48,  34,  86, 101,  92, 146,  78, 179, 134 }, /*left = v   */
107 		{  47,  22,  24, 138, 187, 178,  68,  69,  59 }, /*left = h   */
108 		{  56,  25,  33, 105, 112, 187,  95, 177, 129 }, /*left = d45 */
109 		{  48,  31,  27, 114,  63, 183,  82, 116,  56 }, /*left = d135*/
110 		{  43,  28,  37, 121,  63, 123,  61, 192, 169 }, /*left = d117*/
111 		{  42,  17,  24, 109,  97, 177,  56,  76, 122 }, /*left = d153*/
112 		{  58,  18,  28, 105, 139, 182,  70,  92,  63 }, /*left = d207*/
113 		{  46,  23,  32,  74,  86, 150,  67, 183,  88 }, /*left = d63 */
114 		{  36,  38,  48,  92, 122, 165,  88, 137,  91 }, /*left = tm  */
115 	}, {  /* above = tm */
116 		{  65,  70,  60, 155, 159, 199,  61,  60,  81 }, /*left = dc  */
117 		{  44,  78, 115, 132, 119, 173,  71, 112,  93 }, /*left = v   */
118 		{  39,  38,  21, 184, 227, 206,  42,  32,  64 }, /*left = h   */
119 		{  58,  47,  36, 124, 137, 193,  80,  82,  78 }, /*left = d45 */
120 		{  49,  50,  35, 144,  95, 205,  63,  78,  59 }, /*left = d135*/
121 		{  41,  53,  52, 148,  71, 142,  65, 128,  51 }, /*left = d117*/
122 		{  40,  36,  28, 143, 143, 202,  40,  55, 137 }, /*left = d153*/
123 		{  52,  34,  29, 129, 183, 227,  42,  35,  43 }, /*left = d207*/
124 		{  42,  44,  44, 104, 105, 164,  64, 130,  80 }, /*left = d63 */
125 		{  43,  81,  53, 140, 169, 204,  68,  84,  72 }, /*left = tm  */
126 	}
127 };
128 EXPORT_SYMBOL_GPL(v4l2_vp9_kf_y_mode_prob);
129 
130 const u8 v4l2_vp9_kf_partition_probs[16][3] = {
131 	/* 8x8 -> 4x4 */
132 	{ 158,  97,  94 },	/* a/l both not split   */
133 	{  93,  24,  99 },	/* a split, l not split */
134 	{  85, 119,  44 },	/* l split, a not split */
135 	{  62,  59,  67 },	/* a/l both split       */
136 	/* 16x16 -> 8x8 */
137 	{ 149,  53,  53 },	/* a/l both not split   */
138 	{  94,  20,  48 },	/* a split, l not split */
139 	{  83,  53,  24 },	/* l split, a not split */
140 	{  52,  18,  18 },	/* a/l both split       */
141 	/* 32x32 -> 16x16 */
142 	{ 150,  40,  39 },	/* a/l both not split   */
143 	{  78,  12,  26 },	/* a split, l not split */
144 	{  67,  33,  11 },	/* l split, a not split */
145 	{  24,   7,   5 },	/* a/l both split       */
146 	/* 64x64 -> 32x32 */
147 	{ 174,  35,  49 },	/* a/l both not split   */
148 	{  68,  11,  27 },	/* a split, l not split */
149 	{  57,  15,   9 },	/* l split, a not split */
150 	{  12,   3,   3 },	/* a/l both split       */
151 };
152 EXPORT_SYMBOL_GPL(v4l2_vp9_kf_partition_probs);
153 
154 const u8 v4l2_vp9_kf_uv_mode_prob[10][9] = {
155 	{ 144,  11,  54, 157, 195, 130,  46,  58, 108 },  /* y = dc   */
156 	{ 118,  15, 123, 148, 131, 101,  44,  93, 131 },  /* y = v    */
157 	{ 113,  12,  23, 188, 226, 142,  26,  32, 125 },  /* y = h    */
158 	{ 120,  11,  50, 123, 163, 135,  64,  77, 103 },  /* y = d45  */
159 	{ 113,   9,  36, 155, 111, 157,  32,  44, 161 },  /* y = d135 */
160 	{ 116,   9,  55, 176,  76,  96,  37,  61, 149 },  /* y = d117 */
161 	{ 115,   9,  28, 141, 161, 167,  21,  25, 193 },  /* y = d153 */
162 	{ 120,  12,  32, 145, 195, 142,  32,  38,  86 },  /* y = d207 */
163 	{ 116,  12,  64, 120, 140, 125,  49, 115, 121 },  /* y = d63  */
164 	{ 102,  19,  66, 162, 182, 122,  35,  59, 128 }   /* y = tm   */
165 };
166 EXPORT_SYMBOL_GPL(v4l2_vp9_kf_uv_mode_prob);
167 
168 const struct v4l2_vp9_frame_context v4l2_vp9_default_probs = {
169 	.tx8 = {
170 		{ 100 },
171 		{  66 },
172 	},
173 	.tx16 = {
174 		{ 20, 152 },
175 		{ 15, 101 },
176 	},
177 	.tx32 = {
178 		{ 3, 136, 37 },
179 		{ 5,  52, 13 },
180 	},
181 	.coef = {
182 		{ /* tx = 4x4 */
183 			{ /* block Type 0 */
184 				{ /* Intra */
185 					{ /* Coeff Band 0 */
186 						{ 195,  29, 183 },
187 						{  84,  49, 136 },
188 						{   8,  42,  71 },
189 						{   0,   0,   0 },
190 						{   0,   0,   0 },
191 						{   0,   0,   0 },
192 					},
193 					{ /* Coeff Band 1 */
194 						{  31, 107, 169 },
195 						{  35,  99, 159 },
196 						{  17,  82, 140 },
197 						{   8,  66, 114 },
198 						{   2,  44,  76 },
199 						{   1,  19,  32 },
200 					},
201 					{ /* Coeff Band 2 */
202 						{  40, 132, 201 },
203 						{  29, 114, 187 },
204 						{  13,  91, 157 },
205 						{   7,  75, 127 },
206 						{   3,  58,  95 },
207 						{   1,  28,  47 },
208 					},
209 					{ /* Coeff Band 3 */
210 						{  69, 142, 221 },
211 						{  42, 122, 201 },
212 						{  15,  91, 159 },
213 						{   6,  67, 121 },
214 						{   1,  42,  77 },
215 						{   1,  17,  31 },
216 					},
217 					{ /* Coeff Band 4 */
218 						{ 102, 148, 228 },
219 						{  67, 117, 204 },
220 						{  17,  82, 154 },
221 						{   6,  59, 114 },
222 						{   2,  39,  75 },
223 						{   1,  15,  29 },
224 					},
225 					{ /* Coeff Band 5 */
226 						{ 156,  57, 233 },
227 						{ 119,  57, 212 },
228 						{  58,  48, 163 },
229 						{  29,  40, 124 },
230 						{  12,  30,  81 },
231 						{   3,  12,  31 }
232 					},
233 				},
234 				{ /* Inter */
235 					{ /* Coeff Band 0 */
236 						{ 191, 107, 226 },
237 						{ 124, 117, 204 },
238 						{  25,  99, 155 },
239 						{   0,   0,   0 },
240 						{   0,   0,   0 },
241 						{   0,   0,   0 },
242 					},
243 					{ /* Coeff Band 1 */
244 						{  29, 148, 210 },
245 						{  37, 126, 194 },
246 						{   8,  93, 157 },
247 						{   2,  68, 118 },
248 						{   1,  39,  69 },
249 						{   1,  17,  33 },
250 					},
251 					{ /* Coeff Band 2 */
252 						{  41, 151, 213 },
253 						{  27, 123, 193 },
254 						{   3,  82, 144 },
255 						{   1,  58, 105 },
256 						{   1,  32,  60 },
257 						{   1,  13,  26 },
258 					},
259 					{ /* Coeff Band 3 */
260 						{  59, 159, 220 },
261 						{  23, 126, 198 },
262 						{   4,  88, 151 },
263 						{   1,  66, 114 },
264 						{   1,  38,  71 },
265 						{   1,  18,  34 },
266 					},
267 					{ /* Coeff Band 4 */
268 						{ 114, 136, 232 },
269 						{  51, 114, 207 },
270 						{  11,  83, 155 },
271 						{   3,  56, 105 },
272 						{   1,  33,  65 },
273 						{   1,  17,  34 },
274 					},
275 					{ /* Coeff Band 5 */
276 						{ 149,  65, 234 },
277 						{ 121,  57, 215 },
278 						{  61,  49, 166 },
279 						{  28,  36, 114 },
280 						{  12,  25,  76 },
281 						{   3,  16,  42 },
282 					},
283 				},
284 			},
285 			{ /* block Type 1 */
286 				{ /* Intra */
287 					{ /* Coeff Band 0 */
288 						{ 214,  49, 220 },
289 						{ 132,  63, 188 },
290 						{  42,  65, 137 },
291 						{   0,   0,   0 },
292 						{   0,   0,   0 },
293 						{   0,   0,   0 },
294 					},
295 					{ /* Coeff Band 1 */
296 						{  85, 137, 221 },
297 						{ 104, 131, 216 },
298 						{  49, 111, 192 },
299 						{  21,  87, 155 },
300 						{   2,  49,  87 },
301 						{   1,  16,  28 },
302 					},
303 					{ /* Coeff Band 2 */
304 						{  89, 163, 230 },
305 						{  90, 137, 220 },
306 						{  29, 100, 183 },
307 						{  10,  70, 135 },
308 						{   2,  42,  81 },
309 						{   1,  17,  33 },
310 					},
311 					{ /* Coeff Band 3 */
312 						{ 108, 167, 237 },
313 						{  55, 133, 222 },
314 						{  15,  97, 179 },
315 						{   4,  72, 135 },
316 						{   1,  45,  85 },
317 						{   1,  19,  38 },
318 					},
319 					{ /* Coeff Band 4 */
320 						{ 124, 146, 240 },
321 						{  66, 124, 224 },
322 						{  17,  88, 175 },
323 						{   4,  58, 122 },
324 						{   1,  36,  75 },
325 						{   1,  18,  37 },
326 					},
327 					{ /* Coeff Band 5 */
328 						{ 141,  79, 241 },
329 						{ 126,  70, 227 },
330 						{  66,  58, 182 },
331 						{  30,  44, 136 },
332 						{  12,  34,  96 },
333 						{   2,  20,  47 },
334 					},
335 				},
336 				{ /* Inter */
337 					{ /* Coeff Band 0 */
338 						{ 229,  99, 249 },
339 						{ 143, 111, 235 },
340 						{  46, 109, 192 },
341 						{   0,   0,   0 },
342 						{   0,   0,   0 },
343 						{   0,   0,   0 },
344 					},
345 					{ /* Coeff Band 1 */
346 						{  82, 158, 236 },
347 						{  94, 146, 224 },
348 						{  25, 117, 191 },
349 						{   9,  87, 149 },
350 						{   3,  56,  99 },
351 						{   1,  33,  57 },
352 					},
353 					{ /* Coeff Band 2 */
354 						{  83, 167, 237 },
355 						{  68, 145, 222 },
356 						{  10, 103, 177 },
357 						{   2,  72, 131 },
358 						{   1,  41,  79 },
359 						{   1,  20,  39 },
360 					},
361 					{ /* Coeff Band 3 */
362 						{  99, 167, 239 },
363 						{  47, 141, 224 },
364 						{  10, 104, 178 },
365 						{   2,  73, 133 },
366 						{   1,  44,  85 },
367 						{   1,  22,  47 },
368 					},
369 					{ /* Coeff Band 4 */
370 						{ 127, 145, 243 },
371 						{  71, 129, 228 },
372 						{  17,  93, 177 },
373 						{   3,  61, 124 },
374 						{   1,  41,  84 },
375 						{   1,  21,  52 },
376 					},
377 					{ /* Coeff Band 5 */
378 						{ 157,  78, 244 },
379 						{ 140,  72, 231 },
380 						{  69,  58, 184 },
381 						{  31,  44, 137 },
382 						{  14,  38, 105 },
383 						{   8,  23,  61 },
384 					},
385 				},
386 			},
387 		},
388 		{ /* tx = 8x8 */
389 			{ /* block Type 0 */
390 				{ /* Intra */
391 					{ /* Coeff Band 0 */
392 						{ 125,  34, 187 },
393 						{  52,  41, 133 },
394 						{   6,  31,  56 },
395 						{   0,   0,   0 },
396 						{   0,   0,   0 },
397 						{   0,   0,   0 },
398 					},
399 					{ /* Coeff Band 1 */
400 						{  37, 109, 153 },
401 						{  51, 102, 147 },
402 						{  23,  87, 128 },
403 						{   8,  67, 101 },
404 						{   1,  41,  63 },
405 						{   1,  19,  29 },
406 					},
407 					{ /* Coeff Band 2 */
408 						{  31, 154, 185 },
409 						{  17, 127, 175 },
410 						{   6,  96, 145 },
411 						{   2,  73, 114 },
412 						{   1,  51,  82 },
413 						{   1,  28,  45 },
414 					},
415 					{ /* Coeff Band 3 */
416 						{  23, 163, 200 },
417 						{  10, 131, 185 },
418 						{   2,  93, 148 },
419 						{   1,  67, 111 },
420 						{   1,  41,  69 },
421 						{   1,  14,  24 },
422 					},
423 					{ /* Coeff Band 4 */
424 						{  29, 176, 217 },
425 						{  12, 145, 201 },
426 						{   3, 101, 156 },
427 						{   1,  69, 111 },
428 						{   1,  39,  63 },
429 						{   1,  14,  23 },
430 					},
431 					{ /* Coeff Band 5 */
432 						{  57, 192, 233 },
433 						{  25, 154, 215 },
434 						{   6, 109, 167 },
435 						{   3,  78, 118 },
436 						{   1,  48,  69 },
437 						{   1,  21,  29 },
438 					},
439 				},
440 				{ /* Inter */
441 					{ /* Coeff Band 0 */
442 						{ 202, 105, 245 },
443 						{ 108, 106, 216 },
444 						{  18,  90, 144 },
445 						{   0,   0,   0 },
446 						{   0,   0,   0 },
447 						{   0,   0,   0 },
448 					},
449 					{ /* Coeff Band 1 */
450 						{  33, 172, 219 },
451 						{  64, 149, 206 },
452 						{  14, 117, 177 },
453 						{   5,  90, 141 },
454 						{   2,  61,  95 },
455 						{   1,  37,  57 },
456 					},
457 					{ /* Coeff Band 2 */
458 						{  33, 179, 220 },
459 						{  11, 140, 198 },
460 						{   1,  89, 148 },
461 						{   1,  60, 104 },
462 						{   1,  33,  57 },
463 						{   1,  12,  21 },
464 					},
465 					{ /* Coeff Band 3 */
466 						{  30, 181, 221 },
467 						{   8, 141, 198 },
468 						{   1,  87, 145 },
469 						{   1,  58, 100 },
470 						{   1,  31,  55 },
471 						{   1,  12,  20 },
472 					},
473 					{ /* Coeff Band 4 */
474 						{  32, 186, 224 },
475 						{   7, 142, 198 },
476 						{   1,  86, 143 },
477 						{   1,  58, 100 },
478 						{   1,  31,  55 },
479 						{   1,  12,  22 },
480 					},
481 					{ /* Coeff Band 5 */
482 						{  57, 192, 227 },
483 						{  20, 143, 204 },
484 						{   3,  96, 154 },
485 						{   1,  68, 112 },
486 						{   1,  42,  69 },
487 						{   1,  19,  32 },
488 					},
489 				},
490 			},
491 			{ /* block Type 1 */
492 				{ /* Intra */
493 					{ /* Coeff Band 0 */
494 						{ 212,  35, 215 },
495 						{ 113,  47, 169 },
496 						{  29,  48, 105 },
497 						{   0,   0,   0 },
498 						{   0,   0,   0 },
499 						{   0,   0,   0 },
500 					},
501 					{ /* Coeff Band 1 */
502 						{  74, 129, 203 },
503 						{ 106, 120, 203 },
504 						{  49, 107, 178 },
505 						{  19,  84, 144 },
506 						{   4,  50,  84 },
507 						{   1,  15,  25 },
508 					},
509 					{ /* Coeff Band 2 */
510 						{  71, 172, 217 },
511 						{  44, 141, 209 },
512 						{  15, 102, 173 },
513 						{   6,  76, 133 },
514 						{   2,  51,  89 },
515 						{   1,  24,  42 },
516 					},
517 					{ /* Coeff Band 3 */
518 						{  64, 185, 231 },
519 						{  31, 148, 216 },
520 						{   8, 103, 175 },
521 						{   3,  74, 131 },
522 						{   1,  46,  81 },
523 						{   1,  18,  30 },
524 					},
525 					{ /* Coeff Band 4 */
526 						{  65, 196, 235 },
527 						{  25, 157, 221 },
528 						{   5, 105, 174 },
529 						{   1,  67, 120 },
530 						{   1,  38,  69 },
531 						{   1,  15,  30 },
532 					},
533 					{ /* Coeff Band 5 */
534 						{  65, 204, 238 },
535 						{  30, 156, 224 },
536 						{   7, 107, 177 },
537 						{   2,  70, 124 },
538 						{   1,  42,  73 },
539 						{   1,  18,  34 },
540 					},
541 				},
542 				{ /* Inter */
543 					{ /* Coeff Band 0 */
544 						{ 225,  86, 251 },
545 						{ 144, 104, 235 },
546 						{  42,  99, 181 },
547 						{   0,   0,   0 },
548 						{   0,   0,   0 },
549 						{   0,   0,   0 },
550 					},
551 					{ /* Coeff Band 1 */
552 						{  85, 175, 239 },
553 						{ 112, 165, 229 },
554 						{  29, 136, 200 },
555 						{  12, 103, 162 },
556 						{   6,  77, 123 },
557 						{   2,  53,  84 },
558 					},
559 					{ /* Coeff Band 2 */
560 						{  75, 183, 239 },
561 						{  30, 155, 221 },
562 						{   3, 106, 171 },
563 						{   1,  74, 128 },
564 						{   1,  44,  76 },
565 						{   1,  17,  28 },
566 					},
567 					{ /* Coeff Band 3 */
568 						{  73, 185, 240 },
569 						{  27, 159, 222 },
570 						{   2, 107, 172 },
571 						{   1,  75, 127 },
572 						{   1,  42,  73 },
573 						{   1,  17,  29 },
574 					},
575 					{ /* Coeff Band 4 */
576 						{  62, 190, 238 },
577 						{  21, 159, 222 },
578 						{   2, 107, 172 },
579 						{   1,  72, 122 },
580 						{   1,  40,  71 },
581 						{   1,  18,  32 },
582 					},
583 					{ /* Coeff Band 5 */
584 						{  61, 199, 240 },
585 						{  27, 161, 226 },
586 						{   4, 113, 180 },
587 						{   1,  76, 129 },
588 						{   1,  46,  80 },
589 						{   1,  23,  41 },
590 					},
591 				},
592 			},
593 		},
594 		{ /* tx = 16x16 */
595 			{ /* block Type 0 */
596 				{ /* Intra */
597 					{ /* Coeff Band 0 */
598 						{   7,  27, 153 },
599 						{   5,  30,  95 },
600 						{   1,  16,  30 },
601 						{   0,   0,   0 },
602 						{   0,   0,   0 },
603 						{   0,   0,   0 },
604 					},
605 					{ /* Coeff Band 1 */
606 						{  50,  75, 127 },
607 						{  57,  75, 124 },
608 						{  27,  67, 108 },
609 						{  10,  54,  86 },
610 						{   1,  33,  52 },
611 						{   1,  12,  18 },
612 					},
613 					{ /* Coeff Band 2 */
614 						{  43, 125, 151 },
615 						{  26, 108, 148 },
616 						{   7,  83, 122 },
617 						{   2,  59,  89 },
618 						{   1,  38,  60 },
619 						{   1,  17,  27 },
620 					},
621 					{ /* Coeff Band 3 */
622 						{  23, 144, 163 },
623 						{  13, 112, 154 },
624 						{   2,  75, 117 },
625 						{   1,  50,  81 },
626 						{   1,  31,  51 },
627 						{   1,  14,  23 },
628 					},
629 					{ /* Coeff Band 4 */
630 						{  18, 162, 185 },
631 						{   6, 123, 171 },
632 						{   1,  78, 125 },
633 						{   1,  51,  86 },
634 						{   1,  31,  54 },
635 						{   1,  14,  23 },
636 					},
637 					{ /* Coeff Band 5 */
638 						{  15, 199, 227 },
639 						{   3, 150, 204 },
640 						{   1,  91, 146 },
641 						{   1,  55,  95 },
642 						{   1,  30,  53 },
643 						{   1,  11,  20 },
644 					}
645 				},
646 				{ /* Inter */
647 					{ /* Coeff Band 0 */
648 						{  19,  55, 240 },
649 						{  19,  59, 196 },
650 						{   3,  52, 105 },
651 						{   0,   0,   0 },
652 						{   0,   0,   0 },
653 						{   0,   0,   0 },
654 					},
655 					{ /* Coeff Band 1 */
656 						{  41, 166, 207 },
657 						{ 104, 153, 199 },
658 						{  31, 123, 181 },
659 						{  14, 101, 152 },
660 						{   5,  72, 106 },
661 						{   1,  36,  52 },
662 					},
663 					{ /* Coeff Band 2 */
664 						{  35, 176, 211 },
665 						{  12, 131, 190 },
666 						{   2,  88, 144 },
667 						{   1,  60, 101 },
668 						{   1,  36,  60 },
669 						{   1,  16,  28 },
670 					},
671 					{ /* Coeff Band 3 */
672 						{  28, 183, 213 },
673 						{   8, 134, 191 },
674 						{   1,  86, 142 },
675 						{   1,  56,  96 },
676 						{   1,  30,  53 },
677 						{   1,  12,  20 },
678 					},
679 					{ /* Coeff Band 4 */
680 						{  20, 190, 215 },
681 						{   4, 135, 192 },
682 						{   1,  84, 139 },
683 						{   1,  53,  91 },
684 						{   1,  28,  49 },
685 						{   1,  11,  20 },
686 					},
687 					{ /* Coeff Band 5 */
688 						{  13, 196, 216 },
689 						{   2, 137, 192 },
690 						{   1,  86, 143 },
691 						{   1,  57,  99 },
692 						{   1,  32,  56 },
693 						{   1,  13,  24 },
694 					},
695 				},
696 			},
697 			{ /* block Type 1 */
698 				{ /* Intra */
699 					{ /* Coeff Band 0 */
700 						{ 211,  29, 217 },
701 						{  96,  47, 156 },
702 						{  22,  43,  87 },
703 						{   0,   0,   0 },
704 						{   0,   0,   0 },
705 						{   0,   0,   0 },
706 					},
707 					{ /* Coeff Band 1 */
708 						{  78, 120, 193 },
709 						{ 111, 116, 186 },
710 						{  46, 102, 164 },
711 						{  15,  80, 128 },
712 						{   2,  49,  76 },
713 						{   1,  18,  28 },
714 					},
715 					{ /* Coeff Band 2 */
716 						{  71, 161, 203 },
717 						{  42, 132, 192 },
718 						{  10,  98, 150 },
719 						{   3,  69, 109 },
720 						{   1,  44,  70 },
721 						{   1,  18,  29 },
722 					},
723 					{ /* Coeff Band 3 */
724 						{  57, 186, 211 },
725 						{  30, 140, 196 },
726 						{   4,  93, 146 },
727 						{   1,  62, 102 },
728 						{   1,  38,  65 },
729 						{   1,  16,  27 },
730 					},
731 					{ /* Coeff Band 4 */
732 						{  47, 199, 217 },
733 						{  14, 145, 196 },
734 						{   1,  88, 142 },
735 						{   1,  57,  98 },
736 						{   1,  36,  62 },
737 						{   1,  15,  26 },
738 					},
739 					{ /* Coeff Band 5 */
740 						{  26, 219, 229 },
741 						{   5, 155, 207 },
742 						{   1,  94, 151 },
743 						{   1,  60, 104 },
744 						{   1,  36,  62 },
745 						{   1,  16,  28 },
746 					}
747 				},
748 				{ /* Inter */
749 					{ /* Coeff Band 0 */
750 						{ 233,  29, 248 },
751 						{ 146,  47, 220 },
752 						{  43,  52, 140 },
753 						{   0,   0,   0 },
754 						{   0,   0,   0 },
755 						{   0,   0,   0 },
756 					},
757 					{ /* Coeff Band 1 */
758 						{ 100, 163, 232 },
759 						{ 179, 161, 222 },
760 						{  63, 142, 204 },
761 						{  37, 113, 174 },
762 						{  26,  89, 137 },
763 						{  18,  68,  97 },
764 					},
765 					{ /* Coeff Band 2 */
766 						{  85, 181, 230 },
767 						{  32, 146, 209 },
768 						{   7, 100, 164 },
769 						{   3,  71, 121 },
770 						{   1,  45,  77 },
771 						{   1,  18,  30 },
772 					},
773 					{ /* Coeff Band 3 */
774 						{  65, 187, 230 },
775 						{  20, 148, 207 },
776 						{   2,  97, 159 },
777 						{   1,  68, 116 },
778 						{   1,  40,  70 },
779 						{   1,  14,  29 },
780 					},
781 					{ /* Coeff Band 4 */
782 						{  40, 194, 227 },
783 						{   8, 147, 204 },
784 						{   1,  94, 155 },
785 						{   1,  65, 112 },
786 						{   1,  39,  66 },
787 						{   1,  14,  26 },
788 					},
789 					{ /* Coeff Band 5 */
790 						{  16, 208, 228 },
791 						{   3, 151, 207 },
792 						{   1,  98, 160 },
793 						{   1,  67, 117 },
794 						{   1,  41,  74 },
795 						{   1,  17,  31 },
796 					},
797 				},
798 			},
799 		},
800 		{ /* tx = 32x32 */
801 			{ /* block Type 0 */
802 				{ /* Intra */
803 					{ /* Coeff Band 0 */
804 						{  17,  38, 140 },
805 						{   7,  34,  80 },
806 						{   1,  17,  29 },
807 						{   0,   0,   0 },
808 						{   0,   0,   0 },
809 						{   0,   0,   0 },
810 					},
811 					{ /* Coeff Band 1 */
812 						{  37,  75, 128 },
813 						{  41,  76, 128 },
814 						{  26,  66, 116 },
815 						{  12,  52,  94 },
816 						{   2,  32,  55 },
817 						{   1,  10,  16 },
818 					},
819 					{ /* Coeff Band 2 */
820 						{  50, 127, 154 },
821 						{  37, 109, 152 },
822 						{  16,  82, 121 },
823 						{   5,  59,  85 },
824 						{   1,  35,  54 },
825 						{   1,  13,  20 },
826 					},
827 					{ /* Coeff Band 3 */
828 						{  40, 142, 167 },
829 						{  17, 110, 157 },
830 						{   2,  71, 112 },
831 						{   1,  44,  72 },
832 						{   1,  27,  45 },
833 						{   1,  11,  17 },
834 					},
835 					{ /* Coeff Band 4 */
836 						{  30, 175, 188 },
837 						{   9, 124, 169 },
838 						{   1,  74, 116 },
839 						{   1,  48,  78 },
840 						{   1,  30,  49 },
841 						{   1,  11,  18 },
842 					},
843 					{ /* Coeff Band 5 */
844 						{  10, 222, 223 },
845 						{   2, 150, 194 },
846 						{   1,  83, 128 },
847 						{   1,  48,  79 },
848 						{   1,  27,  45 },
849 						{   1,  11,  17 },
850 					},
851 				},
852 				{ /* Inter */
853 					{ /* Coeff Band 0 */
854 						{  36,  41, 235 },
855 						{  29,  36, 193 },
856 						{  10,  27, 111 },
857 						{   0,   0,   0 },
858 						{   0,   0,   0 },
859 						{   0,   0,   0 },
860 					},
861 					{ /* Coeff Band 1 */
862 						{  85, 165, 222 },
863 						{ 177, 162, 215 },
864 						{ 110, 135, 195 },
865 						{  57, 113, 168 },
866 						{  23,  83, 120 },
867 						{  10,  49,  61 },
868 					},
869 					{ /* Coeff Band 2 */
870 						{  85, 190, 223 },
871 						{  36, 139, 200 },
872 						{   5,  90, 146 },
873 						{   1,  60, 103 },
874 						{   1,  38,  65 },
875 						{   1,  18,  30 },
876 					},
877 					{ /* Coeff Band 3 */
878 						{  72, 202, 223 },
879 						{  23, 141, 199 },
880 						{   2,  86, 140 },
881 						{   1,  56,  97 },
882 						{   1,  36,  61 },
883 						{   1,  16,  27 },
884 					},
885 					{ /* Coeff Band 4 */
886 						{  55, 218, 225 },
887 						{  13, 145, 200 },
888 						{   1,  86, 141 },
889 						{   1,  57,  99 },
890 						{   1,  35,  61 },
891 						{   1,  13,  22 },
892 					},
893 					{ /* Coeff Band 5 */
894 						{  15, 235, 212 },
895 						{   1, 132, 184 },
896 						{   1,  84, 139 },
897 						{   1,  57,  97 },
898 						{   1,  34,  56 },
899 						{   1,  14,  23 },
900 					},
901 				},
902 			},
903 			{ /* block Type 1 */
904 				{ /* Intra */
905 					{ /* Coeff Band 0 */
906 						{ 181,  21, 201 },
907 						{  61,  37, 123 },
908 						{  10,  38,  71 },
909 						{   0,   0,   0 },
910 						{   0,   0,   0 },
911 						{   0,   0,   0 },
912 					},
913 					{ /* Coeff Band 1 */
914 						{  47, 106, 172 },
915 						{  95, 104, 173 },
916 						{  42,  93, 159 },
917 						{  18,  77, 131 },
918 						{   4,  50,  81 },
919 						{   1,  17,  23 },
920 					},
921 					{ /* Coeff Band 2 */
922 						{  62, 147, 199 },
923 						{  44, 130, 189 },
924 						{  28, 102, 154 },
925 						{  18,  75, 115 },
926 						{   2,  44,  65 },
927 						{   1,  12,  19 },
928 					},
929 					{ /* Coeff Band 3 */
930 						{  55, 153, 210 },
931 						{  24, 130, 194 },
932 						{   3,  93, 146 },
933 						{   1,  61,  97 },
934 						{   1,  31,  50 },
935 						{   1,  10,  16 },
936 					},
937 					{ /* Coeff Band 4 */
938 						{  49, 186, 223 },
939 						{  17, 148, 204 },
940 						{   1,  96, 142 },
941 						{   1,  53,  83 },
942 						{   1,  26,  44 },
943 						{   1,  11,  17 },
944 					},
945 					{ /* Coeff Band 5 */
946 						{  13, 217, 212 },
947 						{   2, 136, 180 },
948 						{   1,  78, 124 },
949 						{   1,  50,  83 },
950 						{   1,  29,  49 },
951 						{   1,  14,  23 },
952 					},
953 				},
954 				{ /* Inter */
955 					{ /* Coeff Band 0 */
956 						{ 197,  13, 247 },
957 						{  82,  17, 222 },
958 						{  25,  17, 162 },
959 						{   0,   0,   0 },
960 						{   0,   0,   0 },
961 						{   0,   0,   0 },
962 					},
963 					{ /* Coeff Band 1 */
964 						{ 126, 186, 247 },
965 						{ 234, 191, 243 },
966 						{ 176, 177, 234 },
967 						{ 104, 158, 220 },
968 						{  66, 128, 186 },
969 						{  55,  90, 137 },
970 					},
971 					{ /* Coeff Band 2 */
972 						{ 111, 197, 242 },
973 						{  46, 158, 219 },
974 						{   9, 104, 171 },
975 						{   2,  65, 125 },
976 						{   1,  44,  80 },
977 						{   1,  17,  91 },
978 					},
979 					{ /* Coeff Band 3 */
980 						{ 104, 208, 245 },
981 						{  39, 168, 224 },
982 						{   3, 109, 162 },
983 						{   1,  79, 124 },
984 						{   1,  50, 102 },
985 						{   1,  43, 102 },
986 					},
987 					{ /* Coeff Band 4 */
988 						{  84, 220, 246 },
989 						{  31, 177, 231 },
990 						{   2, 115, 180 },
991 						{   1,  79, 134 },
992 						{   1,  55,  77 },
993 						{   1,  60,  79 },
994 					},
995 					{ /* Coeff Band 5 */
996 						{  43, 243, 240 },
997 						{   8, 180, 217 },
998 						{   1, 115, 166 },
999 						{   1,  84, 121 },
1000 						{   1,  51,  67 },
1001 						{   1,  16,   6 },
1002 					},
1003 				},
1004 			},
1005 		},
1006 	},
1007 
1008 	.skip = { 192, 128, 64 },
1009 	.inter_mode = {
1010 		{  2, 173, 34 },
1011 		{  7, 145, 85 },
1012 		{  7, 166, 63 },
1013 		{  7,  94, 66 },
1014 		{  8,  64, 46 },
1015 		{ 17,  81, 31 },
1016 		{ 25,  29, 30 },
1017 	},
1018 	.interp_filter = {
1019 		{ 235, 162 },
1020 		{  36, 255 },
1021 		{  34,   3 },
1022 		{ 149, 144 },
1023 	},
1024 	.is_inter = { 9, 102, 187, 225 },
1025 	.comp_mode = { 239, 183, 119, 96, 41 },
1026 	.single_ref = {
1027 		{  33,  16 },
1028 		{  77,  74 },
1029 		{ 142, 142 },
1030 		{ 172, 170 },
1031 		{ 238, 247 },
1032 	},
1033 	.comp_ref = { 50, 126, 123, 221, 226 },
1034 	.y_mode = {
1035 		{  65,  32, 18, 144, 162, 194, 41, 51, 98 },
1036 		{ 132,  68, 18, 165, 217, 196, 45, 40, 78 },
1037 		{ 173,  80, 19, 176, 240, 193, 64, 35, 46 },
1038 		{ 221, 135, 38, 194, 248, 121, 96, 85, 29 },
1039 	},
1040 	.uv_mode = {
1041 		{ 120,   7,  76, 176, 208, 126,  28,  54, 103 } /* y = dc */,
1042 		{  48,  12, 154, 155, 139,  90,  34, 117, 119 } /* y = v */,
1043 		{  67,   6,  25, 204, 243, 158,  13,  21,  96 } /* y = h */,
1044 		{  97,   5,  44, 131, 176, 139,  48,  68,  97 } /* y = d45 */,
1045 		{  83,   5,  42, 156, 111, 152,  26,  49, 152 } /* y = d135 */,
1046 		{  80,   5,  58, 178,  74,  83,  33,  62, 145 } /* y = d117 */,
1047 		{  86,   5,  32, 154, 192, 168,  14,  22, 163 } /* y = d153 */,
1048 		{  85,   5,  32, 156, 216, 148,  19,  29,  73 } /* y = d207 */,
1049 		{  77,   7,  64, 116, 132, 122,  37, 126, 120 } /* y = d63 */,
1050 		{ 101,  21, 107, 181, 192, 103,  19,  67, 125 } /* y = tm */
1051 	},
1052 	.partition = {
1053 		/* 8x8 -> 4x4 */
1054 		{ 199, 122, 141 } /* a/l both not split */,
1055 		{ 147,  63, 159 } /* a split, l not split */,
1056 		{ 148, 133, 118 } /* l split, a not split */,
1057 		{ 121, 104, 114 } /* a/l both split */,
1058 		/* 16x16 -> 8x8 */
1059 		{ 174,  73,  87 } /* a/l both not split */,
1060 		{  92,  41,  83 } /* a split, l not split */,
1061 		{  82,  99,  50 } /* l split, a not split */,
1062 		{  53,  39,  39 } /* a/l both split */,
1063 		/* 32x32 -> 16x16 */
1064 		{ 177,  58,  59 } /* a/l both not split */,
1065 		{  68,  26,  63 } /* a split, l not split */,
1066 		{  52,  79,  25 } /* l split, a not split */,
1067 		{  17,  14,  12 } /* a/l both split */,
1068 		/* 64x64 -> 32x32 */
1069 		{ 222,  34,  30 } /* a/l both not split */,
1070 		{  72,  16,  44 } /* a split, l not split */,
1071 		{  58,  32,  12 } /* l split, a not split */,
1072 		{  10,   7,   6 } /* a/l both split */,
1073 	},
1074 
1075 	.mv = {
1076 		.joint = { 32, 64, 96 },
1077 		.sign = { 128, 128 },
1078 		.classes = {
1079 			{ 224, 144, 192, 168, 192, 176, 192, 198, 198, 245 },
1080 			{ 216, 128, 176, 160, 176, 176, 192, 198, 198, 208 },
1081 		},
1082 		.class0_bit = { 216, 208 },
1083 		.bits = {
1084 			{ 136, 140, 148, 160, 176, 192, 224, 234, 234, 240},
1085 			{ 136, 140, 148, 160, 176, 192, 224, 234, 234, 240},
1086 		},
1087 		.class0_fr = {
1088 			{
1089 				{ 128, 128, 64 },
1090 				{  96, 112, 64 },
1091 			},
1092 			{
1093 				{ 128, 128, 64 },
1094 				{  96, 112, 64 },
1095 			},
1096 		},
1097 		.fr = {
1098 			{ 64, 96, 64 },
1099 			{ 64, 96, 64 },
1100 		},
1101 		.class0_hp = { 160, 160 },
1102 		.hp = { 128, 128 },
1103 	},
1104 };
1105 EXPORT_SYMBOL_GPL(v4l2_vp9_default_probs);
1106 
1107 static u32 fastdiv(u32 dividend, u16 divisor)
1108 {
1109 #define DIV_INV(d)	((u32)(((1ULL << 32) + ((d) - 1)) / (d)))
1110 #define DIVS_INV(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9)	\
1111 	DIV_INV(d0), DIV_INV(d1), DIV_INV(d2), DIV_INV(d3),	\
1112 	DIV_INV(d4), DIV_INV(d5), DIV_INV(d6), DIV_INV(d7),	\
1113 	DIV_INV(d8), DIV_INV(d9)
1114 
1115 	static const u32 inv[] = {
1116 		DIV_INV(2), DIV_INV(3), DIV_INV(4), DIV_INV(5),
1117 		DIV_INV(6), DIV_INV(7), DIV_INV(8), DIV_INV(9),
1118 		DIVS_INV(10, 11, 12, 13, 14, 15, 16, 17, 18, 19),
1119 		DIVS_INV(20, 21, 22, 23, 24, 25, 26, 27, 28, 29),
1120 		DIVS_INV(30, 31, 32, 33, 34, 35, 36, 37, 38, 39),
1121 		DIVS_INV(40, 41, 42, 43, 44, 45, 46, 47, 48, 49),
1122 		DIVS_INV(50, 51, 52, 53, 54, 55, 56, 57, 58, 59),
1123 		DIVS_INV(60, 61, 62, 63, 64, 65, 66, 67, 68, 69),
1124 		DIVS_INV(70, 71, 72, 73, 74, 75, 76, 77, 78, 79),
1125 		DIVS_INV(80, 81, 82, 83, 84, 85, 86, 87, 88, 89),
1126 		DIVS_INV(90, 91, 92, 93, 94, 95, 96, 97, 98, 99),
1127 		DIVS_INV(100, 101, 102, 103, 104, 105, 106, 107, 108, 109),
1128 		DIVS_INV(110, 111, 112, 113, 114, 115, 116, 117, 118, 119),
1129 		DIVS_INV(120, 121, 122, 123, 124, 125, 126, 127, 128, 129),
1130 		DIVS_INV(130, 131, 132, 133, 134, 135, 136, 137, 138, 139),
1131 		DIVS_INV(140, 141, 142, 143, 144, 145, 146, 147, 148, 149),
1132 		DIVS_INV(150, 151, 152, 153, 154, 155, 156, 157, 158, 159),
1133 		DIVS_INV(160, 161, 162, 163, 164, 165, 166, 167, 168, 169),
1134 		DIVS_INV(170, 171, 172, 173, 174, 175, 176, 177, 178, 179),
1135 		DIVS_INV(180, 181, 182, 183, 184, 185, 186, 187, 188, 189),
1136 		DIVS_INV(190, 191, 192, 193, 194, 195, 196, 197, 198, 199),
1137 		DIVS_INV(200, 201, 202, 203, 204, 205, 206, 207, 208, 209),
1138 		DIVS_INV(210, 211, 212, 213, 214, 215, 216, 217, 218, 219),
1139 		DIVS_INV(220, 221, 222, 223, 224, 225, 226, 227, 228, 229),
1140 		DIVS_INV(230, 231, 232, 233, 234, 235, 236, 237, 238, 239),
1141 		DIVS_INV(240, 241, 242, 243, 244, 245, 246, 247, 248, 249),
1142 		DIV_INV(250), DIV_INV(251), DIV_INV(252), DIV_INV(253),
1143 		DIV_INV(254), DIV_INV(255), DIV_INV(256),
1144 	};
1145 
1146 	if (divisor == 0)
1147 		return 0;
1148 	else if (divisor == 1)
1149 		return dividend;
1150 
1151 	if (WARN_ON(divisor - 2 >= ARRAY_SIZE(inv)))
1152 		return dividend;
1153 
1154 	return ((u64)dividend * inv[divisor - 2]) >> 32;
1155 }
1156 
1157 /* 6.3.6 inv_recenter_nonneg(v, m) */
1158 static int inv_recenter_nonneg(int v, int m)
1159 {
1160 	if (v > 2 * m)
1161 		return v;
1162 
1163 	if (v & 1)
1164 		return m - ((v + 1) >> 1);
1165 
1166 	return m + (v >> 1);
1167 }
1168 
1169 /*
1170  * part of 6.3.5 inv_remap_prob(deltaProb, prob)
1171  * delta = inv_map_table[deltaProb] done by userspace
1172  */
1173 static int update_prob(int delta, int prob)
1174 {
1175 	if (!delta)
1176 		return prob;
1177 
1178 	return prob <= 128 ?
1179 		1 + inv_recenter_nonneg(delta, prob - 1) :
1180 		255 - inv_recenter_nonneg(delta, 255 - prob);
1181 }
1182 
1183 /* Counterpart to 6.3.2 tx_mode_probs() */
1184 static void update_tx_probs(struct v4l2_vp9_frame_context *probs,
1185 			    const struct v4l2_ctrl_vp9_compressed_hdr *deltas)
1186 {
1187 	int i;
1188 
1189 	for (i = 0; i < ARRAY_SIZE(probs->tx8); i++) {
1190 		u8 *p8x8 = probs->tx8[i];
1191 		u8 *p16x16 = probs->tx16[i];
1192 		u8 *p32x32 = probs->tx32[i];
1193 		const u8 *d8x8 = deltas->tx8[i];
1194 		const u8 *d16x16 = deltas->tx16[i];
1195 		const u8 *d32x32 = deltas->tx32[i];
1196 
1197 		p8x8[0] = update_prob(d8x8[0], p8x8[0]);
1198 		p16x16[0] = update_prob(d16x16[0], p16x16[0]);
1199 		p16x16[1] = update_prob(d16x16[1], p16x16[1]);
1200 		p32x32[0] = update_prob(d32x32[0], p32x32[0]);
1201 		p32x32[1] = update_prob(d32x32[1], p32x32[1]);
1202 		p32x32[2] = update_prob(d32x32[2], p32x32[2]);
1203 	}
1204 }
1205 
1206 #define BAND_6(band) ((band) == 0 ? 3 : 6)
1207 
1208 static void update_coeff(const u8 deltas[6][6][3], u8 probs[6][6][3])
1209 {
1210 	int l, m, n;
1211 
1212 	for (l = 0; l < 6; l++)
1213 		for (m = 0; m < BAND_6(l); m++) {
1214 			u8 *p = probs[l][m];
1215 			const u8 *d = deltas[l][m];
1216 
1217 			for (n = 0; n < 3; n++)
1218 				p[n] = update_prob(d[n], p[n]);
1219 		}
1220 }
1221 
1222 /* Counterpart to 6.3.7 read_coef_probs() */
1223 static void update_coef_probs(struct v4l2_vp9_frame_context *probs,
1224 			      const struct v4l2_ctrl_vp9_compressed_hdr *deltas,
1225 			      const struct v4l2_ctrl_vp9_frame *dec_params)
1226 {
1227 	int i, j, k;
1228 
1229 	for (i = 0; i < ARRAY_SIZE(probs->coef); i++) {
1230 		for (j = 0; j < ARRAY_SIZE(probs->coef[0]); j++)
1231 			for (k = 0; k < ARRAY_SIZE(probs->coef[0][0]); k++)
1232 				update_coeff(deltas->coef[i][j][k], probs->coef[i][j][k]);
1233 
1234 		if (deltas->tx_mode == i)
1235 			break;
1236 	}
1237 }
1238 
1239 /* Counterpart to 6.3.8 read_skip_prob() */
1240 static void update_skip_probs(struct v4l2_vp9_frame_context *probs,
1241 			      const struct v4l2_ctrl_vp9_compressed_hdr *deltas)
1242 {
1243 	int i;
1244 
1245 	for (i = 0; i < ARRAY_SIZE(probs->skip); i++)
1246 		probs->skip[i] = update_prob(deltas->skip[i], probs->skip[i]);
1247 }
1248 
1249 /* Counterpart to 6.3.9 read_inter_mode_probs() */
1250 static void update_inter_mode_probs(struct v4l2_vp9_frame_context *probs,
1251 				    const struct v4l2_ctrl_vp9_compressed_hdr *deltas)
1252 {
1253 	int i;
1254 
1255 	for (i = 0; i < ARRAY_SIZE(probs->inter_mode); i++) {
1256 		u8 *p = probs->inter_mode[i];
1257 		const u8 *d = deltas->inter_mode[i];
1258 
1259 		p[0] = update_prob(d[0], p[0]);
1260 		p[1] = update_prob(d[1], p[1]);
1261 		p[2] = update_prob(d[2], p[2]);
1262 	}
1263 }
1264 
1265 /* Counterpart to 6.3.10 read_interp_filter_probs() */
1266 static void update_interp_filter_probs(struct v4l2_vp9_frame_context *probs,
1267 				       const struct v4l2_ctrl_vp9_compressed_hdr *deltas)
1268 {
1269 	int i;
1270 
1271 	for (i = 0; i < ARRAY_SIZE(probs->interp_filter); i++) {
1272 		u8 *p = probs->interp_filter[i];
1273 		const u8 *d = deltas->interp_filter[i];
1274 
1275 		p[0] = update_prob(d[0], p[0]);
1276 		p[1] = update_prob(d[1], p[1]);
1277 	}
1278 }
1279 
1280 /* Counterpart to 6.3.11 read_is_inter_probs() */
1281 static void update_is_inter_probs(struct v4l2_vp9_frame_context *probs,
1282 				  const struct v4l2_ctrl_vp9_compressed_hdr *deltas)
1283 {
1284 	int i;
1285 
1286 	for (i = 0; i < ARRAY_SIZE(probs->is_inter); i++)
1287 		probs->is_inter[i] = update_prob(deltas->is_inter[i], probs->is_inter[i]);
1288 }
1289 
1290 /* 6.3.12 frame_reference_mode() done entirely in userspace */
1291 
1292 /* Counterpart to 6.3.13 frame_reference_mode_probs() */
1293 static void
1294 update_frame_reference_mode_probs(unsigned int reference_mode,
1295 				  struct v4l2_vp9_frame_context *probs,
1296 				  const struct v4l2_ctrl_vp9_compressed_hdr *deltas)
1297 {
1298 	int i;
1299 
1300 	if (reference_mode == V4L2_VP9_REFERENCE_MODE_SELECT)
1301 		for (i = 0; i < ARRAY_SIZE(probs->comp_mode); i++)
1302 			probs->comp_mode[i] = update_prob(deltas->comp_mode[i],
1303 							  probs->comp_mode[i]);
1304 
1305 	if (reference_mode != V4L2_VP9_REFERENCE_MODE_COMPOUND_REFERENCE)
1306 		for (i = 0; i < ARRAY_SIZE(probs->single_ref); i++) {
1307 			u8 *p = probs->single_ref[i];
1308 			const u8 *d = deltas->single_ref[i];
1309 
1310 			p[0] = update_prob(d[0], p[0]);
1311 			p[1] = update_prob(d[1], p[1]);
1312 		}
1313 
1314 	if (reference_mode != V4L2_VP9_REFERENCE_MODE_SINGLE_REFERENCE)
1315 		for (i = 0; i < ARRAY_SIZE(probs->comp_ref); i++)
1316 			probs->comp_ref[i] = update_prob(deltas->comp_ref[i], probs->comp_ref[i]);
1317 }
1318 
1319 /* Counterpart to 6.3.14 read_y_mode_probs() */
1320 static void update_y_mode_probs(struct v4l2_vp9_frame_context *probs,
1321 				const struct v4l2_ctrl_vp9_compressed_hdr *deltas)
1322 {
1323 	int i, j;
1324 
1325 	for (i = 0; i < ARRAY_SIZE(probs->y_mode); i++)
1326 		for (j = 0; j < ARRAY_SIZE(probs->y_mode[0]); ++j)
1327 			probs->y_mode[i][j] =
1328 				update_prob(deltas->y_mode[i][j], probs->y_mode[i][j]);
1329 }
1330 
1331 /* Counterpart to 6.3.15 read_partition_probs() */
1332 static void update_partition_probs(struct v4l2_vp9_frame_context *probs,
1333 				   const struct v4l2_ctrl_vp9_compressed_hdr *deltas)
1334 {
1335 	int i, j;
1336 
1337 	for (i = 0; i < 4; i++)
1338 		for (j = 0; j < 4; j++) {
1339 			u8 *p = probs->partition[i * 4 + j];
1340 			const u8 *d = deltas->partition[i * 4 + j];
1341 
1342 			p[0] = update_prob(d[0], p[0]);
1343 			p[1] = update_prob(d[1], p[1]);
1344 			p[2] = update_prob(d[2], p[2]);
1345 		}
1346 }
1347 
1348 static inline int update_mv_prob(int delta, int prob)
1349 {
1350 	if (!delta)
1351 		return prob;
1352 
1353 	return delta;
1354 }
1355 
1356 /* Counterpart to 6.3.16 mv_probs() */
1357 static void update_mv_probs(struct v4l2_vp9_frame_context *probs,
1358 			    const struct v4l2_ctrl_vp9_compressed_hdr *deltas,
1359 			    const struct v4l2_ctrl_vp9_frame *dec_params)
1360 {
1361 	u8 *p = probs->mv.joint;
1362 	const u8 *d = deltas->mv.joint;
1363 	unsigned int i, j;
1364 
1365 	p[0] = update_mv_prob(d[0], p[0]);
1366 	p[1] = update_mv_prob(d[1], p[1]);
1367 	p[2] = update_mv_prob(d[2], p[2]);
1368 
1369 	for (i = 0; i < ARRAY_SIZE(probs->mv.sign); i++) {
1370 		p = probs->mv.sign;
1371 		d = deltas->mv.sign;
1372 		p[i] = update_mv_prob(d[i], p[i]);
1373 
1374 		p = probs->mv.classes[i];
1375 		d = deltas->mv.classes[i];
1376 		for (j = 0; j < ARRAY_SIZE(probs->mv.classes[0]); j++)
1377 			p[j] = update_mv_prob(d[j], p[j]);
1378 
1379 		p = probs->mv.class0_bit;
1380 		d = deltas->mv.class0_bit;
1381 		p[i] = update_mv_prob(d[i], p[i]);
1382 
1383 		p = probs->mv.bits[i];
1384 		d = deltas->mv.bits[i];
1385 		for (j = 0; j < ARRAY_SIZE(probs->mv.bits[0]); j++)
1386 			p[j] = update_mv_prob(d[j], p[j]);
1387 
1388 		for (j = 0; j < ARRAY_SIZE(probs->mv.class0_fr[0]); j++) {
1389 			p = probs->mv.class0_fr[i][j];
1390 			d = deltas->mv.class0_fr[i][j];
1391 
1392 			p[0] = update_mv_prob(d[0], p[0]);
1393 			p[1] = update_mv_prob(d[1], p[1]);
1394 			p[2] = update_mv_prob(d[2], p[2]);
1395 		}
1396 
1397 		p = probs->mv.fr[i];
1398 		d = deltas->mv.fr[i];
1399 		for (j = 0; j < ARRAY_SIZE(probs->mv.fr[i]); j++)
1400 			p[j] = update_mv_prob(d[j], p[j]);
1401 
1402 		if (dec_params->flags & V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV) {
1403 			p = probs->mv.class0_hp;
1404 			d = deltas->mv.class0_hp;
1405 			p[i] = update_mv_prob(d[i], p[i]);
1406 
1407 			p = probs->mv.hp;
1408 			d = deltas->mv.hp;
1409 			p[i] = update_mv_prob(d[i], p[i]);
1410 		}
1411 	}
1412 }
1413 
1414 /* Counterpart to 6.3 compressed_header(), but parsing has been done in userspace. */
1415 void v4l2_vp9_fw_update_probs(struct v4l2_vp9_frame_context *probs,
1416 			      const struct v4l2_ctrl_vp9_compressed_hdr *deltas,
1417 			      const struct v4l2_ctrl_vp9_frame *dec_params)
1418 {
1419 	if (deltas->tx_mode == V4L2_VP9_TX_MODE_SELECT)
1420 		update_tx_probs(probs, deltas);
1421 
1422 	update_coef_probs(probs, deltas, dec_params);
1423 
1424 	update_skip_probs(probs, deltas);
1425 
1426 	if (dec_params->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME ||
1427 	    dec_params->flags & V4L2_VP9_FRAME_FLAG_INTRA_ONLY)
1428 		return;
1429 
1430 	update_inter_mode_probs(probs, deltas);
1431 
1432 	if (dec_params->interpolation_filter == V4L2_VP9_INTERP_FILTER_SWITCHABLE)
1433 		update_interp_filter_probs(probs, deltas);
1434 
1435 	update_is_inter_probs(probs, deltas);
1436 
1437 	update_frame_reference_mode_probs(dec_params->reference_mode, probs, deltas);
1438 
1439 	update_y_mode_probs(probs, deltas);
1440 
1441 	update_partition_probs(probs, deltas);
1442 
1443 	update_mv_probs(probs, deltas, dec_params);
1444 }
1445 EXPORT_SYMBOL_GPL(v4l2_vp9_fw_update_probs);
1446 
1447 u8 v4l2_vp9_reset_frame_ctx(const struct v4l2_ctrl_vp9_frame *dec_params,
1448 			    struct v4l2_vp9_frame_context *frame_context)
1449 {
1450 	int i;
1451 
1452 	u8 fctx_idx = dec_params->frame_context_idx;
1453 
1454 	if (dec_params->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME ||
1455 	    dec_params->flags & V4L2_VP9_FRAME_FLAG_INTRA_ONLY ||
1456 	    dec_params->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT) {
1457 		/*
1458 		 * setup_past_independence()
1459 		 * We do nothing here. Instead of storing default probs in some intermediate
1460 		 * location and then copying from that location to appropriate contexts
1461 		 * in save_probs() below, we skip that step and save default probs directly
1462 		 * to appropriate contexts.
1463 		 */
1464 		if (dec_params->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME ||
1465 		    dec_params->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT ||
1466 		    dec_params->reset_frame_context == V4L2_VP9_RESET_FRAME_CTX_ALL)
1467 			for (i = 0; i < 4; ++i)
1468 				/* save_probs(i) */
1469 				memcpy(&frame_context[i], &v4l2_vp9_default_probs,
1470 				       sizeof(v4l2_vp9_default_probs));
1471 		else if (dec_params->reset_frame_context == V4L2_VP9_RESET_FRAME_CTX_SPEC)
1472 			/* save_probs(fctx_idx) */
1473 			memcpy(&frame_context[fctx_idx], &v4l2_vp9_default_probs,
1474 			       sizeof(v4l2_vp9_default_probs));
1475 		fctx_idx = 0;
1476 	}
1477 
1478 	return fctx_idx;
1479 }
1480 EXPORT_SYMBOL_GPL(v4l2_vp9_reset_frame_ctx);
1481 
1482 /* 8.4.1 Merge prob process */
1483 static u8 merge_prob(u8 pre_prob, u32 ct0, u32 ct1, u16 count_sat, u32 max_update_factor)
1484 {
1485 	u32 den, prob, count, factor;
1486 
1487 	den = ct0 + ct1;
1488 	if (!den) {
1489 		/*
1490 		 * prob = 128, count = 0, update_factor = 0
1491 		 * Round2's argument: pre_prob * 256
1492 		 * (pre_prob * 256 + 128) >> 8 == pre_prob
1493 		 */
1494 		return pre_prob;
1495 	}
1496 
1497 	prob = clamp(((ct0 << 8) + (den >> 1)) / den, (u32)1, (u32)255);
1498 	count = min_t(u32, den, count_sat);
1499 	factor = fastdiv(max_update_factor * count, count_sat);
1500 
1501 	/*
1502 	 * Round2(pre_prob * (256 - factor) + prob * factor, 8)
1503 	 * Round2(pre_prob * 256 + (prob - pre_prob) * factor, 8)
1504 	 * (pre_prob * 256 >> 8) + (((prob - pre_prob) * factor + 128) >> 8)
1505 	 */
1506 	return pre_prob + (((prob - pre_prob) * factor + 128) >> 8);
1507 }
1508 
1509 static inline u8 noncoef_merge_prob(u8 pre_prob, u32 ct0, u32 ct1)
1510 {
1511 	return merge_prob(pre_prob, ct0, ct1, 20, 128);
1512 }
1513 
1514 /* 8.4.2 Merge probs process */
1515 /*
1516  * merge_probs() is a recursive function in the spec. We avoid recursion in the kernel.
1517  * That said, the "tree" parameter of merge_probs() controls how deep the recursion goes.
1518  * It turns out that in all cases the recursive calls boil down to a short-ish series
1519  * of merge_prob() invocations (note no "s").
1520  *
1521  * Variant A
1522  * ---------
1523  * merge_probs(small_token_tree, 2):
1524  *	merge_prob(p[1], c[0], c[1] + c[2])
1525  *	merge_prob(p[2], c[1], c[2])
1526  *
1527  * Variant B
1528  * ---------
1529  * merge_probs(binary_tree, 0) or
1530  * merge_probs(tx_size_8_tree, 0):
1531  *	merge_prob(p[0], c[0], c[1])
1532  *
1533  * Variant C
1534  * ---------
1535  * merge_probs(inter_mode_tree, 0):
1536  *	merge_prob(p[0], c[2], c[1] + c[0] + c[3])
1537  *	merge_prob(p[1], c[0], c[1] + c[3])
1538  *	merge_prob(p[2], c[1], c[3])
1539  *
1540  * Variant D
1541  * ---------
1542  * merge_probs(intra_mode_tree, 0):
1543  *	merge_prob(p[0], c[0], c[1] + ... + c[9])
1544  *	merge_prob(p[1], c[9], c[1] + ... + c[8])
1545  *	merge_prob(p[2], c[1], c[2] + ... + c[8])
1546  *	merge_prob(p[3], c[2] + c[4] + c[5], c[3] + c[8] + c[6] + c[7])
1547  *	merge_prob(p[4], c[2], c[4] + c[5])
1548  *	merge_prob(p[5], c[4], c[5])
1549  *	merge_prob(p[6], c[3], c[8] + c[6] + c[7])
1550  *	merge_prob(p[7], c[8], c[6] + c[7])
1551  *	merge_prob(p[8], c[6], c[7])
1552  *
1553  * Variant E
1554  * ---------
1555  * merge_probs(partition_tree, 0) or
1556  * merge_probs(tx_size_32_tree, 0) or
1557  * merge_probs(mv_joint_tree, 0) or
1558  * merge_probs(mv_fr_tree, 0):
1559  *	merge_prob(p[0], c[0], c[1] + c[2] + c[3])
1560  *	merge_prob(p[1], c[1], c[2] + c[3])
1561  *	merge_prob(p[2], c[2], c[3])
1562  *
1563  * Variant F
1564  * ---------
1565  * merge_probs(interp_filter_tree, 0) or
1566  * merge_probs(tx_size_16_tree, 0):
1567  *	merge_prob(p[0], c[0], c[1] + c[2])
1568  *	merge_prob(p[1], c[1], c[2])
1569  *
1570  * Variant G
1571  * ---------
1572  * merge_probs(mv_class_tree, 0):
1573  *	merge_prob(p[0], c[0], c[1] + ... + c[10])
1574  *	merge_prob(p[1], c[1], c[2] + ... + c[10])
1575  *	merge_prob(p[2], c[2] + c[3], c[4] + ... + c[10])
1576  *	merge_prob(p[3], c[2], c[3])
1577  *	merge_prob(p[4], c[4] + c[5], c[6] + ... + c[10])
1578  *	merge_prob(p[5], c[4], c[5])
1579  *	merge_prob(p[6], c[6], c[7] + ... + c[10])
1580  *	merge_prob(p[7], c[7] + c[8], c[9] + c[10])
1581  *	merge_prob(p[8], c[7], c[8])
1582  *	merge_prob(p[9], c[9], [10])
1583  */
1584 
1585 static inline void merge_probs_variant_a(u8 *p, const u32 *c, u16 count_sat, u32 update_factor)
1586 {
1587 	p[1] = merge_prob(p[1], c[0], c[1] + c[2], count_sat, update_factor);
1588 	p[2] = merge_prob(p[2], c[1], c[2], count_sat, update_factor);
1589 }
1590 
1591 static inline void merge_probs_variant_b(u8 *p, const u32 *c, u16 count_sat, u32 update_factor)
1592 {
1593 	p[0] = merge_prob(p[0], c[0], c[1], count_sat, update_factor);
1594 }
1595 
1596 static inline void merge_probs_variant_c(u8 *p, const u32 *c)
1597 {
1598 	p[0] = noncoef_merge_prob(p[0], c[2], c[1] + c[0] + c[3]);
1599 	p[1] = noncoef_merge_prob(p[1], c[0], c[1] + c[3]);
1600 	p[2] = noncoef_merge_prob(p[2], c[1], c[3]);
1601 }
1602 
1603 static void merge_probs_variant_d(u8 *p, const u32 *c)
1604 {
1605 	u32 sum = 0, s2;
1606 
1607 	sum = c[1] + c[2] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9];
1608 
1609 	p[0] = noncoef_merge_prob(p[0], c[0], sum);
1610 	sum -= c[9];
1611 	p[1] = noncoef_merge_prob(p[1], c[9], sum);
1612 	sum -= c[1];
1613 	p[2] = noncoef_merge_prob(p[2], c[1], sum);
1614 	s2 = c[2] + c[4] + c[5];
1615 	sum -= s2;
1616 	p[3] = noncoef_merge_prob(p[3], s2, sum);
1617 	s2 -= c[2];
1618 	p[4] = noncoef_merge_prob(p[4], c[2], s2);
1619 	p[5] = noncoef_merge_prob(p[5], c[4], c[5]);
1620 	sum -= c[3];
1621 	p[6] = noncoef_merge_prob(p[6], c[3], sum);
1622 	sum -= c[8];
1623 	p[7] = noncoef_merge_prob(p[7], c[8], sum);
1624 	p[8] = noncoef_merge_prob(p[8], c[6], c[7]);
1625 }
1626 
1627 static inline void merge_probs_variant_e(u8 *p, const u32 *c)
1628 {
1629 	p[0] = noncoef_merge_prob(p[0], c[0], c[1] + c[2] + c[3]);
1630 	p[1] = noncoef_merge_prob(p[1], c[1], c[2] + c[3]);
1631 	p[2] = noncoef_merge_prob(p[2], c[2], c[3]);
1632 }
1633 
1634 static inline void merge_probs_variant_f(u8 *p, const u32 *c)
1635 {
1636 	p[0] = noncoef_merge_prob(p[0], c[0], c[1] + c[2]);
1637 	p[1] = noncoef_merge_prob(p[1], c[1], c[2]);
1638 }
1639 
1640 static void merge_probs_variant_g(u8 *p, const u32 *c)
1641 {
1642 	u32 sum;
1643 
1644 	sum = c[1] + c[2] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8] + c[9] + c[10];
1645 	p[0] = noncoef_merge_prob(p[0], c[0], sum);
1646 	sum -= c[1];
1647 	p[1] = noncoef_merge_prob(p[1], c[1], sum);
1648 	sum -= c[2] + c[3];
1649 	p[2] = noncoef_merge_prob(p[2], c[2] + c[3], sum);
1650 	p[3] = noncoef_merge_prob(p[3], c[2], c[3]);
1651 	sum -= c[4] + c[5];
1652 	p[4] = noncoef_merge_prob(p[4], c[4] + c[5], sum);
1653 	p[5] = noncoef_merge_prob(p[5], c[4], c[5]);
1654 	sum -= c[6];
1655 	p[6] = noncoef_merge_prob(p[6], c[6], sum);
1656 	p[7] = noncoef_merge_prob(p[7], c[7] + c[8], c[9] + c[10]);
1657 	p[8] = noncoef_merge_prob(p[8], c[7], c[8]);
1658 	p[9] = noncoef_merge_prob(p[9], c[9], c[10]);
1659 }
1660 
1661 /* 8.4.3 Coefficient probability adaptation process */
1662 static inline void adapt_probs_variant_a_coef(u8 *p, const u32 *c, u32 update_factor)
1663 {
1664 	merge_probs_variant_a(p, c, 24, update_factor);
1665 }
1666 
1667 static inline void adapt_probs_variant_b_coef(u8 *p, const u32 *c, u32 update_factor)
1668 {
1669 	merge_probs_variant_b(p, c, 24, update_factor);
1670 }
1671 
1672 static void _adapt_coeff(unsigned int i, unsigned int j, unsigned int k,
1673 			 struct v4l2_vp9_frame_context *probs,
1674 			 const struct v4l2_vp9_frame_symbol_counts *counts,
1675 			 u32 uf)
1676 {
1677 	s32 l, m;
1678 
1679 	for (l = 0; l < ARRAY_SIZE(probs->coef[0][0][0]); l++) {
1680 		for (m = 0; m < BAND_6(l); m++) {
1681 			u8 *p = probs->coef[i][j][k][l][m];
1682 			const u32 counts_more_coefs[2] = {
1683 				*counts->eob[i][j][k][l][m][1],
1684 				*counts->eob[i][j][k][l][m][0] - *counts->eob[i][j][k][l][m][1],
1685 			};
1686 
1687 			adapt_probs_variant_a_coef(p, *counts->coeff[i][j][k][l][m], uf);
1688 			adapt_probs_variant_b_coef(p, counts_more_coefs, uf);
1689 		}
1690 	}
1691 }
1692 
1693 static void _adapt_coef_probs(struct v4l2_vp9_frame_context *probs,
1694 			      const struct v4l2_vp9_frame_symbol_counts *counts,
1695 			      unsigned int uf)
1696 {
1697 	unsigned int i, j, k;
1698 
1699 	for (i = 0; i < ARRAY_SIZE(probs->coef); i++)
1700 		for (j = 0; j < ARRAY_SIZE(probs->coef[0]); j++)
1701 			for (k = 0; k < ARRAY_SIZE(probs->coef[0][0]); k++)
1702 				_adapt_coeff(i, j, k, probs, counts, uf);
1703 }
1704 
1705 void v4l2_vp9_adapt_coef_probs(struct v4l2_vp9_frame_context *probs,
1706 			       struct v4l2_vp9_frame_symbol_counts *counts,
1707 			       bool use_128,
1708 			       bool frame_is_intra)
1709 {
1710 	if (frame_is_intra) {
1711 		_adapt_coef_probs(probs, counts, 112);
1712 	} else {
1713 		if (use_128)
1714 			_adapt_coef_probs(probs, counts, 128);
1715 		else
1716 			_adapt_coef_probs(probs, counts, 112);
1717 	}
1718 }
1719 EXPORT_SYMBOL_GPL(v4l2_vp9_adapt_coef_probs);
1720 
1721 /* 8.4.4 Non coefficient probability adaptation process, adapt_probs() */
1722 static inline void adapt_probs_variant_b(u8 *p, const u32 *c)
1723 {
1724 	merge_probs_variant_b(p, c, 20, 128);
1725 }
1726 
1727 static inline void adapt_probs_variant_c(u8 *p, const u32 *c)
1728 {
1729 	merge_probs_variant_c(p, c);
1730 }
1731 
1732 static inline void adapt_probs_variant_d(u8 *p, const u32 *c)
1733 {
1734 	merge_probs_variant_d(p, c);
1735 }
1736 
1737 static inline void adapt_probs_variant_e(u8 *p, const u32 *c)
1738 {
1739 	merge_probs_variant_e(p, c);
1740 }
1741 
1742 static inline void adapt_probs_variant_f(u8 *p, const u32 *c)
1743 {
1744 	merge_probs_variant_f(p, c);
1745 }
1746 
1747 static inline void adapt_probs_variant_g(u8 *p, const u32 *c)
1748 {
1749 	merge_probs_variant_g(p, c);
1750 }
1751 
1752 /* 8.4.4 Non coefficient probability adaptation process, adapt_prob() */
1753 static inline u8 adapt_prob(u8 prob, const u32 counts[2])
1754 {
1755 	return noncoef_merge_prob(prob, counts[0], counts[1]);
1756 }
1757 
1758 /* 8.4.4 Non coefficient probability adaptation process */
1759 void v4l2_vp9_adapt_noncoef_probs(struct v4l2_vp9_frame_context *probs,
1760 				  struct v4l2_vp9_frame_symbol_counts *counts,
1761 				  u8 reference_mode, u8 interpolation_filter, u8 tx_mode,
1762 				  u32 flags)
1763 {
1764 	unsigned int i, j;
1765 
1766 	for (i = 0; i < ARRAY_SIZE(probs->is_inter); i++)
1767 		probs->is_inter[i] = adapt_prob(probs->is_inter[i], (*counts->intra_inter)[i]);
1768 
1769 	for (i = 0; i < ARRAY_SIZE(probs->comp_mode); i++)
1770 		probs->comp_mode[i] = adapt_prob(probs->comp_mode[i], (*counts->comp)[i]);
1771 
1772 	for (i = 0; i < ARRAY_SIZE(probs->comp_ref); i++)
1773 		probs->comp_ref[i] = adapt_prob(probs->comp_ref[i], (*counts->comp_ref)[i]);
1774 
1775 	if (reference_mode != V4L2_VP9_REFERENCE_MODE_COMPOUND_REFERENCE)
1776 		for (i = 0; i < ARRAY_SIZE(probs->single_ref); i++)
1777 			for (j = 0; j < ARRAY_SIZE(probs->single_ref[0]); j++)
1778 				probs->single_ref[i][j] = adapt_prob(probs->single_ref[i][j],
1779 								     (*counts->single_ref)[i][j]);
1780 
1781 	for (i = 0; i < ARRAY_SIZE(probs->inter_mode); i++)
1782 		adapt_probs_variant_c(probs->inter_mode[i], (*counts->mv_mode)[i]);
1783 
1784 	for (i = 0; i < ARRAY_SIZE(probs->y_mode); i++)
1785 		adapt_probs_variant_d(probs->y_mode[i], (*counts->y_mode)[i]);
1786 
1787 	for (i = 0; i < ARRAY_SIZE(probs->uv_mode); i++)
1788 		adapt_probs_variant_d(probs->uv_mode[i], (*counts->uv_mode)[i]);
1789 
1790 	for (i = 0; i < ARRAY_SIZE(probs->partition); i++)
1791 		adapt_probs_variant_e(probs->partition[i], (*counts->partition)[i]);
1792 
1793 	for (i = 0; i < ARRAY_SIZE(probs->skip); i++)
1794 		probs->skip[i] = adapt_prob(probs->skip[i], (*counts->skip)[i]);
1795 
1796 	if (interpolation_filter == V4L2_VP9_INTERP_FILTER_SWITCHABLE)
1797 		for (i = 0; i < ARRAY_SIZE(probs->interp_filter); i++)
1798 			adapt_probs_variant_f(probs->interp_filter[i], (*counts->filter)[i]);
1799 
1800 	if (tx_mode == V4L2_VP9_TX_MODE_SELECT)
1801 		for (i = 0; i < ARRAY_SIZE(probs->tx8); i++) {
1802 			adapt_probs_variant_b(probs->tx8[i], (*counts->tx8p)[i]);
1803 			adapt_probs_variant_f(probs->tx16[i], (*counts->tx16p)[i]);
1804 			adapt_probs_variant_e(probs->tx32[i], (*counts->tx32p)[i]);
1805 		}
1806 
1807 	adapt_probs_variant_e(probs->mv.joint, *counts->mv_joint);
1808 
1809 	for (i = 0; i < ARRAY_SIZE(probs->mv.sign); i++) {
1810 		probs->mv.sign[i] = adapt_prob(probs->mv.sign[i], (*counts->sign)[i]);
1811 
1812 		adapt_probs_variant_g(probs->mv.classes[i], (*counts->classes)[i]);
1813 
1814 		probs->mv.class0_bit[i] = adapt_prob(probs->mv.class0_bit[i], (*counts->class0)[i]);
1815 
1816 		for (j = 0; j < ARRAY_SIZE(probs->mv.bits[0]); j++)
1817 			probs->mv.bits[i][j] = adapt_prob(probs->mv.bits[i][j],
1818 							  (*counts->bits)[i][j]);
1819 
1820 		for (j = 0; j < ARRAY_SIZE(probs->mv.class0_fr[0]); j++)
1821 			adapt_probs_variant_e(probs->mv.class0_fr[i][j],
1822 					      (*counts->class0_fp)[i][j]);
1823 
1824 		adapt_probs_variant_e(probs->mv.fr[i], (*counts->fp)[i]);
1825 
1826 		if (!(flags & V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV))
1827 			continue;
1828 
1829 		probs->mv.class0_hp[i] = adapt_prob(probs->mv.class0_hp[i],
1830 						    (*counts->class0_hp)[i]);
1831 
1832 		probs->mv.hp[i] = adapt_prob(probs->mv.hp[i], (*counts->hp)[i]);
1833 	}
1834 }
1835 EXPORT_SYMBOL_GPL(v4l2_vp9_adapt_noncoef_probs);
1836 
1837 bool
1838 v4l2_vp9_seg_feat_enabled(const u8 *feature_enabled,
1839 			  unsigned int feature,
1840 			  unsigned int segid)
1841 {
1842 	u8 mask = V4L2_VP9_SEGMENT_FEATURE_ENABLED(feature);
1843 
1844 	return !!(feature_enabled[segid] & mask);
1845 }
1846 EXPORT_SYMBOL_GPL(v4l2_vp9_seg_feat_enabled);
1847 
1848 MODULE_LICENSE("GPL");
1849 MODULE_DESCRIPTION("V4L2 VP9 Helpers");
1850 MODULE_AUTHOR("Andrzej Pietrasiewicz <andrzej.p@collabora.com>");
1851