OPAL  Version 3.14.3
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: 29536 $
28  * $Author: rjongbloed $
29  * $Date: 2013-04-19 18:55:15 +1000 (Fri, 19 Apr 2013) $
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_config.h>
40 
41 #if OPAL_VIDEO
42 
43 #include <rtp/rtp.h>
44 
45 #include <deque>
46 
47 extern double OpalCalcSNR(const BYTE * src1, const BYTE * src2, PINDEX dataLen);
48 
54 {
55  public:
59 
62  void Reset();
63 
66  void SetQuanta(
67  unsigned quanta_
68  );
69 
72  unsigned GetQuanta() const
73  { return m_quanta; }
74 
77  void AddPacket(PINDEX size, bool marker);
78 
81  unsigned GetBitRate();
82 
85  unsigned GetAverageBitRate();
86 
89  unsigned GetAveragePacketSize();
90 
93  unsigned GetTrialBitRate(PINDEX size);
94 
97  PInt64 GetTotalSize() const;
98 
101  PInt64 GetTotalTime() const;
102 
105  size_t GetHistoryCount() const
106  { return m_history.size(); }
107 
110  unsigned GetHistorySize() const
111  { return m_historySize; }
112 
115  PInt64 GetEarliestHistoryTime() const
116  { if (m_history.size() == 0) return 0; return m_history.begin()->m_timeStamp; }
117 
120  unsigned GetHistoryFrames() const;
121 
122  // flush old data from history
123  void Flush();
124 
125  // used to get "now"
126  static PInt64 GetNow();
127 
128  protected:
129 
130  void Flush(PInt64 now);
131 
132  struct History {
133  History(PINDEX size_, PInt64 timeStamp_, bool marker_)
134  : m_size(size_), m_timeStamp(timeStamp_), m_marker(marker_)
135  { }
136 
137  PINDEX m_size;
138  PInt64 m_timeStamp;
139  bool m_marker;
140  };
141 
142  std::deque<History> m_history;
143 
145  PInt64 m_totalSize;
147 
148  unsigned m_quanta;
149  unsigned m_bitRate;
150  bool m_first;
152 };
153 
154 //
155 // Declare a generic video rate controller class.
156 // A rate controller seeks to maintain a constant bit rate by manipulating
157 // the parameters of the video stream
158 //
159 // Before encoding a potential output frame, use the SkipFrame function to determine if the
160 // frame should be skipped.
161 //
162 // If the frame is not skipped, encode the frame and call PushFrame to add the frame to the rate controller queue
163 // PopFrame can then be called to retreive frames to transmit
164 //
165 // PushFrame must always be called with packets from a single video frame, but PopFrame may return packets
166 // from multiple video frames
167 //
168 
169 class OpalMediaFormat;
170 
172 {
173  public:
175 
176  virtual ~OpalVideoRateController();
177 
180  virtual void Open(
181  const OpalMediaFormat & mediaFormat
182  );
183 
188  virtual bool SkipFrame(
189  bool & forceIFrame
190  ) = 0;
191 
194  virtual void Push(
195  RTP_DataFrameList & inputFrames,
196  bool iFrame
197  );
198 
201  virtual bool Pop(
202  RTP_DataFrameList & outputPackets,
203  bool & iFrame,
204  bool force
205  );
206 
210 
211  protected:
212  unsigned m_targetBitRate;
216 
217  struct PacketEntry {
218  PacketEntry(RTP_DataFrame * rtp_, bool iFrame_)
219  : m_rtp(rtp_), m_iFrame(iFrame_)
220  { }
221 
223  bool m_iFrame;
224  };
225  std::deque<PacketEntry> m_packets;
226 };
227 
228 namespace PWLibStupidLinkerHacks {
229  extern int rateControlKickerVal;
230 // static class RateControlKicker { public: RateControlKicker() { rateControlKickerVal = 1; } } rateControlKicker;
231 };
232 
233 #endif // OPAL_VIDEO
234 
235 #endif // OPAL_RATE_CONTROL_H