# ratectl.h

Go to the documentation of this file.

    00001 /*
    00002  * ratectl.h
    00003  *
    00004  * Video rate controller
    00005  *
    00006  * Open Phone Abstraction Library (OPAL)
    00007  *
    00008  * Copyright (C) 2007 Matthias Schneider
    00009  * Copyright (C) 2008 Post Increment
    00010  *
    00011  * The contents of this file are subject to the Mozilla Public License
    00012  * Version 1.0 (the "License"); you may not use this file except in
    00013  * compliance with the License. You may obtain a copy of the License at
    00014  * http://www.mozilla.org/MPL/
    00015  *
    00016  * Software distributed under the License is distributed on an "AS IS"
    00017  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
    00018  * the License for the specific language governing rights and limitations
    00019  * under the License.
    00020  *
    00021  * The Original Code is Open Phone Abstraction Library.
    00022  *
    00023  * The Initial Developer of the Original Code is Matthias Schneider
    00024  *
    00025  * Contributor(s): Post Increment
    00026  *
    00027  * $Revision: 22839 $
    00028  * $Author: rjongbloed $
    00029  * $Date: 2009-06-10 20:37:03 -0500 (Wed, 10 Jun 2009) $
    00030  */
    00031
    00032 #ifndef OPAL_RATE_CONTROL_H
    00033 #define OPAL_RATE_CONTROL_H
    00034
    00035 #ifdef P_USE_PRAGMA
    00036 #pragma interface
    00037 #endif
    00038
    00039 #include <opal/buildopts.h>
    00040
    00041 #if OPAL_VIDEO
    00042
    00043 #include <rtp/rtp.h>
    00044
    00045 extern double OpalCalcSNR(const BYTE * src1, const BYTE * src2, PINDEX dataLen);
    00046
    00051 class OpalBitRateCalculator
    00052 {
    00053   public:
    00056     OpalBitRateCalculator();
    00057
    00060     void Reset();
    00061
    00064     void SetQuanta(
    00065       unsigned quanta_
    00066     );
    00067
    00070     unsigned GetQuanta() const
    00071     { return m_quanta; }
    00072
    00075     void AddPacket(PINDEX size, bool marker);
    00076
    00079     unsigned GetBitRate();
    00080
    00083     unsigned GetAverageBitRate();
    00084
    00087     unsigned GetAveragePacketSize();
    00088
    00091     unsigned GetTrialBitRate(PINDEX size);
    00092
    00095     PInt64 GetTotalSize() const;
    00096
    00099     PInt64 GetTotalTime() const;
    00100
    00103     size_t GetHistoryCount() const
    00104     { return m_history.size(); }
    00105
    00108     unsigned GetHistorySize() const
    00109     { return m_historySize; }
    00110
    00113     PInt64 GetEarliestHistoryTime() const
    00114     { if (m_history.size() == 0) return 0; return m_history.begin()->m_timeStamp; }
    00115
    00118     unsigned GetHistoryFrames() const;
    00119
    00120     // flush old data from history
    00121     void Flush();
    00122
    00123     // used to get "now"
    00124     static PInt64 GetNow();
    00125
    00126   protected:
    00127
    00128     void Flush(PInt64 now);
    00129
    00130     struct History {
    00131       History(PINDEX size_, PInt64 timeStamp_, bool marker_)
    00132         : m_size(size_), m_timeStamp(timeStamp_), m_marker(marker_)
    00133       { }
    00134
    00135       PINDEX m_size;
    00136       PInt64 m_timeStamp;
    00137       bool m_marker;
    00138     };
    00139
    00140     std::deque<History> m_history;
    00141
    00142     PINDEX m_historySize;
    00143     PInt64 m_totalSize;
    00144     PINDEX m_historyFrames;
    00145
    00146     unsigned m_quanta;
    00147     unsigned m_bitRate;
    00148     bool m_first;
    00149     PInt64 m_baseTimeStamp;
    00150 };
    00151
    00152 //
    00153 //  Declare a generic video rate controller class.
    00154 //  A rate controller seeks to maintain a constant bit rate by manipulating
    00155 //  the parameters of the video stream
    00156 //
    00157 //  Before encoding a potential output frame, use the SkipFrame function to determine if the
    00158 //  frame should be skipped.
    00159 //
    00160 //  If the frame is not skipped, encode the frame and call PushFrame to add the frame to the rate controller queue
    00161 //  PopFrame can then be called to retreive frames to transmit
    00162 //
    00163 //  PushFrame must always be called with packets from a single video frame, but PopFrame may return packets
    00164 //  from multiple video frames
    00165 //
    00166
    00167 class OpalMediaFormat;
    00168
    00169 class OpalVideoRateController
    00170 {
    00171   public:
    00172     OpalVideoRateController();
    00173
    00174     virtual ~OpalVideoRateController();
    00175
    00178     virtual void Open(
    00179       const OpalMediaFormat & mediaFormat
    00180     );
    00181
    00186     virtual bool SkipFrame(
    00187       bool & forceIFrame
    00188     ) = 0;
    00189
    00192     virtual void Push(
    00193       RTP_DataFrameList & inputFrames,
    00194       bool iFrame
    00195     );
    00196
    00199     virtual bool Pop(
    00200       RTP_DataFrameList & outputPackets,
    00201       bool & iFrame,
    00202       bool force
    00203     );
    00204
    00207     OpalBitRateCalculator m_bitRateCalc;
    00208
    00209   protected:
    00210     unsigned m_targetBitRate;
    00211     unsigned m_outputFrameTime;
    00212     PInt64   m_inputFrameCount;
    00213     PInt64   m_outputFrameCount;
    00214
    00215     struct PacketEntry {
    00216       PacketEntry(RTP_DataFrame * rtp_, bool iFrame_)
    00217         : m_rtp(rtp_), m_iFrame(iFrame_)
    00218       { }
    00219
    00220       RTP_DataFrame * m_rtp;
    00221       bool m_iFrame;
    00222     };
    00223     std::deque<PacketEntry> m_packets;
    00224 };
    00225
    00226 namespace PWLibStupidLinkerHacks {
    00227   extern int rateControlKickerVal;
    00228 //  static class RateControlKicker { public: RateControlKicker() { rateControlKickerVal = 1; } } rateControlKicker;
    00229 };
    00230
    00231 #endif // OPAL_VIDEO
    00232
    00233 #endif // OPAL_RATE_CONTROL_H