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() |
Peter Boström | ad6fc5a | 2016-05-12 03:01:31 +0200 | [diff] [blame] | 18 | : resampled_frame_(), frame_cnt_(0) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 19 | spatial_resampler_ = new VPMSimpleSpatialResampler(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 20 | vd_ = new VPMVideoDecimator(); |
nisse | 90c335a | 2016-04-27 00:59:22 -0700 | [diff] [blame] | 21 | EnableDenoising(false); |
jackychen | afaae0d | 2016-04-12 23:02:55 -0700 | [diff] [blame] | 22 | denoised_frame_toggle_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | } |
| 24 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 25 | VPMFramePreprocessor::~VPMFramePreprocessor() { |
| 26 | Reset(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 27 | delete vd_; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 28 | delete spatial_resampler_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | } |
| 30 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 31 | void VPMFramePreprocessor::Reset() { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 32 | vd_->Reset(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 33 | spatial_resampler_->Reset(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 34 | frame_cnt_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
| 36 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 37 | void VPMFramePreprocessor::EnableTemporalDecimation(bool enable) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 38 | vd_->EnableTemporalDecimation(enable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 41 | void VPMFramePreprocessor::SetInputFrameResampleMode( |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 42 | VideoFrameResampling resampling_mode) { |
| 43 | spatial_resampler_->SetInputFrameResampleMode(resampling_mode); |
| 44 | } |
| 45 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 46 | int32_t VPMFramePreprocessor::SetTargetResolution(uint32_t width, |
| 47 | uint32_t height, |
| 48 | uint32_t frame_rate) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 49 | if ((width == 0) || (height == 0) || (frame_rate == 0)) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 50 | return VPM_PARAMETER_ERROR; |
| 51 | } |
| 52 | int32_t ret_val = 0; |
| 53 | ret_val = spatial_resampler_->SetTargetFrameSize(width, height); |
| 54 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 55 | if (ret_val < 0) |
| 56 | return ret_val; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 57 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 58 | vd_->SetTargetFramerate(frame_rate); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 59 | return VPM_OK; |
| 60 | } |
| 61 | |
| 62 | void VPMFramePreprocessor::UpdateIncomingframe_rate() { |
| 63 | vd_->UpdateIncomingframe_rate(); |
| 64 | } |
| 65 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 66 | uint32_t VPMFramePreprocessor::GetDecimatedFrameRate() { |
| 67 | return vd_->GetDecimatedFrameRate(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 68 | } |
| 69 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 70 | uint32_t VPMFramePreprocessor::GetDecimatedWidth() const { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 71 | return spatial_resampler_->TargetWidth(); |
| 72 | } |
| 73 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 74 | uint32_t VPMFramePreprocessor::GetDecimatedHeight() const { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 75 | return spatial_resampler_->TargetHeight(); |
| 76 | } |
| 77 | |
nisse | 90c335a | 2016-04-27 00:59:22 -0700 | [diff] [blame] | 78 | void VPMFramePreprocessor::EnableDenoising(bool enable) { |
jackychen | f0b8a37 | 2016-01-19 18:18:52 -0800 | [diff] [blame] | 79 | if (enable) { |
| 80 | denoiser_.reset(new VideoDenoiser(true)); |
| 81 | } else { |
| 82 | denoiser_.reset(); |
| 83 | } |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | const VideoFrame* VPMFramePreprocessor::PreprocessFrame( |
| 87 | const VideoFrame& frame) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 88 | if (frame.IsZeroSize()) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 89 | return nullptr; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | vd_->UpdateIncomingframe_rate(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 93 | if (vd_->DropFrame()) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 94 | return nullptr; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 95 | } |
| 96 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 97 | const VideoFrame* current_frame = &frame; |
| 98 | if (denoiser_) { |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 99 | rtc::scoped_refptr<I420Buffer>* denoised_buffer = &denoised_buffer_[0]; |
| 100 | rtc::scoped_refptr<I420Buffer>* denoised_buffer_prev = &denoised_buffer_[1]; |
jackychen | afaae0d | 2016-04-12 23:02:55 -0700 | [diff] [blame] | 101 | // Swap the buffer to save one memcpy in DenoiseFrame. |
| 102 | if (denoised_frame_toggle_) { |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 103 | denoised_buffer = &denoised_buffer_[1]; |
| 104 | denoised_buffer_prev = &denoised_buffer_[0]; |
jackychen | afaae0d | 2016-04-12 23:02:55 -0700 | [diff] [blame] | 105 | } |
| 106 | // Invert the flag. |
| 107 | denoised_frame_toggle_ ^= 1; |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 108 | denoiser_->DenoiseFrame(current_frame->video_frame_buffer(), |
| 109 | denoised_buffer, |
| 110 | denoised_buffer_prev, true); |
| 111 | denoised_frame_ = VideoFrame(*denoised_buffer, |
| 112 | current_frame->timestamp(), |
| 113 | current_frame->render_time_ms(), |
| 114 | current_frame->rotation()); |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 115 | current_frame = &denoised_frame_; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 116 | } |
| 117 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 118 | if (spatial_resampler_->ApplyResample(current_frame->width(), |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 119 | current_frame->height())) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 120 | if (spatial_resampler_->ResampleFrame(*current_frame, &resampled_frame_) != |
| 121 | VPM_OK) { |
| 122 | return nullptr; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 123 | } |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 124 | current_frame = &resampled_frame_; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 125 | } |
| 126 | |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 127 | ++frame_cnt_; |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 128 | return current_frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | } |
| 130 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame] | 131 | } // namespace webrtc |