2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-02-20 09:00:35 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2015-11-16 16:39:06 -08:00
|
|
|
#include "webrtc/base/logging.h"
|
2015-11-18 22:31:24 +01:00
|
|
|
#include "webrtc/modules/video_processing/video_processing_impl.h"
|
2015-10-28 18:17:40 +01:00
|
|
|
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <assert.h>
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
namespace {
|
|
|
|
|
void SetSubSampling(VideoProcessingModule::FrameStats* stats,
|
|
|
|
|
const int32_t width,
|
|
|
|
|
const int32_t height) {
|
|
|
|
|
if (width * height >= 640 * 480) {
|
|
|
|
|
stats->subSamplWidth = 3;
|
|
|
|
|
stats->subSamplHeight = 3;
|
|
|
|
|
} else if (width * height >= 352 * 288) {
|
|
|
|
|
stats->subSamplWidth = 2;
|
|
|
|
|
stats->subSamplHeight = 2;
|
|
|
|
|
} else if (width * height >= 176 * 144) {
|
|
|
|
|
stats->subSamplWidth = 1;
|
|
|
|
|
stats->subSamplHeight = 1;
|
|
|
|
|
} else {
|
|
|
|
|
stats->subSamplWidth = 0;
|
|
|
|
|
stats->subSamplHeight = 0;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2013-10-03 16:42:41 +00:00
|
|
|
} // namespace
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-09-18 12:24:25 +02:00
|
|
|
VideoProcessingModule* VideoProcessingModule::Create() {
|
|
|
|
|
return new VideoProcessingModuleImpl();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
void VideoProcessingModule::Destroy(VideoProcessingModule* module) {
|
|
|
|
|
if (module)
|
|
|
|
|
delete static_cast<VideoProcessingModuleImpl*>(module);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-09-18 12:24:25 +02:00
|
|
|
VideoProcessingModuleImpl::VideoProcessingModuleImpl() {}
|
|
|
|
|
VideoProcessingModuleImpl::~VideoProcessingModuleImpl() {}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
void VideoProcessingModuleImpl::Reset() {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope mutex(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
deflickering_.Reset();
|
|
|
|
|
brightness_detection_.Reset();
|
|
|
|
|
frame_pre_processor_.Reset();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
int32_t VideoProcessingModule::GetFrameStats(FrameStats* stats,
|
2015-05-29 17:21:40 -07:00
|
|
|
const VideoFrame& frame) {
|
2013-10-03 16:42:41 +00:00
|
|
|
if (frame.IsZeroSize()) {
|
2014-04-10 11:30:49 +00:00
|
|
|
LOG(LS_ERROR) << "Zero size frame.";
|
2013-10-03 16:42:41 +00:00
|
|
|
return VPM_PARAMETER_ERROR;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
int width = frame.width();
|
|
|
|
|
int height = frame.height();
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
ClearFrameStats(stats); // The histogram needs to be zeroed out.
|
|
|
|
|
SetSubSampling(stats, width, height);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
const uint8_t* buffer = frame.buffer(kYPlane);
|
|
|
|
|
// Compute histogram and sum of frame
|
|
|
|
|
for (int i = 0; i < height; i += (1 << stats->subSamplHeight)) {
|
|
|
|
|
int k = i * width;
|
|
|
|
|
for (int j = 0; j < width; j += (1 << stats->subSamplWidth)) {
|
|
|
|
|
stats->hist[buffer[k + j]]++;
|
|
|
|
|
stats->sum += buffer[k + j];
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2013-10-03 16:42:41 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
stats->num_pixels = (width * height) / ((1 << stats->subSamplWidth) *
|
|
|
|
|
(1 << stats->subSamplHeight));
|
|
|
|
|
assert(stats->num_pixels > 0);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
// Compute mean value of frame
|
|
|
|
|
stats->mean = stats->sum / stats->num_pixels;
|
|
|
|
|
|
|
|
|
|
return VPM_OK;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
bool VideoProcessingModule::ValidFrameStats(const FrameStats& stats) {
|
2014-04-10 11:30:49 +00:00
|
|
|
if (stats.num_pixels == 0) {
|
|
|
|
|
LOG(LS_WARNING) << "Invalid frame stats.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-10-03 16:42:41 +00:00
|
|
|
return true;
|
2011-11-14 15:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
void VideoProcessingModule::ClearFrameStats(FrameStats* stats) {
|
|
|
|
|
stats->mean = 0;
|
|
|
|
|
stats->sum = 0;
|
|
|
|
|
stats->num_pixels = 0;
|
|
|
|
|
stats->subSamplWidth = 0;
|
|
|
|
|
stats->subSamplHeight = 0;
|
|
|
|
|
memset(stats->hist, 0, sizeof(stats->hist));
|
2011-11-14 15:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-29 17:21:40 -07:00
|
|
|
int32_t VideoProcessingModule::Brighten(VideoFrame* frame, int delta) {
|
2013-10-03 16:42:41 +00:00
|
|
|
return VideoProcessing::Brighten(frame, delta);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-29 17:21:40 -07:00
|
|
|
int32_t VideoProcessingModuleImpl::Deflickering(VideoFrame* frame,
|
2013-10-03 16:42:41 +00:00
|
|
|
FrameStats* stats) {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope mutex(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
return deflickering_.ProcessFrame(frame, stats);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
int32_t VideoProcessingModuleImpl::BrightnessDetection(
|
2015-05-29 17:21:40 -07:00
|
|
|
const VideoFrame& frame,
|
|
|
|
|
const FrameStats& stats) {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope mutex(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
return brightness_detection_.ProcessFrame(frame, stats);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
void VideoProcessingModuleImpl::EnableTemporalDecimation(bool enable) {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope mutex(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
frame_pre_processor_.EnableTemporalDecimation(enable);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
void VideoProcessingModuleImpl::SetInputFrameResampleMode(VideoFrameResampling
|
|
|
|
|
resampling_mode) {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope cs(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
frame_pre_processor_.SetInputFrameResampleMode(resampling_mode);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
int32_t VideoProcessingModuleImpl::SetTargetResolution(uint32_t width,
|
|
|
|
|
uint32_t height,
|
|
|
|
|
uint32_t frame_rate) {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope cs(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
return frame_pre_processor_.SetTargetResolution(width, height, frame_rate);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-13 16:26:33 -07:00
|
|
|
void VideoProcessingModuleImpl::SetTargetFramerate(int frame_rate) {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope cs(&mutex_);
|
2015-07-13 16:26:33 -07:00
|
|
|
frame_pre_processor_.SetTargetFramerate(frame_rate);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
uint32_t VideoProcessingModuleImpl::Decimatedframe_rate() {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope cs(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
return frame_pre_processor_.Decimatedframe_rate();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
uint32_t VideoProcessingModuleImpl::DecimatedWidth() const {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope cs(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
return frame_pre_processor_.DecimatedWidth();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
uint32_t VideoProcessingModuleImpl::DecimatedHeight() const {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope cs(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
return frame_pre_processor_.DecimatedHeight();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
int32_t VideoProcessingModuleImpl::PreprocessFrame(
|
2015-05-29 17:21:40 -07:00
|
|
|
const VideoFrame& frame,
|
|
|
|
|
VideoFrame** processed_frame) {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope mutex(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
return frame_pre_processor_.PreprocessFrame(frame, processed_frame);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
VideoContentMetrics* VideoProcessingModuleImpl::ContentMetrics() const {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope mutex(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
return frame_pre_processor_.ContentMetrics();
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
void VideoProcessingModuleImpl::EnableContentAnalysis(bool enable) {
|
2015-09-18 12:24:25 +02:00
|
|
|
rtc::CritScope mutex(&mutex_);
|
2013-10-03 16:42:41 +00:00
|
|
|
frame_pre_processor_.EnableContentAnalysis(enable);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-03 16:42:41 +00:00
|
|
|
} // namespace webrtc
|