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 <[email protected]>
  • Loading branch information
josuah committed Oct 26, 2024
1 parent 9fae2e7 commit 7069a8b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 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 @@ -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

Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions drivers/video/video_mcux_mipi_csi2rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
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) / 8;

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 @@ -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
*/
Expand All @@ -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).
*
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7069a8b

Please sign in to comment.