2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
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"
22 TEST_F(VideoProcessingModuleTest, ColorEnhancement)
26 TickInterval acc_ticks;
28 // Use a shorter version of the Foreman clip for this test.
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";
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";
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_) ==
46 // Using ConvertToI420 to add stride to the image.
47 EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0,
49 0, kRotateNone, &video_frame_));
52 ASSERT_EQ(0, VideoProcessingModule::ColorEnhancement(&video_frame_));
55 if (PrintI420VideoFrame(video_frame_, modFile) < 0) {
59 ASSERT_NE(0, feof(source_file_)) << "Error reading source file";
61 printf("\nTime per frame: %d us \n",
62 static_cast<int>(acc_ticks.Microseconds() / frameNum));
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.";
74 ASSERT_EQ(0, fseek(refFile, 0L, SEEK_END));
75 long refLen = ftell(refFile);
76 ASSERT_NE(-1L, refLen);
78 ASSERT_EQ(0, fseek(modFile, 0L, SEEK_END));
79 long testLen = ftell(modFile);
80 ASSERT_NE(-1L, testLen);
82 ASSERT_EQ(refLen, testLen) << "File lengths differ.";
84 I420VideoFrame refVideoFrame;
85 refVideoFrame.CreateEmptyFrame(width_, height_,
86 width_, half_width_, half_width_);
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) ==
93 // Using ConvertToI420 to add stride to the image.
94 EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0,
96 0, kRotateNone, &video_frame_));
97 ASSERT_EQ(frame_length_, fread(ref_buffer.get(), 1, frame_length_,
99 EXPECT_EQ(0, ConvertToI420(kI420, ref_buffer.get(), 0, 0,
101 0, kRotateNone, &refVideoFrame));
102 EXPECT_EQ(0, memcmp(video_frame_.buffer(kYPlane),
103 refVideoFrame.buffer(kYPlane),
105 EXPECT_EQ(0, memcmp(video_frame_.buffer(kUPlane),
106 refVideoFrame.buffer(kUPlane),
108 EXPECT_EQ(0, memcmp(video_frame_.buffer(kVPlane),
109 refVideoFrame.buffer(kVPlane),
112 ASSERT_NE(0, feof(source_file_)) << "Error reading source file";
114 // Verify that all color pixels are enhanced, and no luminance values are
117 scoped_ptr<uint8_t[]> testFrame(new uint8_t[frame_length_]);
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_);
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,
130 ASSERT_EQ(0, VideoProcessingModule::ColorEnhancement(&testVideoFrame));
132 EXPECT_EQ(0, memcmp(testVideoFrame.buffer(kYPlane), testFrame.get(),
134 << "Function is modifying the luminance.";
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";
143 ASSERT_EQ(0, fclose(refFile));
144 ASSERT_EQ(0, fclose(modFile));
147 } // namespace webrtc