blob: a46417b3aeb6c3cb926a8f249dd34e599abf06ca [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/media_opt_util.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <assert.h>
pbos@webrtc.orga4407322013-07-16 12:32:05 +000014#include <math.h>
philipel9d3ab612015-12-21 04:12:39 -080015#include <algorithm>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/video_coding/fec_rate_table.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "modules/video_coding/internal_defines.h"
Sergio Garcia Murillo43800f92018-06-21 16:16:38 +020019#include "modules/video_coding/utility/simulcast_rate_allocator.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/checks.h"
Emircan Uysaler704a7bd2018-08-06 16:14:10 -070021#include "rtc_base/numerics/safe_conversions.h"
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +000022
niklase@google.com470e71d2011-07-07 08:21:25 +000023namespace webrtc {
Peter Boström9cb1f302015-04-01 11:39:49 +020024// Max value of loss rates in off-line model
25static const int kPacketLossMax = 129;
26
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000027namespace media_optimization {
niklase@google.com470e71d2011-07-07 08:21:25 +000028
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020029VCMProtectionParameters::VCMProtectionParameters()
30 : rtt(0),
31 lossPr(0.0f),
32 bitRate(0.0f),
33 packetsPerFrame(0.0f),
34 packetsPerFrameKey(0.0f),
35 frameRate(0.0f),
36 keyFrameSize(0.0f),
37 fecRateDelta(0),
38 fecRateKey(0),
39 codecWidth(0),
40 codecHeight(0),
41 numLayers(1) {}
42
Peter Boström9cb1f302015-04-01 11:39:49 +020043VCMProtectionMethod::VCMProtectionMethod()
44 : _effectivePacketLoss(0),
45 _protectionFactorK(0),
46 _protectionFactorD(0),
47 _scaleProtKey(2.0f),
48 _maxPayloadSize(1460),
Peter Boström9cb1f302015-04-01 11:39:49 +020049 _corrFecCost(1.0),
philipel9d3ab612015-12-21 04:12:39 -080050 _type(kNone) {}
mikhal@webrtc.orga057a952011-08-26 21:17:34 +000051
pbosc0430522016-05-01 17:19:05 -070052VCMProtectionMethod::~VCMProtectionMethod() {}
marpan@google.com86548c62011-07-12 17:12:57 +000053
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020054enum VCMProtectionMethodEnum VCMProtectionMethod::Type() const {
55 return _type;
56}
57
58uint8_t VCMProtectionMethod::RequiredPacketLossER() {
59 return _effectivePacketLoss;
60}
61
62uint8_t VCMProtectionMethod::RequiredProtectionFactorK() {
63 return _protectionFactorK;
64}
65
66uint8_t VCMProtectionMethod::RequiredProtectionFactorD() {
67 return _protectionFactorD;
68}
69
70bool VCMProtectionMethod::RequiredUepProtectionK() {
71 return _useUepProtectionK;
72}
73
74bool VCMProtectionMethod::RequiredUepProtectionD() {
75 return _useUepProtectionD;
76}
77
78int VCMProtectionMethod::MaxFramesFec() const {
79 return 1;
80}
81
pkasting@chromium.org16825b12015-01-12 21:51:21 +000082VCMNackFecMethod::VCMNackFecMethod(int64_t lowRttNackThresholdMs,
83 int64_t highRttNackThresholdMs)
stefan@webrtc.org932ab182011-11-29 11:33:31 +000084 : VCMFecMethod(),
85 _lowRttNackMs(lowRttNackThresholdMs),
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +000086 _highRttNackMs(highRttNackThresholdMs),
87 _maxFramesFec(1) {
stefan@webrtc.org932ab182011-11-29 11:33:31 +000088 assert(lowRttNackThresholdMs >= -1 && highRttNackThresholdMs >= -1);
89 assert(highRttNackThresholdMs == -1 ||
90 lowRttNackThresholdMs <= highRttNackThresholdMs);
91 assert(lowRttNackThresholdMs > -1 || highRttNackThresholdMs == -1);
92 _type = kNackFec;
mikhal@google.com77408882011-07-22 22:05:25 +000093}
94
philipel9d3ab612015-12-21 04:12:39 -080095VCMNackFecMethod::~VCMNackFecMethod() {
96 //
mikhal@google.com77408882011-07-22 22:05:25 +000097}
philipel9d3ab612015-12-21 04:12:39 -080098bool VCMNackFecMethod::ProtectionFactor(
99 const VCMProtectionParameters* parameters) {
100 // Hybrid Nack FEC has three operational modes:
101 // 1. Low RTT (below kLowRttNackMs) - Nack only: Set FEC rate
102 // (_protectionFactorD) to zero. -1 means no FEC.
103 // 2. High RTT (above _highRttNackMs) - FEC Only: Keep FEC factors.
104 // -1 means always allow NACK.
105 // 3. Medium RTT values - Hybrid mode: We will only nack the
106 // residual following the decoding of the FEC (refer to JB logic). FEC
107 // delta protection factor will be adjusted based on the RTT.
mikhal@google.com022716b2011-07-20 23:12:57 +0000108
philipel9d3ab612015-12-21 04:12:39 -0800109 // Otherwise: we count on FEC; if the RTT is below a threshold, then we
110 // nack the residual, based on a decision made in the JB.
mikhal@google.com022716b2011-07-20 23:12:57 +0000111
philipel9d3ab612015-12-21 04:12:39 -0800112 // Compute the protection factors
113 VCMFecMethod::ProtectionFactor(parameters);
114 if (_lowRttNackMs == -1 || parameters->rtt < _lowRttNackMs) {
115 _protectionFactorD = 0;
116 VCMFecMethod::UpdateProtectionFactorD(_protectionFactorD);
mikhal@google.com320813c2011-08-03 20:47:50 +0000117
mikhal@google.com679450f2011-08-01 22:14:58 +0000118 // When in Hybrid mode (RTT range), adjust FEC rates based on the
119 // RTT (NACK effectiveness) - adjustment factor is in the range [0,1].
philipel9d3ab612015-12-21 04:12:39 -0800120 } else if (_highRttNackMs == -1 || parameters->rtt < _highRttNackMs) {
121 // TODO(mikhal): Disabling adjustment temporarily.
122 // uint16_t rttIndex = (uint16_t) parameters->rtt;
123 float adjustRtt = 1.0f; // (float)VCMNackFecTable[rttIndex] / 100.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124
philipel9d3ab612015-12-21 04:12:39 -0800125 // Adjust FEC with NACK on (for delta frame only)
126 // table depends on RTT relative to rttMax (NACK Threshold)
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700127 _protectionFactorD = rtc::saturated_cast<uint8_t>(
128 adjustRtt * rtc::saturated_cast<float>(_protectionFactorD));
philipel9d3ab612015-12-21 04:12:39 -0800129 // update FEC rates after applying adjustment
130 VCMFecMethod::UpdateProtectionFactorD(_protectionFactorD);
131 }
mikhal@google.com679450f2011-08-01 22:14:58 +0000132
philipel9d3ab612015-12-21 04:12:39 -0800133 return true;
mikhal@google.com022716b2011-07-20 23:12:57 +0000134}
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000136int VCMNackFecMethod::ComputeMaxFramesFec(
137 const VCMProtectionParameters* parameters) {
138 if (parameters->numLayers > 2) {
139 // For more than 2 temporal layers we will only have FEC on the base layer,
140 // and the base layers will be pretty far apart. Therefore we force one
141 // frame FEC.
142 return 1;
143 }
144 // We set the max number of frames to base the FEC on so that on average
145 // we will have complete frames in one RTT. Note that this is an upper
146 // bound, and that the actual number of frames used for FEC is decided by the
147 // RTP module based on the actual number of packets and the protection factor.
philipel9d3ab612015-12-21 04:12:39 -0800148 float base_layer_framerate =
149 parameters->frameRate /
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700150 rtc::saturated_cast<float>(1 << (parameters->numLayers - 1));
philipel9d3ab612015-12-21 04:12:39 -0800151 int max_frames_fec = std::max(
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700152 rtc::saturated_cast<int>(
153 2.0f * base_layer_framerate * parameters->rtt / 1000.0f + 0.5f),
philipel9d3ab612015-12-21 04:12:39 -0800154 1);
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000155 // |kUpperLimitFramesFec| is the upper limit on how many frames we
156 // allow any FEC to be based on.
157 if (max_frames_fec > kUpperLimitFramesFec) {
158 max_frames_fec = kUpperLimitFramesFec;
159 }
160 return max_frames_fec;
161}
162
163int VCMNackFecMethod::MaxFramesFec() const {
164 return _maxFramesFec;
165}
166
marpan@webrtc.org88ad06b2012-04-20 16:05:24 +0000167bool VCMNackFecMethod::BitRateTooLowForFec(
168 const VCMProtectionParameters* parameters) {
169 // Bitrate below which we turn off FEC, regardless of reported packet loss.
170 // The condition should depend on resolution and content. For now, use
171 // threshold on bytes per frame, with some effect for the frame size.
172 // The condition for turning off FEC is also based on other factors,
173 // such as |_numLayers|, |_maxFramesFec|, and |_rtt|.
174 int estimate_bytes_per_frame = 1000 * BitsPerFrame(parameters) / 8;
175 int max_bytes_per_frame = kMaxBytesPerFrameForFec;
176 int num_pixels = parameters->codecWidth * parameters->codecHeight;
177 if (num_pixels <= 352 * 288) {
178 max_bytes_per_frame = kMaxBytesPerFrameForFecLow;
179 } else if (num_pixels > 640 * 480) {
180 max_bytes_per_frame = kMaxBytesPerFrameForFecHigh;
181 }
philipel9d3ab612015-12-21 04:12:39 -0800182 // TODO(marpan): add condition based on maximum frames used for FEC,
marpan@webrtc.org88ad06b2012-04-20 16:05:24 +0000183 // and expand condition based on frame size.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000184 // Max round trip time threshold in ms.
185 const int64_t kMaxRttTurnOffFec = 200;
marpan@webrtc.org88ad06b2012-04-20 16:05:24 +0000186 if (estimate_bytes_per_frame < max_bytes_per_frame &&
philipel9d3ab612015-12-21 04:12:39 -0800187 parameters->numLayers < 3 && parameters->rtt < kMaxRttTurnOffFec) {
marpan@webrtc.org88ad06b2012-04-20 16:05:24 +0000188 return true;
189 }
190 return false;
191}
192
philipel9d3ab612015-12-21 04:12:39 -0800193bool VCMNackFecMethod::EffectivePacketLoss(
194 const VCMProtectionParameters* parameters) {
195 // Set the effective packet loss for encoder (based on FEC code).
196 // Compute the effective packet loss and residual packet loss due to FEC.
197 VCMFecMethod::EffectivePacketLoss(parameters);
198 return true;
mikhal@google.com022716b2011-07-20 23:12:57 +0000199}
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
philipel9d3ab612015-12-21 04:12:39 -0800201bool VCMNackFecMethod::UpdateParameters(
202 const VCMProtectionParameters* parameters) {
203 ProtectionFactor(parameters);
204 EffectivePacketLoss(parameters);
205 _maxFramesFec = ComputeMaxFramesFec(parameters);
206 if (BitRateTooLowForFec(parameters)) {
207 _protectionFactorK = 0;
208 _protectionFactorD = 0;
209 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
philipel9d3ab612015-12-21 04:12:39 -0800211 // Protection/fec rates obtained above are defined relative to total number
212 // of packets (total rate: source + fec) FEC in RTP module assumes
213 // protection factor is defined relative to source number of packets so we
214 // should convert the factor to reduce mismatch between mediaOpt's rate and
215 // the actual one
216 _protectionFactorK = VCMFecMethod::ConvertFECRate(_protectionFactorK);
217 _protectionFactorD = VCMFecMethod::ConvertFECRate(_protectionFactorD);
niklase@google.com470e71d2011-07-07 08:21:25 +0000218
philipel9d3ab612015-12-21 04:12:39 -0800219 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000220}
221
philipel9d3ab612015-12-21 04:12:39 -0800222VCMNackMethod::VCMNackMethod() : VCMProtectionMethod() {
223 _type = kNack;
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000224}
225
philipel9d3ab612015-12-21 04:12:39 -0800226VCMNackMethod::~VCMNackMethod() {
227 //
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000228}
229
philipel9d3ab612015-12-21 04:12:39 -0800230bool VCMNackMethod::EffectivePacketLoss(
231 const VCMProtectionParameters* parameter) {
232 // Effective Packet Loss, NA in current version.
233 _effectivePacketLoss = 0;
234 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235}
236
philipel9d3ab612015-12-21 04:12:39 -0800237bool VCMNackMethod::UpdateParameters(
238 const VCMProtectionParameters* parameters) {
239 // Compute the effective packet loss
240 EffectivePacketLoss(parameters);
niklase@google.com470e71d2011-07-07 08:21:25 +0000241
philipel9d3ab612015-12-21 04:12:39 -0800242 // nackCost = (bitRate - nackCost) * (lossPr)
243 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244}
245
philipel9d3ab612015-12-21 04:12:39 -0800246VCMFecMethod::VCMFecMethod() : VCMProtectionMethod() {
247 _type = kFec;
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000248}
philipel9d3ab612015-12-21 04:12:39 -0800249VCMFecMethod::~VCMFecMethod() {
250 //
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000251}
252
philipel9d3ab612015-12-21 04:12:39 -0800253uint8_t VCMFecMethod::BoostCodeRateKey(uint8_t packetFrameDelta,
254 uint8_t packetFrameKey) const {
255 uint8_t boostRateKey = 2;
256 // Default: ratio scales the FEC protection up for I frames
257 uint8_t ratio = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258
philipel9d3ab612015-12-21 04:12:39 -0800259 if (packetFrameDelta > 0) {
260 ratio = (int8_t)(packetFrameKey / packetFrameDelta);
261 }
262 ratio = VCM_MAX(boostRateKey, ratio);
niklase@google.com470e71d2011-07-07 08:21:25 +0000263
philipel9d3ab612015-12-21 04:12:39 -0800264 return ratio;
niklase@google.com470e71d2011-07-07 08:21:25 +0000265}
266
philipel9d3ab612015-12-21 04:12:39 -0800267uint8_t VCMFecMethod::ConvertFECRate(uint8_t codeRateRTP) const {
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700268 return rtc::saturated_cast<uint8_t>(
269 VCM_MIN(255, (0.5 + 255.0 * codeRateRTP /
270 rtc::saturated_cast<float>(255 - codeRateRTP))));
niklase@google.com470e71d2011-07-07 08:21:25 +0000271}
272
mikhal@google.com679450f2011-08-01 22:14:58 +0000273// Update FEC with protectionFactorD
philipel9d3ab612015-12-21 04:12:39 -0800274void VCMFecMethod::UpdateProtectionFactorD(uint8_t protectionFactorD) {
275 _protectionFactorD = protectionFactorD;
mikhal@google.com679450f2011-08-01 22:14:58 +0000276}
277
mikhal@webrtc.orgd0752c32011-10-19 15:48:30 +0000278// Update FEC with protectionFactorK
philipel9d3ab612015-12-21 04:12:39 -0800279void VCMFecMethod::UpdateProtectionFactorK(uint8_t protectionFactorK) {
280 _protectionFactorK = protectionFactorK;
mikhal@webrtc.orgd0752c32011-10-19 15:48:30 +0000281}
282
philipel9d3ab612015-12-21 04:12:39 -0800283bool VCMFecMethod::ProtectionFactor(const VCMProtectionParameters* parameters) {
284 // FEC PROTECTION SETTINGS: varies with packet loss and bitrate
niklase@google.com470e71d2011-07-07 08:21:25 +0000285
philipel9d3ab612015-12-21 04:12:39 -0800286 // No protection if (filtered) packetLoss is 0
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700287 uint8_t packetLoss = rtc::saturated_cast<uint8_t>(255 * parameters->lossPr);
philipel9d3ab612015-12-21 04:12:39 -0800288 if (packetLoss == 0) {
289 _protectionFactorK = 0;
290 _protectionFactorD = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000291 return true;
philipel9d3ab612015-12-21 04:12:39 -0800292 }
293
294 // Parameters for FEC setting:
295 // first partition size, thresholds, table pars, spatial resoln fac.
296
297 // First partition protection: ~ 20%
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700298 uint8_t firstPartitionProt = rtc::saturated_cast<uint8_t>(255 * 0.20);
philipel9d3ab612015-12-21 04:12:39 -0800299
300 // Minimum protection level needed to generate one FEC packet for one
301 // source packet/frame (in RTP sender)
302 uint8_t minProtLevelFec = 85;
303
304 // Threshold on packetLoss and bitRrate/frameRate (=average #packets),
305 // above which we allocate protection to cover at least first partition.
306 uint8_t lossThr = 0;
307 uint8_t packetNumThr = 1;
308
309 // Parameters for range of rate index of table.
310 const uint8_t ratePar1 = 5;
311 const uint8_t ratePar2 = 49;
312
313 // Spatial resolution size, relative to a reference size.
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700314 float spatialSizeToRef = rtc::saturated_cast<float>(parameters->codecWidth *
315 parameters->codecHeight) /
316 (rtc::saturated_cast<float>(704 * 576));
philipel9d3ab612015-12-21 04:12:39 -0800317 // resolnFac: This parameter will generally increase/decrease the FEC rate
318 // (for fixed bitRate and packetLoss) based on system size.
319 // Use a smaller exponent (< 1) to control/soften system size effect.
320 const float resolnFac = 1.0 / powf(spatialSizeToRef, 0.3f);
321
322 const int bitRatePerFrame = BitsPerFrame(parameters);
323
324 // Average number of packets per frame (source and fec):
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700325 const uint8_t avgTotPackets = rtc::saturated_cast<uint8_t>(
326 1.5f + rtc::saturated_cast<float>(bitRatePerFrame) * 1000.0f /
327 rtc::saturated_cast<float>(8.0 * _maxPayloadSize));
philipel9d3ab612015-12-21 04:12:39 -0800328
329 // FEC rate parameters: for P and I frame
330 uint8_t codeRateDelta = 0;
331 uint8_t codeRateKey = 0;
332
333 // Get index for table: the FEC protection depends on an effective rate.
334 // The range on the rate index corresponds to rates (bps)
335 // from ~200k to ~8000k, for 30fps
336 const uint16_t effRateFecTable =
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700337 rtc::saturated_cast<uint16_t>(resolnFac * bitRatePerFrame);
338 uint8_t rateIndexTable = rtc::saturated_cast<uint8_t>(
brandtr05f845d2016-11-16 22:59:39 -0800339 VCM_MAX(VCM_MIN((effRateFecTable - ratePar1) / ratePar1, ratePar2), 0));
philipel9d3ab612015-12-21 04:12:39 -0800340
341 // Restrict packet loss range to 50:
342 // current tables defined only up to 50%
343 if (packetLoss >= kPacketLossMax) {
344 packetLoss = kPacketLossMax - 1;
345 }
346 uint16_t indexTable = rateIndexTable * kPacketLossMax + packetLoss;
347
348 // Check on table index
brandtr71b1c1f2017-01-12 06:16:24 -0800349 RTC_DCHECK_LT(indexTable, kFecRateTableSize);
philipel9d3ab612015-12-21 04:12:39 -0800350
351 // Protection factor for P frame
brandtr71b1c1f2017-01-12 06:16:24 -0800352 codeRateDelta = kFecRateTable[indexTable];
philipel9d3ab612015-12-21 04:12:39 -0800353
354 if (packetLoss > lossThr && avgTotPackets > packetNumThr) {
355 // Set a minimum based on first partition size.
356 if (codeRateDelta < firstPartitionProt) {
357 codeRateDelta = firstPartitionProt;
358 }
359 }
360
361 // Check limit on amount of protection for P frame; 50% is max.
362 if (codeRateDelta >= kPacketLossMax) {
363 codeRateDelta = kPacketLossMax - 1;
364 }
365
philipel9d3ab612015-12-21 04:12:39 -0800366 // For Key frame:
367 // Effectively at a higher rate, so we scale/boost the rate
368 // The boost factor may depend on several factors: ratio of packet
369 // number of I to P frames, how much protection placed on P frames, etc.
brandtr05f845d2016-11-16 22:59:39 -0800370 const uint8_t packetFrameDelta =
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700371 rtc::saturated_cast<uint8_t>(0.5 + parameters->packetsPerFrame);
philipel9d3ab612015-12-21 04:12:39 -0800372 const uint8_t packetFrameKey =
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700373 rtc::saturated_cast<uint8_t>(0.5 + parameters->packetsPerFrameKey);
philipel9d3ab612015-12-21 04:12:39 -0800374 const uint8_t boostKey = BoostCodeRateKey(packetFrameDelta, packetFrameKey);
375
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700376 rateIndexTable = rtc::saturated_cast<uint8_t>(VCM_MAX(
philipel9d3ab612015-12-21 04:12:39 -0800377 VCM_MIN(1 + (boostKey * effRateFecTable - ratePar1) / ratePar1, ratePar2),
brandtr05f845d2016-11-16 22:59:39 -0800378 0));
philipel9d3ab612015-12-21 04:12:39 -0800379 uint16_t indexTableKey = rateIndexTable * kPacketLossMax + packetLoss;
380
brandtr71b1c1f2017-01-12 06:16:24 -0800381 indexTableKey = VCM_MIN(indexTableKey, kFecRateTableSize);
philipel9d3ab612015-12-21 04:12:39 -0800382
383 // Check on table index
brandtr71b1c1f2017-01-12 06:16:24 -0800384 assert(indexTableKey < kFecRateTableSize);
philipel9d3ab612015-12-21 04:12:39 -0800385
386 // Protection factor for I frame
brandtr71b1c1f2017-01-12 06:16:24 -0800387 codeRateKey = kFecRateTable[indexTableKey];
philipel9d3ab612015-12-21 04:12:39 -0800388
389 // Boosting for Key frame.
390 int boostKeyProt = _scaleProtKey * codeRateDelta;
391 if (boostKeyProt >= kPacketLossMax) {
392 boostKeyProt = kPacketLossMax - 1;
393 }
394
395 // Make sure I frame protection is at least larger than P frame protection,
396 // and at least as high as filtered packet loss.
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700397 codeRateKey = rtc::saturated_cast<uint8_t>(
philipel9d3ab612015-12-21 04:12:39 -0800398 VCM_MAX(packetLoss, VCM_MAX(boostKeyProt, codeRateKey)));
399
400 // Check limit on amount of protection for I frame: 50% is max.
401 if (codeRateKey >= kPacketLossMax) {
402 codeRateKey = kPacketLossMax - 1;
403 }
404
405 _protectionFactorK = codeRateKey;
406 _protectionFactorD = codeRateDelta;
407
408 // Generally there is a rate mis-match between the FEC cost estimated
409 // in mediaOpt and the actual FEC cost sent out in RTP module.
410 // This is more significant at low rates (small # of source packets), where
411 // the granularity of the FEC decreases. In this case, non-zero protection
412 // in mediaOpt may generate 0 FEC packets in RTP sender (since actual #FEC
413 // is based on rounding off protectionFactor on actual source packet number).
414 // The correction factor (_corrFecCost) attempts to corrects this, at least
415 // for cases of low rates (small #packets) and low protection levels.
416
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700417 float numPacketsFl =
418 1.0f + (rtc::saturated_cast<float>(bitRatePerFrame) * 1000.0 /
419 rtc::saturated_cast<float>(8.0 * _maxPayloadSize) +
420 0.5);
philipel9d3ab612015-12-21 04:12:39 -0800421
422 const float estNumFecGen =
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700423 0.5f +
424 rtc::saturated_cast<float>(_protectionFactorD * numPacketsFl / 255.0f);
philipel9d3ab612015-12-21 04:12:39 -0800425
426 // We reduce cost factor (which will reduce overhead for FEC and
427 // hybrid method) and not the protectionFactor.
428 _corrFecCost = 1.0f;
429 if (estNumFecGen < 1.1f && _protectionFactorD < minProtLevelFec) {
430 _corrFecCost = 0.5f;
431 }
432 if (estNumFecGen < 0.9f && _protectionFactorD < minProtLevelFec) {
433 _corrFecCost = 0.0f;
434 }
435
philipel9d3ab612015-12-21 04:12:39 -0800436 // DONE WITH FEC PROTECTION SETTINGS
437 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000438}
439
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +0000440int VCMFecMethod::BitsPerFrame(const VCMProtectionParameters* parameters) {
441 // When temporal layers are available FEC will only be applied on the base
442 // layer.
Erik Språngd92288f2018-07-04 10:07:40 +0200443 const float bitRateRatio =
444 webrtc::SimulcastRateAllocator::GetTemporalRateAllocation(
445 parameters->numLayers, 0);
thakis@chromium.org65bc2542012-08-13 19:26:12 +0000446 float frameRateRatio = powf(1 / 2.0, parameters->numLayers - 1);
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +0000447 float bitRate = parameters->bitRate * bitRateRatio;
448 float frameRate = parameters->frameRate * frameRateRatio;
449
450 // TODO(mikhal): Update factor following testing.
451 float adjustmentFactor = 1;
452
Peter Boström5e8351b2016-01-28 23:55:29 +0100453 if (frameRate < 1.0f)
454 frameRate = 1.0f;
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +0000455 // Average bits per frame (units of kbits)
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700456 return rtc::saturated_cast<int>(adjustmentFactor * bitRate / frameRate);
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +0000457}
458
philipel9d3ab612015-12-21 04:12:39 -0800459bool VCMFecMethod::EffectivePacketLoss(
460 const VCMProtectionParameters* parameters) {
461 // Effective packet loss to encoder is based on RPL (residual packet loss)
462 // this is a soft setting based on degree of FEC protection
463 // RPL = received/input packet loss - average_FEC_recovery
464 // note: received/input packet loss may be filtered based on FilteredLoss
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
philipel9d3ab612015-12-21 04:12:39 -0800466 // Effective Packet Loss, NA in current version.
467 _effectivePacketLoss = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000468
philipel9d3ab612015-12-21 04:12:39 -0800469 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000470}
471
philipel9d3ab612015-12-21 04:12:39 -0800472bool VCMFecMethod::UpdateParameters(const VCMProtectionParameters* parameters) {
473 // Compute the protection factor
474 ProtectionFactor(parameters);
niklase@google.com470e71d2011-07-07 08:21:25 +0000475
philipel9d3ab612015-12-21 04:12:39 -0800476 // Compute the effective packet loss
477 EffectivePacketLoss(parameters);
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
philipel9d3ab612015-12-21 04:12:39 -0800479 // Protection/fec rates obtained above is defined relative to total number
480 // of packets (total rate: source+fec) FEC in RTP module assumes protection
481 // factor is defined relative to source number of packets so we should
482 // convert the factor to reduce mismatch between mediaOpt suggested rate and
483 // the actual rate
484 _protectionFactorK = ConvertFECRate(_protectionFactorK);
485 _protectionFactorD = ConvertFECRate(_protectionFactorD);
niklase@google.com470e71d2011-07-07 08:21:25 +0000486
philipel9d3ab612015-12-21 04:12:39 -0800487 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000488}
philipel9d3ab612015-12-21 04:12:39 -0800489VCMLossProtectionLogic::VCMLossProtectionLogic(int64_t nowMs)
490 : _currentParameters(),
491 _rtt(0),
492 _lossPr(0.0f),
493 _bitRate(0.0f),
494 _frameRate(0.0f),
495 _keyFrameSize(0.0f),
496 _fecRateKey(0),
497 _fecRateDelta(0),
498 _lastPrUpdateT(0),
499 _lossPr255(0.9999f),
500 _lossPrHistory(),
501 _shortMaxLossPr255(0),
502 _packetsPerFrame(0.9999f),
503 _packetsPerFrameKey(0.9999f),
Ying Wang1f262cc2018-08-23 13:54:08 +0200504 _codecWidth(704),
505 _codecHeight(576),
philipel9d3ab612015-12-21 04:12:39 -0800506 _numLayers(1) {
507 Reset(nowMs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000508}
509
philipel9d3ab612015-12-21 04:12:39 -0800510VCMLossProtectionLogic::~VCMLossProtectionLogic() {
511 Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000512}
513
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000514void VCMLossProtectionLogic::SetMethod(
515 enum VCMProtectionMethodEnum newMethodType) {
pbosba8c15b2015-07-14 09:36:34 -0700516 if (_selectedMethod && _selectedMethod->Type() == newMethodType)
517 return;
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000518
philipel9d3ab612015-12-21 04:12:39 -0800519 switch (newMethodType) {
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000520 case kNack:
pbosba8c15b2015-07-14 09:36:34 -0700521 _selectedMethod.reset(new VCMNackMethod());
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000522 break;
523 case kFec:
pbosba8c15b2015-07-14 09:36:34 -0700524 _selectedMethod.reset(new VCMFecMethod());
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000525 break;
526 case kNackFec:
pbosba8c15b2015-07-14 09:36:34 -0700527 _selectedMethod.reset(new VCMNackFecMethod(kLowRttNackMs, -1));
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000528 break;
529 case kNone:
pbosba8c15b2015-07-14 09:36:34 -0700530 _selectedMethod.reset();
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000531 break;
532 }
533 UpdateMethod();
niklase@google.com470e71d2011-07-07 08:21:25 +0000534}
535
philipel9d3ab612015-12-21 04:12:39 -0800536void VCMLossProtectionLogic::UpdateRtt(int64_t rtt) {
537 _rtt = rtt;
niklase@google.com470e71d2011-07-07 08:21:25 +0000538}
539
philipel9d3ab612015-12-21 04:12:39 -0800540void VCMLossProtectionLogic::UpdateMaxLossHistory(uint8_t lossPr255,
541 int64_t now) {
542 if (_lossPrHistory[0].timeMs >= 0 &&
543 now - _lossPrHistory[0].timeMs < kLossPrShortFilterWinMs) {
544 if (lossPr255 > _shortMaxLossPr255) {
545 _shortMaxLossPr255 = lossPr255;
niklase@google.com470e71d2011-07-07 08:21:25 +0000546 }
philipel9d3ab612015-12-21 04:12:39 -0800547 } else {
548 // Only add a new value to the history once a second
549 if (_lossPrHistory[0].timeMs == -1) {
550 // First, no shift
551 _shortMaxLossPr255 = lossPr255;
552 } else {
553 // Shift
554 for (int32_t i = (kLossPrHistorySize - 2); i >= 0; i--) {
555 _lossPrHistory[i + 1].lossPr255 = _lossPrHistory[i].lossPr255;
556 _lossPrHistory[i + 1].timeMs = _lossPrHistory[i].timeMs;
557 }
558 }
559 if (_shortMaxLossPr255 == 0) {
560 _shortMaxLossPr255 = lossPr255;
561 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000562
philipel9d3ab612015-12-21 04:12:39 -0800563 _lossPrHistory[0].lossPr255 = _shortMaxLossPr255;
564 _lossPrHistory[0].timeMs = now;
565 _shortMaxLossPr255 = 0;
566 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000567}
568
philipel9d3ab612015-12-21 04:12:39 -0800569uint8_t VCMLossProtectionLogic::MaxFilteredLossPr(int64_t nowMs) const {
570 uint8_t maxFound = _shortMaxLossPr255;
571 if (_lossPrHistory[0].timeMs == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000572 return maxFound;
philipel9d3ab612015-12-21 04:12:39 -0800573 }
574 for (int32_t i = 0; i < kLossPrHistorySize; i++) {
575 if (_lossPrHistory[i].timeMs == -1) {
576 break;
577 }
578 if (nowMs - _lossPrHistory[i].timeMs >
579 kLossPrHistorySize * kLossPrShortFilterWinMs) {
580 // This sample (and all samples after this) is too old
581 break;
582 }
583 if (_lossPrHistory[i].lossPr255 > maxFound) {
584 // This sample is the largest one this far into the history
585 maxFound = _lossPrHistory[i].lossPr255;
586 }
587 }
588 return maxFound;
niklase@google.com470e71d2011-07-07 08:21:25 +0000589}
590
philipel9d3ab612015-12-21 04:12:39 -0800591uint8_t VCMLossProtectionLogic::FilteredLoss(int64_t nowMs,
592 FilterPacketLossMode filter_mode,
593 uint8_t lossPr255) {
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +0000594 // Update the max window filter.
595 UpdateMaxLossHistory(lossPr255, nowMs);
596
597 // Update the recursive average filter.
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700598 _lossPr255.Apply(rtc::saturated_cast<float>(nowMs - _lastPrUpdateT),
599 rtc::saturated_cast<float>(lossPr255));
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +0000600 _lastPrUpdateT = nowMs;
601
602 // Filtered loss: default is received loss (no filtering).
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000603 uint8_t filtered_loss = lossPr255;
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +0000604
605 switch (filter_mode) {
606 case kNoFilter:
607 break;
608 case kAvgFilter:
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700609 filtered_loss = rtc::saturated_cast<uint8_t>(_lossPr255.filtered() + 0.5);
marpan@webrtc.org2dad3fb2012-01-09 18:18:36 +0000610 break;
611 case kMaxFilter:
612 filtered_loss = MaxFilteredLossPr(nowMs);
613 break;
614 }
615
616 return filtered_loss;
niklase@google.com470e71d2011-07-07 08:21:25 +0000617}
618
philipel9d3ab612015-12-21 04:12:39 -0800619void VCMLossProtectionLogic::UpdateFilteredLossPr(uint8_t packetLossEnc) {
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700620 _lossPr = rtc::saturated_cast<float>(packetLossEnc) / 255.0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000621}
622
philipel9d3ab612015-12-21 04:12:39 -0800623void VCMLossProtectionLogic::UpdateBitRate(float bitRate) {
624 _bitRate = bitRate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000625}
626
philipel9d3ab612015-12-21 04:12:39 -0800627void VCMLossProtectionLogic::UpdatePacketsPerFrame(float nPackets,
628 int64_t nowMs) {
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700629 _packetsPerFrame.Apply(
630 rtc::saturated_cast<float>(nowMs - _lastPacketPerFrameUpdateT), nPackets);
philipel9d3ab612015-12-21 04:12:39 -0800631 _lastPacketPerFrameUpdateT = nowMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000632}
633
philipel9d3ab612015-12-21 04:12:39 -0800634void VCMLossProtectionLogic::UpdatePacketsPerFrameKey(float nPackets,
635 int64_t nowMs) {
636 _packetsPerFrameKey.Apply(
Emircan Uysaler704a7bd2018-08-06 16:14:10 -0700637 rtc::saturated_cast<float>(nowMs - _lastPacketPerFrameUpdateTKey),
638 nPackets);
philipel9d3ab612015-12-21 04:12:39 -0800639 _lastPacketPerFrameUpdateTKey = nowMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000640}
641
philipel9d3ab612015-12-21 04:12:39 -0800642void VCMLossProtectionLogic::UpdateKeyFrameSize(float keyFrameSize) {
643 _keyFrameSize = keyFrameSize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000644}
645
perkjc2c24f72016-07-11 01:47:32 -0700646void VCMLossProtectionLogic::UpdateFrameSize(size_t width, size_t height) {
philipel9d3ab612015-12-21 04:12:39 -0800647 _codecWidth = width;
648 _codecHeight = height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000649}
650
mikhal@webrtc.org0e7d9d82011-12-19 19:04:49 +0000651void VCMLossProtectionLogic::UpdateNumLayers(int numLayers) {
652 _numLayers = (numLayers == 0) ? 1 : numLayers;
653}
654
philipel9d3ab612015-12-21 04:12:39 -0800655bool VCMLossProtectionLogic::UpdateMethod() {
656 if (!_selectedMethod)
657 return false;
658 _currentParameters.rtt = _rtt;
659 _currentParameters.lossPr = _lossPr;
660 _currentParameters.bitRate = _bitRate;
661 _currentParameters.frameRate = _frameRate; // rename actual frame rate?
662 _currentParameters.keyFrameSize = _keyFrameSize;
663 _currentParameters.fecRateDelta = _fecRateDelta;
664 _currentParameters.fecRateKey = _fecRateKey;
665 _currentParameters.packetsPerFrame = _packetsPerFrame.filtered();
666 _currentParameters.packetsPerFrameKey = _packetsPerFrameKey.filtered();
667 _currentParameters.codecWidth = _codecWidth;
668 _currentParameters.codecHeight = _codecHeight;
669 _currentParameters.numLayers = _numLayers;
670 return _selectedMethod->UpdateParameters(&_currentParameters);
niklase@google.com470e71d2011-07-07 08:21:25 +0000671}
672
philipel9d3ab612015-12-21 04:12:39 -0800673VCMProtectionMethod* VCMLossProtectionLogic::SelectedMethod() const {
674 return _selectedMethod.get();
niklase@google.com470e71d2011-07-07 08:21:25 +0000675}
676
pbos@webrtc.orgcade82c2015-03-12 10:39:24 +0000677VCMProtectionMethodEnum VCMLossProtectionLogic::SelectedType() const {
pbosba8c15b2015-07-14 09:36:34 -0700678 return _selectedMethod ? _selectedMethod->Type() : kNone;
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000679}
680
philipel9d3ab612015-12-21 04:12:39 -0800681void VCMLossProtectionLogic::Reset(int64_t nowMs) {
682 _lastPrUpdateT = nowMs;
683 _lastPacketPerFrameUpdateT = nowMs;
684 _lastPacketPerFrameUpdateTKey = nowMs;
685 _lossPr255.Reset(0.9999f);
686 _packetsPerFrame.Reset(0.9999f);
687 _fecRateDelta = _fecRateKey = 0;
688 for (int32_t i = 0; i < kLossPrHistorySize; i++) {
689 _lossPrHistory[i].lossPr255 = 0;
690 _lossPrHistory[i].timeMs = -1;
691 }
692 _shortMaxLossPr255 = 0;
693 Release();
mikhal@webrtc.orga057a952011-08-26 21:17:34 +0000694}
695
pbosba8c15b2015-07-14 09:36:34 -0700696void VCMLossProtectionLogic::Release() {
697 _selectedMethod.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000698}
699
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000700} // namespace media_optimization
701} // namespace webrtc