48 #include <sys/types.h>
50 #include <sys/ioctl.h>
69 m_sDeviceName += pDeviceName;
71 m_nChannel = nChannel;
72 m_videoMode = videoMode;
89 video_capability capability;
90 video_channel queryChannel;
91 video_channel selectedChannel;
92 video_window captureWindow;
93 video_picture imageProperties;
97 selectedChannel.channel = m_nChannel;
99 if ((m_nDeviceHandle = open(m_sDeviceName.c_str(), O_RDWR)) == -1)
101 printf (
"Could not open device %s - %s\n", m_sDeviceName.c_str(), strerror(errno));
106 if (ioctl(m_nDeviceHandle, VIDIOCGCAP, &capability) == -1)
108 printf (
"could not obtain device capabilities\n");
112 if ((capability.type & VID_TYPE_CAPTURE) == 0)
114 printf (
"this device cannot capture video to memory\n");
120 while (i < capability.channels)
122 queryChannel.channel = i;
123 if (ioctl (m_nDeviceHandle, VIDIOCGCHAN, &queryChannel) != -1)
125 printf (
"%d. %s\n", queryChannel.channel, queryChannel.name);
131 selectedChannel.norm = VIDEO_MODE_NTSC;
132 if (ioctl (m_nDeviceHandle, VIDIOCSCHAN, &selectedChannel) == -1)
135 printf (
"Could not set channel #%d\nNot a fatal error.", selectedChannel.channel);
141 case e320x240: width = 320; height = 240;
break;
142 case e640x480: width = 640; height = 480;
break;
143 case e800x600: width = 800; height = 600;
break;
144 case e768x576: width = 768; height = 576;
break;
145 case e1024x768: width = 1024; height = 768;
break;
146 case e1280x960: width = 1280; height = 960;
break;
147 default:
return false;
151 if ((capability.type & VID_TYPE_SCALES) != 0)
155 captureWindow.width = width;
156 captureWindow.height = height;
157 captureWindow.chromakey = 0;
158 captureWindow.flags = 0;
159 captureWindow.clips = 0;
160 captureWindow.clipcount = 0;
162 if (ioctl (m_nDeviceHandle, VIDIOCSWIN, &captureWindow) == -1)
165 printf (
"Could not set desired dimensions\nNot a fatal error.\n");
170 if (ioctl(m_nDeviceHandle, VIDIOCGWIN, &captureWindow) == -1)
172 printf (
"Could not obtain capture window dimensions.\n");
174 width = captureWindow.width;
175 height = captureWindow.height;
176 printf (
"Capturing dimensions are : %d, %d\n", width, height);
181 if (ioctl (m_nDeviceHandle, VIDIOCGPICT, &imageProperties) != -1)
185 imageProperties.depth = 24;
186 imageProperties.palette = VIDEO_PALETTE_RGB24;
187 if (ioctl (m_nDeviceHandle, VIDIOCSPICT, &imageProperties) == -1)
189 printf (
"Could not set the video depth and palette.\nPerhaps not a fatal error.\n");
194 if (ioctl (m_nDeviceHandle, VIDIOCGPICT, &imageProperties) == -1)
196 printf (
"Failed to retrieve the video depth and palette.\n");
200 if ((imageProperties.depth != 24) || (imageProperties.palette != VIDEO_PALETTE_RGB24))
202 printf (
"Format is not 24bit RGB.\n");
206 printf (
"Capture depth is 24bit RGB\n");
209 if (ioctl (m_nDeviceHandle, VIDIOCGMBUF, &memoryBuffer) == -1)
211 printf (
"Failed to retrieve information about MMIO space.\n");
217 memoryMap = (
char*)mmap (0, memoryBuffer.size, PROT_READ | PROT_WRITE, MAP_SHARED, m_nDeviceHandle, 0);
218 if ((intptr_t) memoryMap == -1)
220 printf (
"Failed to obtain MMIO space.\n");
225 printf(
"Allocating structures...\n");
227 mmaps = (
struct video_mmap*)(malloc (memoryBuffer.frames * sizeof (
struct video_mmap)));
231 while (i < memoryBuffer.frames)
234 mmaps[i].width = captureWindow.width;
235 mmaps[i].height = captureWindow.height;
236 mmaps[i].format = imageProperties.palette;
240 printf(
"Requesting capture buffers...\n");
243 while (i < (memoryBuffer.frames-1))
245 if (ioctl (m_nDeviceHandle, VIDIOCMCAPTURE, &mmaps[i]) == -1)
251 printf(
"Set index buffer...\n");
253 bufferIndex = memoryBuffer.frames-1;
261 if (ioctl (m_nDeviceHandle, VIDIOCMCAPTURE, &mmaps[bufferIndex]) == -1)
267 if (++bufferIndex == memoryBuffer.frames)
275 if (ioctl(m_nDeviceHandle, VIDIOCSYNC, &mmaps[bufferIndex]) == -1)
280 const unsigned char *frame = (
const unsigned char *) (memoryMap + memoryBuffer.offsets[bufferIndex]);
281 unsigned char *output = ppImages[0]->
pixels;
283 const int nBytes = ppImages[0]->
width * ppImages[0]->
height * 3;
285 for (
int i = 0; i < nBytes; i += 3)
287 output[i] = frame[i + 2];
288 output[i + 1] = frame[i + 1];
289 output[i + 2] = frame[i];
301 munmap (memoryMap, memoryBuffer.size);
304 close (m_nDeviceHandle);
bool CaptureImage(CByteImage **ppImages)
CV4LCapture(const char *pDeviceName="/dev/video0", int nChannel=0, CVideoCaptureInterface::VideoMode videoMode=CVideoCaptureInterface::e640x480)
int width
The width of the image in pixels.
int height
The height of the image in pixels.
unsigned char * pixels
The pointer to the the pixels.
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...