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,
73 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
76 .pixfmt = V4L2_PIX_FMT_H264,
78 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
83 static int venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
85 strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
86 strscpy(cap->card, "amphion vpu encoder", sizeof(cap->card));
87 strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
92 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
94 struct vpu_inst *inst = to_inst(file);
95 const struct vpu_format *fmt;
97 memset(f->reserved, 0, sizeof(f->reserved));
98 fmt = vpu_helper_enum_format(inst, f->type, f->index);
102 f->pixelformat = fmt->pixfmt;
103 f->flags = fmt->flags;
108 static int venc_enum_framesizes(struct file *file, void *fh, struct v4l2_frmsizeenum *fsize)
110 struct vpu_inst *inst = to_inst(file);
111 const struct vpu_core_resources *res;
113 if (!fsize || fsize->index)
116 if (!vpu_helper_find_format(inst, 0, fsize->pixel_format))
119 res = vpu_get_resource(inst);
122 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
123 fsize->stepwise.max_width = res->max_width;
124 fsize->stepwise.max_height = res->max_height;
125 fsize->stepwise.min_width = res->min_width;
126 fsize->stepwise.min_height = res->min_height;
127 fsize->stepwise.step_width = res->step_width;
128 fsize->stepwise.step_height = res->step_height;
133 static int venc_enum_frameintervals(struct file *file, void *fh, struct v4l2_frmivalenum *fival)
135 struct vpu_inst *inst = to_inst(file);
136 const struct vpu_core_resources *res;
138 if (!fival || fival->index)
141 if (!vpu_helper_find_format(inst, 0, fival->pixel_format))
144 if (!fival->width || !fival->height)
147 res = vpu_get_resource(inst);
150 if (fival->width < res->min_width || fival->width > res->max_width ||
151 fival->height < res->min_height || fival->height > res->max_height)
154 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
155 fival->stepwise.min.numerator = 1;
156 fival->stepwise.min.denominator = USHRT_MAX;
157 fival->stepwise.max.numerator = USHRT_MAX;
158 fival->stepwise.max.denominator = 1;
159 fival->stepwise.step.numerator = 1;
160 fival->stepwise.step.denominator = 1;
165 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
167 struct vpu_inst *inst = to_inst(file);
168 struct venc_t *venc = inst->priv;
169 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
170 struct vpu_format *cur_fmt;
173 cur_fmt = vpu_get_format(inst, f->type);
175 pixmp->pixelformat = cur_fmt->pixfmt;
176 pixmp->num_planes = cur_fmt->num_planes;
177 pixmp->width = cur_fmt->width;
178 pixmp->height = cur_fmt->height;
179 pixmp->field = cur_fmt->field;
180 pixmp->flags = cur_fmt->flags;
181 for (i = 0; i < pixmp->num_planes; i++) {
182 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
183 pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i];
186 f->fmt.pix_mp.colorspace = venc->params.color.primaries;
187 f->fmt.pix_mp.xfer_func = venc->params.color.transfer;
188 f->fmt.pix_mp.ycbcr_enc = venc->params.color.matrix;
189 f->fmt.pix_mp.quantization = venc->params.color.full_range;
194 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
196 struct vpu_inst *inst = to_inst(file);
198 vpu_try_fmt_common(inst, f);
203 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
205 struct vpu_inst *inst = to_inst(file);
206 const struct vpu_format *fmt;
207 struct vpu_format *cur_fmt;
209 struct venc_t *venc = inst->priv;
210 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
213 q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
219 fmt = vpu_try_fmt_common(inst, f);
223 cur_fmt = vpu_get_format(inst, f->type);
225 cur_fmt->pixfmt = fmt->pixfmt;
226 cur_fmt->num_planes = fmt->num_planes;
227 cur_fmt->flags = fmt->flags;
228 cur_fmt->width = pix_mp->width;
229 cur_fmt->height = pix_mp->height;
230 for (i = 0; i < fmt->num_planes; i++) {
231 cur_fmt->sizeimage[i] = pix_mp->plane_fmt[i].sizeimage;
232 cur_fmt->bytesperline[i] = pix_mp->plane_fmt[i].bytesperline;
235 if (pix_mp->field != V4L2_FIELD_ANY)
236 cur_fmt->field = pix_mp->field;
238 if (V4L2_TYPE_IS_OUTPUT(f->type)) {
239 venc->params.input_format = cur_fmt->pixfmt;
240 venc->params.src_stride = cur_fmt->bytesperline[0];
241 venc->params.src_width = cur_fmt->width;
242 venc->params.src_height = cur_fmt->height;
243 venc->params.crop.left = 0;
244 venc->params.crop.top = 0;
245 venc->params.crop.width = cur_fmt->width;
246 venc->params.crop.height = cur_fmt->height;
248 venc->params.codec_format = cur_fmt->pixfmt;
249 venc->params.out_width = cur_fmt->width;
250 venc->params.out_height = cur_fmt->height;
253 if (V4L2_TYPE_IS_OUTPUT(f->type)) {
254 if (!vpu_color_check_primaries(pix_mp->colorspace)) {
255 venc->params.color.primaries = pix_mp->colorspace;
256 vpu_color_get_default(venc->params.color.primaries,
257 &venc->params.color.transfer,
258 &venc->params.color.matrix,
259 &venc->params.color.full_range);
261 if (!vpu_color_check_transfers(pix_mp->xfer_func))
262 venc->params.color.transfer = pix_mp->xfer_func;
263 if (!vpu_color_check_matrix(pix_mp->ycbcr_enc))
264 venc->params.color.matrix = pix_mp->ycbcr_enc;
265 if (!vpu_color_check_full_range(pix_mp->quantization))
266 venc->params.color.full_range = pix_mp->quantization;
269 pix_mp->colorspace = venc->params.color.primaries;
270 pix_mp->xfer_func = venc->params.color.transfer;
271 pix_mp->ycbcr_enc = venc->params.color.matrix;
272 pix_mp->quantization = venc->params.color.full_range;
277 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
279 struct vpu_inst *inst = to_inst(file);
280 struct venc_t *venc = inst->priv;
281 struct v4l2_fract *timeperframe = &parm->parm.capture.timeperframe;
286 if (!V4L2_TYPE_IS_OUTPUT(parm->type))
289 if (!vpu_helper_check_type(inst, parm->type))
292 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
293 parm->parm.capture.readbuffers = 0;
294 timeperframe->numerator = venc->params.frame_rate.numerator;
295 timeperframe->denominator = venc->params.frame_rate.denominator;
300 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *parm)
302 struct vpu_inst *inst = to_inst(file);
303 struct venc_t *venc = inst->priv;
304 struct v4l2_fract *timeperframe = &parm->parm.capture.timeperframe;
310 if (!V4L2_TYPE_IS_OUTPUT(parm->type))
313 if (!vpu_helper_check_type(inst, parm->type))
316 if (!timeperframe->numerator)
317 timeperframe->numerator = venc->params.frame_rate.numerator;
318 if (!timeperframe->denominator)
319 timeperframe->denominator = venc->params.frame_rate.denominator;
321 venc->params.frame_rate.numerator = timeperframe->numerator;
322 venc->params.frame_rate.denominator = timeperframe->denominator;
324 rational_best_approximation(venc->params.frame_rate.numerator,
325 venc->params.frame_rate.denominator,
326 venc->params.frame_rate.numerator,
327 venc->params.frame_rate.denominator,
329 venc->params.frame_rate.numerator = n;
330 venc->params.frame_rate.denominator = d;
332 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
333 memset(parm->parm.capture.reserved, 0, sizeof(parm->parm.capture.reserved));
338 static int venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
340 struct vpu_inst *inst = to_inst(file);
341 struct venc_t *venc = inst->priv;
343 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
347 case V4L2_SEL_TGT_CROP_DEFAULT:
348 case V4L2_SEL_TGT_CROP_BOUNDS:
351 s->r.width = inst->out_format.width;
352 s->r.height = inst->out_format.height;
354 case V4L2_SEL_TGT_CROP:
355 s->r = venc->params.crop;
364 static int venc_valid_crop(struct venc_t *venc, const struct vpu_core_resources *res)
366 struct v4l2_rect *rect = NULL;
372 rect = &venc->params.crop;
373 min_width = res->min_width;
374 min_height = res->min_height;
375 src_width = venc->params.src_width;
376 src_height = venc->params.src_height;
378 if (rect->width == 0 || rect->height == 0)
380 if (rect->left > src_width - min_width || rect->top > src_height - min_height)
383 rect->width = min(rect->width, src_width - rect->left);
384 rect->width = max_t(u32, rect->width, min_width);
386 rect->height = min(rect->height, src_height - rect->top);
387 rect->height = max_t(u32, rect->height, min_height);
392 static int venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
394 struct vpu_inst *inst = to_inst(file);
395 const struct vpu_core_resources *res;
396 struct venc_t *venc = inst->priv;
398 res = vpu_get_resource(inst);
402 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
404 if (s->target != V4L2_SEL_TGT_CROP)
407 venc->params.crop.left = ALIGN(s->r.left, res->step_width);
408 venc->params.crop.top = ALIGN(s->r.top, res->step_height);
409 venc->params.crop.width = ALIGN(s->r.width, res->step_width);
410 venc->params.crop.height = ALIGN(s->r.height, res->step_height);
411 if (venc_valid_crop(venc, res)) {
412 venc->params.crop.left = 0;
413 venc->params.crop.top = 0;
414 venc->params.crop.width = venc->params.src_width;
415 venc->params.crop.height = venc->params.src_height;
418 inst->crop = venc->params.crop;
423 static int venc_drain(struct vpu_inst *inst)
425 struct venc_t *venc = inst->priv;
428 if (!inst->fh.m2m_ctx)
431 if (inst->state != VPU_CODEC_STATE_DRAIN)
434 if (!vpu_is_source_empty(inst))
437 if (!venc->input_ready)
440 venc->input_ready = false;
441 vpu_trace(inst->dev, "[%d]\n", inst->id);
442 ret = vpu_session_stop(inst);
445 inst->state = VPU_CODEC_STATE_STOP;
446 wake_up_all(&venc->wq);
451 static int venc_request_eos(struct vpu_inst *inst)
453 inst->state = VPU_CODEC_STATE_DRAIN;
459 static int venc_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *cmd)
461 struct vpu_inst *inst = to_inst(file);
464 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
469 if (cmd->cmd == V4L2_ENC_CMD_STOP) {
470 if (inst->state == VPU_CODEC_STATE_DEINIT)
471 vpu_set_last_buffer_dequeued(inst);
473 venc_request_eos(inst);
475 vpu_inst_unlock(inst);
480 static int venc_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
484 return v4l2_event_subscribe(fh, sub, 0, NULL);
485 case V4L2_EVENT_CTRL:
486 return v4l2_ctrl_subscribe_event(fh, sub);
492 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
493 .vidioc_querycap = venc_querycap,
494 .vidioc_enum_fmt_vid_cap = venc_enum_fmt,
495 .vidioc_enum_fmt_vid_out = venc_enum_fmt,
496 .vidioc_enum_framesizes = venc_enum_framesizes,
497 .vidioc_enum_frameintervals = venc_enum_frameintervals,
498 .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
499 .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
500 .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
501 .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
502 .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
503 .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
504 .vidioc_g_parm = venc_g_parm,
505 .vidioc_s_parm = venc_s_parm,
506 .vidioc_g_selection = venc_g_selection,
507 .vidioc_s_selection = venc_s_selection,
508 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
509 .vidioc_encoder_cmd = venc_encoder_cmd,
510 .vidioc_subscribe_event = venc_subscribe_event,
511 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
512 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
513 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
514 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
515 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
516 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
517 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
518 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
519 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
520 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
523 static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
525 struct vpu_inst *inst = ctrl_to_inst(ctrl);
526 struct venc_t *venc = inst->priv;
531 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
532 venc->params.profile = ctrl->val;
534 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
535 venc->params.level = ctrl->val;
537 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
538 venc->params.rc_enable = ctrl->val;
540 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
541 venc->params.rc_mode = ctrl->val;
543 case V4L2_CID_MPEG_VIDEO_BITRATE:
544 if (ctrl->val != venc->params.bitrate)
545 venc->bitrate_change = true;
546 venc->params.bitrate = ctrl->val;
548 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
549 venc->params.bitrate_max = ctrl->val;
551 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
552 venc->params.gop_length = ctrl->val;
554 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
555 venc->params.bframes = ctrl->val;
557 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
558 venc->params.i_frame_qp = ctrl->val;
560 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
561 venc->params.p_frame_qp = ctrl->val;
563 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
564 venc->params.b_frame_qp = ctrl->val;
566 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
567 venc->request_key_frame = 1;
569 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
570 venc->cpb_size = ctrl->val * 1024;
572 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
573 venc->params.sar.enable = ctrl->val;
575 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
576 venc->params.sar.idc = ctrl->val;
578 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH:
579 venc->params.sar.width = ctrl->val;
581 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT:
582 venc->params.sar.height = ctrl->val;
584 case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
590 vpu_inst_unlock(inst);
595 static const struct v4l2_ctrl_ops venc_ctrl_ops = {
596 .s_ctrl = venc_op_s_ctrl,
597 .g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
600 static int venc_ctrl_init(struct vpu_inst *inst)
602 struct v4l2_ctrl *ctrl;
605 ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
609 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
610 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
611 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
612 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
613 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
614 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
615 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
617 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
618 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
619 V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
621 V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
623 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
624 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
626 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
627 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
628 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
629 ~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) |
630 (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)),
631 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
633 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
634 V4L2_CID_MPEG_VIDEO_BITRATE,
640 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
641 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
642 BITRATE_MIN, BITRATE_MAX,
644 BITRATE_DEFAULT_PEAK);
646 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
647 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, (1 << 16) - 1, 1, 30);
649 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
650 V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0);
652 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
653 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26);
654 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
655 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28);
656 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
657 V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30);
658 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
659 V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0);
660 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
661 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
663 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
664 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
665 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
667 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
669 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
670 V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE, 64, 10240, 1, 1024);
672 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
673 V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE, 0, 1, 1, 1);
674 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
675 V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
676 V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED,
678 V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
679 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
680 V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH,
682 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
683 V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT,
685 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
686 V4L2_CID_MPEG_VIDEO_HEADER_MODE,
687 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
688 ~(1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME),
689 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
691 if (inst->ctrl_handler.error) {
692 ret = inst->ctrl_handler.error;
693 v4l2_ctrl_handler_free(&inst->ctrl_handler);
697 ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
699 dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
700 v4l2_ctrl_handler_free(&inst->ctrl_handler);
707 static bool venc_check_ready(struct vpu_inst *inst, unsigned int type)
709 struct venc_t *venc = inst->priv;
711 if (V4L2_TYPE_IS_OUTPUT(type)) {
712 if (vpu_helper_get_free_space(inst) < venc->cpb_size)
714 return venc->input_ready;
717 if (list_empty(&venc->frames))
722 static u32 venc_get_enable_mask(u32 type)
724 if (V4L2_TYPE_IS_OUTPUT(type))
725 return VENC_OUTPUT_ENABLE;
727 return VENC_CAPTURE_ENABLE;
730 static void venc_set_enable(struct venc_t *venc, u32 type, int enable)
732 u32 mask = venc_get_enable_mask(type);
735 venc->enable |= mask;
737 venc->enable &= ~mask;
740 static u32 venc_get_enable(struct venc_t *venc, u32 type)
742 return venc->enable & venc_get_enable_mask(type);
745 static void venc_input_done(struct vpu_inst *inst)
747 struct venc_t *venc = inst->priv;
750 venc->input_ready = true;
751 vpu_process_output_buffer(inst);
752 if (inst->state == VPU_CODEC_STATE_DRAIN)
754 vpu_inst_unlock(inst);
758 * It's hardware limitation, that there may be several bytes
759 * redundant data at the beginning of frame.
760 * For android platform, the redundant data may cause cts test fail
761 * So driver will strip them
763 static int venc_precheck_encoded_frame(struct vpu_inst *inst, struct venc_frame_t *frame)
768 if (!frame || !frame->bytesused)
772 skipped = vpu_helper_find_startcode(&inst->stream_buffer,
773 inst->cap_format.pixfmt,
774 frame->info.wptr - inst->stream_buffer.phys,
777 frame->bytesused -= skipped;
778 frame->info.wptr = vpu_helper_step_walk(&inst->stream_buffer,
779 frame->info.wptr, skipped);
780 venc->skipped_bytes += skipped;
781 venc->skipped_count++;
787 static int venc_get_one_encoded_frame(struct vpu_inst *inst,
788 struct venc_frame_t *frame,
789 struct vb2_v4l2_buffer *vbuf)
791 struct venc_t *venc = inst->priv;
792 struct vb2_v4l2_buffer *src_buf;
797 src_buf = vpu_find_buf_by_sequence(inst, inst->out_format.type, frame->info.frame_id);
799 v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true);
800 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
801 v4l2_m2m_src_buf_remove_by_buf(inst->fh.m2m_ctx, src_buf);
802 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
804 vbuf->vb2_buf.timestamp = frame->info.timestamp;
806 if (!venc_get_enable(inst->priv, vbuf->vb2_buf.type)) {
807 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
810 if (frame->bytesused > vbuf->vb2_buf.planes[0].length) {
811 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
815 venc_precheck_encoded_frame(inst, frame);
817 if (frame->bytesused) {
818 u32 rptr = frame->info.wptr;
819 void *dst = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
821 vpu_helper_copy_from_stream_buffer(&inst->stream_buffer,
822 &rptr, frame->bytesused, dst);
823 vpu_iface_update_stream_buffer(inst, rptr, 0);
825 vb2_set_plane_payload(&vbuf->vb2_buf, 0, frame->bytesused);
826 vbuf->sequence = frame->info.frame_id;
827 vbuf->field = inst->cap_format.field;
828 vbuf->flags |= frame->info.pic_type;
829 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
830 dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
831 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
834 if (vbuf->flags & V4L2_BUF_FLAG_KEYFRAME)
835 dev_dbg(inst->dev, "[%d][%d]key frame\n", inst->id, frame->info.frame_id);
840 static int venc_get_encoded_frames(struct vpu_inst *inst)
843 struct venc_frame_t *frame;
844 struct venc_frame_t *tmp;
846 if (!inst->fh.m2m_ctx)
849 list_for_each_entry_safe(frame, tmp, &venc->frames, list) {
850 if (venc_get_one_encoded_frame(inst, frame,
851 v4l2_m2m_dst_buf_remove(inst->fh.m2m_ctx)))
853 list_del_init(&frame->list);
860 static int venc_frame_encoded(struct vpu_inst *inst, void *arg)
862 struct vpu_enc_pic_info *info = arg;
863 struct venc_frame_t *frame;
870 frame = vzalloc(sizeof(*frame));
874 memcpy(&frame->info, info, sizeof(frame->info));
875 frame->bytesused = info->frame_size;
878 list_add_tail(&frame->list, &venc->frames);
879 venc->encode_count++;
880 venc_get_encoded_frames(inst);
881 vpu_inst_unlock(inst);
886 static void venc_set_last_buffer_dequeued(struct vpu_inst *inst)
888 struct venc_t *venc = inst->priv;
890 if (venc->stopped && list_empty(&venc->frames))
891 vpu_set_last_buffer_dequeued(inst);
894 static void venc_stop_done(struct vpu_inst *inst)
896 struct venc_t *venc = inst->priv;
899 venc->stopped = true;
900 venc_set_last_buffer_dequeued(inst);
901 vpu_inst_unlock(inst);
903 wake_up_all(&venc->wq);
906 static void venc_event_notify(struct vpu_inst *inst, u32 event, void *data)
910 static void venc_release(struct vpu_inst *inst)
914 static void venc_cleanup(struct vpu_inst *inst)
928 static int venc_start_session(struct vpu_inst *inst, u32 type)
930 struct venc_t *venc = inst->priv;
931 int stream_buffer_size;
934 venc_set_enable(venc, type, 1);
935 if ((venc->enable & VENC_ENABLE_MASK) != VENC_ENABLE_MASK)
938 vpu_iface_init_instance(inst);
939 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
940 if (stream_buffer_size > 0) {
941 inst->stream_buffer.length = max_t(u32, stream_buffer_size, venc->cpb_size * 3);
942 ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
946 inst->use_stream_buffer = true;
947 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
950 ret = vpu_iface_set_encode_params(inst, &venc->params, 0);
953 ret = vpu_session_configure_codec(inst);
957 inst->state = VPU_CODEC_STATE_CONFIGURED;
958 /*vpu_iface_config_memory_resource*/
960 /*config enc expert mode parameter*/
961 ret = vpu_iface_set_encode_params(inst, &venc->params, 1);
965 ret = vpu_session_start(inst);
968 inst->state = VPU_CODEC_STATE_STARTED;
970 venc->bitrate_change = false;
971 venc->input_ready = true;
972 venc->frame_count = 0;
973 venc->encode_count = 0;
974 venc->ready_count = 0;
975 venc->stopped = false;
976 vpu_process_output_buffer(inst);
977 if (venc->frame_count == 0)
978 dev_err(inst->dev, "[%d] there is no input when starting\n", inst->id);
982 venc_set_enable(venc, type, 0);
983 inst->state = VPU_CODEC_STATE_DEINIT;
985 vpu_free_dma(&inst->stream_buffer);
989 static void venc_cleanup_mem_resource(struct vpu_inst *inst)
996 for (i = 0; i < ARRAY_SIZE(venc->enc); i++)
997 vpu_free_dma(&venc->enc[i]);
998 for (i = 0; i < ARRAY_SIZE(venc->ref); i++)
999 vpu_free_dma(&venc->ref[i]);
1002 static void venc_request_mem_resource(struct vpu_inst *inst,
1010 struct venc_t *venc;
1015 if (enc_frame_num > ARRAY_SIZE(venc->enc)) {
1016 dev_err(inst->dev, "[%d] enc num(%d) is out of range\n", inst->id, enc_frame_num);
1019 if (ref_frame_num > ARRAY_SIZE(venc->ref)) {
1020 dev_err(inst->dev, "[%d] ref num(%d) is out of range\n", inst->id, ref_frame_num);
1023 if (act_frame_num > ARRAY_SIZE(venc->act)) {
1024 dev_err(inst->dev, "[%d] act num(%d) is out of range\n", inst->id, act_frame_num);
1028 for (i = 0; i < enc_frame_num; i++) {
1029 venc->enc[i].length = enc_frame_size;
1030 ret = vpu_alloc_dma(inst->core, &venc->enc[i]);
1032 venc_cleanup_mem_resource(inst);
1036 for (i = 0; i < ref_frame_num; i++) {
1037 venc->ref[i].length = ref_frame_size;
1038 ret = vpu_alloc_dma(inst->core, &venc->ref[i]);
1040 venc_cleanup_mem_resource(inst);
1044 if (act_frame_num != 1 || act_frame_size > inst->act.length) {
1045 venc_cleanup_mem_resource(inst);
1048 venc->act[0].length = act_frame_size;
1049 venc->act[0].phys = inst->act.phys;
1050 venc->act[0].virt = inst->act.virt;
1052 for (i = 0; i < enc_frame_num; i++)
1053 vpu_iface_config_memory_resource(inst, MEM_RES_ENC, i, &venc->enc[i]);
1054 for (i = 0; i < ref_frame_num; i++)
1055 vpu_iface_config_memory_resource(inst, MEM_RES_REF, i, &venc->ref[i]);
1056 for (i = 0; i < act_frame_num; i++)
1057 vpu_iface_config_memory_resource(inst, MEM_RES_ACT, i, &venc->act[i]);
1060 static void venc_cleanup_frames(struct venc_t *venc)
1062 struct venc_frame_t *frame;
1063 struct venc_frame_t *tmp;
1065 list_for_each_entry_safe(frame, tmp, &venc->frames, list) {
1066 list_del_init(&frame->list);
1071 static int venc_stop_session(struct vpu_inst *inst, u32 type)
1073 struct venc_t *venc = inst->priv;
1075 venc_set_enable(venc, type, 0);
1076 if (venc->enable & VENC_ENABLE_MASK)
1079 if (inst->state == VPU_CODEC_STATE_DEINIT)
1082 if (inst->state != VPU_CODEC_STATE_STOP)
1083 venc_request_eos(inst);
1085 call_void_vop(inst, wait_prepare);
1086 if (!wait_event_timeout(venc->wq, venc->stopped, VPU_TIMEOUT)) {
1087 set_bit(inst->id, &inst->core->hang_mask);
1088 vpu_session_debug(inst);
1090 call_void_vop(inst, wait_finish);
1092 inst->state = VPU_CODEC_STATE_DEINIT;
1093 venc_cleanup_frames(inst->priv);
1094 vpu_free_dma(&inst->stream_buffer);
1095 venc_cleanup_mem_resource(inst);
1100 static int venc_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
1102 struct venc_t *venc = inst->priv;
1103 struct vb2_v4l2_buffer *vbuf;
1106 if (inst->state == VPU_CODEC_STATE_DEINIT)
1109 vbuf = to_vb2_v4l2_buffer(vb);
1110 if (inst->state == VPU_CODEC_STATE_STARTED)
1111 inst->state = VPU_CODEC_STATE_ACTIVE;
1113 flags = vbuf->flags;
1114 if (venc->request_key_frame) {
1115 vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1116 venc->request_key_frame = 0;
1118 if (venc->bitrate_change) {
1119 vpu_session_update_parameters(inst, &venc->params);
1120 venc->bitrate_change = false;
1122 dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp);
1123 vpu_iface_input_frame(inst, vb);
1124 vbuf->flags = flags;
1125 venc->input_ready = false;
1126 venc->frame_count++;
1127 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1132 static int venc_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
1134 struct venc_t *venc;
1135 struct venc_frame_t *frame = NULL;
1136 struct vb2_v4l2_buffer *vbuf;
1140 if (list_empty(&venc->frames))
1143 frame = list_first_entry(&venc->frames, struct venc_frame_t, list);
1144 vbuf = to_vb2_v4l2_buffer(vb);
1145 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1146 ret = venc_get_one_encoded_frame(inst, frame, vbuf);
1150 list_del_init(&frame->list);
1155 static void venc_on_queue_empty(struct vpu_inst *inst, u32 type)
1157 struct venc_t *venc = inst->priv;
1159 if (V4L2_TYPE_IS_OUTPUT(type))
1163 venc_set_last_buffer_dequeued(inst);
1166 static int venc_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
1168 struct venc_t *venc = inst->priv;
1173 num = scnprintf(str, size, "profile = %d\n", venc->params.profile);
1176 num = scnprintf(str, size, "level = %d\n", venc->params.level);
1179 num = scnprintf(str, size, "fps = %d/%d\n",
1180 venc->params.frame_rate.numerator,
1181 venc->params.frame_rate.denominator);
1184 num = scnprintf(str, size, "%d x %d -> %d x %d\n",
1185 venc->params.src_width,
1186 venc->params.src_height,
1187 venc->params.out_width,
1188 venc->params.out_height);
1191 num = scnprintf(str, size, "(%d, %d) %d x %d\n",
1192 venc->params.crop.left,
1193 venc->params.crop.top,
1194 venc->params.crop.width,
1195 venc->params.crop.height);
1198 num = scnprintf(str, size,
1199 "enable = 0x%x, input = %d, encode = %d, ready = %d, stopped = %d\n",
1201 venc->frame_count, venc->encode_count,
1206 num = scnprintf(str, size, "gop = %d\n", venc->params.gop_length);
1209 num = scnprintf(str, size, "bframes = %d\n", venc->params.bframes);
1212 num = scnprintf(str, size, "rc: %s, mode = %d, bitrate = %d(%d), qp = %d\n",
1213 venc->params.rc_enable ? "enable" : "disable",
1214 venc->params.rc_mode,
1215 venc->params.bitrate,
1216 venc->params.bitrate_max,
1217 venc->params.i_frame_qp);
1220 num = scnprintf(str, size, "sar: enable = %d, idc = %d, %d x %d\n",
1221 venc->params.sar.enable,
1222 venc->params.sar.idc,
1223 venc->params.sar.width,
1224 venc->params.sar.height);
1228 num = scnprintf(str, size,
1229 "colorspace: primaries = %d, transfer = %d, matrix = %d, full_range = %d\n",
1230 venc->params.color.primaries,
1231 venc->params.color.transfer,
1232 venc->params.color.matrix,
1233 venc->params.color.full_range);
1236 num = scnprintf(str, size, "skipped: count = %d, bytes = %d\n",
1237 venc->skipped_count, venc->skipped_bytes);
1246 static struct vpu_inst_ops venc_inst_ops = {
1247 .ctrl_init = venc_ctrl_init,
1248 .check_ready = venc_check_ready,
1249 .input_done = venc_input_done,
1250 .get_one_frame = venc_frame_encoded,
1251 .stop_done = venc_stop_done,
1252 .event_notify = venc_event_notify,
1253 .release = venc_release,
1254 .cleanup = venc_cleanup,
1255 .start = venc_start_session,
1256 .mem_request = venc_request_mem_resource,
1257 .stop = venc_stop_session,
1258 .process_output = venc_process_output,
1259 .process_capture = venc_process_capture,
1260 .on_queue_empty = venc_on_queue_empty,
1261 .get_debug_info = venc_get_debug_info,
1262 .wait_prepare = vpu_inst_unlock,
1263 .wait_finish = vpu_inst_lock,
1266 static void venc_init(struct file *file)
1268 struct vpu_inst *inst = to_inst(file);
1269 struct venc_t *venc;
1270 struct v4l2_format f;
1271 struct v4l2_streamparm parm;
1274 venc->params.qp_min = 1;
1275 venc->params.qp_max = 51;
1276 venc->params.qp_min_i = 1;
1277 venc->params.qp_max_i = 51;
1278 venc->params.bitrate_min = BITRATE_MIN;
1280 memset(&f, 0, sizeof(f));
1281 f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1282 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M;
1283 f.fmt.pix_mp.width = 1280;
1284 f.fmt.pix_mp.height = 720;
1285 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1286 f.fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
1287 venc_s_fmt(file, &inst->fh, &f);
1289 memset(&f, 0, sizeof(f));
1290 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1291 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1292 f.fmt.pix_mp.width = 1280;
1293 f.fmt.pix_mp.height = 720;
1294 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1295 venc_s_fmt(file, &inst->fh, &f);
1297 memset(&parm, 0, sizeof(parm));
1298 parm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1299 parm.parm.capture.timeperframe.numerator = 1;
1300 parm.parm.capture.timeperframe.denominator = 30;
1301 venc_s_parm(file, &inst->fh, &parm);
1304 static int venc_open(struct file *file)
1306 struct vpu_inst *inst;
1307 struct venc_t *venc;
1310 inst = vzalloc(sizeof(*inst));
1314 venc = vzalloc(sizeof(*venc));
1320 inst->ops = &venc_inst_ops;
1321 inst->formats = venc_formats;
1322 inst->type = VPU_CORE_TYPE_ENC;
1324 INIT_LIST_HEAD(&venc->frames);
1325 init_waitqueue_head(&venc->wq);
1327 ret = vpu_v4l2_open(file, inst);
1331 inst->min_buffer_out = VENC_MIN_BUFFER_OUT;
1332 inst->min_buffer_cap = VENC_MIN_BUFFER_CAP;
1338 static const struct v4l2_file_operations venc_fops = {
1339 .owner = THIS_MODULE,
1341 .release = vpu_v4l2_close,
1342 .unlocked_ioctl = video_ioctl2,
1343 .poll = v4l2_m2m_fop_poll,
1344 .mmap = v4l2_m2m_fop_mmap,
1347 const struct v4l2_ioctl_ops *venc_get_ioctl_ops(void)
1349 return &venc_ioctl_ops;
1352 const struct v4l2_file_operations *venc_get_fops(void)