Skip to content

Commit

Permalink
drivers: video: introduce video_bits_per_pixel(pixelformat) helper
Browse files Browse the repository at this point in the history
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 <me@josuah.net>
  • Loading branch information
josuah authored and kartben committed Jan 24, 2025
1 parent a6d68d2 commit 21da345
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
4 changes: 2 additions & 2 deletions drivers/video/video_mcux_csi.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,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) / BITS_PER_BYTE;
}
#endif

Expand All @@ -132,7 +132,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) / BITS_PER_BYTE;
status_t ret;
struct video_format format = *fmt;

Expand Down
6 changes: 3 additions & 3 deletions drivers/video/video_mcux_mipi_csi2rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ static int mipi_csi2rx_update_settings(const struct device *dev, enum video_endp
return -ENOTSUP;
}

bpp = video_pix_fmt_bpp(fmt.pixelformat) * 8;
sensor_byte_clk = sensor_pixel_rate * bpp / drv_data->csi2rxConfig.laneNum / 8;
bpp = video_bits_per_pixel(fmt.pixelformat);
sensor_byte_clk = sensor_pixel_rate * bpp / drv_data->csi2rxConfig.laneNum / BITS_PER_BYTE;

ret = clock_control_get_rate(drv_data->clock_dev, drv_data->clock_root, &root_clk_rate);
if (ret) {
Expand Down Expand Up @@ -224,7 +224,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,
Expand Down
2 changes: 1 addition & 1 deletion drivers/video/video_stm32_dcmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) / BITS_PER_BYTE;

if (bpp == 0 || (ep != VIDEO_EP_OUT && ep != VIDEO_EP_ALL)) {
return -EINVAL;
Expand Down
40 changes: 22 additions & 18 deletions include/zephyr/drivers/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,14 @@ void video_closest_frmival_stepwise(const struct video_frmival_stepwise *stepwis
void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
struct video_frmival_enum *match);

/**
* @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
*/
Expand All @@ -829,14 +837,6 @@ void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
*/
#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).
*
Expand Down Expand Up @@ -966,33 +966,37 @@ void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
*/

/**
* @}
*/

/**
* @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
Expand Down

0 comments on commit 21da345

Please sign in to comment.