4 * H.323 protocol handler
8 * Copyright (c) 2001 March Networks Corporation
9 * Copyright (c) 1999-2000 Equivalence Pty. Ltd.
11 * The contents of this file are subject to the Mozilla Public License
12 * Version 1.0 (the "License"); you may not use this file except in
13 * compliance with the License. You may obtain a copy of the License at
14 * http://www.mozilla.org/MPL/
16 * Software distributed under the License is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
18 * the License for the specific language governing rights and limitations
21 * The Original Code is Open H323 Library.
23 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
25 * Contributor(s): Guilhem Tardy (gtardy@salyens.com)
28 * Revision 1.6 2006/08/01 06:17:50 csoutheren
29 * Fix bit rate control units
30 * Add access function on code for max bit rate
31 * Thanks to Paolo Amadini
33 * Revision 1.5 2006/07/19 07:18:17 csoutheren
34 * Fixed problem with timing in H.263 video codec
35 * Thanks to Paolo Amadini
37 * Revision 1.4 2004/05/12 23:18:44 csoutheren
38 * Adjusted copyright notices for ffh263 and rfc2190 files
40 * Revision 1.3 2004/04/24 00:41:14 rjongbloed
41 * Fixed file names in header comment.
43 * Revision 1.2 2004/04/22 22:35:00 csoutheren
44 * Fixed mispelling of Guilhem Tardy - my apologies to him
46 * Revision 1.1 2004/04/22 22:20:34 csoutheren
47 * New files for RFC2190 H.263 video codec
48 * Added RFC 2190 H.263 code as created by Guilhem Tardy and AliceStreet
49 * Many thanks to them for their contributions.
51 * Revision 1.5 2003/10/31 00:00:00 Guilhem Tardy
52 * Restored RFC2190 compliance.
54 * Revision 1.4 2003/10/05 00:00:00 Guilhem Tardy
55 * Reintroduce ffmpeg own packet slicing technique (now working better).
57 * Revision 1.3 2003/08/04 00:03:16 dereksmithies
58 * Reorganise tests for enabled
60 * Revision 1.2 2003/07/31 05:08:24 dereksmithies
61 * Switch to manual packet fragment technique. Much more reliable, especially so on windows.
63 * Revision 1.1 2003/07/24 05:23:06 dereksmithies
64 * Move ffmpeg h263 support to this file, and designate non standard.
66 * Revision 1.12 2003/06/06 05:18:54 dereksmithies
67 * Fix startup delay bug. Remove all large packets from the network. Improve reliability.
69 * Revision 1.11 2003/05/27 09:22:55 dereksmithies
70 * Updates for code revisions in h263 codec
72 * Revision 1.10 2003/05/14 13:47:58 rjongbloed
73 * Removed static "initialisation" function as this should be done
74 * internally and not in the application.
76 * Revision 1.9 2003/05/05 11:59:21 robertj
77 * Changed to use autoconf style selection of options and subsystems.
79 * Revision 1.8 2003/05/02 04:22:10 craigs
80 * Added lots of extra H.263 support
82 * Revision 1.7 2003/04/27 09:16:38 rogerh
83 * use PBYTE_ORDER instead of endian.h
85 * Revision 1.6 2003/04/21 21:50:22 dereks
86 * Implement suggestion from Guilhem Tardy. Many thanks.
88 * Revision 1.5 2003/04/16 04:26:57 dereks
89 * Initial release of h263 codec, which utilises the ffmpeg library.
90 * Thanks to Guilhem Tardy, and to AliceStreet.
92 * Revision 1.4 2002/09/16 01:14:15 robertj
93 * Added #define so can select if #pragma interface/implementation is used on
94 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
96 * Revision 1.3 2002/09/03 06:19:36 robertj
97 * Normalised the multi-include header prevention ifdef/define symbol.
99 * Revision 1.2 2002/08/05 10:03:47 robertj
100 * Cosmetic changes to normalise the usage of pragma interface/implementation.
102 * Revision 1.1 2002/05/19 22:32:46 dereks
103 * Initial release of stub file for h263 codec. Thanks Guilhem Tardy.
110 * Initial release notes from Guilhem Tardy::
112 * Added support for video capabilities & codec, only tested under Linux!
113 * The code for varying bit rate is copied from h261codec.cxx,
114 * until it is moved to a separate file common to both video codecs.
118 #ifndef __OPAL_FFH263CODEC_H
119 #define __OPAL_FFH263CODEC_H
125 #ifdef H323_RFC2190_AVCODEC
127 struct AVCodecContext
;
130 #include <ptclib/delaychan.h>
132 ///////////////////////////////////////////////////////////////////////////////
134 /**This class is a H.263 video capability.
136 class H323_RFC2190_H263Capability
: public H323VideoCapability
138 PCLASSINFO(H323_RFC2190_H263Capability
, H323VideoCapability
)
141 /**@name Construction */
143 /**Create a new FFH263 Capability
145 H323_RFC2190_H263Capability(
146 unsigned sqcifMPI
= 1, // {1..3600 units seconds/frame, 1..32 units 1/29.97 Hz}
147 unsigned qcifMPI
= 2,
149 unsigned cif4MPI
= 8,
150 unsigned cif16MPI
= 32,
151 unsigned maxBitRate
= 400,
152 BOOL unrestrictedVector
= FALSE
,
153 BOOL arithmeticCoding
= FALSE
, // not supported
154 BOOL advancedPrediction
= FALSE
,
155 BOOL pbFrames
= FALSE
,
156 BOOL temporalSpatialTradeOff
= FALSE
, // not supported
157 unsigned hrd_B
= 0, // not supported
158 unsigned bppMaxKb
= 0, // not supported
159 unsigned slowSqcifMPI
= 0,
160 unsigned slowQcifMPI
= 0,
161 unsigned slowCifMPI
= 0,
162 unsigned slowCif4MPI
= 0,
163 unsigned slowCif16MPI
= 0,
164 BOOL errorCompensation
= FALSE
// not supported
168 /**@name Overrides from class PObject */
170 /**Create a copy of the object.
172 virtual PObject
* Clone() const;
175 /**@name Overrides from class PObject */
179 Comparison
Compare(const PObject
& obj
) const;
182 /**@name Identification functions */
184 /**Get the sub-type of the capability. This is a code dependent on the
185 main type of the capability.
187 This returns one of the four possible combinations of mode and speed
188 using the enum values of the protocol ASN H245_AudioCapability class.
190 virtual unsigned GetSubType() const;
192 /**Get the name of the media data format this class represents.
194 virtual PString
GetFormatName() const;
197 /**@name Protocol manipulation */
199 /**This function is called whenever and outgoing TerminalCapabilitySet
200 or OpenLogicalChannel PDU is being constructed for the control channel.
201 It allows the capability to set the PDU fields from information in
202 members specific to the class.
204 The default behaviour sets the data rate field in the PDU.
206 virtual BOOL
OnSendingPDU(
207 H245_VideoCapability
& pdu
/// PDU to set information on
210 /**This function is called whenever and outgoing RequestMode
211 PDU is being constructed for the control channel. It allows the
212 capability to set the PDU fields from information in members specific
215 The default behaviour sets the resolution and bit rate.
217 virtual BOOL
OnSendingPDU(
218 H245_VideoMode
& pdu
/// PDU to set information on
221 /**This function is called whenever and incoming TerminalCapabilitySet
222 or OpenLogicalChannel PDU has been used to construct the control
223 channel. It allows the capability to set from the PDU fields,
224 information in members specific to the class.
226 The default behaviour gets the data rate field from the PDU.
228 virtual BOOL
OnReceivedPDU(
229 const H245_VideoCapability
& pdu
/// PDU to set information on
232 /**Create the codec instance, allocating resources as required.
234 virtual H323Codec
* CreateCodec(
235 H323Codec::Direction direction
/// Direction in which this instance runs
240 unsigned GetSQCIFMPI() const
245 unsigned GetQCIFMPI() const
250 unsigned GetCIFMPI() const
255 unsigned GetCIF4MPI() const
260 unsigned GetCIF16MPI() const
263 /** Get maximum bit rate
265 unsigned GetMaxBitRate() const
266 { return maxBitRate
; }
268 /** Get unrestrictedVector capabilty
270 BOOL
GetUnrestrictedVectorCapability() const
271 { return unrestrictedVector
; }
273 /** Get arithmeticCoding capabilty
275 BOOL
GetArithmeticCodingCapability() const
276 { return arithmeticCoding
; }
278 /** Get advancedPrediction capabilty
280 BOOL
GetAdvancedPredictionCapability() const
281 { return advancedPrediction
; }
283 /** Get pbFrames capabilty
285 BOOL
GetPbFramesCapability() const
288 /** Get temporal/spatial tradeoff capabilty
290 BOOL
GetTemporalSpatialTradeOffCapability() const
291 { return temporalSpatialTradeOff
; }
295 BOOL
GetHrd_B() const
300 BOOL
GetBppMaxKb() const
305 unsigned GetSlowSQCIFMPI() const
306 { return (sqcifMPI
<0?-sqcifMPI
:0); }
310 unsigned GetSlowQCIFMPI() const
311 { return (qcifMPI
<0?-qcifMPI
:0); }
315 unsigned GetSlowCIFMPI() const
316 { return (cifMPI
<0?-cifMPI
:0); }
320 unsigned GetSlowCIF4MPI() const
321 { return (cif4MPI
<0?-cif4MPI
:0); }
325 unsigned GetSlowCIF16MPI() const
326 { return (cif16MPI
<0?-cif16MPI
:0); }
328 /** Get errorCompensation capabilty
330 BOOL
GetErrorCompensationCapability() const
331 { return errorCompensation
; }
336 signed sqcifMPI
; // {1..3600 units seconds/frame, 1..32 units 1/29.97 Hz}
342 unsigned maxBitRate
; // units of bit/s
344 BOOL unrestrictedVector
;
345 BOOL arithmeticCoding
;
346 BOOL advancedPrediction
;
348 BOOL temporalSpatialTradeOff
;
350 long unsigned hrd_B
; // units of 128 bits
351 unsigned bppMaxKb
; // units of 1024 bits
353 BOOL errorCompensation
;
356 ////////////////////////////////////////////////////////////////
358 class H263Packet
: public PObject
360 PCLASSINFO(H263Packet
, PObject
)
364 H263Packet() { data_size
= hdr_size
= 0; hdr
= data
= NULL
; };
367 void Store(void *data
, int data_size
, void *hdr
, int hdr_size
);
368 BOOL
Read(unsigned & length
, RTP_DataFrame
& frame
);
378 PDECLARE_LIST(H263PacketList
, H263Packet
)
384 //////////////////////////////////////////////////////////////////////
386 /**This class is a H.263 video codec.
388 class H323_RFC2190_H263Codec
: public H323VideoCodec
390 PCLASSINFO(H323_RFC2190_H263Codec
, H323VideoCodec
)
393 /**Create a new H263 video codec
395 H323_RFC2190_H263Codec(
396 Direction direction
, /// Direction in which this instance runs
397 signed sqcifMPI
, /// {1..3600 units seconds/frame, 1..32 units 1/29.97 Hz}
402 unsigned maxBitRate
, /// Maximum bitrate (in bits per second); see SetMaxBitRate
403 BOOL unrestrictedVector
,
404 BOOL arithmeticCoding
,
405 BOOL advancedPrediction
,
409 ~H323_RFC2190_H263Codec();
411 /**Encode the data from the appropriate device.
412 This will encode a frame of data for transmission. The exact size and
413 description of the data placed in the buffer is codec dependent but
414 should be less than H323Capability::GetTxFramesInPacket() *
415 OpalMediaFormat::GetFrameSize() in length.
417 The length parameter is filled with the actual length of the encoded
418 data, often this will be the same as the size parameter.
420 This function is called every GetFrameRate() timestamp units, so MUST
421 take less than (or equal to) that amount of time to complete!
423 Note that a returned length of zero indicates that time has passed but
424 there is no data encoded. This is typically used for silence detection
428 BYTE
* buffer
, /// Buffer of encoded data
429 unsigned & length
, /// Actual length of encoded data buffer
430 RTP_DataFrame
& rtpFrame
/// RTP data frame
433 /**Decode the data and output it to appropriate device.
434 This will decode a single frame of received data. The exact size and
435 description of the data required in the buffer is codec dependent but
436 should be less than H323Capability::GetRxFramesInPacket() *
437 OpalMediaFormat::GetFrameSize() in length.
439 It is expected this function anunciates the data. That is, for example
440 with audio data, the sound is output on a speaker.
442 This function is called every GetFrameRate() timestamp units, so MUST
443 take less than that amount of time to complete!
446 const BYTE
* buffer
, /// Buffer of encoded data
447 unsigned length
, /// Length of encoded data buffer
448 const RTP_DataFrame
& rtp
, /// RTP data frame
449 unsigned & written
/// Number of bytes used from data buffer
452 /**Get the frame rate in RTP timestamp units.
454 virtual unsigned GetFrameRate() const { return rtpTimestampDelta
; }
456 /**Quality of the transmitted video. 1 is good, 31 is poor.
458 void SetTxQualityLevel(int qLevel
);
460 /**Minimum quality limit for the transmitted video.
461 * Default is 1. Encode quality will not be set below this value.
463 virtual void SetTxMinQuality(int qlevel
);
465 /**Maximum quality limit for the transmitted video.
466 * Default is 24. Encode quality will not be set above this value.
468 virtual void SetTxMaxQuality(int qlevel
);
470 /**Number of blocks (that haven't changed) transmitted with each
471 * frame. These blocks fill in the background.
473 void SetBackgroundFill(int fillLevel
);
475 /**Set the current value for video control mode
476 * return the resulting value video control mode
478 virtual void SetVideoMode(unsigned mode
);
480 /**Set maximum bitrate when transmitting video, in bps. A value of 0
481 disables bit rate control. The average bitrate will be less depending
482 on channel dead time, i.e. time that the channel could be transmitting
485 The same value is both suggested to the H.263 codec and used for
486 adaptive packet delay. Use SetVideoMode to enable the appropriate
487 bitrate control mode.
489 @return TRUE if success
491 virtual BOOL
SetMaxBitRate(unsigned bitRate
);
493 /**Get the current value of the maximum bitrate, in bps. If SetMaxBitRate
494 was never called, the returned value is the one negotiated by the
495 capability. A value of 0 indicates that bitrate control is disabled.
497 virtual unsigned GetMaxBitRate() const { return bitRateHighLimit
; }
499 /**Process a OnVideoTemporalSpatialTradeOff indication from remote endpoint.
500 The default behaviour does nothing.
502 virtual void OnVideoTemporalSpatialTradeOff();
504 /**Process a request for a new frame,
505 as part of the picture has been lost.
507 virtual void OnLostPartialPicture();
509 /**Process a request for a new frame,
510 as the entire picture has been lost.
512 virtual void OnLostPicture();
515 The ffmpeg library calls this routine, everytime it has a new packet to
516 send. Note that for every encode thread, this one method is called. Consequently,
517 the 'priv_data' is used to separate out packets from different encode threads.
519 static void RtpCallback(void *data
, int data_size
,
520 void *hdr
, int hdr_size
, void *priv_data
);
524 BOOL
Resize(int width
, int height
);
526 BOOL
RenderFrame(const void *buffer
);
527 BOOL
RenderFrame(const AVFrame
*pict
);
533 H263PacketList encodedPackets
;
534 H263PacketList unusedPackets
;
536 PBYTEArray encFrameBuffer
;
537 PBYTEArray rawFrameBuffer
;
543 AVCodecContext
*context
;
547 unsigned rtpTimestampDelta
;
548 PAdaptiveDelay packetDelay
;
549 PTime lastPacketTime
;
550 PINDEX lastPacketBits
;
552 enum StdSize
{UnknownStdSize
, SQCIF
= 1, QCIF
, CIF
, CIF4
, CIF16
, NumStdSizes
};
554 static int GetStdSize(int width
, int height
); //
555 static int GetStdWidth (StdSize size
);
556 static int GetStdHeight (StdSize size
);
558 signed videoFrameRate
[NumStdSizes
]; /// {1..3600 units seconds/frame, 1..32 units 1/29.97 Hz}
560 BOOL unrestrictedVector
;
561 BOOL arithmeticCoding
;
562 BOOL advancedPrediction
;
566 #endif // H323_AVCODEC
568 #endif // __OPAL_FFH263CODEC_H
571 /////////////////////////////////////////////////////////////////////////////