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 | |
| 13 | namespace webrtc { |
| 14 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 15 | VPMFramePreprocessor::VPMFramePreprocessor() |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 16 | : content_metrics_(nullptr), |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 17 | resampled_frame_(), |
| 18 | enable_ca_(false), |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 19 | enable_denoising_(false), |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 20 | frame_cnt_(0) { |
| 21 | spatial_resampler_ = new VPMSimpleSpatialResampler(); |
| 22 | ca_ = new VPMContentAnalysis(true); |
| 23 | vd_ = new VPMVideoDecimator(); |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 24 | if (enable_denoising_) { |
| 25 | denoiser_ = new VideoDenoiser(); |
| 26 | } else { |
| 27 | denoiser_ = nullptr; |
| 28 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | } |
| 30 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 31 | VPMFramePreprocessor::~VPMFramePreprocessor() { |
| 32 | Reset(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 33 | delete ca_; |
| 34 | delete vd_; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 35 | if (enable_denoising_) |
| 36 | delete denoiser_; |
| 37 | delete spatial_resampler_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } |
| 39 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 40 | void VPMFramePreprocessor::Reset() { |
| 41 | ca_->Release(); |
| 42 | vd_->Reset(); |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 43 | content_metrics_ = nullptr; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 44 | spatial_resampler_->Reset(); |
| 45 | enable_ca_ = false; |
| 46 | frame_cnt_ = 0; |
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::EnableTemporalDecimation(bool enable) { |
| 50 | vd_->EnableTemporalDecimation(enable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | } |
| 52 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 53 | void VPMFramePreprocessor::EnableContentAnalysis(bool enable) { |
| 54 | enable_ca_ = enable; |
| 55 | } |
| 56 | |
| 57 | void VPMFramePreprocessor::SetInputFrameResampleMode( |
| 58 | VideoFrameResampling resampling_mode) { |
| 59 | spatial_resampler_->SetInputFrameResampleMode(resampling_mode); |
| 60 | } |
| 61 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 62 | int32_t VPMFramePreprocessor::SetTargetResolution( |
| 63 | uint32_t width, uint32_t height, uint32_t frame_rate) { |
| 64 | if ( (width == 0) || (height == 0) || (frame_rate == 0)) { |
| 65 | return VPM_PARAMETER_ERROR; |
| 66 | } |
| 67 | int32_t ret_val = 0; |
| 68 | ret_val = spatial_resampler_->SetTargetFrameSize(width, height); |
| 69 | |
| 70 | if (ret_val < 0) return ret_val; |
| 71 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 72 | vd_->SetTargetFramerate(frame_rate); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 73 | return VPM_OK; |
| 74 | } |
| 75 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 76 | void VPMFramePreprocessor::SetTargetFramerate(int frame_rate) { |
| 77 | if (frame_rate == -1) { |
| 78 | vd_->EnableTemporalDecimation(false); |
| 79 | } else { |
| 80 | vd_->EnableTemporalDecimation(true); |
| 81 | vd_->SetTargetFramerate(frame_rate); |
| 82 | } |
| 83 | } |
| 84 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 85 | void VPMFramePreprocessor::UpdateIncomingframe_rate() { |
| 86 | vd_->UpdateIncomingframe_rate(); |
| 87 | } |
| 88 | |
| 89 | uint32_t VPMFramePreprocessor::Decimatedframe_rate() { |
| 90 | return vd_->Decimatedframe_rate(); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | uint32_t VPMFramePreprocessor::DecimatedWidth() const { |
| 95 | return spatial_resampler_->TargetWidth(); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | uint32_t VPMFramePreprocessor::DecimatedHeight() const { |
| 100 | return spatial_resampler_->TargetHeight(); |
| 101 | } |
| 102 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 103 | int32_t VPMFramePreprocessor::PreprocessFrame(const VideoFrame& frame, |
| 104 | VideoFrame** processed_frame) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 105 | if (frame.IsZeroSize()) { |
| 106 | return VPM_PARAMETER_ERROR; |
| 107 | } |
| 108 | |
| 109 | vd_->UpdateIncomingframe_rate(); |
| 110 | |
| 111 | if (vd_->DropFrame()) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 112 | return 1; // drop 1 frame |
| 113 | } |
| 114 | |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 115 | // Resizing incoming frame if needed. Otherwise, remains nullptr. |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 116 | // We are not allowed to resample the input frame (must make a copy of it). |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 117 | *processed_frame = nullptr; |
| 118 | if (denoiser_ != nullptr) { |
| 119 | denoiser_->DenoiseFrame(frame, &denoised_frame_); |
| 120 | *processed_frame = &denoised_frame_; |
| 121 | } |
| 122 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 123 | if (spatial_resampler_->ApplyResample(frame.width(), frame.height())) { |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 124 | int32_t ret; |
| 125 | if (enable_denoising_) { |
| 126 | ret = spatial_resampler_->ResampleFrame(denoised_frame_, |
| 127 | &resampled_frame_); |
| 128 | } else { |
| 129 | ret = spatial_resampler_->ResampleFrame(frame, &resampled_frame_); |
| 130 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 131 | if (ret != VPM_OK) return ret; |
| 132 | *processed_frame = &resampled_frame_; |
| 133 | } |
| 134 | |
| 135 | // Perform content analysis on the frame to be encoded. |
| 136 | if (enable_ca_) { |
| 137 | // Compute new metrics every |kSkipFramesCA| frames, starting with |
| 138 | // the first frame. |
| 139 | if (frame_cnt_ % kSkipFrameCA == 0) { |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 140 | if (*processed_frame == nullptr) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 141 | content_metrics_ = ca_->ComputeContentMetrics(frame); |
| 142 | } else { |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 143 | content_metrics_ = ca_->ComputeContentMetrics(**processed_frame); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 144 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | } |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 146 | } |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame^] | 147 | ++frame_cnt_; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 148 | return VPM_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 149 | } |
| 150 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 151 | VideoContentMetrics* VPMFramePreprocessor::ContentMetrics() const { |
| 152 | return content_metrics_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | } |
| 154 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 155 | } // namespace |