1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022 MediaTek Inc.
4 * Author: Yunfei Dong <yunfei.dong@mediatek.com>
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <media/v4l2-h264.h>
10 #include <media/v4l2-mem2mem.h>
11 #include <media/videobuf2-dma-contig.h>
13 #include "../mtk_vcodec_util.h"
14 #include "../mtk_vcodec_dec.h"
15 #include "../mtk_vcodec_intr.h"
16 #include "../vdec_drv_base.h"
17 #include "../vdec_drv_if.h"
18 #include "../vdec_vpu_if.h"
19 #include "vdec_h264_req_common.h"
22 * enum vdec_h264_core_dec_err_type - core decode error type
24 * @TRANS_BUFFER_FULL: trans buffer is full
25 * @SLICE_HEADER_FULL: slice header buffer is full
27 enum vdec_h264_core_dec_err_type {
28 TRANS_BUFFER_FULL = 1,
33 * struct vdec_h264_slice_lat_dec_param - parameters for decode current frame
35 * @sps: h264 sps syntax parameters
36 * @pps: h264 pps syntax parameters
37 * @slice_header: h264 slice header syntax parameters
38 * @scaling_matrix: h264 scaling list parameters
39 * @decode_params: decoder parameters of each frame used for hardware decode
40 * @h264_dpb_info: dpb reference list
42 struct vdec_h264_slice_lat_dec_param {
43 struct mtk_h264_sps_param sps;
44 struct mtk_h264_pps_param pps;
45 struct mtk_h264_slice_hd_param slice_header;
46 struct slice_api_h264_scaling_matrix scaling_matrix;
47 struct slice_api_h264_decode_param decode_params;
48 struct mtk_h264_dpb_info h264_dpb_info[V4L2_H264_NUM_DPB_ENTRIES];
52 * struct vdec_h264_slice_info - decode information
54 * @nal_info: nal info of current picture
55 * @timeout: Decode timeout: 1 timeout, 0 no timeount
56 * @bs_buf_size: bitstream size
57 * @bs_buf_addr: bitstream buffer dma address
58 * @y_fb_dma: Y frame buffer dma address
59 * @c_fb_dma: C frame buffer dma address
60 * @vdec_fb_va: VDEC frame buffer struct virtual address
61 * @crc: Used to check whether hardware's status is right
63 struct vdec_h264_slice_info {
75 * struct vdec_h264_slice_vsi - shared memory for decode information exchange
76 * between SCP and Host.
78 * @wdma_err_addr: wdma error dma address
79 * @wdma_start_addr: wdma start dma address
80 * @wdma_end_addr: wdma end dma address
81 * @slice_bc_start_addr: slice bc start dma address
82 * @slice_bc_end_addr: slice bc end dma address
83 * @row_info_start_addr: row info start dma address
84 * @row_info_end_addr: row info end dma address
85 * @trans_start: trans start dma address
86 * @trans_end: trans end dma address
87 * @wdma_end_addr_offset: wdma end address offset
89 * @mv_buf_dma: HW working motion vector buffer
90 * dma address (AP-W, VPU-R)
91 * @dec: decode information (AP-R, VPU-W)
92 * @h264_slice_params: decode parameters for hw used
94 struct vdec_h264_slice_vsi {
99 u64 slice_bc_start_addr;
100 u64 slice_bc_end_addr;
101 u64 row_info_start_addr;
102 u64 row_info_end_addr;
105 u64 wdma_end_addr_offset;
107 u64 mv_buf_dma[H264_MAX_MV_NUM];
108 struct vdec_h264_slice_info dec;
109 struct vdec_h264_slice_lat_dec_param h264_slice_params;
113 * struct vdec_h264_slice_share_info - shared information used to exchange
114 * message between lat and core
116 * @sps: sequence header information from user space
117 * @dec_params: decoder params from user space
118 * @h264_slice_params: decoder params used for hardware
119 * @trans_start: trans start dma address
120 * @trans_end: trans end dma address
121 * @nal_info: nal info of current picture
123 struct vdec_h264_slice_share_info {
124 struct v4l2_ctrl_h264_sps sps;
125 struct v4l2_ctrl_h264_decode_params dec_params;
126 struct vdec_h264_slice_lat_dec_param h264_slice_params;
133 * struct vdec_h264_slice_inst - h264 decoder instance
135 * @slice_dec_num: how many picture be decoded
136 * @ctx: point to mtk_vcodec_ctx
137 * @pred_buf: HW working predication buffer
138 * @mv_buf: HW working motion vector buffer
140 * @vsi: vsi used for lat
141 * @vsi_core: vsi used for core
143 * @vsi_ctx: Local VSI data for this decoding context
144 * @h264_slice_param: the parameters that hardware use to decode
146 * @resolution_changed:resolution changed
147 * @realloc_mv_buf: reallocate mv buffer
148 * @cap_num_planes: number of capture queue plane
150 * @dpb: decoded picture buffer used to store reference
152 *@is_field_bitstream: is field bitstream
154 struct vdec_h264_slice_inst {
155 unsigned int slice_dec_num;
156 struct mtk_vcodec_ctx *ctx;
157 struct mtk_vcodec_mem pred_buf;
158 struct mtk_vcodec_mem mv_buf[H264_MAX_MV_NUM];
159 struct vdec_vpu_inst vpu;
160 struct vdec_h264_slice_vsi *vsi;
161 struct vdec_h264_slice_vsi *vsi_core;
163 struct vdec_h264_slice_vsi vsi_ctx;
164 struct vdec_h264_slice_lat_dec_param h264_slice_param;
166 unsigned int resolution_changed;
167 unsigned int realloc_mv_buf;
168 unsigned int cap_num_planes;
170 struct v4l2_h264_dpb_entry dpb[16];
171 bool is_field_bitstream;
174 static int vdec_h264_slice_fill_decode_parameters(struct vdec_h264_slice_inst *inst,
175 struct vdec_h264_slice_share_info *share_info)
177 struct vdec_h264_slice_lat_dec_param *slice_param = &inst->vsi->h264_slice_params;
178 const struct v4l2_ctrl_h264_decode_params *dec_params;
179 const struct v4l2_ctrl_h264_scaling_matrix *src_matrix;
180 const struct v4l2_ctrl_h264_sps *sps;
181 const struct v4l2_ctrl_h264_pps *pps;
184 mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
185 if (IS_ERR(dec_params))
186 return PTR_ERR(dec_params);
189 mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX);
190 if (IS_ERR(src_matrix))
191 return PTR_ERR(src_matrix);
193 sps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SPS);
197 pps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_PPS);
201 if (dec_params->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC) {
202 mtk_vcodec_err(inst, "No support for H.264 field decoding.");
203 inst->is_field_bitstream = true;
207 mtk_vdec_h264_copy_sps_params(&slice_param->sps, sps);
208 mtk_vdec_h264_copy_pps_params(&slice_param->pps, pps);
209 mtk_vdec_h264_copy_scaling_matrix(&slice_param->scaling_matrix, src_matrix);
211 memcpy(&share_info->sps, sps, sizeof(*sps));
212 memcpy(&share_info->dec_params, dec_params, sizeof(*dec_params));
217 static int get_vdec_sig_decode_parameters(struct vdec_h264_slice_inst *inst)
219 const struct v4l2_ctrl_h264_decode_params *dec_params;
220 const struct v4l2_ctrl_h264_sps *sps;
221 const struct v4l2_ctrl_h264_pps *pps;
222 const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
223 struct vdec_h264_slice_lat_dec_param *slice_param = &inst->h264_slice_param;
224 struct v4l2_h264_reflist_builder reflist_builder;
225 struct v4l2_h264_reference v4l2_p0_reflist[V4L2_H264_REF_LIST_LEN];
226 struct v4l2_h264_reference v4l2_b0_reflist[V4L2_H264_REF_LIST_LEN];
227 struct v4l2_h264_reference v4l2_b1_reflist[V4L2_H264_REF_LIST_LEN];
228 u8 *p0_reflist = slice_param->decode_params.ref_pic_list_p0;
229 u8 *b0_reflist = slice_param->decode_params.ref_pic_list_b0;
230 u8 *b1_reflist = slice_param->decode_params.ref_pic_list_b1;
233 mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
234 if (IS_ERR(dec_params))
235 return PTR_ERR(dec_params);
237 sps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SPS);
241 pps = mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_PPS);
246 mtk_vdec_h264_get_ctrl_ptr(inst->ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX);
247 if (IS_ERR(scaling_matrix))
248 return PTR_ERR(scaling_matrix);
250 mtk_vdec_h264_update_dpb(dec_params, inst->dpb);
252 mtk_vdec_h264_copy_sps_params(&slice_param->sps, sps);
253 mtk_vdec_h264_copy_pps_params(&slice_param->pps, pps);
254 mtk_vdec_h264_copy_scaling_matrix(&slice_param->scaling_matrix, scaling_matrix);
256 mtk_vdec_h264_copy_decode_params(&slice_param->decode_params, dec_params, inst->dpb);
257 mtk_vdec_h264_fill_dpb_info(inst->ctx, &slice_param->decode_params,
258 slice_param->h264_dpb_info);
260 /* Build the reference lists */
261 v4l2_h264_init_reflist_builder(&reflist_builder, dec_params, sps, inst->dpb);
262 v4l2_h264_build_p_ref_list(&reflist_builder, v4l2_p0_reflist);
263 v4l2_h264_build_b_ref_lists(&reflist_builder, v4l2_b0_reflist, v4l2_b1_reflist);
265 /* Adapt the built lists to the firmware's expectations */
266 mtk_vdec_h264_get_ref_list(p0_reflist, v4l2_p0_reflist, reflist_builder.num_valid);
267 mtk_vdec_h264_get_ref_list(b0_reflist, v4l2_b0_reflist, reflist_builder.num_valid);
268 mtk_vdec_h264_get_ref_list(b1_reflist, v4l2_b1_reflist, reflist_builder.num_valid);
270 memcpy(&inst->vsi_ctx.h264_slice_params, slice_param,
271 sizeof(inst->vsi_ctx.h264_slice_params));
276 static void vdec_h264_slice_fill_decode_reflist(struct vdec_h264_slice_inst *inst,
277 struct vdec_h264_slice_lat_dec_param *slice_param,
278 struct vdec_h264_slice_share_info *share_info)
280 struct v4l2_ctrl_h264_decode_params *dec_params = &share_info->dec_params;
281 struct v4l2_ctrl_h264_sps *sps = &share_info->sps;
282 struct v4l2_h264_reflist_builder reflist_builder;
283 struct v4l2_h264_reference v4l2_p0_reflist[V4L2_H264_REF_LIST_LEN];
284 struct v4l2_h264_reference v4l2_b0_reflist[V4L2_H264_REF_LIST_LEN];
285 struct v4l2_h264_reference v4l2_b1_reflist[V4L2_H264_REF_LIST_LEN];
286 u8 *p0_reflist = slice_param->decode_params.ref_pic_list_p0;
287 u8 *b0_reflist = slice_param->decode_params.ref_pic_list_b0;
288 u8 *b1_reflist = slice_param->decode_params.ref_pic_list_b1;
290 mtk_vdec_h264_update_dpb(dec_params, inst->dpb);
292 mtk_vdec_h264_copy_decode_params(&slice_param->decode_params, dec_params,
294 mtk_vdec_h264_fill_dpb_info(inst->ctx, &slice_param->decode_params,
295 slice_param->h264_dpb_info);
297 mtk_v4l2_debug(3, "cur poc = %d\n", dec_params->bottom_field_order_cnt);
298 /* Build the reference lists */
299 v4l2_h264_init_reflist_builder(&reflist_builder, dec_params, sps,
301 v4l2_h264_build_p_ref_list(&reflist_builder, v4l2_p0_reflist);
302 v4l2_h264_build_b_ref_lists(&reflist_builder, v4l2_b0_reflist, v4l2_b1_reflist);
304 /* Adapt the built lists to the firmware's expectations */
305 mtk_vdec_h264_get_ref_list(p0_reflist, v4l2_p0_reflist, reflist_builder.num_valid);
306 mtk_vdec_h264_get_ref_list(b0_reflist, v4l2_b0_reflist, reflist_builder.num_valid);
307 mtk_vdec_h264_get_ref_list(b1_reflist, v4l2_b1_reflist, reflist_builder.num_valid);
310 static int vdec_h264_slice_alloc_mv_buf(struct vdec_h264_slice_inst *inst,
311 struct vdec_pic_info *pic)
313 unsigned int buf_sz = mtk_vdec_h264_get_mv_buf_size(pic->buf_w, pic->buf_h);
314 struct mtk_vcodec_mem *mem;
317 mtk_v4l2_debug(3, "size = 0x%x", buf_sz);
318 for (i = 0; i < H264_MAX_MV_NUM; i++) {
319 mem = &inst->mv_buf[i];
321 mtk_vcodec_mem_free(inst->ctx, mem);
323 err = mtk_vcodec_mem_alloc(inst->ctx, mem);
325 mtk_vcodec_err(inst, "failed to allocate mv buf");
333 static void vdec_h264_slice_free_mv_buf(struct vdec_h264_slice_inst *inst)
336 struct mtk_vcodec_mem *mem;
338 for (i = 0; i < H264_MAX_MV_NUM; i++) {
339 mem = &inst->mv_buf[i];
341 mtk_vcodec_mem_free(inst->ctx, mem);
345 static void vdec_h264_slice_get_pic_info(struct vdec_h264_slice_inst *inst)
347 struct mtk_vcodec_ctx *ctx = inst->ctx;
350 data[0] = ctx->picinfo.pic_w;
351 data[1] = ctx->picinfo.pic_h;
352 data[2] = ctx->capture_fourcc;
353 vpu_dec_get_param(&inst->vpu, data, 3, GET_PARAM_PIC_INFO);
355 ctx->picinfo.buf_w = ALIGN(ctx->picinfo.pic_w, VCODEC_DEC_ALIGNED_64);
356 ctx->picinfo.buf_h = ALIGN(ctx->picinfo.pic_h, VCODEC_DEC_ALIGNED_64);
357 ctx->picinfo.fb_sz[0] = inst->vpu.fb_sz[0];
358 ctx->picinfo.fb_sz[1] = inst->vpu.fb_sz[1];
359 inst->cap_num_planes =
360 ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes;
362 mtk_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)",
363 ctx->picinfo.pic_w, ctx->picinfo.pic_h,
364 ctx->picinfo.buf_w, ctx->picinfo.buf_h);
365 mtk_vcodec_debug(inst, "Y/C(%d, %d)", ctx->picinfo.fb_sz[0],
366 ctx->picinfo.fb_sz[1]);
368 if (ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w ||
369 ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h) {
370 inst->resolution_changed = true;
371 if (ctx->last_decoded_picinfo.buf_w != ctx->picinfo.buf_w ||
372 ctx->last_decoded_picinfo.buf_h != ctx->picinfo.buf_h)
373 inst->realloc_mv_buf = true;
375 mtk_v4l2_debug(1, "resChg: (%d %d) : old(%d, %d) -> new(%d, %d)",
376 inst->resolution_changed,
377 inst->realloc_mv_buf,
378 ctx->last_decoded_picinfo.pic_w,
379 ctx->last_decoded_picinfo.pic_h,
380 ctx->picinfo.pic_w, ctx->picinfo.pic_h);
384 static void vdec_h264_slice_get_crop_info(struct vdec_h264_slice_inst *inst,
385 struct v4l2_rect *cr)
389 cr->width = inst->ctx->picinfo.pic_w;
390 cr->height = inst->ctx->picinfo.pic_h;
392 mtk_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d",
393 cr->left, cr->top, cr->width, cr->height);
396 static int vdec_h264_slice_init(struct mtk_vcodec_ctx *ctx)
398 struct vdec_h264_slice_inst *inst;
401 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
407 inst->vpu.id = SCP_IPI_VDEC_LAT;
408 inst->vpu.core_id = SCP_IPI_VDEC_CORE;
410 inst->vpu.codec_type = ctx->current_codec;
411 inst->vpu.capture_type = ctx->capture_fourcc;
413 err = vpu_dec_init(&inst->vpu);
415 mtk_vcodec_err(inst, "vdec_h264 init err=%d", err);
416 goto error_free_inst;
419 vsi_size = round_up(sizeof(struct vdec_h264_slice_vsi), VCODEC_DEC_ALIGNED_64);
420 inst->vsi = inst->vpu.vsi;
422 (struct vdec_h264_slice_vsi *)(((char *)inst->vpu.vsi) + vsi_size);
423 inst->resolution_changed = true;
424 inst->realloc_mv_buf = true;
426 mtk_vcodec_debug(inst, "lat struct size = %d,%d,%d,%d vsi: %d\n",
427 (int)sizeof(struct mtk_h264_sps_param),
428 (int)sizeof(struct mtk_h264_pps_param),
429 (int)sizeof(struct vdec_h264_slice_lat_dec_param),
430 (int)sizeof(struct mtk_h264_dpb_info),
432 mtk_vcodec_debug(inst, "lat H264 instance >> %p, codec_type = 0x%x",
433 inst, inst->vpu.codec_type);
435 ctx->drv_handle = inst;
443 static void vdec_h264_slice_deinit(void *h_vdec)
445 struct vdec_h264_slice_inst *inst = h_vdec;
447 mtk_vcodec_debug_enter(inst);
449 vpu_dec_deinit(&inst->vpu);
450 vdec_h264_slice_free_mv_buf(inst);
451 vdec_msg_queue_deinit(&inst->ctx->msg_queue, inst->ctx);
456 static int vdec_h264_slice_core_decode(struct vdec_lat_buf *lat_buf)
460 u64 y_fb_dma, c_fb_dma;
462 struct mtk_vcodec_ctx *ctx = lat_buf->ctx;
463 struct vdec_h264_slice_inst *inst = ctx->drv_handle;
464 struct vb2_v4l2_buffer *vb2_v4l2;
465 struct vdec_h264_slice_share_info *share_info = lat_buf->private_data;
466 struct mtk_vcodec_mem *mem;
467 struct vdec_vpu_inst *vpu = &inst->vpu;
469 mtk_vcodec_debug(inst, "[h264-core] vdec_h264 core decode");
470 memcpy(&inst->vsi_core->h264_slice_params, &share_info->h264_slice_params,
471 sizeof(share_info->h264_slice_params));
473 fb = ctx->dev->vdec_pdata->get_cap_buffer(ctx);
474 y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
475 vdec_fb_va = (unsigned long)fb;
477 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1)
479 y_fb_dma + inst->ctx->picinfo.buf_w * inst->ctx->picinfo.buf_h;
481 c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
483 mtk_vcodec_debug(inst, "[h264-core] y/c addr = 0x%llx 0x%llx", y_fb_dma,
486 inst->vsi_core->dec.y_fb_dma = y_fb_dma;
487 inst->vsi_core->dec.c_fb_dma = c_fb_dma;
488 inst->vsi_core->dec.vdec_fb_va = vdec_fb_va;
489 inst->vsi_core->dec.nal_info = share_info->nal_info;
490 inst->vsi_core->wdma_start_addr =
491 lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
492 inst->vsi_core->wdma_end_addr =
493 lat_buf->ctx->msg_queue.wdma_addr.dma_addr +
494 lat_buf->ctx->msg_queue.wdma_addr.size;
495 inst->vsi_core->wdma_err_addr = lat_buf->wdma_err_addr.dma_addr;
496 inst->vsi_core->slice_bc_start_addr = lat_buf->slice_bc_addr.dma_addr;
497 inst->vsi_core->slice_bc_end_addr = lat_buf->slice_bc_addr.dma_addr +
498 lat_buf->slice_bc_addr.size;
499 inst->vsi_core->trans_start = share_info->trans_start;
500 inst->vsi_core->trans_end = share_info->trans_end;
501 for (i = 0; i < H264_MAX_MV_NUM; i++) {
502 mem = &inst->mv_buf[i];
503 inst->vsi_core->mv_buf_dma[i] = mem->dma_addr;
506 vb2_v4l2 = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
507 v4l2_m2m_buf_copy_metadata(&lat_buf->ts_info, vb2_v4l2, true);
509 vdec_h264_slice_fill_decode_reflist(inst, &inst->vsi_core->h264_slice_params,
512 err = vpu_dec_core(vpu);
514 mtk_vcodec_err(inst, "core decode err=%d", err);
518 /* wait decoder done interrupt */
519 timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
520 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
522 mtk_vcodec_err(inst, "core decode timeout: pic_%d",
523 ctx->decoded_frame_cnt);
524 inst->vsi_core->dec.timeout = !!timeout;
526 vpu_dec_core_end(vpu);
527 mtk_vcodec_debug(inst, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
528 ctx->decoded_frame_cnt,
529 inst->vsi_core->dec.crc[0], inst->vsi_core->dec.crc[1],
530 inst->vsi_core->dec.crc[2], inst->vsi_core->dec.crc[3],
531 inst->vsi_core->dec.crc[4], inst->vsi_core->dec.crc[5],
532 inst->vsi_core->dec.crc[6], inst->vsi_core->dec.crc[7]);
535 vdec_msg_queue_update_ube_rptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
536 ctx->dev->vdec_pdata->cap_to_disp(ctx, !!err, lat_buf->src_buf_req);
537 mtk_vcodec_debug(inst, "core decode done err=%d", err);
538 ctx->decoded_frame_cnt++;
542 static int vdec_h264_slice_lat_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
543 struct vdec_fb *fb, bool *res_chg)
545 struct vdec_h264_slice_inst *inst = h_vdec;
546 struct vdec_vpu_inst *vpu = &inst->vpu;
547 struct mtk_video_dec_buf *src_buf_info;
548 int nal_start_idx, err, timeout = 0, i;
549 unsigned int data[2];
550 struct vdec_lat_buf *lat_buf;
551 struct vdec_h264_slice_share_info *share_info;
553 struct mtk_vcodec_mem *mem;
555 if (vdec_msg_queue_init(&inst->ctx->msg_queue, inst->ctx,
556 vdec_h264_slice_core_decode,
557 sizeof(*share_info)))
560 /* bs NULL means flush decoder */
562 vdec_msg_queue_wait_lat_buf_full(&inst->ctx->msg_queue);
563 return vpu_dec_reset(vpu);
566 if (inst->is_field_bitstream)
569 lat_buf = vdec_msg_queue_dqbuf(&inst->ctx->msg_queue.lat_ctx);
571 mtk_vcodec_err(inst, "failed to get lat buffer");
574 share_info = lat_buf->private_data;
575 src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
577 buf = (unsigned char *)bs->va;
578 nal_start_idx = mtk_vdec_h264_find_start_code(buf, bs->size);
579 if (nal_start_idx < 0) {
581 goto err_free_fb_out;
584 inst->vsi->dec.nal_info = buf[nal_start_idx];
585 inst->vsi->dec.bs_buf_addr = (u64)bs->dma_addr;
586 inst->vsi->dec.bs_buf_size = bs->size;
588 lat_buf->src_buf_req = src_buf_info->m2m_buf.vb.vb2_buf.req_obj.req;
589 v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb, &lat_buf->ts_info, true);
591 err = vdec_h264_slice_fill_decode_parameters(inst, share_info);
593 goto err_free_fb_out;
595 *res_chg = inst->resolution_changed;
596 if (inst->resolution_changed) {
597 mtk_vcodec_debug(inst, "- resolution changed -");
598 if (inst->realloc_mv_buf) {
599 err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
600 inst->realloc_mv_buf = false;
602 goto err_free_fb_out;
604 inst->resolution_changed = false;
606 for (i = 0; i < H264_MAX_MV_NUM; i++) {
607 mem = &inst->mv_buf[i];
608 inst->vsi->mv_buf_dma[i] = mem->dma_addr;
610 inst->vsi->wdma_start_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr;
611 inst->vsi->wdma_end_addr = lat_buf->ctx->msg_queue.wdma_addr.dma_addr +
612 lat_buf->ctx->msg_queue.wdma_addr.size;
613 inst->vsi->wdma_err_addr = lat_buf->wdma_err_addr.dma_addr;
614 inst->vsi->slice_bc_start_addr = lat_buf->slice_bc_addr.dma_addr;
615 inst->vsi->slice_bc_end_addr = lat_buf->slice_bc_addr.dma_addr +
616 lat_buf->slice_bc_addr.size;
618 inst->vsi->trans_end = inst->ctx->msg_queue.wdma_rptr_addr;
619 inst->vsi->trans_start = inst->ctx->msg_queue.wdma_wptr_addr;
620 mtk_vcodec_debug(inst, "lat:trans(0x%llx 0x%llx) err:0x%llx",
621 inst->vsi->wdma_start_addr,
622 inst->vsi->wdma_end_addr,
623 inst->vsi->wdma_err_addr);
625 mtk_vcodec_debug(inst, "slice(0x%llx 0x%llx) rprt((0x%llx 0x%llx))",
626 inst->vsi->slice_bc_start_addr,
627 inst->vsi->slice_bc_end_addr,
628 inst->vsi->trans_start,
629 inst->vsi->trans_end);
630 err = vpu_dec_start(vpu, data, 2);
632 mtk_vcodec_debug(inst, "lat decode err: %d", err);
636 share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
637 inst->vsi->wdma_end_addr_offset;
638 share_info->trans_start = inst->ctx->msg_queue.wdma_wptr_addr;
639 share_info->nal_info = inst->vsi->dec.nal_info;
641 if (IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
642 memcpy(&share_info->h264_slice_params, &inst->vsi->h264_slice_params,
643 sizeof(share_info->h264_slice_params));
644 vdec_msg_queue_qbuf(&inst->ctx->dev->msg_queue_core_ctx, lat_buf);
647 /* wait decoder done interrupt */
648 timeout = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
649 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_LAT0);
650 inst->vsi->dec.timeout = !!timeout;
652 err = vpu_dec_end(vpu);
653 if (err == SLICE_HEADER_FULL || timeout || err == TRANS_BUFFER_FULL) {
658 share_info->trans_end = inst->ctx->msg_queue.wdma_addr.dma_addr +
659 inst->vsi->wdma_end_addr_offset;
660 vdec_msg_queue_update_ube_wptr(&lat_buf->ctx->msg_queue, share_info->trans_end);
662 if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability)) {
663 memcpy(&share_info->h264_slice_params, &inst->vsi->h264_slice_params,
664 sizeof(share_info->h264_slice_params));
665 vdec_msg_queue_qbuf(&inst->ctx->dev->msg_queue_core_ctx, lat_buf);
667 mtk_vcodec_debug(inst, "dec num: %d lat crc: 0x%x 0x%x 0x%x", inst->slice_dec_num,
668 inst->vsi->dec.crc[0], inst->vsi->dec.crc[1], inst->vsi->dec.crc[2]);
670 inst->slice_dec_num++;
674 if (!IS_VDEC_INNER_RACING(inst->ctx->dev->dec_capability))
675 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
677 vdec_msg_queue_qbuf(&inst->ctx->msg_queue.lat_ctx, lat_buf);
678 mtk_vcodec_err(inst, "slice dec number: %d err: %d", inst->slice_dec_num, err);
682 static int vdec_h264_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
683 struct vdec_fb *unused, bool *res_chg)
685 struct vdec_h264_slice_inst *inst = h_vdec;
686 struct vdec_vpu_inst *vpu = &inst->vpu;
687 struct mtk_video_dec_buf *src_buf_info, *dst_buf_info;
690 unsigned int data[2], i;
691 u64 y_fb_dma, c_fb_dma;
692 struct mtk_vcodec_mem *mem;
693 int err, nal_start_idx;
695 /* bs NULL means flush decoder */
697 return vpu_dec_reset(vpu);
699 fb = inst->ctx->dev->vdec_pdata->get_cap_buffer(inst->ctx);
700 src_buf_info = container_of(bs, struct mtk_video_dec_buf, bs_buffer);
701 dst_buf_info = container_of(fb, struct mtk_video_dec_buf, frame_buffer);
703 y_fb_dma = fb ? (u64)fb->base_y.dma_addr : 0;
704 c_fb_dma = fb ? (u64)fb->base_c.dma_addr : 0;
705 mtk_vcodec_debug(inst, "[h264-dec] [%d] y_dma=%llx c_dma=%llx",
706 inst->ctx->decoded_frame_cnt, y_fb_dma, c_fb_dma);
708 inst->vsi_ctx.dec.bs_buf_addr = (u64)bs->dma_addr;
709 inst->vsi_ctx.dec.bs_buf_size = bs->size;
710 inst->vsi_ctx.dec.y_fb_dma = y_fb_dma;
711 inst->vsi_ctx.dec.c_fb_dma = c_fb_dma;
712 inst->vsi_ctx.dec.vdec_fb_va = (u64)(uintptr_t)fb;
714 v4l2_m2m_buf_copy_metadata(&src_buf_info->m2m_buf.vb,
715 &dst_buf_info->m2m_buf.vb, true);
716 err = get_vdec_sig_decode_parameters(inst);
718 goto err_free_fb_out;
720 buf = (unsigned char *)bs->va;
721 nal_start_idx = mtk_vdec_h264_find_start_code(buf, bs->size);
722 if (nal_start_idx < 0) {
724 goto err_free_fb_out;
726 inst->vsi_ctx.dec.nal_info = buf[nal_start_idx];
728 *res_chg = inst->resolution_changed;
729 if (inst->resolution_changed) {
730 mtk_vcodec_debug(inst, "- resolution changed -");
731 if (inst->realloc_mv_buf) {
732 err = vdec_h264_slice_alloc_mv_buf(inst, &inst->ctx->picinfo);
733 inst->realloc_mv_buf = false;
735 goto err_free_fb_out;
737 inst->resolution_changed = false;
739 for (i = 0; i < H264_MAX_MV_NUM; i++) {
740 mem = &inst->mv_buf[i];
741 inst->vsi_ctx.mv_buf_dma[i] = mem->dma_addr;
745 memcpy(inst->vpu.vsi, &inst->vsi_ctx, sizeof(inst->vsi_ctx));
746 err = vpu_dec_start(vpu, data, 2);
748 goto err_free_fb_out;
750 /* wait decoder done interrupt */
751 err = mtk_vcodec_wait_for_done_ctx(inst->ctx, MTK_INST_IRQ_RECEIVED,
752 WAIT_INTR_TIMEOUT_MS, MTK_VDEC_CORE);
754 mtk_vcodec_err(inst, "decode timeout: pic_%d",
755 inst->ctx->decoded_frame_cnt);
757 inst->vsi->dec.timeout = !!err;
758 err = vpu_dec_end(vpu);
760 goto err_free_fb_out;
762 memcpy(&inst->vsi_ctx, inst->vpu.vsi, sizeof(inst->vsi_ctx));
763 mtk_vcodec_debug(inst, "pic[%d] crc: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
764 inst->ctx->decoded_frame_cnt,
765 inst->vsi_ctx.dec.crc[0], inst->vsi_ctx.dec.crc[1],
766 inst->vsi_ctx.dec.crc[2], inst->vsi_ctx.dec.crc[3],
767 inst->vsi_ctx.dec.crc[4], inst->vsi_ctx.dec.crc[5],
768 inst->vsi_ctx.dec.crc[6], inst->vsi_ctx.dec.crc[7]);
770 inst->ctx->decoded_frame_cnt++;
774 mtk_vcodec_err(inst, "dec frame number: %d err: %d",
775 inst->ctx->decoded_frame_cnt, err);
779 static int vdec_h264_slice_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
780 struct vdec_fb *unused, bool *res_chg)
782 struct vdec_h264_slice_inst *inst = h_vdec;
788 if (inst->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
789 ret = vdec_h264_slice_single_decode(h_vdec, bs, unused, res_chg);
791 ret = vdec_h264_slice_lat_decode(h_vdec, bs, unused, res_chg);
796 static int vdec_h264_slice_get_param(void *h_vdec, enum vdec_get_param_type type,
799 struct vdec_h264_slice_inst *inst = h_vdec;
802 case GET_PARAM_PIC_INFO:
803 vdec_h264_slice_get_pic_info(inst);
805 case GET_PARAM_DPB_SIZE:
806 *(unsigned int *)out = 6;
808 case GET_PARAM_CROP_INFO:
809 vdec_h264_slice_get_crop_info(inst, out);
812 mtk_vcodec_err(inst, "invalid get parameter type=%d", type);
818 const struct vdec_common_if vdec_h264_slice_multi_if = {
819 .init = vdec_h264_slice_init,
820 .decode = vdec_h264_slice_decode,
821 .get_param = vdec_h264_slice_get_param,
822 .deinit = vdec_h264_slice_deinit,