1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 MediaTek Inc.
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 #include <linux/pm_runtime.h>
14 #include "mtk_vcodec_drv.h"
15 #include "mtk_vcodec_enc.h"
16 #include "mtk_vcodec_intr.h"
17 #include "mtk_vcodec_util.h"
18 #include "venc_drv_if.h"
20 #define MTK_VENC_MIN_W 160U
21 #define MTK_VENC_MIN_H 128U
22 #define MTK_VENC_MAX_W 1920U
23 #define MTK_VENC_MAX_H 1088U
24 #define DFT_CFG_WIDTH MTK_VENC_MIN_W
25 #define DFT_CFG_HEIGHT MTK_VENC_MIN_H
26 #define MTK_MAX_CTRLS_HINT 20
28 #define MTK_DEFAULT_FRAMERATE_NUM 1001
29 #define MTK_DEFAULT_FRAMERATE_DENOM 30000
31 static void mtk_venc_worker(struct work_struct *work);
33 static const struct v4l2_frmsize_stepwise mtk_venc_framesizes = {
34 MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16,
35 MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16,
38 #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_venc_framesizes)
40 static int vidioc_venc_s_ctrl(struct v4l2_ctrl *ctrl)
42 struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
43 struct mtk_enc_params *p = &ctx->enc_params;
47 case V4L2_CID_MPEG_VIDEO_BITRATE:
48 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_BITRATE val = %d",
50 p->bitrate = ctrl->val;
51 ctx->param_change |= MTK_ENCODE_PARAM_BITRATE;
53 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
54 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_B_FRAMES val = %d",
56 p->num_b_frame = ctrl->val;
58 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
59 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE val = %d",
61 p->rc_frame = ctrl->val;
63 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
64 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_MAX_QP val = %d",
66 p->h264_max_qp = ctrl->val;
68 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
69 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_HEADER_MODE val = %d",
71 p->seq_hdr_mode = ctrl->val;
73 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
74 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE val = %d",
78 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
79 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_PROFILE val = %d",
81 p->h264_profile = ctrl->val;
83 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
84 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_LEVEL val = %d",
86 p->h264_level = ctrl->val;
88 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
89 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_I_PERIOD val = %d",
91 p->intra_period = ctrl->val;
92 ctx->param_change |= MTK_ENCODE_PARAM_INTRA_PERIOD;
94 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
95 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_GOP_SIZE val = %d",
97 p->gop_size = ctrl->val;
98 ctx->param_change |= MTK_ENCODE_PARAM_GOP_SIZE;
100 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
101 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME");
103 ctx->param_change |= MTK_ENCODE_PARAM_FORCE_INTRA;
113 static const struct v4l2_ctrl_ops mtk_vcodec_enc_ctrl_ops = {
114 .s_ctrl = vidioc_venc_s_ctrl,
117 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f,
118 const struct mtk_video_fmt *formats,
121 if (f->index >= num_formats)
124 f->pixelformat = formats[f->index].fourcc;
129 static const struct mtk_video_fmt *
130 mtk_venc_find_format(u32 fourcc, const struct mtk_vcodec_enc_pdata *pdata)
132 const struct mtk_video_fmt *fmt;
135 for (k = 0; k < pdata->num_capture_formats; k++) {
136 fmt = &pdata->capture_formats[k];
137 if (fmt->fourcc == fourcc)
141 for (k = 0; k < pdata->num_output_formats; k++) {
142 fmt = &pdata->output_formats[k];
143 if (fmt->fourcc == fourcc)
150 static int vidioc_enum_framesizes(struct file *file, void *fh,
151 struct v4l2_frmsizeenum *fsize)
153 const struct mtk_video_fmt *fmt;
155 if (fsize->index != 0)
158 fmt = mtk_venc_find_format(fsize->pixel_format,
159 fh_to_ctx(fh)->dev->venc_pdata);
163 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
164 fsize->stepwise = mtk_venc_framesizes;
169 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
170 struct v4l2_fmtdesc *f)
172 const struct mtk_vcodec_enc_pdata *pdata =
173 fh_to_ctx(priv)->dev->venc_pdata;
175 return vidioc_enum_fmt(f, pdata->capture_formats,
176 pdata->num_capture_formats);
179 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
180 struct v4l2_fmtdesc *f)
182 const struct mtk_vcodec_enc_pdata *pdata =
183 fh_to_ctx(priv)->dev->venc_pdata;
185 return vidioc_enum_fmt(f, pdata->output_formats,
186 pdata->num_output_formats);
189 static int vidioc_venc_querycap(struct file *file, void *priv,
190 struct v4l2_capability *cap)
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));
199 static int vidioc_venc_s_parm(struct file *file, void *priv,
200 struct v4l2_streamparm *a)
202 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
203 struct v4l2_fract *timeperframe = &a->parm.output.timeperframe;
205 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
208 if (timeperframe->numerator == 0 || timeperframe->denominator == 0) {
209 timeperframe->numerator = MTK_DEFAULT_FRAMERATE_NUM;
210 timeperframe->denominator = MTK_DEFAULT_FRAMERATE_DENOM;
213 ctx->enc_params.framerate_num = timeperframe->denominator;
214 ctx->enc_params.framerate_denom = timeperframe->numerator;
215 ctx->param_change |= MTK_ENCODE_PARAM_FRAMERATE;
217 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
222 static int vidioc_venc_g_parm(struct file *file, void *priv,
223 struct v4l2_streamparm *a)
225 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
227 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
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;
239 static struct mtk_q_data *mtk_venc_get_q_data(struct mtk_vcodec_ctx *ctx,
240 enum v4l2_buf_type type)
242 if (V4L2_TYPE_IS_OUTPUT(type))
243 return &ctx->q_data[MTK_Q_DATA_SRC];
245 return &ctx->q_data[MTK_Q_DATA_DST];
248 /* V4L2 specification suggests the driver corrects the format struct if any of
249 * the dimensions is unsupported
251 static int vidioc_try_fmt(struct v4l2_format *f,
252 const struct mtk_video_fmt *fmt)
254 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
256 pix_fmt_mp->field = V4L2_FIELD_NONE;
258 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
259 pix_fmt_mp->num_planes = 1;
260 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
261 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
264 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
267 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
271 /* find next closer width align 16, heign align 32, size align
274 tmp_w = pix_fmt_mp->width;
275 tmp_h = pix_fmt_mp->height;
276 v4l_bound_align_image(&pix_fmt_mp->width,
281 MTK_VENC_MAX_H, 5, 6);
283 if (pix_fmt_mp->width < tmp_w &&
284 (pix_fmt_mp->width + 16) <= MTK_VENC_MAX_W)
285 pix_fmt_mp->width += 16;
286 if (pix_fmt_mp->height < tmp_h &&
287 (pix_fmt_mp->height + 32) <= MTK_VENC_MAX_H)
288 pix_fmt_mp->height += 32;
291 "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d %d",
292 tmp_w, tmp_h, pix_fmt_mp->width,
294 pix_fmt_mp->plane_fmt[0].sizeimage,
295 pix_fmt_mp->plane_fmt[1].sizeimage);
297 pix_fmt_mp->num_planes = fmt->num_planes;
298 pix_fmt_mp->plane_fmt[0].sizeimage =
299 pix_fmt_mp->width * pix_fmt_mp->height +
300 ((ALIGN(pix_fmt_mp->width, 16) * 2) * 16);
301 pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
303 if (pix_fmt_mp->num_planes == 2) {
304 pix_fmt_mp->plane_fmt[1].sizeimage =
305 (pix_fmt_mp->width * pix_fmt_mp->height) / 2 +
306 (ALIGN(pix_fmt_mp->width, 16) * 16);
307 pix_fmt_mp->plane_fmt[2].sizeimage = 0;
308 pix_fmt_mp->plane_fmt[1].bytesperline =
310 pix_fmt_mp->plane_fmt[2].bytesperline = 0;
311 } else if (pix_fmt_mp->num_planes == 3) {
312 pix_fmt_mp->plane_fmt[1].sizeimage =
313 pix_fmt_mp->plane_fmt[2].sizeimage =
314 (pix_fmt_mp->width * pix_fmt_mp->height) / 4 +
315 ((ALIGN(pix_fmt_mp->width, 16) / 2) * 16);
316 pix_fmt_mp->plane_fmt[1].bytesperline =
317 pix_fmt_mp->plane_fmt[2].bytesperline =
318 pix_fmt_mp->width / 2;
322 pix_fmt_mp->flags = 0;
327 static void mtk_venc_set_param(struct mtk_vcodec_ctx *ctx,
328 struct venc_enc_param *param)
330 struct mtk_q_data *q_data_src = &ctx->q_data[MTK_Q_DATA_SRC];
331 struct mtk_enc_params *enc_params = &ctx->enc_params;
333 switch (q_data_src->fmt->fourcc) {
334 case V4L2_PIX_FMT_YUV420M:
335 param->input_yuv_fmt = VENC_YUV_FORMAT_I420;
337 case V4L2_PIX_FMT_YVU420M:
338 param->input_yuv_fmt = VENC_YUV_FORMAT_YV12;
340 case V4L2_PIX_FMT_NV12M:
341 param->input_yuv_fmt = VENC_YUV_FORMAT_NV12;
343 case V4L2_PIX_FMT_NV21M:
344 param->input_yuv_fmt = VENC_YUV_FORMAT_NV21;
347 mtk_v4l2_err("Unsupported fourcc =%d", q_data_src->fmt->fourcc);
350 param->h264_profile = enc_params->h264_profile;
351 param->h264_level = enc_params->h264_level;
353 /* Config visible resolution */
354 param->width = q_data_src->visible_width;
355 param->height = q_data_src->visible_height;
356 /* Config coded resolution */
357 param->buf_width = q_data_src->coded_width;
358 param->buf_height = q_data_src->coded_height;
359 param->frm_rate = enc_params->framerate_num /
360 enc_params->framerate_denom;
361 param->intra_period = enc_params->intra_period;
362 param->gop_size = enc_params->gop_size;
363 param->bitrate = enc_params->bitrate;
366 "fmt 0x%x, P/L %d/%d, w/h %d/%d, buf %d/%d, fps/bps %d/%d, gop %d, i_period %d",
367 param->input_yuv_fmt, param->h264_profile,
368 param->h264_level, param->width, param->height,
369 param->buf_width, param->buf_height,
370 param->frm_rate, param->bitrate,
371 param->gop_size, param->intra_period);
374 static int vidioc_venc_s_fmt_cap(struct file *file, void *priv,
375 struct v4l2_format *f)
377 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
378 const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
379 struct vb2_queue *vq;
380 struct mtk_q_data *q_data;
382 const struct mtk_video_fmt *fmt;
384 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
386 mtk_v4l2_err("fail to get vq");
390 if (vb2_is_busy(vq)) {
391 mtk_v4l2_err("queue busy");
395 q_data = mtk_venc_get_q_data(ctx, f->type);
397 mtk_v4l2_err("fail to get q data");
401 fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
403 fmt = &ctx->dev->venc_pdata->capture_formats[0];
404 f->fmt.pix.pixelformat = fmt->fourcc;
408 ret = vidioc_try_fmt(f, q_data->fmt);
412 q_data->coded_width = f->fmt.pix_mp.width;
413 q_data->coded_height = f->fmt.pix_mp.height;
414 q_data->field = f->fmt.pix_mp.field;
416 for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
417 struct v4l2_plane_pix_format *plane_fmt;
419 plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
420 q_data->bytesperline[i] = plane_fmt->bytesperline;
421 q_data->sizeimage[i] = plane_fmt->sizeimage;
424 if (ctx->state == MTK_STATE_FREE) {
425 ret = venc_if_init(ctx, q_data->fmt->fourcc);
427 mtk_v4l2_err("venc_if_init failed=%d, codec type=%x",
428 ret, q_data->fmt->fourcc);
431 ctx->state = MTK_STATE_INIT;
437 static int vidioc_venc_s_fmt_out(struct file *file, void *priv,
438 struct v4l2_format *f)
440 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
441 const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
442 struct vb2_queue *vq;
443 struct mtk_q_data *q_data;
445 const struct mtk_video_fmt *fmt;
446 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
448 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
450 mtk_v4l2_err("fail to get vq");
454 if (vb2_is_busy(vq)) {
455 mtk_v4l2_err("queue busy");
459 q_data = mtk_venc_get_q_data(ctx, f->type);
461 mtk_v4l2_err("fail to get q data");
465 fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
467 fmt = &ctx->dev->venc_pdata->output_formats[0];
468 f->fmt.pix.pixelformat = fmt->fourcc;
471 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
474 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
478 q_data->visible_width = f->fmt.pix_mp.width;
479 q_data->visible_height = f->fmt.pix_mp.height;
481 ret = vidioc_try_fmt(f, q_data->fmt);
485 q_data->coded_width = f->fmt.pix_mp.width;
486 q_data->coded_height = f->fmt.pix_mp.height;
488 q_data->field = f->fmt.pix_mp.field;
489 ctx->colorspace = f->fmt.pix_mp.colorspace;
490 ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
491 ctx->quantization = f->fmt.pix_mp.quantization;
492 ctx->xfer_func = f->fmt.pix_mp.xfer_func;
494 for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
495 struct v4l2_plane_pix_format *plane_fmt;
497 plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
498 q_data->bytesperline[i] = plane_fmt->bytesperline;
499 q_data->sizeimage[i] = plane_fmt->sizeimage;
505 static int vidioc_venc_g_fmt(struct file *file, void *priv,
506 struct v4l2_format *f)
508 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
509 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
510 struct vb2_queue *vq;
511 struct mtk_q_data *q_data;
514 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
518 q_data = mtk_venc_get_q_data(ctx, f->type);
520 pix->width = q_data->coded_width;
521 pix->height = q_data->coded_height;
522 pix->pixelformat = q_data->fmt->fourcc;
523 pix->field = q_data->field;
524 pix->num_planes = q_data->fmt->num_planes;
525 for (i = 0; i < pix->num_planes; i++) {
526 pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
527 pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
531 pix->colorspace = ctx->colorspace;
532 pix->ycbcr_enc = ctx->ycbcr_enc;
533 pix->quantization = ctx->quantization;
534 pix->xfer_func = ctx->xfer_func;
539 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
540 struct v4l2_format *f)
542 const struct mtk_video_fmt *fmt;
543 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
544 const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
546 fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
548 fmt = &ctx->dev->venc_pdata->capture_formats[0];
549 f->fmt.pix.pixelformat = fmt->fourcc;
551 f->fmt.pix_mp.colorspace = ctx->colorspace;
552 f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc;
553 f->fmt.pix_mp.quantization = ctx->quantization;
554 f->fmt.pix_mp.xfer_func = ctx->xfer_func;
556 return vidioc_try_fmt(f, fmt);
559 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
560 struct v4l2_format *f)
562 const struct mtk_video_fmt *fmt;
563 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
564 const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
566 fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
568 fmt = &ctx->dev->venc_pdata->output_formats[0];
569 f->fmt.pix.pixelformat = fmt->fourcc;
571 if (!f->fmt.pix_mp.colorspace) {
572 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
573 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
574 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
575 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
578 return vidioc_try_fmt(f, fmt);
581 static int vidioc_venc_g_selection(struct file *file, void *priv,
582 struct v4l2_selection *s)
584 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
585 struct mtk_q_data *q_data;
587 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
590 q_data = mtk_venc_get_q_data(ctx, s->type);
595 case V4L2_SEL_TGT_CROP_DEFAULT:
596 case V4L2_SEL_TGT_CROP_BOUNDS:
599 s->r.width = q_data->coded_width;
600 s->r.height = q_data->coded_height;
602 case V4L2_SEL_TGT_CROP:
605 s->r.width = q_data->visible_width;
606 s->r.height = q_data->visible_height;
615 static int vidioc_venc_s_selection(struct file *file, void *priv,
616 struct v4l2_selection *s)
618 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
619 struct mtk_q_data *q_data;
621 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
624 q_data = mtk_venc_get_q_data(ctx, s->type);
629 case V4L2_SEL_TGT_CROP:
630 /* Only support crop from (0,0) */
633 s->r.width = min(s->r.width, q_data->coded_width);
634 s->r.height = min(s->r.height, q_data->coded_height);
635 q_data->visible_width = s->r.width;
636 q_data->visible_height = s->r.height;
644 static int vidioc_venc_qbuf(struct file *file, void *priv,
645 struct v4l2_buffer *buf)
647 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
649 if (ctx->state == MTK_STATE_ABORT) {
650 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
655 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
658 static int vidioc_venc_dqbuf(struct file *file, void *priv,
659 struct v4l2_buffer *buf)
661 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
663 if (ctx->state == MTK_STATE_ABORT) {
664 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
669 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
672 const struct v4l2_ioctl_ops mtk_venc_ioctl_ops = {
673 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
674 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
676 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
677 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
678 .vidioc_qbuf = vidioc_venc_qbuf,
679 .vidioc_dqbuf = vidioc_venc_dqbuf,
681 .vidioc_querycap = vidioc_venc_querycap,
682 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
683 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
684 .vidioc_enum_framesizes = vidioc_enum_framesizes,
686 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
687 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
688 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
689 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
690 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
692 .vidioc_s_parm = vidioc_venc_s_parm,
693 .vidioc_g_parm = vidioc_venc_g_parm,
694 .vidioc_s_fmt_vid_cap_mplane = vidioc_venc_s_fmt_cap,
695 .vidioc_s_fmt_vid_out_mplane = vidioc_venc_s_fmt_out,
697 .vidioc_g_fmt_vid_cap_mplane = vidioc_venc_g_fmt,
698 .vidioc_g_fmt_vid_out_mplane = vidioc_venc_g_fmt,
700 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
701 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
703 .vidioc_g_selection = vidioc_venc_g_selection,
704 .vidioc_s_selection = vidioc_venc_s_selection,
707 static int vb2ops_venc_queue_setup(struct vb2_queue *vq,
708 unsigned int *nbuffers,
709 unsigned int *nplanes,
710 unsigned int sizes[],
711 struct device *alloc_devs[])
713 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
714 struct mtk_q_data *q_data;
717 q_data = mtk_venc_get_q_data(ctx, vq->type);
723 for (i = 0; i < *nplanes; i++)
724 if (sizes[i] < q_data->sizeimage[i])
727 *nplanes = q_data->fmt->num_planes;
728 for (i = 0; i < *nplanes; i++)
729 sizes[i] = q_data->sizeimage[i];
735 static int vb2ops_venc_buf_prepare(struct vb2_buffer *vb)
737 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
738 struct mtk_q_data *q_data;
741 q_data = mtk_venc_get_q_data(ctx, vb->vb2_queue->type);
743 for (i = 0; i < q_data->fmt->num_planes; i++) {
744 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
745 mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
746 i, vb2_plane_size(vb, i),
747 q_data->sizeimage[i]);
755 static void vb2ops_venc_buf_queue(struct vb2_buffer *vb)
757 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
758 struct vb2_v4l2_buffer *vb2_v4l2 =
759 container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
761 struct mtk_video_enc_buf *mtk_buf =
762 container_of(vb2_v4l2, struct mtk_video_enc_buf,
765 if ((vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
766 (ctx->param_change != MTK_ENCODE_PARAM_NONE)) {
767 mtk_v4l2_debug(1, "[%d] Before id=%d encode parameter change %x",
769 vb2_v4l2->vb2_buf.index,
771 mtk_buf->param_change = ctx->param_change;
772 mtk_buf->enc_params = ctx->enc_params;
773 ctx->param_change = MTK_ENCODE_PARAM_NONE;
776 v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
779 static int vb2ops_venc_start_streaming(struct vb2_queue *q, unsigned int count)
781 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
782 struct venc_enc_param param;
786 /* Once state turn into MTK_STATE_ABORT, we need stop_streaming
789 if ((ctx->state == MTK_STATE_ABORT) || (ctx->state == MTK_STATE_FREE)) {
791 goto err_start_stream;
794 /* Do the initialization when both start_streaming have been called */
795 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
796 if (!vb2_start_streaming_called(&ctx->m2m_ctx->cap_q_ctx.q))
799 if (!vb2_start_streaming_called(&ctx->m2m_ctx->out_q_ctx.q))
803 ret = pm_runtime_resume_and_get(&ctx->dev->plat_dev->dev);
805 mtk_v4l2_err("pm_runtime_resume_and_get fail %d", ret);
806 goto err_start_stream;
809 mtk_venc_set_param(ctx, ¶m);
810 ret = venc_if_set_param(ctx, VENC_SET_PARAM_ENC, ¶m);
812 mtk_v4l2_err("venc_if_set_param failed=%d", ret);
813 ctx->state = MTK_STATE_ABORT;
816 ctx->param_change = MTK_ENCODE_PARAM_NONE;
818 if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
819 (ctx->enc_params.seq_hdr_mode !=
820 V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)) {
821 ret = venc_if_set_param(ctx,
822 VENC_SET_PARAM_PREPEND_HEADER,
825 mtk_v4l2_err("venc_if_set_param failed=%d", ret);
826 ctx->state = MTK_STATE_ABORT;
829 ctx->state = MTK_STATE_HEADER;
835 ret = pm_runtime_put(&ctx->dev->plat_dev->dev);
837 mtk_v4l2_err("pm_runtime_put fail %d", ret);
840 for (i = 0; i < q->num_buffers; ++i) {
841 struct vb2_buffer *buf = vb2_get_buffer(q, i);
844 * FIXME: This check is not needed as only active buffers
845 * can be marked as done.
847 if (buf->state == VB2_BUF_STATE_ACTIVE) {
848 mtk_v4l2_debug(0, "[%d] id=%d, type=%d, %d -> VB2_BUF_STATE_QUEUED",
851 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(buf),
852 VB2_BUF_STATE_QUEUED);
859 static void vb2ops_venc_stop_streaming(struct vb2_queue *q)
861 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
862 struct vb2_v4l2_buffer *src_buf, *dst_buf;
865 mtk_v4l2_debug(2, "[%d]-> type=%d", ctx->id, q->type);
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);
873 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx)))
874 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
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",
883 vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q),
884 vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q));
888 /* Release the encoder if both streams are stopped. */
889 ret = venc_if_deinit(ctx);
891 mtk_v4l2_err("venc_if_deinit failed=%d", ret);
893 ret = pm_runtime_put(&ctx->dev->plat_dev->dev);
895 mtk_v4l2_err("pm_runtime_put fail %d", ret);
897 ctx->state = MTK_STATE_FREE;
900 static int vb2ops_venc_buf_out_validate(struct vb2_buffer *vb)
902 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
904 vbuf->field = V4L2_FIELD_NONE;
908 static const struct vb2_ops mtk_venc_vb2_ops = {
909 .queue_setup = vb2ops_venc_queue_setup,
910 .buf_out_validate = vb2ops_venc_buf_out_validate,
911 .buf_prepare = vb2ops_venc_buf_prepare,
912 .buf_queue = vb2ops_venc_buf_queue,
913 .wait_prepare = vb2_ops_wait_prepare,
914 .wait_finish = vb2_ops_wait_finish,
915 .start_streaming = vb2ops_venc_start_streaming,
916 .stop_streaming = vb2ops_venc_stop_streaming,
919 static int mtk_venc_encode_header(void *priv)
921 struct mtk_vcodec_ctx *ctx = priv;
923 struct vb2_v4l2_buffer *src_buf, *dst_buf;
924 struct mtk_vcodec_mem bs_buf;
925 struct venc_done_result enc_result;
927 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
929 mtk_v4l2_debug(1, "No dst buffer");
933 bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
934 bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
935 bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
938 "[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu",
940 dst_buf->vb2_buf.index, bs_buf.va,
941 (u64)bs_buf.dma_addr,
944 ret = venc_if_encode(ctx,
945 VENC_START_OPT_ENCODE_SEQUENCE_HEADER,
946 NULL, &bs_buf, &enc_result);
949 dst_buf->vb2_buf.planes[0].bytesused = 0;
950 ctx->state = MTK_STATE_ABORT;
951 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
952 mtk_v4l2_err("venc_if_encode failed=%d", ret);
955 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
957 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
958 dst_buf->timecode = src_buf->timecode;
960 mtk_v4l2_err("No timestamp for the header buffer.");
963 ctx->state = MTK_STATE_HEADER;
964 dst_buf->vb2_buf.planes[0].bytesused = enc_result.bs_size;
965 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
970 static int mtk_venc_param_change(struct mtk_vcodec_ctx *ctx)
972 struct venc_enc_param enc_prm;
973 struct vb2_v4l2_buffer *vb2_v4l2 = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
974 struct mtk_video_enc_buf *mtk_buf =
975 container_of(vb2_v4l2, struct mtk_video_enc_buf,
980 memset(&enc_prm, 0, sizeof(enc_prm));
981 if (mtk_buf->param_change == MTK_ENCODE_PARAM_NONE)
984 if (mtk_buf->param_change & MTK_ENCODE_PARAM_BITRATE) {
985 enc_prm.bitrate = mtk_buf->enc_params.bitrate;
986 mtk_v4l2_debug(1, "[%d] id=%d, change param br=%d",
988 vb2_v4l2->vb2_buf.index,
990 ret |= venc_if_set_param(ctx,
991 VENC_SET_PARAM_ADJUST_BITRATE,
994 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FRAMERATE) {
995 enc_prm.frm_rate = mtk_buf->enc_params.framerate_num /
996 mtk_buf->enc_params.framerate_denom;
997 mtk_v4l2_debug(1, "[%d] id=%d, change param fr=%d",
999 vb2_v4l2->vb2_buf.index,
1001 ret |= venc_if_set_param(ctx,
1002 VENC_SET_PARAM_ADJUST_FRAMERATE,
1005 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_GOP_SIZE) {
1006 enc_prm.gop_size = mtk_buf->enc_params.gop_size;
1007 mtk_v4l2_debug(1, "change param intra period=%d",
1009 ret |= venc_if_set_param(ctx,
1010 VENC_SET_PARAM_GOP_SIZE,
1013 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) {
1014 mtk_v4l2_debug(1, "[%d] id=%d, change param force I=%d",
1016 vb2_v4l2->vb2_buf.index,
1017 mtk_buf->enc_params.force_intra);
1018 if (mtk_buf->enc_params.force_intra)
1019 ret |= venc_if_set_param(ctx,
1020 VENC_SET_PARAM_FORCE_INTRA,
1024 mtk_buf->param_change = MTK_ENCODE_PARAM_NONE;
1027 ctx->state = MTK_STATE_ABORT;
1028 mtk_v4l2_err("venc_if_set_param %d failed=%d",
1029 mtk_buf->param_change, ret);
1037 * v4l2_m2m_streamoff() holds dev_mutex and waits mtk_venc_worker()
1038 * to call v4l2_m2m_job_finish().
1039 * If mtk_venc_worker() tries to acquire dev_mutex, it will deadlock.
1040 * So this function must not try to acquire dev->dev_mutex.
1041 * This means v4l2 ioctls and mtk_venc_worker() can run at the same time.
1042 * mtk_venc_worker() should be carefully implemented to avoid bugs.
1044 static void mtk_venc_worker(struct work_struct *work)
1046 struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
1048 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1049 struct venc_frm_buf frm_buf;
1050 struct mtk_vcodec_mem bs_buf;
1051 struct venc_done_result enc_result;
1054 /* check dst_buf, dst_buf may be removed in device_run
1055 * to stored encdoe header so we need check dst_buf and
1056 * call job_finish here to prevent recursion
1058 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1060 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1064 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1065 memset(&frm_buf, 0, sizeof(frm_buf));
1066 for (i = 0; i < src_buf->vb2_buf.num_planes ; i++) {
1067 frm_buf.fb_addr[i].dma_addr =
1068 vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, i);
1069 frm_buf.fb_addr[i].size =
1070 (size_t)src_buf->vb2_buf.planes[i].length;
1072 bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1073 bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1074 bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
1077 "Framebuf PA=%llx Size=0x%zx;PA=0x%llx Size=0x%zx;PA=0x%llx Size=%zu",
1078 (u64)frm_buf.fb_addr[0].dma_addr,
1079 frm_buf.fb_addr[0].size,
1080 (u64)frm_buf.fb_addr[1].dma_addr,
1081 frm_buf.fb_addr[1].size,
1082 (u64)frm_buf.fb_addr[2].dma_addr,
1083 frm_buf.fb_addr[2].size);
1085 ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME,
1086 &frm_buf, &bs_buf, &enc_result);
1088 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1089 dst_buf->timecode = src_buf->timecode;
1091 if (enc_result.is_key_frm)
1092 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1095 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1096 dst_buf->vb2_buf.planes[0].bytesused = 0;
1097 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1098 mtk_v4l2_err("venc_if_encode failed=%d", ret);
1100 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1101 dst_buf->vb2_buf.planes[0].bytesused = enc_result.bs_size;
1102 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1103 mtk_v4l2_debug(2, "venc_if_encode bs size=%d",
1104 enc_result.bs_size);
1107 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1109 mtk_v4l2_debug(1, "<=== src_buf[%d] dst_buf[%d] venc_if_encode ret=%d Size=%u===>",
1110 src_buf->vb2_buf.index, dst_buf->vb2_buf.index, ret,
1111 enc_result.bs_size);
1114 static void m2mops_venc_device_run(void *priv)
1116 struct mtk_vcodec_ctx *ctx = priv;
1118 if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
1119 (ctx->state != MTK_STATE_HEADER)) {
1120 /* encode h264 sps/pps header */
1121 mtk_venc_encode_header(ctx);
1122 queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1126 mtk_venc_param_change(ctx);
1127 queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1130 static int m2mops_venc_job_ready(void *m2m_priv)
1132 struct mtk_vcodec_ctx *ctx = m2m_priv;
1134 if (ctx->state == MTK_STATE_ABORT || ctx->state == MTK_STATE_FREE) {
1135 mtk_v4l2_debug(3, "[%d]Not ready: state=0x%x.",
1136 ctx->id, ctx->state);
1143 static void m2mops_venc_job_abort(void *priv)
1145 struct mtk_vcodec_ctx *ctx = priv;
1147 ctx->state = MTK_STATE_ABORT;
1150 const struct v4l2_m2m_ops mtk_venc_m2m_ops = {
1151 .device_run = m2mops_venc_device_run,
1152 .job_ready = m2mops_venc_job_ready,
1153 .job_abort = m2mops_venc_job_abort,
1156 void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_ctx *ctx)
1158 struct mtk_q_data *q_data;
1160 ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
1161 ctx->fh.m2m_ctx = ctx->m2m_ctx;
1162 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1163 INIT_WORK(&ctx->encode_work, mtk_venc_worker);
1165 ctx->colorspace = V4L2_COLORSPACE_REC709;
1166 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1167 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1168 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1170 q_data = &ctx->q_data[MTK_Q_DATA_SRC];
1171 memset(q_data, 0, sizeof(struct mtk_q_data));
1172 q_data->visible_width = DFT_CFG_WIDTH;
1173 q_data->visible_height = DFT_CFG_HEIGHT;
1174 q_data->coded_width = DFT_CFG_WIDTH;
1175 q_data->coded_height = DFT_CFG_HEIGHT;
1176 q_data->field = V4L2_FIELD_NONE;
1178 q_data->fmt = &ctx->dev->venc_pdata->output_formats[0];
1180 v4l_bound_align_image(&q_data->coded_width,
1183 &q_data->coded_height,
1185 MTK_VENC_MAX_H, 5, 6);
1187 if (q_data->coded_width < DFT_CFG_WIDTH &&
1188 (q_data->coded_width + 16) <= MTK_VENC_MAX_W)
1189 q_data->coded_width += 16;
1190 if (q_data->coded_height < DFT_CFG_HEIGHT &&
1191 (q_data->coded_height + 32) <= MTK_VENC_MAX_H)
1192 q_data->coded_height += 32;
1194 q_data->sizeimage[0] =
1195 q_data->coded_width * q_data->coded_height+
1196 ((ALIGN(q_data->coded_width, 16) * 2) * 16);
1197 q_data->bytesperline[0] = q_data->coded_width;
1198 q_data->sizeimage[1] =
1199 (q_data->coded_width * q_data->coded_height) / 2 +
1200 (ALIGN(q_data->coded_width, 16) * 16);
1201 q_data->bytesperline[1] = q_data->coded_width;
1203 q_data = &ctx->q_data[MTK_Q_DATA_DST];
1204 memset(q_data, 0, sizeof(struct mtk_q_data));
1205 q_data->coded_width = DFT_CFG_WIDTH;
1206 q_data->coded_height = DFT_CFG_HEIGHT;
1207 q_data->fmt = &ctx->dev->venc_pdata->capture_formats[0];
1208 q_data->field = V4L2_FIELD_NONE;
1209 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
1210 DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
1211 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
1213 ctx->enc_params.framerate_num = MTK_DEFAULT_FRAMERATE_NUM;
1214 ctx->enc_params.framerate_denom = MTK_DEFAULT_FRAMERATE_DENOM;
1217 int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx)
1219 const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops;
1220 struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
1222 v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT);
1224 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
1226 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE,
1227 ctx->dev->venc_pdata->min_bitrate,
1228 ctx->dev->venc_pdata->max_bitrate, 1, 4000000);
1229 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES,
1231 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
1233 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
1235 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
1237 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
1239 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
1241 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
1243 v4l2_ctrl_new_std_menu(handler, ops,
1244 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1245 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1246 0, V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE);
1247 v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1248 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
1249 0, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
1250 v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1251 V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
1252 0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
1253 if (handler->error) {
1254 mtk_v4l2_err("Init control handler fail %d",
1256 return handler->error;
1259 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1264 int mtk_vcodec_enc_queue_init(void *priv, struct vb2_queue *src_vq,
1265 struct vb2_queue *dst_vq)
1267 struct mtk_vcodec_ctx *ctx = priv;
1270 /* Note: VB2_USERPTR works with dma-contig because mt8173
1272 * https://patchwork.kernel.org/patch/8335461/
1273 * https://patchwork.kernel.org/patch/7596181/
1275 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1276 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1277 src_vq->drv_priv = ctx;
1278 src_vq->buf_struct_size = sizeof(struct mtk_video_enc_buf);
1279 src_vq->ops = &mtk_venc_vb2_ops;
1280 src_vq->mem_ops = &vb2_dma_contig_memops;
1281 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1282 src_vq->lock = &ctx->dev->dev_mutex;
1283 src_vq->dev = &ctx->dev->plat_dev->dev;
1285 ret = vb2_queue_init(src_vq);
1289 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1290 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1291 dst_vq->drv_priv = ctx;
1292 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1293 dst_vq->ops = &mtk_venc_vb2_ops;
1294 dst_vq->mem_ops = &vb2_dma_contig_memops;
1295 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1296 dst_vq->lock = &ctx->dev->dev_mutex;
1297 dst_vq->dev = &ctx->dev->plat_dev->dev;
1299 return vb2_queue_init(dst_vq);
1302 int mtk_venc_unlock(struct mtk_vcodec_ctx *ctx)
1304 struct mtk_vcodec_dev *dev = ctx->dev;
1306 mutex_unlock(&dev->enc_mutex);
1310 int mtk_venc_lock(struct mtk_vcodec_ctx *ctx)
1312 struct mtk_vcodec_dev *dev = ctx->dev;
1314 mutex_lock(&dev->enc_mutex);
1318 void mtk_vcodec_enc_release(struct mtk_vcodec_ctx *ctx)
1320 int ret = venc_if_deinit(ctx);
1323 mtk_v4l2_err("venc_if_deinit failed=%d", ret);
1325 ctx->state = MTK_STATE_FREE;