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/common_video/libyuv/include/webrtc_libyuv.h"
12#include "webrtc/modules/video_processing/main/interface/video_processing.h"
13#include "webrtc/modules/video_processing/main/test/unit_test/video_processing_unittest.h"
14
15using namespace webrtc;
16
17TEST_F(VideoProcessingModuleTest, BrightnessDetection)
18{
19    uint32_t frameNum = 0;
20    int32_t brightnessWarning = 0;
21    uint32_t warningCount = 0;
22    scoped_ptr<uint8_t[]> video_buffer(new uint8_t[frame_length_]);
23    while (fread(video_buffer.get(), 1, frame_length_, source_file_) ==
24           frame_length_)
25    {
26      EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0,
27                                 width_, height_,
28                                 0, kRotateNone, &video_frame_));
29        frameNum++;
30        VideoProcessingModule::FrameStats stats;
31        ASSERT_EQ(0, vpm_->GetFrameStats(&stats, video_frame_));
32        ASSERT_GE(brightnessWarning = vpm_->BrightnessDetection(video_frame_,
33                                                                stats), 0);
34        if (brightnessWarning != VideoProcessingModule::kNoWarning)
35        {
36            warningCount++;
37        }
38    }
39    ASSERT_NE(0, feof(source_file_)) << "Error reading source file";
40
41    // Expect few warnings
42    float warningProportion = static_cast<float>(warningCount) / frameNum * 100;
43    printf("\nWarning proportions:\n");
44    printf("Stock foreman: %.1f %%\n", warningProportion);
45    EXPECT_LT(warningProportion, 10);
46
47    rewind(source_file_);
48    frameNum = 0;
49    warningCount = 0;
50    while (fread(video_buffer.get(), 1, frame_length_, source_file_) ==
51        frame_length_ &&
52        frameNum < 300)
53    {
54        EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0,
55                                   width_, height_,
56                                   0, kRotateNone, &video_frame_));
57        frameNum++;
58
59        uint8_t* frame = video_frame_.buffer(kYPlane);
60        uint32_t yTmp = 0;
61        for (int yIdx = 0; yIdx < width_ * height_; yIdx++)
62        {
63            yTmp = frame[yIdx] << 1;
64            if (yTmp > 255)
65            {
66                yTmp = 255;
67            }
68            frame[yIdx] = static_cast<uint8_t>(yTmp);
69        }
70
71        VideoProcessingModule::FrameStats stats;
72        ASSERT_EQ(0, vpm_->GetFrameStats(&stats, video_frame_));
73        ASSERT_GE(brightnessWarning = vpm_->BrightnessDetection(video_frame_,
74                                                                stats), 0);
75        EXPECT_NE(VideoProcessingModule::kDarkWarning, brightnessWarning);
76        if (brightnessWarning == VideoProcessingModule::kBrightWarning)
77        {
78            warningCount++;
79        }
80    }
81    ASSERT_NE(0, feof(source_file_)) << "Error reading source file";
82
83    // Expect many brightness warnings
84    warningProportion = static_cast<float>(warningCount) / frameNum * 100;
85    printf("Bright foreman: %.1f %%\n", warningProportion);
86    EXPECT_GT(warningProportion, 95);
87
88    rewind(source_file_);
89    frameNum = 0;
90    warningCount = 0;
91    while (fread(video_buffer.get(), 1, frame_length_, source_file_) ==
92        frame_length_ && frameNum < 300)
93    {
94        EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0,
95                                   width_, height_,
96                                   0, kRotateNone, &video_frame_));
97        frameNum++;
98
99        uint8_t* y_plane = video_frame_.buffer(kYPlane);
100        int32_t yTmp = 0;
101        for (int yIdx = 0; yIdx < width_ * height_; yIdx++)
102        {
103            yTmp = y_plane[yIdx] >> 1;
104            y_plane[yIdx] = static_cast<uint8_t>(yTmp);
105        }
106
107        VideoProcessingModule::FrameStats stats;
108        ASSERT_EQ(0, vpm_->GetFrameStats(&stats, video_frame_));
109        ASSERT_GE(brightnessWarning = vpm_->BrightnessDetection(video_frame_,
110                                                                stats), 0);
111        EXPECT_NE(VideoProcessingModule::kBrightWarning, brightnessWarning);
112        if (brightnessWarning == VideoProcessingModule::kDarkWarning)
113        {
114            warningCount++;
115        }
116    }
117    ASSERT_NE(0, feof(source_file_)) << "Error reading source file";
118
119    // Expect many darkness warnings
120    warningProportion = static_cast<float>(warningCount) / frameNum * 100;
121    printf("Dark foreman: %.1f %%\n\n", warningProportion);
122    EXPECT_GT(warningProportion, 90);
123}
124