Home | History | Annotate | Download | only in src

Lines Matching full:capture

29 These drivers should work with other V4L frame capture cards other then my bttv
30 driven frame capture card.
32 Re Written driver for standard V4L mode. Tested using LMLBT44 video capture card.
75 - cvRetrieveFrame should in turn wait for the end of frame capture, and should not
76 trigger the capture of the next frame (the user choses when to do it using GrabFrame)
98 mainloop_v4l2, read_image_v4l2 -> this methods are moved from official v4l2 capture.c example
101 autosetup_capture_mode_v4l -> autodetect capture modes for v4l
102 autosetup_capture_mode_v4l2 -> autodetect capture modes for v4l2
122 - Add capture control support (hue, saturation, brightness, contrast, gain)
123 - Get and change V4L capture controls (hue, saturation, brightness, contrast)
125 icvSetControl -> set capture controls
132 - Detect, get and change V4L2 capture controls (hue, saturation, brightness, contrast, gain)
134 v4l2_scan_controls_enumerate_menu, v4l2_scan_controls -> detect capture control intervals
160 Added v4l2 support for getting capture property CV_CAP_PROP_POS_MSEC.
261 /* Device Capture Objects */
351 static void icvCloseCAM_V4L( CvCaptureCAM_V4L* capture );
353 static int icvGrabFrameCAM_V4L( CvCaptureCAM_V4L* capture );
354 static IplImage* icvRetrieveFrameCAM_V4L( CvCaptureCAM_V4L* capture, int );
356 static double icvGetPropertyCAM_V4L( CvCaptureCAM_V4L* capture, int property_id );
357 static int icvSetPropertyCAM_V4L( CvCaptureCAM_V4L* capture, int property_id, double value );
359 static int icvSetVideoSize( CvCaptureCAM_V4L* capture, int w, int h);
419 static int try_palette_v4l2(CvCaptureCAM_V4L* capture, unsigned long colorspace)
421 CLEAR (capture->form);
423 capture->form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
424 capture->form.fmt.pix.pixelformat = colorspace;
425 capture->form.fmt.pix.field = V4L2_FIELD_ANY;
426 capture->form.fmt.pix.width = DEFAULT_V4L_WIDTH;
427 capture->form.fmt.pix.height = DEFAULT_V4L_HEIGHT;
429 if (-1 == ioctl (capture->deviceHandle, VIDIOC_S_FMT, &capture->form))
433 if (colorspace != capture->form.fmt.pix.pixelformat)
443 static int try_init_v4l(CvCaptureCAM_V4L* capture, char *deviceName)
456 capture->deviceHandle = open(deviceName, O_RDWR);
458 if (capture->deviceHandle == 0)
462 icvCloseCAM_V4L(capture);
468 if (ioctl(capture->deviceHandle, VIDIOCGCAP, &capture->capability) < 0)
471 icvCloseCAM_V4L(capture);
487 static int try_init_v4l2(CvCaptureCAM_V4L* capture, char *deviceName)
498 capture->deviceHandle = open (deviceName, O_RDWR /* required */ | O_NONBLOCK, 0);
499 if (-1 == capture->deviceHandle)
504 icvCloseCAM_V4L(capture);
508 CLEAR (capture->cap);
509 if (-1 == ioctl (capture->deviceHandle, VIDIOC_QUERYCAP, &capture->cap))
514 icvCloseCAM_V4L(capture);
519 if (-1 == ioctl (capture->deviceHandle, VIDIOC_G_INPUT, &deviceIndex))
524 icvCloseCAM_V4L(capture);
529 CLEAR (capture->inp);
530 capture->inp.index = deviceIndex;
531 if (-1 == ioctl (capture->deviceHandle, VIDIOC_ENUMINPUT, &capture->inp))
536 icvCloseCAM_V4L(capture);
544 static int autosetup_capture_mode_v4l2(CvCaptureCAM_V4L* capture)
546 if (try_palette_v4l2(capture, V4L2_PIX_FMT_BGR24) == 0)
548 capture->palette = PALETTE_BGR24;
551 if (try_palette_v4l2(capture, V4L2_PIX_FMT_YVU420) == 0)
553 capture->palette = PALETTE_YVU420;
556 if (try_palette_v4l2(capture, V4L2_PIX_FMT_YUV411P) == 0)
558 capture->palette = PALETTE_YUV411P;
563 if (try_palette_v4l2(capture, V4L2_PIX_FMT_MJPEG) == 0 ||
564 try_palette_v4l2(capture, V4L2_PIX_FMT_JPEG) == 0)
566 capture->palette = PALETTE_MJPEG;
571 if (try_palette_v4l2(capture, V4L2_PIX_FMT_YUYV) == 0)
573 capture->palette = PALETTE_YUYV;
575 else if (try_palette_v4l2(capture, V4L2_PIX_FMT_UYVY) == 0)
577 capture->palette = PALETTE_UYVY;
580 if (try_palette_v4l2(capture, V4L2_PIX_FMT_SN9C10X) == 0)
582 capture->palette = PALETTE_SN9C10X;
584 if (try_palette_v4l2(capture, V4L2_PIX_FMT_SBGGR8) == 0)
586 capture->palette = PALETTE_SBGGR8;
588 if (try_palette_v4l2(capture, V4L2_PIX_FMT_SGBRG) == 0)
590 capture->palette = PALETTE_SGBRG;
592 else if (try_palette_v4l2(capture, V4L2_PIX_FMT_RGB24) == 0)
594 capture->palette = PALETTE_RGB24;
599 icvCloseCAM_V4L(capture);
611 static int autosetup_capture_mode_v4l(CvCaptureCAM_V4L* capture)
614 if(ioctl(capture->deviceHandle, VIDIOCGPICT, &capture->imageProperties) < 0) {
616 icvCloseCAM_V4L(capture);
620 /* Yet MORE things that might have to be changes with your frame capture card */
622 if (try_palette(capture->deviceHandle, &capture->imageProperties, VIDEO_PALETTE_RGB24, 24)) {
625 else if (try_palette(capture->deviceHandle, &capture->imageProperties, VIDEO_PALETTE_YUV420P, 16)) {
628 else if (try_palette(capture->deviceHandle, &capture->imageProperties, VIDEO_PALETTE_YUV420, 16)) {
631 else if (try_palette(capture->deviceHandle, &capture->imageProperties, VIDEO_PALETTE_YUV411P, 16)) {
636 icvCloseCAM_V4L(capture);
649 static void v4l2_scan_controls(CvCaptureCAM_V4L* capture)
660 CLEAR (capture->queryctrl);
661 capture->queryctrl.id = ctrl_id;
663 if (0 == ioctl (capture->deviceHandle, VIDIOC_QUERYCTRL,
664 &capture->queryctrl))
667 if (capture->queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
670 if (capture->queryctrl.id == V4L2_CID_BRIGHTNESS)
672 capture->v4l2_brightness = 1;
673 capture->v4l2_brightness_min = capture->queryctrl.minimum;
674 capture->v4l2_brightness_max = capture->queryctrl.maximum;
677 if (capture->queryctrl.id == V4L2_CID_CONTRAST)
679 capture->v4l2_contrast = 1;
680 capture->v4l2_contrast_min = capture->queryctrl.minimum;
681 capture->v4l2_contrast_max = capture->queryctrl.maximum;
684 if (capture->queryctrl.id == V4L2_CID_SATURATION)
686 capture->v4l2_saturation = 1;
687 capture->v4l2_saturation_min = capture->queryctrl.minimum;
688 capture->v4l2_saturation_max = capture->queryctrl.maximum;
691 if (capture->queryctrl.id == V4L2_CID_HUE)
693 capture->v4l2_hue = 1;
694 capture->v4l2_hue_min = capture->queryctrl.minimum;
695 capture->v4l2_hue_max = capture->queryctrl.maximum;
698 if (capture->queryctrl.id == V4L2_CID_GAIN)
700 capture->v4l2_gain = 1;
701 capture->v4l2_gain_min = capture->queryctrl.minimum;
702 capture->v4l2_gain_max = capture->queryctrl.maximum;
705 if (capture->queryctrl.id == V4L2_CID_EXPOSURE)
707 capture->v4l2_exposure = 1;
708 capture->v4l2_exposure_min = capture->queryctrl.minimum;
709 capture->v4l2_exposure_max = capture->queryctrl.maximum;
728 CLEAR (capture->queryctrl);
729 capture->queryctrl.id = ctrl_id;
731 if (0 == ioctl (capture->deviceHandle, VIDIOC_QUERYCTRL,
732 &capture->queryctrl))
735 if (capture->queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
738 if (capture->queryctrl.id == V4L2_CID_BRIGHTNESS)
740 capture->v4l2_brightness = 1;
741 capture->v4l2_brightness_min = capture->queryctrl.minimum;
742 capture->v4l2_brightness_max = capture->queryctrl.maximum;
745 if (capture->queryctrl.id == V4L2_CID_CONTRAST)
747 capture->v4l2_contrast = 1;
748 capture->v4l2_contrast_min = capture->queryctrl.minimum;
749 capture->v4l2_contrast_max = capture->queryctrl.maximum;
752 if (capture->queryctrl.id == V4L2_CID_SATURATION)
754 capture->v4l2_saturation = 1;
755 capture->v4l2_saturation_min = capture->queryctrl.minimum;
756 capture->v4l2_saturation_max = capture->queryctrl.maximum;
759 if (capture->queryctrl.id == V4L2_CID_HUE)
761 capture->v4l2_hue = 1;
762 capture->v4l2_hue_min = capture->queryctrl.minimum;
763 capture->v4l2_hue_max = capture->queryctrl.maximum;
766 if (capture->queryctrl.id == V4L2_CID_GAIN)
768 capture->v4l2_gain = 1;
769 capture->v4l2_gain_min = capture->queryctrl.minimum;
770 capture->v4l2_gain_max = capture->queryctrl.maximum;
773 if (capture->queryctrl.id == V4L2_CID_EXPOSURE)
775 capture->v4l2_exposure = 1;
776 capture->v4l2_exposure_min = capture->queryctrl.minimum;
777 capture->v4l2_exposure_max = capture->queryctrl.maximum;
793 static int _capture_V4L2 (CvCaptureCAM_V4L *capture, char *deviceName)
797 detect_v4l2 = try_init_v4l2(capture, deviceName);
808 capture->v4l2_brightness = 0;
809 capture->v4l2_contrast = 0;
810 capture->v4l2_saturation = 0;
811 capture->v4l2_hue = 0;
812 capture->v4l2_gain = 0;
813 capture->v4l2_exposure = 0;
815 capture->v4l2_brightness_min = 0;
816 capture->v4l2_contrast_min = 0;
817 capture->v4l2_saturation_min = 0;
818 capture->v4l2_hue_min = 0;
819 capture->v4l2_gain_min = 0;
820 capture->v4l2_exposure_min = 0;
822 capture->v4l2_brightness_max = 0;
823 capture->v4l2_contrast_max = 0;
824 capture->v4l2_saturation_max = 0;
825 capture->v4l2_hue_max = 0;
826 capture->v4l2_gain_max = 0;
827 capture->v4l2_exposure_max = 0;
829 capture->timestamp.tv_sec = 0;
830 capture->timestamp.tv_usec = 0;
833 v4l2_scan_controls(capture);
835 if ((capture->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
837 fprintf( stderr, "VIDEOIO ERROR: V4L2: device %s is unable to capture video memory.\n",deviceName);
838 icvCloseCAM_V4L(capture);
843 have sub "Channel Numbers". For a typical V4L TV capture card, this is usually 1.
844 I myself am using a simple NTSC video input capture card that uses the value of 1.
849 if(capture->inp.index > 0) {
850 CLEAR (capture->inp);
851 capture->inp.index = CHANNEL_NUMBER;
854 if (-1 == ioctl (capture->deviceHandle, VIDIOC_ENUMINPUT, &capture->inp))
857 icvCloseCAM_V4L (capture);
863 CLEAR (capture->form);
864 capture->form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
866 if (-1 == ioctl (capture->deviceHandle, VIDIOC_G_FMT, &capture->form)) {
867 fprintf( stderr, "VIDEOIO ERROR: V4L2: Could not obtain specifics of capture window.\n\n");
868 icvCloseCAM_V4L(capture);
876 if (autosetup_capture_mode_v4l2(capture) == -1)
879 icvSetVideoSize(capture, DEFAULT_V4L_WIDTH, DEFAULT_V4L_HEIGHT);
884 min = capture->form.fmt.pix.width * 2;
886 if (capture->form.fmt.pix.bytesperline < min)
887 capture->form.fmt.pix.bytesperline = min;
889 min = capture->form.fmt.pix.bytesperline * capture->form.fmt.pix.height;
891 if (capture->form.fmt.pix.sizeimage < min)
892 capture->form.fmt.pix.sizeimage = min;
894 CLEAR (capture->req);
900 capture->req.count = buffer_number;
901 capture->req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
902 capture->req.memory = V4L2_MEMORY_MMAP;
904 if (-1 == ioctl (capture->deviceHandle, VIDIOC_REQBUFS, &capture->req))
912 /* free capture, and returns an error code */
913 icvCloseCAM_V4L (capture);
917 if (capture->req.count < buffer_number)
923 /* free capture, and returns an error code */
924 icvCloseCAM_V4L (capture);
934 for (n_buffers = 0; n_buffers < capture->req.count; ++n_buffers)
944 if (-1 == ioctl (capture->deviceHandle, VIDIOC_QUERYBUF, &buf)) {
947 /* free capture, and returns an error code */
948 icvCloseCAM_V4L (capture);
952 capture->buffers[n_buffers].length = buf.length;
953 capture->buffers[n_buffers].start =
958 capture->deviceHandle, buf.m.offset);
960 if (MAP_FAILED == capture->buffers[n_buffers].start) {
963 /* free capture, and returns an error code */
964 icvCloseCAM_V4L (capture);
969 capture->buffers[MAX_V4L_BUFFERS].start = malloc( buf.length );
970 capture->buffers[MAX_V4L_BUFFERS].length = buf.length;
975 cvInitImageHeader( &capture->frame,
976 cvSize( capture->form.fmt.pix.width,
977 capture->form.fmt.pix.height ),
980 capture->frame.imageData = (char *)cvAlloc(capture->frame.imageSize);
989 static int _capture_V4L (CvCaptureCAM_V4L *capture, char *deviceName)
993 detect_v4l = try_init_v4l(capture, deviceName);
1014 if ((capture->capability.type & VID_TYPE_CAPTURE) == 0) {
1017 "device %s is unable to capture video memory.\n",deviceName);
1018 icvCloseCAM_V4L(capture);
1026 have sub "Channel Numbers". For a typical V4L TV capture card, this is usually 1.
1027 I myself am using a simple NTSC video input capture card that uses the value of 1.
1034 if(capture->capability.channels>0) {
1040 if (ioctl(capture->deviceHandle, VIDIOCGCHAN , &selectedChannel) != -1) {
1043 if (ioctl(capture->deviceHandle, VIDIOCSCHAN , &selectedChannel) == -1) {
1054 if(ioctl(capture->deviceHandle, VIDIOCGWIN, &capture->captureWindow) == -1) {
1056 "Could not obtain specifics of capture window.\n\n");
1057 icvCloseCAM_V4L(capture);
1065 if (autosetup_capture_mode_v4l(capture) == -1)
1072 ioctl(capture->deviceHandle, VIDIOCGMBUF, &capture->memoryBuffer);
1073 capture->memoryMap = (char *)mmap(0,
1074 capture->memoryBuffer.size,
1077 capture->deviceHandle,
1079 if (capture->memoryMap == MAP_FAILED) {
1081 icvCloseCAM_V4L(capture);
1086 capture->mmaps = (struct video_mmap *)
1087 (malloc(capture->memoryBuffer.frames * sizeof(struct video_mmap)));
1088 if (!capture->mmaps) {
1090 icvCloseCAM_V4L(capture);
1097 cvInitImageHeader( &capture->frame,
1098 cvSize( capture->captureWindow.width,
1099 capture->captureWindow.height ),
1102 capture->frame.imageData = (char *)cvAlloc(capture->frame.imageSize);
1129 CvCaptureCAM_V4L * capture = (CvCaptureCAM_V4L*)cvAlloc(sizeof(CvCaptureCAM_V4L));
1130 if (!capture) {
1131 fprintf( stderr, "VIDEOIO ERROR: V4L: Could not allocate memory for capture process.\n");
1148 memset(capture,0,sizeof(CvCaptureCAM_V4L));
1151 capture->FirstCapture = 1;
1154 if (_capture_V4L2 (capture, deviceName) == -1) {
1155 icvCloseCAM_V4L(capture);
1159 if (_capture_V4L (capture, deviceName) == -1) {
1160 icvCloseCAM_V4L(capture);
1170 return capture;
1175 static int read_frame_v4l2(CvCaptureCAM_V4L* capture) {
1183 if (-1 == ioctl (capture->deviceHandle, VIDIOC_DQBUF, &buf)) {
1191 if (ioctl(capture->deviceHandle, VIDIOC_QBUF, &buf) == -1)
1205 assert(buf.index < capture->req.count);
1207 memcpy(capture->buffers[MAX_V4L_BUFFERS].start,
1208 capture->buffers[buf.index].start,
1209 capture->buffers[MAX_V4L_BUFFERS].length );
1210 capture->bufferIndex = MAX_V4L_BUFFERS;
1214 if (-1 == ioctl (capture->deviceHandle, VIDIOC_QBUF, &buf))
1217 //set timestamp in capture struct to be timestamp of most recent frame
1218 capture->timestamp = buf.timestamp;
1223 static void mainloop_v4l2(CvCaptureCAM_V4L* capture) {
1235 FD_SET (capture->deviceHandle, &fds);
1241 r = select (capture->deviceHandle+1, &fds, NULL, NULL, &tv);
1257 if (read_frame_v4l2 (capture))
1265 static int icvGrabFrameCAM_V4L(CvCaptureCAM_V4L* capture) {
1267 if (capture->FirstCapture) {
1280 for (capture->bufferIndex = 0;
1281 capture->bufferIndex < ((int)capture->req.count);
1282 ++capture->bufferIndex)
1291 buf.index = (unsigned long)capture->bufferIndex;
1293 if (-1 == ioctl (capture->deviceHandle, VIDIOC_QBUF, &buf)) {
1300 capture->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1301 if (-1 == ioctl (capture->deviceHandle, VIDIOC_STREAMON,
1302 &capture->type)) {
1315 for (capture->bufferIndex = 0;
1316 capture->bufferIndex < (capture->memoryBuffer.frames-1);
1317 ++capture->bufferIndex) {
1319 capture->mmaps[capture->bufferIndex].frame = capture->bufferIndex;
1320 capture->mmaps[capture->bufferIndex].width = capture->captureWindow.width;
1321 capture->mmaps[capture->bufferIndex].height = capture->captureWindow.height;
1322 capture->mmaps[capture->bufferIndex].format = capture->imageProperties.palette;
1324 if (ioctl(capture->deviceHandle, VIDIOCMCAPTURE, &capture->mmaps[capture->bufferIndex]) == -1) {
1325 fprintf( stderr, "VIDEOIO ERROR: V4L: Initial Capture Error: Unable to load initial memory buffers.\n");
1338 mainloop_v4l2(capture);
1343 capture->FirstCapture = 0;
1351 mainloop_v4l2(capture);
1361 capture->mmaps[capture->bufferIndex].frame = capture->bufferIndex;
1362 capture->mmaps[capture->bufferIndex].width = capture->captureWindow.width;
1363 capture->mmaps[capture->bufferIndex].height = capture->captureWindow.height;
1364 capture->mmaps[capture->bufferIndex].format = capture->imageProperties.palette;
1366 if (ioctl (capture->deviceHandle, VIDIOCMCAPTURE,
1367 &capture->mmaps[capture->bufferIndex]) == -1) {
1368 /* capture is on the way, so just exit */
1372 ++capture->bufferIndex;
1373 if (capture->bufferIndex == capture->memoryBuffer.frames) {
1374 capture->bufferIndex = 0;
2108 static IplImage* icvRetrieveFrameCAM_V4L( CvCaptureCAM_V4L* capture, int) {
2117 if (ioctl(capture->deviceHandle, VIDIOCSYNC, &capture->mmaps[capture->bufferIndex].frame) == -1) {
2133 if(((unsigned long)capture->frame.width != capture->form.fmt.pix.width)
2134 || ((unsigned long)capture->frame.height != capture->form.fmt.pix.height)) {
2135 cvFree(&capture->frame.imageData);
2136 cvInitImageHeader( &capture->frame,
2137 cvSize( capture->form.fmt.pix.width,
2138 capture->form.fmt.pix.height ),
2140 capture->frame.imageData = (char *)cvAlloc(capture->frame.imageSize);
2151 if((capture->frame.width != capture->mmaps[capture->bufferIndex].width)
2152 || (capture->frame.height != capture->mmaps[capture->bufferIndex].height)) {
2153 cvFree(&capture->frame.imageData);
2154 cvInitImageHeader( &capture->frame,
2155 cvSize( capture->captureWindow.width,
2156 capture->captureWindow.height ),
2158 capture->frame.imageData = (char *)cvAlloc(capture->frame.imageSize);
2168 switch (capture->palette)
2171 memcpy((char *)capture->frame.imageData,
2172 (char *)capture->buffers[capture->bufferIndex].start,
2173 capture->frame.imageSize);
2177 yuv420p_to_rgb24(capture->form.fmt.pix.width,
2178 capture->form.fmt.pix.height,
2179 (unsigned char*)(capture->buffers[capture->bufferIndex].start),
2180 (unsigned char*)capture->frame.imageData);
2184 yuv411p_to_rgb24(capture->form.fmt.pix.width,
2185 capture->form.fmt.pix.height,
2186 (unsigned char*)(capture->buffers[capture->bufferIndex].start),
2187 (unsigned char*)capture->frame.imageData);
2191 if (!mjpeg_to_rgb24(capture->form.fmt.pix.width,
2192 capture->form.fmt.pix.height,
2193 (unsigned char*)(capture->buffers[capture->bufferIndex]
2195 capture->buffers[capture->bufferIndex].length,
2196 (unsigned char*)capture->frame.imageData))
2202 yuyv_to_rgb24(capture->form.fmt.pix.width,
2203 capture->form.fmt.pix.height,
2204 (unsigned char*)(capture->buffers[capture->bufferIndex].start),
2205 (unsigned char*)capture->frame.imageData);
2208 uyvy_to_rgb24(capture
2209 capture->form.fmt.pix.height,
2210 (unsigned char*)(capture->buffers[capture->bufferIndex].start),
2211 (unsigned char*)capture->frame.imageData);
2214 bayer2rgb24(capture->form.fmt.pix.width,
2215 capture->form.fmt.pix.height,
2216 (unsigned char*)capture->buffers[capture->bufferIndex].start,
2217 (unsigned char*)capture->frame.imageData);
2222 sonix_decompress(capture->form.fmt.pix.width,
2223 capture->form.fmt.pix.height,
2224 (unsigned char*)capture->buffers[capture->bufferIndex].start,
2225 (unsigned char*)capture->buffers[(capture->bufferIndex+1) % capture->req.count].start);
2227 bayer2rgb24(capture->form.fmt.pix.width,
2228 capture->form.fmt.pix.height,
2229 (unsigned char*)capture->buffers[(capture->bufferIndex+1) % capture->req.count].start,
2230 (unsigned char*)capture->frame.imageData);
2234 sgbrg2rgb24(capture->form.fmt.pix.width,
2235 capture->form.fmt.pix.height,
2236 (unsigned char*)capture->buffers[(capture->bufferIndex+1) % capture->req.count].start,
2237 (unsigned char*)capture->frame.imageData);
2240 rgb24_to_rgb24(capture->form.fmt.pix.width,
2241 capture->form.fmt.pix.height,
2242 (unsigned char*)capture->buffers[(capture->bufferIndex+1) % capture->req.count].start,
2243 (unsigned char*)capture->frame.imageData);
2254 switch(capture->imageProperties.palette)
2257 memcpy((char *)capture->frame.imageData,
2258 (char *)(capture->memoryMap + capture->memoryBuffer.offsets[capture->bufferIndex]),
2259 capture->frame.imageSize);
2262 yuv420p_to_rgb24(capture->captureWindow.width,
2263 capture->captureWindow.height,
2264 (unsigned char*)(capture->memoryMap + capture->memoryBuffer.offsets[capture->bufferIndex]),
2265 (unsigned char*)capture->frame.imageData);
2268 yuv420_to_rgb24(capture->captureWindow.width,
2269 capture->captureWindow.height,
2270 (unsigned char*)(capture->memoryMap + capture->memoryBuffer.offsets[capture->bufferIndex]),
2271 (unsigned char*)capture->frame.imageData);
2274 yuv411p_to_rgb24(capture->captureWindow.width,
2275 capture->captureWindow.height,
2276 (unsigned char*)(capture->memoryMap + capture->memoryBuffer.offsets[capture->bufferIndex]),
2277 (unsigned char*)capture->frame.imageData);
2282 capture->imageProperties.palette);
2290 return(&capture->frame);
2293 static double icvGetPropertyCAM_V4L (CvCaptureCAM_V4L* capture,
2307 CLEAR (capture->form);
2308 capture->form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2309 if (-1 == ioctl (capture->deviceHandle, VIDIOC_G_FMT, &capture->form)) {
2317 return capture->form.fmt.pix.width;
2319 return capture->form.fmt.pix.height;
2326 if (capture->FirstCapture) {
2329 return 1000 * capture->timestamp.tv_sec + ((double) capture->timestamp.tv_usec) / 1000;
2333 capture->control.id = V4L2_CID_BRIGHTNESS;
2336 capture->control.id = V4L2_CID_CONTRAST;
2339 capture->control.id = V4L2_CID_SATURATION;
2342 capture->control.id = V4L2_CID_HUE;
2345 capture->control.id = V4L2_CID_GAIN;
2348 capture->control.id = V4L2_CID_EXPOSURE;
2357 if (-1 == ioctl (capture->deviceHandle, VIDIOC_G_CTRL,
2358 &capture->control)) {
2390 v4l2_min = capture->v4l2_brightness_min;
2391 v4l2_max = capture->v4l2_brightness_max;
2394 v4l2_min = capture->v4l2_contrast_min;
2395 v4l2_max = capture->v4l2_contrast_max;
2398 v4l2_min = capture->v4l2_saturation_min;
2399 v4l2_max = capture->v4l2_saturation_max;
2402 v4l2_min = capture->v4l2_hue_min;
2403 v4l2_max = capture->v4l2_hue_max;
2406 v4l2_min = capture->v4l2_gain_min;
2407 v4l2_max = capture->v4l2_gain_max;
2410 v4l2_min = capture->v4l2_exposure_min;
2411 v4l2_max = capture->v4l2_exposure_max;
2416 return ((float)capture->control.value - v4l2_min + 1) / (v4l2_max - v4l2_min);
2428 if (ioctl (capture->deviceHandle,
2429 VIDIOCGWIN, &capture->captureWindow) < 0) {
2433 icvCloseCAM_V4L(capture);
2439 retval = capture->captureWindow.width;
2442 retval = capture->captureWindow.height;
2445 retval = capture->imageProperties.brightness;
2448 retval = capture->imageProperties.contrast;
2451 retval = capture->imageProperties.colour;
2454 retval = capture->imageProperties.hue;
2485 static int icvSetVideoSize( CvCaptureCAM_V4L* capture, int w, int h) {
2492 CLEAR (capture->cropcap);
2493 capture->cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2495 if (ioctl (capture->deviceHandle, VIDIOC_CROPCAP, &capture->cropcap) < 0) {
2499 CLEAR (capture->crop);
2500 capture->crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2501 capture->crop.c= capture->cropcap.defrect;
2504 if (ioctl (capture->deviceHandle, VIDIOC_S_CROP, &capture->crop) < 0) {
2509 CLEAR (capture->form);
2510 capture->form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2513 ioctl (capture->deviceHandle, VIDIOC_G_FMT, &capture->form);
2516 capture->form.fmt.pix.width = w;
2517 capture->form.fmt.pix.height = h;
2518 capture->form.fmt.win.chromakey = 0;
2519 capture->form.fmt.win.field = V4L2_FIELD_ANY;
2520 capture->form.fmt.win.clips = 0;
2521 capture->form.fmt.win.clipcount = 0;
2522 capture->form.fmt.pix.field = V4L2_FIELD_ANY;
2528 ioctl (capture->deviceHandle, VIDIOC_S_FMT, &capture->form);
2534 setfps.parm.capture.timeperframe.numerator = 1;
2535 setfps.parm.capture.timeperframe.denominator = 30;
2536 ioctl (capture->deviceHandle, VIDIOC_S_PARM, &setfps);
2540 capture->FirstCapture = 1;
2543 if (-1 == ioctl (capture->deviceHandle, VIDIOC_G_FMT, &capture->form))
2545 fprintf(stderr, "VIDEOIO ERROR: V4L/V4L2: Could not obtain specifics of capture window.\n\n");
2547 icvCloseCAM_V4L(capture);
2562 if (capture==0) return 0;
2563 if (w>capture->capability.maxwidth) {
2564 w=capture->capability.maxwidth;
2566 if (h>capture->capability.maxheight) {
2567 h=capture->capability.maxheight;
2570 capture->captureWindow.width=w;
2571 capture->captureWindow.height=h;
2573 if (ioctl(capture->deviceHandle, VIDIOCSWIN, &capture->captureWindow) < 0) {
2574 icvCloseCAM_V4L(capture);
2578 if (ioctl(capture->deviceHandle, VIDIOCGWIN, &capture->captureWindow) < 0) {
2579 icvCloseCAM_V4L(capture);
2583 capture->FirstCapture = 1;
2592 static int icvSetControl (CvCaptureCAM_V4L* capture,
2612 CLEAR (capture->control);
2618 capture->control.id = V4L2_CID_BRIGHTNESS;
2621 capture->control.id = V4L2_CID_CONTRAST;
2624 capture->control.id = V4L2_CID_SATURATION;
2627 capture->control.id = V4L2_CID_HUE;
2630 capture->control.id = V4L2_CID_GAIN;
2633 capture->control.id = V4L2_CID_EXPOSURE;
2643 if (-1 == ioctl (capture->deviceHandle,
2644 VIDIOC_G_CTRL, &capture->control)) {
2653 v4l2_min = capture->v4l2_brightness_min;
2654 v4l2_max = capture->v4l2_brightness_max;
2657 v4l2_min = capture->v4l2_contrast_min;
2658 v4l2_max = capture->v4l2_contrast_max;
2661 v4l2_min = capture->v4l2_saturation_min;
2662 v4l2_max = capture->v4l2_saturation_max;
2665 v4l2_min = capture->v4l2_hue_min;
2666 v4l2_max = capture->v4l2_hue_max;
2669 v4l2_min = capture->v4l2_gain_min;
2670 v4l2_max = capture->v4l2_gain_max;
2673 v4l2_min = capture->v4l2_exposure_min;
2674 v4l2_max = capture->v4l2_exposure_max;
2679 CLEAR (capture->control);
2685 capture->control.id = V4L2_CID_BRIGHTNESS;
2688 capture->control.id = V4L2_CID_CONTRAST;
2691 capture->control.id = V4L2_CID_SATURATION;
2694 capture->control.id = V4L2_CID_HUE;
2697 capture->control.id = V4L2_CID_GAIN;
2700 capture->control.id = V4L2_CID_EXPOSURE;
2710 capture->control.value = (int)(value * (v4l2_max - v4l2_min) + v4l2_min);
2713 if (-1 == ioctl (capture->deviceHandle,
2714 VIDIOC_S_CTRL, &capture->control) && errno != ERANGE) {
2733 capture->imageProperties.brightness = v4l_value;
2736 capture->imageProperties.contrast = v4l_value;
2739 capture->imageProperties.colour = v4l_value;
2742 capture->imageProperties.hue = v4l_value;
2759 if (ioctl(capture->deviceHandle, VIDIOCSPICT, &capture->imageProperties)
2764 icvCloseCAM_V4L(capture);
2775 static int icvSetPropertyCAM_V4L( CvCaptureCAM_V4L* capture,
2791 retval = icvSetVideoSize( capture, width, height);
2798 retval = icvSetVideoSize( capture, width, height);
2808 retval = icvSetControl(capture, property_id, value);
2820 static void icvCloseCAM_V4L( CvCaptureCAM_V4L* capture ){
2823 if (capture)
2832 if (capture->mmaps)
2833 free(capture->mmaps);
2834 if (capture->memoryMap)
2835 munmap(capture->memoryMap, capture->memoryBuffer.size);
2844 capture->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2845 if (-1 == ioctl(capture->deviceHandle, VIDIOC_STREAMOFF, &capture->type)) {
2849 for (unsigned int n_buffers_ = 0; n_buffers_ < capture->req.count; ++n_buffers_)
2851 if (-1 == munmap (capture->buffers[n_buffers_].start, capture->buffers[n_buffers_].length)) {
2856 if (capture->buffers[MAX_V4L_BUFFERS].start)
2858 free(capture->buffers[MAX_V4L_BUFFERS].start);
2859 capture->buffers[MAX_V4L_BUFFERS].start = 0;
2864 if (capture->deviceHandle != -1)
2865 close(capture->deviceHandle);
2867 if (capture->frame.imageData) cvFree(&capture->frame.imageData);
2868 //cvFree((void **)capture);
2929 CvCaptureCAM_V4L_CPP* capture = new CvCaptureCAM_V4L_CPP;
2931 if( capture->open( index ))
2932 return (CvCapture*)capture;
2934 delete capture;