videoio.h

Go to the documentation of this file.
00001 /*
00002  * videoio.h
00003  *
00004  * Classes to support streaming video input (grabbing) and output.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-2000 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Contributor(s): Mark Cooke (mpc@star.sr.bham.ac.uk)
00025  *
00026  * $Revision: 22375 $
00027  * $Author: rjongbloed $
00028  * $Date: 2009-04-06 21:47:23 -0500 (Mon, 06 Apr 2009) $
00029  */
00030 
00031 
00032 #ifndef PTLIB_PVIDEOIO_H
00033 #define PTLIB_PVIDEOIO_H
00034 
00035 #ifdef P_USE_PRAGMA
00036 #pragma interface
00037 #endif
00038 #include <ptbuildopts.h>
00039 
00040 #if P_VIDEO
00041 
00042 #include <ptlib/plugin.h>
00043 #include <ptlib/pluginmgr.h>
00044 #include <list>
00045 
00046 class PColourConverter;
00047 
00048 
00049 class PVideoFrameInfo : public PObject
00050 {
00051   PCLASSINFO(PVideoFrameInfo, PObject);
00052 
00053   public:
00054       PVideoFrameInfo();
00055 
00056     enum ResizeMode
00057     {
00058         eScale,
00059         eCropCentre,
00060         eCropTopLeft,
00061         eMaxResizeMode
00062     };
00063 
00064     enum StandardSizes {
00065       SQCIFWidth = 128,  SQCIFHeight = 96,
00066       QCIFWidth  = 176,  QCIFHeight  = 144,
00067       CIFWidth   = 352,  CIFHeight   = 288,
00068       CIF4Width  = 704,  CIF4Height  = 576,
00069       CIF16Width = 1408, CIF16Height = 1152,
00070       i480Width  = 640,  i480Height  = 480,
00071       p720Width  = 960,  p720Height  = 720,
00072       i1080Width = 1920, i1080Height = 1080,
00073       HDTVWidth  = 1920, HDTVHeight  = 1080,
00074       MaxWidth   = 1920, MaxHeight   = 1152
00075     };
00076 
00082     virtual PBoolean SetFrameSize(
00083       unsigned width,   
00084       unsigned height   
00085     );
00086 
00092     virtual PBoolean GetFrameSize(
00093       unsigned & width,
00094       unsigned & height
00095     ) const;
00096 
00101     virtual unsigned GetFrameWidth() const;
00102 
00107     virtual unsigned GetFrameHeight() const;
00108 
00114     virtual PBoolean SetFrameRate(
00115       unsigned rate  
00116     );
00117 
00122     virtual unsigned GetFrameRate() const;
00123 
00129     virtual PBoolean SetColourFormat(
00130       const PString & colourFormat // New colour format for device.
00131     );
00132 
00137     virtual const PString & GetColourFormat() const;
00138 
00141     void SetResizeMode(
00142       ResizeMode mode
00143     ) { if (resizeMode < eMaxResizeMode) resizeMode = mode; }
00144 
00147     ResizeMode GetResizeMode() const { return resizeMode; }
00148 
00151     PINDEX CalculateFrameBytes() const { return CalculateFrameBytes(frameWidth, frameHeight, colourFormat); }
00152     static PINDEX CalculateFrameBytes(
00153       unsigned width,               
00154       unsigned height,              
00155       const PString & colourFormat  
00156     );
00157 
00160     static PBoolean ParseSize(
00161       const PString & str,  
00162       unsigned & width,     
00163       unsigned & height     
00164     );
00165 
00166   protected:
00167     unsigned   frameWidth;
00168     unsigned   frameHeight;
00169     unsigned   frameRate;
00170     PString    colourFormat;
00171     ResizeMode resizeMode;
00172 };
00173 
00174 
00175 class PVideoControlInfo : public PObject
00176 {
00177   PCLASSINFO(PVideoControlInfo, PObject);
00178 
00179  public:
00180 
00181          typedef enum {
00182         ControlPan,
00183         ControlTilt,
00184         ControlZoom
00185      } InputControlType;
00186 
00187         static PString AsString(const InputControlType & type);
00188 
00189           InputControlType type;
00190           long                                    min;
00191           long                                    max;
00192           long                                    step;
00193           long                                    def;
00194           long                                    flags;
00195           long                                    current;
00196 };
00197 
00198 
00202 class PVideoInputControl : public PVideoControlInfo
00203 {
00204     PCLASSINFO(PVideoInputControl, PVideoControlInfo);
00205 
00206 public:
00207         ~PVideoInputControl();
00208 
00209         virtual PBoolean Pan(long value, bool absolute = false );
00210         virtual PBoolean Tilt(long value, bool absolute = false);
00211         virtual PBoolean Zoom(long value, bool absolute = false);
00212 
00213         long GetPan();
00214         long GetTilt();
00215         long GetZoom();
00216 
00217         void Reset();
00218         void SetCurrentPosition(const InputControlType ctype, long current);
00219 
00220         typedef std::list<PVideoControlInfo> InputDeviceControls;
00221 
00222 protected:
00223         PBoolean GetVideoControlInfo(const InputControlType ctype, PVideoControlInfo & control);
00224         PBoolean GetDefaultPosition(const InputControlType ctype, long & def);
00225         PBoolean GetCurrentPosition(const InputControlType ctype, long & current);
00226 
00227         std::list<PVideoControlInfo> m_info;
00228         PMutex ccmutex;
00229 
00230 };
00231 
00232 
00261 class PVideoDevice : public PVideoFrameInfo
00262 {
00263   PCLASSINFO(PVideoDevice, PVideoFrameInfo);
00264 
00265   protected:
00268     PVideoDevice();
00269 
00270 
00271   public:
00274     virtual ~PVideoDevice();
00275 
00276     enum VideoFormat {
00277       PAL,
00278       NTSC,
00279       SECAM,
00280       Auto,
00281       NumVideoFormats
00282     };
00283 
00286     const PString & GetDeviceName() const
00287       { return deviceName; }
00288 
00291     virtual PStringArray GetDeviceNames() const = 0;
00292 
00293     struct OpenArgs {
00294       OpenArgs();
00295 
00296       PPluginManager * pluginMgr;
00297       PString     driverName;
00298       PString     deviceName;
00299       VideoFormat videoFormat;
00300       int         channelNumber;
00301       PString     colourFormat;
00302       bool        convertFormat;
00303       unsigned    rate;
00304       unsigned    width;
00305       unsigned    height;
00306       bool        convertSize;
00307       ResizeMode  resizeMode;
00308       bool        flip;
00309       int         brightness;
00310       int         whiteness;
00311       int         contrast;
00312       int         colour;
00313       int         hue;
00314     };
00315 
00318     virtual PBoolean OpenFull(
00319       const OpenArgs & args,      
00320       PBoolean startImmediate = PTrue  
00321     );
00322 
00325     virtual PBoolean Open(
00326       const PString & deviceName,   
00327       PBoolean startImmediate = PTrue    
00328     ) = 0;
00329 
00332     virtual PBoolean IsOpen() = 0;
00333 
00336     virtual PBoolean Close() = 0;
00337 
00340     virtual PBoolean Start() = 0;
00341 
00344     virtual PBoolean Stop() = 0;
00345 
00346 
00347 #if PTRACING
00348     friend ostream & operator<<(ostream &, VideoFormat);
00349 #endif
00350 
00356     virtual PBoolean SetVideoFormat(
00357       VideoFormat videoFormat   
00358     );
00359 
00364     virtual VideoFormat GetVideoFormat() const;
00365 
00370     virtual int GetNumChannels();
00371 
00379     virtual PBoolean SetChannel(
00380       int channelNumber  
00381     );
00382 
00387     virtual int GetChannel() const;
00388 
00395     virtual PBoolean SetColourFormatConverter(
00396       const PString & colourFormat // New colour format for device.
00397     );
00398 
00402     virtual PBoolean GetVFlipState();
00403 
00407     virtual PBoolean SetVFlipState(
00408       PBoolean newVFlipState    
00409     );
00410 
00416     virtual PBoolean GetFrameSizeLimits(
00417       unsigned & minWidth,   
00418       unsigned & minHeight,  
00419       unsigned & maxWidth,   
00420       unsigned & maxHeight   
00421     ) ;
00422 
00423 
00429     virtual PBoolean SetFrameSizeConverter(
00430       unsigned width,  
00431       unsigned height, 
00432       ResizeMode resizeMode = eMaxResizeMode 
00433     );
00434 
00440     virtual PBoolean SetFrameSizeConverter(
00441       unsigned width,                   
00442       unsigned height,                  
00443           PBoolean  /*bScaleNotCrop*/           
00444           )  { return SetFrameSizeConverter(width,height,eScale); }
00445 
00446 
00455     virtual PBoolean SetFrameSize(
00456       unsigned width,   
00457       unsigned height   
00458     );
00459 
00465     virtual PBoolean GetFrameSize(
00466       unsigned & width,
00467       unsigned & height
00468     ) const;
00469 
00475     virtual PINDEX GetMaxFrameBytes() = 0;
00476 
00477     
00480     int GetLastError() const { return lastError; }
00481 
00482 
00485     virtual PBoolean CanCaptureVideo() const = 0;
00486 
00489     virtual int GetBrightness();
00490 
00493     virtual PBoolean SetBrightness(unsigned newBrightness);
00494 
00495 
00498     virtual int GetWhiteness();
00499 
00502     virtual PBoolean SetWhiteness(unsigned newWhiteness);
00503 
00504 
00507     virtual int GetColour();
00508 
00511     virtual PBoolean SetColour(unsigned newColour);
00512 
00513 
00516     virtual int GetContrast();
00517 
00520     virtual PBoolean SetContrast(unsigned newContrast);
00521 
00522 
00525     virtual int GetHue();
00526 
00529     virtual PBoolean SetHue(unsigned newHue);
00530     
00531     
00534     virtual PBoolean GetParameters(
00535       int *whiteness,
00536       int *brightness,
00537       int *colour,
00538       int *contrast,
00539       int *hue
00540     );
00541 
00542     
00545     virtual PBoolean SetVideoChannelFormat (
00546       int channelNumber, 
00547       VideoFormat videoFormat
00548     );
00549 
00550 
00554     void SetPreferredColourFormat(const PString & colourFmt) { preferredColourFormat = colourFmt; }
00555 
00559     const PString & GetPreferredColourFormat() { return preferredColourFormat; }
00560     
00561   protected:
00562     PINDEX GetMaxFrameBytesConverted(PINDEX rawFrameBytes) const;
00563 
00564     PString      deviceName;
00565     int          lastError;
00566     VideoFormat  videoFormat;
00567     int          channelNumber;
00568     // Preferred native colour format from video input device, empty == no preference
00569     PString      preferredColourFormat;
00570     PBoolean         nativeVerticalFlip;
00571 
00572     PColourConverter * converter;
00573     PBYTEArray         frameStore;
00574 
00575     int          frameBrightness; // 16 bit entity, -1 is no value
00576     int          frameWhiteness;
00577     int          frameContrast;
00578     int          frameColour;
00579     int          frameHue;
00580 };
00581 
00582 
00585 class PVideoOutputDevice : public PVideoDevice
00586 {
00587   PCLASSINFO(PVideoOutputDevice, PVideoDevice);
00588 
00589   public:
00592     PVideoOutputDevice();
00593     
00596     virtual ~PVideoOutputDevice() { Close(); };      
00597 
00600     static PStringArray GetDriverNames(
00601       PPluginManager * pluginMgr = NULL   
00602     );
00603 
00610     static PStringArray GetDriversDeviceNames(
00611       const PString & driverName,         
00612       PPluginManager * pluginMgr = NULL   
00613     );
00614 
00617     static PVideoOutputDevice * CreateDevice(
00618       const PString & driverName,         
00619       PPluginManager * pluginMgr = NULL   
00620     );
00621 
00622     /* Create the matching video output device that corresponds to the device name.
00623 
00624        This is typically used with the return values from GetDriversDeviceNames().
00625      */
00626     static PVideoOutputDevice *CreateDeviceByName(
00627       const PString & deviceName,         
00628       const PString & driverName = PString::Empty(),  
00629       PPluginManager * pluginMgr = NULL   
00630     );
00631 
00637     static PVideoOutputDevice *CreateOpenedDevice(
00638       const PString & driverName,         
00639       const PString & deviceName,         
00640       PBoolean startImmediate = PTrue,         
00641       PPluginManager * pluginMgr = NULL   
00642     );
00643 
00646     static PVideoOutputDevice *CreateOpenedDevice(
00647       const OpenArgs & args,              
00648       PBoolean startImmediate = PTrue          
00649     );
00650 
00653     virtual PBoolean Close() { return PTrue; }
00654 
00657     virtual PBoolean Start() { return PTrue; }
00658 
00661     virtual PBoolean Stop() { return PTrue; }
00662 
00665     virtual PBoolean CanCaptureVideo() const;
00666 
00669     virtual PBoolean SetFrameData(
00670       unsigned x,
00671       unsigned y,
00672       unsigned width,
00673       unsigned height,
00674       const BYTE * data,
00675       PBoolean endFrame = PTrue
00676     ) = 0;
00677     virtual PBoolean SetFrameData(
00678       unsigned x,
00679       unsigned y,
00680       unsigned width,
00681       unsigned height,
00682       const BYTE * data,
00683       PBoolean endFrame,
00684       unsigned flags
00685     );
00686 
00693     virtual PBoolean GetPosition(
00694       int & x,  // X position of device surface
00695       int & y   // Y position of device surface
00696     ) const;
00697 };
00698 
00699 
00702 class PVideoOutputDeviceRGB : public PVideoOutputDevice
00703 {
00704   PCLASSINFO(PVideoOutputDeviceRGB, PVideoOutputDevice);
00705 
00706   public:
00709     PVideoOutputDeviceRGB();
00710 
00721     virtual PBoolean SetColourFormat(
00722       const PString & colourFormat // New colour format for device.
00723     );
00724 
00733     virtual PBoolean SetFrameSize(
00734       unsigned width,   
00735       unsigned height   
00736     );
00737 
00743     virtual PINDEX GetMaxFrameBytes();
00744 
00747     virtual PBoolean SetFrameData(
00748       unsigned x,
00749       unsigned y,
00750       unsigned width,
00751       unsigned height,
00752       const BYTE * data,
00753       PBoolean endFrame = PTrue
00754     );
00755 
00758     virtual PBoolean FrameComplete() = 0;
00759 
00760   protected:
00761     PMutex     mutex;
00762     PINDEX     bytesPerPixel;
00763     PINDEX     scanLineWidth;
00764     bool       swappedRedAndBlue;
00765 };
00766 
00767 
00768 #ifdef SHOULD_BE_MOVED_TO_PLUGIN
00769 
00772 class PVideoOutputDevicePPM : public PVideoOutputDeviceRGB
00773 {
00774   PCLASSINFO(PVideoOutputDevicePPM, PVideoOutputDeviceRGB);
00775 
00776   public:
00779     PVideoOutputDevicePPM();
00780 
00783     virtual PBoolean Open(
00784       const PString & deviceName,   
00785       PBoolean startImmediate = PTrue    
00786     );
00787 
00790     virtual PBoolean IsOpen();
00791 
00794     virtual PBoolean Close();
00795 
00798     virtual PStringArray GetDeviceNames() const;
00799 
00802     virtual PBoolean EndFrame();
00803 
00804   protected:
00805     unsigned   frameNumber;
00806 };
00807 
00808 #endif // SHOULD_BE_MOVED_TO_PLUGIN
00809 
00810 
00813 class PVideoInputDevice : public PVideoDevice
00814 {
00815   PCLASSINFO(PVideoInputDevice, PVideoDevice);
00816 
00817   public:
00820     //PVideoInputDevice();
00821 
00824     ~PVideoInputDevice() { Close(); }
00825 
00828     static PStringArray GetDriverNames(
00829       PPluginManager * pluginMgr = NULL   
00830     );
00831 
00838     static PStringArray GetDriversDeviceNames(
00839       const PString & driverName,         
00840       PPluginManager * pluginMgr = NULL   
00841     );
00842 
00845     static PVideoInputDevice *CreateDevice(
00846       const PString & driverName,         
00847       PPluginManager * pluginMgr = NULL   
00848     );
00849 
00850     /* Create the matching video input device that corresponds to the device name.
00851        So, for "fake" return a device that will generate fake video.
00852        For "Phillips 680 webcam" (eg) will return appropriate grabber.
00853        Note that Phillips will return the appropriate grabber also.
00854 
00855        This is typically used with the return values from GetDriversDeviceNames().
00856      */
00857     static PVideoInputDevice *CreateDeviceByName(
00858       const PString & deviceName,         
00859       const PString & driverName = PString::Empty(),  
00860       PPluginManager * pluginMgr = NULL   
00861     );
00862 
00868     static PVideoInputDevice *CreateOpenedDevice(
00869       const PString & driverName,         
00870       const PString & deviceName,         
00871       PBoolean startImmediate = PTrue,         
00872       PPluginManager * pluginMgr = NULL   
00873     );
00874 
00877     static PVideoInputDevice *CreateOpenedDevice(
00878       const OpenArgs & args,              
00879       PBoolean startImmediate = PTrue          
00880     );
00881 
00882         typedef struct {
00883            std::list<PVideoFrameInfo> framesizes;
00884            std::list<PVideoControlInfo> controls;
00885         } Capabilities;
00886 
00889     virtual bool GetDeviceCapabilities(
00890       Capabilities * capabilities          
00891     ) const { return GetDeviceCapabilities(GetDeviceName(), capabilities); }
00892 
00895     static PBoolean GetDeviceCapabilities(
00896       const PString & deviceName,           
00897       Capabilities * capabilities,          
00898       PPluginManager * pluginMgr = NULL     
00899     );
00900 
00903     static PBoolean GetDeviceCapabilities(
00904       const PString & deviceName,           
00905       const PString & driverName,           
00906       Capabilities * caps,                  
00907       PPluginManager * pluginMgr = NULL     
00908     );
00909 
00912     virtual PBoolean Open(
00913       const PString & deviceName,   
00914       PBoolean startImmediate = PTrue    
00915     ) = 0;
00916 
00917     virtual PBoolean Close(
00918     ) { return PTrue; }
00919 
00922     virtual PBoolean CanCaptureVideo() const;
00923  
00926     virtual PBoolean IsCapturing() = 0;
00927 
00930     virtual PBoolean GetFrame(
00931       PBYTEArray & frame
00932     );
00933 
00936     virtual PBoolean GetFrameData(
00937       BYTE * buffer,                 
00938       PINDEX * bytesReturned,        
00939       unsigned int & flags           
00940     );
00941     virtual PBoolean GetFrameData(
00942       BYTE * buffer,                 
00943       PINDEX * bytesReturned = NULL  
00944     ) = 0;
00945 
00948     virtual PBoolean GetFrameDataNoDelay(
00949       BYTE * buffer,                 
00950       PINDEX * bytesReturned,       
00951       unsigned int & flags           
00952     );
00953     virtual PBoolean GetFrameDataNoDelay(
00954       BYTE * buffer,                 
00955       PINDEX * bytesReturned = NULL  
00956     ) = 0;
00957 
00960     virtual PBoolean TestAllFormats() = 0;
00961 };
00962 
00963 
00965 //
00966 // declare macros and structures needed for video input plugins
00967 //
00968 
00969 template <class className> class PVideoInputPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00970 {
00971   public:
00972     virtual PObject *    CreateInstance(int /*userData*/) const { return new className; }
00973     virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetInputDeviceNames(); }
00974     virtual bool         GetDeviceCapabilities(const PString & deviceName, void * caps) const
00975       { return className::GetDeviceCapabilities(deviceName, (PVideoInputDevice::Capabilities *)caps); }
00976 };
00977 
00978 #define PCREATE_VIDINPUT_PLUGIN(name) \
00979   static PVideoInputPluginServiceDescriptor<PVideoInputDevice_##name> PVideoInputDevice_##name##_descriptor; \
00980   PCREATE_PLUGIN(name, PVideoInputDevice, &PVideoInputDevice_##name##_descriptor)
00981 
00983 //
00984 // declare macros and structures needed for video output plugins
00985 //
00986 
00987 template <class className> class PVideoOutputPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00988 {
00989   public:
00990     virtual PObject *    CreateInstance(int /*userData*/) const { return new className; }
00991     virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetOutputDeviceNames(); }
00992 };
00993 
00994 #define PCREATE_VIDOUTPUT_PLUGIN(name) \
00995   static PVideoOutputPluginServiceDescriptor<PVideoOutputDevice_##name> PVideoOutputDevice_##name##_descriptor; \
00996   PCREATE_PLUGIN(name, PVideoOutputDevice, &PVideoOutputDevice_##name##_descriptor)
00997 
00999 //
01000 // declare classes needed for access to simple video font
01001 //
01002 
01003 class PVideoFont : public PObject
01004 {
01005   PCLASSINFO(PVideoFont, PObject);
01006   public:
01007     enum {
01008       MAX_L_HEIGHT = 11
01009     };
01010     struct LetterData {
01011       char ascii;
01012       const char *line[MAX_L_HEIGHT];
01013     };
01014 
01015     static const LetterData * GetLetterData(char ascii);
01016 };
01017 
01018 #endif // P_VIDEO
01019 
01020 #endif   // PTLIB_PVIDEOIO_H
01021 
01022 // End Of File ///////////////////////////////////////////////////////////////

Generated on Thu May 27 01:36:48 2010 for PTLib by  doxygen 1.4.7