2 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2017 Linaro Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <media/v4l2-mem2mem.h>
21 #include <media/videobuf2-dma-sg.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/v4l2-event.h>
24 #include <media/v4l2-ctrls.h>
26 #include "hfi_venus_io.h"
31 #define NUM_B_FRAMES_MAX 4
33 static u32 get_framesize_uncompressed(unsigned int plane, u32 width, u32 height)
35 u32 y_stride, uv_stride, y_plane;
36 u32 y_sclines, uv_sclines, uv_plane;
39 y_stride = ALIGN(width, 128);
40 uv_stride = ALIGN(width, 128);
41 y_sclines = ALIGN(height, 32);
42 uv_sclines = ALIGN(((height + 1) >> 1), 16);
44 y_plane = y_stride * y_sclines;
45 uv_plane = uv_stride * uv_sclines + SZ_4K;
46 size = y_plane + uv_plane + SZ_8K;
47 size = ALIGN(size, SZ_4K);
52 static u32 get_framesize_compressed(u32 width, u32 height)
54 u32 sz = ALIGN(height, 32) * ALIGN(width, 32) * 3 / 2 / 2;
56 return ALIGN(sz, SZ_4K);
60 * Three resons to keep MPLANE formats (despite that the number of planes
62 * - the MPLANE formats allow only one plane to be used
63 * - the downstream driver use MPLANE formats too
64 * - future firmware versions could add support for >1 planes
66 static const struct venus_format venc_formats[] = {
68 .pixfmt = V4L2_PIX_FMT_NV12,
70 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
72 .pixfmt = V4L2_PIX_FMT_MPEG4,
74 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
76 .pixfmt = V4L2_PIX_FMT_H263,
78 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
80 .pixfmt = V4L2_PIX_FMT_H264,
82 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
84 .pixfmt = V4L2_PIX_FMT_VP8,
86 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
90 static const struct venus_format *
91 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
93 const struct venus_format *fmt = venc_formats;
94 unsigned int size = ARRAY_SIZE(venc_formats);
97 for (i = 0; i < size; i++) {
98 if (fmt[i].pixfmt == pixfmt)
102 if (i == size || fmt[i].type != type)
105 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
106 !venus_helper_check_codec(inst, fmt[i].pixfmt))
112 static const struct venus_format *
113 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
115 const struct venus_format *fmt = venc_formats;
116 unsigned int size = ARRAY_SIZE(venc_formats);
117 unsigned int i, k = 0;
122 for (i = 0; i < size; i++) {
123 if (fmt[i].type != type)
133 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
134 !venus_helper_check_codec(inst, fmt[i].pixfmt))
140 static int venc_v4l2_to_hfi(int id, int value)
143 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
145 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0:
147 return HFI_MPEG4_LEVEL_0;
148 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B:
149 return HFI_MPEG4_LEVEL_0b;
150 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_1:
151 return HFI_MPEG4_LEVEL_1;
152 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_2:
153 return HFI_MPEG4_LEVEL_2;
154 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_3:
155 return HFI_MPEG4_LEVEL_3;
156 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_4:
157 return HFI_MPEG4_LEVEL_4;
158 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_5:
159 return HFI_MPEG4_LEVEL_5;
161 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
163 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
165 return HFI_MPEG4_PROFILE_SIMPLE;
166 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
167 return HFI_MPEG4_PROFILE_ADVANCEDSIMPLE;
169 case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
171 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
172 return HFI_H264_PROFILE_BASELINE;
173 case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
174 return HFI_H264_PROFILE_CONSTRAINED_BASE;
175 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
176 return HFI_H264_PROFILE_MAIN;
177 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
179 return HFI_H264_PROFILE_HIGH;
181 case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
183 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
184 return HFI_H264_LEVEL_1;
185 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
186 return HFI_H264_LEVEL_1b;
187 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
188 return HFI_H264_LEVEL_11;
189 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
190 return HFI_H264_LEVEL_12;
191 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
192 return HFI_H264_LEVEL_13;
193 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
194 return HFI_H264_LEVEL_2;
195 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
196 return HFI_H264_LEVEL_21;
197 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
198 return HFI_H264_LEVEL_22;
199 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
200 return HFI_H264_LEVEL_3;
201 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
202 return HFI_H264_LEVEL_31;
203 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
204 return HFI_H264_LEVEL_32;
205 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
206 return HFI_H264_LEVEL_4;
207 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
208 return HFI_H264_LEVEL_41;
209 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
210 return HFI_H264_LEVEL_42;
211 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
213 return HFI_H264_LEVEL_5;
214 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
215 return HFI_H264_LEVEL_51;
217 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
219 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC:
221 return HFI_H264_ENTROPY_CAVLC;
222 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC:
223 return HFI_H264_ENTROPY_CABAC;
225 case V4L2_CID_MPEG_VIDEO_VPX_PROFILE:
229 return HFI_VPX_PROFILE_VERSION_0;
231 return HFI_VPX_PROFILE_VERSION_1;
233 return HFI_VPX_PROFILE_VERSION_2;
235 return HFI_VPX_PROFILE_VERSION_3;
243 venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
245 strlcpy(cap->driver, "qcom-venus", sizeof(cap->driver));
246 strlcpy(cap->card, "Qualcomm Venus video encoder", sizeof(cap->card));
247 strlcpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
252 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
254 struct venus_inst *inst = to_inst(file);
255 const struct venus_format *fmt;
257 fmt = find_format_by_index(inst, f->index, f->type);
259 memset(f->reserved, 0, sizeof(f->reserved));
264 f->pixelformat = fmt->pixfmt;
269 static const struct venus_format *
270 venc_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
272 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
273 struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
274 const struct venus_format *fmt;
277 memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
278 memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
280 fmt = find_format(inst, pixmp->pixelformat, f->type);
282 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
283 pixmp->pixelformat = V4L2_PIX_FMT_H264;
284 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
285 pixmp->pixelformat = V4L2_PIX_FMT_NV12;
288 fmt = find_format(inst, pixmp->pixelformat, f->type);
293 pixmp->width = clamp(pixmp->width, inst->cap_width.min,
294 inst->cap_width.max);
295 pixmp->height = clamp(pixmp->height, inst->cap_height.min,
296 inst->cap_height.max);
298 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
299 pixmp->height = ALIGN(pixmp->height, 32);
301 pixmp->width = ALIGN(pixmp->width, 2);
302 pixmp->height = ALIGN(pixmp->height, 2);
304 if (pixmp->field == V4L2_FIELD_ANY)
305 pixmp->field = V4L2_FIELD_NONE;
306 pixmp->num_planes = fmt->num_planes;
309 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
310 for (p = 0; p < pixmp->num_planes; p++) {
312 get_framesize_uncompressed(p, pixmp->width,
315 pfmt[p].bytesperline = ALIGN(pixmp->width, 128);
318 pfmt[0].sizeimage = get_framesize_compressed(pixmp->width,
320 pfmt[0].bytesperline = 0;
326 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
328 struct venus_inst *inst = to_inst(file);
330 venc_try_fmt_common(inst, f);
335 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
337 struct venus_inst *inst = to_inst(file);
338 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
339 struct v4l2_pix_format_mplane orig_pixmp;
340 const struct venus_format *fmt;
341 struct v4l2_format format;
342 u32 pixfmt_out = 0, pixfmt_cap = 0;
346 fmt = venc_try_fmt_common(inst, f);
350 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
351 pixfmt_out = pixmp->pixelformat;
352 pixfmt_cap = inst->fmt_cap->pixfmt;
353 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
354 pixfmt_cap = pixmp->pixelformat;
355 pixfmt_out = inst->fmt_out->pixfmt;
358 memset(&format, 0, sizeof(format));
360 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
361 format.fmt.pix_mp.pixelformat = pixfmt_out;
362 format.fmt.pix_mp.width = orig_pixmp.width;
363 format.fmt.pix_mp.height = orig_pixmp.height;
364 venc_try_fmt_common(inst, &format);
366 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
367 inst->out_width = format.fmt.pix_mp.width;
368 inst->out_height = format.fmt.pix_mp.height;
369 inst->colorspace = pixmp->colorspace;
370 inst->ycbcr_enc = pixmp->ycbcr_enc;
371 inst->quantization = pixmp->quantization;
372 inst->xfer_func = pixmp->xfer_func;
375 memset(&format, 0, sizeof(format));
377 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
378 format.fmt.pix_mp.pixelformat = pixfmt_cap;
379 format.fmt.pix_mp.width = orig_pixmp.width;
380 format.fmt.pix_mp.height = orig_pixmp.height;
381 venc_try_fmt_common(inst, &format);
383 inst->width = format.fmt.pix_mp.width;
384 inst->height = format.fmt.pix_mp.height;
386 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
388 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
394 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
396 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
397 struct venus_inst *inst = to_inst(file);
398 const struct venus_format *fmt;
400 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
402 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
407 pixmp->pixelformat = fmt->pixfmt;
409 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
410 pixmp->width = inst->width;
411 pixmp->height = inst->height;
412 pixmp->colorspace = inst->colorspace;
413 pixmp->ycbcr_enc = inst->ycbcr_enc;
414 pixmp->quantization = inst->quantization;
415 pixmp->xfer_func = inst->xfer_func;
416 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
417 pixmp->width = inst->out_width;
418 pixmp->height = inst->out_height;
421 venc_try_fmt_common(inst, f);
427 venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
429 struct venus_inst *inst = to_inst(file);
431 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
435 case V4L2_SEL_TGT_CROP_DEFAULT:
436 case V4L2_SEL_TGT_CROP_BOUNDS:
437 s->r.width = inst->width;
438 s->r.height = inst->height;
440 case V4L2_SEL_TGT_CROP:
441 s->r.width = inst->out_width;
442 s->r.height = inst->out_height;
455 venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
457 struct venus_inst *inst = to_inst(file);
459 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
463 case V4L2_SEL_TGT_CROP:
464 if (s->r.width != inst->out_width ||
465 s->r.height != inst->out_height ||
466 s->r.top != 0 || s->r.left != 0)
476 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
478 struct venus_inst *inst = to_inst(file);
479 struct v4l2_outputparm *out = &a->parm.output;
480 struct v4l2_fract *timeperframe = &out->timeperframe;
481 u64 us_per_frame, fps;
483 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
484 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
487 memset(out->reserved, 0, sizeof(out->reserved));
489 if (!timeperframe->denominator)
490 timeperframe->denominator = inst->timeperframe.denominator;
491 if (!timeperframe->numerator)
492 timeperframe->numerator = inst->timeperframe.numerator;
494 out->capability = V4L2_CAP_TIMEPERFRAME;
496 us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
497 do_div(us_per_frame, timeperframe->denominator);
502 fps = (u64)USEC_PER_SEC;
503 do_div(fps, us_per_frame);
505 inst->timeperframe = *timeperframe;
511 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
513 struct venus_inst *inst = to_inst(file);
515 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
516 a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
519 a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME;
520 a->parm.output.timeperframe = inst->timeperframe;
525 static int venc_enum_framesizes(struct file *file, void *fh,
526 struct v4l2_frmsizeenum *fsize)
528 struct venus_inst *inst = to_inst(file);
529 const struct venus_format *fmt;
531 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
533 fmt = find_format(inst, fsize->pixel_format,
534 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
536 fmt = find_format(inst, fsize->pixel_format,
537 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
545 fsize->stepwise.min_width = inst->cap_width.min;
546 fsize->stepwise.max_width = inst->cap_width.max;
547 fsize->stepwise.step_width = inst->cap_width.step_size;
548 fsize->stepwise.min_height = inst->cap_height.min;
549 fsize->stepwise.max_height = inst->cap_height.max;
550 fsize->stepwise.step_height = inst->cap_height.step_size;
555 static int venc_enum_frameintervals(struct file *file, void *fh,
556 struct v4l2_frmivalenum *fival)
558 struct venus_inst *inst = to_inst(file);
559 const struct venus_format *fmt;
561 fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
563 fmt = find_format(inst, fival->pixel_format,
564 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
566 fmt = find_format(inst, fival->pixel_format,
567 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
575 if (!fival->width || !fival->height)
578 if (fival->width > inst->cap_width.max ||
579 fival->width < inst->cap_width.min ||
580 fival->height > inst->cap_height.max ||
581 fival->height < inst->cap_height.min)
584 fival->stepwise.min.numerator = 1;
585 fival->stepwise.min.denominator = inst->cap_framerate.max;
586 fival->stepwise.max.numerator = 1;
587 fival->stepwise.max.denominator = inst->cap_framerate.min;
588 fival->stepwise.step.numerator = 1;
589 fival->stepwise.step.denominator = inst->cap_framerate.max;
594 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
595 .vidioc_querycap = venc_querycap,
596 .vidioc_enum_fmt_vid_cap_mplane = venc_enum_fmt,
597 .vidioc_enum_fmt_vid_out_mplane = venc_enum_fmt,
598 .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
599 .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
600 .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
601 .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
602 .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
603 .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
604 .vidioc_g_selection = venc_g_selection,
605 .vidioc_s_selection = venc_s_selection,
606 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
607 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
608 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
609 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
610 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
611 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
612 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
613 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
614 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
615 .vidioc_s_parm = venc_s_parm,
616 .vidioc_g_parm = venc_g_parm,
617 .vidioc_enum_framesizes = venc_enum_framesizes,
618 .vidioc_enum_frameintervals = venc_enum_frameintervals,
619 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
620 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
623 static int venc_set_properties(struct venus_inst *inst)
625 struct venc_controls *ctr = &inst->controls.enc;
626 struct hfi_intra_period intra_period;
627 struct hfi_profile_level pl;
628 struct hfi_framerate frate;
629 struct hfi_bitrate brate;
630 struct hfi_idr_period idrp;
631 u32 ptype, rate_control, bitrate, profile = 0, level = 0;
634 ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
635 frate.buffer_type = HFI_BUFFER_OUTPUT;
636 frate.framerate = inst->fps * (1 << 16);
638 ret = hfi_session_set_property(inst, ptype, &frate);
642 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
643 struct hfi_h264_vui_timing_info info;
645 ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO;
647 info.fixed_framerate = 1;
648 info.time_scale = NSEC_PER_SEC;
650 ret = hfi_session_set_property(inst, ptype, &info);
655 ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
656 idrp.idr_period = ctr->gop_size;
657 ret = hfi_session_set_property(inst, ptype, &idrp);
661 if (ctr->num_b_frames) {
662 u32 max_num_b_frames = NUM_B_FRAMES_MAX;
664 ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
665 ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
670 /* intra_period = pframes + bframes + 1 */
671 if (!ctr->num_p_frames)
672 ctr->num_p_frames = 2 * 15 - 1,
674 ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
675 intra_period.pframes = ctr->num_p_frames;
676 intra_period.bframes = ctr->num_b_frames;
678 ret = hfi_session_set_property(inst, ptype, &intra_period);
682 if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
683 rate_control = HFI_RATE_CONTROL_VBR_CFR;
685 rate_control = HFI_RATE_CONTROL_CBR_CFR;
687 ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
688 ret = hfi_session_set_property(inst, ptype, &rate_control);
695 bitrate = ctr->bitrate;
697 ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
698 brate.bitrate = bitrate;
701 ret = hfi_session_set_property(inst, ptype, &brate);
705 if (!ctr->bitrate_peak)
708 bitrate = ctr->bitrate_peak;
710 ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
711 brate.bitrate = bitrate;
714 ret = hfi_session_set_property(inst, ptype, &brate);
718 if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
719 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_PROFILE,
721 level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_LEVEL,
723 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
724 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_VPX_PROFILE,
727 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_MPEG4) {
728 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
730 level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
732 } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H263) {
737 ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
738 pl.profile = profile;
741 ret = hfi_session_set_property(inst, ptype, &pl);
748 static int venc_init_session(struct venus_inst *inst)
752 ret = hfi_session_init(inst, inst->fmt_cap->pixfmt);
756 ret = venus_helper_set_input_resolution(inst, inst->width,
761 ret = venus_helper_set_output_resolution(inst, inst->width,
766 ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
772 hfi_session_deinit(inst);
776 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
778 struct hfi_buffer_requirements bufreq;
781 ret = venc_init_session(inst);
785 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
787 *num = bufreq.count_actual;
789 hfi_session_deinit(inst);
794 static int venc_queue_setup(struct vb2_queue *q,
795 unsigned int *num_buffers, unsigned int *num_planes,
796 unsigned int sizes[], struct device *alloc_devs[])
798 struct venus_inst *inst = vb2_get_drv_priv(q);
799 unsigned int p, num, min = 4;
803 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
804 *num_planes != inst->fmt_out->num_planes)
807 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
808 *num_planes != inst->fmt_cap->num_planes)
811 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
812 sizes[0] < inst->input_buf_size)
815 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
816 sizes[0] < inst->output_buf_size)
823 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
824 *num_planes = inst->fmt_out->num_planes;
826 ret = venc_out_num_buffers(inst, &num);
831 *num_buffers = max(*num_buffers, num);
832 inst->num_input_bufs = *num_buffers;
834 for (p = 0; p < *num_planes; ++p)
835 sizes[p] = get_framesize_uncompressed(p, inst->width,
837 inst->input_buf_size = sizes[0];
839 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
840 *num_planes = inst->fmt_cap->num_planes;
841 *num_buffers = max(*num_buffers, min);
842 inst->num_output_bufs = *num_buffers;
843 sizes[0] = get_framesize_compressed(inst->width, inst->height);
844 inst->output_buf_size = sizes[0];
854 static int venc_verify_conf(struct venus_inst *inst)
856 struct hfi_buffer_requirements bufreq;
859 if (!inst->num_input_bufs || !inst->num_output_bufs)
862 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
866 if (inst->num_output_bufs < bufreq.count_actual ||
867 inst->num_output_bufs < bufreq.count_min)
870 ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
874 if (inst->num_input_bufs < bufreq.count_actual ||
875 inst->num_input_bufs < bufreq.count_min)
881 static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
883 struct venus_inst *inst = vb2_get_drv_priv(q);
886 mutex_lock(&inst->lock);
888 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
889 inst->streamon_out = 1;
891 inst->streamon_cap = 1;
893 if (!(inst->streamon_out & inst->streamon_cap)) {
894 mutex_unlock(&inst->lock);
898 venus_helper_init_instance(inst);
900 inst->sequence_cap = 0;
901 inst->sequence_out = 0;
903 ret = venc_init_session(inst);
907 ret = venc_set_properties(inst);
911 ret = venc_verify_conf(inst);
915 ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
916 inst->num_output_bufs);
920 ret = venus_helper_vb2_start_streaming(inst);
924 mutex_unlock(&inst->lock);
929 hfi_session_deinit(inst);
931 venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
932 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
933 inst->streamon_out = 0;
935 inst->streamon_cap = 0;
936 mutex_unlock(&inst->lock);
940 static const struct vb2_ops venc_vb2_ops = {
941 .queue_setup = venc_queue_setup,
942 .buf_init = venus_helper_vb2_buf_init,
943 .buf_prepare = venus_helper_vb2_buf_prepare,
944 .start_streaming = venc_start_streaming,
945 .stop_streaming = venus_helper_vb2_stop_streaming,
946 .buf_queue = venus_helper_vb2_buf_queue,
949 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
950 u32 tag, u32 bytesused, u32 data_offset, u32 flags,
951 u32 hfi_flags, u64 timestamp_us)
953 struct vb2_v4l2_buffer *vbuf;
954 struct vb2_buffer *vb;
957 if (buf_type == HFI_BUFFER_INPUT)
958 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
960 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
962 vbuf = venus_helper_find_buf(inst, type, tag);
968 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
970 vb2_set_plane_payload(vb, 0, bytesused + data_offset);
971 vb->planes[0].data_offset = data_offset;
972 vb->timestamp = timestamp_us * NSEC_PER_USEC;
973 vbuf->sequence = inst->sequence_cap++;
975 vbuf->sequence = inst->sequence_out++;
978 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
981 static void venc_event_notify(struct venus_inst *inst, u32 event,
982 struct hfi_event_data *data)
984 struct device *dev = inst->core->dev_enc;
986 if (event == EVT_SESSION_ERROR) {
987 inst->session_error = true;
988 dev_err(dev, "enc: event session error %x\n", inst->error);
992 static const struct hfi_inst_ops venc_hfi_ops = {
993 .buf_done = venc_buf_done,
994 .event_notify = venc_event_notify,
997 static const struct v4l2_m2m_ops venc_m2m_ops = {
998 .device_run = venus_helper_m2m_device_run,
999 .job_abort = venus_helper_m2m_job_abort,
1002 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1003 struct vb2_queue *dst_vq)
1005 struct venus_inst *inst = priv;
1008 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1009 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1010 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1011 src_vq->ops = &venc_vb2_ops;
1012 src_vq->mem_ops = &vb2_dma_sg_memops;
1013 src_vq->drv_priv = inst;
1014 src_vq->buf_struct_size = sizeof(struct venus_buffer);
1015 src_vq->allow_zero_bytesused = 1;
1016 src_vq->min_buffers_needed = 1;
1017 src_vq->dev = inst->core->dev;
1018 if (inst->core->res->hfi_version == HFI_VERSION_1XX)
1019 src_vq->bidirectional = 1;
1020 ret = vb2_queue_init(src_vq);
1024 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1025 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1026 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1027 dst_vq->ops = &venc_vb2_ops;
1028 dst_vq->mem_ops = &vb2_dma_sg_memops;
1029 dst_vq->drv_priv = inst;
1030 dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1031 dst_vq->allow_zero_bytesused = 1;
1032 dst_vq->min_buffers_needed = 1;
1033 dst_vq->dev = inst->core->dev;
1034 ret = vb2_queue_init(dst_vq);
1036 vb2_queue_release(src_vq);
1043 static void venc_inst_init(struct venus_inst *inst)
1045 inst->fmt_cap = &venc_formats[2];
1046 inst->fmt_out = &venc_formats[0];
1048 inst->height = ALIGN(720, 32);
1049 inst->out_width = 1280;
1050 inst->out_height = 720;
1052 inst->timeperframe.numerator = 1;
1053 inst->timeperframe.denominator = 15;
1055 inst->cap_width.min = 96;
1056 inst->cap_width.max = 1920;
1057 if (inst->core->res->hfi_version == HFI_VERSION_3XX)
1058 inst->cap_width.max = 3840;
1059 inst->cap_width.step_size = 2;
1060 inst->cap_height.min = 64;
1061 inst->cap_height.max = ALIGN(1080, 32);
1062 if (inst->core->res->hfi_version == HFI_VERSION_3XX)
1063 inst->cap_height.max = ALIGN(2160, 32);
1064 inst->cap_height.step_size = 2;
1065 inst->cap_framerate.min = 1;
1066 inst->cap_framerate.max = 30;
1067 inst->cap_framerate.step_size = 1;
1068 inst->cap_mbs_per_frame.min = 24;
1069 inst->cap_mbs_per_frame.max = 8160;
1072 static int venc_open(struct file *file)
1074 struct venus_core *core = video_drvdata(file);
1075 struct venus_inst *inst;
1078 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1082 INIT_LIST_HEAD(&inst->registeredbufs);
1083 INIT_LIST_HEAD(&inst->internalbufs);
1084 INIT_LIST_HEAD(&inst->list);
1085 mutex_init(&inst->lock);
1088 inst->session_type = VIDC_SESSION_TYPE_ENC;
1090 venus_helper_init_instance(inst);
1092 ret = pm_runtime_get_sync(core->dev_enc);
1096 ret = venc_ctrl_init(inst);
1100 ret = hfi_session_create(inst, &venc_hfi_ops);
1102 goto err_ctrl_deinit;
1104 venc_inst_init(inst);
1107 * create m2m device for every instance, the m2m context scheduling
1108 * is made by firmware side so we do not need to care about.
1110 inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
1111 if (IS_ERR(inst->m2m_dev)) {
1112 ret = PTR_ERR(inst->m2m_dev);
1113 goto err_session_destroy;
1116 inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1117 if (IS_ERR(inst->m2m_ctx)) {
1118 ret = PTR_ERR(inst->m2m_ctx);
1119 goto err_m2m_release;
1122 v4l2_fh_init(&inst->fh, core->vdev_enc);
1124 inst->fh.ctrl_handler = &inst->ctrl_handler;
1125 v4l2_fh_add(&inst->fh);
1126 inst->fh.m2m_ctx = inst->m2m_ctx;
1127 file->private_data = &inst->fh;
1132 v4l2_m2m_release(inst->m2m_dev);
1133 err_session_destroy:
1134 hfi_session_destroy(inst);
1136 venc_ctrl_deinit(inst);
1138 pm_runtime_put_sync(core->dev_enc);
1144 static int venc_close(struct file *file)
1146 struct venus_inst *inst = to_inst(file);
1148 v4l2_m2m_ctx_release(inst->m2m_ctx);
1149 v4l2_m2m_release(inst->m2m_dev);
1150 venc_ctrl_deinit(inst);
1151 hfi_session_destroy(inst);
1152 mutex_destroy(&inst->lock);
1153 v4l2_fh_del(&inst->fh);
1154 v4l2_fh_exit(&inst->fh);
1156 pm_runtime_put_sync(inst->core->dev_enc);
1162 static const struct v4l2_file_operations venc_fops = {
1163 .owner = THIS_MODULE,
1165 .release = venc_close,
1166 .unlocked_ioctl = video_ioctl2,
1167 .poll = v4l2_m2m_fop_poll,
1168 .mmap = v4l2_m2m_fop_mmap,
1171 static int venc_probe(struct platform_device *pdev)
1173 struct device *dev = &pdev->dev;
1174 struct video_device *vdev;
1175 struct venus_core *core;
1179 return -EPROBE_DEFER;
1181 core = dev_get_drvdata(dev->parent);
1183 return -EPROBE_DEFER;
1185 if (core->res->hfi_version == HFI_VERSION_3XX) {
1186 core->core1_clk = devm_clk_get(dev, "core");
1187 if (IS_ERR(core->core1_clk))
1188 return PTR_ERR(core->core1_clk);
1191 platform_set_drvdata(pdev, core);
1193 vdev = video_device_alloc();
1197 strlcpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
1198 vdev->release = video_device_release;
1199 vdev->fops = &venc_fops;
1200 vdev->ioctl_ops = &venc_ioctl_ops;
1201 vdev->vfl_dir = VFL_DIR_M2M;
1202 vdev->v4l2_dev = &core->v4l2_dev;
1203 vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1205 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1207 goto err_vdev_release;
1209 core->vdev_enc = vdev;
1210 core->dev_enc = dev;
1212 video_set_drvdata(vdev, core);
1213 pm_runtime_enable(dev);
1218 video_device_release(vdev);
1222 static int venc_remove(struct platform_device *pdev)
1224 struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1226 video_unregister_device(core->vdev_enc);
1227 pm_runtime_disable(core->dev_enc);
1232 static __maybe_unused int venc_runtime_suspend(struct device *dev)
1234 struct venus_core *core = dev_get_drvdata(dev);
1236 if (core->res->hfi_version == HFI_VERSION_1XX)
1239 writel(0, core->base + WRAPPER_VENC_VCODEC_POWER_CONTROL);
1240 clk_disable_unprepare(core->core1_clk);
1241 writel(1, core->base + WRAPPER_VENC_VCODEC_POWER_CONTROL);
1246 static __maybe_unused int venc_runtime_resume(struct device *dev)
1248 struct venus_core *core = dev_get_drvdata(dev);
1251 if (core->res->hfi_version == HFI_VERSION_1XX)
1254 writel(0, core->base + WRAPPER_VENC_VCODEC_POWER_CONTROL);
1255 ret = clk_prepare_enable(core->core1_clk);
1256 writel(1, core->base + WRAPPER_VENC_VCODEC_POWER_CONTROL);
1261 static const struct dev_pm_ops venc_pm_ops = {
1262 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1263 pm_runtime_force_resume)
1264 SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL)
1267 static const struct of_device_id venc_dt_match[] = {
1268 { .compatible = "venus-encoder" },
1271 MODULE_DEVICE_TABLE(of, venc_dt_match);
1273 static struct platform_driver qcom_venus_enc_driver = {
1274 .probe = venc_probe,
1275 .remove = venc_remove,
1277 .name = "qcom-venus-encoder",
1278 .of_match_table = venc_dt_match,
1282 module_platform_driver(qcom_venus_enc_driver);
1284 MODULE_ALIAS("platform:qcom-venus-encoder");
1285 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
1286 MODULE_LICENSE("GPL v2");