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