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