home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / openh323.tar.gz / openh323.tar / openh323 / include / ffh263codec.h < prev    next >
C/C++ Source or Header  |  2003-08-03  |  12KB  |  385 lines

  1. /*
  2.  * ffh263codec.h
  3.  *
  4.  * H.323 protocol handler
  5.  *
  6.  * Open H323 Library
  7.  *
  8.  * Copyright (c) 1999-2000 Equivalence Pty. Ltd.
  9.  *
  10.  * The contents of this file are subject to the Mozilla Public License
  11.  * Version 1.0 (the "License"); you may not use this file except in
  12.  * compliance with the License. You may obtain a copy of the License at
  13.  * http://www.mozilla.org/MPL/
  14.  *
  15.  * Software distributed under the License is distributed on an "AS IS"
  16.  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  17.  * the License for the specific language governing rights and limitations
  18.  * under the License.
  19.  *
  20.  * The Original Code is Open H323 Library.
  21.  *
  22.  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
  23.  *
  24.  * Contributor(s): Guilhem Tardy (gtardy@marchnetworks.com)
  25.  *
  26.  * $Log: ffh263codec.h,v $
  27.  * Revision 1.3  2003/08/04 00:03:16  dereksmithies
  28.  * Reorganise tests for enabled
  29.  *
  30.  * Revision 1.2  2003/07/31 05:08:24  dereksmithies
  31.  * Switch to manual packet fragment technique. Much more reliable, especially so on windows.
  32.  *
  33.  * Revision 1.1  2003/07/24 05:23:06  dereksmithies
  34.  * Move ffmpeg h263 support to this file, and designate non standard.
  35.  *
  36.  * Revision 1.12  2003/06/06 05:18:54  dereksmithies
  37.  * Fix startup delay bug. Remove all large packets from the network. Improve reliability.
  38.  *
  39.  * Revision 1.11  2003/05/27 09:22:55  dereksmithies
  40.  * Updates for code revisions in h263 codec
  41.  *
  42.  * Revision 1.10  2003/05/14 13:47:58  rjongbloed
  43.  * Removed static "initialisation" function as this should be done
  44.  *   internally and not in the application.
  45.  *
  46.  * Revision 1.9  2003/05/05 11:59:21  robertj
  47.  * Changed to use autoconf style selection of options and subsystems.
  48.  *
  49.  * Revision 1.8  2003/05/02 04:22:10  craigs
  50.  * Added lots of extra H.263 support
  51.  *
  52.  * Revision 1.7  2003/04/27 09:16:38  rogerh
  53.  * use PBYTE_ORDER instead of endian.h
  54.  *
  55.  * Revision 1.6  2003/04/21 21:50:22  dereks
  56.  * Implement suggestion from Guilhem Tardy. Many thanks.
  57.  *
  58.  * Revision 1.5  2003/04/16 04:26:57  dereks
  59.  * Initial release of h263 codec, which utilises the ffmpeg library.
  60.  * Thanks to Guilhem Tardy, and to AliceStreet.
  61.  *
  62.  * Revision 1.4  2002/09/16 01:14:15  robertj
  63.  * Added #define so can select if #pragma interface/implementation is used on
  64.  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
  65.  *
  66.  * Revision 1.3  2002/09/03 06:19:36  robertj
  67.  * Normalised the multi-include header prevention ifdef/define symbol.
  68.  *
  69.  * Revision 1.2  2002/08/05 10:03:47  robertj
  70.  * Cosmetic changes to normalise the usage of pragma interface/implementation.
  71.  *
  72.  * Revision 1.1  2002/05/19 22:32:46  dereks
  73.  * Initial release of stub file for h263 codec. Thanks Guilhem Tardy.
  74.  *
  75.  *
  76.  *
  77.  */
  78.  
  79. /*
  80.  * Initial release notes from Guilhem Tardy::
  81.  *
  82.  * Added support for video capabilities & codec, only tested under Linux!
  83.  * The code for varying bit rate is copied from h261codec.cxx,
  84.  * until it is moved to a separate file common to both video codecs.
  85.  *
  86.  */
  87.  
  88. #ifndef __OPAL_FFH263CODEC_H
  89. #define __OPAL_FFH263CODEC_H
  90.  
  91. #ifdef P_USE_PRAGMA
  92. #pragma interface
  93. #endif
  94.  
  95. #ifdef H323_AVCODEC
  96. struct AVCodec;
  97. struct AVCodecContext;
  98. struct AVFrame;
  99.  
  100. ///////////////////////////////////////////////////////////////////////////////
  101.  
  102. /**This class is a H.263 video capability.
  103.  */
  104. class H323_FFH263Capability : public H323NonStandardVideoCapability
  105. {
  106.   PCLASSINFO(H323_FFH263Capability, H323NonStandardVideoCapability)
  107.  
  108.   public:
  109.   /**@name Construction */
  110.   //@{
  111.     /**Create a new FFH263 Capability
  112.      */ 
  113.     H323_FFH263Capability(
  114.               unsigned sqcifMPI,
  115.               unsigned qcifMPI,
  116.               unsigned cifMPI,
  117.               unsigned cif4MPI,
  118.               unsigned cif16MPI,
  119.               unsigned maxBitRate = 850,
  120.               unsigned videoFrameRate = 25);
  121.   //@}
  122.  
  123.   /**@name Overrides from class PObject */
  124.   //@{
  125.     /**Create a copy of the object.
  126.       */
  127.     virtual PObject * Clone() const;
  128.   //@}
  129.  
  130.     /**@name Identification functions */
  131.   //@{
  132.     /**Get the sub-type of the capability. This is a code dependent on the
  133.        main type of the capability.
  134.  
  135.        This returns one of the four possible combinations of mode and speed
  136.        using the enum values of the protocol ASN H245_AudioCapability class.
  137.      */
  138.     virtual unsigned GetSubType() const;
  139.  
  140.     /**Get the name of the media data format this class represents.
  141.      */
  142.     virtual PString GetFormatName() const;
  143.   //@}
  144.  
  145.   /**@name Protocol manipulation */
  146.   //@{
  147.     /**This function is called whenever and outgoing TerminalCapabilitySet
  148.        or OpenLogicalChannel PDU is being constructed for the control channel.
  149.        It allows the capability to set the PDU fields from information in
  150.        members specific to the class.
  151.  
  152.        The default behaviour sets the data rate field in the PDU.
  153.      */
  154.     virtual BOOL OnSendingPDU(
  155.       H245_VideoCapability & pdu  /// PDU to set information on
  156.     ) const;
  157.  
  158.     /**This function is called whenever and outgoing RequestMode
  159.        PDU is being constructed for the control channel. It allows the
  160.        capability to set the PDU fields from information in members specific
  161.        to the class.
  162.  
  163.        The default behaviour sets the resolution and bit rate.
  164.      */
  165.     virtual BOOL OnSendingPDU(
  166.       H245_VideoMode & pdu  /// PDU to set information on
  167.     ) const;
  168.  
  169.     /**This function is called whenever and incoming TerminalCapabilitySet
  170.        or OpenLogicalChannel PDU has been used to construct the control
  171.        channel. It allows the capability to set from the PDU fields,
  172.        information in members specific to the class.
  173.  
  174.        The default behaviour gets the data rate field from the PDU.
  175.      */
  176.     virtual BOOL OnReceivedPDU(
  177.       const H245_VideoCapability & pdu  /// PDU to set information on
  178.     );
  179.  
  180.     /**Create the codec instance, allocating resources as required.
  181.      */
  182.     virtual H323Codec * CreateCodec(
  183.       H323Codec::Direction direction  /// Direction in which this instance runs
  184.     ) const;
  185.  
  186.  
  187.   //@}
  188.  
  189. protected:
  190.  
  191.     signed sqcifMPI;        // {1..3600 units seconds/frame, 1..32 units 1/29.97 Hz}
  192.     signed qcifMPI;        // {1..3600 units seconds/frame, 1..32 units 1/29.97 Hz}
  193.     signed cifMPI;        // {1..3600 units seconds/frame, 1..32 units 1/29.97 Hz}
  194.     signed cif4MPI;        // {1..3600 units seconds/frame, 1..32 units 1/29.97 Hz}
  195.     signed cif16MPI;        // {1..3600 units seconds/frame, 1..32 units 1/29.97 Hz}
  196.  
  197.     unsigned maxBitRate;    // units of bit/s
  198.     unsigned videoFrameRate;   // frames per second.
  199.  
  200. };
  201. ////////////////////////////////////////////////////////////////
  202.  
  203. class H263Packet : public PObject
  204. {
  205.   PCLASSINFO(H263Packet, PObject)
  206.  
  207.   public:
  208.     H263Packet(void *newData, int newSize);
  209.     ~H263Packet();
  210.  
  211.     int GetSize() { return size; }
  212.  
  213.     void *GetData() { return data; }
  214.  
  215.   private:
  216.     void *data;
  217.     int  size;
  218. };
  219.   
  220. //////////////////////////////////////////////////////////////////////
  221.  
  222. PDECLARE_LIST(H263FragmentList, H263Packet)
  223. #if 0                                //This makes emacs bracket matching code happy.
  224. {
  225. #endif
  226.  public:
  227.   ~H263FragmentList();
  228.  
  229.   PINDEX GetFragmentsRemaining();
  230.   
  231.   PINDEX GetFragmentIndex();
  232.   
  233.   PINDEX GetFragmentsTotal();
  234.   
  235.   virtual H263Packet *GetNextFragment();
  236.   
  237.   void AppendH263Packet(H263Packet *packet);
  238.   
  239.   void AppendH263Packet(unsigned char *data, int size);
  240.   
  241.   void EmptyList();
  242.   
  243.  private:
  244.   PINDEX nPackets;
  245. };
  246.  
  247.  
  248. ///////////////////////////////////////////////////////////////
  249. /**This class is a H.263 video codec.
  250.  */
  251. class H323_FFH263Codec : public H323VideoCodec
  252. {
  253.   PCLASSINFO(H323_FFH263Codec, H323VideoCodec)
  254.  
  255.   public:
  256.     /**Create a new H263 video codec
  257.      */
  258.     H323_FFH263Codec(
  259.       Direction direction,    /// Direction in which this instance runs      
  260.       unsigned sqcifMPI,
  261.       unsigned qcifMPI,
  262.       unsigned cifMPI,
  263.       unsigned cif4MPI,
  264.       unsigned cif16MPI,
  265.       unsigned maxBitRate,
  266.       unsigned videoFrameRate
  267.     );
  268.  
  269.     ~H323_FFH263Codec();
  270.  
  271.     /**Encode the data from the appropriate device.
  272.        This will encode a frame of data for transmission. The exact size and
  273.        description of the data placed in the buffer is codec dependent but
  274.        should be less than H323Capability::GetTxFramesInPacket() *
  275.        OpalMediaFormat::GetFrameSize()  in length.
  276.  
  277.        The length parameter is filled with the actual length of the encoded
  278.        data, often this will be the same as the size parameter.
  279.  
  280.        This function is called every GetFrameRate() timestamp units, so MUST
  281.        take less than (or equal to) that amount of time to complete!
  282.  
  283.        Note that a returned length of zero indicates that time has passed but
  284.        there is no data encoded. This is typically used for silence detection
  285.        in an audio codec.
  286.      */
  287.     virtual BOOL Read(
  288.       BYTE * buffer,        /// Buffer of encoded data
  289.       unsigned & length,    /// Actual length of encoded data buffer
  290.       RTP_DataFrame & rtpFrame    /// RTP data frame
  291.     );
  292.  
  293.     /**Decode the data and output it to appropriate device.
  294.        This will decode a single frame of received data. The exact size and
  295.        description of the data required in the buffer is codec dependent but
  296.        should be less than H323Capability::GetRxFramesInPacket() *
  297.        OpalMediaFormat::GetFrameSize()  in length.
  298.  
  299.        It is expected this function anunciates the data. That is, for example
  300.        with audio data, the sound is output on a speaker.
  301.  
  302.        This function is called every GetFrameRate() timestamp units, so MUST
  303.        take less than that amount of time to complete!
  304.      */
  305.     virtual BOOL Write(
  306.       const BYTE * buffer,      /// Buffer of encoded data
  307.       unsigned length,          /// Length of encoded data buffer
  308.       const RTP_DataFrame & rtp,  /// RTP data frame
  309.       unsigned & written      /// Number of bytes used from data buffer
  310.     );
  311.  
  312.     /**Get the frame rate in RTP timestamp units.
  313.       */
  314.     virtual unsigned GetFrameRate() const { return timestampDelta; }
  315.  
  316.     /**Set the quality level of transmitted video data. 
  317.        Is irrelevant when this codec is used to receive video data.
  318.        Has a value of 1 (good quality) to 31 (poor quality).
  319.        Quality is improved at the expense of bit rate.
  320.     */
  321.     void SetTxQualityLevel(int qLevel);
  322.  
  323.     /**Set the number of blocks in the background that need
  324.        to be transmitted with each frame
  325.     */
  326.     void SetBackgroundFill(int fillLevel);
  327.  
  328.     /**Process a request for a new frame, 
  329.        as part of the picture has been lost.
  330.     */
  331.     virtual void OnLostPartialPicture();
  332.  
  333.     /**
  334.        Process a request for a new frame, 
  335.        as all of the picture has been lost.
  336.     */
  337.     virtual void OnLostPicture();
  338.  
  339.     /**
  340.        The ffmpeg library calls this routine, everytime it has a new packet to 
  341.        send. Note that for every encode thread, this 1 method is called. Consequently, 
  342.        code is required to separate out packets from different encode threads.
  343.     */
  344.     static void RtpCallback(void *data, int size, int packetNumber);
  345.  
  346.   protected:
  347.     BOOL Resize(int width, int height);
  348.  
  349.     BOOL RenderFrame(); 
  350.     BOOL RenderFrame(const void * buffer);
  351.  
  352.     BOOL RenderFrame(AVFrame  *pict);
  353.     BOOL RawToPict(AVFrame  *pict);
  354.  
  355.     void InitialiseCodec();
  356.     void CloseCodec();
  357.  
  358.     H263FragmentList partialPackets;    //used for rebuilding H263 frame from source.
  359.     PINDEX           currentFragment;   // " " "
  360.  
  361.     PBYTEArray encFrameBuffer;
  362.     PBYTEArray rawFrameBuffer;
  363.  
  364.     PINDEX         encFrameLen;
  365.     PINDEX         rawFrameLen;
  366.  
  367.     unsigned timestampDelta;
  368.  
  369.     AVCodec        *codec;
  370.     AVCodecContext *context;
  371.     AVFrame        *picture;
  372.  
  373.     PTime           startTime;
  374.     PINDEX          bitsSent;
  375.  
  376.     unsigned        lastebits;
  377. };
  378.  
  379. #endif // H323_AVCODEC
  380.  
  381. #endif // __OPAL_FFH263CODEC_H
  382.  
  383.  
  384. /////////////////////////////////////////////////////////////////////////////
  385.