Introduce ChainDiffCalculator

to convert flags which chains a video frame part of into chain_diffs

Bug: webrtc:10342
Change-Id: I6fb899eae934078223b101c9f85e2ac101980d4c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/175108
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31306}
diff --git a/modules/video_coding/BUILD.gn b/modules/video_coding/BUILD.gn
index b143839..028b0d5 100644
--- a/modules/video_coding/BUILD.gn
+++ b/modules/video_coding/BUILD.gn
@@ -35,6 +35,20 @@
   ]
 }
 
+rtc_library("chain_diff_calculator") {
+  sources = [
+    "chain_diff_calculator.cc",
+    "chain_diff_calculator.h",
+  ]
+
+  deps = [
+    "../../rtc_base:checks",
+    "../../rtc_base:logging",
+    "//third_party/abseil-cpp/absl/container:inlined_vector",
+    "//third_party/abseil-cpp/absl/types:optional",
+  ]
+}
+
 rtc_library("frame_dependencies_calculator") {
   sources = [
     "frame_dependencies_calculator.cc",
@@ -842,6 +856,7 @@
     testonly = true
 
     sources = [
+      "chain_diff_calculator_unittest.cc",
       "codecs/test/videocodec_test_fixture_config_unittest.cc",
       "codecs/test/videocodec_test_stats_impl_unittest.cc",
       "codecs/test/videoprocessor_unittest.cc",
@@ -888,6 +903,7 @@
     }
 
     deps = [
+      ":chain_diff_calculator",
       ":codec_globals_headers",
       ":encoded_frame",
       ":frame_dependencies_calculator",
diff --git a/modules/video_coding/chain_diff_calculator.cc b/modules/video_coding/chain_diff_calculator.cc
new file mode 100644
index 0000000..5f85271
--- /dev/null
+++ b/modules/video_coding/chain_diff_calculator.cc
@@ -0,0 +1,62 @@
+/*
+ *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+#include "modules/video_coding/chain_diff_calculator.h"
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <algorithm>
+#include <vector>
+
+#include "absl/container/inlined_vector.h"
+#include "absl/types/optional.h"
+#include "rtc_base/logging.h"
+
+namespace webrtc {
+
+void ChainDiffCalculator::Reset(const std::vector<bool>& chains) {
+  last_frame_in_chain_.resize(chains.size());
+  for (size_t i = 0; i < chains.size(); ++i) {
+    if (chains[i]) {
+      last_frame_in_chain_[i] = absl::nullopt;
+    }
+  }
+}
+
+absl::InlinedVector<int, 4> ChainDiffCalculator::ChainDiffs(
+    int64_t frame_id) const {
+  absl::InlinedVector<int, 4> result;
+  result.reserve(last_frame_in_chain_.size());
+  for (const auto& frame_id_in_chain : last_frame_in_chain_) {
+    result.push_back(frame_id_in_chain ? (frame_id - *frame_id_in_chain) : 0);
+  }
+  return result;
+}
+
+absl::InlinedVector<int, 4> ChainDiffCalculator::From(
+    int64_t frame_id,
+    const std::vector<bool>& chains) {
+  auto result = ChainDiffs(frame_id);
+  if (chains.size() != last_frame_in_chain_.size()) {
+    RTC_LOG(LS_ERROR) << "Insconsistent chain configuration for frame#"
+                      << frame_id << ": expected "
+                      << last_frame_in_chain_.size() << " chains, found "
+                      << chains.size();
+  }
+  size_t num_chains = std::min(last_frame_in_chain_.size(), chains.size());
+  for (size_t i = 0; i < num_chains; ++i) {
+    if (chains[i]) {
+      last_frame_in_chain_[i] = frame_id;
+    }
+  }
+  return result;
+}
+
+}  // namespace webrtc
diff --git a/modules/video_coding/chain_diff_calculator.h b/modules/video_coding/chain_diff_calculator.h
new file mode 100644
index 0000000..bca7340
--- /dev/null
+++ b/modules/video_coding/chain_diff_calculator.h
@@ -0,0 +1,46 @@
+/*
+ *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef MODULES_VIDEO_CODING_CHAIN_DIFF_CALCULATOR_H_
+#define MODULES_VIDEO_CODING_CHAIN_DIFF_CALCULATOR_H_
+
+#include <stdint.h>
+
+#include <vector>
+
+#include "absl/container/inlined_vector.h"
+#include "absl/types/optional.h"
+
+namespace webrtc {
+
+// This class is thread compatible.
+class ChainDiffCalculator {
+ public:
+  ChainDiffCalculator() = default;
+  ChainDiffCalculator(const ChainDiffCalculator&) = default;
+  ChainDiffCalculator& operator=(const ChainDiffCalculator&) = default;
+
+  // Restarts chains, i.e. for position where chains[i] == true next chain_diff
+  // will be 0. Saves chains.size() as number of chains in the stream.
+  void Reset(const std::vector<bool>& chains);
+
+  // Returns chain diffs based on flags if frame is part of the chain.
+  absl::InlinedVector<int, 4> From(int64_t frame_id,
+                                   const std::vector<bool>& chains);
+
+ private:
+  absl::InlinedVector<int, 4> ChainDiffs(int64_t frame_id) const;
+
+  absl::InlinedVector<absl::optional<int64_t>, 4> last_frame_in_chain_;
+};
+
+}  // namespace webrtc
+
+#endif  // MODULES_VIDEO_CODING_CHAIN_DIFF_CALCULATOR_H_
diff --git a/modules/video_coding/chain_diff_calculator_unittest.cc b/modules/video_coding/chain_diff_calculator_unittest.cc
new file mode 100644
index 0000000..efd09bd
--- /dev/null
+++ b/modules/video_coding/chain_diff_calculator_unittest.cc
@@ -0,0 +1,126 @@
+/*
+ *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "modules/video_coding/chain_diff_calculator.h"
+
+#include "test/gmock.h"
+#include "test/gtest.h"
+
+namespace webrtc {
+namespace {
+
+using ::testing::ElementsAre;
+using ::testing::SizeIs;
+
+TEST(ChainDiffCalculatorTest, SingleChain) {
+  // Simulate  a stream with 2 temporal layer where chain
+  // protects temporal layer 0.
+  ChainDiffCalculator calculator;
+  // Key frame.
+  calculator.Reset({true});
+  EXPECT_THAT(calculator.From(1, {true}), ElementsAre(0));
+  // T1 delta frame.
+  EXPECT_THAT(calculator.From(2, {false}), ElementsAre(1));
+  // T0 delta frame.
+  EXPECT_THAT(calculator.From(3, {true}), ElementsAre(2));
+}
+
+TEST(ChainDiffCalculatorTest, TwoChainsFullSvc) {
+  // Simulate a full svc stream with 2 spatial and 2 temporal layers.
+  // chains are protecting temporal layers 0.
+  ChainDiffCalculator calculator;
+  // S0 Key frame.
+  calculator.Reset({true, true});
+  EXPECT_THAT(calculator.From(1, {true, true}), ElementsAre(0, 0));
+  // S1 Key frame.
+  EXPECT_THAT(calculator.From(2, {false, true}), ElementsAre(1, 1));
+  // S0T1 delta frame.
+  EXPECT_THAT(calculator.From(3, {false, false}), ElementsAre(2, 1));
+  // S1T1 delta frame.
+  EXPECT_THAT(calculator.From(4, {false, false}), ElementsAre(3, 2));
+  // S0T0 delta frame.
+  EXPECT_THAT(calculator.From(5, {true, true}), ElementsAre(4, 3));
+  // S1T0 delta frame.
+  EXPECT_THAT(calculator.From(6, {false, true}), ElementsAre(1, 1));
+}
+
+TEST(ChainDiffCalculatorTest, TwoChainsKSvc) {
+  // Simulate a k-svc stream with 2 spatial and 2 temporal layers.
+  // chains are protecting temporal layers 0.
+  ChainDiffCalculator calculator;
+  // S0 Key frame.
+  calculator.Reset({true, true});
+  EXPECT_THAT(calculator.From(1, {true, true}), ElementsAre(0, 0));
+  // S1 Key frame.
+  EXPECT_THAT(calculator.From(2, {false, true}), ElementsAre(1, 1));
+  // S0T1 delta frame.
+  EXPECT_THAT(calculator.From(3, {false, false}), ElementsAre(2, 1));
+  // S1T1 delta frame.
+  EXPECT_THAT(calculator.From(4, {false, false}), ElementsAre(3, 2));
+  // S0T0 delta frame.
+  EXPECT_THAT(calculator.From(5, {true, false}), ElementsAre(4, 3));
+  // S1T0 delta frame.
+  EXPECT_THAT(calculator.From(6, {false, true}), ElementsAre(1, 4));
+}
+
+TEST(ChainDiffCalculatorTest, TwoChainsSimulcast) {
+  // Simulate a k-svc stream with 2 spatial and 2 temporal layers.
+  // chains are protecting temporal layers 0.
+  ChainDiffCalculator calculator;
+  // S0 Key frame.
+  calculator.Reset({true, false});
+  EXPECT_THAT(calculator.From(1, {true, false}), ElementsAre(0, 0));
+  // S1 Key frame.
+  calculator.Reset({false, true});
+  EXPECT_THAT(calculator.From(2, {false, true}), ElementsAre(1, 0));
+  // S0T1 delta frame.
+  EXPECT_THAT(calculator.From(3, {false, false}), ElementsAre(2, 1));
+  // S1T1 delta frame.
+  EXPECT_THAT(calculator.From(4, {false, false}), ElementsAre(3, 2));
+  // S0T0 delta frame.
+  EXPECT_THAT(calculator.From(5, {true, false}), ElementsAre(4, 3));
+  // S1T0 delta frame.
+  EXPECT_THAT(calculator.From(6, {false, true}), ElementsAre(1, 4));
+}
+
+TEST(ChainDiffCalculatorTest, ResilentToAbsentChainConfig) {
+  ChainDiffCalculator calculator;
+  // Key frame.
+  calculator.Reset({true, false});
+  EXPECT_THAT(calculator.From(1, {true, false}), ElementsAre(0, 0));
+  // Forgot to set chains. should still return 2 chain_diffs.
+  EXPECT_THAT(calculator.From(2, {}), ElementsAre(1, 0));
+  // chain diffs for next frame(s) are undefined, but still there should be
+  // correct number of them.
+  EXPECT_THAT(calculator.From(3, {true, false}), SizeIs(2));
+  EXPECT_THAT(calculator.From(4, {false, true}), SizeIs(2));
+  // Since previous two frames updated all the chains, can expect what
+  // chain_diffs would be.
+  EXPECT_THAT(calculator.From(5, {false, false}), ElementsAre(2, 1));
+}
+
+TEST(ChainDiffCalculatorTest, ResilentToTooMainChains) {
+  ChainDiffCalculator calculator;
+  // Key frame.
+  calculator.Reset({true, false});
+  EXPECT_THAT(calculator.From(1, {true, false}), ElementsAre(0, 0));
+  // Set wrong number of chains. Expect number of chain_diffs is not changed.
+  EXPECT_THAT(calculator.From(2, {true, true, true}), ElementsAre(1, 0));
+  // chain diffs for next frame(s) are undefined, but still there should be
+  // correct number of them.
+  EXPECT_THAT(calculator.From(3, {true, false}), SizeIs(2));
+  EXPECT_THAT(calculator.From(4, {false, true}), SizeIs(2));
+  // Since previous two frames updated all the chains, can expect what
+  // chain_diffs would be.
+  EXPECT_THAT(calculator.From(5, {false, false}), ElementsAre(2, 1));
+}
+
+}  // namespace
+}  // namespace webrtc