blob: 898b02e8f7f9386574aca7534e39e638f59496d7 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
xians@webrtc.org9a798d32012-02-20 09:00:35 +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 */
pbos854e84c2015-11-16 16:39:06 -080010#include "webrtc/base/logging.h"
Henrik Kjellander0f59a882015-11-18 22:31:24 +010011#include "webrtc/modules/video_processing/video_processing_impl.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010012#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000013
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16namespace webrtc {
17
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000018namespace {
19void SetSubSampling(VideoProcessingModule::FrameStats* stats,
20 const int32_t width,
21 const int32_t height) {
22 if (width * height >= 640 * 480) {
23 stats->subSamplWidth = 3;
24 stats->subSamplHeight = 3;
25 } else if (width * height >= 352 * 288) {
26 stats->subSamplWidth = 2;
27 stats->subSamplHeight = 2;
28 } else if (width * height >= 176 * 144) {
29 stats->subSamplWidth = 1;
30 stats->subSamplHeight = 1;
31 } else {
mikhal@webrtc.org0e196e12012-10-19 15:43:31 +000032 stats->subSamplWidth = 0;
33 stats->subSamplHeight = 0;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000034 }
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +000036} // namespace
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000037
Peter Boströmf4aa4c22015-09-18 12:24:25 +020038VideoProcessingModule* VideoProcessingModule::Create() {
39 return new VideoProcessingModuleImpl();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000040}
41
42void VideoProcessingModule::Destroy(VideoProcessingModule* module) {
43 if (module)
44 delete static_cast<VideoProcessingModuleImpl*>(module);
45}
46
Peter Boströmf4aa4c22015-09-18 12:24:25 +020047VideoProcessingModuleImpl::VideoProcessingModuleImpl() {}
48VideoProcessingModuleImpl::~VideoProcessingModuleImpl() {}
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000049
50void VideoProcessingModuleImpl::Reset() {
Peter Boströmf4aa4c22015-09-18 12:24:25 +020051 rtc::CritScope mutex(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000052 deflickering_.Reset();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000053 brightness_detection_.Reset();
54 frame_pre_processor_.Reset();
55}
56
57int32_t VideoProcessingModule::GetFrameStats(FrameStats* stats,
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070058 const VideoFrame& frame) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000059 if (frame.IsZeroSize()) {
asapersson@webrtc.org2a770822014-04-10 11:30:49 +000060 LOG(LS_ERROR) << "Zero size frame.";
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000061 return VPM_PARAMETER_ERROR;
62 }
63
64 int width = frame.width();
65 int height = frame.height();
66
67 ClearFrameStats(stats); // The histogram needs to be zeroed out.
68 SetSubSampling(stats, width, height);
69
70 const uint8_t* buffer = frame.buffer(kYPlane);
71 // Compute histogram and sum of frame
72 for (int i = 0; i < height; i += (1 << stats->subSamplHeight)) {
73 int k = i * width;
74 for (int j = 0; j < width; j += (1 << stats->subSamplWidth)) {
75 stats->hist[buffer[k + j]]++;
76 stats->sum += buffer[k + j];
77 }
78 }
79
80 stats->num_pixels = (width * height) / ((1 << stats->subSamplWidth) *
81 (1 << stats->subSamplHeight));
82 assert(stats->num_pixels > 0);
83
84 // Compute mean value of frame
85 stats->mean = stats->sum / stats->num_pixels;
86
87 return VPM_OK;
88}
89
90bool VideoProcessingModule::ValidFrameStats(const FrameStats& stats) {
asapersson@webrtc.org2a770822014-04-10 11:30:49 +000091 if (stats.num_pixels == 0) {
92 LOG(LS_WARNING) << "Invalid frame stats.";
93 return false;
94 }
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000095 return true;
96}
97
98void VideoProcessingModule::ClearFrameStats(FrameStats* stats) {
99 stats->mean = 0;
100 stats->sum = 0;
101 stats->num_pixels = 0;
102 stats->subSamplWidth = 0;
103 stats->subSamplHeight = 0;
104 memset(stats->hist, 0, sizeof(stats->hist));
105}
106
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700107int32_t VideoProcessingModule::Brighten(VideoFrame* frame, int delta) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000108 return VideoProcessing::Brighten(frame, delta);
109}
110
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700111int32_t VideoProcessingModuleImpl::Deflickering(VideoFrame* frame,
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000112 FrameStats* stats) {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200113 rtc::CritScope mutex(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000114 return deflickering_.ProcessFrame(frame, stats);
115}
116
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000117int32_t VideoProcessingModuleImpl::BrightnessDetection(
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700118 const VideoFrame& frame,
119 const FrameStats& stats) {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200120 rtc::CritScope mutex(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000121 return brightness_detection_.ProcessFrame(frame, stats);
122}
123
124
125void VideoProcessingModuleImpl::EnableTemporalDecimation(bool enable) {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200126 rtc::CritScope mutex(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000127 frame_pre_processor_.EnableTemporalDecimation(enable);
128}
129
130
131void VideoProcessingModuleImpl::SetInputFrameResampleMode(VideoFrameResampling
132 resampling_mode) {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200133 rtc::CritScope cs(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000134 frame_pre_processor_.SetInputFrameResampleMode(resampling_mode);
135}
136
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000137int32_t VideoProcessingModuleImpl::SetTargetResolution(uint32_t width,
138 uint32_t height,
139 uint32_t frame_rate) {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200140 rtc::CritScope cs(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000141 return frame_pre_processor_.SetTargetResolution(width, height, frame_rate);
142}
143
jackychen6e2ce6e2015-07-13 16:26:33 -0700144void VideoProcessingModuleImpl::SetTargetFramerate(int frame_rate) {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200145 rtc::CritScope cs(&mutex_);
jackychen6e2ce6e2015-07-13 16:26:33 -0700146 frame_pre_processor_.SetTargetFramerate(frame_rate);
147}
148
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000149uint32_t VideoProcessingModuleImpl::Decimatedframe_rate() {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200150 rtc::CritScope cs(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000151 return frame_pre_processor_.Decimatedframe_rate();
152}
153
154uint32_t VideoProcessingModuleImpl::DecimatedWidth() const {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200155 rtc::CritScope cs(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000156 return frame_pre_processor_.DecimatedWidth();
157}
158
159uint32_t VideoProcessingModuleImpl::DecimatedHeight() const {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200160 rtc::CritScope cs(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000161 return frame_pre_processor_.DecimatedHeight();
162}
163
164int32_t VideoProcessingModuleImpl::PreprocessFrame(
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700165 const VideoFrame& frame,
166 VideoFrame** processed_frame) {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200167 rtc::CritScope mutex(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000168 return frame_pre_processor_.PreprocessFrame(frame, processed_frame);
169}
170
171VideoContentMetrics* VideoProcessingModuleImpl::ContentMetrics() const {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200172 rtc::CritScope mutex(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000173 return frame_pre_processor_.ContentMetrics();
174}
175
176void VideoProcessingModuleImpl::EnableContentAnalysis(bool enable) {
Peter Boströmf4aa4c22015-09-18 12:24:25 +0200177 rtc::CritScope mutex(&mutex_);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000178 frame_pre_processor_.EnableContentAnalysis(enable);
179}
180
181} // namespace webrtc