niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
marpan@webrtc.org | 9d76b4e | 2012-02-28 23:39:31 +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 | */ |
| 10 | |
Henrik Kjellander | 0f59a88 | 2015-11-18 22:31:24 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/video_processing/frame_preprocessor.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 13 | #include "webrtc/modules/video_processing/video_denoiser.h" |
| 14 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 17 | VPMFramePreprocessor::VPMFramePreprocessor() |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 18 | : content_metrics_(nullptr), |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 19 | resampled_frame_(), |
| 20 | enable_ca_(false), |
| 21 | frame_cnt_(0) { |
| 22 | spatial_resampler_ = new VPMSimpleSpatialResampler(); |
| 23 | ca_ = new VPMContentAnalysis(true); |
| 24 | vd_ = new VPMVideoDecimator(); |
jackychen | fa0befe | 2016-04-01 07:46:58 -0700 | [diff] [blame] | 25 | EnableDenosing(false); |
jackychen | afaae0d | 2016-04-12 23:02:55 -0700 | [diff] [blame^] | 26 | denoised_frame_toggle_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | } |
| 28 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 29 | VPMFramePreprocessor::~VPMFramePreprocessor() { |
| 30 | Reset(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 31 | delete ca_; |
| 32 | delete vd_; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 33 | delete spatial_resampler_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | } |
| 35 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 36 | void VPMFramePreprocessor::Reset() { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 37 | ca_->Release(); |
| 38 | vd_->Reset(); |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 39 | content_metrics_ = nullptr; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 40 | spatial_resampler_->Reset(); |
| 41 | enable_ca_ = false; |
| 42 | frame_cnt_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 45 | void VPMFramePreprocessor::EnableTemporalDecimation(bool enable) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 46 | vd_->EnableTemporalDecimation(enable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 49 | void VPMFramePreprocessor::EnableContentAnalysis(bool enable) { |
| 50 | enable_ca_ = enable; |
| 51 | } |
| 52 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 53 | void VPMFramePreprocessor::SetInputFrameResampleMode( |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 54 | VideoFrameResampling resampling_mode) { |
| 55 | spatial_resampler_->SetInputFrameResampleMode(resampling_mode); |
| 56 | } |
| 57 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 58 | int32_t VPMFramePreprocessor::SetTargetResolution(uint32_t width, |
| 59 | uint32_t height, |
| 60 | uint32_t frame_rate) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 61 | if ((width == 0) || (height == 0) || (frame_rate == 0)) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 62 | return VPM_PARAMETER_ERROR; |
| 63 | } |
| 64 | int32_t ret_val = 0; |
| 65 | ret_val = spatial_resampler_->SetTargetFrameSize(width, height); |
| 66 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 67 | if (ret_val < 0) |
| 68 | return ret_val; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 69 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 70 | vd_->SetTargetFramerate(frame_rate); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 71 | return VPM_OK; |
| 72 | } |
| 73 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 74 | void VPMFramePreprocessor::SetTargetFramerate(int frame_rate) { |
| 75 | if (frame_rate == -1) { |
| 76 | vd_->EnableTemporalDecimation(false); |
| 77 | } else { |
| 78 | vd_->EnableTemporalDecimation(true); |
| 79 | vd_->SetTargetFramerate(frame_rate); |
| 80 | } |
| 81 | } |
| 82 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 83 | void VPMFramePreprocessor::UpdateIncomingframe_rate() { |
| 84 | vd_->UpdateIncomingframe_rate(); |
| 85 | } |
| 86 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 87 | uint32_t VPMFramePreprocessor::GetDecimatedFrameRate() { |
| 88 | return vd_->GetDecimatedFrameRate(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 89 | } |
| 90 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 91 | uint32_t VPMFramePreprocessor::GetDecimatedWidth() const { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 92 | return spatial_resampler_->TargetWidth(); |
| 93 | } |
| 94 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 95 | uint32_t VPMFramePreprocessor::GetDecimatedHeight() const { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 96 | return spatial_resampler_->TargetHeight(); |
| 97 | } |
| 98 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 99 | void VPMFramePreprocessor::EnableDenosing(bool enable) { |
jackychen | f0b8a37 | 2016-01-19 18:18:52 -0800 | [diff] [blame] | 100 | if (enable) { |
| 101 | denoiser_.reset(new VideoDenoiser(true)); |
| 102 | } else { |
| 103 | denoiser_.reset(); |
| 104 | } |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | const VideoFrame* VPMFramePreprocessor::PreprocessFrame( |
| 108 | const VideoFrame& frame) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 109 | if (frame.IsZeroSize()) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 110 | return nullptr; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | vd_->UpdateIncomingframe_rate(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 114 | if (vd_->DropFrame()) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 115 | return nullptr; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 116 | } |
| 117 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 118 | const VideoFrame* current_frame = &frame; |
| 119 | if (denoiser_) { |
jackychen | afaae0d | 2016-04-12 23:02:55 -0700 | [diff] [blame^] | 120 | VideoFrame* denoised_frame = &denoised_frame_[0]; |
| 121 | VideoFrame* denoised_frame_prev = &denoised_frame_[1]; |
| 122 | // Swap the buffer to save one memcpy in DenoiseFrame. |
| 123 | if (denoised_frame_toggle_) { |
| 124 | denoised_frame = &denoised_frame_[1]; |
| 125 | denoised_frame_prev = &denoised_frame_[0]; |
| 126 | } |
| 127 | // Invert the flag. |
| 128 | denoised_frame_toggle_ ^= 1; |
| 129 | denoiser_->DenoiseFrame(*current_frame, denoised_frame, denoised_frame_prev, |
| 130 | true); |
| 131 | current_frame = denoised_frame; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 132 | } |
| 133 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 134 | if (spatial_resampler_->ApplyResample(current_frame->width(), |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 135 | current_frame->height())) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 136 | if (spatial_resampler_->ResampleFrame(*current_frame, &resampled_frame_) != |
| 137 | VPM_OK) { |
| 138 | return nullptr; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 139 | } |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 140 | current_frame = &resampled_frame_; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // Perform content analysis on the frame to be encoded. |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 144 | if (enable_ca_ && frame_cnt_ % kSkipFrameCA == 0) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 145 | // Compute new metrics every |kSkipFramesCA| frames, starting with |
| 146 | // the first frame. |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 147 | content_metrics_ = ca_->ComputeContentMetrics(*current_frame); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 148 | } |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 149 | ++frame_cnt_; |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 150 | return current_frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | } |
| 152 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 153 | VideoContentMetrics* VPMFramePreprocessor::GetContentMetrics() const { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 154 | return content_metrics_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | } |
| 156 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 157 | } // namespace webrtc |