niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/media_opt_util.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 13 | #include <algorithm> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <float.h> |
| 15 | #include <limits.h> |
pbos@webrtc.org | a440732 | 2013-07-16 12:32:05 +0000 | [diff] [blame] | 16 | #include <math.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 18 | #include "webrtc/modules/include/module_common_types.h" |
pbos@webrtc.org | a440732 | 2013-07-16 12:32:05 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 20 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
| 21 | #include "webrtc/modules/video_coding/fec_tables_xor.h" |
| 22 | #include "webrtc/modules/video_coding/nack_fec_tables.h" |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 23 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | namespace webrtc { |
Peter Boström | 9cb1f30 | 2015-04-01 11:39:49 +0200 | [diff] [blame] | 25 | // Max value of loss rates in off-line model |
| 26 | static const int kPacketLossMax = 129; |
| 27 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 28 | namespace media_optimization { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
Peter Boström | 9cb1f30 | 2015-04-01 11:39:49 +0200 | [diff] [blame] | 30 | VCMProtectionMethod::VCMProtectionMethod() |
| 31 | : _effectivePacketLoss(0), |
| 32 | _protectionFactorK(0), |
| 33 | _protectionFactorD(0), |
| 34 | _scaleProtKey(2.0f), |
| 35 | _maxPayloadSize(1460), |
| 36 | _qmRobustness(new VCMQmRobustness()), |
| 37 | _useUepProtectionK(false), |
| 38 | _useUepProtectionD(true), |
| 39 | _corrFecCost(1.0), |
| 40 | _type(kNone) { |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | VCMProtectionMethod::~VCMProtectionMethod() |
| 44 | { |
| 45 | delete _qmRobustness; |
| 46 | } |
marpan@google.com | 86548c6 | 2011-07-12 17:12:57 +0000 | [diff] [blame] | 47 | void |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 48 | VCMProtectionMethod::UpdateContentMetrics(const |
| 49 | VideoContentMetrics* contentMetrics) |
marpan@google.com | 86548c6 | 2011-07-12 17:12:57 +0000 | [diff] [blame] | 50 | { |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 51 | _qmRobustness->UpdateContent(contentMetrics); |
marpan@google.com | 86548c6 | 2011-07-12 17:12:57 +0000 | [diff] [blame] | 52 | } |
| 53 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 54 | VCMNackFecMethod::VCMNackFecMethod(int64_t lowRttNackThresholdMs, |
| 55 | int64_t highRttNackThresholdMs) |
stefan@webrtc.org | 932ab18 | 2011-11-29 11:33:31 +0000 | [diff] [blame] | 56 | : VCMFecMethod(), |
| 57 | _lowRttNackMs(lowRttNackThresholdMs), |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 58 | _highRttNackMs(highRttNackThresholdMs), |
| 59 | _maxFramesFec(1) { |
stefan@webrtc.org | 932ab18 | 2011-11-29 11:33:31 +0000 | [diff] [blame] | 60 | assert(lowRttNackThresholdMs >= -1 && highRttNackThresholdMs >= -1); |
| 61 | assert(highRttNackThresholdMs == -1 || |
| 62 | lowRttNackThresholdMs <= highRttNackThresholdMs); |
| 63 | assert(lowRttNackThresholdMs > -1 || highRttNackThresholdMs == -1); |
| 64 | _type = kNackFec; |
mikhal@google.com | 7740888 | 2011-07-22 22:05:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | VCMNackFecMethod::~VCMNackFecMethod() |
| 68 | { |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 69 | // |
mikhal@google.com | 7740888 | 2011-07-22 22:05:25 +0000 | [diff] [blame] | 70 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | bool |
stefan@webrtc.org | 932ab18 | 2011-11-29 11:33:31 +0000 | [diff] [blame] | 72 | VCMNackFecMethod::ProtectionFactor(const VCMProtectionParameters* parameters) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | { |
| 74 | // Hybrid Nack FEC has three operational modes: |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 75 | // 1. Low RTT (below kLowRttNackMs) - Nack only: Set FEC rate |
stefan@webrtc.org | 932ab18 | 2011-11-29 11:33:31 +0000 | [diff] [blame] | 76 | // (_protectionFactorD) to zero. -1 means no FEC. |
| 77 | // 2. High RTT (above _highRttNackMs) - FEC Only: Keep FEC factors. |
| 78 | // -1 means always allow NACK. |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 79 | // 3. Medium RTT values - Hybrid mode: We will only nack the |
| 80 | // residual following the decoding of the FEC (refer to JB logic). FEC |
| 81 | // delta protection factor will be adjusted based on the RTT. |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 82 | |
| 83 | // Otherwise: we count on FEC; if the RTT is below a threshold, then we |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | // nack the residual, based on a decision made in the JB. |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 85 | |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 86 | // Compute the protection factors |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 87 | VCMFecMethod::ProtectionFactor(parameters); |
stefan@webrtc.org | 932ab18 | 2011-11-29 11:33:31 +0000 | [diff] [blame] | 88 | if (_lowRttNackMs == -1 || parameters->rtt < _lowRttNackMs) |
mikhal@webrtc.org | d0752c3 | 2011-10-19 15:48:30 +0000 | [diff] [blame] | 89 | { |
| 90 | _protectionFactorD = 0; |
mikhal@webrtc.org | d0752c3 | 2011-10-19 15:48:30 +0000 | [diff] [blame] | 91 | VCMFecMethod::UpdateProtectionFactorD(_protectionFactorD); |
mikhal@webrtc.org | d0752c3 | 2011-10-19 15:48:30 +0000 | [diff] [blame] | 92 | } |
mikhal@google.com | 320813c | 2011-08-03 20:47:50 +0000 | [diff] [blame] | 93 | |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 94 | // When in Hybrid mode (RTT range), adjust FEC rates based on the |
| 95 | // RTT (NACK effectiveness) - adjustment factor is in the range [0,1]. |
stefan@webrtc.org | 932ab18 | 2011-11-29 11:33:31 +0000 | [diff] [blame] | 96 | else if (_highRttNackMs == -1 || parameters->rtt < _highRttNackMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | { |
mikhal@webrtc.org | ae7a052 | 2011-10-03 22:54:34 +0000 | [diff] [blame] | 98 | // TODO(mikhal): Disabling adjustment temporarily. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 99 | // uint16_t rttIndex = (uint16_t) parameters->rtt; |
mikhal@webrtc.org | ae7a052 | 2011-10-03 22:54:34 +0000 | [diff] [blame] | 100 | float adjustRtt = 1.0f;// (float)VCMNackFecTable[rttIndex] / 100.0f; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 102 | // Adjust FEC with NACK on (for delta frame only) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | // table depends on RTT relative to rttMax (NACK Threshold) |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 104 | _protectionFactorD = static_cast<uint8_t> |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 105 | (adjustRtt * |
| 106 | static_cast<float>(_protectionFactorD)); |
mikhal@webrtc.org | d0752c3 | 2011-10-19 15:48:30 +0000 | [diff] [blame] | 107 | // update FEC rates after applying adjustment |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 108 | VCMFecMethod::UpdateProtectionFactorD(_protectionFactorD); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | } |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 110 | |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 111 | return true; |
| 112 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 114 | int VCMNackFecMethod::ComputeMaxFramesFec( |
| 115 | const VCMProtectionParameters* parameters) { |
| 116 | if (parameters->numLayers > 2) { |
| 117 | // For more than 2 temporal layers we will only have FEC on the base layer, |
| 118 | // and the base layers will be pretty far apart. Therefore we force one |
| 119 | // frame FEC. |
| 120 | return 1; |
| 121 | } |
| 122 | // We set the max number of frames to base the FEC on so that on average |
| 123 | // we will have complete frames in one RTT. Note that this is an upper |
| 124 | // bound, and that the actual number of frames used for FEC is decided by the |
| 125 | // RTP module based on the actual number of packets and the protection factor. |
| 126 | float base_layer_framerate = parameters->frameRate / |
| 127 | static_cast<float>(1 << (parameters->numLayers - 1)); |
| 128 | int max_frames_fec = std::max(static_cast<int>( |
| 129 | 2.0f * base_layer_framerate * parameters->rtt / |
| 130 | 1000.0f + 0.5f), 1); |
| 131 | // |kUpperLimitFramesFec| is the upper limit on how many frames we |
| 132 | // allow any FEC to be based on. |
| 133 | if (max_frames_fec > kUpperLimitFramesFec) { |
| 134 | max_frames_fec = kUpperLimitFramesFec; |
| 135 | } |
| 136 | return max_frames_fec; |
| 137 | } |
| 138 | |
| 139 | int VCMNackFecMethod::MaxFramesFec() const { |
| 140 | return _maxFramesFec; |
| 141 | } |
| 142 | |
marpan@webrtc.org | 88ad06b | 2012-04-20 16:05:24 +0000 | [diff] [blame] | 143 | bool VCMNackFecMethod::BitRateTooLowForFec( |
| 144 | const VCMProtectionParameters* parameters) { |
| 145 | // Bitrate below which we turn off FEC, regardless of reported packet loss. |
| 146 | // The condition should depend on resolution and content. For now, use |
| 147 | // threshold on bytes per frame, with some effect for the frame size. |
| 148 | // The condition for turning off FEC is also based on other factors, |
| 149 | // such as |_numLayers|, |_maxFramesFec|, and |_rtt|. |
| 150 | int estimate_bytes_per_frame = 1000 * BitsPerFrame(parameters) / 8; |
| 151 | int max_bytes_per_frame = kMaxBytesPerFrameForFec; |
| 152 | int num_pixels = parameters->codecWidth * parameters->codecHeight; |
| 153 | if (num_pixels <= 352 * 288) { |
| 154 | max_bytes_per_frame = kMaxBytesPerFrameForFecLow; |
| 155 | } else if (num_pixels > 640 * 480) { |
| 156 | max_bytes_per_frame = kMaxBytesPerFrameForFecHigh; |
| 157 | } |
| 158 | // TODO (marpan): add condition based on maximum frames used for FEC, |
| 159 | // and expand condition based on frame size. |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 160 | // Max round trip time threshold in ms. |
| 161 | const int64_t kMaxRttTurnOffFec = 200; |
marpan@webrtc.org | 88ad06b | 2012-04-20 16:05:24 +0000 | [diff] [blame] | 162 | if (estimate_bytes_per_frame < max_bytes_per_frame && |
| 163 | parameters->numLayers < 3 && |
| 164 | parameters->rtt < kMaxRttTurnOffFec) { |
| 165 | return true; |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 170 | bool |
stefan@webrtc.org | 932ab18 | 2011-11-29 11:33:31 +0000 | [diff] [blame] | 171 | VCMNackFecMethod::EffectivePacketLoss(const VCMProtectionParameters* parameters) |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 172 | { |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 173 | // Set the effective packet loss for encoder (based on FEC code). |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 174 | // Compute the effective packet loss and residual packet loss due to FEC. |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 175 | VCMFecMethod::EffectivePacketLoss(parameters); |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 176 | return true; |
| 177 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 179 | bool |
| 180 | VCMNackFecMethod::UpdateParameters(const VCMProtectionParameters* parameters) |
| 181 | { |
| 182 | ProtectionFactor(parameters); |
| 183 | EffectivePacketLoss(parameters); |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 184 | _maxFramesFec = ComputeMaxFramesFec(parameters); |
marpan@webrtc.org | 88ad06b | 2012-04-20 16:05:24 +0000 | [diff] [blame] | 185 | if (BitRateTooLowForFec(parameters)) { |
| 186 | _protectionFactorK = 0; |
| 187 | _protectionFactorD = 0; |
| 188 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 189 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 190 | // Protection/fec rates obtained above are defined relative to total number |
| 191 | // of packets (total rate: source + fec) FEC in RTP module assumes |
| 192 | // protection factor is defined relative to source number of packets so we |
| 193 | // should convert the factor to reduce mismatch between mediaOpt's rate and |
| 194 | // the actual one |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 195 | _protectionFactorK = VCMFecMethod::ConvertFECRate(_protectionFactorK); |
| 196 | _protectionFactorD = VCMFecMethod::ConvertFECRate(_protectionFactorD); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | |
| 198 | return true; |
| 199 | } |
| 200 | |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 201 | VCMNackMethod::VCMNackMethod(): |
| 202 | VCMProtectionMethod() |
| 203 | { |
| 204 | _type = kNack; |
| 205 | } |
| 206 | |
| 207 | VCMNackMethod::~VCMNackMethod() |
| 208 | { |
| 209 | // |
| 210 | } |
| 211 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 212 | bool |
mikhal@google.com | b29d940 | 2011-08-01 16:39:20 +0000 | [diff] [blame] | 213 | VCMNackMethod::EffectivePacketLoss(const VCMProtectionParameters* parameter) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 214 | { |
mikhal@google.com | b29d940 | 2011-08-01 16:39:20 +0000 | [diff] [blame] | 215 | // Effective Packet Loss, NA in current version. |
| 216 | _effectivePacketLoss = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
| 220 | bool |
| 221 | VCMNackMethod::UpdateParameters(const VCMProtectionParameters* parameters) |
| 222 | { |
mikhal@google.com | b29d940 | 2011-08-01 16:39:20 +0000 | [diff] [blame] | 223 | // Compute the effective packet loss |
| 224 | EffectivePacketLoss(parameters); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | |
mikhal@google.com | b29d940 | 2011-08-01 16:39:20 +0000 | [diff] [blame] | 226 | // nackCost = (bitRate - nackCost) * (lossPr) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | return true; |
| 228 | } |
| 229 | |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 230 | VCMFecMethod::VCMFecMethod(): |
| 231 | VCMProtectionMethod() |
| 232 | { |
| 233 | _type = kFec; |
| 234 | } |
| 235 | VCMFecMethod::~VCMFecMethod() |
| 236 | { |
| 237 | // |
| 238 | } |
| 239 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 240 | uint8_t |
| 241 | VCMFecMethod::BoostCodeRateKey(uint8_t packetFrameDelta, |
| 242 | uint8_t packetFrameKey) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 243 | { |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 244 | uint8_t boostRateKey = 2; |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 245 | // Default: ratio scales the FEC protection up for I frames |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 246 | uint8_t ratio = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 247 | |
| 248 | if (packetFrameDelta > 0) |
| 249 | { |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 250 | ratio = (int8_t) (packetFrameKey / packetFrameDelta); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 251 | } |
| 252 | ratio = VCM_MAX(boostRateKey, ratio); |
| 253 | |
| 254 | return ratio; |
| 255 | } |
| 256 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 257 | uint8_t |
| 258 | VCMFecMethod::ConvertFECRate(uint8_t codeRateRTP) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 259 | { |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 260 | return static_cast<uint8_t> (VCM_MIN(255,(0.5 + 255.0 * codeRateRTP / |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | (float)(255 - codeRateRTP)))); |
| 262 | } |
| 263 | |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 264 | // Update FEC with protectionFactorD |
| 265 | void |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 266 | VCMFecMethod::UpdateProtectionFactorD(uint8_t protectionFactorD) |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 267 | { |
| 268 | _protectionFactorD = protectionFactorD; |
mikhal@google.com | 679450f | 2011-08-01 22:14:58 +0000 | [diff] [blame] | 269 | } |
| 270 | |
mikhal@webrtc.org | d0752c3 | 2011-10-19 15:48:30 +0000 | [diff] [blame] | 271 | // Update FEC with protectionFactorK |
| 272 | void |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 273 | VCMFecMethod::UpdateProtectionFactorK(uint8_t protectionFactorK) |
mikhal@webrtc.org | d0752c3 | 2011-10-19 15:48:30 +0000 | [diff] [blame] | 274 | { |
| 275 | _protectionFactorK = protectionFactorK; |
| 276 | } |
| 277 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 278 | bool |
| 279 | VCMFecMethod::ProtectionFactor(const VCMProtectionParameters* parameters) |
| 280 | { |
| 281 | // FEC PROTECTION SETTINGS: varies with packet loss and bitrate |
| 282 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | // No protection if (filtered) packetLoss is 0 |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 284 | uint8_t packetLoss = (uint8_t) (255 * parameters->lossPr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 285 | if (packetLoss == 0) |
| 286 | { |
| 287 | _protectionFactorK = 0; |
| 288 | _protectionFactorD = 0; |
| 289 | return true; |
| 290 | } |
| 291 | |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 292 | // Parameters for FEC setting: |
| 293 | // first partition size, thresholds, table pars, spatial resoln fac. |
| 294 | |
| 295 | // First partition protection: ~ 20% |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 296 | uint8_t firstPartitionProt = (uint8_t) (255 * 0.20); |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 297 | |
marpan@google.com | 3f28061 | 2011-09-09 16:39:40 +0000 | [diff] [blame] | 298 | // Minimum protection level needed to generate one FEC packet for one |
| 299 | // source packet/frame (in RTP sender) |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 300 | uint8_t minProtLevelFec = 85; |
marpan@google.com | 3f28061 | 2011-09-09 16:39:40 +0000 | [diff] [blame] | 301 | |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 302 | // Threshold on packetLoss and bitRrate/frameRate (=average #packets), |
| 303 | // above which we allocate protection to cover at least first partition. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 304 | uint8_t lossThr = 0; |
| 305 | uint8_t packetNumThr = 1; |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 306 | |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 307 | // Parameters for range of rate index of table. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 308 | const uint8_t ratePar1 = 5; |
| 309 | const uint8_t ratePar2 = 49; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 310 | |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 311 | // Spatial resolution size, relative to a reference size. |
| 312 | float spatialSizeToRef = static_cast<float> |
| 313 | (parameters->codecWidth * parameters->codecHeight) / |
| 314 | (static_cast<float>(704 * 576)); |
| 315 | // resolnFac: This parameter will generally increase/decrease the FEC rate |
| 316 | // (for fixed bitRate and packetLoss) based on system size. |
| 317 | // Use a smaller exponent (< 1) to control/soften system size effect. |
ajm@google.com | b0d9f3e | 2011-07-28 18:43:18 +0000 | [diff] [blame] | 318 | const float resolnFac = 1.0 / powf(spatialSizeToRef, 0.3f); |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 319 | |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 320 | const int bitRatePerFrame = BitsPerFrame(parameters); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 321 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 323 | // Average number of packets per frame (source and fec): |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 324 | const uint8_t avgTotPackets = 1 + (uint8_t) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 325 | ((float) bitRatePerFrame * 1000.0 |
| 326 | / (float) (8.0 * _maxPayloadSize) + 0.5); |
| 327 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 328 | // FEC rate parameters: for P and I frame |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 329 | uint8_t codeRateDelta = 0; |
| 330 | uint8_t codeRateKey = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 331 | |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 332 | // Get index for table: the FEC protection depends on an effective rate. |
| 333 | // The range on the rate index corresponds to rates (bps) |
| 334 | // from ~200k to ~8000k, for 30fps |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 335 | const uint16_t effRateFecTable = static_cast<uint16_t> |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 336 | (resolnFac * bitRatePerFrame); |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 337 | uint8_t rateIndexTable = |
| 338 | (uint8_t) VCM_MAX(VCM_MIN((effRateFecTable - ratePar1) / |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 339 | ratePar1, ratePar2), 0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 340 | |
| 341 | // Restrict packet loss range to 50: |
| 342 | // current tables defined only up to 50% |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 343 | if (packetLoss >= kPacketLossMax) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 344 | { |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 345 | packetLoss = kPacketLossMax - 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 346 | } |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 347 | uint16_t indexTable = rateIndexTable * kPacketLossMax + packetLoss; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 348 | |
| 349 | // Check on table index |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 350 | assert(indexTable < kSizeCodeRateXORTable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 351 | |
| 352 | // Protection factor for P frame |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 353 | codeRateDelta = kCodeRateXORTable[indexTable]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 354 | |
| 355 | if (packetLoss > lossThr && avgTotPackets > packetNumThr) |
| 356 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 357 | // Set a minimum based on first partition size. |
| 358 | if (codeRateDelta < firstPartitionProt) |
| 359 | { |
| 360 | codeRateDelta = firstPartitionProt; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // Check limit on amount of protection for P frame; 50% is max. |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 365 | if (codeRateDelta >= kPacketLossMax) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 366 | { |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 367 | codeRateDelta = kPacketLossMax - 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 368 | } |
| 369 | |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 370 | float adjustFec = 1.0f; |
| 371 | // Avoid additional adjustments when layers are active. |
| 372 | // TODO(mikhal/marco): Update adjusmtent based on layer info. |
| 373 | if (parameters->numLayers == 1) |
| 374 | { |
| 375 | adjustFec = _qmRobustness->AdjustFecFactor(codeRateDelta, |
| 376 | parameters->bitRate, |
| 377 | parameters->frameRate, |
| 378 | parameters->rtt, |
| 379 | packetLoss); |
| 380 | } |
marpan@google.com | 59fd0f1 | 2011-07-13 17:19:49 +0000 | [diff] [blame] | 381 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 382 | codeRateDelta = static_cast<uint8_t>(codeRateDelta * adjustFec); |
marpan@google.com | 86548c6 | 2011-07-12 17:12:57 +0000 | [diff] [blame] | 383 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 384 | // For Key frame: |
| 385 | // Effectively at a higher rate, so we scale/boost the rate |
| 386 | // The boost factor may depend on several factors: ratio of packet |
| 387 | // number of I to P frames, how much protection placed on P frames, etc. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 388 | const uint8_t packetFrameDelta = (uint8_t) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 389 | (0.5 + parameters->packetsPerFrame); |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 390 | const uint8_t packetFrameKey = (uint8_t) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 391 | (0.5 + parameters->packetsPerFrameKey); |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 392 | const uint8_t boostKey = BoostCodeRateKey(packetFrameDelta, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 393 | packetFrameKey); |
| 394 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 395 | rateIndexTable = (uint8_t) VCM_MAX(VCM_MIN( |
marpan@google.com | 11d986a | 2011-07-28 17:42:57 +0000 | [diff] [blame] | 396 | 1 + (boostKey * effRateFecTable - ratePar1) / |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 397 | ratePar1,ratePar2),0); |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 398 | uint16_t indexTableKey = rateIndexTable * kPacketLossMax + packetLoss; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 399 | |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 400 | indexTableKey = VCM_MIN(indexTableKey, kSizeCodeRateXORTable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 401 | |
| 402 | // Check on table index |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 403 | assert(indexTableKey < kSizeCodeRateXORTable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 404 | |
| 405 | // Protection factor for I frame |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 406 | codeRateKey = kCodeRateXORTable[indexTableKey]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 407 | |
| 408 | // Boosting for Key frame. |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 409 | int boostKeyProt = _scaleProtKey * codeRateDelta; |
| 410 | if (boostKeyProt >= kPacketLossMax) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 411 | { |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 412 | boostKeyProt = kPacketLossMax - 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // Make sure I frame protection is at least larger than P frame protection, |
| 416 | // and at least as high as filtered packet loss. |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 417 | codeRateKey = static_cast<uint8_t> (VCM_MAX(packetLoss, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 418 | VCM_MAX(boostKeyProt, codeRateKey))); |
| 419 | |
| 420 | // Check limit on amount of protection for I frame: 50% is max. |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 421 | if (codeRateKey >= kPacketLossMax) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 422 | { |
andrew@webrtc.org | 413b993 | 2011-09-02 22:03:56 +0000 | [diff] [blame] | 423 | codeRateKey = kPacketLossMax - 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | _protectionFactorK = codeRateKey; |
| 427 | _protectionFactorD = codeRateDelta; |
| 428 | |
marpan@google.com | 191b780 | 2011-08-01 19:59:57 +0000 | [diff] [blame] | 429 | // Generally there is a rate mis-match between the FEC cost estimated |
| 430 | // in mediaOpt and the actual FEC cost sent out in RTP module. |
| 431 | // This is more significant at low rates (small # of source packets), where |
| 432 | // the granularity of the FEC decreases. In this case, non-zero protection |
| 433 | // in mediaOpt may generate 0 FEC packets in RTP sender (since actual #FEC |
| 434 | // is based on rounding off protectionFactor on actual source packet number). |
| 435 | // The correction factor (_corrFecCost) attempts to corrects this, at least |
marpan@google.com | 3f28061 | 2011-09-09 16:39:40 +0000 | [diff] [blame] | 436 | // for cases of low rates (small #packets) and low protection levels. |
marpan@google.com | f1f3fb3 | 2011-10-03 19:09:45 +0000 | [diff] [blame] | 437 | |
| 438 | float numPacketsFl = 1.0f + ((float) bitRatePerFrame * 1000.0 |
| 439 | / (float) (8.0 * _maxPayloadSize) + 0.5); |
| 440 | |
marpan@google.com | 191b780 | 2011-08-01 19:59:57 +0000 | [diff] [blame] | 441 | const float estNumFecGen = 0.5f + static_cast<float> (_protectionFactorD * |
marpan@google.com | f1f3fb3 | 2011-10-03 19:09:45 +0000 | [diff] [blame] | 442 | numPacketsFl / 255.0f); |
| 443 | |
| 444 | |
marpan@google.com | 3f28061 | 2011-09-09 16:39:40 +0000 | [diff] [blame] | 445 | // We reduce cost factor (which will reduce overhead for FEC and |
marpan@google.com | 191b780 | 2011-08-01 19:59:57 +0000 | [diff] [blame] | 446 | // hybrid method) and not the protectionFactor. |
| 447 | _corrFecCost = 1.0f; |
marpan@google.com | f1f3fb3 | 2011-10-03 19:09:45 +0000 | [diff] [blame] | 448 | if (estNumFecGen < 1.1f && _protectionFactorD < minProtLevelFec) |
marpan@google.com | 191b780 | 2011-08-01 19:59:57 +0000 | [diff] [blame] | 449 | { |
| 450 | _corrFecCost = 0.5f; |
| 451 | } |
marpan@google.com | 3f28061 | 2011-09-09 16:39:40 +0000 | [diff] [blame] | 452 | if (estNumFecGen < 0.9f && _protectionFactorD < minProtLevelFec) |
marpan@google.com | 191b780 | 2011-08-01 19:59:57 +0000 | [diff] [blame] | 453 | { |
| 454 | _corrFecCost = 0.0f; |
| 455 | } |
marpan@google.com | 86548c6 | 2011-07-12 17:12:57 +0000 | [diff] [blame] | 456 | |
marpan@google.com | 59fd0f1 | 2011-07-13 17:19:49 +0000 | [diff] [blame] | 457 | // TODO (marpan): Set the UEP protection on/off for Key and Delta frames |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 458 | _useUepProtectionK = _qmRobustness->SetUepProtection(codeRateKey, |
| 459 | parameters->bitRate, |
| 460 | packetLoss, |
| 461 | 0); |
marpan@google.com | 86548c6 | 2011-07-12 17:12:57 +0000 | [diff] [blame] | 462 | |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 463 | _useUepProtectionD = _qmRobustness->SetUepProtection(codeRateDelta, |
| 464 | parameters->bitRate, |
| 465 | packetLoss, |
| 466 | 1); |
marpan@google.com | 86548c6 | 2011-07-12 17:12:57 +0000 | [diff] [blame] | 467 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 468 | // DONE WITH FEC PROTECTION SETTINGS |
| 469 | return true; |
| 470 | } |
| 471 | |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 472 | int VCMFecMethod::BitsPerFrame(const VCMProtectionParameters* parameters) { |
| 473 | // When temporal layers are available FEC will only be applied on the base |
| 474 | // layer. |
| 475 | const float bitRateRatio = |
| 476 | kVp8LayerRateAlloction[parameters->numLayers - 1][0]; |
thakis@chromium.org | 65bc254 | 2012-08-13 19:26:12 +0000 | [diff] [blame] | 477 | float frameRateRatio = powf(1 / 2.0, parameters->numLayers - 1); |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 478 | float bitRate = parameters->bitRate * bitRateRatio; |
| 479 | float frameRate = parameters->frameRate * frameRateRatio; |
| 480 | |
| 481 | // TODO(mikhal): Update factor following testing. |
| 482 | float adjustmentFactor = 1; |
| 483 | |
| 484 | // Average bits per frame (units of kbits) |
| 485 | return static_cast<int>(adjustmentFactor * bitRate / frameRate); |
| 486 | } |
| 487 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 488 | bool |
| 489 | VCMFecMethod::EffectivePacketLoss(const VCMProtectionParameters* parameters) |
| 490 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 491 | // Effective packet loss to encoder is based on RPL (residual packet loss) |
| 492 | // this is a soft setting based on degree of FEC protection |
| 493 | // RPL = received/input packet loss - average_FEC_recovery |
| 494 | // note: received/input packet loss may be filtered based on FilteredLoss |
| 495 | |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 496 | // Effective Packet Loss, NA in current version. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 497 | _effectivePacketLoss = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 498 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 499 | return true; |
| 500 | } |
| 501 | |
| 502 | bool |
| 503 | VCMFecMethod::UpdateParameters(const VCMProtectionParameters* parameters) |
| 504 | { |
| 505 | // Compute the protection factor |
| 506 | ProtectionFactor(parameters); |
| 507 | |
| 508 | // Compute the effective packet loss |
| 509 | EffectivePacketLoss(parameters); |
| 510 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 511 | // Protection/fec rates obtained above is defined relative to total number |
| 512 | // of packets (total rate: source+fec) FEC in RTP module assumes protection |
| 513 | // factor is defined relative to source number of packets so we should |
| 514 | // convert the factor to reduce mismatch between mediaOpt suggested rate and |
| 515 | // the actual rate |
| 516 | _protectionFactorK = ConvertFECRate(_protectionFactorK); |
| 517 | _protectionFactorD = ConvertFECRate(_protectionFactorD); |
| 518 | |
| 519 | return true; |
| 520 | } |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 521 | VCMLossProtectionLogic::VCMLossProtectionLogic(int64_t nowMs): |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 522 | _currentParameters(), |
| 523 | _rtt(0), |
| 524 | _lossPr(0.0f), |
| 525 | _bitRate(0.0f), |
| 526 | _frameRate(0.0f), |
| 527 | _keyFrameSize(0.0f), |
| 528 | _fecRateKey(0), |
| 529 | _fecRateDelta(0), |
| 530 | _lastPrUpdateT(0), |
| 531 | _lossPr255(0.9999f), |
| 532 | _lossPrHistory(), |
| 533 | _shortMaxLossPr255(0), |
| 534 | _packetsPerFrame(0.9999f), |
| 535 | _packetsPerFrameKey(0.9999f), |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 536 | _codecWidth(0), |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 537 | _codecHeight(0), |
| 538 | _numLayers(1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 539 | { |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 540 | Reset(nowMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 541 | } |
| 542 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 543 | VCMLossProtectionLogic::~VCMLossProtectionLogic() |
| 544 | { |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 545 | Release(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 546 | } |
| 547 | |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 548 | void VCMLossProtectionLogic::SetMethod( |
| 549 | enum VCMProtectionMethodEnum newMethodType) { |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 550 | if (_selectedMethod && _selectedMethod->Type() == newMethodType) |
| 551 | return; |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 552 | |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 553 | switch(newMethodType) { |
| 554 | case kNack: |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 555 | _selectedMethod.reset(new VCMNackMethod()); |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 556 | break; |
| 557 | case kFec: |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 558 | _selectedMethod.reset(new VCMFecMethod()); |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 559 | break; |
| 560 | case kNackFec: |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 561 | _selectedMethod.reset(new VCMNackFecMethod(kLowRttNackMs, -1)); |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 562 | break; |
| 563 | case kNone: |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 564 | _selectedMethod.reset(); |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 565 | break; |
| 566 | } |
| 567 | UpdateMethod(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 568 | } |
| 569 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 570 | void |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 571 | VCMLossProtectionLogic::UpdateRtt(int64_t rtt) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 572 | { |
| 573 | _rtt = rtt; |
| 574 | } |
| 575 | |
| 576 | void |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 577 | VCMLossProtectionLogic::UpdateMaxLossHistory(uint8_t lossPr255, |
| 578 | int64_t now) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 579 | { |
| 580 | if (_lossPrHistory[0].timeMs >= 0 && |
| 581 | now - _lossPrHistory[0].timeMs < kLossPrShortFilterWinMs) |
| 582 | { |
| 583 | if (lossPr255 > _shortMaxLossPr255) |
| 584 | { |
| 585 | _shortMaxLossPr255 = lossPr255; |
| 586 | } |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | // Only add a new value to the history once a second |
| 591 | if (_lossPrHistory[0].timeMs == -1) |
| 592 | { |
| 593 | // First, no shift |
| 594 | _shortMaxLossPr255 = lossPr255; |
| 595 | } |
| 596 | else |
| 597 | { |
| 598 | // Shift |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 599 | for (int32_t i = (kLossPrHistorySize - 2); i >= 0; i--) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 600 | { |
| 601 | _lossPrHistory[i + 1].lossPr255 = _lossPrHistory[i].lossPr255; |
| 602 | _lossPrHistory[i + 1].timeMs = _lossPrHistory[i].timeMs; |
| 603 | } |
| 604 | } |
| 605 | if (_shortMaxLossPr255 == 0) |
| 606 | { |
| 607 | _shortMaxLossPr255 = lossPr255; |
| 608 | } |
| 609 | |
| 610 | _lossPrHistory[0].lossPr255 = _shortMaxLossPr255; |
| 611 | _lossPrHistory[0].timeMs = now; |
| 612 | _shortMaxLossPr255 = 0; |
| 613 | } |
| 614 | } |
| 615 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 616 | uint8_t |
| 617 | VCMLossProtectionLogic::MaxFilteredLossPr(int64_t nowMs) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 618 | { |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 619 | uint8_t maxFound = _shortMaxLossPr255; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 620 | if (_lossPrHistory[0].timeMs == -1) |
| 621 | { |
| 622 | return maxFound; |
| 623 | } |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 624 | for (int32_t i = 0; i < kLossPrHistorySize; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 625 | { |
| 626 | if (_lossPrHistory[i].timeMs == -1) |
| 627 | { |
| 628 | break; |
| 629 | } |
| 630 | if (nowMs - _lossPrHistory[i].timeMs > |
| 631 | kLossPrHistorySize * kLossPrShortFilterWinMs) |
| 632 | { |
| 633 | // This sample (and all samples after this) is too old |
| 634 | break; |
| 635 | } |
| 636 | if (_lossPrHistory[i].lossPr255 > maxFound) |
| 637 | { |
| 638 | // This sample is the largest one this far into the history |
| 639 | maxFound = _lossPrHistory[i].lossPr255; |
| 640 | } |
| 641 | } |
| 642 | return maxFound; |
| 643 | } |
| 644 | |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 645 | uint8_t VCMLossProtectionLogic::FilteredLoss( |
marpan@webrtc.org | 2dad3fb | 2012-01-09 18:18:36 +0000 | [diff] [blame] | 646 | int64_t nowMs, |
| 647 | FilterPacketLossMode filter_mode, |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 648 | uint8_t lossPr255) { |
marpan@webrtc.org | 2dad3fb | 2012-01-09 18:18:36 +0000 | [diff] [blame] | 649 | |
| 650 | // Update the max window filter. |
| 651 | UpdateMaxLossHistory(lossPr255, nowMs); |
| 652 | |
| 653 | // Update the recursive average filter. |
| 654 | _lossPr255.Apply(static_cast<float> (nowMs - _lastPrUpdateT), |
| 655 | static_cast<float> (lossPr255)); |
| 656 | _lastPrUpdateT = nowMs; |
| 657 | |
| 658 | // Filtered loss: default is received loss (no filtering). |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 659 | uint8_t filtered_loss = lossPr255; |
marpan@webrtc.org | 2dad3fb | 2012-01-09 18:18:36 +0000 | [diff] [blame] | 660 | |
| 661 | switch (filter_mode) { |
| 662 | case kNoFilter: |
| 663 | break; |
| 664 | case kAvgFilter: |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 665 | filtered_loss = static_cast<uint8_t>(_lossPr255.filtered() + 0.5); |
marpan@webrtc.org | 2dad3fb | 2012-01-09 18:18:36 +0000 | [diff] [blame] | 666 | break; |
| 667 | case kMaxFilter: |
| 668 | filtered_loss = MaxFilteredLossPr(nowMs); |
| 669 | break; |
| 670 | } |
| 671 | |
| 672 | return filtered_loss; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | void |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 676 | VCMLossProtectionLogic::UpdateFilteredLossPr(uint8_t packetLossEnc) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 677 | { |
| 678 | _lossPr = (float) packetLossEnc / (float) 255.0; |
| 679 | } |
| 680 | |
| 681 | void |
| 682 | VCMLossProtectionLogic::UpdateBitRate(float bitRate) |
| 683 | { |
| 684 | _bitRate = bitRate; |
| 685 | } |
| 686 | |
| 687 | void |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 688 | VCMLossProtectionLogic::UpdatePacketsPerFrame(float nPackets, int64_t nowMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 689 | { |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 690 | _packetsPerFrame.Apply(static_cast<float>(nowMs - _lastPacketPerFrameUpdateT), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 691 | nPackets); |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 692 | _lastPacketPerFrameUpdateT = nowMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | void |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 696 | VCMLossProtectionLogic::UpdatePacketsPerFrameKey(float nPackets, int64_t nowMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 697 | { |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 698 | _packetsPerFrameKey.Apply(static_cast<float>(nowMs - |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 699 | _lastPacketPerFrameUpdateTKey), nPackets); |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 700 | _lastPacketPerFrameUpdateTKey = nowMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | void |
| 704 | VCMLossProtectionLogic::UpdateKeyFrameSize(float keyFrameSize) |
| 705 | { |
| 706 | _keyFrameSize = keyFrameSize; |
| 707 | } |
| 708 | |
| 709 | void |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 710 | VCMLossProtectionLogic::UpdateFrameSize(uint16_t width, |
| 711 | uint16_t height) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 712 | { |
| 713 | _codecWidth = width; |
| 714 | _codecHeight = height; |
| 715 | } |
| 716 | |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 717 | void VCMLossProtectionLogic::UpdateNumLayers(int numLayers) { |
| 718 | _numLayers = (numLayers == 0) ? 1 : numLayers; |
| 719 | } |
| 720 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 721 | bool |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 722 | VCMLossProtectionLogic::UpdateMethod() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 723 | { |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 724 | if (!_selectedMethod) |
| 725 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 726 | _currentParameters.rtt = _rtt; |
| 727 | _currentParameters.lossPr = _lossPr; |
| 728 | _currentParameters.bitRate = _bitRate; |
| 729 | _currentParameters.frameRate = _frameRate; // rename actual frame rate? |
| 730 | _currentParameters.keyFrameSize = _keyFrameSize; |
| 731 | _currentParameters.fecRateDelta = _fecRateDelta; |
| 732 | _currentParameters.fecRateKey = _fecRateKey; |
minyue@webrtc.org | 74aaf29 | 2014-07-16 21:28:26 +0000 | [diff] [blame] | 733 | _currentParameters.packetsPerFrame = _packetsPerFrame.filtered(); |
| 734 | _currentParameters.packetsPerFrameKey = _packetsPerFrameKey.filtered(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 735 | _currentParameters.codecWidth = _codecWidth; |
| 736 | _currentParameters.codecHeight = _codecHeight; |
mikhal@webrtc.org | 0e7d9d8 | 2011-12-19 19:04:49 +0000 | [diff] [blame] | 737 | _currentParameters.numLayers = _numLayers; |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 738 | return _selectedMethod->UpdateParameters(&_currentParameters); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | VCMProtectionMethod* |
| 742 | VCMLossProtectionLogic::SelectedMethod() const |
| 743 | { |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 744 | return _selectedMethod.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 745 | } |
| 746 | |
pbos@webrtc.org | cade82c | 2015-03-12 10:39:24 +0000 | [diff] [blame] | 747 | VCMProtectionMethodEnum VCMLossProtectionLogic::SelectedType() const { |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 748 | return _selectedMethod ? _selectedMethod->Type() : kNone; |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 749 | } |
| 750 | |
mikhal@google.com | 022716b | 2011-07-20 23:12:57 +0000 | [diff] [blame] | 751 | void |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 752 | VCMLossProtectionLogic::Reset(int64_t nowMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 753 | { |
henrik.lundin@webrtc.org | 7d8c72e | 2011-12-21 15:24:01 +0000 | [diff] [blame] | 754 | _lastPrUpdateT = nowMs; |
| 755 | _lastPacketPerFrameUpdateT = nowMs; |
| 756 | _lastPacketPerFrameUpdateTKey = nowMs; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 757 | _lossPr255.Reset(0.9999f); |
| 758 | _packetsPerFrame.Reset(0.9999f); |
| 759 | _fecRateDelta = _fecRateKey = 0; |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 760 | for (int32_t i = 0; i < kLossPrHistorySize; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 761 | { |
| 762 | _lossPrHistory[i].lossPr255 = 0; |
| 763 | _lossPrHistory[i].timeMs = -1; |
| 764 | } |
| 765 | _shortMaxLossPr255 = 0; |
mikhal@webrtc.org | a057a95 | 2011-08-26 21:17:34 +0000 | [diff] [blame] | 766 | Release(); |
| 767 | } |
| 768 | |
pbos | ba8c15b | 2015-07-14 09:36:34 -0700 | [diff] [blame] | 769 | void VCMLossProtectionLogic::Release() { |
| 770 | _selectedMethod.reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 771 | } |
| 772 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 773 | } // namespace media_optimization |
| 774 | } // namespace webrtc |