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