niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 9a798d3 | 2012-02-20 09:00:35 +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 | */ |
pbos | 854e84c | 2015-11-16 16:39:06 -0800 | [diff] [blame] | 10 | #include "webrtc/base/logging.h" |
Henrik Kjellander | 0f59a88 | 2015-11-18 22:31:24 +0100 | [diff] [blame^] | 11 | #include "webrtc/modules/video_processing/video_processing_impl.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 12 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 14 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 18 | namespace { |
| 19 | void 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.org | 0e196e1 | 2012-10-19 15:43:31 +0000 | [diff] [blame] | 32 | stats->subSamplWidth = 0; |
| 33 | stats->subSamplHeight = 0; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 34 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 36 | } // namespace |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 37 | |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 38 | VideoProcessingModule* VideoProcessingModule::Create() { |
| 39 | return new VideoProcessingModuleImpl(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void VideoProcessingModule::Destroy(VideoProcessingModule* module) { |
| 43 | if (module) |
| 44 | delete static_cast<VideoProcessingModuleImpl*>(module); |
| 45 | } |
| 46 | |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 47 | VideoProcessingModuleImpl::VideoProcessingModuleImpl() {} |
| 48 | VideoProcessingModuleImpl::~VideoProcessingModuleImpl() {} |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 49 | |
| 50 | void VideoProcessingModuleImpl::Reset() { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 51 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 52 | deflickering_.Reset(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 53 | brightness_detection_.Reset(); |
| 54 | frame_pre_processor_.Reset(); |
| 55 | } |
| 56 | |
| 57 | int32_t VideoProcessingModule::GetFrameStats(FrameStats* stats, |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 58 | const VideoFrame& frame) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 59 | if (frame.IsZeroSize()) { |
asapersson@webrtc.org | 2a77082 | 2014-04-10 11:30:49 +0000 | [diff] [blame] | 60 | LOG(LS_ERROR) << "Zero size frame."; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 61 | 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 | |
| 90 | bool VideoProcessingModule::ValidFrameStats(const FrameStats& stats) { |
asapersson@webrtc.org | 2a77082 | 2014-04-10 11:30:49 +0000 | [diff] [blame] | 91 | if (stats.num_pixels == 0) { |
| 92 | LOG(LS_WARNING) << "Invalid frame stats."; |
| 93 | return false; |
| 94 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | |
| 98 | void 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-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 107 | int32_t VideoProcessingModule::Brighten(VideoFrame* frame, int delta) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 108 | return VideoProcessing::Brighten(frame, delta); |
| 109 | } |
| 110 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 111 | int32_t VideoProcessingModuleImpl::Deflickering(VideoFrame* frame, |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 112 | FrameStats* stats) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 113 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 114 | return deflickering_.ProcessFrame(frame, stats); |
| 115 | } |
| 116 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 117 | int32_t VideoProcessingModuleImpl::BrightnessDetection( |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 118 | const VideoFrame& frame, |
| 119 | const FrameStats& stats) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 120 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 121 | return brightness_detection_.ProcessFrame(frame, stats); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | void VideoProcessingModuleImpl::EnableTemporalDecimation(bool enable) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 126 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 127 | frame_pre_processor_.EnableTemporalDecimation(enable); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | void VideoProcessingModuleImpl::SetInputFrameResampleMode(VideoFrameResampling |
| 132 | resampling_mode) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 133 | rtc::CritScope cs(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 134 | frame_pre_processor_.SetInputFrameResampleMode(resampling_mode); |
| 135 | } |
| 136 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 137 | int32_t VideoProcessingModuleImpl::SetTargetResolution(uint32_t width, |
| 138 | uint32_t height, |
| 139 | uint32_t frame_rate) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 140 | rtc::CritScope cs(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 141 | return frame_pre_processor_.SetTargetResolution(width, height, frame_rate); |
| 142 | } |
| 143 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 144 | void VideoProcessingModuleImpl::SetTargetFramerate(int frame_rate) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 145 | rtc::CritScope cs(&mutex_); |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 146 | frame_pre_processor_.SetTargetFramerate(frame_rate); |
| 147 | } |
| 148 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 149 | uint32_t VideoProcessingModuleImpl::Decimatedframe_rate() { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 150 | rtc::CritScope cs(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 151 | return frame_pre_processor_.Decimatedframe_rate(); |
| 152 | } |
| 153 | |
| 154 | uint32_t VideoProcessingModuleImpl::DecimatedWidth() const { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 155 | rtc::CritScope cs(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 156 | return frame_pre_processor_.DecimatedWidth(); |
| 157 | } |
| 158 | |
| 159 | uint32_t VideoProcessingModuleImpl::DecimatedHeight() const { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 160 | rtc::CritScope cs(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 161 | return frame_pre_processor_.DecimatedHeight(); |
| 162 | } |
| 163 | |
| 164 | int32_t VideoProcessingModuleImpl::PreprocessFrame( |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 165 | const VideoFrame& frame, |
| 166 | VideoFrame** processed_frame) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 167 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 168 | return frame_pre_processor_.PreprocessFrame(frame, processed_frame); |
| 169 | } |
| 170 | |
| 171 | VideoContentMetrics* VideoProcessingModuleImpl::ContentMetrics() const { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 172 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 173 | return frame_pre_processor_.ContentMetrics(); |
| 174 | } |
| 175 | |
| 176 | void VideoProcessingModuleImpl::EnableContentAnalysis(bool enable) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 177 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 178 | frame_pre_processor_.EnableContentAnalysis(enable); |
| 179 | } |
| 180 | |
| 181 | } // namespace webrtc |