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 #include <linux/pm_runtime.h>
12 
13 #include "mtk_vcodec_drv.h"
14 #include "mtk_vcodec_enc.h"
15 #include "mtk_vcodec_intr.h"
16 #include "mtk_vcodec_util.h"
17 #include "venc_drv_if.h"
18 
19 #define MTK_VENC_MIN_W	160U
20 #define MTK_VENC_MIN_H	128U
21 #define MTK_VENC_HD_MAX_W	1920U
22 #define MTK_VENC_HD_MAX_H	1088U
23 #define MTK_VENC_4K_MAX_W	3840U
24 #define MTK_VENC_4K_MAX_H	2176U
25 
26 #define DFT_CFG_WIDTH	MTK_VENC_MIN_W
27 #define DFT_CFG_HEIGHT	MTK_VENC_MIN_H
28 #define MTK_MAX_CTRLS_HINT	20
29 
30 #define MTK_DEFAULT_FRAMERATE_NUM 1001
31 #define MTK_DEFAULT_FRAMERATE_DENOM 30000
32 #define MTK_VENC_4K_CAPABILITY_ENABLE BIT(0)
33 
34 static void mtk_venc_worker(struct work_struct *work);
35 
36 static const struct v4l2_frmsize_stepwise mtk_venc_hd_framesizes = {
37 	MTK_VENC_MIN_W, MTK_VENC_HD_MAX_W, 16,
38 	MTK_VENC_MIN_H, MTK_VENC_HD_MAX_H, 16,
39 };
40 
41 static const struct v4l2_frmsize_stepwise mtk_venc_4k_framesizes = {
42 	MTK_VENC_MIN_W, MTK_VENC_4K_MAX_W, 16,
43 	MTK_VENC_MIN_H, MTK_VENC_4K_MAX_H, 16,
44 };
45 
vidioc_venc_s_ctrl(struct v4l2_ctrl * ctrl)46 static int vidioc_venc_s_ctrl(struct v4l2_ctrl *ctrl)
47 {
48 	struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
49 	struct mtk_enc_params *p = &ctx->enc_params;
50 	int ret = 0;
51 
52 	switch (ctrl->id) {
53 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
54 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_BITRATE_MODE val= %d",
55 			       ctrl->val);
56 		if (ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) {
57 			mtk_v4l2_err("Unsupported bitrate mode =%d", ctrl->val);
58 			ret = -EINVAL;
59 		}
60 		break;
61 	case V4L2_CID_MPEG_VIDEO_BITRATE:
62 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_BITRATE val = %d",
63 			       ctrl->val);
64 		p->bitrate = ctrl->val;
65 		ctx->param_change |= MTK_ENCODE_PARAM_BITRATE;
66 		break;
67 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
68 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_B_FRAMES val = %d",
69 			       ctrl->val);
70 		p->num_b_frame = ctrl->val;
71 		break;
72 	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
73 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE val = %d",
74 			       ctrl->val);
75 		p->rc_frame = ctrl->val;
76 		break;
77 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
78 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_MAX_QP val = %d",
79 			       ctrl->val);
80 		p->h264_max_qp = ctrl->val;
81 		break;
82 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
83 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_HEADER_MODE val = %d",
84 			       ctrl->val);
85 		p->seq_hdr_mode = ctrl->val;
86 		break;
87 	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
88 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE val = %d",
89 			       ctrl->val);
90 		p->rc_mb = ctrl->val;
91 		break;
92 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
93 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_PROFILE val = %d",
94 			       ctrl->val);
95 		p->h264_profile = ctrl->val;
96 		break;
97 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
98 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_LEVEL val = %d",
99 			       ctrl->val);
100 		p->h264_level = ctrl->val;
101 		break;
102 	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
103 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_I_PERIOD val = %d",
104 			       ctrl->val);
105 		p->intra_period = ctrl->val;
106 		ctx->param_change |= MTK_ENCODE_PARAM_INTRA_PERIOD;
107 		break;
108 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
109 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_GOP_SIZE val = %d",
110 			       ctrl->val);
111 		p->gop_size = ctrl->val;
112 		ctx->param_change |= MTK_ENCODE_PARAM_GOP_SIZE;
113 		break;
114 	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
115 		/*
116 		 * FIXME - what vp8 profiles are actually supported?
117 		 * The ctrl is added (with only profile 0 supported) for now.
118 		 */
119 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_VP8_PROFILE val = %d", ctrl->val);
120 		break;
121 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
122 		mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME");
123 		p->force_intra = 1;
124 		ctx->param_change |= MTK_ENCODE_PARAM_FORCE_INTRA;
125 		break;
126 	default:
127 		ret = -EINVAL;
128 		break;
129 	}
130 
131 	return ret;
132 }
133 
134 static const struct v4l2_ctrl_ops mtk_vcodec_enc_ctrl_ops = {
135 	.s_ctrl = vidioc_venc_s_ctrl,
136 };
137 
vidioc_enum_fmt(struct v4l2_fmtdesc * f,const struct mtk_video_fmt * formats,size_t num_formats)138 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f,
139 			   const struct mtk_video_fmt *formats,
140 			   size_t num_formats)
141 {
142 	if (f->index >= num_formats)
143 		return -EINVAL;
144 
145 	f->pixelformat = formats[f->index].fourcc;
146 
147 	return 0;
148 }
149 
150 static const struct mtk_video_fmt *
mtk_venc_find_format(u32 fourcc,const struct mtk_vcodec_enc_pdata * pdata)151 mtk_venc_find_format(u32 fourcc, const struct mtk_vcodec_enc_pdata *pdata)
152 {
153 	const struct mtk_video_fmt *fmt;
154 	unsigned int k;
155 
156 	for (k = 0; k < pdata->num_capture_formats; k++) {
157 		fmt = &pdata->capture_formats[k];
158 		if (fmt->fourcc == fourcc)
159 			return fmt;
160 	}
161 
162 	for (k = 0; k < pdata->num_output_formats; k++) {
163 		fmt = &pdata->output_formats[k];
164 		if (fmt->fourcc == fourcc)
165 			return fmt;
166 	}
167 
168 	return NULL;
169 }
170 
vidioc_enum_framesizes(struct file * file,void * fh,struct v4l2_frmsizeenum * fsize)171 static int vidioc_enum_framesizes(struct file *file, void *fh,
172 				  struct v4l2_frmsizeenum *fsize)
173 {
174 	const struct mtk_video_fmt *fmt;
175 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(fh);
176 
177 	if (fsize->index != 0)
178 		return -EINVAL;
179 
180 	fmt = mtk_venc_find_format(fsize->pixel_format,
181 				   ctx->dev->venc_pdata);
182 	if (!fmt)
183 		return -EINVAL;
184 
185 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
186 
187 	if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE)
188 		fsize->stepwise = mtk_venc_4k_framesizes;
189 	else
190 		fsize->stepwise = mtk_venc_hd_framesizes;
191 
192 	return 0;
193 }
194 
vidioc_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)195 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
196 				   struct v4l2_fmtdesc *f)
197 {
198 	const struct mtk_vcodec_enc_pdata *pdata =
199 		fh_to_ctx(priv)->dev->venc_pdata;
200 
201 	return vidioc_enum_fmt(f, pdata->capture_formats,
202 			       pdata->num_capture_formats);
203 }
204 
vidioc_enum_fmt_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)205 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
206 				   struct v4l2_fmtdesc *f)
207 {
208 	const struct mtk_vcodec_enc_pdata *pdata =
209 		fh_to_ctx(priv)->dev->venc_pdata;
210 
211 	return vidioc_enum_fmt(f, pdata->output_formats,
212 			       pdata->num_output_formats);
213 }
214 
mtk_vcodec_enc_get_chip_name(void * priv)215 static int mtk_vcodec_enc_get_chip_name(void *priv)
216 {
217 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
218 	struct device *dev = &ctx->dev->plat_dev->dev;
219 
220 	if (of_device_is_compatible(dev->of_node, "mediatek,mt8173-vcodec-enc"))
221 		return 8173;
222 	else if (of_device_is_compatible(dev->of_node, "mediatek,mt8183-vcodec-enc"))
223 		return 8183;
224 	else if (of_device_is_compatible(dev->of_node, "mediatek,mt8192-vcodec-enc"))
225 		return 8192;
226 	else if (of_device_is_compatible(dev->of_node, "mediatek,mt8195-vcodec-enc"))
227 		return 8195;
228 	else if (of_device_is_compatible(dev->of_node, "mediatek,mt8188-vcodec-enc"))
229 		return 8188;
230 	else
231 		return 8173;
232 }
233 
vidioc_venc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)234 static int vidioc_venc_querycap(struct file *file, void *priv,
235 				struct v4l2_capability *cap)
236 {
237 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
238 	struct device *dev = &ctx->dev->plat_dev->dev;
239 	int platform_name = mtk_vcodec_enc_get_chip_name(priv);
240 
241 	strscpy(cap->driver, dev->driver->name, sizeof(cap->driver));
242 	snprintf(cap->card, sizeof(cap->card), "MT%d video encoder", platform_name);
243 
244 	return 0;
245 }
246 
vidioc_venc_s_parm(struct file * file,void * priv,struct v4l2_streamparm * a)247 static int vidioc_venc_s_parm(struct file *file, void *priv,
248 			      struct v4l2_streamparm *a)
249 {
250 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
251 	struct v4l2_fract *timeperframe = &a->parm.output.timeperframe;
252 
253 	if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
254 		return -EINVAL;
255 
256 	if (timeperframe->numerator == 0 || timeperframe->denominator == 0) {
257 		timeperframe->numerator = MTK_DEFAULT_FRAMERATE_NUM;
258 		timeperframe->denominator = MTK_DEFAULT_FRAMERATE_DENOM;
259 	}
260 
261 	ctx->enc_params.framerate_num = timeperframe->denominator;
262 	ctx->enc_params.framerate_denom = timeperframe->numerator;
263 	ctx->param_change |= MTK_ENCODE_PARAM_FRAMERATE;
264 
265 	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
266 
267 	return 0;
268 }
269 
vidioc_venc_g_parm(struct file * file,void * priv,struct v4l2_streamparm * a)270 static int vidioc_venc_g_parm(struct file *file, void *priv,
271 			      struct v4l2_streamparm *a)
272 {
273 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
274 
275 	if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
276 		return -EINVAL;
277 
278 	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
279 	a->parm.output.timeperframe.denominator =
280 			ctx->enc_params.framerate_num;
281 	a->parm.output.timeperframe.numerator =
282 			ctx->enc_params.framerate_denom;
283 
284 	return 0;
285 }
286 
mtk_venc_get_q_data(struct mtk_vcodec_ctx * ctx,enum v4l2_buf_type type)287 static struct mtk_q_data *mtk_venc_get_q_data(struct mtk_vcodec_ctx *ctx,
288 					      enum v4l2_buf_type type)
289 {
290 	if (V4L2_TYPE_IS_OUTPUT(type))
291 		return &ctx->q_data[MTK_Q_DATA_SRC];
292 
293 	return &ctx->q_data[MTK_Q_DATA_DST];
294 }
295 
vidioc_try_fmt_cap(struct v4l2_format * f)296 static void vidioc_try_fmt_cap(struct v4l2_format *f)
297 {
298 	f->fmt.pix_mp.field = V4L2_FIELD_NONE;
299 	f->fmt.pix_mp.num_planes = 1;
300 	f->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
301 	f->fmt.pix_mp.flags = 0;
302 }
303 
304 /* V4L2 specification suggests the driver corrects the format struct if any of
305  * the dimensions is unsupported
306  */
vidioc_try_fmt_out(struct mtk_vcodec_ctx * ctx,struct v4l2_format * f,const struct mtk_video_fmt * fmt)307 static int vidioc_try_fmt_out(struct mtk_vcodec_ctx *ctx, struct v4l2_format *f,
308 			      const struct mtk_video_fmt *fmt)
309 {
310 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
311 	int tmp_w, tmp_h;
312 	unsigned int max_width, max_height;
313 
314 	pix_fmt_mp->field = V4L2_FIELD_NONE;
315 
316 	if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE) {
317 		max_width = MTK_VENC_4K_MAX_W;
318 		max_height = MTK_VENC_4K_MAX_H;
319 	} else {
320 		max_width = MTK_VENC_HD_MAX_W;
321 		max_height = MTK_VENC_HD_MAX_H;
322 	}
323 
324 	pix_fmt_mp->height = clamp(pix_fmt_mp->height, MTK_VENC_MIN_H, max_height);
325 	pix_fmt_mp->width = clamp(pix_fmt_mp->width, MTK_VENC_MIN_W, max_width);
326 
327 	/* find next closer width align 16, heign align 32, size align
328 	 * 64 rectangle
329 	 */
330 	tmp_w = pix_fmt_mp->width;
331 	tmp_h = pix_fmt_mp->height;
332 	v4l_bound_align_image(&pix_fmt_mp->width,
333 			      MTK_VENC_MIN_W,
334 			      max_width, 4,
335 			      &pix_fmt_mp->height,
336 			      MTK_VENC_MIN_H,
337 			      max_height, 5, 6);
338 
339 	if (pix_fmt_mp->width < tmp_w && (pix_fmt_mp->width + 16) <= max_width)
340 		pix_fmt_mp->width += 16;
341 	if (pix_fmt_mp->height < tmp_h && (pix_fmt_mp->height + 32) <= max_height)
342 		pix_fmt_mp->height += 32;
343 
344 	mtk_v4l2_debug(0, "before resize w=%d, h=%d, after resize w=%d, h=%d, sizeimage=%d %d",
345 		       tmp_w, tmp_h, pix_fmt_mp->width,
346 		       pix_fmt_mp->height,
347 		       pix_fmt_mp->plane_fmt[0].sizeimage,
348 		       pix_fmt_mp->plane_fmt[1].sizeimage);
349 
350 	pix_fmt_mp->num_planes = fmt->num_planes;
351 	pix_fmt_mp->plane_fmt[0].sizeimage =
352 			pix_fmt_mp->width * pix_fmt_mp->height +
353 			((ALIGN(pix_fmt_mp->width, 16) * 2) * 16);
354 	pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
355 
356 	if (pix_fmt_mp->num_planes == 2) {
357 		pix_fmt_mp->plane_fmt[1].sizeimage =
358 			(pix_fmt_mp->width * pix_fmt_mp->height) / 2 +
359 			(ALIGN(pix_fmt_mp->width, 16) * 16);
360 		pix_fmt_mp->plane_fmt[2].sizeimage = 0;
361 		pix_fmt_mp->plane_fmt[1].bytesperline =
362 						pix_fmt_mp->width;
363 		pix_fmt_mp->plane_fmt[2].bytesperline = 0;
364 	} else if (pix_fmt_mp->num_planes == 3) {
365 		pix_fmt_mp->plane_fmt[1].sizeimage =
366 		pix_fmt_mp->plane_fmt[2].sizeimage =
367 			(pix_fmt_mp->width * pix_fmt_mp->height) / 4 +
368 			((ALIGN(pix_fmt_mp->width, 16) / 2) * 16);
369 		pix_fmt_mp->plane_fmt[1].bytesperline =
370 			pix_fmt_mp->plane_fmt[2].bytesperline =
371 			pix_fmt_mp->width / 2;
372 	}
373 
374 	pix_fmt_mp->flags = 0;
375 
376 	return 0;
377 }
378 
mtk_venc_set_param(struct mtk_vcodec_ctx * ctx,struct venc_enc_param * param)379 static void mtk_venc_set_param(struct mtk_vcodec_ctx *ctx,
380 				struct venc_enc_param *param)
381 {
382 	struct mtk_q_data *q_data_src = &ctx->q_data[MTK_Q_DATA_SRC];
383 	struct mtk_enc_params *enc_params = &ctx->enc_params;
384 
385 	switch (q_data_src->fmt->fourcc) {
386 	case V4L2_PIX_FMT_YUV420M:
387 		param->input_yuv_fmt = VENC_YUV_FORMAT_I420;
388 		break;
389 	case V4L2_PIX_FMT_YVU420M:
390 		param->input_yuv_fmt = VENC_YUV_FORMAT_YV12;
391 		break;
392 	case V4L2_PIX_FMT_NV12M:
393 		param->input_yuv_fmt = VENC_YUV_FORMAT_NV12;
394 		break;
395 	case V4L2_PIX_FMT_NV21M:
396 		param->input_yuv_fmt = VENC_YUV_FORMAT_NV21;
397 		break;
398 	default:
399 		mtk_v4l2_err("Unsupported fourcc =%d", q_data_src->fmt->fourcc);
400 		break;
401 	}
402 	param->h264_profile = enc_params->h264_profile;
403 	param->h264_level = enc_params->h264_level;
404 
405 	/* Config visible resolution */
406 	param->width = q_data_src->visible_width;
407 	param->height = q_data_src->visible_height;
408 	/* Config coded resolution */
409 	param->buf_width = q_data_src->coded_width;
410 	param->buf_height = q_data_src->coded_height;
411 	param->frm_rate = enc_params->framerate_num /
412 			enc_params->framerate_denom;
413 	param->intra_period = enc_params->intra_period;
414 	param->gop_size = enc_params->gop_size;
415 	param->bitrate = enc_params->bitrate;
416 
417 	mtk_v4l2_debug(0,
418 		"fmt 0x%x, P/L %d/%d, w/h %d/%d, buf %d/%d, fps/bps %d/%d, gop %d, i_period %d",
419 		param->input_yuv_fmt, param->h264_profile,
420 		param->h264_level, param->width, param->height,
421 		param->buf_width, param->buf_height,
422 		param->frm_rate, param->bitrate,
423 		param->gop_size, param->intra_period);
424 }
425 
vidioc_venc_s_fmt_cap(struct file * file,void * priv,struct v4l2_format * f)426 static int vidioc_venc_s_fmt_cap(struct file *file, void *priv,
427 			     struct v4l2_format *f)
428 {
429 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
430 	const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
431 	struct vb2_queue *vq;
432 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, f->type);
433 	int i, ret;
434 	const struct mtk_video_fmt *fmt;
435 
436 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
437 	if (!vq) {
438 		mtk_v4l2_err("fail to get vq");
439 		return -EINVAL;
440 	}
441 
442 	if (vb2_is_busy(vq)) {
443 		mtk_v4l2_err("queue busy");
444 		return -EBUSY;
445 	}
446 
447 	fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
448 	if (!fmt) {
449 		fmt = &ctx->dev->venc_pdata->capture_formats[0];
450 		f->fmt.pix.pixelformat = fmt->fourcc;
451 	}
452 
453 	q_data->fmt = fmt;
454 	vidioc_try_fmt_cap(f);
455 
456 	q_data->coded_width = f->fmt.pix_mp.width;
457 	q_data->coded_height = f->fmt.pix_mp.height;
458 	q_data->field = f->fmt.pix_mp.field;
459 
460 	for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
461 		struct v4l2_plane_pix_format	*plane_fmt;
462 
463 		plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
464 		q_data->bytesperline[i]	= plane_fmt->bytesperline;
465 		q_data->sizeimage[i] = plane_fmt->sizeimage;
466 	}
467 
468 	if (ctx->state == MTK_STATE_FREE) {
469 		ret = venc_if_init(ctx, q_data->fmt->fourcc);
470 		if (ret) {
471 			mtk_v4l2_err("venc_if_init failed=%d, codec type=%x",
472 					ret, q_data->fmt->fourcc);
473 			return -EBUSY;
474 		}
475 		ctx->state = MTK_STATE_INIT;
476 	}
477 
478 	return 0;
479 }
480 
vidioc_venc_s_fmt_out(struct file * file,void * priv,struct v4l2_format * f)481 static int vidioc_venc_s_fmt_out(struct file *file, void *priv,
482 			     struct v4l2_format *f)
483 {
484 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
485 	const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
486 	struct vb2_queue *vq;
487 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, f->type);
488 	int ret, i;
489 	const struct mtk_video_fmt *fmt;
490 
491 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
492 	if (!vq) {
493 		mtk_v4l2_err("fail to get vq");
494 		return -EINVAL;
495 	}
496 
497 	if (vb2_is_busy(vq)) {
498 		mtk_v4l2_err("queue busy");
499 		return -EBUSY;
500 	}
501 
502 	fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
503 	if (!fmt) {
504 		fmt = &ctx->dev->venc_pdata->output_formats[0];
505 		f->fmt.pix.pixelformat = fmt->fourcc;
506 	}
507 
508 	q_data->visible_width = f->fmt.pix_mp.width;
509 	q_data->visible_height = f->fmt.pix_mp.height;
510 	q_data->fmt = fmt;
511 	ret = vidioc_try_fmt_out(ctx, f, q_data->fmt);
512 	if (ret)
513 		return ret;
514 
515 	q_data->coded_width = f->fmt.pix_mp.width;
516 	q_data->coded_height = f->fmt.pix_mp.height;
517 
518 	q_data->field = f->fmt.pix_mp.field;
519 	ctx->colorspace = f->fmt.pix_mp.colorspace;
520 	ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
521 	ctx->quantization = f->fmt.pix_mp.quantization;
522 	ctx->xfer_func = f->fmt.pix_mp.xfer_func;
523 
524 	for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
525 		struct v4l2_plane_pix_format *plane_fmt;
526 
527 		plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
528 		q_data->bytesperline[i] = plane_fmt->bytesperline;
529 		q_data->sizeimage[i] = plane_fmt->sizeimage;
530 	}
531 
532 	return 0;
533 }
534 
vidioc_venc_g_fmt(struct file * file,void * priv,struct v4l2_format * f)535 static int vidioc_venc_g_fmt(struct file *file, void *priv,
536 			     struct v4l2_format *f)
537 {
538 	struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
539 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
540 	struct vb2_queue *vq;
541 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, f->type);
542 	int i;
543 
544 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
545 	if (!vq)
546 		return -EINVAL;
547 
548 
549 	pix->width = q_data->coded_width;
550 	pix->height = q_data->coded_height;
551 	pix->pixelformat = q_data->fmt->fourcc;
552 	pix->field = q_data->field;
553 	pix->num_planes = q_data->fmt->num_planes;
554 	for (i = 0; i < pix->num_planes; i++) {
555 		pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
556 		pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
557 	}
558 
559 	pix->flags = 0;
560 	pix->colorspace = ctx->colorspace;
561 	pix->ycbcr_enc = ctx->ycbcr_enc;
562 	pix->quantization = ctx->quantization;
563 	pix->xfer_func = ctx->xfer_func;
564 
565 	return 0;
566 }
567 
vidioc_try_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)568 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
569 					 struct v4l2_format *f)
570 {
571 	const struct mtk_video_fmt *fmt;
572 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
573 	const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
574 
575 	fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
576 	if (!fmt) {
577 		fmt = &ctx->dev->venc_pdata->capture_formats[0];
578 		f->fmt.pix.pixelformat = fmt->fourcc;
579 	}
580 	f->fmt.pix_mp.colorspace = ctx->colorspace;
581 	f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc;
582 	f->fmt.pix_mp.quantization = ctx->quantization;
583 	f->fmt.pix_mp.xfer_func = ctx->xfer_func;
584 
585 	vidioc_try_fmt_cap(f);
586 
587 	return 0;
588 }
589 
vidioc_try_fmt_vid_out_mplane(struct file * file,void * priv,struct v4l2_format * f)590 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
591 					 struct v4l2_format *f)
592 {
593 	const struct mtk_video_fmt *fmt;
594 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
595 	const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
596 
597 	fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
598 	if (!fmt) {
599 		fmt = &ctx->dev->venc_pdata->output_formats[0];
600 		f->fmt.pix.pixelformat = fmt->fourcc;
601 	}
602 	if (!f->fmt.pix_mp.colorspace) {
603 		f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
604 		f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
605 		f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
606 		f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
607 	}
608 
609 	return vidioc_try_fmt_out(ctx, f, fmt);
610 }
611 
vidioc_venc_g_selection(struct file * file,void * priv,struct v4l2_selection * s)612 static int vidioc_venc_g_selection(struct file *file, void *priv,
613 				     struct v4l2_selection *s)
614 {
615 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
616 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, s->type);
617 
618 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
619 		return -EINVAL;
620 
621 	switch (s->target) {
622 	case V4L2_SEL_TGT_CROP_DEFAULT:
623 	case V4L2_SEL_TGT_CROP_BOUNDS:
624 		s->r.top = 0;
625 		s->r.left = 0;
626 		s->r.width = q_data->coded_width;
627 		s->r.height = q_data->coded_height;
628 		break;
629 	case V4L2_SEL_TGT_CROP:
630 		s->r.top = 0;
631 		s->r.left = 0;
632 		s->r.width = q_data->visible_width;
633 		s->r.height = q_data->visible_height;
634 		break;
635 	default:
636 		return -EINVAL;
637 	}
638 
639 	return 0;
640 }
641 
vidioc_venc_s_selection(struct file * file,void * priv,struct v4l2_selection * s)642 static int vidioc_venc_s_selection(struct file *file, void *priv,
643 				     struct v4l2_selection *s)
644 {
645 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
646 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, s->type);
647 
648 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
649 		return -EINVAL;
650 
651 	switch (s->target) {
652 	case V4L2_SEL_TGT_CROP:
653 		/* Only support crop from (0,0) */
654 		s->r.top = 0;
655 		s->r.left = 0;
656 		s->r.width = min(s->r.width, q_data->coded_width);
657 		s->r.height = min(s->r.height, q_data->coded_height);
658 		q_data->visible_width = s->r.width;
659 		q_data->visible_height = s->r.height;
660 		break;
661 	default:
662 		return -EINVAL;
663 	}
664 	return 0;
665 }
666 
vidioc_venc_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)667 static int vidioc_venc_qbuf(struct file *file, void *priv,
668 			    struct v4l2_buffer *buf)
669 {
670 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
671 
672 	if (ctx->state == MTK_STATE_ABORT) {
673 		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
674 				ctx->id);
675 		return -EIO;
676 	}
677 
678 	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
679 }
680 
vidioc_venc_dqbuf(struct file * file,void * priv,struct v4l2_buffer * buf)681 static int vidioc_venc_dqbuf(struct file *file, void *priv,
682 			     struct v4l2_buffer *buf)
683 {
684 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
685 	int ret;
686 
687 	if (ctx->state == MTK_STATE_ABORT) {
688 		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
689 				ctx->id);
690 		return -EIO;
691 	}
692 
693 	ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
694 	if (ret)
695 		return ret;
696 
697 	/*
698 	 * Complete flush if the user dequeued the 0-payload LAST buffer.
699 	 * We check the payload because a buffer with the LAST flag can also
700 	 * be seen during resolution changes. If we happen to be flushing at
701 	 * that time, the last buffer before the resolution changes could be
702 	 * misinterpreted for the buffer generated by the flush and terminate
703 	 * it earlier than we want.
704 	 */
705 	if (!V4L2_TYPE_IS_OUTPUT(buf->type) &&
706 	    buf->flags & V4L2_BUF_FLAG_LAST &&
707 	    buf->m.planes[0].bytesused == 0 &&
708 	    ctx->is_flushing) {
709 		/*
710 		 * Last CAPTURE buffer is dequeued, we can allow another flush
711 		 * to take place.
712 		 */
713 		ctx->is_flushing = false;
714 	}
715 
716 	return 0;
717 }
718 
vidioc_encoder_cmd(struct file * file,void * priv,struct v4l2_encoder_cmd * cmd)719 static int vidioc_encoder_cmd(struct file *file, void *priv,
720 			      struct v4l2_encoder_cmd *cmd)
721 {
722 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
723 	struct vb2_queue *src_vq, *dst_vq;
724 	int ret;
725 
726 	if (ctx->state == MTK_STATE_ABORT) {
727 		mtk_v4l2_err("[%d] Call to CMD after unrecoverable error",
728 			     ctx->id);
729 		return -EIO;
730 	}
731 
732 	ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, cmd);
733 	if (ret)
734 		return ret;
735 
736 	/* Calling START or STOP is invalid if a flush is in progress */
737 	if (ctx->is_flushing)
738 		return -EBUSY;
739 
740 	mtk_v4l2_debug(1, "encoder cmd=%u", cmd->cmd);
741 
742 	dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
743 				 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
744 	switch (cmd->cmd) {
745 	case V4L2_ENC_CMD_STOP:
746 		src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
747 					 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
748 		if (!vb2_is_streaming(src_vq)) {
749 			mtk_v4l2_debug(1, "Output stream is off. No need to flush.");
750 			return 0;
751 		}
752 		if (!vb2_is_streaming(dst_vq)) {
753 			mtk_v4l2_debug(1, "Capture stream is off. No need to flush.");
754 			return 0;
755 		}
756 		ctx->is_flushing = true;
757 		v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf.vb);
758 		v4l2_m2m_try_schedule(ctx->m2m_ctx);
759 		break;
760 
761 	case V4L2_ENC_CMD_START:
762 		vb2_clear_last_buffer_dequeued(dst_vq);
763 		break;
764 
765 	default:
766 		return -EINVAL;
767 	}
768 
769 	return 0;
770 }
771 
772 const struct v4l2_ioctl_ops mtk_venc_ioctl_ops = {
773 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
774 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
775 
776 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
777 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
778 	.vidioc_qbuf			= vidioc_venc_qbuf,
779 	.vidioc_dqbuf			= vidioc_venc_dqbuf,
780 
781 	.vidioc_querycap		= vidioc_venc_querycap,
782 	.vidioc_enum_fmt_vid_cap	= vidioc_enum_fmt_vid_cap,
783 	.vidioc_enum_fmt_vid_out	= vidioc_enum_fmt_vid_out,
784 	.vidioc_enum_framesizes		= vidioc_enum_framesizes,
785 
786 	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
787 	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
788 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
789 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
790 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
791 
792 	.vidioc_s_parm			= vidioc_venc_s_parm,
793 	.vidioc_g_parm			= vidioc_venc_g_parm,
794 	.vidioc_s_fmt_vid_cap_mplane	= vidioc_venc_s_fmt_cap,
795 	.vidioc_s_fmt_vid_out_mplane	= vidioc_venc_s_fmt_out,
796 
797 	.vidioc_g_fmt_vid_cap_mplane	= vidioc_venc_g_fmt,
798 	.vidioc_g_fmt_vid_out_mplane	= vidioc_venc_g_fmt,
799 
800 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
801 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
802 
803 	.vidioc_g_selection		= vidioc_venc_g_selection,
804 	.vidioc_s_selection		= vidioc_venc_s_selection,
805 
806 	.vidioc_encoder_cmd		= vidioc_encoder_cmd,
807 	.vidioc_try_encoder_cmd		= v4l2_m2m_ioctl_try_encoder_cmd,
808 };
809 
vb2ops_venc_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])810 static int vb2ops_venc_queue_setup(struct vb2_queue *vq,
811 				   unsigned int *nbuffers,
812 				   unsigned int *nplanes,
813 				   unsigned int sizes[],
814 				   struct device *alloc_devs[])
815 {
816 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
817 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, vq->type);
818 	unsigned int i;
819 
820 	if (q_data == NULL)
821 		return -EINVAL;
822 
823 	if (*nplanes) {
824 		for (i = 0; i < *nplanes; i++)
825 			if (sizes[i] < q_data->sizeimage[i])
826 				return -EINVAL;
827 	} else {
828 		*nplanes = q_data->fmt->num_planes;
829 		for (i = 0; i < *nplanes; i++)
830 			sizes[i] = q_data->sizeimage[i];
831 	}
832 
833 	return 0;
834 }
835 
vb2ops_venc_buf_prepare(struct vb2_buffer * vb)836 static int vb2ops_venc_buf_prepare(struct vb2_buffer *vb)
837 {
838 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
839 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, vb->vb2_queue->type);
840 	int i;
841 
842 	for (i = 0; i < q_data->fmt->num_planes; i++) {
843 		if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
844 			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
845 				i, vb2_plane_size(vb, i),
846 				q_data->sizeimage[i]);
847 			return -EINVAL;
848 		}
849 	}
850 
851 	return 0;
852 }
853 
vb2ops_venc_buf_queue(struct vb2_buffer * vb)854 static void vb2ops_venc_buf_queue(struct vb2_buffer *vb)
855 {
856 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
857 	struct vb2_v4l2_buffer *vb2_v4l2 =
858 			container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
859 
860 	struct mtk_video_enc_buf *mtk_buf =
861 			container_of(vb2_v4l2, struct mtk_video_enc_buf,
862 				     m2m_buf.vb);
863 
864 	if ((vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
865 	    (ctx->param_change != MTK_ENCODE_PARAM_NONE)) {
866 		mtk_v4l2_debug(1, "[%d] Before id=%d encode parameter change %x",
867 			       ctx->id,
868 			       vb2_v4l2->vb2_buf.index,
869 			       ctx->param_change);
870 		mtk_buf->param_change = ctx->param_change;
871 		mtk_buf->enc_params = ctx->enc_params;
872 		ctx->param_change = MTK_ENCODE_PARAM_NONE;
873 	}
874 
875 	v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
876 }
877 
vb2ops_venc_start_streaming(struct vb2_queue * q,unsigned int count)878 static int vb2ops_venc_start_streaming(struct vb2_queue *q, unsigned int count)
879 {
880 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
881 	struct venc_enc_param param;
882 	int ret, pm_ret;
883 	int i;
884 
885 	/* Once state turn into MTK_STATE_ABORT, we need stop_streaming
886 	  * to clear it
887 	  */
888 	if ((ctx->state == MTK_STATE_ABORT) || (ctx->state == MTK_STATE_FREE)) {
889 		ret = -EIO;
890 		goto err_start_stream;
891 	}
892 
893 	/* Do the initialization when both start_streaming have been called */
894 	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
895 		if (!vb2_start_streaming_called(&ctx->m2m_ctx->cap_q_ctx.q))
896 			return 0;
897 	} else {
898 		if (!vb2_start_streaming_called(&ctx->m2m_ctx->out_q_ctx.q))
899 			return 0;
900 	}
901 
902 	ret = pm_runtime_resume_and_get(&ctx->dev->plat_dev->dev);
903 	if (ret < 0) {
904 		mtk_v4l2_err("pm_runtime_resume_and_get fail %d", ret);
905 		goto err_start_stream;
906 	}
907 
908 	mtk_venc_set_param(ctx, &param);
909 	ret = venc_if_set_param(ctx, VENC_SET_PARAM_ENC, &param);
910 	if (ret) {
911 		mtk_v4l2_err("venc_if_set_param failed=%d", ret);
912 		ctx->state = MTK_STATE_ABORT;
913 		goto err_set_param;
914 	}
915 	ctx->param_change = MTK_ENCODE_PARAM_NONE;
916 
917 	if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
918 	    (ctx->enc_params.seq_hdr_mode !=
919 				V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)) {
920 		ret = venc_if_set_param(ctx,
921 					VENC_SET_PARAM_PREPEND_HEADER,
922 					NULL);
923 		if (ret) {
924 			mtk_v4l2_err("venc_if_set_param failed=%d", ret);
925 			ctx->state = MTK_STATE_ABORT;
926 			goto err_set_param;
927 		}
928 		ctx->state = MTK_STATE_HEADER;
929 	}
930 
931 	return 0;
932 
933 err_set_param:
934 	pm_ret = pm_runtime_put(&ctx->dev->plat_dev->dev);
935 	if (pm_ret < 0)
936 		mtk_v4l2_err("pm_runtime_put fail %d", pm_ret);
937 
938 err_start_stream:
939 	for (i = 0; i < q->num_buffers; ++i) {
940 		struct vb2_buffer *buf = vb2_get_buffer(q, i);
941 
942 		/*
943 		 * FIXME: This check is not needed as only active buffers
944 		 * can be marked as done.
945 		 */
946 		if (buf->state == VB2_BUF_STATE_ACTIVE) {
947 			mtk_v4l2_debug(0, "[%d] id=%d, type=%d, %d -> VB2_BUF_STATE_QUEUED",
948 					ctx->id, i, q->type,
949 					(int)buf->state);
950 			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(buf),
951 					  VB2_BUF_STATE_QUEUED);
952 		}
953 	}
954 
955 	return ret;
956 }
957 
vb2ops_venc_stop_streaming(struct vb2_queue * q)958 static void vb2ops_venc_stop_streaming(struct vb2_queue *q)
959 {
960 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
961 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
962 	int ret;
963 
964 	mtk_v4l2_debug(2, "[%d]-> type=%d", ctx->id, q->type);
965 
966 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
967 		while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
968 			vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
969 			v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
970 		}
971 		/* STREAMOFF on the CAPTURE queue completes any ongoing flush */
972 		if (ctx->is_flushing) {
973 			struct v4l2_m2m_buffer *b, *n;
974 
975 			mtk_v4l2_debug(1, "STREAMOFF called while flushing");
976 			/*
977 			 * STREAMOFF could be called before the flush buffer is
978 			 * dequeued. Check whether empty flush buf is still in
979 			 * queue before removing it.
980 			 */
981 			v4l2_m2m_for_each_src_buf_safe(ctx->m2m_ctx, b, n) {
982 				if (b == &ctx->empty_flush_buf) {
983 					v4l2_m2m_src_buf_remove_by_buf(ctx->m2m_ctx, &b->vb);
984 					break;
985 				}
986 			}
987 			ctx->is_flushing = false;
988 		}
989 	} else {
990 		while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
991 			if (src_buf != &ctx->empty_flush_buf.vb)
992 				v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
993 		}
994 		if (ctx->is_flushing) {
995 			/*
996 			 * If we are in the middle of a flush, put the flush
997 			 * buffer back into the queue so the next CAPTURE
998 			 * buffer gets returned with the LAST flag set.
999 			 */
1000 			v4l2_m2m_buf_queue(ctx->m2m_ctx,
1001 					   &ctx->empty_flush_buf.vb);
1002 		}
1003 	}
1004 
1005 	if ((q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
1006 	     vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q)) ||
1007 	    (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
1008 	     vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q))) {
1009 		mtk_v4l2_debug(1, "[%d]-> q type %d out=%d cap=%d",
1010 			       ctx->id, q->type,
1011 			       vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q),
1012 			       vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q));
1013 		return;
1014 	}
1015 
1016 	/* Release the encoder if both streams are stopped. */
1017 	ret = venc_if_deinit(ctx);
1018 	if (ret)
1019 		mtk_v4l2_err("venc_if_deinit failed=%d", ret);
1020 
1021 	ret = pm_runtime_put(&ctx->dev->plat_dev->dev);
1022 	if (ret < 0)
1023 		mtk_v4l2_err("pm_runtime_put fail %d", ret);
1024 
1025 	ctx->state = MTK_STATE_FREE;
1026 }
1027 
vb2ops_venc_buf_out_validate(struct vb2_buffer * vb)1028 static int vb2ops_venc_buf_out_validate(struct vb2_buffer *vb)
1029 {
1030 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1031 
1032 	vbuf->field = V4L2_FIELD_NONE;
1033 	return 0;
1034 }
1035 
1036 static const struct vb2_ops mtk_venc_vb2_ops = {
1037 	.queue_setup		= vb2ops_venc_queue_setup,
1038 	.buf_out_validate	= vb2ops_venc_buf_out_validate,
1039 	.buf_prepare		= vb2ops_venc_buf_prepare,
1040 	.buf_queue		= vb2ops_venc_buf_queue,
1041 	.wait_prepare		= vb2_ops_wait_prepare,
1042 	.wait_finish		= vb2_ops_wait_finish,
1043 	.start_streaming	= vb2ops_venc_start_streaming,
1044 	.stop_streaming		= vb2ops_venc_stop_streaming,
1045 };
1046 
mtk_venc_encode_header(void * priv)1047 static int mtk_venc_encode_header(void *priv)
1048 {
1049 	struct mtk_vcodec_ctx *ctx = priv;
1050 	int ret;
1051 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1052 	struct mtk_vcodec_mem bs_buf;
1053 	struct venc_done_result enc_result;
1054 
1055 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1056 	if (!dst_buf) {
1057 		mtk_v4l2_debug(1, "No dst buffer");
1058 		return -EINVAL;
1059 	}
1060 
1061 	bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1062 	bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1063 	bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
1064 
1065 	mtk_v4l2_debug(1,
1066 			"[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu",
1067 			ctx->id,
1068 			dst_buf->vb2_buf.index, bs_buf.va,
1069 			(u64)bs_buf.dma_addr,
1070 			bs_buf.size);
1071 
1072 	ret = venc_if_encode(ctx,
1073 			VENC_START_OPT_ENCODE_SEQUENCE_HEADER,
1074 			NULL, &bs_buf, &enc_result);
1075 
1076 	if (ret) {
1077 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1078 		ctx->state = MTK_STATE_ABORT;
1079 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1080 		mtk_v4l2_err("venc_if_encode failed=%d", ret);
1081 		return -EINVAL;
1082 	}
1083 	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1084 	if (src_buf) {
1085 		dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1086 		dst_buf->timecode = src_buf->timecode;
1087 	} else {
1088 		mtk_v4l2_err("No timestamp for the header buffer.");
1089 	}
1090 
1091 	ctx->state = MTK_STATE_HEADER;
1092 	vb2_set_plane_payload(&dst_buf->vb2_buf, 0, enc_result.bs_size);
1093 	v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1094 
1095 	return 0;
1096 }
1097 
mtk_venc_param_change(struct mtk_vcodec_ctx * ctx)1098 static int mtk_venc_param_change(struct mtk_vcodec_ctx *ctx)
1099 {
1100 	struct venc_enc_param enc_prm;
1101 	struct vb2_v4l2_buffer *vb2_v4l2 = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1102 	struct mtk_video_enc_buf *mtk_buf;
1103 	int ret = 0;
1104 
1105 	/* Don't upcast the empty flush buffer */
1106 	if (vb2_v4l2 == &ctx->empty_flush_buf.vb)
1107 		return 0;
1108 
1109 	mtk_buf = container_of(vb2_v4l2, struct mtk_video_enc_buf, m2m_buf.vb);
1110 
1111 	memset(&enc_prm, 0, sizeof(enc_prm));
1112 	if (mtk_buf->param_change == MTK_ENCODE_PARAM_NONE)
1113 		return 0;
1114 
1115 	if (mtk_buf->param_change & MTK_ENCODE_PARAM_BITRATE) {
1116 		enc_prm.bitrate = mtk_buf->enc_params.bitrate;
1117 		mtk_v4l2_debug(1, "[%d] id=%d, change param br=%d",
1118 				ctx->id,
1119 				vb2_v4l2->vb2_buf.index,
1120 				enc_prm.bitrate);
1121 		ret |= venc_if_set_param(ctx,
1122 					 VENC_SET_PARAM_ADJUST_BITRATE,
1123 					 &enc_prm);
1124 	}
1125 	if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FRAMERATE) {
1126 		enc_prm.frm_rate = mtk_buf->enc_params.framerate_num /
1127 				   mtk_buf->enc_params.framerate_denom;
1128 		mtk_v4l2_debug(1, "[%d] id=%d, change param fr=%d",
1129 			       ctx->id,
1130 			       vb2_v4l2->vb2_buf.index,
1131 			       enc_prm.frm_rate);
1132 		ret |= venc_if_set_param(ctx,
1133 					 VENC_SET_PARAM_ADJUST_FRAMERATE,
1134 					 &enc_prm);
1135 	}
1136 	if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_GOP_SIZE) {
1137 		enc_prm.gop_size = mtk_buf->enc_params.gop_size;
1138 		mtk_v4l2_debug(1, "change param intra period=%d",
1139 			       enc_prm.gop_size);
1140 		ret |= venc_if_set_param(ctx,
1141 					 VENC_SET_PARAM_GOP_SIZE,
1142 					 &enc_prm);
1143 	}
1144 	if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) {
1145 		mtk_v4l2_debug(1, "[%d] id=%d, change param force I=%d",
1146 				ctx->id,
1147 				vb2_v4l2->vb2_buf.index,
1148 				mtk_buf->enc_params.force_intra);
1149 		if (mtk_buf->enc_params.force_intra)
1150 			ret |= venc_if_set_param(ctx,
1151 						 VENC_SET_PARAM_FORCE_INTRA,
1152 						 NULL);
1153 	}
1154 
1155 	mtk_buf->param_change = MTK_ENCODE_PARAM_NONE;
1156 
1157 	if (ret) {
1158 		ctx->state = MTK_STATE_ABORT;
1159 		mtk_v4l2_err("venc_if_set_param %d failed=%d",
1160 				mtk_buf->param_change, ret);
1161 		return -1;
1162 	}
1163 
1164 	return 0;
1165 }
1166 
1167 /*
1168  * v4l2_m2m_streamoff() holds dev_mutex and waits mtk_venc_worker()
1169  * to call v4l2_m2m_job_finish().
1170  * If mtk_venc_worker() tries to acquire dev_mutex, it will deadlock.
1171  * So this function must not try to acquire dev->dev_mutex.
1172  * This means v4l2 ioctls and mtk_venc_worker() can run at the same time.
1173  * mtk_venc_worker() should be carefully implemented to avoid bugs.
1174  */
mtk_venc_worker(struct work_struct * work)1175 static void mtk_venc_worker(struct work_struct *work)
1176 {
1177 	struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
1178 				    encode_work);
1179 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1180 	struct venc_frm_buf frm_buf;
1181 	struct mtk_vcodec_mem bs_buf;
1182 	struct venc_done_result enc_result;
1183 	int ret, i;
1184 
1185 	/* check dst_buf, dst_buf may be removed in device_run
1186 	 * to stored encdoe header so we need check dst_buf and
1187 	 * call job_finish here to prevent recursion
1188 	 */
1189 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1190 	if (!dst_buf) {
1191 		v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1192 		return;
1193 	}
1194 
1195 	src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1196 
1197 	/*
1198 	 * If we see the flush buffer, send an empty buffer with the LAST flag
1199 	 * to the client. is_flushing will be reset at the time the buffer
1200 	 * is dequeued.
1201 	 */
1202 	if (src_buf == &ctx->empty_flush_buf.vb) {
1203 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1204 		dst_buf->flags |= V4L2_BUF_FLAG_LAST;
1205 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1206 		v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1207 		return;
1208 	}
1209 
1210 	memset(&frm_buf, 0, sizeof(frm_buf));
1211 	for (i = 0; i < src_buf->vb2_buf.num_planes ; i++) {
1212 		frm_buf.fb_addr[i].dma_addr =
1213 				vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, i);
1214 		frm_buf.fb_addr[i].size =
1215 				(size_t)src_buf->vb2_buf.planes[i].length;
1216 	}
1217 	bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1218 	bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1219 	bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
1220 
1221 	mtk_v4l2_debug(2,
1222 			"Framebuf PA=%llx Size=0x%zx;PA=0x%llx Size=0x%zx;PA=0x%llx Size=%zu",
1223 			(u64)frm_buf.fb_addr[0].dma_addr,
1224 			frm_buf.fb_addr[0].size,
1225 			(u64)frm_buf.fb_addr[1].dma_addr,
1226 			frm_buf.fb_addr[1].size,
1227 			(u64)frm_buf.fb_addr[2].dma_addr,
1228 			frm_buf.fb_addr[2].size);
1229 
1230 	ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME,
1231 			     &frm_buf, &bs_buf, &enc_result);
1232 
1233 	dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1234 	dst_buf->timecode = src_buf->timecode;
1235 
1236 	if (enc_result.is_key_frm)
1237 		dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1238 
1239 	if (ret) {
1240 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1241 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1242 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1243 		mtk_v4l2_err("venc_if_encode failed=%d", ret);
1244 	} else {
1245 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1246 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, enc_result.bs_size);
1247 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1248 		mtk_v4l2_debug(2, "venc_if_encode bs size=%d",
1249 				 enc_result.bs_size);
1250 	}
1251 
1252 	v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1253 
1254 	mtk_v4l2_debug(1, "<=== src_buf[%d] dst_buf[%d] venc_if_encode ret=%d Size=%u===>",
1255 			src_buf->vb2_buf.index, dst_buf->vb2_buf.index, ret,
1256 			enc_result.bs_size);
1257 }
1258 
m2mops_venc_device_run(void * priv)1259 static void m2mops_venc_device_run(void *priv)
1260 {
1261 	struct mtk_vcodec_ctx *ctx = priv;
1262 
1263 	if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
1264 	    (ctx->state != MTK_STATE_HEADER)) {
1265 		/* encode h264 sps/pps header */
1266 		mtk_venc_encode_header(ctx);
1267 		queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1268 		return;
1269 	}
1270 
1271 	mtk_venc_param_change(ctx);
1272 	queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1273 }
1274 
m2mops_venc_job_ready(void * m2m_priv)1275 static int m2mops_venc_job_ready(void *m2m_priv)
1276 {
1277 	struct mtk_vcodec_ctx *ctx = m2m_priv;
1278 
1279 	if (ctx->state == MTK_STATE_ABORT || ctx->state == MTK_STATE_FREE) {
1280 		mtk_v4l2_debug(3, "[%d]Not ready: state=0x%x.",
1281 			       ctx->id, ctx->state);
1282 		return 0;
1283 	}
1284 
1285 	return 1;
1286 }
1287 
m2mops_venc_job_abort(void * priv)1288 static void m2mops_venc_job_abort(void *priv)
1289 {
1290 	struct mtk_vcodec_ctx *ctx = priv;
1291 
1292 	ctx->state = MTK_STATE_ABORT;
1293 }
1294 
1295 const struct v4l2_m2m_ops mtk_venc_m2m_ops = {
1296 	.device_run	= m2mops_venc_device_run,
1297 	.job_ready	= m2mops_venc_job_ready,
1298 	.job_abort	= m2mops_venc_job_abort,
1299 };
1300 
mtk_vcodec_enc_set_default_params(struct mtk_vcodec_ctx * ctx)1301 void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_ctx *ctx)
1302 {
1303 	struct mtk_q_data *q_data;
1304 
1305 	ctx->m2m_ctx->q_lock = &ctx->q_mutex;
1306 	ctx->fh.m2m_ctx = ctx->m2m_ctx;
1307 	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1308 	INIT_WORK(&ctx->encode_work, mtk_venc_worker);
1309 
1310 	ctx->colorspace = V4L2_COLORSPACE_REC709;
1311 	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1312 	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1313 	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1314 
1315 	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
1316 	memset(q_data, 0, sizeof(struct mtk_q_data));
1317 	q_data->visible_width = DFT_CFG_WIDTH;
1318 	q_data->visible_height = DFT_CFG_HEIGHT;
1319 	q_data->coded_width = DFT_CFG_WIDTH;
1320 	q_data->coded_height = DFT_CFG_HEIGHT;
1321 	q_data->field = V4L2_FIELD_NONE;
1322 
1323 	q_data->fmt = &ctx->dev->venc_pdata->output_formats[0];
1324 
1325 	v4l_bound_align_image(&q_data->coded_width,
1326 				MTK_VENC_MIN_W,
1327 				MTK_VENC_HD_MAX_W, 4,
1328 				&q_data->coded_height,
1329 				MTK_VENC_MIN_H,
1330 				MTK_VENC_HD_MAX_H, 5, 6);
1331 
1332 	if (q_data->coded_width < DFT_CFG_WIDTH &&
1333 		(q_data->coded_width + 16) <= MTK_VENC_HD_MAX_W)
1334 		q_data->coded_width += 16;
1335 	if (q_data->coded_height < DFT_CFG_HEIGHT &&
1336 		(q_data->coded_height + 32) <= MTK_VENC_HD_MAX_H)
1337 		q_data->coded_height += 32;
1338 
1339 	q_data->sizeimage[0] =
1340 		q_data->coded_width * q_data->coded_height+
1341 		((ALIGN(q_data->coded_width, 16) * 2) * 16);
1342 	q_data->bytesperline[0] = q_data->coded_width;
1343 	q_data->sizeimage[1] =
1344 		(q_data->coded_width * q_data->coded_height) / 2 +
1345 		(ALIGN(q_data->coded_width, 16) * 16);
1346 	q_data->bytesperline[1] = q_data->coded_width;
1347 
1348 	q_data = &ctx->q_data[MTK_Q_DATA_DST];
1349 	memset(q_data, 0, sizeof(struct mtk_q_data));
1350 	q_data->coded_width = DFT_CFG_WIDTH;
1351 	q_data->coded_height = DFT_CFG_HEIGHT;
1352 	q_data->fmt = &ctx->dev->venc_pdata->capture_formats[0];
1353 	q_data->field = V4L2_FIELD_NONE;
1354 	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
1355 		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
1356 	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
1357 
1358 	ctx->enc_params.framerate_num = MTK_DEFAULT_FRAMERATE_NUM;
1359 	ctx->enc_params.framerate_denom = MTK_DEFAULT_FRAMERATE_DENOM;
1360 }
1361 
mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx * ctx)1362 int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx)
1363 {
1364 	const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops;
1365 	struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
1366 	u8 h264_max_level;
1367 
1368 	if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE)
1369 		h264_max_level = V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
1370 	else
1371 		h264_max_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
1372 
1373 	v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT);
1374 
1375 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
1376 			  1, 1, 1, 1);
1377 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE,
1378 			  ctx->dev->venc_pdata->min_bitrate,
1379 			  ctx->dev->venc_pdata->max_bitrate, 1, 4000000);
1380 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES,
1381 			0, 2, 1, 0);
1382 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
1383 			0, 1, 1, 1);
1384 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
1385 			0, 51, 1, 51);
1386 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
1387 			0, 65535, 1, 0);
1388 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
1389 			0, 65535, 1, 0);
1390 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
1391 			0, 1, 1, 0);
1392 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
1393 			0, 0, 0, 0);
1394 	v4l2_ctrl_new_std_menu(handler, ops,
1395 			V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1396 			V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1397 			0, V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE);
1398 	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1399 			V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
1400 			0, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
1401 	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1402 			       h264_max_level,
1403 			       0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
1404 	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
1405 			       V4L2_MPEG_VIDEO_VP8_PROFILE_0, 0, V4L2_MPEG_VIDEO_VP8_PROFILE_0);
1406 	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1407 			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
1408 			       ~(1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR),
1409 			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
1410 
1411 
1412 	if (handler->error) {
1413 		mtk_v4l2_err("Init control handler fail %d",
1414 				handler->error);
1415 		return handler->error;
1416 	}
1417 
1418 	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1419 
1420 	return 0;
1421 }
1422 
mtk_vcodec_enc_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)1423 int mtk_vcodec_enc_queue_init(void *priv, struct vb2_queue *src_vq,
1424 			      struct vb2_queue *dst_vq)
1425 {
1426 	struct mtk_vcodec_ctx *ctx = priv;
1427 	int ret;
1428 
1429 	/* Note: VB2_USERPTR works with dma-contig because mt8173
1430 	 * support iommu
1431 	 * https://patchwork.kernel.org/patch/8335461/
1432 	 * https://patchwork.kernel.org/patch/7596181/
1433 	 */
1434 	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1435 	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1436 	src_vq->drv_priv	= ctx;
1437 	src_vq->buf_struct_size = sizeof(struct mtk_video_enc_buf);
1438 	src_vq->ops		= &mtk_venc_vb2_ops;
1439 	src_vq->mem_ops		= &vb2_dma_contig_memops;
1440 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1441 	src_vq->lock		= &ctx->q_mutex;
1442 	src_vq->dev		= &ctx->dev->plat_dev->dev;
1443 
1444 	ret = vb2_queue_init(src_vq);
1445 	if (ret)
1446 		return ret;
1447 
1448 	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1449 	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1450 	dst_vq->drv_priv	= ctx;
1451 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1452 	dst_vq->ops		= &mtk_venc_vb2_ops;
1453 	dst_vq->mem_ops		= &vb2_dma_contig_memops;
1454 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1455 	dst_vq->lock		= &ctx->q_mutex;
1456 	dst_vq->dev		= &ctx->dev->plat_dev->dev;
1457 
1458 	return vb2_queue_init(dst_vq);
1459 }
1460 
mtk_venc_unlock(struct mtk_vcodec_ctx * ctx)1461 int mtk_venc_unlock(struct mtk_vcodec_ctx *ctx)
1462 {
1463 	struct mtk_vcodec_dev *dev = ctx->dev;
1464 
1465 	mutex_unlock(&dev->enc_mutex);
1466 	return 0;
1467 }
1468 
mtk_venc_lock(struct mtk_vcodec_ctx * ctx)1469 int mtk_venc_lock(struct mtk_vcodec_ctx *ctx)
1470 {
1471 	struct mtk_vcodec_dev *dev = ctx->dev;
1472 
1473 	mutex_lock(&dev->enc_mutex);
1474 	return 0;
1475 }
1476 
mtk_vcodec_enc_release(struct mtk_vcodec_ctx * ctx)1477 void mtk_vcodec_enc_release(struct mtk_vcodec_ctx *ctx)
1478 {
1479 	int ret = venc_if_deinit(ctx);
1480 
1481 	if (ret)
1482 		mtk_v4l2_err("venc_if_deinit failed=%d", ret);
1483 
1484 	ctx->state = MTK_STATE_FREE;
1485 }
1486