From: Dominic L. <ma...@us...> - 2005-10-19 20:20:27
|
Update of /cvsroot/robotflow/RobotFlow/Devices/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16126/src Modified Files: V4L2Capture.cc Log Message: testing new driver Index: V4L2Capture.cc =================================================================== RCS file: /cvsroot/robotflow/RobotFlow/Devices/src/V4L2Capture.cc,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** V4L2Capture.cc 29 Mar 2005 15:20:39 -0000 1.15 --- V4L2Capture.cc 19 Oct 2005 20:20:19 -0000 1.16 *************** *** 1,3 **** ! /* Copyright (C) 2002 Dominic Letourneau (dom...@co...) This library is free software; you can redistribute it and/or --- 1,3 ---- ! /* Copyright (C) 2005 Dominic Letourneau (dom...@us...) This library is free software; you can redistribute it and/or *************** *** 16,23 **** */ - #include "V4L2Capture.h" #include "BufferedNode.h" #include "Image.h" #include <stdlib.h> using namespace std; --- 16,23 ---- */ #include "BufferedNode.h" #include "Image.h" #include <stdlib.h> + #include "V4L2Capture.h" using namespace std; *************** *** 118,122 **** V4L2Capture(string nodeName, ParameterSet params) : BufferedNode(nodeName, params) { ! m_outputID = addOutput("IMAGE"); --- 118,122 ---- V4L2Capture(string nodeName, ParameterSet params) : BufferedNode(nodeName, params) { ! m_outputID = addOutput("IMAGE"); *************** *** 133,137 **** string format = object_cast<String>(parameters.get("FORMAT")); ! if (format == "GREYSCALE") { m_pixelsize = 1; --- 133,141 ---- string format = object_cast<String>(parameters.get("FORMAT")); ! m_autoSoftBrightness = dereference_cast<bool>(parameters.get("AUTO_SOFT_BRIGHTNESS")); ! ! m_captureDevice.init(NULL); ! ! /* if (format == "GREYSCALE") { m_pixelsize = 1; *************** *** 154,159 **** } - m_autoSoftBrightness = dereference_cast<bool>(parameters.get("AUTO_SOFT_BRIGHTNESS")); - if (!m_captureDevice.set_contrast(m_contrast)) { throw new GeneralException(string("Unable to set contrast on V4L2 device : ") + m_device, __FILE__,__LINE__); --- 158,161 ---- *************** *** 175,183 **** throw new GeneralException(string("Unable to set auto white balance on V4L2 device : ") + m_device, __FILE__,__LINE__); } ! } virtual void calculate(int output_id, int count, Buffer &out) { Image *image = Image::alloc(m_width,m_height,m_pixelsize); //allocate image --- 177,195 ---- throw new GeneralException(string("Unable to set auto white balance on V4L2 device : ") + m_device, __FILE__,__LINE__); } ! */ } virtual void calculate(int output_id, int count, Buffer &out) { + + + + + m_captureDevice.mainloop(); + + + + + /* Image *image = Image::alloc(m_width,m_height,m_pixelsize); //allocate image *************** *** 199,238 **** } out[count] = ObjectRef(image); } ! private: ! ! void automaticBrightness(Image *image) { ! //cerr<<"automatic brightness ON"<<endl; ! ! float avg_intensity = 0; ! ! //init random number generator ! srand(time(NULL)); ! ! //taking 1% of image pixels ! for (int i = 0; i < (image->get_width() * image->get_height()) / 100; i++) { ! unsigned short *imagePixelPtr = (unsigned short*) image->get_data(); ! int rand_pos = rand() % (image->get_width() * image->get_height()); ! ! avg_intensity += (((imagePixelPtr[rand_pos] >> 10)) & 0x1F + ((imagePixelPtr[rand_pos]) >> 5) & 0x1F + (imagePixelPtr[rand_pos]) & 0x1F); ! ! } ! ! avg_intensity /= 3; ! //cerr<<"average_intensity "<<avg_intensity<<endl; ! ! if (avg_intensity < 10) { ! //+++ brigh ! m_brightness += 10; ! m_captureDevice.set_brightness(m_brightness); ! //cerr<<"brightness "<<m_brightness<<endl; ! } else if (avg_intensity > 20) { ! //--- brightness ! m_brightness -= 10; ! m_captureDevice.set_brightness(m_brightness); ! //cerr<<"brightness "<<m_brightness<<endl; ! } ! } --- 211,219 ---- } out[count] = ObjectRef(image); + */ + out[count] = nilObject; } ! *************** *** 241,656 **** }//namespace RobotFlow ! /*========================================================================= ! capture.cc ! ------------------------------------------------------------------------- ! Example code for video capture under Video4Linux II ! ------------------------------------------------------------------------- ! Copyright 1999, 2000 ! Anna Helena Reali Costa, James R. Bruce ! School of Computer Science ! Carnegie Mellon University ! ------------------------------------------------------------------------- ! This source code is distributed "as is" with absolutely no warranty. ! See LICENSE, which should be included with this distribution. ! ------------------------------------------------------------------------- ! Revision History: ! 2000-02-05: Ported to work with V4L2 API ! 1999-11-23: Quick C++ port to simplify & wrap in an object (jbruce) ! 1999-05-01: Initial version (annar) ! =========================================================================*/ ! ! //==== Capture Class Implementation =======================================// ! ! void grabSetFps(int fd, int fps) ! { ! ! struct v4l2_streamparm params; ! ! printf("called v4l2_set_fps with fps=%d\n",fps); ! params.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; ! ! if (EINVAL == ioctl(fd, VIDIOC_G_PARM, ¶ms)) { ! perror("VIDIOC_G_PARAM"); ! return; ! } ! ! params.parm.capture.capturemode |= V4L2_CAP_TIMEPERFRAME; ! params.parm.capture.timeperframe.numerator = 10000000; ! params.parm.capture.timeperframe.denominator = fps; ! ! if (EINVAL == ioctl(fd, VIDIOC_S_PARM, ¶ms)){ ! perror("VIDIOC_S_PARAM"); ! return; ! } ! ! } ! ! bool capture::initialize(const char *device,int nwidth,int nheight,int nfmt, bool continuous) ! { ! struct v4l2_requestbuffers req; ! struct v4l2_capability capability; ! struct v4l2_input inputs; ! struct v4l2_standard standard; ! v4l2_std_id std; ! ! int err; ! int i; ! ! // Set defaults if not given ! if(!device) device = DEFAULT_VIDEO_DEVICE; ! if(!nfmt) nfmt = DEFAULT_VIDEO_FORMAT; ! if(!nwidth || !nheight){ ! nwidth = DEFAULT_IMAGE_WIDTH; ! nheight = DEFAULT_IMAGE_HEIGHT; ! } ! ! // Open the video device ! vid_fd = open(device, O_RDWR); ! if(vid_fd == -1){ ! printf("Could not open video device [%s]\n",device); ! perror("Error : "); ! return(false); ! } ! ! //query device capabilities ! err = ioctl(vid_fd,VIDIOC_QUERYCAP, &capability); ! ! if(err == EINVAL) { ! perror("Unable to get capabilities (incompatible kernel) "); ! return false; ! } ! else { ! cerr<<"Got capabilities"<<endl; ! printf("Driver : %s \n",capability.driver); ! printf("Cart : %s \n",capability.card); ! printf("Cap : %x \n",capability.capabilities); ! if (capability.capabilities & V4L2_CAP_VIDEO_CAPTURE) { ! cerr<<"Video capture capable!"<<endl; ! } ! else { ! cerr<<"Capture card unrecognized!"<<endl; ! return false; ! } ! } ! ! //get input ! err = ioctl(vid_fd,VIDIOC_G_INPUT,&inputs); ! ! if (err == EINVAL) { ! perror("VIDIOC_G_INPUG "); ! return false; ! } ! ! ! //enum inputs ! err = ioctl(vid_fd, VIDIOC_ENUMINPUT, &inputs); ! ! if (err == EINVAL) { ! perror("VIDEOC_ENUMINPUT : "); ! return false; ! } ! else { ! cerr<<"input index "<<inputs.index<<endl; ! cerr<<"input name "<<inputs.name<<endl; ! cerr<<"input got signal " <<!(inputs.status & V4L2_IN_ST_NO_SIGNAL)<<endl; ! ! } ! ! //set standard to NTSC ! ! std = V4L2_STD_NTSC; ! ! if (EINVAL == ioctl(vid_fd,VIDIOC_S_STD,&std)) { ! perror("VIDIOC_S_STD"); ! return false; ! } ! ! ! if (EINVAL == ioctl(vid_fd,VIDIOC_G_STD,&std)) { ! perror("VIDIOC_G_STD"); ! return false; ! } ! ! //print standard description ! standard.index = 0; ! ! while(1) { ! ! if(EINVAL == ioctl (vid_fd, VIDIOC_ENUMSTD, &standard)) { ! //perror("VIDIOC_ENUMSTD"); ! ! //return false; ! break; ! } ! ! if (standard.id == std) { ! printf ("Current video standard: %s\n", standard.name); ! break; ! } ! ! standard.index++; ! } ! ! ! ! fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; ! ! ! err = ioctl(vid_fd, VIDIOC_G_FMT, &fmt); ! if(err){ ! perror("VIDIOC_G_FMT"); ! return(false); ! } ! ! ! // Set video format ! fmt.fmt.pix.width = nwidth; ! fmt.fmt.pix.height = nheight; ! fmt.fmt.pix.pixelformat = nfmt; ! ! if(EINVAL == ioctl(vid_fd, VIDIOC_S_FMT, &fmt)) { ! perror("S_FMT:"); ! return(false); ! } ! ! //Stop init here if standard I/O ! ! //setting 30 frames per second ! if (continuous) { ! grabSetFps(vid_fd, 30); ! ! ! // Request mmap-able capture buffers ! req.count = STREAMBUFS; ! req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; ! req.memory = V4L2_MEMORY_MMAP; ! ! err = ioctl(vid_fd, VIDIOC_REQBUFS, &req); ! if(err < 0 || req.count < 1){ ! printf("REQBUFS returned error %d, count %d\n", ! errno,req.count); ! return(false); ! } ! ! printf("mmapable buffers count : %i\n",req.count); ! ! ! //initialize memory map for each image buffer ! for(i=0; i<req.count; i++){ ! ! vimage[i].vidbuf.index = i; ! vimage[i].vidbuf.type = req.type; ! ! if(EINVAL == ioctl(vid_fd, VIDIOC_QUERYBUF, &vimage[i].vidbuf)) { ! perror("VIDIOC_QUERYBUF"); ! return(false); ! } ! ! //map memory ! vimage[i].data = (char*)mmap(NULL, ! vimage[i].vidbuf.length, ! PROT_READ | PROT_WRITE, ! MAP_SHARED, ! vid_fd, ! vimage[i].vidbuf.m.offset); ! ! if((int)vimage[i].data == -1){ ! perror("mmap error : "); ! return(false); ! } ! } ! ! //queue buffers ! for(i=0; i<req.count; i++){ ! if((err = ioctl(vid_fd, VIDIOC_QBUF, &vimage[i].vidbuf))){ ! perror("QBUF error"); ! return(false); ! } ! } ! ! // Turn on streaming capture ! if (EINVAL == ioctl(vid_fd, VIDIOC_STREAMON, &vimage[0].vidbuf.type)) { ! perror("VIDIOC_STREAMON"); ! return(false); ! } ! } ! ! width = nwidth; ! height = nheight; ! current = NULL; ! ! return(true); ! } ! ! void capture::close() ! { ! int i,t; ! ! if(vid_fd >= 0){ ! t = V4L2_BUF_TYPE_VIDEO_CAPTURE; ! ioctl(vid_fd, VIDIOC_STREAMOFF, &t); ! ! for(i=0; i<STREAMBUFS; i++){ ! if(vimage[i].data){ ! munmap(vimage[i].data,vimage[i].vidbuf.length); ! } ! } ! } ! } ! ! ! unsigned char *capture::captureFrame(int &index,int &field) ! { ! //if(captured_frame){ ! // printf("there may be a problem capturing frame w/o releasing previous frame"); ! //} ! // struct v4l2_buffer tempbuf; ! int err; ! ! fd_set rdset; ! struct timeval timeout; ! int n; ! struct v4l2_buffer tempbuf; ! ! FD_ZERO(&rdset); ! FD_SET(vid_fd, &rdset); ! timeout.tv_sec = 1; ! timeout.tv_usec = 0; ! n = select(vid_fd + 1, &rdset, NULL, NULL, &timeout); ! err = -1; ! if (n == -1) { ! fprintf(stderr, "select error.\n"); ! perror("select msg:"); ! } ! else if (n == 0) ! fprintf(stderr, "select timeout\n"); ! else if (FD_ISSET(vid_fd, &rdset)) ! err = 0; ! if(err) return(NULL); ! ! // Grab last frame ! do { ! //printf("D"); ! tempbuf.type = vimage[0].vidbuf.type; ! err = ioctl(vid_fd, VIDIOC_DQBUF, &tempbuf); ! if(err) { ! printf("4DQBUF returned error %d\n",errno); ! perror("DQBUF error:"); ! } ! ! //printf("S"); ! FD_ZERO(&rdset); ! FD_SET(vid_fd, &rdset); ! timeout.tv_sec = 0; ! timeout.tv_usec = 0; ! n = select(vid_fd + 1, &rdset, NULL, NULL, &timeout); ! // Uncomment this next line to see how many frames are being skipped. ! // 1=frame skipped, 0=frame used ! //printf("n%d",n); ! if(n==-1) { ! fprintf(stderr, "select error.\n"); ! perror("2select msg:"); ! } ! else if(n==0) { ! break; ! } ! ! if (!FD_ISSET(vid_fd, &rdset)) ! printf("huh\n"); ! ! //printf("Q"); ! err = ioctl(vid_fd, VIDIOC_QBUF, &tempbuf); ! if(err) { ! printf("3QBUF returned error %d\n",errno); ! perror("QBUF error:"); ! } ! } while(true); ! ! field = 0; ! ! if(tempbuf.flags & V4L2_FIELD_TOP) field = 1; ! if(tempbuf.flags & V4L2_FIELD_BOTTOM) field = 0; ! ! // Set current to point to captured frame data ! current = (unsigned char *)vimage[tempbuf.index].data; ! ///timestamp = tempbuf.timestamp; ! index = tempbuf.index; ! ! //cerr<<"grab index : "<<index<<endl; ! ! // gettimeofday(&timeout,NULL); ! // timestamp = (stamp_t)((timeout.tv_sec + timeout.tv_usec/1.0E6) * 1.0E9); ! // printf("field: %d\n",field); ! ! // Initiate the next capture ! //tempbuf.index = (++index - 1) % STREAMBUFS; ! //err = ioctl(vid_fd, VIDIOC_QBUF, &tempbuf); ! //if(err) printf("QBUF returned error %d\n",errno); ! ! captured_frame = true; ! return(current); ! } ! ! void capture::releaseFrame(unsigned char* frame, int index) ! { ! int err; ! struct v4l2_buffer tempbuf; ! //if(frame != current){ ! // printf("frame != current, possibly releasing the wrong frame."); ! //} ! captured_frame = false; ! // Initiate the next capture ! tempbuf.type = vimage[0].vidbuf.type; ! tempbuf.index=index; ! //printf("%lx %d\n",tempbuf.type,tempbuf.index); ! err = ioctl(vid_fd, VIDIOC_QBUF, &tempbuf); ! if(err) { ! printf("2QBUF returned error %d\n",errno); ! perror("QBUF error:"); ! } ! } ! ! struct v4l2_buffer tempbuf; ! ! unsigned char *capture::captureFrame() ! { ! // struct v4l2_buffer tempbuf; ! int err; ! ! fd_set rdset; ! struct timeval timeout; ! int n; ! ! FD_ZERO(&rdset); ! FD_SET(vid_fd, &rdset); ! timeout.tv_sec = 1; ! timeout.tv_usec = 0; ! n = select(vid_fd + 1, &rdset, NULL, NULL, &timeout); ! err = -1; ! if (n == -1) ! fprintf(stderr, "select error.\n"); ! else if (n == 0) ! fprintf(stderr, "select timeout\n"); ! else if (FD_ISSET(vid_fd, &rdset)) ! err = 0; ! if(err) return(NULL); ! ! // Grab last frame ! tempbuf.type = vimage[0].vidbuf.type; ! err = ioctl(vid_fd, VIDIOC_DQBUF, &tempbuf); ! if(err) printf("DQBUF returned error %d\n",errno); ! ! // Set current to point to captured frame data ! //cerr<<"Grab index : "<<tempbuf.index<<endl; ! ! current = (unsigned char *)vimage[tempbuf.index].data; ! //timestamp = tempbuf.timestamp; ! ! // Initiate the next capture ! err = ioctl(vid_fd, VIDIOC_QBUF, &tempbuf); ! if(err) printf("QBUF returned error %d\n",errno); ! ! return(current); ! } bool capture::set_auto_white_balance(bool white_balance) { --- 222,226 ---- }//namespace RobotFlow ! /* bool capture::set_auto_white_balance(bool white_balance) { *************** *** 738,739 **** --- 308,311 ---- } + */ + |