Skip to content

Commit 4b8d3db

Browse files
ngphibangaescolar
authored andcommitted
drivers: video: sw_generator: Fix set_format
Add code to handle invalid formats when setting format Signed-off-by: Phi Bang Nguyen <[email protected]>
1 parent d1275f7 commit 4b8d3db

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

drivers/video/video_sw_generator.c

+37-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
#include <zephyr/drivers/video.h>
99

10+
#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
11+
#include <zephyr/logging/log.h>
12+
LOG_MODULE_REGISTER(video_sw_generator);
13+
1014
#define VIDEO_PATTERN_COLOR_BAR 0
1115
#define VIDEO_PATTERN_FPS 30
1216

@@ -23,15 +27,48 @@ struct video_sw_generator_data {
2327
struct k_poll_signal *signal;
2428
};
2529

30+
static const struct video_format_cap fmts[] = {{
31+
.pixelformat = VIDEO_PIX_FMT_RGB565,
32+
.width_min = 64,
33+
.width_max = 1920,
34+
.height_min = 64,
35+
.height_max = 1080,
36+
.width_step = 1,
37+
.height_step = 1,
38+
}, {
39+
.pixelformat = VIDEO_PIX_FMT_XRGB32,
40+
.width_min = 64,
41+
.width_max = 1920,
42+
.height_min = 64,
43+
.height_max = 1080,
44+
.width_step = 1,
45+
.height_step = 1,
46+
},
47+
{0}};
48+
2649
static int video_sw_generator_set_fmt(const struct device *dev, enum video_endpoint_id ep,
2750
struct video_format *fmt)
2851
{
2952
struct video_sw_generator_data *data = dev->data;
53+
int i = 0;
3054

3155
if (ep != VIDEO_EP_OUT) {
3256
return -EINVAL;
3357
}
3458

59+
for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
60+
if (fmt->pixelformat == fmts[i].pixelformat && fmt->width >= fmts[i].width_min &&
61+
fmt->width <= fmts[i].width_max && fmt->height >= fmts[i].height_min &&
62+
fmt->height <= fmts[i].height_max) {
63+
break;
64+
}
65+
}
66+
67+
if (i == ARRAY_SIZE(fmts)) {
68+
LOG_ERR("Unsupported pixel format or resolution");
69+
return -ENOTSUP;
70+
}
71+
3572
data->fmt = *fmt;
3673

3774
return 0;
@@ -183,25 +220,6 @@ static int video_sw_generator_flush(const struct device *dev, enum video_endpoin
183220
return 0;
184221
}
185222

186-
static const struct video_format_cap fmts[] = {{
187-
.pixelformat = VIDEO_PIX_FMT_RGB565,
188-
.width_min = 64,
189-
.width_max = 1920,
190-
.height_min = 64,
191-
.height_max = 1080,
192-
.width_step = 1,
193-
.height_step = 1,
194-
}, {
195-
.pixelformat = VIDEO_PIX_FMT_XRGB32,
196-
.width_min = 64,
197-
.width_max = 1920,
198-
.height_min = 64,
199-
.height_max = 1080,
200-
.width_step = 1,
201-
.height_step = 1,
202-
},
203-
{0}};
204-
205223
static int video_sw_generator_get_caps(const struct device *dev, enum video_endpoint_id ep,
206224
struct video_caps *caps)
207225
{

0 commit comments

Comments
 (0)