Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: webrtc/modules/video_processing/test/video_processing_unittest.cc

Issue 2362683002: New helper function test::ReadI420Buffer, refactor FrameReader to use it. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/timeutils.h" 19 #include "webrtc/base/timeutils.h"
19 #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"
20 #include "webrtc/test/testsupport/fileutils.h" 22 #include "webrtc/test/testsupport/fileutils.h"
21 23
22 namespace webrtc { 24 namespace webrtc {
23 25
24 namespace { 26 namespace {
25 27
26 // Define command line flag 'gen_files' (default value: false). 28 // Define command line flag 'gen_files' (default value: false).
27 DEFINE_bool(gen_files, false, "Output files for visual inspection."); 29 DEFINE_bool(gen_files, false, "Output files for visual inspection.");
28 30
29 } // namespace 31 } // namespace
30 32
31 static void PreprocessFrameAndVerify(const VideoFrame& source, 33 static void PreprocessFrameAndVerify(const VideoFrame& source,
32 int target_width, 34 int target_width,
33 int target_height, 35 int target_height,
34 VideoProcessing* vpm, 36 VideoProcessing* vpm,
35 const VideoFrame* out_frame); 37 const VideoFrame* out_frame);
36 static void CropFrame(const uint8_t* source_data, 38 static rtc::scoped_refptr<VideoFrameBuffer> CropFrame(
37 int source_width, 39 rtc::scoped_refptr<VideoFrameBuffer> source,
38 int source_height, 40 int offset_x,
39 int offset_x, 41 int offset_y,
40 int offset_y, 42 int cropped_width,
41 int cropped_width, 43 int cropped_height);
42 int cropped_height,
43 VideoFrame* cropped_frame);
44 // 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|,
45 // 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
46 // 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
47 // verified under the same conditions. 47 // verified under the same conditions.
48 static void TestSize(const VideoFrame& source_frame, 48 static void TestSize(const VideoFrame& source_frame,
49 const VideoFrame& cropped_source_frame, 49 const VideoFrameBuffer& cropped_source,
50 int target_width, 50 int target_width,
51 int target_height, 51 int target_height,
52 double expected_psnr, 52 double expected_psnr,
53 VideoProcessing* vpm); 53 VideoProcessing* vpm);
54 static void WriteProcessedFrameForVisualInspection(const VideoFrame& source, 54 static void WriteProcessedFrameForVisualInspection(const VideoFrame& source,
55 const VideoFrame& processed); 55 const VideoFrame& processed);
56 56
57 VideoProcessingTest::VideoProcessingTest() 57 VideoProcessingTest::VideoProcessingTest()
58 : vp_(NULL), 58 : vp_(NULL),
59 source_file_(NULL), 59 source_file_(NULL),
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 int64_t min_runtime = 0; 126 int64_t min_runtime = 0;
127 int64_t total_runtime = 0; 127 int64_t total_runtime = 0;
128 128
129 rewind(source_file_); 129 rewind(source_file_);
130 ASSERT_TRUE(source_file_ != NULL) << "Cannot read input file \n"; 130 ASSERT_TRUE(source_file_ != NULL) << "Cannot read input file \n";
131 131
132 // no temporal decimation 132 // no temporal decimation
133 vp_->EnableTemporalDecimation(false); 133 vp_->EnableTemporalDecimation(false);
134 134
135 // Reading test frame 135 // Reading test frame
136 std::unique_ptr<uint8_t[]> video_buffer(new uint8_t[frame_length_]); 136 rtc::scoped_refptr<VideoFrameBuffer> video_buffer(
137 ASSERT_EQ(frame_length_, 137 test::ReadI420Buffer(width_, height_, source_file_));
138 fread(video_buffer.get(), 1, frame_length_, source_file_)); 138 ASSERT_TRUE(video_buffer);
139 // Using ConvertToI420 to add stride to the image. 139
140 EXPECT_EQ(0, ConvertToI420(kI420, video_buffer.get(), 0, 0, width_, height_, 140 VideoFrame video_frame(video_buffer, kVideoRotation_0, 0);
141 0, kVideoRotation_0, &video_frame_));
142 // Cropped source frame that will contain the expected visible region.
143 VideoFrame cropped_source_frame;
144 cropped_source_frame.CopyFrame(video_frame_);
145 141
146 for (uint32_t run_idx = 0; run_idx < NumRuns; run_idx++) { 142 for (uint32_t run_idx = 0; run_idx < NumRuns; run_idx++) {
147 // Initiate test timer. 143 // Initiate test timer.
148 const int64_t time_start = rtc::TimeNanos(); 144 const int64_t time_start = rtc::TimeNanos();
149 145
150 // Init the sourceFrame with a timestamp. 146 // Init the sourceFrame with a timestamp.
151 int64_t time_start_ms = time_start / rtc::kNumNanosecsPerMillisec; 147 int64_t time_start_ms = time_start / rtc::kNumNanosecsPerMillisec;
152 video_frame_.set_render_time_ms(time_start_ms); 148 video_frame_.set_render_time_ms(time_start_ms);
153 video_frame_.set_timestamp(time_start_ms * 90); 149 video_frame_.set_timestamp(time_start_ms * 90);
154 150
155 // Test scaling to different sizes: source is of |width|/|height| = 352/288. 151 // Test scaling to different sizes: source is of |width|/|height| = 352/288.
156 // Pure scaling: 152 // Pure scaling:
157 TestSize(video_frame_, video_frame_, width_ / 4, height_ / 4, 25.2, vp_); 153 TestSize(video_frame, *video_buffer, width_ / 4, height_ / 4, 25.2, vp_);
158 TestSize(video_frame_, video_frame_, width_ / 2, height_ / 2, 28.1, vp_); 154 TestSize(video_frame, *video_buffer, width_ / 2, height_ / 2, 28.1, vp_);
159 // No resampling: 155 // No resampling:
160 TestSize(video_frame_, video_frame_, width_, height_, -1, vp_); 156 TestSize(video_frame, *video_buffer, width_, height_, -1, vp_);
161 TestSize(video_frame_, video_frame_, 2 * width_, 2 * height_, 32.2, vp_); 157 TestSize(video_frame, *video_buffer, 2 * width_, 2 * height_, 32.2, vp_);
162 158
163 // Scaling and cropping. The cropped source frame is the largest center 159 // Scaling and cropping. The cropped source frame is the largest center
164 // aligned region that can be used from the source while preserving aspect 160 // aligned region that can be used from the source while preserving aspect
165 // ratio. 161 // ratio.
166 CropFrame(video_buffer.get(), width_, height_, 0, 56, 352, 176, 162 TestSize(video_frame_, *CropFrame(video_buffer, 0, 56, 352, 176),
167 &cropped_source_frame); 163 100, 50, 24.0, vp_);
168 TestSize(video_frame_, cropped_source_frame, 100, 50, 24.0, vp_);
169 164
170 CropFrame(video_buffer.get(), width_, height_, 0, 30, 352, 225, 165 TestSize(video_frame_, *CropFrame(video_buffer, 0, 30, 352, 225),
171 &cropped_source_frame); 166 400, 256, 31.3, vp_);
172 TestSize(video_frame_, cropped_source_frame, 400, 256, 31.3, vp_);
173 167
174 CropFrame(video_buffer.get(), width_, height_, 68, 0, 216, 288, 168 TestSize(video_frame_, *CropFrame(video_buffer, 68, 0, 216, 288),
175 &cropped_source_frame); 169 480, 640, 32.15, vp_);
176 TestSize(video_frame_, cropped_source_frame, 480, 640, 32.15, vp_);
177 170
178 CropFrame(video_buffer.get(), width_, height_, 0, 12, 352, 264, 171 TestSize(video_frame_, *CropFrame(video_buffer, 0, 12, 352, 264),
179 &cropped_source_frame); 172 960, 720, 32.2, vp_);
180 TestSize(video_frame_, cropped_source_frame, 960, 720, 32.2, vp_);
181 173
182 CropFrame(video_buffer.get(), width_, height_, 0, 44, 352, 198, 174 TestSize(video_frame_, *CropFrame(video_buffer, 0, 44, 352, 198),
183 &cropped_source_frame); 175 1280, 720, 32.15, vp_);
184 TestSize(video_frame_, cropped_source_frame, 1280, 720, 32.15, vp_);
185 176
186 // Upsampling to odd size. 177 // Upsampling to odd size.
187 CropFrame(video_buffer.get(), width_, height_, 0, 26, 352, 233, 178 TestSize(video_frame_, *CropFrame(video_buffer, 0, 26, 352, 233),
188 &cropped_source_frame); 179 501, 333, 32.05, vp_);
189 TestSize(video_frame_, cropped_source_frame, 501, 333, 32.05, vp_);
190 // Downsample to odd size. 180 // Downsample to odd size.
191 CropFrame(video_buffer.get(), width_, height_, 0, 34, 352, 219, 181 TestSize(video_frame_, *CropFrame(video_buffer, 0, 34, 352, 219),
192 &cropped_source_frame); 182 281, 175, 29.3, vp_);
193 TestSize(video_frame_, cropped_source_frame, 281, 175, 29.3, vp_);
194 183
195 // Stop timer. 184 // Stop timer.
196 const int64_t runtime = 185 const int64_t runtime =
197 (rtc::TimeNanos() - time_start) / rtc::kNumNanosecsPerMicrosec; 186 (rtc::TimeNanos() - time_start) / rtc::kNumNanosecsPerMicrosec;
198 if (runtime < min_runtime || run_idx == 0) { 187 if (runtime < min_runtime || run_idx == 0) {
199 min_runtime = runtime; 188 min_runtime = runtime;
200 } 189 }
201 total_runtime += runtime; 190 total_runtime += runtime;
202 } 191 }
203 192
(...skipping 18 matching lines...) Expand all
222 } 211 }
223 212
224 // Verify the resampled frame. 213 // Verify the resampled frame.
225 EXPECT_TRUE(out_frame != NULL); 214 EXPECT_TRUE(out_frame != NULL);
226 EXPECT_EQ(source.render_time_ms(), (out_frame)->render_time_ms()); 215 EXPECT_EQ(source.render_time_ms(), (out_frame)->render_time_ms());
227 EXPECT_EQ(source.timestamp(), (out_frame)->timestamp()); 216 EXPECT_EQ(source.timestamp(), (out_frame)->timestamp());
228 EXPECT_EQ(target_width, (out_frame)->width()); 217 EXPECT_EQ(target_width, (out_frame)->width());
229 EXPECT_EQ(target_height, (out_frame)->height()); 218 EXPECT_EQ(target_height, (out_frame)->height());
230 } 219 }
231 220
232 void CropFrame(const uint8_t* source_data, 221 rtc::scoped_refptr<VideoFrameBuffer> CropFrame(
233 int source_width, 222 rtc::scoped_refptr<VideoFrameBuffer> source,
234 int source_height, 223 int offset_x,
235 int offset_x, 224 int offset_y,
236 int offset_y, 225 int cropped_width,
237 int cropped_width, 226 int cropped_height) {
238 int cropped_height, 227 offset_x &= ~1;
239 VideoFrame* cropped_frame) { 228 offset_y &= ~1;
240 cropped_frame->CreateEmptyFrame(cropped_width, cropped_height, cropped_width, 229
241 (cropped_width + 1) / 2, 230 size_t start_y = offset_x + offset_y * source->StrideY();
242 (cropped_width + 1) / 2); 231 size_t start_u = offset_x / 2 + (offset_y / 2) * source->StrideU();
243 EXPECT_EQ(0, 232 size_t start_v = offset_x / 2 + (offset_y / 2) * source->StrideV();
244 ConvertToI420(kI420, source_data, offset_x, offset_y, source_width, 233
245 source_height, 0, kVideoRotation_0, cropped_frame)); 234 return rtc::scoped_refptr<VideoFrameBuffer>(
235 new rtc::RefCountedObject<WrappedI420Buffer>(
236 cropped_width, cropped_height,
237 source->DataY() + start_y, source->StrideY(),
238 source->DataU() + start_u, source->StrideU(),
239 source->DataV() + start_v, source->StrideV(),
240 rtc::KeepRefUntilDone(source)));
246 } 241 }
247 242
248 void TestSize(const VideoFrame& source_frame, 243 void TestSize(const VideoFrame& source_frame,
249 const VideoFrame& cropped_source_frame, 244 const VideoFrameBuffer& cropped_source,
250 int target_width, 245 int target_width,
251 int target_height, 246 int target_height,
252 double expected_psnr, 247 double expected_psnr,
253 VideoProcessing* vpm) { 248 VideoProcessing* vpm) {
254 // Resample source_frame to out_frame. 249 // Resample source_frame to out_frame.
255 VideoFrame* out_frame = NULL; 250 VideoFrame* out_frame = NULL;
256 vpm->SetInputFrameResampleMode(kBox); 251 vpm->SetInputFrameResampleMode(kBox);
257 PreprocessFrameAndVerify(source_frame, target_width, target_height, vpm, 252 PreprocessFrameAndVerify(source_frame, target_width, target_height, vpm,
258 out_frame); 253 out_frame);
259 if (out_frame == NULL) 254 if (out_frame == NULL)
260 return; 255 return;
261 WriteProcessedFrameForVisualInspection(source_frame, *out_frame); 256 WriteProcessedFrameForVisualInspection(source_frame, *out_frame);
262 257
263 // Scale |resampled_source_frame| back to the source scale. 258 // Scale |resampled_source_frame| back to the source scale.
264 VideoFrame resampled_source_frame; 259 VideoFrame resampled_source_frame;
265 resampled_source_frame.CopyFrame(*out_frame); 260 resampled_source_frame.CopyFrame(*out_frame);
266 PreprocessFrameAndVerify(resampled_source_frame, cropped_source_frame.width(), 261 PreprocessFrameAndVerify(resampled_source_frame, cropped_source.width(),
267 cropped_source_frame.height(), vpm, out_frame); 262 cropped_source.height(), vpm, out_frame);
268 WriteProcessedFrameForVisualInspection(resampled_source_frame, *out_frame); 263 WriteProcessedFrameForVisualInspection(resampled_source_frame, *out_frame);
269 264
270 // Compute PSNR against the cropped source frame and check expectation. 265 // Compute PSNR against the cropped source frame and check expectation.
271 double psnr = I420PSNR(&cropped_source_frame, out_frame); 266 double psnr = I420PSNR(cropped_source,
267 *out_frame->video_frame_buffer().get());
272 EXPECT_GT(psnr, expected_psnr); 268 EXPECT_GT(psnr, expected_psnr);
273 printf( 269 printf(
274 "PSNR: %f. PSNR is between source of size %d %d, and a modified " 270 "PSNR: %f. PSNR is between source of size %d %d, and a modified "
275 "source which is scaled down/up to: %d %d, and back to source size \n", 271 "source which is scaled down/up to: %d %d, and back to source size \n",
276 psnr, source_frame.width(), source_frame.height(), target_width, 272 psnr, source_frame.width(), source_frame.height(), target_width,
277 target_height); 273 target_height);
278 } 274 }
279 275
280 void WriteProcessedFrameForVisualInspection(const VideoFrame& source, 276 void WriteProcessedFrameForVisualInspection(const VideoFrame& source,
281 const VideoFrame& processed) { 277 const VideoFrame& processed) {
282 // Skip if writing to files is not enabled. 278 // Skip if writing to files is not enabled.
283 if (!FLAGS_gen_files) 279 if (!FLAGS_gen_files)
284 return; 280 return;
285 // Write the processed frame to file for visual inspection. 281 // Write the processed frame to file for visual inspection.
286 std::ostringstream filename; 282 std::ostringstream filename;
287 filename << webrtc::test::OutputPath() << "Resampler_from_" << source.width() 283 filename << webrtc::test::OutputPath() << "Resampler_from_" << source.width()
288 << "x" << source.height() << "_to_" << processed.width() << "x" 284 << "x" << source.height() << "_to_" << processed.width() << "x"
289 << processed.height() << "_30Hz_P420.yuv"; 285 << processed.height() << "_30Hz_P420.yuv";
290 std::cout << "Watch " << filename.str() << " and verify that it is okay." 286 std::cout << "Watch " << filename.str() << " and verify that it is okay."
291 << std::endl; 287 << std::endl;
292 FILE* stand_alone_file = fopen(filename.str().c_str(), "wb"); 288 FILE* stand_alone_file = fopen(filename.str().c_str(), "wb");
293 if (PrintVideoFrame(processed, stand_alone_file) < 0) 289 if (PrintVideoFrame(processed, stand_alone_file) < 0)
294 std::cerr << "Failed to write: " << filename.str() << std::endl; 290 std::cerr << "Failed to write: " << filename.str() << std::endl;
295 if (stand_alone_file) 291 if (stand_alone_file)
296 fclose(stand_alone_file); 292 fclose(stand_alone_file);
297 } 293 }
298 294
299 } // namespace webrtc 295 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698