1 // SPDX-License-Identifier: GPL-2.0
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 <linux/pm_runtime.h>
13 #include "mtk_vcodec_enc.h"
14 #include "venc_drv_if.h"
16 #define MTK_VENC_MIN_W 160U
17 #define MTK_VENC_MIN_H 128U
18 #define MTK_VENC_HD_MAX_W 1920U
19 #define MTK_VENC_HD_MAX_H 1088U
20 #define MTK_VENC_4K_MAX_W 3840U
21 #define MTK_VENC_4K_MAX_H 2176U
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
27 #define MTK_DEFAULT_FRAMERATE_NUM 1001
28 #define MTK_DEFAULT_FRAMERATE_DENOM 30000
29 #define MTK_VENC_4K_CAPABILITY_ENABLE BIT(0)
31 static void mtk_venc_worker(struct work_struct *work);
33 static const struct v4l2_frmsize_stepwise mtk_venc_hd_framesizes = {
34 MTK_VENC_MIN_W, MTK_VENC_HD_MAX_W, 16,
35 MTK_VENC_MIN_H, MTK_VENC_HD_MAX_H, 16,
38 static const struct v4l2_frmsize_stepwise mtk_venc_4k_framesizes = {
39 MTK_VENC_MIN_W, MTK_VENC_4K_MAX_W, 16,
40 MTK_VENC_MIN_H, MTK_VENC_4K_MAX_H, 16,
43 static int vidioc_venc_s_ctrl(struct v4l2_ctrl *ctrl)
45 struct mtk_vcodec_enc_ctx *ctx = ctrl_to_enc_ctx(ctrl);
46 struct mtk_enc_params *p = &ctx->enc_params;
50 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
51 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_BITRATE_MODE val= %d", ctrl->val);
52 if (ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) {
53 mtk_v4l2_venc_err(ctx, "Unsupported bitrate mode =%d", ctrl->val);
57 case V4L2_CID_MPEG_VIDEO_BITRATE:
58 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_BITRATE val = %d", ctrl->val);
59 p->bitrate = ctrl->val;
60 ctx->param_change |= MTK_ENCODE_PARAM_BITRATE;
62 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
63 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_B_FRAMES val = %d", ctrl->val);
64 p->num_b_frame = ctrl->val;
66 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
67 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE val = %d",
69 p->rc_frame = ctrl->val;
71 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
72 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_H264_MAX_QP val = %d", ctrl->val);
73 p->h264_max_qp = ctrl->val;
75 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
76 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_HEADER_MODE val = %d", ctrl->val);
77 p->seq_hdr_mode = ctrl->val;
79 case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
80 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE val = %d", ctrl->val);
83 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
84 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_H264_PROFILE val = %d", ctrl->val);
85 p->h264_profile = ctrl->val;
87 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
88 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_H264_LEVEL val = %d", ctrl->val);
89 p->h264_level = ctrl->val;
91 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
92 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_H264_I_PERIOD val = %d", ctrl->val);
93 p->intra_period = ctrl->val;
94 ctx->param_change |= MTK_ENCODE_PARAM_INTRA_PERIOD;
96 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
97 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_GOP_SIZE val = %d", ctrl->val);
98 p->gop_size = ctrl->val;
99 ctx->param_change |= MTK_ENCODE_PARAM_GOP_SIZE;
101 case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
103 * FIXME - what vp8 profiles are actually supported?
104 * The ctrl is added (with only profile 0 supported) for now.
106 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_VP8_PROFILE val = %d", ctrl->val);
108 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
109 mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME");
111 ctx->param_change |= MTK_ENCODE_PARAM_FORCE_INTRA;
121 static const struct v4l2_ctrl_ops mtk_vcodec_enc_ctrl_ops = {
122 .s_ctrl = vidioc_venc_s_ctrl,
125 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f,
126 const struct mtk_video_fmt *formats,
129 if (f->index >= num_formats)
132 f->pixelformat = formats[f->index].fourcc;
137 static const struct mtk_video_fmt *
138 mtk_venc_find_format(u32 fourcc, const struct mtk_vcodec_enc_pdata *pdata)
140 const struct mtk_video_fmt *fmt;
143 for (k = 0; k < pdata->num_capture_formats; k++) {
144 fmt = &pdata->capture_formats[k];
145 if (fmt->fourcc == fourcc)
149 for (k = 0; k < pdata->num_output_formats; k++) {
150 fmt = &pdata->output_formats[k];
151 if (fmt->fourcc == fourcc)
158 static int vidioc_enum_framesizes(struct file *file, void *fh,
159 struct v4l2_frmsizeenum *fsize)
161 const struct mtk_video_fmt *fmt;
162 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(fh);
164 if (fsize->index != 0)
167 fmt = mtk_venc_find_format(fsize->pixel_format,
168 ctx->dev->venc_pdata);
172 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
174 if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE)
175 fsize->stepwise = mtk_venc_4k_framesizes;
177 fsize->stepwise = mtk_venc_hd_framesizes;
182 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
183 struct v4l2_fmtdesc *f)
185 const struct mtk_vcodec_enc_pdata *pdata =
186 fh_to_enc_ctx(priv)->dev->venc_pdata;
188 return vidioc_enum_fmt(f, pdata->capture_formats,
189 pdata->num_capture_formats);
192 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
193 struct v4l2_fmtdesc *f)
195 const struct mtk_vcodec_enc_pdata *pdata =
196 fh_to_enc_ctx(priv)->dev->venc_pdata;
198 return vidioc_enum_fmt(f, pdata->output_formats,
199 pdata->num_output_formats);
202 static int mtk_vcodec_enc_get_chip_name(void *priv)
204 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
205 struct device *dev = &ctx->dev->plat_dev->dev;
207 if (of_device_is_compatible(dev->of_node, "mediatek,mt8173-vcodec-enc"))
209 else if (of_device_is_compatible(dev->of_node, "mediatek,mt8183-vcodec-enc"))
211 else if (of_device_is_compatible(dev->of_node, "mediatek,mt8192-vcodec-enc"))
213 else if (of_device_is_compatible(dev->of_node, "mediatek,mt8195-vcodec-enc"))
215 else if (of_device_is_compatible(dev->of_node, "mediatek,mt8188-vcodec-enc"))
221 static int vidioc_venc_querycap(struct file *file, void *priv,
222 struct v4l2_capability *cap)
224 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
225 struct device *dev = &ctx->dev->plat_dev->dev;
226 int platform_name = mtk_vcodec_enc_get_chip_name(priv);
228 strscpy(cap->driver, dev->driver->name, sizeof(cap->driver));
229 snprintf(cap->card, sizeof(cap->card), "MT%d video encoder", platform_name);
234 static int vidioc_venc_s_parm(struct file *file, void *priv,
235 struct v4l2_streamparm *a)
237 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
238 struct v4l2_fract *timeperframe = &a->parm.output.timeperframe;
240 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
243 if (timeperframe->numerator == 0 || timeperframe->denominator == 0) {
244 timeperframe->numerator = MTK_DEFAULT_FRAMERATE_NUM;
245 timeperframe->denominator = MTK_DEFAULT_FRAMERATE_DENOM;
248 ctx->enc_params.framerate_num = timeperframe->denominator;
249 ctx->enc_params.framerate_denom = timeperframe->numerator;
250 ctx->param_change |= MTK_ENCODE_PARAM_FRAMERATE;
252 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
257 static int vidioc_venc_g_parm(struct file *file, void *priv,
258 struct v4l2_streamparm *a)
260 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
262 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
265 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
266 a->parm.output.timeperframe.denominator =
267 ctx->enc_params.framerate_num;
268 a->parm.output.timeperframe.numerator =
269 ctx->enc_params.framerate_denom;
274 static struct mtk_q_data *mtk_venc_get_q_data(struct mtk_vcodec_enc_ctx *ctx,
275 enum v4l2_buf_type type)
277 if (V4L2_TYPE_IS_OUTPUT(type))
278 return &ctx->q_data[MTK_Q_DATA_SRC];
280 return &ctx->q_data[MTK_Q_DATA_DST];
283 static void vidioc_try_fmt_cap(struct v4l2_format *f)
285 f->fmt.pix_mp.field = V4L2_FIELD_NONE;
286 f->fmt.pix_mp.num_planes = 1;
287 f->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
288 f->fmt.pix_mp.flags = 0;
291 /* V4L2 specification suggests the driver corrects the format struct if any of
292 * the dimensions is unsupported
294 static int vidioc_try_fmt_out(struct mtk_vcodec_enc_ctx *ctx, struct v4l2_format *f,
295 const struct mtk_video_fmt *fmt)
297 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
299 unsigned int max_width, max_height;
301 pix_fmt_mp->field = V4L2_FIELD_NONE;
303 if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE) {
304 max_width = MTK_VENC_4K_MAX_W;
305 max_height = MTK_VENC_4K_MAX_H;
307 max_width = MTK_VENC_HD_MAX_W;
308 max_height = MTK_VENC_HD_MAX_H;
311 pix_fmt_mp->height = clamp(pix_fmt_mp->height, MTK_VENC_MIN_H, max_height);
312 pix_fmt_mp->width = clamp(pix_fmt_mp->width, MTK_VENC_MIN_W, max_width);
314 /* find next closer width align 16, heign align 32, size align
317 tmp_w = pix_fmt_mp->width;
318 tmp_h = pix_fmt_mp->height;
319 v4l_bound_align_image(&pix_fmt_mp->width,
326 if (pix_fmt_mp->width < tmp_w && (pix_fmt_mp->width + 16) <= max_width)
327 pix_fmt_mp->width += 16;
328 if (pix_fmt_mp->height < tmp_h && (pix_fmt_mp->height + 32) <= max_height)
329 pix_fmt_mp->height += 32;
331 mtk_v4l2_venc_dbg(0, ctx,
332 "before resize wxh=%dx%d, after resize wxh=%dx%d, sizeimage=%d %d",
333 tmp_w, tmp_h, pix_fmt_mp->width,
335 pix_fmt_mp->plane_fmt[0].sizeimage,
336 pix_fmt_mp->plane_fmt[1].sizeimage);
338 pix_fmt_mp->num_planes = fmt->num_planes;
339 pix_fmt_mp->plane_fmt[0].sizeimage =
340 pix_fmt_mp->width * pix_fmt_mp->height +
341 ((ALIGN(pix_fmt_mp->width, 16) * 2) * 16);
342 pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
344 if (pix_fmt_mp->num_planes == 2) {
345 pix_fmt_mp->plane_fmt[1].sizeimage =
346 (pix_fmt_mp->width * pix_fmt_mp->height) / 2 +
347 (ALIGN(pix_fmt_mp->width, 16) * 16);
348 pix_fmt_mp->plane_fmt[2].sizeimage = 0;
349 pix_fmt_mp->plane_fmt[1].bytesperline =
351 pix_fmt_mp->plane_fmt[2].bytesperline = 0;
352 } else if (pix_fmt_mp->num_planes == 3) {
353 pix_fmt_mp->plane_fmt[1].sizeimage =
354 pix_fmt_mp->plane_fmt[2].sizeimage =
355 (pix_fmt_mp->width * pix_fmt_mp->height) / 4 +
356 ((ALIGN(pix_fmt_mp->width, 16) / 2) * 16);
357 pix_fmt_mp->plane_fmt[1].bytesperline =
358 pix_fmt_mp->plane_fmt[2].bytesperline =
359 pix_fmt_mp->width / 2;
362 pix_fmt_mp->flags = 0;
367 static void mtk_venc_set_param(struct mtk_vcodec_enc_ctx *ctx,
368 struct venc_enc_param *param)
370 struct mtk_q_data *q_data_src = &ctx->q_data[MTK_Q_DATA_SRC];
371 struct mtk_enc_params *enc_params = &ctx->enc_params;
373 switch (q_data_src->fmt->fourcc) {
374 case V4L2_PIX_FMT_YUV420M:
375 param->input_yuv_fmt = VENC_YUV_FORMAT_I420;
377 case V4L2_PIX_FMT_YVU420M:
378 param->input_yuv_fmt = VENC_YUV_FORMAT_YV12;
380 case V4L2_PIX_FMT_NV12M:
381 param->input_yuv_fmt = VENC_YUV_FORMAT_NV12;
383 case V4L2_PIX_FMT_NV21M:
384 param->input_yuv_fmt = VENC_YUV_FORMAT_NV21;
387 mtk_v4l2_venc_err(ctx, "Unsupported fourcc =%d", q_data_src->fmt->fourcc);
390 param->h264_profile = enc_params->h264_profile;
391 param->h264_level = enc_params->h264_level;
393 /* Config visible resolution */
394 param->width = q_data_src->visible_width;
395 param->height = q_data_src->visible_height;
396 /* Config coded resolution */
397 param->buf_width = q_data_src->coded_width;
398 param->buf_height = q_data_src->coded_height;
399 param->frm_rate = enc_params->framerate_num /
400 enc_params->framerate_denom;
401 param->intra_period = enc_params->intra_period;
402 param->gop_size = enc_params->gop_size;
403 param->bitrate = enc_params->bitrate;
405 mtk_v4l2_venc_dbg(0, ctx,
406 "fmt 0x%x, P/L %d/%d w/h %d/%d buf %d/%d fps/bps %d/%d gop %d i_per %d",
407 param->input_yuv_fmt, param->h264_profile,
408 param->h264_level, param->width, param->height,
409 param->buf_width, param->buf_height,
410 param->frm_rate, param->bitrate,
411 param->gop_size, param->intra_period);
414 static int vidioc_venc_s_fmt_cap(struct file *file, void *priv,
415 struct v4l2_format *f)
417 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
418 const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
419 struct vb2_queue *vq;
420 struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, f->type);
422 const struct mtk_video_fmt *fmt;
424 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
426 mtk_v4l2_venc_err(ctx, "fail to get vq");
430 if (vb2_is_busy(vq)) {
431 mtk_v4l2_venc_err(ctx, "queue busy");
435 fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
437 fmt = &ctx->dev->venc_pdata->capture_formats[0];
438 f->fmt.pix.pixelformat = fmt->fourcc;
442 vidioc_try_fmt_cap(f);
444 q_data->coded_width = f->fmt.pix_mp.width;
445 q_data->coded_height = f->fmt.pix_mp.height;
446 q_data->field = f->fmt.pix_mp.field;
448 for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
449 struct v4l2_plane_pix_format *plane_fmt;
451 plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
452 q_data->bytesperline[i] = plane_fmt->bytesperline;
453 q_data->sizeimage[i] = plane_fmt->sizeimage;
456 if (ctx->state == MTK_STATE_FREE) {
457 ret = venc_if_init(ctx, q_data->fmt->fourcc);
459 mtk_v4l2_venc_err(ctx, "venc_if_init failed=%d, codec type=%x",
460 ret, q_data->fmt->fourcc);
463 ctx->state = MTK_STATE_INIT;
469 static int vidioc_venc_s_fmt_out(struct file *file, void *priv,
470 struct v4l2_format *f)
472 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
473 const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
474 struct vb2_queue *vq;
475 struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, f->type);
477 const struct mtk_video_fmt *fmt;
479 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
481 mtk_v4l2_venc_err(ctx, "fail to get vq");
485 if (vb2_is_busy(vq)) {
486 mtk_v4l2_venc_err(ctx, "queue busy");
490 fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
492 fmt = &ctx->dev->venc_pdata->output_formats[0];
493 f->fmt.pix.pixelformat = fmt->fourcc;
496 ret = vidioc_try_fmt_out(ctx, f, fmt);
501 q_data->visible_width = f->fmt.pix_mp.width;
502 q_data->visible_height = f->fmt.pix_mp.height;
503 q_data->coded_width = f->fmt.pix_mp.width;
504 q_data->coded_height = f->fmt.pix_mp.height;
506 q_data->field = f->fmt.pix_mp.field;
507 ctx->colorspace = f->fmt.pix_mp.colorspace;
508 ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
509 ctx->quantization = f->fmt.pix_mp.quantization;
510 ctx->xfer_func = f->fmt.pix_mp.xfer_func;
512 for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
513 struct v4l2_plane_pix_format *plane_fmt;
515 plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
516 q_data->bytesperline[i] = plane_fmt->bytesperline;
517 q_data->sizeimage[i] = plane_fmt->sizeimage;
523 static int vidioc_venc_g_fmt(struct file *file, void *priv,
524 struct v4l2_format *f)
526 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
527 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
528 struct vb2_queue *vq;
529 struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, f->type);
532 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
537 pix->width = q_data->coded_width;
538 pix->height = q_data->coded_height;
539 pix->pixelformat = q_data->fmt->fourcc;
540 pix->field = q_data->field;
541 pix->num_planes = q_data->fmt->num_planes;
542 for (i = 0; i < pix->num_planes; i++) {
543 pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
544 pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
548 pix->colorspace = ctx->colorspace;
549 pix->ycbcr_enc = ctx->ycbcr_enc;
550 pix->quantization = ctx->quantization;
551 pix->xfer_func = ctx->xfer_func;
556 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
557 struct v4l2_format *f)
559 const struct mtk_video_fmt *fmt;
560 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
561 const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
563 fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
565 fmt = &ctx->dev->venc_pdata->capture_formats[0];
566 f->fmt.pix.pixelformat = fmt->fourcc;
568 f->fmt.pix_mp.colorspace = ctx->colorspace;
569 f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc;
570 f->fmt.pix_mp.quantization = ctx->quantization;
571 f->fmt.pix_mp.xfer_func = ctx->xfer_func;
573 vidioc_try_fmt_cap(f);
578 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
579 struct v4l2_format *f)
581 const struct mtk_video_fmt *fmt;
582 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
583 const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
585 fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
587 fmt = &ctx->dev->venc_pdata->output_formats[0];
588 f->fmt.pix.pixelformat = fmt->fourcc;
590 if (!f->fmt.pix_mp.colorspace) {
591 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
592 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
593 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
594 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
597 return vidioc_try_fmt_out(ctx, f, fmt);
600 static int vidioc_venc_g_selection(struct file *file, void *priv,
601 struct v4l2_selection *s)
603 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
604 struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, s->type);
606 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
610 case V4L2_SEL_TGT_CROP_DEFAULT:
611 case V4L2_SEL_TGT_CROP_BOUNDS:
614 s->r.width = q_data->coded_width;
615 s->r.height = q_data->coded_height;
617 case V4L2_SEL_TGT_CROP:
620 s->r.width = q_data->visible_width;
621 s->r.height = q_data->visible_height;
630 static int vidioc_venc_s_selection(struct file *file, void *priv,
631 struct v4l2_selection *s)
633 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
634 struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, s->type);
636 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
640 case V4L2_SEL_TGT_CROP:
641 /* Only support crop from (0,0) */
644 s->r.width = min(s->r.width, q_data->coded_width);
645 s->r.height = min(s->r.height, q_data->coded_height);
646 q_data->visible_width = s->r.width;
647 q_data->visible_height = s->r.height;
655 static int vidioc_venc_qbuf(struct file *file, void *priv,
656 struct v4l2_buffer *buf)
658 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
660 if (ctx->state == MTK_STATE_ABORT) {
661 mtk_v4l2_venc_err(ctx, "[%d] Call on QBUF after unrecoverable error",
666 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
669 static int vidioc_venc_dqbuf(struct file *file, void *priv,
670 struct v4l2_buffer *buf)
672 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
675 if (ctx->state == MTK_STATE_ABORT) {
676 mtk_v4l2_venc_err(ctx, "[%d] Call on QBUF after unrecoverable error",
681 ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
686 * Complete flush if the user dequeued the 0-payload LAST buffer.
687 * We check the payload because a buffer with the LAST flag can also
688 * be seen during resolution changes. If we happen to be flushing at
689 * that time, the last buffer before the resolution changes could be
690 * misinterpreted for the buffer generated by the flush and terminate
691 * it earlier than we want.
693 if (!V4L2_TYPE_IS_OUTPUT(buf->type) &&
694 buf->flags & V4L2_BUF_FLAG_LAST &&
695 buf->m.planes[0].bytesused == 0 &&
698 * Last CAPTURE buffer is dequeued, we can allow another flush
701 ctx->is_flushing = false;
707 static int vidioc_encoder_cmd(struct file *file, void *priv,
708 struct v4l2_encoder_cmd *cmd)
710 struct mtk_vcodec_enc_ctx *ctx = fh_to_enc_ctx(priv);
711 struct vb2_queue *src_vq, *dst_vq;
714 if (ctx->state == MTK_STATE_ABORT) {
715 mtk_v4l2_venc_err(ctx, "[%d] Call to CMD after unrecoverable error",
720 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, cmd);
724 /* Calling START or STOP is invalid if a flush is in progress */
725 if (ctx->is_flushing)
728 mtk_v4l2_venc_dbg(1, ctx, "encoder cmd=%u", cmd->cmd);
730 dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
731 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
733 case V4L2_ENC_CMD_STOP:
734 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
735 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
736 if (!vb2_is_streaming(src_vq)) {
737 mtk_v4l2_venc_dbg(1, ctx, "Output stream is off. No need to flush.");
740 if (!vb2_is_streaming(dst_vq)) {
741 mtk_v4l2_venc_dbg(1, ctx, "Capture stream is off. No need to flush.");
744 ctx->is_flushing = true;
745 v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf.vb);
746 v4l2_m2m_try_schedule(ctx->m2m_ctx);
749 case V4L2_ENC_CMD_START:
750 vb2_clear_last_buffer_dequeued(dst_vq);
760 const struct v4l2_ioctl_ops mtk_venc_ioctl_ops = {
761 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
762 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
764 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
765 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
766 .vidioc_qbuf = vidioc_venc_qbuf,
767 .vidioc_dqbuf = vidioc_venc_dqbuf,
769 .vidioc_querycap = vidioc_venc_querycap,
770 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
771 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
772 .vidioc_enum_framesizes = vidioc_enum_framesizes,
774 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
775 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
776 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
777 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
778 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
780 .vidioc_s_parm = vidioc_venc_s_parm,
781 .vidioc_g_parm = vidioc_venc_g_parm,
782 .vidioc_s_fmt_vid_cap_mplane = vidioc_venc_s_fmt_cap,
783 .vidioc_s_fmt_vid_out_mplane = vidioc_venc_s_fmt_out,
785 .vidioc_g_fmt_vid_cap_mplane = vidioc_venc_g_fmt,
786 .vidioc_g_fmt_vid_out_mplane = vidioc_venc_g_fmt,
788 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
789 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
791 .vidioc_g_selection = vidioc_venc_g_selection,
792 .vidioc_s_selection = vidioc_venc_s_selection,
794 .vidioc_encoder_cmd = vidioc_encoder_cmd,
795 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
798 static int vb2ops_venc_queue_setup(struct vb2_queue *vq,
799 unsigned int *nbuffers,
800 unsigned int *nplanes,
801 unsigned int sizes[],
802 struct device *alloc_devs[])
804 struct mtk_vcodec_enc_ctx *ctx = vb2_get_drv_priv(vq);
805 struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, vq->type);
812 if (*nplanes != q_data->fmt->num_planes)
814 for (i = 0; i < *nplanes; i++)
815 if (sizes[i] < q_data->sizeimage[i])
818 *nplanes = q_data->fmt->num_planes;
819 for (i = 0; i < *nplanes; i++)
820 sizes[i] = q_data->sizeimage[i];
826 static int vb2ops_venc_buf_prepare(struct vb2_buffer *vb)
828 struct mtk_vcodec_enc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
829 struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, vb->vb2_queue->type);
832 for (i = 0; i < q_data->fmt->num_planes; i++) {
833 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
834 mtk_v4l2_venc_err(ctx, "data will not fit into plane %d (%lu < %d)",
835 i, vb2_plane_size(vb, i), q_data->sizeimage[i]);
843 static void vb2ops_venc_buf_queue(struct vb2_buffer *vb)
845 struct mtk_vcodec_enc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
846 struct vb2_v4l2_buffer *vb2_v4l2 =
847 container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
849 struct mtk_video_enc_buf *mtk_buf =
850 container_of(vb2_v4l2, struct mtk_video_enc_buf,
853 if ((vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
854 (ctx->param_change != MTK_ENCODE_PARAM_NONE)) {
855 mtk_v4l2_venc_dbg(1, ctx, "[%d] Before id=%d encode parameter change %x",
856 ctx->id, vb2_v4l2->vb2_buf.index, ctx->param_change);
857 mtk_buf->param_change = ctx->param_change;
858 mtk_buf->enc_params = ctx->enc_params;
859 ctx->param_change = MTK_ENCODE_PARAM_NONE;
862 v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
865 static int vb2ops_venc_start_streaming(struct vb2_queue *q, unsigned int count)
867 struct mtk_vcodec_enc_ctx *ctx = vb2_get_drv_priv(q);
868 struct venc_enc_param param;
872 /* Once state turn into MTK_STATE_ABORT, we need stop_streaming
875 if ((ctx->state == MTK_STATE_ABORT) || (ctx->state == MTK_STATE_FREE)) {
877 goto err_start_stream;
880 /* Do the initialization when both start_streaming have been called */
881 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
882 if (!vb2_start_streaming_called(&ctx->m2m_ctx->cap_q_ctx.q))
885 if (!vb2_start_streaming_called(&ctx->m2m_ctx->out_q_ctx.q))
889 mtk_venc_set_param(ctx, ¶m);
890 ret = venc_if_set_param(ctx, VENC_SET_PARAM_ENC, ¶m);
892 mtk_v4l2_venc_err(ctx, "venc_if_set_param failed=%d", ret);
893 ctx->state = MTK_STATE_ABORT;
894 goto err_start_stream;
896 ctx->param_change = MTK_ENCODE_PARAM_NONE;
898 if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
899 (ctx->enc_params.seq_hdr_mode !=
900 V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)) {
901 ret = venc_if_set_param(ctx,
902 VENC_SET_PARAM_PREPEND_HEADER,
905 mtk_v4l2_venc_err(ctx, "venc_if_set_param failed=%d", ret);
906 ctx->state = MTK_STATE_ABORT;
907 goto err_start_stream;
909 ctx->state = MTK_STATE_HEADER;
915 for (i = 0; i < q->num_buffers; ++i) {
916 struct vb2_buffer *buf = vb2_get_buffer(q, i);
919 * FIXME: This check is not needed as only active buffers
920 * can be marked as done.
922 if (buf && buf->state == VB2_BUF_STATE_ACTIVE) {
923 mtk_v4l2_venc_dbg(0, ctx, "[%d] id=%d, type=%d, %d->VB2_BUF_STATE_QUEUED",
924 ctx->id, i, q->type, (int)buf->state);
925 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(buf),
926 VB2_BUF_STATE_QUEUED);
933 static void vb2ops_venc_stop_streaming(struct vb2_queue *q)
935 struct mtk_vcodec_enc_ctx *ctx = vb2_get_drv_priv(q);
936 struct vb2_v4l2_buffer *src_buf, *dst_buf;
939 mtk_v4l2_venc_dbg(2, ctx, "[%d]-> type=%d", ctx->id, q->type);
941 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
942 while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
943 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
944 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
946 /* STREAMOFF on the CAPTURE queue completes any ongoing flush */
947 if (ctx->is_flushing) {
948 struct v4l2_m2m_buffer *b, *n;
950 mtk_v4l2_venc_dbg(1, ctx, "STREAMOFF called while flushing");
952 * STREAMOFF could be called before the flush buffer is
953 * dequeued. Check whether empty flush buf is still in
954 * queue before removing it.
956 v4l2_m2m_for_each_src_buf_safe(ctx->m2m_ctx, b, n) {
957 if (b == &ctx->empty_flush_buf) {
958 v4l2_m2m_src_buf_remove_by_buf(ctx->m2m_ctx, &b->vb);
962 ctx->is_flushing = false;
965 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
966 if (src_buf != &ctx->empty_flush_buf.vb)
967 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
969 if (ctx->is_flushing) {
971 * If we are in the middle of a flush, put the flush
972 * buffer back into the queue so the next CAPTURE
973 * buffer gets returned with the LAST flag set.
975 v4l2_m2m_buf_queue(ctx->m2m_ctx,
976 &ctx->empty_flush_buf.vb);
980 if ((q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
981 vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q)) ||
982 (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
983 vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q))) {
984 mtk_v4l2_venc_dbg(1, ctx, "[%d]-> q type %d out=%d cap=%d",
986 vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q),
987 vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q));
991 /* Release the encoder if both streams are stopped. */
992 ret = venc_if_deinit(ctx);
994 mtk_v4l2_venc_err(ctx, "venc_if_deinit failed=%d", ret);
996 ctx->state = MTK_STATE_FREE;
999 static int vb2ops_venc_buf_out_validate(struct vb2_buffer *vb)
1001 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1003 vbuf->field = V4L2_FIELD_NONE;
1007 static const struct vb2_ops mtk_venc_vb2_ops = {
1008 .queue_setup = vb2ops_venc_queue_setup,
1009 .buf_out_validate = vb2ops_venc_buf_out_validate,
1010 .buf_prepare = vb2ops_venc_buf_prepare,
1011 .buf_queue = vb2ops_venc_buf_queue,
1012 .wait_prepare = vb2_ops_wait_prepare,
1013 .wait_finish = vb2_ops_wait_finish,
1014 .start_streaming = vb2ops_venc_start_streaming,
1015 .stop_streaming = vb2ops_venc_stop_streaming,
1018 static int mtk_venc_encode_header(void *priv)
1020 struct mtk_vcodec_enc_ctx *ctx = priv;
1022 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1023 struct mtk_vcodec_mem bs_buf;
1024 struct venc_done_result enc_result;
1026 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1028 mtk_v4l2_venc_dbg(1, ctx, "No dst buffer");
1032 bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1033 bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1034 bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
1036 mtk_v4l2_venc_dbg(1, ctx,
1037 "[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu",
1038 ctx->id, dst_buf->vb2_buf.index, bs_buf.va,
1039 (u64)bs_buf.dma_addr, bs_buf.size);
1041 ret = venc_if_encode(ctx,
1042 VENC_START_OPT_ENCODE_SEQUENCE_HEADER,
1043 NULL, &bs_buf, &enc_result);
1046 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1047 ctx->state = MTK_STATE_ABORT;
1048 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1049 mtk_v4l2_venc_err(ctx, "venc_if_encode failed=%d", ret);
1052 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1054 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1055 dst_buf->timecode = src_buf->timecode;
1057 mtk_v4l2_venc_err(ctx, "No timestamp for the header buffer.");
1060 ctx->state = MTK_STATE_HEADER;
1061 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, enc_result.bs_size);
1062 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1067 static int mtk_venc_param_change(struct mtk_vcodec_enc_ctx *ctx)
1069 struct venc_enc_param enc_prm;
1070 struct vb2_v4l2_buffer *vb2_v4l2 = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1071 struct mtk_video_enc_buf *mtk_buf;
1074 /* Don't upcast the empty flush buffer */
1075 if (vb2_v4l2 == &ctx->empty_flush_buf.vb)
1078 mtk_buf = container_of(vb2_v4l2, struct mtk_video_enc_buf, m2m_buf.vb);
1080 memset(&enc_prm, 0, sizeof(enc_prm));
1081 if (mtk_buf->param_change == MTK_ENCODE_PARAM_NONE)
1084 if (mtk_buf->param_change & MTK_ENCODE_PARAM_BITRATE) {
1085 enc_prm.bitrate = mtk_buf->enc_params.bitrate;
1086 mtk_v4l2_venc_dbg(1, ctx, "[%d] id=%d, change param br=%d",
1087 ctx->id, vb2_v4l2->vb2_buf.index, enc_prm.bitrate);
1088 ret |= venc_if_set_param(ctx,
1089 VENC_SET_PARAM_ADJUST_BITRATE,
1092 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FRAMERATE) {
1093 enc_prm.frm_rate = mtk_buf->enc_params.framerate_num /
1094 mtk_buf->enc_params.framerate_denom;
1095 mtk_v4l2_venc_dbg(1, ctx, "[%d] id=%d, change param fr=%d",
1096 ctx->id, vb2_v4l2->vb2_buf.index, enc_prm.frm_rate);
1097 ret |= venc_if_set_param(ctx,
1098 VENC_SET_PARAM_ADJUST_FRAMERATE,
1101 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_GOP_SIZE) {
1102 enc_prm.gop_size = mtk_buf->enc_params.gop_size;
1103 mtk_v4l2_venc_dbg(1, ctx, "change param intra period=%d", enc_prm.gop_size);
1104 ret |= venc_if_set_param(ctx,
1105 VENC_SET_PARAM_GOP_SIZE,
1108 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) {
1109 mtk_v4l2_venc_dbg(1, ctx, "[%d] id=%d, change param force I=%d",
1110 ctx->id, vb2_v4l2->vb2_buf.index,
1111 mtk_buf->enc_params.force_intra);
1112 if (mtk_buf->enc_params.force_intra)
1113 ret |= venc_if_set_param(ctx,
1114 VENC_SET_PARAM_FORCE_INTRA,
1118 mtk_buf->param_change = MTK_ENCODE_PARAM_NONE;
1121 ctx->state = MTK_STATE_ABORT;
1122 mtk_v4l2_venc_err(ctx, "venc_if_set_param %d failed=%d",
1123 mtk_buf->param_change, ret);
1131 * v4l2_m2m_streamoff() holds dev_mutex and waits mtk_venc_worker()
1132 * to call v4l2_m2m_job_finish().
1133 * If mtk_venc_worker() tries to acquire dev_mutex, it will deadlock.
1134 * So this function must not try to acquire dev->dev_mutex.
1135 * This means v4l2 ioctls and mtk_venc_worker() can run at the same time.
1136 * mtk_venc_worker() should be carefully implemented to avoid bugs.
1138 static void mtk_venc_worker(struct work_struct *work)
1140 struct mtk_vcodec_enc_ctx *ctx = container_of(work, struct mtk_vcodec_enc_ctx,
1142 struct vb2_v4l2_buffer *src_buf, *dst_buf;
1143 struct venc_frm_buf frm_buf;
1144 struct mtk_vcodec_mem bs_buf;
1145 struct venc_done_result enc_result;
1148 /* check dst_buf, dst_buf may be removed in device_run
1149 * to stored encdoe header so we need check dst_buf and
1150 * call job_finish here to prevent recursion
1152 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1154 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1158 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1161 * If we see the flush buffer, send an empty buffer with the LAST flag
1162 * to the client. is_flushing will be reset at the time the buffer
1165 if (src_buf == &ctx->empty_flush_buf.vb) {
1166 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1167 dst_buf->flags |= V4L2_BUF_FLAG_LAST;
1168 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1169 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1173 memset(&frm_buf, 0, sizeof(frm_buf));
1174 for (i = 0; i < src_buf->vb2_buf.num_planes ; i++) {
1175 frm_buf.fb_addr[i].dma_addr =
1176 vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, i);
1177 frm_buf.fb_addr[i].size =
1178 (size_t)src_buf->vb2_buf.planes[i].length;
1180 bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1181 bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1182 bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
1184 mtk_v4l2_venc_dbg(2, ctx,
1185 "Framebuf PA=%llx Size=0x%zx;PA=0x%llx Size=0x%zx;PA=0x%llx Size=%zu",
1186 (u64)frm_buf.fb_addr[0].dma_addr, frm_buf.fb_addr[0].size,
1187 (u64)frm_buf.fb_addr[1].dma_addr, frm_buf.fb_addr[1].size,
1188 (u64)frm_buf.fb_addr[2].dma_addr, frm_buf.fb_addr[2].size);
1190 ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME,
1191 &frm_buf, &bs_buf, &enc_result);
1193 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1194 dst_buf->timecode = src_buf->timecode;
1196 if (enc_result.is_key_frm)
1197 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1200 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1201 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1202 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1203 mtk_v4l2_venc_err(ctx, "venc_if_encode failed=%d", ret);
1205 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1206 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, enc_result.bs_size);
1207 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1208 mtk_v4l2_venc_dbg(2, ctx, "venc_if_encode bs size=%d",
1209 enc_result.bs_size);
1212 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1214 mtk_v4l2_venc_dbg(1, ctx, "<=== src_buf[%d] dst_buf[%d] venc_if_encode ret=%d Size=%u===>",
1215 src_buf->vb2_buf.index, dst_buf->vb2_buf.index, ret, enc_result.bs_size);
1218 static void m2mops_venc_device_run(void *priv)
1220 struct mtk_vcodec_enc_ctx *ctx = priv;
1222 if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
1223 (ctx->state != MTK_STATE_HEADER)) {
1224 /* encode h264 sps/pps header */
1225 mtk_venc_encode_header(ctx);
1226 queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1230 mtk_venc_param_change(ctx);
1231 queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1234 static int m2mops_venc_job_ready(void *m2m_priv)
1236 struct mtk_vcodec_enc_ctx *ctx = m2m_priv;
1238 if (ctx->state == MTK_STATE_ABORT || ctx->state == MTK_STATE_FREE) {
1239 mtk_v4l2_venc_dbg(3, ctx, "[%d]Not ready: state=0x%x.", ctx->id, ctx->state);
1246 static void m2mops_venc_job_abort(void *priv)
1248 struct mtk_vcodec_enc_ctx *ctx = priv;
1250 ctx->state = MTK_STATE_ABORT;
1253 const struct v4l2_m2m_ops mtk_venc_m2m_ops = {
1254 .device_run = m2mops_venc_device_run,
1255 .job_ready = m2mops_venc_job_ready,
1256 .job_abort = m2mops_venc_job_abort,
1259 void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_enc_ctx *ctx)
1261 struct mtk_q_data *q_data;
1263 ctx->m2m_ctx->q_lock = &ctx->q_mutex;
1264 ctx->fh.m2m_ctx = ctx->m2m_ctx;
1265 ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1266 INIT_WORK(&ctx->encode_work, mtk_venc_worker);
1268 ctx->colorspace = V4L2_COLORSPACE_REC709;
1269 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1270 ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1271 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1273 q_data = &ctx->q_data[MTK_Q_DATA_SRC];
1274 memset(q_data, 0, sizeof(struct mtk_q_data));
1275 q_data->visible_width = DFT_CFG_WIDTH;
1276 q_data->visible_height = DFT_CFG_HEIGHT;
1277 q_data->coded_width = DFT_CFG_WIDTH;
1278 q_data->coded_height = DFT_CFG_HEIGHT;
1279 q_data->field = V4L2_FIELD_NONE;
1281 q_data->fmt = &ctx->dev->venc_pdata->output_formats[0];
1283 v4l_bound_align_image(&q_data->coded_width,
1285 MTK_VENC_HD_MAX_W, 4,
1286 &q_data->coded_height,
1288 MTK_VENC_HD_MAX_H, 5, 6);
1290 if (q_data->coded_width < DFT_CFG_WIDTH &&
1291 (q_data->coded_width + 16) <= MTK_VENC_HD_MAX_W)
1292 q_data->coded_width += 16;
1293 if (q_data->coded_height < DFT_CFG_HEIGHT &&
1294 (q_data->coded_height + 32) <= MTK_VENC_HD_MAX_H)
1295 q_data->coded_height += 32;
1297 q_data->sizeimage[0] =
1298 q_data->coded_width * q_data->coded_height+
1299 ((ALIGN(q_data->coded_width, 16) * 2) * 16);
1300 q_data->bytesperline[0] = q_data->coded_width;
1301 q_data->sizeimage[1] =
1302 (q_data->coded_width * q_data->coded_height) / 2 +
1303 (ALIGN(q_data->coded_width, 16) * 16);
1304 q_data->bytesperline[1] = q_data->coded_width;
1306 q_data = &ctx->q_data[MTK_Q_DATA_DST];
1307 memset(q_data, 0, sizeof(struct mtk_q_data));
1308 q_data->coded_width = DFT_CFG_WIDTH;
1309 q_data->coded_height = DFT_CFG_HEIGHT;
1310 q_data->fmt = &ctx->dev->venc_pdata->capture_formats[0];
1311 q_data->field = V4L2_FIELD_NONE;
1312 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
1313 DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
1314 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
1316 ctx->enc_params.framerate_num = MTK_DEFAULT_FRAMERATE_NUM;
1317 ctx->enc_params.framerate_denom = MTK_DEFAULT_FRAMERATE_DENOM;
1320 int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_enc_ctx *ctx)
1322 const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops;
1323 struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
1326 if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE)
1327 h264_max_level = V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
1329 h264_max_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
1331 v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT);
1333 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
1335 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE,
1336 ctx->dev->venc_pdata->min_bitrate,
1337 ctx->dev->venc_pdata->max_bitrate, 1, 4000000);
1338 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES,
1340 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
1342 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
1344 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
1346 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
1348 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
1350 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
1352 v4l2_ctrl_new_std_menu(handler, ops,
1353 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1354 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1355 0, V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE);
1356 v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1357 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
1358 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
1359 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
1360 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
1361 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
1362 v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1364 0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
1365 v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
1366 V4L2_MPEG_VIDEO_VP8_PROFILE_0, 0, V4L2_MPEG_VIDEO_VP8_PROFILE_0);
1367 v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1368 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
1369 ~(1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR),
1370 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
1373 if (handler->error) {
1374 mtk_v4l2_venc_err(ctx, "Init control handler fail %d", handler->error);
1375 return handler->error;
1378 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1383 int mtk_vcodec_enc_queue_init(void *priv, struct vb2_queue *src_vq,
1384 struct vb2_queue *dst_vq)
1386 struct mtk_vcodec_enc_ctx *ctx = priv;
1389 /* Note: VB2_USERPTR works with dma-contig because mt8173
1391 * https://patchwork.kernel.org/patch/8335461/
1392 * https://patchwork.kernel.org/patch/7596181/
1394 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1395 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1396 src_vq->drv_priv = ctx;
1397 src_vq->buf_struct_size = sizeof(struct mtk_video_enc_buf);
1398 src_vq->ops = &mtk_venc_vb2_ops;
1399 src_vq->mem_ops = &vb2_dma_contig_memops;
1400 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1401 src_vq->lock = &ctx->q_mutex;
1402 src_vq->dev = &ctx->dev->plat_dev->dev;
1404 ret = vb2_queue_init(src_vq);
1408 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1409 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1410 dst_vq->drv_priv = ctx;
1411 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1412 dst_vq->ops = &mtk_venc_vb2_ops;
1413 dst_vq->mem_ops = &vb2_dma_contig_memops;
1414 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1415 dst_vq->lock = &ctx->q_mutex;
1416 dst_vq->dev = &ctx->dev->plat_dev->dev;
1418 return vb2_queue_init(dst_vq);
1421 int mtk_venc_unlock(struct mtk_vcodec_enc_ctx *ctx)
1423 struct mtk_vcodec_enc_dev *dev = ctx->dev;
1425 mutex_unlock(&dev->enc_mutex);
1429 int mtk_venc_lock(struct mtk_vcodec_enc_ctx *ctx)
1431 struct mtk_vcodec_enc_dev *dev = ctx->dev;
1433 mutex_lock(&dev->enc_mutex);
1437 void mtk_vcodec_enc_release(struct mtk_vcodec_enc_ctx *ctx)
1439 int ret = venc_if_deinit(ctx);
1442 mtk_v4l2_venc_err(ctx, "venc_if_deinit failed=%d", ret);
1444 ctx->state = MTK_STATE_FREE;