OPAL Version 3.10.4

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files

- File List
- Globals

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
51 class OpalBitRateCalculator
52 {
53 public:
56 OpalBitRateCalculator();
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
142 PINDEX m_historySize;
143 PInt64 m_totalSize;
144 PINDEX m_historyFrames;
145
146 unsigned m_quanta;
147 unsigned m_bitRate;
148 bool m_first;
149 PInt64 m_baseTimeStamp;
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
169 class OpalVideoRateController
170 {
171 public:
172 OpalVideoRateController();
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
207 OpalBitRateCalculator m_bitRateCalc;
208
209 protected:
210 unsigned m_targetBitRate;
211 unsigned m_outputFrameTime;
212 PInt64 m_inputFrameCount;
213 PInt64 m_outputFrameCount;
214
215 struct PacketEntry {
216 PacketEntry(RTP_DataFrame * rtp_, bool iFrame_)
217 : m_rtp(rtp_), m_iFrame(iFrame_)
218 { }
219
220 RTP_DataFrame * m_rtp;
221 bool m_iFrame;
222 };
223 std::deque<PacketEntry> m_packets;
224 };
225
226 namespace PWLibStupidLinkerHacks {
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