OPAL  Version 3.10.10
ratectl.h
Go to the documentation of this file.
1 /*
2  * ratectl.h
3  *
4  * Video rate controller
5  *
6  * Open Phone Abstraction Library (OPAL)
7  *
8  * Copyright (C) 2007 Matthias Schneider
9  * Copyright (C) 2008 Post Increment
10  *
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/
15  *
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
19  * under the License.
20  *
21  * The Original Code is Open Phone Abstraction Library.
22  *
23  * The Initial Developer of the Original Code is Matthias Schneider
24  *
25  * Contributor(s): Post Increment
26  *
27  * $Revision: 22839 $
28  * $Author: rjongbloed $
29  * $Date: 2009-06-10 20:37:03 -0500 (Wed, 10 Jun 2009) $
30  */
31 
32 #ifndef OPAL_RATE_CONTROL_H
33 #define OPAL_RATE_CONTROL_H
34 
35 #ifdef P_USE_PRAGMA
36 #pragma interface
37 #endif
38 
39 #include <opal/buildopts.h>
40 
41 #if OPAL_VIDEO
42 
43 #include <rtp/rtp.h>
44 
45 extern double OpalCalcSNR(const BYTE * src1, const BYTE * src2, PINDEX dataLen);
46 
52 {
53  public:
57 
60  void Reset();
61 
64  void SetQuanta(
65  unsigned quanta_
66  );
67 
70  unsigned GetQuanta() const
71  { return m_quanta; }
72 
75  void AddPacket(PINDEX size, bool marker);
76 
79  unsigned GetBitRate();
80 
83  unsigned GetAverageBitRate();
84 
87  unsigned GetAveragePacketSize();
88 
91  unsigned GetTrialBitRate(PINDEX size);
92 
95  PInt64 GetTotalSize() const;
96 
99  PInt64 GetTotalTime() const;
100 
103  size_t GetHistoryCount() const
104  { return m_history.size(); }
105 
108  unsigned GetHistorySize() const
109  { return m_historySize; }
110 
113  PInt64 GetEarliestHistoryTime() const
114  { if (m_history.size() == 0) return 0; return m_history.begin()->m_timeStamp; }
115 
118  unsigned GetHistoryFrames() const;
119 
120  // flush old data from history
121  void Flush();
122 
123  // used to get "now"
124  static PInt64 GetNow();
125 
126  protected:
127 
128  void Flush(PInt64 now);
129 
130  struct History {
131  History(PINDEX size_, PInt64 timeStamp_, bool marker_)
132  : m_size(size_), m_timeStamp(timeStamp_), m_marker(marker_)
133  { }
134 
135  PINDEX m_size;
136  PInt64 m_timeStamp;
137  bool m_marker;
138  };
139 
140  std::deque<History> m_history;
141 
143  PInt64 m_totalSize;
145 
146  unsigned m_quanta;
147  unsigned m_bitRate;
148  bool m_first;
150 };
151 
152 //
153 // Declare a generic video rate controller class.
154 // A rate controller seeks to maintain a constant bit rate by manipulating
155 // the parameters of the video stream
156 //
157 // Before encoding a potential output frame, use the SkipFrame function to determine if the
158 // frame should be skipped.
159 //
160 // If the frame is not skipped, encode the frame and call PushFrame to add the frame to the rate controller queue
161 // PopFrame can then be called to retreive frames to transmit
162 //
163 // PushFrame must always be called with packets from a single video frame, but PopFrame may return packets
164 // from multiple video frames
165 //
166 
167 class OpalMediaFormat;
168 
170 {
171  public:
173 
174  virtual ~OpalVideoRateController();
175 
178  virtual void Open(
179  const OpalMediaFormat & mediaFormat
180  );
181 
186  virtual bool SkipFrame(
187  bool & forceIFrame
188  ) = 0;
189 
192  virtual void Push(
193  RTP_DataFrameList & inputFrames,
194  bool iFrame
195  );
196 
199  virtual bool Pop(
200  RTP_DataFrameList & outputPackets,
201  bool & iFrame,
202  bool force
203  );
204 
208 
209  protected:
210  unsigned m_targetBitRate;
214 
215  struct PacketEntry {
216  PacketEntry(RTP_DataFrame * rtp_, bool iFrame_)
217  : m_rtp(rtp_), m_iFrame(iFrame_)
218  { }
219 
221  bool m_iFrame;
222  };
223  std::deque<PacketEntry> m_packets;
224 };
225 
227  extern int rateControlKickerVal;
228 // static class RateControlKicker { public: RateControlKicker() { rateControlKickerVal = 1; } } rateControlKicker;
229 };
230 
231 #endif // OPAL_VIDEO
232 
233 #endif // OPAL_RATE_CONTROL_H
std::deque< History > m_history
Definition: ratectl.h:140
unsigned GetTrialBitRate(PINDEX size)
PInt64 GetTotalSize() const
PINDEX m_size
Definition: ratectl.h:135
std::deque< PacketEntry > m_packets
Definition: ratectl.h:223
unsigned m_outputFrameTime
Definition: ratectl.h:211
double OpalCalcSNR(const BYTE *src1, const BYTE *src2, PINDEX dataLen)
Definition: ratectl.h:226
virtual void Open(const OpalMediaFormat &mediaFormat)
Definition: ratectl.h:51
void AddPacket(PINDEX size, bool marker)
PInt64 m_totalSize
Definition: ratectl.h:143
RTP_DataFrame * m_rtp
Definition: ratectl.h:220
OpalBitRateCalculator m_bitRateCalc
Definition: ratectl.h:207
History(PINDEX size_, PInt64 timeStamp_, bool marker_)
Definition: ratectl.h:131
Definition: rtp.h:71
PINDEX m_historySize
Definition: ratectl.h:142
Definition: mediafmt.h:724
bool m_first
Definition: ratectl.h:148
static PInt64 GetNow()
virtual void Push(RTP_DataFrameList &inputFrames, bool iFrame)
void SetQuanta(unsigned quanta_)
PInt64 m_outputFrameCount
Definition: ratectl.h:213
Definition: ratectl.h:130
PInt64 m_timeStamp
Definition: ratectl.h:136
unsigned GetAveragePacketSize()
unsigned m_targetBitRate
Definition: ratectl.h:210
unsigned GetAverageBitRate()
unsigned GetQuanta() const
Definition: ratectl.h:70
virtual ~OpalVideoRateController()
PINDEX m_historyFrames
Definition: ratectl.h:144
unsigned GetHistoryFrames() const
virtual bool Pop(RTP_DataFrameList &outputPackets, bool &iFrame, bool force)
size_t GetHistoryCount() const
Definition: ratectl.h:103
PacketEntry(RTP_DataFrame *rtp_, bool iFrame_)
Definition: ratectl.h:216
PInt64 GetEarliestHistoryTime() const
Definition: ratectl.h:113
PInt64 GetTotalTime() const
PInt64 m_inputFrameCount
Definition: ratectl.h:212
unsigned m_quanta
Definition: ratectl.h:146
PInt64 m_baseTimeStamp
Definition: ratectl.h:149
bool m_iFrame
Definition: ratectl.h:221
virtual bool SkipFrame(bool &forceIFrame)=0
unsigned m_bitRate
Definition: ratectl.h:147
unsigned GetHistorySize() const
Definition: ratectl.h:108
Definition: ratectl.h:215
Definition: ratectl.h:169
bool m_marker
Definition: ratectl.h:137