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