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