OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2011 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 "webrtc/base/checks.h" | |
12 #include "webrtc/modules/video_processing/main/interface/video_processing.h" | |
13 #include "webrtc/modules/video_processing/main/source/video_decimator.h" | |
14 #include "webrtc/system_wrappers/include/tick_util.h" | |
15 | |
16 #define VD_MIN(a, b) ((a) < (b)) ? (a) : (b) | |
17 | |
18 namespace webrtc { | |
19 | |
20 VPMVideoDecimator::VPMVideoDecimator() { | |
21 Reset(); | |
22 } | |
23 | |
24 VPMVideoDecimator::~VPMVideoDecimator() {} | |
25 | |
26 void VPMVideoDecimator::Reset() { | |
27 overshoot_modifier_ = 0; | |
28 drop_count_ = 0; | |
29 keep_count_ = 0; | |
30 target_frame_rate_ = 30; | |
31 incoming_frame_rate_ = 0.0f; | |
32 memset(incoming_frame_times_, 0, sizeof(incoming_frame_times_)); | |
33 enable_temporal_decimation_ = true; | |
34 } | |
35 | |
36 void VPMVideoDecimator::EnableTemporalDecimation(bool enable) { | |
37 enable_temporal_decimation_ = enable; | |
38 } | |
39 | |
40 void VPMVideoDecimator::SetTargetFramerate(int frame_rate) { | |
41 RTC_DCHECK(frame_rate); | |
42 target_frame_rate_ = frame_rate; | |
43 } | |
44 | |
45 bool VPMVideoDecimator::DropFrame() { | |
46 if (!enable_temporal_decimation_) return false; | |
47 | |
48 if (incoming_frame_rate_ <= 0) return false; | |
49 | |
50 const uint32_t incomingframe_rate = | |
51 static_cast<uint32_t>(incoming_frame_rate_ + 0.5f); | |
52 | |
53 if (target_frame_rate_ == 0) return true; | |
54 | |
55 bool drop = false; | |
56 if (incomingframe_rate > target_frame_rate_) { | |
57 int32_t overshoot = | |
58 overshoot_modifier_ + (incomingframe_rate - target_frame_rate_); | |
59 if (overshoot < 0) { | |
60 overshoot = 0; | |
61 overshoot_modifier_ = 0; | |
62 } | |
63 | |
64 if (overshoot && 2 * overshoot < (int32_t) incomingframe_rate) { | |
65 if (drop_count_) { // Just got here so drop to be sure. | |
66 drop_count_ = 0; | |
67 return true; | |
68 } | |
69 const uint32_t dropVar = incomingframe_rate / overshoot; | |
70 | |
71 if (keep_count_ >= dropVar) { | |
72 drop = true; | |
73 overshoot_modifier_ = -((int32_t) incomingframe_rate % overshoot) / 3; | |
74 keep_count_ = 1; | |
75 } else { | |
76 keep_count_++; | |
77 } | |
78 } else { | |
79 keep_count_ = 0; | |
80 const uint32_t dropVar = overshoot / target_frame_rate_; | |
81 if (drop_count_ < dropVar) { | |
82 drop = true; | |
83 drop_count_++; | |
84 } else { | |
85 overshoot_modifier_ = overshoot % target_frame_rate_; | |
86 drop = false; | |
87 drop_count_ = 0; | |
88 } | |
89 } | |
90 } | |
91 return drop; | |
92 } | |
93 | |
94 | |
95 uint32_t VPMVideoDecimator::Decimatedframe_rate() { | |
96 ProcessIncomingframe_rate(TickTime::MillisecondTimestamp()); | |
97 if (!enable_temporal_decimation_) { | |
98 return static_cast<uint32_t>(incoming_frame_rate_ + 0.5f); | |
99 } | |
100 return VD_MIN(target_frame_rate_, | |
101 static_cast<uint32_t>(incoming_frame_rate_ + 0.5f)); | |
102 } | |
103 | |
104 uint32_t VPMVideoDecimator::Inputframe_rate() { | |
105 ProcessIncomingframe_rate(TickTime::MillisecondTimestamp()); | |
106 return static_cast<uint32_t>(incoming_frame_rate_ + 0.5f); | |
107 } | |
108 | |
109 void VPMVideoDecimator::UpdateIncomingframe_rate() { | |
110 int64_t now = TickTime::MillisecondTimestamp(); | |
111 if (incoming_frame_times_[0] == 0) { | |
112 // First no shift. | |
113 } else { | |
114 // Shift. | |
115 for (int i = kFrameCountHistory_size - 2; i >= 0; i--) { | |
116 incoming_frame_times_[i+1] = incoming_frame_times_[i]; | |
117 } | |
118 } | |
119 incoming_frame_times_[0] = now; | |
120 ProcessIncomingframe_rate(now); | |
121 } | |
122 | |
123 void VPMVideoDecimator::ProcessIncomingframe_rate(int64_t now) { | |
124 int32_t num = 0; | |
125 int32_t nrOfFrames = 0; | |
126 for (num = 1; num < (kFrameCountHistory_size - 1); num++) { | |
127 // Don't use data older than 2sec. | |
128 if (incoming_frame_times_[num] <= 0 || | |
129 now - incoming_frame_times_[num] > kFrameHistoryWindowMs) { | |
130 break; | |
131 } else { | |
132 nrOfFrames++; | |
133 } | |
134 } | |
135 if (num > 1) { | |
136 int64_t diff = now - incoming_frame_times_[num-1]; | |
137 incoming_frame_rate_ = 1.0; | |
138 if (diff > 0) { | |
139 incoming_frame_rate_ = nrOfFrames * 1000.0f / static_cast<float>(diff); | |
140 } | |
141 } else { | |
142 incoming_frame_rate_ = static_cast<float>(nrOfFrames); | |
143 } | |
144 } | |
145 | |
146 } // namespace webrtc | |
OLD | NEW |