blob: b2586fce3fb42b507cd4cd5282f1a7733eb6526e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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/content_metrics_processing.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#include <math.h>
14
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010015#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010016#include "webrtc/modules/video_coding/include/video_coding_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000017
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000018namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000019//////////////////////////////////
20/// VCMContentMetricsProcessing //
21//////////////////////////////////
22
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000023VCMContentMetricsProcessing::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.com470e71d2011-07-07 08:21:25 +000030}
31
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000032VCMContentMetricsProcessing::~VCMContentMetricsProcessing() {
33 delete recursive_avg_;
34 delete uniform_avg_;
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
36
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000037int VCMContentMetricsProcessing::Reset() {
38 recursive_avg_->Reset();
39 uniform_avg_->Reset();
40 frame_cnt_uniform_avg_ = 0;
philipel9d3ab612015-12-21 04:12:39 -080041 avg_motion_level_ = 0.0f;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000042 avg_spatial_level_ = 0.0f;
43 return VCM_OK;
44}
45
46void VCMContentMetricsProcessing::UpdateFrameRate(uint32_t frameRate) {
Peter Boström59b2d3e2016-01-26 16:18:48 +010047 if (frameRate == 0)
48 frameRate = 1;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000049 // Update factor for recursive averaging.
philipel9d3ab612015-12-21 04:12:39 -080050 recursive_avg_factor_ = static_cast<float>(1000.0f) /
51 static_cast<float>(frameRate * kQmMinIntervalMs);
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000052}
53
54VideoContentMetrics* VCMContentMetricsProcessing::LongTermAvgData() {
55 return recursive_avg_;
56}
57
58VideoContentMetrics* VCMContentMetricsProcessing::ShortTermAvgData() {
59 if (frame_cnt_uniform_avg_ == 0) {
60 return NULL;
61 }
62 // Two metrics are used: motion and spatial level.
philipel9d3ab612015-12-21 04:12:39 -080063 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.org9d76b4e2012-02-28 23:39:31 +000067 return uniform_avg_;
68}
69
70void VCMContentMetricsProcessing::ResetShortTermAvgData() {
71 // Reset.
72 avg_motion_level_ = 0.0f;
73 avg_spatial_level_ = 0.0f;
74 frame_cnt_uniform_avg_ = 0;
75}
76
77int VCMContentMetricsProcessing::UpdateContentData(
philipel9d3ab612015-12-21 04:12:39 -080078 const VideoContentMetrics* contentMetrics) {
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000079 if (contentMetrics == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000080 return VCM_OK;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000081 }
82 return ProcessContent(contentMetrics);
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000085int VCMContentMetricsProcessing::ProcessContent(
philipel9d3ab612015-12-21 04:12:39 -080086 const VideoContentMetrics* contentMetrics) {
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000087 // 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.com470e71d2011-07-07 08:21:25 +000094}
95
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000096void VCMContentMetricsProcessing::UpdateUniformAvg(
philipel9d3ab612015-12-21 04:12:39 -080097 const VideoContentMetrics* contentMetrics) {
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000098 // 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;
philipel9d3ab612015-12-21 04:12:39 -0800102 avg_spatial_level_ += contentMetrics->spatial_pred_err;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000103 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104}
105
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000106void VCMContentMetricsProcessing::UpdateRecursiveAvg(
philipel9d3ab612015-12-21 04:12:39 -0800107 const VideoContentMetrics* contentMetrics) {
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000108 // Spatial metrics: 2x2, 1x2(H), 2x1(V).
philipel9d3ab612015-12-21 04:12:39 -0800109 recursive_avg_->spatial_pred_err =
110 (1 - recursive_avg_factor_) * recursive_avg_->spatial_pred_err +
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000111 recursive_avg_factor_ * contentMetrics->spatial_pred_err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000112
philipel9d3ab612015-12-21 04:12:39 -0800113 recursive_avg_->spatial_pred_err_h =
114 (1 - recursive_avg_factor_) * recursive_avg_->spatial_pred_err_h +
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000115 recursive_avg_factor_ * contentMetrics->spatial_pred_err_h;
116
philipel9d3ab612015-12-21 04:12:39 -0800117 recursive_avg_->spatial_pred_err_v =
118 (1 - recursive_avg_factor_) * recursive_avg_->spatial_pred_err_v +
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000119 recursive_avg_factor_ * contentMetrics->spatial_pred_err_v;
120
121 // Motion metric: Derived from NFD (normalized frame difference).
philipel9d3ab612015-12-21 04:12:39 -0800122 recursive_avg_->motion_magnitude =
123 (1 - recursive_avg_factor_) * recursive_avg_->motion_magnitude +
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000124 recursive_avg_factor_ * contentMetrics->motion_magnitude;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125}
philipel9d3ab612015-12-21 04:12:39 -0800126} // namespace webrtc