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 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | namespace webrtc { |
| 14 | |
| 15 | VPMSimpleSpatialResampler::VPMSimpleSpatialResampler() |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame^] | 16 | : resampling_mode_(kFastRescaling), target_width_(0), target_height_(0) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 18 | VPMSimpleSpatialResampler::~VPMSimpleSpatialResampler() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 20 | int32_t VPMSimpleSpatialResampler::SetTargetFrameSize(int32_t width, |
| 21 | int32_t height) { |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 22 | if (resampling_mode_ == kNoRescaling) |
| 23 | return VPM_OK; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 24 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 25 | if (width < 1 || height < 1) |
| 26 | return VPM_PARAMETER_ERROR; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 27 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 28 | target_width_ = width; |
| 29 | target_height_ = height; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 30 | |
| 31 | return VPM_OK; |
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 VPMSimpleSpatialResampler::SetInputFrameResampleMode( |
| 35 | VideoFrameResampling resampling_mode) { |
| 36 | resampling_mode_ = resampling_mode; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 39 | void VPMSimpleSpatialResampler::Reset() { |
| 40 | resampling_mode_ = kFastRescaling; |
| 41 | target_width_ = 0; |
| 42 | target_height_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 45 | int32_t VPMSimpleSpatialResampler::ResampleFrame(const VideoFrame& inFrame, |
| 46 | VideoFrame* outFrame) { |
mikhal@webrtc.org | 4493db5 | 2012-12-13 18:25:36 +0000 | [diff] [blame] | 47 | // Don't copy if frame remains as is. |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 48 | if (resampling_mode_ == kNoRescaling) { |
| 49 | return VPM_OK; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 50 | // Check if re-sampling is needed |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 51 | } else if ((inFrame.width() == target_width_) && |
| 52 | (inFrame.height() == target_height_)) { |
mikhal@webrtc.org | 4493db5 | 2012-12-13 18:25:36 +0000 | [diff] [blame] | 53 | return VPM_OK; |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 54 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame^] | 56 | rtc::scoped_refptr<I420Buffer> scaled_buffer( |
| 57 | buffer_pool_.CreateBuffer(target_width_, target_height_)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame^] | 59 | scaled_buffer->CropAndScaleFrom(inFrame.video_frame_buffer()); |
wu@webrtc.org | 206532e | 2012-11-07 23:37:41 +0000 | [diff] [blame] | 60 | |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame^] | 61 | outFrame->set_video_frame_buffer(scaled_buffer); |
wu@webrtc.org | 206532e | 2012-11-07 23:37:41 +0000 | [diff] [blame] | 62 | // Setting time parameters to the output frame. |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 63 | outFrame->set_timestamp(inFrame.timestamp()); |
| 64 | outFrame->set_render_time_ms(inFrame.render_time_ms()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame^] | 66 | return VPM_OK; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | } |
| 68 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 69 | int32_t VPMSimpleSpatialResampler::TargetHeight() { |
| 70 | return target_height_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | } |
| 72 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 73 | int32_t VPMSimpleSpatialResampler::TargetWidth() { |
| 74 | return target_width_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | } |
| 76 | |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 77 | bool VPMSimpleSpatialResampler::ApplyResample(int32_t width, int32_t height) { |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 78 | if ((width == target_width_ && height == target_height_) || |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 79 | resampling_mode_ == kNoRescaling) |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 80 | return false; |
| 81 | else |
| 82 | return true; |
mikhal@webrtc.org | c4ab870 | 2011-11-01 16:44:24 +0000 | [diff] [blame] | 83 | } |
| 84 | |
mikhal@webrtc.org | b43d807 | 2013-10-03 16:42:41 +0000 | [diff] [blame] | 85 | } // namespace webrtc |