Skip to content

Commit

Permalink
drivers: video: add a virtual device pipeline
Browse files Browse the repository at this point in the history
PR zephyrproject-rtos#79482 is where this commit would be added

Signed-off-by: Josuah Demangeon <me@josuah.net>
  • Loading branch information
josuah committed Nov 12, 2024
1 parent 9f8d85a commit c048319
Show file tree
Hide file tree
Showing 10 changed files with 1,021 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drivers/video/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ zephyr_library_sources_ifdef(CONFIG_VIDEO_OV5640 ov5640.c)
zephyr_library_sources_ifdef(CONFIG_VIDEO_OV7670 ov7670.c)
zephyr_library_sources_ifdef(CONFIG_VIDEO_ESP32 video_esp32_dvp.c)
zephyr_library_sources_ifdef(CONFIG_VIDEO_MCUX_SDMA video_mcux_smartdma.c)
zephyr_library_sources_ifdef(CONFIG_VIDEO_EMUL_IMAGER video_emul_imager.c)
zephyr_library_sources_ifdef(CONFIG_VIDEO_EMUL_RX video_emul_rx.c)
4 changes: 4 additions & 0 deletions drivers/video/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ source "drivers/video/Kconfig.gc2145"

source "drivers/video/Kconfig.mcux_sdma"

source "drivers/video/Kconfig.emul_imager"

source "drivers/video/Kconfig.emul_rx"

endif # VIDEO
18 changes: 18 additions & 0 deletions drivers/video/Kconfig.emul_imager
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2024 tinyVision.ai Inc.
# SPDX-License-Identifier: Apache-2.0

config VIDEO_EMUL_IMAGER
bool "Software implementation of an imager"
depends on DT_HAS_ZEPHYR_VIDEO_EMUL_IMAGER_ENABLED
default y
help
Enable driver for the emulated Imager.

config VIDEO_EMUL_IMAGER_FRAMEBUFFER_SIZE
int "Internal framebuffer size used for link emulation purpose"
default 4096
help
Configure the size of the internal framebuffer the emulated Imager
driver uses to simulate MIPI transfers. This is the first field of
dev->data, and the emulated video MIPI driver will `memcpy()` it
into the video buffer.
10 changes: 10 additions & 0 deletions drivers/video/Kconfig.emul_rx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2024 tinyVision.ai Inc.
# SPDX-License-Identifier: Apache-2.0

config VIDEO_EMUL_RX
bool "Software implementation of video frame RX core"
depends on DT_HAS_ZEPHYR_VIDEO_EMUL_RX_ENABLED
depends on VIDEO_EMUL_IMAGER
default y
help
Enable driver for the MIPI RX emulated DMA engine.
85 changes: 85 additions & 0 deletions drivers/video/video_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <string.h>

#include <zephyr/kernel.h>
#include <zephyr/drivers/video.h>

Expand Down Expand Up @@ -83,3 +85,86 @@ void video_buffer_release(struct video_buffer *vbuf)
VIDEO_COMMON_FREE(block->data);
}
}

int video_format_caps_index(const struct video_format_cap *fmts, const struct video_format *fmt,
size_t *idx)
{
for (int i = 0; fmts[i].pixelformat != 0; i++) {
if (fmts[i].pixelformat == fmt->pixelformat &&
IN_RANGE(fmt->width, fmts[i].width_min, fmts[i].width_max) &&
IN_RANGE(fmt->height, fmts[i].height_min, fmts[i].height_max)) {
*idx = i;
return 0;
}
}
return -ENOENT;
}

uint64_t video_frmival_nsec(const struct video_frmival *frmival)
{
return (uint64_t)NSEC_PER_SEC * frmival->numerator / frmival->denominator;
}

void video_closest_frmival_stepwise(const struct video_frmival_stepwise *stepwise,
const struct video_frmival *desired,
struct video_frmival *match)
{
uint64_t min = stepwise->min.numerator;
uint64_t max = stepwise->max.numerator;
uint64_t step = stepwise->step.numerator;
uint64_t goal = desired->numerator;

/* Set a common denominator to all values */
min *= stepwise->max.denominator * stepwise->step.denominator * desired->denominator;
max *= stepwise->min.denominator * stepwise->step.denominator * desired->denominator;
step *= stepwise->min.denominator * stepwise->max.denominator * desired->denominator;
goal *= stepwise->min.denominator * stepwise->max.denominator * stepwise->step.denominator;

/* Saturate the desired value to the min/max supported */
goal = CLAMP(goal, min, max);

/* Compute a numerator and denominator */
match->numerator = min + DIV_ROUND_CLOSEST(goal - min, step) * step;
match->denominator = stepwise->min.denominator * stepwise->max.denominator *
stepwise->step.denominator * desired->denominator;
}

void video_closest_frmival(const struct device *dev, enum video_endpoint_id ep,
struct video_frmival_enum *match)
{
uint64_t best_diff_nsec = INT32_MAX;
struct video_frmival desired = match->discrete;
struct video_frmival_enum fie = {.format = match->format};

__ASSERT(match->type != VIDEO_FRMIVAL_TYPE_STEPWISE,
"cannot find range matching the range, only a value matching the range");

while (video_enum_frmival(dev, ep, &fie) == 0) {
struct video_frmival tmp = {0};
uint64_t diff_nsec = 0, a, b;

switch (fie.type) {
case VIDEO_FRMIVAL_TYPE_DISCRETE:
tmp = fie.discrete;
break;
case VIDEO_FRMIVAL_TYPE_STEPWISE:
video_closest_frmival_stepwise(&fie.stepwise, &desired, &tmp);
break;
default:
__ASSERT(false, "invalid answer from the queried video device");
}

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;
memcpy(&match->discrete, &tmp, sizeof(tmp));

/* The video_enum_frmival() function will increment fie.index every time.
* Compensate for it to get the current index, not the next index.
*/
match->index = fie.index - 1;
}
}
}
Loading

0 comments on commit c048319

Please sign in to comment.