-
Notifications
You must be signed in to change notification settings - Fork 8k
drivers: video: Add format size #97197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
ngphibang
commented
Oct 8, 2025
- Add a size field into video_format struct.
- Video receiver drivers now need to set the format's size to expose to application. Application should base on the format size to allocate buffers. The caps' min/max_line_count fields (which are needed only for HWs that cannot support the whole image frame) can hence be dropped.
It seems to me that currently there is a hole if a sensor provides a compressed format (ex: OV5640 which is capable, from a HW point of view, to provide JPEG data). So, if the video_bits_per_pixel reports a non zero value, a receiver can estimate the pitch and size (as done in this PR), however if the video_bits_per_pixel is zero (as it would be in case of JPEG), then we cannot apply this calculation and need to get this from another way. (either from the sensor itself, since it could estimate it itself, knowing the quality settings etc etc, or by a rather simple estimation in each receiver, but not based on the video_bits_per_pixel). |
Yes, we should distinguish uncompressed vs compressed formats (can be based on fmt->pitch or video_bits_per_pixel). Will update. |
Yes. Cannot be done on the pitch value I think since pitch is related to the way it is organized into memory, which is not applicable for sensors (which do not write themselves into memory). Hence my proposal to look at video_bits_per_pixel. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/video/video_shell.c
This has a print statement for logging the max count
https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/video/video_emul_rx.c
This will be needing some conversion too, useful for testing this change on CI.
This sounds like a better solution than what I proposed here IMHO #92884 (comment)
8c52f1e
to
c4b4648
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ngphibang. All good to me.
c4b4648
to
bb65040
Compare
73736a2
to
ffea025
Compare
Dropped caps' min_line_count in the recently merged VENC driver. |
Added 4.3 milestone as #95862 was merged which requires drivers to set format size |
drivers/video/video_common.c
Outdated
return -EINVAL; | ||
} | ||
|
||
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea to also set the pitch.
include/zephyr/drivers/video.h
Outdated
* For compressed formats, it gives a rough estimate size of a complete | ||
* compressed frame. | ||
* | ||
* @param fmt The video format |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
@param fmt Pointer to video format struct
so that we use same wording as for other helpers.
drivers/video/video_common.c
Outdated
return -EINVAL; | ||
} | ||
|
||
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fmt->pitch could be done within the default case since it doesn't make sense for the JPEG part for which this will always lead to 0. Thus what about setting fmt->pitch = 0 for JPEG instead ?
drivers/video/video_common.c
Outdated
fmt->size = fmt->width * fmt->height * 2; | ||
return 0; | ||
default: | ||
if (fmt->pitch != 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing the opposite test, aka if (fmt->pitch == 0) { return -ENOSUP; } would allow to have less { } { } and keep the expected behavior code with less indentation.
Aka something like:
switch (fmt->pixelformat) {
case VIDEO_PIX_FMT_JPEG:
/* Rough estimate for the worst case (quality = 100) */
fmt->pitch = 0;
fmt->size = fmt->width * fmt->height * 2;
break;
default:
/* Uncompressed format */
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
if (fmt->pitch == 0) {
return -ENOTSUP;
}
fmt->size = fmt->pitch * fmt->height;
break;
}
return 0;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oups, noticed break were missing here ;) Updated.
drivers/video/video_emul_rx.c
Outdated
} | ||
|
||
/* Cache the format selected locally to use it for getting the size of the buffer */ | ||
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be replaced by video_estimate_fmt_size call ?
drivers/video/video_mcux_csi.c
Outdated
break; | ||
} | ||
|
||
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be replaced by video_estimate_fmt_size ?
drivers/video/video_renesas_ra_ceu.c
Outdated
return ret; | ||
} | ||
|
||
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use video_estimate_fmt_size ?
drivers/video/video_renesas_ra_ceu.c
Outdated
return -EIO; | ||
} | ||
|
||
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use video_estimate_fmt_size ?
drivers/video/video_sw_generator.c
Outdated
return ret; | ||
} | ||
|
||
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use video_estimate_fmt_size ?
drivers/video/video_sw_generator.c
Outdated
|
||
*fmt = data->fmt; | ||
|
||
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use video_estimate_fmt_size ?
3b29e74
to
da0ba92
Compare
drivers/video/video_common.c
Outdated
/* Rough estimate for the worst case (quality = 100) */ | ||
fmt->pitch = 0; | ||
fmt->size = fmt->width * fmt->height * 2; | ||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return 0 at the end of the function seems better to me since currently a rapid look is showing that nothing is returned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It returns for all cases and the compiler does not complain. I think if we return at the end, we need to do "break;" and this will take more lines, just that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I wasn't pointing that a return was missing, I am only pointing that this feel as if there were a return missing at the end since this is a non void function which finish with
}
}
I'd feel better to have in such case only one return.
|
||
/* Cache the format selected locally to use it for getting the size of the buffer */ | ||
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE; | ||
ret = video_estimate_fmt_size(fmt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok for this part. However I think similar one is missing in the init function of this driver since doing a get_fmt without a set first will lead just read copy the fmt structure initialized during the init, into which only pitch is set. Hence video_estimate_fmt_size can simply be called during the _init function instead of the existing fmt->pitch set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it lacks a call in init(), will add it, but we still need to call video_estimate_fmt_size() in each set_format() because the call in init() is only for the default format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes agree. I just used this part to comment since I couldn't point the _init section of course ;)
drivers/video/video_mcux_csi.c
Outdated
} | ||
|
||
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE; | ||
video_estimate_fmt_size(fmt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. However it seems to me that video_estimate_fmt_size call is missing the in get_fmt function, in case of CONFIG_VIDEO_MCUX_MIPI_CSI2RX is not defined. In such situation result of the video_get_format from the source device is returned straight.
Add a helper to estimate format size and pitch. Signed-off-by: Phi Bang Nguyen <[email protected]>
da0ba92
to
a32e42b
Compare
Receiver drivers now need to set the format's size to expose it to the application. Application should base on the format size to allocate buffers. The caps' min/max_line_count (which are needed only for HWs that cannot support the whole image frame) can hence be dropped. Signed-off-by: Phi Bang Nguyen <[email protected]>
The mipid02 is just a bridge driver which does not deal with memory so should not expose caps' min_vbuf_count. Signed-off-by: Phi Bang Nguyen <[email protected]>
Mention some dropped fields in the video_caps structure and the buffer allocation size for application. Signed-off-by: Phi Bang Nguyen <[email protected]>
Mention a new video_estimate_fmt_size helper. Signed-off-by: Phi Bang Nguyen <[email protected]>
a32e42b
to
ab8ea7b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks a lot.
|
Regarding the new issue reported, we might want to add more compressed format in the future in this generic helper so I propose to not take it into account. |