• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/main/source/spatial_resampler.h"
12 
13 
14 namespace webrtc {
15 
VPMSimpleSpatialResampler()16 VPMSimpleSpatialResampler::VPMSimpleSpatialResampler()
17     : resampling_mode_(kFastRescaling),
18       target_width_(0),
19       target_height_(0),
20       scaler_() {}
21 
~VPMSimpleSpatialResampler()22 VPMSimpleSpatialResampler::~VPMSimpleSpatialResampler() {}
23 
24 
SetTargetFrameSize(int32_t width,int32_t height)25 int32_t VPMSimpleSpatialResampler::SetTargetFrameSize(int32_t width,
26                                                       int32_t height) {
27   if (resampling_mode_ == kNoRescaling) return VPM_OK;
28 
29   if (width < 1 || height < 1) 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 I420VideoFrame & inFrame,I420VideoFrame * outFrame)48 int32_t VPMSimpleSpatialResampler::ResampleFrame(const I420VideoFrame& inFrame,
49                                                  I420VideoFrame* 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(),
64                        target_width_, 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,
90                                               int32_t height) {
91   if ((width == target_width_ && height == target_height_) ||
92        resampling_mode_ == kNoRescaling)
93     return false;
94   else
95     return true;
96 }
97 
98 }  // namespace webrtc
99