blob: 3b94098b87b7569caf12707e52622610a39198de [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;
41 avg_motion_level_ = 0.0f;
42 avg_spatial_level_ = 0.0f;
43 return VCM_OK;
44}
45
46void VCMContentMetricsProcessing::UpdateFrameRate(uint32_t frameRate) {
47 // Update factor for recursive averaging.
48 recursive_avg_factor_ = static_cast<float> (1000.0f) /
49 static_cast<float>(frameRate * kQmMinIntervalMs);
50}
51
52VideoContentMetrics* VCMContentMetricsProcessing::LongTermAvgData() {
53 return recursive_avg_;
54}
55
56VideoContentMetrics* VCMContentMetricsProcessing::ShortTermAvgData() {
57 if (frame_cnt_uniform_avg_ == 0) {
58 return NULL;
59 }
60 // Two metrics are used: motion and spatial level.
61 uniform_avg_->motion_magnitude = avg_motion_level_ /
62 static_cast<float>(frame_cnt_uniform_avg_);
63 uniform_avg_->spatial_pred_err = avg_spatial_level_ /
64 static_cast<float>(frame_cnt_uniform_avg_);
65 return uniform_avg_;
66}
67
68void VCMContentMetricsProcessing::ResetShortTermAvgData() {
69 // Reset.
70 avg_motion_level_ = 0.0f;
71 avg_spatial_level_ = 0.0f;
72 frame_cnt_uniform_avg_ = 0;
73}
74
75int VCMContentMetricsProcessing::UpdateContentData(
76 const VideoContentMetrics *contentMetrics) {
77 if (contentMetrics == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000078 return VCM_OK;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000079 }
80 return ProcessContent(contentMetrics);
niklase@google.com470e71d2011-07-07 08:21:25 +000081}
82
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000083int VCMContentMetricsProcessing::ProcessContent(
84 const VideoContentMetrics *contentMetrics) {
85 // Update the recursive averaged metrics: average is over longer window
86 // of time: over QmMinIntervalMs ms.
87 UpdateRecursiveAvg(contentMetrics);
88 // Update the uniform averaged metrics: average is over shorter window
89 // of time: based on ~RTCP reports.
90 UpdateUniformAvg(contentMetrics);
91 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000094void VCMContentMetricsProcessing::UpdateUniformAvg(
95 const VideoContentMetrics *contentMetrics) {
96 // Update frame counter.
97 frame_cnt_uniform_avg_ += 1;
98 // Update averaged metrics: motion and spatial level are used.
99 avg_motion_level_ += contentMetrics->motion_magnitude;
100 avg_spatial_level_ += contentMetrics->spatial_pred_err;
101 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102}
103
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000104void VCMContentMetricsProcessing::UpdateRecursiveAvg(
105 const VideoContentMetrics *contentMetrics) {
marpan@webrtc.orgbd5648f2012-02-17 23:16:58 +0000106
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000107 // Spatial metrics: 2x2, 1x2(H), 2x1(V).
108 recursive_avg_->spatial_pred_err = (1 - recursive_avg_factor_) *
109 recursive_avg_->spatial_pred_err +
110 recursive_avg_factor_ * contentMetrics->spatial_pred_err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000111
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000112 recursive_avg_->spatial_pred_err_h = (1 - recursive_avg_factor_) *
113 recursive_avg_->spatial_pred_err_h +
114 recursive_avg_factor_ * contentMetrics->spatial_pred_err_h;
115
116 recursive_avg_->spatial_pred_err_v = (1 - recursive_avg_factor_) *
117 recursive_avg_->spatial_pred_err_v +
118 recursive_avg_factor_ * contentMetrics->spatial_pred_err_v;
119
120 // Motion metric: Derived from NFD (normalized frame difference).
121 recursive_avg_->motion_magnitude = (1 - recursive_avg_factor_) *
122 recursive_avg_->motion_magnitude +
123 recursive_avg_factor_ * contentMetrics->motion_magnitude;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000125} // namespace