blob: cdbe0efac14cf285daeb20b1aa8edb2cfb3b7192 [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
niklase@google.com470e71d2011-07-07 08:21:25 +000013namespace webrtc {
14
15VPMSimpleSpatialResampler::VPMSimpleSpatialResampler()
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000016 : resampling_mode_(kFastRescaling),
17 target_width_(0),
18 target_height_(0),
19 scaler_() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000020
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000021VPMSimpleSpatialResampler::~VPMSimpleSpatialResampler() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000022
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000023int32_t VPMSimpleSpatialResampler::SetTargetFrameSize(int32_t width,
24 int32_t height) {
mflodman99ab9442015-12-07 22:54:50 -080025 if (resampling_mode_ == kNoRescaling)
26 return VPM_OK;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000027
mflodman99ab9442015-12-07 22:54:50 -080028 if (width < 1 || height < 1)
29 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.
mflodman99ab9442015-12-07 22:54:50 -080051 if (resampling_mode_ == kNoRescaling) {
52 return VPM_OK;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000053 // Check if re-sampling is needed
mflodman99ab9442015-12-07 22:54:50 -080054 } 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;
mflodman99ab9442015-12-07 22:54:50 -080063 ret_val = scaler_.Set(inFrame.width(), inFrame.height(), target_width_,
64 target_height_, kI420, kI420, kScaleBox);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000065 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
mflodman99ab9442015-12-07 22:54:50 -080089bool VPMSimpleSpatialResampler::ApplyResample(int32_t width, int32_t height) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000090 if ((width == target_width_ && height == target_height_) ||
mflodman99ab9442015-12-07 22:54:50 -080091 resampling_mode_ == kNoRescaling)
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000092 return false;
93 else
94 return true;
mikhal@webrtc.orgc4ab8702011-11-01 16:44:24 +000095}
96
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000097} // namespace webrtc