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