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/include/video_processing.h"
16#include "webrtc/modules/video_processing/test/video_processing_unittest.h"
17#include "webrtc/system_wrappers/include/tick_util.h"
18#include "webrtc/test/testsupport/fileutils.h"
19
20namespace webrtc {
21
22#if defined(WEBRTC_IOS)
23TEST_F(VideoProcessingTest, DISABLED_Deflickering) {
24#else
25TEST_F(VideoProcessingTest, Deflickering) {
26#endif
27  enum { NumRuns = 30 };
28  uint32_t frameNum = 0;
29  const uint32_t frame_rate = 15;
30
31  int64_t min_runtime = 0;
32  int64_t avg_runtime = 0;
33
34  // Close automatically opened Foreman.
35  fclose(source_file_);
36  const std::string input_file =
37      webrtc::test::ResourcePath("deflicker_before_cif_short", "yuv");
38  source_file_ = fopen(input_file.c_str(), "rb");
39  ASSERT_TRUE(source_file_ != NULL) << "Cannot read input file: " << input_file
40                                    << "\n";
41
42  const std::string output_file =
43      webrtc::test::OutputPath() + "deflicker_output_cif_short.yuv";
44  FILE* deflickerFile = fopen(output_file.c_str(), "wb");
45  ASSERT_TRUE(deflickerFile != NULL)
46      << "Could not open output file: " << output_file << "\n";
47
48  printf("\nRun time [us / frame]:\n");
49  rtc::scoped_ptr<uint8_t[]> video_buffer(new uint8_t[frame_length_]);
50  for (uint32_t run_idx = 0; run_idx < NumRuns; run_idx++) {
51    TickTime t0;
52    TickTime t1;
53    TickInterval acc_ticks;
54    uint32_t timeStamp = 1;
55
56    frameNum = 0;
57    while (fread(video_buffer.get(), 1, frame_length_, source_file_) ==
58           frame_length_) {
59      frameNum++;
60      EXPECT_EQ(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      VideoProcessing::FrameStats stats;
66      vp_->GetFrameStats(video_frame_, &stats);
67      EXPECT_GT(stats.num_pixels, 0u);
68      ASSERT_EQ(0, vp_->Deflickering(&video_frame_, &stats));
69      t1 = TickTime::Now();
70      acc_ticks += (t1 - t0);
71
72      if (run_idx == 0) {
73        if (PrintVideoFrame(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      min_runtime = acc_ticks.Microseconds();
84    }
85    avg_runtime += acc_ticks.Microseconds();
86
87    rewind(source_file_);
88  }
89  ASSERT_EQ(0, fclose(deflickerFile));
90  // TODO(kjellander): Add verification of deflicker output file.
91
92  printf("\nAverage run time = %d us / frame\n",
93         static_cast<int>(avg_runtime / frameNum / NumRuns));
94  printf("Min run time = %d us / frame\n\n",
95         static_cast<int>(min_runtime / frameNum));
96}
97
98}  // namespace webrtc
99