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,
36 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
37 struct v4l2_pix_format_mplane *pix_mp);
39 static const struct hantro_fmt *
40 hantro_get_formats(const struct hantro_ctx *ctx, unsigned int *num_fmts, bool need_postproc)
42 const struct hantro_fmt *formats;
49 if (ctx->is_encoder) {
50 formats = ctx->dev->variant->enc_fmts;
51 *num_fmts = ctx->dev->variant->num_enc_fmts;
53 formats = ctx->dev->variant->dec_fmts;
54 *num_fmts = ctx->dev->variant->num_dec_fmts;
60 static const struct hantro_fmt *
61 hantro_get_postproc_formats(const struct hantro_ctx *ctx,
62 unsigned int *num_fmts)
64 struct hantro_dev *vpu = ctx->dev;
66 if (ctx->is_encoder || !vpu->variant->postproc_fmts) {
71 *num_fmts = ctx->dev->variant->num_postproc_fmts;
72 return ctx->dev->variant->postproc_fmts;
75 int hantro_get_format_depth(u32 fourcc)
78 case V4L2_PIX_FMT_P010:
79 case V4L2_PIX_FMT_P010_4L4:
80 case V4L2_PIX_FMT_NV15_4L4:
88 hantro_check_depth_match(const struct hantro_fmt *fmt, int bit_depth)
92 if (!fmt->match_depth && !fmt->postprocessed)
95 /* 0 means default depth, which is 8 */
97 bit_depth = HANTRO_DEFAULT_BIT_DEPTH;
99 fmt_depth = hantro_get_format_depth(fmt->fourcc);
102 * Allow only downconversion for postproc formats for now.
103 * It may be possible to relax that on some HW.
105 if (!fmt->match_depth)
106 return fmt_depth <= bit_depth;
108 return fmt_depth == bit_depth;
111 static const struct hantro_fmt *
112 hantro_find_format(const struct hantro_ctx *ctx, u32 fourcc)
114 const struct hantro_fmt *formats;
115 unsigned int i, num_fmts;
117 formats = hantro_get_formats(ctx, &num_fmts, HANTRO_AUTO_POSTPROC);
118 for (i = 0; i < num_fmts; i++)
119 if (formats[i].fourcc == fourcc)
122 formats = hantro_get_postproc_formats(ctx, &num_fmts);
123 for (i = 0; i < num_fmts; i++)
124 if (formats[i].fourcc == fourcc)
129 const struct hantro_fmt *
130 hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream,
131 int bit_depth, bool need_postproc)
133 const struct hantro_fmt *formats;
134 unsigned int i, num_fmts;
136 formats = hantro_get_formats(ctx, &num_fmts, need_postproc);
137 for (i = 0; i < num_fmts; i++) {
138 if (bitstream == (formats[i].codec_mode !=
140 hantro_check_depth_match(&formats[i], bit_depth))
144 formats = hantro_get_postproc_formats(ctx, &num_fmts);
145 for (i = 0; i < num_fmts; i++) {
146 if (bitstream == (formats[i].codec_mode !=
148 hantro_check_depth_match(&formats[i], bit_depth))
155 static int vidioc_querycap(struct file *file, void *priv,
156 struct v4l2_capability *cap)
158 struct hantro_dev *vpu = video_drvdata(file);
159 struct video_device *vdev = video_devdata(file);
161 strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
162 strscpy(cap->card, vdev->name, sizeof(cap->card));
166 static int vidioc_enum_framesizes(struct file *file, void *priv,
167 struct v4l2_frmsizeenum *fsize)
169 struct hantro_ctx *ctx = fh_to_ctx(priv);
170 const struct hantro_fmt *fmt;
172 fmt = hantro_find_format(ctx, fsize->pixel_format);
174 vpu_debug(0, "unsupported bitstream format (%08x)\n",
175 fsize->pixel_format);
179 /* For non-coded formats check if postprocessing scaling is possible */
180 if (fmt->codec_mode == HANTRO_MODE_NONE) {
181 if (hantro_needs_postproc(ctx, fmt))
182 return hanto_postproc_enum_framesizes(ctx, fsize);
185 } else if (fsize->index != 0) {
186 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
191 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
192 fsize->stepwise = fmt->frmsize;
197 static int vidioc_enum_fmt(struct file *file, void *priv,
198 struct v4l2_fmtdesc *f, bool capture)
201 struct hantro_ctx *ctx = fh_to_ctx(priv);
202 const struct hantro_fmt *fmt, *formats;
203 unsigned int num_fmts, i, j = 0;
207 * When dealing with an encoder:
208 * - on the capture side we want to filter out all MODE_NONE formats.
209 * - on the output side we want to filter out all formats that are
211 * When dealing with a decoder:
212 * - on the capture side we want to filter out all formats that are
214 * - on the output side we want to filter out all MODE_NONE formats.
216 skip_mode_none = capture == ctx->is_encoder;
218 formats = hantro_get_formats(ctx, &num_fmts, HANTRO_AUTO_POSTPROC);
219 for (i = 0; i < num_fmts; i++) {
220 bool mode_none = formats[i].codec_mode == HANTRO_MODE_NONE;
223 if (skip_mode_none == mode_none)
225 if (!hantro_check_depth_match(fmt, ctx->bit_depth))
228 f->pixelformat = fmt->fourcc;
235 * Enumerate post-processed formats. As per the specification,
236 * we enumerated these formats after natively decoded formats
237 * as a hint for applications on what's the preferred fomat.
241 formats = hantro_get_postproc_formats(ctx, &num_fmts);
242 for (i = 0; i < num_fmts; i++) {
245 if (!hantro_check_depth_match(fmt, ctx->bit_depth))
248 f->pixelformat = fmt->fourcc;
257 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
258 struct v4l2_fmtdesc *f)
260 return vidioc_enum_fmt(file, priv, f, true);
263 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
264 struct v4l2_fmtdesc *f)
266 return vidioc_enum_fmt(file, priv, f, false);
269 static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
270 struct v4l2_format *f)
272 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
273 struct hantro_ctx *ctx = fh_to_ctx(priv);
275 vpu_debug(4, "f->type = %d\n", f->type);
277 *pix_mp = ctx->src_fmt;
282 static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
283 struct v4l2_format *f)
285 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
286 struct hantro_ctx *ctx = fh_to_ctx(priv);
288 vpu_debug(4, "f->type = %d\n", f->type);
290 *pix_mp = ctx->dst_fmt;
295 static int hantro_try_fmt(const struct hantro_ctx *ctx,
296 struct v4l2_pix_format_mplane *pix_mp,
297 enum v4l2_buf_type type)
299 const struct hantro_fmt *fmt;
300 const struct hantro_fmt *vpu_fmt;
301 bool capture = V4L2_TYPE_IS_CAPTURE(type);
304 coded = capture == ctx->is_encoder;
306 vpu_debug(4, "trying format %c%c%c%c\n",
307 (pix_mp->pixelformat & 0x7f),
308 (pix_mp->pixelformat >> 8) & 0x7f,
309 (pix_mp->pixelformat >> 16) & 0x7f,
310 (pix_mp->pixelformat >> 24) & 0x7f);
312 fmt = hantro_find_format(ctx, pix_mp->pixelformat);
314 fmt = hantro_get_default_fmt(ctx, coded, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
315 pix_mp->pixelformat = fmt->fourcc;
319 pix_mp->num_planes = 1;
321 } else if (ctx->is_encoder) {
322 vpu_fmt = hantro_find_format(ctx, ctx->dst_fmt.pixelformat);
325 * Width/height on the CAPTURE end of a decoder are ignored and
326 * replaced by the OUTPUT ones.
328 pix_mp->width = ctx->src_fmt.width;
329 pix_mp->height = ctx->src_fmt.height;
333 pix_mp->field = V4L2_FIELD_NONE;
335 v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
339 /* Fill remaining fields */
340 v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
342 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE &&
343 !hantro_needs_postproc(ctx, fmt))
344 pix_mp->plane_fmt[0].sizeimage +=
345 hantro_h264_mv_size(pix_mp->width,
347 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_VP9_FRAME &&
348 !hantro_needs_postproc(ctx, fmt))
349 pix_mp->plane_fmt[0].sizeimage +=
350 hantro_vp9_mv_size(pix_mp->width,
352 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_HEVC_SLICE &&
353 !hantro_needs_postproc(ctx, fmt))
354 pix_mp->plane_fmt[0].sizeimage +=
355 hantro_hevc_mv_size(pix_mp->width,
357 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_AV1_FRAME &&
358 !hantro_needs_postproc(ctx, fmt))
359 pix_mp->plane_fmt[0].sizeimage +=
360 hantro_av1_mv_size(pix_mp->width,
362 } else if (!pix_mp->plane_fmt[0].sizeimage) {
364 * For coded formats the application can specify
365 * sizeimage. If the application passes a zero sizeimage,
366 * let's default to the maximum frame size.
368 pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
369 pix_mp->width * pix_mp->height * fmt->max_depth;
375 static int vidioc_try_fmt_cap_mplane(struct file *file, void *priv,
376 struct v4l2_format *f)
378 return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
381 static int vidioc_try_fmt_out_mplane(struct file *file, void *priv,
382 struct v4l2_format *f)
384 return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type);
388 hantro_reset_fmt(struct v4l2_pix_format_mplane *fmt,
389 const struct hantro_fmt *vpu_fmt)
391 memset(fmt, 0, sizeof(*fmt));
393 fmt->pixelformat = vpu_fmt->fourcc;
394 fmt->field = V4L2_FIELD_NONE;
395 fmt->colorspace = V4L2_COLORSPACE_JPEG;
396 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
397 fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
398 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
402 hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
404 const struct hantro_fmt *vpu_fmt;
405 struct v4l2_pix_format_mplane fmt;
407 vpu_fmt = hantro_get_default_fmt(ctx, true, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
411 hantro_reset_fmt(&fmt, vpu_fmt);
412 fmt.width = vpu_fmt->frmsize.min_width;
413 fmt.height = vpu_fmt->frmsize.min_height;
415 hantro_set_fmt_cap(ctx, &fmt);
417 hantro_set_fmt_out(ctx, &fmt, HANTRO_AUTO_POSTPROC);
421 hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth, bool need_postproc)
423 const struct hantro_fmt *raw_vpu_fmt;
424 struct v4l2_pix_format_mplane raw_fmt, *encoded_fmt;
427 raw_vpu_fmt = hantro_get_default_fmt(ctx, false, bit_depth, need_postproc);
431 if (ctx->is_encoder) {
432 encoded_fmt = &ctx->dst_fmt;
433 ctx->vpu_src_fmt = raw_vpu_fmt;
435 encoded_fmt = &ctx->src_fmt;
438 hantro_reset_fmt(&raw_fmt, raw_vpu_fmt);
439 raw_fmt.width = encoded_fmt->width;
440 raw_fmt.height = encoded_fmt->height;
442 ret = hantro_set_fmt_out(ctx, &raw_fmt, need_postproc);
444 ret = hantro_set_fmt_cap(ctx, &raw_fmt);
447 ctx->bit_depth = bit_depth;
448 ctx->need_postproc = need_postproc;
454 void hantro_reset_fmts(struct hantro_ctx *ctx)
456 hantro_reset_encoded_fmt(ctx);
457 hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
461 hantro_update_requires_request(struct hantro_ctx *ctx, u32 fourcc)
464 case V4L2_PIX_FMT_JPEG:
465 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = false;
467 case V4L2_PIX_FMT_MPEG2_SLICE:
468 case V4L2_PIX_FMT_VP8_FRAME:
469 case V4L2_PIX_FMT_H264_SLICE:
470 case V4L2_PIX_FMT_HEVC_SLICE:
471 case V4L2_PIX_FMT_VP9_FRAME:
472 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
480 hantro_update_requires_hold_capture_buf(struct hantro_ctx *ctx, u32 fourcc)
482 struct vb2_queue *vq;
484 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
485 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
488 case V4L2_PIX_FMT_JPEG:
489 case V4L2_PIX_FMT_MPEG2_SLICE:
490 case V4L2_PIX_FMT_VP8_FRAME:
491 case V4L2_PIX_FMT_HEVC_SLICE:
492 case V4L2_PIX_FMT_VP9_FRAME:
493 vq->subsystem_flags &= ~(VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF);
495 case V4L2_PIX_FMT_H264_SLICE:
496 vq->subsystem_flags |= VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
503 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
504 struct v4l2_pix_format_mplane *pix_mp,
507 struct vb2_queue *vq;
510 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
511 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
512 ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
516 if (!ctx->is_encoder) {
517 struct vb2_queue *peer_vq;
520 * In order to support dynamic resolution change,
521 * the decoder admits a resolution change, as long
522 * as the pixelformat remains. Can't be done if streaming.
524 if (vb2_is_streaming(vq) || (vb2_is_busy(vq) &&
525 pix_mp->pixelformat != ctx->src_fmt.pixelformat))
528 * Since format change on the OUTPUT queue will reset
529 * the CAPTURE queue, we can't allow doing so
530 * when the CAPTURE queue has buffers allocated.
532 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
533 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
534 if (vb2_is_busy(peer_vq))
538 * The encoder doesn't admit a format change if
539 * there are OUTPUT buffers allocated.
545 ctx->vpu_src_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
546 ctx->src_fmt = *pix_mp;
549 * Current raw format might have become invalid with newly
550 * selected codec, so reset it to default just to be safe and
551 * keep internal driver state sane. User is mandated to set
552 * the raw format again after we return, so we don't need
554 * Note that hantro_reset_raw_fmt() also propagates size
555 * changes to the raw format.
557 if (!ctx->is_encoder)
558 hantro_reset_raw_fmt(ctx,
559 hantro_get_format_depth(pix_mp->pixelformat),
562 /* Colorimetry information are always propagated. */
563 ctx->dst_fmt.colorspace = pix_mp->colorspace;
564 ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
565 ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
566 ctx->dst_fmt.quantization = pix_mp->quantization;
568 hantro_update_requires_request(ctx, pix_mp->pixelformat);
569 hantro_update_requires_hold_capture_buf(ctx, pix_mp->pixelformat);
571 vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
572 vpu_debug(0, "fmt - w: %d, h: %d\n",
573 pix_mp->width, pix_mp->height);
577 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
578 struct v4l2_pix_format_mplane *pix_mp)
580 struct vb2_queue *vq;
583 /* Change not allowed if queue is busy. */
584 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
585 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
589 if (ctx->is_encoder) {
590 struct vb2_queue *peer_vq;
593 * Since format change on the CAPTURE queue will reset
594 * the OUTPUT queue, we can't allow doing so
595 * when the OUTPUT queue has buffers allocated.
597 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
598 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
599 if (vb2_is_busy(peer_vq) &&
600 (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
601 pix_mp->height != ctx->dst_fmt.height ||
602 pix_mp->width != ctx->dst_fmt.width))
606 ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
610 ctx->vpu_dst_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
611 ctx->dst_fmt = *pix_mp;
614 * Current raw format might have become invalid with newly
615 * selected codec, so reset it to default just to be safe and
616 * keep internal driver state sane. User is mandated to set
617 * the raw format again after we return, so we don't need
619 * Note that hantro_reset_raw_fmt() also propagates size
620 * changes to the raw format.
623 hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
625 /* Colorimetry information are always propagated. */
626 ctx->src_fmt.colorspace = pix_mp->colorspace;
627 ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
628 ctx->src_fmt.xfer_func = pix_mp->xfer_func;
629 ctx->src_fmt.quantization = pix_mp->quantization;
631 vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
632 vpu_debug(0, "fmt - w: %d, h: %d\n",
633 pix_mp->width, pix_mp->height);
635 hantro_update_requires_request(ctx, pix_mp->pixelformat);
641 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
643 return hantro_set_fmt_out(fh_to_ctx(priv), &f->fmt.pix_mp, HANTRO_AUTO_POSTPROC);
647 vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
649 return hantro_set_fmt_cap(fh_to_ctx(priv), &f->fmt.pix_mp);
652 static int vidioc_g_selection(struct file *file, void *priv,
653 struct v4l2_selection *sel)
655 struct hantro_ctx *ctx = fh_to_ctx(priv);
657 /* Crop only supported on source. */
658 if (!ctx->is_encoder ||
659 sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
662 switch (sel->target) {
663 case V4L2_SEL_TGT_CROP_DEFAULT:
664 case V4L2_SEL_TGT_CROP_BOUNDS:
667 sel->r.width = ctx->src_fmt.width;
668 sel->r.height = ctx->src_fmt.height;
670 case V4L2_SEL_TGT_CROP:
673 sel->r.width = ctx->dst_fmt.width;
674 sel->r.height = ctx->dst_fmt.height;
683 static int vidioc_s_selection(struct file *file, void *priv,
684 struct v4l2_selection *sel)
686 struct hantro_ctx *ctx = fh_to_ctx(priv);
687 struct v4l2_rect *rect = &sel->r;
688 struct vb2_queue *vq;
690 /* Crop only supported on source. */
691 if (!ctx->is_encoder ||
692 sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
695 /* Change not allowed if the queue is streaming. */
696 vq = v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx);
697 if (vb2_is_streaming(vq))
700 if (sel->target != V4L2_SEL_TGT_CROP)
704 * We do not support offsets, and we can crop only inside
705 * right-most or bottom-most macroblocks.
707 if (rect->left != 0 || rect->top != 0 ||
708 round_up(rect->width, MB_DIM) != ctx->src_fmt.width ||
709 round_up(rect->height, MB_DIM) != ctx->src_fmt.height) {
710 /* Default to full frame for incorrect settings. */
713 rect->width = ctx->src_fmt.width;
714 rect->height = ctx->src_fmt.height;
716 /* We support widths aligned to 4 pixels and arbitrary heights. */
717 rect->width = round_up(rect->width, 4);
720 ctx->dst_fmt.width = rect->width;
721 ctx->dst_fmt.height = rect->height;
726 static const struct v4l2_event hantro_eos_event = {
727 .type = V4L2_EVENT_EOS
730 static int vidioc_encoder_cmd(struct file *file, void *priv,
731 struct v4l2_encoder_cmd *ec)
733 struct hantro_ctx *ctx = fh_to_ctx(priv);
736 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec);
740 if (!vb2_is_streaming(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx)) ||
741 !vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx)))
744 ret = v4l2_m2m_ioctl_encoder_cmd(file, priv, ec);
748 if (ec->cmd == V4L2_ENC_CMD_STOP &&
749 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
750 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
752 if (ec->cmd == V4L2_ENC_CMD_START)
753 vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q);
758 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
759 .vidioc_querycap = vidioc_querycap,
760 .vidioc_enum_framesizes = vidioc_enum_framesizes,
762 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
763 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
764 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
765 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
766 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
767 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
768 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
769 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
771 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
772 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
773 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
774 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
775 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
776 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
777 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
779 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
780 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
782 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
783 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
785 .vidioc_g_selection = vidioc_g_selection,
786 .vidioc_s_selection = vidioc_s_selection,
788 .vidioc_decoder_cmd = v4l2_m2m_ioctl_stateless_decoder_cmd,
789 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_stateless_try_decoder_cmd,
791 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
792 .vidioc_encoder_cmd = vidioc_encoder_cmd,
796 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
797 unsigned int *num_planes, unsigned int sizes[],
798 struct device *alloc_devs[])
800 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
801 struct v4l2_pix_format_mplane *pixfmt;
805 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
806 pixfmt = &ctx->dst_fmt;
808 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
809 pixfmt = &ctx->src_fmt;
812 vpu_err("invalid queue type: %d\n", vq->type);
817 if (*num_planes != pixfmt->num_planes)
819 for (i = 0; i < pixfmt->num_planes; ++i)
820 if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
825 *num_planes = pixfmt->num_planes;
826 for (i = 0; i < pixfmt->num_planes; ++i)
827 sizes[i] = pixfmt->plane_fmt[i].sizeimage;
832 hantro_buf_plane_check(struct vb2_buffer *vb,
833 struct v4l2_pix_format_mplane *pixfmt)
838 for (i = 0; i < pixfmt->num_planes; ++i) {
839 sz = pixfmt->plane_fmt[i].sizeimage;
840 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
841 i, vb2_plane_size(vb, i), sz);
842 if (vb2_plane_size(vb, i) < sz) {
843 vpu_err("plane %d is too small for output\n", i);
850 static int hantro_buf_prepare(struct vb2_buffer *vb)
852 struct vb2_queue *vq = vb->vb2_queue;
853 struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
854 struct v4l2_pix_format_mplane *pix_fmt;
857 if (V4L2_TYPE_IS_OUTPUT(vq->type))
858 pix_fmt = &ctx->src_fmt;
860 pix_fmt = &ctx->dst_fmt;
861 ret = hantro_buf_plane_check(vb, pix_fmt);
865 * Buffer's bytesused must be written by driver for CAPTURE buffers.
866 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets
867 * it to buffer length).
869 if (V4L2_TYPE_IS_CAPTURE(vq->type)) {
871 vb2_set_plane_payload(vb, 0, 0);
873 vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
879 static void hantro_buf_queue(struct vb2_buffer *vb)
881 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
882 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
884 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
885 vb2_is_streaming(vb->vb2_queue) &&
886 v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
889 for (i = 0; i < vb->num_planes; i++)
890 vb2_set_plane_payload(vb, i, 0);
892 vbuf->field = V4L2_FIELD_NONE;
893 vbuf->sequence = ctx->sequence_cap++;
895 v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
896 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
900 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
903 static bool hantro_vq_is_coded(struct vb2_queue *q)
905 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
907 return ctx->is_encoder != V4L2_TYPE_IS_OUTPUT(q->type);
910 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
912 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
915 v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
917 if (V4L2_TYPE_IS_OUTPUT(q->type))
918 ctx->sequence_out = 0;
920 ctx->sequence_cap = 0;
922 if (hantro_vq_is_coded(q)) {
923 enum hantro_codec_mode codec_mode;
925 if (V4L2_TYPE_IS_OUTPUT(q->type))
926 codec_mode = ctx->vpu_src_fmt->codec_mode;
928 codec_mode = ctx->vpu_dst_fmt->codec_mode;
930 vpu_debug(4, "Codec mode = %d\n", codec_mode);
931 ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
932 if (ctx->codec_ops->init) {
933 ret = ctx->codec_ops->init(ctx);
938 if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) {
939 ret = hantro_postproc_alloc(ctx);
947 if (ctx->codec_ops->exit)
948 ctx->codec_ops->exit(ctx);
953 hantro_return_bufs(struct vb2_queue *q,
954 struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
956 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
959 struct vb2_v4l2_buffer *vbuf;
961 vbuf = buf_remove(ctx->fh.m2m_ctx);
964 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
966 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
970 static void hantro_stop_streaming(struct vb2_queue *q)
972 struct hantro_ctx *ctx = vb2_get_drv_priv(q);
974 if (hantro_vq_is_coded(q)) {
975 hantro_postproc_free(ctx);
976 if (ctx->codec_ops && ctx->codec_ops->exit)
977 ctx->codec_ops->exit(ctx);
981 * The mem2mem framework calls v4l2_m2m_cancel_job before
982 * .stop_streaming, so there isn't any job running and
983 * it is safe to return all the buffers.
985 if (V4L2_TYPE_IS_OUTPUT(q->type))
986 hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
988 hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
990 v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
992 if (V4L2_TYPE_IS_OUTPUT(q->type) &&
993 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
994 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event);
997 static void hantro_buf_request_complete(struct vb2_buffer *vb)
999 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1001 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
1004 static int hantro_buf_out_validate(struct vb2_buffer *vb)
1006 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1008 vbuf->field = V4L2_FIELD_NONE;
1012 const struct vb2_ops hantro_queue_ops = {
1013 .queue_setup = hantro_queue_setup,
1014 .buf_prepare = hantro_buf_prepare,
1015 .buf_queue = hantro_buf_queue,
1016 .buf_out_validate = hantro_buf_out_validate,
1017 .buf_request_complete = hantro_buf_request_complete,
1018 .start_streaming = hantro_start_streaming,
1019 .stop_streaming = hantro_stop_streaming,
1020 .wait_prepare = vb2_ops_wait_prepare,
1021 .wait_finish = vb2_ops_wait_finish,