DC1394Grabber2.C

Go to the documentation of this file.
00001 /*!@file Devices/DC1394Grabber2.C FireWire grabber class based on libdc1394 version 2.x */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/DC1394Grabber2.C $
00035 // $Id: DC1394Grabber2.C 14376 2011-01-11 02:44:34Z pez $
00036 //
00037 
00038 #ifndef DEVICES_DC1394GRABBER2_C_DEFINED
00039 #define DEVICES_DC1394GRABBER2_C_DEFINED
00040 
00041 #include "Devices/DC1394Grabber2.H"
00042 
00043 #include "Devices/DeviceOpts.H"
00044 #include "Image/Image.H"
00045 #include "Image/Pixels.H"
00046 #include "Raster/GenericFrame.H"
00047 #include "Transport/TransportOpts.H"
00048 #include "Util/Assert.H"
00049 #include "Util/SimTime.H"
00050 #include "Util/sformat.H"
00051 #include "Video/VideoFrame.H"
00052 
00053 #include <unistd.h>
00054 
00055 // wait when polling for a frame (in us):
00056 #define IEEE1394WAIT 1
00057 
00058 // This code is somewhat inspired from the grab_gray_image.c example
00059 // code provided with the libdc1394 distribution by Gord Peters, and
00060 // from the Coriander source code.
00061 #ifdef HAVE_DC1394V2
00062 
00063 namespace
00064 {
00065   std::string convertToString(dc1394video_mode_t mode)
00066   {
00067     switch (mode)
00068       {
00069       case DC1394_VIDEO_MODE_160x120_YUV444: return "160x120_YUV444";
00070       case DC1394_VIDEO_MODE_320x240_YUV422: return "320x240_YUV422";
00071       case DC1394_VIDEO_MODE_640x480_YUV411: return "640x480_YUV411";
00072       case DC1394_VIDEO_MODE_640x480_YUV422: return "640x480_YUV422";
00073       case DC1394_VIDEO_MODE_640x480_RGB8: return "640x480_RGB8";
00074       case DC1394_VIDEO_MODE_640x480_MONO8: return "640x480_MONO8";
00075       case DC1394_VIDEO_MODE_640x480_MONO16: return "640x480_MONO16";
00076       case DC1394_VIDEO_MODE_800x600_YUV422: return "800x600_YUV422";
00077       case DC1394_VIDEO_MODE_800x600_RGB8: return "800x600_RGB8";
00078       case DC1394_VIDEO_MODE_800x600_MONO8: return "800x600_MONO8";
00079       case DC1394_VIDEO_MODE_1024x768_YUV422: return "1024x768_YUV422";
00080       case DC1394_VIDEO_MODE_1024x768_RGB8: return "1024x768_RGB8";
00081       case DC1394_VIDEO_MODE_1024x768_MONO8: return "1024x768_MONO8";
00082       case DC1394_VIDEO_MODE_800x600_MONO16: return "800x600_MONO16";
00083       case DC1394_VIDEO_MODE_1024x768_MONO16: return "1024x768_MONO16";
00084       case DC1394_VIDEO_MODE_1280x960_YUV422: return "1280x960_YUV422";
00085       case DC1394_VIDEO_MODE_1280x960_RGB8: return "1280x960_RGB8";
00086       case DC1394_VIDEO_MODE_1280x960_MONO8: return "1280x960_MONO8";
00087       case DC1394_VIDEO_MODE_1600x1200_YUV422: return "1600x1200_YUV422";
00088       case DC1394_VIDEO_MODE_1600x1200_RGB8: return "1600x1200_RGB8";
00089       case DC1394_VIDEO_MODE_1600x1200_MONO8: return "1600x1200_MONO8";
00090       case DC1394_VIDEO_MODE_1280x960_MONO16: return "1280x960_MONO16";
00091       case DC1394_VIDEO_MODE_1600x1200_MONO16: return "1600x1200_MONO16";
00092       case DC1394_VIDEO_MODE_EXIF: return "EXIF";
00093       case DC1394_VIDEO_MODE_FORMAT7_0: return "FORMAT7_0";
00094       case DC1394_VIDEO_MODE_FORMAT7_1: return "FORMAT7_1";
00095       case DC1394_VIDEO_MODE_FORMAT7_2: return "FORMAT7_2";
00096       case DC1394_VIDEO_MODE_FORMAT7_3: return "FORMAT7_3";
00097       case DC1394_VIDEO_MODE_FORMAT7_4: return "FORMAT7_4";
00098       case DC1394_VIDEO_MODE_FORMAT7_5: return "FORMAT7_5";
00099       case DC1394_VIDEO_MODE_FORMAT7_6: return "FORMAT7_6";
00100       case DC1394_VIDEO_MODE_FORMAT7_7: return "FORMAT7_7";
00101       default: break;
00102       }
00103     return "unknown";
00104   }
00105 
00106   std::string convertToString(dc1394framerate_t rate)
00107   {
00108     switch (rate)
00109       {
00110       case DC1394_FRAMERATE_1_875: return "1.875 fps";
00111       case DC1394_FRAMERATE_3_75: return "3.75 fps";
00112       case DC1394_FRAMERATE_7_5: return "7.5 fps";
00113       case DC1394_FRAMERATE_15: return "15 fps";
00114       case DC1394_FRAMERATE_30: return "30 fps";
00115       case DC1394_FRAMERATE_60: return "60 fps";
00116       case DC1394_FRAMERATE_120: return "120 fps";
00117       case DC1394_FRAMERATE_240: return "240 fps";
00118       default: break;
00119       }
00120     return "unknown";
00121   }
00122 
00123   std::string convertToString(dc1394speed_t speed)
00124   {
00125     switch (speed)
00126       {
00127       case DC1394_ISO_SPEED_100: return "100";
00128       case DC1394_ISO_SPEED_200: return "200";
00129       case DC1394_ISO_SPEED_400: return "400";
00130       case DC1394_ISO_SPEED_800: return "800";
00131       case DC1394_ISO_SPEED_1600: return "1600";
00132       case DC1394_ISO_SPEED_3200: return "3200";
00133       default: break;
00134       }
00135     return "unknown";
00136   }
00137 
00138   struct CameraModeInfo
00139   {
00140     VideoFormat vidfmt;
00141     int w;
00142     int h;
00143     dc1394video_mode_t dc1394mode;
00144     dc1394speed_t dc1394speed;
00145   };
00146 
00147   // Figure out which of the IEEE1394 grab modes to use. We don't
00148   // support the various IEEE1394 MONO16 modes.
00149   void find1394mode(const VideoFormat vidfmt, const Dims& dims,
00150                     dc1394video_mode_t* dc1394mode,
00151                     dc1394speed_t* dc1394speed)
00152   {
00153     static const CameraModeInfo modes[] =
00154       {
00155         { VIDFMT_YUV444,  160,  120, DC1394_VIDEO_MODE_160x120_YUV444   , DC1394_ISO_SPEED_400 },
00156         { VIDFMT_YUV422,  320,  240, DC1394_VIDEO_MODE_320x240_YUV422   , DC1394_ISO_SPEED_400 },
00157         { VIDFMT_YUV411,  640,  480, DC1394_VIDEO_MODE_640x480_YUV411   , DC1394_ISO_SPEED_400 },
00158         { VIDFMT_YUV422,  640,  480, DC1394_VIDEO_MODE_640x480_YUV422   , DC1394_ISO_SPEED_400 },
00159         { VIDFMT_RGB24,   640,  480, DC1394_VIDEO_MODE_640x480_RGB8     , DC1394_ISO_SPEED_400 },
00160         { VIDFMT_GREY,    640,  480, DC1394_VIDEO_MODE_640x480_MONO8    , DC1394_ISO_SPEED_400 },
00161         { VIDFMT_YUV422,  800,  600, DC1394_VIDEO_MODE_800x600_YUV422   , DC1394_ISO_SPEED_800 },
00162         { VIDFMT_RGB24,   800,  600, DC1394_VIDEO_MODE_800x600_RGB8     , DC1394_ISO_SPEED_800 },
00163         { VIDFMT_GREY,    800,  600, DC1394_VIDEO_MODE_800x600_MONO8    , DC1394_ISO_SPEED_800 },
00164         { VIDFMT_YUV422, 1024,  768, DC1394_VIDEO_MODE_1024x768_YUV422  , DC1394_ISO_SPEED_800 },
00165         { VIDFMT_RGB24,  1024,  768, DC1394_VIDEO_MODE_1024x768_RGB8    , DC1394_ISO_SPEED_800 },
00166         { VIDFMT_GREY,   1024,  768, DC1394_VIDEO_MODE_1024x768_MONO8   , DC1394_ISO_SPEED_800 },
00167         { VIDFMT_YUV422, 1280,  960, DC1394_VIDEO_MODE_1280x960_YUV422  , DC1394_ISO_SPEED_800 },
00168         { VIDFMT_RGB24,  1280,  960, DC1394_VIDEO_MODE_1280x960_RGB8    , DC1394_ISO_SPEED_800 },
00169         { VIDFMT_GREY,   1280,  960, DC1394_VIDEO_MODE_1280x960_MONO8   , DC1394_ISO_SPEED_800 },
00170         { VIDFMT_YUV422, 1600, 1200, DC1394_VIDEO_MODE_1600x1200_YUV422 , DC1394_ISO_SPEED_800 },
00171         { VIDFMT_RGB24,  1600, 1200, DC1394_VIDEO_MODE_1600x1200_RGB8   , DC1394_ISO_SPEED_800 },
00172         { VIDFMT_GREY,   1600, 1200, DC1394_VIDEO_MODE_1600x1200_MONO8  , DC1394_ISO_SPEED_800 }
00173       };
00174 
00175     for (size_t i = 0; i < sizeof(modes) / sizeof(CameraModeInfo); ++i)
00176       {
00177         if (modes[i].vidfmt == vidfmt
00178             && modes[i].w == dims.w()
00179             && modes[i].h == dims.h())
00180           {
00181             *dc1394mode = modes[i].dc1394mode;
00182             *dc1394speed = modes[i].dc1394speed;
00183             return;
00184           }
00185       }
00186 
00187     std::string msg =
00188       sformat("Unsupported resolution/format combination %s @ %dx%d"
00189               "\nSupported combinations are:",
00190               convertToString(vidfmt).c_str(), dims.w(), dims.h());
00191     for (size_t i = 0; i < sizeof(modes) / sizeof(CameraModeInfo); ++i)
00192       {
00193         msg += sformat("\n\t%7s @ %dx%d",
00194                        convertToString(modes[i].vidfmt).c_str(),
00195                        modes[i].w, modes[i].h);
00196       }
00197 
00198     LFATAL("%s", msg.c_str());
00199   }
00200 
00201   struct FramerateInfo
00202   {
00203     float fps;
00204     dc1394framerate_t dc1394framerate;
00205   };
00206 
00207   // Figure out which framerate to use
00208   dc1394framerate_t find1394framerate(const float fps)
00209   {
00210     static const FramerateInfo framerates[] =
00211       {
00212         { 1.875F, DC1394_FRAMERATE_1_875 },
00213         { 3.75F, DC1394_FRAMERATE_3_75 },
00214         { 7.5F, DC1394_FRAMERATE_7_5 },
00215         { 15.0F, DC1394_FRAMERATE_15 },
00216         { 30.0F, DC1394_FRAMERATE_30 },
00217         { 60.0F, DC1394_FRAMERATE_60 },
00218         { 120.0F, DC1394_FRAMERATE_120 },
00219         { 240.0F, DC1394_FRAMERATE_240 }
00220       };
00221 
00222     for (size_t i = 0; i < sizeof(framerates) / sizeof(FramerateInfo); ++i)
00223       if (framerates[i].fps == fps)
00224         return framerates[i].dc1394framerate;
00225 
00226     std::string msg =
00227       sformat("Unsupported framerate %f fps"
00228               "\nSupported framerates are:", fps);
00229     for (size_t i = 0; i < sizeof(framerates) / sizeof(FramerateInfo); ++i)
00230       msg += sformat("\n\t%.3f fps", framerates[i].fps);
00231 
00232     LFATAL("%s", msg.c_str());
00233     /* can't happen */ return (dc1394framerate_t)-1;
00234   }
00235 
00236 }
00237 #endif
00238 
00239 // ######################################################################
00240 DC1394Grabber2::DC1394Grabber2(OptionManager& mgr,
00241                                const std::string& descrName,
00242                                const std::string& tagName) :
00243   FrameIstream(mgr, descrName, tagName),
00244   // NOTE that contrary to the common case, we give USE_MY_VAL here
00245   // when we construct the OModelParam objects; that means that we
00246   // push our values into the ModelManager as the new default values,
00247   // rather than having our param take its value from the
00248   // ModelManager's default
00249   itsShowInputDetails(&OPT_ShowInputDetails, this),
00250   itsDevName(&OPT_FrameGrabberDevice, this, "/dev/video1394/0", USE_MY_VAL), // device
00251   itsChannel(&OPT_FrameGrabberChannel, this, 0, USE_MY_VAL), // first firewire card
00252   itsSubChan(&OPT_FrameGrabberSubChan, this, 0, USE_MY_VAL),
00253   itsDims(&OPT_FrameGrabberDims, this, Dims(320, 240), USE_MY_VAL),
00254   itsGrabMode(&OPT_FrameGrabberMode, this, VIDFMT_YUV422, USE_MY_VAL), // grab mode
00255   itsByteSwap(&OPT_FrameGrabberByteSwap, this, false, USE_MY_VAL), // no byte-swapping
00256   itsFPS(&OPT_FrameGrabberFPS, this, 30.0, USE_MY_VAL),
00257   itsNbuf(&OPT_FrameGrabberNbuf, this, 4, USE_MY_VAL), // number of grab buffers
00258   itsBrightness(&OPT_FrameGrabberBrightness, this, 32768, USE_MY_VAL | ALLOW_ONLINE_CHANGES),
00259   itsHue(&OPT_FrameGrabberHue, this, 32768, USE_MY_VAL | ALLOW_ONLINE_CHANGES),
00260   itsSaturation(&OPT_FrameGrabberSaturation, this, 90, USE_MY_VAL | ALLOW_ONLINE_CHANGES),
00261   itsExposure(&OPT_FrameGrabberExposure, this, 511, USE_MY_VAL | ALLOW_ONLINE_CHANGES),
00262   itsSharpness(&OPT_FrameGrabberSharpness, this, 80, USE_MY_VAL | ALLOW_ONLINE_CHANGES),
00263   itsWhiteBalBU(&OPT_FrameGrabberWhiteBalBU, this, 95, USE_MY_VAL | ALLOW_ONLINE_CHANGES),
00264   itsWhiteBalRV(&OPT_FrameGrabberWhiteBalRV, this, 87, USE_MY_VAL | ALLOW_ONLINE_CHANGES),
00265   itsGamma(&OPT_FrameGrabberGamma, this, 1, USE_MY_VAL | ALLOW_ONLINE_CHANGES),
00266   itsShutter(&OPT_FrameGrabberShutter, this, 6, USE_MY_VAL | ALLOW_ONLINE_CHANGES),
00267   itsGain(&OPT_FrameGrabberGain, this, 87, USE_MY_VAL | ALLOW_ONLINE_CHANGES)
00268 #ifdef HAVE_DC1394V2
00269   , itsDC1394(NULL)
00270   , itsCam(NULL)
00271 #endif
00272 {}
00273 
00274 // ######################################################################
00275 void DC1394Grabber2::start1()
00276 {
00277 #ifndef HAVE_DC1394V2
00278   LFATAL("you must have libdc1394 version 2.x in order to use DC1394Grabber2");
00279 #else
00280   ASSERT(itsDC1394 == 0);
00281 
00282   itsDC1394 = dc1394_new();
00283 
00284   if (itsDC1394 == 0)
00285     LFATAL("dc1394_new() failed");
00286 
00287   {
00288     dc1394camera_list_t* list = 0;
00289     const dc1394error_t err = dc1394_camera_enumerate(itsDC1394, &list);
00290     if (err != DC1394_SUCCESS)
00291       LFATAL("dc1394_camera_enumerate() failed (%s)",
00292              dc1394_error_get_string(err));
00293 
00294     if (list->num == 0)
00295       LFATAL("no cameras found by dc1394_camera_enumerate()");
00296 
00297     itsCam = dc1394_camera_new(itsDC1394, list->ids[0].guid);
00298     if (itsCam == 0)
00299       LFATAL("failed to initialize camera with guid %llx",
00300              (long long unsigned int)list->ids[0].guid);
00301 
00302     LINFO("using the first camera on the bus");
00303 
00304     dc1394_camera_free_list(list);
00305   }
00306 
00307   // setup capture
00308   dc1394video_mode_t videomode = dc1394video_mode_t();
00309   dc1394speed_t speed = dc1394speed_t();
00310   find1394mode(itsGrabMode.getVal(), itsDims.getVal(),
00311                &videomode, &speed);
00312 
00313   dc1394error_t code = DC1394_SUCCESS;
00314 
00315   if (speed < DC1394_ISO_SPEED_800)
00316     {
00317       code = dc1394_video_set_operation_mode(itsCam, DC1394_OPERATION_MODE_LEGACY);
00318       if (code != DC1394_SUCCESS) LFATAL("couldn't set camera operation mode to 'legacy'");
00319     }
00320   else
00321     {
00322       code = dc1394_video_set_operation_mode(itsCam, DC1394_OPERATION_MODE_1394B);
00323       if (code != DC1394_SUCCESS) LFATAL("couldn't set camera operation mode to '1394B'");
00324     }
00325 
00326   code = dc1394_video_set_iso_speed(itsCam, speed);
00327   if (code != DC1394_SUCCESS) LFATAL("couldn't set camera ISO speed");
00328 
00329   code = dc1394_video_set_mode(itsCam, videomode);
00330   if (code != DC1394_SUCCESS) LFATAL("couldn't set camera video mode");
00331 
00332   const dc1394framerate_t framerate = find1394framerate(itsFPS.getVal());
00333 
00334   code = dc1394_video_set_framerate(itsCam, framerate);
00335   if (code != DC1394_SUCCESS) LFATAL("couldn't set camera framerate");
00336 
00337   code = dc1394_capture_setup(itsCam, 4, DC1394_CAPTURE_FLAGS_DEFAULT);
00338   if (code != DC1394_SUCCESS) LFATAL("capture setup failed; check settings");
00339 
00340   // set features based on ModelParam values
00341 
00342 #define SET_FEATURE(fval, pname, fname)                                 \
00343   do {                                                                  \
00344     if (dc1394_feature_set_value(itsCam, DC1394_FEATURE_ ## fval,       \
00345                                  pname.getVal()) != DC1394_SUCCESS)     \
00346       LERROR("Unable to set " fname);                                   \
00347   } while (0)
00348 
00349   SET_FEATURE(BRIGHTNESS, itsBrightness, "brightness");
00350   SET_FEATURE(EXPOSURE, itsExposure, "exposure");
00351   SET_FEATURE(SHARPNESS, itsSharpness, "sharpness");
00352   SET_FEATURE(HUE, itsHue, "hue");
00353   SET_FEATURE(SATURATION, itsSaturation, "saturation");
00354   SET_FEATURE(GAMMA, itsGamma, "gamma");
00355   SET_FEATURE(SHUTTER, itsShutter, "shutter");
00356   SET_FEATURE(GAIN, itsGain, "gain");
00357 
00358 #undef SET_FEATURE
00359 
00360   if (dc1394_feature_whitebalance_set_value(itsCam,
00361                                             itsWhiteBalBU.getVal(),
00362                                             itsWhiteBalRV.getVal())
00363       != DC1394_SUCCESS)
00364     LERROR("Unable to set white balance");
00365 
00366   // start ISO transmission
00367   code = dc1394_video_set_transmission(itsCam, DC1394_ON);
00368   if (code != DC1394_SUCCESS) LINFO("unable to start camera iso transmission");
00369 
00370   // report camera features if the user asked for details:
00371   if (itsShowInputDetails.getVal())
00372     {
00373       dc1394_camera_print_info(itsCam, stderr);
00374 
00375       dc1394featureset_t features;
00376       if (dc1394_feature_get_all(itsCam, &features) == DC1394_SUCCESS)
00377         dc1394_feature_print_all(&features, stderr);
00378     }
00379 
00380   {
00381     std::string info = sformat("vendor: %s; model: %s; ", itsCam->vendor, itsCam->model);
00382 
00383     dc1394video_mode_t videomode;
00384     if (dc1394_video_get_mode(itsCam, &videomode) == DC1394_SUCCESS)
00385       info += sformat("video mode: %s; ", convertToString(videomode).c_str());
00386     else
00387       info += "video mode: unknown; ";
00388 
00389     dc1394framerate_t framerate;
00390     if (dc1394_video_get_framerate(itsCam, &framerate) == DC1394_SUCCESS)
00391       info += sformat("framerate: %s; ", convertToString(framerate).c_str());
00392     else
00393       info += "framerate: unknown; ";
00394 
00395     dc1394speed_t speed;
00396     if (dc1394_video_get_iso_speed(itsCam, &speed) == DC1394_SUCCESS)
00397       info += sformat("speed: %s; ", convertToString(speed).c_str());
00398     else
00399       info += "speed: unknown; ";
00400 
00401     uint32_t chan = 0;
00402     if (dc1394_video_get_iso_channel(itsCam, &chan) == DC1394_SUCCESS)
00403       info += sformat("ISO channel: %u; ", (unsigned int) chan);
00404     else
00405       info += "ISO channel: unknown; ";
00406 
00407     LINFO("%s", info.c_str());
00408   }
00409 #endif // HAVE_DC1394V2
00410 }
00411 
00412 // ######################################################################
00413 void DC1394Grabber2::stop2()
00414 {
00415 #ifndef HAVE_DC1394V2
00416   // don't LFATAL() in stop() since it may be called in a destructor chain
00417   LERROR("you must have libdc1394 version 2.x in order to use DC1394Grabber2");
00418 #else
00419   if (itsCam != 0)
00420     {
00421       dc1394_video_set_transmission(itsCam, DC1394_OFF);
00422       dc1394_capture_stop(itsCam);
00423       dc1394_camera_free(itsCam);
00424       itsCam = 0;
00425     }
00426   if (itsDC1394 != 0)
00427     {
00428       dc1394_free(itsDC1394);
00429       itsDC1394 = 0;
00430     }
00431 #endif // HAVE_DC1394V2
00432 }
00433 
00434 // ######################################################################
00435 DC1394Grabber2::~DC1394Grabber2()
00436 {  }
00437 
00438 // ######################################################################
00439 GenericFrameSpec DC1394Grabber2::peekFrameSpec()
00440 {
00441   GenericFrameSpec result;
00442 
00443   result.nativeType = GenericFrame::VIDEO;
00444   result.videoFormat = itsGrabMode.getVal();
00445   result.videoByteSwap = itsByteSwap.getVal();
00446   result.dims = itsDims.getVal();
00447   result.floatFlags = 0;
00448 
00449   return result;
00450 }
00451 
00452 // ######################################################################
00453 SimTime DC1394Grabber2::getNaturalFrameTime() const
00454 {
00455   return SimTime::HERTZ(itsFPS.getVal());
00456 }
00457 
00458 // ######################################################################
00459 GenericFrame DC1394Grabber2::readFrame()
00460 {
00461   return GenericFrame(this->grabRaw());
00462 }
00463 
00464 // ######################################################################
00465 VideoFrame DC1394Grabber2::grabRaw()
00466 {
00467 #ifndef HAVE_DC1394V2
00468   LFATAL("you must have libdc1394 version 2.x in order to use DC1394Grabber2");
00469   /* can't happen */ return VideoFrame();
00470 #else
00471   dc1394video_frame_t* frame = 0;
00472 
00473   int code = dc1394_capture_dequeue(itsCam, DC1394_CAPTURE_POLICY_WAIT, &frame);
00474   if (code != DC1394_SUCCESS) LFATAL("unable to capture video frame");
00475 
00476   VideoFrame result(frame->image, frame->total_bytes, itsDims.getVal(),
00477                     itsGrabMode.getVal(), itsByteSwap.getVal(), false);
00478 
00479   code = dc1394_capture_enqueue(itsCam, frame);
00480   if (code != DC1394_SUCCESS) LFATAL("couldn't re-enqueue frame");
00481 
00482   return result;
00483 #endif // HAVE_DC1394V2
00484 }
00485 
00486 // ######################################################################
00487 void DC1394Grabber2::paramChanged(ModelParamBase* const param,
00488                                   const bool valueChanged,
00489                                   ParamClient::ChangeStatus* status)
00490 {
00491 #ifndef HAVE_DC1394V2
00492   LFATAL("you must have libdc1394 version 2.x in order to use DC1394Grabber2");
00493 #else
00494 
00495   FrameIstream::paramChanged(param, valueChanged, status);
00496 
00497   // just handle online changes here (i.e. changes that come while we
00498   // are already started); if the change happens before start() then
00499   // we will pick up the proper dc1394 settings in start() so we don't
00500   // need to handle the change here
00501 
00502 #define HANDLE_PARAM(FVAL, MODELPARAM, FNAME)           \
00503       if (valueChanged && param == &MODELPARAM)         \
00504         {                                               \
00505           LDEBUG("online change of " FNAME " from %s",  \
00506                  MODELPARAM.getName().c_str());         \
00507           if (dc1394_feature_set_value                  \
00508               (itsCam, DC1394_FEATURE_ ## FVAL,         \
00509                MODELPARAM.getVal())                     \
00510               != DC1394_SUCCESS)                        \
00511             {                                           \
00512               *status = ParamClient::CHANGE_REJECTED;   \
00513               LERROR("Unable to set " FNAME);           \
00514             }                                           \
00515         }
00516 
00517   HANDLE_PARAM(BRIGHTNESS, itsBrightness, "brightness");
00518   HANDLE_PARAM(EXPOSURE, itsExposure, "exposure");
00519   HANDLE_PARAM(SHARPNESS, itsSharpness, "sharpness");
00520   HANDLE_PARAM(HUE, itsHue, "hue");
00521   HANDLE_PARAM(SATURATION, itsSaturation, "saturation");
00522   HANDLE_PARAM(GAMMA, itsGamma, "gamma");
00523   HANDLE_PARAM(SHUTTER, itsShutter, "shutter");
00524   HANDLE_PARAM(GAIN, itsGain, "gain");
00525 
00526 #undef HANDLE_PARAM
00527 
00528   if (valueChanged
00529       && (param == &itsWhiteBalBU || param == &itsWhiteBalRV))
00530     {
00531       LDEBUG("online change of white balance from %s",
00532              param->getName().c_str());
00533 
00534       if (dc1394_feature_whitebalance_set_value
00535           (itsCam, itsWhiteBalBU.getVal(), itsWhiteBalRV.getVal())
00536           != DC1394_SUCCESS)
00537         {
00538           *status = ParamClient::CHANGE_REJECTED;
00539           LERROR("Unable to set white balance");
00540         }
00541     }
00542 
00543 #endif // HAVE_DC1394V2
00544 }
00545 
00546 // ######################################################################
00547 /* So things look consistent in everyone's emacs... */
00548 /* Local Variables: */
00549 /* mode: c++ */
00550 /* indent-tabs-mode: nil */
00551 /* End: */
00552 
00553 #endif // DEVICES_DC1394GRABBER2_C_DEFINED
Generated on Sun May 8 08:40:37 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3