Re: [PATCH v2 2/2] media: rockchip: rkcif: Honour the requested bytesperline

From: Faizel K B

Date: Thu Nov 26 2026 - 15:34:01 EST


Hi Michael,

Thanks for the inputs.

On Tue, Nov 24, 2026 at 12:13:20PM +0100, Michael Riesch wrote:
+static u32 rkcif_stream_plane_lines(const struct v4l2_format_info *info,
+ unsigned int plane, u32 height)
+{
+ return plane ? DIV_ROUND_UP(height, info->vdiv) : height;
+}

Is this the same as v4l2_format_plane_height? And if yes, would it make
sense to reuse this one (of course it must be made public for that)?


Yes, apart from the block height alignment, which is 1 for all rkcif
formats. I will export and reuse it.

+
+static void rkcif_stream_set_bytesperline(struct rkcif_stream *stream,
+ struct v4l2_pix_format_mplane *pix,
+ u32 req_bpl)
+{
+ const struct v4l2_format_info *info = v4l2_format_info(pix->pixelformat);
+ u32 bpl = pix->plane_fmt[0].bytesperline;
+ u32 total_lines = 0, max_bpl;
+ unsigned int i;
+
+ for (i = 0; i < info->comp_planes; i++)
+ total_lines += rkcif_stream_plane_lines(info, i, pix->height);

Just to be on the same page: What exactly are you doing here?


When bytesperline is increased, sizeimage must grow with it, or the
buffer is too small for the DMA. For formats with a single memory plane
(e.g. NV16) the Y and UV planes are in the same buffer, so sizeimage is
bytesperline * (Y lines + UV lines). total_lines is that sum, and it's
also used to limit bytesperline so that sizeimage doesn't overflow.

+
+ max_bpl = min(stream->max_bytesperline,
+ ALIGN_DOWN(U32_MAX / total_lines, CIF_BYTESPERLINE_ALIGN));
+ if (req_bpl > bpl && req_bpl <= max_bpl)
+ bpl = ALIGN(req_bpl, CIF_BYTESPERLINE_ALIGN);
+
+ /* The Y and UV planes share the same line width. */
+ if (info->mem_planes == 1) {
+ pix->plane_fmt[0].bytesperline = bpl;
+ pix->plane_fmt[0].sizeimage = bpl * total_lines;
+ } else {
+ for (i = 0; i < info->mem_planes; i++) {
+ pix->plane_fmt[i].bytesperline = bpl;
+ pix->plane_fmt[i].sizeimage =
+ bpl * rkcif_stream_plane_lines(info, i,
+ pix->height);

v4l2_format_plane_size?

It computes the stride from the width and an alignment, so it can't
take the requested bytesperline.


+ }
+ }
+}
+
static int rkcif_stream_fill_format(struct rkcif_stream *stream,
struct v4l2_pix_format_mplane *pix)
{
const struct rkcif_output_fmt *fmt;
- u32 height, width;
+ u32 height, width, req_bpl = 0;
+ unsigned int i;
int ret;

fmt = rkcif_stream_find_output_fmt(stream, true, pix->pixelformat);
height = clamp_t(u32, pix->height, CIF_MIN_HEIGHT, CIF_MAX_HEIGHT);
width = clamp_t(u32, pix->width, CIF_MIN_WIDTH, CIF_MAX_WIDTH);
- ret = v4l2_fill_pixfmt_mp(pix, fmt->fourcc, width, height);
+ for (i = 0; i < pix->num_planes; i++)
+ req_bpl = max(req_bpl, pix->plane_fmt[i].bytesperline);
+ ret = v4l2_fill_pixfmt_mp_aligned(pix, fmt->fourcc, width, height,
+ CIF_BYTESPERLINE_ALIGN);
if (ret)
return ret;

+ rkcif_stream_set_bytesperline(stream, pix, req_bpl);

Wouldn't calling v4l2_fill_pixfmt_mp_aligned(pix, fmt->fourcc, width,
height, req_bpl) do the trick?

I looked at that, but stride_alignment is a u8 and has to be a power of
2, which the requested bytesperline usually isn't. For example, with the
IMX415 in SGBRG10 the minimum is 7728 bytes, and libcamera requests 7936
for the 256-byte alignment of Panfrost.


In summary: I think this is a common problem, so there should be a
common solution. Reuse helpers, or extend them if required.


Agreed. I noticed that some other drivers, e.g. rkisp1, also handle the
requested bytesperline in the driver itself. For v3 I'll add a helper to
v4l2-common that fills the format like v4l2_fill_pixfmt_mp_aligned(),
but keeps a larger bytesperline requested by userspace, up to a maximum.
It computes sizeimage with v4l2_format_plane_height(), so rkcif doesn't
need total_lines and only calls this helper. Does that sound good?

Regards,
Faizel