blob: c91ab2bbe7c4e3fd038b375a92a46a9c555210ff [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org8fe03af2012-01-23 14:56:14 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_MEDIA_OPT_UTIL_H_
12#define MODULES_VIDEO_CODING_MEDIA_OPT_UTIL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <math.h>
15#include <stdlib.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
kwiberg3f55dea2016-02-29 05:51:59 -080017#include <memory>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_coding/internal_defines.h"
20#include "rtc_base/numerics/exp_filter.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020021#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000022
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000023namespace webrtc {
24namespace media_optimization {
25
marpan@google.com30ecda12011-09-09 17:15:12 +000026// Number of time periods used for (max) window filter for packet loss
philipel9d3ab612015-12-21 04:12:39 -080027// TODO(marpan): set reasonable window size for filtered packet loss,
marpan@google.com30ecda12011-09-09 17:15:12 +000028// adjustment should be based on logged/real data of loss stats/correlation.
29enum { kLossPrHistorySize = 10 };
30
31// 1000 ms, total filter length is (kLossPrHistorySize * 1000) ms
mikhal@google.comb29d9402011-08-01 16:39:20 +000032enum { kLossPrShortFilterWinMs = 1000 };
niklase@google.com470e71d2011-07-07 08:21:25 +000033
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +000034// The type of filter used on the received packet loss reports.
35enum FilterPacketLossMode {
philipel9d3ab612015-12-21 04:12:39 -080036 kNoFilter, // No filtering on received loss.
37 kAvgFilter, // Recursive average filter.
38 kMaxFilter // Max-window filter, over the time interval of:
39 // (kLossPrHistorySize * kLossPrShortFilterWinMs) ms.
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +000040};
41
niklase@google.com470e71d2011-07-07 08:21:25 +000042// Thresholds for hybrid NACK/FEC
43// common to media optimization and the jitter buffer.
pkasting@chromium.org16825b12015-01-12 21:51:21 +000044const int64_t kLowRttNackMs = 20;
niklase@google.com470e71d2011-07-07 08:21:25 +000045
philipelae284082016-05-09 12:14:29 +020046// If the RTT is higher than this an extra RTT wont be added to to the jitter
47// buffer delay.
48const int kMaxRttDelayThreshold = 500;
49
philipel9d3ab612015-12-21 04:12:39 -080050struct VCMProtectionParameters {
51 VCMProtectionParameters()
52 : rtt(0),
53 lossPr(0.0f),
54 bitRate(0.0f),
55 packetsPerFrame(0.0f),
56 packetsPerFrameKey(0.0f),
57 frameRate(0.0f),
58 keyFrameSize(0.0f),
59 fecRateDelta(0),
60 fecRateKey(0),
61 codecWidth(0),
62 codecHeight(0),
63 numLayers(1) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000064
philipel9d3ab612015-12-21 04:12:39 -080065 int64_t rtt;
66 float lossPr;
67 float bitRate;
68 float packetsPerFrame;
69 float packetsPerFrameKey;
70 float frameRate;
71 float keyFrameSize;
72 uint8_t fecRateDelta;
73 uint8_t fecRateKey;
74 uint16_t codecWidth;
75 uint16_t codecHeight;
76 int numLayers;
niklase@google.com470e71d2011-07-07 08:21:25 +000077};
78
niklase@google.com470e71d2011-07-07 08:21:25 +000079/******************************/
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000080/* VCMProtectionMethod class */
81/******************************/
niklase@google.com470e71d2011-07-07 08:21:25 +000082
philipel9d3ab612015-12-21 04:12:39 -080083enum VCMProtectionMethodEnum { kNack, kFec, kNackFec, kNone };
84
85class VCMLossProbabilitySample {
86 public:
87 VCMLossProbabilitySample() : lossPr255(0), timeMs(-1) {}
88
89 uint8_t lossPr255;
90 int64_t timeMs;
niklase@google.com470e71d2011-07-07 08:21:25 +000091};
92
philipel9d3ab612015-12-21 04:12:39 -080093class VCMProtectionMethod {
94 public:
95 VCMProtectionMethod();
96 virtual ~VCMProtectionMethod();
niklase@google.com470e71d2011-07-07 08:21:25 +000097
philipel9d3ab612015-12-21 04:12:39 -080098 // Updates the efficiency of the method using the parameters provided
99 //
100 // Input:
101 // - parameters : Parameters used to calculate efficiency
102 //
103 // Return value : True if this method is recommended in
104 // the given conditions.
105 virtual bool UpdateParameters(const VCMProtectionParameters* parameters) = 0;
106
107 // Returns the protection type
108 //
109 // Return value : The protection type
110 enum VCMProtectionMethodEnum Type() const { return _type; }
111
112 // Returns the effective packet loss for ER, required by this protection
113 // method
114 //
115 // Return value : Required effective packet loss
116 virtual uint8_t RequiredPacketLossER() { return _effectivePacketLoss; }
117
118 // Extracts the FEC protection factor for Key frame, required by this
119 // protection method
120 //
121 // Return value : Required protectionFactor for Key frame
122 virtual uint8_t RequiredProtectionFactorK() { return _protectionFactorK; }
123
124 // Extracts the FEC protection factor for Delta frame, required by this
125 // protection method
126 //
127 // Return value : Required protectionFactor for delta frame
128 virtual uint8_t RequiredProtectionFactorD() { return _protectionFactorD; }
129
130 // Extracts whether the FEC Unequal protection (UEP) is used for Key frame.
131 //
132 // Return value : Required Unequal protection on/off state.
133 virtual bool RequiredUepProtectionK() { return _useUepProtectionK; }
134
135 // Extracts whether the the FEC Unequal protection (UEP) is used for Delta
136 // frame.
137 //
138 // Return value : Required Unequal protection on/off state.
139 virtual bool RequiredUepProtectionD() { return _useUepProtectionD; }
140
141 virtual int MaxFramesFec() const { return 1; }
142
philipel9d3ab612015-12-21 04:12:39 -0800143 protected:
144 uint8_t _effectivePacketLoss;
145 uint8_t _protectionFactorK;
146 uint8_t _protectionFactorD;
147 // Estimation of residual loss after the FEC
148 float _scaleProtKey;
149 int32_t _maxPayloadSize;
150
philipel9d3ab612015-12-21 04:12:39 -0800151 bool _useUepProtectionK;
152 bool _useUepProtectionD;
153 float _corrFecCost;
154 enum VCMProtectionMethodEnum _type;
niklase@google.com470e71d2011-07-07 08:21:25 +0000155};
156
philipel9d3ab612015-12-21 04:12:39 -0800157class VCMNackMethod : public VCMProtectionMethod {
158 public:
159 VCMNackMethod();
160 virtual ~VCMNackMethod();
161 virtual bool UpdateParameters(const VCMProtectionParameters* parameters);
162 // Get the effective packet loss
163 bool EffectivePacketLoss(const VCMProtectionParameters* parameter);
niklase@google.com470e71d2011-07-07 08:21:25 +0000164};
165
philipel9d3ab612015-12-21 04:12:39 -0800166class VCMFecMethod : public VCMProtectionMethod {
167 public:
168 VCMFecMethod();
169 virtual ~VCMFecMethod();
170 virtual bool UpdateParameters(const VCMProtectionParameters* parameters);
171 // Get the effective packet loss for ER
172 bool EffectivePacketLoss(const VCMProtectionParameters* parameters);
173 // Get the FEC protection factors
174 bool ProtectionFactor(const VCMProtectionParameters* parameters);
175 // Get the boost for key frame protection
176 uint8_t BoostCodeRateKey(uint8_t packetFrameDelta,
177 uint8_t packetFrameKey) const;
178 // Convert the rates: defined relative to total# packets or source# packets
179 uint8_t ConvertFECRate(uint8_t codeRate) const;
180 // Get the average effective recovery from FEC: for random loss model
181 float AvgRecoveryFEC(const VCMProtectionParameters* parameters) const;
182 // Update FEC with protectionFactorD
183 void UpdateProtectionFactorD(uint8_t protectionFactorD);
184 // Update FEC with protectionFactorK
185 void UpdateProtectionFactorK(uint8_t protectionFactorK);
186 // Compute the bits per frame. Account for temporal layers when applicable.
187 int BitsPerFrame(const VCMProtectionParameters* parameters);
188
189 protected:
190 enum { kUpperLimitFramesFec = 6 };
191 // Thresholds values for the bytes/frame and round trip time, below which we
192 // may turn off FEC, depending on |_numLayers| and |_maxFramesFec|.
193 // Max bytes/frame for VGA, corresponds to ~140k at 25fps.
194 enum { kMaxBytesPerFrameForFec = 700 };
195 // Max bytes/frame for CIF and lower: corresponds to ~80k at 25fps.
196 enum { kMaxBytesPerFrameForFecLow = 400 };
197 // Max bytes/frame for frame size larger than VGA, ~200k at 25fps.
198 enum { kMaxBytesPerFrameForFecHigh = 1000 };
niklase@google.com470e71d2011-07-07 08:21:25 +0000199};
200
philipel9d3ab612015-12-21 04:12:39 -0800201class VCMNackFecMethod : public VCMFecMethod {
202 public:
203 VCMNackFecMethod(int64_t lowRttNackThresholdMs,
204 int64_t highRttNackThresholdMs);
205 virtual ~VCMNackFecMethod();
206 virtual bool UpdateParameters(const VCMProtectionParameters* parameters);
207 // Get the effective packet loss for ER
208 bool EffectivePacketLoss(const VCMProtectionParameters* parameters);
209 // Get the protection factors
210 bool ProtectionFactor(const VCMProtectionParameters* parameters);
211 // Get the max number of frames the FEC is allowed to be based on.
212 int MaxFramesFec() const;
213 // Turn off the FEC based on low bitrate and other factors.
214 bool BitRateTooLowForFec(const VCMProtectionParameters* parameters);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000215
philipel9d3ab612015-12-21 04:12:39 -0800216 private:
217 int ComputeMaxFramesFec(const VCMProtectionParameters* parameters);
218
219 int64_t _lowRttNackMs;
220 int64_t _highRttNackMs;
221 int _maxFramesFec;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222};
223
philipel9d3ab612015-12-21 04:12:39 -0800224class VCMLossProtectionLogic {
225 public:
226 explicit VCMLossProtectionLogic(int64_t nowMs);
227 ~VCMLossProtectionLogic();
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
philipel9d3ab612015-12-21 04:12:39 -0800229 // Set the protection method to be used
230 //
231 // Input:
232 // - newMethodType : New requested protection method type. If one
233 // is already set, it will be deleted and replaced
234 void SetMethod(VCMProtectionMethodEnum newMethodType);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000235
philipel9d3ab612015-12-21 04:12:39 -0800236 // Update the round-trip time
237 //
238 // Input:
239 // - rtt : Round-trip time in seconds.
240 void UpdateRtt(int64_t rtt);
niklase@google.com470e71d2011-07-07 08:21:25 +0000241
philipel9d3ab612015-12-21 04:12:39 -0800242 // Update the filtered packet loss.
243 //
244 // Input:
245 // - packetLossEnc : The reported packet loss filtered
246 // (max window or average)
247 void UpdateFilteredLossPr(uint8_t packetLossEnc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000248
philipel9d3ab612015-12-21 04:12:39 -0800249 // Update the current target bit rate.
250 //
251 // Input:
252 // - bitRate : The current target bit rate in kbits/s
253 void UpdateBitRate(float bitRate);
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000254
philipel9d3ab612015-12-21 04:12:39 -0800255 // Update the number of packets per frame estimate, for delta frames
256 //
257 // Input:
258 // - nPackets : Number of packets in the latest sent frame.
259 void UpdatePacketsPerFrame(float nPackets, int64_t nowMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000260
philipel9d3ab612015-12-21 04:12:39 -0800261 // Update the number of packets per frame estimate, for key frames
262 //
263 // Input:
264 // - nPackets : umber of packets in the latest sent frame.
265 void UpdatePacketsPerFrameKey(float nPackets, int64_t nowMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000266
philipel9d3ab612015-12-21 04:12:39 -0800267 // Update the keyFrameSize estimate
268 //
269 // Input:
270 // - keyFrameSize : The size of the latest sent key frame.
271 void UpdateKeyFrameSize(float keyFrameSize);
niklase@google.com470e71d2011-07-07 08:21:25 +0000272
philipel9d3ab612015-12-21 04:12:39 -0800273 // Update the frame rate
274 //
275 // Input:
276 // - frameRate : The current target frame rate.
277 void UpdateFrameRate(float frameRate) { _frameRate = frameRate; }
niklase@google.com470e71d2011-07-07 08:21:25 +0000278
philipel9d3ab612015-12-21 04:12:39 -0800279 // Update the frame size
280 //
281 // Input:
282 // - width : The codec frame width.
283 // - height : The codec frame height.
perkjc2c24f72016-07-11 01:47:32 -0700284 void UpdateFrameSize(size_t width, size_t height);
niklase@google.com470e71d2011-07-07 08:21:25 +0000285
philipel9d3ab612015-12-21 04:12:39 -0800286 // Update the number of active layers
287 //
288 // Input:
289 // - numLayers : Number of layers used.
290 void UpdateNumLayers(int numLayers);
niklase@google.com470e71d2011-07-07 08:21:25 +0000291
philipel9d3ab612015-12-21 04:12:39 -0800292 // The amount of packet loss to cover for with FEC.
293 //
294 // Input:
295 // - fecRateKey : Packet loss to cover for with FEC when
296 // sending key frames.
297 // - fecRateDelta : Packet loss to cover for with FEC when
298 // sending delta frames.
299 void UpdateFECRates(uint8_t fecRateKey, uint8_t fecRateDelta) {
300 _fecRateKey = fecRateKey;
301 _fecRateDelta = fecRateDelta;
302 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000303
philipel9d3ab612015-12-21 04:12:39 -0800304 // Update the protection methods with the current VCMProtectionParameters
305 // and set the requested protection settings.
306 // Return value : Returns true on update
307 bool UpdateMethod();
niklase@google.com470e71d2011-07-07 08:21:25 +0000308
philipel9d3ab612015-12-21 04:12:39 -0800309 // Returns the method currently selected.
310 //
311 // Return value : The protection method currently selected.
312 VCMProtectionMethod* SelectedMethod() const;
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +0000313
philipel9d3ab612015-12-21 04:12:39 -0800314 // Return the protection type of the currently selected method
315 VCMProtectionMethodEnum SelectedType() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000316
philipel9d3ab612015-12-21 04:12:39 -0800317 // Updates the filtered loss for the average and max window packet loss,
318 // and returns the filtered loss probability in the interval [0, 255].
319 // The returned filtered loss value depends on the parameter |filter_mode|.
320 // The input parameter |lossPr255| is the received packet loss.
niklase@google.com470e71d2011-07-07 08:21:25 +0000321
philipel9d3ab612015-12-21 04:12:39 -0800322 // Return value : The filtered loss probability
323 uint8_t FilteredLoss(int64_t nowMs,
324 FilterPacketLossMode filter_mode,
325 uint8_t lossPr255);
niklase@google.com470e71d2011-07-07 08:21:25 +0000326
philipel9d3ab612015-12-21 04:12:39 -0800327 void Reset(int64_t nowMs);
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000328
philipel9d3ab612015-12-21 04:12:39 -0800329 void Release();
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +0000330
philipel9d3ab612015-12-21 04:12:39 -0800331 private:
332 // Sets the available loss protection methods.
333 void UpdateMaxLossHistory(uint8_t lossPr255, int64_t now);
334 uint8_t MaxFilteredLossPr(int64_t nowMs) const;
kwiberg3f55dea2016-02-29 05:51:59 -0800335 std::unique_ptr<VCMProtectionMethod> _selectedMethod;
philipel9d3ab612015-12-21 04:12:39 -0800336 VCMProtectionParameters _currentParameters;
337 int64_t _rtt;
338 float _lossPr;
339 float _bitRate;
340 float _frameRate;
341 float _keyFrameSize;
342 uint8_t _fecRateKey;
343 uint8_t _fecRateDelta;
344 int64_t _lastPrUpdateT;
345 int64_t _lastPacketPerFrameUpdateT;
346 int64_t _lastPacketPerFrameUpdateTKey;
347 rtc::ExpFilter _lossPr255;
348 VCMLossProbabilitySample _lossPrHistory[kLossPrHistorySize];
349 uint8_t _shortMaxLossPr255;
350 rtc::ExpFilter _packetsPerFrame;
351 rtc::ExpFilter _packetsPerFrameKey;
perkjc2c24f72016-07-11 01:47:32 -0700352 size_t _codecWidth;
353 size_t _codecHeight;
philipel9d3ab612015-12-21 04:12:39 -0800354 int _numLayers;
niklase@google.com470e71d2011-07-07 08:21:25 +0000355};
356
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000357} // namespace media_optimization
358} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000359
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200360#endif // MODULES_VIDEO_CODING_MEDIA_OPT_UTIL_H_