Changeset 347 in flair-src


Ignore:
Timestamp:
Feb 11, 2020, 5:58:59 PM (5 years ago)
Author:
Sanahuja Guillaume
Message:

modif v4l

Location:
trunk/lib/FlairSensorActuator/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FlairSensorActuator/src/V4LCamera.cpp

    r346 r347  
    3434
    3535#define CLEAR(x) memset (&(x), 0, sizeof (x))
    36 #define DEFAULT_V4L_WIDTH  640
    37 #define DEFAULT_V4L_HEIGHT 480
    38 //#define CV_CAP_PROP_POS_MSEC       0
    39 //#define CV_CAP_PROP_POS_FRAMES     1
    40 //#define CV_CAP_PROP_POS_AVI_RATIO  2
    41 //#define CV_CAP_PROP_FRAME_WIDTH    3
    42 //#define CV_CAP_PROP_FRAME_HEIGHT   4
    43 //#define CV_CAP_PROP_FPS            5
    44 //#define CV_CAP_PROP_FOURCC         6
    45 //#define CV_CAP_PROP_FRAME_COUNT    7
    46 #define CV_CAP_PROP_FORMAT         8
    47 #define CV_CAP_PROP_MODE           9
    48 #define CV_CAP_PROP_BRIGHTNESS    10
    49 #define CV_CAP_PROP_CONTRAST      11
    50 #define CV_CAP_PROP_SATURATION    12
    51 #define CV_CAP_PROP_HUE           13
    52 #define CV_CAP_PROP_GAIN          14
    53 //#define CV_CAP_PROP_CONVERT_RGB   15
    54 #define CV_CAP_PROP_SHARPNESS     16
    55 #define CV_CAP_PROP_EXPOSURE      17
    56 #define CV_CAP_PROP_AUTOGAIN      18
    57 #define CV_CAP_PROP_AWB           19
    5836
    5937using std::string;
     
    7957  /* w/o memset some parts  arent initialized - AKA: Fill it with zeros so it is clean */
    8058  memset(&capture,0,sizeof(CvCaptureCAM_V4L));
    81   capture.FirstCapture = 1;
    82   PALETTE_YUYV = 0;
    83   PALETTE_UYVY= 0;
    8459  n_buffers = 0;
    85  
    86   if(init(width,height) == -1) {
    87    Thread::Err("initialisation failed\n");
    88   }
     60
    8961 
    9062  if(format == Image::Type::Format::UYVY) {
    91     if (setV4LProperty(CV_CAP_PROP_FORMAT, V4L2_PIX_FMT_UYVY)<0)
    92       Thread::Err("set capture property error\n");
     63    if(init(width,height,V4L2_PIX_FMT_UYVY) == -1) {
     64     Thread::Err("initialisation failed\n");
     65    }
    9366  } else if (format == Image::Type::Format::YUYV) {
    94     if (setV4LProperty(CV_CAP_PROP_FORMAT, V4L2_PIX_FMT_YUYV) <
    95         0)
    96       Thread::Err("set capture property error\n");
     67    if(init(width,height,V4L2_PIX_FMT_YUYV) == -1) {
     68     Thread::Err("initialisation failed\n");
     69    }
    9770  } else {
    9871    Thread::Err("format not supported\n");
     72  }
     73 
     74  /* This is just a technicality, but all buffers must be filled up before any
     75  staggered SYNC is applied.  SO, filler up. (see V4L HowTo) */
     76
     77  allocBuffers();
     78
     79  for (capture.bufferIndex = 0;capture.bufferIndex < ((int)capture.req.count);++capture.bufferIndex) {
     80    struct v4l2_buffer buf;
     81
     82    CLEAR (buf);
     83
     84    buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
     85    buf.memory      = V4L2_MEMORY_MMAP;
     86    buf.index       = (unsigned long)capture.bufferIndex;
     87
     88    if (-1 == xioctl (device, VIDIOC_QBUF, &buf)) {
     89      Thread::Err("VIDIOC_QBUF error\n");
     90    }
     91  }
     92
     93  /* enable the streaming */
     94  capture.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
     95  if (-1 == xioctl (device, VIDIOC_STREAMON,&capture.type)) {
     96    /* error enabling the stream */
     97    Thread::Err("VIDIOC_STREAMON error\n");
    9998  }
    10099 
     
    123122}
    124123
    125 int V4LCamera::init(int width, int height) {
     124int V4LCamera::init(int width, int height,unsigned long colorspace) {
    126125  CLEAR (capture.cap);
    127126  if(-1 == xioctl(device, VIDIOC_QUERYCAP, &capture.cap)) {
     
    136135    }
    137136  }
    138 
    139   /* Init V4L2 control variables */
    140   capture.v4l2_brightness = 0;
    141   capture.v4l2_contrast = 0;
    142   capture.v4l2_saturation = 0;
    143   capture.v4l2_hue = 0;
    144   capture.v4l2_gain = 0;
    145   capture.v4l2_sharpness = 0;
    146   capture.v4l2_exposure = 0;
    147   capture.v4l2_autogain = 0;
    148   capture.v4l2_awb = 0;
    149 
    150   capture.v4l2_brightness_min = 0;
    151   capture.v4l2_contrast_min = 0;
    152   capture.v4l2_saturation_min = 0;
    153   capture.v4l2_hue_min = 0;
    154   capture.v4l2_gain_min = 0;
    155   capture.v4l2_sharpness_min = 0;
    156   capture.v4l2_exposure_min = 0;
    157   capture.v4l2_autogain_min = 0;
    158   capture.v4l2_awb_min = 0;
    159 
    160   capture.v4l2_brightness_max = 0;
    161   capture.v4l2_contrast_max = 0;
    162   capture.v4l2_saturation_max = 0;
    163   capture.v4l2_hue_max = 0;
    164   capture.v4l2_gain_max = 0;
    165   capture.v4l2_sharpness_max = 0;
    166   capture.v4l2_exposure_max = 0;
    167   capture.v4l2_autogain_max = 0;
    168   capture.v4l2_awb_max = 0;
    169137     
    170138  if ((capture.cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
     
    181149     return -1;
    182150  }
    183 
    184   if (autosetup_capture_mode_v4l2(&capture) == -1) {
    185      return -1;
    186   }
    187 
    188   icvSetVideoSize(&capture, width, height);
     151 
     152  setVideoSize(width, height,colorspace);
    189153
    190154  unsigned int min;
     
    204168}
    205169
    206 int V4LCamera::setV4LProperty(int property_id, double value){
    207   int retval=0;
    208   switch (property_id) {
    209     case CV_CAP_PROP_FORMAT:
    210       capture.form.fmt.pix.pixelformat = (unsigned long)value;
    211       retval == xioctl(device, VIDIOC_S_FMT, &capture.form);
    212       if ((unsigned long)value != capture.form.fmt.pix.pixelformat) {
    213         retval=-1;
    214       }
    215       PALETTE_YUYV = 0;
    216       PALETTE_UYVY= 0;
    217       if((unsigned long)value== V4L2_PIX_FMT_YUYV) {
    218         PALETTE_YUYV = 1;
    219       }
    220       if((unsigned long)value== V4L2_PIX_FMT_UYVY) {
    221         PALETTE_UYVY = 1;
    222       }
    223       break;
    224     default:
    225       Thread::Warn("setting property #%d is not supported\n",property_id);
    226   }
    227 
    228   return retval;
    229 }
    230 
    231 int V4LCamera::icvSetControl(CvCaptureCAM_V4L* capture,int property_id, double value) {
    232  
    233   /* limitation of the input value */
    234   if (value < 0.0) {
    235     value = 0.0;
    236   } else if (value > 1.0) {
    237     value = 1.0;
    238   }
    239 
    240 
    241     /* default value for min and max */
    242     int v4l2_min = 0;
    243     int v4l2_max = 255;
    244 
    245     /* initialisations */
    246     CLEAR (capture->control);
    247 
    248     /* set which control we want to set */
    249     switch (property_id) {
    250 
    251     case CV_CAP_PROP_BRIGHTNESS:
    252         capture->control.id = V4L2_CID_BRIGHTNESS;
    253         break;
    254     case CV_CAP_PROP_CONTRAST:
    255         capture->control.id = V4L2_CID_CONTRAST;
    256         break;
    257     case CV_CAP_PROP_SATURATION:
    258         capture->control.id = V4L2_CID_SATURATION;
    259         break;
    260     case CV_CAP_PROP_HUE:
    261         capture->control.id = V4L2_CID_HUE;
    262         break;
    263     case CV_CAP_PROP_GAIN:
    264         capture->control.id = V4L2_CID_GAIN;
    265         break;
    266     case CV_CAP_PROP_SHARPNESS:
    267         capture->control.id = V4L2_CID_SHARPNESS;
    268         break;
    269     case CV_CAP_PROP_EXPOSURE:
    270         capture->control.id = V4L2_CID_EXPOSURE;
    271         break;
    272     case CV_CAP_PROP_AUTOGAIN:
    273         capture->control.id = V4L2_CID_AUTOGAIN;
    274         break;
    275     case CV_CAP_PROP_AWB:
    276         capture->control.id = V4L2_CID_AUTO_WHITE_BALANCE;
    277         break;
    278     default:
    279         fprintf(stderr,
    280                 "HIGHGUI ERROR: V4L2: setting property #%d is not supported\n",
    281                 property_id);
    282         return -1;
    283     }
    284 
    285     /* get the min and max values */
    286     if (-1 == xioctl (device,VIDIOC_G_CTRL, &capture->control)) {
    287 //          perror ("VIDIOC_G_CTRL for getting min/max values");
    288           return -1;
    289     }
    290 
    291     /* get the min/max values */
    292     switch (property_id) {
    293 
    294     case CV_CAP_PROP_BRIGHTNESS:
    295         v4l2_min = capture->v4l2_brightness_min;
    296         v4l2_max = capture->v4l2_brightness_max;
    297         break;
    298     case CV_CAP_PROP_CONTRAST:
    299         v4l2_min = capture->v4l2_contrast_min;
    300         v4l2_max = capture->v4l2_contrast_max;
    301         break;
    302     case CV_CAP_PROP_SATURATION:
    303         v4l2_min = capture->v4l2_saturation_min;
    304         v4l2_max = capture->v4l2_saturation_max;
    305         break;
    306     case CV_CAP_PROP_HUE:
    307         v4l2_min = capture->v4l2_hue_min;
    308         v4l2_max = capture->v4l2_hue_max;
    309         break;
    310     case CV_CAP_PROP_GAIN:
    311         v4l2_min = capture->v4l2_gain_min;
    312         v4l2_max = capture->v4l2_gain_max;
    313         break;
    314     case CV_CAP_PROP_SHARPNESS:
    315         v4l2_min = capture->v4l2_sharpness_min;
    316         v4l2_max = capture->v4l2_sharpness_max;
    317         break;
    318     case CV_CAP_PROP_EXPOSURE:
    319         v4l2_min = capture->v4l2_exposure_min;
    320         v4l2_max = capture->v4l2_exposure_max;
    321         break;
    322     case CV_CAP_PROP_AUTOGAIN:
    323         v4l2_min = capture->v4l2_autogain_min;
    324         v4l2_max = capture->v4l2_autogain_max;
    325         break;
    326     case CV_CAP_PROP_AWB:
    327         v4l2_min = capture->v4l2_awb_min;
    328         v4l2_max = capture->v4l2_awb_max;
    329         break;
    330     }
    331 
    332     /* initialisations */
    333     CLEAR (capture->control);
    334 
    335     /* set which control we want to set */
    336     switch (property_id) {
    337 
    338     case CV_CAP_PROP_BRIGHTNESS:
    339         capture->control.id = V4L2_CID_BRIGHTNESS;
    340         break;
    341     case CV_CAP_PROP_CONTRAST:
    342         capture->control.id = V4L2_CID_CONTRAST;
    343         break;
    344     case CV_CAP_PROP_SATURATION:
    345         capture->control.id = V4L2_CID_SATURATION;
    346         break;
    347     case CV_CAP_PROP_HUE:
    348         capture->control.id = V4L2_CID_HUE;
    349         break;
    350     case CV_CAP_PROP_GAIN:
    351         capture->control.id = V4L2_CID_GAIN;
    352         break;
    353    case CV_CAP_PROP_SHARPNESS:
    354         capture->control.id = V4L2_CID_SHARPNESS;
    355         break;
    356     case CV_CAP_PROP_EXPOSURE:
    357         capture->control.id = V4L2_CID_EXPOSURE;
    358         break;
    359     case CV_CAP_PROP_AUTOGAIN:
    360         capture->control.id = V4L2_CID_AUTOGAIN;
    361         break;
    362     case CV_CAP_PROP_AWB:
    363         capture->control.id = V4L2_CID_AUTO_WHITE_BALANCE;
    364         break;
    365     default:
    366         fprintf(stderr,
    367                 "HIGHGUI ERROR: V4L2: setting property #%d is not supported\n",
    368                 property_id);
    369         return -1;
    370     }
    371 
    372     /* set the value we want to set to the scaled the value */
    373     capture->control.value = (int)(value * (v4l2_max - v4l2_min) + v4l2_min);
    374 
    375     /* The driver may clamp the value or return ERANGE, ignored here */
    376     if (-1 == xioctl (device,VIDIOC_S_CTRL, &capture->control) && errno != ERANGE) {
    377         perror ("VIDIOC_S_CTRL");
    378         return -1;
    379     }
    380 
    381   /* all was OK */
    382   return 0;
    383 
    384 }
    385 
    386 
    387 int V4LCamera::icvSetVideoSize( CvCaptureCAM_V4L* capture, int w, int h) {
    388 
    389 
    390 
    391     CLEAR (capture->crop);
    392     capture->crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    393     capture->crop.c.left       = 0;
    394     capture->crop.c.top        = 0;
    395     capture->crop.c.height     = h*24;
    396     capture->crop.c.width      = w*24;
     170int V4LCamera::setVideoSize(int width, int height,unsigned long colorspace) {
     171
     172    CLEAR (capture.crop);
     173    capture.crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
     174    capture.crop.c.left       = 0;
     175    capture.crop.c.top        = 0;
     176    capture.crop.c.height     = height*24;
     177    capture.crop.c.width      = width*24;
    397178
    398179    /* set the crop area, but don't exit if the device don't support croping */
    399     xioctl (device, VIDIOC_S_CROP, &capture->crop);
    400 
    401     CLEAR (capture->form);
    402     capture->form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    403 
    404     /* read the current setting, mainly to retreive the pixelformat information */
    405     xioctl (device, VIDIOC_G_FMT, &capture->form);
     180    xioctl (device, VIDIOC_S_CROP, &capture.crop);
     181
     182    CLEAR (capture.form);
     183    capture.form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
     184
     185    /* read the current setting */
     186    xioctl (device, VIDIOC_G_FMT, &capture.form);
    406187
    407188    /* set the values we want to change */
    408     capture->form.fmt.pix.width = w;
    409     capture->form.fmt.pix.height = h;
    410     capture->form.fmt.win.chromakey = 0;
    411     capture->form.fmt.win.field = V4L2_FIELD_ANY;
    412     capture->form.fmt.win.clips = 0;
    413     capture->form.fmt.win.clipcount = 0;
    414     capture->form.fmt.pix.field = V4L2_FIELD_ANY;
    415 
    416     /* ask the device to change the size
    417      * don't test if the set of the size is ok, because some device
    418      * don't allow changing the size, and we will get the real size
    419      * later */
    420     xioctl (device, VIDIOC_S_FMT, &capture->form);
    421 
    422     /* try to set framerate to 30 fps */
    423     struct v4l2_streamparm setfps; 
    424     memset (&setfps, 0, sizeof(struct v4l2_streamparm));
    425     setfps.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    426     setfps.parm.capture.timeperframe.numerator = 1;
    427     setfps.parm.capture.timeperframe.denominator = 30;
    428     xioctl (device, VIDIOC_S_PARM, &setfps);
    429 
    430     /* we need to re-initialize some things, like buffers, because the size has
    431      * changed */
    432     capture->FirstCapture = 1;
     189    capture.form.fmt.pix.width = width;
     190    capture.form.fmt.pix.height = height;
     191    capture.form.fmt.win.chromakey = 0;
     192    capture.form.fmt.win.field = V4L2_FIELD_ANY;
     193    capture.form.fmt.win.clips = 0;
     194    capture.form.fmt.win.clipcount = 0;
     195    capture.form.fmt.pix.field = V4L2_FIELD_ANY;
     196    capture.form.fmt.pix.pixelformat = colorspace;
     197
     198    /* ask the device to change the size*/
     199    if (-1 == xioctl (device, VIDIOC_S_FMT, &capture.form)) {
     200      Thread::Err("Could not set specifics of capture window.\n");
     201      return -1;
     202    }
    433203
    434204    /* Get window info again, to get the real value */
    435     if (-1 == xioctl (device, VIDIOC_G_FMT, &capture->form))
    436     {
    437       fprintf(stderr, "HIGHGUI ERROR: V4L/V4L2: Could not obtain specifics of capture window.\n\n");
    438 
    439      
    440 
    441       return 0;
    442     }
    443 
     205    if (-1 == xioctl (device, VIDIOC_G_FMT, &capture.form)) {
     206      Thread::Err("Could not obtain specifics of capture window.\n");
     207      return -1;
     208    }
    444209    return 0;
    445 
    446  
    447 
    448    
    449   return 0;
    450 
    451 }
    452 int V4LCamera::try_palette_v4l2(CvCaptureCAM_V4L* capture, unsigned long colorspace)
    453 {
    454   CLEAR (capture->form);
    455 
    456   capture->form.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    457   capture->form.fmt.pix.pixelformat = colorspace;
    458   capture->form.fmt.pix.field       = V4L2_FIELD_ANY;
    459   capture->form.fmt.pix.width = DEFAULT_V4L_WIDTH;
    460   capture->form.fmt.pix.height = DEFAULT_V4L_HEIGHT;
    461  
    462   if (-1 == xioctl (device, VIDIOC_S_FMT, &capture->form))
    463       return -1;
    464 
    465  
    466   if (colorspace != capture->form.fmt.pix.pixelformat)
    467     return -1;
    468   else
    469     return 0;
    470 }
    471 int V4LCamera::autosetup_capture_mode_v4l2(CvCaptureCAM_V4L* capture)
    472 {
    473   if (try_palette_v4l2(capture, V4L2_PIX_FMT_YUYV) == 0) {
    474     PALETTE_YUYV = 1;
    475   } else if (try_palette_v4l2(capture, V4L2_PIX_FMT_UYVY) == 0) {
    476     PALETTE_UYVY = 1;
    477   } else {
    478         fprintf(stderr, "HIGHGUI ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV\n");
    479    
    480     return -1;
    481   }
    482  
    483   return 0;
    484210
    485211}
     
    668394}
    669395*/
    670 int V4LCamera::v4l2_alloc_buffers (CvCaptureCAM_V4L *capture, char *deviceName)
    671 {
    672    CLEAR (capture->req);
     396int V4LCamera::allocBuffers() {
     397   CLEAR (capture.req);
    673398   
    674399   unsigned int buffer_number = DEFAULT_V4L_BUFFERS;
     
    676401   try_again:
    677402   
    678    capture->req.count = buffer_number;
    679    capture->req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    680    capture->req.memory = V4L2_MEMORY_MMAP;
    681 
    682    if (-1 == xioctl (device, VIDIOC_REQBUFS, &capture->req))
    683    {
    684        if (EINVAL == errno)
    685        {
    686          fprintf (stderr, "%s does not support memory mapping\n", deviceName);
     403   capture.req.count = buffer_number;
     404   capture.req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
     405   capture.req.memory = V4L2_MEMORY_MMAP;
     406
     407   if (-1 == xioctl (device, VIDIOC_REQBUFS, &capture.req)) {
     408       if (EINVAL == errno) {
     409         Thread::Warn("does not support memory mapping\n");
    687410       } else {
    688411         perror ("VIDIOC_REQBUFS");
    689412       }
    690413       /* free capture, and returns an error code */
    691        
    692414       return -1;
    693415   }
    694416
    695    if (capture->req.count < buffer_number)
    696    {
    697        if (buffer_number == 1)
    698        {
    699            fprintf (stderr, "Insufficient buffer memory on %s\n", deviceName);
    700 
     417   if (capture.req.count < buffer_number) {
     418       if (buffer_number == 1) {
     419           Thread::Warn("Insufficient buffer memory\n");
    701420           /* free capture, and returns an error code */
    702            
    703421           return -1;
    704422       } else {
    705          buffer_number--;
    706          fprintf (stderr, "Insufficient buffer memory on %s -- decreaseing buffers\n", deviceName);
    707          
    708          goto try_again;
     423          buffer_number--;
     424          Thread::Warn("Insufficient buffer memory -- decreaseing buffers\n");
     425          goto try_again;
    709426       }
    710427   }
    711428
    712    for (n_buffers = 0; n_buffers < capture->req.count; ++n_buffers)
    713    {
     429   for (n_buffers = 0; n_buffers < capture.req.count; ++n_buffers) {
    714430       struct v4l2_buffer buf;
    715 
    716431       CLEAR (buf);
    717432
     
    721436
    722437       if (-1 == xioctl (device, VIDIOC_QUERYBUF, &buf)) {
    723            perror ("VIDIOC_QUERYBUF");
    724        
     438           Thread::Warn ("VIDIOC_QUERYBUF error\n");
    725439           /* free capture, and returns an error code */
    726            
    727440           return -1;
    728441       }
    729442
    730        capture->buffers[n_buffers].length = buf.length;
    731        capture->buffers[n_buffers].start =
     443       capture.buffers[n_buffers].length = buf.length;
     444       capture.buffers[n_buffers].start =
    732445         mmap (NULL /* start anywhere */,
    733446               buf.length,
     
    736449               device, buf.m.offset);
    737450
    738        if (MAP_FAILED == capture->buffers[n_buffers].start) {
    739            perror ("mmap");
    740        
     451       if (MAP_FAILED == capture.buffers[n_buffers].start) {
     452           Thread::Warn("mmap error\n");
    741453           /* free capture, and returns an error code */
    742            
    743454           return -1;
    744455       }
    745 
    746 
    747456   }
    748457
    749458   /* Set up Image data */
    750459   /*
    751    cvInitImageHeader( &capture->frame,
    752                       cvSize( capture->captureWindow.width,
    753                               capture->captureWindow.height ),
     460   cvInitImageHeader( &capture.frame,
     461                      cvSize( capture.captureWindow.width,
     462                              capture.captureWindow.height ),
    754463                      IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4 );*/
    755464   /* Allocate space for RGBA data */
    756    capture->imageSize=capture->form.fmt.pix.width*capture->form.fmt.pix.height*2;
    757    capture->imageData = AllocFunction(capture->imageSize);
    758    Printf("cmem allocated %i at %x\n",capture->imageSize,capture->imageData);
     465   capture.imageSize=capture.form.fmt.pix.width*capture.form.fmt.pix.height*2;
     466   capture.imageData = AllocFunction(capture.imageSize);
     467   Printf("cmem allocated %i at %x\n",capture.imageSize,capture.imageData);
    759468   
    760469   return 1;
    761470};
    762 int V4LCamera::cvGrabFrame(CvCaptureCAM_V4L* capture) {
    763 
    764    if (capture->FirstCapture) {
    765       /* Some general initialization must take place the first time through */
    766 
    767       /* This is just a technicality, but all buffers must be filled up before any
    768          staggered SYNC is applied.  SO, filler up. (see V4L HowTo) */
    769 
    770 
    771         v4l2_alloc_buffers (capture, NULL);
    772 
    773         for (capture->bufferIndex = 0;
    774              capture->bufferIndex < ((int)capture->req.count);
    775              ++capture->bufferIndex)
    776         {
    777 
    778           struct v4l2_buffer buf;
    779 
    780           CLEAR (buf);
    781 
    782           buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    783           buf.memory      = V4L2_MEMORY_MMAP;
    784           buf.index       = (unsigned long)capture->bufferIndex;
    785 
    786           if (-1 == xioctl (device, VIDIOC_QBUF, &buf)) {
    787               perror ("VIDIOC_QBUF");
    788               return 0;
    789           }
    790         }
    791 
    792         /* enable the streaming */
    793         capture->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    794         if (-1 == xioctl (device, VIDIOC_STREAMON,
    795                           &capture->type)) {
    796             /* error enabling the stream */
    797             perror ("VIDIOC_STREAMON");
    798             return 0;
    799         }
    800 
    801      
    802 
    803 
    804       /* preparation is ok */
    805       capture->FirstCapture = 0;
    806    }
    807 
    808 
    809 
    810      mainloop_v4l2(capture);
    811 
    812 
    813 
    814    return(1);
    815 }
    816 void V4LCamera::mainloop_v4l2(CvCaptureCAM_V4L* capture) {
     471
     472
     473int V4LCamera::cvGrabFrame(void) {
    817474    unsigned int count;
    818475
     
    848505            }
    849506
    850             if (read_frame_v4l2 (capture))
     507            if (read_frame_v4l2 (&capture))
    851508                break;
    852509        }
    853510    }
    854 }
     511    return(1);
     512}
     513
    855514int V4LCamera::read_frame_v4l2(CvCaptureCAM_V4L* capture) {
    856515    struct v4l2_buffer buf;
     
    891550   return 1;
    892551}
     552
    893553void V4LCamera::Run(void) {
    894554  Time cam_time, new_time, fpsNow, fpsPrev;
     
    897557
    898558  // init image old
    899   if (!cvGrabFrame(&capture)) {
     559  if (!cvGrabFrame()) {
    900560    Printf("Could not grab a frame\n");
    901561  }
     
    904564
    905565  while (!ToBeStopped()) {
    906    
    907    
    908566    // fps counter
    909567    fpsCounter++;
     
    954612    // cam pictures
    955613    cvRetrieveRawFrame(&capture);
    956     if (!cvGrabFrame(&capture)) {
     614    if (!cvGrabFrame()) {
    957615      Printf("Could not grab a frame\n");
    958616    }
     
    1010668
    1011669
    1012     if (PALETTE_YUYV == 1){
    1013          memcpy((char *)capture->imageData,
    1014              (char *)capture->buffers[capture->bufferIndex].start,
    1015              capture->imageSize);
    1016     }
    1017     if (PALETTE_UYVY == 1){
    1018         memcpy((char *)capture->imageData,
    1019              (char *)capture->buffers[capture->bufferIndex].start,
    1020              capture->imageSize);
    1021     }
    1022  
     670   
     671  memcpy((char *)capture->imageData,(char *)capture->buffers[capture->bufferIndex].start,capture->imageSize);
     672   
    1023673 
    1024674
     
    1188838  struct v4l2_queryctrl queryctrl;
    1189839  queryctrl.id = property;
    1190   xioctl (device, VIDIOC_QUERYCTRL,&queryctrl);
     840  if(xioctl (device, VIDIOC_QUERYCTRL,&queryctrl)==-1) {
     841    Thread::Warn("prop %x, VIDIOC_QUERYCTRL failed\n",property);
     842  }
    1191843  int min = queryctrl.minimum;
    1192844  int max = queryctrl.maximum;
     
    1197849  control.id = property;
    1198850  control.value = (int)(value * (max - min) + min);
    1199   xioctl (device,VIDIOC_S_CTRL, &control);
     851  if(xioctl (device,VIDIOC_S_CTRL, &control)==-1) {
     852    Thread::Warn("prop %x, VIDIOC_S_CTRL failed\n",property);
     853  }
    1200854}
    1201855
  • trunk/lib/FlairSensorActuator/src/V4LCamera.h

    r346 r347  
    168168    //int deviceHandle;
    169169    int bufferIndex;
    170     int FirstCapture;
    171170    struct video_capability capability;
    172171    struct video_window     captureWindow;
     
    192191   struct v4l2_queryctrl queryctrl;
    193192   struct v4l2_querymenu querymenu;
    194 
    195    /* V4L2 control variables */
    196    int v4l2_brightness, v4l2_brightness_min, v4l2_brightness_max;
    197    int v4l2_contrast, v4l2_contrast_min, v4l2_contrast_max;
    198    int v4l2_saturation, v4l2_saturation_min, v4l2_saturation_max;
    199    int v4l2_sharpness, v4l2_sharpness_min, v4l2_sharpness_max;
    200    int v4l2_exposure, v4l2_exposure_min, v4l2_exposure_max;
    201    int v4l2_hue, v4l2_hue_min, v4l2_hue_max;
    202    int v4l2_gain, v4l2_gain_min, v4l2_gain_max;
    203    int v4l2_autogain, v4l2_autogain_min, v4l2_autogain_max;
    204    int v4l2_awb, v4l2_awb_min, v4l2_awb_max;
    205 
    206 
    207 
    208193} CvCaptureCAM_V4L;
    209194
    210195CvCaptureCAM_V4L capture;
    211 int init(int width, int height);
    212 int autosetup_capture_mode_v4l2(CvCaptureCAM_V4L* capture);
    213 int icvSetVideoSize( CvCaptureCAM_V4L* capture, int w, int h);
    214 int try_palette_v4l2(CvCaptureCAM_V4L* capture, unsigned long colorspace);
    215 int  PALETTE_YUYV,PALETTE_UYVY;
    216 int setV4LProperty(int property_id, double value );
    217 int icvSetControl (CvCaptureCAM_V4L* capture,int property_id, double value) ;
    218 int cvGrabFrame(CvCaptureCAM_V4L* capture);
    219 int v4l2_alloc_buffers (CvCaptureCAM_V4L *capture, char *deviceName);
    220 void mainloop_v4l2(CvCaptureCAM_V4L* capture);
     196int init(int width, int height,unsigned long colorspace);
     197int setVideoSize(int width, int height,unsigned long colorspace);
     198int cvGrabFrame(void);
     199int allocBuffers (void);
    221200int read_frame_v4l2(CvCaptureCAM_V4L* capture);
    222201void cvRetrieveRawFrame( CvCaptureCAM_V4L* capture);
Note: See TracChangeset for help on using the changeset viewer.