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: 22706 $
00027  * $Author: rjongbloed $
00028  * $Date: 2009-05-23 12:49:06 +0000 (Sat, 23 May 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     enum ResizeMode
00055     {
00056         eScale,
00057         eCropCentre,
00058         eCropTopLeft,
00059         eMaxResizeMode
00060     };
00061 
00062     enum StandardSizes {
00063       SQCIFWidth = 128,  SQCIFHeight = 96,
00064       QCIFWidth  = 176,  QCIFHeight  = 144,
00065       CIFWidth   = 352,  CIFHeight   = 288,
00066       CIF4Width  = 704,  CIF4Height  = 576,
00067       CIF16Width = 1408, CIF16Height = 1152,
00068       i480Width  = 640,  i480Height  = 480,
00069       p720Width  = 960,  p720Height  = 720,
00070       i1080Width = 1920, i1080Height = 1080,
00071       HDTVWidth  = 1920, HDTVHeight  = 1080,
00072       MaxWidth   = 1920, MaxHeight   = 1152
00073     };
00074 
00076     PVideoFrameInfo();
00077     PVideoFrameInfo(
00078       unsigned        frameWidth,
00079       unsigned        frameHeight,
00080       const PString & colourFormat = "YUV420P",
00081       unsigned        frameRate = 15,
00082       ResizeMode      resizeMode = eScale
00083     );
00084 
00090     virtual PBoolean SetFrameSize(
00091       unsigned width,   
00092       unsigned height   
00093     );
00094 
00100     virtual PBoolean GetFrameSize(
00101       unsigned & width,
00102       unsigned & height
00103     ) const;
00104 
00109     virtual unsigned GetFrameWidth() const;
00110 
00115     virtual unsigned GetFrameHeight() const;
00116 
00122     virtual PBoolean SetFrameRate(
00123       unsigned rate  
00124     );
00125 
00130     virtual unsigned GetFrameRate() const;
00131 
00137     virtual PBoolean SetColourFormat(
00138       const PString & colourFormat // New colour format for device.
00139     );
00140 
00145     virtual const PString & GetColourFormat() const;
00146 
00149     void SetResizeMode(
00150       ResizeMode mode
00151     ) { if (resizeMode < eMaxResizeMode) resizeMode = mode; }
00152 
00155     ResizeMode GetResizeMode() const { return resizeMode; }
00156 
00159     PINDEX CalculateFrameBytes() const { return CalculateFrameBytes(frameWidth, frameHeight, colourFormat); }
00160     static PINDEX CalculateFrameBytes(
00161       unsigned width,               
00162       unsigned height,              
00163       const PString & colourFormat  
00164     );
00165 
00179     bool Parse(
00180       const PString & str   
00181     );
00182 
00187     static bool ParseSize(
00188       const PString & str,  
00189       unsigned & width,     
00190       unsigned & height     
00191     );
00192 
00195     static PString AsString(
00196       unsigned width,     
00197       unsigned height     
00198     );
00199 
00200   protected:
00201     unsigned   frameWidth;
00202     unsigned   frameHeight;
00203     unsigned   frameRate;
00204     PString    colourFormat;
00205     ResizeMode resizeMode;
00206 };
00207 
00208 
00209 class PVideoControlInfo : public PObject
00210 {
00211   PCLASSINFO(PVideoControlInfo, PObject);
00212 
00213  public:
00214 
00215          typedef enum {
00216         ControlPan,
00217         ControlTilt,
00218         ControlZoom
00219      } InputControlType;
00220 
00221         static PString AsString(const InputControlType & type);
00222 
00223           InputControlType type;
00224           long                                    min;
00225           long                                    max;
00226           long                                    step;
00227           long                                    def;
00228           long                                    flags;
00229           long                                    current;
00230 };
00231 
00232 
00236 class PVideoInputControl : public PVideoControlInfo
00237 {
00238     PCLASSINFO(PVideoInputControl, PVideoControlInfo);
00239 
00240 public:
00241         ~PVideoInputControl();
00242 
00243         virtual PBoolean Pan(long value, bool absolute = false );
00244         virtual PBoolean Tilt(long value, bool absolute = false);
00245         virtual PBoolean Zoom(long value, bool absolute = false);
00246 
00247         long GetPan();
00248         long GetTilt();
00249         long GetZoom();
00250 
00251         void Reset();
00252         void SetCurrentPosition(const InputControlType ctype, long current);
00253 
00254         typedef std::list<PVideoControlInfo> InputDeviceControls;
00255 
00256 protected:
00257         PBoolean GetVideoControlInfo(const InputControlType ctype, PVideoControlInfo & control);
00258         PBoolean GetDefaultPosition(const InputControlType ctype, long & def);
00259         PBoolean GetCurrentPosition(const InputControlType ctype, long & current);
00260 
00261         std::list<PVideoControlInfo> m_info;
00262         PMutex ccmutex;
00263 
00264 };
00265 
00266 
00295 class PVideoDevice : public PVideoFrameInfo
00296 {
00297   PCLASSINFO(PVideoDevice, PVideoFrameInfo);
00298 
00299   protected:
00302     PVideoDevice();
00303 
00304 
00305   public:
00308     virtual ~PVideoDevice();
00309 
00310     enum VideoFormat {
00311       PAL,
00312       NTSC,
00313       SECAM,
00314       Auto,
00315       NumVideoFormats
00316     };
00317 
00320     const PString & GetDeviceName() const
00321       { return deviceName; }
00322 
00325     virtual PStringArray GetDeviceNames() const = 0;
00326 
00327     struct OpenArgs {
00328       OpenArgs();
00329 
00330       PPluginManager * pluginMgr;
00331       PString     driverName;
00332       PString     deviceName;
00333       VideoFormat videoFormat;
00334       int         channelNumber;
00335       PString     colourFormat;
00336       bool        convertFormat;
00337       unsigned    rate;
00338       unsigned    width;
00339       unsigned    height;
00340       bool        convertSize;
00341       ResizeMode  resizeMode;
00342       bool        flip;
00343       int         brightness;
00344       int         whiteness;
00345       int         contrast;
00346       int         colour;
00347       int         hue;
00348     };
00349 
00352     virtual PBoolean OpenFull(
00353       const OpenArgs & args,      
00354       PBoolean startImmediate = PTrue  
00355     );
00356 
00359     virtual PBoolean Open(
00360       const PString & deviceName,   
00361       PBoolean startImmediate = PTrue    
00362     ) = 0;
00363 
00366     virtual PBoolean IsOpen() = 0;
00367 
00370     virtual PBoolean Close() = 0;
00371 
00374     virtual PBoolean Start() = 0;
00375 
00378     virtual PBoolean Stop() = 0;
00379 
00380 
00381 #if PTRACING
00382     friend ostream & operator<<(ostream &, VideoFormat);
00383 #endif
00384 
00390     virtual PBoolean SetVideoFormat(
00391       VideoFormat videoFormat   
00392     );
00393 
00398     virtual VideoFormat GetVideoFormat() const;
00399 
00404     virtual int GetNumChannels();
00405 
00413     virtual PBoolean SetChannel(
00414       int channelNumber  
00415     );
00416 
00421     virtual int GetChannel() const;
00422 
00429     virtual PBoolean SetColourFormatConverter(
00430       const PString & colourFormat // New colour format for device.
00431     );
00432 
00436     virtual PBoolean GetVFlipState();
00437 
00441     virtual PBoolean SetVFlipState(
00442       PBoolean newVFlipState    
00443     );
00444 
00450     virtual PBoolean GetFrameSizeLimits(
00451       unsigned & minWidth,   
00452       unsigned & minHeight,  
00453       unsigned & maxWidth,   
00454       unsigned & maxHeight   
00455     ) ;
00456 
00457 
00463     virtual PBoolean SetFrameSizeConverter(
00464       unsigned width,  
00465       unsigned height, 
00466       ResizeMode resizeMode = eMaxResizeMode 
00467     );
00468 
00474     virtual PBoolean SetFrameSizeConverter(
00475       unsigned width,                   
00476       unsigned height,                  
00477           PBoolean  /*bScaleNotCrop*/           
00478           )  { return SetFrameSizeConverter(width,height,eScale); }
00479 
00480 
00489     virtual PBoolean SetFrameSize(
00490       unsigned width,   
00491       unsigned height   
00492     );
00493 
00499     virtual PBoolean GetFrameSize(
00500       unsigned & width,
00501       unsigned & height
00502     ) const;
00503 
00509     virtual PINDEX GetMaxFrameBytes() = 0;
00510 
00511     
00514     int GetLastError() const { return lastError; }
00515 
00516 
00519     virtual PBoolean CanCaptureVideo() const = 0;
00520 
00523     virtual int GetBrightness();
00524 
00527     virtual PBoolean SetBrightness(unsigned newBrightness);
00528 
00529 
00532     virtual int GetWhiteness();
00533 
00536     virtual PBoolean SetWhiteness(unsigned newWhiteness);
00537 
00538 
00541     virtual int GetColour();
00542 
00545     virtual PBoolean SetColour(unsigned newColour);
00546 
00547 
00550     virtual int GetContrast();
00551 
00554     virtual PBoolean SetContrast(unsigned newContrast);
00555 
00556 
00559     virtual int GetHue();
00560 
00563     virtual PBoolean SetHue(unsigned newHue);
00564     
00565     
00568     virtual PBoolean GetParameters(
00569       int *whiteness,
00570       int *brightness,
00571       int *colour,
00572       int *contrast,
00573       int *hue
00574     );
00575 
00576     
00579     virtual PBoolean SetVideoChannelFormat (
00580       int channelNumber, 
00581       VideoFormat videoFormat
00582     );
00583 
00584 
00588     void SetPreferredColourFormat(const PString & colourFmt) { preferredColourFormat = colourFmt; }
00589 
00593     const PString & GetPreferredColourFormat() { return preferredColourFormat; }
00594     
00595   protected:
00596     PINDEX GetMaxFrameBytesConverted(PINDEX rawFrameBytes) const;
00597 
00598     PString      deviceName;
00599     int          lastError;
00600     VideoFormat  videoFormat;
00601     int          channelNumber;
00602     // Preferred native colour format from video input device, empty == no preference
00603     PString      preferredColourFormat;
00604     PBoolean         nativeVerticalFlip;
00605 
00606     PColourConverter * converter;
00607     PBYTEArray         frameStore;
00608 
00609     int          frameBrightness; // 16 bit entity, -1 is no value
00610     int          frameWhiteness;
00611     int          frameContrast;
00612     int          frameColour;
00613     int          frameHue;
00614 };
00615 
00616 
00619 class PVideoOutputDevice : public PVideoDevice
00620 {
00621   PCLASSINFO(PVideoOutputDevice, PVideoDevice);
00622 
00623   public:
00626     PVideoOutputDevice();
00627     
00630     virtual ~PVideoOutputDevice() { Close(); };      
00631 
00634     static PStringArray GetDriverNames(
00635       PPluginManager * pluginMgr = NULL   
00636     );
00637 
00644     static PStringArray GetDriversDeviceNames(
00645       const PString & driverName,         
00646       PPluginManager * pluginMgr = NULL   
00647     );
00648 
00651     static PVideoOutputDevice * CreateDevice(
00652       const PString & driverName,         
00653       PPluginManager * pluginMgr = NULL   
00654     );
00655 
00656     /* Create the matching video output device that corresponds to the device name.
00657 
00658        This is typically used with the return values from GetDriversDeviceNames().
00659      */
00660     static PVideoOutputDevice *CreateDeviceByName(
00661       const PString & deviceName,         
00662       const PString & driverName = PString::Empty(),  
00663       PPluginManager * pluginMgr = NULL   
00664     );
00665 
00671     static PVideoOutputDevice *CreateOpenedDevice(
00672       const PString & driverName,         
00673       const PString & deviceName,         
00674       PBoolean startImmediate = PTrue,         
00675       PPluginManager * pluginMgr = NULL   
00676     );
00677 
00680     static PVideoOutputDevice *CreateOpenedDevice(
00681       const OpenArgs & args,              
00682       PBoolean startImmediate = PTrue          
00683     );
00684 
00687     virtual PBoolean Close() { return PTrue; }
00688 
00691     virtual PBoolean Start() { return PTrue; }
00692 
00695     virtual PBoolean Stop() { return PTrue; }
00696 
00699     virtual PBoolean CanCaptureVideo() const;
00700 
00703     virtual PBoolean SetFrameData(
00704       unsigned x,
00705       unsigned y,
00706       unsigned width,
00707       unsigned height,
00708       const BYTE * data,
00709       PBoolean endFrame = PTrue
00710     ) = 0;
00711     virtual PBoolean SetFrameData(
00712       unsigned x,
00713       unsigned y,
00714       unsigned width,
00715       unsigned height,
00716       const BYTE * data,
00717       PBoolean endFrame,
00718       unsigned flags
00719     );
00720 
00727     virtual PBoolean GetPosition(
00728       int & x,  // X position of device surface
00729       int & y   // Y position of device surface
00730     ) const;
00731 };
00732 
00733 
00736 class PVideoOutputDeviceRGB : public PVideoOutputDevice
00737 {
00738   PCLASSINFO(PVideoOutputDeviceRGB, PVideoOutputDevice);
00739 
00740   public:
00743     PVideoOutputDeviceRGB();
00744 
00755     virtual PBoolean SetColourFormat(
00756       const PString & colourFormat // New colour format for device.
00757     );
00758 
00767     virtual PBoolean SetFrameSize(
00768       unsigned width,   
00769       unsigned height   
00770     );
00771 
00777     virtual PINDEX GetMaxFrameBytes();
00778 
00781     virtual PBoolean SetFrameData(
00782       unsigned x,
00783       unsigned y,
00784       unsigned width,
00785       unsigned height,
00786       const BYTE * data,
00787       PBoolean endFrame = PTrue
00788     );
00789 
00792     virtual PBoolean FrameComplete() = 0;
00793 
00794   protected:
00795     PMutex     mutex;
00796     PINDEX     bytesPerPixel;
00797     PINDEX     scanLineWidth;
00798     bool       swappedRedAndBlue;
00799 };
00800 
00801 
00802 #ifdef SHOULD_BE_MOVED_TO_PLUGIN
00803 
00806 class PVideoOutputDevicePPM : public PVideoOutputDeviceRGB
00807 {
00808   PCLASSINFO(PVideoOutputDevicePPM, PVideoOutputDeviceRGB);
00809 
00810   public:
00813     PVideoOutputDevicePPM();
00814 
00817     virtual PBoolean Open(
00818       const PString & deviceName,   
00819       PBoolean startImmediate = PTrue    
00820     );
00821 
00824     virtual PBoolean IsOpen();
00825 
00828     virtual PBoolean Close();
00829 
00832     virtual PStringArray GetDeviceNames() const;
00833 
00836     virtual PBoolean EndFrame();
00837 
00838   protected:
00839     unsigned   frameNumber;
00840 };
00841 
00842 #endif // SHOULD_BE_MOVED_TO_PLUGIN
00843 
00844 
00847 class PVideoInputDevice : public PVideoDevice
00848 {
00849   PCLASSINFO(PVideoInputDevice, PVideoDevice);
00850 
00851   public:
00854     //PVideoInputDevice();
00855 
00858     ~PVideoInputDevice() { Close(); }
00859 
00862     static PStringArray GetDriverNames(
00863       PPluginManager * pluginMgr = NULL   
00864     );
00865 
00872     static PStringArray GetDriversDeviceNames(
00873       const PString & driverName,         
00874       PPluginManager * pluginMgr = NULL   
00875     );
00876 
00879     static PVideoInputDevice *CreateDevice(
00880       const PString & driverName,         
00881       PPluginManager * pluginMgr = NULL   
00882     );
00883 
00884     /* Create the matching video input device that corresponds to the device name.
00885        So, for "fake" return a device that will generate fake video.
00886        For "Phillips 680 webcam" (eg) will return appropriate grabber.
00887        Note that Phillips will return the appropriate grabber also.
00888 
00889        This is typically used with the return values from GetDriversDeviceNames().
00890      */
00891     static PVideoInputDevice *CreateDeviceByName(
00892       const PString & deviceName,         
00893       const PString & driverName = PString::Empty(),  
00894       PPluginManager * pluginMgr = NULL   
00895     );
00896 
00902     static PVideoInputDevice *CreateOpenedDevice(
00903       const PString & driverName,         
00904       const PString & deviceName,         
00905       PBoolean startImmediate = PTrue,         
00906       PPluginManager * pluginMgr = NULL   
00907     );
00908 
00911     static PVideoInputDevice *CreateOpenedDevice(
00912       const OpenArgs & args,              
00913       PBoolean startImmediate = PTrue          
00914     );
00915 
00916         typedef struct {
00917            std::list<PVideoFrameInfo> framesizes;
00918            std::list<PVideoControlInfo> controls;
00919         } Capabilities;
00920 
00923     virtual bool GetDeviceCapabilities(
00924       Capabilities * capabilities          
00925     ) const { return GetDeviceCapabilities(GetDeviceName(), capabilities); }
00926 
00929     static PBoolean GetDeviceCapabilities(
00930       const PString & deviceName,           
00931       Capabilities * capabilities,          
00932       PPluginManager * pluginMgr = NULL     
00933     );
00934 
00937     static PBoolean GetDeviceCapabilities(
00938       const PString & deviceName,           
00939       const PString & driverName,           
00940       Capabilities * caps,                  
00941       PPluginManager * pluginMgr = NULL     
00942     );
00943 
00946     virtual PBoolean Open(
00947       const PString & deviceName,   
00948       PBoolean startImmediate = PTrue    
00949     ) = 0;
00950 
00951     virtual PBoolean Close(
00952     ) { return PTrue; }
00953 
00956     virtual PBoolean CanCaptureVideo() const;
00957  
00960     virtual PBoolean IsCapturing() = 0;
00961 
00964     virtual PBoolean GetFrame(
00965       PBYTEArray & frame
00966     );
00967 
00970     virtual PBoolean GetFrameData(
00971       BYTE * buffer,                 
00972       PINDEX * bytesReturned,        
00973       unsigned int & flags           
00974     );
00975     virtual PBoolean GetFrameData(
00976       BYTE * buffer,                 
00977       PINDEX * bytesReturned = NULL  
00978     ) = 0;
00979 
00982     virtual PBoolean GetFrameDataNoDelay(
00983       BYTE * buffer,                 
00984       PINDEX * bytesReturned,       
00985       unsigned int & flags           
00986     );
00987     virtual PBoolean GetFrameDataNoDelay(
00988       BYTE * buffer,                 
00989       PINDEX * bytesReturned = NULL  
00990     ) = 0;
00991 
00994     virtual PBoolean TestAllFormats() = 0;
00995 };
00996 
00997 
00999 //
01000 // declare macros and structures needed for video input plugins
01001 //
01002 
01003 template <class className> class PVideoInputPluginServiceDescriptor : public PDevicePluginServiceDescriptor
01004 {
01005   public:
01006     virtual PObject *    CreateInstance(int /*userData*/) const { return new className; }
01007     virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetInputDeviceNames(); }
01008     virtual bool         GetDeviceCapabilities(const PString & deviceName, void * caps) const
01009       { return className::GetDeviceCapabilities(deviceName, (PVideoInputDevice::Capabilities *)caps); }
01010 };
01011 
01012 #define PCREATE_VIDINPUT_PLUGIN(name) \
01013   static PVideoInputPluginServiceDescriptor<PVideoInputDevice_##name> PVideoInputDevice_##name##_descriptor; \
01014   PCREATE_PLUGIN(name, PVideoInputDevice, &PVideoInputDevice_##name##_descriptor)
01015 
01016 PPLUGIN_STATIC_LOAD(FakeVideo, PVideoInputDevice);
01017 
01018 #ifdef P_APPSHARE
01019   PPLUGIN_STATIC_LOAD(Application, PVideoInputDevice);
01020 #endif
01021 
01022 #if P_FFVDEV
01023   PPLUGIN_STATIC_LOAD(FFMPEG, PVideoInputDevice);
01024 #endif
01025 
01026 #if P_VIDFILE
01027   PPLUGIN_STATIC_LOAD(YUVFile, PVideoInputDevice);
01028 #endif
01029 
01030 
01032 //
01033 // declare macros and structures needed for video output plugins
01034 //
01035 
01036 template <class className> class PVideoOutputPluginServiceDescriptor : public PDevicePluginServiceDescriptor
01037 {
01038   public:
01039     virtual PObject *    CreateInstance(int /*userData*/) const { return new className; }
01040     virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::GetOutputDeviceNames(); }
01041 };
01042 
01043 #define PCREATE_VIDOUTPUT_PLUGIN(name) \
01044   static PVideoOutputPluginServiceDescriptor<PVideoOutputDevice_##name> PVideoOutputDevice_##name##_descriptor; \
01045   PCREATE_PLUGIN(name, PVideoOutputDevice, &PVideoOutputDevice_##name##_descriptor)
01046 
01047 #if _WIN32
01048   PPLUGIN_STATIC_LOAD(Window, PVideoOutputDevice);
01049 #endif
01050 
01051 #if P_SDL
01052   PPLUGIN_STATIC_LOAD(SDL, PVideoOutputDevice);
01053 #endif
01054 
01055 
01057 //
01058 // declare classes needed for access to simple video font
01059 //
01060 
01061 class PVideoFont : public PObject
01062 {
01063   PCLASSINFO(PVideoFont, PObject);
01064   public:
01065     enum {
01066       MAX_L_HEIGHT = 11
01067     };
01068     struct LetterData {
01069       char ascii;
01070       const char *line[MAX_L_HEIGHT];
01071     };
01072 
01073     static const LetterData * GetLetterData(char ascii);
01074 };
01075 
01076 #endif // P_VIDEO
01077 
01078 #endif   // PTLIB_PVIDEOIO_H
01079 
01080 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Aug 3 20:41:51 2009 for PTLib by  doxygen 1.5.1