|
| 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); |
0 commit comments