Skip to content

Commit 5120e27

Browse files
author
Alain Volmat
committed
samples: usb: uvc: select applicable resolutions from range
Select from commonly used resolution when the video device advertise capabilities using range. Signed-off-by: Alain Volmat <[email protected]>
1 parent 8d8877d commit 5120e27

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

samples/subsys/usb/uvc/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,16 @@
66
# tree, you cannot use them in your own application.
77
source "samples/subsys/usb/common/Kconfig.sample_usbd"
88

9+
menu "UVC specific configuration"
10+
11+
config APP_VIDEO_MAX_RESOLUTIONS
12+
int "Maximum number of advertised resolutions"
13+
default 5
14+
help
15+
Control the maximum number of resolution that will be advertised
16+
to the USB client in case of the video capture supports a range
17+
of resolutions.
18+
19+
endmenu
20+
921
source "Kconfig.zephyr"

samples/subsys/usb/uvc/src/main.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ static int app_add_format(uint32_t pixfmt, uint32_t width, uint32_t height, bool
8181
return ret;
8282
}
8383

84+
struct video_resolution {
85+
uint16_t width;
86+
uint16_t height;
87+
};
88+
89+
static struct video_resolution video_common_fmts[] = {
90+
{ .width = 160, .height = 120, }, /* QQVGA */
91+
{ .width = 320, .height = 240, }, /* QVGA */
92+
{ .width = 640, .height = 480, }, /* VGA */
93+
{ .width = 854, .height = 480, }, /* WVGA */
94+
{ .width = 800, .height = 600, }, /* SVGA */
95+
{ .width = 1280, .height = 720, }, /* HD */
96+
{ .width = 1280, .height = 1024, }, /* SXGA */
97+
{ .width = 1920, .height = 1080, }, /* FHD */
98+
{ .width = 3840, .height = 2160, }, /* UHD */
99+
};
100+
84101
/* Submit to UVC only the formats expected to be working (enough memory for the size, etc.) */
85102
static int app_add_filtered_formats(void)
86103
{
@@ -89,6 +106,7 @@ static int app_add_filtered_formats(void)
89106

90107
for (int i = 0; video_caps.format_caps[i].pixelformat != 0; i++) {
91108
const struct video_format_cap *vcap = &video_caps.format_caps[i];
109+
int count = 1;
92110

93111
ret = app_add_format(vcap->pixelformat, vcap->width_min, vcap->height_min,
94112
has_sup_fmts);
@@ -102,6 +120,39 @@ static int app_add_filtered_formats(void)
102120
if (ret != 0) {
103121
return ret;
104122
}
123+
124+
count++;
125+
}
126+
127+
if (vcap->width_step == 0 && vcap->height_step == 0) {
128+
continue;
129+
}
130+
131+
/* RANGE Resolution processing */
132+
for (int j = 0; j < ARRAY_SIZE(video_common_fmts); j++) {
133+
if (count >= CONFIG_APP_VIDEO_MAX_RESOLUTIONS) {
134+
break;
135+
}
136+
137+
if (!IN_RANGE(video_common_fmts[j].width,
138+
vcap->width_min, vcap->width_max) ||
139+
!IN_RANGE(video_common_fmts[j].height,
140+
vcap->height_min, vcap->height_max)) {
141+
continue;
142+
}
143+
144+
if ((video_common_fmts[j].width - vcap->width_min) % vcap->width_step ||
145+
(video_common_fmts[j].height - vcap->height_min) % vcap->height_step) {
146+
continue;
147+
}
148+
149+
ret = app_add_format(vcap->pixelformat, video_common_fmts[j].width,
150+
video_common_fmts[j].height, has_sup_fmts);
151+
if (ret != 0) {
152+
return ret;
153+
}
154+
155+
count++;
105156
}
106157
}
107158

0 commit comments

Comments
 (0)