CtVideo.cpp 16.1 KB
Newer Older
1
#include <list>
2
#include "HwVideoCtrlObj.h"
3
#include "CtVideo.h"
4
#include "CtAcquisition.h"
5
#include "CtImage.h"
6

7
8
9
10
11
12
#include "PoolThreadMgr.h"
#include "SinkTask.h"
#include "TaskMgr.h"
#include "Binning.h"
#include "SoftRoi.h"

13
using namespace lima;
14
15
enum ParModifyMask
  {
16
17
18
19
20
    PARMODIFYMASK_EXPOSURE 	= 1U << 0,
    PARMODIFYMASK_GAIN 		= 1U << 1,
    PARMODIFYMASK_MODE	 	= 1U << 2,
    PARMODIFYMASK_ROI 		= 1U << 3,
    PARMODIFYMASK_BIN 		= 1U << 4
21
  };
22
23
24
25
26
27
28
29
30
31
32
33
34
// --- CtVideo::Data2Imagetask
class CtVideo::_Data2ImageTask : public SinkTaskBase
{
  DEB_CLASS_NAMESPC(DebModControl,"Data to image","Control");
public:
  _Data2ImageTask(CtVideo &cnt) : SinkTaskBase(),m_cnt(cnt) {}

  virtual void process(Data &aData)
  {
    DEB_MEMBER_FUNCT();
    DEB_PARAM() << DEB_VAR1(aData);
    
    AutoMutex aLock(m_cnt.m_cond.mutex());
35
36
37
38
    VideoImage *anImage = m_cnt.m_write_image;
    while(anImage->inused)
      m_cnt.m_cond.wait();
    anImage->inused = -1;	// Write Mode
39
40
41
    aLock.unlock();
    
    
42
43
44
45
    data2Image(aData,*anImage);

    aLock.lock();
    anImage->inused = 0;	// Unlock
46
47
    ++m_cnt.m_image_counter;

48
49
50
51
52
53
54
    // if read Image is not use, swap
    if(!m_cnt.m_read_image->inused)
      {
	m_cnt.m_write_image = m_cnt.m_read_image;
	m_cnt.m_write_image->frameNumber = -1;
	m_cnt.m_read_image = anImage;

55
56
57
58
59
60
61
	if(m_cnt.m_image_callback)
	  {
	    CtVideo::Image anImageWrapper(&m_cnt,anImage);
	    ImageCallback *cb = m_cnt.m_image_callback;
	    aLock.unlock();
	    cb->newImage(anImageWrapper);
	  }
62
      }
63
64
65
66
  }
private:
  CtVideo &m_cnt;
};
67

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// --- CtVideo::_Data2ImageCBK 
class CtVideo::_Data2ImageCBK : public TaskEventCallback
{
  DEB_CLASS_NAMESPC(DebModControl,"CtVideo::_Data2ImageCBK","Control");
public:
  _Data2ImageCBK(CtVideo &aCtVideo) : m_video(aCtVideo) {}
  virtual void finnished(Data &aData)
  {
    DEB_MEMBER_FUNCT();
    DEB_PARAM() << DEB_VAR1(aData);

    m_video._data2image_finnished(aData);
  }
private:
  CtVideo &m_video;
};
84
85
86
87
88
89
// --- CtVideo::_InternalImageCBK
class CtVideo::_InternalImageCBK : public HwVideoCtrlObj::ImageCallback
{
public:
  _InternalImageCBK(CtVideo &video) : 
    HwVideoCtrlObj::ImageCallback(),
90
    m_video(video),m_buffer(video.m_video->getBuffer()) {}
91
92
93
94
95
96
97
98
99
100
101
102
103
  virtual ~_InternalImageCBK() {}
  
protected:
  virtual bool newImage(char * data,int width,int height,VideoMode mode);
private:
  CtVideo& 		m_video;
  StdBufferCbMgr& 	m_buffer;
};

bool CtVideo::_InternalImageCBK::newImage(char * data,int width,int height,VideoMode mode)
{
  bool liveFlag;
  AutoMutex aLock(m_video.m_cond.mutex());
104
105
  if(m_video.m_stopping_live) return false;

106
107
108
109
110
111
112
113
114
115
116
117
118
119
  liveFlag = m_video.m_pars.live;
  int image_counter = m_video.m_image_counter + 1;
  aLock.unlock();
    
  if(!liveFlag)			// Classic acquisition
    {
      int buffer_nb, concat_frame_nb;
      m_buffer.acqFrameNb2BufferNb(image_counter, buffer_nb, 
				     concat_frame_nb);
      void *ptr = m_buffer.getBufferPtr(buffer_nb,
					concat_frame_nb);
      try
	{
	  lima::image2YUV((unsigned char*)data,width,height,mode,(unsigned char*)ptr);
120
121
122
	  HwFrameInfoType frame_info;
	  frame_info.acq_frame_nb = image_counter;
	  m_buffer.newFrameReady(frame_info);
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	}
      // Should happen only when video format is not implemented (Debug mode)
      catch(Exception &exc)
	{
	  std::cerr << exc.getErrMsg() << std::endl;
	  return false;
	}
    }

  aLock.lock();
  ++m_video.m_image_counter;
  VideoImage *anImage = m_video.m_write_image;
  if(anImage->inused) return true;			// Skip it (Should never happen!)
  anImage->inused = -1;		// Write Mode
  aLock.unlock();

  anImage->setParams(image_counter,width,height,mode);
  memcpy(anImage->buffer,data,int(anImage->size()));

  aLock.lock();
  anImage->inused = 0;
  // if read Image is not use, swap
  if(!m_video.m_read_image->inused)
    {
      m_video.m_write_image = m_video.m_read_image;
      m_video.m_write_image->frameNumber = -1;
      m_video.m_read_image = anImage;
   
      if(m_video.m_image_callback)
	{
	  //TODO should be done in background
	}
    }
  return true;
}
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// --- CtVideo::Image
CtVideo::Image::Image() : m_video(NULL),m_image(NULL) {}

CtVideo::Image::~Image()
{
  if(m_video)
    {
      AutoMutex aLock(m_video->m_cond.mutex());
      --(m_image->inused);
      m_video->m_cond.broadcast();
    }
}

CtVideo::Image::Image(const Image &anOther) :
  m_video(anOther.m_video),
  m_image(anOther.m_image)
{
  if(m_video)
    {
      AutoMutex aLock(m_video->m_cond.mutex());
      ++(m_image->inused);
    }
}

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
CtVideo::Image& CtVideo::Image::operator=(const CtVideo::Image &other)
{
  if(this != &other)
    {
      if(other.m_video)
	{
	  AutoMutex aLock(other.m_video->m_cond.mutex());
	  ++(other.m_image->inused);
	}

      if(m_video)
	{
	  AutoMutex aLock(m_video->m_cond.mutex());
	  --(m_image->inused);
	  m_video->m_cond.broadcast();
	}

      m_video = other.m_video;
      m_image = other.m_image;
    }
  return *this;
}
204
205
206
207
208
209
210
211
212
213
/** @brief an other contructor
 *  This methode should be call under Lock
 */
CtVideo::Image::Image(const CtVideo *video,VideoImage *image) :
  m_video(video),
  m_image(image)
{
  ++(m_image->inused);
}

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const char* CtVideo::Image::buffer() const
{
  return m_image ? m_image->buffer : NULL;
}

int CtVideo::Image::width() const
{
  return m_image ? m_image->width : -1;
}

int CtVideo::Image::height() const
{
  return m_image ? m_image->height : -1;
}

VideoMode CtVideo::Image::mode() const
{
  return m_image ? m_image->mode : Y8;
}

int CtVideo::Image::size() const
{
  return m_image ? int(m_image->size() + 0.5) : 0;
}

int CtVideo::Image::frameNumber() const
{
  return m_image ? m_image->frameNumber : -1;
}
243
// --- CtVideo class
244
CtVideo::CtVideo(CtControl &ct) :
245
  m_pars_modify_mask(0),
246
247
248
249
  m_ready_flag(true),
  m_image_counter(0),
  m_read_image(new VideoImage()),
  m_write_image(new VideoImage()),
250
251
252
  m_image_callback(NULL),
  m_internal_image_callback(NULL),
  m_ct(ct)
253
{
254
  HwInterface *hw = ct.hwInterface();
255
  m_has_video = hw->getHwCtrlObj(m_video);
256
  hw->getHwCtrlObj(m_sync);
257
  m_data_2_image_task = new _Data2ImageTask(*this);
258
259
260

  m_data_2_image_cb = new _Data2ImageCBK(*this);
  m_data_2_image_task->setEventCallback(m_data_2_image_cb);
261
262
263
264

  // Params init
  if(m_has_video)
    {
265
      m_internal_image_callback = new _InternalImageCBK(*this);
266
      m_video->getGain(m_pars.gain);
267
      m_video->registerImageCallback(*m_internal_image_callback);
268
    }
269
270
271
272
}

CtVideo::~CtVideo()
{
273
  m_data_2_image_task->unref();
274
275
276
  m_data_2_image_cb->unref();
  delete m_read_image;
  delete m_write_image;
277
  delete m_internal_image_callback;
278
279
280
281
282
}

// --- parameters
void CtVideo::setParameters(const Parameters &pars)
{
283
284
285
  //All check
  _check_video_mode(pars.mode);

286
  AutoMutex aLock(m_cond.mutex());
287
288
289
290
291
292
293

  if(m_pars.exposure != pars.exposure) 		m_pars_modify_mask |= PARMODIFYMASK_EXPOSURE;
  if(m_pars.gain != pars.gain) 			m_pars_modify_mask |= PARMODIFYMASK_GAIN;
  if(m_pars.mode != pars.mode) 			m_pars_modify_mask |= PARMODIFYMASK_MODE;
  if(m_pars.roi != pars.roi) 			m_pars_modify_mask |= PARMODIFYMASK_ROI;
  if(m_pars.bin != pars.bin) 			m_pars_modify_mask |= PARMODIFYMASK_BIN;

294
  m_pars = pars;
295
  _apply_params();
296
297
298
}
void CtVideo::getParameters(Parameters &pars) const
{
299
  AutoMutex aLock(m_cond.mutex());
300
301
302
303
304
  pars = m_pars;
}

void CtVideo::setLive(bool liveFlag)
{
305
  AutoMutex aLock(m_cond.mutex());
306
307
308

  while(m_stopping_live) m_cond.wait();

309
310
311
312
313
  CtControl::Status status;
  m_ct.getStatus(status);
  if(liveFlag && status.AcquisitionStatus != AcqReady)
    throw LIMA_CTL_EXC(Error, "Can't set live mode if an acquisition is running");

314
  _apply_params(liveFlag);
315
316
  
  if(m_has_video)
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    {
      if(!liveFlag)
	{
	  m_stopping_live = true;
	  aLock.unlock();
	}

      m_video->setLive(liveFlag);

      if(!liveFlag)
	{
	  aLock.lock();
	  m_stopping_live = false;
	  m_cond.signal();
	}
    }
333
334
335
336
337
338
339
340
341
342
343
344
345
  else
    {
      if(liveFlag)
	{
	  CtAcquisition *acqPt = m_ct.acquisition();
	  acqPt->setAcqNbFrames(0);	// Live
	  m_ct.prepareAcq();
	  m_ct.startAcq();
	}
      else
	m_ct.stopAcq();
    }
  m_pars.live = liveFlag;
346
347
348
}
void CtVideo::getLive(bool &liveFlag) const
{
349
  AutoMutex aLock(m_cond.mutex());
350
351
352
  liveFlag = m_pars.live;
}

353
354
355
356
357
358
void CtVideo::setExposure(double anExposure)
{
  AutoMutex aLock(m_cond.mutex());
  m_pars.exposure = anExposure,m_pars_modify_mask |= PARMODIFYMASK_EXPOSURE;
  _apply_params();
}
359

360
361
362
363
364
365
void CtVideo::getExposure(double &anExposure) const
{
  AutoMutex aLock(m_cond.mutex());
  anExposure = m_pars.exposure;
}

366
367
void CtVideo::setGain(double aGain)
{
368
369
  if(!m_has_video)
    throw LIMA_CTL_EXC(Error,"Can't change the gain on Scientific camera");
370
371
372
  if(aGain < 0. || aGain > 1.)
    throw LIMA_CTL_EXC(InvalidValue,"Gain should be between 0. and 1.");

373
  AutoMutex aLock(m_cond.mutex());
374
375
  m_pars.gain = aGain,m_pars_modify_mask |= PARMODIFYMASK_GAIN;
  _apply_params();
376
377
378
}
void CtVideo::getGain(double &aGain) const
{
379
  AutoMutex aLock(m_cond.mutex());
380
381
382
383
384
  aGain = m_pars.gain;
}

void CtVideo::setMode(VideoMode aMode)
{
385
  _check_video_mode(aMode);
386
  AutoMutex aLock(m_cond.mutex());
387
388
  m_pars.mode = aMode,m_pars_modify_mask |= PARMODIFYMASK_MODE;
  _apply_params();
389
390
391
}
void CtVideo::getMode(VideoMode &aMode) const
{
392
  AutoMutex aLock(m_cond.mutex());
393
394
395
396
397
  aMode = m_pars.mode;
}

void CtVideo::setRoi(const Roi &aRoi)
{
398
  AutoMutex aLock(m_cond.mutex());
399
400
  m_pars.roi = aRoi,m_pars_modify_mask |= PARMODIFYMASK_ROI;
  _apply_params();
401
402
403
}
void CtVideo::getRoi(Roi &aRoi) const
{
404
  AutoMutex aLock(m_cond.mutex());
405
406
407
408
409
  aRoi = m_pars.roi;
}

void CtVideo::setBin(const Bin &aBin)
{
410
  AutoMutex aLock(m_cond.mutex());
411
412
  m_pars.bin = aBin,m_pars_modify_mask |= PARMODIFYMASK_BIN;
  _apply_params();
413
414
415
}
void CtVideo::getBin(Bin &aBin) const
{
416
  AutoMutex aLock(m_cond.mutex());
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
417
  aBin = m_pars.bin;
418
419
420
}

// --- images
421
void CtVideo::getLastImage(CtVideo::Image &anImage) const
422
{
423
424
425
426
427
428
429
430
431
432
433
434
435
  AutoMutex aLock(m_cond.mutex());
  if(m_write_image->inused >= 0 && // No writter
     m_write_image->frameNumber >= m_read_image->frameNumber)
    {
      VideoImage *tmp = m_read_image;
      m_read_image = m_write_image;
      m_write_image = tmp;
      m_write_image->frameNumber = -1;
    }
  CtVideo::Image tmpImage(this,m_read_image);
  aLock.unlock();
  
  anImage = tmpImage;
436
}
437

438
439
void CtVideo::getLastImageCounter(int &anImageCounter) const
{
440
441
  AutoMutex aLock(m_cond.mutex());
  anImageCounter = m_image_counter;
442
443
444
445
}

void CtVideo::registerImageCallback(ImageCallback &cb)
{
446
447
448
449
450
451
452
453
454
455
456
457
  DEB_MEMBER_FUNCT();

  AutoMutex aLock(m_cond.mutex());
  DEB_PARAM() << DEB_VAR2(&cb, m_image_callback);
  
  if(m_image_callback)
    {
      DEB_ERROR() << "ImageCallback already registered";
      throw LIMA_CTL_EXC(InvalidValue, "ImageCallback already registered");
    }

  m_image_callback = &cb;
458
}
459

460
461
void CtVideo::unregisterImageCallback(ImageCallback &cb)
{
462
463
464
465
466
467
468
469
470
471
472
  DEB_MEMBER_FUNCT();

  AutoMutex aLock(m_cond.mutex());
  DEB_PARAM() << DEB_VAR2(&cb, m_image_callback);
  if(m_image_callback != &cb)
    {
      DEB_ERROR() << "ImageCallback not registered";
      throw LIMA_CTL_EXC(InvalidValue, "ImageCallback not registered"); 
    }

  m_image_callback = NULL;
473
474
475
476
477
}

// --- video mode
void CtVideo::getSupportedVideoMode(std::list<VideoMode> &modeList)
{
478
479
  DEB_MEMBER_FUNCT();

480
481
482
483
  if(m_has_video)
    m_video->getSupportedVideoMode(modeList);
  else				// TODO
    {
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
      CtImage* image = m_ct.image();
      ImageType anImageType;
      image->getImageType(anImageType);
      switch(anImageType)
	{
	case Bpp8:
	case Bpp8S:
	  modeList.push_back(Y8); break;
	case Bpp10:
	case Bpp10S: 
	case Bpp12:
	case Bpp12S:
	case Bpp14:
	case Bpp14S:
	case Bpp16:
	case Bpp16S:
	  modeList.push_back(Y16); break;
	case Bpp32:
	case Bpp32S:
	  modeList.push_back(Y32); break;
	default:
	  DEB_ERROR() << "Image type not yet managed";
	  throw LIMA_CTL_EXC(Error, "Image type not yet managed");
	}
508
    }
509
}
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558

// --- callback from CtControl
void CtVideo::frameReady(Data &aData)
{
  DEB_MEMBER_FUNCT();
  if(!m_has_video)
    {
      AutoMutex aLock(m_cond.mutex());
      if(m_ready_flag)
	{
	  m_ready_flag = false;
	  Bin aBin = m_pars.bin;
	  Roi aRoi = m_pars.roi;
	  aLock.unlock();
	  _data_2_image(aData,aBin,aRoi);
	}
      else
	m_last_data = aData;
    }
}

void CtVideo::_data_2_image(Data &aData,Bin &aBin,Roi &aRoi)
{
  TaskMgr *anImageCopy = new TaskMgr();
  int runLevel = 0;
  if(aBin.getX() > 1 || aBin.getY() > 1)
    {
      Tasks::Binning *aBinTaskPt = new Tasks::Binning();
      aBinTaskPt->mXFactor = aBin.getX();
      aBinTaskPt->mYFactor = aBin.getY();
      anImageCopy->setLinkTask(runLevel,aBinTaskPt);
      aBinTaskPt->unref();
      ++runLevel;
    }
  if(aRoi.isActive())
    {
      Point topl= aRoi.getTopLeft();
      Point botr= aRoi.getBottomRight();
      Tasks::SoftRoi *aSoftRoiTaskPt = new Tasks::SoftRoi();
      aSoftRoiTaskPt->setRoi(topl.x, botr.x, topl.y, botr.y);
      anImageCopy->setLinkTask(runLevel,aSoftRoiTaskPt);
      aSoftRoiTaskPt->unref();
      ++runLevel;
    }
  anImageCopy->addSinkTask(runLevel,m_data_2_image_task);
  anImageCopy->setInputData(aData);
  
  PoolThreadMgr::get().addProcess(anImageCopy);
}
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574

void CtVideo::_data2image_finnished(Data&)
{
  AutoMutex aLock(m_cond.mutex());
  if(!m_last_data.empty())
    {
      Data aData = m_last_data;
      m_last_data = Data();
      Bin aBin = m_pars.bin;
      Roi aRoi = m_pars.roi;
      aLock.unlock();
      _data_2_image(aData,aBin,aRoi);
    }
  else
    m_ready_flag = true;
}
575

576
void CtVideo::_apply_params(bool aForceLiveFlag)
577
{
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
  if(aForceLiveFlag && !m_pars.live) m_image_counter = -1;
  
  if(aForceLiveFlag || m_pars.live)
    {
      if(m_has_video)
	{
	  if(m_pars_modify_mask & PARMODIFYMASK_MODE)
	    m_video->setVideoMode(m_pars.mode);
	  if(m_pars_modify_mask & PARMODIFYMASK_GAIN)
	    m_video->setGain(m_pars.gain);
	  if(m_pars_modify_mask & PARMODIFYMASK_BIN)
	    {
	      m_hw_bin = m_pars.bin;
	      m_video->checkBin(m_hw_bin);
	      m_video->setBin(m_hw_bin);
	      // Synchronisation with standard acquisition
	      CtImage* image = m_ct.image();
	      image->setBin(m_hw_bin);
	    }
	  if(m_pars_modify_mask & PARMODIFYMASK_ROI)
	    {
	      m_video->checkRoi(m_pars.roi,m_hw_roi);
	      m_video->setRoi(m_hw_roi);
	      // Synchronisation with standard acquisition
	      CtImage* image = m_ct.image();
	      image->setRoi(m_hw_roi);
	    }
	  if(m_pars_modify_mask & PARMODIFYMASK_EXPOSURE)
	    {
607
	      m_sync->setExpTime(m_pars.exposure);
608
609
610
611
612
613
	      CtAcquisition* acquisition = m_ct.acquisition();
	      acquisition->setAcqExpoTime(m_pars.exposure);
	    }
	}
      else			// Scientific Camera
	{
614
615
616
617
618
619
620
621
622
623
624
          if(m_pars_modify_mask & PARMODIFYMASK_EXPOSURE)
            {
              CtAcquisition* acquisition = m_ct.acquisition();
              acquisition->setAcqExpoTime(m_pars.exposure);
	      if(m_pars.live)
	        {
		  m_ct.stopAcq();
		  m_ct.prepareAcq();
		  m_ct.startAcq();
		}
            }
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
	}
      m_pars_modify_mask = 0;	// reset
    }
}

void CtVideo::_read_hw_params()
{
  CtAcquisition *acquisition = m_ct.acquisition();
  acquisition->getAcqExpoTime(m_pars.exposure);

  CtImage* image = m_ct.image();
  image->getRoi(m_hw_roi);
  if(!m_hw_roi.containsRoi(m_pars.roi))
    m_pars.roi = m_hw_roi;
  
  image->getBin(m_hw_bin);
  if(m_hw_bin.getX() > m_pars.bin.getX() ||
     m_hw_bin.getY() > m_pars.bin.getY())
    m_pars.bin = m_hw_bin;
644
}
645
646
647
648
649
650
651
652
653
654
655
656
657
658

void CtVideo::_check_video_mode(VideoMode aMode)
{
  std::list<VideoMode> aModeList;
  getSupportedVideoMode(aModeList);
  bool findMode = false;
  for(std::list<VideoMode>::iterator i = aModeList.begin();
      !findMode && i != aModeList.end();++i)
    findMode = aMode == *i;

  if(!findMode)
    throw LIMA_CTL_EXC(Error,"Video mode is not available for this camera");
}

659
660
661
662
663
/** @brief an Acquisition will start so,
 *  we have to stop video mode
 */ 
void CtVideo::_prepareAcq()
{
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
  AutoMutex aLock(m_cond.mutex());

  while(m_stopping_live) m_cond.wait();

  m_stopping_live = true;
  aLock.unlock();

  if(m_has_video)
    m_video->setLive(false);
  
  aLock.lock();
  m_stopping_live = false;
  m_cond.signal();

  m_pars.live = false;
679
  m_image_counter = -1;
680
  _read_hw_params();
681
}
682
683
684
685
686
687
688
689
690
691
//============================================================================
//			 CtVideo::Parameters
//============================================================================
CtVideo::Parameters::Parameters()
{
  reset();
}
void CtVideo::Parameters::reset()
{
  live = false;
692
693
  exposure = 1.;
  gain = -1.;
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
694
  mode = Y8;
695
696
697
  roi.reset();
  bin.reset();
}