blob: db2b84b174a8e773e91b68667bba2e2c44c1972c [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
mflodmana8565422015-12-07 01:09:52 -080013#include "webrtc/modules/video_processing/video_denoiser.h"
14
niklase@google.com470e71d2011-07-07 08:21:25 +000015namespace webrtc {
16
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000017VPMFramePreprocessor::VPMFramePreprocessor()
jackychen8f9902a2015-11-26 02:59:48 -080018 : content_metrics_(nullptr),
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000019 resampled_frame_(),
20 enable_ca_(false),
21 frame_cnt_(0) {
22 spatial_resampler_ = new VPMSimpleSpatialResampler();
23 ca_ = new VPMContentAnalysis(true);
24 vd_ = new VPMVideoDecimator();
niklase@google.com470e71d2011-07-07 08:21:25 +000025}
26
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000027VPMFramePreprocessor::~VPMFramePreprocessor() {
28 Reset();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000029 delete ca_;
30 delete vd_;
jackychen8f9902a2015-11-26 02:59:48 -080031 delete spatial_resampler_;
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
mflodman99ab9442015-12-07 22:54:50 -080034void VPMFramePreprocessor::Reset() {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000035 ca_->Release();
36 vd_->Reset();
jackychen8f9902a2015-11-26 02:59:48 -080037 content_metrics_ = nullptr;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000038 spatial_resampler_->Reset();
39 enable_ca_ = false;
40 frame_cnt_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
mflodman99ab9442015-12-07 22:54:50 -080043void VPMFramePreprocessor::EnableTemporalDecimation(bool enable) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000044 vd_->EnableTemporalDecimation(enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000047void VPMFramePreprocessor::EnableContentAnalysis(bool enable) {
48 enable_ca_ = enable;
49}
50
mflodman99ab9442015-12-07 22:54:50 -080051void VPMFramePreprocessor::SetInputFrameResampleMode(
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000052 VideoFrameResampling resampling_mode) {
53 spatial_resampler_->SetInputFrameResampleMode(resampling_mode);
54}
55
mflodman99ab9442015-12-07 22:54:50 -080056int32_t VPMFramePreprocessor::SetTargetResolution(uint32_t width,
57 uint32_t height,
58 uint32_t frame_rate) {
mflodmana8565422015-12-07 01:09:52 -080059 if ((width == 0) || (height == 0) || (frame_rate == 0)) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000060 return VPM_PARAMETER_ERROR;
61 }
62 int32_t ret_val = 0;
63 ret_val = spatial_resampler_->SetTargetFrameSize(width, height);
64
mflodman99ab9442015-12-07 22:54:50 -080065 if (ret_val < 0)
66 return ret_val;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000067
jackychen6e2ce6e2015-07-13 16:26:33 -070068 vd_->SetTargetFramerate(frame_rate);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000069 return VPM_OK;
70}
71
jackychen6e2ce6e2015-07-13 16:26:33 -070072void VPMFramePreprocessor::SetTargetFramerate(int frame_rate) {
73 if (frame_rate == -1) {
74 vd_->EnableTemporalDecimation(false);
75 } else {
76 vd_->EnableTemporalDecimation(true);
77 vd_->SetTargetFramerate(frame_rate);
78 }
79}
80
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000081void VPMFramePreprocessor::UpdateIncomingframe_rate() {
82 vd_->UpdateIncomingframe_rate();
83}
84
mflodmana8565422015-12-07 01:09:52 -080085uint32_t VPMFramePreprocessor::GetDecimatedFrameRate() {
86 return vd_->GetDecimatedFrameRate();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000087}
88
mflodmana8565422015-12-07 01:09:52 -080089uint32_t VPMFramePreprocessor::GetDecimatedWidth() const {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000090 return spatial_resampler_->TargetWidth();
91}
92
mflodmana8565422015-12-07 01:09:52 -080093uint32_t VPMFramePreprocessor::GetDecimatedHeight() const {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000094 return spatial_resampler_->TargetHeight();
95}
96
mflodmana8565422015-12-07 01:09:52 -080097void VPMFramePreprocessor::EnableDenosing(bool enable) {
98 denoiser_.reset(new VideoDenoiser());
99}
100
101const VideoFrame* VPMFramePreprocessor::PreprocessFrame(
102 const VideoFrame& frame) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000103 if (frame.IsZeroSize()) {
mflodmana8565422015-12-07 01:09:52 -0800104 return nullptr;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000105 }
106
107 vd_->UpdateIncomingframe_rate();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000108 if (vd_->DropFrame()) {
mflodmana8565422015-12-07 01:09:52 -0800109 return nullptr;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000110 }
111
mflodmana8565422015-12-07 01:09:52 -0800112 const VideoFrame* current_frame = &frame;
113 if (denoiser_) {
114 denoiser_->DenoiseFrame(*current_frame, &denoised_frame_);
115 current_frame = &denoised_frame_;
jackychen8f9902a2015-11-26 02:59:48 -0800116 }
117
mflodmana8565422015-12-07 01:09:52 -0800118 if (spatial_resampler_->ApplyResample(current_frame->width(),
mflodman99ab9442015-12-07 22:54:50 -0800119 current_frame->height())) {
mflodmana8565422015-12-07 01:09:52 -0800120 if (spatial_resampler_->ResampleFrame(*current_frame, &resampled_frame_) !=
121 VPM_OK) {
122 return nullptr;
jackychen8f9902a2015-11-26 02:59:48 -0800123 }
mflodmana8565422015-12-07 01:09:52 -0800124 current_frame = &resampled_frame_;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000125 }
126
127 // Perform content analysis on the frame to be encoded.
mflodmana8565422015-12-07 01:09:52 -0800128 if (enable_ca_ && frame_cnt_ % kSkipFrameCA == 0) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000129 // Compute new metrics every |kSkipFramesCA| frames, starting with
130 // the first frame.
mflodmana8565422015-12-07 01:09:52 -0800131 content_metrics_ = ca_->ComputeContentMetrics(*current_frame);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000132 }
jackychen8f9902a2015-11-26 02:59:48 -0800133 ++frame_cnt_;
mflodmana8565422015-12-07 01:09:52 -0800134 return current_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000135}
136
mflodmana8565422015-12-07 01:09:52 -0800137VideoContentMetrics* VPMFramePreprocessor::GetContentMetrics() const {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000138 return content_metrics_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000139}
140
mflodmana8565422015-12-07 01:09:52 -0800141} // namespace webrtc