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 | */ |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 10 | |
Henrik Kjellander | 0f59a88 | 2015-11-18 22:31:24 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/video_processing/video_processing_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 15 | #include "webrtc/base/checks.h" |
| 16 | #include "webrtc/base/logging.h" |
| 17 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
| 18 | |
| 19 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 22 | namespace { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 23 | |
| 24 | int GetSubSamplingFactor(int width, int height) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 25 | if (width * height >= 640 * 480) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 26 | return 3; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 27 | } else if (width * height >= 352 * 288) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 28 | return 2; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 29 | } else if (width * height >= 176 * 144) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 30 | return 1; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 31 | } else { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 32 | return 0; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 33 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 35 | } // namespace |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 36 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 37 | VideoProcessing* VideoProcessing::Create() { |
| 38 | return new VideoProcessingImpl(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 39 | } |
| 40 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 41 | VideoProcessingImpl::VideoProcessingImpl() {} |
| 42 | VideoProcessingImpl::~VideoProcessingImpl() {} |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 43 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 44 | void VideoProcessing::GetFrameStats(const VideoFrame& frame, |
| 45 | FrameStats* stats) { |
| 46 | ClearFrameStats(stats); // The histogram needs to be zeroed out. |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 47 | if (frame.IsZeroSize()) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 48 | return; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | int width = frame.width(); |
| 52 | int height = frame.height(); |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 53 | stats->sub_sampling_factor = GetSubSamplingFactor(width, height); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 54 | |
| 55 | const uint8_t* buffer = frame.buffer(kYPlane); |
| 56 | // Compute histogram and sum of frame |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 57 | for (int i = 0; i < height; i += (1 << stats->sub_sampling_factor)) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 58 | int k = i * width; |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 59 | for (int j = 0; j < width; j += (1 << stats->sub_sampling_factor)) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 60 | stats->hist[buffer[k + j]]++; |
| 61 | stats->sum += buffer[k + j]; |
| 62 | } |
| 63 | } |
| 64 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 65 | stats->num_pixels = (width * height) / |
| 66 | ((1 << stats->sub_sampling_factor) * (1 << stats->sub_sampling_factor)); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 67 | assert(stats->num_pixels > 0); |
| 68 | |
| 69 | // Compute mean value of frame |
| 70 | stats->mean = stats->sum / stats->num_pixels; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 71 | } |
| 72 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 73 | bool VideoProcessing::ValidFrameStats(const FrameStats& stats) { |
asapersson@webrtc.org | 2a77082 | 2014-04-10 11:30:49 +0000 | [diff] [blame] | 74 | if (stats.num_pixels == 0) { |
| 75 | LOG(LS_WARNING) << "Invalid frame stats."; |
| 76 | return false; |
| 77 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 81 | void VideoProcessing::ClearFrameStats(FrameStats* stats) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 82 | stats->mean = 0; |
| 83 | stats->sum = 0; |
| 84 | stats->num_pixels = 0; |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 85 | stats->sub_sampling_factor = 0; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 86 | memset(stats->hist, 0, sizeof(stats->hist)); |
| 87 | } |
| 88 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 89 | void VideoProcessing::Brighten(int delta, VideoFrame* frame) { |
| 90 | RTC_DCHECK(!frame->IsZeroSize()); |
| 91 | RTC_DCHECK(frame->width() > 0); |
| 92 | RTC_DCHECK(frame->height() > 0); |
| 93 | |
| 94 | int num_pixels = frame->width() * frame->height(); |
| 95 | |
| 96 | int look_up[256]; |
| 97 | for (int i = 0; i < 256; i++) { |
| 98 | int val = i + delta; |
| 99 | look_up[i] = ((((val < 0) ? 0 : val) > 255) ? 255 : val); |
| 100 | } |
| 101 | |
| 102 | uint8_t* temp_ptr = frame->buffer(kYPlane); |
| 103 | for (int i = 0; i < num_pixels; i++) { |
| 104 | *temp_ptr = static_cast<uint8_t>(look_up[*temp_ptr]); |
| 105 | temp_ptr++; |
| 106 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 107 | } |
| 108 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 109 | int32_t VideoProcessingImpl::Deflickering(VideoFrame* frame, |
| 110 | FrameStats* stats) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 111 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 112 | return deflickering_.ProcessFrame(frame, stats); |
| 113 | } |
| 114 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 115 | int32_t VideoProcessingImpl::BrightnessDetection( |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 116 | const VideoFrame& frame, |
| 117 | const FrameStats& stats) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 118 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 119 | return brightness_detection_.ProcessFrame(frame, stats); |
| 120 | } |
| 121 | |
| 122 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 123 | void VideoProcessingImpl::EnableTemporalDecimation(bool enable) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 124 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 125 | frame_pre_processor_.EnableTemporalDecimation(enable); |
| 126 | } |
| 127 | |
| 128 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 129 | void VideoProcessingImpl::SetInputFrameResampleMode(VideoFrameResampling |
| 130 | resampling_mode) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 131 | rtc::CritScope cs(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 132 | frame_pre_processor_.SetInputFrameResampleMode(resampling_mode); |
| 133 | } |
| 134 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 135 | int32_t VideoProcessingImpl::SetTargetResolution(uint32_t width, |
| 136 | uint32_t height, |
| 137 | uint32_t frame_rate) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 138 | rtc::CritScope cs(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 139 | return frame_pre_processor_.SetTargetResolution(width, height, frame_rate); |
| 140 | } |
| 141 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 142 | void VideoProcessingImpl::SetTargetFramerate(int frame_rate) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 143 | rtc::CritScope cs(&mutex_); |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 144 | frame_pre_processor_.SetTargetFramerate(frame_rate); |
| 145 | } |
| 146 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 147 | uint32_t VideoProcessingImpl::GetDecimatedFrameRate() { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 148 | rtc::CritScope cs(&mutex_); |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 149 | return frame_pre_processor_.GetDecimatedFrameRate(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 150 | } |
| 151 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 152 | uint32_t VideoProcessingImpl::GetDecimatedWidth() const { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 153 | rtc::CritScope cs(&mutex_); |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 154 | return frame_pre_processor_.GetDecimatedWidth(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 155 | } |
| 156 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 157 | uint32_t VideoProcessingImpl::GetDecimatedHeight() const { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 158 | rtc::CritScope cs(&mutex_); |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 159 | return frame_pre_processor_.GetDecimatedHeight(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 160 | } |
| 161 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 162 | void VideoProcessingImpl::EnableDenosing(bool enable) { |
| 163 | rtc::CritScope cs(&mutex_); |
| 164 | frame_pre_processor_.EnableDenosing(enable); |
| 165 | } |
| 166 | |
| 167 | const VideoFrame* VideoProcessingImpl::PreprocessFrame( |
| 168 | const VideoFrame& frame) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 169 | rtc::CritScope mutex(&mutex_); |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 170 | return frame_pre_processor_.PreprocessFrame(frame); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 171 | } |
| 172 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 173 | VideoContentMetrics* VideoProcessingImpl::GetContentMetrics() const { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 174 | rtc::CritScope mutex(&mutex_); |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 175 | return frame_pre_processor_.GetContentMetrics(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 176 | } |
| 177 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 178 | void VideoProcessingImpl::EnableContentAnalysis(bool enable) { |
Peter Boström | f4aa4c2 | 2015-09-18 12:24:25 +0200 | [diff] [blame] | 179 | rtc::CritScope mutex(&mutex_); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 180 | frame_pre_processor_.EnableContentAnalysis(enable); |
| 181 | } |
| 182 | |
| 183 | } // namespace webrtc |