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 #define HANTRO_DEFAULT_BIT_DEPTH 8
33 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
34 struct v4l2_pix_format_mplane *pix_mp);
35 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
36 struct v4l2_pix_format_mplane *pix_mp);
38 static const struct hantro_fmt *
39 hantro_get_formats(const struct hantro_ctx *ctx, unsigned int *num_fmts)
41 const struct hantro_fmt *formats;
43 if (ctx->is_encoder) {
44 formats = ctx->dev->variant->enc_fmts;
45 *num_fmts = ctx->dev->variant->num_enc_fmts;
47 formats = ctx->dev->variant->dec_fmts;
48 *num_fmts = ctx->dev->variant->num_dec_fmts;
54 static const struct hantro_fmt *
55 hantro_get_postproc_formats(const struct hantro_ctx *ctx,
56 unsigned int *num_fmts)
58 struct hantro_dev *vpu = ctx->dev;
60 if (ctx->is_encoder || !vpu->variant->postproc_fmts) {
65 *num_fmts = ctx->dev->variant->num_postproc_fmts;
66 return ctx->dev->variant->postproc_fmts;
69 int hantro_get_format_depth(u32 fourcc)
72 case V4L2_PIX_FMT_P010:
73 case V4L2_PIX_FMT_P010_4L4:
81 hantro_check_depth_match(const struct hantro_fmt *fmt, int bit_depth)
85 if (!fmt->match_depth && !fmt->postprocessed)
88 fmt_depth = hantro_get_format_depth(fmt->fourcc);
91 * Allow only downconversion for postproc formats for now.
92 * It may be possible to relax that on some HW.
94 if (!fmt->match_depth)
95 return fmt_depth <= bit_depth;
97 return fmt_depth == bit_depth;
100 static const struct hantro_fmt *
101 hantro_find_format(const struct hantro_ctx *ctx, u32 fourcc)
103 const struct hantro_fmt *formats;
104 unsigned int i, num_fmts;
106 formats = hantro_get_formats(ctx, &num_fmts);
107 for (i = 0; i < num_fmts; i++)
108 if (formats[i].fourcc == fourcc)
111 formats = hantro_get_postproc_formats(ctx, &num_fmts);
112 for (i = 0; i < num_fmts; i++)
113 if (formats[i].fourcc == fourcc)
118 const struct hantro_fmt *
119 hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream, int bit_depth)
121 const struct hantro_fmt *formats;
122 unsigned int i, num_fmts;
124 formats = hantro_get_formats(ctx, &num_fmts);
125 for (i = 0; i < num_fmts; i++) {
126 if (bitstream == (formats[i].codec_mode !=
128 hantro_check_depth_match(&formats[i], bit_depth))
134 static int vidioc_querycap(struct file *file, void *priv,
135 struct v4l2_capability *cap)
137 struct hantro_dev *vpu = video_drvdata(file);
138 struct video_device *vdev = video_devdata(file);
140 strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
141 strscpy(cap->card, vdev->name, sizeof(cap->card));
145 static int vidioc_enum_framesizes(struct file *file, void *priv,
146 struct v4l2_frmsizeenum *fsize)
148 struct hantro_ctx *ctx = fh_to_ctx(priv);
149 const struct hantro_fmt *fmt;
151 fmt = hantro_find_format(ctx, fsize->pixel_format);
153 vpu_debug(0, "unsupported bitstream format (%08x)\n",
154 fsize->pixel_format);
158 /* For non-coded formats check if postprocessing scaling is possible */
159 if (fmt->codec_mode == HANTRO_MODE_NONE) {
160 if (hantro_needs_postproc(ctx, fmt))
161 return hanto_postproc_enum_framesizes(ctx, fsize);
164 } else if (fsize->index != 0) {
165 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
170 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
171 fsize->stepwise = fmt->frmsize;
176 static int vidioc_enum_fmt(struct file *file, void *priv,
177 struct v4l2_fmtdesc *f, bool capture)
180 struct hantro_ctx *ctx = fh_to_ctx(priv);
181 const struct hantro_fmt *fmt, *formats;
182 unsigned int num_fmts, i, j = 0;
186 * When dealing with an encoder:
187 * - on the capture side we want to filter out all MODE_NONE formats.
188 * - on the output side we want to filter out all formats that are
190 * When dealing with a decoder:
191 * - on the capture side we want to filter out all formats that are
193 * - on the output side we want to filter out all MODE_NONE formats.
195 skip_mode_none = capture == ctx->is_encoder;
197 formats = hantro_get_formats(ctx, &num_fmts);
198 for (i = 0; i < num_fmts; i++) {
199 bool mode_none = formats[i].codec_mode == HANTRO_MODE_NONE;
202 if (skip_mode_none == mode_none)
204 if (!hantro_check_depth_match(fmt, ctx->bit_depth))
207 f->pixelformat = fmt->fourcc;
214 * Enumerate post-processed formats. As per the specification,
215 * we enumerated these formats after natively decoded formats
216 * as a hint for applications on what's the preferred fomat.
220 formats = hantro_get_postproc_formats(ctx, &num_fmts);
221 for (i = 0; i < num_fmts; i++) {
224 if (!hantro_check_depth_match(fmt, ctx->bit_depth))
227 f->pixelformat = fmt->fourcc;
236 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
237 struct v4l2_fmtdesc *f)
239 return vidioc_enum_fmt(file, priv, f, true);
242 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
243 struct v4l2_fmtdesc *f)
245 return vidioc_enum_fmt(file, priv, f, false);
248 static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
249 struct v4l2_format *f)
251 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
252 struct hantro_ctx *ctx = fh_to_ctx(priv);
254 vpu_debug(4, "f->type = %d\n", f->type);
256 *pix_mp = ctx->src_fmt;
261 static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
262 struct v4l2_format *f)
264 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
265 struct hantro_ctx *ctx = fh_to_ctx(priv);
267 vpu_debug(4, "f->type = %d\n", f->type);
269 *pix_mp = ctx->dst_fmt;
274 static int hantro_try_fmt(const struct hantro_ctx *ctx,
275 struct v4l2_pix_format_mplane *pix_mp,
276 enum v4l2_buf_type type)
278 const struct hantro_fmt *fmt;
279 bool capture = V4L2_TYPE_IS_CAPTURE(type);
282 coded = capture == ctx->is_encoder;
284 vpu_debug(4, "trying format %c%c%c%c\n",
285 (pix_mp->pixelformat & 0x7f),
286 (pix_mp->pixelformat >> 8) & 0x7f,
287 (pix_mp->pixelformat >> 16) & 0x7f,
288 (pix_mp->pixelformat >> 24) & 0x7f);
290 fmt = hantro_find_format(ctx, pix_mp->pixelformat);
292 fmt = hantro_get_default_fmt(ctx, coded, HANTRO_DEFAULT_BIT_DEPTH);
293 pix_mp->pixelformat = fmt->fourcc;
297 pix_mp->num_planes = 1;
298 } else if (!ctx->is_encoder) {
300 * Width/height on the CAPTURE end of a decoder are ignored and
301 * replaced by the OUTPUT ones.
303 pix_mp->width = ctx->src_fmt.width;
304 pix_mp->height = ctx->src_fmt.height;
307 pix_mp->field = V4L2_FIELD_NONE;
309 v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
313 /* Fill remaining fields */
314 v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
316 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE &&
317 !hantro_needs_postproc(ctx, fmt))
318 pix_mp->plane_fmt[0].sizeimage +=
319 hantro_h264_mv_size(pix_mp->width,
321 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_VP9_FRAME &&
322 !hantro_needs_postproc(ctx, fmt))
323 pix_mp->plane_fmt[0].sizeimage +=
324 hantro_vp9_mv_size(pix_mp->width,
326 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_HEVC_SLICE &&
327 !hantro_needs_postproc(ctx, fmt))
328 pix_mp->plane_fmt[0].sizeimage +=
329 hantro_hevc_mv_size(pix_mp->width,
331 } else if (!pix_mp->plane_fmt[0].sizeimage) {
333 * For coded formats the application can specify
334 * sizeimage. If the application passes a zero sizeimage,
335 * let's default to the maximum frame size.
337 pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
338 pix_mp->width * pix_mp->height * fmt->max_depth;
344 static int vidioc_try_fmt_cap_mplane(struct file *file, void *priv,
345 struct v4l2_format *f)
347 return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
350 static int vidioc_try_fmt_out_mplane(struct file *file, void *priv,
351 struct v4l2_format *f)
353 return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
357 hantro_reset_fmt(struct v4l2_pix_format_mplane *fmt,
358 const struct hantro_fmt *vpu_fmt)
360 memset(fmt, 0, sizeof(*fmt));
362 fmt->pixelformat = vpu_fmt->fourcc;
363 fmt->field = V4L2_FIELD_NONE;
364 fmt->colorspace = V4L2_COLORSPACE_JPEG;
365 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
366 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
367 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
371 hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
373 const struct hantro_fmt *vpu_fmt;
374 struct v4l2_pix_format_mplane fmt;
376 vpu_fmt = hantro_get_default_fmt(ctx, true, HANTRO_DEFAULT_BIT_DEPTH);
380 hantro_reset_fmt(&fmt, vpu_fmt);
381 fmt.width = vpu_fmt->frmsize.min_width;
382 fmt.height = vpu_fmt->frmsize.min_height;
384 hantro_set_fmt_cap(ctx, &fmt);
386 hantro_set_fmt_out(ctx, &fmt);
390 hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth)
392 const struct hantro_fmt *raw_vpu_fmt;
393 struct v4l2_pix_format_mplane raw_fmt, *encoded_fmt;
396 raw_vpu_fmt = hantro_get_default_fmt(ctx, false, bit_depth);
400 if (ctx->is_encoder) {
401 encoded_fmt = &ctx->dst_fmt;
402 ctx->vpu_src_fmt = raw_vpu_fmt;
404 encoded_fmt = &ctx->src_fmt;
407 hantro_reset_fmt(&raw_fmt, raw_vpu_fmt);
408 raw_fmt.width = encoded_fmt->width;
409 raw_fmt.height = encoded_fmt->height;
411 ret = hantro_set_fmt_out(ctx, &raw_fmt);
413 ret = hantro_set_fmt_cap(ctx, &raw_fmt);
416 ctx->bit_depth = bit_depth;
421 void hantro_reset_fmts(struct hantro_ctx *ctx)
423 hantro_reset_encoded_fmt(ctx);
424 hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH);
428 hantro_update_requires_request(struct hantro_ctx *ctx, u32 fourcc)
431 case V4L2_PIX_FMT_JPEG:
432 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = false;
434 case V4L2_PIX_FMT_MPEG2_SLICE:
435 case V4L2_PIX_FMT_VP8_FRAME:
436 case V4L2_PIX_FMT_H264_SLICE:
437 case V4L2_PIX_FMT_HEVC_SLICE:
438 case V4L2_PIX_FMT_VP9_FRAME:
439 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
447 hantro_update_requires_hold_capture_buf(struct hantro_ctx *ctx, u32 fourcc)
449 struct vb2_queue *vq;
451 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
452 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
455 case V4L2_PIX_FMT_JPEG:
456 case V4L2_PIX_FMT_MPEG2_SLICE:
457 case V4L2_PIX_FMT_VP8_FRAME:
458 case V4L2_PIX_FMT_HEVC_SLICE:
459 case V4L2_PIX_FMT_VP9_FRAME:
460 vq->subsystem_flags &= ~(VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF);
462 case V4L2_PIX_FMT_H264_SLICE:
463 vq->subsystem_flags |= VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
470 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
471 struct v4l2_pix_format_mplane *pix_mp)
473 struct vb2_queue *vq;
476 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
477 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
478 ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
482 if (!ctx->is_encoder) {
483 struct vb2_queue *peer_vq;
486 * In order to support dynamic resolution change,
487 * the decoder admits a resolution change, as long
488 * as the pixelformat remains. Can't be done if streaming.
490 if (vb2_is_streaming(vq) || (vb2_is_busy(vq) &&
491 pix_mp->pixelformat != ctx->src_fmt.pixelformat))
494 * Since format change on the OUTPUT queue will reset
495 * the CAPTURE queue, we can't allow doing so
496 * when the CAPTURE queue has buffers allocated.
498 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
499 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
500 if (vb2_is_busy(peer_vq))
504 * The encoder doesn't admit a format change if
505 * there are OUTPUT buffers allocated.
511 ctx->vpu_src_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
512 ctx->src_fmt = *pix_mp;
515 * Current raw format might have become invalid with newly
516 * selected codec, so reset it to default just to be safe and
517 * keep internal driver state sane. User is mandated to set
518 * the raw format again after we return, so we don't need
520 * Note that hantro_reset_raw_fmt() also propagates size
521 * changes to the raw format.
523 if (!ctx->is_encoder)
524 hantro_reset_raw_fmt(ctx, hantro_get_format_depth(pix_mp->pixelformat));
526 /* Colorimetry information are always propagated. */
527 ctx->dst_fmt.colorspace = pix_mp->colorspace;
528 ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
529 ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
530 ctx->dst_fmt.quantization = pix_mp->quantization;
532 hantro_update_requires_request(ctx, pix_mp->pixelformat);
533 hantro_update_requires_hold_capture_buf(ctx, pix_mp->pixelformat);
535 vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
536 vpu_debug(0, "fmt - w: %d, h: %d\n",
537 pix_mp->width, pix_mp->height);
541 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
542 struct v4l2_pix_format_mplane *pix_mp)
544 struct vb2_queue *vq;
547 /* Change not allowed if queue is busy. */
548 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
549 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
553 if (ctx->is_encoder) {
554 struct vb2_queue *peer_vq;
557 * Since format change on the CAPTURE queue will reset
558 * the OUTPUT queue, we can't allow doing so
559 * when the OUTPUT queue has buffers allocated.
561 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
562 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
563 if (vb2_is_busy(peer_vq) &&
564 (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
565 pix_mp->height != ctx->dst_fmt.height ||
566 pix_mp->width != ctx->dst_fmt.width))
570 ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
574 ctx->vpu_dst_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
575 ctx->dst_fmt = *pix_mp;
578 * Current raw format might have become invalid with newly
579 * selected codec, so reset it to default just to be safe and
580 * keep internal driver state sane. User is mandated to set
581 * the raw format again after we return, so we don't need
583 * Note that hantro_reset_raw_fmt() also propagates size
584 * changes to the raw format.
587 hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH);
589 /* Colorimetry information are always propagated. */
590 ctx->src_fmt.colorspace = pix_mp->colorspace;
591 ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
592 ctx->src_fmt.xfer_func = pix_mp->xfer_func;
593 ctx->src_fmt.quantization = pix_mp->quantization;
595 vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
596 vpu_debug(0, "fmt - w: %d, h: %d\n",
597 pix_mp->width, pix_mp->height);
599 hantro_update_requires_request(ctx, pix_mp->pixelformat);
605 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
607 return hantro_set_fmt_out(fh_to_ctx(priv), &f->fmt.pix_mp);
611 vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
613 return hantro_set_fmt_cap(fh_to_ctx(priv), &f->fmt.pix_mp);
616 static int vidioc_g_selection(struct file *file, void *priv,
617 struct v4l2_selection *sel)
619 struct hantro_ctx *ctx = fh_to_ctx(priv);
621 /* Crop only supported on source. */
622 if (!ctx->is_encoder ||
623 sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
626 switch (sel->target) {
627 case V4L2_SEL_TGT_CROP_DEFAULT:
628 case V4L2_SEL_TGT_CROP_BOUNDS:
631 sel->r.width = ctx->src_fmt.width;
632 sel->r.height = ctx->src_fmt.height;
634 case V4L2_SEL_TGT_CROP:
637 sel->r.width = ctx->dst_fmt.width;
638 sel->r.height = ctx->dst_fmt.height;
647 static int vidioc_s_selection(struct file *file, void *priv,
648 struct v4l2_selection *sel)
650 struct hantro_ctx *ctx = fh_to_ctx(priv);
651 struct v4l2_rect *rect = &sel->r;
652 struct vb2_queue *vq;
654 /* Crop only supported on source. */
655 if (!ctx->is_encoder ||
656 sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
659 /* Change not allowed if the queue is streaming. */
660 vq = v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx);
661 if (vb2_is_streaming(vq))
664 if (sel->target != V4L2_SEL_TGT_CROP)
668 * We do not support offsets, and we can crop only inside
669 * right-most or bottom-most macroblocks.
671 if (rect->left != 0 || rect->top != 0 ||
672 round_up(rect->width, MB_DIM) != ctx->src_fmt.width ||
673 round_up(rect->height, MB_DIM) != ctx->src_fmt.height) {
674 /* Default to full frame for incorrect settings. */
677 rect->width = ctx->src_fmt.width;
678 rect->height = ctx->src_fmt.height;
680 /* We support widths aligned to 4 pixels and arbitrary heights. */
681 rect->width = round_up(rect->width, 4);
684 ctx->dst_fmt.width = rect->width;
685 ctx->dst_fmt.height = rect->height;
690 static const struct v4l2_event hantro_eos_event = {
691 .type = V4L2_EVENT_EOS
694 static int vidioc_encoder_cmd(struct file *file, void *priv,
695 struct v4l2_encoder_cmd *ec)
697 struct hantro_ctx *ctx = fh_to_ctx(priv);
700 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec);
704 if (!vb2_is_streaming(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx)) ||
705 !vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)))
708 ret = v4l2_m2m_ioctl_encoder_cmd(file, priv, ec);
712 if (ec->cmd == V4L2_ENC_CMD_STOP &&
713 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
714 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
716 if (ec->cmd == V4L2_ENC_CMD_START)
717 vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q);
722 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
723 .vidioc_querycap = vidioc_querycap,
724 .vidioc_enum_framesizes = vidioc_enum_framesizes,
726 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
727 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
728 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
729 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
730 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
731 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
732 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
733 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
735 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
736 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
737 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
738 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
739 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
740 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
741 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
743 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
744 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
746 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
747 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
749 .vidioc_g_selection = vidioc_g_selection,
750 .vidioc_s_selection = vidioc_s_selection,
752 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
753 .vidioc_encoder_cmd = vidioc_encoder_cmd,
757 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
758 unsigned int *num_planes, unsigned int sizes[],
759 struct device *alloc_devs[])
761 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
762 struct v4l2_pix_format_mplane *pixfmt;
766 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
767 pixfmt = &ctx->dst_fmt;
769 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
770 pixfmt = &ctx->src_fmt;
773 vpu_err("invalid queue type: %d\n", vq->type);
778 if (*num_planes != pixfmt->num_planes)
780 for (i = 0; i < pixfmt->num_planes; ++i)
781 if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
786 *num_planes = pixfmt->num_planes;
787 for (i = 0; i < pixfmt->num_planes; ++i)
788 sizes[i] = pixfmt->plane_fmt[i].sizeimage;
793 hantro_buf_plane_check(struct vb2_buffer *vb,
794 struct v4l2_pix_format_mplane *pixfmt)
799 for (i = 0; i < pixfmt->num_planes; ++i) {
800 sz = pixfmt->plane_fmt[i].sizeimage;
801 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
802 i, vb2_plane_size(vb, i), sz);
803 if (vb2_plane_size(vb, i) < sz) {
804 vpu_err("plane %d is too small for output\n", i);
811 static int hantro_buf_prepare(struct vb2_buffer *vb)
813 struct vb2_queue *vq = vb->vb2_queue;
814 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
815 struct v4l2_pix_format_mplane *pix_fmt;
818 if (V4L2_TYPE_IS_OUTPUT(vq->type))
819 pix_fmt = &ctx->src_fmt;
821 pix_fmt = &ctx->dst_fmt;
822 ret = hantro_buf_plane_check(vb, pix_fmt);
826 * Buffer's bytesused must be written by driver for CAPTURE buffers.
827 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
828 * it to buffer length).
830 if (V4L2_TYPE_IS_CAPTURE(vq->type)) {
832 vb2_set_plane_payload(vb, 0, 0);
834 vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
840 static void hantro_buf_queue(struct vb2_buffer *vb)
842 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
843 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
845 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
846 vb2_is_streaming(vb->vb2_queue) &&
847 v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
850 for (i = 0; i < vb->num_planes; i++)
851 vb2_set_plane_payload(vb, i, 0);
853 vbuf->field = V4L2_FIELD_NONE;
854 vbuf->sequence = ctx->sequence_cap++;
856 v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
857 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
861 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
864 static bool hantro_vq_is_coded(struct vb2_queue *q)
866 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
868 return ctx->is_encoder != V4L2_TYPE_IS_OUTPUT(q->type);
871 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
873 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
876 v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
878 if (V4L2_TYPE_IS_OUTPUT(q->type))
879 ctx->sequence_out = 0;
881 ctx->sequence_cap = 0;
883 if (hantro_vq_is_coded(q)) {
884 enum hantro_codec_mode codec_mode;
886 if (V4L2_TYPE_IS_OUTPUT(q->type))
887 codec_mode = ctx->vpu_src_fmt->codec_mode;
889 codec_mode = ctx->vpu_dst_fmt->codec_mode;
891 vpu_debug(4, "Codec mode = %d\n", codec_mode);
892 ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
893 if (ctx->codec_ops->init) {
894 ret = ctx->codec_ops->init(ctx);
899 if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) {
900 ret = hantro_postproc_alloc(ctx);
908 if (ctx->codec_ops->exit)
909 ctx->codec_ops->exit(ctx);
914 hantro_return_bufs(struct vb2_queue *q,
915 struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
917 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
920 struct vb2_v4l2_buffer *vbuf;
922 vbuf = buf_remove(ctx->fh.m2m_ctx);
925 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
927 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
931 static void hantro_stop_streaming(struct vb2_queue *q)
933 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
935 if (hantro_vq_is_coded(q)) {
936 hantro_postproc_free(ctx);
937 if (ctx->codec_ops && ctx->codec_ops->exit)
938 ctx->codec_ops->exit(ctx);
942 * The mem2mem framework calls v4l2_m2m_cancel_job before
943 * .stop_streaming, so there isn't any job running and
944 * it is safe to return all the buffers.
946 if (V4L2_TYPE_IS_OUTPUT(q->type))
947 hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
949 hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
951 v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
953 if (V4L2_TYPE_IS_OUTPUT(q->type) &&
954 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
955 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
958 static void hantro_buf_request_complete(struct vb2_buffer *vb)
960 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
962 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
965 static int hantro_buf_out_validate(struct vb2_buffer *vb)
967 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
969 vbuf->field = V4L2_FIELD_NONE;
973 const struct vb2_ops hantro_queue_ops = {
974 .queue_setup = hantro_queue_setup,
975 .buf_prepare = hantro_buf_prepare,
976 .buf_queue = hantro_buf_queue,
977 .buf_out_validate = hantro_buf_out_validate,
978 .buf_request_complete = hantro_buf_request_complete,
979 .start_streaming = hantro_start_streaming,
980 .stop_streaming = hantro_stop_streaming,
981 .wait_prepare = vb2_ops_wait_prepare,
982 .wait_finish = vb2_ops_wait_finish,