1 From 103259e6629a419bc07d6348dd1544213daac45b Mon Sep 17 00:00:00 2001
2 From: John Cox <jc@kynesim.co.uk>
3 Date: Thu, 11 Mar 2021 12:51:00 +0000
4 Subject: [PATCH] media: rpivid: Convert to MPLANE
6 Use multi-planar interface rather than single plane interface. This
7 allows dmabufs holding compressed data to be resized.
9 Signed-off-by: John Cox <jc@kynesim.co.uk>
11 drivers/staging/media/rpivid/rpivid.c | 2 +-
12 drivers/staging/media/rpivid/rpivid.h | 4 +-
13 drivers/staging/media/rpivid/rpivid_h265.c | 9 ++-
14 drivers/staging/media/rpivid/rpivid_video.c | 88 ++++++++++-----------
15 drivers/staging/media/rpivid/rpivid_video.h | 4 +-
16 5 files changed, 52 insertions(+), 55 deletions(-)
18 --- a/drivers/staging/media/rpivid/rpivid.c
19 +++ b/drivers/staging/media/rpivid/rpivid.c
20 @@ -283,7 +283,7 @@ static const struct video_device rpivid_
21 .ioctl_ops = &rpivid_ioctl_ops,
23 .release = video_device_release_empty,
24 - .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
25 + .device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING,
28 static const struct v4l2_m2m_ops rpivid_m2m_ops = {
29 --- a/drivers/staging/media/rpivid/rpivid.h
30 +++ b/drivers/staging/media/rpivid/rpivid.h
31 @@ -85,8 +85,8 @@ struct rpivid_ctx {
33 struct rpivid_dev *dev;
35 - struct v4l2_pix_format src_fmt;
36 - struct v4l2_pix_format dst_fmt;
37 + struct v4l2_pix_format_mplane src_fmt;
38 + struct v4l2_pix_format_mplane dst_fmt;
40 // fatal_err is set if an error has occurred s.t. decode cannot
41 // continue (such as running out of CMA)
42 --- a/drivers/staging/media/rpivid/rpivid_h265.c
43 +++ b/drivers/staging/media/rpivid/rpivid_h265.c
44 @@ -1613,7 +1613,7 @@ static void rpivid_h265_setup(struct rpi
45 de->cmd_copy_gptr = ctx->cmdbufs + 0;
47 de->frame_c_offset = ctx->dst_fmt.height * 128;
48 - de->frame_stride = ctx->dst_fmt.bytesperline * 128;
49 + de->frame_stride = ctx->dst_fmt.plane_fmt[0].bytesperline * 128;
51 vb2_dma_contig_plane_dma_addr(&run->dst->vb2_buf, 0);
53 @@ -1654,11 +1654,11 @@ static void rpivid_h265_setup(struct rpi
56 if (run->dst->planes[0].length <
57 - ctx->dst_fmt.sizeimage) {
58 + ctx->dst_fmt.plane_fmt[0].sizeimage) {
59 v4l2_warn(&dev->v4l2_dev,
60 "Capture plane[0] length (%d) < sizeimage (%d)\n",
61 run->dst->planes[0].length,
62 - ctx->dst_fmt.sizeimage);
63 + ctx->dst_fmt.plane_fmt[0].sizeimage);
67 @@ -1812,7 +1812,8 @@ static void rpivid_h265_setup(struct rpi
68 // slices. If this changes we will need idx mapping code.
69 // Uses sh so here rather than trigger
71 - vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
72 + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
73 + V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
76 v4l2_err(&dev->v4l2_dev, "VQ gone!\n");
77 --- a/drivers/staging/media/rpivid/rpivid_video.c
78 +++ b/drivers/staging/media/rpivid/rpivid_video.c
79 @@ -42,25 +42,27 @@ static inline unsigned int constrain2x(u
83 -int rpivid_prepare_src_format(struct v4l2_pix_format *pix_fmt)
84 +int rpivid_prepare_src_format(struct v4l2_pix_format_mplane *pix_fmt)
86 if (pix_fmt->pixelformat != V4L2_PIX_FMT_HEVC_SLICE)
89 /* Zero bytes per line for encoded source. */
90 - pix_fmt->bytesperline = 0;
91 + pix_fmt->plane_fmt[0].bytesperline = 0;
92 /* Choose some minimum size since this can't be 0 */
93 - pix_fmt->sizeimage = max_t(u32, SZ_1K, pix_fmt->sizeimage);
94 + pix_fmt->plane_fmt[0].sizeimage = max_t(u32, SZ_1K,
95 + pix_fmt->plane_fmt[0].sizeimage);
96 + pix_fmt->num_planes = 1;
97 pix_fmt->field = V4L2_FIELD_NONE;
101 -int rpivid_prepare_dst_format(struct v4l2_pix_format *pix_fmt)
102 +int rpivid_prepare_dst_format(struct v4l2_pix_format_mplane *pix_fmt)
104 unsigned int width = pix_fmt->width;
105 unsigned int height = pix_fmt->height;
106 - unsigned int sizeimage = pix_fmt->sizeimage;
107 - unsigned int bytesperline = pix_fmt->bytesperline;
108 + unsigned int sizeimage = pix_fmt->plane_fmt[0].sizeimage;
109 + unsigned int bytesperline = pix_fmt->plane_fmt[0].bytesperline;
111 switch (pix_fmt->pixelformat) {
112 /* For column formats set bytesperline to column height (stride2) */
113 @@ -112,8 +114,9 @@ int rpivid_prepare_dst_format(struct v4l
114 pix_fmt->height = height;
116 pix_fmt->field = V4L2_FIELD_NONE;
117 - pix_fmt->bytesperline = bytesperline;
118 - pix_fmt->sizeimage = sizeimage;
119 + pix_fmt->plane_fmt[0].bytesperline = bytesperline;
120 + pix_fmt->plane_fmt[0].sizeimage = sizeimage;
121 + pix_fmt->num_planes = 1;
125 @@ -222,12 +225,12 @@ static u32 pixelformat_from_sps(const st
129 -static struct v4l2_pix_format
130 +static struct v4l2_pix_format_mplane
131 rpivid_hevc_default_dst_fmt(struct rpivid_ctx * const ctx)
133 const struct v4l2_ctrl_hevc_sps * const sps =
134 rpivid_find_control_data(ctx, V4L2_CID_MPEG_VIDEO_HEVC_SPS);
135 - struct v4l2_pix_format pix_fmt = {
136 + struct v4l2_pix_format_mplane pix_fmt = {
137 .width = sps->pic_width_in_luma_samples,
138 .height = sps->pic_height_in_luma_samples,
139 .pixelformat = pixelformat_from_sps(sps, 0)
140 @@ -267,7 +270,7 @@ static int rpivid_g_fmt_vid_cap(struct f
142 if (!ctx->dst_fmt_set)
143 ctx->dst_fmt = rpivid_hevc_default_dst_fmt(ctx);
144 - f->fmt.pix = ctx->dst_fmt;
145 + f->fmt.pix_mp = ctx->dst_fmt;
149 @@ -276,12 +279,12 @@ static int rpivid_g_fmt_vid_out(struct f
151 struct rpivid_ctx *ctx = rpivid_file2ctx(file);
153 - f->fmt.pix = ctx->src_fmt;
154 + f->fmt.pix_mp = ctx->src_fmt;
158 -static inline void copy_color(struct v4l2_pix_format *d,
159 - const struct v4l2_pix_format *s)
160 +static inline void copy_color(struct v4l2_pix_format_mplane *d,
161 + const struct v4l2_pix_format_mplane *s)
163 d->colorspace = s->colorspace;
164 d->xfer_func = s->xfer_func;
165 @@ -298,12 +301,8 @@ static int rpivid_try_fmt_vid_cap(struct
169 - /* Reject format types we don't support */
170 - if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
173 for (i = 0; (pixelformat = pixelformat_from_sps(sps, i)) != 0; i++) {
174 - if (f->fmt.pix.pixelformat == pixelformat)
175 + if (f->fmt.pix_mp.pixelformat == pixelformat)
179 @@ -317,23 +316,20 @@ static int rpivid_try_fmt_vid_cap(struct
181 // We don't have any way of finding out colourspace so believe
182 // anything we are told - take anything set in src as a default
183 - if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT)
184 - copy_color(&f->fmt.pix, &ctx->src_fmt);
185 + if (f->fmt.pix_mp.colorspace == V4L2_COLORSPACE_DEFAULT)
186 + copy_color(&f->fmt.pix_mp, &ctx->src_fmt);
188 - f->fmt.pix.pixelformat = pixelformat;
189 - return rpivid_prepare_dst_format(&f->fmt.pix);
190 + f->fmt.pix_mp.pixelformat = pixelformat;
191 + return rpivid_prepare_dst_format(&f->fmt.pix_mp);
194 static int rpivid_try_fmt_vid_out(struct file *file, void *priv,
195 struct v4l2_format *f)
197 - if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
200 - if (rpivid_prepare_src_format(&f->fmt.pix)) {
201 + if (rpivid_prepare_src_format(&f->fmt.pix_mp)) {
202 // Set default src format
203 - f->fmt.pix.pixelformat = RPIVID_SRC_PIXELFORMAT_DEFAULT;
204 - rpivid_prepare_src_format(&f->fmt.pix);
205 + f->fmt.pix_mp.pixelformat = RPIVID_SRC_PIXELFORMAT_DEFAULT;
206 + rpivid_prepare_src_format(&f->fmt.pix_mp);
210 @@ -353,7 +349,7 @@ static int rpivid_s_fmt_vid_cap(struct f
214 - ctx->dst_fmt = f->fmt.pix;
215 + ctx->dst_fmt = f->fmt.pix_mp;
216 ctx->dst_fmt_set = 1;
219 @@ -374,14 +370,14 @@ static int rpivid_s_fmt_vid_out(struct f
223 - ctx->src_fmt = f->fmt.pix;
224 + ctx->src_fmt = f->fmt.pix_mp;
225 ctx->dst_fmt_set = 0; // Setting src invalidates dst
227 vq->subsystem_flags |=
228 VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF;
230 /* Propagate colorspace information to capture. */
231 - copy_color(&ctx->dst_fmt, &f->fmt.pix);
232 + copy_color(&ctx->dst_fmt, &f->fmt.pix_mp);
236 @@ -389,14 +385,14 @@ const struct v4l2_ioctl_ops rpivid_ioctl
237 .vidioc_querycap = rpivid_querycap,
239 .vidioc_enum_fmt_vid_cap = rpivid_enum_fmt_vid_cap,
240 - .vidioc_g_fmt_vid_cap = rpivid_g_fmt_vid_cap,
241 - .vidioc_try_fmt_vid_cap = rpivid_try_fmt_vid_cap,
242 - .vidioc_s_fmt_vid_cap = rpivid_s_fmt_vid_cap,
243 + .vidioc_g_fmt_vid_cap_mplane = rpivid_g_fmt_vid_cap,
244 + .vidioc_try_fmt_vid_cap_mplane = rpivid_try_fmt_vid_cap,
245 + .vidioc_s_fmt_vid_cap_mplane = rpivid_s_fmt_vid_cap,
247 .vidioc_enum_fmt_vid_out = rpivid_enum_fmt_vid_out,
248 - .vidioc_g_fmt_vid_out = rpivid_g_fmt_vid_out,
249 - .vidioc_try_fmt_vid_out = rpivid_try_fmt_vid_out,
250 - .vidioc_s_fmt_vid_out = rpivid_s_fmt_vid_out,
251 + .vidioc_g_fmt_vid_out_mplane = rpivid_g_fmt_vid_out,
252 + .vidioc_try_fmt_vid_out_mplane = rpivid_try_fmt_vid_out,
253 + .vidioc_s_fmt_vid_out_mplane = rpivid_s_fmt_vid_out,
255 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
256 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
257 @@ -421,7 +417,7 @@ static int rpivid_queue_setup(struct vb2
258 struct device *alloc_devs[])
260 struct rpivid_ctx *ctx = vb2_get_drv_priv(vq);
261 - struct v4l2_pix_format *pix_fmt;
262 + struct v4l2_pix_format_mplane *pix_fmt;
264 if (V4L2_TYPE_IS_OUTPUT(vq->type))
265 pix_fmt = &ctx->src_fmt;
266 @@ -429,10 +425,10 @@ static int rpivid_queue_setup(struct vb2
267 pix_fmt = &ctx->dst_fmt;
270 - if (sizes[0] < pix_fmt->sizeimage)
271 + if (sizes[0] < pix_fmt->plane_fmt[0].sizeimage)
274 - sizes[0] = pix_fmt->sizeimage;
275 + sizes[0] = pix_fmt->plane_fmt[0].sizeimage;
279 @@ -471,17 +467,17 @@ static int rpivid_buf_prepare(struct vb2
281 struct vb2_queue *vq = vb->vb2_queue;
282 struct rpivid_ctx *ctx = vb2_get_drv_priv(vq);
283 - struct v4l2_pix_format *pix_fmt;
284 + struct v4l2_pix_format_mplane *pix_fmt;
286 if (V4L2_TYPE_IS_OUTPUT(vq->type))
287 pix_fmt = &ctx->src_fmt;
289 pix_fmt = &ctx->dst_fmt;
291 - if (vb2_plane_size(vb, 0) < pix_fmt->sizeimage)
292 + if (vb2_plane_size(vb, 0) < pix_fmt->plane_fmt[0].sizeimage)
295 - vb2_set_plane_payload(vb, 0, pix_fmt->sizeimage);
296 + vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage);
300 @@ -567,7 +563,7 @@ int rpivid_queue_init(void *priv, struct
301 struct rpivid_ctx *ctx = priv;
304 - src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
305 + src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
306 src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
307 src_vq->drv_priv = ctx;
308 src_vq->buf_struct_size = sizeof(struct rpivid_buffer);
309 @@ -584,7 +580,7 @@ int rpivid_queue_init(void *priv, struct
313 - dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
314 + dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
315 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
316 dst_vq->drv_priv = ctx;
317 dst_vq->buf_struct_size = sizeof(struct rpivid_buffer);
318 --- a/drivers/staging/media/rpivid/rpivid_video.h
319 +++ b/drivers/staging/media/rpivid/rpivid_video.h
320 @@ -24,7 +24,7 @@ extern const struct v4l2_ioctl_ops rpivi
322 int rpivid_queue_init(void *priv, struct vb2_queue *src_vq,
323 struct vb2_queue *dst_vq);
324 -int rpivid_prepare_src_format(struct v4l2_pix_format *pix_fmt);
325 -int rpivid_prepare_dst_format(struct v4l2_pix_format *pix_fmt);
326 +int rpivid_prepare_src_format(struct v4l2_pix_format_mplane *pix_fmt);
327 +int rpivid_prepare_dst_format(struct v4l2_pix_format_mplane *pix_fmt);