• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 MediaTek Inc.
4  * Author: PC Chen <pc.chen@mediatek.com>
5  *         Tiffany Lin <tiffany.lin@mediatek.com>
6  */
7 
8 #include <media/v4l2-event.h>
9 #include <media/v4l2-mem2mem.h>
10 #include <media/videobuf2-dma-contig.h>
11 
12 #include "mtk_vcodec_drv.h"
13 #include "mtk_vcodec_dec.h"
14 #include "mtk_vcodec_intr.h"
15 #include "mtk_vcodec_util.h"
16 #include "vdec_drv_if.h"
17 #include "mtk_vcodec_dec_pm.h"
18 
19 #define OUT_FMT_IDX	0
20 #define CAP_FMT_IDX	3
21 
22 #define MTK_VDEC_MIN_W	64U
23 #define MTK_VDEC_MIN_H	64U
24 #define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
25 #define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
26 
27 static const struct mtk_video_fmt mtk_video_formats[] = {
28 	{
29 		.fourcc = V4L2_PIX_FMT_H264,
30 		.type = MTK_FMT_DEC,
31 		.num_planes = 1,
32 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
33 	},
34 	{
35 		.fourcc = V4L2_PIX_FMT_VP8,
36 		.type = MTK_FMT_DEC,
37 		.num_planes = 1,
38 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
39 	},
40 	{
41 		.fourcc = V4L2_PIX_FMT_VP9,
42 		.type = MTK_FMT_DEC,
43 		.num_planes = 1,
44 		.flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
45 	},
46 	{
47 		.fourcc = V4L2_PIX_FMT_MT21C,
48 		.type = MTK_FMT_FRAME,
49 		.num_planes = 2,
50 	},
51 };
52 
53 static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
54 	{
55 		.fourcc	= V4L2_PIX_FMT_H264,
56 		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
57 				MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
58 	},
59 	{
60 		.fourcc	= V4L2_PIX_FMT_VP8,
61 		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
62 				MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
63 	},
64 	{
65 		.fourcc = V4L2_PIX_FMT_VP9,
66 		.stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
67 				MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
68 	},
69 };
70 
71 #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
72 #define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
73 
mtk_vdec_find_format(struct v4l2_format * f)74 static const struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
75 {
76 	const struct mtk_video_fmt *fmt;
77 	unsigned int k;
78 
79 	for (k = 0; k < NUM_FORMATS; k++) {
80 		fmt = &mtk_video_formats[k];
81 		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
82 			return fmt;
83 	}
84 
85 	return NULL;
86 }
87 
mtk_vdec_get_q_data(struct mtk_vcodec_ctx * ctx,enum v4l2_buf_type type)88 static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
89 					      enum v4l2_buf_type type)
90 {
91 	if (V4L2_TYPE_IS_OUTPUT(type))
92 		return &ctx->q_data[MTK_Q_DATA_SRC];
93 
94 	return &ctx->q_data[MTK_Q_DATA_DST];
95 }
96 
97 /*
98  * This function tries to clean all display buffers, the buffers will return
99  * in display order.
100  * Note the buffers returned from codec driver may still be in driver's
101  * reference list.
102  */
get_display_buffer(struct mtk_vcodec_ctx * ctx)103 static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
104 {
105 	struct vdec_fb *disp_frame_buffer = NULL;
106 	struct mtk_video_dec_buf *dstbuf;
107 	struct vb2_v4l2_buffer *vb;
108 
109 	mtk_v4l2_debug(3, "[%d]", ctx->id);
110 	if (vdec_if_get_param(ctx,
111 			GET_PARAM_DISP_FRAME_BUFFER,
112 			&disp_frame_buffer)) {
113 		mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
114 			ctx->id);
115 		return NULL;
116 	}
117 
118 	if (disp_frame_buffer == NULL) {
119 		mtk_v4l2_debug(3, "No display frame buffer");
120 		return NULL;
121 	}
122 
123 	dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf,
124 				frame_buffer);
125 	vb = &dstbuf->m2m_buf.vb;
126 	mutex_lock(&ctx->lock);
127 	if (dstbuf->used) {
128 		vb2_set_plane_payload(&vb->vb2_buf, 0,
129 				      ctx->picinfo.fb_sz[0]);
130 		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
131 			vb2_set_plane_payload(&vb->vb2_buf, 1,
132 					      ctx->picinfo.fb_sz[1]);
133 
134 		mtk_v4l2_debug(2,
135 				"[%d]status=%x queue id=%d to done_list %d",
136 				ctx->id, disp_frame_buffer->status,
137 				vb->vb2_buf.index,
138 				dstbuf->queued_in_vb2);
139 
140 		v4l2_m2m_buf_done(vb, VB2_BUF_STATE_DONE);
141 		ctx->decoded_frame_cnt++;
142 	}
143 	mutex_unlock(&ctx->lock);
144 	return &vb->vb2_buf;
145 }
146 
147 /*
148  * This function tries to clean all capture buffers that are not used as
149  * reference buffers by codec driver any more
150  * In this case, we need re-queue buffer to vb2 buffer if user space
151  * already returns this buffer to v4l2 or this buffer is just the output of
152  * previous sps/pps/resolution change decode, or do nothing if user
153  * space still owns this buffer
154  */
get_free_buffer(struct mtk_vcodec_ctx * ctx)155 static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
156 {
157 	struct mtk_video_dec_buf *dstbuf;
158 	struct vdec_fb *free_frame_buffer = NULL;
159 	struct vb2_v4l2_buffer *vb;
160 
161 	if (vdec_if_get_param(ctx,
162 				GET_PARAM_FREE_FRAME_BUFFER,
163 				&free_frame_buffer)) {
164 		mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->id);
165 		return NULL;
166 	}
167 	if (free_frame_buffer == NULL) {
168 		mtk_v4l2_debug(3, " No free frame buffer");
169 		return NULL;
170 	}
171 
172 	mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
173 			ctx->id, free_frame_buffer);
174 
175 	dstbuf = container_of(free_frame_buffer, struct mtk_video_dec_buf,
176 				frame_buffer);
177 	vb = &dstbuf->m2m_buf.vb;
178 
179 	mutex_lock(&ctx->lock);
180 	if (dstbuf->used) {
181 		if ((dstbuf->queued_in_vb2) &&
182 		    (dstbuf->queued_in_v4l2) &&
183 		    (free_frame_buffer->status == FB_ST_FREE)) {
184 			/*
185 			 * After decode sps/pps or non-display buffer, we don't
186 			 * need to return capture buffer to user space, but
187 			 * just re-queue this capture buffer to vb2 queue.
188 			 * This reduce overheads that dq/q unused capture
189 			 * buffer. In this case, queued_in_vb2 = true.
190 			 */
191 			mtk_v4l2_debug(2,
192 				"[%d]status=%x queue id=%d to rdy_queue %d",
193 				ctx->id, free_frame_buffer->status,
194 				vb->vb2_buf.index,
195 				dstbuf->queued_in_vb2);
196 			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
197 		} else if (!dstbuf->queued_in_vb2 && dstbuf->queued_in_v4l2) {
198 			/*
199 			 * If buffer in v4l2 driver but not in vb2 queue yet,
200 			 * and we get this buffer from free_list, it means
201 			 * that codec driver do not use this buffer as
202 			 * reference buffer anymore. We should q buffer to vb2
203 			 * queue, so later work thread could get this buffer
204 			 * for decode. In this case, queued_in_vb2 = false
205 			 * means this buffer is not from previous decode
206 			 * output.
207 			 */
208 			mtk_v4l2_debug(2,
209 					"[%d]status=%x queue id=%d to rdy_queue",
210 					ctx->id, free_frame_buffer->status,
211 					vb->vb2_buf.index);
212 			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
213 			dstbuf->queued_in_vb2 = true;
214 		} else {
215 			/*
216 			 * Codec driver do not need to reference this capture
217 			 * buffer and this buffer is not in v4l2 driver.
218 			 * Then we don't need to do any thing, just add log when
219 			 * we need to debug buffer flow.
220 			 * When this buffer q from user space, it could
221 			 * directly q to vb2 buffer
222 			 */
223 			mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d",
224 					ctx->id, free_frame_buffer->status,
225 					vb->vb2_buf.index,
226 					dstbuf->queued_in_vb2,
227 					dstbuf->queued_in_v4l2);
228 		}
229 		dstbuf->used = false;
230 	}
231 	mutex_unlock(&ctx->lock);
232 	return &vb->vb2_buf;
233 }
234 
clean_display_buffer(struct mtk_vcodec_ctx * ctx)235 static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
236 {
237 	struct vb2_buffer *framptr;
238 
239 	do {
240 		framptr = get_display_buffer(ctx);
241 	} while (framptr);
242 }
243 
clean_free_buffer(struct mtk_vcodec_ctx * ctx)244 static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
245 {
246 	struct vb2_buffer *framptr;
247 
248 	do {
249 		framptr = get_free_buffer(ctx);
250 	} while (framptr);
251 }
252 
mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx * ctx)253 static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
254 {
255 	static const struct v4l2_event ev_src_ch = {
256 		.type = V4L2_EVENT_SOURCE_CHANGE,
257 		.u.src_change.changes =
258 		V4L2_EVENT_SRC_CH_RESOLUTION,
259 	};
260 
261 	mtk_v4l2_debug(1, "[%d]", ctx->id);
262 	v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
263 }
264 
mtk_vdec_flush_decoder(struct mtk_vcodec_ctx * ctx)265 static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
266 {
267 	bool res_chg;
268 	int ret = 0;
269 
270 	ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
271 	if (ret)
272 		mtk_v4l2_err("DecodeFinal failed, ret=%d", ret);
273 
274 	clean_display_buffer(ctx);
275 	clean_free_buffer(ctx);
276 }
277 
mtk_vdec_update_fmt(struct mtk_vcodec_ctx * ctx,unsigned int pixelformat)278 static void mtk_vdec_update_fmt(struct mtk_vcodec_ctx *ctx,
279 				unsigned int pixelformat)
280 {
281 	const struct mtk_video_fmt *fmt;
282 	struct mtk_q_data *dst_q_data;
283 	unsigned int k;
284 
285 	dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
286 	for (k = 0; k < NUM_FORMATS; k++) {
287 		fmt = &mtk_video_formats[k];
288 		if (fmt->fourcc == pixelformat) {
289 			mtk_v4l2_debug(1, "Update cap fourcc(%d -> %d)",
290 				dst_q_data->fmt->fourcc, pixelformat);
291 			dst_q_data->fmt = fmt;
292 			return;
293 		}
294 	}
295 
296 	mtk_v4l2_err("Cannot get fourcc(%d), using init value", pixelformat);
297 }
298 
mtk_vdec_pic_info_update(struct mtk_vcodec_ctx * ctx)299 static int mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
300 {
301 	unsigned int dpbsize = 0;
302 	int ret;
303 
304 	if (vdec_if_get_param(ctx,
305 				GET_PARAM_PIC_INFO,
306 				&ctx->last_decoded_picinfo)) {
307 		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
308 				ctx->id);
309 		return -EINVAL;
310 	}
311 
312 	if (ctx->last_decoded_picinfo.pic_w == 0 ||
313 		ctx->last_decoded_picinfo.pic_h == 0 ||
314 		ctx->last_decoded_picinfo.buf_w == 0 ||
315 		ctx->last_decoded_picinfo.buf_h == 0) {
316 		mtk_v4l2_err("Cannot get correct pic info");
317 		return -EINVAL;
318 	}
319 
320 	if (ctx->last_decoded_picinfo.cap_fourcc != ctx->picinfo.cap_fourcc &&
321 		ctx->picinfo.cap_fourcc != 0)
322 		mtk_vdec_update_fmt(ctx, ctx->picinfo.cap_fourcc);
323 
324 	if ((ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w) ||
325 	    (ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h))
326 		return 0;
327 
328 	mtk_v4l2_debug(1,
329 			"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
330 			ctx->id, ctx->last_decoded_picinfo.pic_w,
331 			ctx->last_decoded_picinfo.pic_h,
332 			ctx->picinfo.pic_w, ctx->picinfo.pic_h,
333 			ctx->last_decoded_picinfo.buf_w,
334 			ctx->last_decoded_picinfo.buf_h);
335 
336 	ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
337 	if (dpbsize == 0)
338 		mtk_v4l2_err("Incorrect dpb size, ret=%d", ret);
339 
340 	ctx->dpb_size = dpbsize;
341 
342 	return ret;
343 }
344 
mtk_vdec_worker(struct work_struct * work)345 static void mtk_vdec_worker(struct work_struct *work)
346 {
347 	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
348 				decode_work);
349 	struct mtk_vcodec_dev *dev = ctx->dev;
350 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
351 	struct mtk_vcodec_mem buf;
352 	struct vdec_fb *pfb;
353 	bool res_chg = false;
354 	int ret;
355 	struct mtk_video_dec_buf *dst_buf_info, *src_buf_info;
356 
357 	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
358 	if (src_buf == NULL) {
359 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
360 		mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->id);
361 		return;
362 	}
363 
364 	dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
365 	if (dst_buf == NULL) {
366 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
367 		mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
368 		return;
369 	}
370 
371 	src_buf_info = container_of(src_buf, struct mtk_video_dec_buf,
372 				    m2m_buf.vb);
373 	dst_buf_info = container_of(dst_buf, struct mtk_video_dec_buf,
374 				    m2m_buf.vb);
375 
376 	pfb = &dst_buf_info->frame_buffer;
377 	pfb->base_y.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
378 	pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
379 	pfb->base_y.size = ctx->picinfo.fb_sz[0];
380 
381 	pfb->base_c.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 1);
382 	pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 1);
383 	pfb->base_c.size = ctx->picinfo.fb_sz[1];
384 	pfb->status = 0;
385 	mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
386 
387 	mtk_v4l2_debug(3,
388 			"id=%d Framebuf  pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
389 			dst_buf->vb2_buf.index, pfb,
390 			pfb->base_y.va, &pfb->base_y.dma_addr,
391 			&pfb->base_c.dma_addr, pfb->base_y.size);
392 
393 	if (src_buf_info->lastframe) {
394 		mtk_v4l2_debug(1, "Got empty flush input buffer.");
395 		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
396 
397 		/* update dst buf status */
398 		dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
399 		mutex_lock(&ctx->lock);
400 		dst_buf_info->used = false;
401 		mutex_unlock(&ctx->lock);
402 
403 		vdec_if_decode(ctx, NULL, NULL, &res_chg);
404 		clean_display_buffer(ctx);
405 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
406 		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
407 			vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
408 		dst_buf->flags |= V4L2_BUF_FLAG_LAST;
409 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
410 		clean_free_buffer(ctx);
411 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
412 		return;
413 	}
414 	buf.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
415 	buf.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
416 	buf.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
417 	if (!buf.va) {
418 		v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
419 		mtk_v4l2_err("[%d] id=%d src_addr is NULL!!",
420 				ctx->id, src_buf->vb2_buf.index);
421 		return;
422 	}
423 	mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
424 			ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
425 	dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
426 	dst_buf->timecode = src_buf->timecode;
427 	mutex_lock(&ctx->lock);
428 	dst_buf_info->used = true;
429 	mutex_unlock(&ctx->lock);
430 	src_buf_info->used = true;
431 
432 	ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
433 
434 	if (ret) {
435 		mtk_v4l2_err(
436 			" <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
437 			ctx->id,
438 			src_buf->vb2_buf.index,
439 			buf.size,
440 			src_buf->vb2_buf.timestamp,
441 			dst_buf->vb2_buf.index,
442 			ret, res_chg);
443 		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
444 		if (ret == -EIO) {
445 			mutex_lock(&ctx->lock);
446 			src_buf_info->error = true;
447 			mutex_unlock(&ctx->lock);
448 		}
449 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
450 	} else if (!res_chg) {
451 		/*
452 		 * we only return src buffer with VB2_BUF_STATE_DONE
453 		 * when decode success without resolution change
454 		 */
455 		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
456 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
457 	}
458 
459 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
460 	clean_display_buffer(ctx);
461 	clean_free_buffer(ctx);
462 
463 	if (!ret && res_chg) {
464 		mtk_vdec_pic_info_update(ctx);
465 		/*
466 		 * On encountering a resolution change in the stream.
467 		 * The driver must first process and decode all
468 		 * remaining buffers from before the resolution change
469 		 * point, so call flush decode here
470 		 */
471 		mtk_vdec_flush_decoder(ctx);
472 		/*
473 		 * After all buffers containing decoded frames from
474 		 * before the resolution change point ready to be
475 		 * dequeued on the CAPTURE queue, the driver sends a
476 		 * V4L2_EVENT_SOURCE_CHANGE event for source change
477 		 * type V4L2_EVENT_SRC_CH_RESOLUTION
478 		 */
479 		mtk_vdec_queue_res_chg_event(ctx);
480 	}
481 	v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
482 }
483 
vidioc_try_decoder_cmd(struct file * file,void * priv,struct v4l2_decoder_cmd * cmd)484 static int vidioc_try_decoder_cmd(struct file *file, void *priv,
485 				struct v4l2_decoder_cmd *cmd)
486 {
487 	switch (cmd->cmd) {
488 	case V4L2_DEC_CMD_STOP:
489 	case V4L2_DEC_CMD_START:
490 		if (cmd->flags != 0) {
491 			mtk_v4l2_err("cmd->flags=%u", cmd->flags);
492 			return -EINVAL;
493 		}
494 		break;
495 	default:
496 		return -EINVAL;
497 	}
498 	return 0;
499 }
500 
501 
vidioc_decoder_cmd(struct file * file,void * priv,struct v4l2_decoder_cmd * cmd)502 static int vidioc_decoder_cmd(struct file *file, void *priv,
503 				struct v4l2_decoder_cmd *cmd)
504 {
505 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
506 	struct vb2_queue *src_vq, *dst_vq;
507 	int ret;
508 
509 	ret = vidioc_try_decoder_cmd(file, priv, cmd);
510 	if (ret)
511 		return ret;
512 
513 	mtk_v4l2_debug(1, "decoder cmd=%u", cmd->cmd);
514 	dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
515 				V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
516 	switch (cmd->cmd) {
517 	case V4L2_DEC_CMD_STOP:
518 		src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
519 				V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
520 		if (!vb2_is_streaming(src_vq)) {
521 			mtk_v4l2_debug(1, "Output stream is off. No need to flush.");
522 			return 0;
523 		}
524 		if (!vb2_is_streaming(dst_vq)) {
525 			mtk_v4l2_debug(1, "Capture stream is off. No need to flush.");
526 			return 0;
527 		}
528 		v4l2_m2m_buf_queue(ctx->m2m_ctx,
529 				   &ctx->empty_flush_buf->m2m_buf.vb);
530 		v4l2_m2m_try_schedule(ctx->m2m_ctx);
531 		break;
532 
533 	case V4L2_DEC_CMD_START:
534 		vb2_clear_last_buffer_dequeued(dst_vq);
535 		break;
536 
537 	default:
538 		return -EINVAL;
539 	}
540 
541 	return 0;
542 }
543 
mtk_vdec_unlock(struct mtk_vcodec_ctx * ctx)544 void mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
545 {
546 	mutex_unlock(&ctx->dev->dec_mutex);
547 }
548 
mtk_vdec_lock(struct mtk_vcodec_ctx * ctx)549 void mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
550 {
551 	mutex_lock(&ctx->dev->dec_mutex);
552 }
553 
mtk_vcodec_dec_release(struct mtk_vcodec_ctx * ctx)554 void mtk_vcodec_dec_release(struct mtk_vcodec_ctx *ctx)
555 {
556 	vdec_if_deinit(ctx);
557 	ctx->state = MTK_STATE_FREE;
558 }
559 
mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx * ctx)560 void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
561 {
562 	struct mtk_q_data *q_data;
563 
564 	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
565 	ctx->fh.m2m_ctx = ctx->m2m_ctx;
566 	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
567 	INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
568 	ctx->colorspace = V4L2_COLORSPACE_REC709;
569 	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
570 	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
571 	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
572 
573 	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
574 	memset(q_data, 0, sizeof(struct mtk_q_data));
575 	q_data->visible_width = DFT_CFG_WIDTH;
576 	q_data->visible_height = DFT_CFG_HEIGHT;
577 	q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
578 	q_data->field = V4L2_FIELD_NONE;
579 
580 	q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
581 	q_data->bytesperline[0] = 0;
582 
583 	q_data = &ctx->q_data[MTK_Q_DATA_DST];
584 	memset(q_data, 0, sizeof(struct mtk_q_data));
585 	q_data->visible_width = DFT_CFG_WIDTH;
586 	q_data->visible_height = DFT_CFG_HEIGHT;
587 	q_data->coded_width = DFT_CFG_WIDTH;
588 	q_data->coded_height = DFT_CFG_HEIGHT;
589 	q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
590 	q_data->field = V4L2_FIELD_NONE;
591 
592 	v4l_bound_align_image(&q_data->coded_width,
593 				MTK_VDEC_MIN_W,
594 				MTK_VDEC_MAX_W, 4,
595 				&q_data->coded_height,
596 				MTK_VDEC_MIN_H,
597 				MTK_VDEC_MAX_H, 5, 6);
598 
599 	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
600 	q_data->bytesperline[0] = q_data->coded_width;
601 	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
602 	q_data->bytesperline[1] = q_data->coded_width;
603 }
604 
vidioc_vdec_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)605 static int vidioc_vdec_qbuf(struct file *file, void *priv,
606 			    struct v4l2_buffer *buf)
607 {
608 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
609 
610 	if (ctx->state == MTK_STATE_ABORT) {
611 		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
612 				ctx->id);
613 		return -EIO;
614 	}
615 
616 	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
617 }
618 
vidioc_vdec_dqbuf(struct file * file,void * priv,struct v4l2_buffer * buf)619 static int vidioc_vdec_dqbuf(struct file *file, void *priv,
620 			     struct v4l2_buffer *buf)
621 {
622 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
623 
624 	if (ctx->state == MTK_STATE_ABORT) {
625 		mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
626 				ctx->id);
627 		return -EIO;
628 	}
629 
630 	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
631 }
632 
vidioc_vdec_querycap(struct file * file,void * priv,struct v4l2_capability * cap)633 static int vidioc_vdec_querycap(struct file *file, void *priv,
634 				struct v4l2_capability *cap)
635 {
636 	strscpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
637 	strscpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
638 	strscpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
639 
640 	return 0;
641 }
642 
vidioc_vdec_subscribe_evt(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)643 static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
644 				     const struct v4l2_event_subscription *sub)
645 {
646 	switch (sub->type) {
647 	case V4L2_EVENT_EOS:
648 		return v4l2_event_subscribe(fh, sub, 2, NULL);
649 	case V4L2_EVENT_SOURCE_CHANGE:
650 		return v4l2_src_change_event_subscribe(fh, sub);
651 	default:
652 		return v4l2_ctrl_subscribe_event(fh, sub);
653 	}
654 }
655 
vidioc_try_fmt(struct v4l2_format * f,const struct mtk_video_fmt * fmt)656 static int vidioc_try_fmt(struct v4l2_format *f,
657 			  const struct mtk_video_fmt *fmt)
658 {
659 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
660 	int i;
661 
662 	pix_fmt_mp->field = V4L2_FIELD_NONE;
663 
664 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
665 		pix_fmt_mp->num_planes = 1;
666 		pix_fmt_mp->plane_fmt[0].bytesperline = 0;
667 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
668 		int tmp_w, tmp_h;
669 
670 		pix_fmt_mp->height = clamp(pix_fmt_mp->height,
671 					MTK_VDEC_MIN_H,
672 					MTK_VDEC_MAX_H);
673 		pix_fmt_mp->width = clamp(pix_fmt_mp->width,
674 					MTK_VDEC_MIN_W,
675 					MTK_VDEC_MAX_W);
676 
677 		/*
678 		 * Find next closer width align 64, heign align 64, size align
679 		 * 64 rectangle
680 		 * Note: This only get default value, the real HW needed value
681 		 *       only available when ctx in MTK_STATE_HEADER state
682 		 */
683 		tmp_w = pix_fmt_mp->width;
684 		tmp_h = pix_fmt_mp->height;
685 		v4l_bound_align_image(&pix_fmt_mp->width,
686 					MTK_VDEC_MIN_W,
687 					MTK_VDEC_MAX_W, 6,
688 					&pix_fmt_mp->height,
689 					MTK_VDEC_MIN_H,
690 					MTK_VDEC_MAX_H, 6, 9);
691 
692 		if (pix_fmt_mp->width < tmp_w &&
693 			(pix_fmt_mp->width + 64) <= MTK_VDEC_MAX_W)
694 			pix_fmt_mp->width += 64;
695 		if (pix_fmt_mp->height < tmp_h &&
696 			(pix_fmt_mp->height + 64) <= MTK_VDEC_MAX_H)
697 			pix_fmt_mp->height += 64;
698 
699 		mtk_v4l2_debug(0,
700 			"before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
701 			tmp_w, tmp_h, pix_fmt_mp->width,
702 			pix_fmt_mp->height,
703 			pix_fmt_mp->width * pix_fmt_mp->height);
704 
705 		pix_fmt_mp->num_planes = fmt->num_planes;
706 		pix_fmt_mp->plane_fmt[0].sizeimage =
707 				pix_fmt_mp->width * pix_fmt_mp->height;
708 		pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
709 
710 		if (pix_fmt_mp->num_planes == 2) {
711 			pix_fmt_mp->plane_fmt[1].sizeimage =
712 				(pix_fmt_mp->width * pix_fmt_mp->height) / 2;
713 			pix_fmt_mp->plane_fmt[1].bytesperline =
714 				pix_fmt_mp->width;
715 		}
716 	}
717 
718 	for (i = 0; i < pix_fmt_mp->num_planes; i++)
719 		memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
720 			   sizeof(pix_fmt_mp->plane_fmt[0].reserved));
721 
722 	pix_fmt_mp->flags = 0;
723 	memset(&pix_fmt_mp->reserved, 0x0, sizeof(pix_fmt_mp->reserved));
724 	return 0;
725 }
726 
vidioc_try_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)727 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
728 				struct v4l2_format *f)
729 {
730 	const struct mtk_video_fmt *fmt;
731 
732 	fmt = mtk_vdec_find_format(f);
733 	if (!fmt) {
734 		f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
735 		fmt = mtk_vdec_find_format(f);
736 	}
737 
738 	return vidioc_try_fmt(f, fmt);
739 }
740 
vidioc_try_fmt_vid_out_mplane(struct file * file,void * priv,struct v4l2_format * f)741 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
742 				struct v4l2_format *f)
743 {
744 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
745 	const struct mtk_video_fmt *fmt;
746 
747 	fmt = mtk_vdec_find_format(f);
748 	if (!fmt) {
749 		f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
750 		fmt = mtk_vdec_find_format(f);
751 	}
752 
753 	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
754 		mtk_v4l2_err("sizeimage of output format must be given");
755 		return -EINVAL;
756 	}
757 
758 	return vidioc_try_fmt(f, fmt);
759 }
760 
vidioc_vdec_g_selection(struct file * file,void * priv,struct v4l2_selection * s)761 static int vidioc_vdec_g_selection(struct file *file, void *priv,
762 			struct v4l2_selection *s)
763 {
764 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
765 	struct mtk_q_data *q_data;
766 
767 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
768 		return -EINVAL;
769 
770 	q_data = &ctx->q_data[MTK_Q_DATA_DST];
771 
772 	switch (s->target) {
773 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
774 		s->r.left = 0;
775 		s->r.top = 0;
776 		s->r.width = ctx->picinfo.pic_w;
777 		s->r.height = ctx->picinfo.pic_h;
778 		break;
779 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
780 		s->r.left = 0;
781 		s->r.top = 0;
782 		s->r.width = ctx->picinfo.buf_w;
783 		s->r.height = ctx->picinfo.buf_h;
784 		break;
785 	case V4L2_SEL_TGT_COMPOSE:
786 		if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
787 			/* set to default value if header info not ready yet*/
788 			s->r.left = 0;
789 			s->r.top = 0;
790 			s->r.width = q_data->visible_width;
791 			s->r.height = q_data->visible_height;
792 		}
793 		break;
794 	default:
795 		return -EINVAL;
796 	}
797 
798 	if (ctx->state < MTK_STATE_HEADER) {
799 		/* set to default value if header info not ready yet*/
800 		s->r.left = 0;
801 		s->r.top = 0;
802 		s->r.width = q_data->visible_width;
803 		s->r.height = q_data->visible_height;
804 		return 0;
805 	}
806 
807 	return 0;
808 }
809 
vidioc_vdec_s_selection(struct file * file,void * priv,struct v4l2_selection * s)810 static int vidioc_vdec_s_selection(struct file *file, void *priv,
811 				struct v4l2_selection *s)
812 {
813 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
814 
815 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
816 		return -EINVAL;
817 
818 	switch (s->target) {
819 	case V4L2_SEL_TGT_COMPOSE:
820 		s->r.left = 0;
821 		s->r.top = 0;
822 		s->r.width = ctx->picinfo.pic_w;
823 		s->r.height = ctx->picinfo.pic_h;
824 		break;
825 	default:
826 		return -EINVAL;
827 	}
828 
829 	return 0;
830 }
831 
vidioc_vdec_s_fmt(struct file * file,void * priv,struct v4l2_format * f)832 static int vidioc_vdec_s_fmt(struct file *file, void *priv,
833 			     struct v4l2_format *f)
834 {
835 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
836 	struct v4l2_pix_format_mplane *pix_mp;
837 	struct mtk_q_data *q_data;
838 	int ret = 0;
839 	const struct mtk_video_fmt *fmt;
840 
841 	mtk_v4l2_debug(3, "[%d]", ctx->id);
842 
843 	q_data = mtk_vdec_get_q_data(ctx, f->type);
844 	if (!q_data)
845 		return -EINVAL;
846 
847 	pix_mp = &f->fmt.pix_mp;
848 	/*
849 	 * Setting OUTPUT format after OUTPUT buffers are allocated is invalid
850 	 * if using the stateful API.
851 	 */
852 	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
853 	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
854 		mtk_v4l2_err("out_q_ctx buffers already requested");
855 		ret = -EBUSY;
856 	}
857 
858 	/*
859 	 * Setting CAPTURE format after CAPTURE buffers are allocated is
860 	 * invalid.
861 	 */
862 	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
863 	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
864 		mtk_v4l2_err("cap_q_ctx buffers already requested");
865 		ret = -EBUSY;
866 	}
867 
868 	fmt = mtk_vdec_find_format(f);
869 	if (fmt == NULL) {
870 		if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
871 			f->fmt.pix.pixelformat =
872 				mtk_video_formats[OUT_FMT_IDX].fourcc;
873 			fmt = mtk_vdec_find_format(f);
874 		} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
875 			f->fmt.pix.pixelformat =
876 				mtk_video_formats[CAP_FMT_IDX].fourcc;
877 			fmt = mtk_vdec_find_format(f);
878 		}
879 	}
880 	if (fmt == NULL)
881 		return -EINVAL;
882 
883 	q_data->fmt = fmt;
884 	vidioc_try_fmt(f, q_data->fmt);
885 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
886 		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
887 		q_data->coded_width = pix_mp->width;
888 		q_data->coded_height = pix_mp->height;
889 
890 		ctx->colorspace = pix_mp->colorspace;
891 		ctx->ycbcr_enc = pix_mp->ycbcr_enc;
892 		ctx->quantization = pix_mp->quantization;
893 		ctx->xfer_func = pix_mp->xfer_func;
894 
895 		if (ctx->state == MTK_STATE_FREE) {
896 			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
897 			if (ret) {
898 				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
899 					ctx->id, ret);
900 				return -EINVAL;
901 			}
902 			ctx->state = MTK_STATE_INIT;
903 		}
904 	}
905 
906 	return 0;
907 }
908 
vidioc_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)909 static int vidioc_enum_framesizes(struct file *file, void *priv,
910 				struct v4l2_frmsizeenum *fsize)
911 {
912 	int i = 0;
913 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
914 
915 	if (fsize->index != 0)
916 		return -EINVAL;
917 
918 	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
919 		if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
920 			continue;
921 
922 		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
923 		fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
924 		if (!(ctx->dev->dec_capability &
925 				VCODEC_CAPABILITY_4K_DISABLED)) {
926 			mtk_v4l2_debug(3, "4K is enabled");
927 			fsize->stepwise.max_width =
928 					VCODEC_DEC_4K_CODED_WIDTH;
929 			fsize->stepwise.max_height =
930 					VCODEC_DEC_4K_CODED_HEIGHT;
931 		}
932 		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
933 				ctx->dev->dec_capability,
934 				fsize->stepwise.min_width,
935 				fsize->stepwise.max_width,
936 				fsize->stepwise.step_width,
937 				fsize->stepwise.min_height,
938 				fsize->stepwise.max_height,
939 				fsize->stepwise.step_height);
940 		return 0;
941 	}
942 
943 	return -EINVAL;
944 }
945 
vidioc_enum_fmt(struct v4l2_fmtdesc * f,bool output_queue)946 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
947 {
948 	const struct mtk_video_fmt *fmt;
949 	int i, j = 0;
950 
951 	for (i = 0; i < NUM_FORMATS; i++) {
952 		if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
953 			continue;
954 		if (!output_queue &&
955 			(mtk_video_formats[i].type != MTK_FMT_FRAME))
956 			continue;
957 
958 		if (j == f->index)
959 			break;
960 		++j;
961 	}
962 
963 	if (i == NUM_FORMATS)
964 		return -EINVAL;
965 
966 	fmt = &mtk_video_formats[i];
967 	f->pixelformat = fmt->fourcc;
968 	f->flags = fmt->flags;
969 
970 	return 0;
971 }
972 
vidioc_vdec_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)973 static int vidioc_vdec_enum_fmt_vid_cap(struct file *file, void *priv,
974 					struct v4l2_fmtdesc *f)
975 {
976 	return vidioc_enum_fmt(f, false);
977 }
978 
vidioc_vdec_enum_fmt_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)979 static int vidioc_vdec_enum_fmt_vid_out(struct file *file, void *priv,
980 					struct v4l2_fmtdesc *f)
981 {
982 	return vidioc_enum_fmt(f, true);
983 }
984 
vidioc_vdec_g_fmt(struct file * file,void * priv,struct v4l2_format * f)985 static int vidioc_vdec_g_fmt(struct file *file, void *priv,
986 			     struct v4l2_format *f)
987 {
988 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
989 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
990 	struct vb2_queue *vq;
991 	struct mtk_q_data *q_data;
992 
993 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
994 	if (!vq) {
995 		mtk_v4l2_err("no vb2 queue for type=%d", f->type);
996 		return -EINVAL;
997 	}
998 
999 	q_data = mtk_vdec_get_q_data(ctx, f->type);
1000 
1001 	pix_mp->field = V4L2_FIELD_NONE;
1002 	pix_mp->colorspace = ctx->colorspace;
1003 	pix_mp->ycbcr_enc = ctx->ycbcr_enc;
1004 	pix_mp->quantization = ctx->quantization;
1005 	pix_mp->xfer_func = ctx->xfer_func;
1006 
1007 	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
1008 	    (ctx->state >= MTK_STATE_HEADER)) {
1009 		/* Until STREAMOFF is called on the CAPTURE queue
1010 		 * (acknowledging the event), the driver operates as if
1011 		 * the resolution hasn't changed yet.
1012 		 * So we just return picinfo yet, and update picinfo in
1013 		 * stop_streaming hook function
1014 		 */
1015 		q_data->sizeimage[0] = ctx->picinfo.fb_sz[0];
1016 		q_data->sizeimage[1] = ctx->picinfo.fb_sz[1];
1017 		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
1018 		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
1019 		q_data->coded_width = ctx->picinfo.buf_w;
1020 		q_data->coded_height = ctx->picinfo.buf_h;
1021 		ctx->last_decoded_picinfo.cap_fourcc = q_data->fmt->fourcc;
1022 
1023 		/*
1024 		 * Width and height are set to the dimensions
1025 		 * of the movie, the buffer is bigger and
1026 		 * further processing stages should crop to this
1027 		 * rectangle.
1028 		 */
1029 		pix_mp->width = q_data->coded_width;
1030 		pix_mp->height = q_data->coded_height;
1031 
1032 		/*
1033 		 * Set pixelformat to the format in which mt vcodec
1034 		 * outputs the decoded frame
1035 		 */
1036 		pix_mp->num_planes = q_data->fmt->num_planes;
1037 		pix_mp->pixelformat = q_data->fmt->fourcc;
1038 		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1039 		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1040 		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1041 		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1042 
1043 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1044 		/*
1045 		 * This is run on OUTPUT
1046 		 * The buffer contains compressed image
1047 		 * so width and height have no meaning.
1048 		 * Assign value here to pass v4l2-compliance test
1049 		 */
1050 		pix_mp->width = q_data->visible_width;
1051 		pix_mp->height = q_data->visible_height;
1052 		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1053 		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1054 		pix_mp->pixelformat = q_data->fmt->fourcc;
1055 		pix_mp->num_planes = q_data->fmt->num_planes;
1056 	} else {
1057 		pix_mp->width = q_data->coded_width;
1058 		pix_mp->height = q_data->coded_height;
1059 		pix_mp->num_planes = q_data->fmt->num_planes;
1060 		pix_mp->pixelformat = q_data->fmt->fourcc;
1061 		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1062 		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1063 		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1064 		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1065 
1066 		mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
1067 				ctx->id, f->type, ctx->state);
1068 	}
1069 
1070 	return 0;
1071 }
1072 
vb2ops_vdec_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])1073 static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
1074 				unsigned int *nbuffers,
1075 				unsigned int *nplanes,
1076 				unsigned int sizes[],
1077 				struct device *alloc_devs[])
1078 {
1079 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
1080 	struct mtk_q_data *q_data;
1081 	unsigned int i;
1082 
1083 	q_data = mtk_vdec_get_q_data(ctx, vq->type);
1084 
1085 	if (q_data == NULL) {
1086 		mtk_v4l2_err("vq->type=%d err\n", vq->type);
1087 		return -EINVAL;
1088 	}
1089 
1090 	if (*nplanes) {
1091 		for (i = 0; i < *nplanes; i++) {
1092 			if (sizes[i] < q_data->sizeimage[i])
1093 				return -EINVAL;
1094 		}
1095 	} else {
1096 		if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1097 			*nplanes = 2;
1098 		else
1099 			*nplanes = 1;
1100 
1101 		for (i = 0; i < *nplanes; i++)
1102 			sizes[i] = q_data->sizeimage[i];
1103 	}
1104 
1105 	mtk_v4l2_debug(1,
1106 			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
1107 			ctx->id, vq->type, *nplanes, *nbuffers,
1108 			sizes[0], sizes[1]);
1109 
1110 	return 0;
1111 }
1112 
vb2ops_vdec_buf_prepare(struct vb2_buffer * vb)1113 static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
1114 {
1115 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1116 	struct mtk_q_data *q_data;
1117 	int i;
1118 
1119 	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
1120 			ctx->id, vb->vb2_queue->type, vb->index);
1121 
1122 	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
1123 
1124 	for (i = 0; i < q_data->fmt->num_planes; i++) {
1125 		if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
1126 			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
1127 				i, vb2_plane_size(vb, i),
1128 				q_data->sizeimage[i]);
1129 		}
1130 	}
1131 
1132 	return 0;
1133 }
1134 
vb2ops_vdec_buf_queue(struct vb2_buffer * vb)1135 static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
1136 {
1137 	struct vb2_v4l2_buffer *src_buf;
1138 	struct mtk_vcodec_mem src_mem;
1139 	bool res_chg = false;
1140 	int ret = 0;
1141 	unsigned int dpbsize = 1, i = 0;
1142 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1143 	struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
1144 	struct mtk_video_dec_buf *buf = NULL;
1145 	struct mtk_q_data *dst_q_data;
1146 
1147 	mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
1148 			ctx->id, vb->vb2_queue->type,
1149 			vb->index, vb);
1150 	/*
1151 	 * check if this buffer is ready to be used after decode
1152 	 */
1153 	if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1154 		vb2_v4l2 = to_vb2_v4l2_buffer(vb);
1155 		buf = container_of(vb2_v4l2, struct mtk_video_dec_buf,
1156 				   m2m_buf.vb);
1157 		mutex_lock(&ctx->lock);
1158 		if (!buf->used) {
1159 			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
1160 			buf->queued_in_vb2 = true;
1161 			buf->queued_in_v4l2 = true;
1162 		} else {
1163 			buf->queued_in_vb2 = false;
1164 			buf->queued_in_v4l2 = true;
1165 		}
1166 		mutex_unlock(&ctx->lock);
1167 		return;
1168 	}
1169 
1170 	v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
1171 
1172 	if (ctx->state != MTK_STATE_INIT) {
1173 		mtk_v4l2_debug(3, "[%d] already init driver %d",
1174 				ctx->id, ctx->state);
1175 		return;
1176 	}
1177 
1178 	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1179 	if (!src_buf) {
1180 		mtk_v4l2_err("No src buffer");
1181 		return;
1182 	}
1183 	buf = container_of(src_buf, struct mtk_video_dec_buf, m2m_buf.vb);
1184 	if (buf->lastframe) {
1185 		/* This shouldn't happen. Just in case. */
1186 		mtk_v4l2_err("Invalid flush buffer.");
1187 		v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1188 		return;
1189 	}
1190 
1191 	src_mem.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
1192 	src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
1193 	src_mem.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
1194 	mtk_v4l2_debug(2,
1195 			"[%d] buf id=%d va=%p dma=%pad size=%zx",
1196 			ctx->id, src_buf->vb2_buf.index,
1197 			src_mem.va, &src_mem.dma_addr,
1198 			src_mem.size);
1199 
1200 	ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
1201 	if (ret || !res_chg) {
1202 		/*
1203 		 * fb == NULL means to parse SPS/PPS header or
1204 		 * resolution info in src_mem. Decode can fail
1205 		 * if there is no SPS header or picture info
1206 		 * in bs
1207 		 */
1208 
1209 		src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1210 		if (ret == -EIO) {
1211 			mtk_v4l2_err("[%d] Unrecoverable error in vdec_if_decode.",
1212 					ctx->id);
1213 			ctx->state = MTK_STATE_ABORT;
1214 			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1215 		} else {
1216 			v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1217 		}
1218 		mtk_v4l2_debug(ret ? 0 : 1,
1219 			       "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
1220 			       ctx->id, src_buf->vb2_buf.index,
1221 			       src_mem.size, ret, res_chg);
1222 		return;
1223 	}
1224 
1225 	if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
1226 		mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
1227 				ctx->id);
1228 		return;
1229 	}
1230 
1231 	ctx->last_decoded_picinfo = ctx->picinfo;
1232 	dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
1233 	for (i = 0; i < dst_q_data->fmt->num_planes; i++) {
1234 		dst_q_data->sizeimage[i] = ctx->picinfo.fb_sz[i];
1235 		dst_q_data->bytesperline[i] = ctx->picinfo.buf_w;
1236 	}
1237 
1238 	mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
1239 			ctx->id,
1240 			ctx->picinfo.buf_w, ctx->picinfo.buf_h,
1241 			ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1242 			dst_q_data->sizeimage[0],
1243 			dst_q_data->sizeimage[1]);
1244 
1245 	ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
1246 	if (dpbsize == 0)
1247 		mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
1248 
1249 	ctx->dpb_size = dpbsize;
1250 	ctx->state = MTK_STATE_HEADER;
1251 	mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
1252 
1253 	mtk_vdec_queue_res_chg_event(ctx);
1254 }
1255 
vb2ops_vdec_buf_finish(struct vb2_buffer * vb)1256 static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
1257 {
1258 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1259 	struct vb2_v4l2_buffer *vb2_v4l2;
1260 	struct mtk_video_dec_buf *buf;
1261 	bool buf_error;
1262 
1263 	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
1264 	buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, m2m_buf.vb);
1265 	mutex_lock(&ctx->lock);
1266 	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1267 		buf->queued_in_v4l2 = false;
1268 		buf->queued_in_vb2 = false;
1269 	}
1270 	buf_error = buf->error;
1271 	mutex_unlock(&ctx->lock);
1272 
1273 	if (buf_error) {
1274 		mtk_v4l2_err("Unrecoverable error on buffer.");
1275 		ctx->state = MTK_STATE_ABORT;
1276 	}
1277 }
1278 
vb2ops_vdec_buf_init(struct vb2_buffer * vb)1279 static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
1280 {
1281 	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
1282 					struct vb2_v4l2_buffer, vb2_buf);
1283 	struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
1284 					struct mtk_video_dec_buf, m2m_buf.vb);
1285 
1286 	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1287 		buf->used = false;
1288 		buf->queued_in_v4l2 = false;
1289 	} else {
1290 		buf->lastframe = false;
1291 	}
1292 
1293 	return 0;
1294 }
1295 
vb2ops_vdec_start_streaming(struct vb2_queue * q,unsigned int count)1296 static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1297 {
1298 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1299 
1300 	if (ctx->state == MTK_STATE_FLUSH)
1301 		ctx->state = MTK_STATE_HEADER;
1302 
1303 	return 0;
1304 }
1305 
vb2ops_vdec_stop_streaming(struct vb2_queue * q)1306 static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
1307 {
1308 	struct vb2_v4l2_buffer *src_buf = NULL, *dst_buf = NULL;
1309 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1310 
1311 	mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
1312 			ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt);
1313 
1314 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1315 		while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
1316 			struct mtk_video_dec_buf *buf_info = container_of(
1317 				 src_buf, struct mtk_video_dec_buf, m2m_buf.vb);
1318 			if (!buf_info->lastframe)
1319 				v4l2_m2m_buf_done(src_buf,
1320 						VB2_BUF_STATE_ERROR);
1321 		}
1322 		return;
1323 	}
1324 
1325 	if (ctx->state >= MTK_STATE_HEADER) {
1326 
1327 		/* Until STREAMOFF is called on the CAPTURE queue
1328 		 * (acknowledging the event), the driver operates
1329 		 * as if the resolution hasn't changed yet, i.e.
1330 		 * VIDIOC_G_FMT< etc. return previous resolution.
1331 		 * So we update picinfo here
1332 		 */
1333 		ctx->picinfo = ctx->last_decoded_picinfo;
1334 
1335 		mtk_v4l2_debug(2,
1336 				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
1337 				ctx->id, ctx->last_decoded_picinfo.pic_w,
1338 				ctx->last_decoded_picinfo.pic_h,
1339 				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1340 				ctx->last_decoded_picinfo.buf_w,
1341 				ctx->last_decoded_picinfo.buf_h);
1342 
1343 		mtk_vdec_flush_decoder(ctx);
1344 	}
1345 	ctx->state = MTK_STATE_FLUSH;
1346 
1347 	while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
1348 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1349 		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
1350 			vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
1351 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1352 	}
1353 
1354 }
1355 
m2mops_vdec_device_run(void * priv)1356 static void m2mops_vdec_device_run(void *priv)
1357 {
1358 	struct mtk_vcodec_ctx *ctx = priv;
1359 	struct mtk_vcodec_dev *dev = ctx->dev;
1360 
1361 	queue_work(dev->decode_workqueue, &ctx->decode_work);
1362 }
1363 
m2mops_vdec_job_ready(void * m2m_priv)1364 static int m2mops_vdec_job_ready(void *m2m_priv)
1365 {
1366 	struct mtk_vcodec_ctx *ctx = m2m_priv;
1367 
1368 	mtk_v4l2_debug(3, "[%d]", ctx->id);
1369 
1370 	if (ctx->state == MTK_STATE_ABORT)
1371 		return 0;
1372 
1373 	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
1374 	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
1375 		return 0;
1376 
1377 	if (ctx->state != MTK_STATE_HEADER)
1378 		return 0;
1379 
1380 	return 1;
1381 }
1382 
m2mops_vdec_job_abort(void * priv)1383 static void m2mops_vdec_job_abort(void *priv)
1384 {
1385 	struct mtk_vcodec_ctx *ctx = priv;
1386 
1387 	ctx->state = MTK_STATE_ABORT;
1388 }
1389 
mtk_vdec_g_v_ctrl(struct v4l2_ctrl * ctrl)1390 static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
1391 {
1392 	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
1393 	int ret = 0;
1394 
1395 	switch (ctrl->id) {
1396 	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
1397 		if (ctx->state >= MTK_STATE_HEADER) {
1398 			ctrl->val = ctx->dpb_size;
1399 		} else {
1400 			mtk_v4l2_debug(0, "Seqinfo not ready");
1401 			ctrl->val = 0;
1402 		}
1403 		break;
1404 	default:
1405 		ret = -EINVAL;
1406 	}
1407 	return ret;
1408 }
1409 
1410 static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
1411 	.g_volatile_ctrl = mtk_vdec_g_v_ctrl,
1412 };
1413 
mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx * ctx)1414 int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
1415 {
1416 	struct v4l2_ctrl *ctrl;
1417 
1418 	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
1419 
1420 	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
1421 				&mtk_vcodec_dec_ctrl_ops,
1422 				V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
1423 				0, 32, 1, 1);
1424 	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1425 	v4l2_ctrl_new_std_menu(&ctx->ctrl_hdl,
1426 				&mtk_vcodec_dec_ctrl_ops,
1427 				V4L2_CID_MPEG_VIDEO_VP9_PROFILE,
1428 				V4L2_MPEG_VIDEO_VP9_PROFILE_0,
1429 				0, V4L2_MPEG_VIDEO_VP9_PROFILE_0);
1430 
1431 	if (ctx->ctrl_hdl.error) {
1432 		mtk_v4l2_err("Adding control failed %d",
1433 				ctx->ctrl_hdl.error);
1434 		return ctx->ctrl_hdl.error;
1435 	}
1436 
1437 	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1438 	return 0;
1439 }
1440 
1441 const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
1442 	.device_run	= m2mops_vdec_device_run,
1443 	.job_ready	= m2mops_vdec_job_ready,
1444 	.job_abort	= m2mops_vdec_job_abort,
1445 };
1446 
1447 static const struct vb2_ops mtk_vdec_vb2_ops = {
1448 	.queue_setup	= vb2ops_vdec_queue_setup,
1449 	.buf_prepare	= vb2ops_vdec_buf_prepare,
1450 	.buf_queue	= vb2ops_vdec_buf_queue,
1451 	.wait_prepare	= vb2_ops_wait_prepare,
1452 	.wait_finish	= vb2_ops_wait_finish,
1453 	.buf_init	= vb2ops_vdec_buf_init,
1454 	.buf_finish	= vb2ops_vdec_buf_finish,
1455 	.start_streaming	= vb2ops_vdec_start_streaming,
1456 	.stop_streaming	= vb2ops_vdec_stop_streaming,
1457 };
1458 
1459 const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
1460 	.vidioc_streamon	= v4l2_m2m_ioctl_streamon,
1461 	.vidioc_streamoff	= v4l2_m2m_ioctl_streamoff,
1462 	.vidioc_reqbufs		= v4l2_m2m_ioctl_reqbufs,
1463 	.vidioc_querybuf	= v4l2_m2m_ioctl_querybuf,
1464 	.vidioc_expbuf		= v4l2_m2m_ioctl_expbuf,
1465 
1466 	.vidioc_qbuf		= vidioc_vdec_qbuf,
1467 	.vidioc_dqbuf		= vidioc_vdec_dqbuf,
1468 
1469 	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
1470 	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
1471 
1472 	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
1473 	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
1474 	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
1475 	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
1476 
1477 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
1478 
1479 	.vidioc_enum_fmt_vid_cap	= vidioc_vdec_enum_fmt_vid_cap,
1480 	.vidioc_enum_fmt_vid_out	= vidioc_vdec_enum_fmt_vid_out,
1481 	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
1482 
1483 	.vidioc_querycap		= vidioc_vdec_querycap,
1484 	.vidioc_subscribe_event		= vidioc_vdec_subscribe_evt,
1485 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
1486 	.vidioc_g_selection             = vidioc_vdec_g_selection,
1487 	.vidioc_s_selection             = vidioc_vdec_s_selection,
1488 
1489 	.vidioc_decoder_cmd = vidioc_decoder_cmd,
1490 	.vidioc_try_decoder_cmd = vidioc_try_decoder_cmd,
1491 };
1492 
mtk_vcodec_dec_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1493 int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
1494 			   struct vb2_queue *dst_vq)
1495 {
1496 	struct mtk_vcodec_ctx *ctx = priv;
1497 	int ret = 0;
1498 
1499 	mtk_v4l2_debug(3, "[%d]", ctx->id);
1500 
1501 	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1502 	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
1503 	src_vq->drv_priv	= ctx;
1504 	src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1505 	src_vq->ops		= &mtk_vdec_vb2_ops;
1506 	src_vq->mem_ops		= &vb2_dma_contig_memops;
1507 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1508 	src_vq->lock		= &ctx->dev->dev_mutex;
1509 	src_vq->dev             = &ctx->dev->plat_dev->dev;
1510 
1511 	ret = vb2_queue_init(src_vq);
1512 	if (ret) {
1513 		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
1514 		return ret;
1515 	}
1516 	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1517 	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
1518 	dst_vq->drv_priv	= ctx;
1519 	dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1520 	dst_vq->ops		= &mtk_vdec_vb2_ops;
1521 	dst_vq->mem_ops		= &vb2_dma_contig_memops;
1522 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1523 	dst_vq->lock		= &ctx->dev->dev_mutex;
1524 	dst_vq->dev             = &ctx->dev->plat_dev->dev;
1525 
1526 	ret = vb2_queue_init(dst_vq);
1527 	if (ret)
1528 		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
1529 
1530 	return ret;
1531 }
1532