CtVideo.cpp 27.3 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"
7
#include "lima/SoftOpExternalMgr.h"
Florent Langlois's avatar
Florent Langlois committed
8

9
10
11
12
13
#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
14
15
16
17
18
19
20
21

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

  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);
41
    while(anImage->inused) {
Florent Langlois's avatar
Florent Langlois committed
42
      m_cnt.m_cond.wait();
43
44
45
46
47
48
      if (anImage != m_cnt.m_write_image) {
	DEB_TRACE() << "Read/write swapped";
	anImage = m_cnt.m_write_image;
      }
    }
    DEB_TRACE() << DEB_VAR1(*anImage);
Florent Langlois's avatar
Florent Langlois committed
49
50
51
52
53
    anImage->inused = -1;	// Write Mode
    aLock.unlock();
    
    data2Image(aData,*anImage);
    
54
55
56
    //Check if data is still available
    bool still_available = _check_available(aData);

Florent Langlois's avatar
Florent Langlois committed
57
58
    aLock.lock();
    anImage->inused = 0;	// Unlock
59
60
61
62
63
64
65
    if(!still_available)
      {
	DEB_ALWAYS() << "invalidate video copy";
	anImage->frameNumber = -1; // invalidate copy
	return;
      }

Florent Langlois's avatar
Florent Langlois committed
66
67
68
69
70
    ++m_cnt.m_image_counter;

    // if read Image is not use, swap
    if(!m_cnt.m_read_image->inused)
      {
71
	DEB_TRACE() << "swapping read and write";
Florent Langlois's avatar
Florent Langlois committed
72
73
74
75
76
77
	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)
	  {
78
	    CtVideo::Image anImageWrapper(&m_cnt,anImage,aLock);
Florent Langlois's avatar
Florent Langlois committed
79
80
81
82
83
84
	    ImageCallback *cb = m_cnt.m_image_callback;
	    aLock.unlock();
	    cb->newImage(anImageWrapper);
	  }
      }
  }
85
86

  long m_nb_buffer;
87
88
  bool m_data_always_available;

Florent Langlois's avatar
Florent Langlois committed
89
private:
90
91
  inline bool _check_available(Data& aData)
  {
92
    DEB_MEMBER_FUNCT();
93
94
    CtControl::ImageStatus status;
    m_cnt.m_ct.getImageStatus(status);
95
96
97
98

    if (m_data_always_available)
      return true;

99
    long offset = status.LastImageAcquired - aData.frameNumber;
100
101
    DEB_TRACE() << DEB_VAR4(status.LastImageAcquired, aData.frameNumber,
			    offset, m_nb_buffer);
102
103
104
    return offset < m_nb_buffer;
  }

Florent Langlois's avatar
Florent Langlois committed
105
106
107
  CtVideo &m_cnt;
};

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
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
// --- 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() {}
  
150
    Timestamp		m_start_time;
Florent Langlois's avatar
Florent Langlois committed
151
152
153
154
155
156
157
158
159
160
161
162
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);

163
  
Florent Langlois's avatar
Florent Langlois committed
164
165
166
167
  bool liveFlag;
  AutoMutex aLock(m_video.m_cond.mutex());
  if(m_video.m_stopping_live) return false;

168
169
  Timestamp now = Timestamp::now();

Florent Langlois's avatar
Florent Langlois committed
170
  liveFlag = m_video.m_pars.live;
171
  int image_counter = (int) m_video.m_image_counter + 1;
Florent Langlois's avatar
Florent Langlois committed
172
173
174
175
  aLock.unlock();
    
  if(!liveFlag)			// Classic acquisition
    {
176
      void *ptr = m_buffer.getFrameBufferPtr(image_counter);
Florent Langlois's avatar
Florent Langlois committed
177
178
179
180
181
      try
	{
	  lima::image2YUV((unsigned char*)data,width,height,mode,(unsigned char*)ptr);
	  HwFrameInfoType frame_info;
	  frame_info.acq_frame_nb = image_counter;
182
	  frame_info.frame_timestamp = now - m_start_time;
Florent Langlois's avatar
Florent Langlois committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
	  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)
	{
214
	  CtVideo::Image anImageWrapper(&m_video,anImage,aLock);
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
	  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
230
231
232
233
	}
    }
  return true;
}
234
#ifdef WITH_CONFIG
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// --- _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);
250
    video_setting.set("auto_gain_mode",convert_2_string(pars.auto_gain_mode));
251
    video_setting.set("video_source",convert_2_string(pars.video_source));
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
    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);

280
281
282
283
    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);

284
285
286
287
    std::string str_video_source;
    if(video_setting.get("video_source",str_video_source))
      convert_from_string(str_video_source,pars.video_source);

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
    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;
};
319
320
#endif //WITH_CONFIG

Florent Langlois's avatar
Florent Langlois committed
321
322
323
324
325
326
327
328
// --- CtVideo::Image
CtVideo::Image::Image() : m_video(NULL),m_image(NULL) {}

CtVideo::Image::~Image()
{
  if(m_video)
    {
      AutoMutex aLock(m_video->m_cond.mutex());
329
330
      if (--(m_image->inused) == 0)
	m_video->m_cond.broadcast();
Florent Langlois's avatar
Florent Langlois committed
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
    }
}

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());
358
359
	  if (--(m_image->inused) == 0)
	    m_video->m_cond.broadcast();
Florent Langlois's avatar
Florent Langlois committed
360
361
362
363
364
365
366
367
368
369
	}

      m_video = other.m_video;
      m_image = other.m_image;
    }
  return *this;
}
/** @brief an other contructor
 *  This methode should be call under Lock
 */
370
CtVideo::Image::Image(const CtVideo *video,VideoImage *image, AutoMutex&l) :
Florent Langlois's avatar
Florent Langlois committed
371
372
373
  m_video(video),
  m_image(image)
{
374
375
  if (!m_video || (&l.mutex() != &m_video->m_cond.mutex()) || !l.locked())
    throw LIMA_CTL_EXC(InvalidValue, "Invalid video/mutex");
Florent Langlois's avatar
Florent Langlois committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  ++(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
412
  m_image_counter(-1),
Florent Langlois's avatar
Florent Langlois committed
413
414
415
416
417
  m_read_image(new VideoImage()),
  m_write_image(new VideoImage()),
  m_image_callback(NULL),
  m_internal_image_callback(NULL),
  m_ct(ct),
418
419
  m_stopping_live(false),
  m_active_flag(false)
Florent Langlois's avatar
Florent Langlois committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
{
  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
448
449
void CtVideo::setActive(bool aFlag)
{
450
451
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(aFlag);
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
452
453
454
455
456
457
458
459
460
  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
461
462
463
// --- parameters
void CtVideo::setParameters(const Parameters &pars)
{
464
465
466
467
468
469
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(pars);

  if(m_ct.acquisition()->isMonitorMode())
    THROW_CTL_ERROR(Error) << "Can't change any parameters on monitor mode";

Florent Langlois's avatar
Florent Langlois committed
470
471
472
473
474
475
476
477
478
479
480
  //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;

481
  bool previousLive = m_pars.live;
Florent Langlois's avatar
Florent Langlois committed
482
  m_pars = pars;
483
  m_pars.live = previousLive;
484
  _apply_params(aLock);
485
486
487
  aLock.unlock();

  _setLive(pars.live);
Florent Langlois's avatar
Florent Langlois committed
488
489
490
491
492
493
494
}
void CtVideo::getParameters(Parameters &pars) const
{
  AutoMutex aLock(m_cond.mutex());
  pars = m_pars;
}

495
void CtVideo::_setLive(bool liveFlag)
Florent Langlois's avatar
Florent Langlois committed
496
{
497
  DEB_MEMBER_FUNCT();
498
  DEB_PARAM() << DEB_VAR1(liveFlag);
Florent Langlois's avatar
Florent Langlois committed
499
500
501
502
503
504
505
506
507
508
  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)
509
    THROW_CTL_ERROR(Error) <<  "Can't set live mode if an acquisition is running";
Florent Langlois's avatar
Florent Langlois committed
510

511
  _apply_params(aLock,liveFlag);
Florent Langlois's avatar
Florent Langlois committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
  
  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
    {
532
      DEB_TRACE() << "NO specific video controller do the defaults :";
Florent Langlois's avatar
Florent Langlois committed
533
534
535
536
      if(liveFlag)
	{
	  CtAcquisition *acqPt = m_ct.acquisition();
	  acqPt->setAcqNbFrames(0);	// Live
537
	  DEB_TRACE() << "Set the number of frame to collect to 0 at the CtAcquisition level";
538
	  aLock.unlock();
539
	  DEB_TRACE() << "Preparing the acquisition at the controller level";
Florent Langlois's avatar
Florent Langlois committed
540
	  m_ct.prepareAcq();
541
	  aLock.lock();
542
	  DEB_TRACE() << "Starting the acquisition at teh controller level";
Florent Langlois's avatar
Florent Langlois committed
543
	  m_ct.startAcq();
544
	  DEB_TRACE() << "Done with the startAcq.";
Florent Langlois's avatar
Florent Langlois committed
545
	}
546
      else {
547
	aLock.unlock();
548
	DEB_TRACE() << "Stopping the acquisition at the controller level";
Florent Langlois's avatar
Florent Langlois committed
549
	m_ct.stopAcq();
550
	aLock.lock();
551
      }
Florent Langlois's avatar
Florent Langlois committed
552
553
    }
  m_pars.live = liveFlag;
554
  m_active_flag = m_active_flag || m_pars.live;
Florent Langlois's avatar
Florent Langlois committed
555
556
557
558
559
560
561
562
563
}
void CtVideo::getLive(bool &liveFlag) const
{
  AutoMutex aLock(m_cond.mutex());
  liveFlag = m_pars.live;
}

void CtVideo::setExposure(double anExposure)
{
564
565
566
567
568
569
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(anExposure);

  if(m_ct.acquisition()->isMonitorMode())
    THROW_CTL_ERROR(Error) << "Can't change the gain on monitor mode";

Florent Langlois's avatar
Florent Langlois committed
570
571
  AutoMutex aLock(m_cond.mutex());
  m_pars.exposure = anExposure,m_pars_modify_mask |= PARMODIFYMASK_EXPOSURE;
572
  _apply_params(aLock);
Florent Langlois's avatar
Florent Langlois committed
573
574
575
576
577
578
579
580
581
582
}

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

void CtVideo::setGain(double aGain)
{
583
  DEB_MEMBER_FUNCT();
584
585
  DEB_PARAM() << DEB_VAR1(aGain);

586
587
  if(m_ct.acquisition()->isMonitorMode())
    THROW_CTL_ERROR(Error) << "Can't change the gain on monitor mode";
Florent Langlois's avatar
Florent Langlois committed
588
  if(!m_has_video)
589
    THROW_CTL_ERROR(Error) << "Can't change the gain on Scientific camera";
Florent Langlois's avatar
Florent Langlois committed
590
  if(aGain < 0. || aGain > 1.)
591
    THROW_CTL_ERROR(InvalidValue) << "Gain should be between 0. and 1.";
Florent Langlois's avatar
Florent Langlois committed
592
593

  AutoMutex aLock(m_cond.mutex());
594
595
596
  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
597
  m_pars.gain = aGain,m_pars_modify_mask |= PARMODIFYMASK_GAIN;
598
  _apply_params(aLock);
Florent Langlois's avatar
Florent Langlois committed
599
600
601
602
}
void CtVideo::getGain(double &aGain) const
{
  AutoMutex aLock(m_cond.mutex());
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
  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();
640
641
642
643
644
645
  if(!m_has_video)
    {
      modes.push_back(OFF);
      return;
    }

646
647
648
649
650
651
652
653
654
655
656
657
658
  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();
659
660
  if(m_ct.acquisition()->isMonitorMode())
    THROW_CTL_ERROR(Error) << "Can't change the auto gain on monitor mode";
661
662
663
664
665
666
667
668
669
670
671
  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
672
673
}

674
675
676
677
678
679
680
681
682
683
684
685
686
687
void CtVideo::setVideoSource(VideoSource source)
{
  DEB_MEMBER_FUNCT();
  AutoMutex aLock(m_cond.mutex());
  m_pars.video_source = source;
  _apply_params(aLock);
}

void CtVideo::getVideoSource(VideoSource& source) const
{
  AutoMutex aLock(m_cond.mutex());
  source = m_pars.video_source;
}

Florent Langlois's avatar
Florent Langlois committed
688
689
void CtVideo::setMode(VideoMode aMode)
{
690
691
692
693
694
695
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(aMode);

  if(m_ct.acquisition()->isMonitorMode())
    THROW_CTL_ERROR(Error) << "Can't change video mode on monitor mode";

Florent Langlois's avatar
Florent Langlois committed
696
697
698
  _check_video_mode(aMode);
  AutoMutex aLock(m_cond.mutex());
  m_pars.mode = aMode,m_pars_modify_mask |= PARMODIFYMASK_MODE;
699
  _apply_params(aLock);
Florent Langlois's avatar
Florent Langlois committed
700
701
702
703
}
void CtVideo::getMode(VideoMode &aMode) const
{
  AutoMutex aLock(m_cond.mutex());
704
705
706
707
708
709
710
711
  if(m_has_video)
    aMode = m_pars.mode;
  else
    {
      std::list<VideoMode> modeList;
      getSupportedVideoMode(modeList);
      aMode = modeList.front();
    }
Florent Langlois's avatar
Florent Langlois committed
712
713
714
715
}

void CtVideo::setRoi(const Roi &aRoi)
{
716
717
718
719
720
721
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(aRoi);

  if(m_ct.acquisition()->isMonitorMode())
    THROW_CTL_ERROR(Error) << "Can't change the gain on monitor mode";

Florent Langlois's avatar
Florent Langlois committed
722
723
  AutoMutex aLock(m_cond.mutex());
  m_pars.roi = aRoi,m_pars_modify_mask |= PARMODIFYMASK_ROI;
724
  _apply_params(aLock);
Florent Langlois's avatar
Florent Langlois committed
725
726
727
728
729
730
731
732
733
}
void CtVideo::getRoi(Roi &aRoi) const
{
  AutoMutex aLock(m_cond.mutex());
  aRoi = m_pars.roi;
}

void CtVideo::setBin(const Bin &aBin)
{
734
735
736
737
738
739
  DEB_MEMBER_FUNCT();
  DEB_PARAM() << DEB_VAR1(aBin);

  if(m_ct.acquisition()->isMonitorMode())
      THROW_CTL_ERROR(Error) << "Can't change the gain on monitor mode";

Florent Langlois's avatar
Florent Langlois committed
740
741
  AutoMutex aLock(m_cond.mutex());
  m_pars.bin = aBin,m_pars_modify_mask |= PARMODIFYMASK_BIN;
742
  _apply_params(aLock);
Florent Langlois's avatar
Florent Langlois committed
743
744
745
746
747
748
749
750
751
752
}
void CtVideo::getBin(Bin &aBin) const
{
  AutoMutex aLock(m_cond.mutex());
  aBin = m_pars.bin;
}

// --- images
void CtVideo::getLastImage(CtVideo::Image &anImage) const
{
753
  DEB_MEMBER_FUNCT();
Florent Langlois's avatar
Florent Langlois committed
754
  AutoMutex aLock(m_cond.mutex());
755
756
757
  DEB_TRACE() << DEB_VAR3(m_write_image->inused,
			  m_write_image->frameNumber,
			  m_read_image->frameNumber);
Florent Langlois's avatar
Florent Langlois committed
758
759
760
  if(m_write_image->inused >= 0 && // No writter
     m_write_image->frameNumber >= m_read_image->frameNumber)
    {
761
      DEB_TRACE() << "swapping read and write";
Florent Langlois's avatar
Florent Langlois committed
762
763
764
      VideoImage *tmp = m_read_image;
      m_read_image = m_write_image;
      m_write_image = tmp;
765
766
      if (!m_read_image->inused)
	m_write_image->frameNumber = -1;
Florent Langlois's avatar
Florent Langlois committed
767
    }
768
  CtVideo::Image tmpImage(this,m_read_image,aLock);
Florent Langlois's avatar
Florent Langlois committed
769
770
771
  aLock.unlock();
  
  anImage = tmpImage;
772
  DEB_RETURN() << DEB_VAR1(anImage.m_image->frameNumber);
Florent Langlois's avatar
Florent Langlois committed
773
774
}

775
void CtVideo::getLastImageCounter(long long &anImageCounter) const
Florent Langlois's avatar
Florent Langlois committed
776
777
{
  AutoMutex aLock(m_cond.mutex());
778
  anImageCounter = m_image_counter;
Florent Langlois's avatar
Florent Langlois committed
779
780
781
782
783
784
785
786
787
788
}

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)
789
    THROW_CTL_ERROR(InvalidValue) << "ImageCallback already registered";
Florent Langlois's avatar
Florent Langlois committed
790
791
792
793
794
795
796
797
798
799
800

  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)
801
    THROW_CTL_ERROR(InvalidValue) << "ImageCallback not registered"; 
Florent Langlois's avatar
Florent Langlois committed
802
803
804
805
806

  m_image_callback = NULL;
}

// --- video mode
807
void CtVideo::getSupportedVideoMode(std::list<VideoMode> &modeList) const
Florent Langlois's avatar
Florent Langlois committed
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
{
  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;
835
836
837
838
839
840
841
842
843

	case Bpp1:
	case Bpp4:
	case Bpp6:
	  modeList.push_back(Y8); break;
	case Bpp24:
	case Bpp24S:
	  modeList.push_back(Y32); break;

Florent Langlois's avatar
Florent Langlois committed
844
	default:
845
	  THROW_CTL_ERROR(Error) <<  "Image type not yet managed";
Florent Langlois's avatar
Florent Langlois committed
846
847
848
849
850
851
852
853
854
855
856
	}
    }
}

// --- 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
857
      if(m_active_flag)
Florent Langlois's avatar
Florent Langlois committed
858
	{
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
859
860
861
862
863
864
865
866
867
868
	  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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
	}
    }
}

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
884
      aBinTaskPt->setProcessingInPlace(false);
Florent Langlois's avatar
Florent Langlois committed
885
886
887
888
889
890
891
892
893
894
      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
895
      aSoftRoiTaskPt->setProcessingInPlace(!runLevel);
Florent Langlois's avatar
Florent Langlois committed
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
      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;
}

922
void CtVideo::_apply_params(AutoMutex &aLock,bool aForceLiveFlag)
Florent Langlois's avatar
Florent Langlois committed
923
{
924
925
926
  if(m_ct.acquisition()->isMonitorMode())
    return;

Florent Langlois's avatar
Florent Langlois committed
927
928
929
930
931
932
933
934
  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)
935
936
937
938
939
940
941
	    {
	      m_video->setVideoMode(m_pars.mode);
	      CtImage* image = m_ct.image();
	      // change on video mode can change the image depth,so ask ctimage
	      // to resynchronize with the camera
	      image->syncDim();
	    }
942
943
944
945
946
	  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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
	    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)
	        {
980
		  aLock.unlock();
981
		  m_ct.stopAcq();
Florent Langlois's avatar
Florent Langlois committed
982
983
		  m_ct.prepareAcq();
		  m_ct.startAcq();
984
		  aLock.lock();
985
		  m_pars.live = true;
Florent Langlois's avatar
Florent Langlois committed
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
		}
            }
	}
      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)
{
For faster browsing, not all history is shown. View entire blame