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 * @mv_joint: motion vector joint counts.
145 * @sign: motion vector sign counts.
146 * @classes: motion vector class counts.
147 * @class0: motion vector class0 bit counts.
148 * @bits: motion vector bits counts.
149 * @class0_fp: motion vector class0 fractional bit counts.
150 * @fp: motion vector fractional bit counts.
151 * @class0_hp: motion vector class0 high precision fractional bit counts.
152 * @hp: motion vector high precision fractional bit counts.
153 */
154 struct vdec_vp9_slice_counts_map {
155 u32 skip[3][2];
156 u32 y_mode[4][10];
157 u32 filter[4][3];
158 u32 sign[2][2];
159 u32 classes[2][11];
160 u32 class0[2][2];
161 u32 bits[2][10][2];
162 u32 class0_fp[2][2][4];
163 u32 fp[2][4];
164 u32 class0_hp[2][2];
165 u32 hp[2][2];
166 };
167
168 /*
169 * struct vdec_vp9_slice_uncompressed_header - vp9 uncompressed header syntax
170 * used for decoding
171 */
172 struct vdec_vp9_slice_uncompressed_header {
173 u8 profile;
174 u8 last_frame_type;
175 u8 frame_type;
176
177 u8 last_show_frame;
178 u8 show_frame;
179 u8 error_resilient_mode;
180
181 u8 bit_depth;
182 u8 padding0[1];
183 u16 last_frame_width;
184 u16 last_frame_height;
185 u16 frame_width;
186 u16 frame_height;
187
188 u8 intra_only;
189 u8 reset_frame_context;
190 u8 ref_frame_sign_bias[4];
191 u8 allow_high_precision_mv;
192 u8 interpolation_filter;
193
194 u8 refresh_frame_context;
195 u8 frame_parallel_decoding_mode;
196 u8 frame_context_idx;
197
198 /* loop_filter_params */
199 u8 loop_filter_level;
200 u8 loop_filter_sharpness;
201 u8 loop_filter_delta_enabled;
202 s8 loop_filter_ref_deltas[4];
203 s8 loop_filter_mode_deltas[2];
204
205 /* quantization_params */
206 u8 base_q_idx;
207 s8 delta_q_y_dc;
208 s8 delta_q_uv_dc;
209 s8 delta_q_uv_ac;
210
211 /* segmentation_params */
212 u8 segmentation_enabled;
213 u8 segmentation_update_map;
214 u8 segmentation_tree_probs[7];
215 u8 padding1[1];
216 u8 segmentation_temporal_udpate;
217 u8 segmentation_pred_prob[3];
218 u8 segmentation_update_data;
219 u8 segmentation_abs_or_delta_update;
220 u8 feature_enabled[8];
221 s16 feature_value[8][4];
222
223 /* tile_info */
224 u8 tile_cols_log2;
225 u8 tile_rows_log2;
226 u8 padding2[2];
227
228 u16 uncompressed_header_size;
229 u16 header_size_in_bytes;
230
231 /* LAT OUT, CORE IN */
232 u32 dequant[8][4];
233 };
234
235 /*
236 * struct vdec_vp9_slice_compressed_header - vp9 compressed header syntax
237 * used for decoding.
238 */
239 struct vdec_vp9_slice_compressed_header {
240 u8 tx_mode;
241 u8 ref_mode;
242 u8 comp_fixed_ref;
243 u8 comp_var_ref[2];
244 u8 padding[3];
245 };
246
247 /*
248 * struct vdec_vp9_slice_tiles - vp9 tile syntax
249 */
250 struct vdec_vp9_slice_tiles {
251 u32 size[4][64];
252 u32 mi_rows[4];
253 u32 mi_cols[64];
254 u8 actual_rows;
255 u8 padding[7];
256 };
257
258 /*
259 * struct vdec_vp9_slice_reference - vp9 reference frame information
260 */
261 struct vdec_vp9_slice_reference {
262 u16 frame_width;
263 u16 frame_height;
264 u8 bit_depth;
265 u8 subsampling_x;
266 u8 subsampling_y;
267 u8 padding;
268 };
269
270 /*
271 * struct vdec_vp9_slice_frame - vp9 syntax used for decoding
272 */
273 struct vdec_vp9_slice_frame {
274 struct vdec_vp9_slice_uncompressed_header uh;
275 struct vdec_vp9_slice_compressed_header ch;
276 struct vdec_vp9_slice_tiles tiles;
277 struct vdec_vp9_slice_reference ref[3];
278 };
279
280 /*
281 * struct vdec_vp9_slice_init_vsi - VSI used to initialize instance
282 */
283 struct vdec_vp9_slice_init_vsi {
284 unsigned int architecture;
285 unsigned int reserved;
286 u64 core_vsi;
287 /* default frame context's position in MicroP */
288 u64 default_frame_ctx;
289 };
290
291 /*
292 * struct vdec_vp9_slice_mem - memory address and size
293 */
294 struct vdec_vp9_slice_mem {
295 union {
296 u64 buf;
297 dma_addr_t dma_addr;
298 };
299 union {
300 size_t size;
301 dma_addr_t dma_addr_end;
302 u64 padding;
303 };
304 };
305
306 /*
307 * struct vdec_vp9_slice_bs - input buffer for decoding
308 */
309 struct vdec_vp9_slice_bs {
310 struct vdec_vp9_slice_mem buf;
311 struct vdec_vp9_slice_mem frame;
312 };
313
314 /*
315 * struct vdec_vp9_slice_fb - frame buffer for decoding
316 */
317 struct vdec_vp9_slice_fb {
318 struct vdec_vp9_slice_mem y;
319 struct vdec_vp9_slice_mem c;
320 };
321
322 /*
323 * struct vdec_vp9_slice_state - decoding state
324 */
325 struct vdec_vp9_slice_state {
326 int err;
327 unsigned int full;
328 unsigned int timeout;
329 unsigned int perf;
330
331 unsigned int crc[12];
332 };
333
334 /**
335 * struct vdec_vp9_slice_vsi - exchange decoding information
336 * between Main CPU and MicroP
337 *
338 * @bs: input buffer
339 * @fb: output buffer
340 * @ref: 3 reference buffers
341 * @mv: mv working buffer
342 * @seg: segmentation working buffer
343 * @tile: tile buffer
344 * @prob: prob table buffer, used to set/update prob table
345 * @counts: counts table buffer, used to update prob table
346 * @ube: general buffer
347 * @trans: trans buffer position in general buffer
348 * @err_map: error buffer
349 * @row_info: row info buffer
350 * @frame: decoding syntax
351 * @state: decoding state
352 */
353 struct vdec_vp9_slice_vsi {
354 /* used in LAT stage */
355 struct vdec_vp9_slice_bs bs;
356 /* used in Core stage */
357 struct vdec_vp9_slice_fb fb;
358 struct vdec_vp9_slice_fb ref[3];
359
360 struct vdec_vp9_slice_mem mv[2];
361 struct vdec_vp9_slice_mem seg[2];
362 struct vdec_vp9_slice_mem tile;
363 struct vdec_vp9_slice_mem prob;
364 struct vdec_vp9_slice_mem counts;
365
366 /* LAT stage's output, Core stage's input */
367 struct vdec_vp9_slice_mem ube;
368 struct vdec_vp9_slice_mem trans;
369 struct vdec_vp9_slice_mem err_map;
370 struct vdec_vp9_slice_mem row_info;
371
372 /* decoding parameters */
373 struct vdec_vp9_slice_frame frame;
374
375 struct vdec_vp9_slice_state state;
376 };
377
378 /**
379 * struct vdec_vp9_slice_pfc - per-frame context that contains a local vsi.
380 * pass it from lat to core
381 *
382 * @vsi: local vsi. copy to/from remote vsi before/after decoding
383 * @ref_idx: reference buffer index
384 * @seq: picture sequence
385 * @state: decoding state
386 */
387 struct vdec_vp9_slice_pfc {
388 struct vdec_vp9_slice_vsi vsi;
389
390 u64 ref_idx[3];
391
392 int seq;
393
394 /* LAT/Core CRC */
395 struct vdec_vp9_slice_state state[2];
396 };
397
398 /*
399 * enum vdec_vp9_slice_resolution_level
400 */
401 enum vdec_vp9_slice_resolution_level {
402 VP9_RES_NONE,
403 VP9_RES_FHD,
404 VP9_RES_4K,
405 VP9_RES_8K,
406 };
407
408 /*
409 * struct vdec_vp9_slice_ref - picture's width & height should kept
410 * for later decoding as reference picture
411 */
412 struct vdec_vp9_slice_ref {
413 unsigned int width;
414 unsigned int height;
415 };
416
417 /**
418 * struct vdec_vp9_slice_instance - represent one vp9 instance
419 *
420 * @ctx: pointer to codec's context
421 * @vpu: VPU instance
422 * @seq: global picture sequence
423 * @level: level of current resolution
424 * @width: width of last picture
425 * @height: height of last picture
426 * @frame_type: frame_type of last picture
427 * @irq: irq to Main CPU or MicroP
428 * @show_frame: show_frame of last picture
429 * @dpb: picture information (width/height) for reference
430 * @mv: mv working buffer
431 * @seg: segmentation working buffer
432 * @tile: tile buffer
433 * @prob: prob table buffer, used to set/update prob table
434 * @counts: counts table buffer, used to update prob table
435 * @frame_ctx: 4 frame context according to VP9 Spec
436 * @frame_ctx_helper: 4 frame context according to newest kernel spec
437 * @dirty: state of each frame context
438 * @init_vsi: vsi used for initialized VP9 instance
439 * @vsi: vsi used for decoding/flush ...
440 * @core_vsi: vsi used for Core stage
441 *
442 * @sc_pfc: per frame context single core
443 * @counts_map: used map to counts_helper
444 * @counts_helper: counts table according to newest kernel spec
445 */
446 struct vdec_vp9_slice_instance {
447 struct mtk_vcodec_dec_ctx *ctx;
448 struct vdec_vpu_inst vpu;
449
450 int seq;
451
452 enum vdec_vp9_slice_resolution_level level;
453
454 /* for resolution change and get_pic_info */
455 unsigned int width;
456 unsigned int height;
457
458 /* for last_frame_type */
459 unsigned int frame_type;
460 unsigned int irq;
461
462 unsigned int show_frame;
463
464 /* maintain vp9 reference frame state */
465 struct vdec_vp9_slice_ref dpb[VB2_MAX_FRAME];
466
467 /*
468 * normal working buffers
469 * mv[0]/seg[0]/tile/prob/counts is used for LAT
470 * mv[1]/seg[1] is used for CORE
471 */
472 struct mtk_vcodec_mem mv[2];
473 struct mtk_vcodec_mem seg[2];
474 struct mtk_vcodec_mem tile;
475 struct mtk_vcodec_mem prob;
476 struct mtk_vcodec_mem counts;
477
478 /* 4 prob tables */
479 struct vdec_vp9_slice_frame_ctx frame_ctx[4];
480 /*4 helper tables */
481 struct v4l2_vp9_frame_context frame_ctx_helper;
482 unsigned char dirty[4];
483
484 /* MicroP vsi */
485 union {
486 struct vdec_vp9_slice_init_vsi *init_vsi;
487 struct vdec_vp9_slice_vsi *vsi;
488 };
489 struct vdec_vp9_slice_vsi *core_vsi;
490
491 struct vdec_vp9_slice_pfc sc_pfc;
492 struct vdec_vp9_slice_counts_map counts_map;
493 struct v4l2_vp9_frame_symbol_counts counts_helper;
494 };
495
496 /*
497 * all VP9 instances could share this default frame context.
498 */
499 static struct vdec_vp9_slice_frame_ctx *vdec_vp9_slice_default_frame_ctx;
500 static DEFINE_MUTEX(vdec_vp9_slice_frame_ctx_lock);
501
502 static int vdec_vp9_slice_core_decode(struct vdec_lat_buf *lat_buf);
503
vdec_vp9_slice_init_default_frame_ctx(struct vdec_vp9_slice_instance * instance)504 static int vdec_vp9_slice_init_default_frame_ctx(struct vdec_vp9_slice_instance *instance)
505 {
506 struct vdec_vp9_slice_frame_ctx *remote_frame_ctx;
507 struct vdec_vp9_slice_frame_ctx *frame_ctx;
508 struct mtk_vcodec_dec_ctx *ctx;
509 struct vdec_vp9_slice_init_vsi *vsi;
510 int ret = 0;
511
512 ctx = instance->ctx;
513 vsi = instance->vpu.vsi;
514 if (!ctx || !vsi)
515 return -EINVAL;
516
517 remote_frame_ctx = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
518 (u32)vsi->default_frame_ctx);
519 if (!remote_frame_ctx) {
520 mtk_vdec_err(ctx, "failed to map default frame ctx\n");
521 return -EINVAL;
522 }
523
524 mutex_lock(&vdec_vp9_slice_frame_ctx_lock);
525 if (vdec_vp9_slice_default_frame_ctx)
526 goto out;
527
528 frame_ctx = kmemdup(remote_frame_ctx, sizeof(*frame_ctx), GFP_KERNEL);
529 if (!frame_ctx) {
530 ret = -ENOMEM;
531 goto out;
532 }
533
534 vdec_vp9_slice_default_frame_ctx = frame_ctx;
535
536 out:
537 mutex_unlock(&vdec_vp9_slice_frame_ctx_lock);
538
539 return ret;
540 }
541
vdec_vp9_slice_alloc_working_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_vsi * vsi)542 static int vdec_vp9_slice_alloc_working_buffer(struct vdec_vp9_slice_instance *instance,
543 struct vdec_vp9_slice_vsi *vsi)
544 {
545 struct mtk_vcodec_dec_ctx *ctx = instance->ctx;
546 enum vdec_vp9_slice_resolution_level level;
547 /* super blocks */
548 unsigned int max_sb_w;
549 unsigned int max_sb_h;
550 unsigned int max_w;
551 unsigned int max_h;
552 unsigned int w;
553 unsigned int h;
554 size_t size;
555 int ret;
556 int i;
557
558 w = vsi->frame.uh.frame_width;
559 h = vsi->frame.uh.frame_height;
560
561 if (w > VCODEC_DEC_4K_CODED_WIDTH ||
562 h > VCODEC_DEC_4K_CODED_HEIGHT) {
563 return -EINVAL;
564 } else if (w > MTK_VDEC_MAX_W || h > MTK_VDEC_MAX_H) {
565 /* 4K */
566 level = VP9_RES_4K;
567 max_w = VCODEC_DEC_4K_CODED_WIDTH;
568 max_h = VCODEC_DEC_4K_CODED_HEIGHT;
569 } else {
570 /* FHD */
571 level = VP9_RES_FHD;
572 max_w = MTK_VDEC_MAX_W;
573 max_h = MTK_VDEC_MAX_H;
574 }
575
576 if (level == instance->level)
577 return 0;
578
579 mtk_vdec_debug(ctx, "resolution level changed, from %u to %u, %ux%u",
580 instance->level, level, w, h);
581
582 max_sb_w = DIV_ROUND_UP(max_w, 64);
583 max_sb_h = DIV_ROUND_UP(max_h, 64);
584 ret = -ENOMEM;
585
586 /*
587 * Lat-flush must wait core idle, otherwise core will
588 * use released buffers
589 */
590
591 size = (max_sb_w * max_sb_h + 2) * 576;
592 for (i = 0; i < 2; i++) {
593 if (instance->mv[i].va)
594 mtk_vcodec_mem_free(ctx, &instance->mv[i]);
595 instance->mv[i].size = size;
596 if (mtk_vcodec_mem_alloc(ctx, &instance->mv[i]))
597 goto err;
598 }
599
600 size = (max_sb_w * max_sb_h * 32) + 256;
601 for (i = 0; i < 2; i++) {
602 if (instance->seg[i].va)
603 mtk_vcodec_mem_free(ctx, &instance->seg[i]);
604 instance->seg[i].size = size;
605 if (mtk_vcodec_mem_alloc(ctx, &instance->seg[i]))
606 goto err;
607 }
608
609 if (!instance->tile.va) {
610 instance->tile.size = VP9_TILE_BUF_SIZE;
611 if (mtk_vcodec_mem_alloc(ctx, &instance->tile))
612 goto err;
613 }
614
615 if (!instance->prob.va) {
616 instance->prob.size = VP9_PROB_BUF_SIZE;
617 if (mtk_vcodec_mem_alloc(ctx, &instance->prob))
618 goto err;
619 }
620
621 if (!instance->counts.va) {
622 instance->counts.size = VP9_COUNTS_BUF_SIZE;
623 if (mtk_vcodec_mem_alloc(ctx, &instance->counts))
624 goto err;
625 }
626
627 instance->level = level;
628 return 0;
629
630 err:
631 instance->level = VP9_RES_NONE;
632 return ret;
633 }
634
vdec_vp9_slice_free_working_buffer(struct vdec_vp9_slice_instance * instance)635 static void vdec_vp9_slice_free_working_buffer(struct vdec_vp9_slice_instance *instance)
636 {
637 struct mtk_vcodec_dec_ctx *ctx = instance->ctx;
638 int i;
639
640 for (i = 0; i < ARRAY_SIZE(instance->mv); i++) {
641 if (instance->mv[i].va)
642 mtk_vcodec_mem_free(ctx, &instance->mv[i]);
643 }
644 for (i = 0; i < ARRAY_SIZE(instance->seg); i++) {
645 if (instance->seg[i].va)
646 mtk_vcodec_mem_free(ctx, &instance->seg[i]);
647 }
648 if (instance->tile.va)
649 mtk_vcodec_mem_free(ctx, &instance->tile);
650 if (instance->prob.va)
651 mtk_vcodec_mem_free(ctx, &instance->prob);
652 if (instance->counts.va)
653 mtk_vcodec_mem_free(ctx, &instance->counts);
654
655 instance->level = VP9_RES_NONE;
656 }
657
vdec_vp9_slice_vsi_from_remote(struct vdec_vp9_slice_vsi * vsi,struct vdec_vp9_slice_vsi * remote_vsi,int skip)658 static void vdec_vp9_slice_vsi_from_remote(struct vdec_vp9_slice_vsi *vsi,
659 struct vdec_vp9_slice_vsi *remote_vsi,
660 int skip)
661 {
662 struct vdec_vp9_slice_frame *rf;
663 struct vdec_vp9_slice_frame *f;
664
665 /*
666 * compressed header
667 * dequant
668 * buffer position
669 * decode state
670 */
671 if (!skip) {
672 rf = &remote_vsi->frame;
673 f = &vsi->frame;
674 memcpy(&f->ch, &rf->ch, sizeof(f->ch));
675 memcpy(&f->uh.dequant, &rf->uh.dequant, sizeof(f->uh.dequant));
676 memcpy(&vsi->trans, &remote_vsi->trans, sizeof(vsi->trans));
677 }
678
679 memcpy(&vsi->state, &remote_vsi->state, sizeof(vsi->state));
680 }
681
vdec_vp9_slice_vsi_to_remote(struct vdec_vp9_slice_vsi * vsi,struct vdec_vp9_slice_vsi * remote_vsi)682 static void vdec_vp9_slice_vsi_to_remote(struct vdec_vp9_slice_vsi *vsi,
683 struct vdec_vp9_slice_vsi *remote_vsi)
684 {
685 memcpy(remote_vsi, vsi, sizeof(*vsi));
686 }
687
vdec_vp9_slice_tile_offset(int idx,int mi_num,int tile_log2)688 static int vdec_vp9_slice_tile_offset(int idx, int mi_num, int tile_log2)
689 {
690 int sbs = (mi_num + 7) >> 3;
691 int offset = ((idx * sbs) >> tile_log2) << 3;
692
693 return min(offset, mi_num);
694 }
695
696 static
vdec_vp9_slice_setup_single_from_src_to_dst(struct vdec_vp9_slice_instance * instance)697 int vdec_vp9_slice_setup_single_from_src_to_dst(struct vdec_vp9_slice_instance *instance)
698 {
699 struct vb2_v4l2_buffer *src;
700 struct vb2_v4l2_buffer *dst;
701
702 src = v4l2_m2m_next_src_buf(instance->ctx->m2m_ctx);
703 if (!src)
704 return -EINVAL;
705
706 dst = v4l2_m2m_next_dst_buf(instance->ctx->m2m_ctx);
707 if (!dst)
708 return -EINVAL;
709
710 v4l2_m2m_buf_copy_metadata(src, dst, true);
711
712 return 0;
713 }
714
vdec_vp9_slice_setup_lat_from_src_buf(struct vdec_vp9_slice_instance * instance,struct vdec_lat_buf * lat_buf)715 static int vdec_vp9_slice_setup_lat_from_src_buf(struct vdec_vp9_slice_instance *instance,
716 struct vdec_lat_buf *lat_buf)
717 {
718 struct vb2_v4l2_buffer *src;
719 struct vb2_v4l2_buffer *dst;
720
721 src = v4l2_m2m_next_src_buf(instance->ctx->m2m_ctx);
722 if (!src)
723 return -EINVAL;
724
725 lat_buf->src_buf_req = src->vb2_buf.req_obj.req;
726
727 dst = &lat_buf->ts_info;
728 v4l2_m2m_buf_copy_metadata(src, dst, true);
729 return 0;
730 }
731
vdec_vp9_slice_setup_hdr(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_uncompressed_header * uh,struct v4l2_ctrl_vp9_frame * hdr)732 static void vdec_vp9_slice_setup_hdr(struct vdec_vp9_slice_instance *instance,
733 struct vdec_vp9_slice_uncompressed_header *uh,
734 struct v4l2_ctrl_vp9_frame *hdr)
735 {
736 int i;
737
738 uh->profile = hdr->profile;
739 uh->last_frame_type = instance->frame_type;
740 uh->frame_type = !HDR_FLAG(KEY_FRAME);
741 uh->last_show_frame = instance->show_frame;
742 uh->show_frame = HDR_FLAG(SHOW_FRAME);
743 uh->error_resilient_mode = HDR_FLAG(ERROR_RESILIENT);
744 uh->bit_depth = hdr->bit_depth;
745 uh->last_frame_width = instance->width;
746 uh->last_frame_height = instance->height;
747 uh->frame_width = hdr->frame_width_minus_1 + 1;
748 uh->frame_height = hdr->frame_height_minus_1 + 1;
749 uh->intra_only = HDR_FLAG(INTRA_ONLY);
750 /* map v4l2 enum to values defined in VP9 spec for firmware */
751 switch (hdr->reset_frame_context) {
752 case V4L2_VP9_RESET_FRAME_CTX_NONE:
753 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_NONE0;
754 break;
755 case V4L2_VP9_RESET_FRAME_CTX_SPEC:
756 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_SPEC;
757 break;
758 case V4L2_VP9_RESET_FRAME_CTX_ALL:
759 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_ALL;
760 break;
761 default:
762 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_NONE0;
763 break;
764 }
765 /*
766 * ref_frame_sign_bias specifies the intended direction
767 * of the motion vector in time for each reference frame.
768 * - INTRA_FRAME = 0,
769 * - LAST_FRAME = 1,
770 * - GOLDEN_FRAME = 2,
771 * - ALTREF_FRAME = 3,
772 * ref_frame_sign_bias[INTRA_FRAME] is always 0
773 * and VDA only passes another 3 directions
774 */
775 uh->ref_frame_sign_bias[0] = 0;
776 for (i = 0; i < 3; i++)
777 uh->ref_frame_sign_bias[i + 1] =
778 !!(hdr->ref_frame_sign_bias & (1 << i));
779 uh->allow_high_precision_mv = HDR_FLAG(ALLOW_HIGH_PREC_MV);
780 uh->interpolation_filter = hdr->interpolation_filter;
781 uh->refresh_frame_context = HDR_FLAG(REFRESH_FRAME_CTX);
782 uh->frame_parallel_decoding_mode = HDR_FLAG(PARALLEL_DEC_MODE);
783 uh->frame_context_idx = hdr->frame_context_idx;
784
785 /* tile info */
786 uh->tile_cols_log2 = hdr->tile_cols_log2;
787 uh->tile_rows_log2 = hdr->tile_rows_log2;
788
789 uh->uncompressed_header_size = hdr->uncompressed_header_size;
790 uh->header_size_in_bytes = hdr->compressed_header_size;
791 }
792
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)793 static void vdec_vp9_slice_setup_frame_ctx(struct vdec_vp9_slice_instance *instance,
794 struct vdec_vp9_slice_uncompressed_header *uh,
795 struct v4l2_ctrl_vp9_frame *hdr)
796 {
797 int error_resilient_mode;
798 int reset_frame_context;
799 int key_frame;
800 int intra_only;
801 int i;
802
803 key_frame = HDR_FLAG(KEY_FRAME);
804 intra_only = HDR_FLAG(INTRA_ONLY);
805 error_resilient_mode = HDR_FLAG(ERROR_RESILIENT);
806 reset_frame_context = uh->reset_frame_context;
807
808 /*
809 * according to "6.2 Uncompressed header syntax" in
810 * "VP9 Bitstream & Decoding Process Specification",
811 * reset @frame_context_idx when (FrameIsIntra || error_resilient_mode)
812 */
813 if (key_frame || intra_only || error_resilient_mode) {
814 /*
815 * @reset_frame_context specifies
816 * whether the frame context should be
817 * reset to default values:
818 * 0 or 1 means do not reset any frame context
819 * 2 resets just the context specified in the frame header
820 * 3 resets all contexts
821 */
822 if (key_frame || error_resilient_mode ||
823 reset_frame_context == 3) {
824 /* use default table */
825 for (i = 0; i < 4; i++)
826 instance->dirty[i] = 0;
827 } else if (reset_frame_context == 2) {
828 instance->dirty[uh->frame_context_idx] = 0;
829 }
830 uh->frame_context_idx = 0;
831 }
832 }
833
vdec_vp9_slice_setup_loop_filter(struct vdec_vp9_slice_uncompressed_header * uh,struct v4l2_vp9_loop_filter * lf)834 static void vdec_vp9_slice_setup_loop_filter(struct vdec_vp9_slice_uncompressed_header *uh,
835 struct v4l2_vp9_loop_filter *lf)
836 {
837 int i;
838
839 uh->loop_filter_level = lf->level;
840 uh->loop_filter_sharpness = lf->sharpness;
841 uh->loop_filter_delta_enabled = LF_FLAG(DELTA_ENABLED);
842 for (i = 0; i < 4; i++)
843 uh->loop_filter_ref_deltas[i] = lf->ref_deltas[i];
844 for (i = 0; i < 2; i++)
845 uh->loop_filter_mode_deltas[i] = lf->mode_deltas[i];
846 }
847
vdec_vp9_slice_setup_quantization(struct vdec_vp9_slice_uncompressed_header * uh,struct v4l2_vp9_quantization * quant)848 static void vdec_vp9_slice_setup_quantization(struct vdec_vp9_slice_uncompressed_header *uh,
849 struct v4l2_vp9_quantization *quant)
850 {
851 uh->base_q_idx = quant->base_q_idx;
852 uh->delta_q_y_dc = quant->delta_q_y_dc;
853 uh->delta_q_uv_dc = quant->delta_q_uv_dc;
854 uh->delta_q_uv_ac = quant->delta_q_uv_ac;
855 }
856
vdec_vp9_slice_setup_segmentation(struct vdec_vp9_slice_uncompressed_header * uh,struct v4l2_vp9_segmentation * seg)857 static void vdec_vp9_slice_setup_segmentation(struct vdec_vp9_slice_uncompressed_header *uh,
858 struct v4l2_vp9_segmentation *seg)
859 {
860 int i;
861 int j;
862
863 uh->segmentation_enabled = SEG_FLAG(ENABLED);
864 uh->segmentation_update_map = SEG_FLAG(UPDATE_MAP);
865 for (i = 0; i < 7; i++)
866 uh->segmentation_tree_probs[i] = seg->tree_probs[i];
867 uh->segmentation_temporal_udpate = SEG_FLAG(TEMPORAL_UPDATE);
868 for (i = 0; i < 3; i++)
869 uh->segmentation_pred_prob[i] = seg->pred_probs[i];
870 uh->segmentation_update_data = SEG_FLAG(UPDATE_DATA);
871 uh->segmentation_abs_or_delta_update = SEG_FLAG(ABS_OR_DELTA_UPDATE);
872 for (i = 0; i < 8; i++) {
873 uh->feature_enabled[i] = seg->feature_enabled[i];
874 for (j = 0; j < 4; j++)
875 uh->feature_value[i][j] = seg->feature_data[i][j];
876 }
877 }
878
vdec_vp9_slice_setup_tile(struct vdec_vp9_slice_vsi * vsi,struct v4l2_ctrl_vp9_frame * hdr)879 static int vdec_vp9_slice_setup_tile(struct vdec_vp9_slice_vsi *vsi,
880 struct v4l2_ctrl_vp9_frame *hdr)
881 {
882 unsigned int rows_log2;
883 unsigned int cols_log2;
884 unsigned int rows;
885 unsigned int cols;
886 unsigned int mi_rows;
887 unsigned int mi_cols;
888 struct vdec_vp9_slice_tiles *tiles;
889 int offset;
890 int start;
891 int end;
892 int i;
893
894 rows_log2 = hdr->tile_rows_log2;
895 cols_log2 = hdr->tile_cols_log2;
896 rows = 1 << rows_log2;
897 cols = 1 << cols_log2;
898 tiles = &vsi->frame.tiles;
899 tiles->actual_rows = 0;
900
901 if (rows > 4 || cols > 64)
902 return -EINVAL;
903
904 /* setup mi rows/cols information */
905 mi_rows = (hdr->frame_height_minus_1 + 1 + 7) >> 3;
906 mi_cols = (hdr->frame_width_minus_1 + 1 + 7) >> 3;
907
908 for (i = 0; i < rows; i++) {
909 start = vdec_vp9_slice_tile_offset(i, mi_rows, rows_log2);
910 end = vdec_vp9_slice_tile_offset(i + 1, mi_rows, rows_log2);
911 offset = end - start;
912 tiles->mi_rows[i] = (offset + 7) >> 3;
913 if (tiles->mi_rows[i])
914 tiles->actual_rows++;
915 }
916
917 for (i = 0; i < cols; i++) {
918 start = vdec_vp9_slice_tile_offset(i, mi_cols, cols_log2);
919 end = vdec_vp9_slice_tile_offset(i + 1, mi_cols, cols_log2);
920 offset = end - start;
921 tiles->mi_cols[i] = (offset + 7) >> 3;
922 }
923
924 return 0;
925 }
926
vdec_vp9_slice_setup_state(struct vdec_vp9_slice_vsi * vsi)927 static void vdec_vp9_slice_setup_state(struct vdec_vp9_slice_vsi *vsi)
928 {
929 memset(&vsi->state, 0, sizeof(vsi->state));
930 }
931
vdec_vp9_slice_setup_ref_idx(struct vdec_vp9_slice_pfc * pfc,struct v4l2_ctrl_vp9_frame * hdr)932 static void vdec_vp9_slice_setup_ref_idx(struct vdec_vp9_slice_pfc *pfc,
933 struct v4l2_ctrl_vp9_frame *hdr)
934 {
935 pfc->ref_idx[0] = hdr->last_frame_ts;
936 pfc->ref_idx[1] = hdr->golden_frame_ts;
937 pfc->ref_idx[2] = hdr->alt_frame_ts;
938 }
939
vdec_vp9_slice_setup_pfc(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_pfc * pfc)940 static int vdec_vp9_slice_setup_pfc(struct vdec_vp9_slice_instance *instance,
941 struct vdec_vp9_slice_pfc *pfc)
942 {
943 struct v4l2_ctrl_vp9_frame *hdr;
944 struct vdec_vp9_slice_uncompressed_header *uh;
945 struct v4l2_ctrl *hdr_ctrl;
946 struct vdec_vp9_slice_vsi *vsi;
947 int ret;
948
949 /* frame header */
950 hdr_ctrl = v4l2_ctrl_find(&instance->ctx->ctrl_hdl, V4L2_CID_STATELESS_VP9_FRAME);
951 if (!hdr_ctrl || !hdr_ctrl->p_cur.p)
952 return -EINVAL;
953
954 hdr = hdr_ctrl->p_cur.p;
955 vsi = &pfc->vsi;
956 uh = &vsi->frame.uh;
957
958 /* setup vsi information */
959 vdec_vp9_slice_setup_hdr(instance, uh, hdr);
960 vdec_vp9_slice_setup_frame_ctx(instance, uh, hdr);
961 vdec_vp9_slice_setup_loop_filter(uh, &hdr->lf);
962 vdec_vp9_slice_setup_quantization(uh, &hdr->quant);
963 vdec_vp9_slice_setup_segmentation(uh, &hdr->seg);
964 ret = vdec_vp9_slice_setup_tile(vsi, hdr);
965 if (ret)
966 return ret;
967 vdec_vp9_slice_setup_state(vsi);
968
969 /* core stage needs buffer index to get ref y/c ... */
970 vdec_vp9_slice_setup_ref_idx(pfc, hdr);
971
972 pfc->seq = instance->seq;
973 instance->seq++;
974
975 return 0;
976 }
977
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)978 static int vdec_vp9_slice_setup_lat_buffer(struct vdec_vp9_slice_instance *instance,
979 struct vdec_vp9_slice_vsi *vsi,
980 struct mtk_vcodec_mem *bs,
981 struct vdec_lat_buf *lat_buf)
982 {
983 int i;
984
985 vsi->bs.buf.dma_addr = bs->dma_addr;
986 vsi->bs.buf.size = bs->size;
987 vsi->bs.frame.dma_addr = bs->dma_addr;
988 vsi->bs.frame.size = bs->size;
989
990 for (i = 0; i < 2; i++) {
991 vsi->mv[i].dma_addr = instance->mv[i].dma_addr;
992 vsi->mv[i].size = instance->mv[i].size;
993 }
994 for (i = 0; i < 2; i++) {
995 vsi->seg[i].dma_addr = instance->seg[i].dma_addr;
996 vsi->seg[i].size = instance->seg[i].size;
997 }
998 vsi->tile.dma_addr = instance->tile.dma_addr;
999 vsi->tile.size = instance->tile.size;
1000 vsi->prob.dma_addr = instance->prob.dma_addr;
1001 vsi->prob.size = instance->prob.size;
1002 vsi->counts.dma_addr = instance->counts.dma_addr;
1003 vsi->counts.size = instance->counts.size;
1004
1005 vsi->ube.dma_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
1006 vsi->ube.size = lat_buf->ctx->msg_queue.wdma_addr.size;
1007 vsi->trans.dma_addr = lat_buf->ctx->msg_queue.wdma_wptr_addr;
1008 /* used to store trans end */
1009 vsi->trans.dma_addr_end = lat_buf->ctx->msg_queue.wdma_rptr_addr;
1010 vsi->err_map.dma_addr = lat_buf->wdma_err_addr.dma_addr;
1011 vsi->err_map.size = lat_buf->wdma_err_addr.size;
1012
1013 vsi->row_info.buf = 0;
1014 vsi->row_info.size = 0;
1015
1016 return 0;
1017 }
1018
vdec_vp9_slice_setup_prob_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_vsi * vsi)1019 static int vdec_vp9_slice_setup_prob_buffer(struct vdec_vp9_slice_instance *instance,
1020 struct vdec_vp9_slice_vsi *vsi)
1021 {
1022 struct vdec_vp9_slice_frame_ctx *frame_ctx;
1023 struct vdec_vp9_slice_uncompressed_header *uh;
1024
1025 uh = &vsi->frame.uh;
1026
1027 mtk_vdec_debug(instance->ctx, "ctx dirty %u idx %d\n",
1028 instance->dirty[uh->frame_context_idx],
1029 uh->frame_context_idx);
1030
1031 if (instance->dirty[uh->frame_context_idx])
1032 frame_ctx = &instance->frame_ctx[uh->frame_context_idx];
1033 else
1034 frame_ctx = vdec_vp9_slice_default_frame_ctx;
1035 memcpy(instance->prob.va, frame_ctx, sizeof(*frame_ctx));
1036
1037 return 0;
1038 }
1039
vdec_vp9_slice_setup_seg_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_vsi * vsi,struct mtk_vcodec_mem * buf)1040 static void vdec_vp9_slice_setup_seg_buffer(struct vdec_vp9_slice_instance *instance,
1041 struct vdec_vp9_slice_vsi *vsi,
1042 struct mtk_vcodec_mem *buf)
1043 {
1044 struct vdec_vp9_slice_uncompressed_header *uh;
1045
1046 /* reset segment buffer */
1047 uh = &vsi->frame.uh;
1048 if (uh->frame_type == 0 ||
1049 uh->intra_only ||
1050 uh->error_resilient_mode ||
1051 uh->frame_width != instance->width ||
1052 uh->frame_height != instance->height) {
1053 mtk_vdec_debug(instance->ctx, "reset seg\n");
1054 memset(buf->va, 0, buf->size);
1055 }
1056 }
1057
1058 /*
1059 * parse tiles according to `6.4 Decode tiles syntax`
1060 * in "vp9-bitstream-specification"
1061 *
1062 * frame contains uncompress header, compressed header and several tiles.
1063 * this function parses tiles' position and size, stores them to tile buffer
1064 * for decoding.
1065 */
vdec_vp9_slice_setup_tile_buffer(struct vdec_vp9_slice_instance * instance,struct vdec_vp9_slice_vsi * vsi,struct mtk_vcodec_mem * bs)1066 static int vdec_vp9_slice_setup_tile_buffer(struct vdec_vp9_slice_instance *instance,
1067 struct vdec_vp9_slice_vsi *vsi,
1068 struct mtk_vcodec_mem *bs)
1069 {
1070 struct vdec_vp9_slice_uncompressed_header *uh;
1071 unsigned int rows_log2;
1072 unsigned int cols_log2;
1073 unsigned int rows;
1074 unsigned int cols;
1075 unsigned int mi_row;
1076 unsigned int mi_col;
1077 unsigned int offset;
1078 unsigned int pa;
1079 unsigned int size;
1080 struct vdec_vp9_slice_tiles *tiles;
1081 unsigned char *pos;
1082 unsigned char *end;
1083 unsigned char *va;
1084 unsigned int *tb;
1085 int i;
1086 int j;
1087
1088 uh = &vsi->frame.uh;
1089 rows_log2 = uh->tile_rows_log2;
1090 cols_log2 = uh->tile_cols_log2;
1091 rows = 1 << rows_log2;
1092 cols = 1 << cols_log2;
1093
1094 if (rows > 4 || cols > 64) {
1095 mtk_vdec_err(instance->ctx, "tile_rows %u tile_cols %u\n", rows, cols);
1096 return -EINVAL;
1097 }
1098
1099 offset = uh->uncompressed_header_size +
1100 uh->header_size_in_bytes;
1101 if (bs->size <= offset) {
1102 mtk_vdec_err(instance->ctx, "bs size %zu tile offset %u\n", bs->size, offset);
1103 return -EINVAL;
1104 }
1105
1106 tiles = &vsi->frame.tiles;
1107 /* setup tile buffer */
1108
1109 va = (unsigned char *)bs->va;
1110 pos = va + offset;
1111 end = va + bs->size;
1112 /* truncated */
1113 pa = (unsigned int)bs->dma_addr + offset;
1114 tb = instance->tile.va;
1115 for (i = 0; i < rows; i++) {
1116 for (j = 0; j < cols; j++) {
1117 if (i == rows - 1 &&
1118 j == cols - 1) {
1119 size = (unsigned int)(end - pos);
1120 } else {
1121 if (end - pos < 4)
1122 return -EINVAL;
1123
1124 size = (pos[0] << 24) | (pos[1] << 16) |
1125 (pos[2] << 8) | pos[3];
1126 pos += 4;
1127 pa += 4;
1128 offset += 4;
1129 if (end - pos < size)
1130 return -EINVAL;
1131 }
1132 tiles->size[i][j] = size;
1133 if (tiles->mi_rows[i]) {
1134 *tb++ = (size << 3) + ((offset << 3) & 0x7f);
1135 *tb++ = pa & ~0xf;
1136 *tb++ = (pa << 3) & 0x7f;
1137 mi_row = (tiles->mi_rows[i] - 1) & 0x1ff;
1138 mi_col = (tiles->mi_cols[j] - 1) & 0x3f;
1139 *tb++ = (mi_row << 6) + mi_col;
1140 }
1141 pos += size;
1142 pa += size;
1143 offset += size;
1144 }
1145 }
1146
1147 return 0;
1148 }
1149
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)1150 static int vdec_vp9_slice_setup_lat(struct vdec_vp9_slice_instance *instance,
1151 struct mtk_vcodec_mem *bs,
1152 struct vdec_lat_buf *lat_buf,
1153 struct vdec_vp9_slice_pfc *pfc)
1154 {
1155 struct vdec_vp9_slice_vsi *vsi = &pfc->vsi;
1156 int ret;
1157
1158 ret = vdec_vp9_slice_setup_lat_from_src_buf(instance, lat_buf);
1159 if (ret)
1160 goto err;
1161
1162 ret = vdec_vp9_slice_setup_pfc(instance, pfc);
1163 if (ret)
1164 goto err;
1165
1166 ret = vdec_vp9_slice_alloc_working_buffer(instance, vsi);
1167 if (ret)
1168 goto err;
1169
1170 ret = vdec_vp9_slice_setup_lat_buffer(instance, vsi, bs, lat_buf);
1171 if (ret)
1172 goto err;
1173
1174 vdec_vp9_slice_setup_seg_buffer(instance, vsi, &instance->seg[0]);
1175
1176 /* setup prob/tile buffers for LAT */
1177
1178 ret = vdec_vp9_slice_setup_prob_buffer(instance, vsi);
1179 if (ret)
1180 goto err;
1181
1182 ret = vdec_vp9_slice_setup_tile_buffer(instance, vsi, bs);
1183 if (ret)
1184 goto err;
1185
1186 return 0;
1187
1188 err:
1189 return ret;
1190 }
1191
1192 static
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 for (i = 0; i < vq->num_buffers; i++) {
1699 if (vb == vq->bufs[i]) {
1700 instance->dpb[i].width = w;
1701 instance->dpb[i].height = h;
1702 break;
1703 }
1704 }
1705
1706 /*
1707 * get buffer's width/height from instance
1708 * get buffer address from vb2buf
1709 */
1710 for (i = 0; i < 3; i++) {
1711 ref = &vsi->frame.ref[i];
1712 vb = vb2_find_buffer(vq, pfc->ref_idx[i]);
1713 if (!vb) {
1714 ref->frame_width = w;
1715 ref->frame_height = h;
1716 memset(&vsi->ref[i], 0, sizeof(vsi->ref[i]));
1717 } else {
1718 int idx = vb->index;
1719
1720 ref->frame_width = instance->dpb[idx].width;
1721 ref->frame_height = instance->dpb[idx].height;
1722 vsi->ref[i].y.dma_addr =
1723 vb2_dma_contig_plane_dma_addr(vb, 0);
1724 if (plane == 1)
1725 vsi->ref[i].c.dma_addr =
1726 vsi->ref[i].y.dma_addr + size;
1727 else
1728 vsi->ref[i].c.dma_addr =
1729 vb2_dma_contig_plane_dma_addr(vb, 1);
1730 }
1731 }
1732
1733 return 0;
1734 }
1735
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)1736 static void vdec_vp9_slice_setup_single_buffer(struct vdec_vp9_slice_instance *instance,
1737 struct vdec_vp9_slice_pfc *pfc,
1738 struct vdec_vp9_slice_vsi *vsi,
1739 struct mtk_vcodec_mem *bs,
1740 struct vdec_fb *fb)
1741 {
1742 int i;
1743
1744 vsi->bs.buf.dma_addr = bs->dma_addr;
1745 vsi->bs.buf.size = bs->size;
1746 vsi->bs.frame.dma_addr = bs->dma_addr;
1747 vsi->bs.frame.size = bs->size;
1748
1749 for (i = 0; i < 2; i++) {
1750 vsi->mv[i].dma_addr = instance->mv[i].dma_addr;
1751 vsi->mv[i].size = instance->mv[i].size;
1752 }
1753 for (i = 0; i < 2; i++) {
1754 vsi->seg[i].dma_addr = instance->seg[i].dma_addr;
1755 vsi->seg[i].size = instance->seg[i].size;
1756 }
1757 vsi->tile.dma_addr = instance->tile.dma_addr;
1758 vsi->tile.size = instance->tile.size;
1759 vsi->prob.dma_addr = instance->prob.dma_addr;
1760 vsi->prob.size = instance->prob.size;
1761 vsi->counts.dma_addr = instance->counts.dma_addr;
1762 vsi->counts.size = instance->counts.size;
1763
1764 vsi->row_info.buf = 0;
1765 vsi->row_info.size = 0;
1766
1767 vdec_vp9_slice_setup_core_buffer(instance, pfc, vsi, fb, NULL);
1768 }
1769
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)1770 static int vdec_vp9_slice_setup_core(struct vdec_vp9_slice_instance *instance,
1771 struct vdec_fb *fb,
1772 struct vdec_lat_buf *lat_buf,
1773 struct vdec_vp9_slice_pfc *pfc)
1774 {
1775 struct vdec_vp9_slice_vsi *vsi = &pfc->vsi;
1776 int ret;
1777
1778 vdec_vp9_slice_setup_state(vsi);
1779
1780 ret = vdec_vp9_slice_setup_core_to_dst_buf(instance, lat_buf);
1781 if (ret)
1782 goto err;
1783
1784 ret = vdec_vp9_slice_setup_core_buffer(instance, pfc, vsi, fb, lat_buf);
1785 if (ret)
1786 goto err;
1787
1788 vdec_vp9_slice_setup_seg_buffer(instance, vsi, &instance->seg[1]);
1789
1790 return 0;
1791
1792 err:
1793 return ret;
1794 }
1795
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)1796 static int vdec_vp9_slice_setup_single(struct vdec_vp9_slice_instance *instance,
1797 struct mtk_vcodec_mem *bs,
1798 struct vdec_fb *fb,
1799 struct vdec_vp9_slice_pfc *pfc)
1800 {
1801 struct vdec_vp9_slice_vsi *vsi = &pfc->vsi;
1802 int ret;
1803
1804 ret = vdec_vp9_slice_setup_single_from_src_to_dst(instance);
1805 if (ret)
1806 goto err;
1807
1808 ret = vdec_vp9_slice_setup_pfc(instance, pfc);
1809 if (ret)
1810 goto err;
1811
1812 ret = vdec_vp9_slice_alloc_working_buffer(instance, vsi);
1813 if (ret)
1814 goto err;
1815
1816 vdec_vp9_slice_setup_single_buffer(instance, pfc, vsi, bs, fb);
1817 vdec_vp9_slice_setup_seg_buffer(instance, vsi, &instance->seg[0]);
1818
1819 ret = vdec_vp9_slice_setup_prob_buffer(instance, vsi);
1820 if (ret)
1821 goto err;
1822
1823 ret = vdec_vp9_slice_setup_tile_buffer(instance, vsi, bs);
1824 if (ret)
1825 goto err;
1826
1827 return 0;
1828
1829 err:
1830 return ret;
1831 }
1832
vdec_vp9_slice_update_core(struct vdec_vp9_slice_instance * instance,struct vdec_lat_buf * lat_buf,struct vdec_vp9_slice_pfc * pfc)1833 static int vdec_vp9_slice_update_core(struct vdec_vp9_slice_instance *instance,
1834 struct vdec_lat_buf *lat_buf,
1835 struct vdec_vp9_slice_pfc *pfc)
1836 {
1837 struct vdec_vp9_slice_vsi *vsi;
1838
1839 vsi = &pfc->vsi;
1840 memcpy(&pfc->state[1], &vsi->state, sizeof(vsi->state));
1841
1842 mtk_vdec_debug(instance->ctx, "Frame %u Y_CRC %08x %08x %08x %08x\n",
1843 pfc->seq, vsi->state.crc[0], vsi->state.crc[1],
1844 vsi->state.crc[2], vsi->state.crc[3]);
1845 mtk_vdec_debug(instance->ctx, "Frame %u C_CRC %08x %08x %08x %08x\n",
1846 pfc->seq, vsi->state.crc[4], vsi->state.crc[5],
1847 vsi->state.crc[6], vsi->state.crc[7]);
1848
1849 return 0;
1850 }
1851
vdec_vp9_slice_init(struct mtk_vcodec_dec_ctx * ctx)1852 static int vdec_vp9_slice_init(struct mtk_vcodec_dec_ctx *ctx)
1853 {
1854 struct vdec_vp9_slice_instance *instance;
1855 struct vdec_vp9_slice_init_vsi *vsi;
1856 int ret;
1857
1858 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
1859 if (!instance)
1860 return -ENOMEM;
1861
1862 instance->ctx = ctx;
1863 instance->vpu.id = SCP_IPI_VDEC_LAT;
1864 instance->vpu.core_id = SCP_IPI_VDEC_CORE;
1865 instance->vpu.ctx = ctx;
1866 instance->vpu.codec_type = ctx->current_codec;
1867
1868 ret = vpu_dec_init(&instance->vpu);
1869 if (ret) {
1870 mtk_vdec_err(ctx, "failed to init vpu dec, ret %d\n", ret);
1871 goto error_vpu_init;
1872 }
1873
1874 /* init vsi and global flags */
1875
1876 vsi = instance->vpu.vsi;
1877 if (!vsi) {
1878 mtk_vdec_err(ctx, "failed to get VP9 vsi\n");
1879 ret = -EINVAL;
1880 goto error_vsi;
1881 }
1882 instance->init_vsi = vsi;
1883 instance->core_vsi = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
1884 (u32)vsi->core_vsi);
1885 if (!instance->core_vsi) {
1886 mtk_vdec_err(ctx, "failed to get VP9 core vsi\n");
1887 ret = -EINVAL;
1888 goto error_vsi;
1889 }
1890
1891 instance->irq = 1;
1892
1893 ret = vdec_vp9_slice_init_default_frame_ctx(instance);
1894 if (ret)
1895 goto error_default_frame_ctx;
1896
1897 ctx->drv_handle = instance;
1898
1899 return 0;
1900
1901 error_default_frame_ctx:
1902 error_vsi:
1903 vpu_dec_deinit(&instance->vpu);
1904 error_vpu_init:
1905 kfree(instance);
1906 return ret;
1907 }
1908
vdec_vp9_slice_deinit(void * h_vdec)1909 static void vdec_vp9_slice_deinit(void *h_vdec)
1910 {
1911 struct vdec_vp9_slice_instance *instance = h_vdec;
1912
1913 if (!instance)
1914 return;
1915
1916 vpu_dec_deinit(&instance->vpu);
1917 vdec_vp9_slice_free_working_buffer(instance);
1918 vdec_msg_queue_deinit(&instance->ctx->msg_queue, instance->ctx);
1919 kfree(instance);
1920 }
1921
vdec_vp9_slice_flush(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)1922 static int vdec_vp9_slice_flush(void *h_vdec, struct mtk_vcodec_mem *bs,
1923 struct vdec_fb *fb, bool *res_chg)
1924 {
1925 struct vdec_vp9_slice_instance *instance = h_vdec;
1926
1927 mtk_vdec_debug(instance->ctx, "flush ...\n");
1928 if (instance->ctx->dev->vdec_pdata->hw_arch != MTK_VDEC_PURE_SINGLE_CORE)
1929 vdec_msg_queue_wait_lat_buf_full(&instance->ctx->msg_queue);
1930 return vpu_dec_reset(&instance->vpu);
1931 }
1932
vdec_vp9_slice_get_pic_info(struct vdec_vp9_slice_instance * instance)1933 static void vdec_vp9_slice_get_pic_info(struct vdec_vp9_slice_instance *instance)
1934 {
1935 struct mtk_vcodec_dec_ctx *ctx = instance->ctx;
1936 unsigned int data[3];
1937
1938 mtk_vdec_debug(instance->ctx, "w %u h %u\n", ctx->picinfo.pic_w, ctx->picinfo.pic_h);
1939
1940 data[0] = ctx->picinfo.pic_w;
1941 data[1] = ctx->picinfo.pic_h;
1942 data[2] = ctx->capture_fourcc;
1943 vpu_dec_get_param(&instance->vpu, data, 3, GET_PARAM_PIC_INFO);
1944
1945 ctx->picinfo.buf_w = ALIGN(ctx->picinfo.pic_w, 64);
1946 ctx->picinfo.buf_h = ALIGN(ctx->picinfo.pic_h, 64);
1947 ctx->picinfo.fb_sz[0] = instance->vpu.fb_sz[0];
1948 ctx->picinfo.fb_sz[1] = instance->vpu.fb_sz[1];
1949 }
1950
vdec_vp9_slice_get_dpb_size(struct vdec_vp9_slice_instance * instance,unsigned int * dpb_sz)1951 static void vdec_vp9_slice_get_dpb_size(struct vdec_vp9_slice_instance *instance,
1952 unsigned int *dpb_sz)
1953 {
1954 /* refer VP9 specification */
1955 *dpb_sz = 9;
1956 }
1957
vdec_vp9_slice_get_param(void * h_vdec,enum vdec_get_param_type type,void * out)1958 static int vdec_vp9_slice_get_param(void *h_vdec, enum vdec_get_param_type type, void *out)
1959 {
1960 struct vdec_vp9_slice_instance *instance = h_vdec;
1961
1962 switch (type) {
1963 case GET_PARAM_PIC_INFO:
1964 vdec_vp9_slice_get_pic_info(instance);
1965 break;
1966 case GET_PARAM_DPB_SIZE:
1967 vdec_vp9_slice_get_dpb_size(instance, out);
1968 break;
1969 case GET_PARAM_CROP_INFO:
1970 mtk_vdec_debug(instance->ctx, "No need to get vp9 crop information.");
1971 break;
1972 default:
1973 mtk_vdec_err(instance->ctx, "invalid get parameter type=%d\n", type);
1974 return -EINVAL;
1975 }
1976
1977 return 0;
1978 }
1979
vdec_vp9_slice_single_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)1980 static int vdec_vp9_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
1981 struct vdec_fb *fb, bool *res_chg)
1982 {
1983 struct vdec_vp9_slice_instance *instance = h_vdec;
1984 struct vdec_vp9_slice_pfc *pfc = &instance->sc_pfc;
1985 struct vdec_vp9_slice_vsi *vsi;
1986 struct mtk_vcodec_dec_ctx *ctx;
1987 int ret;
1988
1989 if (!instance || !instance->ctx)
1990 return -EINVAL;
1991 ctx = instance->ctx;
1992
1993 /* bs NULL means flush decoder */
1994 if (!bs)
1995 return vdec_vp9_slice_flush(h_vdec, bs, fb, res_chg);
1996
1997 fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
1998 if (!fb)
1999 return -EBUSY;
2000
2001 vsi = &pfc->vsi;
2002
2003 ret = vdec_vp9_slice_setup_single(instance, bs, fb, pfc);
2004 if (ret) {
2005 mtk_vdec_err(ctx, "Failed to setup VP9 single ret %d\n", ret);
2006 return ret;
2007 }
2008 vdec_vp9_slice_vsi_to_remote(vsi, instance->vsi);
2009
2010 ret = vpu_dec_start(&instance->vpu, NULL, 0);
2011 if (ret) {
2012 mtk_vdec_err(ctx, "Failed to dec VP9 ret %d\n", ret);
2013 return ret;
2014 }
2015
2016 ret = mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
2017 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
2018 /* update remote vsi if decode timeout */
2019 if (ret) {
2020 mtk_vdec_err(ctx, "VP9 decode timeout %d\n", ret);
2021 WRITE_ONCE(instance->vsi->state.timeout, 1);
2022 }
2023
2024 vpu_dec_end(&instance->vpu);
2025
2026 vdec_vp9_slice_vsi_from_remote(vsi, instance->vsi, 0);
2027 ret = vdec_vp9_slice_update_single(instance, pfc);
2028 if (ret) {
2029 mtk_vdec_err(ctx, "VP9 decode error: %d\n", ret);
2030 return ret;
2031 }
2032
2033 instance->ctx->decoded_frame_cnt++;
2034 return 0;
2035 }
2036
vdec_vp9_slice_lat_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)2037 static int vdec_vp9_slice_lat_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
2038 struct vdec_fb *fb, bool *res_chg)
2039 {
2040 struct vdec_vp9_slice_instance *instance = h_vdec;
2041 struct vdec_lat_buf *lat_buf;
2042 struct vdec_vp9_slice_pfc *pfc;
2043 struct vdec_vp9_slice_vsi *vsi;
2044 struct mtk_vcodec_dec_ctx *ctx;
2045 int ret;
2046
2047 if (!instance || !instance->ctx)
2048 return -EINVAL;
2049 ctx = instance->ctx;
2050
2051 /* init msgQ for the first time */
2052 if (vdec_msg_queue_init(&ctx->msg_queue, ctx,
2053 vdec_vp9_slice_core_decode,
2054 sizeof(*pfc)))
2055 return -ENOMEM;
2056
2057 /* bs NULL means flush decoder */
2058 if (!bs)
2059 return vdec_vp9_slice_flush(h_vdec, bs, fb, res_chg);
2060
2061 lat_buf = vdec_msg_queue_dqbuf(&instance->ctx->msg_queue.lat_ctx);
2062 if (!lat_buf) {
2063 mtk_vdec_debug(ctx, "Failed to get VP9 lat buf\n");
2064 return -EAGAIN;
2065 }
2066 pfc = (struct vdec_vp9_slice_pfc *)lat_buf->private_data;
2067 if (!pfc) {
2068 ret = -EINVAL;
2069 goto err_free_fb_out;
2070 }
2071 vsi = &pfc->vsi;
2072
2073 ret = vdec_vp9_slice_setup_lat(instance, bs, lat_buf, pfc);
2074 if (ret) {
2075 mtk_vdec_err(ctx, "Failed to setup VP9 lat ret %d\n", ret);
2076 goto err_free_fb_out;
2077 }
2078 vdec_vp9_slice_vsi_to_remote(vsi, instance->vsi);
2079
2080 ret = vpu_dec_start(&instance->vpu, NULL, 0);
2081 if (ret) {
2082 mtk_vdec_err(ctx, "Failed to dec VP9 ret %d\n", ret);
2083 goto err_free_fb_out;
2084 }
2085
2086 if (instance->irq) {
2087 ret = mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
2088 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_LAT0);
2089 /* update remote vsi if decode timeout */
2090 if (ret) {
2091 mtk_vdec_err(ctx, "VP9 decode timeout %d pic %d\n", ret, pfc->seq);
2092 WRITE_ONCE(instance->vsi->state.timeout, 1);
2093 }
2094 vpu_dec_end(&instance->vpu);
2095 }
2096
2097 vdec_vp9_slice_vsi_from_remote(vsi, instance->vsi, 0);
2098 ret = vdec_vp9_slice_update_lat(instance, lat_buf, pfc);
2099
2100 /* LAT trans full, no more UBE or decode timeout */
2101 if (ret) {
2102 mtk_vdec_err(ctx, "VP9 decode error: %d\n", ret);
2103 goto err_free_fb_out;
2104 }
2105
2106 mtk_vdec_debug(ctx, "lat dma addr: 0x%lx 0x%lx\n",
2107 (unsigned long)pfc->vsi.trans.dma_addr,
2108 (unsigned long)pfc->vsi.trans.dma_addr_end);
2109
2110 vdec_msg_queue_update_ube_wptr(&ctx->msg_queue,
2111 vsi->trans.dma_addr_end +
2112 ctx->msg_queue.wdma_addr.dma_addr);
2113 vdec_msg_queue_qbuf(&ctx->msg_queue.core_ctx, lat_buf);
2114
2115 return 0;
2116 err_free_fb_out:
2117 vdec_msg_queue_qbuf(&ctx->msg_queue.lat_ctx, lat_buf);
2118 return ret;
2119 }
2120
vdec_vp9_slice_decode(void * h_vdec,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)2121 static int vdec_vp9_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
2122 struct vdec_fb *fb, bool *res_chg)
2123 {
2124 struct vdec_vp9_slice_instance *instance = h_vdec;
2125 int ret;
2126
2127 if (instance->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
2128 ret = vdec_vp9_slice_single_decode(h_vdec, bs, fb, res_chg);
2129 else
2130 ret = vdec_vp9_slice_lat_decode(h_vdec, bs, fb, res_chg);
2131
2132 return ret;
2133 }
2134
vdec_vp9_slice_core_decode(struct vdec_lat_buf * lat_buf)2135 static int vdec_vp9_slice_core_decode(struct vdec_lat_buf *lat_buf)
2136 {
2137 struct vdec_vp9_slice_instance *instance;
2138 struct vdec_vp9_slice_pfc *pfc;
2139 struct mtk_vcodec_dec_ctx *ctx = NULL;
2140 struct vdec_fb *fb = NULL;
2141 int ret = -EINVAL;
2142
2143 if (!lat_buf)
2144 goto err;
2145
2146 pfc = lat_buf->private_data;
2147 ctx = lat_buf->ctx;
2148 if (!pfc || !ctx)
2149 goto err;
2150
2151 instance = ctx->drv_handle;
2152 if (!instance)
2153 goto err;
2154
2155 fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
2156 if (!fb) {
2157 ret = -EBUSY;
2158 goto err;
2159 }
2160
2161 ret = vdec_vp9_slice_setup_core(instance, fb, lat_buf, pfc);
2162 if (ret) {
2163 mtk_vdec_err(ctx, "vdec_vp9_slice_setup_core\n");
2164 goto err;
2165 }
2166 vdec_vp9_slice_vsi_to_remote(&pfc->vsi, instance->core_vsi);
2167
2168 ret = vpu_dec_core(&instance->vpu);
2169 if (ret) {
2170 mtk_vdec_err(ctx, "vpu_dec_core\n");
2171 goto err;
2172 }
2173
2174 if (instance->irq) {
2175 ret = mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
2176 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
2177 /* update remote vsi if decode timeout */
2178 if (ret) {
2179 mtk_vdec_err(ctx, "VP9 core timeout pic %d\n", pfc->seq);
2180 WRITE_ONCE(instance->core_vsi->state.timeout, 1);
2181 }
2182 vpu_dec_core_end(&instance->vpu);
2183 }
2184
2185 vdec_vp9_slice_vsi_from_remote(&pfc->vsi, instance->core_vsi, 1);
2186 ret = vdec_vp9_slice_update_core(instance, lat_buf, pfc);
2187 if (ret) {
2188 mtk_vdec_err(ctx, "vdec_vp9_slice_update_core\n");
2189 goto err;
2190 }
2191
2192 pfc->vsi.trans.dma_addr_end += ctx->msg_queue.wdma_addr.dma_addr;
2193 mtk_vdec_debug(ctx, "core dma_addr_end 0x%lx\n",
2194 (unsigned long)pfc->vsi.trans.dma_addr_end);
2195 vdec_msg_queue_update_ube_rptr(&ctx->msg_queue, pfc->vsi.trans.dma_addr_end);
2196 ctx->dev->vdec_pdata->cap_to_disp(ctx, 0, lat_buf->src_buf_req);
2197
2198 return 0;
2199
2200 err:
2201 if (ctx && pfc) {
2202 /* always update read pointer */
2203 vdec_msg_queue_update_ube_rptr(&ctx->msg_queue, pfc->vsi.trans.dma_addr_end);
2204
2205 if (fb)
2206 ctx->dev->vdec_pdata->cap_to_disp(ctx, 1, lat_buf->src_buf_req);
2207 }
2208 return ret;
2209 }
2210
2211 const struct vdec_common_if vdec_vp9_slice_lat_if = {
2212 .init = vdec_vp9_slice_init,
2213 .decode = vdec_vp9_slice_decode,
2214 .get_param = vdec_vp9_slice_get_param,
2215 .deinit = vdec_vp9_slice_deinit,
2216 };
2217