blob: 494a03f62c1f9af7e3fc55e16f99e377f0517b11 [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"
Henrik Kjellander0f59a882015-11-18 22:31:24 +010012#include "webrtc/modules/video_processing/include/video_processing.h"
13#include "webrtc/modules/video_processing/video_decimator.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010014#include "webrtc/system_wrappers/include/tick_util.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
26void 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.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() {
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.com470e71d2011-07-07 08:21:25 +000062 }
63
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000064 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.com470e71d2011-07-07 08:21:25 +000089 }
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000090 }
91 return drop;
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
94
mflodmana8565422015-12-07 01:09:52 -080095uint32_t VPMVideoDecimator::GetDecimatedFrameRate() {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000096ProcessIncomingframe_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.com470e71d2011-07-07 08:21:25 +0000102}
103
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000104uint32_t VPMVideoDecimator::Inputframe_rate() {
105 ProcessIncomingframe_rate(TickTime::MillisecondTimestamp());
106 return static_cast<uint32_t>(incoming_frame_rate_ + 0.5f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107}
108
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000109void 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.com470e71d2011-07-07 08:21:25 +0000117 }
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000118 }
119 incoming_frame_times_[0] = now;
120 ProcessIncomingframe_rate(now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121}
122
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000123void 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.com470e71d2011-07-07 08:21:25 +0000133 }
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000134 }
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.com470e71d2011-07-07 08:21:25 +0000140 }
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000141 } else {
142 incoming_frame_rate_ = static_cast<float>(nrOfFrames);
143 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000144}
145
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000146} // namespace webrtc