Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_DV2.cpp
1 #include "VideoSource_DV2.hh"
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <sys/ioctl.h>
5 #include <unistd.h>
6 #include <sys/mman.h>
7 
8 
9 using namespace BIAS;
10 using namespace std;
11 
13 {
14  Width_ = MIP_DV_WIDTH;
15  Height_ = MIP_DV_HEIGHT;
16  ColorChannels_ = 1;
18  DeviceChannel_ = 0x63;
19 
20  SetDebugLevel(D_DV2);
21  DV1394FD_ = 0;
22 
23  FrameSize_ = 0;
24  Format_ = DV1394_PAL;
25  Buffer_ = NULL;
26  DefaultDevice_ = "/dev/dv1394";
27 
28 
29 }
30 
32 {
33 
34 
35 }
36 
37 
38 
40 {
41  return OpenDevice(DefaultDevice_);
42 }
43 
44 
45 
46 int VideoSource_DV2::OpenDevice(const string &device)
47 {
48  DV1394FD_ = open(device.c_str(),O_RDONLY);
49  if (DV1394FD_ < 0) {
50  BIASERR("Can not open file: "<<device);
51  perror(device.c_str());
52  return -1;
53  }
54 
55  InitData_.channel = DeviceChannel_;
56  // size of ringbuffer */
57  InitData_.n_frames = 10;
58  InitData_.format = Format_;
59  InitData_.cip_n = 0;
60  InitData_.cip_d = 0;
61  InitData_.syt_offset = 0;
62 
63 
64  switch (Format_) {
65  case DV1394_PAL:
66  FrameSize_ = DV1394_PAL_FRAME_SIZE;
67  break;
68  case DV1394_NTSC:
69  FrameSize_ = DV1394_NTSC_FRAME_SIZE;
70  break;
71  }
72  ioctl(DV1394FD_,DV1394_INIT, InitData_);
73  BufferSize_ = FrameSize_ * InitData_.n_frames;
74 
75 // Buffer_ = (unsigned char *)mmap(0, BufferSize_, PROT_READ, MAP_SHARED, DV1394FD_, 0);
76 // if (Buffer_ <0) {
77 // perror("Error mmap() :");
78 // return -1;
79 // }
80 // cout <<(void *)Buffer_<<endl;
81 
82 
83 
84  CompleteInitialized_ = true;
85 
86  return 0;
87 }
88 
89 
90 
91 
93 {
94  munmap(Buffer_, BufferSize_);
95 
96  close(DV1394FD_);
97  CompleteInitialized_ = false;
98 
99  return 0;
100 }
101 
102 
103 
105 {
106  DeviceChannel_ = channel;
107  return 0;
108 }
109 
110 
112 {
113  struct timeval time;
114  int got;
115  int ReceiveNFrames = 1;
116  int NewBuffers = 0;
117 
118 
119 // cout <<"awaiting image"<<endl;
120 
121  ioctl(DV1394FD_, DV1394_RECEIVE_FRAMES,ReceiveNFrames);
122 
123  got = ioctl(DV1394FD_, DV1394_GET_STATUS, & Status_);
124  NewBuffers = (Status_.first_clear_frame+ReceiveNFrames)% InitData_.n_frames;
125  BIASDOUT_CLEAN(D_DV2,"First clear buffer: "<<Status_.first_clear_frame)
126  BIASDOUT_CLEAN(D_DV2,"N buffers: "<<NewBuffers);
127 
128  if (NewBuffers >0) {
129  gettimeofday(&time,NULL);
130  image.SetTime(time.tv_sec,time.tv_usec);
131 
132  read(DV1394FD_, image.GetImageData(), FrameSize_);
133  image.InvalidateUID();
134  return 0;
135  }
136  else return -1;
137 
138 }
139 
140 
142 {
143 
144  if (VideoSource::PreGrab() <0 )
145  return -1;
146  ioctl(DV1394FD_,DV1394_START_RECEIVE,NULL);
147 
148  return 0;
149 
150 }
151 
152 
154 {
155 
156 
157  if (VideoSource::PostGrab() <0 )
158  return -1;
159 
160  ioctl(DV1394FD_,DV1394_SHUTDOWN, NULL);
161 
162  return 0;
163 }
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
Defines a common interface to different devices.
struct dv1394_init InitData_
int UseChannel(int channel)
int PostGrab()
Stop anything started in PreGrab()
int OpenDevice()
selects the first available device to open (e.g.
enum pal_or_ntsc Format_
DV, color model used for digital video cameras such as Mini-DV.
Definition: ImageBase.hh:140
const char * DefaultDevice_
Device name.
BIAS::ImageBase::EColorModel ColorMode_
Color mode used by camera.
int DeviceChannel_
Device channel.
bool CompleteInitialized_
Complete_initialized_ is set when OpenDevice(), UseChannel() etc. are done.
int Width_
Image format.
int GrabSingle(BIAS::Camera< unsigned char > &image)
void SetDebugLevel(const long int lv)
Definition: Debug.hh:318
virtual int PreGrab()
Do last preparations before grabbing (e.g. start ISO transfer)
int PreGrab()
Do last preparations before grabbing (e.g. start ISO transfer)
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
virtual int PostGrab()
Stop anything started in PreGrab()
unsigned char * Buffer_
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
struct dv1394_status Status_