blob: 74a570fb07822694c407d478ff39b98987d9ca7d [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()
Niels Möller718a7632016-06-13 13:06:01 +020016 : resampling_mode_(kFastRescaling), target_width_(0), target_height_(0) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000017
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000018VPMSimpleSpatialResampler::~VPMSimpleSpatialResampler() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000019
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000020int32_t VPMSimpleSpatialResampler::SetTargetFrameSize(int32_t width,
21 int32_t height) {
mflodman99ab9442015-12-07 22:54:50 -080022 if (resampling_mode_ == kNoRescaling)
23 return VPM_OK;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000024
mflodman99ab9442015-12-07 22:54:50 -080025 if (width < 1 || height < 1)
26 return VPM_PARAMETER_ERROR;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000027
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000028 target_width_ = width;
29 target_height_ = height;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000030
31 return VPM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000034void VPMSimpleSpatialResampler::SetInputFrameResampleMode(
35 VideoFrameResampling resampling_mode) {
36 resampling_mode_ = resampling_mode;
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
38
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000039void VPMSimpleSpatialResampler::Reset() {
40 resampling_mode_ = kFastRescaling;
41 target_width_ = 0;
42 target_height_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000043}
44
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070045int32_t VPMSimpleSpatialResampler::ResampleFrame(const VideoFrame& inFrame,
46 VideoFrame* outFrame) {
mikhal@webrtc.org4493db52012-12-13 18:25:36 +000047 // Don't copy if frame remains as is.
mflodman99ab9442015-12-07 22:54:50 -080048 if (resampling_mode_ == kNoRescaling) {
49 return VPM_OK;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000050 // Check if re-sampling is needed
mflodman99ab9442015-12-07 22:54:50 -080051 } else if ((inFrame.width() == target_width_) &&
52 (inFrame.height() == target_height_)) {
mikhal@webrtc.org4493db52012-12-13 18:25:36 +000053 return VPM_OK;
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000054 }
niklase@google.com470e71d2011-07-07 08:21:25 +000055
Niels Möller718a7632016-06-13 13:06:01 +020056 rtc::scoped_refptr<I420Buffer> scaled_buffer(
57 buffer_pool_.CreateBuffer(target_width_, target_height_));
niklase@google.com470e71d2011-07-07 08:21:25 +000058
Niels Möller718a7632016-06-13 13:06:01 +020059 scaled_buffer->CropAndScaleFrom(inFrame.video_frame_buffer());
wu@webrtc.org206532e2012-11-07 23:37:41 +000060
nisse72e735d2016-06-17 02:55:14 -070061 outFrame->set_video_frame_buffer(scaled_buffer);
62 // Setting time parameters to the output frame.
63 outFrame->set_timestamp(inFrame.timestamp());
64 outFrame->set_render_time_ms(inFrame.render_time_ms());
niklase@google.com470e71d2011-07-07 08:21:25 +000065
Niels Möller718a7632016-06-13 13:06:01 +020066 return VPM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000069int32_t VPMSimpleSpatialResampler::TargetHeight() {
70 return target_height_;
niklase@google.com470e71d2011-07-07 08:21:25 +000071}
72
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000073int32_t VPMSimpleSpatialResampler::TargetWidth() {
74 return target_width_;
niklase@google.com470e71d2011-07-07 08:21:25 +000075}
76
mflodman99ab9442015-12-07 22:54:50 -080077bool VPMSimpleSpatialResampler::ApplyResample(int32_t width, int32_t height) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000078 if ((width == target_width_ && height == target_height_) ||
mflodman99ab9442015-12-07 22:54:50 -080079 resampling_mode_ == kNoRescaling)
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +000080 return false;
81 else
82 return true;
mikhal@webrtc.orgc4ab8702011-11-01 16:44:24 +000083}
84
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000085} // namespace webrtc