1 // SPDX-License-Identifier: GPL-2.0
3 * Raspberry Pi HEVC driver
5 * Copyright (C) 2020 Raspberry Pi (Trading) Ltd
7 * Based on the Cedrus VPU driver, that is:
9 * Copyright (C) 2016 Florent Revest <florent.revest@free-electrons.com>
10 * Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
11 * Copyright (C) 2018 Bootlin
14 #include <media/videobuf2-dma-contig.h>
15 #include <media/v4l2-device.h>
16 #include <media/v4l2-ioctl.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-mem2mem.h>
21 #include "rpivid_hw.h"
22 #include "rpivid_video.h"
23 #include "rpivid_dec.h"
25 #define RPIVID_DECODE_SRC BIT(0)
26 #define RPIVID_DECODE_DST BIT(1)
28 #define RPIVID_MIN_WIDTH 16U
29 #define RPIVID_MIN_HEIGHT 16U
30 #define RPIVID_DEFAULT_WIDTH 1920U
31 #define RPIVID_DEFAULT_HEIGHT 1088U
32 #define RPIVID_MAX_WIDTH 4096U
33 #define RPIVID_MAX_HEIGHT 4096U
35 static inline struct rpivid_ctx *rpivid_file2ctx(struct file *file)
37 return container_of(file->private_data, struct rpivid_ctx, fh);
40 /* constrain x to y,y*2 */
41 static inline unsigned int constrain2x(unsigned int x, unsigned int y)
48 size_t rpivid_round_up_size(const size_t x)
50 /* Admit no size < 256 */
51 const unsigned int n = x < 256 ? 8 : ilog2(x);
53 return x >= (3 << n) ? 4 << n : (3 << n);
56 size_t rpivid_bit_buf_size(unsigned int w, unsigned int h, unsigned int bits_minus8)
58 const size_t wxh = w * h;
61 /* Annex A gives a min compression of 2 @ lvl 3.1
62 * (wxh <= 983040) and min 4 thereafter but avoid
63 * the odity of 983041 having a lower limit than
65 * Multiply by 3/2 for 4:2:0
67 bits_alloc = wxh < 983040 ? wxh * 3 / 4 :
68 wxh < 983040 * 2 ? 983040 * 3 / 4 :
70 /* Allow for bit depth */
71 bits_alloc += (bits_alloc * bits_minus8) / 8;
72 return rpivid_round_up_size(bits_alloc);
75 void rpivid_prepare_src_format(struct v4l2_pix_format_mplane *pix_fmt)
84 w = RPIVID_DEFAULT_WIDTH;
85 h = RPIVID_DEFAULT_HEIGHT;
87 if (w > RPIVID_MAX_WIDTH)
89 if (h > RPIVID_MAX_HEIGHT)
90 h = RPIVID_MAX_HEIGHT;
92 if (!pix_fmt->plane_fmt[0].sizeimage ||
93 pix_fmt->plane_fmt[0].sizeimage > SZ_32M) {
94 /* Unspecified or way too big - pick max for size */
95 size = rpivid_bit_buf_size(w, h, 2);
98 size = max_t(u32, SZ_4K, pix_fmt->plane_fmt[0].sizeimage);
100 pix_fmt->pixelformat = V4L2_PIX_FMT_HEVC_SLICE;
103 pix_fmt->num_planes = 1;
104 pix_fmt->field = V4L2_FIELD_NONE;
105 /* Zero bytes per line for encoded source. */
106 pix_fmt->plane_fmt[0].bytesperline = 0;
107 pix_fmt->plane_fmt[0].sizeimage = size;
110 /* Take any pix_format and make it valid */
111 static void rpivid_prepare_dst_format(struct v4l2_pix_format_mplane *pix_fmt)
113 unsigned int width = pix_fmt->width;
114 unsigned int height = pix_fmt->height;
115 unsigned int sizeimage = pix_fmt->plane_fmt[0].sizeimage;
116 unsigned int bytesperline = pix_fmt->plane_fmt[0].bytesperline;
119 width = RPIVID_DEFAULT_WIDTH;
120 if (width > RPIVID_MAX_WIDTH)
121 width = RPIVID_MAX_WIDTH;
123 height = RPIVID_DEFAULT_HEIGHT;
124 if (height > RPIVID_MAX_HEIGHT)
125 height = RPIVID_MAX_HEIGHT;
127 /* For column formats set bytesperline to column height (stride2) */
128 switch (pix_fmt->pixelformat) {
130 pix_fmt->pixelformat = V4L2_PIX_FMT_NV12_COL128;
132 case V4L2_PIX_FMT_NV12_COL128:
133 /* Width rounds up to columns */
134 width = ALIGN(width, 128);
136 /* 16 aligned height - not sure we even need that */
137 height = ALIGN(height, 16);
139 * Accept suggested shape if at least min & < 2 * min
141 bytesperline = constrain2x(bytesperline, height * 3 / 2);
144 * Again allow plausible variation in case added padding is
147 sizeimage = constrain2x(sizeimage, bytesperline * width);
150 case V4L2_PIX_FMT_NV12_10_COL128:
151 /* width in pixels (3 pels = 4 bytes) rounded to 128 byte
154 width = ALIGN(((width + 2) / 3), 32) * 3;
156 /* 16-aligned height. */
157 height = ALIGN(height, 16);
160 * Accept suggested shape if at least min & < 2 * min
162 bytesperline = constrain2x(bytesperline, height * 3 / 2);
165 * Again allow plausible variation in case added padding is
168 sizeimage = constrain2x(sizeimage,
169 bytesperline * width * 4 / 3);
173 pix_fmt->width = width;
174 pix_fmt->height = height;
176 pix_fmt->field = V4L2_FIELD_NONE;
177 pix_fmt->plane_fmt[0].bytesperline = bytesperline;
178 pix_fmt->plane_fmt[0].sizeimage = sizeimage;
179 pix_fmt->num_planes = 1;
182 static int rpivid_querycap(struct file *file, void *priv,
183 struct v4l2_capability *cap)
185 strscpy(cap->driver, RPIVID_NAME, sizeof(cap->driver));
186 strscpy(cap->card, RPIVID_NAME, sizeof(cap->card));
187 snprintf(cap->bus_info, sizeof(cap->bus_info),
188 "platform:%s", RPIVID_NAME);
193 static int rpivid_enum_fmt_vid_out(struct file *file, void *priv,
194 struct v4l2_fmtdesc *f)
198 // H.265 Slice only currently
200 f->pixelformat = V4L2_PIX_FMT_HEVC_SLICE;
207 static int rpivid_hevc_validate_sps(const struct v4l2_ctrl_hevc_sps * const sps)
209 const unsigned int ctb_log2_size_y =
210 sps->log2_min_luma_coding_block_size_minus3 + 3 +
211 sps->log2_diff_max_min_luma_coding_block_size;
212 const unsigned int min_tb_log2_size_y =
213 sps->log2_min_luma_transform_block_size_minus2 + 2;
214 const unsigned int max_tb_log2_size_y = min_tb_log2_size_y +
215 sps->log2_diff_max_min_luma_transform_block_size;
217 /* Local limitations */
218 if (sps->pic_width_in_luma_samples < 32 ||
219 sps->pic_width_in_luma_samples > 4096)
221 if (sps->pic_height_in_luma_samples < 32 ||
222 sps->pic_height_in_luma_samples > 4096)
224 if (!(sps->bit_depth_luma_minus8 == 0 ||
225 sps->bit_depth_luma_minus8 == 2))
227 if (sps->bit_depth_luma_minus8 != sps->bit_depth_chroma_minus8)
229 if (sps->chroma_format_idc != 1)
232 /* Limits from H.265 7.4.3.2.1 */
233 if (sps->log2_max_pic_order_cnt_lsb_minus4 > 12)
235 if (sps->sps_max_dec_pic_buffering_minus1 > 15)
237 if (sps->sps_max_num_reorder_pics >
238 sps->sps_max_dec_pic_buffering_minus1)
240 if (ctb_log2_size_y > 6)
242 if (max_tb_log2_size_y > 5)
244 if (max_tb_log2_size_y > ctb_log2_size_y)
246 if (sps->max_transform_hierarchy_depth_inter >
247 (ctb_log2_size_y - min_tb_log2_size_y))
249 if (sps->max_transform_hierarchy_depth_intra >
250 (ctb_log2_size_y - min_tb_log2_size_y))
252 /* Check pcm stuff */
253 if (sps->num_short_term_ref_pic_sets > 64)
255 if (sps->num_long_term_ref_pics_sps > 32)
260 static inline int is_sps_set(const struct v4l2_ctrl_hevc_sps * const sps)
262 return sps && sps->pic_width_in_luma_samples != 0;
265 static u32 pixelformat_from_sps(const struct v4l2_ctrl_hevc_sps * const sps,
270 if (!is_sps_set(sps) || !rpivid_hevc_validate_sps(sps)) {
271 /* Treat this as an error? For now return both */
273 pf = V4L2_PIX_FMT_NV12_COL128;
275 pf = V4L2_PIX_FMT_NV12_10_COL128;
276 } else if (index == 0) {
277 if (sps->bit_depth_luma_minus8 == 0)
278 pf = V4L2_PIX_FMT_NV12_COL128;
279 else if (sps->bit_depth_luma_minus8 == 2)
280 pf = V4L2_PIX_FMT_NV12_10_COL128;
286 static struct v4l2_pix_format_mplane
287 rpivid_hevc_default_dst_fmt(struct rpivid_ctx * const ctx)
289 const struct v4l2_ctrl_hevc_sps * const sps =
290 rpivid_find_control_data(ctx, V4L2_CID_MPEG_VIDEO_HEVC_SPS);
291 struct v4l2_pix_format_mplane pix_fmt;
293 memset(&pix_fmt, 0, sizeof(pix_fmt));
294 if (is_sps_set(sps)) {
295 pix_fmt.width = sps->pic_width_in_luma_samples;
296 pix_fmt.height = sps->pic_height_in_luma_samples;
297 pix_fmt.pixelformat = pixelformat_from_sps(sps, 0);
300 rpivid_prepare_dst_format(&pix_fmt);
304 static u32 rpivid_hevc_get_dst_pixelformat(struct rpivid_ctx * const ctx,
307 const struct v4l2_ctrl_hevc_sps * const sps =
308 rpivid_find_control_data(ctx, V4L2_CID_MPEG_VIDEO_HEVC_SPS);
310 return pixelformat_from_sps(sps, index);
313 static int rpivid_enum_fmt_vid_cap(struct file *file, void *priv,
314 struct v4l2_fmtdesc *f)
316 struct rpivid_ctx * const ctx = rpivid_file2ctx(file);
318 const u32 pf = rpivid_hevc_get_dst_pixelformat(ctx, f->index);
328 * get dst format - sets it to default if otherwise unset
329 * returns a pointer to the struct as a convienience
331 static struct v4l2_pix_format_mplane *get_dst_fmt(struct rpivid_ctx *const ctx)
333 if (!ctx->dst_fmt_set)
334 ctx->dst_fmt = rpivid_hevc_default_dst_fmt(ctx);
335 return &ctx->dst_fmt;
338 static int rpivid_g_fmt_vid_cap(struct file *file, void *priv,
339 struct v4l2_format *f)
341 struct rpivid_ctx *ctx = rpivid_file2ctx(file);
343 f->fmt.pix_mp = *get_dst_fmt(ctx);
347 static int rpivid_g_fmt_vid_out(struct file *file, void *priv,
348 struct v4l2_format *f)
350 struct rpivid_ctx *ctx = rpivid_file2ctx(file);
352 f->fmt.pix_mp = ctx->src_fmt;
356 static inline void copy_color(struct v4l2_pix_format_mplane *d,
357 const struct v4l2_pix_format_mplane *s)
359 d->colorspace = s->colorspace;
360 d->xfer_func = s->xfer_func;
361 d->ycbcr_enc = s->ycbcr_enc;
362 d->quantization = s->quantization;
365 static int rpivid_try_fmt_vid_cap(struct file *file, void *priv,
366 struct v4l2_format *f)
368 struct rpivid_ctx *ctx = rpivid_file2ctx(file);
369 const struct v4l2_ctrl_hevc_sps * const sps =
370 rpivid_find_control_data(ctx, V4L2_CID_MPEG_VIDEO_HEVC_SPS);
374 for (i = 0; (pixelformat = pixelformat_from_sps(sps, i)) != 0; i++) {
375 if (f->fmt.pix_mp.pixelformat == pixelformat)
379 // We don't have any way of finding out colourspace so believe
380 // anything we are told - take anything set in src as a default
381 if (f->fmt.pix_mp.colorspace == V4L2_COLORSPACE_DEFAULT)
382 copy_color(&f->fmt.pix_mp, &ctx->src_fmt);
384 f->fmt.pix_mp.pixelformat = pixelformat;
385 rpivid_prepare_dst_format(&f->fmt.pix_mp);
389 static int rpivid_try_fmt_vid_out(struct file *file, void *priv,
390 struct v4l2_format *f)
392 rpivid_prepare_src_format(&f->fmt.pix_mp);
396 static int rpivid_s_fmt_vid_cap(struct file *file, void *priv,
397 struct v4l2_format *f)
399 struct rpivid_ctx *ctx = rpivid_file2ctx(file);
400 struct vb2_queue *vq;
403 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
407 ret = rpivid_try_fmt_vid_cap(file, priv, f);
411 ctx->dst_fmt = f->fmt.pix_mp;
412 ctx->dst_fmt_set = 1;
417 static int rpivid_s_fmt_vid_out(struct file *file, void *priv,
418 struct v4l2_format *f)
420 struct rpivid_ctx *ctx = rpivid_file2ctx(file);
421 struct vb2_queue *vq;
424 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
428 ret = rpivid_try_fmt_vid_out(file, priv, f);
432 ctx->src_fmt = f->fmt.pix_mp;
433 ctx->dst_fmt_set = 0; // Setting src invalidates dst
435 vq->subsystem_flags |=
436 VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
438 /* Propagate colorspace information to capture. */
439 copy_color(&ctx->dst_fmt, &f->fmt.pix_mp);
443 const struct v4l2_ioctl_ops rpivid_ioctl_ops = {
444 .vidioc_querycap = rpivid_querycap,
446 .vidioc_enum_fmt_vid_cap = rpivid_enum_fmt_vid_cap,
447 .vidioc_g_fmt_vid_cap_mplane = rpivid_g_fmt_vid_cap,
448 .vidioc_try_fmt_vid_cap_mplane = rpivid_try_fmt_vid_cap,
449 .vidioc_s_fmt_vid_cap_mplane = rpivid_s_fmt_vid_cap,
451 .vidioc_enum_fmt_vid_out = rpivid_enum_fmt_vid_out,
452 .vidioc_g_fmt_vid_out_mplane = rpivid_g_fmt_vid_out,
453 .vidioc_try_fmt_vid_out_mplane = rpivid_try_fmt_vid_out,
454 .vidioc_s_fmt_vid_out_mplane = rpivid_s_fmt_vid_out,
456 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
457 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
458 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
459 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
460 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
461 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
462 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
464 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
465 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
467 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_stateless_try_decoder_cmd,
468 .vidioc_decoder_cmd = v4l2_m2m_ioctl_stateless_decoder_cmd,
470 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
471 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
474 static int rpivid_queue_setup(struct vb2_queue *vq, unsigned int *nbufs,
475 unsigned int *nplanes, unsigned int sizes[],
476 struct device *alloc_devs[])
478 struct rpivid_ctx *ctx = vb2_get_drv_priv(vq);
479 struct v4l2_pix_format_mplane *pix_fmt;
481 if (V4L2_TYPE_IS_OUTPUT(vq->type))
482 pix_fmt = &ctx->src_fmt;
484 pix_fmt = get_dst_fmt(ctx);
487 if (sizes[0] < pix_fmt->plane_fmt[0].sizeimage)
490 sizes[0] = pix_fmt->plane_fmt[0].sizeimage;
497 static void rpivid_queue_cleanup(struct vb2_queue *vq, u32 state)
499 struct rpivid_ctx *ctx = vb2_get_drv_priv(vq);
500 struct vb2_v4l2_buffer *vbuf;
503 if (V4L2_TYPE_IS_OUTPUT(vq->type))
504 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
506 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
511 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
513 v4l2_m2m_buf_done(vbuf, state);
517 static int rpivid_buf_out_validate(struct vb2_buffer *vb)
519 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
521 vbuf->field = V4L2_FIELD_NONE;
525 static int rpivid_buf_prepare(struct vb2_buffer *vb)
527 struct vb2_queue *vq = vb->vb2_queue;
528 struct rpivid_ctx *ctx = vb2_get_drv_priv(vq);
529 struct v4l2_pix_format_mplane *pix_fmt;
531 if (V4L2_TYPE_IS_OUTPUT(vq->type))
532 pix_fmt = &ctx->src_fmt;
534 pix_fmt = &ctx->dst_fmt;
536 if (vb2_plane_size(vb, 0) < pix_fmt->plane_fmt[0].sizeimage)
539 vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
544 /* Only stops the clock if streaom off on both output & capture */
545 static void stop_clock(struct rpivid_dev *dev, struct rpivid_ctx *ctx)
547 if (ctx->src_stream_on ||
551 clk_set_min_rate(dev->clock, 0);
552 clk_disable_unprepare(dev->clock);
555 /* Always starts the clock if it isn't already on this ctx */
556 static int start_clock(struct rpivid_dev *dev, struct rpivid_ctx *ctx)
561 max_hevc_clock = clk_get_max_rate(dev->clock);
563 rv = clk_set_min_rate(dev->clock, max_hevc_clock);
565 dev_err(dev->dev, "Failed to set clock rate\n");
569 rv = clk_prepare_enable(dev->clock);
571 dev_err(dev->dev, "Failed to enable clock\n");
578 static int rpivid_start_streaming(struct vb2_queue *vq, unsigned int count)
580 struct rpivid_ctx *ctx = vb2_get_drv_priv(vq);
581 struct rpivid_dev *dev = ctx->dev;
584 if (!V4L2_TYPE_IS_OUTPUT(vq->type)) {
585 ctx->dst_stream_on = 1;
589 if (ctx->src_fmt.pixelformat != V4L2_PIX_FMT_HEVC_SLICE) {
594 if (ctx->src_stream_on)
597 ret = start_clock(dev, ctx);
601 if (dev->dec_ops->start)
602 ret = dev->dec_ops->start(ctx);
604 goto fail_stop_clock;
606 ctx->src_stream_on = 1;
611 stop_clock(dev, ctx);
613 v4l2_err(&dev->v4l2_dev, "%s: qtype=%d: FAIL\n", __func__, vq->type);
614 rpivid_queue_cleanup(vq, VB2_BUF_STATE_QUEUED);
618 static void rpivid_stop_streaming(struct vb2_queue *vq)
620 struct rpivid_ctx *ctx = vb2_get_drv_priv(vq);
621 struct rpivid_dev *dev = ctx->dev;
623 if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
624 ctx->src_stream_on = 0;
625 if (dev->dec_ops->stop)
626 dev->dec_ops->stop(ctx);
628 ctx->dst_stream_on = 0;
631 rpivid_queue_cleanup(vq, VB2_BUF_STATE_ERROR);
633 vb2_wait_for_all_buffers(vq);
635 stop_clock(dev, ctx);
638 static void rpivid_buf_queue(struct vb2_buffer *vb)
640 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
641 struct rpivid_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
643 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
646 static void rpivid_buf_request_complete(struct vb2_buffer *vb)
648 struct rpivid_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
650 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
653 static struct vb2_ops rpivid_qops = {
654 .queue_setup = rpivid_queue_setup,
655 .buf_prepare = rpivid_buf_prepare,
656 .buf_queue = rpivid_buf_queue,
657 .buf_out_validate = rpivid_buf_out_validate,
658 .buf_request_complete = rpivid_buf_request_complete,
659 .start_streaming = rpivid_start_streaming,
660 .stop_streaming = rpivid_stop_streaming,
661 .wait_prepare = vb2_ops_wait_prepare,
662 .wait_finish = vb2_ops_wait_finish,
665 int rpivid_queue_init(void *priv, struct vb2_queue *src_vq,
666 struct vb2_queue *dst_vq)
668 struct rpivid_ctx *ctx = priv;
671 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
672 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
673 src_vq->drv_priv = ctx;
674 src_vq->buf_struct_size = sizeof(struct rpivid_buffer);
675 src_vq->ops = &rpivid_qops;
676 src_vq->mem_ops = &vb2_dma_contig_memops;
677 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
678 src_vq->lock = &ctx->ctx_mutex;
679 src_vq->dev = ctx->dev->dev;
680 src_vq->supports_requests = true;
681 src_vq->requires_requests = true;
683 ret = vb2_queue_init(src_vq);
687 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
688 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
689 dst_vq->drv_priv = ctx;
690 dst_vq->buf_struct_size = sizeof(struct rpivid_buffer);
691 dst_vq->min_buffers_needed = 1;
692 dst_vq->ops = &rpivid_qops;
693 dst_vq->mem_ops = &vb2_dma_contig_memops;
694 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
695 dst_vq->lock = &ctx->ctx_mutex;
696 dst_vq->dev = ctx->dev->dev;
698 return vb2_queue_init(dst_vq);