media: rockchip: rkcif: ENUM_FMT and bytesperline fixes
cover media: rockchip: rkcif: ENUM_FMT and bytesperline fixes
This series fixes two issues in the rkcif MIPI capture path, found while using the libcamera simple pipeline handler with a Sony IMX415 on a Radxa ROCK 5B+ (RK3588).
Patch 1 makes VIDIOC_ENUM_FMT honour the mbus_code field, as required for V4L2_CAP_IO_MC devices. Without it, userspace can select a pixel format that doesn't match the media bus format, e.g. a different Bayer order, and the captured images have wrong colours.
Patch 2 aligns bytesperline to 8 bytes, as required by the MIPI DMA, and honours a larger value requested by userspace. This allows the capture buffers to be imported as DMA-BUFs by the libcamera GPU software ISP on Panfrost, which needs a 64-byte stride alignment, instead of copying every frame.
Tested on ROCK 5B+ with IMX415 (SGBRG10, 3864x2192): correct colours without any userspace workaround, and a stride of 7936 bytes accepted and used for zero-copy import. The corresponding libcamera change is under review at:
https://patchwork.libcamera.org/patch/28662/
Faizel K B (2):
media: rockchip: rkcif: Filter enumerated formats by media bus code
media: rockchip: rkcif: Honour the requested bytesperline on MIPI
.../platform/rockchip/rkcif/rkcif-stream.c | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.43.0
PATCH 1/2 media: rockchip: rkcif: Filter enumerated formats by media bus code
VIDIOC_ENUM_FMT ignores the mbus_code field and returns all formats, although the video devices set V4L2_CAP_IO_MC. Return only the formats that match the requested media bus code.
Otherwise userspace can pick a pixel format that doesn't match the media bus format, and then interprets the captured data in the wrong format.
--- .../media/platform/rockchip/rkcif/rkcif-stream.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +11 -4
@@ -395,13 +395,20 @@ static int rkcif_stream_enum_formats(struct file *file, void *priv, struct v4l2_fmtdesc *f) {struct rkcif_stream *stream =video_drvdata (file); +unsigned int i, n =0 ; -if (f->index >= stream->out_fmts_num) -return -EINVAL ; +for (i =0 ; i < stream->out_fmts_num; i++) { +if (f->mbus_code && + stream->out_fmts[i].mbus_code != f->mbus_code) +continue ; - f->pixelformat = stream->out_fmts[f->index].fourcc; +if (n++ == f->index) { + f->pixelformat = stream->out_fmts[i].fourcc; +return 0 ; + } + } -return 0 ; +return -EINVAL ; } static int rkcif_stream_enum_framesizes(struct file *file, void *fh,
Signature · 1 line
2.43.0
PATCH 2/2 media: rockchip: rkcif: Honour the requested bytesperline on MIPI
In reply to Faizel K BThe MIPI DMA uses bytesperline as the line stride, which must be a multiple of 8 bytes, but v4l2_fill_pixfmt_mp() doesn't align it. Any larger bytesperline requested by userspace is also replaced with the minimum.
Align bytesperline to 8 bytes and honour a larger requested value, up to the limit of the line width register. This allows DMA-BUF importers that need a larger stride alignment to use the buffers directly.
--- .../platform/rockchip/rkcif/rkcif-stream.c | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +21 -1
@@ -23,6 +23,7 @@#define CIF_MIN_HEIGHT 64#define CIF_MAX_WIDTH 8192#define CIF_MAX_HEIGHT 8192 +#define CIF_MAX_BYTESPERLINE 0xffff8/* VLW: 20 bits, 8-byte aligned */ static inline struct rkcif_buffer *to_rkcif_buffer (struct vb2_v4l2_buffer *vb) {
@@ -334,20 +335,39 @@ static const struct vb2_ops rkcif_stream_vb2_ops = { .stop_streaming = rkcif_stream_stop_streaming, }; +static void rkcif_stream_set_bytesperline (struct v4l2_pix_format_mplane *pix, +u32 req_bpl) +{ +u32 min_bpl =ALIGN (pix->plane_fmt[0 ].bytesperline,8 ); +u32 max_bpl =min_t (u32,CIF_MAX_BYTESPERLINE , +ALIGN_DOWN (U32_MAX / pix->height,8 )); +u32 bpl = min_bpl; + +if (req_bpl > min_bpl && req_bpl <= max_bpl) + bpl =ALIGN (req_bpl,8 ); + + pix->plane_fmt[0 ].bytesperline = bpl; + pix->plane_fmt[0 ].sizeimage = bpl * pix->height; +} +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;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 ); + req_bpl = pix->plane_fmt[0 ].bytesperline; ret =v4l2_fill_pixfmt_mp (pix, fmt->fourcc, width, height);if (ret)return ret; +if (stream->interface->type ==RKCIF_IF_MIPI ) +rkcif_stream_set_bytesperline (pix, req_bpl); + pix->field =V4L2_FIELD_NONE ;return 0 ;
Signature · 1 line
2.43.0
Mehdi Djait
maintainerRe: [PATCH 1/2] media: rockchip: rkcif: Filter enumerated formats by media bus code
In reply to Faizel K BHello,
Quoted 18 lines · Faizel K B
VIDIOC_ENUM_FMTignores thembus_codefield and returns all formats, although the video devices setV4L2_CAP_IO_MC. Return only the formats that match the requested media bus code.Otherwise userspace can pick a pixel format that doesn't match the media bus format, and then interprets the captured data in the wrong format.
Signed-off-by Faizel K B <[email protected]>--- .../media/platform/rockchip/rkcif/rkcif-stream.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)drivers/media/platform/rockchip/rkcif/rkcif-stream.c +0
-0
drivers/media/platform/rockchip/rkcif/rkcif-stream.c static int rkcif_stream_enum_formats(struct file *file, void *priv, · line 402
Quoted 10 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +4
-2@@ -395,13 +395,20 @@ static int rkcif_stream_enum_formats(struct file *file, void *priv, struct v4l2_fmtdesc *f) {struct rkcif_stream *stream =video_drvdata (file); +unsigned int i, n =0 ; -if (f->index >= stream->out_fmts_num) -return -EINVAL ; +for (i =0 ; i < stream->out_fmts_num; i++) { +if (f->mbus_code && + stream->out_fmts[i].mbus_code != f->mbus_code)
what about dvp capture. The dvp_out_fmts[] does not have the mbus_code set.
drivers/media/platform/rockchip/rkcif/rkcif-stream.c static int rkcif_stream_enum_formats(struct file *file, void *priv, · line 403
Quoted 1 line
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +1
-0+continue ;
--
Kind Regards
Mehdi Djait
Re: [PATCH 1/2] media: rockchip: rkcif: Filter enumerated formats by media bus code
Hi Mehdi,
Quoted 8 lines · Mehdi Djait
Quoted 8 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +1
-0@@ -395,13 +395,20 @@ static int rkcif_stream_enum_formats(struct file *file, void *priv, struct v4l2_fmtdesc *f) {struct rkcif_stream *stream =video_drvdata (file); +unsigned int i, n =0 ;
Quoted 9 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c static int rkcif_stream_enum_formats(struct file *file, void *priv, · line 402
Quoted 5 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +3
-2-if (f->index >= stream->out_fmts_num) -return -EINVAL ; +for (i =0 ; i < stream->out_fmts_num; i++) { +if (f->mbus_code && + stream->out_fmts[i].mbus_code != f->mbus_code)what about dvp capture. The dvp_out_fmts[] does not have the
mbus_codeset.
Thanks for the review. Yes, the DVP case is missing. How about only filtering the formats that have a media bus code, so DVP behaves as
before (see the change).
const struct rkcif_output_fmt *fmt = &stream->out_fmts[i];
if (f->mbus_code && fmt->mbus_code &&
fmt->mbus_code != f->mbus_code)
continue;I don't have DVP hardware to test with. I can submit a v2 if that's ok.
Regards,
Faizel
Michael Riesch
maintainerRe: [PATCH 2/2] media: rockchip: rkcif: Honour the requested bytesperline on MIPI
In reply to Faizel K BHi there,
Thanks for the patch!
Quoted 2 lines · Faizel K B
The MIPI DMA uses bytesperline as the line stride, which must be a multiple of 8 bytes, but
v4l2_fill_pixfmt_mp()doesn't align it.
Come to think of it, TRM p. 2111 says for e.g. VICAP_MIPI0_VLW_ID0:
"Virtual line width of even frame for ID0 path(must be aligned to double word)." which I would interpret as "it must be a multiple of four bytes". What do you think?
Quoted 25 lines
Any larger bytesperline requested by userspace is also replaced with the minimum.
Align bytesperline to 8 bytes and honour a larger requested value, up to the limit of the line width register. This allows DMA-BUF importers that need a larger stride alignment to use the buffers directly.
Signed-off-by Faizel K B <[email protected]>--- .../platform/rockchip/rkcif/rkcif-stream.c | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)drivers/media/platform/rockchip/rkcif/rkcif-stream.c +1
-0@@ -23,6 +23,7 @@#define CIF_MIN_HEIGHT 64#define CIF_MAX_WIDTH 8192#define CIF_MAX_HEIGHT 8192 +#define CIF_MAX_BYTESPERLINE 0xffff8/* VLW: 20 bits, 8-byte aligned */ static inline struct rkcif_buffer *to_rkcif_buffer (struct vb2_v4l2_buffer *vb) {
drivers/media/platform/rockchip/rkcif/rkcif-stream.c static const struct vb2_ops rkcif_stream_vb2_ops = { · line 341
Quoted 7 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +4
-0@@ -334,20 +335,39 @@ static const struct vb2_ops rkcif_stream_vb2_ops = { .stop_streaming = rkcif_stream_stop_streaming, }; +static void rkcif_stream_set_bytesperline (struct v4l2_pix_format_mplane *pix, +u32 req_bpl) +{ +u32 min_bpl =ALIGN (pix->plane_fmt[0 ].bytesperline,8 );
Please add support for the bytesperline field in all the plane_fmt entries here...
Quoted 13 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +11
-0+u32 max_bpl =min_t (u32,CIF_MAX_BYTESPERLINE , +ALIGN_DOWN (U32_MAX / pix->height,8 )); +u32 bpl = min_bpl; + +if (req_bpl > min_bpl && req_bpl <= max_bpl) + bpl =ALIGN (req_bpl,8 ); + + pix->plane_fmt[0 ].bytesperline = bpl; + pix->plane_fmt[0 ].sizeimage = bpl * pix->height; +} +static int rkcif_stream_fill_format (struct rkcif_stream *stream,struct v4l2_pix_format_mplane *pix)
drivers/media/platform/rockchip/rkcif/rkcif-stream.c static const struct vb2_ops rkcif_stream_vb2_ops = { · line 363
Quoted 10 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +2
-1{const struct rkcif_output_fmt *fmt; -u32 height, width; +u32 height, width, req_bpl;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 ); + req_bpl = pix->plane_fmt[0 ].bytesperline;
... and here.
drivers/media/platform/rockchip/rkcif/rkcif-stream.c static const struct vb2_ops rkcif_stream_vb2_ops = { · line 369
Quoted 6 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +2
-0ret =v4l2_fill_pixfmt_mp (pix, fmt->fourcc, width, height);if (ret)return ret; +if (stream->interface->type ==RKCIF_IF_MIPI ) +rkcif_stream_set_bytesperline (pix, req_bpl);
Why is this restricted to the MIPI interfaces?
Best regards,
Michael
Quoted 4 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +1
-0+ pix->field =V4L2_FIELD_NONE ;return 0 ;
Michael Riesch
maintainerRe: [PATCH 1/2] media: rockchip: rkcif: Filter enumerated formats by media bus code
In reply to Faizel K BHi there,
Quoted 23 lines · Faizel K B
Hi Mehdi,
Quoted 9 lines · Mehdi Djait
Quoted 9 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +1
-0@@ -395,13 +395,20 @@ static int rkcif_stream_enum_formats(struct file *file, void *priv, struct v4l2_fmtdesc *f) { struct rkcif_stream *stream = video_drvdata(file); + unsigned int i, n = 0;Quoted 9 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c static int rkcif_stream_enum_formats(struct file *file, void *priv, · line 402
Quoted 5 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +3
-2- if (f->index >= stream->out_fmts_num) - return -EINVAL; + for (i = 0; i < stream->out_fmts_num; i++) { + if (f->mbus_code && + stream->out_fmts[i].mbus_code != f->mbus_code)what about dvp capture. The dvp_out_fmts[] does not have the
mbus_codeset.
Quoted 3 lines
Thanks for the review. Yes, the DVP case is missing. How about only filtering the formats that have a media bus code, so DVP behaves as before (see the change).
This sounds reasonable.
Quoted 8 lines
const struct
rkcif_output_fmt*fmt = &stream->out_fmts[i];if (
f->mbus_code&&fmt->mbus_code&&
fmt->mbus_code!=f->mbus_code)
continue;I don't have DVP hardware to test with. I can submit a v2 if that's ok.
Looking forward to the v2.
Best regards,
Michael
Quoted 5 lines
Regards,
Faizel
Re: [PATCH 2/2] media: rockchip: rkcif: Honour the requested bytesperline on MIPI
In reply to Michael RieschHi Michael,
Quoted 6 lines · Michael Riesch
Come to think of it, TRM p. 2111 says for e.g.
VICAP_MIPI0_VLW_ID0:
"Virtual line width of even frame for ID0 path(must be aligned to double word)." which I would interpret as "it must be a multiple of four bytes". What do you think?
The VICAP register summary defines it.TRM part 1 (V1.0, page 2081) "DW - Double WORD (64 bits) access". So 8 bytes.
Quoted 13 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c static const struct vb2_ops rkcif_stream_vb2_ops = { · line 341
Quoted 4 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +4
-0+static void rkcif_stream_set_bytesperline (struct v4l2_pix_format_mplane *pix, +u32 req_bpl) +{ +u32 min_bpl =ALIGN (pix->plane_fmt[0 ].bytesperline,8 );Please add support for the bytesperline field in all the
plane_fmtentries here...drivers/media/platform/rockchip/rkcif/rkcif-stream.c static const struct vb2_ops rkcif_stream_vb2_ops = { · line 363
Quoted 2 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +1
-0width =clamp_t (u32, pix->width,CIF_MIN_WIDTH ,CIF_MAX_WIDTH ); + req_bpl = pix->plane_fmt[0 ].bytesperline;... and here.
Quoted 9 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c static const struct vb2_ops rkcif_stream_vb2_ops = { · line 369
Quoted 6 lines
drivers/media/platform/rockchip/rkcif/rkcif-stream.c +2
-0ret =v4l2_fill_pixfmt_mp (pix, fmt->fourcc, width, height);if (ret)return ret; +if (stream->interface->type ==RKCIF_IF_MIPI ) +rkcif_stream_set_bytesperline (pix, req_bpl);Why is this restricted to the MIPI interfaces?
The MIPI formats all have a single plane, and the MIPI path already programs the line width from bytesperline. The DVP path uses the width, and I can't test it. But I agree it should not depend on the interface.
For v2 I would:
- take the requested bytesperline of all planes into account, and use the largest for all planes, as the DVP has one line width register for Y and UV
- compute the sizeimage of each plane from it
- limit it to the line width field (20 bits MIPI, 15 bits DVP)
- program RKCIF_DVP_VIR_LINE_WIDTH from bytesperline, with the current value as the minimum
Does that sound ok?
A question on the DVP: the PX30 TRM (Part 2, 5.3) says raw data is stored as 16 bits per pixel, even from an 8-bit source. This matches the width * 2 in the driver. But for V4L2_PIX_FMT_S*8 (e.g. V4L2_PIX_FMT_SRGGB8) bytesperline is the width, so the buffer is half the size the hardware writes. Am I missing something?
I can only test the MIPI path, on RK3588 (Radxa ROCK 5B+). Could you test the DVP part on PX30 or RK3568?
Regards,
Faizel
Michael Riesch
maintainerRe: [PATCH 2/2] media: rockchip: rkcif: Honour the requested bytesperline on MIPI
Hi Faizel,
Quoted 4 lines · Faizel K B
Hi Michael,
Quoted 1 line · Michael Riesch
Quoted 8 lines
Quoted 5 lines
Come to think of it, TRM p. 2111 says for e.g.
VICAP_MIPI0_VLW_ID0:
"Virtual line width of even frame for ID0 path(must be aligned to double word)." which I would interpret as "it must be a multiple of four bytes". What do you think?The VICAP register summary defines it.TRM part 1 (V1.0, page 2081) "DW - Double WORD (64 bits) access". So 8 bytes.
Ack.
Quoted 38 lines
Quoted 14 lines
Quoted 5 lines
+static void rkcif_stream_set_bytesperline(struct
v4l2_pix_format_mplane*pix,
+ u32req_bpl)
+{
+ u32min_bpl=ALIGN(pix->plane_fmt[0].bytesperline, 8);Please add support for the bytesperline field in all the
plane_fmtentries here...Quoted 2 lines
width =
clamp_t(u32, pix->width, CIF_MIN_WIDTH, CIF_MAX_WIDTH); +req_bpl= pix->plane_fmt[0].bytesperline;... and here.
Quoted 9 lines
Quoted 6 lines
ret =
v4l2_fill_pixfmt_mp(pix, fmt->fourcc, width, height); if (ret)
return ret;+ if (
stream->interface->type==RKCIF_IF_MIPI) +rkcif_stream_set_bytesperline(pix, req_bpl);Why is this restricted to the MIPI interfaces?
The MIPI formats all have a single plane, and the MIPI path already programs the line width from bytesperline. The DVP path uses the width, and I can't test it. But I agree it should not depend on the interface.
For v2 I would:
- take the requested bytesperline of all planes into account, and use the largest for all planes, as the DVP has one line width register for Y and UV
- compute the sizeimage of each plane from it
- limit it to the line width field (20 bits MIPI, 15 bits DVP)
- programRKCIF_DVP_VIR_LINE_WIDTHfrom bytesperline, with the current value as the minimum
Quoted 1 line
Does that sound ok?
That does sound reasonable, yes.
Quoted 5 lines
A question on the DVP: the PX30 TRM (Part 2, 5.3) says raw data is stored as 16 bits per pixel, even from an 8-bit source. This matches the width * 2 in the driver. But for V4L2_PIX_FMT_S*8 (e.g.
V4L2_PIX_FMT_SRGGB8) bytesperline is the width, so the buffer is half the size the hardware writes. Am I missing something?
I don't have any PX30 board, and actually it is the first time I hear about that. Maybe Mehdi can chime in?
Quoted 2 lines
I can only test the MIPI path, on RK3588 (Radxa ROCK 5B+). Could you test the DVP part on PX30 or RK3568?
Currently, I don't have any active DVP setup, but when you send the updated patches I'll ask around.
Thanks and best regards,
Michael
Quoted 3 lines
Regards,
Faizel
_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Mehdi Djait
maintainerRe: [PATCH 2/2] media: rockchip: rkcif: Honour the requested bytesperline on MIPI
Hello,
Quoted 60 lines · Michael Riesch
Hi Faizel,
Quoted 4 lines · Faizel K B
Hi Michael,
Quoted 1 line · Michael Riesch
Quoted 8 lines
Quoted 5 lines
Come to think of it, TRM p. 2111 says for e.g.
VICAP_MIPI0_VLW_ID0:
"Virtual line width of even frame for ID0 path(must be aligned to double word)." which I would interpret as "it must be a multiple of four bytes". What do you think?The VICAP register summary defines it.TRM part 1 (V1.0, page 2081) "DW - Double WORD (64 bits) access". So 8 bytes.
Ack.
Quoted 38 lines
Quoted 14 lines
Quoted 5 lines
+static void rkcif_stream_set_bytesperline(struct
v4l2_pix_format_mplane*pix,
+ u32req_bpl)
+{
+ u32min_bpl=ALIGN(pix->plane_fmt[0].bytesperline, 8);Please add support for the bytesperline field in all the
plane_fmtentries here...Quoted 2 lines
width =
clamp_t(u32, pix->width, CIF_MIN_WIDTH, CIF_MAX_WIDTH); +req_bpl= pix->plane_fmt[0].bytesperline;... and here.
Quoted 9 lines
Quoted 6 lines
ret =
v4l2_fill_pixfmt_mp(pix, fmt->fourcc, width, height); if (ret)
return ret;+ if (
stream->interface->type==RKCIF_IF_MIPI) +rkcif_stream_set_bytesperline(pix, req_bpl);Why is this restricted to the MIPI interfaces?
The MIPI formats all have a single plane, and the MIPI path already programs the line width from bytesperline. The DVP path uses the width, and I can't test it. But I agree it should not depend on the interface.
For v2 I would:
- take the requested bytesperline of all planes into account, and use the largest for all planes, as the DVP has one line width register for Y and UV
- compute the sizeimage of each plane from it
- limit it to the line width field (20 bits MIPI, 15 bits DVP)
- programRKCIF_DVP_VIR_LINE_WIDTHfrom bytesperline, with the current value as the minimumQuoted 1 line
Does that sound ok?
That does sound reasonable, yes.
Quoted 8 lines
Quoted 5 lines
A question on the DVP: the PX30 TRM (Part 2, 5.3) says raw data is stored as 16 bits per pixel, even from an 8-bit source. This matches the width * 2 in the driver. But for V4L2_PIX_FMT_S*8 (e.g.
V4L2_PIX_FMT_SRGGB8) bytesperline is the width, so the buffer is half the size the hardware writes. Am I missing something?I don't have any PX30 board, and actually it is the first time I hear about that. Maybe Mehdi can chime in?
Unfortunately I also don't have hw with DVP and the one time I did look online, it is really not that easy to find.
The PX30 board I used to delevop the rkcif is a custom hw that I don't have access to anymore.
--
Kind Regards
Mehdi Djait