00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <cstddef>
00039 #include <string>
00040
00041 #include "Component/OptionManager.H"
00042 #include "Devices/DeviceOpts.H"
00043 #include "Image/Image.H"
00044 #include "Image/Pixels.H"
00045 #include "Image/MathOps.H"
00046 #include "Raster/GenericFrame.H"
00047 #include "Raster/Raster.H"
00048 #include "Util/Assert.H"
00049 #include "Util/SimTime.H"
00050 #include "Util/sformat.H"
00051 #include "Video/VideoFrame.H"
00052
00053 #include "Devices/XCgrabber.H"
00054
00055
00056 #define XCWAIT 50
00057
00058
00059 XCgrabber::XCgrabber(OptionManager& mgr, const std::string& descrName,
00060 const std::string& tagName, const ParamFlag flags) :
00061 FrameIstream(mgr, descrName, tagName),
00062 itsChannel(&OPT_FrameGrabberChannel, this, 0, USE_MY_VAL),
00063 itsDims(&OPT_FrameGrabberDims, this, Dims(1920, 1080), USE_MY_VAL),
00064 itsOffset(&OPT_FrameGrabberOffset, this, Dims(0, 0), USE_MY_VAL),
00065 itsNbuf(&OPT_FrameGrabberNbuf, this, 4, USE_MY_VAL),
00066 itsGrabMode(&OPT_FrameGrabberMode, this, VIDFMT_BAYER_GB, USE_MY_VAL),
00067 itsByteSwap(&OPT_FrameGrabberByteSwap, this, false, USE_MY_VAL),
00068 itsWhiteBalTarR(&OPT_FrameGrabberWhiteBalTargetR, this, false, USE_MY_VAL),
00069 itsWhiteBalTarG(&OPT_FrameGrabberWhiteBalTargetG, this, false, USE_MY_VAL),
00070 itsWhiteBalTarB(&OPT_FrameGrabberWhiteBalTargetB, this, false, USE_MY_VAL),
00071 itsWhiteBalRefR(&OPT_FrameGrabberWhiteBalReferenceR, this, false, USE_MY_VAL),
00072 itsWhiteBalRefG(&OPT_FrameGrabberWhiteBalReferenceG, this, false, USE_MY_VAL),
00073 itsWhiteBalRefB(&OPT_FrameGrabberWhiteBalReferenceB, this, false, USE_MY_VAL),
00074 itsGamma(&OPT_XCFrameGrabberGamma, this, 1.0, USE_MY_VAL | ALLOW_ONLINE_CHANGES),
00075 itsFPS(&OPT_FrameGrabberFPS, this, 30.0, USE_MY_VAL)
00076 #ifdef HAVE_XCLIB
00077 , itsFormatFile(&OPT_XCFormatFileName, this),
00078 itsCameraOk(false), itsLastBuf(0), itsImgBuf(NULL),
00079 itsStateid(PXMODE_DIGI), itsUnitMap(0), itsPximg(NULL)
00080 #endif
00081 {
00082 #ifdef HAVE_XCLIB
00083 memset(&itsXclib, 0, sizeof(itsXclib));
00084 itsXclib.ddch.len = sizeof(itsXclib);
00085 itsXclib.ddch.mos = XCMOS_LIBS;
00086 #endif
00087 }
00088
00089
00090 void XCgrabber::start1()
00091 {
00092 #ifndef HAVE_XCLIB
00093 LFATAL("you must have XC support and the xclib library in order to use XCgrabber");
00094 #else
00095
00096
00097 itsUnitMap = (1 << itsChannel.getVal());
00098 const int nbuf = itsNbuf.getVal();
00099 itsStateid = PXMODE_DIGI + itsChannel.getVal();
00100 struct xclib::pxvidstate *statep;
00101
00102
00103 if (itsFormatFile.getVal().empty()) {
00104 LINFO("Using default setup configure format");
00105 char* format = (char*)("default");
00106 if (int ret = xclib::xclib_open(&itsXclib, NULL, NULL, format, NULL))
00107 LFATAL("Cannot open XC library [%s]", err(ret));
00108 } else {
00109 LINFO("Using input format file '%s' as configure file", itsFormatFile.getVal().c_str());
00110 char* formatFile = (char*)(itsFormatFile.getVal().c_str());
00111 if (int ret = xclib::xclib_open(&itsXclib, NULL, NULL, NULL, formatFile))
00112 LFATAL("Cannot open XC library [%s]", err(ret));
00113
00114
00115
00116 if (int ret = itsXclib.xcdev.setVideoConfig(&itsXclib.xcdev, itsUnitMap, 0, itsStateid, NULL, NULL))
00117 LERROR("Set video configure error [%s]", err(ret));
00118 if (int ret = itsXclib.xcdev.setCameraConfig(&itsXclib.xcdev, itsUnitMap, 1, itsStateid, NULL, NULL))
00119 LERROR("Set camera config error [%s]", err(ret));
00120 }
00121
00122
00123 struct xclib::pxlibservice *pxlib = &(itsXclib.pxlib);
00124 struct xclib::xcdevservice *xcdev = &(itsXclib.xcdev);
00125
00126
00127
00128 int pixiemask = 0x1;
00129
00130
00131 switch(itsGrabMode.getVal()) {
00132 case VIDFMT_BAYER_GB12: itsBitDepth = 12; itsNumComp = 1; pixiemask = 0x1; break;
00133 case VIDFMT_BAYER_GR12: itsBitDepth = 12; itsNumComp = 1; pixiemask = 0x1; break;
00134 case VIDFMT_BAYER_RG12: itsBitDepth = 12; itsNumComp = 1; pixiemask = 0x1; break;
00135 case VIDFMT_BAYER_BG12: itsBitDepth = 12; itsNumComp = 1; pixiemask = 0x1; break;
00136 case VIDFMT_BAYER_GB: itsBitDepth = 8; itsNumComp = 1; pixiemask = 0x1; break;
00137 case VIDFMT_BAYER_GR: itsBitDepth = 8; itsNumComp = 1; pixiemask = 0x1; break;
00138 case VIDFMT_BAYER_RG: itsBitDepth = 8; itsNumComp = 1; pixiemask = 0x1; break;
00139 case VIDFMT_BAYER_BG: itsBitDepth = 8; itsNumComp = 1; pixiemask = 0x1; break;
00140 case VIDFMT_GREY: itsBitDepth = 8; itsNumComp = 1; pixiemask = 0x1; break;
00141 case VIDFMT_RGB24: itsBitDepth = 8; itsNumComp = 3; pixiemask = 0x7; break;
00142 default: LFATAL("ERROR unsupported video format for the XC grabber");
00143 }
00144
00145
00146 struct xclib::pxdevinfo pxinfo;
00147 memset(&pxinfo, 0, sizeof(pxinfo)); pxinfo.ddch.len = sizeof(pxinfo); pxinfo.ddch.mos = PXMOS_DEVINFO;
00148 if (int ret = itsXclib.pxdev.getDevInfo(&itsXclib.pxdev, itsUnitMap, 0, &pxinfo) != 0)
00149 LERROR("Error trying to get pxdev info [%s]", err(ret));
00150
00151 LINFO("Framegrabber board family %d, model %d, submodel %d", pxinfo.family, pxinfo.model, pxinfo.submodel);
00152 LINFO("Found %d units, frame buffer memory = %.4f Kbytes", pxinfo.nunits, float(pxinfo.memsize) / 1024);
00153 LINFO("Driver ID: %s", pxinfo.driverid);
00154 LINFO("Library ID: %s", pxinfo.libraryid);
00155
00156
00157 struct xclib::xcdevinfo xcinfo;
00158 memset(&xcinfo, 0, sizeof(xcinfo)); xcinfo.ddch.len = sizeof(xcinfo); xcinfo.ddch.mos = XCMOS_DEVINFO;
00159 if (int ret = itsXclib.xcdev.getDevInfo(&itsXclib.xcdev, itsUnitMap, 0, &xcinfo) != 0)
00160 LERROR("Error trying to get xcdev info [%s]", err(ret));
00161 LINFO("PCI Bus %d, virq %d", xcinfo.pcibus, xcinfo.virq);
00162
00163
00164 statep = NULL;
00165 if (int ret = pxlib->allocStateCopy(pxlib, 0, 0, &statep))
00166 LFATAL("Allocate state copy (video state) error [%s]", err(ret));
00167
00168 if (itsFormatFile.getVal().empty() == false) {
00169 if (int ret = pxlib->importStateCopy(pxlib, 0, 0, statep, 0, (char *)(itsFormatFile.getVal().c_str()), NULL))
00170 LFATAL("Import state copy (video state) from '%s' error [%s]", itsFormatFile.getVal().c_str(), err(ret));
00171 } else {
00172 if (int ret = pxlib->initStateCopy(pxlib, 0, 0, statep, &pxinfo, (char*)("default"), PXMODE_DIGI))
00173 LFATAL("Init state copy (video state) error [%s]", err(ret));
00174 }
00175
00176
00177 LINFO("pxvidimage bayerpattern: %d, %d, %d, %d, %d, %d",
00178 statep->vidimage->bp.order,
00179 statep->vidimage->bp.mode,
00180 statep->vidimage->bp.arg[0],
00181 statep->vidimage->bp.arg[1],
00182 statep->vidimage->bp.arg[2],
00183 statep->vidimage->bp.arg[3]);
00184
00185 LINFO("pxvidimage colorspace: %d, %d, %d",
00186 statep->vidimage->cs.order,
00187 statep->vidimage->cs.mode,
00188 int(statep->vidimage->cs.scale));
00189
00190 LINFO("pxvidimage whitebalance: %d, %d, %d",
00191 statep->vidimage->wb.order,
00192 statep->vidimage->wb.mode,
00193 int(statep->vidimage->wb.gamma[0][0]));
00194
00195 LINFO("pxvidimage sharp: %d, %d, %d, %d, %d",
00196 statep->vidimage->sh.order,
00197 statep->vidimage->sh.mode,
00198 statep->vidimage->sh.scale,
00199 statep->vidimage->sh.into[0],
00200 statep->vidimage->sh.from[0]);
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 if (int ret = xcdev->setLiveSeqBuf(xcdev, itsUnitMap, 0, itsStateid, NULL, NULL, 1, nbuf, 1, 0, 1, 0) != 0)
00246 LFATAL("start capture error [%s]; the imaging board cannot work in live mode", err(ret));
00247 LINFO("Live capture starting with %d buffers...", nbuf);
00248
00249
00250 xclib::pxywindow pxywin;
00251 pxywin.nw.x = itsOffset.getVal().w();
00252 pxywin.nw.y = itsOffset.getVal().h();
00253 pxywin.se.x = itsDims.getVal().w() + itsOffset.getVal().w();
00254 pxywin.se.y = itsDims.getVal().h() + itsOffset.getVal().h();
00255
00256
00257 const int dataMode = (itsBitDepth == 8 ? PXDATUINT8 : PXDATUINT16);
00258
00259
00260 LINFO("Allocating %d pximage structs for fast I/O transfers", nbuf);
00261 itsPximg = new struct xclib::pximage*[nbuf];
00262 for (int i = 0; i < nbuf; ++i) {
00263
00264 itsPximg[i] = new (struct xclib::pximage);
00265 if (int ret = pxlib->initPximage(pxlib, itsUnitMap, itsPximg[i], 1, PXHINTFRAME, 0, itsStateid, i+1, 0) < 1)
00266 LFATAL("Cannot allocate pximage %d/%d [%s]", i, nbuf, err(ret));
00267
00268
00269 (void)itsPximg[i]->xwind(itsPximg[i], &pxywin, int('s'));
00270
00271
00272 if (int ret = itsPximg[i]->ioset(itsPximg[i], PXRXSCAN | PXIWRAP, dataMode, pixiemask) < 0)
00273 LFATAL("Cannot setup I/O access for pximage buffer %d/%d [%s]", i, nbuf, err(ret));
00274 }
00275
00276
00277 xclib::pxywindow *pxyw = itsPximg[0]->xwind(itsPximg[0], NULL, 'i');
00278 LINFO("Native frame: [%d .. %d] x [%d .. %d]", pxyw->nw.x, pxyw->se.x, pxyw->nw.y, pxyw->se.y);
00279 pxyw = itsPximg[0]->xwind(itsPximg[0], NULL, 'w');
00280 LINFO("Capture window: [%d .. %d] x [%d .. %d]", pxyw->nw.x, pxyw->se.x, pxyw->nw.y, pxyw->se.y);
00281
00282
00283 itsLastBuf = (xclib::pxbuffer_t)xcdev->getLiveStatus(xcdev, itsUnitMap, 0, PXVIST_DONE | PXVIST_BUFFER);
00284 LINFO("Waiting for grabber to start streaming...");
00285 while (itsLastBuf == 0) {
00286 usleep(XCWAIT);
00287 itsLastBuf = (xclib::pxbuffer_t)xcdev->getLiveStatus(xcdev, itsUnitMap, 0, PXVIST_DONE | PXVIST_BUFFER);
00288 }
00289 LINFO("Grabber streaming ok.");
00290
00291 const unsigned int bufSz = itsDims.getVal().sz() * int(ceil(itsBitDepth / 8) * itsNumComp);
00292 itsImgBuf = (byte*)malloc(bufSz);
00293
00294 itsCameraOk = true;
00295
00296 #endif // HAVE_XCLIB
00297 }
00298
00299
00300 void XCgrabber::stop2()
00301 {
00302 #ifndef HAVE_XCLIB
00303
00304 LERROR("you must have XC support and the xclib library in order to use XCgrabber");
00305 #else
00306
00307 if (itsCameraOk) { xclib::xclib_close(&itsXclib); itsCameraOk = false; }
00308 if (itsImgBuf) { free(itsImgBuf); itsImgBuf = NULL; }
00309 if (itsPximg) {
00310 for (int i = 0; i < itsNbuf.getVal(); ++i) delete itsPximg[i];
00311 delete [] itsPximg;
00312 itsPximg = NULL;
00313 }
00314
00315 #endif // HAVE_XCLIB
00316 }
00317
00318
00319 XCgrabber::~XCgrabber()
00320 { }
00321
00322
00323 GenericFrameSpec XCgrabber::peekFrameSpec()
00324 {
00325 GenericFrameSpec result;
00326 result.nativeType = GenericFrame::VIDEO;
00327 result.videoFormat = itsGrabMode.getVal();
00328 result.videoByteSwap = itsByteSwap.getVal();
00329 result.dims = itsDims.getVal();
00330 result.floatFlags = 0;
00331 return result;
00332 }
00333
00334
00335 SimTime XCgrabber::getNaturalFrameTime() const
00336 {
00337 return SimTime::HERTZ(itsFPS.getVal());
00338 }
00339
00340
00341 GenericFrame XCgrabber::readFrame()
00342 {
00343 return GenericFrame(this->grabRaw());
00344 }
00345
00346
00347 VideoFrame XCgrabber::grabRaw()
00348 {
00349 #ifndef HAVE_XCLIB
00350 LFATAL("you must have XC support and the xclib library in order to use XCgrabber");
00351 return VideoFrame();
00352 #else
00353 ASSERT(itsCameraOk);
00354
00355
00356 struct xclib::xcdevservice *xcdev = &(itsXclib.xcdev);
00357
00358
00359 xclib::pxbuffer_t bufferID =
00360 (xclib::pxbuffer_t)xcdev->getLiveStatus(xcdev, itsUnitMap, 0, PXVIST_DONE | PXVIST_BUFFER);
00361 while (bufferID == itsLastBuf) {
00362 bufferID = (xclib::pxbuffer_t)xcdev->getLiveStatus(xcdev, itsUnitMap, 0, PXVIST_DONE | PXVIST_BUFFER);
00363 usleep(XCWAIT);
00364 }
00365
00366 if (bufferID != (itsLastBuf % itsNbuf.getVal()) + 1)
00367 LERROR("Buffer mis-order (dropped frame)! last buf = %d, curr buf = %d", (int)itsLastBuf, (int)bufferID);
00368
00369 pthread_mutex_lock(&qmutex_buf);
00370 itsLastBuf = bufferID;
00371 pthread_mutex_unlock(&qmutex_buf);
00372
00373 const unsigned int bufSz = itsDims.getVal().sz() * int(ceil(itsBitDepth / 8)) * itsNumComp;
00374 const unsigned int imgSz = itsDims.getVal().sz();
00375
00376 unsigned int readsz = itsPximg[bufferID-1]->ioread(itsPximg[bufferID-1], PXRXSCAN | PXIWRAP, itsImgBuf, bufSz, 0, 0);
00377 if (readsz != imgSz) {
00378 LFATAL("Error in reading frame buffer (size error), got %d, expected size = %d", readsz, imgSz);
00379 return VideoFrame();
00380 }
00381
00382 return VideoFrame(itsImgBuf, bufSz, itsDims.getVal(), itsGrabMode.getVal(), itsByteSwap.getVal(), false);
00383
00384 #endif // HAVE_XCLIB
00385 }
00386
00387
00388 void XCgrabber::paramChanged(ModelParamBase* const param,
00389 const bool valueChanged,
00390 ParamClient::ChangeStatus* status)
00391 {
00392 #ifndef HAVE_XCLIB
00393 LFATAL("you must have XC support and the xclib library in order to use XCgrabber");
00394 #else
00395 FrameIstream::paramChanged(param, valueChanged, status);
00396 #endif // HAVE_XCLIB
00397 }
00398
00399
00400 #ifdef HAVE_XCLIB
00401 xclib::pxbuffer_t XCgrabber::getCurrBufID()
00402 {
00403 xclib::pxbuffer_t tmp;
00404 pthread_mutex_lock(&qmutex_buf);
00405 tmp = itsLastBuf;
00406 pthread_mutex_unlock(&qmutex_buf);
00407 return tmp;
00408 }
00409 #endif
00410
00411
00412 const char* XCgrabber::err(const int errcode)
00413 {
00414 #ifdef HAVE_XCLIB
00415 return itsXclib.pxaux.errorCodeString(&itsXclib.pxaux, errcode);
00416 #else
00417 return NULL;
00418 #endif
00419 }
00420
00421
00422
00423
00424
00425
00426