blob: fd0d0efb97d2a713ba45b191e198ace584bc763c [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();
jackychenfa0befe2016-04-01 07:46:58 -070025 EnableDenosing(false);
niklase@google.com470e71d2011-07-07 08:21:25 +000026}
27
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000028VPMFramePreprocessor::~VPMFramePreprocessor() {
29 Reset();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000030 delete ca_;
31 delete vd_;
jackychen8f9902a2015-11-26 02:59:48 -080032 delete spatial_resampler_;
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
mflodman99ab9442015-12-07 22:54:50 -080035void VPMFramePreprocessor::Reset() {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000036 ca_->Release();
37 vd_->Reset();
jackychen8f9902a2015-11-26 02:59:48 -080038 content_metrics_ = nullptr;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000039 spatial_resampler_->Reset();
40 enable_ca_ = false;
41 frame_cnt_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000042}
43
mflodman99ab9442015-12-07 22:54:50 -080044void VPMFramePreprocessor::EnableTemporalDecimation(bool enable) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000045 vd_->EnableTemporalDecimation(enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000048void VPMFramePreprocessor::EnableContentAnalysis(bool enable) {
49 enable_ca_ = enable;
50}
51
mflodman99ab9442015-12-07 22:54:50 -080052void VPMFramePreprocessor::SetInputFrameResampleMode(
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000053 VideoFrameResampling resampling_mode) {
54 spatial_resampler_->SetInputFrameResampleMode(resampling_mode);
55}
56
mflodman99ab9442015-12-07 22:54:50 -080057int32_t VPMFramePreprocessor::SetTargetResolution(uint32_t width,
58 uint32_t height,
59 uint32_t frame_rate) {
mflodmana8565422015-12-07 01:09:52 -080060 if ((width == 0) || (height == 0) || (frame_rate == 0)) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000061 return VPM_PARAMETER_ERROR;
62 }
63 int32_t ret_val = 0;
64 ret_val = spatial_resampler_->SetTargetFrameSize(width, height);
65
mflodman99ab9442015-12-07 22:54:50 -080066 if (ret_val < 0)
67 return ret_val;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000068
jackychen6e2ce6e2015-07-13 16:26:33 -070069 vd_->SetTargetFramerate(frame_rate);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000070 return VPM_OK;
71}
72
jackychen6e2ce6e2015-07-13 16:26:33 -070073void VPMFramePreprocessor::SetTargetFramerate(int frame_rate) {
74 if (frame_rate == -1) {
75 vd_->EnableTemporalDecimation(false);
76 } else {
77 vd_->EnableTemporalDecimation(true);
78 vd_->SetTargetFramerate(frame_rate);
79 }
80}
81
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000082void VPMFramePreprocessor::UpdateIncomingframe_rate() {
83 vd_->UpdateIncomingframe_rate();
84}
85
mflodmana8565422015-12-07 01:09:52 -080086uint32_t VPMFramePreprocessor::GetDecimatedFrameRate() {
87 return vd_->GetDecimatedFrameRate();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000088}
89
mflodmana8565422015-12-07 01:09:52 -080090uint32_t VPMFramePreprocessor::GetDecimatedWidth() const {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000091 return spatial_resampler_->TargetWidth();
92}
93
mflodmana8565422015-12-07 01:09:52 -080094uint32_t VPMFramePreprocessor::GetDecimatedHeight() const {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000095 return spatial_resampler_->TargetHeight();
96}
97
mflodmana8565422015-12-07 01:09:52 -080098void VPMFramePreprocessor::EnableDenosing(bool enable) {
jackychenf0b8a372016-01-19 18:18:52 -080099 if (enable) {
100 denoiser_.reset(new VideoDenoiser(true));
101 } else {
102 denoiser_.reset();
103 }
mflodmana8565422015-12-07 01:09:52 -0800104}
105
106const VideoFrame* VPMFramePreprocessor::PreprocessFrame(
107 const VideoFrame& frame) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000108 if (frame.IsZeroSize()) {
mflodmana8565422015-12-07 01:09:52 -0800109 return nullptr;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000110 }
111
112 vd_->UpdateIncomingframe_rate();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000113 if (vd_->DropFrame()) {
mflodmana8565422015-12-07 01:09:52 -0800114 return nullptr;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000115 }
116
mflodmana8565422015-12-07 01:09:52 -0800117 const VideoFrame* current_frame = &frame;
118 if (denoiser_) {
jackychenfa0befe2016-04-01 07:46:58 -0700119 denoiser_->DenoiseFrame(*current_frame, &denoised_frame_,
120 &denoised_frame_prev_, 0);
mflodmana8565422015-12-07 01:09:52 -0800121 current_frame = &denoised_frame_;
jackychen8f9902a2015-11-26 02:59:48 -0800122 }
123
mflodmana8565422015-12-07 01:09:52 -0800124 if (spatial_resampler_->ApplyResample(current_frame->width(),
mflodman99ab9442015-12-07 22:54:50 -0800125 current_frame->height())) {
mflodmana8565422015-12-07 01:09:52 -0800126 if (spatial_resampler_->ResampleFrame(*current_frame, &resampled_frame_) !=
127 VPM_OK) {
128 return nullptr;
jackychen8f9902a2015-11-26 02:59:48 -0800129 }
mflodmana8565422015-12-07 01:09:52 -0800130 current_frame = &resampled_frame_;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000131 }
132
133 // Perform content analysis on the frame to be encoded.
mflodmana8565422015-12-07 01:09:52 -0800134 if (enable_ca_ && frame_cnt_ % kSkipFrameCA == 0) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000135 // Compute new metrics every |kSkipFramesCA| frames, starting with
136 // the first frame.
mflodmana8565422015-12-07 01:09:52 -0800137 content_metrics_ = ca_->ComputeContentMetrics(*current_frame);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000138 }
jackychen8f9902a2015-11-26 02:59:48 -0800139 ++frame_cnt_;
mflodmana8565422015-12-07 01:09:52 -0800140 return current_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
142
mflodmana8565422015-12-07 01:09:52 -0800143VideoContentMetrics* VPMFramePreprocessor::GetContentMetrics() const {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000144 return content_metrics_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145}
146
mflodmana8565422015-12-07 01:09:52 -0800147} // namespace webrtc