#include "precomp.hpp"
-#if !defined WIN32 && ((defined HAVE_CAMV4L && defined HAVE_CAMV4L2) || defined HAVE_VIDEOIO)
+#if !defined WIN32 && (defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO)
#define CLEAR(x) memset (&(x), 0, sizeof (x))
#include <sys/types.h>
#include <sys/mman.h>
+#ifdef HAVE_CAMVAL
#include <linux/videodev.h>
+#endif
#include <string.h>
#include <stdlib.h>
-#include <asm/types.h> /* for videodev2.h */
#include <assert.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#ifdef HAVE_CAMV4L2
+#include <asm/types.h> /* for videodev2.h */
#include <linux/videodev2.h>
#endif
int deviceHandle;
int bufferIndex;
int FirstCapture;
+#ifdef HAVE_CAMV4L
struct video_capability capability;
struct video_window captureWindow;
struct video_picture imageProperties;
struct video_mbuf memoryBuffer;
struct video_mmap *mmaps;
+#endif /* HAVE_CAMV4L */
char *memoryMap;
IplImage frame;
static int numCameras = 0;
static int indexList = 0;
-#ifdef HAVE_CAMV4L2
-
-// IOCTL handling for V4L2
static int xioctl( int fd, int request, void *arg)
{
}
-#endif /* HAVE_CAMV4L2 */
-
/* Simple test program: Find number of Video Sources available.
Start from 0 and go to MAX_CAMERAS while checking for the device with that name.
If it fails on the first attempt of /dev/video0, then check if /dev/video is valid.
}; /* End icvInitCapture_V4L */
+#ifdef HAVE_CAMV4L
+
static int
try_palette(int fd,
struct video_picture *cam_pic,
return 0;
}
+#endif /* HAVE_CAMV4L */
+
#ifdef HAVE_CAMV4L2
static int try_palette_v4l2(CvCaptureCAM_V4L* capture, unsigned long colorspace)
#endif /* HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
+
static int try_init_v4l(CvCaptureCAM_V4L* capture, char *deviceName)
{
/* No matter what the name - it still must be opened! */
capture->deviceHandle = open(deviceName, O_RDWR);
-
if (capture->deviceHandle == 0)
{
detect = -1;
if (ioctl(capture->deviceHandle, VIDIOCGCAP, &capture->capability) < 0)
{
detect = 0;
-
icvCloseCAM_V4L(capture);
}
else
}
+#endif /* HAVE_CAMV4L */
+
#ifdef HAVE_CAMV4L2
static int try_init_v4l2(CvCaptureCAM_V4L* capture, char *deviceName)
{
-
- // if detect = -1 then unable to open device
- // if detect = 0 then detected nothing
- // if detect = 1 then V4L2 device
- int detect = 0;
-
-
// Test device for V4L2 compability
+ // Return value:
+ // -1 then unable to open device
+ // 0 then detected nothing
+ // 1 then V4L2 device
+
+ int deviceIndex;
/* Open and test V4L2 device */
capture->deviceHandle = open (deviceName, O_RDWR /* required */ | O_NONBLOCK, 0);
-
-
-
- if (capture->deviceHandle == 0)
+ if (-1 == capture->deviceHandle)
{
- detect = -1;
-
+#ifndef NDEBUG
+ fprintf(stderr, "(DEBUG) try_init_v4l2 open \"%s\": %s\n", deviceName, strerror(errno));
+#endif
icvCloseCAM_V4L(capture);
+ return -1;
}
- if (detect == 0)
+ CLEAR (capture->cap);
+ if (-1 == xioctl (capture->deviceHandle, VIDIOC_QUERYCAP, &capture->cap))
{
- CLEAR (capture->cap);
- if (-1 == xioctl (capture->deviceHandle, VIDIOC_QUERYCAP, &capture->cap))
- {
- detect = 0;
+#ifndef NDEBUG
+ fprintf(stderr, "(DEBUG) try_init_v4l2 VIDIOC_QUERYCAP \"%s\": %s\n", deviceName, strerror(errno));
+#endif
+ icvCloseCAM_V4L(capture);
+ return 0;
+ }
- icvCloseCAM_V4L(capture);
- }
- else
- {
- CLEAR (capture->capability);
- capture->capability.type = capture->cap.capabilities;
+ /* Query channels number */
+ if (-1 == xioctl (capture->deviceHandle, VIDIOC_G_INPUT, &deviceIndex))
+ {
+#ifndef NDEBUG
+ fprintf(stderr, "(DEBUG) try_init_v4l2 VIDIOC_G_INPUT \"%s\": %s\n", deviceName, strerror(errno));
+#endif
+ icvCloseCAM_V4L(capture);
+ return 0;
+ }
- /* Query channels number */
- if (-1 != xioctl (capture->deviceHandle, VIDIOC_G_INPUT, &capture->capability.channels))
- {
- detect = 1;
- }
- }
+ /* Query information about current input */
+ CLEAR (capture->inp);
+ capture->inp.index = deviceIndex;
+ if (-1 == xioctl (capture->deviceHandle, VIDIOC_ENUMINPUT, &capture->inp))
+ {
+#ifndef NDEBUG
+ fprintf(stderr, "(DEBUG) try_init_v4l2 VIDIOC_ENUMINPUT \"%s\": %s\n", deviceName, strerror(errno));
+#endif
+ icvCloseCAM_V4L(capture);
+ return 0;
}
- return detect;
+ return 1;
}
else
#ifdef HAVE_JPEG
-#ifdef __USE_GNU
- /* support for MJPEG is only available with libjpeg and gcc,
- because it's use libjepg and fmemopen()
- */
if (try_palette_v4l2(capture, V4L2_PIX_FMT_MJPEG) == 0 ||
try_palette_v4l2(capture, V4L2_PIX_FMT_JPEG) == 0)
{
}
else
#endif
-#endif
if (try_palette_v4l2(capture, V4L2_PIX_FMT_YUYV) == 0)
{
#endif /* HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
+
static int autosetup_capture_mode_v4l(CvCaptureCAM_V4L* capture)
{
}
+#endif /* HAVE_CAMV4L */
+
#ifdef HAVE_CAMV4L2
static void v4l2_scan_controls_enumerate_menu(CvCaptureCAM_V4L* capture)
/* Set up Image data */
cvInitImageHeader( &capture->frame,
- cvSize( capture->captureWindow.width,
- capture->captureWindow.height ),
+ cvSize( capture->form.fmt.pix.width,
+ capture->form.fmt.pix.height ),
IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4 );
/* Allocate space for RGBA data */
capture->frame.imageData = (char *)cvAlloc(capture->frame.imageSize);
#endif /* HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
+
static int _capture_V4L (CvCaptureCAM_V4L *capture, char *deviceName)
{
int detect_v4l = 0;
return 1;
}; /* End _capture_V4L */
+#endif /* HAVE_CAMV4L */
+
static CvCaptureCAM_V4L * icvCaptureFromCAM_V4L (int index)
{
static int autoindex;
icvCloseCAM_V4L(capture);
V4L2_SUPPORT = 0;
#endif /* HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
if (_capture_V4L (capture, deviceName) == -1) {
icvCloseCAM_V4L(capture);
return NULL;
}
+#endif /* HAVE_CAMV4L */
#ifdef HAVE_CAMV4L2
} else {
V4L2_SUPPORT = 1;
#ifdef HAVE_CAMV4L2
+#ifdef HAVE_CAMV4L
if (V4L2_SUPPORT == 1)
+#endif
{
for (capture->bufferIndex = 0;
perror ("VIDIOC_STREAMON");
return 0;
}
- } else
+ }
#endif /* HAVE_CAMV4L2 */
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
+ else
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
{
for (capture->bufferIndex = 0;
}
}
+#endif /* HAVE_CAMV4L */
#if defined(V4L_ABORT_BADJPEG) && defined(HAVE_CAMV4L2)
if (V4L2_SUPPORT == 1)
mainloop_v4l2(capture);
- } else
+ }
#endif /* HAVE_CAMV4L2 */
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
+ else
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
{
capture->mmaps[capture->bufferIndex].frame = capture->bufferIndex;
}
}
+#endif /* HAVE_CAMV4L */
return(1);
}
#ifdef HAVE_CAMV4L2
if (V4L2_SUPPORT == 0)
#endif /* HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
{
/* [FD] this really belongs here */
}
}
+#endif /* HAVE_CAMV4L */
/* Now get what has already been captured as a IplImage return */
capture->frame.imageData = (char *)cvAlloc(capture->frame.imageSize);
}
- } else
+ }
#endif /* HAVE_CAMV4L2 */
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
+ else
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
{
if((capture->frame.width != capture->mmaps[capture->bufferIndex].width)
}
}
+#endif /* HAVE_CAMV4L */
#ifdef HAVE_CAMV4L2
(unsigned char*)capture->frame.imageData);
break;
#ifdef HAVE_JPEG
-#ifdef __USE_GNU
- /* support for MJPEG is only available with libjpeg and gcc,
- because it's use libjepg and fmemopen()
- */
case PALETTE_MJPEG:
if (!mjpeg_to_rgb24(capture->form.fmt.pix.width,
capture->form.fmt.pix.height,
return 0;
break;
#endif
-#endif
case PALETTE_YUYV:
yuyv_to_rgb24(capture->form.fmt.pix.width,
(unsigned char*)capture->frame.imageData);
break;
}
- } else
+ }
#endif /* HAVE_CAMV4L2 */
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
+ else
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
{
switch(capture->imageProperties.palette) {
}
}
+#endif /* HAVE_CAMV4L */
return(&capture->frame);
}
#ifdef HAVE_CAMV4L2
+#ifdef HAVE_CAMV4L
if (V4L2_SUPPORT == 1)
+#endif
{
/* default value for min and max */
/* all was OK, so convert to 0.0 - 1.0 range, and return the value */
return ((float)capture->control.value - v4l2_min + 1) / (v4l2_max - v4l2_min);
- } else
+ }
#endif /* HAVE_CAMV4L2 */
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
+ else
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
{
int retval = -1;
return float (retval) / 0xFFFF;
}
+#endif /* HAVE_CAMV4L */
};
return 0;
- } else
+ }
#endif /* HAVE_CAMV4L2 */
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
+ else
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
{
if (capture==0) return 0;
capture->FirstCapture = 1;
}
+#endif /* HAVE_CAMV4L */
return 0;
perror ("VIDIOC_S_CTRL");
return -1;
}
- } else
+ }
#endif /* HAVE_CAMV4L2 */
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
+ else
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
{
int v4l_value;
return -1;
}
}
+#endif /* HAVE_CAMV4L */
/* all was OK */
return 0;
#ifdef HAVE_CAMV4L2
if (V4L2_SUPPORT == 0)
#endif /* HAVE_CAMV4L2 */
+#ifdef HAVE_CAMV4L
{
if (capture->mmaps)
munmap(capture->memoryMap, capture->memoryBuffer.size);
}
+#endif /* HAVE_CAMV4L */
+#if defined(HAVE_CAMV4L) && defined(HAVE_CAMV4L2)
+ else
+#endif /* HAVE_CAMV4L && HAVE_CAMV4L2 */
#ifdef HAVE_CAMV4L2
- else {
+ {
capture->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- if (ioctl(capture->deviceHandle, VIDIOC_STREAMOFF, &capture->type) < 0) {
+ if (-1 == ioctl(capture->deviceHandle, VIDIOC_STREAMOFF, &capture->type)) {
perror ("Unable to stop the stream.");
}