philipel | ceac5d5 | 2021-12-07 18:13:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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/frame_buffer3.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <iterator> |
| 15 | #include <queue> |
| 16 | #include <utility> |
| 17 | |
| 18 | #include "absl/algorithm/container.h" |
| 19 | #include "absl/container/inlined_vector.h" |
| 20 | #include "rtc_base/logging.h" |
| 21 | #include "rtc_base/numerics/sequence_number_util.h" |
| 22 | #include "system_wrappers/include/field_trial.h" |
| 23 | |
| 24 | namespace webrtc { |
| 25 | namespace { |
| 26 | bool ValidReferences(const EncodedFrame& frame) { |
| 27 | // All references must point backwards, and duplicates are not allowed. |
| 28 | for (size_t i = 0; i < frame.num_references; ++i) { |
| 29 | if (frame.references[i] >= frame.Id()) |
| 30 | return false; |
| 31 | |
| 32 | for (size_t j = i + 1; j < frame.num_references; ++j) { |
| 33 | if (frame.references[i] == frame.references[j]) |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | // Since FrameBuffer::FrameInfo is private it can't be used in the function |
| 42 | // signature, hence the FrameIteratorT type. |
| 43 | template <typename FrameIteratorT> |
| 44 | rtc::ArrayView<const int64_t> GetReferences(const FrameIteratorT& it) { |
| 45 | return {it->second.encoded_frame->references, |
| 46 | std::min<size_t>(it->second.encoded_frame->num_references, |
| 47 | EncodedFrame::kMaxFrameReferences)}; |
| 48 | } |
| 49 | |
| 50 | template <typename FrameIteratorT> |
| 51 | int64_t GetFrameId(const FrameIteratorT& it) { |
| 52 | return it->first; |
| 53 | } |
| 54 | |
| 55 | template <typename FrameIteratorT> |
| 56 | int64_t GetTimestamp(const FrameIteratorT& it) { |
| 57 | return it->second.encoded_frame->Timestamp(); |
| 58 | } |
| 59 | |
| 60 | template <typename FrameIteratorT> |
| 61 | bool IsLastFrameInTemporalUnit(const FrameIteratorT& it) { |
| 62 | return it->second.encoded_frame->is_last_spatial_layer; |
| 63 | } |
| 64 | } // namespace |
| 65 | |
| 66 | FrameBuffer::FrameBuffer(int max_size, int max_decode_history) |
| 67 | : legacy_frame_id_jump_behavior_( |
| 68 | field_trial::IsEnabled("WebRTC-LegacyFrameIdJumpBehavior")), |
| 69 | max_size_(max_size), |
| 70 | decoded_frame_history_(max_decode_history) {} |
| 71 | |
| 72 | void FrameBuffer::InsertFrame(std::unique_ptr<EncodedFrame> frame) { |
| 73 | if (!ValidReferences(*frame)) { |
| 74 | RTC_DLOG(LS_WARNING) << "Frame " << frame->Id() |
| 75 | << " has invalid references, dropping frame."; |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | if (frame->Id() <= decoded_frame_history_.GetLastDecodedFrameId()) { |
| 80 | if (legacy_frame_id_jump_behavior_ && frame->is_keyframe() && |
| 81 | AheadOf(frame->Timestamp(), |
| 82 | *decoded_frame_history_.GetLastDecodedFrameTimestamp())) { |
| 83 | RTC_DLOG(LS_WARNING) |
| 84 | << "Keyframe " << frame->Id() |
| 85 | << " has newer timestamp but older picture id, clearing buffer."; |
| 86 | Clear(); |
| 87 | } else { |
| 88 | // Already decoded past this frame. |
| 89 | return; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (frames_.size() == max_size_) { |
| 94 | if (frame->is_keyframe()) { |
| 95 | RTC_DLOG(LS_WARNING) << "Keyframe " << frame->Id() |
| 96 | << " inserted into full buffer, clearing buffer."; |
| 97 | Clear(); |
| 98 | } else { |
| 99 | // No space for this frame. |
| 100 | return; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | const int64_t frame_id = frame->Id(); |
| 105 | auto insert_res = frames_.emplace(frame_id, FrameInfo{std::move(frame)}); |
| 106 | if (!insert_res.second) { |
| 107 | // Frame has already been inserted. |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | if (frames_.size() == max_size_) { |
| 112 | RTC_DLOG(LS_WARNING) << "Frame " << frame_id |
| 113 | << " inserted, buffer is now full."; |
| 114 | } |
| 115 | |
| 116 | PropagateContinuity(insert_res.first); |
| 117 | FindNextAndLastDecodableTemporalUnit(); |
| 118 | } |
| 119 | |
| 120 | absl::InlinedVector<std::unique_ptr<EncodedFrame>, 4> |
| 121 | FrameBuffer::ExtractNextDecodableTemporalUnit() { |
| 122 | absl::InlinedVector<std::unique_ptr<EncodedFrame>, 4> res; |
| 123 | if (!next_decodable_temporal_unit_) { |
| 124 | return res; |
| 125 | } |
| 126 | |
| 127 | auto end_it = std::next(next_decodable_temporal_unit_->last_frame); |
| 128 | for (auto it = next_decodable_temporal_unit_->first_frame; it != end_it; |
| 129 | ++it) { |
| 130 | decoded_frame_history_.InsertDecoded(GetFrameId(it), GetTimestamp(it)); |
| 131 | res.push_back(std::move(it->second.encoded_frame)); |
| 132 | } |
| 133 | |
| 134 | DropNextDecodableTemporalUnit(); |
| 135 | return res; |
| 136 | } |
| 137 | |
| 138 | void FrameBuffer::DropNextDecodableTemporalUnit() { |
| 139 | if (!next_decodable_temporal_unit_) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | auto end_it = std::next(next_decodable_temporal_unit_->last_frame); |
| 144 | num_dropped_frames_ += std::count_if( |
| 145 | frames_.begin(), end_it, |
| 146 | [](const auto& f) { return f.second.encoded_frame != nullptr; }); |
| 147 | |
| 148 | frames_.erase(frames_.begin(), end_it); |
| 149 | FindNextAndLastDecodableTemporalUnit(); |
| 150 | } |
| 151 | |
| 152 | absl::optional<int64_t> FrameBuffer::LastContinuousFrameId() const { |
| 153 | return last_continuous_frame_id_; |
| 154 | } |
| 155 | |
| 156 | absl::optional<int64_t> FrameBuffer::LastContinuousTemporalUnitFrameId() const { |
| 157 | return last_continuous_temporal_unit_frame_id_; |
| 158 | } |
| 159 | |
| 160 | absl::optional<uint32_t> FrameBuffer::NextDecodableTemporalUnitRtpTimestamp() |
| 161 | const { |
| 162 | if (!next_decodable_temporal_unit_) { |
| 163 | return absl::nullopt; |
| 164 | } |
| 165 | return GetTimestamp(next_decodable_temporal_unit_->first_frame); |
| 166 | } |
| 167 | |
| 168 | absl::optional<uint32_t> FrameBuffer::LastDecodableTemporalUnitRtpTimestamp() |
| 169 | const { |
| 170 | return last_decodable_temporal_unit_timestamp_; |
| 171 | } |
| 172 | |
| 173 | int FrameBuffer::GetTotalNumberOfContinuousTemporalUnits() const { |
| 174 | return num_continuous_temporal_units_; |
| 175 | } |
| 176 | int FrameBuffer::GetTotalNumberOfDroppedFrames() const { |
| 177 | return num_dropped_frames_; |
| 178 | } |
| 179 | |
| 180 | bool FrameBuffer::IsContinuous(const FrameIterator& it) const { |
| 181 | for (int64_t reference : GetReferences(it)) { |
| 182 | if (decoded_frame_history_.WasDecoded(reference)) { |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | auto reference_frame_it = frames_.find(reference); |
| 187 | if (reference_frame_it != frames_.end() && |
| 188 | reference_frame_it->second.continuous) { |
| 189 | continue; |
| 190 | } |
| 191 | |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | void FrameBuffer::PropagateContinuity(const FrameIterator& frame_it) { |
| 199 | for (auto it = frame_it; it != frames_.end(); ++it) { |
| 200 | if (!it->second.continuous) { |
| 201 | if (IsContinuous(it)) { |
| 202 | it->second.continuous = true; |
| 203 | if (last_continuous_frame_id_ < GetFrameId(it)) { |
| 204 | last_continuous_frame_id_ = GetFrameId(it); |
| 205 | } |
| 206 | if (IsLastFrameInTemporalUnit(it)) { |
| 207 | num_continuous_temporal_units_++; |
| 208 | if (last_continuous_temporal_unit_frame_id_ < GetFrameId(it)) { |
| 209 | last_continuous_temporal_unit_frame_id_ = GetFrameId(it); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void FrameBuffer::FindNextAndLastDecodableTemporalUnit() { |
| 218 | next_decodable_temporal_unit_.reset(); |
| 219 | last_decodable_temporal_unit_timestamp_.reset(); |
| 220 | |
| 221 | if (!last_continuous_temporal_unit_frame_id_) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | FrameIterator first_frame_it = frames_.begin(); |
| 226 | FrameIterator last_frame_it = frames_.begin(); |
| 227 | absl::InlinedVector<int64_t, 4> frames_in_temporal_unit; |
| 228 | for (auto frame_it = frames_.begin(); frame_it != frames_.end();) { |
| 229 | if (GetFrameId(frame_it) > *last_continuous_temporal_unit_frame_id_) { |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | if (GetTimestamp(frame_it) != GetTimestamp(first_frame_it)) { |
| 234 | frames_in_temporal_unit.clear(); |
| 235 | first_frame_it = frame_it; |
| 236 | } |
| 237 | |
| 238 | frames_in_temporal_unit.push_back(GetFrameId(frame_it)); |
| 239 | |
| 240 | last_frame_it = frame_it++; |
| 241 | |
| 242 | if (IsLastFrameInTemporalUnit(last_frame_it)) { |
| 243 | bool temporal_unit_decodable = true; |
| 244 | for (auto it = first_frame_it; it != frame_it && temporal_unit_decodable; |
| 245 | ++it) { |
| 246 | for (int64_t reference : GetReferences(it)) { |
| 247 | if (!decoded_frame_history_.WasDecoded(reference) && |
| 248 | !absl::c_linear_search(frames_in_temporal_unit, reference)) { |
| 249 | // A frame in the temporal unit has a non-decoded reference outside |
| 250 | // the temporal unit, so it's not yet ready to be decoded. |
| 251 | temporal_unit_decodable = false; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (temporal_unit_decodable) { |
| 258 | if (!next_decodable_temporal_unit_) { |
| 259 | next_decodable_temporal_unit_ = {first_frame_it, last_frame_it}; |
| 260 | } |
| 261 | |
| 262 | last_decodable_temporal_unit_timestamp_ = GetTimestamp(first_frame_it); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void FrameBuffer::Clear() { |
| 269 | frames_.clear(); |
| 270 | next_decodable_temporal_unit_.reset(); |
| 271 | last_decodable_temporal_unit_timestamp_.reset(); |
| 272 | last_continuous_frame_id_.reset(); |
| 273 | last_continuous_temporal_unit_frame_id_.reset(); |
| 274 | decoded_frame_history_.Clear(); |
| 275 | } |
| 276 | |
| 277 | } // namespace webrtc |