• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 DecodedFramesHistory::LayerHistory::LayerHistory() = default;
22 DecodedFramesHistory::LayerHistory::~LayerHistory() = default;
23 
DecodedFramesHistory(size_t window_size)24 DecodedFramesHistory::DecodedFramesHistory(size_t window_size)
25     : window_size_(window_size) {}
26 
27 DecodedFramesHistory::~DecodedFramesHistory() = default;
28 
InsertDecoded(const VideoLayerFrameId & frameid,uint32_t timestamp)29 void DecodedFramesHistory::InsertDecoded(const VideoLayerFrameId& frameid,
30                                          uint32_t timestamp) {
31   last_decoded_frame_ = frameid;
32   last_decoded_frame_timestamp_ = timestamp;
33   if (static_cast<int>(layers_.size()) < frameid.spatial_layer + 1) {
34     size_t old_size = layers_.size();
35     layers_.resize(frameid.spatial_layer + 1);
36 
37     for (size_t i = old_size; i < layers_.size(); ++i)
38       layers_[i].buffer.resize(window_size_);
39 
40     layers_[frameid.spatial_layer].last_picture_id = frameid.picture_id;
41     layers_[frameid.spatial_layer]
42         .buffer[PictureIdToIndex(frameid.picture_id)] = true;
43     return;
44   }
45 
46   int new_index = PictureIdToIndex(frameid.picture_id);
47   LayerHistory& history = layers_[frameid.spatial_layer];
48 
49   RTC_DCHECK(history.last_picture_id < frameid.picture_id);
50 
51   // Clears expired values from the cyclic buffer.
52   if (history.last_picture_id) {
53     int64_t id_jump = frameid.picture_id - *history.last_picture_id;
54     int last_index = PictureIdToIndex(*history.last_picture_id);
55 
56     if (id_jump >= window_size_) {
57       std::fill(history.buffer.begin(), history.buffer.end(), false);
58     } else if (new_index > last_index) {
59       std::fill(history.buffer.begin() + last_index + 1,
60                 history.buffer.begin() + new_index, false);
61     } else {
62       std::fill(history.buffer.begin() + last_index + 1, history.buffer.end(),
63                 false);
64       std::fill(history.buffer.begin(), history.buffer.begin() + new_index,
65                 false);
66     }
67   }
68 
69   history.buffer[new_index] = true;
70   history.last_picture_id = frameid.picture_id;
71 }
72 
WasDecoded(const VideoLayerFrameId & frameid)73 bool DecodedFramesHistory::WasDecoded(const VideoLayerFrameId& frameid) {
74   // Unseen before spatial layer.
75   if (static_cast<int>(layers_.size()) < frameid.spatial_layer + 1)
76     return false;
77 
78   LayerHistory& history = layers_[frameid.spatial_layer];
79 
80   if (!history.last_picture_id)
81     return false;
82 
83   // Reference to the picture_id out of the stored history should happen.
84   if (frameid.picture_id <= *history.last_picture_id - window_size_) {
85     RTC_LOG(LS_WARNING) << "Referencing a frame out of the history window. "
86                            "Assuming it was undecoded to avoid artifacts.";
87     return false;
88   }
89 
90   if (frameid.picture_id > history.last_picture_id)
91     return false;
92 
93   return history.buffer[PictureIdToIndex(frameid.picture_id)];
94 }
95 
Clear()96 void DecodedFramesHistory::Clear() {
97   layers_.clear();
98   last_decoded_frame_timestamp_.reset();
99   last_decoded_frame_.reset();
100 }
101 
102 absl::optional<VideoLayerFrameId>
GetLastDecodedFrameId()103 DecodedFramesHistory::GetLastDecodedFrameId() {
104   return last_decoded_frame_;
105 }
106 
GetLastDecodedFrameTimestamp()107 absl::optional<uint32_t> DecodedFramesHistory::GetLastDecodedFrameTimestamp() {
108   return last_decoded_frame_timestamp_;
109 }
110 
PictureIdToIndex(int64_t frame_id) const111 int DecodedFramesHistory::PictureIdToIndex(int64_t frame_id) const {
112   int m = frame_id % window_size_;
113   return m >= 0 ? m : m + window_size_;
114 }
115 
116 }  // namespace video_coding
117 }  // namespace webrtc
118