blob: 3edd0541085a53c86d3a3960ba485c84a44e020a [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +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
Henrik Kjellander2557b862015-11-18 22:00:21 +010011#include "webrtc/modules/video_coding/media_opt_util.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
niklase@google.com470e71d2011-07-07 08:21:25 +000013#include <float.h>
14#include <limits.h>
pbos@webrtc.orga4407322013-07-16 12:32:05 +000015#include <math.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
philipel9d3ab612015-12-21 04:12:39 -080017#include <algorithm>
brandtr05f845d2016-11-16 22:59:39 -080018#include <limits>
philipel9d3ab612015-12-21 04:12:39 -080019
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/include/module_common_types.h"
pbos@webrtc.orga4407322013-07-16 12:32:05 +000021#include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010022#include "webrtc/modules/video_coding/include/video_coding_defines.h"
brandtr71b1c1f2017-01-12 06:16:24 -080023#include "webrtc/modules/video_coding/fec_rate_table.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010024#include "webrtc/modules/video_coding/nack_fec_tables.h"
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +000025
niklase@google.com470e71d2011-07-07 08:21:25 +000026namespace webrtc {
Peter Boström9cb1f302015-04-01 11:39:49 +020027// Max value of loss rates in off-line model
28static const int kPacketLossMax = 129;
29
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000030namespace media_optimization {
niklase@google.com470e71d2011-07-07 08:21:25 +000031
Peter Boström9cb1f302015-04-01 11:39:49 +020032VCMProtectionMethod::VCMProtectionMethod()
33 : _effectivePacketLoss(0),
34 _protectionFactorK(0),
35 _protectionFactorD(0),
36 _scaleProtKey(2.0f),
37 _maxPayloadSize(1460),
Peter Boström9cb1f302015-04-01 11:39:49 +020038 _corrFecCost(1.0),
philipel9d3ab612015-12-21 04:12:39 -080039 _type(kNone) {}
mikhal@webrtc.orga057a952011-08-26 21:17:34 +000040
pbosc0430522016-05-01 17:19:05 -070041VCMProtectionMethod::~VCMProtectionMethod() {}
marpan@google.com86548c62011-07-12 17:12:57 +000042
pkasting@chromium.org16825b12015-01-12 21:51:21 +000043VCMNackFecMethod::VCMNackFecMethod(int64_t lowRttNackThresholdMs,
44 int64_t highRttNackThresholdMs)
stefan@webrtc.org932ab182011-11-29 11:33:31 +000045 : VCMFecMethod(),
46 _lowRttNackMs(lowRttNackThresholdMs),
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +000047 _highRttNackMs(highRttNackThresholdMs),
48 _maxFramesFec(1) {
stefan@webrtc.org932ab182011-11-29 11:33:31 +000049 assert(lowRttNackThresholdMs >= -1 && highRttNackThresholdMs >= -1);
50 assert(highRttNackThresholdMs == -1 ||
51 lowRttNackThresholdMs <= highRttNackThresholdMs);
52 assert(lowRttNackThresholdMs > -1 || highRttNackThresholdMs == -1);
53 _type = kNackFec;
mikhal@google.com77408882011-07-22 22:05:25 +000054}
55
philipel9d3ab612015-12-21 04:12:39 -080056VCMNackFecMethod::~VCMNackFecMethod() {
57 //
mikhal@google.com77408882011-07-22 22:05:25 +000058}
philipel9d3ab612015-12-21 04:12:39 -080059bool VCMNackFecMethod::ProtectionFactor(
60 const VCMProtectionParameters* parameters) {
61 // Hybrid Nack FEC has three operational modes:
62 // 1. Low RTT (below kLowRttNackMs) - Nack only: Set FEC rate
63 // (_protectionFactorD) to zero. -1 means no FEC.
64 // 2. High RTT (above _highRttNackMs) - FEC Only: Keep FEC factors.
65 // -1 means always allow NACK.
66 // 3. Medium RTT values - Hybrid mode: We will only nack the
67 // residual following the decoding of the FEC (refer to JB logic). FEC
68 // delta protection factor will be adjusted based on the RTT.
mikhal@google.com022716b2011-07-20 23:12:57 +000069
philipel9d3ab612015-12-21 04:12:39 -080070 // Otherwise: we count on FEC; if the RTT is below a threshold, then we
71 // nack the residual, based on a decision made in the JB.
mikhal@google.com022716b2011-07-20 23:12:57 +000072
philipel9d3ab612015-12-21 04:12:39 -080073 // Compute the protection factors
74 VCMFecMethod::ProtectionFactor(parameters);
75 if (_lowRttNackMs == -1 || parameters->rtt < _lowRttNackMs) {
76 _protectionFactorD = 0;
77 VCMFecMethod::UpdateProtectionFactorD(_protectionFactorD);
mikhal@google.com320813c2011-08-03 20:47:50 +000078
mikhal@google.com679450f2011-08-01 22:14:58 +000079 // When in Hybrid mode (RTT range), adjust FEC rates based on the
80 // RTT (NACK effectiveness) - adjustment factor is in the range [0,1].
philipel9d3ab612015-12-21 04:12:39 -080081 } else if (_highRttNackMs == -1 || parameters->rtt < _highRttNackMs) {
82 // TODO(mikhal): Disabling adjustment temporarily.
83 // uint16_t rttIndex = (uint16_t) parameters->rtt;
84 float adjustRtt = 1.0f; // (float)VCMNackFecTable[rttIndex] / 100.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +000085
philipel9d3ab612015-12-21 04:12:39 -080086 // Adjust FEC with NACK on (for delta frame only)
87 // table depends on RTT relative to rttMax (NACK Threshold)
88 _protectionFactorD = static_cast<uint8_t>(
89 adjustRtt * static_cast<float>(_protectionFactorD));
90 // update FEC rates after applying adjustment
91 VCMFecMethod::UpdateProtectionFactorD(_protectionFactorD);
92 }
mikhal@google.com679450f2011-08-01 22:14:58 +000093
philipel9d3ab612015-12-21 04:12:39 -080094 return true;
mikhal@google.com022716b2011-07-20 23:12:57 +000095}
niklase@google.com470e71d2011-07-07 08:21:25 +000096
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +000097int VCMNackFecMethod::ComputeMaxFramesFec(
98 const VCMProtectionParameters* parameters) {
99 if (parameters->numLayers > 2) {
100 // For more than 2 temporal layers we will only have FEC on the base layer,
101 // and the base layers will be pretty far apart. Therefore we force one
102 // frame FEC.
103 return 1;
104 }
105 // We set the max number of frames to base the FEC on so that on average
106 // we will have complete frames in one RTT. Note that this is an upper
107 // bound, and that the actual number of frames used for FEC is decided by the
108 // RTP module based on the actual number of packets and the protection factor.
philipel9d3ab612015-12-21 04:12:39 -0800109 float base_layer_framerate =
110 parameters->frameRate /
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000111 static_cast<float>(1 << (parameters->numLayers - 1));
philipel9d3ab612015-12-21 04:12:39 -0800112 int max_frames_fec = std::max(
113 static_cast<int>(2.0f * base_layer_framerate * parameters->rtt / 1000.0f +
114 0.5f),
115 1);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000116 // |kUpperLimitFramesFec| is the upper limit on how many frames we
117 // allow any FEC to be based on.
118 if (max_frames_fec > kUpperLimitFramesFec) {
119 max_frames_fec = kUpperLimitFramesFec;
120 }
121 return max_frames_fec;
122}
123
124int VCMNackFecMethod::MaxFramesFec() const {
125 return _maxFramesFec;
126}
127
marpan@webrtc.org88ad06b2012-04-20 16:05:24 +0000128bool VCMNackFecMethod::BitRateTooLowForFec(
129 const VCMProtectionParameters* parameters) {
130 // Bitrate below which we turn off FEC, regardless of reported packet loss.
131 // The condition should depend on resolution and content. For now, use
132 // threshold on bytes per frame, with some effect for the frame size.
133 // The condition for turning off FEC is also based on other factors,
134 // such as |_numLayers|, |_maxFramesFec|, and |_rtt|.
135 int estimate_bytes_per_frame = 1000 * BitsPerFrame(parameters) / 8;
136 int max_bytes_per_frame = kMaxBytesPerFrameForFec;
137 int num_pixels = parameters->codecWidth * parameters->codecHeight;
138 if (num_pixels <= 352 * 288) {
139 max_bytes_per_frame = kMaxBytesPerFrameForFecLow;
140 } else if (num_pixels > 640 * 480) {
141 max_bytes_per_frame = kMaxBytesPerFrameForFecHigh;
142 }
philipel9d3ab612015-12-21 04:12:39 -0800143 // TODO(marpan): add condition based on maximum frames used for FEC,
marpan@webrtc.org88ad06b2012-04-20 16:05:24 +0000144 // and expand condition based on frame size.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000145 // Max round trip time threshold in ms.
146 const int64_t kMaxRttTurnOffFec = 200;
marpan@webrtc.org88ad06b2012-04-20 16:05:24 +0000147 if (estimate_bytes_per_frame < max_bytes_per_frame &&
philipel9d3ab612015-12-21 04:12:39 -0800148 parameters->numLayers < 3 && parameters->rtt < kMaxRttTurnOffFec) {
marpan@webrtc.org88ad06b2012-04-20 16:05:24 +0000149 return true;
150 }
151 return false;
152}
153
philipel9d3ab612015-12-21 04:12:39 -0800154bool VCMNackFecMethod::EffectivePacketLoss(
155 const VCMProtectionParameters* parameters) {
156 // Set the effective packet loss for encoder (based on FEC code).
157 // Compute the effective packet loss and residual packet loss due to FEC.
158 VCMFecMethod::EffectivePacketLoss(parameters);
159 return true;
mikhal@google.com022716b2011-07-20 23:12:57 +0000160}
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
philipel9d3ab612015-12-21 04:12:39 -0800162bool VCMNackFecMethod::UpdateParameters(
163 const VCMProtectionParameters* parameters) {
164 ProtectionFactor(parameters);
165 EffectivePacketLoss(parameters);
166 _maxFramesFec = ComputeMaxFramesFec(parameters);
167 if (BitRateTooLowForFec(parameters)) {
168 _protectionFactorK = 0;
169 _protectionFactorD = 0;
170 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
philipel9d3ab612015-12-21 04:12:39 -0800172 // Protection/fec rates obtained above are defined relative to total number
173 // of packets (total rate: source + fec) FEC in RTP module assumes
174 // protection factor is defined relative to source number of packets so we
175 // should convert the factor to reduce mismatch between mediaOpt's rate and
176 // the actual one
177 _protectionFactorK = VCMFecMethod::ConvertFECRate(_protectionFactorK);
178 _protectionFactorD = VCMFecMethod::ConvertFECRate(_protectionFactorD);
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
philipel9d3ab612015-12-21 04:12:39 -0800180 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181}
182
philipel9d3ab612015-12-21 04:12:39 -0800183VCMNackMethod::VCMNackMethod() : VCMProtectionMethod() {
184 _type = kNack;
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000185}
186
philipel9d3ab612015-12-21 04:12:39 -0800187VCMNackMethod::~VCMNackMethod() {
188 //
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000189}
190
philipel9d3ab612015-12-21 04:12:39 -0800191bool VCMNackMethod::EffectivePacketLoss(
192 const VCMProtectionParameters* parameter) {
193 // Effective Packet Loss, NA in current version.
194 _effectivePacketLoss = 0;
195 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
197
philipel9d3ab612015-12-21 04:12:39 -0800198bool VCMNackMethod::UpdateParameters(
199 const VCMProtectionParameters* parameters) {
200 // Compute the effective packet loss
201 EffectivePacketLoss(parameters);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
philipel9d3ab612015-12-21 04:12:39 -0800203 // nackCost = (bitRate - nackCost) * (lossPr)
204 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205}
206
philipel9d3ab612015-12-21 04:12:39 -0800207VCMFecMethod::VCMFecMethod() : VCMProtectionMethod() {
208 _type = kFec;
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000209}
philipel9d3ab612015-12-21 04:12:39 -0800210VCMFecMethod::~VCMFecMethod() {
211 //
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000212}
213
philipel9d3ab612015-12-21 04:12:39 -0800214uint8_t VCMFecMethod::BoostCodeRateKey(uint8_t packetFrameDelta,
215 uint8_t packetFrameKey) const {
216 uint8_t boostRateKey = 2;
217 // Default: ratio scales the FEC protection up for I frames
218 uint8_t ratio = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
philipel9d3ab612015-12-21 04:12:39 -0800220 if (packetFrameDelta > 0) {
221 ratio = (int8_t)(packetFrameKey / packetFrameDelta);
222 }
223 ratio = VCM_MAX(boostRateKey, ratio);
niklase@google.com470e71d2011-07-07 08:21:25 +0000224
philipel9d3ab612015-12-21 04:12:39 -0800225 return ratio;
niklase@google.com470e71d2011-07-07 08:21:25 +0000226}
227
philipel9d3ab612015-12-21 04:12:39 -0800228uint8_t VCMFecMethod::ConvertFECRate(uint8_t codeRateRTP) const {
229 return static_cast<uint8_t>(VCM_MIN(
230 255,
231 (0.5 + 255.0 * codeRateRTP / static_cast<float>(255 - codeRateRTP))));
niklase@google.com470e71d2011-07-07 08:21:25 +0000232}
233
mikhal@google.com679450f2011-08-01 22:14:58 +0000234// Update FEC with protectionFactorD
philipel9d3ab612015-12-21 04:12:39 -0800235void VCMFecMethod::UpdateProtectionFactorD(uint8_t protectionFactorD) {
236 _protectionFactorD = protectionFactorD;
mikhal@google.com679450f2011-08-01 22:14:58 +0000237}
238
mikhal@webrtc.orgd0752c32011-10-19 15:48:30 +0000239// Update FEC with protectionFactorK
philipel9d3ab612015-12-21 04:12:39 -0800240void VCMFecMethod::UpdateProtectionFactorK(uint8_t protectionFactorK) {
241 _protectionFactorK = protectionFactorK;
mikhal@webrtc.orgd0752c32011-10-19 15:48:30 +0000242}
243
philipel9d3ab612015-12-21 04:12:39 -0800244bool VCMFecMethod::ProtectionFactor(const VCMProtectionParameters* parameters) {
245 // FEC PROTECTION SETTINGS: varies with packet loss and bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000246
philipel9d3ab612015-12-21 04:12:39 -0800247 // No protection if (filtered) packetLoss is 0
brandtr05f845d2016-11-16 22:59:39 -0800248 uint8_t packetLoss = static_cast<uint8_t>(255 * parameters->lossPr);
philipel9d3ab612015-12-21 04:12:39 -0800249 if (packetLoss == 0) {
250 _protectionFactorK = 0;
251 _protectionFactorD = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252 return true;
philipel9d3ab612015-12-21 04:12:39 -0800253 }
254
255 // Parameters for FEC setting:
256 // first partition size, thresholds, table pars, spatial resoln fac.
257
258 // First partition protection: ~ 20%
brandtr05f845d2016-11-16 22:59:39 -0800259 uint8_t firstPartitionProt = static_cast<uint8_t>(255 * 0.20);
philipel9d3ab612015-12-21 04:12:39 -0800260
261 // Minimum protection level needed to generate one FEC packet for one
262 // source packet/frame (in RTP sender)
263 uint8_t minProtLevelFec = 85;
264
265 // Threshold on packetLoss and bitRrate/frameRate (=average #packets),
266 // above which we allocate protection to cover at least first partition.
267 uint8_t lossThr = 0;
268 uint8_t packetNumThr = 1;
269
270 // Parameters for range of rate index of table.
271 const uint8_t ratePar1 = 5;
272 const uint8_t ratePar2 = 49;
273
274 // Spatial resolution size, relative to a reference size.
275 float spatialSizeToRef =
276 static_cast<float>(parameters->codecWidth * parameters->codecHeight) /
277 (static_cast<float>(704 * 576));
278 // resolnFac: This parameter will generally increase/decrease the FEC rate
279 // (for fixed bitRate and packetLoss) based on system size.
280 // Use a smaller exponent (< 1) to control/soften system size effect.
281 const float resolnFac = 1.0 / powf(spatialSizeToRef, 0.3f);
282
283 const int bitRatePerFrame = BitsPerFrame(parameters);
284
285 // Average number of packets per frame (source and fec):
brandtr05f845d2016-11-16 22:59:39 -0800286 const uint8_t avgTotPackets = static_cast<uint8_t>(
287 std::min(static_cast<float>(std::numeric_limits<uint8_t>::max()),
288 1.5f +
289 static_cast<float>(bitRatePerFrame) * 1000.0f /
290 static_cast<float>(8.0 * _maxPayloadSize)));
philipel9d3ab612015-12-21 04:12:39 -0800291
292 // FEC rate parameters: for P and I frame
293 uint8_t codeRateDelta = 0;
294 uint8_t codeRateKey = 0;
295
296 // Get index for table: the FEC protection depends on an effective rate.
297 // The range on the rate index corresponds to rates (bps)
298 // from ~200k to ~8000k, for 30fps
299 const uint16_t effRateFecTable =
300 static_cast<uint16_t>(resolnFac * bitRatePerFrame);
brandtr05f845d2016-11-16 22:59:39 -0800301 uint8_t rateIndexTable = static_cast<uint8_t>(
302 VCM_MAX(VCM_MIN((effRateFecTable - ratePar1) / ratePar1, ratePar2), 0));
philipel9d3ab612015-12-21 04:12:39 -0800303
304 // Restrict packet loss range to 50:
305 // current tables defined only up to 50%
306 if (packetLoss >= kPacketLossMax) {
307 packetLoss = kPacketLossMax - 1;
308 }
309 uint16_t indexTable = rateIndexTable * kPacketLossMax + packetLoss;
310
311 // Check on table index
brandtr71b1c1f2017-01-12 06:16:24 -0800312 RTC_DCHECK_LT(indexTable, kFecRateTableSize);
philipel9d3ab612015-12-21 04:12:39 -0800313
314 // Protection factor for P frame
brandtr71b1c1f2017-01-12 06:16:24 -0800315 codeRateDelta = kFecRateTable[indexTable];
philipel9d3ab612015-12-21 04:12:39 -0800316
317 if (packetLoss > lossThr && avgTotPackets > packetNumThr) {
318 // Set a minimum based on first partition size.
319 if (codeRateDelta < firstPartitionProt) {
320 codeRateDelta = firstPartitionProt;
321 }
322 }
323
324 // Check limit on amount of protection for P frame; 50% is max.
325 if (codeRateDelta >= kPacketLossMax) {
326 codeRateDelta = kPacketLossMax - 1;
327 }
328
philipel9d3ab612015-12-21 04:12:39 -0800329 // For Key frame:
330 // Effectively at a higher rate, so we scale/boost the rate
331 // The boost factor may depend on several factors: ratio of packet
332 // number of I to P frames, how much protection placed on P frames, etc.
brandtr05f845d2016-11-16 22:59:39 -0800333 const uint8_t packetFrameDelta =
334 static_cast<uint8_t>(0.5 + parameters->packetsPerFrame);
philipel9d3ab612015-12-21 04:12:39 -0800335 const uint8_t packetFrameKey =
brandtr05f845d2016-11-16 22:59:39 -0800336 static_cast<uint8_t>(0.5 + parameters->packetsPerFrameKey);
philipel9d3ab612015-12-21 04:12:39 -0800337 const uint8_t boostKey = BoostCodeRateKey(packetFrameDelta, packetFrameKey);
338
brandtr05f845d2016-11-16 22:59:39 -0800339 rateIndexTable = static_cast<uint8_t>(VCM_MAX(
philipel9d3ab612015-12-21 04:12:39 -0800340 VCM_MIN(1 + (boostKey * effRateFecTable - ratePar1) / ratePar1, ratePar2),
brandtr05f845d2016-11-16 22:59:39 -0800341 0));
philipel9d3ab612015-12-21 04:12:39 -0800342 uint16_t indexTableKey = rateIndexTable * kPacketLossMax + packetLoss;
343
brandtr71b1c1f2017-01-12 06:16:24 -0800344 indexTableKey = VCM_MIN(indexTableKey, kFecRateTableSize);
philipel9d3ab612015-12-21 04:12:39 -0800345
346 // Check on table index
brandtr71b1c1f2017-01-12 06:16:24 -0800347 assert(indexTableKey < kFecRateTableSize);
philipel9d3ab612015-12-21 04:12:39 -0800348
349 // Protection factor for I frame
brandtr71b1c1f2017-01-12 06:16:24 -0800350 codeRateKey = kFecRateTable[indexTableKey];
philipel9d3ab612015-12-21 04:12:39 -0800351
352 // Boosting for Key frame.
353 int boostKeyProt = _scaleProtKey * codeRateDelta;
354 if (boostKeyProt >= kPacketLossMax) {
355 boostKeyProt = kPacketLossMax - 1;
356 }
357
358 // Make sure I frame protection is at least larger than P frame protection,
359 // and at least as high as filtered packet loss.
360 codeRateKey = static_cast<uint8_t>(
361 VCM_MAX(packetLoss, VCM_MAX(boostKeyProt, codeRateKey)));
362
363 // Check limit on amount of protection for I frame: 50% is max.
364 if (codeRateKey >= kPacketLossMax) {
365 codeRateKey = kPacketLossMax - 1;
366 }
367
368 _protectionFactorK = codeRateKey;
369 _protectionFactorD = codeRateDelta;
370
371 // Generally there is a rate mis-match between the FEC cost estimated
372 // in mediaOpt and the actual FEC cost sent out in RTP module.
373 // This is more significant at low rates (small # of source packets), where
374 // the granularity of the FEC decreases. In this case, non-zero protection
375 // in mediaOpt may generate 0 FEC packets in RTP sender (since actual #FEC
376 // is based on rounding off protectionFactor on actual source packet number).
377 // The correction factor (_corrFecCost) attempts to corrects this, at least
378 // for cases of low rates (small #packets) and low protection levels.
379
380 float numPacketsFl = 1.0f + (static_cast<float>(bitRatePerFrame) * 1000.0 /
381 static_cast<float>(8.0 * _maxPayloadSize) +
382 0.5);
383
384 const float estNumFecGen =
385 0.5f + static_cast<float>(_protectionFactorD * numPacketsFl / 255.0f);
386
387 // We reduce cost factor (which will reduce overhead for FEC and
388 // hybrid method) and not the protectionFactor.
389 _corrFecCost = 1.0f;
390 if (estNumFecGen < 1.1f && _protectionFactorD < minProtLevelFec) {
391 _corrFecCost = 0.5f;
392 }
393 if (estNumFecGen < 0.9f && _protectionFactorD < minProtLevelFec) {
394 _corrFecCost = 0.0f;
395 }
396
philipel9d3ab612015-12-21 04:12:39 -0800397 // DONE WITH FEC PROTECTION SETTINGS
398 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000399}
400
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +0000401int VCMFecMethod::BitsPerFrame(const VCMProtectionParameters* parameters) {
402 // When temporal layers are available FEC will only be applied on the base
403 // layer.
404 const float bitRateRatio =
philipel9d3ab612015-12-21 04:12:39 -0800405 kVp8LayerRateAlloction[parameters->numLayers - 1][0];
thakis@chromium.org65bc2542012-08-13 19:26:12 +0000406 float frameRateRatio = powf(1 / 2.0, parameters->numLayers - 1);
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +0000407 float bitRate = parameters->bitRate * bitRateRatio;
408 float frameRate = parameters->frameRate * frameRateRatio;
409
410 // TODO(mikhal): Update factor following testing.
411 float adjustmentFactor = 1;
412
Peter Boström5e8351b2016-01-28 23:55:29 +0100413 if (frameRate < 1.0f)
414 frameRate = 1.0f;
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +0000415 // Average bits per frame (units of kbits)
416 return static_cast<int>(adjustmentFactor * bitRate / frameRate);
417}
418
philipel9d3ab612015-12-21 04:12:39 -0800419bool VCMFecMethod::EffectivePacketLoss(
420 const VCMProtectionParameters* parameters) {
421 // Effective packet loss to encoder is based on RPL (residual packet loss)
422 // this is a soft setting based on degree of FEC protection
423 // RPL = received/input packet loss - average_FEC_recovery
424 // note: received/input packet loss may be filtered based on FilteredLoss
niklase@google.com470e71d2011-07-07 08:21:25 +0000425
philipel9d3ab612015-12-21 04:12:39 -0800426 // Effective Packet Loss, NA in current version.
427 _effectivePacketLoss = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000428
philipel9d3ab612015-12-21 04:12:39 -0800429 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000430}
431
philipel9d3ab612015-12-21 04:12:39 -0800432bool VCMFecMethod::UpdateParameters(const VCMProtectionParameters* parameters) {
433 // Compute the protection factor
434 ProtectionFactor(parameters);
niklase@google.com470e71d2011-07-07 08:21:25 +0000435
philipel9d3ab612015-12-21 04:12:39 -0800436 // Compute the effective packet loss
437 EffectivePacketLoss(parameters);
niklase@google.com470e71d2011-07-07 08:21:25 +0000438
philipel9d3ab612015-12-21 04:12:39 -0800439 // Protection/fec rates obtained above is defined relative to total number
440 // of packets (total rate: source+fec) FEC in RTP module assumes protection
441 // factor is defined relative to source number of packets so we should
442 // convert the factor to reduce mismatch between mediaOpt suggested rate and
443 // the actual rate
444 _protectionFactorK = ConvertFECRate(_protectionFactorK);
445 _protectionFactorD = ConvertFECRate(_protectionFactorD);
niklase@google.com470e71d2011-07-07 08:21:25 +0000446
philipel9d3ab612015-12-21 04:12:39 -0800447 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000448}
philipel9d3ab612015-12-21 04:12:39 -0800449VCMLossProtectionLogic::VCMLossProtectionLogic(int64_t nowMs)
450 : _currentParameters(),
451 _rtt(0),
452 _lossPr(0.0f),
453 _bitRate(0.0f),
454 _frameRate(0.0f),
455 _keyFrameSize(0.0f),
456 _fecRateKey(0),
457 _fecRateDelta(0),
458 _lastPrUpdateT(0),
459 _lossPr255(0.9999f),
460 _lossPrHistory(),
461 _shortMaxLossPr255(0),
462 _packetsPerFrame(0.9999f),
463 _packetsPerFrameKey(0.9999f),
464 _codecWidth(0),
465 _codecHeight(0),
466 _numLayers(1) {
467 Reset(nowMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468}
469
philipel9d3ab612015-12-21 04:12:39 -0800470VCMLossProtectionLogic::~VCMLossProtectionLogic() {
471 Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000472}
473
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000474void VCMLossProtectionLogic::SetMethod(
475 enum VCMProtectionMethodEnum newMethodType) {
pbosba8c15b2015-07-14 09:36:34 -0700476 if (_selectedMethod && _selectedMethod->Type() == newMethodType)
477 return;
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000478
philipel9d3ab612015-12-21 04:12:39 -0800479 switch (newMethodType) {
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000480 case kNack:
pbosba8c15b2015-07-14 09:36:34 -0700481 _selectedMethod.reset(new VCMNackMethod());
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000482 break;
483 case kFec:
pbosba8c15b2015-07-14 09:36:34 -0700484 _selectedMethod.reset(new VCMFecMethod());
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000485 break;
486 case kNackFec:
pbosba8c15b2015-07-14 09:36:34 -0700487 _selectedMethod.reset(new VCMNackFecMethod(kLowRttNackMs, -1));
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000488 break;
489 case kNone:
pbosba8c15b2015-07-14 09:36:34 -0700490 _selectedMethod.reset();
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000491 break;
492 }
493 UpdateMethod();
niklase@google.com470e71d2011-07-07 08:21:25 +0000494}
495
philipel9d3ab612015-12-21 04:12:39 -0800496void VCMLossProtectionLogic::UpdateRtt(int64_t rtt) {
497 _rtt = rtt;
niklase@google.com470e71d2011-07-07 08:21:25 +0000498}
499
philipel9d3ab612015-12-21 04:12:39 -0800500void VCMLossProtectionLogic::UpdateMaxLossHistory(uint8_t lossPr255,
501 int64_t now) {
502 if (_lossPrHistory[0].timeMs >= 0 &&
503 now - _lossPrHistory[0].timeMs < kLossPrShortFilterWinMs) {
504 if (lossPr255 > _shortMaxLossPr255) {
505 _shortMaxLossPr255 = lossPr255;
niklase@google.com470e71d2011-07-07 08:21:25 +0000506 }
philipel9d3ab612015-12-21 04:12:39 -0800507 } else {
508 // Only add a new value to the history once a second
509 if (_lossPrHistory[0].timeMs == -1) {
510 // First, no shift
511 _shortMaxLossPr255 = lossPr255;
512 } else {
513 // Shift
514 for (int32_t i = (kLossPrHistorySize - 2); i >= 0; i--) {
515 _lossPrHistory[i + 1].lossPr255 = _lossPrHistory[i].lossPr255;
516 _lossPrHistory[i + 1].timeMs = _lossPrHistory[i].timeMs;
517 }
518 }
519 if (_shortMaxLossPr255 == 0) {
520 _shortMaxLossPr255 = lossPr255;
521 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000522
philipel9d3ab612015-12-21 04:12:39 -0800523 _lossPrHistory[0].lossPr255 = _shortMaxLossPr255;
524 _lossPrHistory[0].timeMs = now;
525 _shortMaxLossPr255 = 0;
526 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000527}
528
philipel9d3ab612015-12-21 04:12:39 -0800529uint8_t VCMLossProtectionLogic::MaxFilteredLossPr(int64_t nowMs) const {
530 uint8_t maxFound = _shortMaxLossPr255;
531 if (_lossPrHistory[0].timeMs == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000532 return maxFound;
philipel9d3ab612015-12-21 04:12:39 -0800533 }
534 for (int32_t i = 0; i < kLossPrHistorySize; i++) {
535 if (_lossPrHistory[i].timeMs == -1) {
536 break;
537 }
538 if (nowMs - _lossPrHistory[i].timeMs >
539 kLossPrHistorySize * kLossPrShortFilterWinMs) {
540 // This sample (and all samples after this) is too old
541 break;
542 }
543 if (_lossPrHistory[i].lossPr255 > maxFound) {
544 // This sample is the largest one this far into the history
545 maxFound = _lossPrHistory[i].lossPr255;
546 }
547 }
548 return maxFound;
niklase@google.com470e71d2011-07-07 08:21:25 +0000549}
550
philipel9d3ab612015-12-21 04:12:39 -0800551uint8_t VCMLossProtectionLogic::FilteredLoss(int64_t nowMs,
552 FilterPacketLossMode filter_mode,
553 uint8_t lossPr255) {
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +0000554 // Update the max window filter.
555 UpdateMaxLossHistory(lossPr255, nowMs);
556
557 // Update the recursive average filter.
philipel9d3ab612015-12-21 04:12:39 -0800558 _lossPr255.Apply(static_cast<float>(nowMs - _lastPrUpdateT),
559 static_cast<float>(lossPr255));
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +0000560 _lastPrUpdateT = nowMs;
561
562 // Filtered loss: default is received loss (no filtering).
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000563 uint8_t filtered_loss = lossPr255;
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +0000564
565 switch (filter_mode) {
566 case kNoFilter:
567 break;
568 case kAvgFilter:
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000569 filtered_loss = static_cast<uint8_t>(_lossPr255.filtered() + 0.5);
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +0000570 break;
571 case kMaxFilter:
572 filtered_loss = MaxFilteredLossPr(nowMs);
573 break;
574 }
575
576 return filtered_loss;
niklase@google.com470e71d2011-07-07 08:21:25 +0000577}
578
philipel9d3ab612015-12-21 04:12:39 -0800579void VCMLossProtectionLogic::UpdateFilteredLossPr(uint8_t packetLossEnc) {
580 _lossPr = static_cast<float>(packetLossEnc) / 255.0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000581}
582
philipel9d3ab612015-12-21 04:12:39 -0800583void VCMLossProtectionLogic::UpdateBitRate(float bitRate) {
584 _bitRate = bitRate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000585}
586
philipel9d3ab612015-12-21 04:12:39 -0800587void VCMLossProtectionLogic::UpdatePacketsPerFrame(float nPackets,
588 int64_t nowMs) {
589 _packetsPerFrame.Apply(static_cast<float>(nowMs - _lastPacketPerFrameUpdateT),
590 nPackets);
591 _lastPacketPerFrameUpdateT = nowMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000592}
593
philipel9d3ab612015-12-21 04:12:39 -0800594void VCMLossProtectionLogic::UpdatePacketsPerFrameKey(float nPackets,
595 int64_t nowMs) {
596 _packetsPerFrameKey.Apply(
597 static_cast<float>(nowMs - _lastPacketPerFrameUpdateTKey), nPackets);
598 _lastPacketPerFrameUpdateTKey = nowMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000599}
600
philipel9d3ab612015-12-21 04:12:39 -0800601void VCMLossProtectionLogic::UpdateKeyFrameSize(float keyFrameSize) {
602 _keyFrameSize = keyFrameSize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000603}
604
perkjc2c24f72016-07-11 01:47:32 -0700605void VCMLossProtectionLogic::UpdateFrameSize(size_t width, size_t height) {
philipel9d3ab612015-12-21 04:12:39 -0800606 _codecWidth = width;
607 _codecHeight = height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000608}
609
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +0000610void VCMLossProtectionLogic::UpdateNumLayers(int numLayers) {
611 _numLayers = (numLayers == 0) ? 1 : numLayers;
612}
613
philipel9d3ab612015-12-21 04:12:39 -0800614bool VCMLossProtectionLogic::UpdateMethod() {
615 if (!_selectedMethod)
616 return false;
617 _currentParameters.rtt = _rtt;
618 _currentParameters.lossPr = _lossPr;
619 _currentParameters.bitRate = _bitRate;
620 _currentParameters.frameRate = _frameRate; // rename actual frame rate?
621 _currentParameters.keyFrameSize = _keyFrameSize;
622 _currentParameters.fecRateDelta = _fecRateDelta;
623 _currentParameters.fecRateKey = _fecRateKey;
624 _currentParameters.packetsPerFrame = _packetsPerFrame.filtered();
625 _currentParameters.packetsPerFrameKey = _packetsPerFrameKey.filtered();
626 _currentParameters.codecWidth = _codecWidth;
627 _currentParameters.codecHeight = _codecHeight;
628 _currentParameters.numLayers = _numLayers;
629 return _selectedMethod->UpdateParameters(&_currentParameters);
niklase@google.com470e71d2011-07-07 08:21:25 +0000630}
631
philipel9d3ab612015-12-21 04:12:39 -0800632VCMProtectionMethod* VCMLossProtectionLogic::SelectedMethod() const {
633 return _selectedMethod.get();
niklase@google.com470e71d2011-07-07 08:21:25 +0000634}
635
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000636VCMProtectionMethodEnum VCMLossProtectionLogic::SelectedType() const {
pbosba8c15b2015-07-14 09:36:34 -0700637 return _selectedMethod ? _selectedMethod->Type() : kNone;
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000638}
639
philipel9d3ab612015-12-21 04:12:39 -0800640void VCMLossProtectionLogic::Reset(int64_t nowMs) {
641 _lastPrUpdateT = nowMs;
642 _lastPacketPerFrameUpdateT = nowMs;
643 _lastPacketPerFrameUpdateTKey = nowMs;
644 _lossPr255.Reset(0.9999f);
645 _packetsPerFrame.Reset(0.9999f);
646 _fecRateDelta = _fecRateKey = 0;
647 for (int32_t i = 0; i < kLossPrHistorySize; i++) {
648 _lossPrHistory[i].lossPr255 = 0;
649 _lossPrHistory[i].timeMs = -1;
650 }
651 _shortMaxLossPr255 = 0;
652 Release();
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000653}
654
pbosba8c15b2015-07-14 09:36:34 -0700655void VCMLossProtectionLogic::Release() {
656 _selectedMethod.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000657}
658
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000659} // namespace media_optimization
660} // namespace webrtc