blob: e5254fa852da91dd2f05b37beefd3c35aaaeae70 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Kjellander0f59a882015-11-18 22:31:24 +010011#include "webrtc/modules/video_processing/spatial_resampler.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13
14namespace webrtc {
15
16VPMSimpleSpatialResampler::VPMSimpleSpatialResampler()
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000017 : resampling_mode_(kFastRescaling),
18 target_width_(0),
19 target_height_(0),
20 scaler_() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000021
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000022VPMSimpleSpatialResampler::~VPMSimpleSpatialResampler() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000023
niklase@google.com470e71d2011-07-07 08:21:25 +000024
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000025int32_t VPMSimpleSpatialResampler::SetTargetFrameSize(int32_t width,
26 int32_t height) {
27 if (resampling_mode_ == kNoRescaling) return VPM_OK;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000028
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000029 if (width < 1 || height < 1) return VPM_PARAMETER_ERROR;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000030
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000031 target_width_ = width;
32 target_height_ = height;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000033
34 return VPM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
36
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000037void VPMSimpleSpatialResampler::SetInputFrameResampleMode(
38 VideoFrameResampling resampling_mode) {
39 resampling_mode_ = resampling_mode;
niklase@google.com470e71d2011-07-07 08:21:25 +000040}
41
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000042void VPMSimpleSpatialResampler::Reset() {
43 resampling_mode_ = kFastRescaling;
44 target_width_ = 0;
45 target_height_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070048int32_t VPMSimpleSpatialResampler::ResampleFrame(const VideoFrame& inFrame,
49 VideoFrame* outFrame) {
mikhal@webrtc.org4493db52012-12-13 18:25:36 +000050 // Don't copy if frame remains as is.
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000051 if (resampling_mode_ == kNoRescaling)
mikhal@webrtc.org4493db52012-12-13 18:25:36 +000052 return VPM_OK;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000053 // Check if re-sampling is needed
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000054 else if ((inFrame.width() == target_width_) &&
55 (inFrame.height() == target_height_)) {
mikhal@webrtc.org4493db52012-12-13 18:25:36 +000056 return VPM_OK;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000057 }
niklase@google.com470e71d2011-07-07 08:21:25 +000058
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000059 // Setting scaler
marpan@webrtc.org5567ebf2012-06-26 16:47:36 +000060 // TODO(mikhal/marpan): Should we allow for setting the filter mode in
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000061 // _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.com470e71d2011-07-07 08:21:25 +000067
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000068 ret_val = scaler_.Scale(inFrame, outFrame);
wu@webrtc.org206532e2012-11-07 23:37:41 +000069
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.org9fedff72012-10-24 18:33:04 +000072 outFrame->set_timestamp(inFrame.timestamp());
73 outFrame->set_render_time_ms(inFrame.render_time_ms());
niklase@google.com470e71d2011-07-07 08:21:25 +000074
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000075 if (ret_val == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +000076 return VPM_OK;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000077 else
78 return VPM_SCALE_ERROR;
niklase@google.com470e71d2011-07-07 08:21:25 +000079}
80
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000081int32_t VPMSimpleSpatialResampler::TargetHeight() {
82 return target_height_;
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000085int32_t VPMSimpleSpatialResampler::TargetWidth() {
86 return target_width_;
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000089bool VPMSimpleSpatialResampler::ApplyResample(int32_t width,
90 int32_t height) {
91 if ((width == target_width_ && height == target_height_) ||
92 resampling_mode_ == kNoRescaling)
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000093 return false;
94 else
95 return true;
mikhal@webrtc.orgc4ab8702011-11-01 16:44:24 +000096}
97
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000098} // namespace webrtc