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: 19032 $
00027  * $Author: rjongbloed $
00028  * $Date: 2007-12-03 11:08:47 +0000 (Mon, 03 Dec 2007) $
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       HDTVWidth  = 1920, HDTVHeight  = 1080,
00074     };
00075 
00081     virtual PBoolean SetFrameSize(
00082       unsigned width,   
00083       unsigned height   
00084     );
00085 
00091     virtual PBoolean GetFrameSize(
00092       unsigned & width,
00093       unsigned & height
00094     ) const;
00095 
00100     virtual unsigned GetFrameWidth() const;
00101 
00106     virtual unsigned GetFrameHeight() const;
00107 
00113     virtual PBoolean SetFrameRate(
00114       unsigned rate  
00115     );
00116 
00121     virtual unsigned GetFrameRate() const;
00122 
00128     virtual PBoolean SetColourFormat(
00129       const PString & colourFormat // New colour format for device.
00130     );
00131 
00136     virtual const PString & GetColourFormat() const;
00137 
00140     void SetResizeMode(
00141       ResizeMode mode
00142     ) { if (resizeMode < eMaxResizeMode) resizeMode = mode; }
00143 
00146     ResizeMode GetResizeMode() const { return resizeMode; }
00147 
00150     PINDEX CalculateFrameBytes() const { return CalculateFrameBytes(frameWidth, frameHeight, colourFormat); }
00151     static PINDEX CalculateFrameBytes(
00152       unsigned width,               
00153       unsigned height,              
00154       const PString & colourFormat  
00155     );
00156 
00159     static PBoolean ParseSize(
00160       const PString & str,  
00161       unsigned & width,     
00162       unsigned & height     
00163     );
00164 
00165   protected:
00166     unsigned   frameWidth;
00167     unsigned   frameHeight;
00168     unsigned   frameRate;
00169     PString    colourFormat;
00170     ResizeMode resizeMode;
00171 };
00172 
00173 
00202 class PVideoDevice : public PVideoFrameInfo
00203 {
00204   PCLASSINFO(PVideoDevice, PVideoFrameInfo);
00205 
00206   protected:
00209     PVideoDevice();
00210 
00211 
00212   public:
00215     virtual ~PVideoDevice();
00216 
00217     enum VideoFormat {
00218       PAL,
00219       NTSC,
00220       SECAM,
00221       Auto,
00222       NumVideoFormats
00223     };
00224 
00227     const PString & GetDeviceName() const
00228       { return deviceName; }
00229 
00232     virtual PStringList GetDeviceNames() const = 0;
00233 
00234     struct OpenArgs {
00235       OpenArgs();
00236 
00237       PPluginManager * pluginMgr;
00238       PString     driverName;
00239       PString     deviceName;
00240       VideoFormat videoFormat;
00241       int         channelNumber;
00242       PString     colourFormat;
00243       bool        convertFormat;
00244       unsigned    rate;
00245       unsigned    width;
00246       unsigned    height;
00247       bool        convertSize;
00248       ResizeMode  resizeMode;
00249       bool        flip;
00250       int         brightness;
00251       int         whiteness;
00252       int         contrast;
00253       int         colour;
00254       int         hue;
00255     };
00256 
00259     virtual PBoolean OpenFull(
00260       const OpenArgs & args,      
00261       PBoolean startImmediate = PTrue  
00262     );
00263 
00266     virtual PBoolean Open(
00267       const PString & deviceName,   
00268       PBoolean startImmediate = PTrue    
00269     ) = 0;
00270 
00273     virtual PBoolean IsOpen() = 0;
00274 
00277     virtual PBoolean Close() = 0;
00278 
00281     virtual PBoolean Start() = 0;
00282 
00285     virtual PBoolean Stop() = 0;
00286 
00287 
00288 #if PTRACING
00289     friend ostream & operator<<(ostream &, VideoFormat);
00290 #endif
00291 
00297     virtual PBoolean SetVideoFormat(
00298       VideoFormat videoFormat   
00299     );
00300 
00305     virtual VideoFormat GetVideoFormat() const;
00306 
00311     virtual int GetNumChannels();
00312 
00320     virtual PBoolean SetChannel(
00321       int channelNumber  
00322     );
00323 
00328     virtual int GetChannel() const;
00329 
00336     virtual PBoolean SetColourFormatConverter(
00337       const PString & colourFormat // New colour format for device.
00338     );
00339 
00343     virtual PBoolean GetVFlipState();
00344 
00348     virtual PBoolean SetVFlipState(
00349       PBoolean newVFlipState    
00350     );
00351 
00357     virtual PBoolean GetFrameSizeLimits(
00358       unsigned & minWidth,   
00359       unsigned & minHeight,  
00360       unsigned & maxWidth,   
00361       unsigned & maxHeight   
00362     ) ;
00363 
00364 
00370     virtual PBoolean SetFrameSizeConverter(
00371       unsigned width,  
00372       unsigned height, 
00373       ResizeMode resizeMode = eMaxResizeMode 
00374     );
00375 
00381     virtual PBoolean SetFrameSizeConverter(
00382       unsigned width,                   
00383       unsigned height,                  
00384           PBoolean  /*bScaleNotCrop*/           
00385           )  { return SetFrameSizeConverter(width,height,eScale); }
00386 
00387 
00396     virtual PBoolean SetFrameSize(
00397       unsigned width,   
00398       unsigned height   
00399     );
00400 
00406     virtual PBoolean GetFrameSize(
00407       unsigned & width,
00408       unsigned & height
00409     ) const;
00410 
00416     virtual PINDEX GetMaxFrameBytes() = 0;
00417 
00418     
00421     int GetLastError() const { return lastError; }
00422 
00423 
00426     virtual PBoolean CanCaptureVideo() const = 0;
00427 
00430     virtual int GetBrightness();
00431 
00434     virtual PBoolean SetBrightness(unsigned newBrightness);
00435 
00436 
00439     virtual int GetWhiteness();
00440 
00443     virtual PBoolean SetWhiteness(unsigned newWhiteness);
00444 
00445 
00448     virtual int GetColour();
00449 
00452     virtual PBoolean SetColour(unsigned newColour);
00453 
00454 
00457     virtual int GetContrast();
00458 
00461     virtual PBoolean SetContrast(unsigned newContrast);
00462 
00463 
00466     virtual int GetHue();
00467 
00470     virtual PBoolean SetHue(unsigned newHue);
00471     
00472     
00475     virtual PBoolean GetParameters(
00476       int *whiteness,
00477       int *brightness,
00478       int *colour,
00479       int *contrast,
00480       int *hue
00481     );
00482 
00483     
00486     virtual PBoolean SetVideoChannelFormat (
00487       int channelNumber, 
00488       VideoFormat videoFormat
00489     );
00490 
00491 
00495     void SetPreferredColourFormat(const PString & colourFmt) { preferredColourFormat = colourFmt; }
00496 
00500     const PString & GetPreferredColourFormat() { return preferredColourFormat; }
00501     
00502     int GetNumberOfFrames () { return numberOfFrames; }
00503     
00504   protected:
00505     PINDEX GetMaxFrameBytesConverted(PINDEX rawFrameBytes) const;
00506 
00507     PString      deviceName;
00508     int          lastError;
00509     VideoFormat  videoFormat;
00510     int          channelNumber;
00511     // Preferred native colour format from video input device, empty == no preference
00512     PString      preferredColourFormat;
00513     PBoolean         nativeVerticalFlip;
00514 
00515     PColourConverter * converter;
00516     PBYTEArray         frameStore;
00517 
00518     int          frameBrightness; // 16 bit entity, -1 is no value
00519     int          frameWhiteness;
00520     int          frameContrast;
00521     int          frameColour;
00522     int          frameHue;
00523     int          numberOfFrames;
00524 };
00525 
00526 
00529 class PVideoOutputDevice : public PVideoDevice
00530 {
00531   PCLASSINFO(PVideoOutputDevice, PVideoDevice);
00532 
00533   public:
00536     PVideoOutputDevice();
00537     
00540     virtual ~PVideoOutputDevice() { Close(); };      
00541 
00544     static PStringList GetDriverNames(
00545       PPluginManager * pluginMgr = NULL   
00546     );
00547 
00554     static PStringList GetDriversDeviceNames(
00555       const PString & driverName,         
00556       PPluginManager * pluginMgr = NULL   
00557     );
00558 
00561     static PVideoOutputDevice * CreateDevice(
00562       const PString & driverName,         
00563       PPluginManager * pluginMgr = NULL   
00564     );
00565 
00566     /* Create the matching video output device that corresponds to the device name.
00567 
00568        This is typically used with the return values from GetDriversDeviceNames().
00569      */
00570     static PVideoOutputDevice *CreateDeviceByName(
00571       const PString & deviceName,         
00572       const PString & driverName = PString::Empty(),  
00573       PPluginManager * pluginMgr = NULL   
00574     );
00575 
00581     static PVideoOutputDevice *CreateOpenedDevice(
00582       const PString & driverName,         
00583       const PString & deviceName,         
00584       PBoolean startImmediate = PTrue,         
00585       PPluginManager * pluginMgr = NULL   
00586     );
00587 
00590     static PVideoOutputDevice *CreateOpenedDevice(
00591       const OpenArgs & args,              
00592       PBoolean startImmediate = PTrue          
00593     );
00594 
00597     virtual PBoolean Close() { return PTrue; }
00598 
00601     virtual PBoolean Start() { return PTrue; }
00602 
00605     virtual PBoolean Stop() { return PTrue; }
00606 
00609     virtual PBoolean CanCaptureVideo() const;
00610 
00613     virtual PBoolean SetFrameData(
00614       unsigned x,
00615       unsigned y,
00616       unsigned width,
00617       unsigned height,
00618       const BYTE * data,
00619       PBoolean endFrame = PTrue
00620     ) = 0;
00621 
00628     virtual PBoolean GetPosition(
00629       int & x,  // X position of device surface
00630       int & y   // Y position of device surface
00631     ) const;
00632 };
00633 
00634 
00637 class PVideoOutputDeviceRGB : public PVideoOutputDevice
00638 {
00639   PCLASSINFO(PVideoOutputDeviceRGB, PVideoOutputDevice);
00640 
00641   public:
00644     PVideoOutputDeviceRGB();
00645 
00656     virtual PBoolean SetColourFormat(
00657       const PString & colourFormat // New colour format for device.
00658     );
00659 
00668     virtual PBoolean SetFrameSize(
00669       unsigned width,   
00670       unsigned height   
00671     );
00672 
00678     virtual PINDEX GetMaxFrameBytes();
00679 
00682     virtual PBoolean SetFrameData(
00683       unsigned x,
00684       unsigned y,
00685       unsigned width,
00686       unsigned height,
00687       const BYTE * data,
00688       PBoolean endFrame = PTrue
00689     );
00690 
00693     virtual PBoolean FrameComplete() = 0;
00694 
00695   protected:
00696     PMutex     mutex;
00697     PINDEX     bytesPerPixel;
00698     PINDEX     scanLineWidth;
00699     bool       swappedRedAndBlue;
00700 };
00701 
00702 
00703 #ifdef SHOULD_BE_MOVED_TO_PLUGIN
00704 
00707 class PVideoOutputDevicePPM : public PVideoOutputDeviceRGB
00708 {
00709   PCLASSINFO(PVideoOutputDevicePPM, PVideoOutputDeviceRGB);
00710 
00711   public:
00714     PVideoOutputDevicePPM();
00715 
00718     virtual PBoolean Open(
00719       const PString & deviceName,   
00720       PBoolean startImmediate = PTrue    
00721     );
00722 
00725     virtual PBoolean IsOpen();
00726 
00729     virtual PBoolean Close();
00730 
00733     virtual PStringList GetDeviceNames() const;
00734 
00737     virtual PBoolean EndFrame();
00738 
00739   protected:
00740     unsigned   frameNumber;
00741 };
00742 
00743 #endif // SHOULD_BE_MOVED_TO_PLUGIN
00744 
00745 
00748 class PVideoInputDevice : public PVideoDevice
00749 {
00750   PCLASSINFO(PVideoInputDevice, PVideoDevice);
00751 
00752   public:
00755     //PVideoInputDevice();
00756 
00759     ~PVideoInputDevice() { Close(); }
00760 
00763     static PStringList GetDriverNames(
00764       PPluginManager * pluginMgr = NULL   
00765     );
00766 
00773     static PStringList GetDriversDeviceNames(
00774       const PString & driverName,         
00775       PPluginManager * pluginMgr = NULL   
00776     );
00777 
00780     static PVideoInputDevice *CreateDevice(
00781       const PString & driverName,         
00782       PPluginManager * pluginMgr = NULL   
00783     );
00784 
00785     /* Create the matching video input device that corresponds to the device name.
00786        So, for "fake" return a device that will generate fake video.
00787        For "Phillips 680 webcam" (eg) will return appropriate grabber.
00788        Note that Phillips will return the appropriate grabber also.
00789 
00790        This is typically used with the return values from GetDriversDeviceNames().
00791      */
00792     static PVideoInputDevice *CreateDeviceByName(
00793       const PString & deviceName,         
00794       const PString & driverName = PString::Empty(),  
00795       PPluginManager * pluginMgr = NULL   
00796     );
00797 
00803     static PVideoInputDevice *CreateOpenedDevice(
00804       const PString & driverName,         
00805       const PString & deviceName,         
00806       PBoolean startImmediate = PTrue,         
00807       PPluginManager * pluginMgr = NULL   
00808     );
00809 
00812     static PVideoInputDevice *CreateOpenedDevice(
00813       const OpenArgs & args,              
00814       PBoolean startImmediate = PTrue          
00815     );
00816 
00817     typedef std::list<PVideoFrameInfo> Capabilities;
00818 
00821     static PBoolean GetDeviceCapabilities(
00822       const PString & deviceName,           
00823       Capabilities * capabilities,          
00824       PPluginManager * pluginMgr = NULL     
00825     );
00826 
00829     static PBoolean GetDeviceCapabilities(
00830       const PString & deviceName,           
00831       const PString & driverName,           
00832       Capabilities * caps,                  
00833       PPluginManager * pluginMgr = NULL     
00834     );
00835 
00838     virtual PBoolean Open(
00839       const PString & deviceName,   
00840       PBoolean startImmediate = PTrue    
00841     ) = 0;
00842 
00843     virtual PBoolean Close(
00844     ) { return PTrue; }
00845 
00848     virtual PBoolean CanCaptureVideo() const;
00849  
00852     virtual PBoolean IsCapturing() = 0;
00853 
00856     virtual PBoolean GetFrame(
00857       PBYTEArray & frame
00858     );
00859 
00862     virtual PBoolean GetFrameData(
00863       BYTE * buffer,                 
00864       PINDEX * bytesReturned = NULL  
00865     ) = 0;
00866 
00869     virtual PBoolean GetFrameDataNoDelay(
00870       BYTE * buffer,                 
00871       PINDEX * bytesReturned = NULL  
00872     ) = 0;
00873 
00876     virtual PBoolean TestAllFormats() = 0;
00877 };
00878 
00879 
00881 //
00882 // declare macros and structures needed for video input plugins
00883 //
00884 
00885 template <class className> class PVideoInputPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00886 {
00887   public:
00888     virtual PObject *   CreateInstance(int /*userData*/) const { return new className; }
00889     virtual PStringList GetDeviceNames(int /*userData*/) const { return className::GetInputDeviceNames(); }
00890     virtual bool GetDeviceCapabilities(const PString & deviceName, void * caps) const
00891       { return className::GetDeviceCapabilities(deviceName, (PVideoInputDevice::Capabilities *)caps); }
00892 };
00893 
00894 #define PCREATE_VIDINPUT_PLUGIN(name) \
00895   static PVideoInputPluginServiceDescriptor<PVideoInputDevice_##name> PVideoInputDevice_##name##_descriptor; \
00896   PCREATE_PLUGIN(name, PVideoInputDevice, &PVideoInputDevice_##name##_descriptor)
00897 
00899 //
00900 // declare macros and structures needed for video output plugins
00901 //
00902 
00903 template <class className> class PVideoOutputPluginServiceDescriptor : public PDevicePluginServiceDescriptor
00904 {
00905   public:
00906     virtual PObject *   CreateInstance(int /*userData*/) const { return new className; }
00907     virtual PStringList GetDeviceNames(int /*userData*/) const { return className::GetOutputDeviceNames(); }
00908 };
00909 
00910 #define PCREATE_VIDOUTPUT_PLUGIN(name) \
00911   static PVideoOutputPluginServiceDescriptor<PVideoOutputDevice_##name> PVideoOutputDevice_##name##_descriptor; \
00912   PCREATE_PLUGIN(name, PVideoOutputDevice, &PVideoOutputDevice_##name##_descriptor)
00913 
00915 //
00916 // declare classes needed for access to simple video font
00917 //
00918 
00919 class PVideoFont : public PObject
00920 {
00921   PCLASSINFO(PVideoFont, PObject);
00922   public:
00923     enum {
00924       MAX_L_HEIGHT = 11
00925     };
00926     struct LetterData {
00927       char ascii;
00928       const char *line[MAX_L_HEIGHT];
00929     };
00930 
00931     static const LetterData * GetLetterData(char ascii);
00932 };
00933 
00934 #endif // P_VIDEO
00935 
00936 #endif   // _PVIDEOIO
00937 
00938 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Dec 10 11:18:57 2007 for PTLib by  doxygen 1.5.1