Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / video_processing / main / test / unit_test / color_enhancement_test.cc
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, ColorEnhancement)
23 {
24     TickTime t0;
25     TickTime t1;
26     TickInterval acc_ticks;
27
28     // Use a shorter version of the Foreman clip for this test.
29     fclose(source_file_);
30     const std::string video_file =
31       webrtc::test::ResourcePath("foreman_cif_short", "yuv");
32     source_file_  = fopen(video_file.c_str(), "rb");
33     ASSERT_TRUE(source_file_ != NULL) <<
34         "Cannot read source file: " + video_file + "\n";
35
36     std::string output_file = webrtc::test::OutputPath() +
37         "foremanColorEnhancedVPM_cif_short.yuv";
38     FILE* modFile = fopen(output_file.c_str(), "w+b");
39     ASSERT_TRUE(modFile != NULL) << "Could not open output file.\n";
40
41     uint32_t frameNum = 0;
42     scoped_ptr<uint8_t[]> video_buffer(new uint8_t[frame_length_]);
43     while (fread(video_buffer.get(), 1, frame_length_, source_file_) ==
44         frame_length_)
45     {
46         // Using ConvertToI420 to add stride to the image.
47         EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0,
48                                    width_, height_,
49                                    0, kRotateNone, &video_frame_));
50         frameNum++;
51         t0 = TickTime::Now();
52         ASSERT_EQ(0, VideoProcessingModule::ColorEnhancement(&video_frame_));
53         t1 = TickTime::Now();
54         acc_ticks += t1 - t0;
55         if (PrintI420VideoFrame(video_frame_, modFile) < 0) {
56           return;
57         }
58     }
59     ASSERT_NE(0, feof(source_file_)) << "Error reading source file";
60
61     printf("\nTime per frame: %d us \n",
62         static_cast<int>(acc_ticks.Microseconds() / frameNum));
63     rewind(modFile);
64
65     printf("Comparing files...\n\n");
66     std::string reference_filename =
67         webrtc::test::ResourcePath("foremanColorEnhanced_cif_short", "yuv");
68     FILE* refFile = fopen(reference_filename.c_str(), "rb");
69     ASSERT_TRUE(refFile != NULL) << "Cannot open reference file: " <<
70         reference_filename << "\n"
71         "Create the reference by running Matlab script createTable.m.";
72
73     // get file lenghts
74     ASSERT_EQ(0, fseek(refFile, 0L, SEEK_END));
75     long refLen = ftell(refFile);
76     ASSERT_NE(-1L, refLen);
77     rewind(refFile);
78     ASSERT_EQ(0, fseek(modFile, 0L, SEEK_END));
79     long testLen = ftell(modFile);
80     ASSERT_NE(-1L, testLen);
81     rewind(modFile);
82     ASSERT_EQ(refLen, testLen) << "File lengths differ.";
83
84     I420VideoFrame refVideoFrame;
85     refVideoFrame.CreateEmptyFrame(width_, height_,
86                                    width_, half_width_, half_width_);
87
88     // Compare frame-by-frame.
89     scoped_ptr<uint8_t[]> ref_buffer(new uint8_t[frame_length_]);
90     while (fread(video_buffer.get(), 1, frame_length_, modFile) ==
91         frame_length_)
92     {
93         // Using ConvertToI420 to add stride to the image.
94         EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0,
95                                    width_, height_,
96                                    0, kRotateNone, &video_frame_));
97         ASSERT_EQ(frame_length_, fread(ref_buffer.get(), 1, frame_length_,
98                                        refFile));
99         EXPECT_EQ(0, ConvertToI420(kI420, ref_buffer.get(), 0, 0,
100                                    width_, height_,
101                                     0, kRotateNone, &refVideoFrame));
102         EXPECT_EQ(0, memcmp(video_frame_.buffer(kYPlane),
103                             refVideoFrame.buffer(kYPlane),
104                             size_y_));
105         EXPECT_EQ(0, memcmp(video_frame_.buffer(kUPlane),
106                             refVideoFrame.buffer(kUPlane),
107                             size_uv_));
108         EXPECT_EQ(0, memcmp(video_frame_.buffer(kVPlane),
109                             refVideoFrame.buffer(kVPlane),
110                             size_uv_));
111     }
112     ASSERT_NE(0, feof(source_file_)) << "Error reading source file";
113
114     // Verify that all color pixels are enhanced, and no luminance values are
115     // altered.
116
117     scoped_ptr<uint8_t[]> testFrame(new uint8_t[frame_length_]);
118
119     // Use value 128 as probe value, since we know that this will be changed
120     // in the enhancement.
121     memset(testFrame.get(), 128, frame_length_);
122
123     I420VideoFrame testVideoFrame;
124     testVideoFrame.CreateEmptyFrame(width_, height_,
125                                     width_, half_width_, half_width_);
126     EXPECT_EQ(0, ConvertToI420(kI420, testFrame.get(), 0, 0,
127                                width_, height_, 0, kRotateNone,
128                                &testVideoFrame));
129
130     ASSERT_EQ(0, VideoProcessingModule::ColorEnhancement(&testVideoFrame));
131
132     EXPECT_EQ(0, memcmp(testVideoFrame.buffer(kYPlane), testFrame.get(),
133                         size_y_))
134       << "Function is modifying the luminance.";
135
136     EXPECT_NE(0, memcmp(testVideoFrame.buffer(kUPlane),
137                         testFrame.get() + size_y_, size_uv_)) <<
138                         "Function is not modifying all chrominance pixels";
139     EXPECT_NE(0, memcmp(testVideoFrame.buffer(kVPlane),
140                         testFrame.get() + size_y_ + size_uv_, size_uv_)) <<
141                         "Function is not modifying all chrominance pixels";
142
143     ASSERT_EQ(0, fclose(refFile));
144     ASSERT_EQ(0, fclose(modFile));
145 }
146
147 }  // namespace webrtc