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