Skip to content

Commit bc382bc

Browse files
committed
tests: drivers: video: sw_stats: add tests for VIDEO_SW_DRIVER
Test it by filling a buffer manually with test data and verify that the statistics produced are matching. Signed-off-by: Josuah Demangeon <me@josuah.net>
1 parent aa1ded7 commit bc382bc

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(drivers_video_sw_stats)
6+
7+
target_sources(app PRIVATE src/main.c)

tests/drivers/video/sw_stats/prj.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_VIDEO=y
3+
CONFIG_VIDEO_SW_STATS=y
4+
CONFIG_VIDEO_BUFFER_POOL_SZ_MAX=16384
5+
CONFIG_VIDEO_BUFFER_POOL_NUM_MAX=1
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2025 tinyVision.ai Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
#include <zephyr/drivers/video.h>
9+
#include <zephyr/drivers/video-controls.h>
10+
11+
#define WIDTH 640
12+
#define HEIGHT 480
13+
#define ERR_MARGIN 3
14+
15+
uint8_t rggb8frame[WIDTH * HEIGHT * 1];
16+
17+
ZTEST(video_sw_stats, test_video_sw_stats)
18+
{
19+
const struct device *dev = device_get_binding("VIDEO_SW_STATS");
20+
struct video_stats_channels chan = {.base.flags = VIDEO_STATS_CHANNELS};
21+
struct video_stats_histogram hist = {.base.flags = VIDEO_STATS_HISTOGRAM};
22+
struct video_buffer *vbufp;
23+
struct video_buffer vbuf = {
24+
.buffer = rggb8frame,
25+
.size = sizeof(rggb8frame),
26+
};
27+
struct video_format fmt = {
28+
.pixelformat = VIDEO_PIX_FMT_RGGB8,
29+
.width = WIDTH,
30+
.pitch = WIDTH * video_bits_per_pixel(VIDEO_PIX_FMT_RGGB8) / BITS_PER_BYTE,
31+
.height = HEIGHT,
32+
};
33+
34+
zexpect_not_null(dev);
35+
zexpect_ok(device_is_ready(dev));
36+
37+
/* Load test data on the buffer: upper half completely white */
38+
memset(rggb8frame, 0xff, sizeof(rggb8frame) / 2);
39+
40+
zexpect_ok(video_set_format(dev, VIDEO_EP_IN, &fmt));
41+
42+
/* Test loading a buffer into the device */
43+
zexpect_ok(video_enqueue(dev, VIDEO_EP_IN, &vbuf));
44+
45+
/* Test collecting image statistics out of this buffer */
46+
zexpect_ok(video_get_stats(dev, VIDEO_EP_IN, &chan.base));
47+
zexpect_ok(video_get_stats(dev, VIDEO_EP_IN, &hist.base));
48+
49+
/* Test collecting image statistics out of this buffer */
50+
zexpect_ok(video_dequeue(dev, VIDEO_EP_IN, &vbufp, K_NO_WAIT));
51+
zexpect_not_null(vbufp);
52+
53+
/* Check the statistics content of channel averages */
54+
zexpect_equal(chan.base.flags & VIDEO_STATS_CHANNELS_RGB, VIDEO_STATS_CHANNELS_RGB);
55+
zexpect_within(chan.rgb[0], 0xff / 2 - ERR_MARGIN, 0xff / 2 + ERR_MARGIN);
56+
zexpect_within(chan.rgb[1], 0xff / 2 - ERR_MARGIN, 0xff / 2 + ERR_MARGIN);
57+
zexpect_within(chan.rgb[2], 0xff / 2 - ERR_MARGIN, 0xff / 2 + ERR_MARGIN);
58+
59+
/* Check the statistics content */
60+
zexpect_equal(hist.base.flags & VIDEO_STATS_HISTOGRAM_RGB, VIDEO_STATS_HISTOGRAM_RGB);
61+
zexpect_not_equal(hist.num_buckets, 0, "The histogram size must not be zero.");
62+
zexpect_not_equal(hist.num_values, 0, "The histogram must not be empty.");
63+
zexpect_within(hist.buckets[0], hist.num_values / 2 - ERR_MARGIN,
64+
hist.num_values / 2 + ERR_MARGIN, "Half of the image is 0x00");
65+
zexpect_within(hist.buckets[hist.num_buckets - 1], hist.num_values / 2 - ERR_MARGIN,
66+
hist.num_values / 2 + ERR_MARGIN, "Half of the image is 0xff");
67+
}
68+
69+
ZTEST_SUITE(video_sw_stats, NULL, NULL, NULL, NULL, NULL);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2025 tinyVision.ai Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
tests:
5+
drivers.video.api:
6+
tags:
7+
- drivers
8+
- video
9+
platform_allow: native_sim

0 commit comments

Comments
 (0)