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