blob: 7580c95fc753a6a49699b884b1a491725454c036 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
stefan@webrtc.orgc35f5ce2012-04-11 07:42:252 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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 04:47:3111#include "modules/video_coding/media_opt_util.h"
niklase@google.com470e71d2011-07-07 08:21:2512
pbos@webrtc.orga4407322013-07-16 12:32:0513#include <math.h>
Jonas Olssona4d87372019-07-05 17:08:3314
philipel9d3ab612015-12-21 12:12:3915#include <algorithm>
16
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "modules/video_coding/fec_rate_table.h"
Yves Gerey3e707812018-11-28 15:47:4918#include "modules/video_coding/internal_defines.h"
Sergio Garcia Murillo43800f92018-06-21 14:16:3819#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Yves Gerey3e707812018-11-28 15:47:4920#include "rtc_base/checks.h"
Rasmus Brandt2b9317a2019-10-30 12:01:4621#include "rtc_base/experiments/rate_control_settings.h"
Emircan Uysaler704a7bd2018-08-06 23:14:1022#include "rtc_base/numerics/safe_conversions.h"
mikhal@webrtc.org0e7d9d82011-12-19 19:04:4923
niklase@google.com470e71d2011-07-07 08:21:2524namespace webrtc {
Peter Boström9cb1f302015-04-01 09:39:4925// Max value of loss rates in off-line model
26static const int kPacketLossMax = 129;
27
stefan@webrtc.orga64300a2013-03-04 15:24:4028namespace media_optimization {
niklase@google.com470e71d2011-07-07 08:21:2529
Stefan Holmerdbdb3a02018-07-17 14:03:4630VCMProtectionParameters::VCMProtectionParameters()
31 : rtt(0),
32 lossPr(0.0f),
33 bitRate(0.0f),
34 packetsPerFrame(0.0f),
35 packetsPerFrameKey(0.0f),
36 frameRate(0.0f),
37 keyFrameSize(0.0f),
38 fecRateDelta(0),
39 fecRateKey(0),
40 codecWidth(0),
41 codecHeight(0),
42 numLayers(1) {}
43
Peter Boström9cb1f302015-04-01 09:39:4944VCMProtectionMethod::VCMProtectionMethod()
45 : _effectivePacketLoss(0),
46 _protectionFactorK(0),
47 _protectionFactorD(0),
48 _scaleProtKey(2.0f),
49 _maxPayloadSize(1460),
Peter Boström9cb1f302015-04-01 09:39:4950 _corrFecCost(1.0),
philipel9d3ab612015-12-21 12:12:3951 _type(kNone) {}
mikhal@webrtc.orga057a952011-08-26 21:17:3452
pbosc0430522016-05-02 00:19:0553VCMProtectionMethod::~VCMProtectionMethod() {}
marpan@google.com86548c62011-07-12 17:12:5754
Stefan Holmerdbdb3a02018-07-17 14:03:4655enum VCMProtectionMethodEnum VCMProtectionMethod::Type() const {
56 return _type;
57}
58
59uint8_t VCMProtectionMethod::RequiredPacketLossER() {
60 return _effectivePacketLoss;
61}
62
63uint8_t VCMProtectionMethod::RequiredProtectionFactorK() {
64 return _protectionFactorK;
65}
66
67uint8_t VCMProtectionMethod::RequiredProtectionFactorD() {
68 return _protectionFactorD;
69}
70
71bool VCMProtectionMethod::RequiredUepProtectionK() {
72 return _useUepProtectionK;
73}
74
75bool VCMProtectionMethod::RequiredUepProtectionD() {
76 return _useUepProtectionD;
77}
78
79int VCMProtectionMethod::MaxFramesFec() const {
80 return 1;
81}
82
pkasting@chromium.org16825b12015-01-12 21:51:2183VCMNackFecMethod::VCMNackFecMethod(int64_t lowRttNackThresholdMs,
84 int64_t highRttNackThresholdMs)
stefan@webrtc.org932ab182011-11-29 11:33:3185 : VCMFecMethod(),
86 _lowRttNackMs(lowRttNackThresholdMs),
stefan@webrtc.orgc35f5ce2012-04-11 07:42:2587 _highRttNackMs(highRttNackThresholdMs),
88 _maxFramesFec(1) {
Mirko Bonadei25ab3222021-07-08 18:08:2089 RTC_DCHECK(lowRttNackThresholdMs >= -1 && highRttNackThresholdMs >= -1);
90 RTC_DCHECK(highRttNackThresholdMs == -1 ||
91 lowRttNackThresholdMs <= highRttNackThresholdMs);
92 RTC_DCHECK(lowRttNackThresholdMs > -1 || highRttNackThresholdMs == -1);
stefan@webrtc.org932ab182011-11-29 11:33:3193 _type = kNackFec;
mikhal@google.com77408882011-07-22 22:05:2594}
95
philipel9d3ab612015-12-21 12:12:3996VCMNackFecMethod::~VCMNackFecMethod() {
97 //
mikhal@google.com77408882011-07-22 22:05:2598}
philipel9d3ab612015-12-21 12:12:3999bool VCMNackFecMethod::ProtectionFactor(
100 const VCMProtectionParameters* parameters) {
101 // Hybrid Nack FEC has three operational modes:
102 // 1. Low RTT (below kLowRttNackMs) - Nack only: Set FEC rate
103 // (_protectionFactorD) to zero. -1 means no FEC.
104 // 2. High RTT (above _highRttNackMs) - FEC Only: Keep FEC factors.
105 // -1 means always allow NACK.
106 // 3. Medium RTT values - Hybrid mode: We will only nack the
107 // residual following the decoding of the FEC (refer to JB logic). FEC
108 // delta protection factor will be adjusted based on the RTT.
mikhal@google.com022716b2011-07-20 23:12:57109
philipel9d3ab612015-12-21 12:12:39110 // Otherwise: we count on FEC; if the RTT is below a threshold, then we
111 // nack the residual, based on a decision made in the JB.
mikhal@google.com022716b2011-07-20 23:12:57112
philipel9d3ab612015-12-21 12:12:39113 // Compute the protection factors
114 VCMFecMethod::ProtectionFactor(parameters);
115 if (_lowRttNackMs == -1 || parameters->rtt < _lowRttNackMs) {
116 _protectionFactorD = 0;
117 VCMFecMethod::UpdateProtectionFactorD(_protectionFactorD);
mikhal@google.com320813c2011-08-03 20:47:50118
mikhal@google.com679450f2011-08-01 22:14:58119 // When in Hybrid mode (RTT range), adjust FEC rates based on the
120 // RTT (NACK effectiveness) - adjustment factor is in the range [0,1].
philipel9d3ab612015-12-21 12:12:39121 } else if (_highRttNackMs == -1 || parameters->rtt < _highRttNackMs) {
122 // TODO(mikhal): Disabling adjustment temporarily.
123 // uint16_t rttIndex = (uint16_t) parameters->rtt;
124 float adjustRtt = 1.0f; // (float)VCMNackFecTable[rttIndex] / 100.0f;
niklase@google.com470e71d2011-07-07 08:21:25125
philipel9d3ab612015-12-21 12:12:39126 // Adjust FEC with NACK on (for delta frame only)
127 // table depends on RTT relative to rttMax (NACK Threshold)
Emircan Uysaler704a7bd2018-08-06 23:14:10128 _protectionFactorD = rtc::saturated_cast<uint8_t>(
129 adjustRtt * rtc::saturated_cast<float>(_protectionFactorD));
philipel9d3ab612015-12-21 12:12:39130 // update FEC rates after applying adjustment
131 VCMFecMethod::UpdateProtectionFactorD(_protectionFactorD);
132 }
mikhal@google.com679450f2011-08-01 22:14:58133
philipel9d3ab612015-12-21 12:12:39134 return true;
mikhal@google.com022716b2011-07-20 23:12:57135}
niklase@google.com470e71d2011-07-07 08:21:25136
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25137int VCMNackFecMethod::ComputeMaxFramesFec(
138 const VCMProtectionParameters* parameters) {
139 if (parameters->numLayers > 2) {
140 // For more than 2 temporal layers we will only have FEC on the base layer,
141 // and the base layers will be pretty far apart. Therefore we force one
142 // frame FEC.
143 return 1;
144 }
145 // We set the max number of frames to base the FEC on so that on average
146 // we will have complete frames in one RTT. Note that this is an upper
147 // bound, and that the actual number of frames used for FEC is decided by the
148 // RTP module based on the actual number of packets and the protection factor.
philipel9d3ab612015-12-21 12:12:39149 float base_layer_framerate =
150 parameters->frameRate /
Emircan Uysaler704a7bd2018-08-06 23:14:10151 rtc::saturated_cast<float>(1 << (parameters->numLayers - 1));
philipel9d3ab612015-12-21 12:12:39152 int max_frames_fec = std::max(
Emircan Uysaler704a7bd2018-08-06 23:14:10153 rtc::saturated_cast<int>(
154 2.0f * base_layer_framerate * parameters->rtt / 1000.0f + 0.5f),
philipel9d3ab612015-12-21 12:12:39155 1);
Artem Titovdcd7fc72021-08-09 11:02:57156 // `kUpperLimitFramesFec` is the upper limit on how many frames we
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25157 // allow any FEC to be based on.
158 if (max_frames_fec > kUpperLimitFramesFec) {
159 max_frames_fec = kUpperLimitFramesFec;
160 }
161 return max_frames_fec;
162}
163
164int VCMNackFecMethod::MaxFramesFec() const {
165 return _maxFramesFec;
166}
167
marpan@webrtc.org88ad06b2012-04-20 16:05:24168bool VCMNackFecMethod::BitRateTooLowForFec(
169 const VCMProtectionParameters* parameters) {
170 // Bitrate below which we turn off FEC, regardless of reported packet loss.
171 // The condition should depend on resolution and content. For now, use
172 // threshold on bytes per frame, with some effect for the frame size.
173 // The condition for turning off FEC is also based on other factors,
Artem Titovdcd7fc72021-08-09 11:02:57174 // such as `_numLayers`, `_maxFramesFec`, and `_rtt`.
marpan@webrtc.org88ad06b2012-04-20 16:05:24175 int estimate_bytes_per_frame = 1000 * BitsPerFrame(parameters) / 8;
176 int max_bytes_per_frame = kMaxBytesPerFrameForFec;
177 int num_pixels = parameters->codecWidth * parameters->codecHeight;
178 if (num_pixels <= 352 * 288) {
179 max_bytes_per_frame = kMaxBytesPerFrameForFecLow;
180 } else if (num_pixels > 640 * 480) {
181 max_bytes_per_frame = kMaxBytesPerFrameForFecHigh;
182 }
philipel9d3ab612015-12-21 12:12:39183 // TODO(marpan): add condition based on maximum frames used for FEC,
marpan@webrtc.org88ad06b2012-04-20 16:05:24184 // and expand condition based on frame size.
pkasting@chromium.org16825b12015-01-12 21:51:21185 // Max round trip time threshold in ms.
186 const int64_t kMaxRttTurnOffFec = 200;
marpan@webrtc.org88ad06b2012-04-20 16:05:24187 if (estimate_bytes_per_frame < max_bytes_per_frame &&
philipel9d3ab612015-12-21 12:12:39188 parameters->numLayers < 3 && parameters->rtt < kMaxRttTurnOffFec) {
marpan@webrtc.org88ad06b2012-04-20 16:05:24189 return true;
190 }
191 return false;
192}
193
philipel9d3ab612015-12-21 12:12:39194bool VCMNackFecMethod::EffectivePacketLoss(
195 const VCMProtectionParameters* parameters) {
196 // Set the effective packet loss for encoder (based on FEC code).
197 // Compute the effective packet loss and residual packet loss due to FEC.
198 VCMFecMethod::EffectivePacketLoss(parameters);
199 return true;
mikhal@google.com022716b2011-07-20 23:12:57200}
niklase@google.com470e71d2011-07-07 08:21:25201
philipel9d3ab612015-12-21 12:12:39202bool VCMNackFecMethod::UpdateParameters(
203 const VCMProtectionParameters* parameters) {
204 ProtectionFactor(parameters);
205 EffectivePacketLoss(parameters);
206 _maxFramesFec = ComputeMaxFramesFec(parameters);
207 if (BitRateTooLowForFec(parameters)) {
208 _protectionFactorK = 0;
209 _protectionFactorD = 0;
210 }
niklase@google.com470e71d2011-07-07 08:21:25211
philipel9d3ab612015-12-21 12:12:39212 // Protection/fec rates obtained above are defined relative to total number
213 // of packets (total rate: source + fec) FEC in RTP module assumes
214 // protection factor is defined relative to source number of packets so we
215 // should convert the factor to reduce mismatch between mediaOpt's rate and
216 // the actual one
217 _protectionFactorK = VCMFecMethod::ConvertFECRate(_protectionFactorK);
218 _protectionFactorD = VCMFecMethod::ConvertFECRate(_protectionFactorD);
niklase@google.com470e71d2011-07-07 08:21:25219
philipel9d3ab612015-12-21 12:12:39220 return true;
niklase@google.com470e71d2011-07-07 08:21:25221}
222
philipel9d3ab612015-12-21 12:12:39223VCMNackMethod::VCMNackMethod() : VCMProtectionMethod() {
224 _type = kNack;
mikhal@webrtc.orga057a952011-08-26 21:17:34225}
226
philipel9d3ab612015-12-21 12:12:39227VCMNackMethod::~VCMNackMethod() {
228 //
mikhal@webrtc.orga057a952011-08-26 21:17:34229}
230
philipel9d3ab612015-12-21 12:12:39231bool VCMNackMethod::EffectivePacketLoss(
232 const VCMProtectionParameters* parameter) {
233 // Effective Packet Loss, NA in current version.
234 _effectivePacketLoss = 0;
235 return true;
niklase@google.com470e71d2011-07-07 08:21:25236}
237
philipel9d3ab612015-12-21 12:12:39238bool VCMNackMethod::UpdateParameters(
239 const VCMProtectionParameters* parameters) {
240 // Compute the effective packet loss
241 EffectivePacketLoss(parameters);
niklase@google.com470e71d2011-07-07 08:21:25242
philipel9d3ab612015-12-21 12:12:39243 // nackCost = (bitRate - nackCost) * (lossPr)
244 return true;
niklase@google.com470e71d2011-07-07 08:21:25245}
246
Rasmus Brandt2b9317a2019-10-30 12:01:46247VCMFecMethod::VCMFecMethod()
248 : VCMProtectionMethod(),
249 rate_control_settings_(RateControlSettings::ParseFromFieldTrials()) {
philipel9d3ab612015-12-21 12:12:39250 _type = kFec;
mikhal@webrtc.orga057a952011-08-26 21:17:34251}
Rasmus Brandt2b9317a2019-10-30 12:01:46252
253VCMFecMethod::~VCMFecMethod() = default;
mikhal@webrtc.orga057a952011-08-26 21:17:34254
philipel9d3ab612015-12-21 12:12:39255uint8_t VCMFecMethod::BoostCodeRateKey(uint8_t packetFrameDelta,
256 uint8_t packetFrameKey) const {
257 uint8_t boostRateKey = 2;
258 // Default: ratio scales the FEC protection up for I frames
259 uint8_t ratio = 1;
niklase@google.com470e71d2011-07-07 08:21:25260
philipel9d3ab612015-12-21 12:12:39261 if (packetFrameDelta > 0) {
262 ratio = (int8_t)(packetFrameKey / packetFrameDelta);
263 }
264 ratio = VCM_MAX(boostRateKey, ratio);
niklase@google.com470e71d2011-07-07 08:21:25265
philipel9d3ab612015-12-21 12:12:39266 return ratio;
niklase@google.com470e71d2011-07-07 08:21:25267}
268
philipel9d3ab612015-12-21 12:12:39269uint8_t VCMFecMethod::ConvertFECRate(uint8_t codeRateRTP) const {
Emircan Uysaler704a7bd2018-08-06 23:14:10270 return rtc::saturated_cast<uint8_t>(
271 VCM_MIN(255, (0.5 + 255.0 * codeRateRTP /
272 rtc::saturated_cast<float>(255 - codeRateRTP))));
niklase@google.com470e71d2011-07-07 08:21:25273}
274
mikhal@google.com679450f2011-08-01 22:14:58275// Update FEC with protectionFactorD
philipel9d3ab612015-12-21 12:12:39276void VCMFecMethod::UpdateProtectionFactorD(uint8_t protectionFactorD) {
277 _protectionFactorD = protectionFactorD;
mikhal@google.com679450f2011-08-01 22:14:58278}
279
mikhal@webrtc.orgd0752c32011-10-19 15:48:30280// Update FEC with protectionFactorK
philipel9d3ab612015-12-21 12:12:39281void VCMFecMethod::UpdateProtectionFactorK(uint8_t protectionFactorK) {
282 _protectionFactorK = protectionFactorK;
mikhal@webrtc.orgd0752c32011-10-19 15:48:30283}
284
philipel9d3ab612015-12-21 12:12:39285bool VCMFecMethod::ProtectionFactor(const VCMProtectionParameters* parameters) {
286 // FEC PROTECTION SETTINGS: varies with packet loss and bitrate
niklase@google.com470e71d2011-07-07 08:21:25287
philipel9d3ab612015-12-21 12:12:39288 // No protection if (filtered) packetLoss is 0
Emircan Uysaler704a7bd2018-08-06 23:14:10289 uint8_t packetLoss = rtc::saturated_cast<uint8_t>(255 * parameters->lossPr);
philipel9d3ab612015-12-21 12:12:39290 if (packetLoss == 0) {
291 _protectionFactorK = 0;
292 _protectionFactorD = 0;
niklase@google.com470e71d2011-07-07 08:21:25293 return true;
philipel9d3ab612015-12-21 12:12:39294 }
295
296 // Parameters for FEC setting:
297 // first partition size, thresholds, table pars, spatial resoln fac.
298
299 // First partition protection: ~ 20%
Emircan Uysaler704a7bd2018-08-06 23:14:10300 uint8_t firstPartitionProt = rtc::saturated_cast<uint8_t>(255 * 0.20);
philipel9d3ab612015-12-21 12:12:39301
302 // Minimum protection level needed to generate one FEC packet for one
303 // source packet/frame (in RTP sender)
304 uint8_t minProtLevelFec = 85;
305
306 // Threshold on packetLoss and bitRrate/frameRate (=average #packets),
307 // above which we allocate protection to cover at least first partition.
308 uint8_t lossThr = 0;
309 uint8_t packetNumThr = 1;
310
311 // Parameters for range of rate index of table.
312 const uint8_t ratePar1 = 5;
313 const uint8_t ratePar2 = 49;
314
315 // Spatial resolution size, relative to a reference size.
Emircan Uysaler704a7bd2018-08-06 23:14:10316 float spatialSizeToRef = rtc::saturated_cast<float>(parameters->codecWidth *
317 parameters->codecHeight) /
318 (rtc::saturated_cast<float>(704 * 576));
philipel9d3ab612015-12-21 12:12:39319 // resolnFac: This parameter will generally increase/decrease the FEC rate
320 // (for fixed bitRate and packetLoss) based on system size.
321 // Use a smaller exponent (< 1) to control/soften system size effect.
322 const float resolnFac = 1.0 / powf(spatialSizeToRef, 0.3f);
323
324 const int bitRatePerFrame = BitsPerFrame(parameters);
325
326 // Average number of packets per frame (source and fec):
Emircan Uysaler704a7bd2018-08-06 23:14:10327 const uint8_t avgTotPackets = rtc::saturated_cast<uint8_t>(
328 1.5f + rtc::saturated_cast<float>(bitRatePerFrame) * 1000.0f /
329 rtc::saturated_cast<float>(8.0 * _maxPayloadSize));
philipel9d3ab612015-12-21 12:12:39330
331 // FEC rate parameters: for P and I frame
332 uint8_t codeRateDelta = 0;
333 uint8_t codeRateKey = 0;
334
335 // Get index for table: the FEC protection depends on an effective rate.
336 // The range on the rate index corresponds to rates (bps)
337 // from ~200k to ~8000k, for 30fps
338 const uint16_t effRateFecTable =
Emircan Uysaler704a7bd2018-08-06 23:14:10339 rtc::saturated_cast<uint16_t>(resolnFac * bitRatePerFrame);
340 uint8_t rateIndexTable = rtc::saturated_cast<uint8_t>(
brandtr05f845d2016-11-17 06:59:39341 VCM_MAX(VCM_MIN((effRateFecTable - ratePar1) / ratePar1, ratePar2), 0));
philipel9d3ab612015-12-21 12:12:39342
343 // Restrict packet loss range to 50:
344 // current tables defined only up to 50%
345 if (packetLoss >= kPacketLossMax) {
346 packetLoss = kPacketLossMax - 1;
347 }
348 uint16_t indexTable = rateIndexTable * kPacketLossMax + packetLoss;
349
350 // Check on table index
brandtr71b1c1f2017-01-12 14:16:24351 RTC_DCHECK_LT(indexTable, kFecRateTableSize);
philipel9d3ab612015-12-21 12:12:39352
353 // Protection factor for P frame
brandtr71b1c1f2017-01-12 14:16:24354 codeRateDelta = kFecRateTable[indexTable];
philipel9d3ab612015-12-21 12:12:39355
356 if (packetLoss > lossThr && avgTotPackets > packetNumThr) {
357 // Set a minimum based on first partition size.
358 if (codeRateDelta < firstPartitionProt) {
359 codeRateDelta = firstPartitionProt;
360 }
361 }
362
363 // Check limit on amount of protection for P frame; 50% is max.
364 if (codeRateDelta >= kPacketLossMax) {
365 codeRateDelta = kPacketLossMax - 1;
366 }
367
philipel9d3ab612015-12-21 12:12:39368 // For Key frame:
369 // Effectively at a higher rate, so we scale/boost the rate
370 // The boost factor may depend on several factors: ratio of packet
371 // number of I to P frames, how much protection placed on P frames, etc.
brandtr05f845d2016-11-17 06:59:39372 const uint8_t packetFrameDelta =
Emircan Uysaler704a7bd2018-08-06 23:14:10373 rtc::saturated_cast<uint8_t>(0.5 + parameters->packetsPerFrame);
philipel9d3ab612015-12-21 12:12:39374 const uint8_t packetFrameKey =
Emircan Uysaler704a7bd2018-08-06 23:14:10375 rtc::saturated_cast<uint8_t>(0.5 + parameters->packetsPerFrameKey);
philipel9d3ab612015-12-21 12:12:39376 const uint8_t boostKey = BoostCodeRateKey(packetFrameDelta, packetFrameKey);
377
Emircan Uysaler704a7bd2018-08-06 23:14:10378 rateIndexTable = rtc::saturated_cast<uint8_t>(VCM_MAX(
philipel9d3ab612015-12-21 12:12:39379 VCM_MIN(1 + (boostKey * effRateFecTable - ratePar1) / ratePar1, ratePar2),
brandtr05f845d2016-11-17 06:59:39380 0));
philipel9d3ab612015-12-21 12:12:39381 uint16_t indexTableKey = rateIndexTable * kPacketLossMax + packetLoss;
382
brandtr71b1c1f2017-01-12 14:16:24383 indexTableKey = VCM_MIN(indexTableKey, kFecRateTableSize);
philipel9d3ab612015-12-21 12:12:39384
385 // Check on table index
Mirko Bonadei25ab3222021-07-08 18:08:20386 RTC_DCHECK_LT(indexTableKey, kFecRateTableSize);
philipel9d3ab612015-12-21 12:12:39387
388 // Protection factor for I frame
brandtr71b1c1f2017-01-12 14:16:24389 codeRateKey = kFecRateTable[indexTableKey];
philipel9d3ab612015-12-21 12:12:39390
391 // Boosting for Key frame.
392 int boostKeyProt = _scaleProtKey * codeRateDelta;
393 if (boostKeyProt >= kPacketLossMax) {
394 boostKeyProt = kPacketLossMax - 1;
395 }
396
397 // Make sure I frame protection is at least larger than P frame protection,
398 // and at least as high as filtered packet loss.
Emircan Uysaler704a7bd2018-08-06 23:14:10399 codeRateKey = rtc::saturated_cast<uint8_t>(
philipel9d3ab612015-12-21 12:12:39400 VCM_MAX(packetLoss, VCM_MAX(boostKeyProt, codeRateKey)));
401
402 // Check limit on amount of protection for I frame: 50% is max.
403 if (codeRateKey >= kPacketLossMax) {
404 codeRateKey = kPacketLossMax - 1;
405 }
406
407 _protectionFactorK = codeRateKey;
408 _protectionFactorD = codeRateDelta;
409
410 // Generally there is a rate mis-match between the FEC cost estimated
411 // in mediaOpt and the actual FEC cost sent out in RTP module.
412 // This is more significant at low rates (small # of source packets), where
413 // the granularity of the FEC decreases. In this case, non-zero protection
414 // in mediaOpt may generate 0 FEC packets in RTP sender (since actual #FEC
415 // is based on rounding off protectionFactor on actual source packet number).
416 // The correction factor (_corrFecCost) attempts to corrects this, at least
417 // for cases of low rates (small #packets) and low protection levels.
418
Emircan Uysaler704a7bd2018-08-06 23:14:10419 float numPacketsFl =
420 1.0f + (rtc::saturated_cast<float>(bitRatePerFrame) * 1000.0 /
421 rtc::saturated_cast<float>(8.0 * _maxPayloadSize) +
422 0.5);
philipel9d3ab612015-12-21 12:12:39423
424 const float estNumFecGen =
Emircan Uysaler704a7bd2018-08-06 23:14:10425 0.5f +
426 rtc::saturated_cast<float>(_protectionFactorD * numPacketsFl / 255.0f);
philipel9d3ab612015-12-21 12:12:39427
428 // We reduce cost factor (which will reduce overhead for FEC and
429 // hybrid method) and not the protectionFactor.
430 _corrFecCost = 1.0f;
431 if (estNumFecGen < 1.1f && _protectionFactorD < minProtLevelFec) {
432 _corrFecCost = 0.5f;
433 }
434 if (estNumFecGen < 0.9f && _protectionFactorD < minProtLevelFec) {
435 _corrFecCost = 0.0f;
436 }
437
philipel9d3ab612015-12-21 12:12:39438 // DONE WITH FEC PROTECTION SETTINGS
439 return true;
niklase@google.com470e71d2011-07-07 08:21:25440}
441
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49442int VCMFecMethod::BitsPerFrame(const VCMProtectionParameters* parameters) {
443 // When temporal layers are available FEC will only be applied on the base
444 // layer.
Erik Språngd92288f2018-07-04 08:07:40445 const float bitRateRatio =
446 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
Rasmus Brandt2b9317a2019-10-30 12:01:46447 parameters->numLayers, 0,
448 rate_control_settings_.Vp8BaseHeavyTl3RateAllocation());
thakis@chromium.org65bc2542012-08-13 19:26:12449 float frameRateRatio = powf(1 / 2.0, parameters->numLayers - 1);
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49450 float bitRate = parameters->bitRate * bitRateRatio;
451 float frameRate = parameters->frameRate * frameRateRatio;
452
453 // TODO(mikhal): Update factor following testing.
454 float adjustmentFactor = 1;
455
Peter Boström5e8351b2016-01-28 22:55:29456 if (frameRate < 1.0f)
457 frameRate = 1.0f;
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49458 // Average bits per frame (units of kbits)
Emircan Uysaler704a7bd2018-08-06 23:14:10459 return rtc::saturated_cast<int>(adjustmentFactor * bitRate / frameRate);
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49460}
461
philipel9d3ab612015-12-21 12:12:39462bool VCMFecMethod::EffectivePacketLoss(
463 const VCMProtectionParameters* parameters) {
464 // Effective packet loss to encoder is based on RPL (residual packet loss)
465 // this is a soft setting based on degree of FEC protection
466 // RPL = received/input packet loss - average_FEC_recovery
467 // note: received/input packet loss may be filtered based on FilteredLoss
niklase@google.com470e71d2011-07-07 08:21:25468
philipel9d3ab612015-12-21 12:12:39469 // Effective Packet Loss, NA in current version.
470 _effectivePacketLoss = 0;
niklase@google.com470e71d2011-07-07 08:21:25471
philipel9d3ab612015-12-21 12:12:39472 return true;
niklase@google.com470e71d2011-07-07 08:21:25473}
474
philipel9d3ab612015-12-21 12:12:39475bool VCMFecMethod::UpdateParameters(const VCMProtectionParameters* parameters) {
476 // Compute the protection factor
477 ProtectionFactor(parameters);
niklase@google.com470e71d2011-07-07 08:21:25478
philipel9d3ab612015-12-21 12:12:39479 // Compute the effective packet loss
480 EffectivePacketLoss(parameters);
niklase@google.com470e71d2011-07-07 08:21:25481
philipel9d3ab612015-12-21 12:12:39482 // Protection/fec rates obtained above is defined relative to total number
483 // of packets (total rate: source+fec) FEC in RTP module assumes protection
484 // factor is defined relative to source number of packets so we should
485 // convert the factor to reduce mismatch between mediaOpt suggested rate and
486 // the actual rate
487 _protectionFactorK = ConvertFECRate(_protectionFactorK);
488 _protectionFactorD = ConvertFECRate(_protectionFactorD);
niklase@google.com470e71d2011-07-07 08:21:25489
philipel9d3ab612015-12-21 12:12:39490 return true;
niklase@google.com470e71d2011-07-07 08:21:25491}
philipel9d3ab612015-12-21 12:12:39492VCMLossProtectionLogic::VCMLossProtectionLogic(int64_t nowMs)
493 : _currentParameters(),
494 _rtt(0),
495 _lossPr(0.0f),
496 _bitRate(0.0f),
497 _frameRate(0.0f),
498 _keyFrameSize(0.0f),
499 _fecRateKey(0),
500 _fecRateDelta(0),
501 _lastPrUpdateT(0),
502 _lossPr255(0.9999f),
503 _lossPrHistory(),
504 _shortMaxLossPr255(0),
505 _packetsPerFrame(0.9999f),
506 _packetsPerFrameKey(0.9999f),
Ying Wang1f262cc2018-08-23 11:54:08507 _codecWidth(704),
508 _codecHeight(576),
philipel9d3ab612015-12-21 12:12:39509 _numLayers(1) {
510 Reset(nowMs);
niklase@google.com470e71d2011-07-07 08:21:25511}
512
philipel9d3ab612015-12-21 12:12:39513VCMLossProtectionLogic::~VCMLossProtectionLogic() {
514 Release();
niklase@google.com470e71d2011-07-07 08:21:25515}
516
pbos@webrtc.orgcade82c2015-03-12 10:39:24517void VCMLossProtectionLogic::SetMethod(
518 enum VCMProtectionMethodEnum newMethodType) {
pbosba8c15b2015-07-14 16:36:34519 if (_selectedMethod && _selectedMethod->Type() == newMethodType)
520 return;
mikhal@webrtc.orga057a952011-08-26 21:17:34521
philipel9d3ab612015-12-21 12:12:39522 switch (newMethodType) {
pbos@webrtc.orgcade82c2015-03-12 10:39:24523 case kNack:
pbosba8c15b2015-07-14 16:36:34524 _selectedMethod.reset(new VCMNackMethod());
pbos@webrtc.orgcade82c2015-03-12 10:39:24525 break;
526 case kFec:
pbosba8c15b2015-07-14 16:36:34527 _selectedMethod.reset(new VCMFecMethod());
pbos@webrtc.orgcade82c2015-03-12 10:39:24528 break;
529 case kNackFec:
pbosba8c15b2015-07-14 16:36:34530 _selectedMethod.reset(new VCMNackFecMethod(kLowRttNackMs, -1));
pbos@webrtc.orgcade82c2015-03-12 10:39:24531 break;
532 case kNone:
pbosba8c15b2015-07-14 16:36:34533 _selectedMethod.reset();
pbos@webrtc.orgcade82c2015-03-12 10:39:24534 break;
535 }
536 UpdateMethod();
niklase@google.com470e71d2011-07-07 08:21:25537}
538
philipel9d3ab612015-12-21 12:12:39539void VCMLossProtectionLogic::UpdateRtt(int64_t rtt) {
540 _rtt = rtt;
niklase@google.com470e71d2011-07-07 08:21:25541}
542
philipel9d3ab612015-12-21 12:12:39543void VCMLossProtectionLogic::UpdateMaxLossHistory(uint8_t lossPr255,
544 int64_t now) {
545 if (_lossPrHistory[0].timeMs >= 0 &&
546 now - _lossPrHistory[0].timeMs < kLossPrShortFilterWinMs) {
547 if (lossPr255 > _shortMaxLossPr255) {
548 _shortMaxLossPr255 = lossPr255;
niklase@google.com470e71d2011-07-07 08:21:25549 }
philipel9d3ab612015-12-21 12:12:39550 } else {
551 // Only add a new value to the history once a second
552 if (_lossPrHistory[0].timeMs == -1) {
553 // First, no shift
554 _shortMaxLossPr255 = lossPr255;
555 } else {
556 // Shift
557 for (int32_t i = (kLossPrHistorySize - 2); i >= 0; i--) {
558 _lossPrHistory[i + 1].lossPr255 = _lossPrHistory[i].lossPr255;
559 _lossPrHistory[i + 1].timeMs = _lossPrHistory[i].timeMs;
560 }
561 }
562 if (_shortMaxLossPr255 == 0) {
563 _shortMaxLossPr255 = lossPr255;
564 }
niklase@google.com470e71d2011-07-07 08:21:25565
philipel9d3ab612015-12-21 12:12:39566 _lossPrHistory[0].lossPr255 = _shortMaxLossPr255;
567 _lossPrHistory[0].timeMs = now;
568 _shortMaxLossPr255 = 0;
569 }
niklase@google.com470e71d2011-07-07 08:21:25570}
571
philipel9d3ab612015-12-21 12:12:39572uint8_t VCMLossProtectionLogic::MaxFilteredLossPr(int64_t nowMs) const {
573 uint8_t maxFound = _shortMaxLossPr255;
574 if (_lossPrHistory[0].timeMs == -1) {
niklase@google.com470e71d2011-07-07 08:21:25575 return maxFound;
philipel9d3ab612015-12-21 12:12:39576 }
577 for (int32_t i = 0; i < kLossPrHistorySize; i++) {
578 if (_lossPrHistory[i].timeMs == -1) {
579 break;
580 }
581 if (nowMs - _lossPrHistory[i].timeMs >
582 kLossPrHistorySize * kLossPrShortFilterWinMs) {
583 // This sample (and all samples after this) is too old
584 break;
585 }
586 if (_lossPrHistory[i].lossPr255 > maxFound) {
587 // This sample is the largest one this far into the history
588 maxFound = _lossPrHistory[i].lossPr255;
589 }
590 }
591 return maxFound;
niklase@google.com470e71d2011-07-07 08:21:25592}
593
philipel9d3ab612015-12-21 12:12:39594uint8_t VCMLossProtectionLogic::FilteredLoss(int64_t nowMs,
595 FilterPacketLossMode filter_mode,
596 uint8_t lossPr255) {
marpan@webrtc.org2dad3fb2012-01-09 18:18:36597 // Update the max window filter.
598 UpdateMaxLossHistory(lossPr255, nowMs);
599
600 // Update the recursive average filter.
Emircan Uysaler704a7bd2018-08-06 23:14:10601 _lossPr255.Apply(rtc::saturated_cast<float>(nowMs - _lastPrUpdateT),
602 rtc::saturated_cast<float>(lossPr255));
marpan@webrtc.org2dad3fb2012-01-09 18:18:36603 _lastPrUpdateT = nowMs;
604
605 // Filtered loss: default is received loss (no filtering).
pbos@webrtc.org7b859cc2013-04-02 15:54:38606 uint8_t filtered_loss = lossPr255;
marpan@webrtc.org2dad3fb2012-01-09 18:18:36607
608 switch (filter_mode) {
609 case kNoFilter:
610 break;
611 case kAvgFilter:
Emircan Uysaler704a7bd2018-08-06 23:14:10612 filtered_loss = rtc::saturated_cast<uint8_t>(_lossPr255.filtered() + 0.5);
marpan@webrtc.org2dad3fb2012-01-09 18:18:36613 break;
614 case kMaxFilter:
615 filtered_loss = MaxFilteredLossPr(nowMs);
616 break;
617 }
618
619 return filtered_loss;
niklase@google.com470e71d2011-07-07 08:21:25620}
621
philipel9d3ab612015-12-21 12:12:39622void VCMLossProtectionLogic::UpdateFilteredLossPr(uint8_t packetLossEnc) {
Emircan Uysaler704a7bd2018-08-06 23:14:10623 _lossPr = rtc::saturated_cast<float>(packetLossEnc) / 255.0;
niklase@google.com470e71d2011-07-07 08:21:25624}
625
philipel9d3ab612015-12-21 12:12:39626void VCMLossProtectionLogic::UpdateBitRate(float bitRate) {
627 _bitRate = bitRate;
niklase@google.com470e71d2011-07-07 08:21:25628}
629
philipel9d3ab612015-12-21 12:12:39630void VCMLossProtectionLogic::UpdatePacketsPerFrame(float nPackets,
631 int64_t nowMs) {
Emircan Uysaler704a7bd2018-08-06 23:14:10632 _packetsPerFrame.Apply(
633 rtc::saturated_cast<float>(nowMs - _lastPacketPerFrameUpdateT), nPackets);
philipel9d3ab612015-12-21 12:12:39634 _lastPacketPerFrameUpdateT = nowMs;
niklase@google.com470e71d2011-07-07 08:21:25635}
636
philipel9d3ab612015-12-21 12:12:39637void VCMLossProtectionLogic::UpdatePacketsPerFrameKey(float nPackets,
638 int64_t nowMs) {
639 _packetsPerFrameKey.Apply(
Emircan Uysaler704a7bd2018-08-06 23:14:10640 rtc::saturated_cast<float>(nowMs - _lastPacketPerFrameUpdateTKey),
641 nPackets);
philipel9d3ab612015-12-21 12:12:39642 _lastPacketPerFrameUpdateTKey = nowMs;
niklase@google.com470e71d2011-07-07 08:21:25643}
644
philipel9d3ab612015-12-21 12:12:39645void VCMLossProtectionLogic::UpdateKeyFrameSize(float keyFrameSize) {
646 _keyFrameSize = keyFrameSize;
niklase@google.com470e71d2011-07-07 08:21:25647}
648
perkjc2c24f72016-07-11 08:47:32649void VCMLossProtectionLogic::UpdateFrameSize(size_t width, size_t height) {
philipel9d3ab612015-12-21 12:12:39650 _codecWidth = width;
651 _codecHeight = height;
niklase@google.com470e71d2011-07-07 08:21:25652}
653
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49654void VCMLossProtectionLogic::UpdateNumLayers(int numLayers) {
655 _numLayers = (numLayers == 0) ? 1 : numLayers;
656}
657
philipel9d3ab612015-12-21 12:12:39658bool VCMLossProtectionLogic::UpdateMethod() {
659 if (!_selectedMethod)
660 return false;
661 _currentParameters.rtt = _rtt;
662 _currentParameters.lossPr = _lossPr;
663 _currentParameters.bitRate = _bitRate;
664 _currentParameters.frameRate = _frameRate; // rename actual frame rate?
665 _currentParameters.keyFrameSize = _keyFrameSize;
666 _currentParameters.fecRateDelta = _fecRateDelta;
667 _currentParameters.fecRateKey = _fecRateKey;
668 _currentParameters.packetsPerFrame = _packetsPerFrame.filtered();
669 _currentParameters.packetsPerFrameKey = _packetsPerFrameKey.filtered();
670 _currentParameters.codecWidth = _codecWidth;
671 _currentParameters.codecHeight = _codecHeight;
672 _currentParameters.numLayers = _numLayers;
673 return _selectedMethod->UpdateParameters(&_currentParameters);
niklase@google.com470e71d2011-07-07 08:21:25674}
675
philipel9d3ab612015-12-21 12:12:39676VCMProtectionMethod* VCMLossProtectionLogic::SelectedMethod() const {
677 return _selectedMethod.get();
niklase@google.com470e71d2011-07-07 08:21:25678}
679
pbos@webrtc.orgcade82c2015-03-12 10:39:24680VCMProtectionMethodEnum VCMLossProtectionLogic::SelectedType() const {
pbosba8c15b2015-07-14 16:36:34681 return _selectedMethod ? _selectedMethod->Type() : kNone;
mikhal@webrtc.orga057a952011-08-26 21:17:34682}
683
philipel9d3ab612015-12-21 12:12:39684void VCMLossProtectionLogic::Reset(int64_t nowMs) {
685 _lastPrUpdateT = nowMs;
686 _lastPacketPerFrameUpdateT = nowMs;
687 _lastPacketPerFrameUpdateTKey = nowMs;
688 _lossPr255.Reset(0.9999f);
689 _packetsPerFrame.Reset(0.9999f);
690 _fecRateDelta = _fecRateKey = 0;
691 for (int32_t i = 0; i < kLossPrHistorySize; i++) {
692 _lossPrHistory[i].lossPr255 = 0;
693 _lossPrHistory[i].timeMs = -1;
694 }
695 _shortMaxLossPr255 = 0;
696 Release();
mikhal@webrtc.orga057a952011-08-26 21:17:34697}
698
pbosba8c15b2015-07-14 16:36:34699void VCMLossProtectionLogic::Release() {
700 _selectedMethod.reset();
niklase@google.com470e71d2011-07-07 08:21:25701}
702
stefan@webrtc.orga64300a2013-03-04 15:24:40703} // namespace media_optimization
704} // namespace webrtc