1 /*
2  *  Copyright (c) 2012 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 <stdio.h>
12 #include <stdlib.h>
13 
14 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
15 #include "webrtc/modules/video_processing/main/interface/video_processing.h"
16 #include "webrtc/modules/video_processing/main/test/unit_test/video_processing_unittest.h"
17 #include "webrtc/system_wrappers/interface/tick_util.h"
18 #include "webrtc/test/testsupport/fileutils.h"
19 
20 namespace webrtc {
21 
22 TEST_F(VideoProcessingModuleTest, Deflickering)
23 {
24     enum { NumRuns = 30 };
25     uint32_t frameNum = 0;
26     const uint32_t frame_rate = 15;
27 
28     int64_t min_runtime = 0;
29     int64_t avg_runtime = 0;
30 
31     // Close automatically opened Foreman.
32     fclose(source_file_);
33     const std::string input_file =
34         webrtc::test::ResourcePath("deflicker_before_cif_short", "yuv");
35     source_file_  = fopen(input_file.c_str(), "rb");
36     ASSERT_TRUE(source_file_ != NULL) <<
37         "Cannot read input file: " << input_file << "\n";
38 
39     const std::string output_file =
40         webrtc::test::OutputPath() + "deflicker_output_cif_short.yuv";
41     FILE* deflickerFile = fopen(output_file.c_str(), "wb");
42     ASSERT_TRUE(deflickerFile != NULL) <<
43         "Could not open output file: " << output_file << "\n";
44 
45     printf("\nRun time [us / frame]:\n");
46     rtc::scoped_ptr<uint8_t[]> video_buffer(new uint8_t[frame_length_]);
47     for (uint32_t run_idx = 0; run_idx < NumRuns; run_idx++)
48     {
49         TickTime t0;
50         TickTime t1;
51         TickInterval acc_ticks;
52         uint32_t timeStamp = 1;
53 
54         frameNum = 0;
55         while (fread(video_buffer.get(), 1, frame_length_, source_file_) ==
56                frame_length_)
57         {
58             frameNum++;
59             EXPECT_EQ(
60                 0, ConvertToI420(kI420, video_buffer.get(), 0, 0, width_,
61                                  height_, 0, kVideoRotation_0, &video_frame_));
62             video_frame_.set_timestamp(timeStamp);
63 
64             t0 = TickTime::Now();
65             VideoProcessingModule::FrameStats stats;
66             ASSERT_EQ(0, vpm_->GetFrameStats(&stats, video_frame_));
67             ASSERT_EQ(0, vpm_->Deflickering(&video_frame_, &stats));
68             t1 = TickTime::Now();
69             acc_ticks += (t1 - t0);
70 
71             if (run_idx == 0)
72             {
73               if (PrintI420VideoFrame(video_frame_, deflickerFile) < 0) {
74                 return;
75               }
76             }
77             timeStamp += (90000 / frame_rate);
78         }
79         ASSERT_NE(0, feof(source_file_)) << "Error reading source file";
80 
81         printf("%u\n", static_cast<int>(acc_ticks.Microseconds() / frameNum));
82         if (acc_ticks.Microseconds() < min_runtime || run_idx == 0)
83         {
84             min_runtime = acc_ticks.Microseconds();
85         }
86         avg_runtime += acc_ticks.Microseconds();
87 
88         rewind(source_file_);
89     }
90     ASSERT_EQ(0, fclose(deflickerFile));
91     // TODO(kjellander): Add verification of deflicker output file.
92 
93     printf("\nAverage run time = %d us / frame\n",
94         static_cast<int>(avg_runtime / frameNum / NumRuns));
95     printf("Min run time = %d us / frame\n\n",
96         static_cast<int>(min_runtime / frameNum));
97 }
98 
99 }  // namespace webrtc
100