15 #include "webcamVideoV4L.h" 17 #ifdef HAVE_VIDEO4LINUX 19 #include "webcamVideoCursorV4L.h" 23 #include <sys/ioctl.h> 24 #include <linux/videodev2.h> 27 #ifndef VIDIOC_ENUM_FRAMESIZES 28 enum v4l2_frmsizetypes {
29 V4L2_FRMSIZE_TYPE_DISCRETE = 1,
30 V4L2_FRMSIZE_TYPE_CONTINUOUS = 2,
31 V4L2_FRMSIZE_TYPE_STEPWISE = 3,
34 struct v4l2_frmsize_discrete {
39 struct v4l2_frmsize_stepwise {
48 struct v4l2_frmsizeenum {
53 struct v4l2_frmsize_discrete discrete;
54 struct v4l2_frmsize_stepwise stepwise;
59 #define VIDIOC_ENUM_FRAMESIZES _IOWR('V', 74, struct v4l2_frmsizeenum) 62 #ifndef VIDIOC_ENUM_FRAMEINTERVALS 63 enum v4l2_frmivaltypes {
64 V4L2_FRMIVAL_TYPE_DISCRETE = 1,
65 V4L2_FRMIVAL_TYPE_CONTINUOUS = 2,
66 V4L2_FRMIVAL_TYPE_STEPWISE = 3,
69 struct v4l2_frmival_stepwise {
70 struct v4l2_fract min;
71 struct v4l2_fract max;
72 struct v4l2_fract step;
75 struct v4l2_frmivalenum {
82 struct v4l2_fract discrete;
83 struct v4l2_frmival_stepwise stepwise;
88 #define VIDIOC_ENUM_FRAMEINTERVALS _IOWR('V', 75, struct v4l2_frmivalenum) 100 add_options_for_size(
int fd,
const string &dev,
const char *name,
unsigned width,
unsigned height,
unsigned pixelformat) {
101 struct v4l2_frmivalenum frmivalenum;
102 for (
int k = 0;; k++) {
103 memset(&frmivalenum, 0,
sizeof frmivalenum);
104 frmivalenum.index = k;
105 frmivalenum.pixel_format = pixelformat;
106 frmivalenum.width = width;
107 frmivalenum.height = height;
108 if (ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmivalenum) == -1) {
112 switch (frmivalenum.type) {
113 case V4L2_FRMIVAL_TYPE_DISCRETE:
114 fps = ((double) frmivalenum.discrete.denominator) / ((double) frmivalenum.discrete.numerator);
117 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
118 case V4L2_FRMIVAL_TYPE_STEPWISE:
121 double max_fps = ((double) frmivalenum.stepwise.max.denominator) / ((double) frmivalenum.stepwise.max.numerator);
131 PT(WebcamVideoV4L) wc =
new WebcamVideoV4L;
135 wc->_size_y = height;
137 wc->_pformat = pixelformat;
138 wc->_pixel_format = string((
char*) &pixelformat, 4);
140 WebcamVideoV4L::_all_webcams.push_back(DCAST(
WebcamVideo, wc));
150 void find_all_webcams_v4l() {
151 struct v4l2_capability cap2;
155 pattern.match_files(devs);
156 for (vector_string::iterator it = devs.begin(); it != devs.end(); ++it) {
157 int fd = open(it->c_str(), O_RDWR);
160 if (ioctl(fd, VIDIOC_QUERYCAP, &cap2) != -1) {
161 if ((cap2.capabilities & V4L2_CAP_VIDEO_CAPTURE) &&
162 (cap2.capabilities & V4L2_CAP_STREAMING)) {
163 struct v4l2_fmtdesc fmt;
164 for (
int i = 0;; i++) {
165 memset(&fmt, 0,
sizeof fmt);
167 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
168 if (ioctl(fd, VIDIOC_ENUM_FMT, &fmt) == -1) {
173 switch (fmt.pixelformat) {
175 case V4L2_PIX_FMT_MJPEG:
177 case V4L2_PIX_FMT_YUYV:
178 case V4L2_PIX_FMT_BGR24:
179 case V4L2_PIX_FMT_BGR32:
180 case V4L2_PIX_FMT_RGB24:
181 case V4L2_PIX_FMT_RGB32:
188 struct v4l2_frmsizeenum frmsizeenum;
189 for (
int j = 0;; j++) {
190 memset(&frmsizeenum, 0,
sizeof frmsizeenum);
191 frmsizeenum.index = j;
192 frmsizeenum.pixel_format = fmt.pixelformat;
193 if (ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsizeenum) == -1) {
197 switch (frmsizeenum.type) {
198 case V4L2_FRMSIZE_TYPE_DISCRETE:
201 add_options_for_size(fd, *it, (
const char *)cap2.card,
202 frmsizeenum.discrete.width,
203 frmsizeenum.discrete.height,
207 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
213 for (; width <= frmsizeenum.stepwise.max_width; width *= 2) {
215 for (; height <= frmsizeenum.stepwise.max_height; height *= 2) {
217 add_options_for_size(fd, *it, (
const char *)cap2.card, width, height, fmt.pixelformat);
223 case V4L2_FRMSIZE_TYPE_STEPWISE:
226 for (; width <= frmsizeenum.stepwise.max_width; width *= 2) {
228 for (; height <= frmsizeenum.stepwise.max_height; height *= 2) {
229 if ((width - frmsizeenum.stepwise.min_width) % frmsizeenum.stepwise.step_width == 0 &&
230 (height - frmsizeenum.stepwise.min_height) % frmsizeenum.stepwise.step_height == 0) {
232 add_options_for_size(fd, *it, (
const char *)cap2.card, width, height, fmt.pixelformat);
256 return new WebcamVideoCursorV4L(
this);
Allows you to open a webcam or other video capture device as a video stream.
A MovieVideo is actually any source that provides a sequence of video frames.
static int up_to_power_2(int value)
Returns the smallest power of 2 greater than or equal to value.
TypeHandle is the identifier used to differentiate C++ class types.
This class can be used to test for string matches against standard Unix-shell filename globbing conve...