Skip to content

Commit 644123a

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 644123a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

drivers/video/video_common.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,27 @@ 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+
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
454+
455+
switch (fmt->pixelformat) {
456+
case VIDEO_PIX_FMT_JPEG:
457+
/* Rough estimate for the worst case (quality = 100) */
458+
fmt->size = fmt->width * fmt->height * 2;
459+
return 0;
460+
default:
461+
if (fmt->pitch != 0) {
462+
/* Uncompressed format */
463+
fmt->size = fmt->pitch * fmt->height;
464+
return 0;
465+
} else {
466+
return -ENOTSUP;
467+
}
468+
}
469+
}

include/zephyr/drivers/video.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,20 @@ 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+
* For uncompressed formats, it gives the actual size and pitch of the
995+
* whole raw image without any padding.
996+
*
997+
* For compressed formats, it gives a rough estimate size of a complete
998+
* compressed frame.
999+
*
1000+
* @param fmt The video format
1001+
* @return 0 on success, otherwise a negative errno code
1002+
*/
1003+
int video_estimate_fmt_size(struct video_format *fmt);
1004+
9911005
/**
9921006
* @defgroup video_pixel_formats Video pixel formats
9931007
* The '|' characters separate the pixels or logical blocks, and spaces separate the bytes.

0 commit comments

Comments
 (0)