blob: c6623fa836cc934cd3fdb7357e6d38bb25f483ff [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
jackychen6e2ce6e2015-07-13 16:26:33 -070011#include "webrtc/base/checks.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020012#include "webrtc/base/timeutils.h"
Henrik Kjellander0f59a882015-11-18 22:31:24 +010013#include "webrtc/modules/video_processing/include/video_processing.h"
14#include "webrtc/modules/video_processing/video_decimator.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16#define VD_MIN(a, b) ((a) < (b)) ? (a) : (b)
17
18namespace webrtc {
19
wu@webrtc.org21a5d442014-05-29 19:43:26 +000020VPMVideoDecimator::VPMVideoDecimator() {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000021 Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000022}
23
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000024VPMVideoDecimator::~VPMVideoDecimator() {}
25
mflodman99ab9442015-12-07 22:54:50 -080026void VPMVideoDecimator::Reset() {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000027 overshoot_modifier_ = 0;
28 drop_count_ = 0;
29 keep_count_ = 0;
30 target_frame_rate_ = 30;
31 incoming_frame_rate_ = 0.0f;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000032 memset(incoming_frame_times_, 0, sizeof(incoming_frame_times_));
33 enable_temporal_decimation_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000036void VPMVideoDecimator::EnableTemporalDecimation(bool enable) {
37 enable_temporal_decimation_ = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
jackychen6e2ce6e2015-07-13 16:26:33 -070040void VPMVideoDecimator::SetTargetFramerate(int frame_rate) {
henrikg91d6ede2015-09-17 00:24:34 -070041 RTC_DCHECK(frame_rate);
wu@webrtc.org21a5d442014-05-29 19:43:26 +000042 target_frame_rate_ = frame_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +000043}
44
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000045bool VPMVideoDecimator::DropFrame() {
mflodman99ab9442015-12-07 22:54:50 -080046 if (!enable_temporal_decimation_)
47 return false;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000048
mflodman99ab9442015-12-07 22:54:50 -080049 if (incoming_frame_rate_ <= 0)
50 return false;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000051
52 const uint32_t incomingframe_rate =
53 static_cast<uint32_t>(incoming_frame_rate_ + 0.5f);
54
mflodman99ab9442015-12-07 22:54:50 -080055 if (target_frame_rate_ == 0)
56 return true;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000057
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.com470e71d2011-07-07 08:21:25 +000065 }
66
mflodman99ab9442015-12-07 22:54:50 -080067 if (overshoot && 2 * overshoot < (int32_t)incomingframe_rate) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000068 if (drop_count_) { // Just got here so drop to be sure.
mflodman99ab9442015-12-07 22:54:50 -080069 drop_count_ = 0;
70 return true;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000071 }
72 const uint32_t dropVar = incomingframe_rate / overshoot;
73
74 if (keep_count_ >= dropVar) {
mflodman99ab9442015-12-07 22:54:50 -080075 drop = true;
76 overshoot_modifier_ = -((int32_t)incomingframe_rate % overshoot) / 3;
77 keep_count_ = 1;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000078 } else {
mflodman99ab9442015-12-07 22:54:50 -080079 keep_count_++;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000080 }
81 } else {
82 keep_count_ = 0;
83 const uint32_t dropVar = overshoot / target_frame_rate_;
84 if (drop_count_ < dropVar) {
mflodman99ab9442015-12-07 22:54:50 -080085 drop = true;
86 drop_count_++;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000087 } else {
mflodman99ab9442015-12-07 22:54:50 -080088 overshoot_modifier_ = overshoot % target_frame_rate_;
89 drop = false;
90 drop_count_ = 0;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000091 }
niklase@google.com470e71d2011-07-07 08:21:25 +000092 }
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000093 }
94 return drop;
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
96
mflodmana8565422015-12-07 01:09:52 -080097uint32_t VPMVideoDecimator::GetDecimatedFrameRate() {
Niels Möllerd28db7f2016-05-10 16:31:47 +020098 ProcessIncomingframe_rate(rtc::TimeMillis());
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000099 if (!enable_temporal_decimation_) {
100 return static_cast<uint32_t>(incoming_frame_rate_ + 0.5f);
101 }
102 return VD_MIN(target_frame_rate_,
mflodman99ab9442015-12-07 22:54:50 -0800103 static_cast<uint32_t>(incoming_frame_rate_ + 0.5f));
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
105
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000106uint32_t VPMVideoDecimator::Inputframe_rate() {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200107 ProcessIncomingframe_rate(rtc::TimeMillis());
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000108 return static_cast<uint32_t>(incoming_frame_rate_ + 0.5f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109}
110
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000111void VPMVideoDecimator::UpdateIncomingframe_rate() {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200112 int64_t now = rtc::TimeMillis();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000113 if (incoming_frame_times_[0] == 0) {
114 // First no shift.
115 } else {
116 // Shift.
117 for (int i = kFrameCountHistory_size - 2; i >= 0; i--) {
mflodman99ab9442015-12-07 22:54:50 -0800118 incoming_frame_times_[i + 1] = incoming_frame_times_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000119 }
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000120 }
121 incoming_frame_times_[0] = now;
122 ProcessIncomingframe_rate(now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000123}
124
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000125void 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.com470e71d2011-07-07 08:21:25 +0000135 }
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000136 }
137 if (num > 1) {
mflodman99ab9442015-12-07 22:54:50 -0800138 int64_t diff = now - incoming_frame_times_[num - 1];
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000139 incoming_frame_rate_ = 1.0;
140 if (diff > 0) {
141 incoming_frame_rate_ = nrOfFrames * 1000.0f / static_cast<float>(diff);
niklase@google.com470e71d2011-07-07 08:21:25 +0000142 }
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000143 } else {
144 incoming_frame_rate_ = static_cast<float>(nrOfFrames);
145 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000146}
147
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000148} // namespace webrtc