1 // SPDX-License-Identifier: GPL-2.0
3 * Hantro VPU codec driver
5 * Copyright (C) 2018 Collabora, Ltd.
6 * Copyright (C) 2018 Rockchip Electronics Co., Ltd.
7 * Alpha Lin <Alpha.Lin@rock-chips.com>
8 * Jeffy Chen <jeffy.chen@rock-chips.com>
10 * Copyright 2018 Google LLC.
11 * Tomasz Figa <tfiga@chromium.org>
13 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
14 * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
17 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/videodev2.h>
22 #include <linux/workqueue.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-mem2mem.h>
28 #include "hantro_hw.h"
29 #include "hantro_v4l2.h"
31 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
32 struct v4l2_pix_format_mplane *pix_mp);
33 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
34 struct v4l2_pix_format_mplane *pix_mp);
36 static const struct hantro_fmt *
37 hantro_get_formats(const struct hantro_ctx *ctx, unsigned int *num_fmts)
39 const struct hantro_fmt *formats;
41 if (ctx->is_encoder) {
42 formats = ctx->dev->variant->enc_fmts;
43 *num_fmts = ctx->dev->variant->num_enc_fmts;
45 formats = ctx->dev->variant->dec_fmts;
46 *num_fmts = ctx->dev->variant->num_dec_fmts;
52 static const struct hantro_fmt *
53 hantro_get_postproc_formats(const struct hantro_ctx *ctx,
54 unsigned int *num_fmts)
56 struct hantro_dev *vpu = ctx->dev;
58 if (ctx->is_encoder || !vpu->variant->postproc_fmts) {
63 *num_fmts = ctx->dev->variant->num_postproc_fmts;
64 return ctx->dev->variant->postproc_fmts;
67 static const struct hantro_fmt *
68 hantro_find_format(const struct hantro_ctx *ctx, u32 fourcc)
70 const struct hantro_fmt *formats;
71 unsigned int i, num_fmts;
73 formats = hantro_get_formats(ctx, &num_fmts);
74 for (i = 0; i < num_fmts; i++)
75 if (formats[i].fourcc == fourcc)
78 formats = hantro_get_postproc_formats(ctx, &num_fmts);
79 for (i = 0; i < num_fmts; i++)
80 if (formats[i].fourcc == fourcc)
85 static const struct hantro_fmt *
86 hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream)
88 const struct hantro_fmt *formats;
89 unsigned int i, num_fmts;
91 formats = hantro_get_formats(ctx, &num_fmts);
92 for (i = 0; i < num_fmts; i++) {
93 if (bitstream == (formats[i].codec_mode !=
100 static int vidioc_querycap(struct file *file, void *priv,
101 struct v4l2_capability *cap)
103 struct hantro_dev *vpu = video_drvdata(file);
104 struct video_device *vdev = video_devdata(file);
106 strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
107 strscpy(cap->card, vdev->name, sizeof(cap->card));
108 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform: %s",
109 vpu->dev->driver->name);
113 static int vidioc_enum_framesizes(struct file *file, void *priv,
114 struct v4l2_frmsizeenum *fsize)
116 struct hantro_ctx *ctx = fh_to_ctx(priv);
117 const struct hantro_fmt *fmt;
119 fmt = hantro_find_format(ctx, fsize->pixel_format);
121 vpu_debug(0, "unsupported bitstream format (%08x)\n",
122 fsize->pixel_format);
126 /* For non-coded formats check if postprocessing scaling is possible */
127 if (fmt->codec_mode == HANTRO_MODE_NONE && hantro_needs_postproc(ctx, fmt)) {
128 return hanto_postproc_enum_framesizes(ctx, fsize);
129 } else if (fsize->index != 0) {
130 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
135 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
136 fsize->stepwise = fmt->frmsize;
141 static int vidioc_enum_fmt(struct file *file, void *priv,
142 struct v4l2_fmtdesc *f, bool capture)
145 struct hantro_ctx *ctx = fh_to_ctx(priv);
146 const struct hantro_fmt *fmt, *formats;
147 unsigned int num_fmts, i, j = 0;
151 * When dealing with an encoder:
152 * - on the capture side we want to filter out all MODE_NONE formats.
153 * - on the output side we want to filter out all formats that are
155 * When dealing with a decoder:
156 * - on the capture side we want to filter out all formats that are
158 * - on the output side we want to filter out all MODE_NONE formats.
160 skip_mode_none = capture == ctx->is_encoder;
162 formats = hantro_get_formats(ctx, &num_fmts);
163 for (i = 0; i < num_fmts; i++) {
164 bool mode_none = formats[i].codec_mode == HANTRO_MODE_NONE;
166 if (skip_mode_none == mode_none)
170 f->pixelformat = fmt->fourcc;
177 * Enumerate post-processed formats. As per the specification,
178 * we enumerated these formats after natively decoded formats
179 * as a hint for applications on what's the preferred fomat.
183 formats = hantro_get_postproc_formats(ctx, &num_fmts);
184 for (i = 0; i < num_fmts; i++) {
187 f->pixelformat = fmt->fourcc;
196 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
197 struct v4l2_fmtdesc *f)
199 return vidioc_enum_fmt(file, priv, f, true);
202 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
203 struct v4l2_fmtdesc *f)
205 return vidioc_enum_fmt(file, priv, f, false);
208 static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
209 struct v4l2_format *f)
211 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
212 struct hantro_ctx *ctx = fh_to_ctx(priv);
214 vpu_debug(4, "f->type = %d\n", f->type);
216 *pix_mp = ctx->src_fmt;
221 static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
222 struct v4l2_format *f)
224 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
225 struct hantro_ctx *ctx = fh_to_ctx(priv);
227 vpu_debug(4, "f->type = %d\n", f->type);
229 *pix_mp = ctx->dst_fmt;
234 static int hantro_try_fmt(const struct hantro_ctx *ctx,
235 struct v4l2_pix_format_mplane *pix_mp,
236 enum v4l2_buf_type type)
238 const struct hantro_fmt *fmt, *vpu_fmt;
239 bool capture = V4L2_TYPE_IS_CAPTURE(type);
242 coded = capture == ctx->is_encoder;
244 vpu_debug(4, "trying format %c%c%c%c\n",
245 (pix_mp->pixelformat & 0x7f),
246 (pix_mp->pixelformat >> 8) & 0x7f,
247 (pix_mp->pixelformat >> 16) & 0x7f,
248 (pix_mp->pixelformat >> 24) & 0x7f);
250 fmt = hantro_find_format(ctx, pix_mp->pixelformat);
252 fmt = hantro_get_default_fmt(ctx, coded);
253 pix_mp->pixelformat = fmt->fourcc;
257 pix_mp->num_planes = 1;
259 } else if (ctx->is_encoder) {
260 vpu_fmt = ctx->vpu_dst_fmt;
262 vpu_fmt = ctx->vpu_src_fmt;
264 * Width/height on the CAPTURE end of a decoder are ignored and
265 * replaced by the OUTPUT ones.
267 pix_mp->width = ctx->src_fmt.width;
268 pix_mp->height = ctx->src_fmt.height;
271 pix_mp->field = V4L2_FIELD_NONE;
273 v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
277 /* Fill remaining fields */
278 v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
280 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE &&
281 !hantro_needs_postproc(ctx, fmt))
282 pix_mp->plane_fmt[0].sizeimage +=
283 hantro_h264_mv_size(pix_mp->width,
285 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_VP9_FRAME &&
286 !hantro_needs_postproc(ctx, fmt))
287 pix_mp->plane_fmt[0].sizeimage +=
288 hantro_vp9_mv_size(pix_mp->width,
290 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_HEVC_SLICE &&
291 !hantro_needs_postproc(ctx, fmt))
292 pix_mp->plane_fmt[0].sizeimage +=
293 hantro_hevc_mv_size(pix_mp->width,
295 } else if (!pix_mp->plane_fmt[0].sizeimage) {
297 * For coded formats the application can specify
298 * sizeimage. If the application passes a zero sizeimage,
299 * let's default to the maximum frame size.
301 pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
302 pix_mp->width * pix_mp->height * fmt->max_depth;
308 static int vidioc_try_fmt_cap_mplane(struct file *file, void *priv,
309 struct v4l2_format *f)
311 return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
314 static int vidioc_try_fmt_out_mplane(struct file *file, void *priv,
315 struct v4l2_format *f)
317 return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
321 hantro_reset_fmt(struct v4l2_pix_format_mplane *fmt,
322 const struct hantro_fmt *vpu_fmt)
324 memset(fmt, 0, sizeof(*fmt));
326 fmt->pixelformat = vpu_fmt->fourcc;
327 fmt->field = V4L2_FIELD_NONE;
328 fmt->colorspace = V4L2_COLORSPACE_JPEG;
329 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
330 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
331 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
335 hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
337 const struct hantro_fmt *vpu_fmt;
338 struct v4l2_pix_format_mplane *fmt;
340 vpu_fmt = hantro_get_default_fmt(ctx, true);
342 if (ctx->is_encoder) {
343 ctx->vpu_dst_fmt = vpu_fmt;
346 ctx->vpu_src_fmt = vpu_fmt;
350 hantro_reset_fmt(fmt, vpu_fmt);
351 fmt->width = vpu_fmt->frmsize.min_width;
352 fmt->height = vpu_fmt->frmsize.min_height;
354 hantro_set_fmt_cap(ctx, fmt);
356 hantro_set_fmt_out(ctx, fmt);
360 hantro_reset_raw_fmt(struct hantro_ctx *ctx)
362 const struct hantro_fmt *raw_vpu_fmt;
363 struct v4l2_pix_format_mplane *raw_fmt, *encoded_fmt;
365 raw_vpu_fmt = hantro_get_default_fmt(ctx, false);
367 if (ctx->is_encoder) {
368 ctx->vpu_src_fmt = raw_vpu_fmt;
369 raw_fmt = &ctx->src_fmt;
370 encoded_fmt = &ctx->dst_fmt;
372 ctx->vpu_dst_fmt = raw_vpu_fmt;
373 raw_fmt = &ctx->dst_fmt;
374 encoded_fmt = &ctx->src_fmt;
377 hantro_reset_fmt(raw_fmt, raw_vpu_fmt);
378 raw_fmt->width = encoded_fmt->width;
379 raw_fmt->height = encoded_fmt->height;
381 hantro_set_fmt_out(ctx, raw_fmt);
383 hantro_set_fmt_cap(ctx, raw_fmt);
386 void hantro_reset_fmts(struct hantro_ctx *ctx)
388 hantro_reset_encoded_fmt(ctx);
389 hantro_reset_raw_fmt(ctx);
393 hantro_update_requires_request(struct hantro_ctx *ctx, u32 fourcc)
396 case V4L2_PIX_FMT_JPEG:
397 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = false;
399 case V4L2_PIX_FMT_MPEG2_SLICE:
400 case V4L2_PIX_FMT_VP8_FRAME:
401 case V4L2_PIX_FMT_H264_SLICE:
402 case V4L2_PIX_FMT_HEVC_SLICE:
403 case V4L2_PIX_FMT_VP9_FRAME:
404 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
412 hantro_update_requires_hold_capture_buf(struct hantro_ctx *ctx, u32 fourcc)
414 struct vb2_queue *vq;
416 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
417 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
420 case V4L2_PIX_FMT_JPEG:
421 case V4L2_PIX_FMT_MPEG2_SLICE:
422 case V4L2_PIX_FMT_VP8_FRAME:
423 case V4L2_PIX_FMT_HEVC_SLICE:
424 case V4L2_PIX_FMT_VP9_FRAME:
425 vq->subsystem_flags &= ~(VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF);
427 case V4L2_PIX_FMT_H264_SLICE:
428 vq->subsystem_flags |= VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
435 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
436 struct v4l2_pix_format_mplane *pix_mp)
438 struct vb2_queue *vq;
441 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
442 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
443 ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
447 if (!ctx->is_encoder) {
448 struct vb2_queue *peer_vq;
451 * In order to support dynamic resolution change,
452 * the decoder admits a resolution change, as long
453 * as the pixelformat remains. Can't be done if streaming.
455 if (vb2_is_streaming(vq) || (vb2_is_busy(vq) &&
456 pix_mp->pixelformat != ctx->src_fmt.pixelformat))
459 * Since format change on the OUTPUT queue will reset
460 * the CAPTURE queue, we can't allow doing so
461 * when the CAPTURE queue has buffers allocated.
463 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
464 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
465 if (vb2_is_busy(peer_vq))
469 * The encoder doesn't admit a format change if
470 * there are OUTPUT buffers allocated.
476 ctx->vpu_src_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
477 ctx->src_fmt = *pix_mp;
480 * Current raw format might have become invalid with newly
481 * selected codec, so reset it to default just to be safe and
482 * keep internal driver state sane. User is mandated to set
483 * the raw format again after we return, so we don't need
485 * Note that hantro_reset_raw_fmt() also propagates size
486 * changes to the raw format.
488 if (!ctx->is_encoder)
489 hantro_reset_raw_fmt(ctx);
491 /* Colorimetry information are always propagated. */
492 ctx->dst_fmt.colorspace = pix_mp->colorspace;
493 ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
494 ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
495 ctx->dst_fmt.quantization = pix_mp->quantization;
497 hantro_update_requires_request(ctx, pix_mp->pixelformat);
498 hantro_update_requires_hold_capture_buf(ctx, pix_mp->pixelformat);
500 vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
501 vpu_debug(0, "fmt - w: %d, h: %d\n",
502 pix_mp->width, pix_mp->height);
506 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
507 struct v4l2_pix_format_mplane *pix_mp)
509 struct vb2_queue *vq;
512 /* Change not allowed if queue is busy. */
513 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
514 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
518 if (ctx->is_encoder) {
519 struct vb2_queue *peer_vq;
522 * Since format change on the CAPTURE queue will reset
523 * the OUTPUT queue, we can't allow doing so
524 * when the OUTPUT queue has buffers allocated.
526 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
527 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
528 if (vb2_is_busy(peer_vq) &&
529 (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
530 pix_mp->height != ctx->dst_fmt.height ||
531 pix_mp->width != ctx->dst_fmt.width))
535 ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
539 ctx->vpu_dst_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
540 ctx->dst_fmt = *pix_mp;
543 * Current raw format might have become invalid with newly
544 * selected codec, so reset it to default just to be safe and
545 * keep internal driver state sane. User is mandated to set
546 * the raw format again after we return, so we don't need
548 * Note that hantro_reset_raw_fmt() also propagates size
549 * changes to the raw format.
552 hantro_reset_raw_fmt(ctx);
554 /* Colorimetry information are always propagated. */
555 ctx->src_fmt.colorspace = pix_mp->colorspace;
556 ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
557 ctx->src_fmt.xfer_func = pix_mp->xfer_func;
558 ctx->src_fmt.quantization = pix_mp->quantization;
560 vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
561 vpu_debug(0, "fmt - w: %d, h: %d\n",
562 pix_mp->width, pix_mp->height);
564 hantro_update_requires_request(ctx, pix_mp->pixelformat);
570 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
572 return hantro_set_fmt_out(fh_to_ctx(priv), &f->fmt.pix_mp);
576 vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
578 return hantro_set_fmt_cap(fh_to_ctx(priv), &f->fmt.pix_mp);
581 static int vidioc_g_selection(struct file *file, void *priv,
582 struct v4l2_selection *sel)
584 struct hantro_ctx *ctx = fh_to_ctx(priv);
586 /* Crop only supported on source. */
587 if (!ctx->is_encoder ||
588 sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
591 switch (sel->target) {
592 case V4L2_SEL_TGT_CROP_DEFAULT:
593 case V4L2_SEL_TGT_CROP_BOUNDS:
596 sel->r.width = ctx->src_fmt.width;
597 sel->r.height = ctx->src_fmt.height;
599 case V4L2_SEL_TGT_CROP:
602 sel->r.width = ctx->dst_fmt.width;
603 sel->r.height = ctx->dst_fmt.height;
612 static int vidioc_s_selection(struct file *file, void *priv,
613 struct v4l2_selection *sel)
615 struct hantro_ctx *ctx = fh_to_ctx(priv);
616 struct v4l2_rect *rect = &sel->r;
617 struct vb2_queue *vq;
619 /* Crop only supported on source. */
620 if (!ctx->is_encoder ||
621 sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
624 /* Change not allowed if the queue is streaming. */
625 vq = v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx);
626 if (vb2_is_streaming(vq))
629 if (sel->target != V4L2_SEL_TGT_CROP)
633 * We do not support offsets, and we can crop only inside
634 * right-most or bottom-most macroblocks.
636 if (rect->left != 0 || rect->top != 0 ||
637 round_up(rect->width, MB_DIM) != ctx->src_fmt.width ||
638 round_up(rect->height, MB_DIM) != ctx->src_fmt.height) {
639 /* Default to full frame for incorrect settings. */
642 rect->width = ctx->src_fmt.width;
643 rect->height = ctx->src_fmt.height;
645 /* We support widths aligned to 4 pixels and arbitrary heights. */
646 rect->width = round_up(rect->width, 4);
649 ctx->dst_fmt.width = rect->width;
650 ctx->dst_fmt.height = rect->height;
655 static const struct v4l2_event hantro_eos_event = {
656 .type = V4L2_EVENT_EOS
659 static int vidioc_encoder_cmd(struct file *file, void *priv,
660 struct v4l2_encoder_cmd *ec)
662 struct hantro_ctx *ctx = fh_to_ctx(priv);
665 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec);
669 if (!vb2_is_streaming(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx)) ||
670 !vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)))
673 ret = v4l2_m2m_ioctl_encoder_cmd(file, priv, ec);
677 if (ec->cmd == V4L2_ENC_CMD_STOP &&
678 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
679 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
681 if (ec->cmd == V4L2_ENC_CMD_START)
682 vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q);
687 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
688 .vidioc_querycap = vidioc_querycap,
689 .vidioc_enum_framesizes = vidioc_enum_framesizes,
691 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
692 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
693 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
694 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
695 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
696 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
697 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
698 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
700 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
701 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
702 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
703 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
704 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
705 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
706 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
708 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
709 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
711 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
712 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
714 .vidioc_g_selection = vidioc_g_selection,
715 .vidioc_s_selection = vidioc_s_selection,
717 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
718 .vidioc_encoder_cmd = vidioc_encoder_cmd,
722 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
723 unsigned int *num_planes, unsigned int sizes[],
724 struct device *alloc_devs[])
726 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
727 struct v4l2_pix_format_mplane *pixfmt;
731 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
732 pixfmt = &ctx->dst_fmt;
734 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
735 pixfmt = &ctx->src_fmt;
738 vpu_err("invalid queue type: %d\n", vq->type);
743 if (*num_planes != pixfmt->num_planes)
745 for (i = 0; i < pixfmt->num_planes; ++i)
746 if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
751 *num_planes = pixfmt->num_planes;
752 for (i = 0; i < pixfmt->num_planes; ++i)
753 sizes[i] = pixfmt->plane_fmt[i].sizeimage;
758 hantro_buf_plane_check(struct vb2_buffer *vb,
759 struct v4l2_pix_format_mplane *pixfmt)
764 for (i = 0; i < pixfmt->num_planes; ++i) {
765 sz = pixfmt->plane_fmt[i].sizeimage;
766 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
767 i, vb2_plane_size(vb, i), sz);
768 if (vb2_plane_size(vb, i) < sz) {
769 vpu_err("plane %d is too small for output\n", i);
776 static int hantro_buf_prepare(struct vb2_buffer *vb)
778 struct vb2_queue *vq = vb->vb2_queue;
779 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
780 struct v4l2_pix_format_mplane *pix_fmt;
783 if (V4L2_TYPE_IS_OUTPUT(vq->type))
784 pix_fmt = &ctx->src_fmt;
786 pix_fmt = &ctx->dst_fmt;
787 ret = hantro_buf_plane_check(vb, pix_fmt);
791 * Buffer's bytesused must be written by driver for CAPTURE buffers.
792 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
793 * it to buffer length).
795 if (V4L2_TYPE_IS_CAPTURE(vq->type)) {
797 vb2_set_plane_payload(vb, 0, 0);
799 vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
805 static void hantro_buf_queue(struct vb2_buffer *vb)
807 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
808 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
810 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
811 vb2_is_streaming(vb->vb2_queue) &&
812 v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
815 for (i = 0; i < vb->num_planes; i++)
816 vb2_set_plane_payload(vb, i, 0);
818 vbuf->field = V4L2_FIELD_NONE;
819 vbuf->sequence = ctx->sequence_cap++;
821 v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
822 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
826 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
829 static bool hantro_vq_is_coded(struct vb2_queue *q)
831 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
833 return ctx->is_encoder != V4L2_TYPE_IS_OUTPUT(q->type);
836 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
838 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
841 v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
843 if (V4L2_TYPE_IS_OUTPUT(q->type))
844 ctx->sequence_out = 0;
846 ctx->sequence_cap = 0;
848 if (hantro_vq_is_coded(q)) {
849 enum hantro_codec_mode codec_mode;
851 if (V4L2_TYPE_IS_OUTPUT(q->type))
852 codec_mode = ctx->vpu_src_fmt->codec_mode;
854 codec_mode = ctx->vpu_dst_fmt->codec_mode;
856 vpu_debug(4, "Codec mode = %d\n", codec_mode);
857 ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
858 if (ctx->codec_ops->init) {
859 ret = ctx->codec_ops->init(ctx);
864 if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) {
865 ret = hantro_postproc_alloc(ctx);
873 if (ctx->codec_ops->exit)
874 ctx->codec_ops->exit(ctx);
879 hantro_return_bufs(struct vb2_queue *q,
880 struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
882 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
885 struct vb2_v4l2_buffer *vbuf;
887 vbuf = buf_remove(ctx->fh.m2m_ctx);
890 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
892 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
896 static void hantro_stop_streaming(struct vb2_queue *q)
898 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
900 if (hantro_vq_is_coded(q)) {
901 hantro_postproc_free(ctx);
902 if (ctx->codec_ops && ctx->codec_ops->exit)
903 ctx->codec_ops->exit(ctx);
907 * The mem2mem framework calls v4l2_m2m_cancel_job before
908 * .stop_streaming, so there isn't any job running and
909 * it is safe to return all the buffers.
911 if (V4L2_TYPE_IS_OUTPUT(q->type))
912 hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
914 hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
916 v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
918 if (V4L2_TYPE_IS_OUTPUT(q->type) &&
919 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
920 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
923 static void hantro_buf_request_complete(struct vb2_buffer *vb)
925 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
927 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
930 static int hantro_buf_out_validate(struct vb2_buffer *vb)
932 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
934 vbuf->field = V4L2_FIELD_NONE;
938 const struct vb2_ops hantro_queue_ops = {
939 .queue_setup = hantro_queue_setup,
940 .buf_prepare = hantro_buf_prepare,
941 .buf_queue = hantro_buf_queue,
942 .buf_out_validate = hantro_buf_out_validate,
943 .buf_request_complete = hantro_buf_request_complete,
944 .start_streaming = hantro_start_streaming,
945 .stop_streaming = hantro_stop_streaming,
946 .wait_prepare = vb2_ops_wait_prepare,
947 .wait_finish = vb2_ops_wait_finish,