aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/video_processing/video_processing_impl.cc
blob: f34886f10f22ca8361bc535d91041388976da300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  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.
 */

#include "webrtc/modules/video_processing/video_processing_impl.h"

#include <assert.h>

#include "webrtc/base/checks.h"
#include "webrtc/base/logging.h"
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"

namespace webrtc {

namespace {

int GetSubSamplingFactor(int width, int height) {
  if (width * height >= 640 * 480) {
    return 3;
  } else if (width * height >= 352 * 288) {
    return 2;
  } else if (width * height >= 176 * 144) {
    return 1;
  } else {
    return 0;
  }
}
}  // namespace

VideoProcessing* VideoProcessing::Create() {
  return new VideoProcessingImpl();
}

VideoProcessingImpl::VideoProcessingImpl() {}
VideoProcessingImpl::~VideoProcessingImpl() {}

void VideoProcessing::GetFrameStats(const VideoFrame& frame,
                                    FrameStats* stats) {
  ClearFrameStats(stats);  // The histogram needs to be zeroed out.
  if (frame.IsZeroSize()) {
    return;
  }

  int width = frame.width();
  int height = frame.height();
  stats->sub_sampling_factor = GetSubSamplingFactor(width, height);

  const uint8_t* buffer = frame.buffer(kYPlane);
  // Compute histogram and sum of frame
  for (int i = 0; i < height; i += (1 << stats->sub_sampling_factor)) {
    int k = i * width;
    for (int j = 0; j < width; j += (1 << stats->sub_sampling_factor)) {
      stats->hist[buffer[k + j]]++;
      stats->sum += buffer[k + j];
    }
  }

  stats->num_pixels = (width * height) / ((1 << stats->sub_sampling_factor) *
                                          (1 << stats->sub_sampling_factor));
  assert(stats->num_pixels > 0);

  // Compute mean value of frame
  stats->mean = stats->sum / stats->num_pixels;
}

bool VideoProcessing::ValidFrameStats(const FrameStats& stats) {
  if (stats.num_pixels == 0) {
    LOG(LS_WARNING) << "Invalid frame stats.";
    return false;
  }
  return true;
}

void VideoProcessing::ClearFrameStats(FrameStats* stats) {
  stats->mean = 0;
  stats->sum = 0;
  stats->num_pixels = 0;
  stats->sub_sampling_factor = 0;
  memset(stats->hist, 0, sizeof(stats->hist));
}

void VideoProcessing::Brighten(int delta, VideoFrame* frame) {
  RTC_DCHECK(!frame->IsZeroSize());
  RTC_DCHECK(frame->width() > 0);
  RTC_DCHECK(frame->height() > 0);

  int num_pixels = frame->width() * frame->height();

  int look_up[256];
  for (int i = 0; i < 256; i++) {
    int val = i + delta;
    look_up[i] = ((((val < 0) ? 0 : val) > 255) ? 255 : val);
  }

  uint8_t* temp_ptr = frame->buffer(kYPlane);
  for (int i = 0; i < num_pixels; i++) {
    *temp_ptr = static_cast<uint8_t>(look_up[*temp_ptr]);
    temp_ptr++;
  }
}

int32_t VideoProcessingImpl::Deflickering(VideoFrame* frame,
                                          FrameStats* stats) {
  rtc::CritScope mutex(&mutex_);
  return deflickering_.ProcessFrame(frame, stats);
}

int32_t VideoProcessingImpl::BrightnessDetection(const VideoFrame& frame,
                                                 const FrameStats& stats) {
  rtc::CritScope mutex(&mutex_);
  return brightness_detection_.ProcessFrame(frame, stats);
}

void VideoProcessingImpl::EnableTemporalDecimation(bool enable) {
  rtc::CritScope mutex(&mutex_);
  frame_pre_processor_.EnableTemporalDecimation(enable);
}

void VideoProcessingImpl::SetInputFrameResampleMode(
    VideoFrameResampling resampling_mode) {
  rtc::CritScope cs(&mutex_);
  frame_pre_processor_.SetInputFrameResampleMode(resampling_mode);
}

int32_t VideoProcessingImpl::SetTargetResolution(uint32_t width,
                                                 uint32_t height,
                                                 uint32_t frame_rate) {
  rtc::CritScope cs(&mutex_);
  return frame_pre_processor_.SetTargetResolution(width, height, frame_rate);
}

void VideoProcessingImpl::SetTargetFramerate(int frame_rate) {
  rtc::CritScope cs(&mutex_);
  frame_pre_processor_.SetTargetFramerate(frame_rate);
}

uint32_t VideoProcessingImpl::GetDecimatedFrameRate() {
  rtc::CritScope cs(&mutex_);
  return frame_pre_processor_.GetDecimatedFrameRate();
}

uint32_t VideoProcessingImpl::GetDecimatedWidth() const {
  rtc::CritScope cs(&mutex_);
  return frame_pre_processor_.GetDecimatedWidth();
}

uint32_t VideoProcessingImpl::GetDecimatedHeight() const {
  rtc::CritScope cs(&mutex_);
  return frame_pre_processor_.GetDecimatedHeight();
}

void VideoProcessingImpl::EnableDenosing(bool enable) {
  rtc::CritScope cs(&mutex_);
  frame_pre_processor_.EnableDenosing(enable);
}

const VideoFrame* VideoProcessingImpl::PreprocessFrame(
    const VideoFrame& frame) {
  rtc::CritScope mutex(&mutex_);
  return frame_pre_processor_.PreprocessFrame(frame);
}

VideoContentMetrics* VideoProcessingImpl::GetContentMetrics() const {
  rtc::CritScope mutex(&mutex_);
  return frame_pre_processor_.GetContentMetrics();
}

void VideoProcessingImpl::EnableContentAnalysis(bool enable) {
  rtc::CritScope mutex(&mutex_);
  frame_pre_processor_.EnableContentAnalysis(enable);
}

}  // namespace webrtc