| 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> | 13 #include <gflags/gflags.h> | 
| 14 | 14 | 
| 15 #include <memory> | 15 #include <memory> | 
| 16 #include <string> | 16 #include <string> | 
| 17 | 17 | 
| 18 #include "webrtc/base/keep_ref_until_done.h" | 18 #include "webrtc/base/keep_ref_until_done.h" | 
| 19 #include "webrtc/base/timeutils.h" | 19 #include "webrtc/base/timeutils.h" | 
| 20 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 20 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 
|  | 21 #include "webrtc/test/frame_utils.h" | 
| 21 #include "webrtc/test/testsupport/fileutils.h" | 22 #include "webrtc/test/testsupport/fileutils.h" | 
| 22 | 23 | 
| 23 namespace webrtc { | 24 namespace webrtc { | 
| 24 | 25 | 
| 25 namespace { | 26 namespace { | 
| 26 | 27 | 
| 27 // Define command line flag 'gen_files' (default value: false). | 28 // Define command line flag 'gen_files' (default value: false). | 
| 28 DEFINE_bool(gen_files, false, "Output files for visual inspection."); | 29 DEFINE_bool(gen_files, false, "Output files for visual inspection."); | 
| 29 | 30 | 
| 30 }  // namespace | 31 }  // namespace | 
| 31 | 32 | 
| 32 static void PreprocessFrameAndVerify(const VideoFrame& source, | 33 static void PreprocessFrameAndVerify(const VideoFrame& source, | 
| 33                                      int target_width, | 34                                      int target_width, | 
| 34                                      int target_height, | 35                                      int target_height, | 
| 35                                      VideoProcessing* vpm, | 36                                      VideoProcessing* vpm, | 
| 36                                      const VideoFrame* out_frame); | 37                                      const VideoFrame* out_frame); | 
| 37 rtc::scoped_refptr<VideoFrameBuffer> CropBuffer( | 38 static rtc::scoped_refptr<VideoFrameBuffer> CropBuffer( | 
| 38     const rtc::scoped_refptr<VideoFrameBuffer>& source_buffer, | 39     rtc::scoped_refptr<VideoFrameBuffer> source_buffer, | 
| 39     int source_width, |  | 
| 40     int source_height, |  | 
| 41     int offset_x, | 40     int offset_x, | 
| 42     int offset_y, | 41     int offset_y, | 
| 43     int cropped_width, | 42     int cropped_width, | 
| 44     int cropped_height); | 43     int cropped_height); | 
| 45 // 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|, | 
| 46 // 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 | 
| 47 // 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 | 
| 48 // verified under the same conditions. | 47 // verified under the same conditions. | 
| 49 static void TestSize( | 48 static void TestSize( | 
| 50     const VideoFrame& source_frame, | 49     const VideoFrame& source_frame, | 
| 51     const rtc::scoped_refptr<VideoFrameBuffer>& cropped_source_buffer, | 50     const VideoFrameBuffer& cropped_source_buffer, | 
| 52     int target_width, | 51     int target_width, | 
| 53     int target_height, | 52     int target_height, | 
| 54     double expected_psnr, | 53     double expected_psnr, | 
| 55     VideoProcessing* vpm); | 54     VideoProcessing* vpm); | 
| 56 static void WriteProcessedFrameForVisualInspection(const VideoFrame& source, | 55 static void WriteProcessedFrameForVisualInspection(const VideoFrame& source, | 
| 57                                                    const VideoFrame& processed); | 56                                                    const VideoFrame& processed); | 
| 58 | 57 | 
| 59 VideoProcessingTest::VideoProcessingTest() | 58 VideoProcessingTest::VideoProcessingTest() | 
| 60     : vp_(NULL), | 59     : vp_(NULL), | 
| 61       source_file_(NULL), | 60       source_file_(NULL), | 
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 126   int64_t min_runtime = 0; | 125   int64_t min_runtime = 0; | 
| 127   int64_t total_runtime = 0; | 126   int64_t total_runtime = 0; | 
| 128 | 127 | 
| 129   rewind(source_file_); | 128   rewind(source_file_); | 
| 130   ASSERT_TRUE(source_file_ != NULL) << "Cannot read input file \n"; | 129   ASSERT_TRUE(source_file_ != NULL) << "Cannot read input file \n"; | 
| 131 | 130 | 
| 132   // no temporal decimation | 131   // no temporal decimation | 
| 133   vp_->EnableTemporalDecimation(false); | 132   vp_->EnableTemporalDecimation(false); | 
| 134 | 133 | 
| 135   // Reading test frame | 134   // Reading test frame | 
| 136   rtc::scoped_refptr<webrtc::I420Buffer> buffer = | 135   rtc::scoped_refptr<VideoFrameBuffer> video_buffer( | 
| 137       I420Buffer::Create(width_, height_, width_, half_width_, half_width_); | 136       test::ReadI420Buffer(width_, height_, source_file_)); | 
| 138 | 137   ASSERT_TRUE(video_buffer); | 
| 139   ASSERT_EQ(static_cast<size_t>(size_y_), |  | 
| 140             fread(buffer->MutableDataY(), 1, size_y_, source_file_)); |  | 
| 141   ASSERT_EQ(static_cast<size_t>(size_uv_), |  | 
| 142             fread(buffer->MutableDataU(), 1, size_uv_, source_file_)); |  | 
| 143   ASSERT_EQ(static_cast<size_t>(size_uv_), |  | 
| 144             fread(buffer->MutableDataV(), 1, size_uv_, source_file_)); |  | 
| 145 | 138 | 
| 146   for (uint32_t run_idx = 0; run_idx < NumRuns; run_idx++) { | 139   for (uint32_t run_idx = 0; run_idx < NumRuns; run_idx++) { | 
| 147     // Initiate test timer. | 140     // Initiate test timer. | 
| 148     const int64_t time_start = rtc::TimeNanos(); | 141     const int64_t time_start = rtc::TimeNanos(); | 
| 149 | 142 | 
| 150     // Init the sourceFrame with a timestamp. | 143     // Init the sourceFrame with a timestamp. | 
| 151     int64_t time_start_ms = time_start / rtc::kNumNanosecsPerMillisec; | 144     int64_t time_start_ms = time_start / rtc::kNumNanosecsPerMillisec; | 
| 152     VideoFrame video_frame(buffer, time_start_ms * 90, time_start_ms, | 145     VideoFrame video_frame(video_buffer, time_start_ms * 90, time_start_ms, | 
| 153                            webrtc::kVideoRotation_0); | 146                            webrtc::kVideoRotation_0); | 
| 154 | 147 | 
| 155     // Test scaling to different sizes: source is of |width|/|height| = 352/288. | 148     // Test scaling to different sizes: source is of |width|/|height| = 352/288. | 
| 156     // Pure scaling: | 149     // Pure scaling: | 
| 157     TestSize(video_frame, buffer, width_ / 4, height_ / 4, 25.2, vp_); | 150     TestSize(video_frame, *video_buffer, width_ / 4, height_ / 4, 25.2, vp_); | 
| 158     TestSize(video_frame, buffer, width_ / 2, height_ / 2, 28.1, vp_); | 151     TestSize(video_frame, *video_buffer, width_ / 2, height_ / 2, 28.1, vp_); | 
| 159     // No resampling: | 152     // No resampling: | 
| 160     TestSize(video_frame, buffer, width_, height_, -1, vp_); | 153     TestSize(video_frame, *video_buffer, width_, height_, -1, vp_); | 
| 161     TestSize(video_frame, buffer, 2 * width_, 2 * height_, 32.2, vp_); | 154     TestSize(video_frame, *video_buffer, 2 * width_, 2 * height_, 32.2, vp_); | 
| 162 | 155 | 
| 163     // Scaling and cropping. The cropped source frame is the largest center | 156     // Scaling and cropping. The cropped source frame is the largest center | 
| 164     // aligned region that can be used from the source while preserving aspect | 157     // aligned region that can be used from the source while preserving aspect | 
| 165     // ratio. | 158     // ratio. | 
| 166     TestSize(video_frame, CropBuffer(buffer, width_, height_, 0, 56, 352, 176), | 159     TestSize(video_frame, *CropBuffer(video_buffer, 0, 56, 352, 176), | 
| 167              100, 50, 24.0, vp_); | 160              100, 50, 24.0, vp_); | 
| 168     TestSize(video_frame, CropBuffer(buffer, width_, height_, 0, 30, 352, 225), | 161     TestSize(video_frame, *CropBuffer(video_buffer, 0, 30, 352, 225), | 
| 169              400, 256, 31.3, vp_); | 162              400, 256, 31.3, vp_); | 
| 170     TestSize(video_frame, CropBuffer(buffer, width_, height_, 68, 0, 216, 288), | 163     TestSize(video_frame, *CropBuffer(video_buffer, 68, 0, 216, 288), | 
| 171              480, 640, 32.15, vp_); | 164              480, 640, 32.15, vp_); | 
| 172     TestSize(video_frame, CropBuffer(buffer, width_, height_, 0, 12, 352, 264), | 165     TestSize(video_frame, *CropBuffer(video_buffer, 0, 12, 352, 264), | 
| 173              960, 720, 32.2, vp_); | 166              960, 720, 32.2, vp_); | 
| 174     TestSize(video_frame, CropBuffer(buffer, width_, height_, 0, 44, 352, 198), | 167     TestSize(video_frame, *CropBuffer(video_buffer, 0, 44, 352, 198), | 
| 175              1280, 720, 32.15, vp_); | 168              1280, 720, 32.15, vp_); | 
| 176 | 169 | 
| 177     // Upsampling to odd size. | 170     // Upsampling to odd size. | 
| 178     TestSize(video_frame, CropBuffer(buffer, width_, height_, 0, 26, 352, 233), | 171     TestSize(video_frame, *CropBuffer(video_buffer, 0, 26, 352, 233), | 
| 179              501, 333, 32.05, vp_); | 172              501, 333, 32.05, vp_); | 
| 180     // Downsample to odd size. | 173     // Downsample to odd size. | 
| 181     TestSize(video_frame, CropBuffer(buffer, width_, height_, 0, 34, 352, 219), | 174     TestSize(video_frame, *CropBuffer(video_buffer, 0, 34, 352, 219), | 
| 182              281, 175, 29.3, vp_); | 175              281, 175, 29.3, vp_); | 
| 183 | 176 | 
| 184     // Stop timer. | 177     // Stop timer. | 
| 185     const int64_t runtime = | 178     const int64_t runtime = | 
| 186         (rtc::TimeNanos() - time_start) / rtc::kNumNanosecsPerMicrosec; | 179         (rtc::TimeNanos() - time_start) / rtc::kNumNanosecsPerMicrosec; | 
| 187     if (runtime < min_runtime || run_idx == 0) { | 180     if (runtime < min_runtime || run_idx == 0) { | 
| 188       min_runtime = runtime; | 181       min_runtime = runtime; | 
| 189     } | 182     } | 
| 190     total_runtime += runtime; | 183     total_runtime += runtime; | 
| 191   } | 184   } | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
| 212 | 205 | 
| 213   // Verify the resampled frame. | 206   // Verify the resampled frame. | 
| 214   EXPECT_TRUE(out_frame != NULL); | 207   EXPECT_TRUE(out_frame != NULL); | 
| 215   EXPECT_EQ(source.render_time_ms(), (out_frame)->render_time_ms()); | 208   EXPECT_EQ(source.render_time_ms(), (out_frame)->render_time_ms()); | 
| 216   EXPECT_EQ(source.timestamp(), (out_frame)->timestamp()); | 209   EXPECT_EQ(source.timestamp(), (out_frame)->timestamp()); | 
| 217   EXPECT_EQ(target_width, (out_frame)->width()); | 210   EXPECT_EQ(target_width, (out_frame)->width()); | 
| 218   EXPECT_EQ(target_height, (out_frame)->height()); | 211   EXPECT_EQ(target_height, (out_frame)->height()); | 
| 219 } | 212 } | 
| 220 | 213 | 
| 221 rtc::scoped_refptr<VideoFrameBuffer> CropBuffer( | 214 rtc::scoped_refptr<VideoFrameBuffer> CropBuffer( | 
| 222     const rtc::scoped_refptr<VideoFrameBuffer>& source_buffer, | 215     rtc::scoped_refptr<VideoFrameBuffer> source_buffer, | 
| 223     int source_width, |  | 
| 224     int source_height, |  | 
| 225     int offset_x, | 216     int offset_x, | 
| 226     int offset_y, | 217     int offset_y, | 
| 227     int cropped_width, | 218     int cropped_width, | 
| 228     int cropped_height) { | 219     int cropped_height) { | 
| 229   // Force even. | 220   // Force even. | 
| 230   offset_x &= 1; | 221   offset_x &= ~1; | 
| 231   offset_y &= 1; | 222   offset_y &= ~1; | 
| 232 | 223 | 
| 233   size_t y_start = offset_x + offset_y * source_buffer->StrideY(); | 224   size_t y_start = offset_x + offset_y * source_buffer->StrideY(); | 
| 234   size_t u_start = (offset_x / 2) + (offset_y / 2) * source_buffer->StrideU(); | 225   size_t u_start = (offset_x / 2) + (offset_y / 2) * source_buffer->StrideU(); | 
| 235   size_t v_start = (offset_x / 2) + (offset_y / 2) * source_buffer->StrideU(); | 226   size_t v_start = (offset_x / 2) + (offset_y / 2) * source_buffer->StrideV(); | 
| 236 | 227 | 
| 237   return rtc::scoped_refptr<VideoFrameBuffer>( | 228   return rtc::scoped_refptr<VideoFrameBuffer>( | 
| 238       new rtc::RefCountedObject<WrappedI420Buffer>( | 229       new rtc::RefCountedObject<WrappedI420Buffer>( | 
| 239           cropped_width, cropped_height, source_buffer->DataY() + y_start, | 230           cropped_width, cropped_height, source_buffer->DataY() + y_start, | 
| 240           source_buffer->StrideY(), source_buffer->DataU() + u_start, | 231           source_buffer->StrideY(), source_buffer->DataU() + u_start, | 
| 241           source_buffer->StrideU(), source_buffer->DataV() + v_start, | 232           source_buffer->StrideU(), source_buffer->DataV() + v_start, | 
| 242           source_buffer->StrideV(), rtc::KeepRefUntilDone(source_buffer))); | 233           source_buffer->StrideV(), rtc::KeepRefUntilDone(source_buffer))); | 
| 243 } | 234 } | 
| 244 | 235 | 
| 245 void TestSize(const VideoFrame& source_frame, | 236 void TestSize(const VideoFrame& source_frame, | 
| 246               const rtc::scoped_refptr<VideoFrameBuffer>& cropped_source_buffer, | 237               const VideoFrameBuffer& cropped_source, | 
| 247               int target_width, | 238               int target_width, | 
| 248               int target_height, | 239               int target_height, | 
| 249               double expected_psnr, | 240               double expected_psnr, | 
| 250               VideoProcessing* vpm) { | 241               VideoProcessing* vpm) { | 
| 251   // Resample source_frame to out_frame. | 242   // Resample source_frame to out_frame. | 
| 252   VideoFrame* out_frame = NULL; | 243   VideoFrame* out_frame = NULL; | 
| 253   vpm->SetInputFrameResampleMode(kBox); | 244   vpm->SetInputFrameResampleMode(kBox); | 
| 254   PreprocessFrameAndVerify(source_frame, target_width, target_height, vpm, | 245   PreprocessFrameAndVerify(source_frame, target_width, target_height, vpm, | 
| 255                            out_frame); | 246                            out_frame); | 
| 256   if (out_frame == NULL) | 247   if (out_frame == NULL) | 
| 257     return; | 248     return; | 
| 258   WriteProcessedFrameForVisualInspection(source_frame, *out_frame); | 249   WriteProcessedFrameForVisualInspection(source_frame, *out_frame); | 
| 259 | 250 | 
| 260   // Scale |resampled_source_frame| back to the source scale. | 251   // Scale |resampled_source_frame| back to the source scale. | 
| 261   VideoFrame resampled_source_frame; | 252   VideoFrame resampled_source_frame; | 
| 262   resampled_source_frame.CopyFrame(*out_frame); | 253   resampled_source_frame.CopyFrame(*out_frame); | 
| 263   PreprocessFrameAndVerify(resampled_source_frame, | 254   // Compute PSNR against the cropped source frame and check expectation. | 
| 264                            cropped_source_buffer->width(), | 255   PreprocessFrameAndVerify(resampled_source_frame, cropped_source.width(), | 
| 265                            cropped_source_buffer->height(), vpm, out_frame); | 256                            cropped_source.height(), vpm, out_frame); | 
| 266   WriteProcessedFrameForVisualInspection(resampled_source_frame, *out_frame); | 257   WriteProcessedFrameForVisualInspection(resampled_source_frame, *out_frame); | 
| 267 | 258 | 
| 268   // Compute PSNR against the cropped source frame and check expectation. | 259   // Compute PSNR against the cropped source frame and check expectation. | 
| 269   double psnr = | 260   double psnr = | 
| 270       I420PSNR(*cropped_source_buffer, *out_frame->video_frame_buffer()); | 261       I420PSNR(cropped_source, *out_frame->video_frame_buffer()); | 
| 271   EXPECT_GT(psnr, expected_psnr); | 262   EXPECT_GT(psnr, expected_psnr); | 
| 272   printf( | 263   printf( | 
| 273       "PSNR: %f. PSNR is between source of size %d %d, and a modified " | 264       "PSNR: %f. PSNR is between source of size %d %d, and a modified " | 
| 274       "source which is scaled down/up to: %d %d, and back to source size \n", | 265       "source which is scaled down/up to: %d %d, and back to source size \n", | 
| 275       psnr, source_frame.width(), source_frame.height(), target_width, | 266       psnr, source_frame.width(), source_frame.height(), target_width, | 
| 276       target_height); | 267       target_height); | 
| 277 } | 268 } | 
| 278 | 269 | 
| 279 void WriteProcessedFrameForVisualInspection(const VideoFrame& source, | 270 void WriteProcessedFrameForVisualInspection(const VideoFrame& source, | 
| 280                                             const VideoFrame& processed) { | 271                                             const VideoFrame& processed) { | 
| 281   // Skip if writing to files is not enabled. | 272   // Skip if writing to files is not enabled. | 
| 282   if (!FLAGS_gen_files) | 273   if (!FLAGS_gen_files) | 
| 283     return; | 274     return; | 
| 284   // Write the processed frame to file for visual inspection. | 275   // Write the processed frame to file for visual inspection. | 
| 285   std::ostringstream filename; | 276   std::ostringstream filename; | 
| 286   filename << webrtc::test::OutputPath() << "Resampler_from_" << source.width() | 277   filename << webrtc::test::OutputPath() << "Resampler_from_" << source.width() | 
| 287            << "x" << source.height() << "_to_" << processed.width() << "x" | 278            << "x" << source.height() << "_to_" << processed.width() << "x" | 
| 288            << processed.height() << "_30Hz_P420.yuv"; | 279            << processed.height() << "_30Hz_P420.yuv"; | 
| 289   std::cout << "Watch " << filename.str() << " and verify that it is okay." | 280   std::cout << "Watch " << filename.str() << " and verify that it is okay." | 
| 290             << std::endl; | 281             << std::endl; | 
| 291   FILE* stand_alone_file = fopen(filename.str().c_str(), "wb"); | 282   FILE* stand_alone_file = fopen(filename.str().c_str(), "wb"); | 
| 292   if (PrintVideoFrame(processed, stand_alone_file) < 0) | 283   if (PrintVideoFrame(processed, stand_alone_file) < 0) | 
| 293     std::cerr << "Failed to write: " << filename.str() << std::endl; | 284     std::cerr << "Failed to write: " << filename.str() << std::endl; | 
| 294   if (stand_alone_file) | 285   if (stand_alone_file) | 
| 295     fclose(stand_alone_file); | 286     fclose(stand_alone_file); | 
| 296 } | 287 } | 
| 297 | 288 | 
| 298 }  // namespace webrtc | 289 }  // namespace webrtc | 
| OLD | NEW | 
|---|