Skip to content

Commit c7987b2

Browse files
author
Alain Volmat
committed
samples: video: capture: 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 <alain.volmat@foss.st.com>
1 parent 4620ba6 commit c7987b2

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

samples/drivers/video/capture/Kconfig

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

66
menu "Video capture configuration"
77

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

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ int main(void)
9595
struct video_frmival frmival;
9696
struct video_frmival_enum fie;
9797
enum video_buf_type type = VIDEO_BUF_TYPE_OUTPUT;
98+
struct video_selection sel = {
99+
.type = VIDEO_BUF_TYPE_OUTPUT,
100+
};
98101
unsigned int frame = 0;
102+
uint32_t frame_width, frame_height;
99103
size_t bsize;
100104
int i = 0;
101105
int err;
@@ -139,6 +143,26 @@ int main(void)
139143
return 0;
140144
}
141145

146+
/* Set the crop setting if necessary */
147+
#if CONFIG_VIDEO_SOURCE_CROP_WIDTH && CONFIG_VIDEO_SOURCE_CROP_HEIGHT
148+
sel.target = VIDEO_SEL_TGT_CROP;
149+
sel.rect.left = CONFIG_VIDEO_SOURCE_CROP_LEFT;
150+
sel.rect.top = CONFIG_VIDEO_SOURCE_CROP_TOP;
151+
sel.rect.width = CONFIG_VIDEO_SOURCE_CROP_WIDTH;
152+
sel.rect.height = CONFIG_VIDEO_SOURCE_CROP_HEIGHT;
153+
if (video_set_selection(video_dev, &sel)) {
154+
LOG_ERR("Unable to set selection crop");
155+
return 0;
156+
}
157+
LOG_INF("Selection crop set to (%u,%u)/%ux%u",
158+
sel.rect.left, sel.rect.top,
159+
sel.rect.width, sel.rect.height);
160+
#endif
161+
162+
frame_height = fmt.height;
163+
frame_width = fmt.width;
164+
165+
#if CONFIG_VIDEO_FRAME_HEIGHT || CONFIG_VIDEO_FRAME_WIDTH
142166
#if CONFIG_VIDEO_FRAME_HEIGHT
143167
fmt.height = CONFIG_VIDEO_FRAME_HEIGHT;
144168
#endif
@@ -147,6 +171,31 @@ int main(void)
147171
fmt.width = CONFIG_VIDEO_FRAME_WIDTH;
148172
#endif
149173

174+
/*
175+
* Check (if possible) if targeted size if same as crop
176+
* and if compose is necessary
177+
*/
178+
sel.target = VIDEO_SEL_TGT_CROP;
179+
err = video_get_selection(video_dev, &sel);
180+
if (err == 0) {
181+
if (sel.rect.width != fmt.width || sel.rect.height != fmt.height) {
182+
sel.target = VIDEO_SEL_TGT_COMPOSE;
183+
sel.rect.left = 0;
184+
sel.rect.top = 0;
185+
sel.rect.width = fmt.width;
186+
sel.rect.height = fmt.height;
187+
err = video_set_selection(video_dev, &sel);
188+
if (err != -ENOSYS && err < 0) {
189+
LOG_ERR("Unable to set selection compose");
190+
return 0;
191+
}
192+
}
193+
} else if (err != -ENOSYS && err < 0) {
194+
LOG_ERR("Unable to get selection crop");
195+
return 0;
196+
}
197+
#endif
198+
150199
if (strcmp(CONFIG_VIDEO_PIXEL_FORMAT, "")) {
151200
fmt.pixelformat = VIDEO_FOURCC_FROM_STR(CONFIG_VIDEO_PIXEL_FORMAT);
152201
}

0 commit comments

Comments
 (0)