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