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: 21506 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-11-12 23:48:07 +0000 (Wed, 12 Nov 2008) $
00029  */
00030 
00031 
00032 #ifndef _PVIDEOIO
00033 #define _PVIDEOIO
00034 
00035 #ifdef P_USE_PRAGMA
00036 #pragma interface
00037 #endif
00038 
00039 #ifndef _PTLIB_H
00040 #include <ptlib.h>
00041 #endif
00042 
00043 #if P_VIDEO
00044 
00045 #include <ptlib/plugin.h>
00046 #include <ptlib/pluginmgr.h>
00047 #include <list>
00048 
00049 class PColourConverter;
00050 
00051 
00052 class PVideoFrameInfo : public PObject
00053 {
00054   PCLASSINFO(PVideoFrameInfo, PObject);
00055 
00056   public:
00057       PVideoFrameInfo();
00058 
00059     enum ResizeMode
00060     {
00061         eScale,
00062         eCropCentre,
00063         eCropTopLeft,
00064         eMaxResizeMode
00065     };
00066 
00067     enum StandardSizes {
00068       SQCIFWidth = 128,  SQCIFHeight = 96,
00069       QCIFWidth  = 176,  QCIFHeight  = 144,
00070       CIFWidth   = 352,  CIFHeight   = 288,
00071       CIF4Width  = 704,  CIF4Height  = 576,
00072       CIF16Width = 1408, CIF16Height = 1152,
00073       i480Width  = 640,  i480Height  = 480,
00074       p720Width  = 960,  p720Height  = 720,
00075       i1080Width = 1920, i1080Height = 1080,
00076       HDTVWidth  = 1920, HDTVHeight  = 1080,
00077       MaxWidth   = 1920, MaxHeight   = 1152
00078     };
00079 
00085     virtual PBoolean SetFrameSize(
00086       unsigned width,   
00087       unsigned height   
00088     );
00089 
00095     virtual PBoolean GetFrameSize(
00096       unsigned & width,
00097       unsigned & height
00098     ) const;
00099 
00104     virtual unsigned GetFrameWidth() const;
00105 
00110     virtual unsigned GetFrameHeight() const;
00111 
00117     virtual PBoolean SetFrameRate(
00118       unsigned rate  
00119     );
00120 
00125     virtual unsigned GetFrameRate() const;
00126 
00132     virtual PBoolean SetColourFormat(
00133       const PString & colourFormat // New colour format for device.
00134     );
00135 
00140     virtual const PString & GetColourFormat() const;
00141 
00144     void SetResizeMode(
00145       ResizeMode mode
00146     ) { if (resizeMode < eMaxResizeMode) resizeMode = mode; }
00147 
00150     ResizeMode GetResizeMode() const { return resizeMode; }
00151 
00154     PINDEX CalculateFrameBytes() const { return CalculateFrameBytes(frameWidth, frameHeight, colourFormat); }
00155     static PINDEX CalculateFrameBytes(
00156       unsigned width,               
00157       unsigned height,              
00158       const PString & colourFormat  
00159     );
00160 
00163     static PBoolean ParseSize(
00164       const PString & str,  
00165       unsigned & width,     
00166       unsigned & height     
00167     );
00168 
00169   protected:
00170     unsigned   frameWidth;
00171     unsigned   frameHeight;
00172     unsigned   frameRate;
00173     PString    colourFormat;
00174     ResizeMode resizeMode;
00175 };
00176 
00177 
00178 class PVideoControlInfo : public PObject
00179 {
00180   PCLASSINFO(PVideoControlInfo, PObject);
00181 
00182  public:
00183 
00184          typedef enum {
00185         ControlPan,
00186         ControlTilt,
00187         ControlZoom
00188      } InputControlType;
00189 
00190         static PString AsString(const InputControlType & type);
00191 
00192           InputControlType type;
00193           long                                    min;
00194           long                                    max;
00195           long                                    step;
00196           long                                    def;
00197           long                                    flags;
00198           long                                    current;
00199 };
00200 
00201 
00205 class PVideoInputControl : public PVideoControlInfo
00206 {
00207     PCLASSINFO(PVideoInputControl, PVideoControlInfo);
00208 
00209 public:
00210         ~PVideoInputControl();
00211 
00212         virtual PBoolean Pan(long value, bool absolute = false );
00213         virtual PBoolean Tilt(long value, bool absolute = false);
00214         virtual PBoolean Zoom(long value, bool absolute = false);
00215 
00216         long GetPan();
00217         long GetTilt();
00218         long GetZoom();
00219 
00220         void Reset();
00221         void SetCurrentPosition(const InputControlType ctype, long current);
00222 
00223         typedef std::list<PVideoControlInfo> InputDeviceControls;
00224 
00225 protected:
00226         PBoolean GetVideoControlInfo(const InputControlType ctype, PVideoControlInfo & control);
00227         PBoolean GetDefaultPosition(const InputControlType ctype, long & def);
00228         PBoolean GetCurrentPosition(const InputControlType ctype, long & current);
00229 
00230         std::list<PVideoControlInfo> m_info;
00231         PMutex ccmutex;
00232 
00233 };
00234 
00235 
00264 class PVideoDevice : public PVideoFrameInfo
00265 {
00266   PCLASSINFO(PVideoDevice, PVideoFrameInfo);
00267 
00268   protected:
00271     PVideoDevice();
00272 
00273 
00274   public:
00277     virtual ~PVideoDevice();
00278 
00279     enum VideoFormat {
00280       PAL,
00281       NTSC,
00282       SECAM,
00283       Auto,
00284       NumVideoFormats
00285     };
00286 
00289     const PString & GetDeviceName() const
00290       { return deviceName; }
00291 
00294     virtual PStringArray GetDeviceNames() const = 0;
00295 
00296     struct OpenArgs {
00297       OpenArgs();
00298 
00299       PPluginManager * pluginMgr;
00300       PString     driverName;
00301       PString     deviceName;
00302       VideoFormat videoFormat;
00303       int         channelNumber;
00304       PString     colourFormat;
00305       bool        convertFormat;
00306       unsigned    rate;
00307       unsigned    width;
00308       unsigned    height;
00309       bool        convertSize;
00310       ResizeMode  resizeMode;
00311       bool        flip;
00312       int         brightness;
00313       int         whiteness;
00314       int         contrast;
00315       int         colour;
00316       int         hue;
00317     };
00318 
00321     virtual PBoolean OpenFull(
00322       const OpenArgs & args,      
00323       PBoolean startImmediate = PTrue  
00324     );
00325 
00328     virtual PBoolean Open(
00329       const PString & deviceName,   
00330       PBoolean startImmediate = PTrue    
00331     ) = 0;
00332 
00335     virtual PBoolean IsOpen() = 0;
00336 
00339     virtual PBoolean Close() = 0;
00340 
00343     virtual PBoolean Start() = 0;
00344 
00347     virtual PBoolean Stop() = 0;
00348 
00349 
00350 #if PTRACING
00351     friend ostream & operator<<(ostream &, VideoFormat);
00352 #endif
00353 
00359     virtual PBoolean SetVideoFormat(
00360       VideoFormat videoFormat   
00361     );
00362 
00367     virtual VideoFormat GetVideoFormat() const;
00368 
00373     virtual int GetNumChannels();
00374 
00382     virtual PBoolean SetChannel(
00383       int channelNumber  
00384     );
00385 
00390     virtual int GetChannel() const;
00391 
00398     virtual PBoolean SetColourFormatConverter(
00399       const PString & colourFormat // New colour format for device.
00400     );
00401 
00405     virtual PBoolean GetVFlipState();
00406 
00410     virtual PBoolean SetVFlipState(
00411       PBoolean newVFlipState    
00412     );
00413 
00419     virtual PBoolean GetFrameSizeLimits(
00420       unsigned & minWidth,   
00421       unsigned & minHeight,  
00422       unsigned & maxWidth,   
00423       unsigned & maxHeight   
00424     ) ;
00425 
00426 
00432     virtual PBoolean SetFrameSizeConverter(
00433       unsigned width,  
00434       unsigned height, 
00435       ResizeMode resizeMode = eMaxResizeMode 
00436     );
00437 
00443     virtual PBoolean SetFrameSizeConverter(
00444       unsigned width,                   
00445       unsigned height,                  
00446           PBoolean  /*bScaleNotCrop*/           
00447           )  { return SetFrameSizeConverter(width,height,eScale); }
00448 
00449 
00458     virtual PBoolean SetFrameSize(
00459       unsigned width,   
00460       unsigned height   
00461     );
00462 
00468     virtual PBoolean GetFrameSize(
00469       unsigned & width,
00470       unsigned & height
00471     ) const;
00472 
00478     virtual PINDEX GetMaxFrameBytes() = 0;
00479 
00480     
00483     int GetLastError() const { return lastError; }
00484 
00485 
00488     virtual PBoolean CanCaptureVideo() const = 0;
00489 
00492     virtual int GetBrightness();
00493 
00496     virtual PBoolean SetBrightness(unsigned newBrightness);
00497 
00498 
00501     virtual int GetWhiteness();
00502 
00505     virtual PBoolean SetWhiteness(unsigned newWhiteness);
00506 
00507 
00510     virtual int GetColour();
00511 
00514     virtual PBoolean SetColour(unsigned newColour);
00515 
00516 
00519     virtual int GetContrast();
00520 
00523     virtual PBoolean SetContrast(unsigned newContrast);
00524 
00525 
00528     virtual int GetHue();
00529 
00532     virtual PBoolean SetHue(unsigned newHue);
00533     
00534     
00537     virtual PBoolean GetParameters(
00538       int *whiteness,
00539       int *brightness,
00540       int *colour,
00541       int *contrast,
00542       int *hue
00543     );
00544 
00545     
00548     virtual PBoolean SetVideoChannelFormat (
00549       int channelNumber, 
00550       VideoFormat videoFormat
00551     );
00552 
00553 
00557     void SetPreferredColourFormat(const PString & colourFmt) { preferredColourFormat = colourFmt; }
00558 
00562     const PString & GetPreferredColourFormat() { return preferredColourFormat; }
00563     
00564   protected:
00565     PINDEX GetMaxFrameBytesConverted(PINDEX rawFrameBytes) const;
00566 
00567     PString      deviceName;
00568     int          lastError;
00569     VideoFormat  videoFormat;
00570     int          channelNumber;
00571     // Preferred native colour format from video input device, empty == no preference
00572     PString      preferredColourFormat;
00573     PBoolean         nativeVerticalFlip;
00574 
00575     PColourConverter * converter;
00576     PBYTEArray         frameStore;
00577 
00578     int          frameBrightness; // 16 bit entity, -1 is no value
00579     int          frameWhiteness;
00580     int          frameContrast;
00581     int          frameColour;
00582     int          frameHue;
00583 };
00584 
00585 
00588 class PVideoOutputDevice : public PVideoDevice
00589 {
00590   PCLASSINFO(PVideoOutputDevice, PVideoDevice);
00591 
00592   public:
00595     PVideoOutputDevice();
00596     
00599     virtual ~PVideoOutputDevice() { Close(); };      
00600 
00603     static PStringArray GetDriverNames(
00604       PPluginManager * pluginMgr = NULL   
00605     );
00606 
00613     static PStringArray GetDriversDeviceNames(
00614       const PString & driverName,         
00615       PPluginManager * pluginMgr = NULL   
00616     );
00617 
00620     static PVideoOutputDevice * CreateDevice(
00621       const PString & driverName,         
00622       PPluginManager * pluginMgr = NULL   
00623     );
00624 
00625     /* Create the matching video output device that corresponds to the device name.
00626 
00627        This is typically used with the return values from GetDriversDeviceNames().
00628      */
00629     static PVideoOutputDevice *CreateDeviceByName(
00630       const PString & deviceName,         
00631       const PString & driverName = PString::Empty(),  
00632       PPluginManager * pluginMgr = NULL   
00633     );
00634 
00640     static PVideoOutputDevice *CreateOpenedDevice(
00641       const PString & driverName,         
00642       const PString & deviceName,         
00643       PBoolean startImmediate = PTrue,         
00644       PPluginManager * pluginMgr = NULL   
00645     );
00646 
00649     static PVideoOutputDevice *CreateOpenedDevice(
00650       const OpenArgs & args,              
00651       PBoolean startImmediate = PTrue          
00652     );
00653 
00656     virtual PBoolean Close() { return PTrue; }
00657 
00660     virtual PBoolean Start() { return PTrue; }
00661 
00664     virtual PBoolean Stop() { return PTrue; }
00665 
00668     virtual PBoolean CanCaptureVideo() const;
00669 
00672     virtual PBoolean SetFrameData(
00673       unsigned x,
00674       unsigned y,
00675       unsigned width,
00676       unsigned height,
00677       const BYTE * data,
00678       PBoolean endFrame = PTrue
00679     ) = 0;
00680     virtual PBoolean SetFrameData(
00681       unsigned x,
00682       unsigned y,
00683       unsigned width,
00684       unsigned height,
00685       const BYTE * data,
00686       PBoolean endFrame,
00687       unsigned flags
00688     );
00689 
00696     virtual PBoolean GetPosition(
00697       int & x,  // X position of device surface
00698       int & y   // Y position of device surface
00699     ) const;
00700 };
00701 
00702 
00705 class PVideoOutputDeviceRGB : public PVideoOutputDevice
00706 {
00707   PCLASSINFO(PVideoOutputDeviceRGB, PVideoOutputDevice);
00708 
00709   public:
00712     PVideoOutputDeviceRGB();
00713 
00724     virtual PBoolean SetColourFormat(
00725       const PString & colourFormat // New colour format for device.
00726     );
00727 
00736     virtual PBoolean SetFrameSize(
00737       unsigned width,   
00738       unsigned height   
00739     );
00740 
00746     virtual PINDEX GetMaxFrameBytes();
00747 
00750     virtual PBoolean SetFrameData(
00751       unsigned x,
00752       unsigned y,
00753       unsigned width,
00754       unsigned height,
00755       const BYTE * data,
00756       PBoolean endFrame = PTrue
00757     );
00758 
00761     virtual PBoolean FrameComplete() = 0;
00762 
00763   protected:
00764     PMutex     mutex;
00765     PINDEX     bytesPerPixel;
00766     PINDEX     scanLineWidth;
00767     bool       swappedRedAndBlue;
00768 };
00769 
00770 
00771 #ifdef SHOULD_BE_MOVED_TO_PLUGIN
00772 
00775 class PVideoOutputDevicePPM : public PVideoOutputDeviceRGB
00776 {
00777   PCLASSINFO(PVideoOutputDevicePPM, PVideoOutputDeviceRGB);
00778 
00779   public:
00782     PVideoOutputDevicePPM();
00783 
00786     virtual PBoolean Open(
00787       const PString & deviceName,   
00788       PBoolean startImmediate = PTrue    
00789     );
00790 
00793     virtual PBoolean IsOpen();
00794 
00797     virtual PBoolean Close();
00798 
00801     virtual PStringArray GetDeviceNames() const;
00802 
00805     virtual PBoolean EndFrame();
00806 
00807   protected:
00808     unsigned   frameNumber;
00809 };
00810 
00811 #endif // SHOULD_BE_MOVED_TO_PLUGIN
00812 
00813 
00816 class PVideoInputDevice : public PVideoDevice
00817 {
00818   PCLASSINFO(PVideoInputDevice, PVideoDevice);
00819 
00820   public:
00823     //PVideoInputDevice();
00824 
00827     ~PVideoInputDevice() { Close(); }
00828 
00831     static PStringArray GetDriverNames(
00832       PPluginManager * pluginMgr = NULL   
00833     );
00834 
00841     static PStringArray GetDriversDeviceNames(
00842       const PString & driverName,         
00843       PPluginManager * pluginMgr = NULL   
00844     );
00845 
00848     static PVideoInputDevice *CreateDevice(
00849       const PString & driverName,         
00850       PPluginManager * pluginMgr = NULL   
00851     );
00852 
00853     /* Create the matching video input device that corresponds to the device name.
00854        So, for "fake" return a device that will generate fake video.
00855        For "Phillips 680 webcam" (eg) will return appropriate grabber.
00856        Note that Phillips will return the appropriate grabber also.
00857 
00858        This is typically used with the return values from GetDriversDeviceNames().
00859      */
00860     static PVideoInputDevice *CreateDeviceByName(
00861       const PString & deviceName,         
00862       const PString & driverName = PString::Empty(),  
00863       PPluginManager * pluginMgr = NULL   
00864     );
00865 
00871     static PVideoInputDevice *CreateOpenedDevice(
00872       const PString & driverName,         
00873       const PString & deviceName,         
00874       PBoolean startImmediate = PTrue,         
00875       PPluginManager * pluginMgr = NULL   
00876     );
00877 
00880     static PVideoInputDevice *CreateOpenedDevice(
00881       const OpenArgs & args,              
00882       PBoolean startImmediate = PTrue          
00883     );
00884 
00885         typedef struct {
00886            std::list<PVideoFrameInfo> framesizes;
00887            std::list<PVideoControlInfo> controls;
00888         } Capabilities;
00889 
00892     virtual bool GetDeviceCapabilities(
00893       Capabilities * capabilities          
00894     ) const { return GetDeviceCapabilities(GetDeviceName(), capabilities); }
00895 
00898     static PBoolean GetDeviceCapabilities(
00899       const PString & deviceName,           
00900       Capabilities * capabilities,          
00901       PPluginManager * pluginMgr = NULL     
00902     );
00903 
00906     static PBoolean GetDeviceCapabilities(
00907       const PString & deviceName,           
00908       const PString & driverName,           
00909       Capabilities * caps,                  
00910       PPluginManager * pluginMgr = NULL     
00911     );
00912 
00915     virtual PBoolean Open(
00916       const PString & deviceName,   
00917       PBoolean startImmediate = PTrue    
00918     ) = 0;
00919 
00920     virtual PBoolean Close(
00921     ) { return PTrue; }
00922 
00925     virtual PBoolean CanCaptureVideo() const;
00926  
00929     virtual PBoolean IsCapturing() = 0;
00930 
00933     virtual PBoolean GetFrame(
00934       PBYTEArray & frame
00935     );
00936 
00939     virtual PBoolean GetFrameData(
00940       BYTE * buffer,                 
00941       PINDEX * bytesReturned,        
00942       unsigned int & flags           
00943     );
00944     virtual PBoolean GetFrameData(
00945       BYTE * buffer,                 
00946       PINDEX * bytesReturned = NULL  
00947     ) = 0;
00948 
00951     virtual PBoolean GetFrameDataNoDelay(
00952       BYTE * buffer,                 
00953       PINDEX * bytesReturned,       
00954       unsigned int & flags           
00955     );
00956     virtual PBoolean GetFrameDataNoDelay(
00957       BYTE * buffer,                 
00958       PINDEX * bytesReturned = NULL  
00959     ) = 0;
00960 
00963     virtual PBoolean TestAllFormats() = 0;
00964 };
00965 
00966 
00968 //
00969 // declare macros and structures needed for video input plugins
00970 //
00971 
00972 template <class className> class PVideoInputPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00973 {
00974   public:
00975     virtual PObject *    CreateInstance(int /*userData*/) const { return new className; }
00976     virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetInputDeviceNames(); }
00977     virtual bool         GetDeviceCapabilities(const PString & deviceName, void * caps) const
00978       { return className::GetDeviceCapabilities(deviceName, (PVideoInputDevice::Capabilities *)caps); }
00979 };
00980 
00981 #define PCREATE_VIDINPUT_PLUGIN(name) \
00982   static PVideoInputPluginServiceDescriptor<PVideoInputDevice_##name> PVideoInputDevice_##name##_descriptor; \
00983   PCREATE_PLUGIN(name, PVideoInputDevice, &PVideoInputDevice_##name##_descriptor)
00984 
00986 //
00987 // declare macros and structures needed for video output plugins
00988 //
00989 
00990 template <class className> class PVideoOutputPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00991 {
00992   public:
00993     virtual PObject *    CreateInstance(int /*userData*/) const { return new className; }
00994     virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetOutputDeviceNames(); }
00995 };
00996 
00997 #define PCREATE_VIDOUTPUT_PLUGIN(name) \
00998   static PVideoOutputPluginServiceDescriptor<PVideoOutputDevice_##name> PVideoOutputDevice_##name##_descriptor; \
00999   PCREATE_PLUGIN(name, PVideoOutputDevice, &PVideoOutputDevice_##name##_descriptor)
01000 
01002 //
01003 // declare classes needed for access to simple video font
01004 //
01005 
01006 class PVideoFont : public PObject
01007 {
01008   PCLASSINFO(PVideoFont, PObject);
01009   public:
01010     enum {
01011       MAX_L_HEIGHT = 11
01012     };
01013     struct LetterData {
01014       char ascii;
01015       const char *line[MAX_L_HEIGHT];
01016     };
01017 
01018     static const LetterData * GetLetterData(char ascii);
01019 };
01020 
01021 #endif // P_VIDEO
01022 
01023 #endif   // _PVIDEOIO
01024 
01025 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Feb 23 01:57:54 2009 for PTLib by  doxygen 1.5.1