xref: /aosp_15_r20/external/webrtc/modules/video_coding/utility/decoded_frames_history.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 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 
11 #include "modules/video_coding/utility/decoded_frames_history.h"
12 
13 #include <algorithm>
14 
15 #include "rtc_base/checks.h"
16 #include "rtc_base/logging.h"
17 
18 namespace webrtc {
19 namespace video_coding {
20 
DecodedFramesHistory(size_t window_size)21 DecodedFramesHistory::DecodedFramesHistory(size_t window_size)
22     : buffer_(window_size) {}
23 
24 DecodedFramesHistory::~DecodedFramesHistory() = default;
25 
InsertDecoded(int64_t frame_id,uint32_t timestamp)26 void DecodedFramesHistory::InsertDecoded(int64_t frame_id, uint32_t timestamp) {
27   last_decoded_frame_ = frame_id;
28   last_decoded_frame_timestamp_ = timestamp;
29   int new_index = FrameIdToIndex(frame_id);
30 
31   RTC_DCHECK(last_frame_id_ < frame_id);
32 
33   // Clears expired values from the cyclic buffer_.
34   if (last_frame_id_) {
35     int64_t id_jump = frame_id - *last_frame_id_;
36     int last_index = FrameIdToIndex(*last_frame_id_);
37 
38     if (id_jump >= static_cast<int64_t>(buffer_.size())) {
39       std::fill(buffer_.begin(), buffer_.end(), false);
40     } else if (new_index > last_index) {
41       std::fill(buffer_.begin() + last_index + 1, buffer_.begin() + new_index,
42                 false);
43     } else {
44       std::fill(buffer_.begin() + last_index + 1, buffer_.end(), false);
45       std::fill(buffer_.begin(), buffer_.begin() + new_index, false);
46     }
47   }
48 
49   buffer_[new_index] = true;
50   last_frame_id_ = frame_id;
51 }
52 
WasDecoded(int64_t frame_id) const53 bool DecodedFramesHistory::WasDecoded(int64_t frame_id) const {
54   if (!last_frame_id_)
55     return false;
56 
57   // Reference to the picture_id out of the stored should happen.
58   if (frame_id <= *last_frame_id_ - static_cast<int64_t>(buffer_.size())) {
59     RTC_LOG(LS_WARNING) << "Referencing a frame out of the window. "
60                            "Assuming it was undecoded to avoid artifacts.";
61     return false;
62   }
63 
64   if (frame_id > last_frame_id_)
65     return false;
66 
67   return buffer_[FrameIdToIndex(frame_id)];
68 }
69 
Clear()70 void DecodedFramesHistory::Clear() {
71   last_decoded_frame_timestamp_.reset();
72   last_decoded_frame_.reset();
73   std::fill(buffer_.begin(), buffer_.end(), false);
74   last_frame_id_.reset();
75 }
76 
GetLastDecodedFrameId() const77 absl::optional<int64_t> DecodedFramesHistory::GetLastDecodedFrameId() const {
78   return last_decoded_frame_;
79 }
80 
GetLastDecodedFrameTimestamp() const81 absl::optional<uint32_t> DecodedFramesHistory::GetLastDecodedFrameTimestamp()
82     const {
83   return last_decoded_frame_timestamp_;
84 }
85 
FrameIdToIndex(int64_t frame_id) const86 int DecodedFramesHistory::FrameIdToIndex(int64_t frame_id) const {
87   int m = frame_id % buffer_.size();
88   return m >= 0 ? m : m + buffer_.size();
89 }
90 
91 }  // namespace video_coding
92 }  // namespace webrtc
93