Replace c-style cast and constrain value in VCMFecMethod::ProtectionFactor.

BUG=None

Review-Url: https://codereview.webrtc.org/2501083003
Cr-Commit-Position: refs/heads/master@{#15116}
diff --git a/webrtc/modules/video_coding/media_opt_util.cc b/webrtc/modules/video_coding/media_opt_util.cc
index 24acfe9..ed926e9 100644
--- a/webrtc/modules/video_coding/media_opt_util.cc
+++ b/webrtc/modules/video_coding/media_opt_util.cc
@@ -15,6 +15,7 @@
 #include <math.h>
 
 #include <algorithm>
+#include <limits>
 
 #include "webrtc/modules/include/module_common_types.h"
 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h"
@@ -244,7 +245,7 @@
   // FEC PROTECTION SETTINGS: varies with packet loss and bitrate
 
   // No protection if (filtered) packetLoss is 0
-  uint8_t packetLoss = (uint8_t)(255 * parameters->lossPr);
+  uint8_t packetLoss = static_cast<uint8_t>(255 * parameters->lossPr);
   if (packetLoss == 0) {
     _protectionFactorK = 0;
     _protectionFactorD = 0;
@@ -255,7 +256,7 @@
   // first partition size, thresholds, table pars, spatial resoln fac.
 
   // First partition protection: ~ 20%
-  uint8_t firstPartitionProt = (uint8_t)(255 * 0.20);
+  uint8_t firstPartitionProt = static_cast<uint8_t>(255 * 0.20);
 
   // Minimum protection level needed to generate one FEC packet for one
   // source packet/frame (in RTP sender)
@@ -282,10 +283,11 @@
   const int bitRatePerFrame = BitsPerFrame(parameters);
 
   // Average number of packets per frame (source and fec):
-  const uint8_t avgTotPackets =
-      1 + (uint8_t)(static_cast<float>(bitRatePerFrame) * 1000.0 /
-                        static_cast<float>(8.0 * _maxPayloadSize) +
-                    0.5);
+  const uint8_t avgTotPackets = static_cast<uint8_t>(
+      std::min(static_cast<float>(std::numeric_limits<uint8_t>::max()),
+               1.5f +
+                   static_cast<float>(bitRatePerFrame) * 1000.0f /
+                       static_cast<float>(8.0 * _maxPayloadSize)));
 
   // FEC rate parameters: for P and I frame
   uint8_t codeRateDelta = 0;
@@ -296,8 +298,8 @@
   // from ~200k to ~8000k, for 30fps
   const uint16_t effRateFecTable =
       static_cast<uint16_t>(resolnFac * bitRatePerFrame);
-  uint8_t rateIndexTable = (uint8_t)VCM_MAX(
-      VCM_MIN((effRateFecTable - ratePar1) / ratePar1, ratePar2), 0);
+  uint8_t rateIndexTable = static_cast<uint8_t>(
+      VCM_MAX(VCM_MIN((effRateFecTable - ratePar1) / ratePar1, ratePar2), 0));
 
   // Restrict packet loss range to 50:
   // current tables defined only up to 50%
@@ -328,14 +330,15 @@
   // Effectively at a higher rate, so we scale/boost the rate
   // The boost factor may depend on several factors: ratio of packet
   // number of I to P frames, how much protection placed on P frames, etc.
-  const uint8_t packetFrameDelta = (uint8_t)(0.5 + parameters->packetsPerFrame);
+  const uint8_t packetFrameDelta =
+      static_cast<uint8_t>(0.5 + parameters->packetsPerFrame);
   const uint8_t packetFrameKey =
-      (uint8_t)(0.5 + parameters->packetsPerFrameKey);
+      static_cast<uint8_t>(0.5 + parameters->packetsPerFrameKey);
   const uint8_t boostKey = BoostCodeRateKey(packetFrameDelta, packetFrameKey);
 
-  rateIndexTable = (uint8_t)VCM_MAX(
+  rateIndexTable = static_cast<uint8_t>(VCM_MAX(
       VCM_MIN(1 + (boostKey * effRateFecTable - ratePar1) / ratePar1, ratePar2),
-      0);
+      0));
   uint16_t indexTableKey = rateIndexTable * kPacketLossMax + packetLoss;
 
   indexTableKey = VCM_MIN(indexTableKey, kSizeCodeRateXORTable);