blob: a3ec3c8c1c617d57c41d5ff3a40952857e4445a4 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
marpan@webrtc.org9d76b4e2012-02-28 23:39:31 +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/frame_preprocessor.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13namespace webrtc {
14
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000015VPMFramePreprocessor::VPMFramePreprocessor()
tommi@webrtc.org41617152015-01-29 12:12:49 +000016 : content_metrics_(NULL),
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000017 resampled_frame_(),
18 enable_ca_(false),
19 frame_cnt_(0) {
20 spatial_resampler_ = new VPMSimpleSpatialResampler();
21 ca_ = new VPMContentAnalysis(true);
22 vd_ = new VPMVideoDecimator();
niklase@google.com470e71d2011-07-07 08:21:25 +000023}
24
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000025VPMFramePreprocessor::~VPMFramePreprocessor() {
26 Reset();
27 delete spatial_resampler_;
28 delete ca_;
29 delete vd_;
niklase@google.com470e71d2011-07-07 08:21:25 +000030}
31
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000032void VPMFramePreprocessor::Reset() {
33 ca_->Release();
34 vd_->Reset();
35 content_metrics_ = NULL;
36 spatial_resampler_->Reset();
37 enable_ca_ = false;
38 frame_cnt_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
40
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000041void VPMFramePreprocessor::EnableTemporalDecimation(bool enable) {
42 vd_->EnableTemporalDecimation(enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000043}
44
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000045void VPMFramePreprocessor::EnableContentAnalysis(bool enable) {
46 enable_ca_ = enable;
47}
48
49void VPMFramePreprocessor::SetInputFrameResampleMode(
50 VideoFrameResampling resampling_mode) {
51 spatial_resampler_->SetInputFrameResampleMode(resampling_mode);
52}
53
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000054int32_t VPMFramePreprocessor::SetTargetResolution(
55 uint32_t width, uint32_t height, uint32_t frame_rate) {
56 if ( (width == 0) || (height == 0) || (frame_rate == 0)) {
57 return VPM_PARAMETER_ERROR;
58 }
59 int32_t ret_val = 0;
60 ret_val = spatial_resampler_->SetTargetFrameSize(width, height);
61
62 if (ret_val < 0) return ret_val;
63
jackychen6e2ce6e2015-07-13 16:26:33 -070064 vd_->SetTargetFramerate(frame_rate);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000065 return VPM_OK;
66}
67
jackychen6e2ce6e2015-07-13 16:26:33 -070068void VPMFramePreprocessor::SetTargetFramerate(int frame_rate) {
69 if (frame_rate == -1) {
70 vd_->EnableTemporalDecimation(false);
71 } else {
72 vd_->EnableTemporalDecimation(true);
73 vd_->SetTargetFramerate(frame_rate);
74 }
75}
76
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000077void VPMFramePreprocessor::UpdateIncomingframe_rate() {
78 vd_->UpdateIncomingframe_rate();
79}
80
81uint32_t VPMFramePreprocessor::Decimatedframe_rate() {
82 return vd_->Decimatedframe_rate();
83}
84
85
86uint32_t VPMFramePreprocessor::DecimatedWidth() const {
87 return spatial_resampler_->TargetWidth();
88}
89
90
91uint32_t VPMFramePreprocessor::DecimatedHeight() const {
92 return spatial_resampler_->TargetHeight();
93}
94
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070095int32_t VPMFramePreprocessor::PreprocessFrame(const VideoFrame& frame,
96 VideoFrame** processed_frame) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000097 if (frame.IsZeroSize()) {
98 return VPM_PARAMETER_ERROR;
99 }
100
101 vd_->UpdateIncomingframe_rate();
102
103 if (vd_->DropFrame()) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000104 return 1; // drop 1 frame
105 }
106
107 // Resizing incoming frame if needed. Otherwise, remains NULL.
108 // We are not allowed to resample the input frame (must make a copy of it).
109 *processed_frame = NULL;
110 if (spatial_resampler_->ApplyResample(frame.width(), frame.height())) {
111 int32_t ret = spatial_resampler_->ResampleFrame(frame, &resampled_frame_);
112 if (ret != VPM_OK) return ret;
113 *processed_frame = &resampled_frame_;
114 }
115
116 // Perform content analysis on the frame to be encoded.
117 if (enable_ca_) {
118 // Compute new metrics every |kSkipFramesCA| frames, starting with
119 // the first frame.
120 if (frame_cnt_ % kSkipFrameCA == 0) {
121 if (*processed_frame == NULL) {
122 content_metrics_ = ca_->ComputeContentMetrics(frame);
123 } else {
124 content_metrics_ = ca_->ComputeContentMetrics(resampled_frame_);
125 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000126 }
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000127 ++frame_cnt_;
128 }
129 return VPM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130}
131
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000132VideoContentMetrics* VPMFramePreprocessor::ContentMetrics() const {
133 return content_metrics_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134}
135
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000136} // namespace