1 /* SPDX-License-Identifier: Apache-2.0 OR MIT */
2 /*
3 * Copyright (c) 2017 Rockchip Electronics Co., Ltd.
4 */
5
6 #define MODULE_TAG "hal_vp8e_entropy"
7
8 #include <string.h>
9
10 #include "mpp_common.h"
11
12 #include "hal_vp8e_base.h"
13 #include "hal_vp8e_entropy.h"
14 #include "hal_vp8e_putbit.h"
15 #include "hal_vp8e_table.h"
16
17 #define COST_BOOL(prob, bin)\
18 ((bin) ? vp8_prob_cost_tbl[255 - (prob)] : vp8_prob_cost_tbl[prob])
19
calc_mvprob(RK_U32 left,RK_U32 right,RK_U32 prob)20 static RK_U32 calc_mvprob(RK_U32 left, RK_U32 right, RK_U32 prob)
21 {
22 RK_U32 p;
23
24 if (left + right) {
25 p = (left * 255) / (left + right);
26 p &= -2;
27 if (!p)
28 p = 1;
29 } else
30 p = prob;
31
32 return p;
33 }
34
update_prob(RK_U32 prob,RK_U32 left,RK_U32 right,RK_U32 old_prob,RK_U32 new_prob,RK_U32 fixed)35 static RK_U32 update_prob(RK_U32 prob, RK_U32 left, RK_U32 right,
36 RK_U32 old_prob, RK_U32 new_prob, RK_U32 fixed)
37 {
38 RK_S32 u, s;
39
40 u = (RK_S32)fixed + ((vp8_prob_cost_tbl[255 - prob] - vp8_prob_cost_tbl[prob]) >> 8);
41 s = ((RK_S32)left * (vp8_prob_cost_tbl[old_prob] - vp8_prob_cost_tbl[new_prob]) +
42 (RK_S32)right * (vp8_prob_cost_tbl[255 - old_prob] - vp8_prob_cost_tbl[255 - new_prob])) >> 8;
43
44 return (s > u);
45 }
46
get_cost_tree(Vp8eTree const * tree,RK_S32 * prob)47 static RK_S32 get_cost_tree(Vp8eTree const *tree, RK_S32 *prob)
48 {
49 RK_S32 bit_cost = 0;
50 RK_S32 value = tree->value;
51 RK_S32 number = tree->number;
52 RK_S32 const *index = tree->index;
53
54 while (number--) {
55 bit_cost += COST_BOOL(prob[*index++], (value >> number) & 1);
56 }
57
58 return bit_cost;
59 }
60
vp8e_init_entropy(void * hal)61 MPP_RET vp8e_init_entropy(void *hal)
62 {
63 HalVp8eCtx *ctx = (HalVp8eCtx *)hal;
64
65 Vp8eVpuBuf *buffers = (Vp8eVpuBuf *)ctx->buffers;
66 Vp8eHalEntropy *entropy = &ctx->entropy;
67
68 entropy->update_coeff_prob_flag = 0;
69 if (!ctx->sps.refresh_entropy || ctx->picbuf.cur_pic->i_frame) {
70 if (!entropy->default_coeff_prob_flag) {
71 memcpy(entropy->coeff_prob, default_prob_coeff_tbl, sizeof(default_prob_coeff_tbl));
72 entropy->update_coeff_prob_flag = 1;
73 }
74 memcpy(entropy->mv_prob, default_prob_mv_tbl, sizeof(default_prob_mv_tbl));
75 entropy->default_coeff_prob_flag = 1;
76 }
77
78 memcpy(entropy->old_coeff_prob, entropy->coeff_prob, sizeof(entropy->coeff_prob));
79 if (ctx->frame_cnt == 0 || !ctx->last_frm_intra)
80 memcpy(entropy->old_mv_prob, entropy->mv_prob, sizeof(entropy->mv_prob));
81
82 entropy->skip_false_prob = default_skip_false_prob_tbl[ctx->rc->qp_hdr];
83
84 if (ctx->frame_cnt == 0)
85 return MPP_OK;
86
87 if ((!ctx->picbuf.cur_pic->ipf) && (!ctx->picbuf.cur_pic->grf) &&
88 (!ctx->picbuf.cur_pic->arf))
89 return MPP_OK;
90
91 if (ctx->prev_frame_lost)
92 return MPP_OK;
93 {
94 RK_S32 i, j, k, l;
95
96 RK_U32 p, left, right;
97 RK_U32 old_p, upd_p = 0;
98
99 RK_U32 type;
100 RK_U32 branch_cnt[2];
101 RK_U16 *p_tmp = NULL;
102
103 RK_U16 *p_cnt = (RK_U16 *)mpp_buffer_get_ptr(buffers->hw_prob_count_buf);
104
105 for (i = 0; i < 4; i++) {
106 for (j = 0; j < 7; j++) {
107 for (k = 0; k < 3; k++) {
108 RK_S32 tmp, ii;
109
110 tmp = i * 7 * 3 + j * 3 + k;
111 tmp += 2 * 4 * 7 * 3;
112 ii = offset_tbl[tmp];
113
114 right = ii >= 0 ? p_cnt[ii] : 0;
115
116 for (l = 2; l--;) {
117 old_p = entropy->coeff_prob[i][j][k][l];
118 old_p = coeff_update_prob_tbl[i][j][k][l];
119
120 tmp -= 4 * 7 * 3;
121 ii = offset_tbl[tmp];
122 left = ii >= 0 ? p_cnt[ii] : 0;
123 if (left + right) {
124 p = ((left * 256) + ((left + right) >> 1)) / (left + right);
125 if (p > 255) p = 255;
126 } else
127 p = old_p;
128
129 if (update_prob(upd_p, left, right, old_p, p, 8)) {
130 entropy->coeff_prob[i][j][k][l] = p;
131 entropy->update_coeff_prob_flag = 1;
132 }
133 right += left;
134 }
135 }
136 }
137 }
138
139 if (entropy->update_coeff_prob_flag)
140 entropy->default_coeff_prob_flag = 0;
141
142 p_tmp = p_cnt + VP8_PROB_COUNT_MV_OFFSET;
143 for (i = 0; i < 2; i++) {
144 left = *p_tmp++;
145 right = *p_tmp++;
146
147 p = calc_mvprob(left, right, entropy->old_mv_prob[i][0]);
148
149 if (update_prob(mv_update_prob_tbl[i][0], left, right,
150 entropy->old_mv_prob[i][0], p, 6))
151 entropy->mv_prob[i][0] = p;
152
153 right += left;
154 left = *p_tmp++;
155 right -= left - p_tmp[0];
156
157 p = calc_mvprob(left, right, entropy->old_mv_prob[i][1]);
158 if (update_prob(mv_update_prob_tbl[i][1], left, right,
159 entropy->old_mv_prob[i][1], p, 6))
160 entropy->mv_prob[i][1] = p;
161
162 for (j = 0; j < 2; j++) {
163 left = *p_tmp++;
164 right = *p_tmp++;
165 p = calc_mvprob(left, right, entropy->old_mv_prob[i][4 + j]);
166 if (update_prob(mv_update_prob_tbl[i][4 + j], left, right,
167 entropy->old_mv_prob[i][4 + j], p, 6))
168 entropy->mv_prob[i][4 + j] = p;
169 branch_cnt[j] = left + right;
170 }
171
172 p = calc_mvprob(branch_cnt[0], branch_cnt[1], entropy->old_mv_prob[i][3]);
173 if (update_prob(mv_update_prob_tbl[i][3], branch_cnt[0], branch_cnt[1],
174 entropy->old_mv_prob[i][3], p, 6))
175 entropy->mv_prob[i][3] = p;
176
177 type = branch_cnt[0] + branch_cnt[1];
178
179 for (j = 0; j < 2; j++) {
180 left = *p_tmp++;
181 right = *p_tmp++;
182 p = calc_mvprob(left, right, entropy->old_mv_prob[i][7 + j]);
183 if (update_prob(mv_update_prob_tbl[i][7 + j], left, right,
184 entropy->old_mv_prob[i][7 + j], p, 6))
185 entropy->mv_prob[i][7 + j] = p;
186 branch_cnt[j] = left + right;
187 }
188
189 p = calc_mvprob(branch_cnt[0], branch_cnt[1], entropy->old_mv_prob[i][6]);
190 if (update_prob(mv_update_prob_tbl[i][6], branch_cnt[0], branch_cnt[1],
191 entropy->old_mv_prob[i][6], p, 6))
192 entropy->mv_prob[i][6] = p;
193
194 p = calc_mvprob(type, branch_cnt[0] + branch_cnt[1],
195 entropy->old_mv_prob[i][2]);
196 if (update_prob(mv_update_prob_tbl[i][2], type, branch_cnt[0] + branch_cnt[1],
197 entropy->old_mv_prob[i][2], p, 6))
198 entropy->mv_prob[i][2] = p;
199 }
200 }
201 {
202 entropy->last_prob = 255;
203 entropy->gf_prob = 128;
204 memcpy(entropy->y_mode_prob, y_mode_prob_tbl, sizeof(y_mode_prob_tbl));
205 memcpy(entropy->uv_mode_prob, uv_mode_prob_tbl, sizeof(uv_mode_prob_tbl));
206 }
207 return MPP_OK;
208 }
209
vp8e_swap_endian(RK_U32 * buf,RK_U32 bytes)210 MPP_RET vp8e_swap_endian(RK_U32 *buf, RK_U32 bytes)
211 {
212 RK_U32 i = 0;
213 RK_S32 words = bytes / 4;
214
215 while (words > 0) {
216 RK_U32 val = buf[i];
217 RK_U32 tmp = 0;
218
219 tmp |= (val & 0xFF) << 24;
220 tmp |= (val & 0xFF00) << 8;
221 tmp |= (val & 0xFF0000) >> 8;
222 tmp |= (val & 0xFF000000) >> 24;
223
224 {
225 RK_U32 val2 = buf[i + 1];
226 RK_U32 tmp2 = 0;
227
228 tmp2 |= (val2 & 0xFF) << 24;
229 tmp2 |= (val2 & 0xFF00) << 8;
230 tmp2 |= (val2 & 0xFF0000) >> 8;
231 tmp2 |= (val2 & 0xFF000000) >> 24;
232
233 buf[i] = tmp2;
234 words--;
235 i++;
236 }
237
238 buf[i] = tmp;
239 words--;
240 i++;
241 }
242 return MPP_OK;
243 }
244
245
vp8e_write_entropy_tables(void * hal)246 MPP_RET vp8e_write_entropy_tables(void *hal)
247 {
248 HalVp8eCtx *ctx = (HalVp8eCtx *)hal;
249
250 Vp8eVpuBuf *buffers = (Vp8eVpuBuf *)ctx->buffers;
251 Vp8eHalEntropy *entropy = &ctx->entropy;
252
253 RK_U8 *table = (RK_U8 *)mpp_buffer_get_ptr(buffers->hw_cabac_table_buf);
254 /* Write probability tables to ASIC linear memory, reg + mem */
255 memset(table, 0, 56); // use 56 bytes
256 table[0] = entropy->skip_false_prob;
257 table[1] = entropy->intra_prob;
258 table[2] = entropy->last_prob;
259 table[3] = entropy->gf_prob;
260 table[4] = entropy->segment_prob[0];
261 table[5] = entropy->segment_prob[1];
262 table[6] = entropy->segment_prob[2];
263
264 table[8] = entropy->y_mode_prob[0];
265 table[9] = entropy->y_mode_prob[1];
266 table[10] = entropy->y_mode_prob[2];
267 table[11] = entropy->y_mode_prob[3];
268 table[12] = entropy->uv_mode_prob[0];
269 table[13] = entropy->uv_mode_prob[1];
270 table[14] = entropy->uv_mode_prob[2];
271
272 /* MV probabilities x+y: short, sign, size 8-9 */
273 table[16] = entropy->mv_prob[1][0];
274 table[17] = entropy->mv_prob[0][0];
275 table[18] = entropy->mv_prob[1][1];
276 table[19] = entropy->mv_prob[0][1];
277 table[20] = entropy->mv_prob[1][17];
278 table[21] = entropy->mv_prob[1][18];
279 table[22] = entropy->mv_prob[0][17];
280 table[23] = entropy->mv_prob[0][18];
281 {
282 RK_S32 i, j, k, l;
283 /* MV X size */
284 for (i = 0; i < 8; i++)
285 table[24 + i] = entropy->mv_prob[1][9 + i];
286
287 /* MV Y size */
288 for (i = 0; i < 8; i++)
289 table[32 + i] = entropy->mv_prob[0][9 + i];
290
291 /* MV X short tree */
292 for (i = 0; i < 7; i++)
293 table[40 + i] = entropy->mv_prob[1][2 + i];
294
295 /* MV Y short tree */
296 for (i = 0; i < 7; i++)
297 table[48 + i] = entropy->mv_prob[0][2 + i];
298
299 /* Update the ASIC table when needed. */
300 if (entropy->update_coeff_prob_flag) {
301 table += 56;
302 /* DCT coeff probabilities 0-2, two fields per line. */
303 for (i = 0; i < 4; i++)
304 for (j = 0; j < 8; j++)
305 for (k = 0; k < 3; k++) {
306 for (l = 0; l < 3; l++) {
307 *table++ = entropy->coeff_prob[i][j][k][l];
308 }
309 *table++ = 0;
310 }
311
312 /* ASIC second probability table in ext mem.
313 * DCT coeff probabilities 4 5 6 7 8 9 10 3 on each line.
314 * coeff 3 was moved from first table to second so it is last. */
315 for (i = 0; i < 4; i++)
316 for (j = 0; j < 8; j++)
317 for (k = 0; k < 3; k++) {
318 for (l = 4; l < 11; l++) {
319 *table++ = entropy->coeff_prob[i][j][k][l];
320 }
321 *table++ = entropy->coeff_prob[i][j][k][3];
322 }
323 }
324 }
325 table = mpp_buffer_get_ptr(buffers->hw_cabac_table_buf);
326 if (entropy->update_coeff_prob_flag)
327 vp8e_swap_endian((RK_U32 *) table, 56 + 8 * 48 + 8 * 96);
328 else
329 vp8e_swap_endian((RK_U32 *) table, 56);
330
331 return MPP_OK;
332 }
333
vp8e_calc_cost_mv(RK_S32 mvd,RK_S32 * mv_prob)334 RK_S32 vp8e_calc_cost_mv(RK_S32 mvd, RK_S32 *mv_prob)
335 {
336 RK_S32 i = 0;
337 RK_S32 bit_cost = 0;
338
339 RK_S32 tmp = MPP_ABS(mvd >> 1);
340
341 if (tmp < 8) {
342 bit_cost += COST_BOOL(mv_prob[0], 0);
343 bit_cost += get_cost_tree(&mv_tree_tbl[tmp], mv_prob + 2);
344 if (!tmp)
345 return bit_cost;
346
347 /* Sign */
348 bit_cost += COST_BOOL(mv_prob[1], mvd < 0);
349 return bit_cost;
350 }
351
352 bit_cost += COST_BOOL(mv_prob[0], 1);
353
354 for (i = 0; i < 3; i++) {
355 bit_cost += COST_BOOL(mv_prob[9 + i], (tmp >> i) & 1);
356 }
357
358 for (i = 9; i > 3; i--) {
359 bit_cost += COST_BOOL(mv_prob[9 + i], (tmp >> i) & 1);
360 }
361
362 if (tmp > 15) {
363 bit_cost += COST_BOOL(mv_prob[9 + 3], (tmp >> 3) & 1);
364 }
365
366 bit_cost += COST_BOOL(mv_prob[1], mvd < 0);
367
368 return bit_cost;
369 }
370
vp8e_calc_coeff_prob(Vp8ePutBitBuf * bitbuf,RK_S32 (* curr)[4][8][3][11],RK_S32 (* prev)[4][8][3][11])371 MPP_RET vp8e_calc_coeff_prob(Vp8ePutBitBuf *bitbuf, RK_S32 (*curr)[4][8][3][11],
372 RK_S32 (*prev)[4][8][3][11])
373 {
374 RK_S32 i, j, k, l;
375 RK_S32 prob, new, old;
376
377 for (i = 0; i < 4; i++) {
378 for (j = 0; j < 8; j++) {
379 for (k = 0; k < 3; k++) {
380 for (l = 0; l < 11; l++) {
381 prob = coeff_update_prob_tbl[i][j][k][l];
382 old = (RK_S32) (*prev)[i][j][k][l];
383 new = (RK_S32) (*curr)[i][j][k][l];
384
385 if (new == old) {
386 vp8e_put_bool(bitbuf, prob, 0);
387 } else {
388 vp8e_put_bool(bitbuf, prob, 1);
389 vp8e_put_lit(bitbuf, new, 8);
390 }
391 }
392 }
393 }
394 }
395 return MPP_OK;
396 }
397
vp8e_calc_mv_prob(Vp8ePutBitBuf * bitbuf,RK_S32 (* curr)[2][19],RK_S32 (* prev)[2][19])398 MPP_RET vp8e_calc_mv_prob(Vp8ePutBitBuf *bitbuf, RK_S32 (*curr)[2][19],
399 RK_S32 (*prev)[2][19])
400 {
401 RK_S32 i, j;
402 RK_S32 prob, new, old;
403
404 for (i = 0; i < 2; i++) {
405 for (j = 0; j < 19; j++) {
406 prob = mv_update_prob_tbl[i][j];
407 old = (RK_S32) (*prev)[i][j];
408 new = (RK_S32) (*curr)[i][j];
409
410 if (new == old) {
411 vp8e_put_bool(bitbuf, prob, 0);
412 } else {
413 vp8e_put_bool(bitbuf, prob, 1);
414 vp8e_put_lit(bitbuf, new >> 1, 7);
415 }
416 }
417 }
418 return MPP_OK;
419 }
420