niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | ddfdfed | 2012-07-03 13:21:22 +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/spatial_resampler.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | namespace webrtc { |
| 15 | |
| 16 | VPMSimpleSpatialResampler::VPMSimpleSpatialResampler() |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 17 | : resampling_mode_(kFastRescaling), |
| 18 | target_width_(0), |
| 19 | target_height_(0), |
| 20 | scaler_() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 22 | VPMSimpleSpatialResampler::~VPMSimpleSpatialResampler() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 25 | int32_t VPMSimpleSpatialResampler::SetTargetFrameSize(int32_t width, |
| 26 | int32_t height) { |
| 27 | if (resampling_mode_ == kNoRescaling) return VPM_OK; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 28 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 29 | if (width < 1 || height < 1) return VPM_PARAMETER_ERROR; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 30 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 31 | target_width_ = width; |
| 32 | target_height_ = height; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 33 | |
| 34 | return VPM_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
| 36 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 37 | void VPMSimpleSpatialResampler::SetInputFrameResampleMode( |
| 38 | VideoFrameResampling resampling_mode) { |
| 39 | resampling_mode_ = resampling_mode; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | } |
| 41 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 42 | void VPMSimpleSpatialResampler::Reset() { |
| 43 | resampling_mode_ = kFastRescaling; |
| 44 | target_width_ = 0; |
| 45 | target_height_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 48 | int32_t VPMSimpleSpatialResampler::ResampleFrame(const VideoFrame& inFrame, |
| 49 | VideoFrame* outFrame) { |
mikhal@webrtc.org | 4493db5 | 2012-12-13 18:25:36 +0000 | [diff] [blame] | 50 | // Don't copy if frame remains as is. |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 51 | if (resampling_mode_ == kNoRescaling) |
mikhal@webrtc.org | 4493db5 | 2012-12-13 18:25:36 +0000 | [diff] [blame] | 52 | return VPM_OK; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 53 | // Check if re-sampling is needed |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 54 | else if ((inFrame.width() == target_width_) && |
| 55 | (inFrame.height() == target_height_)) { |
mikhal@webrtc.org | 4493db5 | 2012-12-13 18:25:36 +0000 | [diff] [blame] | 56 | return VPM_OK; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 57 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 59 | // Setting scaler |
marpan@webrtc.org | 5567ebf | 2012-06-26 16:47:36 +0000 | [diff] [blame] | 60 | // TODO(mikhal/marpan): Should we allow for setting the filter mode in |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 61 | // _scale.Set() with |resampling_mode_|? |
| 62 | int ret_val = 0; |
| 63 | ret_val = scaler_.Set(inFrame.width(), inFrame.height(), |
| 64 | target_width_, target_height_, kI420, kI420, kScaleBox); |
| 65 | if (ret_val < 0) |
| 66 | return ret_val; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 68 | ret_val = scaler_.Scale(inFrame, outFrame); |
wu@webrtc.org | 206532e | 2012-11-07 23:37:41 +0000 | [diff] [blame] | 69 | |
| 70 | // Setting time parameters to the output frame. |
| 71 | // Timestamp will be reset in Scale call above, so we should set it after. |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 72 | outFrame->set_timestamp(inFrame.timestamp()); |
| 73 | outFrame->set_render_time_ms(inFrame.render_time_ms()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 74 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 75 | if (ret_val == 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | return VPM_OK; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 77 | else |
| 78 | return VPM_SCALE_ERROR; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 81 | int32_t VPMSimpleSpatialResampler::TargetHeight() { |
| 82 | return target_height_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 85 | int32_t VPMSimpleSpatialResampler::TargetWidth() { |
| 86 | return target_width_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 89 | bool VPMSimpleSpatialResampler::ApplyResample(int32_t width, |
| 90 | int32_t height) { |
| 91 | if ((width == target_width_ && height == target_height_) || |
| 92 | resampling_mode_ == kNoRescaling) |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 93 | return false; |
| 94 | else |
| 95 | return true; |
mikhal@webrtc.org | c4ab870 | 2011-11-01 16:44:24 +0000 | [diff] [blame] | 96 | } |
| 97 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 98 | } // namespace webrtc |