1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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 
11 #include "webrtc/modules/video_processing/spatial_resampler.h"
12 
13 namespace webrtc {
14 
VPMSimpleSpatialResampler()15 VPMSimpleSpatialResampler::VPMSimpleSpatialResampler()
16     : resampling_mode_(kFastRescaling),
17       target_width_(0),
18       target_height_(0),
19       scaler_() {}
20 
~VPMSimpleSpatialResampler()21 VPMSimpleSpatialResampler::~VPMSimpleSpatialResampler() {}
22 
SetTargetFrameSize(int32_t width,int32_t height)23 int32_t VPMSimpleSpatialResampler::SetTargetFrameSize(int32_t width,
24                                                       int32_t height) {
25   if (resampling_mode_ == kNoRescaling)
26     return VPM_OK;
27 
28   if (width < 1 || height < 1)
29     return VPM_PARAMETER_ERROR;
30 
31   target_width_ = width;
32   target_height_ = height;
33 
34   return VPM_OK;
35 }
36 
SetInputFrameResampleMode(VideoFrameResampling resampling_mode)37 void VPMSimpleSpatialResampler::SetInputFrameResampleMode(
38     VideoFrameResampling resampling_mode) {
39   resampling_mode_ = resampling_mode;
40 }
41 
Reset()42 void VPMSimpleSpatialResampler::Reset() {
43   resampling_mode_ = kFastRescaling;
44   target_width_ = 0;
45   target_height_ = 0;
46 }
47 
ResampleFrame(const VideoFrame & inFrame,VideoFrame * outFrame)48 int32_t VPMSimpleSpatialResampler::ResampleFrame(const VideoFrame& inFrame,
49                                                  VideoFrame* outFrame) {
50   // Don't copy if frame remains as is.
51   if (resampling_mode_ == kNoRescaling) {
52     return VPM_OK;
53   // Check if re-sampling is needed
54   } else if ((inFrame.width() == target_width_) &&
55              (inFrame.height() == target_height_)) {
56     return VPM_OK;
57   }
58 
59   // Setting scaler
60   // TODO(mikhal/marpan): Should we allow for setting the filter mode in
61   // _scale.Set() with |resampling_mode_|?
62   int ret_val = 0;
63   ret_val = scaler_.Set(inFrame.width(), inFrame.height(), target_width_,
64                         target_height_, kI420, kI420, kScaleBox);
65   if (ret_val < 0)
66     return ret_val;
67 
68   ret_val = scaler_.Scale(inFrame, outFrame);
69 
70   // Setting time parameters to the output frame.
71   // Timestamp will be reset in Scale call above, so we should set it after.
72   outFrame->set_timestamp(inFrame.timestamp());
73   outFrame->set_render_time_ms(inFrame.render_time_ms());
74 
75   if (ret_val == 0)
76     return VPM_OK;
77   else
78     return VPM_SCALE_ERROR;
79 }
80 
TargetHeight()81 int32_t VPMSimpleSpatialResampler::TargetHeight() {
82   return target_height_;
83 }
84 
TargetWidth()85 int32_t VPMSimpleSpatialResampler::TargetWidth() {
86   return target_width_;
87 }
88 
ApplyResample(int32_t width,int32_t height)89 bool VPMSimpleSpatialResampler::ApplyResample(int32_t width, int32_t height) {
90   if ((width == target_width_ && height == target_height_) ||
91       resampling_mode_ == kNoRescaling)
92     return false;
93   else
94     return true;
95 }
96 
97 }  // namespace webrtc
98