1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
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>
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"
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
30 static void mtk_venc_worker(struct work_struct *work);
32 static struct mtk_video_fmt mtk_video_formats[] = {
34 .fourcc = V4L2_PIX_FMT_NV12M,
35 .type = MTK_FMT_FRAME,
39 .fourcc = V4L2_PIX_FMT_NV21M,
40 .type = MTK_FMT_FRAME,
44 .fourcc = V4L2_PIX_FMT_YUV420M,
45 .type = MTK_FMT_FRAME,
49 .fourcc = V4L2_PIX_FMT_YVU420M,
50 .type = MTK_FMT_FRAME,
54 .fourcc = V4L2_PIX_FMT_H264,
59 .fourcc = V4L2_PIX_FMT_VP8,
65 #define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
67 static const struct mtk_codec_framesizes mtk_venc_framesizes[] = {
69 .fourcc = V4L2_PIX_FMT_H264,
70 .stepwise = { MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16,
71 MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16 },
74 .fourcc = V4L2_PIX_FMT_VP8,
75 .stepwise = { MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16,
76 MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16 },
80 #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_venc_framesizes)
82 static int vidioc_venc_s_ctrl(struct v4l2_ctrl *ctrl)
84 struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
85 struct mtk_enc_params *p = &ctx->enc_params;
89 case V4L2_CID_MPEG_VIDEO_BITRATE:
90 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_BITRATE val = %d",
92 p->bitrate = ctrl->val;
93 ctx->param_change |= MTK_ENCODE_PARAM_BITRATE;
95 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
96 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_B_FRAMES val = %d",
98 p->num_b_frame = ctrl->val;
100 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
101 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE val = %d",
103 p->rc_frame = ctrl->val;
105 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
106 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_MAX_QP val = %d",
108 p->h264_max_qp = ctrl->val;
110 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
111 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_HEADER_MODE val = %d",
113 p->seq_hdr_mode = ctrl->val;
115 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
116 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE val = %d",
118 p->rc_mb = ctrl->val;
120 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
121 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_PROFILE val = %d",
123 p->h264_profile = ctrl->val;
125 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
126 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_LEVEL val = %d",
128 p->h264_level = ctrl->val;
130 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
131 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_I_PERIOD val = %d",
133 p->intra_period = ctrl->val;
134 ctx->param_change |= MTK_ENCODE_PARAM_INTRA_PERIOD;
136 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
137 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_GOP_SIZE val = %d",
139 p->gop_size = ctrl->val;
140 ctx->param_change |= MTK_ENCODE_PARAM_GOP_SIZE;
142 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
143 mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME");
145 ctx->param_change |= MTK_ENCODE_PARAM_FORCE_INTRA;
155 static const struct v4l2_ctrl_ops mtk_vcodec_enc_ctrl_ops = {
156 .s_ctrl = vidioc_venc_s_ctrl,
159 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
161 struct mtk_video_fmt *fmt;
164 for (i = 0; i < NUM_FORMATS; ++i) {
165 if (output_queue && mtk_video_formats[i].type != MTK_FMT_FRAME)
167 if (!output_queue && mtk_video_formats[i].type != MTK_FMT_ENC)
171 fmt = &mtk_video_formats[i];
172 f->pixelformat = fmt->fourcc;
173 memset(f->reserved, 0, sizeof(f->reserved));
182 static int vidioc_enum_framesizes(struct file *file, void *fh,
183 struct v4l2_frmsizeenum *fsize)
187 if (fsize->index != 0)
190 for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
191 if (fsize->pixel_format != mtk_venc_framesizes[i].fourcc)
194 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
195 fsize->stepwise = mtk_venc_framesizes[i].stepwise;
202 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
203 struct v4l2_fmtdesc *f)
205 return vidioc_enum_fmt(f, false);
208 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
209 struct v4l2_fmtdesc *f)
211 return vidioc_enum_fmt(f, true);
214 static int vidioc_venc_querycap(struct file *file, void *priv,
215 struct v4l2_capability *cap)
217 strscpy(cap->driver, MTK_VCODEC_ENC_NAME, sizeof(cap->driver));
218 strscpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
219 strscpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
224 static int vidioc_venc_s_parm(struct file *file, void *priv,
225 struct v4l2_streamparm *a)
227 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
229 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
232 ctx->enc_params.framerate_num =
233 a->parm.output.timeperframe.denominator;
234 ctx->enc_params.framerate_denom =
235 a->parm.output.timeperframe.numerator;
236 ctx->param_change |= MTK_ENCODE_PARAM_FRAMERATE;
238 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
243 static int vidioc_venc_g_parm(struct file *file, void *priv,
244 struct v4l2_streamparm *a)
246 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
248 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
251 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
252 a->parm.output.timeperframe.denominator =
253 ctx->enc_params.framerate_num;
254 a->parm.output.timeperframe.numerator =
255 ctx->enc_params.framerate_denom;
260 static struct mtk_q_data *mtk_venc_get_q_data(struct mtk_vcodec_ctx *ctx,
261 enum v4l2_buf_type type)
263 if (V4L2_TYPE_IS_OUTPUT(type))
264 return &ctx->q_data[MTK_Q_DATA_SRC];
266 return &ctx->q_data[MTK_Q_DATA_DST];
269 static struct mtk_video_fmt *mtk_venc_find_format(struct v4l2_format *f)
271 struct mtk_video_fmt *fmt;
274 for (k = 0; k < NUM_FORMATS; k++) {
275 fmt = &mtk_video_formats[k];
276 if (fmt->fourcc == f->fmt.pix.pixelformat)
283 /* V4L2 specification suggests the driver corrects the format struct if any of
284 * the dimensions is unsupported
286 static int vidioc_try_fmt(struct v4l2_format *f, struct mtk_video_fmt *fmt)
288 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
291 pix_fmt_mp->field = V4L2_FIELD_NONE;
293 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
294 pix_fmt_mp->num_planes = 1;
295 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
296 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
299 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
302 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
306 /* find next closer width align 16, heign align 32, size align
309 tmp_w = pix_fmt_mp->width;
310 tmp_h = pix_fmt_mp->height;
311 v4l_bound_align_image(&pix_fmt_mp->width,
316 MTK_VENC_MAX_H, 5, 6);
318 if (pix_fmt_mp->width < tmp_w &&
319 (pix_fmt_mp->width + 16) <= MTK_VENC_MAX_W)
320 pix_fmt_mp->width += 16;
321 if (pix_fmt_mp->height < tmp_h &&
322 (pix_fmt_mp->height + 32) <= MTK_VENC_MAX_H)
323 pix_fmt_mp->height += 32;
326 "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d %d",
327 tmp_w, tmp_h, pix_fmt_mp->width,
329 pix_fmt_mp->plane_fmt[0].sizeimage,
330 pix_fmt_mp->plane_fmt[1].sizeimage);
332 pix_fmt_mp->num_planes = fmt->num_planes;
333 pix_fmt_mp->plane_fmt[0].sizeimage =
334 pix_fmt_mp->width * pix_fmt_mp->height +
335 ((ALIGN(pix_fmt_mp->width, 16) * 2) * 16);
336 pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
338 if (pix_fmt_mp->num_planes == 2) {
339 pix_fmt_mp->plane_fmt[1].sizeimage =
340 (pix_fmt_mp->width * pix_fmt_mp->height) / 2 +
341 (ALIGN(pix_fmt_mp->width, 16) * 16);
342 pix_fmt_mp->plane_fmt[2].sizeimage = 0;
343 pix_fmt_mp->plane_fmt[1].bytesperline =
345 pix_fmt_mp->plane_fmt[2].bytesperline = 0;
346 } else if (pix_fmt_mp->num_planes == 3) {
347 pix_fmt_mp->plane_fmt[1].sizeimage =
348 pix_fmt_mp->plane_fmt[2].sizeimage =
349 (pix_fmt_mp->width * pix_fmt_mp->height) / 4 +
350 ((ALIGN(pix_fmt_mp->width, 16) / 2) * 16);
351 pix_fmt_mp->plane_fmt[1].bytesperline =
352 pix_fmt_mp->plane_fmt[2].bytesperline =
353 pix_fmt_mp->width / 2;
357 for (i = 0; i < pix_fmt_mp->num_planes; i++)
358 memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
359 sizeof(pix_fmt_mp->plane_fmt[0].reserved));
361 pix_fmt_mp->flags = 0;
362 memset(&pix_fmt_mp->reserved, 0x0,
363 sizeof(pix_fmt_mp->reserved));
368 static void mtk_venc_set_param(struct mtk_vcodec_ctx *ctx,
369 struct venc_enc_param *param)
371 struct mtk_q_data *q_data_src = &ctx->q_data[MTK_Q_DATA_SRC];
372 struct mtk_enc_params *enc_params = &ctx->enc_params;
374 switch (q_data_src->fmt->fourcc) {
375 case V4L2_PIX_FMT_YUV420M:
376 param->input_yuv_fmt = VENC_YUV_FORMAT_I420;
378 case V4L2_PIX_FMT_YVU420M:
379 param->input_yuv_fmt = VENC_YUV_FORMAT_YV12;
381 case V4L2_PIX_FMT_NV12M:
382 param->input_yuv_fmt = VENC_YUV_FORMAT_NV12;
384 case V4L2_PIX_FMT_NV21M:
385 param->input_yuv_fmt = VENC_YUV_FORMAT_NV21;
388 mtk_v4l2_err("Unsupported fourcc =%d", q_data_src->fmt->fourcc);
391 param->h264_profile = enc_params->h264_profile;
392 param->h264_level = enc_params->h264_level;
394 /* Config visible resolution */
395 param->width = q_data_src->visible_width;
396 param->height = q_data_src->visible_height;
397 /* Config coded resolution */
398 param->buf_width = q_data_src->coded_width;
399 param->buf_height = q_data_src->coded_height;
400 param->frm_rate = enc_params->framerate_num /
401 enc_params->framerate_denom;
402 param->intra_period = enc_params->intra_period;
403 param->gop_size = enc_params->gop_size;
404 param->bitrate = enc_params->bitrate;
407 "fmt 0x%x, P/L %d/%d, w/h %d/%d, buf %d/%d, fps/bps %d/%d, gop %d, i_period %d",
408 param->input_yuv_fmt, param->h264_profile,
409 param->h264_level, param->width, param->height,
410 param->buf_width, param->buf_height,
411 param->frm_rate, param->bitrate,
412 param->gop_size, param->intra_period);
415 static int vidioc_venc_s_fmt_cap(struct file *file, void *priv,
416 struct v4l2_format *f)
418 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
419 struct vb2_queue *vq;
420 struct mtk_q_data *q_data;
422 struct mtk_video_fmt *fmt;
424 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
426 mtk_v4l2_err("fail to get vq");
430 if (vb2_is_busy(vq)) {
431 mtk_v4l2_err("queue busy");
435 q_data = mtk_venc_get_q_data(ctx, f->type);
437 mtk_v4l2_err("fail to get q data");
441 fmt = mtk_venc_find_format(f);
443 f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
444 fmt = mtk_venc_find_format(f);
448 ret = vidioc_try_fmt(f, q_data->fmt);
452 q_data->coded_width = f->fmt.pix_mp.width;
453 q_data->coded_height = f->fmt.pix_mp.height;
454 q_data->field = f->fmt.pix_mp.field;
456 for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
457 struct v4l2_plane_pix_format *plane_fmt;
459 plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
460 q_data->bytesperline[i] = plane_fmt->bytesperline;
461 q_data->sizeimage[i] = plane_fmt->sizeimage;
464 if (ctx->state == MTK_STATE_FREE) {
465 ret = venc_if_init(ctx, q_data->fmt->fourcc);
467 mtk_v4l2_err("venc_if_init failed=%d, codec type=%x",
468 ret, q_data->fmt->fourcc);
471 ctx->state = MTK_STATE_INIT;
477 static int vidioc_venc_s_fmt_out(struct file *file, void *priv,
478 struct v4l2_format *f)
480 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
481 struct vb2_queue *vq;
482 struct mtk_q_data *q_data;
484 struct mtk_video_fmt *fmt;
485 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
487 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
489 mtk_v4l2_err("fail to get vq");
493 if (vb2_is_busy(vq)) {
494 mtk_v4l2_err("queue busy");
498 q_data = mtk_venc_get_q_data(ctx, f->type);
500 mtk_v4l2_err("fail to get q data");
504 fmt = mtk_venc_find_format(f);
506 f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
507 fmt = mtk_venc_find_format(f);
510 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
513 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
517 q_data->visible_width = f->fmt.pix_mp.width;
518 q_data->visible_height = f->fmt.pix_mp.height;
520 ret = vidioc_try_fmt(f, q_data->fmt);
524 q_data->coded_width = f->fmt.pix_mp.width;
525 q_data->coded_height = f->fmt.pix_mp.height;
527 q_data->field = f->fmt.pix_mp.field;
528 ctx->colorspace = f->fmt.pix_mp.colorspace;
529 ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
530 ctx->quantization = f->fmt.pix_mp.quantization;
531 ctx->xfer_func = f->fmt.pix_mp.xfer_func;
533 for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
534 struct v4l2_plane_pix_format *plane_fmt;
536 plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
537 q_data->bytesperline[i] = plane_fmt->bytesperline;
538 q_data->sizeimage[i] = plane_fmt->sizeimage;
544 static int vidioc_venc_g_fmt(struct file *file, void *priv,
545 struct v4l2_format *f)
547 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
548 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
549 struct vb2_queue *vq;
550 struct mtk_q_data *q_data;
553 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
557 q_data = mtk_venc_get_q_data(ctx, f->type);
559 pix->width = q_data->coded_width;
560 pix->height = q_data->coded_height;
561 pix->pixelformat = q_data->fmt->fourcc;
562 pix->field = q_data->field;
563 pix->num_planes = q_data->fmt->num_planes;
564 for (i = 0; i < pix->num_planes; i++) {
565 pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
566 pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
567 memset(&(pix->plane_fmt[i].reserved[0]), 0x0,
568 sizeof(pix->plane_fmt[i].reserved));
572 pix->colorspace = ctx->colorspace;
573 pix->ycbcr_enc = ctx->ycbcr_enc;
574 pix->quantization = ctx->quantization;
575 pix->xfer_func = ctx->xfer_func;
580 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
581 struct v4l2_format *f)
583 struct mtk_video_fmt *fmt;
584 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
586 fmt = mtk_venc_find_format(f);
588 f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
589 fmt = mtk_venc_find_format(f);
591 f->fmt.pix_mp.colorspace = ctx->colorspace;
592 f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc;
593 f->fmt.pix_mp.quantization = ctx->quantization;
594 f->fmt.pix_mp.xfer_func = ctx->xfer_func;
596 return vidioc_try_fmt(f, fmt);
599 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
600 struct v4l2_format *f)
602 struct mtk_video_fmt *fmt;
604 fmt = mtk_venc_find_format(f);
606 f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
607 fmt = mtk_venc_find_format(f);
609 if (!f->fmt.pix_mp.colorspace) {
610 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
611 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
612 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
613 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
616 return vidioc_try_fmt(f, fmt);
619 static int vidioc_venc_g_selection(struct file *file, void *priv,
620 struct v4l2_selection *s)
622 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
623 struct mtk_q_data *q_data;
625 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
628 q_data = mtk_venc_get_q_data(ctx, s->type);
633 case V4L2_SEL_TGT_CROP_DEFAULT:
634 case V4L2_SEL_TGT_CROP_BOUNDS:
637 s->r.width = q_data->coded_width;
638 s->r.height = q_data->coded_height;
640 case V4L2_SEL_TGT_CROP:
643 s->r.width = q_data->visible_width;
644 s->r.height = q_data->visible_height;
653 static int vidioc_venc_s_selection(struct file *file, void *priv,
654 struct v4l2_selection *s)
656 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
657 struct mtk_q_data *q_data;
659 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
662 q_data = mtk_venc_get_q_data(ctx, s->type);
667 case V4L2_SEL_TGT_CROP:
668 /* Only support crop from (0,0) */
671 s->r.width = min(s->r.width, q_data->coded_width);
672 s->r.height = min(s->r.height, q_data->coded_height);
673 q_data->visible_width = s->r.width;
674 q_data->visible_height = s->r.height;
682 static int vidioc_venc_qbuf(struct file *file, void *priv,
683 struct v4l2_buffer *buf)
685 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
687 if (ctx->state == MTK_STATE_ABORT) {
688 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
693 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
696 static int vidioc_venc_dqbuf(struct file *file, void *priv,
697 struct v4l2_buffer *buf)
699 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
701 if (ctx->state == MTK_STATE_ABORT) {
702 mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
707 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
710 const struct v4l2_ioctl_ops mtk_venc_ioctl_ops = {
711 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
712 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
714 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
715 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
716 .vidioc_qbuf = vidioc_venc_qbuf,
717 .vidioc_dqbuf = vidioc_venc_dqbuf,
719 .vidioc_querycap = vidioc_venc_querycap,
720 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
721 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
722 .vidioc_enum_framesizes = vidioc_enum_framesizes,
724 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
725 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
726 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
727 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
728 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
730 .vidioc_s_parm = vidioc_venc_s_parm,
731 .vidioc_g_parm = vidioc_venc_g_parm,
732 .vidioc_s_fmt_vid_cap_mplane = vidioc_venc_s_fmt_cap,
733 .vidioc_s_fmt_vid_out_mplane = vidioc_venc_s_fmt_out,
735 .vidioc_g_fmt_vid_cap_mplane = vidioc_venc_g_fmt,
736 .vidioc_g_fmt_vid_out_mplane = vidioc_venc_g_fmt,
738 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
739 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
741 .vidioc_g_selection = vidioc_venc_g_selection,
742 .vidioc_s_selection = vidioc_venc_s_selection,
745 static int vb2ops_venc_queue_setup(struct vb2_queue *vq,
746 unsigned int *nbuffers,
747 unsigned int *nplanes,
748 unsigned int sizes[],
749 struct device *alloc_devs[])
751 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
752 struct mtk_q_data *q_data;
755 q_data = mtk_venc_get_q_data(ctx, vq->type);
761 for (i = 0; i < *nplanes; i++)
762 if (sizes[i] < q_data->sizeimage[i])
765 *nplanes = q_data->fmt->num_planes;
766 for (i = 0; i < *nplanes; i++)
767 sizes[i] = q_data->sizeimage[i];
773 static int vb2ops_venc_buf_prepare(struct vb2_buffer *vb)
775 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
776 struct mtk_q_data *q_data;
779 q_data = mtk_venc_get_q_data(ctx, vb->vb2_queue->type);
781 for (i = 0; i < q_data->fmt->num_planes; i++) {
782 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
783 mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
784 i, vb2_plane_size(vb, i),
785 q_data->sizeimage[i]);
793 static void vb2ops_venc_buf_queue(struct vb2_buffer *vb)
795 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
796 struct vb2_v4l2_buffer *vb2_v4l2 =
797 container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
799 struct mtk_video_enc_buf *mtk_buf =
800 container_of(vb2_v4l2, struct mtk_video_enc_buf, vb);
802 if ((vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
803 (ctx->param_change != MTK_ENCODE_PARAM_NONE)) {
804 mtk_v4l2_debug(1, "[%d] Before id=%d encode parameter change %x",
806 mtk_buf->vb.vb2_buf.index,
808 mtk_buf->param_change = ctx->param_change;
809 mtk_buf->enc_params = ctx->enc_params;
810 ctx->param_change = MTK_ENCODE_PARAM_NONE;
813 v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
816 static int vb2ops_venc_start_streaming(struct vb2_queue *q, unsigned int count)
818 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
819 struct venc_enc_param param;
823 /* Once state turn into MTK_STATE_ABORT, we need stop_streaming
826 if ((ctx->state == MTK_STATE_ABORT) || (ctx->state == MTK_STATE_FREE)) {
831 /* Do the initialization when both start_streaming have been called */
832 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
833 if (!vb2_start_streaming_called(&ctx->m2m_ctx->cap_q_ctx.q))
836 if (!vb2_start_streaming_called(&ctx->m2m_ctx->out_q_ctx.q))
840 mtk_venc_set_param(ctx, ¶m);
841 ret = venc_if_set_param(ctx, VENC_SET_PARAM_ENC, ¶m);
843 mtk_v4l2_err("venc_if_set_param failed=%d", ret);
844 ctx->state = MTK_STATE_ABORT;
847 ctx->param_change = MTK_ENCODE_PARAM_NONE;
849 if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
850 (ctx->enc_params.seq_hdr_mode !=
851 V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)) {
852 ret = venc_if_set_param(ctx,
853 VENC_SET_PARAM_PREPEND_HEADER,
856 mtk_v4l2_err("venc_if_set_param failed=%d", ret);
857 ctx->state = MTK_STATE_ABORT;
860 ctx->state = MTK_STATE_HEADER;
866 for (i = 0; i < q->num_buffers; ++i) {
867 if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
868 mtk_v4l2_debug(0, "[%d] id=%d, type=%d, %d -> VB2_BUF_STATE_QUEUED",
870 (int)q->bufs[i]->state);
871 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(q->bufs[i]),
872 VB2_BUF_STATE_QUEUED);
879 static void vb2ops_venc_stop_streaming(struct vb2_queue *q)
881 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
882 struct vb2_v4l2_buffer *src_buf, *dst_buf;
885 mtk_v4l2_debug(2, "[%d]-> type=%d", ctx->id, q->type);
887 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
888 while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
889 dst_buf->vb2_buf.planes[0].bytesused = 0;
890 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
893 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx)))
894 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
897 if ((q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
898 vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q)) ||
899 (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
900 vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q))) {
901 mtk_v4l2_debug(1, "[%d]-> q type %d out=%d cap=%d",
903 vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q),
904 vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q));
908 /* Release the encoder if both streams are stopped. */
909 ret = venc_if_deinit(ctx);
911 mtk_v4l2_err("venc_if_deinit failed=%d", ret);
913 ctx->state = MTK_STATE_FREE;
916 static const struct vb2_ops mtk_venc_vb2_ops = {
917 .queue_setup = vb2ops_venc_queue_setup,
918 .buf_prepare = vb2ops_venc_buf_prepare,
919 .buf_queue = vb2ops_venc_buf_queue,
920 .wait_prepare = vb2_ops_wait_prepare,
921 .wait_finish = vb2_ops_wait_finish,
922 .start_streaming = vb2ops_venc_start_streaming,
923 .stop_streaming = vb2ops_venc_stop_streaming,
926 static int mtk_venc_encode_header(void *priv)
928 struct mtk_vcodec_ctx *ctx = priv;
930 struct vb2_v4l2_buffer *src_buf, *dst_buf;
931 struct mtk_vcodec_mem bs_buf;
932 struct venc_done_result enc_result;
934 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
936 mtk_v4l2_debug(1, "No dst buffer");
940 bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
941 bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
942 bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
945 "[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu",
947 dst_buf->vb2_buf.index, bs_buf.va,
948 (u64)bs_buf.dma_addr,
951 ret = venc_if_encode(ctx,
952 VENC_START_OPT_ENCODE_SEQUENCE_HEADER,
953 NULL, &bs_buf, &enc_result);
956 dst_buf->vb2_buf.planes[0].bytesused = 0;
957 ctx->state = MTK_STATE_ABORT;
958 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
959 mtk_v4l2_err("venc_if_encode failed=%d", ret);
962 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
964 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
965 dst_buf->timecode = src_buf->timecode;
967 mtk_v4l2_err("No timestamp for the header buffer.");
970 ctx->state = MTK_STATE_HEADER;
971 dst_buf->vb2_buf.planes[0].bytesused = enc_result.bs_size;
972 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
977 static int mtk_venc_param_change(struct mtk_vcodec_ctx *ctx)
979 struct venc_enc_param enc_prm;
980 struct vb2_v4l2_buffer *vb2_v4l2 = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
981 struct mtk_video_enc_buf *mtk_buf =
982 container_of(vb2_v4l2, struct mtk_video_enc_buf, vb);
986 memset(&enc_prm, 0, sizeof(enc_prm));
987 if (mtk_buf->param_change == MTK_ENCODE_PARAM_NONE)
990 if (mtk_buf->param_change & MTK_ENCODE_PARAM_BITRATE) {
991 enc_prm.bitrate = mtk_buf->enc_params.bitrate;
992 mtk_v4l2_debug(1, "[%d] id=%d, change param br=%d",
994 mtk_buf->vb.vb2_buf.index,
996 ret |= venc_if_set_param(ctx,
997 VENC_SET_PARAM_ADJUST_BITRATE,
1000 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FRAMERATE) {
1001 enc_prm.frm_rate = mtk_buf->enc_params.framerate_num /
1002 mtk_buf->enc_params.framerate_denom;
1003 mtk_v4l2_debug(1, "[%d] id=%d, change param fr=%d",
1005 mtk_buf->vb.vb2_buf.index,
1007 ret |= venc_if_set_param(ctx,
1008 VENC_SET_PARAM_ADJUST_FRAMERATE,
1011 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_GOP_SIZE) {
1012 enc_prm.gop_size = mtk_buf->enc_params.gop_size;
1013 mtk_v4l2_debug(1, "change param intra period=%d",
1015 ret |= venc_if_set_param(ctx,
1016 VENC_SET_PARAM_GOP_SIZE,
1019 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) {
1020 mtk_v4l2_debug(1, "[%d] id=%d, change param force I=%d",
1022 mtk_buf->vb.vb2_buf.index,
1023 mtk_buf->enc_params.force_intra);
1024 if (mtk_buf->enc_params.force_intra)
1025 ret |= venc_if_set_param(ctx,
1026 VENC_SET_PARAM_FORCE_INTRA,
1030 mtk_buf->param_change = MTK_ENCODE_PARAM_NONE;
1033 ctx->state = MTK_STATE_ABORT;
1034 mtk_v4l2_err("venc_if_set_param %d failed=%d",
1035 mtk_buf->param_change, ret);
1043 * v4l2_m2m_streamoff() holds dev_mutex and waits mtk_venc_worker()
1044 * to call v4l2_m2m_job_finish().
1045 * If mtk_venc_worker() tries to acquire dev_mutex, it will deadlock.
1046 * So this function must not try to acquire dev->dev_mutex.
1047 * This means v4l2 ioctls and mtk_venc_worker() can run at the same time.
1048 * mtk_venc_worker() should be carefully implemented to avoid bugs.
1050 static void mtk_venc_worker(struct work_struct *work)
1052 struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
1054 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1055 struct venc_frm_buf frm_buf;
1056 struct mtk_vcodec_mem bs_buf;
1057 struct venc_done_result enc_result;
1060 /* check dst_buf, dst_buf may be removed in device_run
1061 * to stored encdoe header so we need check dst_buf and
1062 * call job_finish here to prevent recursion
1064 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1066 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1070 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1071 memset(&frm_buf, 0, sizeof(frm_buf));
1072 for (i = 0; i < src_buf->vb2_buf.num_planes ; i++) {
1073 frm_buf.fb_addr[i].dma_addr =
1074 vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, i);
1075 frm_buf.fb_addr[i].size =
1076 (size_t)src_buf->vb2_buf.planes[i].length;
1078 bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1079 bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1080 bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
1083 "Framebuf PA=%llx Size=0x%zx;PA=0x%llx Size=0x%zx;PA=0x%llx Size=%zu",
1084 (u64)frm_buf.fb_addr[0].dma_addr,
1085 frm_buf.fb_addr[0].size,
1086 (u64)frm_buf.fb_addr[1].dma_addr,
1087 frm_buf.fb_addr[1].size,
1088 (u64)frm_buf.fb_addr[2].dma_addr,
1089 frm_buf.fb_addr[2].size);
1091 ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME,
1092 &frm_buf, &bs_buf, &enc_result);
1094 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1095 dst_buf->timecode = src_buf->timecode;
1097 if (enc_result.is_key_frm)
1098 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1101 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1102 dst_buf->vb2_buf.planes[0].bytesused = 0;
1103 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1104 mtk_v4l2_err("venc_if_encode failed=%d", ret);
1106 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1107 dst_buf->vb2_buf.planes[0].bytesused = enc_result.bs_size;
1108 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1109 mtk_v4l2_debug(2, "venc_if_encode bs size=%d",
1110 enc_result.bs_size);
1113 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1115 mtk_v4l2_debug(1, "<=== src_buf[%d] dst_buf[%d] venc_if_encode ret=%d Size=%u===>",
1116 src_buf->vb2_buf.index, dst_buf->vb2_buf.index, ret,
1117 enc_result.bs_size);
1120 static void m2mops_venc_device_run(void *priv)
1122 struct mtk_vcodec_ctx *ctx = priv;
1124 if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
1125 (ctx->state != MTK_STATE_HEADER)) {
1126 /* encode h264 sps/pps header */
1127 mtk_venc_encode_header(ctx);
1128 queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1132 mtk_venc_param_change(ctx);
1133 queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1136 static int m2mops_venc_job_ready(void *m2m_priv)
1138 struct mtk_vcodec_ctx *ctx = m2m_priv;
1140 if (ctx->state == MTK_STATE_ABORT || ctx->state == MTK_STATE_FREE) {
1141 mtk_v4l2_debug(3, "[%d]Not ready: state=0x%x.",
1142 ctx->id, ctx->state);
1149 static void m2mops_venc_job_abort(void *priv)
1151 struct mtk_vcodec_ctx *ctx = priv;
1153 ctx->state = MTK_STATE_ABORT;
1156 const struct v4l2_m2m_ops mtk_venc_m2m_ops = {
1157 .device_run = m2mops_venc_device_run,
1158 .job_ready = m2mops_venc_job_ready,
1159 .job_abort = m2mops_venc_job_abort,
1162 void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_ctx *ctx)
1164 struct mtk_q_data *q_data;
1166 ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
1167 ctx->fh.m2m_ctx = ctx->m2m_ctx;
1168 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1169 INIT_WORK(&ctx->encode_work, mtk_venc_worker);
1171 ctx->colorspace = V4L2_COLORSPACE_REC709;
1172 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1173 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1174 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1176 q_data = &ctx->q_data[MTK_Q_DATA_SRC];
1177 memset(q_data, 0, sizeof(struct mtk_q_data));
1178 q_data->visible_width = DFT_CFG_WIDTH;
1179 q_data->visible_height = DFT_CFG_HEIGHT;
1180 q_data->coded_width = DFT_CFG_WIDTH;
1181 q_data->coded_height = DFT_CFG_HEIGHT;
1182 q_data->field = V4L2_FIELD_NONE;
1184 q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
1186 v4l_bound_align_image(&q_data->coded_width,
1189 &q_data->coded_height,
1191 MTK_VENC_MAX_H, 5, 6);
1193 if (q_data->coded_width < DFT_CFG_WIDTH &&
1194 (q_data->coded_width + 16) <= MTK_VENC_MAX_W)
1195 q_data->coded_width += 16;
1196 if (q_data->coded_height < DFT_CFG_HEIGHT &&
1197 (q_data->coded_height + 32) <= MTK_VENC_MAX_H)
1198 q_data->coded_height += 32;
1200 q_data->sizeimage[0] =
1201 q_data->coded_width * q_data->coded_height+
1202 ((ALIGN(q_data->coded_width, 16) * 2) * 16);
1203 q_data->bytesperline[0] = q_data->coded_width;
1204 q_data->sizeimage[1] =
1205 (q_data->coded_width * q_data->coded_height) / 2 +
1206 (ALIGN(q_data->coded_width, 16) * 16);
1207 q_data->bytesperline[1] = q_data->coded_width;
1209 q_data = &ctx->q_data[MTK_Q_DATA_DST];
1210 memset(q_data, 0, sizeof(struct mtk_q_data));
1211 q_data->coded_width = DFT_CFG_WIDTH;
1212 q_data->coded_height = DFT_CFG_HEIGHT;
1213 q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
1214 q_data->field = V4L2_FIELD_NONE;
1215 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
1216 DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
1217 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
1221 int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx)
1223 const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops;
1224 struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
1226 v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT);
1228 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE,
1229 1, 4000000, 1, 4000000);
1230 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES,
1232 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
1234 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
1236 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
1238 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
1240 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
1242 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
1244 v4l2_ctrl_new_std_menu(handler, ops,
1245 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1246 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1247 0, V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE);
1248 v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1249 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
1250 0, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
1251 v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1252 V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
1253 0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
1254 if (handler->error) {
1255 mtk_v4l2_err("Init control handler fail %d",
1257 return handler->error;
1260 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1265 int mtk_vcodec_enc_queue_init(void *priv, struct vb2_queue *src_vq,
1266 struct vb2_queue *dst_vq)
1268 struct mtk_vcodec_ctx *ctx = priv;
1271 /* Note: VB2_USERPTR works with dma-contig because mt8173
1273 * https://patchwork.kernel.org/patch/8335461/
1274 * https://patchwork.kernel.org/patch/7596181/
1276 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1277 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1278 src_vq->drv_priv = ctx;
1279 src_vq->buf_struct_size = sizeof(struct mtk_video_enc_buf);
1280 src_vq->ops = &mtk_venc_vb2_ops;
1281 src_vq->mem_ops = &vb2_dma_contig_memops;
1282 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1283 src_vq->lock = &ctx->dev->dev_mutex;
1284 src_vq->dev = &ctx->dev->plat_dev->dev;
1286 ret = vb2_queue_init(src_vq);
1290 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1291 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1292 dst_vq->drv_priv = ctx;
1293 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1294 dst_vq->ops = &mtk_venc_vb2_ops;
1295 dst_vq->mem_ops = &vb2_dma_contig_memops;
1296 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1297 dst_vq->lock = &ctx->dev->dev_mutex;
1298 dst_vq->dev = &ctx->dev->plat_dev->dev;
1300 return vb2_queue_init(dst_vq);
1303 int mtk_venc_unlock(struct mtk_vcodec_ctx *ctx)
1305 struct mtk_vcodec_dev *dev = ctx->dev;
1307 mutex_unlock(&dev->enc_mutex);
1311 int mtk_venc_lock(struct mtk_vcodec_ctx *ctx)
1313 struct mtk_vcodec_dev *dev = ctx->dev;
1315 mutex_lock(&dev->enc_mutex);
1319 void mtk_vcodec_enc_release(struct mtk_vcodec_ctx *ctx)
1321 int ret = venc_if_deinit(ctx);
1324 mtk_v4l2_err("venc_if_deinit failed=%d", ret);
1326 ctx->state = MTK_STATE_FREE;