1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020-2021 NXP
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>
27 #include "vpu_helpers.h"
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
40 struct vpu_encode_params params;
41 u32 request_key_frame;
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;
63 struct list_head list;
64 struct vpu_enc_pic_info info;
69 static const struct vpu_format venc_formats[] = {
71 .pixfmt = V4L2_PIX_FMT_NV12M,
74 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
75 .sibling = V4L2_PIX_FMT_NV12,
78 .pixfmt = V4L2_PIX_FMT_NV12,
81 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
82 .sibling = V4L2_PIX_FMT_NV12M,
85 .pixfmt = V4L2_PIX_FMT_H264,
88 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
89 .flags = V4L2_FMT_FLAG_COMPRESSED
94 static int venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
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));
103 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
105 struct vpu_inst *inst = to_inst(file);
106 const struct vpu_format *fmt;
108 memset(f->reserved, 0, sizeof(f->reserved));
109 fmt = vpu_helper_enum_format(inst, f->type, f->index);
113 f->pixelformat = fmt->pixfmt;
114 f->flags = fmt->flags;
119 static int venc_enum_framesizes(struct file *file, void *fh, struct v4l2_frmsizeenum *fsize)
121 struct vpu_inst *inst = to_inst(file);
122 const struct vpu_core_resources *res;
124 if (!fsize || fsize->index)
127 if (!vpu_helper_find_format(inst, 0, fsize->pixel_format))
130 res = vpu_get_resource(inst);
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;
144 static int venc_enum_frameintervals(struct file *file, void *fh, struct v4l2_frmivalenum *fival)
146 struct vpu_inst *inst = to_inst(file);
147 const struct vpu_core_resources *res;
149 if (!fival || fival->index)
152 if (!vpu_helper_find_format(inst, 0, fival->pixel_format))
155 if (!fival->width || !fival->height)
158 res = vpu_get_resource(inst);
161 if (fival->width < res->min_width || fival->width > res->max_width ||
162 fival->height < res->min_height || fival->height > res->max_height)
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;
176 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
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;
184 cur_fmt = vpu_get_format(inst, f->type);
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);
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;
205 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
207 struct vpu_inst *inst = to_inst(file);
208 struct vpu_format fmt;
210 vpu_try_fmt_common(inst, f, &fmt);
215 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
217 struct vpu_inst *inst = to_inst(file);
218 struct vpu_format fmt;
219 struct vpu_format *cur_fmt;
221 struct venc_t *venc = inst->priv;
222 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
224 q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
230 if (vpu_try_fmt_common(inst, f, &fmt))
233 cur_fmt = vpu_get_format(inst, f->type);
235 memcpy(cur_fmt, &fmt, sizeof(*cur_fmt));
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;
247 venc->params.codec_format = cur_fmt->pixfmt;
248 venc->params.out_width = cur_fmt->width;
249 venc->params.out_height = cur_fmt->height;
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;
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;
267 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
269 struct vpu_inst *inst = to_inst(file);
270 struct venc_t *venc = inst->priv;
271 struct v4l2_fract *timeperframe;
276 if (!V4L2_TYPE_IS_OUTPUT(parm->type))
279 if (!vpu_helper_check_type(inst, parm->type))
282 timeperframe = &parm->parm.capture.timeperframe;
283 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
284 parm->parm.capture.readbuffers = 0;
285 timeperframe->numerator = venc->params.frame_rate.numerator;
286 timeperframe->denominator = venc->params.frame_rate.denominator;
291 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
293 struct vpu_inst *inst = to_inst(file);
294 struct venc_t *venc = inst->priv;
295 struct v4l2_fract *timeperframe;
301 if (!V4L2_TYPE_IS_OUTPUT(parm->type))
304 if (!vpu_helper_check_type(inst, parm->type))
307 timeperframe = &parm->parm.capture.timeperframe;
308 if (!timeperframe->numerator)
309 timeperframe->numerator = venc->params.frame_rate.numerator;
310 if (!timeperframe->denominator)
311 timeperframe->denominator = venc->params.frame_rate.denominator;
313 venc->params.frame_rate.numerator = timeperframe->numerator;
314 venc->params.frame_rate.denominator = timeperframe->denominator;
316 rational_best_approximation(venc->params.frame_rate.numerator,
317 venc->params.frame_rate.denominator,
318 venc->params.frame_rate.numerator,
319 venc->params.frame_rate.denominator,
321 venc->params.frame_rate.numerator = n;
322 venc->params.frame_rate.denominator = d;
324 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
325 memset(parm->parm.capture.reserved, 0, sizeof(parm->parm.capture.reserved));
330 static int venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
332 struct vpu_inst *inst = to_inst(file);
333 struct venc_t *venc = inst->priv;
335 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
339 case V4L2_SEL_TGT_CROP_DEFAULT:
340 case V4L2_SEL_TGT_CROP_BOUNDS:
343 s->r.width = inst->out_format.width;
344 s->r.height = inst->out_format.height;
346 case V4L2_SEL_TGT_CROP:
347 s->r = venc->params.crop;
356 static int venc_valid_crop(struct venc_t *venc, const struct vpu_core_resources *res)
358 struct v4l2_rect *rect = NULL;
364 rect = &venc->params.crop;
365 min_width = res->min_width;
366 min_height = res->min_height;
367 src_width = venc->params.src_width;
368 src_height = venc->params.src_height;
370 if (rect->width == 0 || rect->height == 0)
372 if (rect->left > src_width - min_width || rect->top > src_height - min_height)
375 rect->width = min(rect->width, src_width - rect->left);
376 rect->width = max_t(u32, rect->width, min_width);
378 rect->height = min(rect->height, src_height - rect->top);
379 rect->height = max_t(u32, rect->height, min_height);
384 static int venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
386 struct vpu_inst *inst = to_inst(file);
387 const struct vpu_core_resources *res;
388 struct venc_t *venc = inst->priv;
390 res = vpu_get_resource(inst);
394 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
396 if (s->target != V4L2_SEL_TGT_CROP)
399 venc->params.crop.left = ALIGN(s->r.left, res->step_width);
400 venc->params.crop.top = ALIGN(s->r.top, res->step_height);
401 venc->params.crop.width = ALIGN(s->r.width, res->step_width);
402 venc->params.crop.height = ALIGN(s->r.height, res->step_height);
403 if (venc_valid_crop(venc, res)) {
404 venc->params.crop.left = 0;
405 venc->params.crop.top = 0;
406 venc->params.crop.width = venc->params.src_width;
407 venc->params.crop.height = venc->params.src_height;
410 inst->crop = venc->params.crop;
415 static int venc_drain(struct vpu_inst *inst)
417 struct venc_t *venc = inst->priv;
420 if (!inst->fh.m2m_ctx)
423 if (inst->state != VPU_CODEC_STATE_DRAIN)
426 if (!vpu_is_source_empty(inst))
429 if (!venc->input_ready)
432 venc->input_ready = false;
433 vpu_trace(inst->dev, "[%d]\n", inst->id);
434 ret = vpu_session_stop(inst);
437 inst->state = VPU_CODEC_STATE_STOP;
438 wake_up_all(&venc->wq);
443 static int venc_request_eos(struct vpu_inst *inst)
445 inst->state = VPU_CODEC_STATE_DRAIN;
451 static int venc_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *cmd)
453 struct vpu_inst *inst = to_inst(file);
456 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
461 if (cmd->cmd == V4L2_ENC_CMD_STOP) {
462 if (inst->state == VPU_CODEC_STATE_DEINIT)
463 vpu_set_last_buffer_dequeued(inst, true);
465 venc_request_eos(inst);
467 vpu_inst_unlock(inst);
472 static int venc_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
476 return v4l2_event_subscribe(fh, sub, 0, NULL);
477 case V4L2_EVENT_CTRL:
478 return v4l2_ctrl_subscribe_event(fh, sub);
484 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
485 .vidioc_querycap = venc_querycap,
486 .vidioc_enum_fmt_vid_cap = venc_enum_fmt,
487 .vidioc_enum_fmt_vid_out = venc_enum_fmt,
488 .vidioc_enum_framesizes = venc_enum_framesizes,
489 .vidioc_enum_frameintervals = venc_enum_frameintervals,
490 .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
491 .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
492 .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
493 .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
494 .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
495 .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
496 .vidioc_g_parm = venc_g_parm,
497 .vidioc_s_parm = venc_s_parm,
498 .vidioc_g_selection = venc_g_selection,
499 .vidioc_s_selection = venc_s_selection,
500 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
501 .vidioc_encoder_cmd = venc_encoder_cmd,
502 .vidioc_subscribe_event = venc_subscribe_event,
503 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
504 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
505 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
506 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
507 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
508 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
509 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
510 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
511 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
512 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
515 static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
517 struct vpu_inst *inst = ctrl_to_inst(ctrl);
518 struct venc_t *venc = inst->priv;
523 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
524 venc->params.profile = ctrl->val;
526 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
527 venc->params.level = ctrl->val;
529 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
530 venc->params.rc_enable = ctrl->val;
532 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
533 venc->params.rc_mode = ctrl->val;
535 case V4L2_CID_MPEG_VIDEO_BITRATE:
536 if (ctrl->val != venc->params.bitrate)
537 venc->bitrate_change = true;
538 venc->params.bitrate = ctrl->val;
540 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
541 venc->params.bitrate_max = ctrl->val;
543 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
544 venc->params.gop_length = ctrl->val;
546 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
547 venc->params.bframes = ctrl->val;
549 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
550 venc->params.i_frame_qp = ctrl->val;
552 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
553 venc->params.p_frame_qp = ctrl->val;
555 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
556 venc->params.b_frame_qp = ctrl->val;
558 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
559 venc->request_key_frame = 1;
561 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
562 venc->cpb_size = ctrl->val * 1024;
564 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
565 venc->params.sar.enable = ctrl->val;
567 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
568 venc->params.sar.idc = ctrl->val;
570 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
571 venc->params.sar.width = ctrl->val;
573 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
574 venc->params.sar.height = ctrl->val;
576 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
582 vpu_inst_unlock(inst);
587 static const struct v4l2_ctrl_ops venc_ctrl_ops = {
588 .s_ctrl = venc_op_s_ctrl,
589 .g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
592 static int venc_ctrl_init(struct vpu_inst *inst)
594 struct v4l2_ctrl *ctrl;
597 ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
601 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
602 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
603 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
604 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
605 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
606 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
607 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
609 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
610 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
611 V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
613 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
615 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
616 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
618 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
619 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
620 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
621 ~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) |
622 (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)),
623 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
625 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
626 V4L2_CID_MPEG_VIDEO_BITRATE,
632 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
633 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
634 BITRATE_MIN, BITRATE_MAX,
636 BITRATE_DEFAULT_PEAK);
638 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
639 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 8000, 1, 30);
641 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
642 V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0);
644 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
645 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26);
646 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
647 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28);
648 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
649 V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30);
650 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
651 V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0);
652 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
653 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
655 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
656 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
657 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
659 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
661 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
662 V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE, 64, 10240, 1, 1024);
664 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
665 V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE, 0, 1, 1, 1);
666 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
667 V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
668 V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
670 V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
671 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
672 V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
674 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
675 V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
677 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
678 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
679 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
680 ~(1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME),
681 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
683 if (inst->ctrl_handler.error) {
684 ret = inst->ctrl_handler.error;
685 v4l2_ctrl_handler_free(&inst->ctrl_handler);
689 ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
691 dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
692 v4l2_ctrl_handler_free(&inst->ctrl_handler);
699 static bool venc_check_ready(struct vpu_inst *inst, unsigned int type)
701 struct venc_t *venc = inst->priv;
703 if (V4L2_TYPE_IS_OUTPUT(type)) {
704 if (vpu_helper_get_free_space(inst) < venc->cpb_size)
706 return venc->input_ready;
709 if (list_empty(&venc->frames))
714 static u32 venc_get_enable_mask(u32 type)
716 if (V4L2_TYPE_IS_OUTPUT(type))
717 return VENC_OUTPUT_ENABLE;
719 return VENC_CAPTURE_ENABLE;
722 static void venc_set_enable(struct venc_t *venc, u32 type, int enable)
724 u32 mask = venc_get_enable_mask(type);
727 venc->enable |= mask;
729 venc->enable &= ~mask;
732 static u32 venc_get_enable(struct venc_t *venc, u32 type)
734 return venc->enable & venc_get_enable_mask(type);
737 static void venc_input_done(struct vpu_inst *inst)
739 struct venc_t *venc = inst->priv;
742 venc->input_ready = true;
743 vpu_process_output_buffer(inst);
744 if (inst->state == VPU_CODEC_STATE_DRAIN)
746 vpu_inst_unlock(inst);
750 * It's hardware limitation, that there may be several bytes
751 * redundant data at the beginning of frame.
752 * For android platform, the redundant data may cause cts test fail
753 * So driver will strip them
755 static int venc_precheck_encoded_frame(struct vpu_inst *inst, struct venc_frame_t *frame)
760 if (!frame || !frame->bytesused)
764 skipped = vpu_helper_find_startcode(&inst->stream_buffer,
765 inst->cap_format.pixfmt,
766 frame->info.wptr - inst->stream_buffer.phys,
769 frame->bytesused -= skipped;
770 frame->info.wptr = vpu_helper_step_walk(&inst->stream_buffer,
771 frame->info.wptr, skipped);
772 venc->skipped_bytes += skipped;
773 venc->skipped_count++;
779 static int venc_get_one_encoded_frame(struct vpu_inst *inst,
780 struct venc_frame_t *frame,
781 struct vb2_v4l2_buffer *vbuf)
783 struct venc_t *venc = inst->priv;
784 struct vb2_v4l2_buffer *src_buf;
789 src_buf = vpu_find_buf_by_sequence(inst, inst->out_format.type, frame->info.frame_id);
791 v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true);
792 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
793 v4l2_m2m_src_buf_remove_by_buf(inst->fh.m2m_ctx, src_buf);
794 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
796 vbuf->vb2_buf.timestamp = frame->info.timestamp;
798 if (!venc_get_enable(inst->priv, vbuf->vb2_buf.type)) {
799 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
802 if (frame->bytesused > vbuf->vb2_buf.planes[0].length) {
803 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
807 venc_precheck_encoded_frame(inst, frame);
809 if (frame->bytesused) {
810 u32 rptr = frame->info.wptr;
811 void *dst = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
813 vpu_helper_copy_from_stream_buffer(&inst->stream_buffer,
814 &rptr, frame->bytesused, dst);
815 vpu_iface_update_stream_buffer(inst, rptr, 0);
817 vb2_set_plane_payload(&vbuf->vb2_buf, 0, frame->bytesused);
818 vbuf->sequence = frame->info.frame_id;
819 vbuf->field = inst->cap_format.field;
820 vbuf->flags |= frame->info.pic_type;
821 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
822 dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
823 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
826 if (vbuf->flags & V4L2_BUF_FLAG_KEYFRAME)
827 dev_dbg(inst->dev, "[%d][%d]key frame\n", inst->id, frame->info.frame_id);
832 static int venc_get_encoded_frames(struct vpu_inst *inst)
835 struct venc_frame_t *frame;
836 struct venc_frame_t *tmp;
838 if (!inst->fh.m2m_ctx)
841 list_for_each_entry_safe(frame, tmp, &venc->frames, list) {
842 if (venc_get_one_encoded_frame(inst, frame,
843 v4l2_m2m_dst_buf_remove(inst->fh.m2m_ctx)))
845 list_del_init(&frame->list);
852 static int venc_frame_encoded(struct vpu_inst *inst, void *arg)
854 struct vpu_enc_pic_info *info = arg;
855 struct venc_frame_t *frame;
862 frame = vzalloc(sizeof(*frame));
866 memcpy(&frame->info, info, sizeof(frame->info));
867 frame->bytesused = info->frame_size;
870 list_add_tail(&frame->list, &venc->frames);
871 venc->encode_count++;
872 venc_get_encoded_frames(inst);
873 vpu_inst_unlock(inst);
878 static void venc_set_last_buffer_dequeued(struct vpu_inst *inst)
880 struct venc_t *venc = inst->priv;
882 if (venc->stopped && list_empty(&venc->frames))
883 vpu_set_last_buffer_dequeued(inst, true);
886 static void venc_stop_done(struct vpu_inst *inst)
888 struct venc_t *venc = inst->priv;
891 venc->stopped = true;
892 venc_set_last_buffer_dequeued(inst);
893 vpu_inst_unlock(inst);
895 wake_up_all(&venc->wq);
898 static void venc_event_notify(struct vpu_inst *inst, u32 event, void *data)
902 static void venc_release(struct vpu_inst *inst)
906 static void venc_cleanup(struct vpu_inst *inst)
919 static int venc_start_session(struct vpu_inst *inst, u32 type)
921 struct venc_t *venc = inst->priv;
922 int stream_buffer_size;
925 venc_set_enable(venc, type, 1);
926 if ((venc->enable & VENC_ENABLE_MASK) != VENC_ENABLE_MASK)
929 vpu_iface_init_instance(inst);
930 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
931 if (stream_buffer_size > 0) {
932 inst->stream_buffer.length = max_t(u32, stream_buffer_size, venc->cpb_size * 3);
933 ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
937 inst->use_stream_buffer = true;
938 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
941 ret = vpu_iface_set_encode_params(inst, &venc->params, 0);
944 ret = vpu_session_configure_codec(inst);
948 inst->state = VPU_CODEC_STATE_CONFIGURED;
949 /*vpu_iface_config_memory_resource*/
951 /*config enc expert mode parameter*/
952 ret = vpu_iface_set_encode_params(inst, &venc->params, 1);
956 ret = vpu_session_start(inst);
959 inst->state = VPU_CODEC_STATE_STARTED;
961 venc->bitrate_change = false;
962 venc->input_ready = true;
963 venc->frame_count = 0;
964 venc->encode_count = 0;
965 venc->ready_count = 0;
966 venc->stopped = false;
967 vpu_process_output_buffer(inst);
968 if (venc->frame_count == 0)
969 dev_err(inst->dev, "[%d] there is no input when starting\n", inst->id);
973 venc_set_enable(venc, type, 0);
974 inst->state = VPU_CODEC_STATE_DEINIT;
976 vpu_free_dma(&inst->stream_buffer);
980 static void venc_cleanup_mem_resource(struct vpu_inst *inst)
987 for (i = 0; i < ARRAY_SIZE(venc->enc); i++)
988 vpu_free_dma(&venc->enc[i]);
989 for (i = 0; i < ARRAY_SIZE(venc->ref); i++)
990 vpu_free_dma(&venc->ref[i]);
993 static void venc_request_mem_resource(struct vpu_inst *inst,
1001 struct venc_t *venc;
1006 if (enc_frame_num > ARRAY_SIZE(venc->enc)) {
1007 dev_err(inst->dev, "[%d] enc num(%d) is out of range\n", inst->id, enc_frame_num);
1010 if (ref_frame_num > ARRAY_SIZE(venc->ref)) {
1011 dev_err(inst->dev, "[%d] ref num(%d) is out of range\n", inst->id, ref_frame_num);
1014 if (act_frame_num > ARRAY_SIZE(venc->act)) {
1015 dev_err(inst->dev, "[%d] act num(%d) is out of range\n", inst->id, act_frame_num);
1019 for (i = 0; i < enc_frame_num; i++) {
1020 venc->enc[i].length = enc_frame_size;
1021 ret = vpu_alloc_dma(inst->core, &venc->enc[i]);
1023 venc_cleanup_mem_resource(inst);
1027 for (i = 0; i < ref_frame_num; i++) {
1028 venc->ref[i].length = ref_frame_size;
1029 ret = vpu_alloc_dma(inst->core, &venc->ref[i]);
1031 venc_cleanup_mem_resource(inst);
1035 if (act_frame_num != 1 || act_frame_size > inst->act.length) {
1036 venc_cleanup_mem_resource(inst);
1039 venc->act[0].length = act_frame_size;
1040 venc->act[0].phys = inst->act.phys;
1041 venc->act[0].virt = inst->act.virt;
1043 for (i = 0; i < enc_frame_num; i++)
1044 vpu_iface_config_memory_resource(inst, MEM_RES_ENC, i, &venc->enc[i]);
1045 for (i = 0; i < ref_frame_num; i++)
1046 vpu_iface_config_memory_resource(inst, MEM_RES_REF, i, &venc->ref[i]);
1047 for (i = 0; i < act_frame_num; i++)
1048 vpu_iface_config_memory_resource(inst, MEM_RES_ACT, i, &venc->act[i]);
1051 static void venc_cleanup_frames(struct venc_t *venc)
1053 struct venc_frame_t *frame;
1054 struct venc_frame_t *tmp;
1056 list_for_each_entry_safe(frame, tmp, &venc->frames, list) {
1057 list_del_init(&frame->list);
1062 static int venc_stop_session(struct vpu_inst *inst, u32 type)
1064 struct venc_t *venc = inst->priv;
1066 venc_set_enable(venc, type, 0);
1067 if (venc->enable & VENC_ENABLE_MASK)
1070 if (inst->state == VPU_CODEC_STATE_DEINIT)
1073 if (inst->state != VPU_CODEC_STATE_STOP)
1074 venc_request_eos(inst);
1076 call_void_vop(inst, wait_prepare);
1077 if (!wait_event_timeout(venc->wq, venc->stopped, VPU_TIMEOUT)) {
1078 set_bit(inst->id, &inst->core->hang_mask);
1079 vpu_session_debug(inst);
1081 call_void_vop(inst, wait_finish);
1083 inst->state = VPU_CODEC_STATE_DEINIT;
1084 venc_cleanup_frames(inst->priv);
1085 vpu_free_dma(&inst->stream_buffer);
1086 venc_cleanup_mem_resource(inst);
1091 static int venc_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
1093 struct venc_t *venc = inst->priv;
1094 struct vb2_v4l2_buffer *vbuf;
1097 if (inst->state == VPU_CODEC_STATE_DEINIT)
1100 vbuf = to_vb2_v4l2_buffer(vb);
1101 if (inst->state == VPU_CODEC_STATE_STARTED)
1102 inst->state = VPU_CODEC_STATE_ACTIVE;
1104 flags = vbuf->flags;
1105 if (venc->request_key_frame) {
1106 vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1107 venc->request_key_frame = 0;
1109 if (venc->bitrate_change) {
1110 vpu_session_update_parameters(inst, &venc->params);
1111 venc->bitrate_change = false;
1113 dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp);
1114 vpu_iface_input_frame(inst, vb);
1115 vbuf->flags = flags;
1116 venc->input_ready = false;
1117 venc->frame_count++;
1118 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1123 static int venc_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
1125 struct venc_t *venc;
1126 struct venc_frame_t *frame = NULL;
1127 struct vb2_v4l2_buffer *vbuf;
1131 if (list_empty(&venc->frames))
1134 frame = list_first_entry(&venc->frames, struct venc_frame_t, list);
1135 vbuf = to_vb2_v4l2_buffer(vb);
1136 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1137 ret = venc_get_one_encoded_frame(inst, frame, vbuf);
1141 list_del_init(&frame->list);
1146 static void venc_on_queue_empty(struct vpu_inst *inst, u32 type)
1148 struct venc_t *venc = inst->priv;
1150 if (V4L2_TYPE_IS_OUTPUT(type))
1154 venc_set_last_buffer_dequeued(inst);
1157 static int venc_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
1159 struct venc_t *venc = inst->priv;
1164 num = scnprintf(str, size, "profile = %d\n", venc->params.profile);
1167 num = scnprintf(str, size, "level = %d\n", venc->params.level);
1170 num = scnprintf(str, size, "fps = %d/%d\n",
1171 venc->params.frame_rate.numerator,
1172 venc->params.frame_rate.denominator);
1175 num = scnprintf(str, size, "%d x %d -> %d x %d\n",
1176 venc->params.src_width,
1177 venc->params.src_height,
1178 venc->params.out_width,
1179 venc->params.out_height);
1182 num = scnprintf(str, size, "(%d, %d) %d x %d\n",
1183 venc->params.crop.left,
1184 venc->params.crop.top,
1185 venc->params.crop.width,
1186 venc->params.crop.height);
1189 num = scnprintf(str, size,
1190 "enable = 0x%x, input = %d, encode = %d, ready = %d, stopped = %d\n",
1192 venc->frame_count, venc->encode_count,
1197 num = scnprintf(str, size, "gop = %d\n", venc->params.gop_length);
1200 num = scnprintf(str, size, "bframes = %d\n", venc->params.bframes);
1203 num = scnprintf(str, size, "rc: %s, mode = %d, bitrate = %d(%d), qp = %d\n",
1204 venc->params.rc_enable ? "enable" : "disable",
1205 venc->params.rc_mode,
1206 venc->params.bitrate,
1207 venc->params.bitrate_max,
1208 venc->params.i_frame_qp);
1211 num = scnprintf(str, size, "sar: enable = %d, idc = %d, %d x %d\n",
1212 venc->params.sar.enable,
1213 venc->params.sar.idc,
1214 venc->params.sar.width,
1215 venc->params.sar.height);
1219 num = scnprintf(str, size,
1220 "colorspace: primaries = %d, transfer = %d, matrix = %d, full_range = %d\n",
1221 venc->params.color.primaries,
1222 venc->params.color.transfer,
1223 venc->params.color.matrix,
1224 venc->params.color.full_range);
1227 num = scnprintf(str, size, "skipped: count = %d, bytes = %d\n",
1228 venc->skipped_count, venc->skipped_bytes);
1237 static struct vpu_inst_ops venc_inst_ops = {
1238 .ctrl_init = venc_ctrl_init,
1239 .check_ready = venc_check_ready,
1240 .input_done = venc_input_done,
1241 .get_one_frame = venc_frame_encoded,
1242 .stop_done = venc_stop_done,
1243 .event_notify = venc_event_notify,
1244 .release = venc_release,
1245 .cleanup = venc_cleanup,
1246 .start = venc_start_session,
1247 .mem_request = venc_request_mem_resource,
1248 .stop = venc_stop_session,
1249 .process_output = venc_process_output,
1250 .process_capture = venc_process_capture,
1251 .on_queue_empty = venc_on_queue_empty,
1252 .get_debug_info = venc_get_debug_info,
1253 .wait_prepare = vpu_inst_unlock,
1254 .wait_finish = vpu_inst_lock,
1257 static void venc_init(struct file *file)
1259 struct vpu_inst *inst = to_inst(file);
1260 struct venc_t *venc;
1261 struct v4l2_format f;
1262 struct v4l2_streamparm parm;
1265 venc->params.qp_min = 1;
1266 venc->params.qp_max = 51;
1267 venc->params.qp_min_i = 1;
1268 venc->params.qp_max_i = 51;
1269 venc->params.bitrate_min = BITRATE_MIN;
1271 memset(&f, 0, sizeof(f));
1272 f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1273 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M;
1274 f.fmt.pix_mp.width = 1280;
1275 f.fmt.pix_mp.height = 720;
1276 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1277 venc_s_fmt(file, &inst->fh, &f);
1279 memset(&f, 0, sizeof(f));
1280 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1281 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1282 f.fmt.pix_mp.width = 1280;
1283 f.fmt.pix_mp.height = 720;
1284 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1285 venc_s_fmt(file, &inst->fh, &f);
1287 memset(&parm, 0, sizeof(parm));
1288 parm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1289 parm.parm.capture.timeperframe.numerator = 1;
1290 parm.parm.capture.timeperframe.denominator = 30;
1291 venc_s_parm(file, &inst->fh, &parm);
1294 static int venc_open(struct file *file)
1296 struct vpu_inst *inst;
1297 struct venc_t *venc;
1300 inst = vzalloc(sizeof(*inst));
1304 venc = vzalloc(sizeof(*venc));
1310 inst->ops = &venc_inst_ops;
1311 inst->formats = venc_formats;
1312 inst->type = VPU_CORE_TYPE_ENC;
1314 INIT_LIST_HEAD(&venc->frames);
1315 init_waitqueue_head(&venc->wq);
1317 ret = vpu_v4l2_open(file, inst);
1321 inst->min_buffer_out = VENC_MIN_BUFFER_OUT;
1322 inst->min_buffer_cap = VENC_MIN_BUFFER_CAP;
1328 static const struct v4l2_file_operations venc_fops = {
1329 .owner = THIS_MODULE,
1331 .release = vpu_v4l2_close,
1332 .unlocked_ioctl = video_ioctl2,
1333 .poll = v4l2_m2m_fop_poll,
1334 .mmap = v4l2_m2m_fop_mmap,
1337 const struct v4l2_ioctl_ops *venc_get_ioctl_ops(void)
1339 return &venc_ioctl_ops;
1342 const struct v4l2_file_operations *venc_get_fops(void)