blob: e987594ead32110b4a2be77af912b3775e06f5e4 [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
13#include <assert.h>
14
15namespace webrtc
16{
17
wuchengli@chromium.org30377c72013-09-28 06:06:18 +000018// The first kIgnoredSampleCount samples will be ignored.
19static const int32_t kIgnoredSampleCount = 5;
20
niklase@google.com470e71d2011-07-07 08:21:25 +000021VCMCodecTimer::VCMCodecTimer()
22:
23_filteredMax(0),
wuchengli@chromium.org30377c72013-09-28 06:06:18 +000024_ignoredSampleCount(0),
niklase@google.com470e71d2011-07-07 08:21:25 +000025_shortMax(0),
26_history()
27{
28 Reset();
29}
30
niklase@google.com470e71d2011-07-07 08:21:25 +000031void VCMCodecTimer::Reset()
32{
33 _filteredMax = 0;
wuchengli@chromium.org30377c72013-09-28 06:06:18 +000034 _ignoredSampleCount = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000035 _shortMax = 0;
36 for (int i=0; i < MAX_HISTORY_SIZE; i++)
37 {
38 _history[i].shortMax = 0;
39 _history[i].timeMs = -1;
40 }
41}
42
43// Update the max-value filter
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000044void VCMCodecTimer::MaxFilter(int32_t decodeTime, int64_t nowMs)
niklase@google.com470e71d2011-07-07 08:21:25 +000045{
wuchengli@chromium.org30377c72013-09-28 06:06:18 +000046 if (_ignoredSampleCount >= kIgnoredSampleCount)
niklase@google.com470e71d2011-07-07 08:21:25 +000047 {
48 UpdateMaxHistory(decodeTime, nowMs);
49 ProcessHistory(nowMs);
50 }
51 else
52 {
wuchengli@chromium.org30377c72013-09-28 06:06:18 +000053 _ignoredSampleCount++;
niklase@google.com470e71d2011-07-07 08:21:25 +000054 }
55}
56
57void
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000058VCMCodecTimer::UpdateMaxHistory(int32_t decodeTime, int64_t now)
niklase@google.com470e71d2011-07-07 08:21:25 +000059{
60 if (_history[0].timeMs >= 0 &&
61 now - _history[0].timeMs < SHORT_FILTER_MS)
62 {
63 if (decodeTime > _shortMax)
64 {
65 _shortMax = decodeTime;
66 }
67 }
68 else
69 {
70 // Only add a new value to the history once a second
71 if(_history[0].timeMs == -1)
72 {
73 // First, no shift
74 _shortMax = decodeTime;
75 }
76 else
77 {
78 // Shift
79 for(int i = (MAX_HISTORY_SIZE - 2); i >= 0 ; i--)
80 {
81 _history[i+1].shortMax = _history[i].shortMax;
82 _history[i+1].timeMs = _history[i].timeMs;
83 }
84 }
85 if (_shortMax == 0)
86 {
87 _shortMax = decodeTime;
88 }
89
90 _history[0].shortMax = _shortMax;
91 _history[0].timeMs = now;
92 _shortMax = 0;
93 }
94}
95
96void
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000097VCMCodecTimer::ProcessHistory(int64_t nowMs)
niklase@google.com470e71d2011-07-07 08:21:25 +000098{
99 _filteredMax = _shortMax;
100 if (_history[0].timeMs == -1)
101 {
102 return;
103 }
104 for (int i=0; i < MAX_HISTORY_SIZE; i++)
105 {
106 if (_history[i].timeMs == -1)
107 {
108 break;
109 }
110 if (nowMs - _history[i].timeMs > MAX_HISTORY_SIZE * SHORT_FILTER_MS)
111 {
112 // This sample (and all samples after this) is too old
113 break;
114 }
115 if (_history[i].shortMax > _filteredMax)
116 {
117 // This sample is the largest one this far into the history
118 _filteredMax = _history[i].shortMax;
119 }
120 }
121}
122
123// Get the maximum observed time within a time window
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000124int32_t VCMCodecTimer::RequiredDecodeTimeMs(FrameType /*frameType*/) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000125{
126 return _filteredMax;
127}
128
129}