source: eviacam/trunk/fuentes/creavision/videoInput.h @ 567

Last change on this file since 567 was 567, checked in by mabarracus, 4 years ago

updated sources to version 2.0.3

  • Property svn:executable set to *
File size: 11.9 KB
Line 
1#ifndef _VIDEOINPUT
2#define _VIDEOINPUT
3
4//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10//THE SOFTWARE.
11
12//////////////////////////////////////////////////////////
13//Written by Theodore Watson - theo.watson@gmail.com    //
14//Do whatever you want with this code but if you find   //
15//a bug or make an improvement I would love to know!    //
16//                                                                                                              //
17//Warning This code is experimental                                     //
18//use at your own risk :)                                                               //
19//////////////////////////////////////////////////////////
20/////////////////////////////////////////////////////////
21/*                     Shoutouts
22
23Thanks to:
24                       
25                   Dillip Kumar Kara for crossbar code.
26                   Zachary Lieberman for getting me into this stuff
27                   and for being so generous with time and code.
28                   The guys at Potion Design for helping me with VC++
29                   Josh Fisher for being a serious C++ nerd :)
30                   Golan Levin for helping me debug the strangest
31                   and slowest bug in the world!
32                   
33                   And all the people using this library who send in
34                   bugs, suggestions and improvements who keep me working on
35                   the next version - yeah thanks a lot ;)
36                   
37*/
38/////////////////////////////////////////////////////////
39
40
41
42#include <stdlib.h>
43#include <stdio.h>
44#include <math.h>
45#include <string.h>
46#include <wchar.h>
47
48//this is for TryEnterCriticalSection
49#ifndef _WIN32_WINNT
50        #   define _WIN32_WINNT 0x400
51#endif
52#include <windows.h>
53
54
55//Example Usage
56/*
57        //create a videoInput object
58        videoInput VI;
59       
60        //Prints out a list of available devices and returns num of devices found
61        int numDevices = VI.listDevices();     
62       
63        int device1 = 0;  //this could be any deviceID that shows up in listDevices
64        int device2 = 1;  //this could be any deviceID that shows up in listDevices
65       
66        //if you want to capture at a different frame rate (default is 30)
67        //specify it here, you are not guaranteed to get this fps though.
68        //VI.setIdealFramerate(dev, 60);       
69       
70        //setup the first device - there are a number of options:
71       
72        VI.setupDevice(device1);                                                  //setup the first device with the default settings
73        //VI.setupDevice(device1, VI_COMPOSITE);                          //or setup device with specific connection type
74        //VI.setupDevice(device1, 320, 240);                              //or setup device with specified video size
75        //VI.setupDevice(device1, 320, 240, VI_COMPOSITE);  //or setup device with video size and connection type
76
77        //VI.setFormat(device1, VI_NTSC_M);                                     //if your card doesn't remember what format it should be
78                                                                                                                //call this with the appropriate format listed above
79                                                                                                                //NOTE: must be called after setupDevice!
80       
81        //optionally setup a second (or third, fourth ...) device - same options as above
82        VI.setupDevice(device2);                                                 
83
84        //As requested width and height can not always be accomodated
85        //make sure to check the size once the device is setup
86
87        int width       = VI.getWidth(device1);
88        int height      = VI.getHeight(device1);
89        int size        = VI.getSize(device1);
90       
91        unsigned char * yourBuffer1 = new unsigned char[size];
92        unsigned char * yourBuffer2 = new unsigned char[size];
93       
94        //to get the data from the device first check if the data is new
95        if(VI.isFrameNew(device1)){
96                VI.getPixels(device1, yourBuffer1, false, false);       //fills pixels as a BGR (for openCV) unsigned char array - no flipping
97                VI.getPixels(device1, yourBuffer2, true, true);         //fills pixels as a RGB (for openGL) unsigned char array - flipping!
98        }
99       
100        //same applies to device2 etc
101       
102        //to get a settings dialog for the device
103        VI.showSettingsWindow(device1);
104       
105       
106        //Shut down devices properly
107        VI.stopDevice(device1);
108        VI.stopDevice(device2);
109*/
110
111
112//////////////////////////////////////   VARS AND DEFS   //////////////////////////////////
113
114
115//STUFF YOU CAN CHANGE
116
117//change for verbose debug info
118static bool verbose = true;
119
120//if you need VI to use multi threaded com
121#define VI_COM_MULTI_THREADED
122
123//STUFF YOU DON'T CHANGE
124
125//videoInput defines
126#define VI_VERSION       0.1995
127#define VI_MAX_CAMERAS  20
128#define VI_NUM_TYPES    18 //DON'T TOUCH
129#define VI_NUM_FORMATS  18 //DON'T TOUCH
130
131//defines for setPhyCon - tuner is not as well supported as composite and s-video
132#define VI_COMPOSITE 0
133#define VI_S_VIDEO   1
134#define VI_TUNER     2
135#define VI_USB       3
136#define VI_1394          4
137
138//defines for formats
139#define VI_NTSC_M       0
140#define VI_PAL_B        1
141#define VI_PAL_D        2
142#define VI_PAL_G        3
143#define VI_PAL_H        4
144#define VI_PAL_I        5
145#define VI_PAL_M        6
146#define VI_PAL_N        7
147#define VI_PAL_NC       8
148#define VI_SECAM_B      9
149#define VI_SECAM_D      10
150#define VI_SECAM_G      11
151#define VI_SECAM_H      12
152#define VI_SECAM_K      13
153#define VI_SECAM_K1     14
154#define VI_SECAM_L      15
155#define VI_NTSC_M_J     16
156#define VI_NTSC_433     17
157
158
159//allows us to directShow classes here with the includes in the cpp
160struct ICaptureGraphBuilder2;
161struct IGraphBuilder;
162struct IBaseFilter;
163struct IAMCrossbar;
164struct IMediaControl;
165struct ISampleGrabber;
166struct IMediaEventEx;
167struct IAMStreamConfig;
168struct _AMMediaType;
169class SampleGrabberCallback;
170typedef _AMMediaType AM_MEDIA_TYPE;
171
172//keeps track of how many instances of VI are being used
173//don't touch
174static int comInitCount = 0;
175
176
177////////////////////////////////////////   VIDEO DEVICE   ///////////////////////////////////
178
179class videoDevice{
180
181       
182        public:
183                 
184                videoDevice();
185                void setSize(int w, int h);
186                void NukeDownstream(IBaseFilter *pBF);
187                void destroyGraph();
188                ~videoDevice();
189               
190                int videoSize;
191                int width;
192                int height;
193                int tryWidth;
194                int tryHeight;
195               
196                ICaptureGraphBuilder2 *pCaptureGraph;   // Capture graph builder object
197                IGraphBuilder *pGraph;                                  // Graph builder object
198            IMediaControl *pControl;                            // Media control object
199                IBaseFilter *pVideoInputFilter;                 // Video Capture filter
200                IBaseFilter *pGrabberF;
201                IBaseFilter * pDestFilter;
202                IAMStreamConfig *streamConf;
203                ISampleGrabber * pGrabber;                      // Grabs frame
204                AM_MEDIA_TYPE * pAmMediaType;
205               
206                IMediaEventEx * pMediaEvent;
207               
208                GUID videoType;
209                long formatType;
210               
211                SampleGrabberCallback * sgCallback;                             
212               
213                bool tryDiffSize;
214                bool useCrossbar;
215                bool readyToCapture;
216                bool sizeSet;
217                bool setupStarted;
218                bool specificFormat;
219                bool autoReconnect;
220                int  nFramesForReconnect;
221                unsigned long nFramesRunning;
222                int  connection;
223                int      storeConn;
224                int  myID;
225                long requestedFrameTime; //ie fps
226               
227                char    nDeviceName[255];
228                WCHAR   wDeviceName[255];
229               
230                unsigned char * pixels;
231                char * pBuffer;
232
233};
234
235
236
237
238//////////////////////////////////////   VIDEO INPUT   /////////////////////////////////////
239
240
241
242class videoInput{
243
244        public:
245                videoInput();
246                ~videoInput();
247                               
248                //turns off console messages - default is to print messages
249                static void setVerbose(bool _verbose);
250               
251                //Functions in rough order they should be used.
252                static int listDevices(bool silent = false);
253
254                //needs to be called after listDevices - otherwise returns NULL
255                static char * getDeviceName(int deviceID);
256               
257                //choose to use callback based capture - or single threaded
258                void setUseCallback(bool useCallback); 
259               
260                //call before setupDevice
261                //directshow will try and get the closest possible framerate to what is requested
262                void setIdealFramerate(int deviceID, int idealFramerate);
263
264                //some devices will stop delivering frames after a while - this method gives you the option to try and reconnect
265                //to a device if videoInput detects that a device has stopped delivering frames.
266                //you MUST CALL isFrameNew every app loop for this to have any effect
267                void setAutoReconnectOnFreeze(int deviceNumber, bool doReconnect, int numMissedFramesBeforeReconnect);
268               
269                //Choose one of these four to setup your device
270                bool setupDevice(int deviceID);
271                bool setupDevice(int deviceID, int w, int h);
272
273                //These two are only for capture cards
274                //USB and Firewire cameras souldn't specify connection
275                bool setupDevice(int deviceID, int connection); 
276                bool setupDevice(int deviceID, int w, int h, int connection); 
277               
278                //If you need to you can set your NTSC/PAL/SECAM
279                //preference here. if it is available it will be used.
280                //see #defines above for available formats - eg VI_NTSC_M or VI_PAL_B
281                //should be called after setupDevice
282                //can be called multiple times
283                bool setFormat(int deviceNumber, int format);   
284                               
285                //Tells you when a new frame has arrived - you should call this if you have specified setAutoReconnectOnFreeze to true
286                bool isFrameNew(int deviceID); 
287               
288                bool isDeviceSetup(int deviceID);
289                   
290                //Returns the pixels - flipRedAndBlue toggles RGB/BGR flipping - and you can flip the image too
291                unsigned char * getPixels(int deviceID, bool flipRedAndBlue = true, bool flipImage = false);
292               
293                //Or pass in a buffer for getPixels to fill returns true if successful.
294                bool getPixels(int id, unsigned char * pixels, bool flipRedAndBlue = true, bool flipImage = false);
295               
296                //Launches a pop up settings window
297                //For some reason in GLUT you have to call it twice each time.
298                void showSettingsWindow(int deviceID);
299               
300                //Manual control over settings thanks.....
301                //These are experimental for now.
302                bool setVideoSettingFilter(int deviceID, long Property, long lValue, long Flags = NULL, bool useDefaultValue = false);
303                bool setVideoSettingFilterPct(int deviceID, long Property, float pctValue, long Flags = NULL);
304                bool getVideoSettingFilter(int deviceID, long Property, long &min, long &max, long &SteppingDelta, long &currentValue, long &flags, long &defaultValue);
305
306                bool setVideoSettingCamera(int deviceID, long Property, long lValue, long Flags = NULL, bool useDefaultValue = false);
307                bool setVideoSettingCameraPct(int deviceID, long Property, float pctValue, long Flags = NULL);
308                bool getVideoSettingCamera(int deviceID, long Property, long &min, long &max, long &SteppingDelta, long &currentValue, long &flags, long &defaultValue);
309
310                //bool setVideoSettingCam(int deviceID, long Property, long lValue, long Flags = NULL, bool useDefaultValue = false);
311
312                //get width, height and number of pixels
313                int  getWidth(int deviceID);
314                int  getHeight(int deviceID);
315                int  getSize(int deviceID);
316               
317                //completely stops and frees a device
318                void stopDevice(int deviceID);
319               
320                //as above but then sets it up with same settings
321                bool restartDevice(int deviceID);
322               
323                //number of devices available
324                int  devicesFound;
325               
326                long propBrightness;
327                long propContrast;
328                long propHue;
329                long propSaturation;
330                long propSharpness;
331                long propGamma;
332                long propColorEnable;
333                long propWhiteBalance;
334                long propBacklightCompensation;
335                long propGain;
336
337                long propPan;
338                long propTilt;
339                long propRoll;
340                long propZoom;
341                long propExposure;
342                long propIris;
343                long propFocus;
344                               
345               
346        private:               
347                void setPhyCon(int deviceID, int conn);                   
348                void setAttemptCaptureSize(int deviceID, int w, int h);   
349                bool setup(int deviceID);
350                void processPixels(unsigned char * src, unsigned char * dst, int width, int height, bool bRGB, bool bFlip);
351                int  start(int deviceID, videoDevice * VD);                   
352                int  getDeviceCount();
353                void getMediaSubtypeAsString(GUID type, char * typeAsString);
354               
355                HRESULT getDevice(IBaseFilter **pSrcFilter, int deviceID, WCHAR * wDeviceName, char * nDeviceName);
356                static HRESULT ShowFilterPropertyPages(IBaseFilter *pFilter);
357                HRESULT SaveGraphFile(IGraphBuilder *pGraph, WCHAR *wszPath);
358                HRESULT routeCrossbar(ICaptureGraphBuilder2 **ppBuild, IBaseFilter **pVidInFilter, int conType, GUID captureMode);
359                       
360                //don't touch
361                static bool comInit();
362                static bool comUnInit();
363
364                int  connection;
365                int  callbackSetCount;
366                bool bCallback;
367               
368                GUID CAPTURE_MODE;
369               
370                //Extra video subtypes
371                GUID MEDIASUBTYPE_Y800;
372                GUID MEDIASUBTYPE_Y8;
373                GUID MEDIASUBTYPE_GREY;
374
375                videoDevice * VDList[VI_MAX_CAMERAS];
376                GUID mediaSubtypes[VI_NUM_TYPES];
377                long formatTypes[VI_NUM_FORMATS];
378
379                static void __cdecl basicThread(void * objPtr);
380
381                static char deviceNames[VI_MAX_CAMERAS][255];
382
383}; 
384 
385 #endif
Note: See TracBrowser for help on using the repository browser.