blob: 8d6d8bbaa6ef41c55997ec9b2c557e4f06970677 [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()
Peter Boströmad6fc5a2016-05-12 03:01:31 +020018 : resampled_frame_(), frame_cnt_(0) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000019 spatial_resampler_ = new VPMSimpleSpatialResampler();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000020 vd_ = new VPMVideoDecimator();
nisse90c335a2016-04-27 00:59:22 -070021 EnableDenoising(false);
niklase@google.com470e71d2011-07-07 08:21:25 +000022}
23
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000024VPMFramePreprocessor::~VPMFramePreprocessor() {
25 Reset();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000026 delete vd_;
jackychen8f9902a2015-11-26 02:59:48 -080027 delete spatial_resampler_;
niklase@google.com470e71d2011-07-07 08:21:25 +000028}
29
mflodman99ab9442015-12-07 22:54:50 -080030void VPMFramePreprocessor::Reset() {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000031 vd_->Reset();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000032 spatial_resampler_->Reset();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000033 frame_cnt_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
mflodman99ab9442015-12-07 22:54:50 -080036void VPMFramePreprocessor::EnableTemporalDecimation(bool enable) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000037 vd_->EnableTemporalDecimation(enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
mflodman99ab9442015-12-07 22:54:50 -080040void VPMFramePreprocessor::SetInputFrameResampleMode(
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000041 VideoFrameResampling resampling_mode) {
42 spatial_resampler_->SetInputFrameResampleMode(resampling_mode);
43}
44
mflodman99ab9442015-12-07 22:54:50 -080045int32_t VPMFramePreprocessor::SetTargetResolution(uint32_t width,
46 uint32_t height,
47 uint32_t frame_rate) {
mflodmana8565422015-12-07 01:09:52 -080048 if ((width == 0) || (height == 0) || (frame_rate == 0)) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000049 return VPM_PARAMETER_ERROR;
50 }
51 int32_t ret_val = 0;
52 ret_val = spatial_resampler_->SetTargetFrameSize(width, height);
53
mflodman99ab9442015-12-07 22:54:50 -080054 if (ret_val < 0)
55 return ret_val;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000056
jackychen6e2ce6e2015-07-13 16:26:33 -070057 vd_->SetTargetFramerate(frame_rate);
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000058 return VPM_OK;
59}
60
61void VPMFramePreprocessor::UpdateIncomingframe_rate() {
62 vd_->UpdateIncomingframe_rate();
63}
64
mflodmana8565422015-12-07 01:09:52 -080065uint32_t VPMFramePreprocessor::GetDecimatedFrameRate() {
66 return vd_->GetDecimatedFrameRate();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000067}
68
mflodmana8565422015-12-07 01:09:52 -080069uint32_t VPMFramePreprocessor::GetDecimatedWidth() const {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000070 return spatial_resampler_->TargetWidth();
71}
72
mflodmana8565422015-12-07 01:09:52 -080073uint32_t VPMFramePreprocessor::GetDecimatedHeight() const {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000074 return spatial_resampler_->TargetHeight();
75}
76
nisse90c335a2016-04-27 00:59:22 -070077void VPMFramePreprocessor::EnableDenoising(bool enable) {
jackychenf0b8a372016-01-19 18:18:52 -080078 if (enable) {
79 denoiser_.reset(new VideoDenoiser(true));
80 } else {
81 denoiser_.reset();
82 }
mflodmana8565422015-12-07 01:09:52 -080083}
84
85const VideoFrame* VPMFramePreprocessor::PreprocessFrame(
86 const VideoFrame& frame) {
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000087 if (frame.IsZeroSize()) {
mflodmana8565422015-12-07 01:09:52 -080088 return nullptr;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000089 }
90
91 vd_->UpdateIncomingframe_rate();
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000092 if (vd_->DropFrame()) {
mflodmana8565422015-12-07 01:09:52 -080093 return nullptr;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +000094 }
95
mflodmana8565422015-12-07 01:09:52 -080096 const VideoFrame* current_frame = &frame;
97 if (denoiser_) {
nisse18ee17d2016-11-07 01:34:59 -080098 denoised_frame_ = VideoFrame(
99 denoiser_->DenoiseFrame(current_frame->video_frame_buffer(), true),
100 current_frame->timestamp(), current_frame->render_time_ms(),
101 current_frame->rotation());
Niels Möller6af2e862016-06-17 09:12:44 +0200102 current_frame = &denoised_frame_;
jackychen8f9902a2015-11-26 02:59:48 -0800103 }
104
mflodmana8565422015-12-07 01:09:52 -0800105 if (spatial_resampler_->ApplyResample(current_frame->width(),
mflodman99ab9442015-12-07 22:54:50 -0800106 current_frame->height())) {
mflodmana8565422015-12-07 01:09:52 -0800107 if (spatial_resampler_->ResampleFrame(*current_frame, &resampled_frame_) !=
108 VPM_OK) {
109 return nullptr;
jackychen8f9902a2015-11-26 02:59:48 -0800110 }
mflodmana8565422015-12-07 01:09:52 -0800111 current_frame = &resampled_frame_;
mikhal@webrtc.orgb43d8072013-10-03 16:42:41 +0000112 }
113
jackychen8f9902a2015-11-26 02:59:48 -0800114 ++frame_cnt_;
mflodmana8565422015-12-07 01:09:52 -0800115 return current_frame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116}
117
mflodmana8565422015-12-07 01:09:52 -0800118} // namespace webrtc