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(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | } |
| 26 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 27 | VPMFramePreprocessor::~VPMFramePreprocessor() { |
| 28 | Reset(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 29 | delete ca_; |
| 30 | delete vd_; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 31 | delete spatial_resampler_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 34 | void VPMFramePreprocessor::Reset() { |
| 35 | ca_->Release(); |
| 36 | vd_->Reset(); |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 37 | content_metrics_ = nullptr; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 38 | spatial_resampler_->Reset(); |
| 39 | enable_ca_ = false; |
| 40 | frame_cnt_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | } |
| 42 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 43 | void VPMFramePreprocessor::EnableTemporalDecimation(bool enable) { |
| 44 | vd_->EnableTemporalDecimation(enable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } |
| 46 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 47 | void VPMFramePreprocessor::EnableContentAnalysis(bool enable) { |
| 48 | enable_ca_ = enable; |
| 49 | } |
| 50 | |
| 51 | void VPMFramePreprocessor::SetInputFrameResampleMode( |
| 52 | VideoFrameResampling resampling_mode) { |
| 53 | spatial_resampler_->SetInputFrameResampleMode(resampling_mode); |
| 54 | } |
| 55 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 56 | int32_t VPMFramePreprocessor::SetTargetResolution( |
| 57 | uint32_t width, uint32_t height, uint32_t frame_rate) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 58 | if ((width == 0) || (height == 0) || (frame_rate == 0)) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 59 | return VPM_PARAMETER_ERROR; |
| 60 | } |
| 61 | int32_t ret_val = 0; |
| 62 | ret_val = spatial_resampler_->SetTargetFrameSize(width, height); |
| 63 | |
| 64 | if (ret_val < 0) return ret_val; |
| 65 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 66 | vd_->SetTargetFramerate(frame_rate); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 67 | return VPM_OK; |
| 68 | } |
| 69 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 70 | void VPMFramePreprocessor::SetTargetFramerate(int frame_rate) { |
| 71 | if (frame_rate == -1) { |
| 72 | vd_->EnableTemporalDecimation(false); |
| 73 | } else { |
| 74 | vd_->EnableTemporalDecimation(true); |
| 75 | vd_->SetTargetFramerate(frame_rate); |
| 76 | } |
| 77 | } |
| 78 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 79 | void VPMFramePreprocessor::UpdateIncomingframe_rate() { |
| 80 | vd_->UpdateIncomingframe_rate(); |
| 81 | } |
| 82 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 83 | uint32_t VPMFramePreprocessor::GetDecimatedFrameRate() { |
| 84 | return vd_->GetDecimatedFrameRate(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 88 | uint32_t VPMFramePreprocessor::GetDecimatedWidth() const { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 89 | return spatial_resampler_->TargetWidth(); |
| 90 | } |
| 91 | |
| 92 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 93 | uint32_t VPMFramePreprocessor::GetDecimatedHeight() const { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 94 | return spatial_resampler_->TargetHeight(); |
| 95 | } |
| 96 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 97 | void VPMFramePreprocessor::EnableDenosing(bool enable) { |
| 98 | denoiser_.reset(new VideoDenoiser()); |
| 99 | } |
| 100 | |
| 101 | const VideoFrame* VPMFramePreprocessor::PreprocessFrame( |
| 102 | const VideoFrame& frame) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 103 | if (frame.IsZeroSize()) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 104 | return nullptr; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | vd_->UpdateIncomingframe_rate(); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 108 | if (vd_->DropFrame()) { |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 109 | return nullptr; |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 110 | } |
| 111 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 112 | const VideoFrame* current_frame = &frame; |
| 113 | if (denoiser_) { |
| 114 | denoiser_->DenoiseFrame(*current_frame, &denoised_frame_); |
| 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(), |
| 119 | current_frame->height())) { |
| 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 | |
| 127 | // Perform content analysis on the frame to be encoded. |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 128 | if (enable_ca_ && frame_cnt_ % kSkipFrameCA == 0) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 129 | // Compute new metrics every |kSkipFramesCA| frames, starting with |
| 130 | // the first frame. |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 131 | content_metrics_ = ca_->ComputeContentMetrics(*current_frame); |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 132 | } |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 133 | ++frame_cnt_; |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 134 | return current_frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | } |
| 136 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 137 | VideoContentMetrics* VPMFramePreprocessor::GetContentMetrics() const { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 138 | return content_metrics_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
mflodman | a856542 | 2015-12-07 01:09:52 -0800 | [diff] [blame^] | 141 | } // namespace webrtc |