drivers: video: Move format pitch setting to bridge drivers

The format pitch (bytesperline) field is typically set by the bridge
drivers, i.e. DMA, ISP drivers who actually handle the memory as they
know exactly the memory layout constraints.

Application just set the pixel format and resolution and must always
read back this field to see what the driver actually sets (to allocate
buffers for example).

Also, drop format pitch setting in sensor drivers as this is not needed.

Signed-off-by: Phi Bang Nguyen <phibang.nguyen@nxp.com>
This commit is contained in:
Phi Bang Nguyen 2025-05-14 19:46:14 +02:00 committed by Benjamin Cabé
commit 0c1e2c9e6d
17 changed files with 39 additions and 31 deletions

View file

@ -1183,7 +1183,6 @@ static int gc2145_init(const struct device *dev)
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
fmt.width = RESOLUTION_QVGA_W;
fmt.height = RESOLUTION_QVGA_H;
fmt.pitch = RESOLUTION_QVGA_W * video_bits_per_pixel(fmt.pixelformat) / BITS_PER_BYTE;
ret = gc2145_set_fmt(dev, &fmt);
if (ret) {

View file

@ -564,7 +564,6 @@ static int mt9m114_init(const struct device *dev)
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
fmt.width = 480;
fmt.height = 272;
fmt.pitch = fmt.width * video_bits_per_pixel(fmt.pixelformat) / BITS_PER_BYTE;
ret = mt9m114_set_fmt(dev, &fmt);
if (ret) {

View file

@ -1032,7 +1032,6 @@ static int ov2640_init(const struct device *dev)
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
fmt.width = SVGA_HSIZE;
fmt.height = SVGA_VSIZE;
fmt.pitch = SVGA_HSIZE * video_bits_per_pixel(fmt.pixelformat) / BITS_PER_BYTE;
ret = ov2640_set_fmt(dev, &fmt);
if (ret) {
LOG_ERR("Unable to configure default format");

View file

@ -1433,7 +1433,6 @@ static int ov5640_init(const struct device *dev)
fmt.width = 1280;
fmt.height = 720;
}
fmt.pitch = fmt.width * video_bits_per_pixel(fmt.pixelformat) / BITS_PER_BYTE;
ret = ov5640_set_fmt(dev, &fmt);
if (ret) {
LOG_ERR("Unable to configure default format");

View file

@ -554,7 +554,6 @@ static int ov7670_init(const struct device *dev)
fmt.pixelformat = VIDEO_PIX_FMT_YUYV;
fmt.width = 640;
fmt.height = 480;
fmt.pitch = fmt.width * video_bits_per_pixel(fmt.pixelformat) / BITS_PER_BYTE;
ret = ov7670_set_fmt(dev, &fmt);
if (ret < 0) {
return ret;

View file

@ -592,7 +592,6 @@ static int ov7725_init(const struct device *dev)
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
fmt.width = 640;
fmt.height = 480;
fmt.pitch = fmt.width * video_bits_per_pixel(fmt.pixelformat) / BITS_PER_BYTE;
ret = ov7725_set_fmt(dev, &fmt);
if (ret) {
LOG_ERR("Unable to configure default format");

View file

@ -368,7 +368,6 @@ int emul_imager_init(const struct device *dev)
fmt.pixelformat = fmts[0].pixelformat;
fmt.width = fmts[0].width_min;
fmt.height = fmts[0].height_min;
fmt.pitch = fmt.width * video_bits_per_pixel(fmt.pixelformat) / BITS_PER_BYTE;
ret = emul_imager_set_fmt(dev, &fmt);
if (ret < 0) {

View file

@ -67,7 +67,9 @@ static int emul_rx_set_fmt(const struct device *const dev, struct video_format *
}
/* Cache the format selected locally to use it for getting the size of the buffer */
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
data->fmt = *fmt;
return 0;
}
@ -216,6 +218,9 @@ int emul_rx_init(const struct device *dev)
return ret;
}
data->fmt.pitch =
data->fmt.width * video_bits_per_pixel(data->fmt.pixelformat) / BITS_PER_BYTE;
k_fifo_init(&data->fifo_in);
k_fifo_init(&data->fifo_out);
k_work_init(&data->work, &emul_rx_worker);

View file

@ -269,6 +269,8 @@ static int video_esp32_get_fmt(const struct device *dev, struct video_format *fm
return ret;
}
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
return 0;
}
@ -276,14 +278,22 @@ static int video_esp32_set_fmt(const struct device *dev, struct video_format *fm
{
const struct video_esp32_config *cfg = dev->config;
struct video_esp32_data *data = dev->data;
int ret;
if (fmt == NULL) {
return -EINVAL;
}
ret = video_set_format(cfg->source_dev, fmt);
if (ret < 0) {
return ret;
}
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
data->video_format = *fmt;
return video_set_format(cfg->source_dev, fmt);
return 0;
}
static int video_esp32_enqueue(const struct device *dev, struct video_buffer *vbuf)

View file

@ -133,16 +133,11 @@ static int video_mcux_csi_set_fmt(const struct device *dev, struct video_format
{
const struct video_mcux_csi_config *config = dev->config;
struct video_mcux_csi_data *data = dev->data;
unsigned int bpp = video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
status_t ret;
struct video_format format = *fmt;
if (bpp == 0) {
return -EINVAL;
}
data->csi_config.bytesPerPixel = bpp;
data->csi_config.linePitch_Bytes = fmt->pitch;
data->csi_config.bytesPerPixel = video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
data->csi_config.linePitch_Bytes = fmt->width * data->csi_config.bytesPerPixel;
#if defined(CONFIG_VIDEO_MCUX_MIPI_CSI2RX)
if (fmt->pixelformat != VIDEO_PIX_FMT_XRGB32 && fmt->pixelformat != VIDEO_PIX_FMT_XYUV32) {
return -ENOTSUP;
@ -172,6 +167,8 @@ static int video_mcux_csi_set_fmt(const struct device *dev, struct video_format
return -EIO;
}
fmt->pitch = data->csi_config.linePitch_Bytes;
return 0;
}

View file

@ -218,6 +218,7 @@ static const struct video_format_cap fmts[] = {
static int nxp_video_sdma_set_format(const struct device *dev, struct video_format *fmt)
{
const struct nxp_video_sdma_config *config = dev->config;
int ret;
if (fmt == NULL) {
return -EINVAL;
@ -230,14 +231,20 @@ static int nxp_video_sdma_set_format(const struct device *dev, struct video_form
if ((fmt->pixelformat != fmts[0].pixelformat) ||
(fmt->width != fmts[0].width_min) ||
(fmt->height != fmts[0].height_min) ||
(fmt->pitch != fmts[0].width_min * 2)) {
(fmt->height != fmts[0].height_min)) {
LOG_ERR("Unsupported format");
return -ENOTSUP;
}
/* Forward format to sensor device */
return video_set_format(config->sensor_dev, fmt);
ret = video_set_format(config->sensor_dev, fmt);
if (ret < 0) {
return ret;
}
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
return 0;
}
static int nxp_video_sdma_get_format(const struct device *dev, struct video_format *fmt)
@ -267,8 +274,7 @@ static int nxp_video_sdma_get_format(const struct device *dev, struct video_form
/* Verify that format is RGB565 */
if ((fmt->pixelformat != fmts[0].pixelformat) ||
(fmt->width != fmts[0].width_min) ||
(fmt->height != fmts[0].height_min) ||
(fmt->pitch != fmts[0].width_min * 2)) {
(fmt->height != fmts[0].height_min)) {
/* Update format of sensor */
fmt->pixelformat = fmts[0].pixelformat;
fmt->width = fmts[0].width_min;
@ -281,6 +287,8 @@ static int nxp_video_sdma_get_format(const struct device *dev, struct video_form
}
}
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
return 0;
}

View file

@ -197,6 +197,8 @@ static int video_stm32_dcmi_set_fmt(const struct device *dev, struct video_forma
return ret;
}
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
data->fmt = *fmt;
return 0;
@ -218,6 +220,8 @@ static int video_stm32_dcmi_get_fmt(const struct device *dev, struct video_forma
return ret;
}
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
data->fmt = *fmt;
return 0;

View file

@ -81,6 +81,8 @@ static int video_sw_generator_set_fmt(const struct device *dev, struct video_for
return -ENOTSUP;
}
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
data->fmt = *fmt;
return 0;

View file

@ -150,7 +150,6 @@ int main(void)
#if CONFIG_VIDEO_FRAME_WIDTH
fmt.width = CONFIG_VIDEO_FRAME_WIDTH;
fmt.pitch = fmt.width * video_bits_per_pixel(fmt.pixelformat) / BITS_PER_BYTE;
#endif
if (strcmp(CONFIG_VIDEO_PIXEL_FORMAT, "")) {

View file

@ -79,7 +79,6 @@ int main(void)
/* Set format */
fmt.width = CONFIG_VIDEO_WIDTH;
fmt.height = CONFIG_VIDEO_HEIGHT;
fmt.pitch = fmt.width * video_bits_per_pixel(fmt.pixelformat) / BITS_PER_BYTE;
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
if (video_set_format(video_dev, &fmt)) {

View file

@ -36,40 +36,34 @@ ZTEST(video_common, test_video_format_caps_index)
fmt.width = 100;
fmt.height = 100;
fmt.pitch = 100 * 2;
ret = video_format_caps_index(fmts, &fmt, &idx);
zassert_ok(ret, "expecting minimum value to match");
zassert_equal(idx, YUYV_A);
fmt.width = 1000;
fmt.height = 1000;
fmt.pitch = 1000 * 2;
ret = video_format_caps_index(fmts, &fmt, &idx);
zassert_ok(ret, "expecting maximum value to match");
zassert_equal(idx, YUYV_A);
fmt.width = 1920;
fmt.height = 1080;
fmt.pitch = 1920 * 2;
ret = video_format_caps_index(fmts, &fmt, &idx);
zassert_ok(ret, "expecting exact match to work");
zassert_equal(idx, YUYV_B);
fmt.width = 1001;
fmt.height = 1000;
fmt.pitch = 1001 * 2;
ret = video_format_caps_index(fmts, &fmt, &idx);
zassert_not_ok(ret, "expecting 1 above maximum width to mismatch");
fmt.width = 1000;
fmt.height = 1001;
fmt.pitch = 1000 * 2;
ret = video_format_caps_index(fmts, &fmt, &idx);
zassert_not_ok(ret, "expecting 1 above maximum height to mismatch");
fmt.width = 1280;
fmt.height = 720;
fmt.pitch = 1280 * 2;
ret = video_format_caps_index(fmts, &fmt, &idx);
zassert_not_ok(ret);
zassert_not_ok(ret, "expecting wrong format to mismatch");
@ -78,13 +72,11 @@ ZTEST(video_common, test_video_format_caps_index)
fmt.width = 1000;
fmt.height = 1000;
fmt.pitch = 1000 * 2;
ret = video_format_caps_index(fmts, &fmt, &idx);
zassert_not_ok(ret, "expecting wrong format to mismatch");
fmt.width = 1280;
fmt.height = 720;
fmt.pitch = 1280 * 2;
ret = video_format_caps_index(fmts, &fmt, &idx);
zassert_ok(ret, "expecting exact match to work");
zassert_equal(idx, RGB565);

View file

@ -158,7 +158,6 @@ ZTEST(video_common, test_video_vbuf)
fmt.pixelformat = caps.format_caps[0].pixelformat;
fmt.width = caps.format_caps[0].width_max;
fmt.height = caps.format_caps[0].height_max;
fmt.pitch = fmt.width * video_bits_per_pixel(fmt.pixelformat) / BITS_PER_BYTE;
fmt.type = type;
zexpect_ok(video_set_format(rx_dev, &fmt));