Calculate bitrate and frame rate mismatches in video codec tests

Bug: webrtc:10812
Change-Id: I3408c0d7adefc37d088a5c6e10fce4f95aa1b668
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/228943
Commit-Queue: Sergey Silkin <ssilkin@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34782}
diff --git a/api/test/videocodec_test_stats.cc b/api/test/videocodec_test_stats.cc
index b973dc2..0cf00da 100644
--- a/api/test/videocodec_test_stats.cc
+++ b/api/test/videocodec_test_stats.cc
@@ -91,6 +91,9 @@
   map["max_delta_frame_delay_sec"] = std::to_string(max_delta_frame_delay_sec);
   map["time_to_reach_target_bitrate_sec"] =
       std::to_string(time_to_reach_target_bitrate_sec);
+  map["avg_bitrate_mismatch_pct"] = std::to_string(avg_bitrate_mismatch_pct);
+  map["avg_framerate_mismatch_pct"] =
+      std::to_string(avg_framerate_mismatch_pct);
   map["avg_key_frame_size_bytes"] = std::to_string(avg_key_frame_size_bytes);
   map["avg_delta_frame_size_bytes"] =
       std::to_string(avg_delta_frame_size_bytes);
diff --git a/api/test/videocodec_test_stats.h b/api/test/videocodec_test_stats.h
index 02a18a7..3f86233 100644
--- a/api/test/videocodec_test_stats.h
+++ b/api/test/videocodec_test_stats.h
@@ -105,6 +105,8 @@
     float max_key_frame_delay_sec = 0.0f;
     float max_delta_frame_delay_sec = 0.0f;
     float time_to_reach_target_bitrate_sec = 0.0f;
+    float avg_bitrate_mismatch_pct = 0.0f;
+    float avg_framerate_mismatch_pct = 0.0f;
 
     float avg_key_frame_size_bytes = 0.0f;
     float avg_delta_frame_size_bytes = 0.0f;
diff --git a/modules/video_coding/codecs/test/videocodec_test_stats_impl.cc b/modules/video_coding/codecs/test/videocodec_test_stats_impl.cc
index 914f639..07c3308 100644
--- a/modules/video_coding/codecs/test/videocodec_test_stats_impl.cc
+++ b/modules/video_coding/codecs/test/videocodec_test_stats_impl.cc
@@ -202,6 +202,7 @@
   const size_t target_bitrate_kbps =
       CalcLayerTargetBitrateKbps(first_frame_num, last_frame_num, spatial_idx,
                                  temporal_idx, aggregate_independent_layers);
+  const size_t target_bitrate_bps = 1000 * target_bitrate_kbps;
   RTC_CHECK_GT(target_bitrate_kbps, 0);  // We divide by `target_bitrate_kbps`.
 
   for (size_t frame_num = first_frame_num; frame_num <= last_frame_num;
@@ -313,8 +314,8 @@
   video_stat.temporal_idx = temporal_idx;
 
   RTC_CHECK_GT(duration_sec, 0);
-  video_stat.bitrate_kbps =
-      static_cast<size_t>(8 * video_stat.length_bytes / 1000 / duration_sec);
+  const float bitrate_bps = 8 * video_stat.length_bytes / duration_sec;
+  video_stat.bitrate_kbps = static_cast<size_t>((bitrate_bps + 500) / 1000);
   video_stat.framerate_fps = video_stat.num_encoded_frames / duration_sec;
 
   // http://bugs.webrtc.org/10400: On Windows, we only get millisecond
@@ -340,6 +341,12 @@
   video_stat.max_key_frame_delay_sec = MaxDelaySec(key_frame_size_bytes);
   video_stat.max_delta_frame_delay_sec = MaxDelaySec(key_frame_size_bytes);
 
+  video_stat.avg_bitrate_mismatch_pct =
+      100 * (bitrate_bps - target_bitrate_bps) / target_bitrate_bps;
+  video_stat.avg_framerate_mismatch_pct =
+      100 * (video_stat.framerate_fps - input_framerate_fps) /
+      input_framerate_fps;
+
   video_stat.avg_key_frame_size_bytes =
       key_frame_size_bytes.GetMean().value_or(0);
   video_stat.avg_delta_frame_size_bytes =