1 // SPDX-License-Identifier: GPL-2.0
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 : [BIT(0)] display this frame or not (AP-R, VPU-W)
114 * [BIT(1)] reset segment data or not (AP-R, VPU-W)
115 * [BIT(2)] trig decoder hardware or not (AP-R, VPU-W)
116 * [BIT(3)] ask VPU to set bits(0~4) accordingly (AP-W, VPU-R)
117 * [BIT(4)] do not reset segment data before every frame (AP-R, VPU-W)
118 * @show_existing_frame : inform this frame is show existing frame
120 * @frm_to_show_idx : index to show frame (AP-R, VPU-W)
122 * @refresh_frm_flags : indicate when frame need to refine reference count
124 * @resolution_changed : resolution change in this frame (AP-R, VPU-W)
126 * @frm_bufs : maintain reference buffer info (AP-R/W, VPU-R/W)
127 * @ref_frm_map : maintain reference buffer map info (AP-R/W, VPU-R/W)
128 * @new_fb_idx : index to frm_bufs array (AP-R, VPU-W)
129 * @frm_num : decoded frame number, include sub-frame count (AP-R, VPU-W)
130 * @mv_buf : motion vector working buffer (AP-W, VPU-R)
131 * @frm_refs : maintain three reference buffer info (AP-R/W, VPU-R/W)
132 * @seg_id_buf : segmentation map working buffer (AP-W, VPU-R)
134 struct vdec_vp9_vsi
{
135 unsigned char sf_bs_buf
[VP9_SUPER_FRAME_BS_SZ
];
136 struct vp9_sf_ref_fb sf_ref_fb
[VP9_MAX_FRM_BUF_NUM
-1];
137 int sf_next_ref_fb_idx
;
138 unsigned int sf_frm_cnt
;
139 unsigned int sf_frm_offset
[VP9_MAX_FRM_BUF_NUM
-1];
140 unsigned int sf_frm_sz
[VP9_MAX_FRM_BUF_NUM
-1];
141 unsigned int sf_frm_idx
;
142 unsigned int sf_init
;
144 struct mtk_vcodec_mem bs
;
145 struct vdec_fb cur_fb
;
150 unsigned int buf_sz_y_bs
;
151 unsigned int buf_sz_c_bs
;
152 unsigned int buf_len_sz_y
;
153 unsigned int buf_len_sz_c
;
154 unsigned int profile
;
155 unsigned int show_frame
;
156 unsigned int show_existing_frame
;
157 unsigned int frm_to_show_idx
;
158 unsigned int refresh_frm_flags
;
159 unsigned int resolution_changed
;
161 struct vp9_ref_cnt_buf frm_bufs
[VP9_MAX_FRM_BUF_NUM
];
162 int ref_frm_map
[MAX_NUM_REF_FRAMES
];
163 unsigned int new_fb_idx
;
164 unsigned int frm_num
;
165 struct vp9_dram_buf mv_buf
;
167 struct vp9_ref_buf frm_refs
[REFS_PER_FRAME
];
168 struct vp9_dram_buf seg_id_buf
;
173 * struct vdec_vp9_inst - vp9 decode instance
174 * @mv_buf : working buffer for mv
175 * @seg_id_buf : working buffer for segmentation map
176 * @dec_fb : vdec_fb node to link fb to different fb_xxx_list
177 * @available_fb_node_list : current available vdec_fb node
178 * @fb_use_list : current used or referenced vdec_fb
179 * @fb_free_list : current available to free vdec_fb
180 * @fb_disp_list : current available to display vdec_fb
181 * @cur_fb : current frame buffer
182 * @ctx : current decode context
183 * @vpu : vpu instance information
184 * @vsi : shared buffer between host and VPU firmware
185 * @total_frm_cnt : total frame count, it do not include sub-frames in super
187 * @mem : instance memory information
189 struct vdec_vp9_inst
{
190 struct mtk_vcodec_mem mv_buf
;
191 struct mtk_vcodec_mem seg_id_buf
;
193 struct vdec_fb_node dec_fb
[VP9_MAX_FRM_BUF_NODE_NUM
];
194 struct list_head available_fb_node_list
;
195 struct list_head fb_use_list
;
196 struct list_head fb_free_list
;
197 struct list_head fb_disp_list
;
198 struct vdec_fb
*cur_fb
;
199 struct mtk_vcodec_ctx
*ctx
;
200 struct vdec_vpu_inst vpu
;
201 struct vdec_vp9_vsi
*vsi
;
202 unsigned int total_frm_cnt
;
203 struct mtk_vcodec_mem mem
;
206 static bool vp9_is_sf_ref_fb(struct vdec_vp9_inst
*inst
, struct vdec_fb
*fb
)
209 struct vdec_vp9_vsi
*vsi
= inst
->vsi
;
211 for (i
= 0; i
< ARRAY_SIZE(vsi
->sf_ref_fb
); i
++) {
212 if (fb
== &vsi
->sf_ref_fb
[i
].fb
)
218 static struct vdec_fb
*vp9_rm_from_fb_use_list(struct vdec_vp9_inst
221 struct vdec_fb
*fb
= NULL
;
222 struct vdec_fb_node
*node
;
224 list_for_each_entry(node
, &inst
->fb_use_list
, list
) {
225 fb
= (struct vdec_fb
*)node
->fb
;
226 if (fb
->base_y
.va
== addr
) {
227 list_move_tail(&node
->list
,
228 &inst
->available_fb_node_list
);
235 static void vp9_add_to_fb_free_list(struct vdec_vp9_inst
*inst
,
238 struct vdec_fb_node
*node
;
241 node
= list_first_entry_or_null(&inst
->available_fb_node_list
,
242 struct vdec_fb_node
, list
);
246 list_move_tail(&node
->list
, &inst
->fb_free_list
);
249 mtk_vcodec_debug(inst
, "No free fb node");
253 static void vp9_free_sf_ref_fb(struct vdec_fb
*fb
)
255 struct vp9_sf_ref_fb
*sf_ref_fb
=
256 container_of(fb
, struct vp9_sf_ref_fb
, fb
);
261 static void vp9_ref_cnt_fb(struct vdec_vp9_inst
*inst
, int *idx
,
264 struct vdec_vp9_vsi
*vsi
= inst
->vsi
;
267 if (ref_idx
>= 0 && vsi
->frm_bufs
[ref_idx
].ref_cnt
> 0) {
268 vsi
->frm_bufs
[ref_idx
].ref_cnt
--;
270 if (vsi
->frm_bufs
[ref_idx
].ref_cnt
== 0) {
271 if (!vp9_is_sf_ref_fb(inst
,
272 vsi
->frm_bufs
[ref_idx
].buf
.fb
)) {
275 fb
= vp9_rm_from_fb_use_list(inst
,
276 vsi
->frm_bufs
[ref_idx
].buf
.fb
->base_y
.va
);
277 vp9_add_to_fb_free_list(inst
, fb
);
280 vsi
->frm_bufs
[ref_idx
].buf
.fb
);
285 vsi
->frm_bufs
[new_idx
].ref_cnt
++;
288 static void vp9_free_all_sf_ref_fb(struct vdec_vp9_inst
*inst
)
291 struct vdec_vp9_vsi
*vsi
= inst
->vsi
;
293 for (i
= 0; i
< ARRAY_SIZE(vsi
->sf_ref_fb
); i
++) {
294 if (vsi
->sf_ref_fb
[i
].fb
.base_y
.va
) {
295 mtk_vcodec_mem_free(inst
->ctx
,
296 &vsi
->sf_ref_fb
[i
].fb
.base_y
);
297 mtk_vcodec_mem_free(inst
->ctx
,
298 &vsi
->sf_ref_fb
[i
].fb
.base_c
);
299 vsi
->sf_ref_fb
[i
].used
= 0;
304 /* For each sub-frame except the last one, the driver will dynamically
305 * allocate reference buffer by calling vp9_get_sf_ref_fb()
306 * The last sub-frame will use the original fb provided by the
307 * vp9_dec_decode() interface
309 static int vp9_get_sf_ref_fb(struct vdec_vp9_inst
*inst
)
312 struct mtk_vcodec_mem
*mem_basy_y
;
313 struct mtk_vcodec_mem
*mem_basy_c
;
314 struct vdec_vp9_vsi
*vsi
= inst
->vsi
;
317 idx
< ARRAY_SIZE(vsi
->sf_ref_fb
);
319 if (vsi
->sf_ref_fb
[idx
].fb
.base_y
.va
&&
320 vsi
->sf_ref_fb
[idx
].used
== 0) {
326 idx
< ARRAY_SIZE(vsi
->sf_ref_fb
);
328 if (vsi
->sf_ref_fb
[idx
].fb
.base_y
.va
== NULL
)
332 if (idx
== ARRAY_SIZE(vsi
->sf_ref_fb
)) {
333 mtk_vcodec_err(inst
, "List Full");
337 mem_basy_y
= &vsi
->sf_ref_fb
[idx
].fb
.base_y
;
338 mem_basy_y
->size
= vsi
->buf_sz_y_bs
+
341 if (mtk_vcodec_mem_alloc(inst
->ctx
, mem_basy_y
)) {
342 mtk_vcodec_err(inst
, "Cannot allocate sf_ref_buf y_buf");
346 mem_basy_c
= &vsi
->sf_ref_fb
[idx
].fb
.base_c
;
347 mem_basy_c
->size
= vsi
->buf_sz_c_bs
+
350 if (mtk_vcodec_mem_alloc(inst
->ctx
, mem_basy_c
)) {
351 mtk_vcodec_err(inst
, "Cannot allocate sf_ref_fb c_buf");
354 vsi
->sf_ref_fb
[idx
].used
= 0;
359 static bool vp9_alloc_work_buf(struct vdec_vp9_inst
*inst
)
361 struct vdec_vp9_vsi
*vsi
= inst
->vsi
;
363 struct mtk_vcodec_mem
*mem
;
365 unsigned int max_pic_w
;
366 unsigned int max_pic_h
;
369 if (!(inst
->ctx
->dev
->dec_capability
&
370 VCODEC_CAPABILITY_4K_DISABLED
)) {
371 max_pic_w
= VCODEC_DEC_4K_CODED_WIDTH
;
372 max_pic_h
= VCODEC_DEC_4K_CODED_HEIGHT
;
374 max_pic_w
= MTK_VDEC_MAX_W
;
375 max_pic_h
= MTK_VDEC_MAX_H
;
378 if ((vsi
->pic_w
> max_pic_w
) ||
379 (vsi
->pic_h
> max_pic_h
)) {
380 mtk_vcodec_err(inst
, "Invalid w/h %d/%d",
381 vsi
->pic_w
, vsi
->pic_h
);
385 mtk_vcodec_debug(inst
, "BUF CHG(%d): w/h/sb_w/sb_h=%d/%d/%d/%d",
386 vsi
->resolution_changed
,
394 mtk_vcodec_mem_free(inst
->ctx
, mem
);
396 mem
->size
= ((vsi
->buf_w
/ 64) *
397 (vsi
->buf_h
/ 64) + 2) * 36 * 16;
398 result
= mtk_vcodec_mem_alloc(inst
->ctx
, mem
);
401 mtk_vcodec_err(inst
, "Cannot allocate mv_buf");
404 /* Set the va again */
405 vsi
->mv_buf
.va
= (unsigned long)mem
->va
;
406 vsi
->mv_buf
.pa
= (unsigned long)mem
->dma_addr
;
407 vsi
->mv_buf
.sz
= (unsigned int)mem
->size
;
410 mem
= &inst
->seg_id_buf
;
412 mtk_vcodec_mem_free(inst
->ctx
, mem
);
414 mem
->size
= VP9_SEG_ID_SZ
;
415 result
= mtk_vcodec_mem_alloc(inst
->ctx
, mem
);
418 mtk_vcodec_err(inst
, "Cannot allocate seg_id_buf");
421 /* Set the va again */
422 vsi
->seg_id_buf
.va
= (unsigned long)mem
->va
;
423 vsi
->seg_id_buf
.pa
= (unsigned long)mem
->dma_addr
;
424 vsi
->seg_id_buf
.sz
= (unsigned int)mem
->size
;
427 vp9_free_all_sf_ref_fb(inst
);
428 vsi
->sf_next_ref_fb_idx
= vp9_get_sf_ref_fb(inst
);
433 static bool vp9_add_to_fb_disp_list(struct vdec_vp9_inst
*inst
,
436 struct vdec_fb_node
*node
;
439 mtk_vcodec_err(inst
, "fb == NULL");
443 node
= list_first_entry_or_null(&inst
->available_fb_node_list
,
444 struct vdec_fb_node
, list
);
447 list_move_tail(&node
->list
, &inst
->fb_disp_list
);
449 mtk_vcodec_err(inst
, "No available fb node");
456 /* If any buffer updating is signaled it should be done here. */
457 static void vp9_swap_frm_bufs(struct vdec_vp9_inst
*inst
)
459 struct vdec_vp9_vsi
*vsi
= inst
->vsi
;
460 struct vp9_fb_info
*frm_to_show
;
461 int ref_index
= 0, mask
;
463 for (mask
= vsi
->refresh_frm_flags
; mask
; mask
>>= 1) {
465 vp9_ref_cnt_fb(inst
, &vsi
->ref_frm_map
[ref_index
],
470 frm_to_show
= &vsi
->frm_bufs
[vsi
->new_fb_idx
].buf
;
471 vsi
->frm_bufs
[vsi
->new_fb_idx
].ref_cnt
--;
473 if (frm_to_show
->fb
!= inst
->cur_fb
) {
474 /* This frame is show exist frame and no decode output
475 * copy frame data from frm_to_show to current CAPTURE
478 if ((frm_to_show
->fb
!= NULL
) &&
479 (inst
->cur_fb
->base_y
.size
>=
480 frm_to_show
->fb
->base_y
.size
) &&
481 (inst
->cur_fb
->base_c
.size
>=
482 frm_to_show
->fb
->base_c
.size
)) {
483 memcpy((void *)inst
->cur_fb
->base_y
.va
,
484 (void *)frm_to_show
->fb
->base_y
.va
,
485 frm_to_show
->fb
->base_y
.size
);
486 memcpy((void *)inst
->cur_fb
->base_c
.va
,
487 (void *)frm_to_show
->fb
->base_c
.va
,
488 frm_to_show
->fb
->base_c
.size
);
490 /* After resolution change case, current CAPTURE buffer
491 * may have less buffer size than frm_to_show buffer
494 if (frm_to_show
->fb
!= NULL
)
496 "inst->cur_fb->base_y.size=%zu, frm_to_show->fb.base_y.size=%zu",
497 inst
->cur_fb
->base_y
.size
,
498 frm_to_show
->fb
->base_y
.size
);
500 if (!vp9_is_sf_ref_fb(inst
, inst
->cur_fb
)) {
501 if (vsi
->show_frame
& BIT(0))
502 vp9_add_to_fb_disp_list(inst
, inst
->cur_fb
);
505 if (!vp9_is_sf_ref_fb(inst
, inst
->cur_fb
)) {
506 if (vsi
->show_frame
& BIT(0))
507 vp9_add_to_fb_disp_list(inst
, frm_to_show
->fb
);
511 /* when ref_cnt ==0, move this fb to fb_free_list. v4l2 driver will
514 if (vsi
->frm_bufs
[vsi
->new_fb_idx
].ref_cnt
== 0) {
515 if (!vp9_is_sf_ref_fb(
516 inst
, vsi
->frm_bufs
[vsi
->new_fb_idx
].buf
.fb
)) {
519 fb
= vp9_rm_from_fb_use_list(inst
,
520 vsi
->frm_bufs
[vsi
->new_fb_idx
].buf
.fb
->base_y
.va
);
522 vp9_add_to_fb_free_list(inst
, fb
);
525 vsi
->frm_bufs
[vsi
->new_fb_idx
].buf
.fb
);
529 /* if this super frame and it is not last sub-frame, get next fb for
532 if (vsi
->sf_frm_cnt
> 0 && vsi
->sf_frm_idx
!= vsi
->sf_frm_cnt
- 1)
533 vsi
->sf_next_ref_fb_idx
= vp9_get_sf_ref_fb(inst
);
536 static bool vp9_wait_dec_end(struct vdec_vp9_inst
*inst
)
538 struct mtk_vcodec_ctx
*ctx
= inst
->ctx
;
540 mtk_vcodec_wait_for_done_ctx(inst
->ctx
,
541 MTK_INST_IRQ_RECEIVED
,
542 WAIT_INTR_TIMEOUT_MS
);
544 if (ctx
->irq_status
& MTK_VDEC_IRQ_STATUS_DEC_SUCCESS
)
550 static struct vdec_vp9_inst
*vp9_alloc_inst(struct mtk_vcodec_ctx
*ctx
)
553 struct mtk_vcodec_mem mem
;
554 struct vdec_vp9_inst
*inst
;
556 memset(&mem
, 0, sizeof(mem
));
557 mem
.size
= sizeof(struct vdec_vp9_inst
);
558 result
= mtk_vcodec_mem_alloc(ctx
, &mem
);
568 static void vp9_free_inst(struct vdec_vp9_inst
*inst
)
570 struct mtk_vcodec_mem mem
;
574 mtk_vcodec_mem_free(inst
->ctx
, &mem
);
577 static bool vp9_decode_end_proc(struct vdec_vp9_inst
*inst
)
579 struct vdec_vp9_vsi
*vsi
= inst
->vsi
;
582 if (!vsi
->show_existing_frame
) {
583 ret
= vp9_wait_dec_end(inst
);
585 mtk_vcodec_err(inst
, "Decode failed, Decode Timeout @[%d]",
590 if (vpu_dec_end(&inst
->vpu
)) {
591 mtk_vcodec_err(inst
, "vp9_dec_vpu_end failed");
594 mtk_vcodec_debug(inst
, "Decode Ok @%d (%d/%d)", vsi
->frm_num
,
595 vsi
->pic_w
, vsi
->pic_h
);
597 mtk_vcodec_debug(inst
, "Decode Ok @%d (show_existing_frame)",
601 vp9_swap_frm_bufs(inst
);
606 static bool vp9_is_last_sub_frm(struct vdec_vp9_inst
*inst
)
608 struct vdec_vp9_vsi
*vsi
= inst
->vsi
;
610 if (vsi
->sf_frm_cnt
<= 0 || vsi
->sf_frm_idx
== vsi
->sf_frm_cnt
)
616 static struct vdec_fb
*vp9_rm_from_fb_disp_list(struct vdec_vp9_inst
*inst
)
618 struct vdec_fb_node
*node
;
619 struct vdec_fb
*fb
= NULL
;
621 node
= list_first_entry_or_null(&inst
->fb_disp_list
,
622 struct vdec_fb_node
, list
);
624 fb
= (struct vdec_fb
*)node
->fb
;
625 fb
->status
|= FB_ST_DISPLAY
;
626 list_move_tail(&node
->list
, &inst
->available_fb_node_list
);
627 mtk_vcodec_debug(inst
, "[FB] get disp fb %p st=%d",
628 node
->fb
, fb
->status
);
630 mtk_vcodec_debug(inst
, "[FB] there is no disp fb");
635 static bool vp9_add_to_fb_use_list(struct vdec_vp9_inst
*inst
,
638 struct vdec_fb_node
*node
;
641 mtk_vcodec_debug(inst
, "fb == NULL");
645 node
= list_first_entry_or_null(&inst
->available_fb_node_list
,
646 struct vdec_fb_node
, list
);
649 list_move_tail(&node
->list
, &inst
->fb_use_list
);
651 mtk_vcodec_err(inst
, "No free fb node");
657 static void vp9_reset(struct vdec_vp9_inst
*inst
)
659 struct vdec_fb_node
*node
, *tmp
;
661 list_for_each_entry_safe(node
, tmp
, &inst
->fb_use_list
, list
)
662 list_move_tail(&node
->list
, &inst
->fb_free_list
);
664 vp9_free_all_sf_ref_fb(inst
);
665 inst
->vsi
->sf_next_ref_fb_idx
= vp9_get_sf_ref_fb(inst
);
667 if (vpu_dec_reset(&inst
->vpu
))
668 mtk_vcodec_err(inst
, "vp9_dec_vpu_reset failed");
670 /* Set the va again, since vpu_dec_reset will clear mv_buf in vpu */
671 inst
->vsi
->mv_buf
.va
= (unsigned long)inst
->mv_buf
.va
;
672 inst
->vsi
->mv_buf
.pa
= (unsigned long)inst
->mv_buf
.dma_addr
;
673 inst
->vsi
->mv_buf
.sz
= (unsigned long)inst
->mv_buf
.size
;
675 /* Set the va again, since vpu_dec_reset will clear seg_id_buf in vpu */
676 inst
->vsi
->seg_id_buf
.va
= (unsigned long)inst
->seg_id_buf
.va
;
677 inst
->vsi
->seg_id_buf
.pa
= (unsigned long)inst
->seg_id_buf
.dma_addr
;
678 inst
->vsi
->seg_id_buf
.sz
= (unsigned long)inst
->seg_id_buf
.size
;
682 static void init_all_fb_lists(struct vdec_vp9_inst
*inst
)
686 INIT_LIST_HEAD(&inst
->available_fb_node_list
);
687 INIT_LIST_HEAD(&inst
->fb_use_list
);
688 INIT_LIST_HEAD(&inst
->fb_free_list
);
689 INIT_LIST_HEAD(&inst
->fb_disp_list
);
691 for (i
= 0; i
< ARRAY_SIZE(inst
->dec_fb
); i
++) {
692 INIT_LIST_HEAD(&inst
->dec_fb
[i
].list
);
693 inst
->dec_fb
[i
].fb
= NULL
;
694 list_add_tail(&inst
->dec_fb
[i
].list
,
695 &inst
->available_fb_node_list
);
699 static void get_pic_info(struct vdec_vp9_inst
*inst
, struct vdec_pic_info
*pic
)
701 pic
->fb_sz
[0] = inst
->vsi
->buf_sz_y_bs
+ inst
->vsi
->buf_len_sz_y
;
702 pic
->fb_sz
[1] = inst
->vsi
->buf_sz_c_bs
+ inst
->vsi
->buf_len_sz_c
;
704 pic
->pic_w
= inst
->vsi
->pic_w
;
705 pic
->pic_h
= inst
->vsi
->pic_h
;
706 pic
->buf_w
= inst
->vsi
->buf_w
;
707 pic
->buf_h
= inst
->vsi
->buf_h
;
709 mtk_vcodec_debug(inst
, "pic(%d, %d), buf(%d, %d)",
710 pic
->pic_w
, pic
->pic_h
, pic
->buf_w
, pic
->buf_h
);
711 mtk_vcodec_debug(inst
, "fb size: Y(%d), C(%d)",
716 static void get_disp_fb(struct vdec_vp9_inst
*inst
, struct vdec_fb
**out_fb
)
719 *out_fb
= vp9_rm_from_fb_disp_list(inst
);
721 (*out_fb
)->status
|= FB_ST_DISPLAY
;
724 static void get_free_fb(struct vdec_vp9_inst
*inst
, struct vdec_fb
**out_fb
)
726 struct vdec_fb_node
*node
;
727 struct vdec_fb
*fb
= NULL
;
729 node
= list_first_entry_or_null(&inst
->fb_free_list
,
730 struct vdec_fb_node
, list
);
732 list_move_tail(&node
->list
, &inst
->available_fb_node_list
);
733 fb
= (struct vdec_fb
*)node
->fb
;
734 fb
->status
|= FB_ST_FREE
;
735 mtk_vcodec_debug(inst
, "[FB] get free fb %p st=%d",
736 node
->fb
, fb
->status
);
738 mtk_vcodec_debug(inst
, "[FB] there is no free fb");
744 static int validate_vsi_array_indexes(struct vdec_vp9_inst
*inst
,
745 struct vdec_vp9_vsi
*vsi
) {
746 if (vsi
->sf_frm_idx
>= VP9_MAX_FRM_BUF_NUM
- 1) {
747 mtk_vcodec_err(inst
, "Invalid vsi->sf_frm_idx=%u.",
751 if (vsi
->frm_to_show_idx
>= VP9_MAX_FRM_BUF_NUM
) {
752 mtk_vcodec_err(inst
, "Invalid vsi->frm_to_show_idx=%u.",
753 vsi
->frm_to_show_idx
);
756 if (vsi
->new_fb_idx
>= VP9_MAX_FRM_BUF_NUM
) {
757 mtk_vcodec_err(inst
, "Invalid vsi->new_fb_idx=%u.",
764 static void vdec_vp9_deinit(void *h_vdec
)
766 struct vdec_vp9_inst
*inst
= (struct vdec_vp9_inst
*)h_vdec
;
767 struct mtk_vcodec_mem
*mem
;
770 ret
= vpu_dec_deinit(&inst
->vpu
);
772 mtk_vcodec_err(inst
, "vpu_dec_deinit failed");
776 mtk_vcodec_mem_free(inst
->ctx
, mem
);
778 mem
= &inst
->seg_id_buf
;
780 mtk_vcodec_mem_free(inst
->ctx
, mem
);
782 vp9_free_all_sf_ref_fb(inst
);
786 static int vdec_vp9_init(struct mtk_vcodec_ctx
*ctx
)
788 struct vdec_vp9_inst
*inst
;
790 inst
= vp9_alloc_inst(ctx
);
794 inst
->total_frm_cnt
= 0;
797 inst
->vpu
.id
= IPI_VDEC_VP9
;
800 if (vpu_dec_init(&inst
->vpu
)) {
801 mtk_vcodec_err(inst
, "vp9_dec_vpu_init failed");
802 goto err_deinit_inst
;
805 inst
->vsi
= (struct vdec_vp9_vsi
*)inst
->vpu
.vsi
;
807 inst
->vsi
->show_frame
|= BIT(3);
809 init_all_fb_lists(inst
);
811 ctx
->drv_handle
= inst
;
820 static int vdec_vp9_decode(void *h_vdec
, struct mtk_vcodec_mem
*bs
,
821 struct vdec_fb
*fb
, bool *res_chg
)
824 struct vdec_vp9_inst
*inst
= (struct vdec_vp9_inst
*)h_vdec
;
825 struct vdec_vp9_vsi
*vsi
= inst
->vsi
;
831 if ((bs
== NULL
) && (fb
== NULL
)) {
832 mtk_vcodec_debug(inst
, "[EOS]");
838 mtk_vcodec_err(inst
, "bs == NULL");
842 mtk_vcodec_debug(inst
, "Input BS Size = %zu", bs
->size
);
845 struct vdec_fb
*cur_fb
= NULL
;
847 data
[0] = *((unsigned int *)bs
->va
);
848 data
[1] = *((unsigned int *)(bs
->va
+ 4));
849 data
[2] = *((unsigned int *)(bs
->va
+ 8));
857 unsigned int sf_bs_sz
;
858 unsigned int sf_bs_off
;
859 unsigned char *sf_bs_src
;
860 unsigned char *sf_bs_dst
;
862 sf_bs_sz
= bs
->size
> VP9_SUPER_FRAME_BS_SZ
?
863 VP9_SUPER_FRAME_BS_SZ
: bs
->size
;
864 sf_bs_off
= VP9_SUPER_FRAME_BS_SZ
- sf_bs_sz
;
865 sf_bs_src
= bs
->va
+ bs
->size
- sf_bs_sz
;
866 sf_bs_dst
= vsi
->sf_bs_buf
+ sf_bs_off
;
867 memcpy(sf_bs_dst
, sf_bs_src
, sf_bs_sz
);
869 if ((vsi
->sf_frm_cnt
> 0) &&
870 (vsi
->sf_frm_idx
< vsi
->sf_frm_cnt
)) {
871 unsigned int idx
= vsi
->sf_frm_idx
;
873 memcpy((void *)bs
->va
,
875 vsi
->sf_frm_offset
[idx
]),
876 vsi
->sf_frm_sz
[idx
]);
880 if (!(vsi
->show_frame
& BIT(4)))
881 memset(inst
->seg_id_buf
.va
, 0, inst
->seg_id_buf
.size
);
883 ret
= vpu_dec_start(&inst
->vpu
, data
, 3);
885 mtk_vcodec_err(inst
, "vpu_dec_start failed");
889 if (vsi
->show_frame
& BIT(1)) {
890 memset(inst
->seg_id_buf
.va
, 0, inst
->seg_id_buf
.size
);
892 if (vsi
->show_frame
& BIT(2)) {
893 if (vpu_dec_start(&inst
->vpu
, NULL
, 0)) {
894 mtk_vcodec_err(inst
, "vpu trig decoder failed");
900 ret
= validate_vsi_array_indexes(inst
, vsi
);
902 mtk_vcodec_err(inst
, "Invalid values from VPU.");
906 if (vsi
->resolution_changed
) {
907 if (!vp9_alloc_work_buf(inst
)) {
913 if (vsi
->sf_frm_cnt
> 0) {
914 cur_fb
= &vsi
->sf_ref_fb
[vsi
->sf_next_ref_fb_idx
].fb
;
916 if (vsi
->sf_frm_idx
< vsi
->sf_frm_cnt
)
917 inst
->cur_fb
= cur_fb
;
924 vsi
->frm_bufs
[vsi
->new_fb_idx
].buf
.fb
= inst
->cur_fb
;
925 if (!vp9_is_sf_ref_fb(inst
, inst
->cur_fb
))
926 vp9_add_to_fb_use_list(inst
, inst
->cur_fb
);
928 mtk_vcodec_debug(inst
, "[#pic %d]", vsi
->frm_num
);
930 if (vsi
->show_existing_frame
)
931 mtk_vcodec_debug(inst
,
932 "drv->new_fb_idx=%d, drv->frm_to_show_idx=%d",
933 vsi
->new_fb_idx
, vsi
->frm_to_show_idx
);
935 if (vsi
->show_existing_frame
&& (vsi
->frm_to_show_idx
<
936 VP9_MAX_FRM_BUF_NUM
)) {
937 mtk_vcodec_debug(inst
,
938 "Skip Decode drv->new_fb_idx=%d, drv->frm_to_show_idx=%d",
939 vsi
->new_fb_idx
, vsi
->frm_to_show_idx
);
941 vp9_ref_cnt_fb(inst
, &vsi
->new_fb_idx
,
942 vsi
->frm_to_show_idx
);
945 /* VPU assign the buffer pointer in its address space,
948 for (i
= 0; i
< ARRAY_SIZE(vsi
->frm_refs
); i
++) {
949 unsigned int idx
= vsi
->frm_refs
[i
].idx
;
951 vsi
->frm_refs
[i
].buf
= &vsi
->frm_bufs
[idx
].buf
;
954 if (vsi
->resolution_changed
) {
956 mtk_vcodec_debug(inst
, "VDEC_ST_RESOLUTION_CHANGED");
962 if (!vp9_decode_end_proc(inst
)) {
963 mtk_vcodec_err(inst
, "vp9_decode_end_proc");
968 if (vp9_is_last_sub_frm(inst
))
972 inst
->total_frm_cnt
++;
976 vp9_add_to_fb_free_list(inst
, fb
);
981 static void get_crop_info(struct vdec_vp9_inst
*inst
, struct v4l2_rect
*cr
)
985 cr
->width
= inst
->vsi
->pic_w
;
986 cr
->height
= inst
->vsi
->pic_h
;
987 mtk_vcodec_debug(inst
, "get crop info l=%d, t=%d, w=%d, h=%d\n",
988 cr
->left
, cr
->top
, cr
->width
, cr
->height
);
991 static int vdec_vp9_get_param(void *h_vdec
, enum vdec_get_param_type type
,
994 struct vdec_vp9_inst
*inst
= (struct vdec_vp9_inst
*)h_vdec
;
998 case GET_PARAM_DISP_FRAME_BUFFER
:
999 get_disp_fb(inst
, out
);
1001 case GET_PARAM_FREE_FRAME_BUFFER
:
1002 get_free_fb(inst
, out
);
1004 case GET_PARAM_PIC_INFO
:
1005 get_pic_info(inst
, out
);
1007 case GET_PARAM_DPB_SIZE
:
1008 *((unsigned int *)out
) = MAX_VP9_DPB_SIZE
;
1010 case GET_PARAM_CROP_INFO
:
1011 get_crop_info(inst
, out
);
1014 mtk_vcodec_err(inst
, "not supported param type %d", type
);
1022 const struct vdec_common_if vdec_vp9_if
= {
1023 .init
= vdec_vp9_init
,
1024 .decode
= vdec_vp9_decode
,
1025 .get_param
= vdec_vp9_get_param
,
1026 .deinit
= vdec_vp9_deinit
,