xref: /aosp_15_r20/external/webrtc/modules/video_coding/chain_diff_calculator.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
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 #include "modules/video_coding/chain_diff_calculator.h"
11 
12 #include <stddef.h>
13 #include <stdint.h>
14 
15 #include <algorithm>
16 #include <vector>
17 
18 #include "absl/container/inlined_vector.h"
19 #include "absl/types/optional.h"
20 #include "rtc_base/logging.h"
21 
22 namespace webrtc {
23 
Reset(const std::vector<bool> & chains)24 void ChainDiffCalculator::Reset(const std::vector<bool>& chains) {
25   last_frame_in_chain_.resize(chains.size());
26   for (size_t i = 0; i < chains.size(); ++i) {
27     if (chains[i]) {
28       last_frame_in_chain_[i] = absl::nullopt;
29     }
30   }
31 }
32 
ChainDiffs(int64_t frame_id) const33 absl::InlinedVector<int, 4> ChainDiffCalculator::ChainDiffs(
34     int64_t frame_id) const {
35   absl::InlinedVector<int, 4> result;
36   result.reserve(last_frame_in_chain_.size());
37   for (const auto& frame_id_in_chain : last_frame_in_chain_) {
38     result.push_back(frame_id_in_chain ? (frame_id - *frame_id_in_chain) : 0);
39   }
40   return result;
41 }
42 
From(int64_t frame_id,const std::vector<bool> & chains)43 absl::InlinedVector<int, 4> ChainDiffCalculator::From(
44     int64_t frame_id,
45     const std::vector<bool>& chains) {
46   auto result = ChainDiffs(frame_id);
47   if (chains.size() != last_frame_in_chain_.size()) {
48     RTC_LOG(LS_ERROR) << "Insconsistent chain configuration for frame#"
49                       << frame_id << ": expected "
50                       << last_frame_in_chain_.size() << " chains, found "
51                       << chains.size();
52   }
53   size_t num_chains = std::min(last_frame_in_chain_.size(), chains.size());
54   for (size_t i = 0; i < num_chains; ++i) {
55     if (chains[i]) {
56       last_frame_in_chain_[i] = frame_id;
57     }
58   }
59   return result;
60 }
61 
62 }  // namespace webrtc
63