blob: 0c3a6dbc6c562f655168c73e2766ab0da688a7f8 [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) {
47 // Update factor for recursive averaging.
philipel9d3ab612015-12-21 04:12:39 -080048 recursive_avg_factor_ = static_cast<float>(1000.0f) /
49 static_cast<float>(frameRate * kQmMinIntervalMs);
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000050}
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.
philipel9d3ab612015-12-21 04:12:39 -080061 uniform_avg_->motion_magnitude =
62 avg_motion_level_ / static_cast<float>(frame_cnt_uniform_avg_);
63 uniform_avg_->spatial_pred_err =
64 avg_spatial_level_ / static_cast<float>(frame_cnt_uniform_avg_);
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000065 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(
philipel9d3ab612015-12-21 04:12:39 -080076 const VideoContentMetrics* contentMetrics) {
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000077 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(
philipel9d3ab612015-12-21 04:12:39 -080084 const VideoContentMetrics* contentMetrics) {
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000085 // 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(
philipel9d3ab612015-12-21 04:12:39 -080095 const VideoContentMetrics* contentMetrics) {
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +000096 // 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;
philipel9d3ab612015-12-21 04:12:39 -0800100 avg_spatial_level_ += contentMetrics->spatial_pred_err;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000101 return;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102}
103
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000104void VCMContentMetricsProcessing::UpdateRecursiveAvg(
philipel9d3ab612015-12-21 04:12:39 -0800105 const VideoContentMetrics* contentMetrics) {
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000106 // Spatial metrics: 2x2, 1x2(H), 2x1(V).
philipel9d3ab612015-12-21 04:12:39 -0800107 recursive_avg_->spatial_pred_err =
108 (1 - recursive_avg_factor_) * recursive_avg_->spatial_pred_err +
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000109 recursive_avg_factor_ * contentMetrics->spatial_pred_err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
philipel9d3ab612015-12-21 04:12:39 -0800111 recursive_avg_->spatial_pred_err_h =
112 (1 - recursive_avg_factor_) * recursive_avg_->spatial_pred_err_h +
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000113 recursive_avg_factor_ * contentMetrics->spatial_pred_err_h;
114
philipel9d3ab612015-12-21 04:12:39 -0800115 recursive_avg_->spatial_pred_err_v =
116 (1 - recursive_avg_factor_) * recursive_avg_->spatial_pred_err_v +
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000117 recursive_avg_factor_ * contentMetrics->spatial_pred_err_v;
118
119 // Motion metric: Derived from NFD (normalized frame difference).
philipel9d3ab612015-12-21 04:12:39 -0800120 recursive_avg_->motion_magnitude =
121 (1 - recursive_avg_factor_) * recursive_avg_->motion_magnitude +
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +0000122 recursive_avg_factor_ * contentMetrics->motion_magnitude;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123}
philipel9d3ab612015-12-21 04:12:39 -0800124} // namespace webrtc