1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2021 MediaTek Inc.
4 * Author: George Sun <george.sun@mediatek.com>
5 */
6
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <media/videobuf2-dma-contig.h>
10 #include <media/v4l2-vp9.h>
11
12 #include "../mtk_vcodec_dec.h"
13 #include "../../common/mtk_vcodec_intr.h"
14 #include "../vdec_drv_base.h"
15 #include "../vdec_drv_if.h"
16 #include "../vdec_vpu_if.h"
17
18 /* reset_frame_context defined in VP9 spec */
19 #define VP9_RESET_FRAME_CONTEXT_NONE0 0
20 #define VP9_RESET_FRAME_CONTEXT_NONE1 1
21 #define VP9_RESET_FRAME_CONTEXT_SPEC 2
22 #define VP9_RESET_FRAME_CONTEXT_ALL 3
23
24 #define VP9_TILE_BUF_SIZE 4096
25 #define VP9_PROB_BUF_SIZE 2560
26 #define VP9_COUNTS_BUF_SIZE 16384
27
28 #define HDR_FLAG(x) (!!((hdr)->flags & V4L2_VP9_FRAME_FLAG_##x))
29 #define LF_FLAG(x) (!!((lf)->flags & V4L2_VP9_LOOP_FILTER_FLAG_##x))
30 #define SEG_FLAG(x) (!!((seg)->flags & V4L2_VP9_SEGMENTATION_FLAG_##x))
31 #define VP9_BAND_6(band) ((band) == 0 ? 3 : 6)
32
33 /*
34 * struct vdec_vp9_slice_frame_ctx - vp9 prob tables footprint
35 */
36 struct vdec_vp9_slice_frame_ctx {
37 struct {
38 u8 probs[6][3];
39 u8 padding[2];
40 } coef_probs[4][2][2][6];
41
42 u8 y_mode_prob[4][16];
43 u8 switch_interp_prob[4][16];
44 u8 seg[32]; /* ignore */
45 u8 comp_inter_prob[16];
46 u8 comp_ref_prob[16];
47 u8 single_ref_prob[5][2];
48 u8 single_ref_prob_padding[6];
49
50 u8 joint[3];
51 u8 joint_padding[13];
52 struct {
53 u8 sign;
54 u8 classes[10];
55 u8 padding[5];
56 } sign_classes[2];
57 struct {
58 u8 class0[1];
59 u8 bits[10];
60 u8 padding[5];
61 } class0_bits[2];
62 struct {
63 u8 class0_fp[2][3];
64 u8 fp[3];
65 u8 class0_hp;
66 u8 hp;
67 u8 padding[5];
68 } class0_fp_hp[2];
69
70 u8 uv_mode_prob[10][16];
71 u8 uv_mode_prob_padding[2][16];
72
73 u8 partition_prob[16][4];
74
75 u8 inter_mode_probs[7][4];
76 u8 skip_probs[4];
77
78 u8 tx_p8x8[2][4];
79 u8 tx_p16x16[2][4];
80 u8 tx_p32x32[2][4];
81 u8 intra_inter_prob[8];
82 };
83
84 /*
85 * struct vdec_vp9_slice_frame_counts - vp9 counts tables footprint
86 */
87 struct vdec_vp9_slice_frame_counts {
88 union {
89 struct {
90 u32 band_0[3];
91 u32 padding0[1];
92 u32 band_1_5[5][6];
93 u32 padding1[2];
94 } eob_branch[4][2][2];
95 u32 eob_branch_space[256 * 4];
96 };
97
98 struct {
99 u32 band_0[3][4];
100 u32 band_1_5[5][6][4];
101 } coef_probs[4][2][2];
102
103 u32 intra_inter[4][2];
104 u32 comp_inter[5][2];
105 u32 comp_inter_padding[2];
106 u32 comp_ref[5][2];
107 u32 comp_ref_padding[2];
108 u32 single_ref[5][2][2];
109 u32 inter_mode[7][4];
110 u32 y_mode[4][12];
111 u32 uv_mode[10][10];
112 u32 partition[16][4];
113 u32 switchable_interp[4][4];
114
115 u32 tx_p8x8[2][2];
116 u32 tx_p16x16[2][4];
117 u32 tx_p32x32[2][4];
118
119 u32 skip[3][4];
120
121 u32 joint[4];
122
123 struct {
124 u32 sign[2];
125 u32 class0[2];
126 u32 classes[12];
127 u32 bits[10][2];
128 u32 padding[4];
129 u32 class0_fp[2][4];
130 u32 fp[4];
131 u32 class0_hp[2];
132 u32 hp[2];
133 } mvcomp[2];
134
135 u32 reserved[126][4];
136 };
137
138 /**
139 * struct vdec_vp9_slice_counts_map - vp9 counts tables to map
140 * v4l2_vp9_frame_symbol_counts
141 * @skip: skip counts.
142 * @y_mode: Y prediction mode counts.
143 * @filter: interpolation filter counts.
144 * @sign: motion vector sign counts.
145 * @classes: motion vector class counts.
146 * @class0: motion vector class0 bit counts.
147 * @bits: motion vector bits counts.
148 * @class0_fp: motion vector class0 fractional bit counts.
149 * @fp: motion vector fractional bit counts.
150 * @class0_hp: motion vector class0 high precision fractional bit counts.
151 * @hp: motion vector high precision fractional bit counts.
152 */
153 struct vdec_vp9_slice_counts_map {
154 u32 skip[3][2];
155 u32 y_mode[4][10];
156 u32 filter[4][3];
157 u32 sign[2][2];
158 u32 classes[2][11];
159 u32 class0[2][2];
160 u32 bits[2][10][2];
161 u32 class0_fp[2][2][4];
162 u32 fp[2][4];
163 u32 class0_hp[2][2];
164 u32 hp[2][2];
165 };
166
167 /*
168 * struct vdec_vp9_slice_uncompressed_header - vp9 uncompressed header syntax
169 * used for decoding
170 */
171 struct vdec_vp9_slice_uncompressed_header {
172 u8 profile;
173 u8 last_frame_type;
174 u8 frame_type;
175
176 u8 last_show_frame;
177 u8 show_frame;
178 u8 error_resilient_mode;
179
180 u8 bit_depth;
181 u8 padding0[1];
182 u16 last_frame_width;
183 u16 last_frame_height;
184 u16 frame_width;
185 u16 frame_height;
186
187 u8 intra_only;
188 u8 reset_frame_context;
189 u8 ref_frame_sign_bias[4];
190 u8 allow_high_precision_mv;
191 u8 interpolation_filter;
192
193 u8 refresh_frame_context;
194 u8 frame_parallel_decoding_mode;
195 u8 frame_context_idx;
196
197 /* loop_filter_params */
198 u8 loop_filter_level;
199 u8 loop_filter_sharpness;
200 u8 loop_filter_delta_enabled;
201 s8 loop_filter_ref_deltas[4];
202 s8 loop_filter_mode_deltas[2];
203
204 /* quantization_params */
205 u8 base_q_idx;
206 s8 delta_q_y_dc;
207 s8 delta_q_uv_dc;
208 s8 delta_q_uv_ac;
209
210 /* segmentation_params */
211 u8 segmentation_enabled;
212 u8 segmentation_update_map;
213 u8 segmentation_tree_probs[7];
214 u8 padding1[1];
215 u8 segmentation_temporal_udpate;
216 u8 segmentation_pred_prob[3];
217 u8 segmentation_update_data;
218 u8 segmentation_abs_or_delta_update;
219 u8 feature_enabled[8];
220 s16 feature_value[8][4];
221
222 /* tile_info */
223 u8 tile_cols_log2;
224 u8 tile_rows_log2;
225 u8 padding2[2];
226
227 u16 uncompressed_header_size;
228 u16 header_size_in_bytes;
229
230 /* LAT OUT, CORE IN */
231 u32 dequant[8][4];
232 };
233
234 /*
235 * struct vdec_vp9_slice_compressed_header - vp9 compressed header syntax
236 * used for decoding.
237 */
238 struct vdec_vp9_slice_compressed_header {
239 u8 tx_mode;
240 u8 ref_mode;
241 u8 comp_fixed_ref;
242 u8 comp_var_ref[2];
243 u8 padding[3];
244 };
245
246 /*
247 * struct vdec_vp9_slice_tiles - vp9 tile syntax
248 */
249 struct vdec_vp9_slice_tiles {
250 u32 size[4][64];
251 u32 mi_rows[4];
252 u32 mi_cols[64];
253 u8 actual_rows;
254 u8 padding[7];
255 };
256
257 /*
258 * struct vdec_vp9_slice_reference - vp9 reference frame information
259 */
260 struct vdec_vp9_slice_reference {
261 u16 frame_width;
262 u16 frame_height;
263 u8 bit_depth;
264 u8 subsampling_x;
265 u8 subsampling_y;
266 u8 padding;
267 };
268
269 /*
270 * struct vdec_vp9_slice_frame - vp9 syntax used for decoding
271 */
272 struct vdec_vp9_slice_frame {
273 struct vdec_vp9_slice_uncompressed_header uh;
274 struct vdec_vp9_slice_compressed_header ch;
275 struct vdec_vp9_slice_tiles tiles;
276 struct vdec_vp9_slice_reference ref[3];
277 };
278
279 /*
280 * struct vdec_vp9_slice_init_vsi - VSI used to initialize instance
281 */
282 struct vdec_vp9_slice_init_vsi {
283 unsigned int architecture;
284 unsigned int reserved;
285 u64 core_vsi;
286 /* default frame context's position in MicroP */
287 u64 default_frame_ctx;
288 };
289
290 /*
291 * struct vdec_vp9_slice_mem - memory address and size
292 */
293 struct vdec_vp9_slice_mem {
294 union {
295 u64 buf;
296 dma_addr_t dma_addr;
297 };
298 union {
299 size_t size;
300 dma_addr_t dma_addr_end;
301 u64 padding;
302 };
303 };
304
305 /*
306 * struct vdec_vp9_slice_bs - input buffer for decoding
307 */
308 struct vdec_vp9_slice_bs {
309 struct vdec_vp9_slice_mem buf;
310 struct vdec_vp9_slice_mem frame;
311 };
312
313 /*
314 * struct vdec_vp9_slice_fb - frame buffer for decoding
315 */
316 struct vdec_vp9_slice_fb {
317 struct vdec_vp9_slice_mem y;
318 struct vdec_vp9_slice_mem c;
319 };
320
321 /*
322 * struct vdec_vp9_slice_state - decoding state
323 */
324 struct vdec_vp9_slice_state {
325 int err;
326 unsigned int full;
327 unsigned int timeout;
328 unsigned int perf;
329
330 unsigned int crc[12];
331 };
332
333 /**
334 * struct vdec_vp9_slice_vsi - exchange decoding information
335 * between Main CPU and MicroP
336 *
337 * @bs: input buffer
338 * @fb: output buffer
339 * @ref: 3 reference buffers
340 * @mv: mv working buffer
341 * @seg: segmentation working buffer
342 * @tile: tile buffer
343 * @prob: prob table buffer, used to set/update prob table
344 * @counts: counts table buffer, used to update prob table
345 * @ube: general buffer
346 * @trans: trans buffer position in general buffer
347 * @err_map: error buffer
348 * @row_info: row info buffer
349 * @frame: decoding syntax
350 * @state: decoding state
351 */
352 struct vdec_vp9_slice_vsi {
353 /* used in LAT stage */
354 struct vdec_vp9_slice_bs bs;
355 /* used in Core stage */
356 struct vdec_vp9_slice_fb fb;
357 struct vdec_vp9_slice_fb ref[3];
358
359 struct vdec_vp9_slice_mem mv[2];
360 struct vdec_vp9_slice_mem seg[2];
361 struct vdec_vp9_slice_mem tile;
362 struct vdec_vp9_slice_mem prob;
363 struct vdec_vp9_slice_mem counts;
364
365 /* LAT stage's output, Core stage's input */
366 struct vdec_vp9_slice_mem ube;
367 struct vdec_vp9_slice_mem trans;
368 struct vdec_vp9_slice_mem err_map;
369 struct vdec_vp9_slice_mem row_info;
370
371 /* decoding parameters */
372 struct vdec_vp9_slice_frame frame;
373
374 struct vdec_vp9_slice_state state;
375 };
376
377 /**
378 * struct vdec_vp9_slice_pfc - per-frame context that contains a local vsi.
379 * pass it from lat to core
380 *
381 * @vsi: local vsi. copy to/from remote vsi before/after decoding
382 * @ref_idx: reference buffer index
383 * @seq: picture sequence
384 * @state: decoding state
385 */
386 struct vdec_vp9_slice_pfc {
387 struct vdec_vp9_slice_vsi vsi;
388
389 u64 ref_idx[3];
390
391 int seq;
392
393 /* LAT/Core CRC */
394 struct vdec_vp9_slice_state state[2];
395 };
396
397 /*
398 * enum vdec_vp9_slice_resolution_level
399 */
400 enum vdec_vp9_slice_resolution_level {
401 VP9_RES_NONE,
402 VP9_RES_FHD,
403 VP9_RES_4K,
404 VP9_RES_8K,
405 };
406
407 /*
408 * struct vdec_vp9_slice_ref - picture's width & height should kept
409 * for later decoding as reference picture
410 */
411 struct vdec_vp9_slice_ref {
412 unsigned int width;
413 unsigned int height;
414 };
415
416 /**
417 * struct vdec_vp9_slice_instance - represent one vp9 instance
418 *
419 * @ctx: pointer to codec's context
420 * @vpu: VPU instance
421 * @seq: global picture sequence
422 * @level: level of current resolution
423 * @width: width of last picture
424 * @height: height of last picture
425 * @frame_type: frame_type of last picture
426 * @irq: irq to Main CPU or MicroP
427 * @show_frame: show_frame of last picture
428 * @dpb: picture information (width/height) for reference
429 * @mv: mv working buffer
430 * @seg: segmentation working buffer
431 * @tile: tile buffer
432 * @prob: prob table buffer, used to set/update prob table
433 * @counts: counts table buffer, used to update prob table
434 * @frame_ctx: 4 frame context according to VP9 Spec
435 * @frame_ctx_helper: 4 frame context according to newest kernel spec
436 * @dirty: state of each frame context
437 * @init_vsi: vsi used for initialized VP9 instance
438 * @vsi: vsi used for decoding/flush ...
439 * @core_vsi: vsi used for Core stage
440 *
441 * @sc_pfc: per frame context single core
442 * @counts_map: used map to counts_helper
443 * @counts_helper: counts table according to newest kernel spec
444 */
445 struct vdec_vp9_slice_instance {
446 struct mtk_vcodec_dec_ctx *ctx;
447 struct vdec_vpu_inst vpu;
448
449 int seq;
450
451 enum vdec_vp9_slice_resolution_level level;
452
453 /* for resolution change and get_pic_info */
454 unsigned int width;
455 unsigned int height;
456
457 /* for last_frame_type */
458 unsigned int frame_type;
459 unsigned int irq;
460
461 unsigned int show_frame;
462
463 /* maintain vp9 reference frame state */
464 struct vdec_vp9_slice_ref dpb[VB2_MAX_FRAME];
465
466 /*
467 * normal working buffers
468 * mv[0]/seg[0]/tile/prob/counts is used for LAT
469 * mv[1]/seg[1] is used for CORE
470 */
471 struct mtk_vcodec_mem mv[2];
472 struct mtk_vcodec_mem seg[2];
473 struct mtk_vcodec_mem tile;
474 struct mtk_vcodec_mem prob;
475 struct mtk_vcodec_mem counts;
476
477 /* 4 prob tables */
478 struct vdec_vp9_slice_frame_ctx frame_ctx[4];
479 /*4 helper tables */
480 struct v4l2_vp9_frame_context frame_ctx_helper;
481 unsigned char dirty[4];
482
483 /* MicroP vsi */
484 union {
485 struct vdec_vp9_slice_init_vsi *init_vsi;
486 struct vdec_vp9_slice_vsi *vsi;
487 };
488 struct vdec_vp9_slice_vsi *core_vsi;
489
490 struct vdec_vp9_slice_pfc sc_pfc;
491 struct vdec_vp9_slice_counts_map counts_map;
492 struct v4l2_vp9_frame_symbol_counts counts_helper;
493 };
494
495 /*
496 * all VP9 instances could share this default frame context.
497 */
498 static struct vdec_vp9_slice_frame_ctx *vdec_vp9_slice_default_frame_ctx;
499 static DEFINE_MUTEX(vdec_vp9_slice_frame_ctx_lock);
500
501 static int vdec_vp9_slice_core_decode(struct vdec_lat_buf *lat_buf);
502
vdec_vp9_slice_init_default_frame_ctx(struct vdec_vp9_slice_instance * instance)503 static int vdec_vp9_slice_init_default_frame_ctx(struct vdec_vp9_slice_instance *instance)
504 {
505 struct vdec_vp9_slice_frame_ctx *remote_frame_ctx;
506 struct vdec_vp9_slice_frame_ctx *frame_ctx;
507 struct mtk_vcodec_dec_ctx *ctx;
508 struct vdec_vp9_slice_init_vsi *vsi;
509 int ret = 0;
510
511 ctx = instance->ctx;
512 vsi = instance->vpu.vsi;
513 if (!ctx || !vsi)
514 return -EINVAL;
515
516 remote_frame_ctx = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
517 (u32)vsi->default_frame_ctx);
518 if (!remote_frame_ctx) {
519 mtk_vdec_err(ctx, "failed to map default frame ctx\n");
520 return -EINVAL;
521 }
522
523 mutex_lock(&vdec_vp9_slice_frame_ctx_lock);
524 if (vdec_vp9_slice_default_frame_ctx)
525 goto out;
526
527 frame_ctx = kmemdup(remote_frame_ctx, sizeof(*frame_ctx), GFP_KERNEL);
528 if (!frame_ctx) {
529 ret = -ENOMEM;
530 goto out;
531 }
532
533 vdec_vp9_slice_default_frame_ctx = frame_ctx;
534
535 out:
536 mutex_unlock(&vdec_vp9_slice_frame_ctx_lock);
537
538 return ret;
539 }
540
vdec_vp9_slice_alloc_working_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_vsi * vsi)541 static int vdec_vp9_slice_alloc_working_buffer(struct vdec_vp9_slice_instance *instance,
542 struct vdec_vp9_slice_vsi *vsi)
543 {
544 struct mtk_vcodec_dec_ctx *ctx = instance->ctx;
545 enum vdec_vp9_slice_resolution_level level;
546 /* super blocks */
547 unsigned int max_sb_w;
548 unsigned int max_sb_h;
549 unsigned int max_w;
550 unsigned int max_h;
551 unsigned int w;
552 unsigned int h;
553 size_t size;
554 int ret;
555 int i;
556
557 w = vsi->frame.uh.frame_width;
558 h = vsi->frame.uh.frame_height;
559
560 if (w > VCODEC_DEC_4K_CODED_WIDTH ||
561 h > VCODEC_DEC_4K_CODED_HEIGHT) {
562 return -EINVAL;
563 } else if (w > MTK_VDEC_MAX_W || h > MTK_VDEC_MAX_H) {
564 /* 4K */
565 level = VP9_RES_4K;
566 max_w = VCODEC_DEC_4K_CODED_WIDTH;
567 max_h = VCODEC_DEC_4K_CODED_HEIGHT;
568 } else {
569 /* FHD */
570 level = VP9_RES_FHD;
571 max_w = MTK_VDEC_MAX_W;
572 max_h = MTK_VDEC_MAX_H;
573 }
574
575 if (level == instance->level)
576 return 0;
577
578 mtk_vdec_debug(ctx, "resolution level changed, from %u to %u, %ux%u",
579 instance->level, level, w, h);
580
581 max_sb_w = DIV_ROUND_UP(max_w, 64);
582 max_sb_h = DIV_ROUND_UP(max_h, 64);
583 ret = -ENOMEM;
584
585 /*
586 * Lat-flush must wait core idle, otherwise core will
587 * use released buffers
588 */
589
590 size = (max_sb_w * max_sb_h + 2) * 576;
591 for (i = 0; i < 2; i++) {
592 if (instance->mv[i].va)
593 mtk_vcodec_mem_free(ctx, &instance->mv[i]);
594 instance->mv[i].size = size;
595 if (mtk_vcodec_mem_alloc(ctx, &instance->mv[i]))
596 goto err;
597 }
598
599 size = (max_sb_w * max_sb_h * 32) + 256;
600 for (i = 0; i < 2; i++) {
601 if (instance->seg[i].va)
602 mtk_vcodec_mem_free(ctx, &instance->seg[i]);
603 instance->seg[i].size = size;
604 if (mtk_vcodec_mem_alloc(ctx, &instance->seg[i]))
605 goto err;
606 }
607
608 if (!instance->tile.va) {
609 instance->tile.size = VP9_TILE_BUF_SIZE;
610 if (mtk_vcodec_mem_alloc(ctx, &instance->tile))
611 goto err;
612 }
613
614 if (!instance->prob.va) {
615 instance->prob.size = VP9_PROB_BUF_SIZE;
616 if (mtk_vcodec_mem_alloc(ctx, &instance->prob))
617 goto err;
618 }
619
620 if (!instance->counts.va) {
621 instance->counts.size = VP9_COUNTS_BUF_SIZE;
622 if (mtk_vcodec_mem_alloc(ctx, &instance->counts))
623 goto err;
624 }
625
626 instance->level = level;
627 return 0;
628
629 err:
630 instance->level = VP9_RES_NONE;
631 return ret;
632 }
633
vdec_vp9_slice_free_working_buffer(struct vdec_vp9_slice_instance * instance)634 static void vdec_vp9_slice_free_working_buffer(struct vdec_vp9_slice_instance *instance)
635 {
636 struct mtk_vcodec_dec_ctx *ctx = instance->ctx;
637 int i;
638
639 for (i = 0; i < ARRAY_SIZE(instance->mv); i++) {
640 if (instance->mv[i].va)
641 mtk_vcodec_mem_free(ctx, &instance->mv[i]);
642 }
643 for (i = 0; i < ARRAY_SIZE(instance->seg); i++) {
644 if (instance->seg[i].va)
645 mtk_vcodec_mem_free(ctx, &instance->seg[i]);
646 }
647 if (instance->tile.va)
648 mtk_vcodec_mem_free(ctx, &instance->tile);
649 if (instance->prob.va)
650 mtk_vcodec_mem_free(ctx, &instance->prob);
651 if (instance->counts.va)
652 mtk_vcodec_mem_free(ctx, &instance->counts);
653
654 instance->level = VP9_RES_NONE;
655 }
656
vdec_vp9_slice_vsi_from_remote(struct vdec_vp9_slice_vsi * vsi,struct vdec_vp9_slice_vsi * remote_vsi,int skip)657 static void vdec_vp9_slice_vsi_from_remote(struct vdec_vp9_slice_vsi *vsi,
658 struct vdec_vp9_slice_vsi *remote_vsi,
659 int skip)
660 {
661 struct vdec_vp9_slice_frame *rf;
662 struct vdec_vp9_slice_frame *f;
663
664 /*
665 * compressed header
666 * dequant
667 * buffer position
668 * decode state
669 */
670 if (!skip) {
671 rf = &remote_vsi->frame;
672 f = &vsi->frame;
673 memcpy(&f->ch, &rf->ch, sizeof(f->ch));
674 memcpy(&f->uh.dequant, &rf->uh.dequant, sizeof(f->uh.dequant));
675 memcpy(&vsi->trans, &remote_vsi->trans, sizeof(vsi->trans));
676 }
677
678 memcpy(&vsi->state, &remote_vsi->state, sizeof(vsi->state));
679 }
680
vdec_vp9_slice_vsi_to_remote(struct vdec_vp9_slice_vsi * vsi,struct vdec_vp9_slice_vsi * remote_vsi)681 static void vdec_vp9_slice_vsi_to_remote(struct vdec_vp9_slice_vsi *vsi,
682 struct vdec_vp9_slice_vsi *remote_vsi)
683 {
684 memcpy(remote_vsi, vsi, sizeof(*vsi));
685 }
686
vdec_vp9_slice_tile_offset(int idx,int mi_num,int tile_log2)687 static int vdec_vp9_slice_tile_offset(int idx, int mi_num, int tile_log2)
688 {
689 int sbs = (mi_num + 7) >> 3;
690 int offset = ((idx * sbs) >> tile_log2) << 3;
691
692 return min(offset, mi_num);
693 }
694
695 static
vdec_vp9_slice_setup_single_from_src_to_dst(struct vdec_vp9_slice_instance * instance)696 int vdec_vp9_slice_setup_single_from_src_to_dst(struct vdec_vp9_slice_instance *instance)
697 {
698 struct vb2_v4l2_buffer *src;
699 struct vb2_v4l2_buffer *dst;
700
701 src = v4l2_m2m_next_src_buf(instance->ctx->m2m_ctx);
702 if (!src)
703 return -EINVAL;
704
705 dst = v4l2_m2m_next_dst_buf(instance->ctx->m2m_ctx);
706 if (!dst)
707 return -EINVAL;
708
709 v4l2_m2m_buf_copy_metadata(src, dst, true);
710
711 return 0;
712 }
713
vdec_vp9_slice_setup_lat_from_src_buf(struct vdec_vp9_slice_instance * instance,struct vdec_lat_buf * lat_buf)714 static int vdec_vp9_slice_setup_lat_from_src_buf(struct vdec_vp9_slice_instance *instance,
715 struct vdec_lat_buf *lat_buf)
716 {
717 struct vb2_v4l2_buffer *src;
718 struct vb2_v4l2_buffer *dst;
719
720 src = v4l2_m2m_next_src_buf(instance->ctx->m2m_ctx);
721 if (!src)
722 return -EINVAL;
723
724 lat_buf->src_buf_req = src->vb2_buf.req_obj.req;
725
726 dst = &lat_buf->ts_info;
727 v4l2_m2m_buf_copy_metadata(src, dst, true);
728 return 0;
729 }
730
vdec_vp9_slice_setup_hdr(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_uncompressed_header * uh,struct v4l2_ctrl_vp9_frame * hdr)731 static void vdec_vp9_slice_setup_hdr(struct vdec_vp9_slice_instance *instance,
732 struct vdec_vp9_slice_uncompressed_header *uh,
733 struct v4l2_ctrl_vp9_frame *hdr)
734 {
735 int i;
736
737 uh->profile = hdr->profile;
738 uh->last_frame_type = instance->frame_type;
739 uh->frame_type = !HDR_FLAG(KEY_FRAME);
740 uh->last_show_frame = instance->show_frame;
741 uh->show_frame = HDR_FLAG(SHOW_FRAME);
742 uh->error_resilient_mode = HDR_FLAG(ERROR_RESILIENT);
743 uh->bit_depth = hdr->bit_depth;
744 uh->last_frame_width = instance->width;
745 uh->last_frame_height = instance->height;
746 uh->frame_width = hdr->frame_width_minus_1 + 1;
747 uh->frame_height = hdr->frame_height_minus_1 + 1;
748 uh->intra_only = HDR_FLAG(INTRA_ONLY);
749 /* map v4l2 enum to values defined in VP9 spec for firmware */
750 switch (hdr->reset_frame_context) {
751 case V4L2_VP9_RESET_FRAME_CTX_NONE:
752 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_NONE0;
753 break;
754 case V4L2_VP9_RESET_FRAME_CTX_SPEC:
755 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_SPEC;
756 break;
757 case V4L2_VP9_RESET_FRAME_CTX_ALL:
758 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_ALL;
759 break;
760 default:
761 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_NONE0;
762 break;
763 }
764 /*
765 * ref_frame_sign_bias specifies the intended direction
766 * of the motion vector in time for each reference frame.
767 * - INTRA_FRAME = 0,
768 * - LAST_FRAME = 1,
769 * - GOLDEN_FRAME = 2,
770 * - ALTREF_FRAME = 3,
771 * ref_frame_sign_bias[INTRA_FRAME] is always 0
772 * and VDA only passes another 3 directions
773 */
774 uh->ref_frame_sign_bias[0] = 0;
775 for (i = 0; i < 3; i++)
776 uh->ref_frame_sign_bias[i + 1] =
777 !!(hdr->ref_frame_sign_bias & (1 << i));
778 uh->allow_high_precision_mv = HDR_FLAG(ALLOW_HIGH_PREC_MV);
779 uh->interpolation_filter = hdr->interpolation_filter;
780 uh->refresh_frame_context = HDR_FLAG(REFRESH_FRAME_CTX);
781 uh->frame_parallel_decoding_mode = HDR_FLAG(PARALLEL_DEC_MODE);
782 uh->frame_context_idx = hdr->frame_context_idx;
783
784 /* tile info */
785 uh->tile_cols_log2 = hdr->tile_cols_log2;
786 uh->tile_rows_log2 = hdr->tile_rows_log2;
787
788 uh->uncompressed_header_size = hdr->uncompressed_header_size;
789 uh->header_size_in_bytes = hdr->compressed_header_size;
790 }
791
vdec_vp9_slice_setup_frame_ctx(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_uncompressed_header * uh,struct v4l2_ctrl_vp9_frame * hdr)792 static void vdec_vp9_slice_setup_frame_ctx(struct vdec_vp9_slice_instance *instance,
793 struct vdec_vp9_slice_uncompressed_header *uh,
794 struct v4l2_ctrl_vp9_frame *hdr)
795 {
796 int error_resilient_mode;
797 int reset_frame_context;
798 int key_frame;
799 int intra_only;
800 int i;
801
802 key_frame = HDR_FLAG(KEY_FRAME);
803 intra_only = HDR_FLAG(INTRA_ONLY);
804 error_resilient_mode = HDR_FLAG(ERROR_RESILIENT);
805 reset_frame_context = uh->reset_frame_context;
806
807 /*
808 * according to "6.2 Uncompressed header syntax" in
809 * "VP9 Bitstream & Decoding Process Specification",
810 * reset @frame_context_idx when (FrameIsIntra || error_resilient_mode)
811 */
812 if (key_frame || intra_only || error_resilient_mode) {
813 /*
814 * @reset_frame_context specifies
815 * whether the frame context should be
816 * reset to default values:
817 * 0 or 1 means do not reset any frame context
818 * 2 resets just the context specified in the frame header
819 * 3 resets all contexts
820 */
821 if (key_frame || error_resilient_mode ||
822 reset_frame_context == 3) {
823 /* use default table */
824 for (i = 0; i < 4; i++)
825 instance->dirty[i] = 0;
826 } else if (reset_frame_context == 2) {
827 instance->dirty[uh->frame_context_idx] = 0;
828 }
829 uh->frame_context_idx = 0;
830 }
831 }
832
vdec_vp9_slice_setup_loop_filter(struct vdec_vp9_slice_uncompressed_header * uh,struct v4l2_vp9_loop_filter * lf)833 static void vdec_vp9_slice_setup_loop_filter(struct vdec_vp9_slice_uncompressed_header *uh,
834 struct v4l2_vp9_loop_filter *lf)
835 {
836 int i;
837
838 uh->loop_filter_level = lf->level;
839 uh->loop_filter_sharpness = lf->sharpness;
840 uh->loop_filter_delta_enabled = LF_FLAG(DELTA_ENABLED);
841 for (i = 0; i < 4; i++)
842 uh->loop_filter_ref_deltas[i] = lf->ref_deltas[i];
843 for (i = 0; i < 2; i++)
844 uh->loop_filter_mode_deltas[i] = lf->mode_deltas[i];
845 }
846
vdec_vp9_slice_setup_quantization(struct vdec_vp9_slice_uncompressed_header * uh,struct v4l2_vp9_quantization * quant)847 static void vdec_vp9_slice_setup_quantization(struct vdec_vp9_slice_uncompressed_header *uh,
848 struct v4l2_vp9_quantization *quant)
849 {
850 uh->base_q_idx = quant->base_q_idx;
851 uh->delta_q_y_dc = quant->delta_q_y_dc;
852 uh->delta_q_uv_dc = quant->delta_q_uv_dc;
853 uh->delta_q_uv_ac = quant->delta_q_uv_ac;
854 }
855
vdec_vp9_slice_setup_segmentation(struct vdec_vp9_slice_uncompressed_header * uh,struct v4l2_vp9_segmentation * seg)856 static void vdec_vp9_slice_setup_segmentation(struct vdec_vp9_slice_uncompressed_header *uh,
857 struct v4l2_vp9_segmentation *seg)
858 {
859 int i;
860 int j;
861
862 uh->segmentation_enabled = SEG_FLAG(ENABLED);
863 uh->segmentation_update_map = SEG_FLAG(UPDATE_MAP);
864 for (i = 0; i < 7; i++)
865 uh->segmentation_tree_probs[i] = seg->tree_probs[i];
866 uh->segmentation_temporal_udpate = SEG_FLAG(TEMPORAL_UPDATE);
867 for (i = 0; i < 3; i++)
868 uh->segmentation_pred_prob[i] = seg->pred_probs[i];
869 uh->segmentation_update_data = SEG_FLAG(UPDATE_DATA);
870 uh->segmentation_abs_or_delta_update = SEG_FLAG(ABS_OR_DELTA_UPDATE);
871 for (i = 0; i < 8; i++) {
872 uh->feature_enabled[i] = seg->feature_enabled[i];
873 for (j = 0; j < 4; j++)
874 uh->feature_value[i][j] = seg->feature_data[i][j];
875 }
876 }
877
vdec_vp9_slice_setup_tile(struct vdec_vp9_slice_vsi * vsi,struct v4l2_ctrl_vp9_frame * hdr)878 static int vdec_vp9_slice_setup_tile(struct vdec_vp9_slice_vsi *vsi,
879 struct v4l2_ctrl_vp9_frame *hdr)
880 {
881 unsigned int rows_log2;
882 unsigned int cols_log2;
883 unsigned int rows;
884 unsigned int cols;
885 unsigned int mi_rows;
886 unsigned int mi_cols;
887 struct vdec_vp9_slice_tiles *tiles;
888 int offset;
889 int start;
890 int end;
891 int i;
892
893 rows_log2 = hdr->tile_rows_log2;
894 cols_log2 = hdr->tile_cols_log2;
895 rows = 1 << rows_log2;
896 cols = 1 << cols_log2;
897 tiles = &vsi->frame.tiles;
898 tiles->actual_rows = 0;
899
900 if (rows > 4 || cols > 64)
901 return -EINVAL;
902
903 /* setup mi rows/cols information */
904 mi_rows = (hdr->frame_height_minus_1 + 1 + 7) >> 3;
905 mi_cols = (hdr->frame_width_minus_1 + 1 + 7) >> 3;
906
907 for (i = 0; i < rows; i++) {
908 start = vdec_vp9_slice_tile_offset(i, mi_rows, rows_log2);
909 end = vdec_vp9_slice_tile_offset(i + 1, mi_rows, rows_log2);
910 offset = end - start;
911 tiles->mi_rows[i] = (offset + 7) >> 3;
912 if (tiles->mi_rows[i])
913 tiles->actual_rows++;
914 }
915
916 for (i = 0; i < cols; i++) {
917 start = vdec_vp9_slice_tile_offset(i, mi_cols, cols_log2);
918 end = vdec_vp9_slice_tile_offset(i + 1, mi_cols, cols_log2);
919 offset = end - start;
920 tiles->mi_cols[i] = (offset + 7) >> 3;
921 }
922
923 return 0;
924 }
925
vdec_vp9_slice_setup_state(struct vdec_vp9_slice_vsi * vsi)926 static void vdec_vp9_slice_setup_state(struct vdec_vp9_slice_vsi *vsi)
927 {
928 memset(&vsi->state, 0, sizeof(vsi->state));
929 }
930
vdec_vp9_slice_setup_ref_idx(struct vdec_vp9_slice_pfc * pfc,struct v4l2_ctrl_vp9_frame * hdr)931 static void vdec_vp9_slice_setup_ref_idx(struct vdec_vp9_slice_pfc *pfc,
932 struct v4l2_ctrl_vp9_frame *hdr)
933 {
934 pfc->ref_idx[0] = hdr->last_frame_ts;
935 pfc->ref_idx[1] = hdr->golden_frame_ts;
936 pfc->ref_idx[2] = hdr->alt_frame_ts;
937 }
938
vdec_vp9_slice_setup_pfc(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_pfc * pfc)939 static int vdec_vp9_slice_setup_pfc(struct vdec_vp9_slice_instance *instance,
940 struct vdec_vp9_slice_pfc *pfc)
941 {
942 struct v4l2_ctrl_vp9_frame *hdr;
943 struct vdec_vp9_slice_uncompressed_header *uh;
944 struct v4l2_ctrl *hdr_ctrl;
945 struct vdec_vp9_slice_vsi *vsi;
946 int ret;
947
948 /* frame header */
949 hdr_ctrl = v4l2_ctrl_find(&instance->ctx->ctrl_hdl, V4L2_CID_STATELESS_VP9_FRAME);
950 if (!hdr_ctrl || !hdr_ctrl->p_cur.p)
951 return -EINVAL;
952
953 hdr = hdr_ctrl->p_cur.p;
954 vsi = &pfc->vsi;
955 uh = &vsi->frame.uh;
956
957 /* setup vsi information */
958 vdec_vp9_slice_setup_hdr(instance, uh, hdr);
959 vdec_vp9_slice_setup_frame_ctx(instance, uh, hdr);
960 vdec_vp9_slice_setup_loop_filter(uh, &hdr->lf);
961 vdec_vp9_slice_setup_quantization(uh, &hdr->quant);
962 vdec_vp9_slice_setup_segmentation(uh, &hdr->seg);
963 ret = vdec_vp9_slice_setup_tile(vsi, hdr);
964 if (ret)
965 return ret;
966 vdec_vp9_slice_setup_state(vsi);
967
968 /* core stage needs buffer index to get ref y/c ... */
969 vdec_vp9_slice_setup_ref_idx(pfc, hdr);
970
971 pfc->seq = instance->seq;
972 instance->seq++;
973
974 return 0;
975 }
976
vdec_vp9_slice_setup_lat_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_vsi * vsi,struct mtk_vcodec_mem * bs,struct vdec_lat_buf * lat_buf)977 static int vdec_vp9_slice_setup_lat_buffer(struct vdec_vp9_slice_instance *instance,
978 struct vdec_vp9_slice_vsi *vsi,
979 struct mtk_vcodec_mem *bs,
980 struct vdec_lat_buf *lat_buf)
981 {
982 int i;
983
984 vsi->bs.buf.dma_addr = bs->dma_addr;
985 vsi->bs.buf.size = bs->size;
986 vsi->bs.frame.dma_addr = bs->dma_addr;
987 vsi->bs.frame.size = bs->size;
988
989 for (i = 0; i < 2; i++) {
990 vsi->mv[i].dma_addr = instance->mv[i].dma_addr;
991 vsi->mv[i].size = instance->mv[i].size;
992 }
993 for (i = 0; i < 2; i++) {
994 vsi->seg[i].dma_addr = instance->seg[i].dma_addr;
995 vsi->seg[i].size = instance->seg[i].size;
996 }
997 vsi->tile.dma_addr = instance->tile.dma_addr;
998 vsi->tile.size = instance->tile.size;
999 vsi->prob.dma_addr = instance->prob.dma_addr;
1000 vsi->prob.size = instance->prob.size;
1001 vsi->counts.dma_addr = instance->counts.dma_addr;
1002 vsi->counts.size = instance->counts.size;
1003
1004 vsi->ube.dma_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
1005 vsi->ube.size = lat_buf->ctx->msg_queue.wdma_addr.size;
1006 vsi->trans.dma_addr = lat_buf->ctx->msg_queue.wdma_wptr_addr;
1007 /* used to store trans end */
1008 vsi->trans.dma_addr_end = lat_buf->ctx->msg_queue.wdma_rptr_addr;
1009 vsi->err_map.dma_addr = lat_buf->wdma_err_addr.dma_addr;
1010 vsi->err_map.size = lat_buf->wdma_err_addr.size;
1011
1012 vsi->row_info.buf = 0;
1013 vsi->row_info.size = 0;
1014
1015 return 0;
1016 }
1017
vdec_vp9_slice_setup_prob_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_vsi * vsi)1018 static int vdec_vp9_slice_setup_prob_buffer(struct vdec_vp9_slice_instance *instance,
1019 struct vdec_vp9_slice_vsi *vsi)
1020 {
1021 struct vdec_vp9_slice_frame_ctx *frame_ctx;
1022 struct vdec_vp9_slice_uncompressed_header *uh;
1023
1024 uh = &vsi->frame.uh;
1025
1026 mtk_vdec_debug(instance->ctx, "ctx dirty %u idx %d\n",
1027 instance->dirty[uh->frame_context_idx],
1028 uh->frame_context_idx);
1029
1030 if (instance->dirty[uh->frame_context_idx])
1031 frame_ctx = &instance->frame_ctx[uh->frame_context_idx];
1032 else
1033 frame_ctx = vdec_vp9_slice_default_frame_ctx;
1034 memcpy(instance->prob.va, frame_ctx, sizeof(*frame_ctx));
1035
1036 return 0;
1037 }
1038
vdec_vp9_slice_setup_seg_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_vsi * vsi,struct mtk_vcodec_mem * buf)1039 static void vdec_vp9_slice_setup_seg_buffer(struct vdec_vp9_slice_instance *instance,
1040 struct vdec_vp9_slice_vsi *vsi,
1041 struct mtk_vcodec_mem *buf)
1042 {
1043 struct vdec_vp9_slice_uncompressed_header *uh;
1044
1045 /* reset segment buffer */
1046 uh = &vsi->frame.uh;
1047 if (uh->frame_type == 0 ||
1048 uh->intra_only ||
1049 uh->error_resilient_mode ||
1050 uh->frame_width != instance->width ||
1051 uh->frame_height != instance->height) {
1052 mtk_vdec_debug(instance->ctx, "reset seg\n");
1053 memset(buf->va, 0, buf->size);
1054 }
1055 }
1056
1057 /*
1058 * parse tiles according to `6.4 Decode tiles syntax`
1059 * in "vp9-bitstream-specification"
1060 *
1061 * frame contains uncompress header, compressed header and several tiles.
1062 * this function parses tiles' position and size, stores them to tile buffer
1063 * for decoding.
1064 */
vdec_vp9_slice_setup_tile_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_vsi * vsi,struct mtk_vcodec_mem * bs)1065 static int vdec_vp9_slice_setup_tile_buffer(struct vdec_vp9_slice_instance *instance,
1066 struct vdec_vp9_slice_vsi *vsi,
1067 struct mtk_vcodec_mem *bs)
1068 {
1069 struct vdec_vp9_slice_uncompressed_header *uh;
1070 unsigned int rows_log2;
1071 unsigned int cols_log2;
1072 unsigned int rows;
1073 unsigned int cols;
1074 unsigned int mi_row;
1075 unsigned int mi_col;
1076 unsigned int offset;
1077 dma_addr_t pa;
1078 unsigned int size;
1079 struct vdec_vp9_slice_tiles *tiles;
1080 unsigned char *pos;
1081 unsigned char *end;
1082 unsigned char *va;
1083 unsigned int *tb;
1084 int i;
1085 int j;
1086
1087 uh = &vsi->frame.uh;
1088 rows_log2 = uh->tile_rows_log2;
1089 cols_log2 = uh->tile_cols_log2;
1090 rows = 1 << rows_log2;
1091 cols = 1 << cols_log2;
1092
1093 if (rows > 4 || cols > 64) {
1094 mtk_vdec_err(instance->ctx, "tile_rows %u tile_cols %u\n", rows, cols);
1095 return -EINVAL;
1096 }
1097
1098 offset = uh->uncompressed_header_size +
1099 uh->header_size_in_bytes;
1100 if (bs->size <= offset) {
1101 mtk_vdec_err(instance->ctx, "bs size %zu tile offset %u\n", bs->size, offset);
1102 return -EINVAL;
1103 }
1104
1105 tiles = &vsi->frame.tiles;
1106 /* setup tile buffer */
1107
1108 va = (unsigned char *)bs->va;
1109 pos = va + offset;
1110 end = va + bs->size;
1111 /* truncated */
1112 pa = bs->dma_addr + offset;
1113 tb = instance->tile.va;
1114 for (i = 0; i < rows; i++) {
1115 for (j = 0; j < cols; j++) {
1116 if (i == rows - 1 &&
1117 j == cols - 1) {
1118 size = (unsigned int)(end - pos);
1119 } else {
1120 if (end - pos < 4)
1121 return -EINVAL;
1122
1123 size = (pos[0] << 24) | (pos[1] << 16) |
1124 (pos[2] << 8) | pos[3];
1125 pos += 4;
1126 pa += 4;
1127 offset += 4;
1128 if (end - pos < size)
1129 return -EINVAL;
1130 }
1131 tiles->size[i][j] = size;
1132 if (tiles->mi_rows[i]) {
1133 *tb++ = (size << 3) + ((offset << 3) & 0x7f);
1134 *tb++ = pa & ~0xf;
1135 *tb++ = (pa << 3) & 0x7f;
1136 mi_row = (tiles->mi_rows[i] - 1) & 0x1ff;
1137 mi_col = (tiles->mi_cols[j] - 1) & 0x3f;
1138 *tb++ = (mi_row << 6) + mi_col;
1139 }
1140 pos += size;
1141 pa += size;
1142 offset += size;
1143 }
1144 }
1145
1146 return 0;
1147 }
1148
vdec_vp9_slice_setup_lat(struct vdec_vp9_slice_instance * instance,struct mtk_vcodec_mem * bs,struct vdec_lat_buf * lat_buf,struct vdec_vp9_slice_pfc * pfc)1149 static int vdec_vp9_slice_setup_lat(struct vdec_vp9_slice_instance *instance,
1150 struct mtk_vcodec_mem *bs,
1151 struct vdec_lat_buf *lat_buf,
1152 struct vdec_vp9_slice_pfc *pfc)
1153 {
1154 struct vdec_vp9_slice_vsi *vsi = &pfc->vsi;
1155 int ret;
1156
1157 ret = vdec_vp9_slice_setup_lat_from_src_buf(instance, lat_buf);
1158 if (ret)
1159 goto err;
1160
1161 ret = vdec_vp9_slice_setup_pfc(instance, pfc);
1162 if (ret)
1163 goto err;
1164
1165 ret = vdec_vp9_slice_alloc_working_buffer(instance, vsi);
1166 if (ret)
1167 goto err;
1168
1169 ret = vdec_vp9_slice_setup_lat_buffer(instance, vsi, bs, lat_buf);
1170 if (ret)
1171 goto err;
1172
1173 vdec_vp9_slice_setup_seg_buffer(instance, vsi, &instance->seg[0]);
1174
1175 /* setup prob/tile buffers for LAT */
1176
1177 ret = vdec_vp9_slice_setup_prob_buffer(instance, vsi);
1178 if (ret)
1179 goto err;
1180
1181 ret = vdec_vp9_slice_setup_tile_buffer(instance, vsi, bs);
1182 if (ret)
1183 goto err;
1184
1185 return 0;
1186
1187 err:
1188 return ret;
1189 }
1190
1191 /* clang stack usage explodes if this is inlined */
1192 static noinline_for_stack
vdec_vp9_slice_map_counts_eob_coef(unsigned int i,unsigned int j,unsigned int k,struct vdec_vp9_slice_frame_counts * counts,struct v4l2_vp9_frame_symbol_counts * counts_helper)1193 void vdec_vp9_slice_map_counts_eob_coef(unsigned int i, unsigned int j, unsigned int k,
1194 struct vdec_vp9_slice_frame_counts *counts,
1195 struct v4l2_vp9_frame_symbol_counts *counts_helper)
1196 {
1197 u32 l = 0, m;
1198
1199 /*
1200 * helper eo -> mtk eo
1201 * helpre e1 -> mtk c3
1202 * helper c0 -> c0
1203 * helper c1 -> c1
1204 * helper c2 -> c2
1205 */
1206 for (m = 0; m < 3; m++) {
1207 counts_helper->coeff[i][j][k][l][m] =
1208 (u32 (*)[3]) & counts->coef_probs[i][j][k].band_0[m];
1209 counts_helper->eob[i][j][k][l][m][0] =
1210 &counts->eob_branch[i][j][k].band_0[m];
1211 counts_helper->eob[i][j][k][l][m][1] =
1212 &counts->coef_probs[i][j][k].band_0[m][3];
1213 }
1214
1215 for (l = 1; l < 6; l++) {
1216 for (m = 0; m < 6; m++) {
1217 counts_helper->coeff[i][j][k][l][m] =
1218 (u32 (*)[3]) & counts->coef_probs[i][j][k].band_1_5[l - 1][m];
1219 counts_helper->eob[i][j][k][l][m][0] =
1220 &counts->eob_branch[i][j][k].band_1_5[l - 1][m];
1221 counts_helper->eob[i][j][k][l][m][1] =
1222 &counts->coef_probs[i][j][k].band_1_5[l - 1][m][3];
1223 }
1224 }
1225 }
1226
vdec_vp9_slice_counts_map_helper(struct vdec_vp9_slice_counts_map * counts_map,struct vdec_vp9_slice_frame_counts * counts,struct v4l2_vp9_frame_symbol_counts * counts_helper)1227 static void vdec_vp9_slice_counts_map_helper(struct vdec_vp9_slice_counts_map *counts_map,
1228 struct vdec_vp9_slice_frame_counts *counts,
1229 struct v4l2_vp9_frame_symbol_counts *counts_helper)
1230 {
1231 int i, j, k;
1232
1233 counts_helper->partition = &counts->partition;
1234 counts_helper->intra_inter = &counts->intra_inter;
1235 counts_helper->tx32p = &counts->tx_p32x32;
1236 counts_helper->tx16p = &counts->tx_p16x16;
1237 counts_helper->tx8p = &counts->tx_p8x8;
1238 counts_helper->uv_mode = &counts->uv_mode;
1239
1240 counts_helper->comp = &counts->comp_inter;
1241 counts_helper->comp_ref = &counts->comp_ref;
1242 counts_helper->single_ref = &counts->single_ref;
1243 counts_helper->mv_mode = &counts->inter_mode;
1244 counts_helper->mv_joint = &counts->joint;
1245
1246 for (i = 0; i < ARRAY_SIZE(counts_map->skip); i++)
1247 memcpy(counts_map->skip[i], counts->skip[i],
1248 sizeof(counts_map->skip[0]));
1249 counts_helper->skip = &counts_map->skip;
1250
1251 for (i = 0; i < ARRAY_SIZE(counts_map->y_mode); i++)
1252 memcpy(counts_map->y_mode[i], counts->y_mode[i],
1253 sizeof(counts_map->y_mode[0]));
1254 counts_helper->y_mode = &counts_map->y_mode;
1255
1256 for (i = 0; i < ARRAY_SIZE(counts_map->filter); i++)
1257 memcpy(counts_map->filter[i], counts->switchable_interp[i],
1258 sizeof(counts_map->filter[0]));
1259 counts_helper->filter = &counts_map->filter;
1260
1261 for (i = 0; i < ARRAY_SIZE(counts_map->sign); i++)
1262 memcpy(counts_map->sign[i], counts->mvcomp[i].sign,
1263 sizeof(counts_map->sign[0]));
1264 counts_helper->sign = &counts_map->sign;
1265
1266 for (i = 0; i < ARRAY_SIZE(counts_map->classes); i++)
1267 memcpy(counts_map->classes[i], counts->mvcomp[i].classes,
1268 sizeof(counts_map->classes[0]));
1269 counts_helper->classes = &counts_map->classes;
1270
1271 for (i = 0; i < ARRAY_SIZE(counts_map->class0); i++)
1272 memcpy(counts_map->class0[i], counts->mvcomp[i].class0,
1273 sizeof(counts_map->class0[0]));
1274 counts_helper->class0 = &counts_map->class0;
1275
1276 for (i = 0; i < ARRAY_SIZE(counts_map->bits); i++)
1277 for (j = 0; j < ARRAY_SIZE(counts_map->bits[0]); j++)
1278 memcpy(counts_map->bits[i][j], counts->mvcomp[i].bits[j],
1279 sizeof(counts_map->bits[0][0]));
1280 counts_helper->bits = &counts_map->bits;
1281
1282 for (i = 0; i < ARRAY_SIZE(counts_map->class0_fp); i++)
1283 for (j = 0; j < ARRAY_SIZE(counts_map->class0_fp[0]); j++)
1284 memcpy(counts_map->class0_fp[i][j], counts->mvcomp[i].class0_fp[j],
1285 sizeof(counts_map->class0_fp[0][0]));
1286 counts_helper->class0_fp = &counts_map->class0_fp;
1287
1288 for (i = 0; i < ARRAY_SIZE(counts_map->fp); i++)
1289 memcpy(counts_map->fp[i], counts->mvcomp[i].fp,
1290 sizeof(counts_map->fp[0]));
1291 counts_helper->fp = &counts_map->fp;
1292
1293 for (i = 0; i < ARRAY_SIZE(counts_map->class0_hp); i++)
1294 memcpy(counts_map->class0_hp[i], counts->mvcomp[i].class0_hp,
1295 sizeof(counts_map->class0_hp[0]));
1296 counts_helper->class0_hp = &counts_map->class0_hp;
1297
1298 for (i = 0; i < ARRAY_SIZE(counts_map->hp); i++)
1299 memcpy(counts_map->hp[i], counts->mvcomp[i].hp, sizeof(counts_map->hp[0]));
1300
1301 counts_helper->hp = &counts_map->hp;
1302
1303 for (i = 0; i < 4; i++)
1304 for (j = 0; j < 2; j++)
1305 for (k = 0; k < 2; k++)
1306 vdec_vp9_slice_map_counts_eob_coef(i, j, k, counts, counts_helper);
1307 }
1308
vdec_vp9_slice_map_to_coef(unsigned int i,unsigned int j,unsigned int k,struct vdec_vp9_slice_frame_ctx * frame_ctx,struct v4l2_vp9_frame_context * frame_ctx_helper)1309 static void vdec_vp9_slice_map_to_coef(unsigned int i, unsigned int j, unsigned int k,
1310 struct vdec_vp9_slice_frame_ctx *frame_ctx,
1311 struct v4l2_vp9_frame_context *frame_ctx_helper)
1312 {
1313 u32 l, m;
1314
1315 for (l = 0; l < ARRAY_SIZE(frame_ctx_helper->coef[0][0][0]); l++) {
1316 for (m = 0; m < VP9_BAND_6(l); m++) {
1317 memcpy(frame_ctx_helper->coef[i][j][k][l][m],
1318 frame_ctx->coef_probs[i][j][k][l].probs[m],
1319 sizeof(frame_ctx_helper->coef[i][j][k][l][0]));
1320 }
1321 }
1322 }
1323
vdec_vp9_slice_map_from_coef(unsigned int i,unsigned int j,unsigned int k,struct vdec_vp9_slice_frame_ctx * frame_ctx,struct v4l2_vp9_frame_context * frame_ctx_helper)1324 static void vdec_vp9_slice_map_from_coef(unsigned int i, unsigned int j, unsigned int k,
1325 struct vdec_vp9_slice_frame_ctx *frame_ctx,
1326 struct v4l2_vp9_frame_context *frame_ctx_helper)
1327 {
1328 u32 l, m;
1329
1330 for (l = 0; l < ARRAY_SIZE(frame_ctx_helper->coef[0][0][0]); l++) {
1331 for (m = 0; m < VP9_BAND_6(l); m++) {
1332 memcpy(frame_ctx->coef_probs[i][j][k][l].probs[m],
1333 frame_ctx_helper->coef[i][j][k][l][m],
1334 sizeof(frame_ctx_helper->coef[i][j][k][l][0]));
1335 }
1336 }
1337 }
1338
1339 static
vdec_vp9_slice_framectx_map_helper(bool frame_is_intra,struct vdec_vp9_slice_frame_ctx * pre_frame_ctx,struct vdec_vp9_slice_frame_ctx * frame_ctx,struct v4l2_vp9_frame_context * frame_ctx_helper)1340 void vdec_vp9_slice_framectx_map_helper(bool frame_is_intra,
1341 struct vdec_vp9_slice_frame_ctx *pre_frame_ctx,
1342 struct vdec_vp9_slice_frame_ctx *frame_ctx,
1343 struct v4l2_vp9_frame_context *frame_ctx_helper)
1344 {
1345 struct v4l2_vp9_frame_mv_context *mv = &frame_ctx_helper->mv;
1346 u32 i, j, k;
1347
1348 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->coef); i++)
1349 for (j = 0; j < ARRAY_SIZE(frame_ctx_helper->coef[0]); j++)
1350 for (k = 0; k < ARRAY_SIZE(frame_ctx_helper->coef[0][0]); k++)
1351 vdec_vp9_slice_map_to_coef(i, j, k, pre_frame_ctx,
1352 frame_ctx_helper);
1353
1354 /*
1355 * use previous prob when frame is not intra or
1356 * we should use the prob updated by the compressed header parse
1357 */
1358 if (!frame_is_intra)
1359 frame_ctx = pre_frame_ctx;
1360
1361 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx8); i++)
1362 memcpy(frame_ctx_helper->tx8[i], frame_ctx->tx_p8x8[i],
1363 sizeof(frame_ctx_helper->tx8[0]));
1364
1365 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx16); i++)
1366 memcpy(frame_ctx_helper->tx16[i], frame_ctx->tx_p16x16[i],
1367 sizeof(frame_ctx_helper->tx16[0]));
1368
1369 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx32); i++)
1370 memcpy(frame_ctx_helper->tx32[i], frame_ctx->tx_p32x32[i],
1371 sizeof(frame_ctx_helper->tx32[0]));
1372
1373 memcpy(frame_ctx_helper->skip, frame_ctx->skip_probs, sizeof(frame_ctx_helper->skip));
1374
1375 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->inter_mode); i++)
1376 memcpy(frame_ctx_helper->inter_mode[i], frame_ctx->inter_mode_probs[i],
1377 sizeof(frame_ctx_helper->inter_mode[0]));
1378
1379 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->interp_filter); i++)
1380 memcpy(frame_ctx_helper->interp_filter[i], frame_ctx->switch_interp_prob[i],
1381 sizeof(frame_ctx_helper->interp_filter[0]));
1382
1383 memcpy(frame_ctx_helper->is_inter, frame_ctx->intra_inter_prob,
1384 sizeof(frame_ctx_helper->is_inter));
1385
1386 memcpy(frame_ctx_helper->comp_mode, frame_ctx->comp_inter_prob,
1387 sizeof(frame_ctx_helper->comp_mode));
1388
1389 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->single_ref); i++)
1390 memcpy(frame_ctx_helper->single_ref[i], frame_ctx->single_ref_prob[i],
1391 sizeof(frame_ctx_helper->single_ref[0]));
1392
1393 memcpy(frame_ctx_helper->comp_ref, frame_ctx->comp_ref_prob,
1394 sizeof(frame_ctx_helper->comp_ref));
1395
1396 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->y_mode); i++)
1397 memcpy(frame_ctx_helper->y_mode[i], frame_ctx->y_mode_prob[i],
1398 sizeof(frame_ctx_helper->y_mode[0]));
1399
1400 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->uv_mode); i++)
1401 memcpy(frame_ctx_helper->uv_mode[i], frame_ctx->uv_mode_prob[i],
1402 sizeof(frame_ctx_helper->uv_mode[0]));
1403
1404 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->partition); i++)
1405 memcpy(frame_ctx_helper->partition[i], frame_ctx->partition_prob[i],
1406 sizeof(frame_ctx_helper->partition[0]));
1407
1408 memcpy(mv->joint, frame_ctx->joint, sizeof(mv->joint));
1409
1410 for (i = 0; i < ARRAY_SIZE(mv->sign); i++)
1411 mv->sign[i] = frame_ctx->sign_classes[i].sign;
1412
1413 for (i = 0; i < ARRAY_SIZE(mv->classes); i++)
1414 memcpy(mv->classes[i], frame_ctx->sign_classes[i].classes,
1415 sizeof(mv->classes[i]));
1416
1417 for (i = 0; i < ARRAY_SIZE(mv->class0_bit); i++)
1418 mv->class0_bit[i] = frame_ctx->class0_bits[i].class0[0];
1419
1420 for (i = 0; i < ARRAY_SIZE(mv->bits); i++)
1421 memcpy(mv->bits[i], frame_ctx->class0_bits[i].bits, sizeof(mv->bits[0]));
1422
1423 for (i = 0; i < ARRAY_SIZE(mv->class0_fr); i++)
1424 for (j = 0; j < ARRAY_SIZE(mv->class0_fr[0]); j++)
1425 memcpy(mv->class0_fr[i][j], frame_ctx->class0_fp_hp[i].class0_fp[j],
1426 sizeof(mv->class0_fr[0][0]));
1427
1428 for (i = 0; i < ARRAY_SIZE(mv->fr); i++)
1429 memcpy(mv->fr[i], frame_ctx->class0_fp_hp[i].fp, sizeof(mv->fr[0]));
1430
1431 for (i = 0; i < ARRAY_SIZE(mv->class0_hp); i++)
1432 mv->class0_hp[i] = frame_ctx->class0_fp_hp[i].class0_hp;
1433
1434 for (i = 0; i < ARRAY_SIZE(mv->hp); i++)
1435 mv->hp[i] = frame_ctx->class0_fp_hp[i].hp;
1436 }
1437
vdec_vp9_slice_helper_map_framectx(struct v4l2_vp9_frame_context * frame_ctx_helper,struct vdec_vp9_slice_frame_ctx * frame_ctx)1438 static void vdec_vp9_slice_helper_map_framectx(struct v4l2_vp9_frame_context *frame_ctx_helper,
1439 struct vdec_vp9_slice_frame_ctx *frame_ctx)
1440 {
1441 struct v4l2_vp9_frame_mv_context *mv = &frame_ctx_helper->mv;
1442 u32 i, j, k;
1443
1444 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx8); i++)
1445 memcpy(frame_ctx->tx_p8x8[i], frame_ctx_helper->tx8[i],
1446 sizeof(frame_ctx_helper->tx8[0]));
1447
1448 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx16); i++)
1449 memcpy(frame_ctx->tx_p16x16[i], frame_ctx_helper->tx16[i],
1450 sizeof(frame_ctx_helper->tx16[0]));
1451
1452 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx32); i++)
1453 memcpy(frame_ctx->tx_p32x32[i], frame_ctx_helper->tx32[i],
1454 sizeof(frame_ctx_helper->tx32[0]));
1455
1456 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->coef); i++)
1457 for (j = 0; j < ARRAY_SIZE(frame_ctx_helper->coef[0]); j++)
1458 for (k = 0; k < ARRAY_SIZE(frame_ctx_helper->coef[0][0]); k++)
1459 vdec_vp9_slice_map_from_coef(i, j, k, frame_ctx,
1460 frame_ctx_helper);
1461
1462 memcpy(frame_ctx->skip_probs, frame_ctx_helper->skip, sizeof(frame_ctx_helper->skip));
1463
1464 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->inter_mode); i++)
1465 memcpy(frame_ctx->inter_mode_probs[i], frame_ctx_helper->inter_mode[i],
1466 sizeof(frame_ctx_helper->inter_mode[0]));
1467
1468 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->interp_filter); i++)
1469 memcpy(frame_ctx->switch_interp_prob[i], frame_ctx_helper->interp_filter[i],
1470 sizeof(frame_ctx_helper->interp_filter[0]));
1471
1472 memcpy(frame_ctx->intra_inter_prob, frame_ctx_helper->is_inter,
1473 sizeof(frame_ctx_helper->is_inter));
1474
1475 memcpy(frame_ctx->comp_inter_prob, frame_ctx_helper->comp_mode,
1476 sizeof(frame_ctx_helper->comp_mode));
1477
1478 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->single_ref); i++)
1479 memcpy(frame_ctx->single_ref_prob[i], frame_ctx_helper->single_ref[i],
1480 sizeof(frame_ctx_helper->single_ref[0]));
1481
1482 memcpy(frame_ctx->comp_ref_prob, frame_ctx_helper->comp_ref,
1483 sizeof(frame_ctx_helper->comp_ref));
1484
1485 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->y_mode); i++)
1486 memcpy(frame_ctx->y_mode_prob[i], frame_ctx_helper->y_mode[i],
1487 sizeof(frame_ctx_helper->y_mode[0]));
1488
1489 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->uv_mode); i++)
1490 memcpy(frame_ctx->uv_mode_prob[i], frame_ctx_helper->uv_mode[i],
1491 sizeof(frame_ctx_helper->uv_mode[0]));
1492
1493 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->partition); i++)
1494 memcpy(frame_ctx->partition_prob[i], frame_ctx_helper->partition[i],
1495 sizeof(frame_ctx_helper->partition[0]));
1496
1497 memcpy(frame_ctx->joint, mv->joint, sizeof(mv->joint));
1498
1499 for (i = 0; i < ARRAY_SIZE(mv->sign); i++)
1500 frame_ctx->sign_classes[i].sign = mv->sign[i];
1501
1502 for (i = 0; i < ARRAY_SIZE(mv->classes); i++)
1503 memcpy(frame_ctx->sign_classes[i].classes, mv->classes[i],
1504 sizeof(mv->classes[i]));
1505
1506 for (i = 0; i < ARRAY_SIZE(mv->class0_bit); i++)
1507 frame_ctx->class0_bits[i].class0[0] = mv->class0_bit[i];
1508
1509 for (i = 0; i < ARRAY_SIZE(mv->bits); i++)
1510 memcpy(frame_ctx->class0_bits[i].bits, mv->bits[i], sizeof(mv->bits[0]));
1511
1512 for (i = 0; i < ARRAY_SIZE(mv->class0_fr); i++)
1513 for (j = 0; j < ARRAY_SIZE(mv->class0_fr[0]); j++)
1514 memcpy(frame_ctx->class0_fp_hp[i].class0_fp[j], mv->class0_fr[i][j],
1515 sizeof(mv->class0_fr[0][0]));
1516
1517 for (i = 0; i < ARRAY_SIZE(mv->fr); i++)
1518 memcpy(frame_ctx->class0_fp_hp[i].fp, mv->fr[i], sizeof(mv->fr[0]));
1519
1520 for (i = 0; i < ARRAY_SIZE(mv->class0_hp); i++)
1521 frame_ctx->class0_fp_hp[i].class0_hp = mv->class0_hp[i];
1522
1523 for (i = 0; i < ARRAY_SIZE(mv->hp); i++)
1524 frame_ctx->class0_fp_hp[i].hp = mv->hp[i];
1525 }
1526
vdec_vp9_slice_update_prob(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_vsi * vsi)1527 static int vdec_vp9_slice_update_prob(struct vdec_vp9_slice_instance *instance,
1528 struct vdec_vp9_slice_vsi *vsi)
1529 {
1530 struct vdec_vp9_slice_frame_ctx *pre_frame_ctx;
1531 struct v4l2_vp9_frame_context *pre_frame_ctx_helper;
1532 struct vdec_vp9_slice_frame_ctx *frame_ctx;
1533 struct vdec_vp9_slice_frame_counts *counts;
1534 struct v4l2_vp9_frame_symbol_counts *counts_helper;
1535 struct vdec_vp9_slice_uncompressed_header *uh;
1536 bool frame_is_intra;
1537 bool use_128;
1538
1539 uh = &vsi->frame.uh;
1540 pre_frame_ctx = &instance->frame_ctx[uh->frame_context_idx];
1541 pre_frame_ctx_helper = &instance->frame_ctx_helper;
1542 frame_ctx = (struct vdec_vp9_slice_frame_ctx *)instance->prob.va;
1543 counts = (struct vdec_vp9_slice_frame_counts *)instance->counts.va;
1544 counts_helper = &instance->counts_helper;
1545
1546 if (!uh->refresh_frame_context)
1547 return 0;
1548
1549 if (!uh->frame_parallel_decoding_mode) {
1550 vdec_vp9_slice_counts_map_helper(&instance->counts_map, counts, counts_helper);
1551
1552 frame_is_intra = !vsi->frame.uh.frame_type || vsi->frame.uh.intra_only;
1553 /* check default prob */
1554 if (!instance->dirty[uh->frame_context_idx])
1555 vdec_vp9_slice_framectx_map_helper(frame_is_intra,
1556 vdec_vp9_slice_default_frame_ctx,
1557 frame_ctx,
1558 pre_frame_ctx_helper);
1559 else
1560 vdec_vp9_slice_framectx_map_helper(frame_is_intra,
1561 pre_frame_ctx,
1562 frame_ctx,
1563 pre_frame_ctx_helper);
1564
1565 use_128 = !frame_is_intra && !vsi->frame.uh.last_frame_type;
1566 v4l2_vp9_adapt_coef_probs(pre_frame_ctx_helper,
1567 counts_helper,
1568 use_128,
1569 frame_is_intra);
1570 if (!frame_is_intra)
1571 v4l2_vp9_adapt_noncoef_probs(pre_frame_ctx_helper,
1572 counts_helper,
1573 V4L2_VP9_REFERENCE_MODE_SINGLE_REFERENCE,
1574 vsi->frame.uh.interpolation_filter,
1575 vsi->frame.ch.tx_mode,
1576 vsi->frame.uh.allow_high_precision_mv ?
1577 V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV : 0);
1578 vdec_vp9_slice_helper_map_framectx(pre_frame_ctx_helper, pre_frame_ctx);
1579 } else {
1580 memcpy(pre_frame_ctx, frame_ctx, sizeof(*frame_ctx));
1581 }
1582
1583 instance->dirty[uh->frame_context_idx] = 1;
1584
1585 return 0;
1586 }
1587
vdec_vp9_slice_update_single(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_pfc * pfc)1588 static int vdec_vp9_slice_update_single(struct vdec_vp9_slice_instance *instance,
1589 struct vdec_vp9_slice_pfc *pfc)
1590 {
1591 struct vdec_vp9_slice_vsi *vsi;
1592
1593 vsi = &pfc->vsi;
1594 memcpy(&pfc->state[0], &vsi->state, sizeof(vsi->state));
1595
1596 mtk_vdec_debug(instance->ctx, "Frame %u Y_CRC %08x %08x %08x %08x\n",
1597 pfc->seq, vsi->state.crc[0], vsi->state.crc[1],
1598 vsi->state.crc[2], vsi->state.crc[3]);
1599 mtk_vdec_debug(instance->ctx, "Frame %u C_CRC %08x %08x %08x %08x\n",
1600 pfc->seq, vsi->state.crc[4], vsi->state.crc[5],
1601 vsi->state.crc[6], vsi->state.crc[7]);
1602
1603 vdec_vp9_slice_update_prob(instance, vsi);
1604
1605 instance->width = vsi->frame.uh.frame_width;
1606 instance->height = vsi->frame.uh.frame_height;
1607 instance->frame_type = vsi->frame.uh.frame_type;
1608 instance->show_frame = vsi->frame.uh.show_frame;
1609
1610 return 0;
1611 }
1612
vdec_vp9_slice_update_lat(struct vdec_vp9_slice_instance * instance,struct vdec_lat_buf * lat_buf,struct vdec_vp9_slice_pfc * pfc)1613 static int vdec_vp9_slice_update_lat(struct vdec_vp9_slice_instance *instance,
1614 struct vdec_lat_buf *lat_buf,
1615 struct vdec_vp9_slice_pfc *pfc)
1616 {
1617 struct vdec_vp9_slice_vsi *vsi;
1618
1619 vsi = &pfc->vsi;
1620 memcpy(&pfc->state[0], &vsi->state, sizeof(vsi->state));
1621
1622 mtk_vdec_debug(instance->ctx, "Frame %u LAT CRC 0x%08x %lx %lx\n",
1623 pfc->seq, vsi->state.crc[0],
1624 (unsigned long)vsi->trans.dma_addr,
1625 (unsigned long)vsi->trans.dma_addr_end);
1626
1627 /* buffer full, need to re-decode */
1628 if (vsi->state.full) {
1629 /* buffer not enough */
1630 if (vsi->trans.dma_addr_end - vsi->trans.dma_addr ==
1631 vsi->ube.size)
1632 return -ENOMEM;
1633 return -EAGAIN;
1634 }
1635
1636 vdec_vp9_slice_update_prob(instance, vsi);
1637
1638 instance->width = vsi->frame.uh.frame_width;
1639 instance->height = vsi->frame.uh.frame_height;
1640 instance->frame_type = vsi->frame.uh.frame_type;
1641 instance->show_frame = vsi->frame.uh.show_frame;
1642
1643 return 0;
1644 }
1645
vdec_vp9_slice_setup_core_to_dst_buf(struct vdec_vp9_slice_instance * instance,struct vdec_lat_buf * lat_buf)1646 static int vdec_vp9_slice_setup_core_to_dst_buf(struct vdec_vp9_slice_instance *instance,
1647 struct vdec_lat_buf *lat_buf)
1648 {
1649 struct vb2_v4l2_buffer *dst;
1650
1651 dst = v4l2_m2m_next_dst_buf(instance->ctx->m2m_ctx);
1652 if (!dst)
1653 return -EINVAL;
1654
1655 v4l2_m2m_buf_copy_metadata(&lat_buf->ts_info, dst, true);
1656 return 0;
1657 }
1658
vdec_vp9_slice_setup_core_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_pfc * pfc,struct vdec_vp9_slice_vsi * vsi,struct vdec_fb * fb,struct vdec_lat_buf * lat_buf)1659 static int vdec_vp9_slice_setup_core_buffer(struct vdec_vp9_slice_instance *instance,
1660 struct vdec_vp9_slice_pfc *pfc,
1661 struct vdec_vp9_slice_vsi *vsi,
1662 struct vdec_fb *fb,
1663 struct vdec_lat_buf *lat_buf)
1664 {
1665 struct vb2_buffer *vb;
1666 struct vb2_queue *vq;
1667 struct vdec_vp9_slice_reference *ref;
1668 int plane;
1669 int size;
1670 int w;
1671 int h;
1672 int i;
1673
1674 plane = instance->ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes;
1675 w = vsi->frame.uh.frame_width;
1676 h = vsi->frame.uh.frame_height;
1677 size = ALIGN(w, 64) * ALIGN(h, 64);
1678
1679 /* frame buffer */
1680 vsi->fb.y.dma_addr = fb->base_y.dma_addr;
1681 if (plane == 1)
1682 vsi->fb.c.dma_addr = fb->base_y.dma_addr + size;
1683 else
1684 vsi->fb.c.dma_addr = fb->base_c.dma_addr;
1685
1686 /* reference buffers */
1687 vq = v4l2_m2m_get_vq(instance->ctx->m2m_ctx,
1688 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
1689 if (!vq)
1690 return -EINVAL;
1691
1692 /* get current output buffer */
1693 vb = &v4l2_m2m_next_dst_buf(instance->ctx->m2m_ctx)->vb2_buf;
1694 if (!vb)
1695 return -EINVAL;
1696
1697 /* update internal buffer's width/height */
1698 instance->dpb[vb->index].width = w;
1699 instance->dpb[vb->index].height = h;
1700
1701 /*
1702 * get buffer's width/height from instance
1703 * get buffer address from vb2buf
1704 */
1705 for (i = 0; i < 3; i++) {
1706 ref = &vsi->frame.ref[i];
1707 vb = vb2_find_buffer(vq, pfc->ref_idx[i]);
1708 if (!vb) {
1709 ref->frame_width = w;
1710 ref->frame_height = h;
1711 memset(&vsi->ref[i], 0, sizeof(vsi->ref[i]));
1712 } else {
1713 int idx = vb->index;
1714
1715 ref->frame_width = instance->dpb[idx].width;
1716 ref->frame_height = instance->dpb[idx].height;
1717 vsi->ref[i].y.dma_addr =
1718 vb2_dma_contig_plane_dma_addr(vb, 0);
1719 if (plane == 1)
1720 vsi->ref[i].c.dma_addr =
1721 vsi->ref[i].y.dma_addr + size;
1722 else
1723 vsi->ref[i].c.dma_addr =
1724 vb2_dma_contig_plane_dma_addr(vb, 1);
1725 }
1726 }
1727
1728 return 0;
1729 }
1730
vdec_vp9_slice_setup_single_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_pfc * pfc,struct vdec_vp9_slice_vsi * vsi,struct mtk_vcodec_mem * bs,struct vdec_fb * fb)1731 static void vdec_vp9_slice_setup_single_buffer(struct vdec_vp9_slice_instance *instance,
1732 struct vdec_vp9_slice_pfc *pfc,
1733 struct vdec_vp9_slice_vsi *vsi,
1734 struct mtk_vcodec_mem *bs,
1735 struct vdec_fb *fb)
1736 {
1737 int i;
1738
1739 vsi->bs.buf.dma_addr = bs->dma_addr;
1740 vsi->bs.buf.size = bs->size;
1741 vsi->bs.frame.dma_addr = bs->dma_addr;
1742 vsi->bs.frame.size = bs->size;
1743
1744 for (i = 0; i < 2; i++) {
1745 vsi->mv[i].dma_addr = instance->mv[i].dma_addr;
1746 vsi->mv[i].size = instance->mv[i].size;
1747 }
1748 for (i = 0; i < 2; i++) {
1749 vsi->seg[i].dma_addr = instance->seg[i].dma_addr;
1750 vsi->seg[i].size = instance->seg[i].size;
1751 }
1752 vsi->tile.dma_addr = instance->tile.dma_addr;
1753 vsi->tile.size = instance->tile.size;
1754 vsi->prob.dma_addr = instance->prob.dma_addr;
1755 vsi->prob.size = instance->prob.size;
1756 vsi->counts.dma_addr = instance->counts.dma_addr;
1757 vsi->counts.size = instance->counts.size;
1758
1759 vsi->row_info.buf = 0;
1760 vsi->row_info.size = 0;
1761
1762 vdec_vp9_slice_setup_core_buffer(instance, pfc, vsi, fb, NULL);
1763 }
1764
vdec_vp9_slice_setup_core(struct vdec_vp9_slice_instance * instance,struct vdec_fb * fb,struct vdec_lat_buf * lat_buf,struct vdec_vp9_slice_pfc * pfc)1765 static int vdec_vp9_slice_setup_core(struct vdec_vp9_slice_instance *instance,
1766 struct vdec_fb *fb,
1767 struct vdec_lat_buf *lat_buf,
1768 struct vdec_vp9_slice_pfc *pfc)
1769 {
1770 struct vdec_vp9_slice_vsi *vsi = &pfc->vsi;
1771 int ret;
1772
1773 vdec_vp9_slice_setup_state(vsi);
1774
1775 ret = vdec_vp9_slice_setup_core_to_dst_buf(instance, lat_buf);
1776 if (ret)
1777 goto err;
1778
1779 ret = vdec_vp9_slice_setup_core_buffer(instance, pfc, vsi, fb, lat_buf);
1780 if (ret)
1781 goto err;
1782
1783 vdec_vp9_slice_setup_seg_buffer(instance, vsi, &instance->seg[1]);
1784
1785 return 0;
1786
1787 err:
1788 return ret;
1789 }
1790
vdec_vp9_slice_setup_single(struct vdec_vp9_slice_instance * instance,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,struct vdec_vp9_slice_pfc * pfc)1791 static int vdec_vp9_slice_setup_single(struct vdec_vp9_slice_instance *instance,
1792 struct mtk_vcodec_mem *bs,
1793 struct vdec_fb *fb,
1794 struct vdec_vp9_slice_pfc *pfc)
1795 {
1796 struct vdec_vp9_slice_vsi *vsi = &pfc->vsi;
1797 int ret;
1798
1799 ret = vdec_vp9_slice_setup_single_from_src_to_dst(instance);
1800 if (ret)
1801 goto err;
1802
1803 ret = vdec_vp9_slice_setup_pfc(instance, pfc);
1804 if (ret)
1805 goto err;
1806
1807 ret = vdec_vp9_slice_alloc_working_buffer(instance, vsi);
1808 if (ret)
1809 goto err;
1810
1811 vdec_vp9_slice_setup_single_buffer(instance, pfc, vsi, bs, fb);
1812 vdec_vp9_slice_setup_seg_buffer(instance, vsi, &instance->seg[0]);
1813
1814 ret = vdec_vp9_slice_setup_prob_buffer(instance, vsi);
1815 if (ret)
1816 goto err;
1817
1818 ret = vdec_vp9_slice_setup_tile_buffer(instance, vsi, bs);
1819 if (ret)
1820 goto err;
1821
1822 return 0;
1823
1824 err:
1825 return ret;
1826 }
1827
vdec_vp9_slice_update_core(struct vdec_vp9_slice_instance * instance,struct vdec_lat_buf * lat_buf,struct vdec_vp9_slice_pfc * pfc)1828 static int vdec_vp9_slice_update_core(struct vdec_vp9_slice_instance *instance,
1829 struct vdec_lat_buf *lat_buf,
1830 struct vdec_vp9_slice_pfc *pfc)
1831 {
1832 struct vdec_vp9_slice_vsi *vsi;
1833
1834 vsi = &pfc->vsi;
1835 memcpy(&pfc->state[1], &vsi->state, sizeof(vsi->state));
1836
1837 mtk_vdec_debug(instance->ctx, "Frame %u Y_CRC %08x %08x %08x %08x\n",
1838 pfc->seq, vsi->state.crc[0], vsi->state.crc[1],
1839 vsi->state.crc[2], vsi->state.crc[3]);
1840 mtk_vdec_debug(instance->ctx, "Frame %u C_CRC %08x %08x %08x %08x\n",
1841 pfc->seq, vsi->state.crc[4], vsi->state.crc[5],
1842 vsi->state.crc[6], vsi->state.crc[7]);
1843
1844 return 0;
1845 }
1846
vdec_vp9_slice_init(struct mtk_vcodec_dec_ctx * ctx)1847 static int vdec_vp9_slice_init(struct mtk_vcodec_dec_ctx *ctx)
1848 {
1849 struct vdec_vp9_slice_instance *instance;
1850 struct vdec_vp9_slice_init_vsi *vsi;
1851 int ret;
1852
1853 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
1854 if (!instance)
1855 return -ENOMEM;
1856
1857 instance->ctx = ctx;
1858 instance->vpu.id = SCP_IPI_VDEC_LAT;
1859 instance->vpu.core_id = SCP_IPI_VDEC_CORE;
1860 instance->vpu.ctx = ctx;
1861 instance->vpu.codec_type = ctx->current_codec;
1862
1863 ret = vpu_dec_init(&instance->vpu);
1864 if (ret) {
1865 mtk_vdec_err(ctx, "failed to init vpu dec, ret %d\n", ret);
1866 goto error_vpu_init;
1867 }
1868
1869 /* init vsi and global flags */
1870
1871 vsi = instance->vpu.vsi;
1872 if (!vsi) {
1873 mtk_vdec_err(ctx, "failed to get VP9 vsi\n");
1874 ret = -EINVAL;
1875 goto error_vsi;
1876 }
1877 instance->init_vsi = vsi;
1878 instance->core_vsi = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
1879 (u32)vsi->core_vsi);
1880 if (!instance->core_vsi) {
1881 mtk_vdec_err(ctx, "failed to get VP9 core vsi\n");
1882 ret = -EINVAL;
1883 goto error_vsi;
1884 }
1885
1886 instance->irq = 1;
1887
1888 ret = vdec_vp9_slice_init_default_frame_ctx(instance);
1889 if (ret)
1890 goto error_default_frame_ctx;
1891
1892 ctx->drv_handle = instance;
1893
1894 return 0;
1895
1896 error_default_frame_ctx:
1897 error_vsi:
1898 vpu_dec_deinit(&instance->vpu);
1899 error_vpu_init:
1900 kfree(instance);
1901 return ret;
1902 }
1903
vdec_vp9_slice_deinit(void * h_vdec)1904 static void vdec_vp9_slice_deinit(void *h_vdec)
1905 {
1906 struct vdec_vp9_slice_instance *instance = h_vdec;
1907
1908 if (!instance)
1909 return;
1910
1911 vpu_dec_deinit(&instance->vpu);
1912 vdec_vp9_slice_free_working_buffer(instance);
1913 vdec_msg_queue_deinit(&instance->ctx->msg_queue, instance->ctx);
1914 kfree(instance);
1915 }
1916
vdec_vp9_slice_flush(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)1917 static int vdec_vp9_slice_flush(void *h_vdec, struct mtk_vcodec_mem *bs,
1918 struct vdec_fb *fb, bool *res_chg)
1919 {
1920 struct vdec_vp9_slice_instance *instance = h_vdec;
1921
1922 mtk_vdec_debug(instance->ctx, "flush ...\n");
1923 if (instance->ctx->dev->vdec_pdata->hw_arch != MTK_VDEC_PURE_SINGLE_CORE)
1924 vdec_msg_queue_wait_lat_buf_full(&instance->ctx->msg_queue);
1925 return vpu_dec_reset(&instance->vpu);
1926 }
1927
vdec_vp9_slice_get_pic_info(struct vdec_vp9_slice_instance * instance)1928 static void vdec_vp9_slice_get_pic_info(struct vdec_vp9_slice_instance *instance)
1929 {
1930 struct mtk_vcodec_dec_ctx *ctx = instance->ctx;
1931 unsigned int data[3];
1932
1933 mtk_vdec_debug(instance->ctx, "w %u h %u\n", ctx->picinfo.pic_w, ctx->picinfo.pic_h);
1934
1935 data[0] = ctx->picinfo.pic_w;
1936 data[1] = ctx->picinfo.pic_h;
1937 data[2] = ctx->capture_fourcc;
1938 vpu_dec_get_param(&instance->vpu, data, 3, GET_PARAM_PIC_INFO);
1939
1940 ctx->picinfo.buf_w = ALIGN(ctx->picinfo.pic_w, 64);
1941 ctx->picinfo.buf_h = ALIGN(ctx->picinfo.pic_h, 64);
1942 ctx->picinfo.fb_sz[0] = instance->vpu.fb_sz[0];
1943 ctx->picinfo.fb_sz[1] = instance->vpu.fb_sz[1];
1944 }
1945
vdec_vp9_slice_get_dpb_size(struct vdec_vp9_slice_instance * instance,unsigned int * dpb_sz)1946 static void vdec_vp9_slice_get_dpb_size(struct vdec_vp9_slice_instance *instance,
1947 unsigned int *dpb_sz)
1948 {
1949 /* refer VP9 specification */
1950 *dpb_sz = 9;
1951 }
1952
vdec_vp9_slice_get_param(void * h_vdec,enum vdec_get_param_type type,void * out)1953 static int vdec_vp9_slice_get_param(void *h_vdec, enum vdec_get_param_type type, void *out)
1954 {
1955 struct vdec_vp9_slice_instance *instance = h_vdec;
1956
1957 switch (type) {
1958 case GET_PARAM_PIC_INFO:
1959 vdec_vp9_slice_get_pic_info(instance);
1960 break;
1961 case GET_PARAM_DPB_SIZE:
1962 vdec_vp9_slice_get_dpb_size(instance, out);
1963 break;
1964 case GET_PARAM_CROP_INFO:
1965 mtk_vdec_debug(instance->ctx, "No need to get vp9 crop information.");
1966 break;
1967 default:
1968 mtk_vdec_err(instance->ctx, "invalid get parameter type=%d\n", type);
1969 return -EINVAL;
1970 }
1971
1972 return 0;
1973 }
1974
vdec_vp9_slice_single_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)1975 static int vdec_vp9_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
1976 struct vdec_fb *fb, bool *res_chg)
1977 {
1978 struct vdec_vp9_slice_instance *instance = h_vdec;
1979 struct vdec_vp9_slice_pfc *pfc = &instance->sc_pfc;
1980 struct vdec_vp9_slice_vsi *vsi;
1981 struct mtk_vcodec_dec_ctx *ctx;
1982 int ret;
1983
1984 if (!instance || !instance->ctx)
1985 return -EINVAL;
1986 ctx = instance->ctx;
1987
1988 /* bs NULL means flush decoder */
1989 if (!bs)
1990 return vdec_vp9_slice_flush(h_vdec, bs, fb, res_chg);
1991
1992 fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
1993 if (!fb)
1994 return -EBUSY;
1995
1996 vsi = &pfc->vsi;
1997
1998 ret = vdec_vp9_slice_setup_single(instance, bs, fb, pfc);
1999 if (ret) {
2000 mtk_vdec_err(ctx, "Failed to setup VP9 single ret %d\n", ret);
2001 return ret;
2002 }
2003 vdec_vp9_slice_vsi_to_remote(vsi, instance->vsi);
2004
2005 ret = vpu_dec_start(&instance->vpu, NULL, 0);
2006 if (ret) {
2007 mtk_vdec_err(ctx, "Failed to dec VP9 ret %d\n", ret);
2008 return ret;
2009 }
2010
2011 ret = mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
2012 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
2013 /* update remote vsi if decode timeout */
2014 if (ret) {
2015 mtk_vdec_err(ctx, "VP9 decode timeout %d\n", ret);
2016 WRITE_ONCE(instance->vsi->state.timeout, 1);
2017 }
2018
2019 vpu_dec_end(&instance->vpu);
2020
2021 vdec_vp9_slice_vsi_from_remote(vsi, instance->vsi, 0);
2022 ret = vdec_vp9_slice_update_single(instance, pfc);
2023 if (ret) {
2024 mtk_vdec_err(ctx, "VP9 decode error: %d\n", ret);
2025 return ret;
2026 }
2027
2028 instance->ctx->decoded_frame_cnt++;
2029 return 0;
2030 }
2031
vdec_vp9_slice_lat_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)2032 static int vdec_vp9_slice_lat_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
2033 struct vdec_fb *fb, bool *res_chg)
2034 {
2035 struct vdec_vp9_slice_instance *instance = h_vdec;
2036 struct vdec_lat_buf *lat_buf;
2037 struct vdec_vp9_slice_pfc *pfc;
2038 struct vdec_vp9_slice_vsi *vsi;
2039 struct mtk_vcodec_dec_ctx *ctx;
2040 int ret;
2041
2042 if (!instance || !instance->ctx)
2043 return -EINVAL;
2044 ctx = instance->ctx;
2045
2046 /* init msgQ for the first time */
2047 if (vdec_msg_queue_init(&ctx->msg_queue, ctx,
2048 vdec_vp9_slice_core_decode,
2049 sizeof(*pfc)))
2050 return -ENOMEM;
2051
2052 /* bs NULL means flush decoder */
2053 if (!bs)
2054 return vdec_vp9_slice_flush(h_vdec, bs, fb, res_chg);
2055
2056 lat_buf = vdec_msg_queue_dqbuf(&instance->ctx->msg_queue.lat_ctx);
2057 if (!lat_buf) {
2058 mtk_vdec_debug(ctx, "Failed to get VP9 lat buf\n");
2059 return -EAGAIN;
2060 }
2061 pfc = (struct vdec_vp9_slice_pfc *)lat_buf->private_data;
2062 if (!pfc) {
2063 ret = -EINVAL;
2064 goto err_free_fb_out;
2065 }
2066 vsi = &pfc->vsi;
2067
2068 ret = vdec_vp9_slice_setup_lat(instance, bs, lat_buf, pfc);
2069 if (ret) {
2070 mtk_vdec_err(ctx, "Failed to setup VP9 lat ret %d\n", ret);
2071 goto err_free_fb_out;
2072 }
2073 vdec_vp9_slice_vsi_to_remote(vsi, instance->vsi);
2074
2075 ret = vpu_dec_start(&instance->vpu, NULL, 0);
2076 if (ret) {
2077 mtk_vdec_err(ctx, "Failed to dec VP9 ret %d\n", ret);
2078 goto err_free_fb_out;
2079 }
2080
2081 if (instance->irq) {
2082 ret = mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
2083 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_LAT0);
2084 /* update remote vsi if decode timeout */
2085 if (ret) {
2086 mtk_vdec_err(ctx, "VP9 decode timeout %d pic %d\n", ret, pfc->seq);
2087 WRITE_ONCE(instance->vsi->state.timeout, 1);
2088 }
2089 vpu_dec_end(&instance->vpu);
2090 }
2091
2092 vdec_vp9_slice_vsi_from_remote(vsi, instance->vsi, 0);
2093 ret = vdec_vp9_slice_update_lat(instance, lat_buf, pfc);
2094
2095 /* LAT trans full, no more UBE or decode timeout */
2096 if (ret) {
2097 mtk_vdec_err(ctx, "VP9 decode error: %d\n", ret);
2098 goto err_free_fb_out;
2099 }
2100
2101 mtk_vdec_debug(ctx, "lat dma addr: 0x%lx 0x%lx\n",
2102 (unsigned long)pfc->vsi.trans.dma_addr,
2103 (unsigned long)pfc->vsi.trans.dma_addr_end);
2104
2105 vdec_msg_queue_update_ube_wptr(&ctx->msg_queue,
2106 vsi->trans.dma_addr_end +
2107 ctx->msg_queue.wdma_addr.dma_addr);
2108 vdec_msg_queue_qbuf(&ctx->msg_queue.core_ctx, lat_buf);
2109
2110 return 0;
2111 err_free_fb_out:
2112 vdec_msg_queue_qbuf(&ctx->msg_queue.lat_ctx, lat_buf);
2113 return ret;
2114 }
2115
vdec_vp9_slice_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)2116 static int vdec_vp9_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
2117 struct vdec_fb *fb, bool *res_chg)
2118 {
2119 struct vdec_vp9_slice_instance *instance = h_vdec;
2120 int ret;
2121
2122 if (instance->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
2123 ret = vdec_vp9_slice_single_decode(h_vdec, bs, fb, res_chg);
2124 else
2125 ret = vdec_vp9_slice_lat_decode(h_vdec, bs, fb, res_chg);
2126
2127 return ret;
2128 }
2129
vdec_vp9_slice_core_decode(struct vdec_lat_buf * lat_buf)2130 static int vdec_vp9_slice_core_decode(struct vdec_lat_buf *lat_buf)
2131 {
2132 struct vdec_vp9_slice_instance *instance;
2133 struct vdec_vp9_slice_pfc *pfc;
2134 struct mtk_vcodec_dec_ctx *ctx = NULL;
2135 struct vdec_fb *fb = NULL;
2136 int ret = -EINVAL;
2137
2138 if (!lat_buf)
2139 goto err;
2140
2141 pfc = lat_buf->private_data;
2142 ctx = lat_buf->ctx;
2143 if (!pfc || !ctx)
2144 goto err;
2145
2146 instance = ctx->drv_handle;
2147 if (!instance)
2148 goto err;
2149
2150 fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
2151 if (!fb) {
2152 ret = -EBUSY;
2153 goto err;
2154 }
2155
2156 ret = vdec_vp9_slice_setup_core(instance, fb, lat_buf, pfc);
2157 if (ret) {
2158 mtk_vdec_err(ctx, "vdec_vp9_slice_setup_core\n");
2159 goto err;
2160 }
2161 vdec_vp9_slice_vsi_to_remote(&pfc->vsi, instance->core_vsi);
2162
2163 ret = vpu_dec_core(&instance->vpu);
2164 if (ret) {
2165 mtk_vdec_err(ctx, "vpu_dec_core\n");
2166 goto err;
2167 }
2168
2169 if (instance->irq) {
2170 ret = mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
2171 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
2172 /* update remote vsi if decode timeout */
2173 if (ret) {
2174 mtk_vdec_err(ctx, "VP9 core timeout pic %d\n", pfc->seq);
2175 WRITE_ONCE(instance->core_vsi->state.timeout, 1);
2176 }
2177 vpu_dec_core_end(&instance->vpu);
2178 }
2179
2180 vdec_vp9_slice_vsi_from_remote(&pfc->vsi, instance->core_vsi, 1);
2181 ret = vdec_vp9_slice_update_core(instance, lat_buf, pfc);
2182 if (ret) {
2183 mtk_vdec_err(ctx, "vdec_vp9_slice_update_core\n");
2184 goto err;
2185 }
2186
2187 pfc->vsi.trans.dma_addr_end += ctx->msg_queue.wdma_addr.dma_addr;
2188 mtk_vdec_debug(ctx, "core dma_addr_end 0x%lx\n",
2189 (unsigned long)pfc->vsi.trans.dma_addr_end);
2190 vdec_msg_queue_update_ube_rptr(&ctx->msg_queue, pfc->vsi.trans.dma_addr_end);
2191 ctx->dev->vdec_pdata->cap_to_disp(ctx, 0, lat_buf->src_buf_req);
2192
2193 return 0;
2194
2195 err:
2196 if (ctx && pfc) {
2197 /* always update read pointer */
2198 vdec_msg_queue_update_ube_rptr(&ctx->msg_queue, pfc->vsi.trans.dma_addr_end);
2199
2200 if (fb)
2201 ctx->dev->vdec_pdata->cap_to_disp(ctx, 1, lat_buf->src_buf_req);
2202 }
2203 return ret;
2204 }
2205
2206 const struct vdec_common_if vdec_vp9_slice_lat_if = {
2207 .init = vdec_vp9_slice_init,
2208 .decode = vdec_vp9_slice_decode,
2209 .get_param = vdec_vp9_slice_get_param,
2210 .deinit = vdec_vp9_slice_deinit,
2211 };
2212