1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
5 * Kai-Sean Yang <kai-sean.yang@mediatek.com>
6 * Tiffany Lin <tiffany.lin@mediatek.com>
10 #include <linux/slab.h>
11 #include <linux/syscalls.h>
12 #include <linux/delay.h>
13 #include <linux/time.h>
15 #include "../mtk_vcodec_intr.h"
16 #include "../vdec_drv_base.h"
17 #include "../vdec_vpu_if.h"
19 #define VP9_SUPER_FRAME_BS_SZ 64
20 #define MAX_VP9_DPB_SIZE 9
22 #define REFS_PER_FRAME 3
23 #define MAX_NUM_REF_FRAMES 8
24 #define VP9_MAX_FRM_BUF_NUM 9
25 #define VP9_MAX_FRM_BUF_NODE_NUM (VP9_MAX_FRM_BUF_NUM * 2)
26 #define VP9_SEG_ID_SZ 0x12000
29 * struct vp9_dram_buf - contains buffer info for vpu
33 * @padding : for 64 bytes alignment
43 * struct vp9_fb_info - contains frame buffer info
45 * @reserved : reserved field used by vpu
49 unsigned int reserved[32];
53 * struct vp9_ref_cnt_buf - contains reference buffer information
54 * @buf : referenced frame buffer
55 * @ref_cnt : referenced frame buffer's reference count.
56 * When reference count=0, remove it from reference list
58 struct vp9_ref_cnt_buf {
59 struct vp9_fb_info buf;
64 * struct vp9_fb_info - contains current frame's reference buffer information
65 * @buf : reference buffer
66 * @idx : reference buffer index to frm_bufs
67 * @reserved : reserved field used by vpu
70 struct vp9_fb_info *buf;
72 unsigned int reserved[6];
76 * struct vp9_fb_info - contains frame buffer info
77 * @fb : super frame reference frame buffer
78 * @used : this reference frame info entry is used
79 * @padding : for 64 bytes size align
81 struct vp9_sf_ref_fb {
88 * struct vdec_vp9_vsi - shared buffer between host and VPU firmware
89 * AP-W/R : AP is writer/reader on this item
90 * VPU-W/R: VPU is write/reader on this item
91 * @sf_bs_buf : super frame backup buffer (AP-W, VPU-R)
92 * @sf_ref_fb : record supoer frame reference buffer information
94 * @sf_next_ref_fb_idx : next available super frame (AP-W, VPU-R)
95 * @sf_frm_cnt : super frame count, filled by vpu (AP-R, VPU-W)
96 * @sf_frm_offset : super frame offset, filled by vpu (AP-R, VPU-W)
97 * @sf_frm_sz : super frame size, filled by vpu (AP-R, VPU-W)
98 * @sf_frm_idx : current super frame (AP-R, VPU-W)
99 * @sf_init : inform super frame info already parsed by vpu (AP-R, VPU-W)
100 * @fb : capture buffer (AP-W, VPU-R)
101 * @bs : bs buffer (AP-W, VPU-R)
102 * @cur_fb : current show capture buffer (AP-R/W, VPU-R/W)
103 * @pic_w : picture width (AP-R, VPU-W)
104 * @pic_h : picture height (AP-R, VPU-W)
105 * @buf_w : codec width (AP-R, VPU-W)
106 * @buf_h : coded height (AP-R, VPU-W)
107 * @buf_sz_y_bs : ufo compressed y plane size (AP-R, VPU-W)
108 * @buf_sz_c_bs : ufo compressed cbcr plane size (AP-R, VPU-W)
109 * @buf_len_sz_y : size used to store y plane ufo info (AP-R, VPU-W)
110 * @buf_len_sz_c : size used to store cbcr plane ufo info (AP-R, VPU-W)
112 * @profile : profile sparsed from vpu (AP-R, VPU-W)
113 * @show_frame : display this frame or not (AP-R, VPU-W)
114 * @show_existing_frame : inform this frame is show existing frame
116 * @frm_to_show_idx : index to show frame (AP-R, VPU-W)
118 * @refresh_frm_flags : indicate when frame need to refine reference count
120 * @resolution_changed : resolution change in this frame (AP-R, VPU-W)
122 * @frm_bufs : maintain reference buffer info (AP-R/W, VPU-R/W)
123 * @ref_frm_map : maintain reference buffer map info (AP-R/W, VPU-R/W)
124 * @new_fb_idx : index to frm_bufs array (AP-R, VPU-W)
125 * @frm_num : decoded frame number, include sub-frame count (AP-R, VPU-W)
126 * @mv_buf : motion vector working buffer (AP-W, VPU-R)
127 * @frm_refs : maintain three reference buffer info (AP-R/W, VPU-R/W)
128 * @seg_id_buf : segmentation map working buffer (AP-W, VPU-R)
130 struct vdec_vp9_vsi {
131 unsigned char sf_bs_buf[VP9_SUPER_FRAME_BS_SZ];
132 struct vp9_sf_ref_fb sf_ref_fb[VP9_MAX_FRM_BUF_NUM-1];
133 int sf_next_ref_fb_idx;
134 unsigned int sf_frm_cnt;
135 unsigned int sf_frm_offset[VP9_MAX_FRM_BUF_NUM-1];
136 unsigned int sf_frm_sz[VP9_MAX_FRM_BUF_NUM-1];
137 unsigned int sf_frm_idx;
138 unsigned int sf_init;
140 struct mtk_vcodec_mem bs;
141 struct vdec_fb cur_fb;
146 unsigned int buf_sz_y_bs;
147 unsigned int buf_sz_c_bs;
148 unsigned int buf_len_sz_y;
149 unsigned int buf_len_sz_c;
150 unsigned int profile;
151 unsigned int show_frame;
152 unsigned int show_existing_frame;
153 unsigned int frm_to_show_idx;
154 unsigned int refresh_frm_flags;
155 unsigned int resolution_changed;
157 struct vp9_ref_cnt_buf frm_bufs[VP9_MAX_FRM_BUF_NUM];
158 int ref_frm_map[MAX_NUM_REF_FRAMES];
159 unsigned int new_fb_idx;
160 unsigned int frm_num;
161 struct vp9_dram_buf mv_buf;
163 struct vp9_ref_buf frm_refs[REFS_PER_FRAME];
164 struct vp9_dram_buf seg_id_buf;
169 * struct vdec_vp9_inst - vp9 decode instance
170 * @mv_buf : working buffer for mv
171 * @seg_id_buf : working buffer for segmentation map
172 * @dec_fb : vdec_fb node to link fb to different fb_xxx_list
173 * @available_fb_node_list : current available vdec_fb node
174 * @fb_use_list : current used or referenced vdec_fb
175 * @fb_free_list : current available to free vdec_fb
176 * @fb_disp_list : current available to display vdec_fb
177 * @cur_fb : current frame buffer
178 * @ctx : current decode context
179 * @vpu : vpu instance information
180 * @vsi : shared buffer between host and VPU firmware
181 * @total_frm_cnt : total frame count, it do not include sub-frames in super
183 * @mem : instance memory information
185 struct vdec_vp9_inst {
186 struct mtk_vcodec_mem mv_buf;
187 struct mtk_vcodec_mem seg_id_buf;
189 struct vdec_fb_node dec_fb[VP9_MAX_FRM_BUF_NODE_NUM];
190 struct list_head available_fb_node_list;
191 struct list_head fb_use_list;
192 struct list_head fb_free_list;
193 struct list_head fb_disp_list;
194 struct vdec_fb *cur_fb;
195 struct mtk_vcodec_ctx *ctx;
196 struct vdec_vpu_inst vpu;
197 struct vdec_vp9_vsi *vsi;
198 unsigned int total_frm_cnt;
199 struct mtk_vcodec_mem mem;
202 static bool vp9_is_sf_ref_fb(struct vdec_vp9_inst *inst, struct vdec_fb *fb)
205 struct vdec_vp9_vsi *vsi = inst->vsi;
207 for (i = 0; i < ARRAY_SIZE(vsi->sf_ref_fb); i++) {
208 if (fb == &vsi->sf_ref_fb[i].fb)
214 static struct vdec_fb *vp9_rm_from_fb_use_list(struct vdec_vp9_inst
217 struct vdec_fb *fb = NULL;
218 struct vdec_fb_node *node;
220 list_for_each_entry(node, &inst->fb_use_list, list) {
221 fb = (struct vdec_fb *)node->fb;
222 if (fb->base_y.va == addr) {
223 list_move_tail(&node->list,
224 &inst->available_fb_node_list);
231 static void vp9_add_to_fb_free_list(struct vdec_vp9_inst *inst,
234 struct vdec_fb_node *node;
237 node = list_first_entry_or_null(&inst->available_fb_node_list,
238 struct vdec_fb_node, list);
242 list_move_tail(&node->list, &inst->fb_free_list);
245 mtk_vcodec_debug(inst, "No free fb node");
249 static void vp9_free_sf_ref_fb(struct vdec_fb *fb)
251 struct vp9_sf_ref_fb *sf_ref_fb =
252 container_of(fb, struct vp9_sf_ref_fb, fb);
257 static void vp9_ref_cnt_fb(struct vdec_vp9_inst *inst, int *idx,
260 struct vdec_vp9_vsi *vsi = inst->vsi;
263 if (ref_idx >= 0 && vsi->frm_bufs[ref_idx].ref_cnt > 0) {
264 vsi->frm_bufs[ref_idx].ref_cnt--;
266 if (vsi->frm_bufs[ref_idx].ref_cnt == 0) {
267 if (!vp9_is_sf_ref_fb(inst,
268 vsi->frm_bufs[ref_idx].buf.fb)) {
271 fb = vp9_rm_from_fb_use_list(inst,
272 vsi->frm_bufs[ref_idx].buf.fb->base_y.va);
273 vp9_add_to_fb_free_list(inst, fb);
276 vsi->frm_bufs[ref_idx].buf.fb);
281 vsi->frm_bufs[new_idx].ref_cnt++;
284 static void vp9_free_all_sf_ref_fb(struct vdec_vp9_inst *inst)
287 struct vdec_vp9_vsi *vsi = inst->vsi;
289 for (i = 0; i < ARRAY_SIZE(vsi->sf_ref_fb); i++) {
290 if (vsi->sf_ref_fb[i].fb.base_y.va) {
291 mtk_vcodec_mem_free(inst->ctx,
292 &vsi->sf_ref_fb[i].fb.base_y);
293 mtk_vcodec_mem_free(inst->ctx,
294 &vsi->sf_ref_fb[i].fb.base_c);
295 vsi->sf_ref_fb[i].used = 0;
300 /* For each sub-frame except the last one, the driver will dynamically
301 * allocate reference buffer by calling vp9_get_sf_ref_fb()
302 * The last sub-frame will use the original fb provided by the
303 * vp9_dec_decode() interface
305 static int vp9_get_sf_ref_fb(struct vdec_vp9_inst *inst)
308 struct mtk_vcodec_mem *mem_basy_y;
309 struct mtk_vcodec_mem *mem_basy_c;
310 struct vdec_vp9_vsi *vsi = inst->vsi;
313 idx < ARRAY_SIZE(vsi->sf_ref_fb);
315 if (vsi->sf_ref_fb[idx].fb.base_y.va &&
316 vsi->sf_ref_fb[idx].used == 0) {
322 idx < ARRAY_SIZE(vsi->sf_ref_fb);
324 if (vsi->sf_ref_fb[idx].fb.base_y.va == NULL)
328 if (idx == ARRAY_SIZE(vsi->sf_ref_fb)) {
329 mtk_vcodec_err(inst, "List Full");
333 mem_basy_y = &vsi->sf_ref_fb[idx].fb.base_y;
334 mem_basy_y->size = vsi->buf_sz_y_bs +
337 if (mtk_vcodec_mem_alloc(inst->ctx, mem_basy_y)) {
338 mtk_vcodec_err(inst, "Cannot allocate sf_ref_buf y_buf");
342 mem_basy_c = &vsi->sf_ref_fb[idx].fb.base_c;
343 mem_basy_c->size = vsi->buf_sz_c_bs +
346 if (mtk_vcodec_mem_alloc(inst->ctx, mem_basy_c)) {
347 mtk_vcodec_err(inst, "Cannot allocate sf_ref_fb c_buf");
350 vsi->sf_ref_fb[idx].used = 0;
355 static bool vp9_alloc_work_buf(struct vdec_vp9_inst *inst)
357 struct vdec_vp9_vsi *vsi = inst->vsi;
359 struct mtk_vcodec_mem *mem;
361 unsigned int max_pic_w;
362 unsigned int max_pic_h;
365 if (!(inst->ctx->dev->dec_capability &
366 VCODEC_CAPABILITY_4K_DISABLED)) {
367 max_pic_w = VCODEC_DEC_4K_CODED_WIDTH;
368 max_pic_h = VCODEC_DEC_4K_CODED_HEIGHT;
370 max_pic_w = MTK_VDEC_MAX_W;
371 max_pic_h = MTK_VDEC_MAX_H;
374 if ((vsi->pic_w > max_pic_w) ||
375 (vsi->pic_h > max_pic_h)) {
376 mtk_vcodec_err(inst, "Invalid w/h %d/%d",
377 vsi->pic_w, vsi->pic_h);
381 mtk_vcodec_debug(inst, "BUF CHG(%d): w/h/sb_w/sb_h=%d/%d/%d/%d",
382 vsi->resolution_changed,
390 mtk_vcodec_mem_free(inst->ctx, mem);
392 mem->size = ((vsi->buf_w / 64) *
393 (vsi->buf_h / 64) + 2) * 36 * 16;
394 result = mtk_vcodec_mem_alloc(inst->ctx, mem);
397 mtk_vcodec_err(inst, "Cannot allocate mv_buf");
400 /* Set the va again */
401 vsi->mv_buf.va = (unsigned long)mem->va;
402 vsi->mv_buf.pa = (unsigned long)mem->dma_addr;
403 vsi->mv_buf.sz = (unsigned int)mem->size;
406 mem = &inst->seg_id_buf;
408 mtk_vcodec_mem_free(inst->ctx, mem);
410 mem->size = VP9_SEG_ID_SZ;
411 result = mtk_vcodec_mem_alloc(inst->ctx, mem);
414 mtk_vcodec_err(inst, "Cannot allocate seg_id_buf");
417 /* Set the va again */
418 vsi->seg_id_buf.va = (unsigned long)mem->va;
419 vsi->seg_id_buf.pa = (unsigned long)mem->dma_addr;
420 vsi->seg_id_buf.sz = (unsigned int)mem->size;
423 vp9_free_all_sf_ref_fb(inst);
424 vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
429 static bool vp9_add_to_fb_disp_list(struct vdec_vp9_inst *inst,
432 struct vdec_fb_node *node;
435 mtk_vcodec_err(inst, "fb == NULL");
439 node = list_first_entry_or_null(&inst->available_fb_node_list,
440 struct vdec_fb_node, list);
443 list_move_tail(&node->list, &inst->fb_disp_list);
445 mtk_vcodec_err(inst, "No available fb node");
452 /* If any buffer updating is signaled it should be done here. */
453 static void vp9_swap_frm_bufs(struct vdec_vp9_inst *inst)
455 struct vdec_vp9_vsi *vsi = inst->vsi;
456 struct vp9_fb_info *frm_to_show;
457 int ref_index = 0, mask;
459 for (mask = vsi->refresh_frm_flags; mask; mask >>= 1) {
461 vp9_ref_cnt_fb(inst, &vsi->ref_frm_map[ref_index],
466 frm_to_show = &vsi->frm_bufs[vsi->new_fb_idx].buf;
467 vsi->frm_bufs[vsi->new_fb_idx].ref_cnt--;
469 if (frm_to_show->fb != inst->cur_fb) {
470 /* This frame is show exist frame and no decode output
471 * copy frame data from frm_to_show to current CAPTURE
474 if ((frm_to_show->fb != NULL) &&
475 (inst->cur_fb->base_y.size >=
476 frm_to_show->fb->base_y.size) &&
477 (inst->cur_fb->base_c.size >=
478 frm_to_show->fb->base_c.size)) {
479 memcpy((void *)inst->cur_fb->base_y.va,
480 (void *)frm_to_show->fb->base_y.va,
481 frm_to_show->fb->base_y.size);
482 memcpy((void *)inst->cur_fb->base_c.va,
483 (void *)frm_to_show->fb->base_c.va,
484 frm_to_show->fb->base_c.size);
486 /* After resolution change case, current CAPTURE buffer
487 * may have less buffer size than frm_to_show buffer
490 if (frm_to_show->fb != NULL)
492 "inst->cur_fb->base_y.size=%zu, frm_to_show->fb.base_y.size=%zu",
493 inst->cur_fb->base_y.size,
494 frm_to_show->fb->base_y.size);
496 if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
498 vp9_add_to_fb_disp_list(inst, inst->cur_fb);
501 if (!vp9_is_sf_ref_fb(inst, inst->cur_fb)) {
503 vp9_add_to_fb_disp_list(inst, frm_to_show->fb);
507 /* when ref_cnt ==0, move this fb to fb_free_list. v4l2 driver will
510 if (vsi->frm_bufs[vsi->new_fb_idx].ref_cnt == 0) {
511 if (!vp9_is_sf_ref_fb(
512 inst, vsi->frm_bufs[vsi->new_fb_idx].buf.fb)) {
515 fb = vp9_rm_from_fb_use_list(inst,
516 vsi->frm_bufs[vsi->new_fb_idx].buf.fb->base_y.va);
518 vp9_add_to_fb_free_list(inst, fb);
521 vsi->frm_bufs[vsi->new_fb_idx].buf.fb);
525 /* if this super frame and it is not last sub-frame, get next fb for
528 if (vsi->sf_frm_cnt > 0 && vsi->sf_frm_idx != vsi->sf_frm_cnt - 1)
529 vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
532 static bool vp9_wait_dec_end(struct vdec_vp9_inst *inst)
534 struct mtk_vcodec_ctx *ctx = inst->ctx;
536 mtk_vcodec_wait_for_done_ctx(inst->ctx,
537 MTK_INST_IRQ_RECEIVED,
538 WAIT_INTR_TIMEOUT_MS);
540 if (ctx->irq_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
546 static struct vdec_vp9_inst *vp9_alloc_inst(struct mtk_vcodec_ctx *ctx)
549 struct mtk_vcodec_mem mem;
550 struct vdec_vp9_inst *inst;
552 memset(&mem, 0, sizeof(mem));
553 mem.size = sizeof(struct vdec_vp9_inst);
554 result = mtk_vcodec_mem_alloc(ctx, &mem);
564 static void vp9_free_inst(struct vdec_vp9_inst *inst)
566 struct mtk_vcodec_mem mem;
570 mtk_vcodec_mem_free(inst->ctx, &mem);
573 static bool vp9_decode_end_proc(struct vdec_vp9_inst *inst)
575 struct vdec_vp9_vsi *vsi = inst->vsi;
578 if (!vsi->show_existing_frame) {
579 ret = vp9_wait_dec_end(inst);
581 mtk_vcodec_err(inst, "Decode failed, Decode Timeout @[%d]",
586 if (vpu_dec_end(&inst->vpu)) {
587 mtk_vcodec_err(inst, "vp9_dec_vpu_end failed");
590 mtk_vcodec_debug(inst, "Decode Ok @%d (%d/%d)", vsi->frm_num,
591 vsi->pic_w, vsi->pic_h);
593 mtk_vcodec_debug(inst, "Decode Ok @%d (show_existing_frame)",
597 vp9_swap_frm_bufs(inst);
602 static bool vp9_is_last_sub_frm(struct vdec_vp9_inst *inst)
604 struct vdec_vp9_vsi *vsi = inst->vsi;
606 if (vsi->sf_frm_cnt <= 0 || vsi->sf_frm_idx == vsi->sf_frm_cnt)
612 static struct vdec_fb *vp9_rm_from_fb_disp_list(struct vdec_vp9_inst *inst)
614 struct vdec_fb_node *node;
615 struct vdec_fb *fb = NULL;
617 node = list_first_entry_or_null(&inst->fb_disp_list,
618 struct vdec_fb_node, list);
620 fb = (struct vdec_fb *)node->fb;
621 fb->status |= FB_ST_DISPLAY;
622 list_move_tail(&node->list, &inst->available_fb_node_list);
623 mtk_vcodec_debug(inst, "[FB] get disp fb %p st=%d",
624 node->fb, fb->status);
626 mtk_vcodec_debug(inst, "[FB] there is no disp fb");
631 static bool vp9_add_to_fb_use_list(struct vdec_vp9_inst *inst,
634 struct vdec_fb_node *node;
637 mtk_vcodec_debug(inst, "fb == NULL");
641 node = list_first_entry_or_null(&inst->available_fb_node_list,
642 struct vdec_fb_node, list);
645 list_move_tail(&node->list, &inst->fb_use_list);
647 mtk_vcodec_err(inst, "No free fb node");
653 static void vp9_reset(struct vdec_vp9_inst *inst)
655 struct vdec_fb_node *node, *tmp;
657 list_for_each_entry_safe(node, tmp, &inst->fb_use_list, list)
658 list_move_tail(&node->list, &inst->fb_free_list);
660 vp9_free_all_sf_ref_fb(inst);
661 inst->vsi->sf_next_ref_fb_idx = vp9_get_sf_ref_fb(inst);
663 if (vpu_dec_reset(&inst->vpu))
664 mtk_vcodec_err(inst, "vp9_dec_vpu_reset failed");
666 /* Set the va again, since vpu_dec_reset will clear mv_buf in vpu */
667 inst->vsi->mv_buf.va = (unsigned long)inst->mv_buf.va;
668 inst->vsi->mv_buf.pa = (unsigned long)inst->mv_buf.dma_addr;
669 inst->vsi->mv_buf.sz = (unsigned long)inst->mv_buf.size;
671 /* Set the va again, since vpu_dec_reset will clear seg_id_buf in vpu */
672 inst->vsi->seg_id_buf.va = (unsigned long)inst->seg_id_buf.va;
673 inst->vsi->seg_id_buf.pa = (unsigned long)inst->seg_id_buf.dma_addr;
674 inst->vsi->seg_id_buf.sz = (unsigned long)inst->seg_id_buf.size;
678 static void init_all_fb_lists(struct vdec_vp9_inst *inst)
682 INIT_LIST_HEAD(&inst->available_fb_node_list);
683 INIT_LIST_HEAD(&inst->fb_use_list);
684 INIT_LIST_HEAD(&inst->fb_free_list);
685 INIT_LIST_HEAD(&inst->fb_disp_list);
687 for (i = 0; i < ARRAY_SIZE(inst->dec_fb); i++) {
688 INIT_LIST_HEAD(&inst->dec_fb[i].list);
689 inst->dec_fb[i].fb = NULL;
690 list_add_tail(&inst->dec_fb[i].list,
691 &inst->available_fb_node_list);
695 static void get_pic_info(struct vdec_vp9_inst *inst, struct vdec_pic_info *pic)
697 pic->fb_sz[0] = inst->vsi->buf_sz_y_bs + inst->vsi->buf_len_sz_y;
698 pic->fb_sz[1] = inst->vsi->buf_sz_c_bs + inst->vsi->buf_len_sz_c;
700 pic->pic_w = inst->vsi->pic_w;
701 pic->pic_h = inst->vsi->pic_h;
702 pic->buf_w = inst->vsi->buf_w;
703 pic->buf_h = inst->vsi->buf_h;
705 mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
706 pic->pic_w, pic->pic_h, pic->buf_w, pic->buf_h);
707 mtk_vcodec_debug(inst, "fb size: Y(%d), C(%d)",
712 static void get_disp_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
715 *out_fb = vp9_rm_from_fb_disp_list(inst);
717 (*out_fb)->status |= FB_ST_DISPLAY;
720 static void get_free_fb(struct vdec_vp9_inst *inst, struct vdec_fb **out_fb)
722 struct vdec_fb_node *node;
723 struct vdec_fb *fb = NULL;
725 node = list_first_entry_or_null(&inst->fb_free_list,
726 struct vdec_fb_node, list);
728 list_move_tail(&node->list, &inst->available_fb_node_list);
729 fb = (struct vdec_fb *)node->fb;
730 fb->status |= FB_ST_FREE;
731 mtk_vcodec_debug(inst, "[FB] get free fb %p st=%d",
732 node->fb, fb->status);
734 mtk_vcodec_debug(inst, "[FB] there is no free fb");
740 static int validate_vsi_array_indexes(struct vdec_vp9_inst *inst,
741 struct vdec_vp9_vsi *vsi) {
742 if (vsi->sf_frm_idx >= VP9_MAX_FRM_BUF_NUM - 1) {
743 mtk_vcodec_err(inst, "Invalid vsi->sf_frm_idx=%u.",
747 if (vsi->frm_to_show_idx >= VP9_MAX_FRM_BUF_NUM) {
748 mtk_vcodec_err(inst, "Invalid vsi->frm_to_show_idx=%u.",
749 vsi->frm_to_show_idx);
752 if (vsi->new_fb_idx >= VP9_MAX_FRM_BUF_NUM) {
753 mtk_vcodec_err(inst, "Invalid vsi->new_fb_idx=%u.",
760 static void vdec_vp9_deinit(unsigned long h_vdec)
762 struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
763 struct mtk_vcodec_mem *mem;
766 ret = vpu_dec_deinit(&inst->vpu);
768 mtk_vcodec_err(inst, "vpu_dec_deinit failed");
772 mtk_vcodec_mem_free(inst->ctx, mem);
774 mem = &inst->seg_id_buf;
776 mtk_vcodec_mem_free(inst->ctx, mem);
778 vp9_free_all_sf_ref_fb(inst);
782 static int vdec_vp9_init(struct mtk_vcodec_ctx *ctx, unsigned long *h_vdec)
784 struct vdec_vp9_inst *inst;
786 inst = vp9_alloc_inst(ctx);
790 inst->total_frm_cnt = 0;
793 inst->vpu.id = IPI_VDEC_VP9;
794 inst->vpu.dev = ctx->dev->vpu_plat_dev;
796 inst->vpu.handler = vpu_dec_ipi_handler;
798 if (vpu_dec_init(&inst->vpu)) {
799 mtk_vcodec_err(inst, "vp9_dec_vpu_init failed");
800 goto err_deinit_inst;
803 inst->vsi = (struct vdec_vp9_vsi *)inst->vpu.vsi;
804 init_all_fb_lists(inst);
806 (*h_vdec) = (unsigned long)inst;
815 static int vdec_vp9_decode(unsigned long h_vdec, struct mtk_vcodec_mem *bs,
816 struct vdec_fb *fb, bool *res_chg)
819 struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
820 struct vdec_vp9_vsi *vsi = inst->vsi;
826 if ((bs == NULL) && (fb == NULL)) {
827 mtk_vcodec_debug(inst, "[EOS]");
833 mtk_vcodec_err(inst, "bs == NULL");
837 mtk_vcodec_debug(inst, "Input BS Size = %zu", bs->size);
840 struct vdec_fb *cur_fb = NULL;
842 data[0] = *((unsigned int *)bs->va);
843 data[1] = *((unsigned int *)(bs->va + 4));
844 data[2] = *((unsigned int *)(bs->va + 8));
852 unsigned int sf_bs_sz;
853 unsigned int sf_bs_off;
854 unsigned char *sf_bs_src;
855 unsigned char *sf_bs_dst;
857 sf_bs_sz = bs->size > VP9_SUPER_FRAME_BS_SZ ?
858 VP9_SUPER_FRAME_BS_SZ : bs->size;
859 sf_bs_off = VP9_SUPER_FRAME_BS_SZ - sf_bs_sz;
860 sf_bs_src = bs->va + bs->size - sf_bs_sz;
861 sf_bs_dst = vsi->sf_bs_buf + sf_bs_off;
862 memcpy(sf_bs_dst, sf_bs_src, sf_bs_sz);
864 if ((vsi->sf_frm_cnt > 0) &&
865 (vsi->sf_frm_idx < vsi->sf_frm_cnt)) {
866 unsigned int idx = vsi->sf_frm_idx;
868 memcpy((void *)bs->va,
870 vsi->sf_frm_offset[idx]),
871 vsi->sf_frm_sz[idx]);
874 memset(inst->seg_id_buf.va, 0, inst->seg_id_buf.size);
875 ret = vpu_dec_start(&inst->vpu, data, 3);
877 mtk_vcodec_err(inst, "vpu_dec_start failed");
881 ret = validate_vsi_array_indexes(inst, vsi);
883 mtk_vcodec_err(inst, "Invalid values from VPU.");
887 if (vsi->resolution_changed) {
888 if (!vp9_alloc_work_buf(inst)) {
894 if (vsi->sf_frm_cnt > 0) {
895 cur_fb = &vsi->sf_ref_fb[vsi->sf_next_ref_fb_idx].fb;
897 if (vsi->sf_frm_idx < vsi->sf_frm_cnt)
898 inst->cur_fb = cur_fb;
905 vsi->frm_bufs[vsi->new_fb_idx].buf.fb = inst->cur_fb;
906 if (!vp9_is_sf_ref_fb(inst, inst->cur_fb))
907 vp9_add_to_fb_use_list(inst, inst->cur_fb);
909 mtk_vcodec_debug(inst, "[#pic %d]", vsi->frm_num);
911 if (vsi->show_existing_frame)
912 mtk_vcodec_debug(inst,
913 "drv->new_fb_idx=%d, drv->frm_to_show_idx=%d",
914 vsi->new_fb_idx, vsi->frm_to_show_idx);
916 if (vsi->show_existing_frame && (vsi->frm_to_show_idx <
917 VP9_MAX_FRM_BUF_NUM)) {
918 mtk_vcodec_debug(inst,
919 "Skip Decode drv->new_fb_idx=%d, drv->frm_to_show_idx=%d",
920 vsi->new_fb_idx, vsi->frm_to_show_idx);
922 vp9_ref_cnt_fb(inst, &vsi->new_fb_idx,
923 vsi->frm_to_show_idx);
926 /* VPU assign the buffer pointer in its address space,
929 for (i = 0; i < ARRAY_SIZE(vsi->frm_refs); i++) {
930 unsigned int idx = vsi->frm_refs[i].idx;
932 vsi->frm_refs[i].buf = &vsi->frm_bufs[idx].buf;
935 if (vsi->resolution_changed) {
937 mtk_vcodec_debug(inst, "VDEC_ST_RESOLUTION_CHANGED");
943 if (vp9_decode_end_proc(inst) != true) {
944 mtk_vcodec_err(inst, "vp9_decode_end_proc");
949 if (vp9_is_last_sub_frm(inst))
953 inst->total_frm_cnt++;
957 vp9_add_to_fb_free_list(inst, fb);
962 static void get_crop_info(struct vdec_vp9_inst *inst, struct v4l2_rect *cr)
966 cr->width = inst->vsi->pic_w;
967 cr->height = inst->vsi->pic_h;
968 mtk_vcodec_debug(inst, "get crop info l=%d, t=%d, w=%d, h=%d\n",
969 cr->left, cr->top, cr->width, cr->height);
972 static int vdec_vp9_get_param(unsigned long h_vdec,
973 enum vdec_get_param_type type, void *out)
975 struct vdec_vp9_inst *inst = (struct vdec_vp9_inst *)h_vdec;
979 case GET_PARAM_DISP_FRAME_BUFFER:
980 get_disp_fb(inst, out);
982 case GET_PARAM_FREE_FRAME_BUFFER:
983 get_free_fb(inst, out);
985 case GET_PARAM_PIC_INFO:
986 get_pic_info(inst, out);
988 case GET_PARAM_DPB_SIZE:
989 *((unsigned int *)out) = MAX_VP9_DPB_SIZE;
991 case GET_PARAM_CROP_INFO:
992 get_crop_info(inst, out);
995 mtk_vcodec_err(inst, "not supported param type %d", type);
1003 static struct vdec_common_if vdec_vp9_if = {
1004 .init = vdec_vp9_init,
1005 .decode = vdec_vp9_decode,
1006 .get_param = vdec_vp9_get_param,
1007 .deinit = vdec_vp9_deinit,
1010 struct vdec_common_if *get_vp9_dec_comm_if(void);
1012 struct vdec_common_if *get_vp9_dec_comm_if(void)
1014 return &vdec_vp9_if;