1 /*
2 * Copyright (c) 2024 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
10 enum {
11 RGB565,
12 YUYV_A,
13 YUYV_B,
14 };
15
16 static const struct video_format_cap fmts[] = {
17 [RGB565] = {.pixelformat = VIDEO_PIX_FMT_RGB565,
18 .width_min = 1280, .width_max = 1280, .width_step = 50,
19 .height_min = 720, .height_max = 720, .height_step = 50},
20 [YUYV_A] = {.pixelformat = VIDEO_PIX_FMT_YUYV,
21 .width_min = 100, .width_max = 1000, .width_step = 50,
22 .height_min = 100, .height_max = 1000, .height_step = 50},
23 [YUYV_B] = {.pixelformat = VIDEO_PIX_FMT_YUYV,
24 .width_min = 1920, .width_max = 1920, .width_step = 0,
25 .height_min = 1080, .height_max = 1080, .height_step = 0},
26 {0},
27 };
28
ZTEST(video_common,test_video_format_caps_index)29 ZTEST(video_common, test_video_format_caps_index)
30 {
31 struct video_format fmt = {0};
32 size_t idx;
33 int ret;
34
35 fmt.pixelformat = VIDEO_PIX_FMT_YUYV;
36
37 fmt.width = 100;
38 fmt.height = 100;
39 ret = video_format_caps_index(fmts, &fmt, &idx);
40 zassert_ok(ret, "expecting minimum value to match");
41 zassert_equal(idx, YUYV_A);
42
43 fmt.width = 1000;
44 fmt.height = 1000;
45 ret = video_format_caps_index(fmts, &fmt, &idx);
46 zassert_ok(ret, "expecting maximum value to match");
47 zassert_equal(idx, YUYV_A);
48
49 fmt.width = 1920;
50 fmt.height = 1080;
51 ret = video_format_caps_index(fmts, &fmt, &idx);
52 zassert_ok(ret, "expecting exact match to work");
53 zassert_equal(idx, YUYV_B);
54
55 fmt.width = 1001;
56 fmt.height = 1000;
57 ret = video_format_caps_index(fmts, &fmt, &idx);
58 zassert_not_ok(ret, "expecting 1 above maximum width to mismatch");
59
60 fmt.width = 1000;
61 fmt.height = 1001;
62 ret = video_format_caps_index(fmts, &fmt, &idx);
63 zassert_not_ok(ret, "expecting 1 above maximum height to mismatch");
64
65 fmt.width = 1280;
66 fmt.height = 720;
67 ret = video_format_caps_index(fmts, &fmt, &idx);
68 zassert_not_ok(ret);
69 zassert_not_ok(ret, "expecting wrong format to mismatch");
70
71 fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
72
73 fmt.width = 1000;
74 fmt.height = 1000;
75 ret = video_format_caps_index(fmts, &fmt, &idx);
76 zassert_not_ok(ret, "expecting wrong format to mismatch");
77
78 fmt.width = 1280;
79 fmt.height = 720;
80 ret = video_format_caps_index(fmts, &fmt, &idx);
81 zassert_ok(ret, "expecting exact match to work");
82 zassert_equal(idx, RGB565);
83 }
84
ZTEST(video_common,test_video_frmival_nsec)85 ZTEST(video_common, test_video_frmival_nsec)
86 {
87 zassert_equal(
88 video_frmival_nsec(&(struct video_frmival){.numerator = 1, .denominator = 15}),
89 66666666);
90
91 zassert_equal(
92 video_frmival_nsec(&(struct video_frmival){.numerator = 1, .denominator = 30}),
93 33333333);
94
95 zassert_equal(
96 video_frmival_nsec(&(struct video_frmival){.numerator = 5, .denominator = 1}),
97 5000000000);
98
99 zassert_equal(
100 video_frmival_nsec(&(struct video_frmival){.numerator = 1, .denominator = 1750000}),
101 571);
102 }
103
ZTEST(video_common,test_video_closest_frmival_stepwise)104 ZTEST(video_common, test_video_closest_frmival_stepwise)
105 {
106 struct video_frmival_stepwise stepwise;
107 struct video_frmival desired;
108 struct video_frmival expected;
109 struct video_frmival match;
110
111 stepwise.min.numerator = 1;
112 stepwise.min.denominator = 30;
113 stepwise.max.numerator = 30;
114 stepwise.max.denominator = 30;
115 stepwise.step.numerator = 1;
116 stepwise.step.denominator = 30;
117
118 desired.numerator = 1;
119 desired.denominator = 1;
120 video_closest_frmival_stepwise(&stepwise, &desired, &match);
121 zassert_equal(video_frmival_nsec(&match), video_frmival_nsec(&desired), "1 / 1");
122
123 desired.numerator = 3;
124 desired.denominator = 30;
125 video_closest_frmival_stepwise(&stepwise, &desired, &match);
126 zassert_equal(video_frmival_nsec(&match), video_frmival_nsec(&desired), "3 / 30");
127
128 desired.numerator = 7;
129 desired.denominator = 80;
130 expected.numerator = 3;
131 expected.denominator = 30;
132 video_closest_frmival_stepwise(&stepwise, &desired, &match);
133 zassert_equal(video_frmival_nsec(&match), video_frmival_nsec(&expected), "7 / 80");
134
135 desired.numerator = 1;
136 desired.denominator = 120;
137 video_closest_frmival_stepwise(&stepwise, &desired, &match);
138 zassert_equal(video_frmival_nsec(&match), video_frmival_nsec(&stepwise.min), "1 / 120");
139
140 desired.numerator = 100;
141 desired.denominator = 1;
142 video_closest_frmival_stepwise(&stepwise, &desired, &match);
143 zassert_equal(video_frmival_nsec(&match), video_frmival_nsec(&stepwise.max), "100 / 1");
144 }
145
146 ZTEST_SUITE(video_common, NULL, NULL, NULL, NULL, NULL);
147