blob: 60add8fc4bae3fbf30475d9c00d5a7d0826373d6 [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
Henrik Kjellander2557b862015-11-18 22:00:21 +010011#include "webrtc/modules/video_coding/codec_timer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
magjedc4a74e92016-03-16 07:51:44 -070013#include <assert.h>
14
philipelcce46fc2015-12-21 03:04:49 -080015namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000016
wuchengli@chromium.org30377c72013-09-28 06:06:18 +000017// The first kIgnoredSampleCount samples will be ignored.
magjedc4a74e92016-03-16 07:51:44 -070018static const int32_t kIgnoredSampleCount = 5;
wuchengli@chromium.org30377c72013-09-28 06:06:18 +000019
niklase@google.com470e71d2011-07-07 08:21:25 +000020VCMCodecTimer::VCMCodecTimer()
magjedc4a74e92016-03-16 07:51:44 -070021 : _filteredMax(0), _ignoredSampleCount(0), _shortMax(0), _history() {
22 Reset();
23}
niklase@google.com470e71d2011-07-07 08:21:25 +000024
magjedc4a74e92016-03-16 07:51:44 -070025void VCMCodecTimer::Reset() {
26 _filteredMax = 0;
27 _ignoredSampleCount = 0;
28 _shortMax = 0;
29 for (int i = 0; i < MAX_HISTORY_SIZE; i++) {
30 _history[i].shortMax = 0;
31 _history[i].timeMs = -1;
32 }
33}
34
35// Update the max-value filter
36void VCMCodecTimer::MaxFilter(int32_t decodeTime, int64_t nowMs) {
37 if (_ignoredSampleCount >= kIgnoredSampleCount) {
38 UpdateMaxHistory(decodeTime, nowMs);
39 ProcessHistory(nowMs);
40 } else {
41 _ignoredSampleCount++;
42 }
43}
44
45void VCMCodecTimer::UpdateMaxHistory(int32_t decodeTime, int64_t now) {
46 if (_history[0].timeMs >= 0 && now - _history[0].timeMs < SHORT_FILTER_MS) {
47 if (decodeTime > _shortMax) {
48 _shortMax = decodeTime;
49 }
50 } else {
51 // Only add a new value to the history once a second
52 if (_history[0].timeMs == -1) {
53 // First, no shift
54 _shortMax = decodeTime;
55 } else {
56 // Shift
57 for (int i = (MAX_HISTORY_SIZE - 2); i >= 0; i--) {
58 _history[i + 1].shortMax = _history[i].shortMax;
59 _history[i + 1].timeMs = _history[i].timeMs;
60 }
61 }
62 if (_shortMax == 0) {
63 _shortMax = decodeTime;
64 }
65
66 _history[0].shortMax = _shortMax;
67 _history[0].timeMs = now;
68 _shortMax = 0;
69 }
70}
71
72void VCMCodecTimer::ProcessHistory(int64_t nowMs) {
73 _filteredMax = _shortMax;
74 if (_history[0].timeMs == -1) {
philipelcce46fc2015-12-21 03:04:49 -080075 return;
76 }
magjedc4a74e92016-03-16 07:51:44 -070077 for (int i = 0; i < MAX_HISTORY_SIZE; i++) {
78 if (_history[i].timeMs == -1) {
79 break;
80 }
81 if (nowMs - _history[i].timeMs > MAX_HISTORY_SIZE * SHORT_FILTER_MS) {
82 // This sample (and all samples after this) is too old
83 break;
84 }
85 if (_history[i].shortMax > _filteredMax) {
86 // This sample is the largest one this far into the history
87 _filteredMax = _history[i].shortMax;
88 }
philipelcce46fc2015-12-21 03:04:49 -080089 }
niklase@google.com470e71d2011-07-07 08:21:25 +000090}
91
magjedc4a74e92016-03-16 07:51:44 -070092// Get the maximum observed time within a time window
93int32_t VCMCodecTimer::RequiredDecodeTimeMs(FrameType /*frameType*/) const {
94 return _filteredMax;
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
philipelcce46fc2015-12-21 03:04:49 -080096} // namespace webrtc