niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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/content_metrics_processing.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <math.h> |
| 14 | |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 15 | #include "webrtc/modules/include/module_common_types.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 16 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 18 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | ////////////////////////////////// |
| 20 | /// VCMContentMetricsProcessing // |
| 21 | ////////////////////////////////// |
| 22 | |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 23 | VCMContentMetricsProcessing::VCMContentMetricsProcessing() |
| 24 | : recursive_avg_factor_(1 / 150.0f), // matched to 30fps. |
| 25 | frame_cnt_uniform_avg_(0), |
| 26 | avg_motion_level_(0.0f), |
| 27 | avg_spatial_level_(0.0f) { |
| 28 | recursive_avg_ = new VideoContentMetrics(); |
| 29 | uniform_avg_ = new VideoContentMetrics(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | } |
| 31 | |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 32 | VCMContentMetricsProcessing::~VCMContentMetricsProcessing() { |
| 33 | delete recursive_avg_; |
| 34 | delete uniform_avg_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
| 36 | |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 37 | int VCMContentMetricsProcessing::Reset() { |
| 38 | recursive_avg_->Reset(); |
| 39 | uniform_avg_->Reset(); |
| 40 | frame_cnt_uniform_avg_ = 0; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 41 | avg_motion_level_ = 0.0f; |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 42 | avg_spatial_level_ = 0.0f; |
| 43 | return VCM_OK; |
| 44 | } |
| 45 | |
| 46 | void VCMContentMetricsProcessing::UpdateFrameRate(uint32_t frameRate) { |
Peter Boström | 59b2d3e | 2016-01-26 16:18:48 +0100 | [diff] [blame] | 47 | if (frameRate == 0) |
| 48 | frameRate = 1; |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 49 | // Update factor for recursive averaging. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 50 | recursive_avg_factor_ = static_cast<float>(1000.0f) / |
| 51 | static_cast<float>(frameRate * kQmMinIntervalMs); |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | VideoContentMetrics* VCMContentMetricsProcessing::LongTermAvgData() { |
| 55 | return recursive_avg_; |
| 56 | } |
| 57 | |
| 58 | VideoContentMetrics* VCMContentMetricsProcessing::ShortTermAvgData() { |
| 59 | if (frame_cnt_uniform_avg_ == 0) { |
| 60 | return NULL; |
| 61 | } |
| 62 | // Two metrics are used: motion and spatial level. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 63 | uniform_avg_->motion_magnitude = |
| 64 | avg_motion_level_ / static_cast<float>(frame_cnt_uniform_avg_); |
| 65 | uniform_avg_->spatial_pred_err = |
| 66 | avg_spatial_level_ / static_cast<float>(frame_cnt_uniform_avg_); |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 67 | return uniform_avg_; |
| 68 | } |
| 69 | |
| 70 | void VCMContentMetricsProcessing::ResetShortTermAvgData() { |
| 71 | // Reset. |
| 72 | avg_motion_level_ = 0.0f; |
| 73 | avg_spatial_level_ = 0.0f; |
| 74 | frame_cnt_uniform_avg_ = 0; |
| 75 | } |
| 76 | |
| 77 | int VCMContentMetricsProcessing::UpdateContentData( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 78 | const VideoContentMetrics* contentMetrics) { |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 79 | if (contentMetrics == NULL) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | return VCM_OK; |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 81 | } |
| 82 | return ProcessContent(contentMetrics); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 85 | int VCMContentMetricsProcessing::ProcessContent( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 86 | const VideoContentMetrics* contentMetrics) { |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 87 | // Update the recursive averaged metrics: average is over longer window |
| 88 | // of time: over QmMinIntervalMs ms. |
| 89 | UpdateRecursiveAvg(contentMetrics); |
| 90 | // Update the uniform averaged metrics: average is over shorter window |
| 91 | // of time: based on ~RTCP reports. |
| 92 | UpdateUniformAvg(contentMetrics); |
| 93 | return VCM_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | } |
| 95 | |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 96 | void VCMContentMetricsProcessing::UpdateUniformAvg( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 97 | const VideoContentMetrics* contentMetrics) { |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 98 | // Update frame counter. |
| 99 | frame_cnt_uniform_avg_ += 1; |
| 100 | // Update averaged metrics: motion and spatial level are used. |
| 101 | avg_motion_level_ += contentMetrics->motion_magnitude; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 102 | avg_spatial_level_ += contentMetrics->spatial_pred_err; |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 103 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | } |
| 105 | |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 106 | void VCMContentMetricsProcessing::UpdateRecursiveAvg( |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 107 | const VideoContentMetrics* contentMetrics) { |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 108 | // Spatial metrics: 2x2, 1x2(H), 2x1(V). |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 109 | recursive_avg_->spatial_pred_err = |
| 110 | (1 - recursive_avg_factor_) * recursive_avg_->spatial_pred_err + |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 111 | recursive_avg_factor_ * contentMetrics->spatial_pred_err; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 113 | recursive_avg_->spatial_pred_err_h = |
| 114 | (1 - recursive_avg_factor_) * recursive_avg_->spatial_pred_err_h + |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 115 | recursive_avg_factor_ * contentMetrics->spatial_pred_err_h; |
| 116 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 117 | recursive_avg_->spatial_pred_err_v = |
| 118 | (1 - recursive_avg_factor_) * recursive_avg_->spatial_pred_err_v + |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 119 | recursive_avg_factor_ * contentMetrics->spatial_pred_err_v; |
| 120 | |
| 121 | // Motion metric: Derived from NFD (normalized frame difference). |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 122 | recursive_avg_->motion_magnitude = |
| 123 | (1 - recursive_avg_factor_) * recursive_avg_->motion_magnitude + |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +0000 | [diff] [blame] | 124 | recursive_avg_factor_ * contentMetrics->motion_magnitude; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 126 | } // namespace webrtc |