CtVideo.cpp 23.6 KB
Newer Older
Florent Langlois's avatar
Florent Langlois committed
1
#include <list>
2
3
4
5
6
#include "lima/HwVideoCtrlObj.h"
#include "lima/CtVideo.h"
#include "lima/CtAcquisition.h"
#include "lima/CtImage.h"
#include "lima/CtBuffer.h"
Florent Langlois's avatar
Florent Langlois committed
7

8
9
10
11
12
#include "processlib/PoolThreadMgr.h"
#include "processlib/SinkTask.h"
#include "processlib/TaskMgr.h"
#include "processlib/Binning.h"
#include "processlib/SoftRoi.h"
Florent Langlois's avatar
Florent Langlois committed
13
14
15
16
17
18
19
20

using namespace lima;
enum ParModifyMask
  {
    PARMODIFYMASK_EXPOSURE 	= 1U << 0,
    PARMODIFYMASK_GAIN 		= 1U << 1,
    PARMODIFYMASK_MODE	 	= 1U << 2,
    PARMODIFYMASK_ROI 		= 1U << 3,
21
22
    PARMODIFYMASK_BIN 		= 1U << 4,
    PARMODIFYMASK_AUTO_GAIN     = 1U << 5,
Florent Langlois's avatar
Florent Langlois committed
23
24
25
26
27
28
  };
// --- CtVideo::Data2Imagetask
class CtVideo::_Data2ImageTask : public SinkTaskBase
{
  DEB_CLASS_NAMESPC(DebModControl,"Data to image","Control");
public:
29
  _Data2ImageTask(CtVideo &cnt) : SinkTaskBase(),m_nb_buffer(0),m_cnt(cnt) {}
Florent Langlois's avatar
Florent Langlois committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

  virtual void process(Data &aData)
  {
    DEB_MEMBER_FUNCT();
    DEB_PARAM() << DEB_VAR1(aData);
    
    AutoMutex aLock(m_cnt.m_cond.mutex());
    VideoImage *anImage = m_cnt.m_write_image;
    DEB_TRACE() << DEB_VAR1(*anImage);
    while(anImage->inused)
      m_cnt.m_cond.wait();
    anImage->inused = -1;	// Write Mode
    aLock.unlock();
    
    data2Image(aData,*anImage);
    
46
47
48
    //Check if data is still available
    bool still_available = _check_available(aData);

Florent Langlois's avatar
Florent Langlois committed
49
50
    aLock.lock();
    anImage->inused = 0;	// Unlock
51
52
53
54
55
56
57
    if(!still_available)
      {
	DEB_ALWAYS() << "invalidate video copy";
	anImage->frameNumber = -1; // invalidate copy
	return;
      }

Florent Langlois's avatar
Florent Langlois committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    ++m_cnt.m_image_counter;

    // 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;

	if(m_cnt.m_image_callback)
	  {
	    CtVideo::Image anImageWrapper(&m_cnt,anImage);
	    ImageCallback *cb = m_cnt.m_image_callback;
	    aLock.unlock();
	    cb->newImage(anImageWrapper);
	  }
      }
  }
76
77

  long m_nb_buffer;
Florent Langlois's avatar
Florent Langlois committed
78
private:
79
80
81
82
83
84
85
86
87
  inline bool _check_available(Data& aData)
  {
    CtControl::ImageStatus status;
    m_cnt.m_ct.getImageStatus(status);
    
    long offset = status.LastImageAcquired - aData.frameNumber;
    return offset < m_nb_buffer;
  }

Florent Langlois's avatar
Florent Langlois committed
88
89
90
  CtVideo &m_cnt;
};

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class CtVideo::_videoBackgroundCallback : public TaskEventCallback
{
public:
  _videoBackgroundCallback(CtVideo::ImageCallback *cbk,CtVideo::Image& image) :
    TaskEventCallback(),m_cbk(cbk),m_image(image)
  {
  }
  virtual void finished(Data&)
  {
    m_cbk->newImage(m_image);
  }
private:
  CtVideo::ImageCallback*	m_cbk;
  CtVideo::Image		m_image;
};

Florent Langlois's avatar
Florent Langlois committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// --- CtVideo::_Data2ImageCBK 
class CtVideo::_Data2ImageCBK : public TaskEventCallback
{
  DEB_CLASS_NAMESPC(DebModControl,"CtVideo::_Data2ImageCBK","Control");
public:
  _Data2ImageCBK(CtVideo &aCtVideo) : m_video(aCtVideo) {}
  virtual void finished(Data &aData)
  {
    DEB_MEMBER_FUNCT();
    DEB_PARAM() << DEB_VAR1(aData);

    m_video._data2image_finnished(aData);
  }
private:
  CtVideo &m_video;
};
// --- CtVideo::_InternalImageCBK
class CtVideo::_InternalImageCBK : public HwVideoCtrlObj::ImageCallback
{
  DEB_CLASS_NAMESPC(DebModControl,"CtVideo::_InternalImageCBK","Control");
public:
  _InternalImageCBK(CtVideo &video) : 
    HwVideoCtrlObj::ImageCallback(),
    m_video(video),m_buffer(video.m_video->getBuffer()) {}
  virtual ~_InternalImageCBK() {}
  
133
    Timestamp		m_start_time;
Florent Langlois's avatar
Florent Langlois committed
134
135
136
137
138
139
140
141
142
143
144
145
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)
{
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR4((void*)data,width,height,mode);

146
  
Florent Langlois's avatar
Florent Langlois committed
147
148
149
150
  bool liveFlag;
  AutoMutex aLock(m_video.m_cond.mutex());
  if(m_video.m_stopping_live) return false;

151
152
  Timestamp now = Timestamp::now();

Florent Langlois's avatar
Florent Langlois committed
153
  liveFlag = m_video.m_pars.live;
154
  int image_counter = (int) m_video.m_image_counter + 1;
Florent Langlois's avatar
Florent Langlois committed
155
156
157
158
  aLock.unlock();
    
  if(!liveFlag)			// Classic acquisition
    {
159
      void *ptr = m_buffer.getFrameBufferPtr(image_counter);
Florent Langlois's avatar
Florent Langlois committed
160
161
162
163
164
      try
	{
	  lima::image2YUV((unsigned char*)data,width,height,mode,(unsigned char*)ptr);
	  HwFrameInfoType frame_info;
	  frame_info.acq_frame_nb = image_counter;
165
	  frame_info.frame_timestamp = now - m_start_time;
Florent Langlois's avatar
Florent Langlois committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
	  m_buffer.newFrameReady(frame_info);
	}
      // 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)
	{
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
	  CtVideo::Image anImageWrapper(&m_video,anImage);
	  aLock.unlock();

	  SinkTaskBase* cbk_task = new SinkTaskBase();
	  _videoBackgroundCallback *video_cbk = new _videoBackgroundCallback(m_video.m_image_callback,
									     anImageWrapper);


	  cbk_task->setEventCallback(video_cbk);
	  video_cbk->unref();

	  TaskMgr *mgr = new TaskMgr();
	  mgr->addSinkTask(0,cbk_task);
	  cbk_task->unref();

	  PoolThreadMgr::get().addProcess(mgr);
Florent Langlois's avatar
Florent Langlois committed
213
214
215
216
	}
    }
  return true;
}
217
#ifdef WITH_CONFIG
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// --- _ConfigHandler
class CtVideo::_ConfigHandler : public CtConfig::ModuleTypeCallback
{
public:
  _ConfigHandler(CtVideo& video):
    CtConfig::ModuleTypeCallback("Video"),
    m_video(video) {}
  virtual void store(Setting& video_setting)
  {
    CtVideo::Parameters pars;
    m_video.getParameters(pars);

    video_setting.set("live",pars.live);
    video_setting.set("exposure",pars.exposure);
    video_setting.set("gain",pars.gain);
233
    video_setting.set("auto_gain_mode",convert_2_string(pars.auto_gain_mode));
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
    video_setting.set("mode",convert_2_string(pars.mode));

    // --- Roi
    Setting roi_setting = video_setting.addChild("roi");

    const Point& topleft = pars.roi.getTopLeft();
    roi_setting.set("x",topleft.x);
    roi_setting.set("y",topleft.y);

    const Size& roiSize = pars.roi.getSize();
    roi_setting.set("width",roiSize.getWidth());
    roi_setting.set("height",roiSize.getHeight());

    // --- Bin
    Setting bin_setting = video_setting.addChild("bin");
    
    bin_setting.set("x",pars.bin.getX());
    bin_setting.set("y",pars.bin.getY());
  }
  virtual void restore(const Setting& video_setting)
  {
    CtVideo::Parameters pars;
    m_video.getParameters(pars);

    video_setting.get("live",pars.live);
    video_setting.get("exposure",pars.exposure);
    video_setting.get("gain",pars.gain);

262
263
264
265
    std::string str_auto_gain_mode;
    if(video_setting.get("auto_gain_mode",str_auto_gain_mode))
      convert_from_string(str_auto_gain_mode,pars.auto_gain_mode);

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    std::string strmode;
    if(video_setting.get("mode",strmode))
      convert_from_string(strmode,pars.mode);

    // --- Bin
    Setting bin_setting;
    if(video_setting.getChild("bin",bin_setting))
      {
	int x,y;
	if(bin_setting.get("x",x) &&
	   bin_setting.get("y",y))
	  pars.bin = Bin(x,y);
      }
    // --- Roi
    Setting roi_setting;
    if(video_setting.getChild("roi",roi_setting))
      {
	Point topleft;
	int width,height;
	if(roi_setting.get("x",topleft.x) &&
	   roi_setting.get("y",topleft.y) &&
	   roi_setting.get("width",width) &&
	   roi_setting.get("height",height))
	  pars.roi = Roi(topleft.x,topleft.y,
			 width,height);
      }
    m_video.setParameters(pars);
  }
private:
  CtVideo& m_video;
};
297
298
#endif //WITH_CONFIG

Florent Langlois's avatar
Florent Langlois committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// --- 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);
    }
}

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;
}
/** @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);
}

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;
}

long long CtVideo::Image::frameNumber() const
{
  return m_image ? m_image->frameNumber : -1;
}
// --- CtVideo class
CtVideo::CtVideo(CtControl &ct) :
  m_pars_modify_mask(0),
  m_ready_flag(true),
Arafat Noureddine's avatar
Arafat Noureddine committed
388
  m_image_counter(-1),
Florent Langlois's avatar
Florent Langlois committed
389
390
391
392
393
  m_read_image(new VideoImage()),
  m_write_image(new VideoImage()),
  m_image_callback(NULL),
  m_internal_image_callback(NULL),
  m_ct(ct),
394
395
  m_stopping_live(false),
  m_active_flag(false)
Florent Langlois's avatar
Florent Langlois committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
{
  HwInterface *hw = ct.hwInterface();
  m_has_video = hw->getHwCtrlObj(m_video);
  hw->getHwCtrlObj(m_sync);
  m_data_2_image_task = new _Data2ImageTask(*this);

  m_data_2_image_cb = new _Data2ImageCBK(*this);
  m_data_2_image_task->setEventCallback(m_data_2_image_cb);

  // Params init
  if(m_has_video)
    {
      m_internal_image_callback = new _InternalImageCBK(*this);
      m_video->getGain(m_pars.gain);
      m_video->getVideoMode(m_pars.mode);
      m_video->registerImageCallback(*m_internal_image_callback);
    }
}

CtVideo::~CtVideo()
{
  m_data_2_image_task->unref();
  m_data_2_image_cb->unref();
  delete m_read_image;
  delete m_write_image;
  delete m_internal_image_callback;
}

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
424
425
426
427
428
429
430
431
432
433
434
void CtVideo::setActive(bool aFlag)
{
  AutoMutex aLock(m_cond.mutex());
  m_active_flag = aFlag;
}

bool CtVideo::isActive() const
{
  AutoMutex aLock(m_cond.mutex());
  return m_active_flag;
}
Florent Langlois's avatar
Florent Langlois committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// --- parameters
void CtVideo::setParameters(const Parameters &pars)
{
  //All check
  _check_video_mode(pars.mode);

  AutoMutex aLock(m_cond.mutex());

  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;

449
  bool previousLive = m_pars.live;
Florent Langlois's avatar
Florent Langlois committed
450
  m_pars = pars;
451
  m_pars.live = previousLive;
452
  _apply_params(aLock);
453
454
455
  aLock.unlock();

  _setLive(pars.live);
Florent Langlois's avatar
Florent Langlois committed
456
457
458
459
460
461
462
}
void CtVideo::getParameters(Parameters &pars) const
{
  AutoMutex aLock(m_cond.mutex());
  pars = m_pars;
}

463
void CtVideo::_setLive(bool liveFlag)
Florent Langlois's avatar
Florent Langlois committed
464
{
465
  DEB_MEMBER_FUNCT();
466
  DEB_PARAM() << DEB_VAR1(liveFlag);
Florent Langlois's avatar
Florent Langlois committed
467
468
469
470
471
472
473
474
475
476
  AutoMutex aLock(m_cond.mutex());

  while(m_stopping_live) m_cond.wait();

  if(m_pars.live == liveFlag)	// nothing to do
    return;

  CtControl::Status status;
  m_ct.getStatus(status);
  if(liveFlag && status.AcquisitionStatus != AcqReady)
477
    THROW_CTL_ERROR(Error) <<  "Can't set live mode if an acquisition is running";
Florent Langlois's avatar
Florent Langlois committed
478

479
  _apply_params(aLock,liveFlag);
Florent Langlois's avatar
Florent Langlois committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
  
  if(m_has_video)
    {
      if(!liveFlag)
	{
	  m_stopping_live = true;
	  aLock.unlock();
	}

      m_video->setLive(liveFlag);

      if(!liveFlag)
	{
	  aLock.lock();
	  m_stopping_live = false;
	  m_cond.signal();
	}
    }
  else
    {
500
      DEB_TRACE() << "NO specific video controller do the defaults :";
Florent Langlois's avatar
Florent Langlois committed
501
502
503
504
      if(liveFlag)
	{
	  CtAcquisition *acqPt = m_ct.acquisition();
	  acqPt->setAcqNbFrames(0);	// Live
505
	  DEB_TRACE() << "Set the number of frame to collect to 0 at the CtAcquisition level";
506
	  aLock.unlock();
507
	  DEB_TRACE() << "Preparing the acquisition at the controller level";
Florent Langlois's avatar
Florent Langlois committed
508
	  m_ct.prepareAcq();
509
	  aLock.lock();
510
	  DEB_TRACE() << "Starting the acquisition at teh controller level";
Florent Langlois's avatar
Florent Langlois committed
511
	  m_ct.startAcq();
512
	  DEB_TRACE() << "Done with the startAcq.";
Florent Langlois's avatar
Florent Langlois committed
513
	}
514
515
516
      else {
	//aLock.unlock();
	DEB_TRACE() << "Stopping the acquisition at the controller level";
Florent Langlois's avatar
Florent Langlois committed
517
	m_ct.stopAcq();
518
      }
Florent Langlois's avatar
Florent Langlois committed
519
520
    }
  m_pars.live = liveFlag;
521
  m_active_flag = m_active_flag || m_pars.live;
Florent Langlois's avatar
Florent Langlois committed
522
523
524
525
526
527
528
529
530
531
532
}
void CtVideo::getLive(bool &liveFlag) const
{
  AutoMutex aLock(m_cond.mutex());
  liveFlag = m_pars.live;
}

void CtVideo::setExposure(double anExposure)
{
  AutoMutex aLock(m_cond.mutex());
  m_pars.exposure = anExposure,m_pars_modify_mask |= PARMODIFYMASK_EXPOSURE;
533
  _apply_params(aLock);
Florent Langlois's avatar
Florent Langlois committed
534
535
536
537
538
539
540
541
542
543
}

void CtVideo::getExposure(double &anExposure) const
{
  AutoMutex aLock(m_cond.mutex());
  anExposure = m_pars.exposure;
}

void CtVideo::setGain(double aGain)
{
544
  DEB_MEMBER_FUNCT();
545
546
  DEB_PARAM() << DEB_VAR1(aGain);

Florent Langlois's avatar
Florent Langlois committed
547
  if(!m_has_video)
548
    THROW_CTL_ERROR(Error) << "Can't change the gain on Scientific camera";
Florent Langlois's avatar
Florent Langlois committed
549
  if(aGain < 0. || aGain > 1.)
550
    THROW_CTL_ERROR(InvalidValue) << "Gain should be between 0. and 1.";
Florent Langlois's avatar
Florent Langlois committed
551
552

  AutoMutex aLock(m_cond.mutex());
553
554
555
  if(m_pars.auto_gain_mode == ON)
    THROW_CTL_ERROR(Error) << "Should disable auto gain to set gain";

Florent Langlois's avatar
Florent Langlois committed
556
  m_pars.gain = aGain,m_pars_modify_mask |= PARMODIFYMASK_GAIN;
557
  _apply_params(aLock);
Florent Langlois's avatar
Florent Langlois committed
558
559
560
561
}
void CtVideo::getGain(double &aGain) const
{
  AutoMutex aLock(m_cond.mutex());
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
  if(m_pars.auto_gain_mode == OFF)
    aGain = m_pars.gain;
  else
    m_video->getGain(aGain);
}

bool CtVideo::checkAutoGainMode(AutoGainMode mode) const
{
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(mode);

  bool check_flag = false;
  if(m_has_video)
    {
      switch(mode)
	{
	case ON:
	  check_flag = m_video->checkAutoGainMode(HwVideoCtrlObj::ON);
	  break;
	case OFF:
	  check_flag = m_video->checkAutoGainMode(HwVideoCtrlObj::OFF);
	  break;
	case ON_LIVE:
	  check_flag = m_video->checkAutoGainMode(HwVideoCtrlObj::ON) &&
	    m_video->checkAutoGainMode(HwVideoCtrlObj::OFF);
	  break;
	default:
	  break;
	}
    }
  DEB_RETURN() << DEB_VAR1(check_flag);
  return check_flag;
}

void CtVideo::getAutoGainModeList(AutoGainModeList& modes) const
{
  DEB_MEMBER_FUNCT();
  int nb_modes = 0;
  if(m_video->checkAutoGainMode(HwVideoCtrlObj::OFF))
    modes.push_back(OFF),++nb_modes;
  if(m_video->checkAutoGainMode(HwVideoCtrlObj::ON))
    modes.push_back(ON),++nb_modes;

  if(nb_modes == 2)		// All modes
    modes.push_back(ON_LIVE);
}

void CtVideo::setAutoGainMode(AutoGainMode mode)
{
  DEB_MEMBER_FUNCT();
  if(!checkAutoGainMode(mode))
    THROW_CTL_ERROR(NotSupported) << DEB_VAR1(mode);
  AutoMutex aLock(m_cond.mutex());
  m_pars.auto_gain_mode = mode,m_pars_modify_mask |= PARMODIFYMASK_AUTO_GAIN;
  _apply_params(aLock);
}

void CtVideo::getAutoGainMode(AutoGainMode& mode) const
{
  AutoMutex aLock(m_cond.mutex());
  mode = m_pars.auto_gain_mode;
Florent Langlois's avatar
Florent Langlois committed
623
624
625
626
627
628
629
}

void CtVideo::setMode(VideoMode aMode)
{
  _check_video_mode(aMode);
  AutoMutex aLock(m_cond.mutex());
  m_pars.mode = aMode,m_pars_modify_mask |= PARMODIFYMASK_MODE;
630
  _apply_params(aLock);
Florent Langlois's avatar
Florent Langlois committed
631
632
633
634
635
636
637
638
639
640
641
}
void CtVideo::getMode(VideoMode &aMode) const
{
  AutoMutex aLock(m_cond.mutex());
  aMode = m_pars.mode;
}

void CtVideo::setRoi(const Roi &aRoi)
{
  AutoMutex aLock(m_cond.mutex());
  m_pars.roi = aRoi,m_pars_modify_mask |= PARMODIFYMASK_ROI;
642
  _apply_params(aLock);
Florent Langlois's avatar
Florent Langlois committed
643
644
645
646
647
648
649
650
651
652
653
}
void CtVideo::getRoi(Roi &aRoi) const
{
  AutoMutex aLock(m_cond.mutex());
  aRoi = m_pars.roi;
}

void CtVideo::setBin(const Bin &aBin)
{
  AutoMutex aLock(m_cond.mutex());
  m_pars.bin = aBin,m_pars_modify_mask |= PARMODIFYMASK_BIN;
654
  _apply_params(aLock);
Florent Langlois's avatar
Florent Langlois committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
}
void CtVideo::getBin(Bin &aBin) const
{
  AutoMutex aLock(m_cond.mutex());
  aBin = m_pars.bin;
}

// --- images
void CtVideo::getLastImage(CtVideo::Image &anImage) const
{
  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;
}

680
void CtVideo::getLastImageCounter(long long &anImageCounter) const
Florent Langlois's avatar
Florent Langlois committed
681
682
{
  AutoMutex aLock(m_cond.mutex());
683
  anImageCounter = m_image_counter;
Florent Langlois's avatar
Florent Langlois committed
684
685
686
687
688
689
690
691
692
693
}

void CtVideo::registerImageCallback(ImageCallback &cb)
{
  DEB_MEMBER_FUNCT();

  AutoMutex aLock(m_cond.mutex());
  DEB_PARAM() << DEB_VAR2(&cb, m_image_callback);
  
  if(m_image_callback)
694
    THROW_CTL_ERROR(InvalidValue) << "ImageCallback already registered";
Florent Langlois's avatar
Florent Langlois committed
695
696
697
698
699
700
701
702
703
704
705

  m_image_callback = &cb;
}

void CtVideo::unregisterImageCallback(ImageCallback &cb)
{
  DEB_MEMBER_FUNCT();

  AutoMutex aLock(m_cond.mutex());
  DEB_PARAM() << DEB_VAR2(&cb, m_image_callback);
  if(m_image_callback != &cb)
706
    THROW_CTL_ERROR(InvalidValue) << "ImageCallback not registered"; 
Florent Langlois's avatar
Florent Langlois committed
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740

  m_image_callback = NULL;
}

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

  if(m_has_video)
    m_video->getSupportedVideoMode(modeList);
  else				// TODO
    {
      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:
741
	  THROW_CTL_ERROR(Error) <<  "Image type not yet managed";
Florent Langlois's avatar
Florent Langlois committed
742
743
744
745
746
747
748
749
750
751
752
	}
    }
}

// --- callback from CtControl
void CtVideo::frameReady(Data &aData)
{
  DEB_MEMBER_FUNCT();
  if(!m_has_video)
    {
      AutoMutex aLock(m_cond.mutex());
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
753
      if(m_active_flag)
Florent Langlois's avatar
Florent Langlois committed
754
	{
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
755
756
757
758
759
760
761
762
763
764
	  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;
Florent Langlois's avatar
Florent Langlois committed
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
	}
    }
}

void CtVideo::_data_2_image(Data &aData,Bin &aBin,Roi &aRoi)
{
  DEB_MEMBER_FUNCT();
  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);
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
780
      aBinTaskPt->setProcessingInPlace(false);
Florent Langlois's avatar
Florent Langlois committed
781
782
783
784
785
786
787
788
789
790
      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);
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
791
      aSoftRoiTaskPt->setProcessingInPlace(!runLevel);
Florent Langlois's avatar
Florent Langlois committed
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
      aSoftRoiTaskPt->unref();
      ++runLevel;
    }
  anImageCopy->addSinkTask(runLevel,m_data_2_image_task);
  anImageCopy->setInputData(aData);
  
  PoolThreadMgr::get().addProcess(anImageCopy);
}

void CtVideo::_data2image_finnished(Data&)
{
  DEB_MEMBER_FUNCT();
  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;
}

818
void CtVideo::_apply_params(AutoMutex &aLock,bool aForceLiveFlag)
Florent Langlois's avatar
Florent Langlois committed
819
820
821
822
823
824
825
826
827
828
{
  if(aForceLiveFlag && !m_pars.live)
      m_read_image->frameNumber = m_write_image->frameNumber = 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);
829
830
831
832
833
	  if(m_pars_modify_mask & PARMODIFYMASK_AUTO_GAIN)
	    m_video->setHwAutoGainMode(m_pars.auto_gain_mode == OFF ? 
				       HwVideoCtrlObj::OFF : HwVideoCtrlObj::ON);
	  if(m_pars.auto_gain_mode == OFF && 
	     (m_pars_modify_mask & PARMODIFYMASK_GAIN))
Florent Langlois's avatar
Florent Langlois committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
	    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)
	    {
	      m_sync->setExpTime(m_pars.exposure);
	      CtAcquisition* acquisition = m_ct.acquisition();
	      acquisition->setAcqExpoTime(m_pars.exposure);
	    }
	}
      else			// Scientific Camera
	{
          if(m_pars_modify_mask & PARMODIFYMASK_EXPOSURE)
            {
              CtAcquisition* acquisition = m_ct.acquisition();
              acquisition->setAcqExpoTime(m_pars.exposure);
	      if(m_pars.live)
	        {
		  m_ct.stopAcq();
868
		  aLock.unlock();
Florent Langlois's avatar
Florent Langlois committed
869
		  m_ct.prepareAcq();
870
		  aLock.lock();
Florent Langlois's avatar
Florent Langlois committed
871
		  m_ct.startAcq();
872
		  m_pars.live = true;
Florent Langlois's avatar
Florent Langlois committed
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
		}
            }
	}
      m_pars_modify_mask = 0;	// reset
    }
}

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

void CtVideo::_check_video_mode(VideoMode aMode)
{
888
889
  DEB_MEMBER_FUNCT();

Florent Langlois's avatar
Florent Langlois committed
890
891
892
893
894
895
896
897
  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)
898
899
900
901
    {
      THROW_CTL_ERROR(InvalidValue) << "Video mode: " <<
	DEB_VAR1(aMode) << " is not available for this camera";
    }
Florent Langlois's avatar
Florent Langlois committed
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
}

/** @brief an Acquisition will start so,
 *  we have to stop video mode
 */ 
void CtVideo::_prepareAcq()
{
  DEB_MEMBER_FUNCT();
  DEB_TRACE() << DEB_VAR1(m_stopping_live);

  AutoMutex aLock(m_cond.mutex());

  while(m_stopping_live) m_cond.wait();

  m_stopping_live = true;
  aLock.unlock();

  if(m_has_video)
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
    {
      m_video->setLive(false);
      if(m_pars.auto_gain_mode == ON_LIVE)
	{
	  int nb_frames;
	  m_sync->getNbFrames(nb_frames);
	  m_video->setAutoGainMode(nb_frames ? 
				   HwVideoCtrlObj::OFF : HwVideoCtrlObj::ON);
	  if(!nb_frames)
	    m_video->setGain(m_pars.gain);
	}
      else
	{
	  m_video->setAutoGainMode(m_pars.auto_gain_mode == OFF ? 
				   HwVideoCtrlObj::OFF : HwVideoCtrlObj::ON);
	  if(m_pars.auto_gain_mode == OFF)
	    m_video->setGain(m_pars.gain);
	}
    }
Florent Langlois's avatar
Florent Langlois committed
939
940
941
942
943
944
945
946
947
948
949
  
  aLock.lock();
  m_stopping_live = false;
  m_cond.signal();

  m_pars.live = false;
  m_image_counter = -1;
  _read_hw_params();
  
  m_read_image->frameNumber = -1;
  m_write_image->frameNumber = -1;
950
951
952

  CtBuffer* buffer = m_ct.buffer();
  buffer->getNumber(m_data_2_image_task->m_nb_buffer);
Florent Langlois's avatar
Florent Langlois committed
953
}
954

955
956
void CtVideo::_startAcqTime()
{
957
958
  if(m_internal_image_callback)
    m_internal_image_callback->m_start_time = Timestamp::now();
959
}
960
#ifdef WITH_CONFIG
961
962
963
964
CtConfig::ModuleTypeCallback* CtVideo::_getConfigHandler()
{
  return new _ConfigHandler(*this);
}
965
#endif //WITH_CONFIG
966

Florent Langlois's avatar
Florent Langlois committed
967
968
969
970
971
972
973
974
975
976
977
978
//============================================================================
//			 CtVideo::Parameters
//============================================================================
CtVideo::Parameters::Parameters()
{
  reset();
}
void CtVideo::Parameters::reset()
{
  live = false;
  exposure = 1.;
  gain = -1.;
979
  auto_gain_mode = CtVideo::OFF;
Florent Langlois's avatar
Florent Langlois committed
980
981
982
983
  mode = Y8;
  roi.reset();
  bin.reset();
}