1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020-2021 NXP
4  */
5 
6 #include <linux/init.h>
7 #include <linux/interconnect.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/delay.h>
13 #include <linux/videodev2.h>
14 #include <linux/ktime.h>
15 #include <linux/rational.h>
16 #include <linux/vmalloc.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-event.h>
19 #include <media/v4l2-mem2mem.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/videobuf2-v4l2.h>
22 #include <media/videobuf2-dma-contig.h>
23 #include <media/videobuf2-vmalloc.h>
24 #include "vpu.h"
25 #include "vpu_defs.h"
26 #include "vpu_core.h"
27 #include "vpu_helpers.h"
28 #include "vpu_v4l2.h"
29 #include "vpu_cmds.h"
30 #include "vpu_rpc.h"
31 
32 #define VENC_OUTPUT_ENABLE	BIT(0)
33 #define VENC_CAPTURE_ENABLE	BIT(1)
34 #define VENC_ENABLE_MASK	(VENC_OUTPUT_ENABLE | VENC_CAPTURE_ENABLE)
35 #define VENC_MAX_BUF_CNT	8
36 #define VENC_MIN_BUFFER_OUT	6
37 #define VENC_MIN_BUFFER_CAP	6
38 
39 struct venc_t {
40 	struct vpu_encode_params params;
41 	u32 request_key_frame;
42 	u32 input_ready;
43 	u32 cpb_size;
44 	bool bitrate_change;
45 
46 	struct vpu_buffer enc[VENC_MAX_BUF_CNT];
47 	struct vpu_buffer ref[VENC_MAX_BUF_CNT];
48 	struct vpu_buffer act[VENC_MAX_BUF_CNT];
49 	struct list_head frames;
50 	u32 frame_count;
51 	u32 encode_count;
52 	u32 ready_count;
53 	u32 enable;
54 	u32 stopped;
55 
56 	u32 skipped_count;
57 	u32 skipped_bytes;
58 
59 	wait_queue_head_t wq;
60 };
61 
62 struct venc_frame_t {
63 	struct list_head list;
64 	struct vpu_enc_pic_info info;
65 	u32 bytesused;
66 	s64 timestamp;
67 };
68 
69 static const struct vpu_format venc_formats[] = {
70 	{
71 		.pixfmt = V4L2_PIX_FMT_NV12M,
72 		.mem_planes = 2,
73 		.comp_planes = 2,
74 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
75 		.sibling = V4L2_PIX_FMT_NV12,
76 	},
77 	{
78 		.pixfmt = V4L2_PIX_FMT_NV12,
79 		.mem_planes = 1,
80 		.comp_planes = 2,
81 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
82 		.sibling = V4L2_PIX_FMT_NV12M,
83 	},
84 	{
85 		.pixfmt = V4L2_PIX_FMT_H264,
86 		.mem_planes = 1,
87 		.comp_planes = 1,
88 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
89 		.flags = V4L2_FMT_FLAG_COMPRESSED
90 	},
91 	{0, 0, 0, 0},
92 };
93 
94 static int venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
95 {
96 	strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
97 	strscpy(cap->card, "amphion vpu encoder", sizeof(cap->card));
98 	strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
99 
100 	return 0;
101 }
102 
103 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
104 {
105 	struct vpu_inst *inst = to_inst(file);
106 	const struct vpu_format *fmt;
107 
108 	memset(f->reserved, 0, sizeof(f->reserved));
109 	fmt = vpu_helper_enum_format(inst, f->type, f->index);
110 	if (!fmt)
111 		return -EINVAL;
112 
113 	f->pixelformat = fmt->pixfmt;
114 	f->flags = fmt->flags;
115 
116 	return 0;
117 }
118 
119 static int venc_enum_framesizes(struct file *file, void *fh, struct v4l2_frmsizeenum *fsize)
120 {
121 	struct vpu_inst *inst = to_inst(file);
122 	const struct vpu_core_resources *res;
123 
124 	if (!fsize || fsize->index)
125 		return -EINVAL;
126 
127 	if (!vpu_helper_find_format(inst, 0, fsize->pixel_format))
128 		return -EINVAL;
129 
130 	res = vpu_get_resource(inst);
131 	if (!res)
132 		return -EINVAL;
133 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
134 	fsize->stepwise.max_width = res->max_width;
135 	fsize->stepwise.max_height = res->max_height;
136 	fsize->stepwise.min_width = res->min_width;
137 	fsize->stepwise.min_height = res->min_height;
138 	fsize->stepwise.step_width = res->step_width;
139 	fsize->stepwise.step_height = res->step_height;
140 
141 	return 0;
142 }
143 
144 static int venc_enum_frameintervals(struct file *file, void *fh, struct v4l2_frmivalenum *fival)
145 {
146 	struct vpu_inst *inst = to_inst(file);
147 	const struct vpu_core_resources *res;
148 
149 	if (!fival || fival->index)
150 		return -EINVAL;
151 
152 	if (!vpu_helper_find_format(inst, 0, fival->pixel_format))
153 		return -EINVAL;
154 
155 	if (!fival->width || !fival->height)
156 		return -EINVAL;
157 
158 	res = vpu_get_resource(inst);
159 	if (!res)
160 		return -EINVAL;
161 	if (fival->width < res->min_width || fival->width > res->max_width ||
162 	    fival->height < res->min_height || fival->height > res->max_height)
163 		return -EINVAL;
164 
165 	fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
166 	fival->stepwise.min.numerator = 1;
167 	fival->stepwise.min.denominator = USHRT_MAX;
168 	fival->stepwise.max.numerator = USHRT_MAX;
169 	fival->stepwise.max.denominator = 1;
170 	fival->stepwise.step.numerator = 1;
171 	fival->stepwise.step.denominator = 1;
172 
173 	return 0;
174 }
175 
176 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
177 {
178 	struct vpu_inst *inst = to_inst(file);
179 	struct venc_t *venc = inst->priv;
180 	struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
181 	struct vpu_format *cur_fmt;
182 	int i;
183 
184 	cur_fmt = vpu_get_format(inst, f->type);
185 
186 	pixmp->pixelformat = cur_fmt->pixfmt;
187 	pixmp->num_planes = cur_fmt->mem_planes;
188 	pixmp->width = cur_fmt->width;
189 	pixmp->height = cur_fmt->height;
190 	pixmp->field = cur_fmt->field;
191 	pixmp->flags = cur_fmt->flags;
192 	for (i = 0; i < pixmp->num_planes; i++) {
193 		pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
194 		pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
195 	}
196 
197 	f->fmt.pix_mp.colorspace = venc->params.color.primaries;
198 	f->fmt.pix_mp.xfer_func = venc->params.color.transfer;
199 	f->fmt.pix_mp.ycbcr_enc = venc->params.color.matrix;
200 	f->fmt.pix_mp.quantization = venc->params.color.full_range;
201 
202 	return 0;
203 }
204 
205 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
206 {
207 	struct vpu_inst *inst = to_inst(file);
208 	struct vpu_format fmt;
209 
210 	vpu_try_fmt_common(inst, f, &fmt);
211 
212 	return 0;
213 }
214 
215 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
216 {
217 	struct vpu_inst *inst = to_inst(file);
218 	struct vpu_format fmt;
219 	struct vpu_format *cur_fmt;
220 	struct vb2_queue *q;
221 	struct venc_t *venc = inst->priv;
222 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
223 
224 	q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
225 	if (!q)
226 		return -EINVAL;
227 	if (vb2_is_busy(q))
228 		return -EBUSY;
229 
230 	if (vpu_try_fmt_common(inst, f, &fmt))
231 		return -EINVAL;
232 
233 	cur_fmt = vpu_get_format(inst, f->type);
234 
235 	memcpy(cur_fmt, &fmt, sizeof(*cur_fmt));
236 
237 	if (V4L2_TYPE_IS_OUTPUT(f->type)) {
238 		venc->params.input_format = cur_fmt->pixfmt;
239 		venc->params.src_stride = cur_fmt->bytesperline[0];
240 		venc->params.src_width = cur_fmt->width;
241 		venc->params.src_height = cur_fmt->height;
242 		venc->params.crop.left = 0;
243 		venc->params.crop.top = 0;
244 		venc->params.crop.width = cur_fmt->width;
245 		venc->params.crop.height = cur_fmt->height;
246 	} else {
247 		venc->params.codec_format = cur_fmt->pixfmt;
248 		venc->params.out_width = cur_fmt->width;
249 		venc->params.out_height = cur_fmt->height;
250 	}
251 
252 	if (V4L2_TYPE_IS_OUTPUT(f->type)) {
253 		venc->params.color.primaries = pix_mp->colorspace;
254 		venc->params.color.transfer = pix_mp->xfer_func;
255 		venc->params.color.matrix = pix_mp->ycbcr_enc;
256 		venc->params.color.full_range = pix_mp->quantization;
257 	}
258 
259 	pix_mp->colorspace = venc->params.color.primaries;
260 	pix_mp->xfer_func = venc->params.color.transfer;
261 	pix_mp->ycbcr_enc = venc->params.color.matrix;
262 	pix_mp->quantization = venc->params.color.full_range;
263 
264 	return 0;
265 }
266 
267 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
268 {
269 	struct vpu_inst *inst = to_inst(file);
270 	struct venc_t *venc = inst->priv;
271 	struct v4l2_fract *timeperframe = &parm->parm.capture.timeperframe;
272 
273 	if (!parm)
274 		return -EINVAL;
275 
276 	if (!V4L2_TYPE_IS_OUTPUT(parm->type))
277 		return -EINVAL;
278 
279 	if (!vpu_helper_check_type(inst, parm->type))
280 		return -EINVAL;
281 
282 	parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
283 	parm->parm.capture.readbuffers = 0;
284 	timeperframe->numerator = venc->params.frame_rate.numerator;
285 	timeperframe->denominator = venc->params.frame_rate.denominator;
286 
287 	return 0;
288 }
289 
290 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
291 {
292 	struct vpu_inst *inst = to_inst(file);
293 	struct venc_t *venc = inst->priv;
294 	struct v4l2_fract *timeperframe = &parm->parm.capture.timeperframe;
295 	unsigned long n, d;
296 
297 	if (!parm)
298 		return -EINVAL;
299 
300 	if (!V4L2_TYPE_IS_OUTPUT(parm->type))
301 		return -EINVAL;
302 
303 	if (!vpu_helper_check_type(inst, parm->type))
304 		return -EINVAL;
305 
306 	if (!timeperframe->numerator)
307 		timeperframe->numerator = venc->params.frame_rate.numerator;
308 	if (!timeperframe->denominator)
309 		timeperframe->denominator = venc->params.frame_rate.denominator;
310 
311 	venc->params.frame_rate.numerator = timeperframe->numerator;
312 	venc->params.frame_rate.denominator = timeperframe->denominator;
313 
314 	rational_best_approximation(venc->params.frame_rate.numerator,
315 				    venc->params.frame_rate.denominator,
316 				    venc->params.frame_rate.numerator,
317 				    venc->params.frame_rate.denominator,
318 				    &n, &d);
319 	venc->params.frame_rate.numerator = n;
320 	venc->params.frame_rate.denominator = d;
321 
322 	parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
323 	memset(parm->parm.capture.reserved, 0, sizeof(parm->parm.capture.reserved));
324 
325 	return 0;
326 }
327 
328 static int venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
329 {
330 	struct vpu_inst *inst = to_inst(file);
331 	struct venc_t *venc = inst->priv;
332 
333 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
334 		return -EINVAL;
335 
336 	switch (s->target) {
337 	case V4L2_SEL_TGT_CROP_DEFAULT:
338 	case V4L2_SEL_TGT_CROP_BOUNDS:
339 		s->r.left = 0;
340 		s->r.top = 0;
341 		s->r.width = inst->out_format.width;
342 		s->r.height = inst->out_format.height;
343 		break;
344 	case V4L2_SEL_TGT_CROP:
345 		s->r = venc->params.crop;
346 		break;
347 	default:
348 		return -EINVAL;
349 	}
350 
351 	return 0;
352 }
353 
354 static int venc_valid_crop(struct venc_t *venc, const struct vpu_core_resources *res)
355 {
356 	struct v4l2_rect *rect = NULL;
357 	u32 min_width;
358 	u32 min_height;
359 	u32 src_width;
360 	u32 src_height;
361 
362 	rect = &venc->params.crop;
363 	min_width = res->min_width;
364 	min_height = res->min_height;
365 	src_width = venc->params.src_width;
366 	src_height = venc->params.src_height;
367 
368 	if (rect->width == 0 || rect->height == 0)
369 		return -EINVAL;
370 	if (rect->left > src_width - min_width || rect->top > src_height - min_height)
371 		return -EINVAL;
372 
373 	rect->width = min(rect->width, src_width - rect->left);
374 	rect->width = max_t(u32, rect->width, min_width);
375 
376 	rect->height = min(rect->height, src_height - rect->top);
377 	rect->height = max_t(u32, rect->height, min_height);
378 
379 	return 0;
380 }
381 
382 static int venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
383 {
384 	struct vpu_inst *inst = to_inst(file);
385 	const struct vpu_core_resources *res;
386 	struct venc_t *venc = inst->priv;
387 
388 	res = vpu_get_resource(inst);
389 	if (!res)
390 		return -EINVAL;
391 
392 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
393 		return -EINVAL;
394 	if (s->target != V4L2_SEL_TGT_CROP)
395 		return -EINVAL;
396 
397 	venc->params.crop.left = ALIGN(s->r.left, res->step_width);
398 	venc->params.crop.top = ALIGN(s->r.top, res->step_height);
399 	venc->params.crop.width = ALIGN(s->r.width, res->step_width);
400 	venc->params.crop.height = ALIGN(s->r.height, res->step_height);
401 	if (venc_valid_crop(venc, res)) {
402 		venc->params.crop.left = 0;
403 		venc->params.crop.top = 0;
404 		venc->params.crop.width = venc->params.src_width;
405 		venc->params.crop.height = venc->params.src_height;
406 	}
407 
408 	inst->crop = venc->params.crop;
409 
410 	return 0;
411 }
412 
413 static int venc_drain(struct vpu_inst *inst)
414 {
415 	struct venc_t *venc = inst->priv;
416 	int ret;
417 
418 	if (!inst->fh.m2m_ctx)
419 		return 0;
420 
421 	if (inst->state != VPU_CODEC_STATE_DRAIN)
422 		return 0;
423 
424 	if (!vpu_is_source_empty(inst))
425 		return 0;
426 
427 	if (!venc->input_ready)
428 		return 0;
429 
430 	venc->input_ready = false;
431 	vpu_trace(inst->dev, "[%d]\n", inst->id);
432 	ret = vpu_session_stop(inst);
433 	if (ret)
434 		return ret;
435 	inst->state = VPU_CODEC_STATE_STOP;
436 	wake_up_all(&venc->wq);
437 
438 	return 0;
439 }
440 
441 static int venc_request_eos(struct vpu_inst *inst)
442 {
443 	inst->state = VPU_CODEC_STATE_DRAIN;
444 	venc_drain(inst);
445 
446 	return 0;
447 }
448 
449 static int venc_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *cmd)
450 {
451 	struct vpu_inst *inst = to_inst(file);
452 	int ret;
453 
454 	ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
455 	if (ret)
456 		return ret;
457 
458 	vpu_inst_lock(inst);
459 	if (cmd->cmd == V4L2_ENC_CMD_STOP) {
460 		if (inst->state == VPU_CODEC_STATE_DEINIT)
461 			vpu_set_last_buffer_dequeued(inst);
462 		else
463 			venc_request_eos(inst);
464 	}
465 	vpu_inst_unlock(inst);
466 
467 	return 0;
468 }
469 
470 static int venc_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
471 {
472 	switch (sub->type) {
473 	case V4L2_EVENT_EOS:
474 		return v4l2_event_subscribe(fh, sub, 0, NULL);
475 	case V4L2_EVENT_CTRL:
476 		return v4l2_ctrl_subscribe_event(fh, sub);
477 	default:
478 		return -EINVAL;
479 	}
480 }
481 
482 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
483 	.vidioc_querycap               = venc_querycap,
484 	.vidioc_enum_fmt_vid_cap       = venc_enum_fmt,
485 	.vidioc_enum_fmt_vid_out       = venc_enum_fmt,
486 	.vidioc_enum_framesizes        = venc_enum_framesizes,
487 	.vidioc_enum_frameintervals    = venc_enum_frameintervals,
488 	.vidioc_g_fmt_vid_cap_mplane   = venc_g_fmt,
489 	.vidioc_g_fmt_vid_out_mplane   = venc_g_fmt,
490 	.vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
491 	.vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
492 	.vidioc_s_fmt_vid_cap_mplane   = venc_s_fmt,
493 	.vidioc_s_fmt_vid_out_mplane   = venc_s_fmt,
494 	.vidioc_g_parm                 = venc_g_parm,
495 	.vidioc_s_parm                 = venc_s_parm,
496 	.vidioc_g_selection            = venc_g_selection,
497 	.vidioc_s_selection            = venc_s_selection,
498 	.vidioc_try_encoder_cmd        = v4l2_m2m_ioctl_try_encoder_cmd,
499 	.vidioc_encoder_cmd            = venc_encoder_cmd,
500 	.vidioc_subscribe_event        = venc_subscribe_event,
501 	.vidioc_unsubscribe_event      = v4l2_event_unsubscribe,
502 	.vidioc_reqbufs                = v4l2_m2m_ioctl_reqbufs,
503 	.vidioc_querybuf               = v4l2_m2m_ioctl_querybuf,
504 	.vidioc_create_bufs	       = v4l2_m2m_ioctl_create_bufs,
505 	.vidioc_prepare_buf	       = v4l2_m2m_ioctl_prepare_buf,
506 	.vidioc_qbuf                   = v4l2_m2m_ioctl_qbuf,
507 	.vidioc_expbuf                 = v4l2_m2m_ioctl_expbuf,
508 	.vidioc_dqbuf                  = v4l2_m2m_ioctl_dqbuf,
509 	.vidioc_streamon               = v4l2_m2m_ioctl_streamon,
510 	.vidioc_streamoff              = v4l2_m2m_ioctl_streamoff,
511 };
512 
513 static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
514 {
515 	struct vpu_inst *inst = ctrl_to_inst(ctrl);
516 	struct venc_t *venc = inst->priv;
517 	int ret = 0;
518 
519 	vpu_inst_lock(inst);
520 	switch (ctrl->id) {
521 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
522 		venc->params.profile = ctrl->val;
523 		break;
524 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
525 		venc->params.level = ctrl->val;
526 		break;
527 	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
528 		venc->params.rc_enable = ctrl->val;
529 		break;
530 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
531 		venc->params.rc_mode = ctrl->val;
532 		break;
533 	case V4L2_CID_MPEG_VIDEO_BITRATE:
534 		if (ctrl->val != venc->params.bitrate)
535 			venc->bitrate_change = true;
536 		venc->params.bitrate = ctrl->val;
537 		break;
538 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
539 		venc->params.bitrate_max = ctrl->val;
540 		break;
541 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
542 		venc->params.gop_length = ctrl->val;
543 		break;
544 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
545 		venc->params.bframes = ctrl->val;
546 		break;
547 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
548 		venc->params.i_frame_qp = ctrl->val;
549 		break;
550 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
551 		venc->params.p_frame_qp = ctrl->val;
552 		break;
553 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
554 		venc->params.b_frame_qp = ctrl->val;
555 		break;
556 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
557 		venc->request_key_frame = 1;
558 		break;
559 	case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
560 		venc->cpb_size = ctrl->val * 1024;
561 		break;
562 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
563 		venc->params.sar.enable = ctrl->val;
564 		break;
565 	case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
566 		venc->params.sar.idc = ctrl->val;
567 		break;
568 	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
569 		venc->params.sar.width = ctrl->val;
570 		break;
571 	case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
572 		venc->params.sar.height = ctrl->val;
573 		break;
574 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
575 		break;
576 	default:
577 		ret = -EINVAL;
578 		break;
579 	}
580 	vpu_inst_unlock(inst);
581 
582 	return ret;
583 }
584 
585 static const struct v4l2_ctrl_ops venc_ctrl_ops = {
586 	.s_ctrl = venc_op_s_ctrl,
587 	.g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
588 };
589 
590 static int venc_ctrl_init(struct vpu_inst *inst)
591 {
592 	struct v4l2_ctrl *ctrl;
593 	int ret;
594 
595 	ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
596 	if (ret)
597 		return ret;
598 
599 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
600 			       V4L2_CID_MPEG_VIDEO_H264_PROFILE,
601 			       V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
602 			       ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
603 				 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
604 				 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
605 			       V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
606 
607 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
608 			       V4L2_CID_MPEG_VIDEO_H264_LEVEL,
609 			       V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
610 			       0x0,
611 			       V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
612 
613 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
614 			  V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
615 
616 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
617 			       V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
618 			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
619 			       ~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) |
620 				 (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)),
621 			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
622 
623 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
624 			  V4L2_CID_MPEG_VIDEO_BITRATE,
625 			  BITRATE_MIN,
626 			  BITRATE_MAX,
627 			  BITRATE_STEP,
628 			  BITRATE_DEFAULT);
629 
630 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
631 			  V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
632 			  BITRATE_MIN, BITRATE_MAX,
633 			  BITRATE_STEP,
634 			  BITRATE_DEFAULT_PEAK);
635 
636 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
637 			  V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 8000, 1, 30);
638 
639 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
640 			  V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0);
641 
642 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
643 			  V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26);
644 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
645 			  V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28);
646 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
647 			  V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30);
648 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
649 			  V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0);
650 	ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
651 				 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
652 	if (ctrl)
653 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
654 	ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
655 				 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
656 	if (ctrl)
657 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
658 
659 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
660 			  V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE, 64, 10240, 1, 1024);
661 
662 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
663 			  V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE, 0, 1, 1, 1);
664 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
665 			       V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
666 			       V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
667 			       0x0,
668 			       V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
669 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
670 			  V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
671 			  0, USHRT_MAX, 1, 1);
672 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
673 			  V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
674 			  0, USHRT_MAX, 1, 1);
675 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
676 			       V4L2_CID_MPEG_VIDEO_HEADER_MODE,
677 			       V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
678 			       ~(1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME),
679 			       V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
680 
681 	if (inst->ctrl_handler.error) {
682 		ret = inst->ctrl_handler.error;
683 		v4l2_ctrl_handler_free(&inst->ctrl_handler);
684 		return ret;
685 	}
686 
687 	ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
688 	if (ret) {
689 		dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
690 		v4l2_ctrl_handler_free(&inst->ctrl_handler);
691 		return ret;
692 	}
693 
694 	return 0;
695 }
696 
697 static bool venc_check_ready(struct vpu_inst *inst, unsigned int type)
698 {
699 	struct venc_t *venc = inst->priv;
700 
701 	if (V4L2_TYPE_IS_OUTPUT(type)) {
702 		if (vpu_helper_get_free_space(inst) < venc->cpb_size)
703 			return false;
704 		return venc->input_ready;
705 	}
706 
707 	if (list_empty(&venc->frames))
708 		return false;
709 	return true;
710 }
711 
712 static u32 venc_get_enable_mask(u32 type)
713 {
714 	if (V4L2_TYPE_IS_OUTPUT(type))
715 		return VENC_OUTPUT_ENABLE;
716 	else
717 		return VENC_CAPTURE_ENABLE;
718 }
719 
720 static void venc_set_enable(struct venc_t *venc, u32 type, int enable)
721 {
722 	u32 mask = venc_get_enable_mask(type);
723 
724 	if (enable)
725 		venc->enable |= mask;
726 	else
727 		venc->enable &= ~mask;
728 }
729 
730 static u32 venc_get_enable(struct venc_t *venc, u32 type)
731 {
732 	return venc->enable & venc_get_enable_mask(type);
733 }
734 
735 static void venc_input_done(struct vpu_inst *inst)
736 {
737 	struct venc_t *venc = inst->priv;
738 
739 	vpu_inst_lock(inst);
740 	venc->input_ready = true;
741 	vpu_process_output_buffer(inst);
742 	if (inst->state == VPU_CODEC_STATE_DRAIN)
743 		venc_drain(inst);
744 	vpu_inst_unlock(inst);
745 }
746 
747 /*
748  * It's hardware limitation, that there may be several bytes
749  * redundant data at the beginning of frame.
750  * For android platform, the redundant data may cause cts test fail
751  * So driver will strip them
752  */
753 static int venc_precheck_encoded_frame(struct vpu_inst *inst, struct venc_frame_t *frame)
754 {
755 	struct venc_t *venc;
756 	int skipped;
757 
758 	if (!frame || !frame->bytesused)
759 		return -EINVAL;
760 
761 	venc = inst->priv;
762 	skipped = vpu_helper_find_startcode(&inst->stream_buffer,
763 					    inst->cap_format.pixfmt,
764 					    frame->info.wptr - inst->stream_buffer.phys,
765 					    frame->bytesused);
766 	if (skipped > 0) {
767 		frame->bytesused -= skipped;
768 		frame->info.wptr = vpu_helper_step_walk(&inst->stream_buffer,
769 							frame->info.wptr, skipped);
770 		venc->skipped_bytes += skipped;
771 		venc->skipped_count++;
772 	}
773 
774 	return 0;
775 }
776 
777 static int venc_get_one_encoded_frame(struct vpu_inst *inst,
778 				      struct venc_frame_t *frame,
779 				      struct vb2_v4l2_buffer *vbuf)
780 {
781 	struct venc_t *venc = inst->priv;
782 	struct vb2_v4l2_buffer *src_buf;
783 
784 	if (!vbuf)
785 		return -EAGAIN;
786 
787 	src_buf = vpu_find_buf_by_sequence(inst, inst->out_format.type, frame->info.frame_id);
788 	if (src_buf) {
789 		v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true);
790 		vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
791 		v4l2_m2m_src_buf_remove_by_buf(inst->fh.m2m_ctx, src_buf);
792 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
793 	} else {
794 		vbuf->vb2_buf.timestamp = frame->info.timestamp;
795 	}
796 	if (!venc_get_enable(inst->priv, vbuf->vb2_buf.type)) {
797 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
798 		return 0;
799 	}
800 	if (frame->bytesused > vbuf->vb2_buf.planes[0].length) {
801 		v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
802 		return -ENOMEM;
803 	}
804 
805 	venc_precheck_encoded_frame(inst, frame);
806 
807 	if (frame->bytesused) {
808 		u32 rptr = frame->info.wptr;
809 		void *dst = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
810 
811 		vpu_helper_copy_from_stream_buffer(&inst->stream_buffer,
812 						   &rptr, frame->bytesused, dst);
813 		vpu_iface_update_stream_buffer(inst, rptr, 0);
814 	}
815 	vb2_set_plane_payload(&vbuf->vb2_buf, 0, frame->bytesused);
816 	vbuf->sequence = frame->info.frame_id;
817 	vbuf->field = inst->cap_format.field;
818 	vbuf->flags |= frame->info.pic_type;
819 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
820 	dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
821 	v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
822 	venc->ready_count++;
823 
824 	if (vbuf->flags & V4L2_BUF_FLAG_KEYFRAME)
825 		dev_dbg(inst->dev, "[%d][%d]key frame\n", inst->id, frame->info.frame_id);
826 
827 	return 0;
828 }
829 
830 static int venc_get_encoded_frames(struct vpu_inst *inst)
831 {
832 	struct venc_t *venc;
833 	struct venc_frame_t *frame;
834 	struct venc_frame_t *tmp;
835 
836 	if (!inst->fh.m2m_ctx)
837 		return 0;
838 	venc = inst->priv;
839 	list_for_each_entry_safe(frame, tmp, &venc->frames, list) {
840 		if (venc_get_one_encoded_frame(inst, frame,
841 					       v4l2_m2m_dst_buf_remove(inst->fh.m2m_ctx)))
842 			break;
843 		list_del_init(&frame->list);
844 		vfree(frame);
845 	}
846 
847 	return 0;
848 }
849 
850 static int venc_frame_encoded(struct vpu_inst *inst, void *arg)
851 {
852 	struct vpu_enc_pic_info *info = arg;
853 	struct venc_frame_t *frame;
854 	struct venc_t *venc;
855 	int ret = 0;
856 
857 	if (!info)
858 		return -EINVAL;
859 	venc = inst->priv;
860 	frame = vzalloc(sizeof(*frame));
861 	if (!frame)
862 		return -ENOMEM;
863 
864 	memcpy(&frame->info, info, sizeof(frame->info));
865 	frame->bytesused = info->frame_size;
866 
867 	vpu_inst_lock(inst);
868 	list_add_tail(&frame->list, &venc->frames);
869 	venc->encode_count++;
870 	venc_get_encoded_frames(inst);
871 	vpu_inst_unlock(inst);
872 
873 	return ret;
874 }
875 
876 static void venc_set_last_buffer_dequeued(struct vpu_inst *inst)
877 {
878 	struct venc_t *venc = inst->priv;
879 
880 	if (venc->stopped && list_empty(&venc->frames))
881 		vpu_set_last_buffer_dequeued(inst);
882 }
883 
884 static void venc_stop_done(struct vpu_inst *inst)
885 {
886 	struct venc_t *venc = inst->priv;
887 
888 	vpu_inst_lock(inst);
889 	venc->stopped = true;
890 	venc_set_last_buffer_dequeued(inst);
891 	vpu_inst_unlock(inst);
892 
893 	wake_up_all(&venc->wq);
894 }
895 
896 static void venc_event_notify(struct vpu_inst *inst, u32 event, void *data)
897 {
898 }
899 
900 static void venc_release(struct vpu_inst *inst)
901 {
902 }
903 
904 static void venc_cleanup(struct vpu_inst *inst)
905 {
906 	struct venc_t *venc;
907 
908 	if (!inst)
909 		return;
910 
911 	venc = inst->priv;
912 	vfree(venc);
913 	inst->priv = NULL;
914 	vfree(inst);
915 }
916 
917 static int venc_start_session(struct vpu_inst *inst, u32 type)
918 {
919 	struct venc_t *venc = inst->priv;
920 	int stream_buffer_size;
921 	int ret;
922 
923 	venc_set_enable(venc, type, 1);
924 	if ((venc->enable & VENC_ENABLE_MASK) != VENC_ENABLE_MASK)
925 		return 0;
926 
927 	vpu_iface_init_instance(inst);
928 	stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
929 	if (stream_buffer_size > 0) {
930 		inst->stream_buffer.length = max_t(u32, stream_buffer_size, venc->cpb_size * 3);
931 		ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
932 		if (ret)
933 			goto error;
934 
935 		inst->use_stream_buffer = true;
936 		vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
937 	}
938 
939 	ret = vpu_iface_set_encode_params(inst, &venc->params, 0);
940 	if (ret)
941 		goto error;
942 	ret = vpu_session_configure_codec(inst);
943 	if (ret)
944 		goto error;
945 
946 	inst->state = VPU_CODEC_STATE_CONFIGURED;
947 	/*vpu_iface_config_memory_resource*/
948 
949 	/*config enc expert mode parameter*/
950 	ret = vpu_iface_set_encode_params(inst, &venc->params, 1);
951 	if (ret)
952 		goto error;
953 
954 	ret = vpu_session_start(inst);
955 	if (ret)
956 		goto error;
957 	inst->state = VPU_CODEC_STATE_STARTED;
958 
959 	venc->bitrate_change = false;
960 	venc->input_ready = true;
961 	venc->frame_count = 0;
962 	venc->encode_count = 0;
963 	venc->ready_count = 0;
964 	venc->stopped = false;
965 	vpu_process_output_buffer(inst);
966 	if (venc->frame_count == 0)
967 		dev_err(inst->dev, "[%d] there is no input when starting\n", inst->id);
968 
969 	return 0;
970 error:
971 	venc_set_enable(venc, type, 0);
972 	inst->state = VPU_CODEC_STATE_DEINIT;
973 
974 	vpu_free_dma(&inst->stream_buffer);
975 	return ret;
976 }
977 
978 static void venc_cleanup_mem_resource(struct vpu_inst *inst)
979 {
980 	struct venc_t *venc;
981 	u32 i;
982 
983 	venc = inst->priv;
984 
985 	for (i = 0; i < ARRAY_SIZE(venc->enc); i++)
986 		vpu_free_dma(&venc->enc[i]);
987 	for (i = 0; i < ARRAY_SIZE(venc->ref); i++)
988 		vpu_free_dma(&venc->ref[i]);
989 }
990 
991 static void venc_request_mem_resource(struct vpu_inst *inst,
992 				      u32 enc_frame_size,
993 				      u32 enc_frame_num,
994 				      u32 ref_frame_size,
995 				      u32 ref_frame_num,
996 				      u32 act_frame_size,
997 				      u32 act_frame_num)
998 {
999 	struct venc_t *venc;
1000 	u32 i;
1001 	int ret;
1002 
1003 	venc = inst->priv;
1004 	if (enc_frame_num > ARRAY_SIZE(venc->enc)) {
1005 		dev_err(inst->dev, "[%d] enc num(%d) is out of range\n", inst->id, enc_frame_num);
1006 		return;
1007 	}
1008 	if (ref_frame_num > ARRAY_SIZE(venc->ref)) {
1009 		dev_err(inst->dev, "[%d] ref num(%d) is out of range\n", inst->id, ref_frame_num);
1010 		return;
1011 	}
1012 	if (act_frame_num > ARRAY_SIZE(venc->act)) {
1013 		dev_err(inst->dev, "[%d] act num(%d) is out of range\n", inst->id, act_frame_num);
1014 		return;
1015 	}
1016 
1017 	for (i = 0; i < enc_frame_num; i++) {
1018 		venc->enc[i].length = enc_frame_size;
1019 		ret = vpu_alloc_dma(inst->core, &venc->enc[i]);
1020 		if (ret) {
1021 			venc_cleanup_mem_resource(inst);
1022 			return;
1023 		}
1024 	}
1025 	for (i = 0; i < ref_frame_num; i++) {
1026 		venc->ref[i].length = ref_frame_size;
1027 		ret = vpu_alloc_dma(inst->core, &venc->ref[i]);
1028 		if (ret) {
1029 			venc_cleanup_mem_resource(inst);
1030 			return;
1031 		}
1032 	}
1033 	if (act_frame_num != 1 || act_frame_size > inst->act.length) {
1034 		venc_cleanup_mem_resource(inst);
1035 		return;
1036 	}
1037 	venc->act[0].length = act_frame_size;
1038 	venc->act[0].phys = inst->act.phys;
1039 	venc->act[0].virt = inst->act.virt;
1040 
1041 	for (i = 0; i < enc_frame_num; i++)
1042 		vpu_iface_config_memory_resource(inst, MEM_RES_ENC, i, &venc->enc[i]);
1043 	for (i = 0; i < ref_frame_num; i++)
1044 		vpu_iface_config_memory_resource(inst, MEM_RES_REF, i, &venc->ref[i]);
1045 	for (i = 0; i < act_frame_num; i++)
1046 		vpu_iface_config_memory_resource(inst, MEM_RES_ACT, i, &venc->act[i]);
1047 }
1048 
1049 static void venc_cleanup_frames(struct venc_t *venc)
1050 {
1051 	struct venc_frame_t *frame;
1052 	struct venc_frame_t *tmp;
1053 
1054 	list_for_each_entry_safe(frame, tmp, &venc->frames, list) {
1055 		list_del_init(&frame->list);
1056 		vfree(frame);
1057 	}
1058 }
1059 
1060 static int venc_stop_session(struct vpu_inst *inst, u32 type)
1061 {
1062 	struct venc_t *venc = inst->priv;
1063 
1064 	venc_set_enable(venc, type, 0);
1065 	if (venc->enable & VENC_ENABLE_MASK)
1066 		return 0;
1067 
1068 	if (inst->state == VPU_CODEC_STATE_DEINIT)
1069 		return 0;
1070 
1071 	if (inst->state != VPU_CODEC_STATE_STOP)
1072 		venc_request_eos(inst);
1073 
1074 	call_void_vop(inst, wait_prepare);
1075 	if (!wait_event_timeout(venc->wq, venc->stopped, VPU_TIMEOUT)) {
1076 		set_bit(inst->id, &inst->core->hang_mask);
1077 		vpu_session_debug(inst);
1078 	}
1079 	call_void_vop(inst, wait_finish);
1080 
1081 	inst->state = VPU_CODEC_STATE_DEINIT;
1082 	venc_cleanup_frames(inst->priv);
1083 	vpu_free_dma(&inst->stream_buffer);
1084 	venc_cleanup_mem_resource(inst);
1085 
1086 	return 0;
1087 }
1088 
1089 static int venc_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
1090 {
1091 	struct venc_t *venc = inst->priv;
1092 	struct vb2_v4l2_buffer *vbuf;
1093 	u32 flags;
1094 
1095 	if (inst->state == VPU_CODEC_STATE_DEINIT)
1096 		return -EINVAL;
1097 
1098 	vbuf = to_vb2_v4l2_buffer(vb);
1099 	if (inst->state == VPU_CODEC_STATE_STARTED)
1100 		inst->state = VPU_CODEC_STATE_ACTIVE;
1101 
1102 	flags = vbuf->flags;
1103 	if (venc->request_key_frame) {
1104 		vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1105 		venc->request_key_frame = 0;
1106 	}
1107 	if (venc->bitrate_change) {
1108 		vpu_session_update_parameters(inst, &venc->params);
1109 		venc->bitrate_change = false;
1110 	}
1111 	dev_dbg(inst->dev, "[%d][INPUT  TS]%32lld\n", inst->id, vb->timestamp);
1112 	vpu_iface_input_frame(inst, vb);
1113 	vbuf->flags = flags;
1114 	venc->input_ready = false;
1115 	venc->frame_count++;
1116 	vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1117 
1118 	return 0;
1119 }
1120 
1121 static int venc_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
1122 {
1123 	struct venc_t *venc;
1124 	struct venc_frame_t *frame = NULL;
1125 	struct vb2_v4l2_buffer *vbuf;
1126 	int ret;
1127 
1128 	venc = inst->priv;
1129 	if (list_empty(&venc->frames))
1130 		return -EINVAL;
1131 
1132 	frame = list_first_entry(&venc->frames, struct venc_frame_t, list);
1133 	vbuf = to_vb2_v4l2_buffer(vb);
1134 	v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1135 	ret = venc_get_one_encoded_frame(inst, frame, vbuf);
1136 	if (ret)
1137 		return ret;
1138 
1139 	list_del_init(&frame->list);
1140 	vfree(frame);
1141 	return 0;
1142 }
1143 
1144 static void venc_on_queue_empty(struct vpu_inst *inst, u32 type)
1145 {
1146 	struct venc_t *venc = inst->priv;
1147 
1148 	if (V4L2_TYPE_IS_OUTPUT(type))
1149 		return;
1150 
1151 	if (venc->stopped)
1152 		venc_set_last_buffer_dequeued(inst);
1153 }
1154 
1155 static int venc_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
1156 {
1157 	struct venc_t *venc = inst->priv;
1158 	int num = -1;
1159 
1160 	switch (i) {
1161 	case 0:
1162 		num = scnprintf(str, size, "profile = %d\n", venc->params.profile);
1163 		break;
1164 	case 1:
1165 		num = scnprintf(str, size, "level = %d\n", venc->params.level);
1166 		break;
1167 	case 2:
1168 		num = scnprintf(str, size, "fps = %d/%d\n",
1169 				venc->params.frame_rate.numerator,
1170 				venc->params.frame_rate.denominator);
1171 		break;
1172 	case 3:
1173 		num = scnprintf(str, size, "%d x %d -> %d x %d\n",
1174 				venc->params.src_width,
1175 				venc->params.src_height,
1176 				venc->params.out_width,
1177 				venc->params.out_height);
1178 		break;
1179 	case 4:
1180 		num = scnprintf(str, size, "(%d, %d)  %d x %d\n",
1181 				venc->params.crop.left,
1182 				venc->params.crop.top,
1183 				venc->params.crop.width,
1184 				venc->params.crop.height);
1185 		break;
1186 	case 5:
1187 		num = scnprintf(str, size,
1188 				"enable = 0x%x, input = %d, encode = %d, ready = %d, stopped = %d\n",
1189 				venc->enable,
1190 				venc->frame_count, venc->encode_count,
1191 				venc->ready_count,
1192 				venc->stopped);
1193 		break;
1194 	case 6:
1195 		num = scnprintf(str, size, "gop = %d\n", venc->params.gop_length);
1196 		break;
1197 	case 7:
1198 		num = scnprintf(str, size, "bframes = %d\n", venc->params.bframes);
1199 		break;
1200 	case 8:
1201 		num = scnprintf(str, size, "rc: %s, mode = %d, bitrate = %d(%d), qp = %d\n",
1202 				venc->params.rc_enable ? "enable" : "disable",
1203 				venc->params.rc_mode,
1204 				venc->params.bitrate,
1205 				venc->params.bitrate_max,
1206 				venc->params.i_frame_qp);
1207 		break;
1208 	case 9:
1209 		num = scnprintf(str, size, "sar: enable = %d, idc = %d, %d x %d\n",
1210 				venc->params.sar.enable,
1211 				venc->params.sar.idc,
1212 				venc->params.sar.width,
1213 				venc->params.sar.height);
1214 
1215 		break;
1216 	case 10:
1217 		num = scnprintf(str, size,
1218 				"colorspace: primaries = %d, transfer = %d, matrix = %d, full_range = %d\n",
1219 				venc->params.color.primaries,
1220 				venc->params.color.transfer,
1221 				venc->params.color.matrix,
1222 				venc->params.color.full_range);
1223 		break;
1224 	case 11:
1225 		num = scnprintf(str, size, "skipped: count = %d, bytes = %d\n",
1226 				venc->skipped_count, venc->skipped_bytes);
1227 		break;
1228 	default:
1229 		break;
1230 	}
1231 
1232 	return num;
1233 }
1234 
1235 static struct vpu_inst_ops venc_inst_ops = {
1236 	.ctrl_init = venc_ctrl_init,
1237 	.check_ready = venc_check_ready,
1238 	.input_done = venc_input_done,
1239 	.get_one_frame = venc_frame_encoded,
1240 	.stop_done = venc_stop_done,
1241 	.event_notify = venc_event_notify,
1242 	.release = venc_release,
1243 	.cleanup = venc_cleanup,
1244 	.start = venc_start_session,
1245 	.mem_request = venc_request_mem_resource,
1246 	.stop = venc_stop_session,
1247 	.process_output = venc_process_output,
1248 	.process_capture = venc_process_capture,
1249 	.on_queue_empty = venc_on_queue_empty,
1250 	.get_debug_info = venc_get_debug_info,
1251 	.wait_prepare = vpu_inst_unlock,
1252 	.wait_finish = vpu_inst_lock,
1253 };
1254 
1255 static void venc_init(struct file *file)
1256 {
1257 	struct vpu_inst *inst = to_inst(file);
1258 	struct venc_t *venc;
1259 	struct v4l2_format f;
1260 	struct v4l2_streamparm parm;
1261 
1262 	venc = inst->priv;
1263 	venc->params.qp_min = 1;
1264 	venc->params.qp_max = 51;
1265 	venc->params.qp_min_i = 1;
1266 	venc->params.qp_max_i = 51;
1267 	venc->params.bitrate_min = BITRATE_MIN;
1268 
1269 	memset(&f, 0, sizeof(f));
1270 	f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1271 	f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M;
1272 	f.fmt.pix_mp.width = 1280;
1273 	f.fmt.pix_mp.height = 720;
1274 	f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1275 	venc_s_fmt(file, &inst->fh, &f);
1276 
1277 	memset(&f, 0, sizeof(f));
1278 	f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1279 	f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1280 	f.fmt.pix_mp.width = 1280;
1281 	f.fmt.pix_mp.height = 720;
1282 	f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1283 	venc_s_fmt(file, &inst->fh, &f);
1284 
1285 	memset(&parm, 0, sizeof(parm));
1286 	parm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1287 	parm.parm.capture.timeperframe.numerator = 1;
1288 	parm.parm.capture.timeperframe.denominator = 30;
1289 	venc_s_parm(file, &inst->fh, &parm);
1290 }
1291 
1292 static int venc_open(struct file *file)
1293 {
1294 	struct vpu_inst *inst;
1295 	struct venc_t *venc;
1296 	int ret;
1297 
1298 	inst = vzalloc(sizeof(*inst));
1299 	if (!inst)
1300 		return -ENOMEM;
1301 
1302 	venc = vzalloc(sizeof(*venc));
1303 	if (!venc) {
1304 		vfree(inst);
1305 		return -ENOMEM;
1306 	}
1307 
1308 	inst->ops = &venc_inst_ops;
1309 	inst->formats = venc_formats;
1310 	inst->type = VPU_CORE_TYPE_ENC;
1311 	inst->priv = venc;
1312 	INIT_LIST_HEAD(&venc->frames);
1313 	init_waitqueue_head(&venc->wq);
1314 
1315 	ret = vpu_v4l2_open(file, inst);
1316 	if (ret)
1317 		return ret;
1318 
1319 	inst->min_buffer_out = VENC_MIN_BUFFER_OUT;
1320 	inst->min_buffer_cap = VENC_MIN_BUFFER_CAP;
1321 	venc_init(file);
1322 
1323 	return 0;
1324 }
1325 
1326 static const struct v4l2_file_operations venc_fops = {
1327 	.owner = THIS_MODULE,
1328 	.open = venc_open,
1329 	.release = vpu_v4l2_close,
1330 	.unlocked_ioctl = video_ioctl2,
1331 	.poll = v4l2_m2m_fop_poll,
1332 	.mmap = v4l2_m2m_fop_mmap,
1333 };
1334 
1335 const struct v4l2_ioctl_ops *venc_get_ioctl_ops(void)
1336 {
1337 	return &venc_ioctl_ops;
1338 }
1339 
1340 const struct v4l2_file_operations *venc_get_fops(void)
1341 {
1342 	return &venc_fops;
1343 }
1344