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