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