Skip to content

drivers: video: common: fix video_closest_frmival() fie.index #89674

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

Merged

Conversation

josuah
Copy link
Collaborator

@josuah josuah commented May 8, 2025

Downstream:

Fix bug introduced in 46a262f where the fie.index field was expected to be incremented by the driver, while it is the responsibility of the caller to increment it.

Fixes #89663
Split out of #88003

Fix bug introduced in 46a262f where the
fie.index field was expected to be incremented by the driver, while it
is the responsibility of the caller to increment it.

Fixes zephyrproject-rtos#89663

Signed-off-by: Josuah Demangeon <me@josuah.net>
@josuah josuah added area: Drivers area: Tests Issues related to a particular existing or missing test area: Video Video subsystem area: camera labels May 8, 2025
@josuah josuah requested review from avolmat-st and ngphibang May 8, 2025 14:57
@github-actions github-actions bot requested a review from loicpoulain May 8, 2025 14:57
@josuah
Copy link
Collaborator Author

josuah commented May 8, 2025

Example of implementation that correctly keep fie.index unchanged:

zephyr/drivers/video/ov5640.c

Lines 1195 to 1221 in af77efb

static int ov5640_enum_frmival(const struct device *dev, enum video_endpoint_id ep,
struct video_frmival_enum *fie)
{
uint8_t i = 0;
if (ov5640_is_dvp(dev)) {
return -ENOTSUP;
}
for (i = 0; i < ARRAY_SIZE(csi2_modes); i++) {
if (fie->format->width == csi2_modes[i].width &&
fie->format->height == csi2_modes[i].height) {
break;
}
}
if (i == ARRAY_SIZE(csi2_modes) || fie->index >= ARRAY_SIZE(ov5640_frame_rates) ||
ov5640_frame_rates[fie->index] > csi2_modes[i].max_frmrate) {
return -EINVAL;
}
fie->type = VIDEO_FRMIVAL_TYPE_DISCRETE;
fie->discrete.numerator = 1;
fie->discrete.denominator = ov5640_frame_rates[fie->index];
return 0;
}

@josuah
Copy link
Collaborator Author

josuah commented May 8, 2025

As suggested, a second commit for a small optimization for exact matches.
Unrelated to the bug but applied to the same function.

@@ -153,10 +153,16 @@ void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
a = video_frmival_nsec(&desired);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more small optimization if I get it right. This first calculation of the desired frmival (a) only has to be done once, so should be done before entering the while loop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied thank you!

@@ -153,10 +153,16 @@ void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
a = video_frmival_nsec(&desired);
b = video_frmival_nsec(&tmp);
diff_nsec = a > b ? a - b : b - a;

if (diff_nsec < best_diff_nsec) {
best_diff_nsec = diff_nsec;
match->index = fie.index;
memcpy(&match->discrete, &tmp, sizeof(tmp));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about simply doing match->discrete = tmp here ? Meaning, simply letting the compiler decide what is the best way to do that, and figure out optimization, depending on alignments etc. There is good chances that it would end up to be same as memcpy in this case but this is also I think more easy to read.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better I agree!

@josuah josuah force-pushed the pr-video-fix-frmival branch from 6eb2646 to c32f8ff Compare May 8, 2025 23:16
@josuah
Copy link
Collaborator Author

josuah commented May 8, 2025

force-push:

  • Apply extra improvement suggested

@josuah josuah requested a review from avolmat-st May 8, 2025 23:17
@josuah josuah force-pushed the pr-video-fix-frmival branch from c32f8ff to bd176c2 Compare May 9, 2025 00:24
@github-actions github-actions bot requested review from facchinm and pillo79 May 9, 2025 00:25
josuah added 2 commits May 9, 2025 00:32
In video_closest_frmival(), immediately stop searching when an exact
match is found, as a small performance optimization. Variables
that could be computed only once were moved further outside.

Signed-off-by: Josuah Demangeon <me@josuah.net>
The assert message was not very helpful, CODE_UNREACHABLE is more
readable and requires fewer effort while proofreading.

Signed-off-by: Josuah Demangeon <me@josuah.net>
@josuah josuah force-pushed the pr-video-fix-frmival branch from bd176c2 to df243bc Compare May 9, 2025 00:33
@josuah josuah removed request for pillo79 and facchinm May 9, 2025 00:33
Copy link

sonarqubecloud bot commented May 9, 2025

@kartben kartben merged commit e1f4181 into zephyrproject-rtos:main May 12, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: camera area: Drivers area: Tests Issues related to a particular existing or missing test area: Video Video subsystem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

drivers: video: common: API misuse in video_closest_frmival()
5 participants