Skip to content

Commit aa8b23d

Browse files
committed
drivers: video: Add helper to estimate format size
Add a helper to estimate format size and pitch. Signed-off-by: Phi Bang Nguyen <[email protected]>
1 parent 5c8380d commit aa8b23d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

drivers/video/video_common.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,26 @@ int64_t video_get_csi_link_freq(const struct device *dev, uint8_t bpp, uint8_t l
443443
/* CSI D-PHY is using a DDR data bus so bitrate is twice the frequency */
444444
return ctrl.val64 * bpp / (2 * lane_nb);
445445
}
446+
447+
int video_estimate_fmt_size(struct video_format *fmt)
448+
{
449+
if (fmt == NULL) {
450+
return -EINVAL;
451+
}
452+
453+
switch (fmt->pixelformat) {
454+
case VIDEO_PIX_FMT_JPEG:
455+
/* Rough estimate for the worst case (quality = 100) */
456+
fmt->pitch = 0;
457+
fmt->size = fmt->width * fmt->height * 2;
458+
return 0;
459+
default:
460+
/* Uncompressed format */
461+
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
462+
if (fmt->pitch == 0) {
463+
return -ENOTSUP;
464+
}
465+
fmt->size = fmt->pitch * fmt->height;
466+
return 0;
467+
}
468+
}

include/zephyr/drivers/video.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,22 @@ void video_closest_frmival(const struct device *dev, struct video_frmival_enum *
988988
*/
989989
int64_t video_get_csi_link_freq(const struct device *dev, uint8_t bpp, uint8_t lane_nb);
990990

991+
/**
992+
* @brief Estimate the size and pitch in bytes of a @ref video_format
993+
*
994+
* This helper should only be used by drivers that support the whole image frame.
995+
*
996+
* For uncompressed formats, it gives the actual size and pitch of the
997+
* whole raw image without any padding.
998+
*
999+
* For compressed formats, it gives a rough estimate size of a complete
1000+
* compressed frame.
1001+
*
1002+
* @param fmt Pointer to the video format structure
1003+
* @return 0 on success, otherwise a negative errno code
1004+
*/
1005+
int video_estimate_fmt_size(struct video_format *fmt);
1006+
9911007
/**
9921008
* @defgroup video_pixel_formats Video pixel formats
9931009
* The '|' characters separate the pixels or logical blocks, and spaces separate the bytes.

0 commit comments

Comments
 (0)