OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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 | |
11 #include "webrtc/modules/video_processing/main/interface/video_processing.h" | |
12 #include "webrtc/modules/video_processing/main/source/brightness_detection.h" | |
13 | |
14 #include <math.h> | |
15 | |
16 namespace webrtc { | |
17 | |
18 VPMBrightnessDetection::VPMBrightnessDetection() { | |
19 Reset(); | |
20 } | |
21 | |
22 VPMBrightnessDetection::~VPMBrightnessDetection() {} | |
23 | |
24 void VPMBrightnessDetection::Reset() { | |
25 frame_cnt_bright_ = 0; | |
26 frame_cnt_dark_ = 0; | |
27 } | |
28 | |
29 int32_t VPMBrightnessDetection::ProcessFrame( | |
30 const VideoFrame& frame, | |
31 const VideoProcessingModule::FrameStats& stats) { | |
32 if (frame.IsZeroSize()) { | |
33 return VPM_PARAMETER_ERROR; | |
34 } | |
35 int width = frame.width(); | |
36 int height = frame.height(); | |
37 | |
38 if (!VideoProcessingModule::ValidFrameStats(stats)) { | |
39 return VPM_PARAMETER_ERROR; | |
40 } | |
41 | |
42 const uint8_t frame_cnt_alarm = 2; | |
43 | |
44 // Get proportion in lowest bins. | |
45 uint8_t low_th = 20; | |
46 float prop_low = 0; | |
47 for (uint32_t i = 0; i < low_th; i++) { | |
48 prop_low += stats.hist[i]; | |
49 } | |
50 prop_low /= stats.num_pixels; | |
51 | |
52 // Get proportion in highest bins. | |
53 unsigned char high_th = 230; | |
54 float prop_high = 0; | |
55 for (uint32_t i = high_th; i < 256; i++) { | |
56 prop_high += stats.hist[i]; | |
57 } | |
58 prop_high /= stats.num_pixels; | |
59 | |
60 if (prop_high < 0.4) { | |
61 if (stats.mean < 90 || stats.mean > 170) { | |
62 // Standard deviation of Y | |
63 const uint8_t* buffer = frame.buffer(kYPlane); | |
64 float std_y = 0; | |
65 for (int h = 0; h < height; h += (1 << stats.subSamplHeight)) { | |
66 int row = h*width; | |
67 for (int w = 0; w < width; w += (1 << stats.subSamplWidth)) { | |
68 std_y += (buffer[w + row] - stats.mean) * (buffer[w + row] - | |
69 stats.mean); | |
70 } | |
71 } | |
72 std_y = sqrt(std_y / stats.num_pixels); | |
73 | |
74 // Get percentiles. | |
75 uint32_t sum = 0; | |
76 uint32_t median_y = 140; | |
77 uint32_t perc05 = 0; | |
78 uint32_t perc95 = 255; | |
79 float pos_perc05 = stats.num_pixels * 0.05f; | |
80 float pos_median = stats.num_pixels * 0.5f; | |
81 float posPerc95 = stats.num_pixels * 0.95f; | |
82 for (uint32_t i = 0; i < 256; i++) { | |
83 sum += stats.hist[i]; | |
84 if (sum < pos_perc05) perc05 = i; // 5th perc. | |
85 if (sum < pos_median) median_y = i; // 50th perc. | |
86 if (sum < posPerc95) | |
87 perc95 = i; // 95th perc. | |
88 else | |
89 break; | |
90 } | |
91 | |
92 // Check if image is too dark | |
93 if ((std_y < 55) && (perc05 < 50)) { | |
94 if (median_y < 60 || stats.mean < 80 || perc95 < 130 || | |
95 prop_low > 0.20) { | |
96 frame_cnt_dark_++; | |
97 } else { | |
98 frame_cnt_dark_ = 0; | |
99 } | |
100 } else { | |
101 frame_cnt_dark_ = 0; | |
102 } | |
103 | |
104 // Check if image is too bright | |
105 if ((std_y < 52) && (perc95 > 200) && (median_y > 160)) { | |
106 if (median_y > 185 || stats.mean > 185 || perc05 > 140 || | |
107 prop_high > 0.25) { | |
108 frame_cnt_bright_++; | |
109 } else { | |
110 frame_cnt_bright_ = 0; | |
111 } | |
112 } else { | |
113 frame_cnt_bright_ = 0; | |
114 } | |
115 } else { | |
116 frame_cnt_dark_ = 0; | |
117 frame_cnt_bright_ = 0; | |
118 } | |
119 } else { | |
120 frame_cnt_bright_++; | |
121 frame_cnt_dark_ = 0; | |
122 } | |
123 | |
124 if (frame_cnt_dark_ > frame_cnt_alarm) { | |
125 return VideoProcessingModule::kDarkWarning; | |
126 } else if (frame_cnt_bright_ > frame_cnt_alarm) { | |
127 return VideoProcessingModule::kBrightWarning; | |
128 } else { | |
129 return VideoProcessingModule::kNoWarning; | |
130 } | |
131 } | |
132 | |
133 } // namespace webrtc | |
OLD | NEW |