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" |
Henrik Kjellander | 0f59a88 | 2015-11-18 22:31:24 +0100 | [diff] [blame] | 12 | #include "webrtc/modules/video_processing/include/video_processing.h" |
| 13 | #include "webrtc/modules/video_processing/video_decimator.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 14 | #include "webrtc/system_wrappers/include/tick_util.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 | |
| 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; |
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() { |
| 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; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 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 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 90 | } |
| 91 | return drop; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 95 | uint32_t VPMVideoDecimator::GetDecimatedFrameRate() { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 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)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | } |
| 103 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 104 | uint32_t VPMVideoDecimator::Inputframe_rate() { |
| 105 | ProcessIncomingframe_rate(TickTime::MillisecondTimestamp()); |
| 106 | return static_cast<uint32_t>(incoming_frame_rate_ + 0.5f); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 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]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 118 | } |
| 119 | incoming_frame_times_[0] = now; |
| 120 | ProcessIncomingframe_rate(now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | } |
| 122 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 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++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 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); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 141 | } else { |
| 142 | incoming_frame_rate_ = static_cast<float>(nrOfFrames); |
| 143 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | } |
| 145 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 146 | } // namespace webrtc |