Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
webcam_support.h
Go to the documentation of this file.
1 #ifndef __WEBCAM_SUPPORT_H
2 #define __WEBCAM_SUPPORT_H
3 
4 #include <string>
5 #if defined (_WIN32) && !defined (__CYGWIN__) && defined (NETCOMM_WEBCAM)
6 #include <dshow.h>
7 #include <qedit.h>
8 class WebcamSupport;
9 
10 //Global DShow variable
11 extern AM_MEDIA_TYPE g_StillMediaType;
12 //DirectShow works with a callback interface...
13 class SampleGrabberCallback : public ISampleGrabberCB
14 {
15 public:
16  WebcamSupport *ws;
17  STDMETHODIMP_( ULONG ) AddRef()
18  {
19  return 1;
20  }
21  STDMETHODIMP_( ULONG ) Release()
22  {
23  return 2;
24  }
25 
26  STDMETHODIMP QueryInterface( REFIID riid, void **ppvObject )
27  {
28  if (NULL == ppvObject) return E_POINTER;
29  if ( riid == __uuidof( IUnknown ) ) {
30  *ppvObject = static_cast< IUnknown* > (this);
31  return S_OK;
32  }
33  if ( riid == __uuidof( ISampleGrabberCB ) ) {
34  *ppvObject = static_cast< ISampleGrabberCB* > (this);
35  return S_OK;
36  }
37  return E_NOTIMPL;
38  }
39 
40  STDMETHODIMP SampleCB( double Time, IMediaSample *pSample )
41  {
42  return E_NOTIMPL;
43  }
44 
45  STDMETHODIMP BufferCB( double Time, BYTE *pBuffer, long BufferLen );
46 };
47 #endif
48 #ifdef __APPLE__
49 #include <QuickTimeComponents.h>
50 pascal OSErr processFrame( SGChannel c,
51  Ptr p,
52  long len,
53  long *offset,
54  long chRefCon,
55  TimeValue time,
56  short writeType,
57  long refcon );
58 #endif
59 #define DEFAULT_CAPTURE_DRIVER 0
60 //This is a limit for jpeg size so we just don't consider bigger shots (should even be less)
61 #define MAX_JPEG_SIZE 5000
62 
63 //CAPTURE WINDOW SIZE MUST BE DETERMINED AND THEREFORE WILL NOT BE ADJUSTABLE
64 
65 /*
66  * Webcam Support : class that provide webcam image capture for both win32 and Linux for now
67  * the use of this class requires a few parameters in vegastrike.config :
68  * FOR LINUX :
69  * - video_device (/dev/video is default if none provided)
70  * - video_mode (NTSC is default)
71  * - channel_mode (defaults to CHANNEL_TUNER)
72  * - region (defaults to REGION_NTSC_CABLE)
73  *
74  */
75 
76 #ifdef linux
77 /* Framegrabber device structure */
78 
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <unistd.h>
82 #include <string.h>
83 
84 #include <sys/time.h>
85 
86 #include <sys/types.h>
87 #include <sys/stat.h>
88 #include <sys/ioctl.h>
89 #include <sys/mman.h>
90 #include <fcntl.h>
91 #include <linux/types.h>
92 
93 #include <linux/videodev.h>
94 
95 #include <pthread.h>
96 
97 /* ------ Defines used for framegrabber setup */
98 
99 /* SETTING_ used for fg_get_setting() and fg_set_setting() */
100 
101 #define SETTING_BRIGHTNESS 0
102 #define SETTING_HUE 1
103 #define SETTING_COLOUR 2
104 #define SETTING_CONTRAST 3
105 
106 /* CHANNEL_ used for fg_set_channel() */
107 
108 #define CHANNEL_TUNER 0
109 #define CHANNEL_COMPOSITE 1
110 #define CHANNEL_SVIDEO 2
111 
112 /* VIDEOMODE_ used for fg_set_channel() */
113 
114 #define VIDEOMODE_PAL VIDEO_MODE_PAL
115 #define VIDEOMODE_NTSC VIDEO_MODE_NTSC
116 #define VIDEOMODE_SECAM VIDEO_MODE_SECAM
117 
118 /* FORMAT_ used by fg_start_grab_image() */
119 
120 #define FORMAT_GREY VIDEO_PALETTE_GREY
121 #define FORMAT_RGB565 VIDEO_PALETTE_RGB565
122 #define FORMAT_RGB24 VIDEO_PALETTE_RGB24
123 #define FORMAT_RGB32 VIDEO_PALETTE_RGB32
124 #define FORMAT_YUV422P VIDEO_PALETTE_YUV422P
125 #define FORMAT_YUV420P VIDEO_PALETTE_YUV420P
126 
127 /* REGION_ used by fg_set_frequency() */
128 
129 #define REGION_NTSC_BROADCAST 0
130 #define REGION_NTSC_CABLE 1
131 #define REGION_NTSC_CABLE_HRC 2
132 #define REGION_NTSC_BROADCAST_JAPAN 3
133 #define REGION_NTSC_CABLE_JAPAN 4
134 #define REGION_PAL_EUROPE 5
135 #define REGION_PAL_EUROPE_EAST 6
136 #define REGION_PAL_ITALY 7
137 #define REGION_PAL_NEWZEALAND 8
138 #define REGION_PAL_AUSTRALIA 9
139 #define REGION_PAL_IRELAND 10
140 
141 /* ------- Defines for internal use */
142 
143 #define IMAGE_BUFFER_EMPTY 0
144 #define IMAGE_BUFFER_FULL 1
145 #define IMAGE_BUFFER_INUSE 2
146 
147 static const int error_exit_status = -1;
148 
149 /* Channel frequency tables */
150 
151 #define NUM_CHANNEL_LISTS 11
152 struct CHANLIST
153 {
154  char *name;
155  int freq;
156 };
157 struct CHANLISTS
158 {
159  char *name;
160  struct CHANLIST *list;
161  int count;
162 };
163 #define CHAN_COUNT( x ) ( sizeof (x)/sizeof (struct CHANLIST) )
164 
165 /* ------- Framegrabber device structure */
166 struct fgdevice
167 {
168  int video_dev;
169  int width;
170  int height;
171  int input;
172  int format;
173  struct video_mmap vid_mmap[2];
174  int current_grab_number;
175  struct video_mbuf vid_mbuf;
176  char *video_map;
177  int grabbing_active;
178  int have_new_frame;
179  void *current_image;
180  pthread_mutex_t buffer_mutex;
181  pthread_t grab_thread;
182  pthread_cond_t buffer_cond;
183  int totalframecount;
184  int image_size;
185  int image_pixels;
186  int framecount;
187  int fps_update_interval;
188  double fps;
189  double lasttime;
190  unsigned short *y8_to_rgb565;
191  unsigned char *rgb565_to_y8;
192 };
193 
194 /* --------- Function prototypes */
195 
196 #ifdef __cplusplus
197 extern "C"
198 {
199 #endif
200 
201 int fg_open_device( struct fgdevice *fg, const char *devicename );
202 int fg_print_info( struct fgdevice *fg );
203 int fg_close_device( struct fgdevice *fg );
204 
205 void fg_set_fps_interval( struct fgdevice *fg, int interval );
206 double fg_get_fps( struct fgdevice *fg );
207 
208 int fg_set_channel( struct fgdevice *fg, int channel, int videomode );
209 int fg_set_frequency( struct fgdevice *fg, int region, int index );
210 
211 int fg_get_setting( struct fgdevice *fg, int which_setting );
212 int fg_set_setting( struct fgdevice *fg, int which_setting, int value );
213 
214 int fg_start_grab_image( struct fgdevice *fg, int width, int height, int format );
215 int fg_stop_grab_image( struct fgdevice *fg );
216 void * fg_get_next_image( struct fgdevice *fg );
217 
218 double timeGet( void );
219 
220 int getFrequency( int region, int channel );
221 
222 #ifdef __cplusplus
223 }
224 #endif
225 #endif
226 
228 {
229 private:
230 #ifdef linux
231  struct fgdevice fg;
232 
233  int region;
234  int channel;
235  int oldchannel;
236  char channeltext[128];
237 #endif
238 #if defined (_WIN32) && !defined (__CYGWIN__) && defined (NETCOMM_WEBCAM)
239  SampleGrabberCallback g_SampleCB;
240  IGraphBuilder *pGraph;
241  ICaptureGraphBuilder2 *pCaptureGraph;
242  ICreateDevEnum *pDevEnum;
243  IEnumMoniker *pEnum;
244  IMoniker *pMoniker;
245  IBaseFilter *pCap;
246  IBaseFilter *pNull;
247  IMediaControl *pControl;
248  ISampleGrabber *pSampleGrabber;
249 
250  friend class SampleGrabberCallback;
251 #endif
252 #ifdef __APPLE__
253  typedef struct
254  {
255  SeqGrabComponent sg_component;
256  SGChannel sg_channel;
257  GWorldPtr sg_world;
258  short video_width, video_height;
259  } videoRec;
260  Boolean gQuicktimeInitialized;
261 //Sequence Grabber info
262  videoRec *video;
263 #endif
264 
265  int width;
266  int height;
267  int depth;
268  int fps;
269  double last_time;
270  double period;
271  bool grabbing;
272  int jpeg_quality;
273 
274 public:
275 //Buffer containing current jpeg image capture
276  char *old_buffer;
277  int old_size;
278  char *jpeg_buffer;
280  int nbframes;
281 
282  WebcamSupport();
283  WebcamSupport( int f, int w, int h );
284 
285  int Init();
286  void Shutdown();
287  void GetInfo();
288  int GetFps()
289  {
290  return this->fps;
291  }
292  bool isReady();
293 
294  void StartCapture();
295  void EndCapture();
296  std::string CaptureImage();
297  int GetCapturedSize();
298 
299  int CopyImage();
300  void DoError( long code, char *msg );
301 };
302 //Windows specific stuff to convert BMP to JPEG
303 #if defined (_WIN32) && !defined (__CYGWIN__)
304 /*
305  * #ifndef HAVE_BOOLEAN
306  * #define HAVE_BOOLEAN
307  * #define FALSE 0
308  * #define TRUE 1
309  * typedef unsigned char boolean;
310  * #endif
311  * #ifndef XMD_H
312  * #define XMD_H
313  * typedef int INT32;
314  * #endif
315  */
316 #ifdef _WIN32
317 #ifndef NOMINMAX
318 #define NOMINMAX
319 #endif //tells VCC not to generate min/max macros
320 #include <windows.h>
321 #endif
322 #include <string>
323 
324 extern "C" {
325 #include "jconfig.h"
326 #include "jmorecfg.h"
327 #include "jpeglib.h"
328 }
329 
330 char * JpegFromCapture( HANDLE hDib, BYTE *bmpBuffer, long bmpLength, int *jpegLength, int nQuality, std::string csJpeg = "" );
331 char * JpegFromBmp( BITMAPFILEHEADER &bfh,
332  LPBITMAPINFOHEADER &lpbi,
333  BYTE *bmpbuffer,
334  long BufferLen,
335  int *jpglength,
336  int quality,
337  std::string csJpeg );
338 BOOL JpegFromDib( HANDLE hDib, //Handle to DIB
339  int nQuality, //JPEG quality (0-100)
340  std::string csJpeg, //Pathname to target jpeg file
341  std::string *pcsMsg ); //Error msg to return
342 
343 BOOL DibToSamps2( BITMAPFILEHEADER &bfh,
344  LPBITMAPINFOHEADER &pbBmHdr,
345  BYTE *bmpbuffer,
346  int nSampsPerRow,
347  struct jpeg_compress_struct cinfo,
348  JSAMPARRAY jsmpPixels );
349 BOOL DibToSamps( HANDLE hDib, int nSampsPerRow, struct jpeg_compress_struct cinfo, JSAMPARRAY jsmpPixels, std::string *pcsMsg );
350 
351 RGBQUAD QuadFromWord( WORD b16 );
352 #endif
353 
354 #endif
355