From 7069a8b16053dbb30693e09ba12b39e24de6c527 Mon Sep 17 00:00:00 2001 From: Josuah Demangeon Date: Mon, 21 Oct 2024 10:34:26 +0000 Subject: [PATCH] drivers: video: introduce video_bits_per_pixel(pixelformat) helper This was present in the form of video_pix_fmt_bpp() inside ST and NXP drivers, and was returning the number of bytes, which does not allow support for 10-bits, 4-bits or non-byte aligned video formats. The helper leverages the VIDEO_PIX_FMT_*_BITS macros. Signed-off-by: Josuah Demangeon --- drivers/video/video_mcux_csi.c | 4 +-- drivers/video/video_mcux_mipi_csi2rx.c | 4 +-- drivers/video/video_stm32_dcmi.c | 2 +- include/zephyr/drivers/video.h | 40 ++++++++++++++------------ 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/drivers/video/video_mcux_csi.c b/drivers/video/video_mcux_csi.c index 244dc61ab6a6684..1aa65e5aa39b41f 100644 --- a/drivers/video/video_mcux_csi.c +++ b/drivers/video/video_mcux_csi.c @@ -133,7 +133,7 @@ static inline void video_pix_fmt_convert(struct video_format *fmt, bool isGetFmt break; } - fmt->pitch = fmt->width * video_pix_fmt_bpp(fmt->pixelformat); + fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / 8; } #endif @@ -142,7 +142,7 @@ static int video_mcux_csi_set_fmt(const struct device *dev, enum video_endpoint_ { const struct video_mcux_csi_config *config = dev->config; struct video_mcux_csi_data *data = dev->data; - unsigned int bpp = video_pix_fmt_bpp(fmt->pixelformat); + unsigned int bpp = video_bits_per_pixel(fmt->pixelformat) / 8; status_t ret; struct video_format format = *fmt; diff --git a/drivers/video/video_mcux_mipi_csi2rx.c b/drivers/video/video_mcux_mipi_csi2rx.c index a73c17efa07fe0f..a3e5cc7c3142e4a 100644 --- a/drivers/video/video_mcux_mipi_csi2rx.c +++ b/drivers/video/video_mcux_mipi_csi2rx.c @@ -78,7 +78,7 @@ static int mipi_csi2rx_update_settings(const struct device *dev, enum video_endp return -ENOTSUP; } - bpp = video_pix_fmt_bpp(fmt.pixelformat) * 8; + bpp = video_bits_per_pixel(fmt.pixelformat); sensor_byte_clk = sensor_pixel_rate * bpp / drv_data->csi2rxConfig.laneNum / 8; ret = clock_control_get_rate(drv_data->clock_dev, drv_data->clock_root, &root_clk_rate); @@ -229,7 +229,7 @@ static int mipi_csi2rx_get_frmival(const struct device *dev, enum video_endpoint static uint64_t mipi_csi2rx_cal_frame_size(const struct video_format *fmt) { - return fmt->height * fmt->width * video_pix_fmt_bpp(fmt->pixelformat) * 8; + return fmt->height * fmt->width * video_bits_per_pixel(fmt->pixelformat); } static uint64_t mipi_csi2rx_estimate_pixel_rate(const struct video_frmival *cur_fmival, diff --git a/drivers/video/video_stm32_dcmi.c b/drivers/video/video_stm32_dcmi.c index 2af5dc14b5a9cd6..49f0b3cdcdadfbd 100644 --- a/drivers/video/video_stm32_dcmi.c +++ b/drivers/video/video_stm32_dcmi.c @@ -198,7 +198,7 @@ static int video_stm32_dcmi_set_fmt(const struct device *dev, { const struct video_stm32_dcmi_config *config = dev->config; struct video_stm32_dcmi_data *data = dev->data; - unsigned int bpp = video_pix_fmt_bpp(fmt->pixelformat); + unsigned int bpp = video_bits_per_pixel(fmt->pixelformat) / 8; if (bpp == 0 || (ep != VIDEO_EP_OUT && ep != VIDEO_EP_ALL)) { return -EINVAL; diff --git a/include/zephyr/drivers/video.h b/include/zephyr/drivers/video.h index 187bee0ad7320ab..f1e7acd9317b61d 100644 --- a/include/zephyr/drivers/video.h +++ b/include/zephyr/drivers/video.h @@ -753,6 +753,14 @@ struct video_buffer *video_buffer_alloc(size_t size); */ void video_buffer_release(struct video_buffer *buf); +/** + * @defgroup video_pixel_formats Video pixel formats + * The @c | characters separate the pixels, and spaces separate the bytes. + * The uppercase letter represents the most significant bit. + * The lowercase letters represent the rest of the bits. + * @{ + */ + /** * @brief Four-character-code uniquely identifying the pixel format */ @@ -770,14 +778,6 @@ void video_buffer_release(struct video_buffer *buf); */ #define VIDEO_FOURCC_FROM_STR(str) VIDEO_FOURCC((str)[0], (str)[1], (str)[2], (str)[3]) -/** - * @defgroup video_pixel_formats Video pixel formats - * The @c | characters separate the pixels, and spaces separate the bytes. - * The uppercase letter represents the most significant bit. - * The lowercase letters represent the rest of the bits. - * @{ - */ - /** * @name Bayer formats (R, G, B channels). * @@ -907,33 +907,37 @@ void video_buffer_release(struct video_buffer *buf); */ /** - * @} - */ - -/** - * @brief Get number of bytes per pixel of a pixel format + * @brief Get number of bits per pixel of a pixel format + * + * @param pixfmt FourCC pixel format value (@ref video_pixel_formats). * - * @param pixfmt FourCC pixel format value (\ref video_pixel_formats). + * @retval 0 if the format is unhandled or if it is variable number of bits + * @retval bit size of one pixel for this format */ -static inline unsigned int video_pix_fmt_bpp(uint32_t pixfmt) +static inline unsigned int video_bits_per_pixel(uint32_t pixfmt) { switch (pixfmt) { case VIDEO_PIX_FMT_BGGR8: case VIDEO_PIX_FMT_GBRG8: case VIDEO_PIX_FMT_GRBG8: case VIDEO_PIX_FMT_RGGB8: - return 1; + return 8; case VIDEO_PIX_FMT_RGB565: case VIDEO_PIX_FMT_YUYV: - return 2; + return 16; case VIDEO_PIX_FMT_XRGB32: case VIDEO_PIX_FMT_XYUV32: - return 4; + return 32; default: + /* Variable number of bits per pixel or unknown format */ return 0; } } +/** + * @} + */ + #ifdef __cplusplus } #endif