Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_DV.cpp
1 #include "VideoSource_DV.hh"
2 #include <stdio.h>
3 #include <Image/Camera.hh>
4 
5 using namespace BIAS;
6 using namespace std;
7 
9 int VideoSource_DV::Instances_[MAX_1394_CARDS];
10 raw1394handle_t VideoSource_DV::IEEE1394Handle_[MAX_1394_CARDS];
11 
12 VideoSource_DV *videosourcefromchannel[MAX_1394_CHANNELS];
13 
14 
15 
17 {
19 }
20 
21 
23 {
25 }
26 
27 
28 
30 {
31  Width_ = MIP_DV_WIDTH;
32  Height_ = MIP_DV_HEIGHT;
33  ColorChannels_ = 1;
35  MaxTrials_ = 2;
36  Channel_ = 63;
37 SetDebugLevel(D_DV);
38 
39  IEEE1394Card_ = Card;
40 
41 
42  if (IEEE1394Card_ > MAX_1394_CARDS) {
43  BIASERR("VideoSource_DV::VideoSource_DV(int Card): requested card #"<<IEEE1394Card_<< " exceeds maximum of "<<MAX_1394_CARDS);
44  return ;
45  }
46 
47  if (InstancesTotal_ == 0) {
48  BIASDOUT(D_DV,"Initializing Instances_[] and IEEE1394Handle_[]");
49  for (int i=0; i<MAX_1394_CARDS; i++) {
50  Instances_[i] = 0;
51  IEEE1394Handle_[i] = NULL;
52  }
53  for (int i=0; i<MAX_1394_CHANNELS; i++)
54  videosourcefromchannel[i] = NULL;
55  }
56 
58  IEEE1394Card_ = Card;
59 
60  if ( Instances_[IEEE1394Card_] == 0) {
61  // I am the first instance on this Card, so open now
62  BIASDOUT(D_DV,"IEEE1394Card_: "<<IEEE1394Card_);
63  IEEE1394Handle_[IEEE1394Card_] = raw1394_new_handle();
64  if (IEEE1394Handle_[IEEE1394Card_] == NULL) {
65  perror("raw1394 - couldn't get handle");
66  BIASERR("This error usually means that the ieee1394 drivers are not loaded or that /dev/raw1394 not exist.\n");
67  return ;
68  }
69 
70  if (raw1394_set_port(IEEE1394Handle_[IEEE1394Card_],IEEE1394Card_) < 0)
71  BIASERR("raw1394 - couldn't set port");
72  }
73 
74 
75 
77  videosourcefromchannel[Channel_] = this;
78 
79 }
80 
82 {
83  InstancesTotal_ --;
85  if (Instances_[IEEE1394Card_] == 0) {
86  // I am the last instance on this card, so close the door
87  raw1394_destroy_handle(IEEE1394Handle_[IEEE1394Card_]);
88  BIASDOUT(D_DV,"I am the last Instance on this bus, so shutting the lights.");
89  }
90  videosourcefromchannel[Channel_] = NULL;
91 
92 
93 }
94 
95 
96 
98 {
99  return 0;
100 }
101 
102 
103 
104 int VideoSource_DV::OpenDevice(const char *device)
105 {
106  return 0;
107 }
108 
109 
110 
111 
113 {
114  return 0;
115 }
116 
117 
118 
120 {
121  Channel_ = channel;
122  return 0;
123 }
124 
125 
127 {
128  cout<<"GrabSingle()"<<endl;
129  int trials = 0;
130  bool done = false;
131  struct timeval time;
132 
133  gettimeofday(&time,NULL);
134  image.SetTime(time.tv_sec,time.tv_usec);
135 
136  GrabBuffer_ = image.GetImageData();
137 
138 
139 
140 
141  while (trials < MaxTrials_ && !done) {
142  FrameFinished_ = false;
143  FrameStarted_ = false;
144  DroppedFrame_ = false;
145 
146  while (!FrameFinished_) {
147  raw1394_loop_iterate(IEEE1394Handle_[IEEE1394Card_]);
148  // cout <<" raw1394_loop_iterate()"<<endl;
149  }
150 
151 
152  trials ++;
153  done = !DroppedFrame_;
154  }
155 
156  image.InvalidateUID();
157 
158  if (DroppedFrame_)
159  return -1;
160  else
161  return 0;
162 }
163 
164 int BIAS::raw_dv_handler(raw1394handle_t handle, int channel, size_t length,
165  quadlet_t *data)
166 {
167  static int packetcount = 0;
168  // cout << "raw_dv_tomem_handler()"<<endl;
169 
170 
171  if (length > 16) {
172  cout << "length >16"<<endl;
173  unsigned char *p = (unsigned char*) & data[3];
174  int section_type = p[0] >> 5; /* section type is in bits 5 - 7 */
175  int dif_sequence = p[1] >> 4; /* dif sequence number is in bits 4 - 7 */
176  int dif_block = p[2];
177 
178  // detect begin of frame
179  if (section_type == 0 && dif_sequence == 0) {
180  if (!videosourcefromchannel[channel]->FrameFinished_)
181  if (videosourcefromchannel[channel]->FrameStarted_)
182  videosourcefromchannel[channel]->DroppedFrame_ = true;
183  else
184  videosourcefromchannel[channel]->FrameStarted_ = true;
185  }
186 
187 
188 
189 
190  if (videosourcefromchannel[channel]->FrameStarted_ &&
191  !videosourcefromchannel[channel]->FrameFinished_) {
192  packetcount++;
193  switch (section_type) {
194  case 0: // 1 Header block
195  memcpy(videosourcefromchannel[channel]->GrabBuffer_
196  + dif_sequence * 150 * 80, p, 480);
197  break;
198 
199  case 1: // 2 Subcode blocks
200  memcpy(videosourcefromchannel[channel]->GrabBuffer_
201  + dif_sequence * 150 * 80 + (1 + dif_block) * 80,p, 480);
202  break;
203 
204  case 2: // 3 VAUX blocks
205  memcpy(videosourcefromchannel[channel]->GrabBuffer_+
206  dif_sequence * 150 * 80 + (3 + dif_block) * 80, p, 480);
207  break;
208 
209  case 3: // 9 Audio blocks interleaved with video
210  memcpy(videosourcefromchannel[channel]->GrabBuffer_
211  + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80,p, 480);
212  break;
213 
214  case 4: // 135 Video blocks interleaved with audio
215  memcpy(videosourcefromchannel[channel]->GrabBuffer_+
216  dif_sequence * 150 * 80 + (7 + (dif_block / 15) + dif_block)
217  * 80, p, 480);
218  break;
219 
220  default: // we cant handle any other data
221  cout <<"raw_dv_tomem_handler(): unknown data"<<endl;
222  break;
223  }
224  cout << packetcount << " " ;
225  }
226 
227 
228  if (packetcount == 300 && !videosourcefromchannel[channel]->FrameFinished_) {
229  videosourcefromchannel[channel]->FrameFinished_ = true;
230  packetcount = 0;
231  }
232 
233 
234  }
235  return 0;
236 }
237 
238 
240 {
241  iso_handler_t g_oldhandler;
242  BIASDOUT(D_DV,"PreGrab(), channel: "<< Channel_);
243  g_oldhandler = raw1394_set_iso_handler(IEEE1394Handle_[IEEE1394Card_],
245 
246  if (raw1394_start_iso_rcv(IEEE1394Handle_[IEEE1394Card_], Channel_) < 0) {
247  BIASERR("raw1394 - couldn't start iso receive");
248  return -1;
249  }
250 
251 // raw1394_set_bus_reset_handler(handle, reset_handler);
252  BIASDOUT(D_DV,"PreGrab() done");
253 
254  return 0;
255 
256 }
257 
258 
260 {
261  BIASDOUT(D_DV,"PostGrab()");
262 
263  raw1394_stop_iso_rcv(IEEE1394Handle_[IEEE1394Card_], Channel_);
264  return 0;
265 }
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
276 
Defines a common interface to different devices.
static int Instances_[MAX_1394_CARDS]
DV, color model used for digital video cameras such as Mini-DV.
Definition: ImageBase.hh:140
BIAS::ImageBase::EColorModel ColorMode_
Color mode used by camera.
unsigned char * GrabBuffer_
int UseChannel(int channel)
static int InstancesTotal_
static raw1394handle_t IEEE1394Handle_[MAX_1394_CARDS]
int Width_
Image format.
int PreGrab()
Do last preparations before grabbing (e.g. start ISO transfer)
void VideoSource_DV_Internal_Init_(int card)
void SetDebugLevel(const long int lv)
Definition: Debug.hh:318
void InvalidateUID()
sets the image&#39;s uid to invalid
Definition: ImageBase.hh:597
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
int GrabSingle(BIAS::Camera< unsigned char > &image)
int OpenDevice()
selects the first available device to open (e.g.
int PostGrab()
Stop anything started in PreGrab()
void SetTime(unsigned long long int sec, unsigned long long int usec)
Set time and ensure correct format (seconds and useconds after &#39;sec&#39;)
Definition: Camera.hh:124
friend int raw_dv_handler(raw1394handle_t handle, int channel, size_t length, quadlet_t *data)
This class extends VideoSource for the use of IEEE1394-connected DV-camcorders.