1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2021 MediaTek Inc.
4 * Author: George Sun <george.sun@mediatek.com>
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <media/videobuf2-dma-contig.h>
10 #include <media/v4l2-vp9.h>
12 #include "../mtk_vcodec_util.h"
13 #include "../mtk_vcodec_dec.h"
14 #include "../mtk_vcodec_intr.h"
15 #include "../vdec_drv_base.h"
16 #include "../vdec_drv_if.h"
17 #include "../vdec_vpu_if.h"
19 /* reset_frame_context defined in VP9 spec */
20 #define VP9_RESET_FRAME_CONTEXT_NONE0 0
21 #define VP9_RESET_FRAME_CONTEXT_NONE1 1
22 #define VP9_RESET_FRAME_CONTEXT_SPEC 2
23 #define VP9_RESET_FRAME_CONTEXT_ALL 3
25 #define VP9_TILE_BUF_SIZE 4096
26 #define VP9_PROB_BUF_SIZE 2560
27 #define VP9_COUNTS_BUF_SIZE 16384
29 #define HDR_FLAG(x) (!!((hdr)->flags & V4L2_VP9_FRAME_FLAG_##x))
30 #define LF_FLAG(x) (!!((lf)->flags & V4L2_VP9_LOOP_FILTER_FLAG_##x))
31 #define SEG_FLAG(x) (!!((seg)->flags & V4L2_VP9_SEGMENTATION_FLAG_##x))
32 #define VP9_BAND_6(band) ((band) == 0 ? 3 : 6)
35 * struct vdec_vp9_slice_frame_ctx - vp9 prob tables footprint
37 struct vdec_vp9_slice_frame_ctx {
41 } coef_probs[4][2][2][6];
43 u8 y_mode_prob[4][16];
44 u8 switch_interp_prob[4][16];
45 u8 seg[32]; /* ignore */
46 u8 comp_inter_prob[16];
48 u8 single_ref_prob[5][2];
49 u8 single_ref_prob_padding[6];
71 u8 uv_mode_prob[10][16];
72 u8 uv_mode_prob_padding[2][16];
74 u8 partition_prob[16][4];
76 u8 inter_mode_probs[7][4];
82 u8 intra_inter_prob[8];
86 * struct vdec_vp9_slice_frame_counts - vp9 counts tables footprint
88 struct vdec_vp9_slice_frame_counts {
95 } eob_branch[4][2][2];
96 u32 eob_branch_space[256 * 4];
101 u32 band_1_5[5][6][4];
102 } coef_probs[4][2][2];
104 u32 intra_inter[4][2];
105 u32 comp_inter[5][2];
106 u32 comp_inter_padding[2];
108 u32 comp_ref_padding[2];
109 u32 single_ref[5][2][2];
110 u32 inter_mode[7][4];
113 u32 partition[16][4];
114 u32 switchable_interp[4][4];
136 u32 reserved[126][4];
140 * struct vdec_vp9_slice_counts_map - vp9 counts tables to map
141 * v4l2_vp9_frame_symbol_counts
142 * @skip: skip counts.
143 * @y_mode: Y prediction mode counts.
144 * @filter: interpolation filter counts.
145 * @mv_joint: motion vector joint counts.
146 * @sign: motion vector sign counts.
147 * @classes: motion vector class counts.
148 * @class0: motion vector class0 bit counts.
149 * @bits: motion vector bits counts.
150 * @class0_fp: motion vector class0 fractional bit counts.
151 * @fp: motion vector fractional bit counts.
152 * @class0_hp: motion vector class0 high precision fractional bit counts.
153 * @hp: motion vector high precision fractional bit counts.
155 struct vdec_vp9_slice_counts_map {
163 u32 class0_fp[2][2][4];
170 * struct vdec_vp9_slice_uncompressed_header - vp9 uncompressed header syntax
173 struct vdec_vp9_slice_uncompressed_header {
180 u8 error_resilient_mode;
184 u16 last_frame_width;
185 u16 last_frame_height;
190 u8 reset_frame_context;
191 u8 ref_frame_sign_bias[4];
192 u8 allow_high_precision_mv;
193 u8 interpolation_filter;
195 u8 refresh_frame_context;
196 u8 frame_parallel_decoding_mode;
197 u8 frame_context_idx;
199 /* loop_filter_params */
200 u8 loop_filter_level;
201 u8 loop_filter_sharpness;
202 u8 loop_filter_delta_enabled;
203 s8 loop_filter_ref_deltas[4];
204 s8 loop_filter_mode_deltas[2];
206 /* quantization_params */
212 /* segmentation_params */
213 u8 segmentation_enabled;
214 u8 segmentation_update_map;
215 u8 segmentation_tree_probs[7];
217 u8 segmentation_temporal_udpate;
218 u8 segmentation_pred_prob[3];
219 u8 segmentation_update_data;
220 u8 segmentation_abs_or_delta_update;
221 u8 feature_enabled[8];
222 s16 feature_value[8][4];
229 u16 uncompressed_header_size;
230 u16 header_size_in_bytes;
232 /* LAT OUT, CORE IN */
237 * struct vdec_vp9_slice_compressed_header - vp9 compressed header syntax
240 struct vdec_vp9_slice_compressed_header {
249 * struct vdec_vp9_slice_tiles - vp9 tile syntax
251 struct vdec_vp9_slice_tiles {
260 * struct vdec_vp9_slice_reference - vp9 reference frame information
262 struct vdec_vp9_slice_reference {
272 * struct vdec_vp9_slice_frame - vp9 syntax used for decoding
274 struct vdec_vp9_slice_frame {
275 struct vdec_vp9_slice_uncompressed_header uh;
276 struct vdec_vp9_slice_compressed_header ch;
277 struct vdec_vp9_slice_tiles tiles;
278 struct vdec_vp9_slice_reference ref[3];
282 * struct vdec_vp9_slice_init_vsi - VSI used to initialize instance
284 struct vdec_vp9_slice_init_vsi {
285 unsigned int architecture;
286 unsigned int reserved;
288 /* default frame context's position in MicroP */
289 u64 default_frame_ctx;
293 * struct vdec_vp9_slice_mem - memory address and size
295 struct vdec_vp9_slice_mem {
302 dma_addr_t dma_addr_end;
308 * struct vdec_vp9_slice_bs - input buffer for decoding
310 struct vdec_vp9_slice_bs {
311 struct vdec_vp9_slice_mem buf;
312 struct vdec_vp9_slice_mem frame;
316 * struct vdec_vp9_slice_fb - frame buffer for decoding
318 struct vdec_vp9_slice_fb {
319 struct vdec_vp9_slice_mem y;
320 struct vdec_vp9_slice_mem c;
324 * struct vdec_vp9_slice_state - decoding state
326 struct vdec_vp9_slice_state {
329 unsigned int timeout;
332 unsigned int crc[12];
336 * struct vdec_vp9_slice_vsi - exchange decoding information
337 * between Main CPU and MicroP
341 * @ref: 3 reference buffers
342 * @mv: mv working buffer
343 * @seg: segmentation working buffer
345 * @prob: prob table buffer, used to set/update prob table
346 * @counts: counts table buffer, used to update prob table
347 * @ube: general buffer
348 * @trans: trans buffer position in general buffer
349 * @err_map: error buffer
350 * @row_info: row info buffer
351 * @frame: decoding syntax
352 * @state: decoding state
354 struct vdec_vp9_slice_vsi {
355 /* used in LAT stage */
356 struct vdec_vp9_slice_bs bs;
357 /* used in Core stage */
358 struct vdec_vp9_slice_fb fb;
359 struct vdec_vp9_slice_fb ref[3];
361 struct vdec_vp9_slice_mem mv[2];
362 struct vdec_vp9_slice_mem seg[2];
363 struct vdec_vp9_slice_mem tile;
364 struct vdec_vp9_slice_mem prob;
365 struct vdec_vp9_slice_mem counts;
367 /* LAT stage's output, Core stage's input */
368 struct vdec_vp9_slice_mem ube;
369 struct vdec_vp9_slice_mem trans;
370 struct vdec_vp9_slice_mem err_map;
371 struct vdec_vp9_slice_mem row_info;
373 /* decoding parameters */
374 struct vdec_vp9_slice_frame frame;
376 struct vdec_vp9_slice_state state;
380 * struct vdec_vp9_slice_pfc - per-frame context that contains a local vsi.
381 * pass it from lat to core
383 * @vsi: local vsi. copy to/from remote vsi before/after decoding
384 * @ref_idx: reference buffer index
385 * @seq: picture sequence
386 * @state: decoding state
388 struct vdec_vp9_slice_pfc {
389 struct vdec_vp9_slice_vsi vsi;
396 struct vdec_vp9_slice_state state[2];
400 * enum vdec_vp9_slice_resolution_level
402 enum vdec_vp9_slice_resolution_level {
410 * struct vdec_vp9_slice_ref - picture's width & height should kept
411 * for later decoding as reference picture
413 struct vdec_vp9_slice_ref {
419 * struct vdec_vp9_slice_instance - represent one vp9 instance
421 * @ctx: pointer to codec's context
423 * @seq: global picture sequence
424 * @level: level of current resolution
425 * @width: width of last picture
426 * @height: height of last picture
427 * @frame_type: frame_type of last picture
428 * @irq: irq to Main CPU or MicroP
429 * @show_frame: show_frame of last picture
430 * @dpb: picture information (width/height) for reference
431 * @mv: mv working buffer
432 * @seg: segmentation working buffer
434 * @prob: prob table buffer, used to set/update prob table
435 * @counts: counts table buffer, used to update prob table
436 * @frame_ctx: 4 frame context according to VP9 Spec
437 * @frame_ctx_helper: 4 frame context according to newest kernel spec
438 * @dirty: state of each frame context
439 * @init_vsi: vsi used for initialized VP9 instance
440 * @vsi: vsi used for decoding/flush ...
441 * @core_vsi: vsi used for Core stage
443 * @sc_pfc: per frame context single core
444 * @counts_map: used map to counts_helper
445 * @counts_helper: counts table according to newest kernel spec
447 struct vdec_vp9_slice_instance {
448 struct mtk_vcodec_ctx *ctx;
449 struct vdec_vpu_inst vpu;
453 enum vdec_vp9_slice_resolution_level level;
455 /* for resolution change and get_pic_info */
459 /* for last_frame_type */
460 unsigned int frame_type;
463 unsigned int show_frame;
465 /* maintain vp9 reference frame state */
466 struct vdec_vp9_slice_ref dpb[VB2_MAX_FRAME];
469 * normal working buffers
470 * mv[0]/seg[0]/tile/prob/counts is used for LAT
471 * mv[1]/seg[1] is used for CORE
473 struct mtk_vcodec_mem mv[2];
474 struct mtk_vcodec_mem seg[2];
475 struct mtk_vcodec_mem tile;
476 struct mtk_vcodec_mem prob;
477 struct mtk_vcodec_mem counts;
480 struct vdec_vp9_slice_frame_ctx frame_ctx[4];
482 struct v4l2_vp9_frame_context frame_ctx_helper;
483 unsigned char dirty[4];
487 struct vdec_vp9_slice_init_vsi *init_vsi;
488 struct vdec_vp9_slice_vsi *vsi;
490 struct vdec_vp9_slice_vsi *core_vsi;
492 struct vdec_vp9_slice_pfc sc_pfc;
493 struct vdec_vp9_slice_counts_map counts_map;
494 struct v4l2_vp9_frame_symbol_counts counts_helper;
498 * all VP9 instances could share this default frame context.
500 static struct vdec_vp9_slice_frame_ctx *vdec_vp9_slice_default_frame_ctx;
501 static DEFINE_MUTEX(vdec_vp9_slice_frame_ctx_lock);
503 static int vdec_vp9_slice_core_decode(struct vdec_lat_buf *lat_buf);
505 static int vdec_vp9_slice_init_default_frame_ctx(struct vdec_vp9_slice_instance *instance)
507 struct vdec_vp9_slice_frame_ctx *remote_frame_ctx;
508 struct vdec_vp9_slice_frame_ctx *frame_ctx;
509 struct mtk_vcodec_ctx *ctx;
510 struct vdec_vp9_slice_init_vsi *vsi;
514 vsi = instance->vpu.vsi;
518 remote_frame_ctx = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
519 (u32)vsi->default_frame_ctx);
520 if (!remote_frame_ctx) {
521 mtk_vcodec_err(instance, "failed to map default frame ctx\n");
525 mutex_lock(&vdec_vp9_slice_frame_ctx_lock);
526 if (vdec_vp9_slice_default_frame_ctx)
529 frame_ctx = kmemdup(remote_frame_ctx, sizeof(*frame_ctx), GFP_KERNEL);
535 vdec_vp9_slice_default_frame_ctx = frame_ctx;
538 mutex_unlock(&vdec_vp9_slice_frame_ctx_lock);
543 static int vdec_vp9_slice_alloc_working_buffer(struct vdec_vp9_slice_instance *instance,
544 struct vdec_vp9_slice_vsi *vsi)
546 struct mtk_vcodec_ctx *ctx = instance->ctx;
547 enum vdec_vp9_slice_resolution_level level;
549 unsigned int max_sb_w;
550 unsigned int max_sb_h;
559 w = vsi->frame.uh.frame_width;
560 h = vsi->frame.uh.frame_height;
562 if (w > VCODEC_DEC_4K_CODED_WIDTH ||
563 h > VCODEC_DEC_4K_CODED_HEIGHT) {
565 } else if (w > MTK_VDEC_MAX_W || h > MTK_VDEC_MAX_H) {
568 max_w = VCODEC_DEC_4K_CODED_WIDTH;
569 max_h = VCODEC_DEC_4K_CODED_HEIGHT;
573 max_w = MTK_VDEC_MAX_W;
574 max_h = MTK_VDEC_MAX_H;
577 if (level == instance->level)
580 mtk_vcodec_debug(instance, "resolution level changed, from %u to %u, %ux%u",
581 instance->level, level, w, h);
583 max_sb_w = DIV_ROUND_UP(max_w, 64);
584 max_sb_h = DIV_ROUND_UP(max_h, 64);
588 * Lat-flush must wait core idle, otherwise core will
589 * use released buffers
592 size = (max_sb_w * max_sb_h + 2) * 576;
593 for (i = 0; i < 2; i++) {
594 if (instance->mv[i].va)
595 mtk_vcodec_mem_free(ctx, &instance->mv[i]);
596 instance->mv[i].size = size;
597 if (mtk_vcodec_mem_alloc(ctx, &instance->mv[i]))
601 size = (max_sb_w * max_sb_h * 32) + 256;
602 for (i = 0; i < 2; i++) {
603 if (instance->seg[i].va)
604 mtk_vcodec_mem_free(ctx, &instance->seg[i]);
605 instance->seg[i].size = size;
606 if (mtk_vcodec_mem_alloc(ctx, &instance->seg[i]))
610 if (!instance->tile.va) {
611 instance->tile.size = VP9_TILE_BUF_SIZE;
612 if (mtk_vcodec_mem_alloc(ctx, &instance->tile))
616 if (!instance->prob.va) {
617 instance->prob.size = VP9_PROB_BUF_SIZE;
618 if (mtk_vcodec_mem_alloc(ctx, &instance->prob))
622 if (!instance->counts.va) {
623 instance->counts.size = VP9_COUNTS_BUF_SIZE;
624 if (mtk_vcodec_mem_alloc(ctx, &instance->counts))
628 instance->level = level;
632 instance->level = VP9_RES_NONE;
636 static void vdec_vp9_slice_free_working_buffer(struct vdec_vp9_slice_instance *instance)
638 struct mtk_vcodec_ctx *ctx = instance->ctx;
641 for (i = 0; i < ARRAY_SIZE(instance->mv); i++) {
642 if (instance->mv[i].va)
643 mtk_vcodec_mem_free(ctx, &instance->mv[i]);
645 for (i = 0; i < ARRAY_SIZE(instance->seg); i++) {
646 if (instance->seg[i].va)
647 mtk_vcodec_mem_free(ctx, &instance->seg[i]);
649 if (instance->tile.va)
650 mtk_vcodec_mem_free(ctx, &instance->tile);
651 if (instance->prob.va)
652 mtk_vcodec_mem_free(ctx, &instance->prob);
653 if (instance->counts.va)
654 mtk_vcodec_mem_free(ctx, &instance->counts);
656 instance->level = VP9_RES_NONE;
659 static void vdec_vp9_slice_vsi_from_remote(struct vdec_vp9_slice_vsi *vsi,
660 struct vdec_vp9_slice_vsi *remote_vsi,
663 struct vdec_vp9_slice_frame *rf;
664 struct vdec_vp9_slice_frame *f;
673 rf = &remote_vsi->frame;
675 memcpy(&f->ch, &rf->ch, sizeof(f->ch));
676 memcpy(&f->uh.dequant, &rf->uh.dequant, sizeof(f->uh.dequant));
677 memcpy(&vsi->trans, &remote_vsi->trans, sizeof(vsi->trans));
680 memcpy(&vsi->state, &remote_vsi->state, sizeof(vsi->state));
683 static void vdec_vp9_slice_vsi_to_remote(struct vdec_vp9_slice_vsi *vsi,
684 struct vdec_vp9_slice_vsi *remote_vsi)
686 memcpy(remote_vsi, vsi, sizeof(*vsi));
689 static int vdec_vp9_slice_tile_offset(int idx, int mi_num, int tile_log2)
691 int sbs = (mi_num + 7) >> 3;
692 int offset = ((idx * sbs) >> tile_log2) << 3;
694 return min(offset, mi_num);
698 int vdec_vp9_slice_setup_single_from_src_to_dst(struct vdec_vp9_slice_instance *instance)
700 struct vb2_v4l2_buffer *src;
701 struct vb2_v4l2_buffer *dst;
703 src = v4l2_m2m_next_src_buf(instance->ctx->m2m_ctx);
707 dst = v4l2_m2m_next_dst_buf(instance->ctx->m2m_ctx);
711 v4l2_m2m_buf_copy_metadata(src, dst, true);
716 static int vdec_vp9_slice_setup_lat_from_src_buf(struct vdec_vp9_slice_instance *instance,
717 struct vdec_lat_buf *lat_buf)
719 struct vb2_v4l2_buffer *src;
720 struct vb2_v4l2_buffer *dst;
722 src = v4l2_m2m_next_src_buf(instance->ctx->m2m_ctx);
726 lat_buf->src_buf_req = src->vb2_buf.req_obj.req;
728 dst = &lat_buf->ts_info;
729 v4l2_m2m_buf_copy_metadata(src, dst, true);
733 static void vdec_vp9_slice_setup_hdr(struct vdec_vp9_slice_instance *instance,
734 struct vdec_vp9_slice_uncompressed_header *uh,
735 struct v4l2_ctrl_vp9_frame *hdr)
739 uh->profile = hdr->profile;
740 uh->last_frame_type = instance->frame_type;
741 uh->frame_type = !HDR_FLAG(KEY_FRAME);
742 uh->last_show_frame = instance->show_frame;
743 uh->show_frame = HDR_FLAG(SHOW_FRAME);
744 uh->error_resilient_mode = HDR_FLAG(ERROR_RESILIENT);
745 uh->bit_depth = hdr->bit_depth;
746 uh->last_frame_width = instance->width;
747 uh->last_frame_height = instance->height;
748 uh->frame_width = hdr->frame_width_minus_1 + 1;
749 uh->frame_height = hdr->frame_height_minus_1 + 1;
750 uh->intra_only = HDR_FLAG(INTRA_ONLY);
751 /* map v4l2 enum to values defined in VP9 spec for firmware */
752 switch (hdr->reset_frame_context) {
753 case V4L2_VP9_RESET_FRAME_CTX_NONE:
754 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_NONE0;
756 case V4L2_VP9_RESET_FRAME_CTX_SPEC:
757 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_SPEC;
759 case V4L2_VP9_RESET_FRAME_CTX_ALL:
760 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_ALL;
763 uh->reset_frame_context = VP9_RESET_FRAME_CONTEXT_NONE0;
767 * ref_frame_sign_bias specifies the intended direction
768 * of the motion vector in time for each reference frame.
771 * - GOLDEN_FRAME = 2,
772 * - ALTREF_FRAME = 3,
773 * ref_frame_sign_bias[INTRA_FRAME] is always 0
774 * and VDA only passes another 3 directions
776 uh->ref_frame_sign_bias[0] = 0;
777 for (i = 0; i < 3; i++)
778 uh->ref_frame_sign_bias[i + 1] =
779 !!(hdr->ref_frame_sign_bias & (1 << i));
780 uh->allow_high_precision_mv = HDR_FLAG(ALLOW_HIGH_PREC_MV);
781 uh->interpolation_filter = hdr->interpolation_filter;
782 uh->refresh_frame_context = HDR_FLAG(REFRESH_FRAME_CTX);
783 uh->frame_parallel_decoding_mode = HDR_FLAG(PARALLEL_DEC_MODE);
784 uh->frame_context_idx = hdr->frame_context_idx;
787 uh->tile_cols_log2 = hdr->tile_cols_log2;
788 uh->tile_rows_log2 = hdr->tile_rows_log2;
790 uh->uncompressed_header_size = hdr->uncompressed_header_size;
791 uh->header_size_in_bytes = hdr->compressed_header_size;
794 static void vdec_vp9_slice_setup_frame_ctx(struct vdec_vp9_slice_instance *instance,
795 struct vdec_vp9_slice_uncompressed_header *uh,
796 struct v4l2_ctrl_vp9_frame *hdr)
798 int error_resilient_mode;
799 int reset_frame_context;
804 key_frame = HDR_FLAG(KEY_FRAME);
805 intra_only = HDR_FLAG(INTRA_ONLY);
806 error_resilient_mode = HDR_FLAG(ERROR_RESILIENT);
807 reset_frame_context = uh->reset_frame_context;
810 * according to "6.2 Uncompressed header syntax" in
811 * "VP9 Bitstream & Decoding Process Specification",
812 * reset @frame_context_idx when (FrameIsIntra || error_resilient_mode)
814 if (key_frame || intra_only || error_resilient_mode) {
816 * @reset_frame_context specifies
817 * whether the frame context should be
818 * reset to default values:
819 * 0 or 1 means do not reset any frame context
820 * 2 resets just the context specified in the frame header
821 * 3 resets all contexts
823 if (key_frame || error_resilient_mode ||
824 reset_frame_context == 3) {
825 /* use default table */
826 for (i = 0; i < 4; i++)
827 instance->dirty[i] = 0;
828 } else if (reset_frame_context == 2) {
829 instance->dirty[uh->frame_context_idx] = 0;
831 uh->frame_context_idx = 0;
835 static void vdec_vp9_slice_setup_loop_filter(struct vdec_vp9_slice_uncompressed_header *uh,
836 struct v4l2_vp9_loop_filter *lf)
840 uh->loop_filter_level = lf->level;
841 uh->loop_filter_sharpness = lf->sharpness;
842 uh->loop_filter_delta_enabled = LF_FLAG(DELTA_ENABLED);
843 for (i = 0; i < 4; i++)
844 uh->loop_filter_ref_deltas[i] = lf->ref_deltas[i];
845 for (i = 0; i < 2; i++)
846 uh->loop_filter_mode_deltas[i] = lf->mode_deltas[i];
849 static void vdec_vp9_slice_setup_quantization(struct vdec_vp9_slice_uncompressed_header *uh,
850 struct v4l2_vp9_quantization *quant)
852 uh->base_q_idx = quant->base_q_idx;
853 uh->delta_q_y_dc = quant->delta_q_y_dc;
854 uh->delta_q_uv_dc = quant->delta_q_uv_dc;
855 uh->delta_q_uv_ac = quant->delta_q_uv_ac;
858 static void vdec_vp9_slice_setup_segmentation(struct vdec_vp9_slice_uncompressed_header *uh,
859 struct v4l2_vp9_segmentation *seg)
864 uh->segmentation_enabled = SEG_FLAG(ENABLED);
865 uh->segmentation_update_map = SEG_FLAG(UPDATE_MAP);
866 for (i = 0; i < 7; i++)
867 uh->segmentation_tree_probs[i] = seg->tree_probs[i];
868 uh->segmentation_temporal_udpate = SEG_FLAG(TEMPORAL_UPDATE);
869 for (i = 0; i < 3; i++)
870 uh->segmentation_pred_prob[i] = seg->pred_probs[i];
871 uh->segmentation_update_data = SEG_FLAG(UPDATE_DATA);
872 uh->segmentation_abs_or_delta_update = SEG_FLAG(ABS_OR_DELTA_UPDATE);
873 for (i = 0; i < 8; i++) {
874 uh->feature_enabled[i] = seg->feature_enabled[i];
875 for (j = 0; j < 4; j++)
876 uh->feature_value[i][j] = seg->feature_data[i][j];
880 static int vdec_vp9_slice_setup_tile(struct vdec_vp9_slice_vsi *vsi,
881 struct v4l2_ctrl_vp9_frame *hdr)
883 unsigned int rows_log2;
884 unsigned int cols_log2;
887 unsigned int mi_rows;
888 unsigned int mi_cols;
889 struct vdec_vp9_slice_tiles *tiles;
895 rows_log2 = hdr->tile_rows_log2;
896 cols_log2 = hdr->tile_cols_log2;
897 rows = 1 << rows_log2;
898 cols = 1 << cols_log2;
899 tiles = &vsi->frame.tiles;
900 tiles->actual_rows = 0;
902 if (rows > 4 || cols > 64)
905 /* setup mi rows/cols information */
906 mi_rows = (hdr->frame_height_minus_1 + 1 + 7) >> 3;
907 mi_cols = (hdr->frame_width_minus_1 + 1 + 7) >> 3;
909 for (i = 0; i < rows; i++) {
910 start = vdec_vp9_slice_tile_offset(i, mi_rows, rows_log2);
911 end = vdec_vp9_slice_tile_offset(i + 1, mi_rows, rows_log2);
912 offset = end - start;
913 tiles->mi_rows[i] = (offset + 7) >> 3;
914 if (tiles->mi_rows[i])
915 tiles->actual_rows++;
918 for (i = 0; i < cols; i++) {
919 start = vdec_vp9_slice_tile_offset(i, mi_cols, cols_log2);
920 end = vdec_vp9_slice_tile_offset(i + 1, mi_cols, cols_log2);
921 offset = end - start;
922 tiles->mi_cols[i] = (offset + 7) >> 3;
928 static void vdec_vp9_slice_setup_state(struct vdec_vp9_slice_vsi *vsi)
930 memset(&vsi->state, 0, sizeof(vsi->state));
933 static void vdec_vp9_slice_setup_ref_idx(struct vdec_vp9_slice_pfc *pfc,
934 struct v4l2_ctrl_vp9_frame *hdr)
936 pfc->ref_idx[0] = hdr->last_frame_ts;
937 pfc->ref_idx[1] = hdr->golden_frame_ts;
938 pfc->ref_idx[2] = hdr->alt_frame_ts;
941 static int vdec_vp9_slice_setup_pfc(struct vdec_vp9_slice_instance *instance,
942 struct vdec_vp9_slice_pfc *pfc)
944 struct v4l2_ctrl_vp9_frame *hdr;
945 struct vdec_vp9_slice_uncompressed_header *uh;
946 struct v4l2_ctrl *hdr_ctrl;
947 struct vdec_vp9_slice_vsi *vsi;
951 hdr_ctrl = v4l2_ctrl_find(&instance->ctx->ctrl_hdl, V4L2_CID_STATELESS_VP9_FRAME);
952 if (!hdr_ctrl || !hdr_ctrl->p_cur.p)
955 hdr = hdr_ctrl->p_cur.p;
959 /* setup vsi information */
960 vdec_vp9_slice_setup_hdr(instance, uh, hdr);
961 vdec_vp9_slice_setup_frame_ctx(instance, uh, hdr);
962 vdec_vp9_slice_setup_loop_filter(uh, &hdr->lf);
963 vdec_vp9_slice_setup_quantization(uh, &hdr->quant);
964 vdec_vp9_slice_setup_segmentation(uh, &hdr->seg);
965 ret = vdec_vp9_slice_setup_tile(vsi, hdr);
968 vdec_vp9_slice_setup_state(vsi);
970 /* core stage needs buffer index to get ref y/c ... */
971 vdec_vp9_slice_setup_ref_idx(pfc, hdr);
973 pfc->seq = instance->seq;
979 static int vdec_vp9_slice_setup_lat_buffer(struct vdec_vp9_slice_instance *instance,
980 struct vdec_vp9_slice_vsi *vsi,
981 struct mtk_vcodec_mem *bs,
982 struct vdec_lat_buf *lat_buf)
986 vsi->bs.buf.dma_addr = bs->dma_addr;
987 vsi->bs.buf.size = bs->size;
988 vsi->bs.frame.dma_addr = bs->dma_addr;
989 vsi->bs.frame.size = bs->size;
991 for (i = 0; i < 2; i++) {
992 vsi->mv[i].dma_addr = instance->mv[i].dma_addr;
993 vsi->mv[i].size = instance->mv[i].size;
995 for (i = 0; i < 2; i++) {
996 vsi->seg[i].dma_addr = instance->seg[i].dma_addr;
997 vsi->seg[i].size = instance->seg[i].size;
999 vsi->tile.dma_addr = instance->tile.dma_addr;
1000 vsi->tile.size = instance->tile.size;
1001 vsi->prob.dma_addr = instance->prob.dma_addr;
1002 vsi->prob.size = instance->prob.size;
1003 vsi->counts.dma_addr = instance->counts.dma_addr;
1004 vsi->counts.size = instance->counts.size;
1006 vsi->ube.dma_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
1007 vsi->ube.size = lat_buf->ctx->msg_queue.wdma_addr.size;
1008 vsi->trans.dma_addr = lat_buf->ctx->msg_queue.wdma_wptr_addr;
1009 /* used to store trans end */
1010 vsi->trans.dma_addr_end = lat_buf->ctx->msg_queue.wdma_rptr_addr;
1011 vsi->err_map.dma_addr = lat_buf->wdma_err_addr.dma_addr;
1012 vsi->err_map.size = lat_buf->wdma_err_addr.size;
1014 vsi->row_info.buf = 0;
1015 vsi->row_info.size = 0;
1020 static int vdec_vp9_slice_setup_prob_buffer(struct vdec_vp9_slice_instance *instance,
1021 struct vdec_vp9_slice_vsi *vsi)
1023 struct vdec_vp9_slice_frame_ctx *frame_ctx;
1024 struct vdec_vp9_slice_uncompressed_header *uh;
1026 uh = &vsi->frame.uh;
1028 mtk_vcodec_debug(instance, "ctx dirty %u idx %d\n",
1029 instance->dirty[uh->frame_context_idx],
1030 uh->frame_context_idx);
1032 if (instance->dirty[uh->frame_context_idx])
1033 frame_ctx = &instance->frame_ctx[uh->frame_context_idx];
1035 frame_ctx = vdec_vp9_slice_default_frame_ctx;
1036 memcpy(instance->prob.va, frame_ctx, sizeof(*frame_ctx));
1041 static void vdec_vp9_slice_setup_seg_buffer(struct vdec_vp9_slice_instance *instance,
1042 struct vdec_vp9_slice_vsi *vsi,
1043 struct mtk_vcodec_mem *buf)
1045 struct vdec_vp9_slice_uncompressed_header *uh;
1047 /* reset segment buffer */
1048 uh = &vsi->frame.uh;
1049 if (uh->frame_type == 0 ||
1051 uh->error_resilient_mode ||
1052 uh->frame_width != instance->width ||
1053 uh->frame_height != instance->height) {
1054 mtk_vcodec_debug(instance, "reset seg\n");
1055 memset(buf->va, 0, buf->size);
1060 * parse tiles according to `6.4 Decode tiles syntax`
1061 * in "vp9-bitstream-specification"
1063 * frame contains uncompress header, compressed header and several tiles.
1064 * this function parses tiles' position and size, stores them to tile buffer
1067 static int vdec_vp9_slice_setup_tile_buffer(struct vdec_vp9_slice_instance *instance,
1068 struct vdec_vp9_slice_vsi *vsi,
1069 struct mtk_vcodec_mem *bs)
1071 struct vdec_vp9_slice_uncompressed_header *uh;
1072 unsigned int rows_log2;
1073 unsigned int cols_log2;
1076 unsigned int mi_row;
1077 unsigned int mi_col;
1078 unsigned int offset;
1081 struct vdec_vp9_slice_tiles *tiles;
1089 uh = &vsi->frame.uh;
1090 rows_log2 = uh->tile_rows_log2;
1091 cols_log2 = uh->tile_cols_log2;
1092 rows = 1 << rows_log2;
1093 cols = 1 << cols_log2;
1095 if (rows > 4 || cols > 64) {
1096 mtk_vcodec_err(instance, "tile_rows %u tile_cols %u\n",
1101 offset = uh->uncompressed_header_size +
1102 uh->header_size_in_bytes;
1103 if (bs->size <= offset) {
1104 mtk_vcodec_err(instance, "bs size %zu tile offset %u\n",
1109 tiles = &vsi->frame.tiles;
1110 /* setup tile buffer */
1112 va = (unsigned char *)bs->va;
1114 end = va + bs->size;
1116 pa = (unsigned int)bs->dma_addr + offset;
1117 tb = instance->tile.va;
1118 for (i = 0; i < rows; i++) {
1119 for (j = 0; j < cols; j++) {
1120 if (i == rows - 1 &&
1122 size = (unsigned int)(end - pos);
1127 size = (pos[0] << 24) | (pos[1] << 16) |
1128 (pos[2] << 8) | pos[3];
1132 if (end - pos < size)
1135 tiles->size[i][j] = size;
1136 if (tiles->mi_rows[i]) {
1137 *tb++ = (size << 3) + ((offset << 3) & 0x7f);
1139 *tb++ = (pa << 3) & 0x7f;
1140 mi_row = (tiles->mi_rows[i] - 1) & 0x1ff;
1141 mi_col = (tiles->mi_cols[j] - 1) & 0x3f;
1142 *tb++ = (mi_row << 6) + mi_col;
1153 static int vdec_vp9_slice_setup_lat(struct vdec_vp9_slice_instance *instance,
1154 struct mtk_vcodec_mem *bs,
1155 struct vdec_lat_buf *lat_buf,
1156 struct vdec_vp9_slice_pfc *pfc)
1158 struct vdec_vp9_slice_vsi *vsi = &pfc->vsi;
1161 ret = vdec_vp9_slice_setup_lat_from_src_buf(instance, lat_buf);
1165 ret = vdec_vp9_slice_setup_pfc(instance, pfc);
1169 ret = vdec_vp9_slice_alloc_working_buffer(instance, vsi);
1173 ret = vdec_vp9_slice_setup_lat_buffer(instance, vsi, bs, lat_buf);
1177 vdec_vp9_slice_setup_seg_buffer(instance, vsi, &instance->seg[0]);
1179 /* setup prob/tile buffers for LAT */
1181 ret = vdec_vp9_slice_setup_prob_buffer(instance, vsi);
1185 ret = vdec_vp9_slice_setup_tile_buffer(instance, vsi, bs);
1196 void vdec_vp9_slice_map_counts_eob_coef(unsigned int i, unsigned int j, unsigned int k,
1197 struct vdec_vp9_slice_frame_counts *counts,
1198 struct v4l2_vp9_frame_symbol_counts *counts_helper)
1203 * helper eo -> mtk eo
1204 * helpre e1 -> mtk c3
1209 for (m = 0; m < 3; m++) {
1210 counts_helper->coeff[i][j][k][l][m] =
1211 (u32 (*)[3]) & counts->coef_probs[i][j][k].band_0[m];
1212 counts_helper->eob[i][j][k][l][m][0] =
1213 &counts->eob_branch[i][j][k].band_0[m];
1214 counts_helper->eob[i][j][k][l][m][1] =
1215 &counts->coef_probs[i][j][k].band_0[m][3];
1218 for (l = 1; l < 6; l++) {
1219 for (m = 0; m < 6; m++) {
1220 counts_helper->coeff[i][j][k][l][m] =
1221 (u32 (*)[3]) & counts->coef_probs[i][j][k].band_1_5[l - 1][m];
1222 counts_helper->eob[i][j][k][l][m][0] =
1223 &counts->eob_branch[i][j][k].band_1_5[l - 1][m];
1224 counts_helper->eob[i][j][k][l][m][1] =
1225 &counts->coef_probs[i][j][k].band_1_5[l - 1][m][3];
1230 static void vdec_vp9_slice_counts_map_helper(struct vdec_vp9_slice_counts_map *counts_map,
1231 struct vdec_vp9_slice_frame_counts *counts,
1232 struct v4l2_vp9_frame_symbol_counts *counts_helper)
1236 counts_helper->partition = &counts->partition;
1237 counts_helper->intra_inter = &counts->intra_inter;
1238 counts_helper->tx32p = &counts->tx_p32x32;
1239 counts_helper->tx16p = &counts->tx_p16x16;
1240 counts_helper->tx8p = &counts->tx_p8x8;
1241 counts_helper->uv_mode = &counts->uv_mode;
1243 counts_helper->comp = &counts->comp_inter;
1244 counts_helper->comp_ref = &counts->comp_ref;
1245 counts_helper->single_ref = &counts->single_ref;
1246 counts_helper->mv_mode = &counts->inter_mode;
1247 counts_helper->mv_joint = &counts->joint;
1249 for (i = 0; i < ARRAY_SIZE(counts_map->skip); i++)
1250 memcpy(counts_map->skip[i], counts->skip[i],
1251 sizeof(counts_map->skip[0]));
1252 counts_helper->skip = &counts_map->skip;
1254 for (i = 0; i < ARRAY_SIZE(counts_map->y_mode); i++)
1255 memcpy(counts_map->y_mode[i], counts->y_mode[i],
1256 sizeof(counts_map->y_mode[0]));
1257 counts_helper->y_mode = &counts_map->y_mode;
1259 for (i = 0; i < ARRAY_SIZE(counts_map->filter); i++)
1260 memcpy(counts_map->filter[i], counts->switchable_interp[i],
1261 sizeof(counts_map->filter[0]));
1262 counts_helper->filter = &counts_map->filter;
1264 for (i = 0; i < ARRAY_SIZE(counts_map->sign); i++)
1265 memcpy(counts_map->sign[i], counts->mvcomp[i].sign,
1266 sizeof(counts_map->sign[0]));
1267 counts_helper->sign = &counts_map->sign;
1269 for (i = 0; i < ARRAY_SIZE(counts_map->classes); i++)
1270 memcpy(counts_map->classes[i], counts->mvcomp[i].classes,
1271 sizeof(counts_map->classes[0]));
1272 counts_helper->classes = &counts_map->classes;
1274 for (i = 0; i < ARRAY_SIZE(counts_map->class0); i++)
1275 memcpy(counts_map->class0[i], counts->mvcomp[i].class0,
1276 sizeof(counts_map->class0[0]));
1277 counts_helper->class0 = &counts_map->class0;
1279 for (i = 0; i < ARRAY_SIZE(counts_map->bits); i++)
1280 for (j = 0; j < ARRAY_SIZE(counts_map->bits[0]); j++)
1281 memcpy(counts_map->bits[i][j], counts->mvcomp[i].bits[j],
1282 sizeof(counts_map->bits[0][0]));
1283 counts_helper->bits = &counts_map->bits;
1285 for (i = 0; i < ARRAY_SIZE(counts_map->class0_fp); i++)
1286 for (j = 0; j < ARRAY_SIZE(counts_map->class0_fp[0]); j++)
1287 memcpy(counts_map->class0_fp[i][j], counts->mvcomp[i].class0_fp[j],
1288 sizeof(counts_map->class0_fp[0][0]));
1289 counts_helper->class0_fp = &counts_map->class0_fp;
1291 for (i = 0; i < ARRAY_SIZE(counts_map->fp); i++)
1292 memcpy(counts_map->fp[i], counts->mvcomp[i].fp,
1293 sizeof(counts_map->fp[0]));
1294 counts_helper->fp = &counts_map->fp;
1296 for (i = 0; i < ARRAY_SIZE(counts_map->class0_hp); i++)
1297 memcpy(counts_map->class0_hp[i], counts->mvcomp[i].class0_hp,
1298 sizeof(counts_map->class0_hp[0]));
1299 counts_helper->class0_hp = &counts_map->class0_hp;
1301 for (i = 0; i < ARRAY_SIZE(counts_map->hp); i++)
1302 memcpy(counts_map->hp[i], counts->mvcomp[i].hp, sizeof(counts_map->hp[0]));
1304 counts_helper->hp = &counts_map->hp;
1306 for (i = 0; i < 4; i++)
1307 for (j = 0; j < 2; j++)
1308 for (k = 0; k < 2; k++)
1309 vdec_vp9_slice_map_counts_eob_coef(i, j, k, counts, counts_helper);
1312 static void vdec_vp9_slice_map_to_coef(unsigned int i, unsigned int j, unsigned int k,
1313 struct vdec_vp9_slice_frame_ctx *frame_ctx,
1314 struct v4l2_vp9_frame_context *frame_ctx_helper)
1318 for (l = 0; l < ARRAY_SIZE(frame_ctx_helper->coef[0][0][0]); l++) {
1319 for (m = 0; m < VP9_BAND_6(l); m++) {
1320 memcpy(frame_ctx_helper->coef[i][j][k][l][m],
1321 frame_ctx->coef_probs[i][j][k][l].probs[m],
1322 sizeof(frame_ctx_helper->coef[i][j][k][l][0]));
1327 static void vdec_vp9_slice_map_from_coef(unsigned int i, unsigned int j, unsigned int k,
1328 struct vdec_vp9_slice_frame_ctx *frame_ctx,
1329 struct v4l2_vp9_frame_context *frame_ctx_helper)
1333 for (l = 0; l < ARRAY_SIZE(frame_ctx_helper->coef[0][0][0]); l++) {
1334 for (m = 0; m < VP9_BAND_6(l); m++) {
1335 memcpy(frame_ctx->coef_probs[i][j][k][l].probs[m],
1336 frame_ctx_helper->coef[i][j][k][l][m],
1337 sizeof(frame_ctx_helper->coef[i][j][k][l][0]));
1343 void vdec_vp9_slice_framectx_map_helper(bool frame_is_intra,
1344 struct vdec_vp9_slice_frame_ctx *pre_frame_ctx,
1345 struct vdec_vp9_slice_frame_ctx *frame_ctx,
1346 struct v4l2_vp9_frame_context *frame_ctx_helper)
1348 struct v4l2_vp9_frame_mv_context *mv = &frame_ctx_helper->mv;
1351 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->coef); i++)
1352 for (j = 0; j < ARRAY_SIZE(frame_ctx_helper->coef[0]); j++)
1353 for (k = 0; k < ARRAY_SIZE(frame_ctx_helper->coef[0][0]); k++)
1354 vdec_vp9_slice_map_to_coef(i, j, k, pre_frame_ctx,
1358 * use previous prob when frame is not intra or
1359 * we should use the prob updated by the compressed header parse
1361 if (!frame_is_intra)
1362 frame_ctx = pre_frame_ctx;
1364 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx8); i++)
1365 memcpy(frame_ctx_helper->tx8[i], frame_ctx->tx_p8x8[i],
1366 sizeof(frame_ctx_helper->tx8[0]));
1368 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx16); i++)
1369 memcpy(frame_ctx_helper->tx16[i], frame_ctx->tx_p16x16[i],
1370 sizeof(frame_ctx_helper->tx16[0]));
1372 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx32); i++)
1373 memcpy(frame_ctx_helper->tx32[i], frame_ctx->tx_p32x32[i],
1374 sizeof(frame_ctx_helper->tx32[0]));
1376 memcpy(frame_ctx_helper->skip, frame_ctx->skip_probs, sizeof(frame_ctx_helper->skip));
1378 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->inter_mode); i++)
1379 memcpy(frame_ctx_helper->inter_mode[i], frame_ctx->inter_mode_probs[i],
1380 sizeof(frame_ctx_helper->inter_mode[0]));
1382 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->interp_filter); i++)
1383 memcpy(frame_ctx_helper->interp_filter[i], frame_ctx->switch_interp_prob[i],
1384 sizeof(frame_ctx_helper->interp_filter[0]));
1386 memcpy(frame_ctx_helper->is_inter, frame_ctx->intra_inter_prob,
1387 sizeof(frame_ctx_helper->is_inter));
1389 memcpy(frame_ctx_helper->comp_mode, frame_ctx->comp_inter_prob,
1390 sizeof(frame_ctx_helper->comp_mode));
1392 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->single_ref); i++)
1393 memcpy(frame_ctx_helper->single_ref[i], frame_ctx->single_ref_prob[i],
1394 sizeof(frame_ctx_helper->single_ref[0]));
1396 memcpy(frame_ctx_helper->comp_ref, frame_ctx->comp_ref_prob,
1397 sizeof(frame_ctx_helper->comp_ref));
1399 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->y_mode); i++)
1400 memcpy(frame_ctx_helper->y_mode[i], frame_ctx->y_mode_prob[i],
1401 sizeof(frame_ctx_helper->y_mode[0]));
1403 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->uv_mode); i++)
1404 memcpy(frame_ctx_helper->uv_mode[i], frame_ctx->uv_mode_prob[i],
1405 sizeof(frame_ctx_helper->uv_mode[0]));
1407 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->partition); i++)
1408 memcpy(frame_ctx_helper->partition[i], frame_ctx->partition_prob[i],
1409 sizeof(frame_ctx_helper->partition[0]));
1411 memcpy(mv->joint, frame_ctx->joint, sizeof(mv->joint));
1413 for (i = 0; i < ARRAY_SIZE(mv->sign); i++)
1414 mv->sign[i] = frame_ctx->sign_classes[i].sign;
1416 for (i = 0; i < ARRAY_SIZE(mv->classes); i++)
1417 memcpy(mv->classes[i], frame_ctx->sign_classes[i].classes,
1418 sizeof(mv->classes[i]));
1420 for (i = 0; i < ARRAY_SIZE(mv->class0_bit); i++)
1421 mv->class0_bit[i] = frame_ctx->class0_bits[i].class0[0];
1423 for (i = 0; i < ARRAY_SIZE(mv->bits); i++)
1424 memcpy(mv->bits[i], frame_ctx->class0_bits[i].bits, sizeof(mv->bits[0]));
1426 for (i = 0; i < ARRAY_SIZE(mv->class0_fr); i++)
1427 for (j = 0; j < ARRAY_SIZE(mv->class0_fr[0]); j++)
1428 memcpy(mv->class0_fr[i][j], frame_ctx->class0_fp_hp[i].class0_fp[j],
1429 sizeof(mv->class0_fr[0][0]));
1431 for (i = 0; i < ARRAY_SIZE(mv->fr); i++)
1432 memcpy(mv->fr[i], frame_ctx->class0_fp_hp[i].fp, sizeof(mv->fr[0]));
1434 for (i = 0; i < ARRAY_SIZE(mv->class0_hp); i++)
1435 mv->class0_hp[i] = frame_ctx->class0_fp_hp[i].class0_hp;
1437 for (i = 0; i < ARRAY_SIZE(mv->hp); i++)
1438 mv->hp[i] = frame_ctx->class0_fp_hp[i].hp;
1441 static void vdec_vp9_slice_helper_map_framectx(struct v4l2_vp9_frame_context *frame_ctx_helper,
1442 struct vdec_vp9_slice_frame_ctx *frame_ctx)
1444 struct v4l2_vp9_frame_mv_context *mv = &frame_ctx_helper->mv;
1447 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx8); i++)
1448 memcpy(frame_ctx->tx_p8x8[i], frame_ctx_helper->tx8[i],
1449 sizeof(frame_ctx_helper->tx8[0]));
1451 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx16); i++)
1452 memcpy(frame_ctx->tx_p16x16[i], frame_ctx_helper->tx16[i],
1453 sizeof(frame_ctx_helper->tx16[0]));
1455 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->tx32); i++)
1456 memcpy(frame_ctx->tx_p32x32[i], frame_ctx_helper->tx32[i],
1457 sizeof(frame_ctx_helper->tx32[0]));
1459 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->coef); i++)
1460 for (j = 0; j < ARRAY_SIZE(frame_ctx_helper->coef[0]); j++)
1461 for (k = 0; k < ARRAY_SIZE(frame_ctx_helper->coef[0][0]); k++)
1462 vdec_vp9_slice_map_from_coef(i, j, k, frame_ctx,
1465 memcpy(frame_ctx->skip_probs, frame_ctx_helper->skip, sizeof(frame_ctx_helper->skip));
1467 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->inter_mode); i++)
1468 memcpy(frame_ctx->inter_mode_probs[i], frame_ctx_helper->inter_mode[i],
1469 sizeof(frame_ctx_helper->inter_mode[0]));
1471 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->interp_filter); i++)
1472 memcpy(frame_ctx->switch_interp_prob[i], frame_ctx_helper->interp_filter[i],
1473 sizeof(frame_ctx_helper->interp_filter[0]));
1475 memcpy(frame_ctx->intra_inter_prob, frame_ctx_helper->is_inter,
1476 sizeof(frame_ctx_helper->is_inter));
1478 memcpy(frame_ctx->comp_inter_prob, frame_ctx_helper->comp_mode,
1479 sizeof(frame_ctx_helper->comp_mode));
1481 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->single_ref); i++)
1482 memcpy(frame_ctx->single_ref_prob[i], frame_ctx_helper->single_ref[i],
1483 sizeof(frame_ctx_helper->single_ref[0]));
1485 memcpy(frame_ctx->comp_ref_prob, frame_ctx_helper->comp_ref,
1486 sizeof(frame_ctx_helper->comp_ref));
1488 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->y_mode); i++)
1489 memcpy(frame_ctx->y_mode_prob[i], frame_ctx_helper->y_mode[i],
1490 sizeof(frame_ctx_helper->y_mode[0]));
1492 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->uv_mode); i++)
1493 memcpy(frame_ctx->uv_mode_prob[i], frame_ctx_helper->uv_mode[i],
1494 sizeof(frame_ctx_helper->uv_mode[0]));
1496 for (i = 0; i < ARRAY_SIZE(frame_ctx_helper->partition); i++)
1497 memcpy(frame_ctx->partition_prob[i], frame_ctx_helper->partition[i],
1498 sizeof(frame_ctx_helper->partition[0]));
1500 memcpy(frame_ctx->joint, mv->joint, sizeof(mv->joint));
1502 for (i = 0; i < ARRAY_SIZE(mv->sign); i++)
1503 frame_ctx->sign_classes[i].sign = mv->sign[i];
1505 for (i = 0; i < ARRAY_SIZE(mv->classes); i++)
1506 memcpy(frame_ctx->sign_classes[i].classes, mv->classes[i],
1507 sizeof(mv->classes[i]));
1509 for (i = 0; i < ARRAY_SIZE(mv->class0_bit); i++)
1510 frame_ctx->class0_bits[i].class0[0] = mv->class0_bit[i];
1512 for (i = 0; i < ARRAY_SIZE(mv->bits); i++)
1513 memcpy(frame_ctx->class0_bits[i].bits, mv->bits[i], sizeof(mv->bits[0]));
1515 for (i = 0; i < ARRAY_SIZE(mv->class0_fr); i++)
1516 for (j = 0; j < ARRAY_SIZE(mv->class0_fr[0]); j++)
1517 memcpy(frame_ctx->class0_fp_hp[i].class0_fp[j], mv->class0_fr[i][j],
1518 sizeof(mv->class0_fr[0][0]));
1520 for (i = 0; i < ARRAY_SIZE(mv->fr); i++)
1521 memcpy(frame_ctx->class0_fp_hp[i].fp, mv->fr[i], sizeof(mv->fr[0]));
1523 for (i = 0; i < ARRAY_SIZE(mv->class0_hp); i++)
1524 frame_ctx->class0_fp_hp[i].class0_hp = mv->class0_hp[i];
1526 for (i = 0; i < ARRAY_SIZE(mv->hp); i++)
1527 frame_ctx->class0_fp_hp[i].hp = mv->hp[i];
1530 static int vdec_vp9_slice_update_prob(struct vdec_vp9_slice_instance *instance,
1531 struct vdec_vp9_slice_vsi *vsi)
1533 struct vdec_vp9_slice_frame_ctx *pre_frame_ctx;
1534 struct v4l2_vp9_frame_context *pre_frame_ctx_helper;
1535 struct vdec_vp9_slice_frame_ctx *frame_ctx;
1536 struct vdec_vp9_slice_frame_counts *counts;
1537 struct v4l2_vp9_frame_symbol_counts *counts_helper;
1538 struct vdec_vp9_slice_uncompressed_header *uh;
1539 bool frame_is_intra;
1542 uh = &vsi->frame.uh;
1543 pre_frame_ctx = &instance->frame_ctx[uh->frame_context_idx];
1544 pre_frame_ctx_helper = &instance->frame_ctx_helper;
1545 frame_ctx = (struct vdec_vp9_slice_frame_ctx *)instance->prob.va;
1546 counts = (struct vdec_vp9_slice_frame_counts *)instance->counts.va;
1547 counts_helper = &instance->counts_helper;
1549 if (!uh->refresh_frame_context)
1552 if (!uh->frame_parallel_decoding_mode) {
1553 vdec_vp9_slice_counts_map_helper(&instance->counts_map, counts, counts_helper);
1555 frame_is_intra = !vsi->frame.uh.frame_type || vsi->frame.uh.intra_only;
1556 /* check default prob */
1557 if (!instance->dirty[uh->frame_context_idx])
1558 vdec_vp9_slice_framectx_map_helper(frame_is_intra,
1559 vdec_vp9_slice_default_frame_ctx,
1561 pre_frame_ctx_helper);
1563 vdec_vp9_slice_framectx_map_helper(frame_is_intra,
1566 pre_frame_ctx_helper);
1568 use_128 = !frame_is_intra && !vsi->frame.uh.last_frame_type;
1569 v4l2_vp9_adapt_coef_probs(pre_frame_ctx_helper,
1573 if (!frame_is_intra)
1574 v4l2_vp9_adapt_noncoef_probs(pre_frame_ctx_helper,
1576 V4L2_VP9_REFERENCE_MODE_SINGLE_REFERENCE,
1577 vsi->frame.uh.interpolation_filter,
1578 vsi->frame.ch.tx_mode,
1579 vsi->frame.uh.allow_high_precision_mv ?
1580 V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV : 0);
1581 vdec_vp9_slice_helper_map_framectx(pre_frame_ctx_helper, pre_frame_ctx);
1583 memcpy(pre_frame_ctx, frame_ctx, sizeof(*frame_ctx));
1586 instance->dirty[uh->frame_context_idx] = 1;
1591 static int vdec_vp9_slice_update_single(struct vdec_vp9_slice_instance *instance,
1592 struct vdec_vp9_slice_pfc *pfc)
1594 struct vdec_vp9_slice_vsi *vsi;
1597 memcpy(&pfc->state[0], &vsi->state, sizeof(vsi->state));
1599 mtk_vcodec_debug(instance, "Frame %u Y_CRC %08x %08x %08x %08x\n",
1601 vsi->state.crc[0], vsi->state.crc[1],
1602 vsi->state.crc[2], vsi->state.crc[3]);
1603 mtk_vcodec_debug(instance, "Frame %u C_CRC %08x %08x %08x %08x\n",
1605 vsi->state.crc[4], vsi->state.crc[5],
1606 vsi->state.crc[6], vsi->state.crc[7]);
1608 vdec_vp9_slice_update_prob(instance, vsi);
1610 instance->width = vsi->frame.uh.frame_width;
1611 instance->height = vsi->frame.uh.frame_height;
1612 instance->frame_type = vsi->frame.uh.frame_type;
1613 instance->show_frame = vsi->frame.uh.show_frame;
1618 static int vdec_vp9_slice_update_lat(struct vdec_vp9_slice_instance *instance,
1619 struct vdec_lat_buf *lat_buf,
1620 struct vdec_vp9_slice_pfc *pfc)
1622 struct vdec_vp9_slice_vsi *vsi;
1625 memcpy(&pfc->state[0], &vsi->state, sizeof(vsi->state));
1627 mtk_vcodec_debug(instance, "Frame %u LAT CRC 0x%08x %lx %lx\n",
1628 pfc->seq, vsi->state.crc[0],
1629 (unsigned long)vsi->trans.dma_addr,
1630 (unsigned long)vsi->trans.dma_addr_end);
1632 /* buffer full, need to re-decode */
1633 if (vsi->state.full) {
1634 /* buffer not enough */
1635 if (vsi->trans.dma_addr_end - vsi->trans.dma_addr ==
1641 vdec_vp9_slice_update_prob(instance, vsi);
1643 instance->width = vsi->frame.uh.frame_width;
1644 instance->height = vsi->frame.uh.frame_height;
1645 instance->frame_type = vsi->frame.uh.frame_type;
1646 instance->show_frame = vsi->frame.uh.show_frame;
1651 static int vdec_vp9_slice_setup_core_to_dst_buf(struct vdec_vp9_slice_instance *instance,
1652 struct vdec_lat_buf *lat_buf)
1654 struct vb2_v4l2_buffer *dst;
1656 dst = v4l2_m2m_next_dst_buf(instance->ctx->m2m_ctx);
1660 v4l2_m2m_buf_copy_metadata(&lat_buf->ts_info, dst, true);
1664 static int vdec_vp9_slice_setup_core_buffer(struct vdec_vp9_slice_instance *instance,
1665 struct vdec_vp9_slice_pfc *pfc,
1666 struct vdec_vp9_slice_vsi *vsi,
1668 struct vdec_lat_buf *lat_buf)
1670 struct vb2_buffer *vb;
1671 struct vb2_queue *vq;
1672 struct vdec_vp9_slice_reference *ref;
1679 plane = instance->ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes;
1680 w = vsi->frame.uh.frame_width;
1681 h = vsi->frame.uh.frame_height;
1682 size = ALIGN(w, 64) * ALIGN(h, 64);
1685 vsi->fb.y.dma_addr = fb->base_y.dma_addr;
1687 vsi->fb.c.dma_addr = fb->base_y.dma_addr + size;
1689 vsi->fb.c.dma_addr = fb->base_c.dma_addr;
1691 /* reference buffers */
1692 vq = v4l2_m2m_get_vq(instance->ctx->m2m_ctx,
1693 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
1697 /* get current output buffer */
1698 vb = &v4l2_m2m_next_dst_buf(instance->ctx->m2m_ctx)->vb2_buf;
1702 /* update internal buffer's width/height */
1703 for (i = 0; i < vq->num_buffers; i++) {
1704 if (vb == vq->bufs[i]) {
1705 instance->dpb[i].width = w;
1706 instance->dpb[i].height = h;
1712 * get buffer's width/height from instance
1713 * get buffer address from vb2buf
1715 for (i = 0; i < 3; i++) {
1716 ref = &vsi->frame.ref[i];
1717 vb = vb2_find_buffer(vq, pfc->ref_idx[i]);
1719 ref->frame_width = w;
1720 ref->frame_height = h;
1721 memset(&vsi->ref[i], 0, sizeof(vsi->ref[i]));
1723 int idx = vb->index;
1725 ref->frame_width = instance->dpb[idx].width;
1726 ref->frame_height = instance->dpb[idx].height;
1727 vsi->ref[i].y.dma_addr =
1728 vb2_dma_contig_plane_dma_addr(vb, 0);
1730 vsi->ref[i].c.dma_addr =
1731 vsi->ref[i].y.dma_addr + size;
1733 vsi->ref[i].c.dma_addr =
1734 vb2_dma_contig_plane_dma_addr(vb, 1);
1741 static void vdec_vp9_slice_setup_single_buffer(struct vdec_vp9_slice_instance *instance,
1742 struct vdec_vp9_slice_pfc *pfc,
1743 struct vdec_vp9_slice_vsi *vsi,
1744 struct mtk_vcodec_mem *bs,
1749 vsi->bs.buf.dma_addr = bs->dma_addr;
1750 vsi->bs.buf.size = bs->size;
1751 vsi->bs.frame.dma_addr = bs->dma_addr;
1752 vsi->bs.frame.size = bs->size;
1754 for (i = 0; i < 2; i++) {
1755 vsi->mv[i].dma_addr = instance->mv[i].dma_addr;
1756 vsi->mv[i].size = instance->mv[i].size;
1758 for (i = 0; i < 2; i++) {
1759 vsi->seg[i].dma_addr = instance->seg[i].dma_addr;
1760 vsi->seg[i].size = instance->seg[i].size;
1762 vsi->tile.dma_addr = instance->tile.dma_addr;
1763 vsi->tile.size = instance->tile.size;
1764 vsi->prob.dma_addr = instance->prob.dma_addr;
1765 vsi->prob.size = instance->prob.size;
1766 vsi->counts.dma_addr = instance->counts.dma_addr;
1767 vsi->counts.size = instance->counts.size;
1769 vsi->row_info.buf = 0;
1770 vsi->row_info.size = 0;
1772 vdec_vp9_slice_setup_core_buffer(instance, pfc, vsi, fb, NULL);
1775 static int vdec_vp9_slice_setup_core(struct vdec_vp9_slice_instance *instance,
1777 struct vdec_lat_buf *lat_buf,
1778 struct vdec_vp9_slice_pfc *pfc)
1780 struct vdec_vp9_slice_vsi *vsi = &pfc->vsi;
1783 vdec_vp9_slice_setup_state(vsi);
1785 ret = vdec_vp9_slice_setup_core_to_dst_buf(instance, lat_buf);
1789 ret = vdec_vp9_slice_setup_core_buffer(instance, pfc, vsi, fb, lat_buf);
1793 vdec_vp9_slice_setup_seg_buffer(instance, vsi, &instance->seg[1]);
1801 static int vdec_vp9_slice_setup_single(struct vdec_vp9_slice_instance *instance,
1802 struct mtk_vcodec_mem *bs,
1804 struct vdec_vp9_slice_pfc *pfc)
1806 struct vdec_vp9_slice_vsi *vsi = &pfc->vsi;
1809 ret = vdec_vp9_slice_setup_single_from_src_to_dst(instance);
1813 ret = vdec_vp9_slice_setup_pfc(instance, pfc);
1817 ret = vdec_vp9_slice_alloc_working_buffer(instance, vsi);
1821 vdec_vp9_slice_setup_single_buffer(instance, pfc, vsi, bs, fb);
1822 vdec_vp9_slice_setup_seg_buffer(instance, vsi, &instance->seg[0]);
1824 ret = vdec_vp9_slice_setup_prob_buffer(instance, vsi);
1828 ret = vdec_vp9_slice_setup_tile_buffer(instance, vsi, bs);
1838 static int vdec_vp9_slice_update_core(struct vdec_vp9_slice_instance *instance,
1839 struct vdec_lat_buf *lat_buf,
1840 struct vdec_vp9_slice_pfc *pfc)
1842 struct vdec_vp9_slice_vsi *vsi;
1845 memcpy(&pfc->state[1], &vsi->state, sizeof(vsi->state));
1847 mtk_vcodec_debug(instance, "Frame %u Y_CRC %08x %08x %08x %08x\n",
1849 vsi->state.crc[0], vsi->state.crc[1],
1850 vsi->state.crc[2], vsi->state.crc[3]);
1851 mtk_vcodec_debug(instance, "Frame %u C_CRC %08x %08x %08x %08x\n",
1853 vsi->state.crc[4], vsi->state.crc[5],
1854 vsi->state.crc[6], vsi->state.crc[7]);
1859 static int vdec_vp9_slice_init(struct mtk_vcodec_ctx *ctx)
1861 struct vdec_vp9_slice_instance *instance;
1862 struct vdec_vp9_slice_init_vsi *vsi;
1865 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
1869 instance->ctx = ctx;
1870 instance->vpu.id = SCP_IPI_VDEC_LAT;
1871 instance->vpu.core_id = SCP_IPI_VDEC_CORE;
1872 instance->vpu.ctx = ctx;
1873 instance->vpu.codec_type = ctx->current_codec;
1875 ret = vpu_dec_init(&instance->vpu);
1877 mtk_vcodec_err(instance, "failed to init vpu dec, ret %d\n", ret);
1878 goto error_vpu_init;
1881 /* init vsi and global flags */
1883 vsi = instance->vpu.vsi;
1885 mtk_vcodec_err(instance, "failed to get VP9 vsi\n");
1889 instance->init_vsi = vsi;
1890 instance->core_vsi = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
1891 (u32)vsi->core_vsi);
1892 if (!instance->core_vsi) {
1893 mtk_vcodec_err(instance, "failed to get VP9 core vsi\n");
1900 ret = vdec_vp9_slice_init_default_frame_ctx(instance);
1902 goto error_default_frame_ctx;
1904 ctx->drv_handle = instance;
1908 error_default_frame_ctx:
1910 vpu_dec_deinit(&instance->vpu);
1916 static void vdec_vp9_slice_deinit(void *h_vdec)
1918 struct vdec_vp9_slice_instance *instance = h_vdec;
1923 vpu_dec_deinit(&instance->vpu);
1924 vdec_vp9_slice_free_working_buffer(instance);
1925 vdec_msg_queue_deinit(&instance->ctx->msg_queue, instance->ctx);
1929 static int vdec_vp9_slice_flush(void *h_vdec, struct mtk_vcodec_mem *bs,
1930 struct vdec_fb *fb, bool *res_chg)
1932 struct vdec_vp9_slice_instance *instance = h_vdec;
1934 mtk_vcodec_debug(instance, "flush ...\n");
1935 if (instance->ctx->dev->vdec_pdata->hw_arch != MTK_VDEC_PURE_SINGLE_CORE)
1936 vdec_msg_queue_wait_lat_buf_full(&instance->ctx->msg_queue);
1937 return vpu_dec_reset(&instance->vpu);
1940 static void vdec_vp9_slice_get_pic_info(struct vdec_vp9_slice_instance *instance)
1942 struct mtk_vcodec_ctx *ctx = instance->ctx;
1943 unsigned int data[3];
1945 mtk_vcodec_debug(instance, "w %u h %u\n",
1946 ctx->picinfo.pic_w, ctx->picinfo.pic_h);
1948 data[0] = ctx->picinfo.pic_w;
1949 data[1] = ctx->picinfo.pic_h;
1950 data[2] = ctx->capture_fourcc;
1951 vpu_dec_get_param(&instance->vpu, data, 3, GET_PARAM_PIC_INFO);
1953 ctx->picinfo.buf_w = ALIGN(ctx->picinfo.pic_w, 64);
1954 ctx->picinfo.buf_h = ALIGN(ctx->picinfo.pic_h, 64);
1955 ctx->picinfo.fb_sz[0] = instance->vpu.fb_sz[0];
1956 ctx->picinfo.fb_sz[1] = instance->vpu.fb_sz[1];
1959 static void vdec_vp9_slice_get_dpb_size(struct vdec_vp9_slice_instance *instance,
1960 unsigned int *dpb_sz)
1962 /* refer VP9 specification */
1966 static int vdec_vp9_slice_get_param(void *h_vdec, enum vdec_get_param_type type, void *out)
1968 struct vdec_vp9_slice_instance *instance = h_vdec;
1971 case GET_PARAM_PIC_INFO:
1972 vdec_vp9_slice_get_pic_info(instance);
1974 case GET_PARAM_DPB_SIZE:
1975 vdec_vp9_slice_get_dpb_size(instance, out);
1977 case GET_PARAM_CROP_INFO:
1978 mtk_vcodec_debug(instance, "No need to get vp9 crop information.");
1981 mtk_vcodec_err(instance, "invalid get parameter type=%d\n",
1989 static int vdec_vp9_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
1990 struct vdec_fb *fb, bool *res_chg)
1992 struct vdec_vp9_slice_instance *instance = h_vdec;
1993 struct vdec_vp9_slice_pfc *pfc = &instance->sc_pfc;
1994 struct vdec_vp9_slice_vsi *vsi;
1995 struct mtk_vcodec_ctx *ctx;
1998 if (!instance || !instance->ctx)
2000 ctx = instance->ctx;
2002 /* bs NULL means flush decoder */
2004 return vdec_vp9_slice_flush(h_vdec, bs, fb, res_chg);
2006 fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
2012 ret = vdec_vp9_slice_setup_single(instance, bs, fb, pfc);
2014 mtk_vcodec_err(instance, "Failed to setup VP9 single ret %d\n", ret);
2017 vdec_vp9_slice_vsi_to_remote(vsi, instance->vsi);
2019 ret = vpu_dec_start(&instance->vpu, NULL, 0);
2021 mtk_vcodec_err(instance, "Failed to dec VP9 ret %d\n", ret);
2025 ret = mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
2026 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
2027 /* update remote vsi if decode timeout */
2029 mtk_vcodec_err(instance, "VP9 decode timeout %d\n", ret);
2030 WRITE_ONCE(instance->vsi->state.timeout, 1);
2033 vpu_dec_end(&instance->vpu);
2035 vdec_vp9_slice_vsi_from_remote(vsi, instance->vsi, 0);
2036 ret = vdec_vp9_slice_update_single(instance, pfc);
2038 mtk_vcodec_err(instance, "VP9 decode error: %d\n", ret);
2042 instance->ctx->decoded_frame_cnt++;
2046 static int vdec_vp9_slice_lat_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
2047 struct vdec_fb *fb, bool *res_chg)
2049 struct vdec_vp9_slice_instance *instance = h_vdec;
2050 struct vdec_lat_buf *lat_buf;
2051 struct vdec_vp9_slice_pfc *pfc;
2052 struct vdec_vp9_slice_vsi *vsi;
2053 struct mtk_vcodec_ctx *ctx;
2056 if (!instance || !instance->ctx)
2058 ctx = instance->ctx;
2060 /* init msgQ for the first time */
2061 if (vdec_msg_queue_init(&ctx->msg_queue, ctx,
2062 vdec_vp9_slice_core_decode,
2066 /* bs NULL means flush decoder */
2068 return vdec_vp9_slice_flush(h_vdec, bs, fb, res_chg);
2070 lat_buf = vdec_msg_queue_dqbuf(&instance->ctx->msg_queue.lat_ctx);
2072 mtk_vcodec_err(instance, "Failed to get VP9 lat buf\n");
2075 pfc = (struct vdec_vp9_slice_pfc *)lat_buf->private_data;
2080 ret = vdec_vp9_slice_setup_lat(instance, bs, lat_buf, pfc);
2082 mtk_vcodec_err(instance, "Failed to setup VP9 lat ret %d\n", ret);
2085 vdec_vp9_slice_vsi_to_remote(vsi, instance->vsi);
2087 ret = vpu_dec_start(&instance->vpu, NULL, 0);
2089 mtk_vcodec_err(instance, "Failed to dec VP9 ret %d\n", ret);
2093 if (instance->irq) {
2094 ret = mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
2095 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_LAT0);
2096 /* update remote vsi if decode timeout */
2098 mtk_vcodec_err(instance, "VP9 decode timeout %d pic %d\n", ret, pfc->seq);
2099 WRITE_ONCE(instance->vsi->state.timeout, 1);
2101 vpu_dec_end(&instance->vpu);
2104 vdec_vp9_slice_vsi_from_remote(vsi, instance->vsi, 0);
2105 ret = vdec_vp9_slice_update_lat(instance, lat_buf, pfc);
2107 /* LAT trans full, no more UBE or decode timeout */
2109 mtk_vcodec_err(instance, "VP9 decode error: %d\n", ret);
2113 mtk_vcodec_debug(instance, "lat dma addr: 0x%lx 0x%lx\n",
2114 (unsigned long)pfc->vsi.trans.dma_addr,
2115 (unsigned long)pfc->vsi.trans.dma_addr_end);
2117 vdec_msg_queue_update_ube_wptr(&ctx->msg_queue,
2118 vsi->trans.dma_addr_end +
2119 ctx->msg_queue.wdma_addr.dma_addr);
2120 vdec_msg_queue_qbuf(&ctx->dev->msg_queue_core_ctx, lat_buf);
2125 static int vdec_vp9_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
2126 struct vdec_fb *fb, bool *res_chg)
2128 struct vdec_vp9_slice_instance *instance = h_vdec;
2131 if (instance->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
2132 ret = vdec_vp9_slice_single_decode(h_vdec, bs, fb, res_chg);
2134 ret = vdec_vp9_slice_lat_decode(h_vdec, bs, fb, res_chg);
2139 static int vdec_vp9_slice_core_decode(struct vdec_lat_buf *lat_buf)
2141 struct vdec_vp9_slice_instance *instance;
2142 struct vdec_vp9_slice_pfc *pfc;
2143 struct mtk_vcodec_ctx *ctx = NULL;
2144 struct vdec_fb *fb = NULL;
2150 pfc = lat_buf->private_data;
2155 instance = ctx->drv_handle;
2159 fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
2165 ret = vdec_vp9_slice_setup_core(instance, fb, lat_buf, pfc);
2167 mtk_vcodec_err(instance, "vdec_vp9_slice_setup_core\n");
2170 vdec_vp9_slice_vsi_to_remote(&pfc->vsi, instance->core_vsi);
2172 ret = vpu_dec_core(&instance->vpu);
2174 mtk_vcodec_err(instance, "vpu_dec_core\n");
2178 if (instance->irq) {
2179 ret = mtk_vcodec_wait_for_done_ctx(ctx, MTK_INST_IRQ_RECEIVED,
2180 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
2181 /* update remote vsi if decode timeout */
2183 mtk_vcodec_err(instance, "VP9 core timeout pic %d\n", pfc->seq);
2184 WRITE_ONCE(instance->core_vsi->state.timeout, 1);
2186 vpu_dec_core_end(&instance->vpu);
2189 vdec_vp9_slice_vsi_from_remote(&pfc->vsi, instance->core_vsi, 1);
2190 ret = vdec_vp9_slice_update_core(instance, lat_buf, pfc);
2192 mtk_vcodec_err(instance, "vdec_vp9_slice_update_core\n");
2196 pfc->vsi.trans.dma_addr_end += ctx->msg_queue.wdma_addr.dma_addr;
2197 mtk_vcodec_debug(instance, "core dma_addr_end 0x%lx\n",
2198 (unsigned long)pfc->vsi.trans.dma_addr_end);
2199 vdec_msg_queue_update_ube_rptr(&ctx->msg_queue, pfc->vsi.trans.dma_addr_end);
2200 ctx->dev->vdec_pdata->cap_to_disp(ctx, 0, lat_buf->src_buf_req);
2206 /* always update read pointer */
2207 vdec_msg_queue_update_ube_rptr(&ctx->msg_queue, pfc->vsi.trans.dma_addr_end);
2210 ctx->dev->vdec_pdata->cap_to_disp(ctx, 1, lat_buf->src_buf_req);
2215 const struct vdec_common_if vdec_vp9_slice_lat_if = {
2216 .init = vdec_vp9_slice_init,
2217 .decode = vdec_vp9_slice_decode,
2218 .get_param = vdec_vp9_slice_get_param,
2219 .deinit = vdec_vp9_slice_deinit,