OLD | NEW |
| (Empty) |
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 "webrtc/modules/video_processing/main/test/unit_test/video_processing_u
nittest.h" | |
12 | |
13 #include <string> | |
14 | |
15 #include <gflags/gflags.h> | |
16 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | |
17 #include "webrtc/system_wrappers/include/tick_util.h" | |
18 #include "webrtc/test/testsupport/fileutils.h" | |
19 #include "webrtc/test/testsupport/gtest_disable.h" | |
20 | |
21 namespace webrtc { | |
22 | |
23 namespace { | |
24 | |
25 // Define command line flag 'gen_files' (default value: false). | |
26 DEFINE_bool(gen_files, false, "Output files for visual inspection."); | |
27 | |
28 } // namespace | |
29 | |
30 static void PreprocessFrameAndVerify(const VideoFrame& source, | |
31 int target_width, | |
32 int target_height, | |
33 VideoProcessingModule* vpm, | |
34 VideoFrame** out_frame); | |
35 static void CropFrame(const uint8_t* source_data, | |
36 int source_width, | |
37 int source_height, | |
38 int offset_x, | |
39 int offset_y, | |
40 int cropped_width, | |
41 int cropped_height, | |
42 VideoFrame* cropped_frame); | |
43 // The |source_data| is cropped and scaled to |target_width| x |target_height|, | |
44 // and then scaled back to the expected cropped size. |expected_psnr| is used to | |
45 // verify basic quality, and is set to be ~0.1/0.05dB lower than actual PSNR | |
46 // verified under the same conditions. | |
47 static void TestSize(const VideoFrame& source_frame, | |
48 const VideoFrame& cropped_source_frame, | |
49 int target_width, | |
50 int target_height, | |
51 double expected_psnr, | |
52 VideoProcessingModule* vpm); | |
53 static bool CompareFrames(const webrtc::VideoFrame& frame1, | |
54 const webrtc::VideoFrame& frame2); | |
55 static void WriteProcessedFrameForVisualInspection(const VideoFrame& source, | |
56 const VideoFrame& processed); | |
57 | |
58 VideoProcessingModuleTest::VideoProcessingModuleTest() | |
59 : vpm_(NULL), | |
60 source_file_(NULL), | |
61 width_(352), | |
62 half_width_((width_ + 1) / 2), | |
63 height_(288), | |
64 size_y_(width_ * height_), | |
65 size_uv_(half_width_ * ((height_ + 1) / 2)), | |
66 frame_length_(CalcBufferSize(kI420, width_, height_)) {} | |
67 | |
68 void VideoProcessingModuleTest::SetUp() { | |
69 vpm_ = VideoProcessingModule::Create(); | |
70 ASSERT_TRUE(vpm_ != NULL); | |
71 | |
72 ASSERT_EQ(0, video_frame_.CreateEmptyFrame(width_, height_, width_, | |
73 half_width_, half_width_)); | |
74 // Clear video frame so DrMemory/Valgrind will allow reads of the buffer. | |
75 memset(video_frame_.buffer(kYPlane), 0, video_frame_.allocated_size(kYPlane)); | |
76 memset(video_frame_.buffer(kUPlane), 0, video_frame_.allocated_size(kUPlane)); | |
77 memset(video_frame_.buffer(kVPlane), 0, video_frame_.allocated_size(kVPlane)); | |
78 const std::string video_file = | |
79 webrtc::test::ResourcePath("foreman_cif", "yuv"); | |
80 source_file_ = fopen(video_file.c_str(),"rb"); | |
81 ASSERT_TRUE(source_file_ != NULL) << | |
82 "Cannot read source file: " + video_file + "\n"; | |
83 } | |
84 | |
85 void VideoProcessingModuleTest::TearDown() { | |
86 if (source_file_ != NULL) { | |
87 ASSERT_EQ(0, fclose(source_file_)); | |
88 } | |
89 source_file_ = NULL; | |
90 | |
91 if (vpm_ != NULL) { | |
92 VideoProcessingModule::Destroy(vpm_); | |
93 } | |
94 vpm_ = NULL; | |
95 } | |
96 | |
97 TEST_F(VideoProcessingModuleTest, DISABLED_ON_IOS(HandleNullBuffer)) { | |
98 // TODO(mikhal/stefan): Do we need this one? | |
99 VideoProcessingModule::FrameStats stats; | |
100 // Video frame with unallocated buffer. | |
101 VideoFrame videoFrame; | |
102 | |
103 EXPECT_EQ(-3, vpm_->GetFrameStats(&stats, videoFrame)); | |
104 | |
105 EXPECT_EQ(-1, vpm_->Deflickering(&videoFrame, &stats)); | |
106 | |
107 EXPECT_EQ(-3, vpm_->BrightnessDetection(videoFrame, stats)); | |
108 } | |
109 | |
110 TEST_F(VideoProcessingModuleTest, DISABLED_ON_IOS(HandleBadStats)) { | |
111 VideoProcessingModule::FrameStats stats; | |
112 rtc::scoped_ptr<uint8_t[]> video_buffer(new uint8_t[frame_length_]); | |
113 ASSERT_EQ(frame_length_, fread(video_buffer.get(), 1, frame_length_, | |
114 source_file_)); | |
115 EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0, width_, height_, | |
116 0, kVideoRotation_0, &video_frame_)); | |
117 | |
118 EXPECT_EQ(-1, vpm_->Deflickering(&video_frame_, &stats)); | |
119 | |
120 EXPECT_EQ(-3, vpm_->BrightnessDetection(video_frame_, stats)); | |
121 } | |
122 | |
123 TEST_F(VideoProcessingModuleTest, DISABLED_ON_IOS(IdenticalResultsAfterReset)) { | |
124 VideoFrame video_frame2; | |
125 VideoProcessingModule::FrameStats stats; | |
126 // Only testing non-static functions here. | |
127 rtc::scoped_ptr<uint8_t[]> video_buffer(new uint8_t[frame_length_]); | |
128 ASSERT_EQ(frame_length_, fread(video_buffer.get(), 1, frame_length_, | |
129 source_file_)); | |
130 EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0, width_, height_, | |
131 0, kVideoRotation_0, &video_frame_)); | |
132 ASSERT_EQ(0, vpm_->GetFrameStats(&stats, video_frame_)); | |
133 ASSERT_EQ(0, video_frame2.CopyFrame(video_frame_)); | |
134 ASSERT_EQ(0, vpm_->Deflickering(&video_frame_, &stats)); | |
135 vpm_->Reset(); | |
136 // Retrieve frame stats again in case Deflickering() has zeroed them. | |
137 ASSERT_EQ(0, vpm_->GetFrameStats(&stats, video_frame2)); | |
138 ASSERT_EQ(0, vpm_->Deflickering(&video_frame2, &stats)); | |
139 EXPECT_TRUE(CompareFrames(video_frame_, video_frame2)); | |
140 | |
141 ASSERT_EQ(frame_length_, fread(video_buffer.get(), 1, frame_length_, | |
142 source_file_)); | |
143 EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0, width_, height_, | |
144 0, kVideoRotation_0, &video_frame_)); | |
145 ASSERT_EQ(0, vpm_->GetFrameStats(&stats, video_frame_)); | |
146 video_frame2.CopyFrame(video_frame_); | |
147 ASSERT_EQ(0, vpm_->BrightnessDetection(video_frame_, stats)); | |
148 vpm_->Reset(); | |
149 ASSERT_EQ(0, vpm_->BrightnessDetection(video_frame2, stats)); | |
150 EXPECT_TRUE(CompareFrames(video_frame_, video_frame2)); | |
151 } | |
152 | |
153 TEST_F(VideoProcessingModuleTest, DISABLED_ON_IOS(FrameStats)) { | |
154 VideoProcessingModule::FrameStats stats; | |
155 rtc::scoped_ptr<uint8_t[]> video_buffer(new uint8_t[frame_length_]); | |
156 ASSERT_EQ(frame_length_, fread(video_buffer.get(), 1, frame_length_, | |
157 source_file_)); | |
158 EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0, width_, height_, | |
159 0, kVideoRotation_0, &video_frame_)); | |
160 | |
161 EXPECT_FALSE(vpm_->ValidFrameStats(stats)); | |
162 EXPECT_EQ(0, vpm_->GetFrameStats(&stats, video_frame_)); | |
163 EXPECT_TRUE(vpm_->ValidFrameStats(stats)); | |
164 | |
165 printf("\nFrameStats\n"); | |
166 printf("mean: %u\nnum_pixels: %u\nsubSamplWidth: " | |
167 "%u\nsumSamplHeight: %u\nsum: %u\n\n", | |
168 static_cast<unsigned int>(stats.mean), | |
169 static_cast<unsigned int>(stats.num_pixels), | |
170 static_cast<unsigned int>(stats.subSamplHeight), | |
171 static_cast<unsigned int>(stats.subSamplWidth), | |
172 static_cast<unsigned int>(stats.sum)); | |
173 | |
174 vpm_->ClearFrameStats(&stats); | |
175 EXPECT_FALSE(vpm_->ValidFrameStats(stats)); | |
176 } | |
177 | |
178 TEST_F(VideoProcessingModuleTest, DISABLED_ON_IOS(PreprocessorLogic)) { | |
179 // Disable temporal sampling (frame dropping). | |
180 vpm_->EnableTemporalDecimation(false); | |
181 int resolution = 100; | |
182 EXPECT_EQ(VPM_OK, vpm_->SetTargetResolution(resolution, resolution, 15)); | |
183 EXPECT_EQ(VPM_OK, vpm_->SetTargetResolution(resolution, resolution, 30)); | |
184 // Disable spatial sampling. | |
185 vpm_->SetInputFrameResampleMode(kNoRescaling); | |
186 EXPECT_EQ(VPM_OK, vpm_->SetTargetResolution(resolution, resolution, 30)); | |
187 VideoFrame* out_frame = NULL; | |
188 // Set rescaling => output frame != NULL. | |
189 vpm_->SetInputFrameResampleMode(kFastRescaling); | |
190 PreprocessFrameAndVerify(video_frame_, resolution, resolution, vpm_, | |
191 &out_frame); | |
192 // No rescaling=> output frame = NULL. | |
193 vpm_->SetInputFrameResampleMode(kNoRescaling); | |
194 EXPECT_EQ(VPM_OK, vpm_->PreprocessFrame(video_frame_, &out_frame)); | |
195 EXPECT_TRUE(out_frame == NULL); | |
196 } | |
197 | |
198 TEST_F(VideoProcessingModuleTest, DISABLED_ON_IOS(Resampler)) { | |
199 enum { NumRuns = 1 }; | |
200 | |
201 int64_t min_runtime = 0; | |
202 int64_t total_runtime = 0; | |
203 | |
204 rewind(source_file_); | |
205 ASSERT_TRUE(source_file_ != NULL) << | |
206 "Cannot read input file \n"; | |
207 | |
208 // CA not needed here | |
209 vpm_->EnableContentAnalysis(false); | |
210 // no temporal decimation | |
211 vpm_->EnableTemporalDecimation(false); | |
212 | |
213 // Reading test frame | |
214 rtc::scoped_ptr<uint8_t[]> video_buffer(new uint8_t[frame_length_]); | |
215 ASSERT_EQ(frame_length_, fread(video_buffer.get(), 1, frame_length_, | |
216 source_file_)); | |
217 // Using ConvertToI420 to add stride to the image. | |
218 EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0, width_, height_, | |
219 0, kVideoRotation_0, &video_frame_)); | |
220 // Cropped source frame that will contain the expected visible region. | |
221 VideoFrame cropped_source_frame; | |
222 cropped_source_frame.CopyFrame(video_frame_); | |
223 | |
224 for (uint32_t run_idx = 0; run_idx < NumRuns; run_idx++) { | |
225 // Initiate test timer. | |
226 const TickTime time_start = TickTime::Now(); | |
227 | |
228 // Init the sourceFrame with a timestamp. | |
229 video_frame_.set_render_time_ms(time_start.MillisecondTimestamp()); | |
230 video_frame_.set_timestamp(time_start.MillisecondTimestamp() * 90); | |
231 | |
232 // Test scaling to different sizes: source is of |width|/|height| = 352/288. | |
233 // Pure scaling: | |
234 TestSize(video_frame_, video_frame_, width_ / 4, height_ / 4, 25.2, vpm_); | |
235 TestSize(video_frame_, video_frame_, width_ / 2, height_ / 2, 28.1, vpm_); | |
236 // No resampling: | |
237 TestSize(video_frame_, video_frame_, width_, height_, -1, vpm_); | |
238 TestSize(video_frame_, video_frame_, 2 * width_, 2 * height_, 32.2, vpm_); | |
239 | |
240 // Scaling and cropping. The cropped source frame is the largest center | |
241 // aligned region that can be used from the source while preserving aspect | |
242 // ratio. | |
243 CropFrame(video_buffer.get(), width_, height_, 0, 56, 352, 176, | |
244 &cropped_source_frame); | |
245 TestSize(video_frame_, cropped_source_frame, 100, 50, 24.0, vpm_); | |
246 | |
247 CropFrame(video_buffer.get(), width_, height_, 0, 30, 352, 225, | |
248 &cropped_source_frame); | |
249 TestSize(video_frame_, cropped_source_frame, 400, 256, 31.3, vpm_); | |
250 | |
251 CropFrame(video_buffer.get(), width_, height_, 68, 0, 216, 288, | |
252 &cropped_source_frame); | |
253 TestSize(video_frame_, cropped_source_frame, 480, 640, 32.15, vpm_); | |
254 | |
255 CropFrame(video_buffer.get(), width_, height_, 0, 12, 352, 264, | |
256 &cropped_source_frame); | |
257 TestSize(video_frame_, cropped_source_frame, 960, 720, 32.2, vpm_); | |
258 | |
259 CropFrame(video_buffer.get(), width_, height_, 0, 44, 352, 198, | |
260 &cropped_source_frame); | |
261 TestSize(video_frame_, cropped_source_frame, 1280, 720, 32.15, vpm_); | |
262 | |
263 // Upsampling to odd size. | |
264 CropFrame(video_buffer.get(), width_, height_, 0, 26, 352, 233, | |
265 &cropped_source_frame); | |
266 TestSize(video_frame_, cropped_source_frame, 501, 333, 32.05, vpm_); | |
267 // Downsample to odd size. | |
268 CropFrame(video_buffer.get(), width_, height_, 0, 34, 352, 219, | |
269 &cropped_source_frame); | |
270 TestSize(video_frame_, cropped_source_frame, 281, 175, 29.3, vpm_); | |
271 | |
272 // Stop timer. | |
273 const int64_t runtime = (TickTime::Now() - time_start).Microseconds(); | |
274 if (runtime < min_runtime || run_idx == 0) { | |
275 min_runtime = runtime; | |
276 } | |
277 total_runtime += runtime; | |
278 } | |
279 | |
280 printf("\nAverage run time = %d us / frame\n", | |
281 static_cast<int>(total_runtime)); | |
282 printf("Min run time = %d us / frame\n\n", | |
283 static_cast<int>(min_runtime)); | |
284 } | |
285 | |
286 void PreprocessFrameAndVerify(const VideoFrame& source, | |
287 int target_width, | |
288 int target_height, | |
289 VideoProcessingModule* vpm, | |
290 VideoFrame** out_frame) { | |
291 ASSERT_EQ(VPM_OK, vpm->SetTargetResolution(target_width, target_height, 30)); | |
292 ASSERT_EQ(VPM_OK, vpm->PreprocessFrame(source, out_frame)); | |
293 | |
294 // If no resizing is needed, expect NULL. | |
295 if (target_width == source.width() && target_height == source.height()) { | |
296 EXPECT_EQ(NULL, *out_frame); | |
297 return; | |
298 } | |
299 | |
300 // Verify the resampled frame. | |
301 EXPECT_TRUE(*out_frame != NULL); | |
302 EXPECT_EQ(source.render_time_ms(), (*out_frame)->render_time_ms()); | |
303 EXPECT_EQ(source.timestamp(), (*out_frame)->timestamp()); | |
304 EXPECT_EQ(target_width, (*out_frame)->width()); | |
305 EXPECT_EQ(target_height, (*out_frame)->height()); | |
306 } | |
307 | |
308 void CropFrame(const uint8_t* source_data, | |
309 int source_width, | |
310 int source_height, | |
311 int offset_x, | |
312 int offset_y, | |
313 int cropped_width, | |
314 int cropped_height, | |
315 VideoFrame* cropped_frame) { | |
316 cropped_frame->CreateEmptyFrame(cropped_width, cropped_height, cropped_width, | |
317 (cropped_width + 1) / 2, | |
318 (cropped_width + 1) / 2); | |
319 EXPECT_EQ(0, | |
320 ConvertToI420(kI420, source_data, offset_x, offset_y, source_width, | |
321 source_height, 0, kVideoRotation_0, cropped_frame)); | |
322 } | |
323 | |
324 void TestSize(const VideoFrame& source_frame, | |
325 const VideoFrame& cropped_source_frame, | |
326 int target_width, | |
327 int target_height, | |
328 double expected_psnr, | |
329 VideoProcessingModule* vpm) { | |
330 // Resample source_frame to out_frame. | |
331 VideoFrame* out_frame = NULL; | |
332 vpm->SetInputFrameResampleMode(kBox); | |
333 PreprocessFrameAndVerify(source_frame, target_width, target_height, vpm, | |
334 &out_frame); | |
335 if (out_frame == NULL) | |
336 return; | |
337 WriteProcessedFrameForVisualInspection(source_frame, *out_frame); | |
338 | |
339 // Scale |resampled_source_frame| back to the source scale. | |
340 VideoFrame resampled_source_frame; | |
341 resampled_source_frame.CopyFrame(*out_frame); | |
342 PreprocessFrameAndVerify(resampled_source_frame, cropped_source_frame.width(), | |
343 cropped_source_frame.height(), vpm, &out_frame); | |
344 WriteProcessedFrameForVisualInspection(resampled_source_frame, *out_frame); | |
345 | |
346 // Compute PSNR against the cropped source frame and check expectation. | |
347 double psnr = I420PSNR(&cropped_source_frame, out_frame); | |
348 EXPECT_GT(psnr, expected_psnr); | |
349 printf("PSNR: %f. PSNR is between source of size %d %d, and a modified " | |
350 "source which is scaled down/up to: %d %d, and back to source size \n", | |
351 psnr, source_frame.width(), source_frame.height(), | |
352 target_width, target_height); | |
353 } | |
354 | |
355 bool CompareFrames(const webrtc::VideoFrame& frame1, | |
356 const webrtc::VideoFrame& frame2) { | |
357 for (int plane = 0; plane < webrtc::kNumOfPlanes; plane ++) { | |
358 webrtc::PlaneType plane_type = static_cast<webrtc::PlaneType>(plane); | |
359 int allocated_size1 = frame1.allocated_size(plane_type); | |
360 int allocated_size2 = frame2.allocated_size(plane_type); | |
361 if (allocated_size1 != allocated_size2) | |
362 return false; | |
363 const uint8_t* plane_buffer1 = frame1.buffer(plane_type); | |
364 const uint8_t* plane_buffer2 = frame2.buffer(plane_type); | |
365 if (memcmp(plane_buffer1, plane_buffer2, allocated_size1)) | |
366 return false; | |
367 } | |
368 return true; | |
369 } | |
370 | |
371 void WriteProcessedFrameForVisualInspection(const VideoFrame& source, | |
372 const VideoFrame& processed) { | |
373 // Skip if writing to files is not enabled. | |
374 if (!FLAGS_gen_files) | |
375 return; | |
376 // Write the processed frame to file for visual inspection. | |
377 std::ostringstream filename; | |
378 filename << webrtc::test::OutputPath() << "Resampler_from_" << source.width() | |
379 << "x" << source.height() << "_to_" << processed.width() << "x" | |
380 << processed.height() << "_30Hz_P420.yuv"; | |
381 std::cout << "Watch " << filename.str() << " and verify that it is okay." | |
382 << std::endl; | |
383 FILE* stand_alone_file = fopen(filename.str().c_str(), "wb"); | |
384 if (PrintVideoFrame(processed, stand_alone_file) < 0) | |
385 std::cerr << "Failed to write: " << filename.str() << std::endl; | |
386 if (stand_alone_file) | |
387 fclose(stand_alone_file); | |
388 } | |
389 | |
390 } // namespace webrtc | |
OLD | NEW |