Skip to content

Commit b437b43

Browse files
author
Alain Volmat
committed
samples: video: capture_to_lvgl: add crop/compose support
Demonstrate the crop/compose API by introducing 4 new CONFIG options in order to define the crop area. Moreover, if the selection API is available and if the targetted size is different from the current crop size, then try to apply a compose in order to reach the targetted format size. Signed-off-by: Alain Volmat <[email protected]>
1 parent 3e7df3a commit b437b43

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

samples/drivers/video/capture_to_lvgl/Kconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ mainmenu "Video capture to LVGL sample application"
77

88
menu "Video capture configuration"
99

10+
config VIDEO_SOURCE_CROP_LEFT
11+
int "Crop area left value"
12+
default 0
13+
help
14+
Left value of the crop area within the video source.
15+
16+
config VIDEO_SOURCE_CROP_TOP
17+
int "Crop area top value"
18+
default 0
19+
help
20+
Top value of the crop area within the video source.
21+
22+
config VIDEO_SOURCE_CROP_WIDTH
23+
int "Crop area width value"
24+
default 0
25+
help
26+
Width value of the crop area within the video source.
27+
If set to 0, the crop is not applied.
28+
29+
config VIDEO_SOURCE_CROP_HEIGHT
30+
int "Crop area height value"
31+
default 0
32+
help
33+
Height value of the crop area within the video source.
34+
If set to 0, the crop is not applied.
35+
1036
config VIDEO_WIDTH
1137
int "Define the width of the video"
1238
default 320

samples/drivers/video/capture_to_lvgl/src/main.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ int main(void)
2323
struct video_format fmt;
2424
struct video_caps caps;
2525
enum video_buf_type type = VIDEO_BUF_TYPE_OUTPUT;
26+
struct video_selection sel = {
27+
.type = VIDEO_BUF_TYPE_OUTPUT,
28+
};
2629
size_t bsize;
2730
int i = 0;
31+
int err;
2832

2933
display_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
3034
if (!device_is_ready(display_dev)) {
@@ -66,11 +70,50 @@ int main(void)
6670
return 0;
6771
}
6872

73+
/* Set the crop setting if necessary */
74+
#if CONFIG_VIDEO_SOURCE_CROP_WIDTH && CONFIG_VIDEO_SOURCE_CROP_HEIGHT
75+
sel.target = VIDEO_SEL_TGT_CROP;
76+
sel.rect.left = CONFIG_VIDEO_SOURCE_CROP_LEFT;
77+
sel.rect.top = CONFIG_VIDEO_SOURCE_CROP_TOP;
78+
sel.rect.width = CONFIG_VIDEO_SOURCE_CROP_WIDTH;
79+
sel.rect.height = CONFIG_VIDEO_SOURCE_CROP_HEIGHT;
80+
if (video_set_selection(video_dev, &sel)) {
81+
LOG_ERR("Unable to set selection crop");
82+
return 0;
83+
}
84+
LOG_INF("Selection crop set to (%u,%u)/%ux%u",
85+
sel.rect.left, sel.rect.top, sel.rect.width, sel.rect.height);
86+
#endif
87+
6988
/* Set format */
7089
fmt.width = CONFIG_VIDEO_WIDTH;
7190
fmt.height = CONFIG_VIDEO_HEIGHT;
7291
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
7392

93+
/*
94+
* Check (if possible) if targeted size is same as crop
95+
* and if compose is necessary
96+
*/
97+
sel.target = VIDEO_SEL_TGT_CROP;
98+
err = video_get_selection(video_dev, &sel);
99+
if (err < 0 && err != -ENOSYS) {
100+
LOG_ERR("Unable to get selection crop");
101+
return 0;
102+
}
103+
104+
if (err == 0 && (sel.rect.width != fmt.width || sel.rect.height != fmt.height)) {
105+
sel.target = VIDEO_SEL_TGT_COMPOSE;
106+
sel.rect.left = 0;
107+
sel.rect.top = 0;
108+
sel.rect.width = fmt.width;
109+
sel.rect.height = fmt.height;
110+
err = video_set_selection(video_dev, &sel);
111+
if (err < 0 && err != -ENOSYS) {
112+
LOG_ERR("Unable to set selection compose");
113+
return 0;
114+
}
115+
}
116+
74117
if (video_set_format(video_dev, &fmt)) {
75118
LOG_ERR("Unable to set up video format");
76119
return 0;
@@ -134,8 +177,6 @@ int main(void)
134177
/* Grab video frames */
135178
vbuf->type = type;
136179
while (1) {
137-
int err;
138-
139180
err = video_dequeue(video_dev, &vbuf, K_FOREVER);
140181
if (err) {
141182
LOG_ERR("Unable to dequeue video buf");

0 commit comments

Comments
 (0)