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 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/video_coding/codec_timer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | |
| 15 | namespace webrtc |
| 16 | { |
| 17 | |
wuchengli@chromium.org | 30377c7 | 2013-09-28 06:06:18 +0000 | [diff] [blame] | 18 | // The first kIgnoredSampleCount samples will be ignored. |
| 19 | static const int32_t kIgnoredSampleCount = 5; |
| 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | VCMCodecTimer::VCMCodecTimer() |
| 22 | : |
| 23 | _filteredMax(0), |
wuchengli@chromium.org | 30377c7 | 2013-09-28 06:06:18 +0000 | [diff] [blame] | 24 | _ignoredSampleCount(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | _shortMax(0), |
| 26 | _history() |
| 27 | { |
| 28 | Reset(); |
| 29 | } |
| 30 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | void VCMCodecTimer::Reset() |
| 32 | { |
| 33 | _filteredMax = 0; |
wuchengli@chromium.org | 30377c7 | 2013-09-28 06:06:18 +0000 | [diff] [blame] | 34 | _ignoredSampleCount = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | _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.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 44 | void VCMCodecTimer::MaxFilter(int32_t decodeTime, int64_t nowMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | { |
wuchengli@chromium.org | 30377c7 | 2013-09-28 06:06:18 +0000 | [diff] [blame] | 46 | if (_ignoredSampleCount >= kIgnoredSampleCount) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | { |
| 48 | UpdateMaxHistory(decodeTime, nowMs); |
| 49 | ProcessHistory(nowMs); |
| 50 | } |
| 51 | else |
| 52 | { |
wuchengli@chromium.org | 30377c7 | 2013-09-28 06:06:18 +0000 | [diff] [blame] | 53 | _ignoredSampleCount++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | void |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 58 | VCMCodecTimer::UpdateMaxHistory(int32_t decodeTime, int64_t now) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | { |
| 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 | |
| 96 | void |
pbos@webrtc.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 97 | VCMCodecTimer::ProcessHistory(int64_t nowMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | { |
| 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.org | 7b859cc | 2013-04-02 15:54:38 +0000 | [diff] [blame] | 124 | int32_t VCMCodecTimer::RequiredDecodeTimeMs(FrameType /*frameType*/) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | { |
| 126 | return _filteredMax; |
| 127 | } |
| 128 | |
| 129 | } |