niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 11 | #include "webrtc/base/checks.h" |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 12 | #include "webrtc/base/timeutils.h" |
Henrik Kjellander | 0f59a88 | 2015-11-18 22:31:24 +0100 | [diff] [blame] | 13 | #include "webrtc/modules/video_processing/include/video_processing.h" |
| 14 | #include "webrtc/modules/video_processing/video_decimator.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
| 16 | #define VD_MIN(a, b) ((a) < (b)) ? (a) : (b) |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
wu@webrtc.org | 21a5d44 | 2014-05-29 19:43:26 +0000 | [diff] [blame] | 20 | VPMVideoDecimator::VPMVideoDecimator() { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 21 | Reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | } |
| 23 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 24 | VPMVideoDecimator::~VPMVideoDecimator() {} |
| 25 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 26 | void VPMVideoDecimator::Reset() { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 27 | overshoot_modifier_ = 0; |
| 28 | drop_count_ = 0; |
| 29 | keep_count_ = 0; |
| 30 | target_frame_rate_ = 30; |
| 31 | incoming_frame_rate_ = 0.0f; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 32 | memset(incoming_frame_times_, 0, sizeof(incoming_frame_times_)); |
| 33 | enable_temporal_decimation_ = true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | } |
| 35 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 36 | void VPMVideoDecimator::EnableTemporalDecimation(bool enable) { |
| 37 | enable_temporal_decimation_ = enable; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } |
| 39 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 40 | void VPMVideoDecimator::SetTargetFramerate(int frame_rate) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 41 | RTC_DCHECK(frame_rate); |
wu@webrtc.org | 21a5d44 | 2014-05-29 19:43:26 +0000 | [diff] [blame] | 42 | target_frame_rate_ = frame_rate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 45 | bool VPMVideoDecimator::DropFrame() { |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 46 | if (!enable_temporal_decimation_) |
| 47 | return false; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 48 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 49 | if (incoming_frame_rate_ <= 0) |
| 50 | return false; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 51 | |
| 52 | const uint32_t incomingframe_rate = |
| 53 | static_cast<uint32_t>(incoming_frame_rate_ + 0.5f); |
| 54 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 55 | if (target_frame_rate_ == 0) |
| 56 | return true; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 57 | |
| 58 | bool drop = false; |
| 59 | if (incomingframe_rate > target_frame_rate_) { |
| 60 | int32_t overshoot = |
| 61 | overshoot_modifier_ + (incomingframe_rate - target_frame_rate_); |
| 62 | if (overshoot < 0) { |
| 63 | overshoot = 0; |
| 64 | overshoot_modifier_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 67 | if (overshoot && 2 * overshoot < (int32_t)incomingframe_rate) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 68 | if (drop_count_) { // Just got here so drop to be sure. |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 69 | drop_count_ = 0; |
| 70 | return true; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 71 | } |
| 72 | const uint32_t dropVar = incomingframe_rate / overshoot; |
| 73 | |
| 74 | if (keep_count_ >= dropVar) { |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 75 | drop = true; |
| 76 | overshoot_modifier_ = -((int32_t)incomingframe_rate % overshoot) / 3; |
| 77 | keep_count_ = 1; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 78 | } else { |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 79 | keep_count_++; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 80 | } |
| 81 | } else { |
| 82 | keep_count_ = 0; |
| 83 | const uint32_t dropVar = overshoot / target_frame_rate_; |
| 84 | if (drop_count_ < dropVar) { |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 85 | drop = true; |
| 86 | drop_count_++; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 87 | } else { |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 88 | overshoot_modifier_ = overshoot % target_frame_rate_; |
| 89 | drop = false; |
| 90 | drop_count_ = 0; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 91 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 93 | } |
| 94 | return drop; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | } |
| 96 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 97 | uint32_t VPMVideoDecimator::GetDecimatedFrameRate() { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 98 | ProcessIncomingframe_rate(rtc::TimeMillis()); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 99 | if (!enable_temporal_decimation_) { |
| 100 | return static_cast<uint32_t>(incoming_frame_rate_ + 0.5f); |
| 101 | } |
| 102 | return VD_MIN(target_frame_rate_, |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 103 | static_cast<uint32_t>(incoming_frame_rate_ + 0.5f)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | } |
| 105 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 106 | uint32_t VPMVideoDecimator::Inputframe_rate() { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 107 | ProcessIncomingframe_rate(rtc::TimeMillis()); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 108 | return static_cast<uint32_t>(incoming_frame_rate_ + 0.5f); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | } |
| 110 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 111 | void VPMVideoDecimator::UpdateIncomingframe_rate() { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 112 | int64_t now = rtc::TimeMillis(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 113 | if (incoming_frame_times_[0] == 0) { |
| 114 | // First no shift. |
| 115 | } else { |
| 116 | // Shift. |
| 117 | for (int i = kFrameCountHistory_size - 2; i >= 0; i--) { |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 118 | incoming_frame_times_[i + 1] = incoming_frame_times_[i]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 120 | } |
| 121 | incoming_frame_times_[0] = now; |
| 122 | ProcessIncomingframe_rate(now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | } |
| 124 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 125 | void VPMVideoDecimator::ProcessIncomingframe_rate(int64_t now) { |
| 126 | int32_t num = 0; |
| 127 | int32_t nrOfFrames = 0; |
| 128 | for (num = 1; num < (kFrameCountHistory_size - 1); num++) { |
| 129 | // Don't use data older than 2sec. |
| 130 | if (incoming_frame_times_[num] <= 0 || |
| 131 | now - incoming_frame_times_[num] > kFrameHistoryWindowMs) { |
| 132 | break; |
| 133 | } else { |
| 134 | nrOfFrames++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 136 | } |
| 137 | if (num > 1) { |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 138 | int64_t diff = now - incoming_frame_times_[num - 1]; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 139 | incoming_frame_rate_ = 1.0; |
| 140 | if (diff > 0) { |
| 141 | incoming_frame_rate_ = nrOfFrames * 1000.0f / static_cast<float>(diff); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 143 | } else { |
| 144 | incoming_frame_rate_ = static_cast<float>(nrOfFrames); |
| 145 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | } |
| 147 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 148 | } // namespace webrtc |