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