Chromium Code Reviews| 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 <math.h> | 11 #include <math.h> |
| 12 #include <string.h> | 12 #include <string.h> |
| 13 | 13 |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "webrtc/base/bind.h" | 15 #include "webrtc/base/bind.h" |
| 16 #include "webrtc/test/fake_texture_frame.h" | 16 #include "webrtc/test/fake_texture_frame.h" |
| 17 #include "webrtc/test/frame_utils.h" | 17 #include "webrtc/test/frame_utils.h" |
| 18 #include "webrtc/video_frame.h" | 18 #include "webrtc/video_frame.h" |
| 19 | 19 |
| 20 namespace webrtc { | 20 namespace webrtc { |
| 21 | 21 |
| 22 int ExpectedSize(int plane_stride, int image_height, PlaneType type); | 22 namespace { |
| 23 | |
| 24 int ExpectedSize(int plane_stride, int image_height, PlaneType type) { | |
| 25 if (type == kYPlane) | |
| 26 return plane_stride * image_height; | |
| 27 return plane_stride * ((image_height + 1) / 2); | |
| 28 } | |
| 29 | |
| 30 rtc::scoped_refptr<I420Buffer> CreateGradient(int width, int height) { | |
| 31 rtc::scoped_refptr<I420Buffer> buffer( | |
| 32 new rtc::RefCountedObject<I420Buffer>(width, height)); | |
| 33 // Initialize with gradient, Y = 128(x/w + y/h), U = 256 x/w, V = 256 y/h | |
| 34 for (int x = 0; x < width; x++) { | |
| 35 for (int y = 0; y < height; y++) { | |
| 36 buffer->MutableDataY()[x + y * width] = | |
| 37 128 * (x * height + y * width) / (width * height); | |
| 38 } | |
| 39 } | |
| 40 int chroma_width = (width + 1) / 2; | |
| 41 int chroma_height = (height + 1) / 2; | |
| 42 for (int x = 0; x < chroma_width; x++) { | |
| 43 for (int y = 0; y < chroma_height; y++) { | |
| 44 buffer->MutableDataU()[x + y * chroma_width] = | |
| 45 255 * x / (chroma_width - 1); | |
| 46 buffer->MutableDataV()[x + y * chroma_width] = | |
| 47 255 * y / (chroma_height - 1); | |
| 48 } | |
| 49 } | |
| 50 return buffer; | |
| 51 } | |
| 52 | |
| 53 void CheckCrop(webrtc::VideoFrameBuffer* frame, | |
| 54 // The offsets and sizes describe the rectangle extracted from | |
| 55 // the original (gradient) frame, in relative coordinates where | |
|
pbos-webrtc
2016/06/03 14:53:22
Put comment outside argument list
nisse-webrtc
2016/06/09 08:10:05
Done.
| |
| 56 // the original frame correspond to the unit square, | |
| 57 // 0.0 <= x, y < 1.0. | |
| 58 double offset_x, | |
| 59 double offset_y, | |
| 60 double rel_width, | |
| 61 double rel_height) { | |
| 62 int width = frame->width(); | |
| 63 int height = frame->height(); | |
| 64 // Check that pixel values in the corners match the gradient used | |
| 65 // for initialization. | |
| 66 for (int i = 0; i < 2; i++) { | |
| 67 for (int j = 0; j < 2; j++) { | |
| 68 // Pixel coordinates of the corner. | |
| 69 int x = i * (width - 1); | |
| 70 int y = j * (height - 1); | |
| 71 // Relative coordinates, range 0.0 - 1.0 correspond to the | |
| 72 // size of the uncropped input frame. | |
| 73 double orig_x = offset_x + i * rel_width; | |
| 74 double orig_y = offset_y + j * rel_height; | |
| 75 | |
| 76 EXPECT_NEAR(frame->DataY()[x + y * frame->StrideY()] / 256.0, | |
| 77 (orig_x + orig_y) / 2, 0.02); | |
| 78 EXPECT_NEAR(frame->DataU()[x / 2 + (y / 2) * frame->StrideU()] / 256.0, | |
| 79 orig_x, 0.02); | |
| 80 EXPECT_NEAR(frame->DataV()[x / 2 + (y / 2) * frame->StrideV()] / 256.0, | |
| 81 orig_y, 0.02); | |
| 82 } | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 } // namespace | |
| 23 | 87 |
| 24 TEST(TestVideoFrame, InitialValues) { | 88 TEST(TestVideoFrame, InitialValues) { |
| 25 VideoFrame frame; | 89 VideoFrame frame; |
| 26 EXPECT_TRUE(frame.IsZeroSize()); | 90 EXPECT_TRUE(frame.IsZeroSize()); |
| 27 EXPECT_EQ(kVideoRotation_0, frame.rotation()); | 91 EXPECT_EQ(kVideoRotation_0, frame.rotation()); |
| 28 } | 92 } |
| 29 | 93 |
| 30 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { | 94 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { |
| 31 VideoFrame frame; | 95 VideoFrame frame; |
| 32 VideoFrame frame2; | 96 VideoFrame frame2; |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 TEST(TestI420FrameBuffer, Copy) { | 298 TEST(TestI420FrameBuffer, Copy) { |
| 235 rtc::scoped_refptr<I420Buffer> buf1( | 299 rtc::scoped_refptr<I420Buffer> buf1( |
| 236 new rtc::RefCountedObject<I420Buffer>(20, 10)); | 300 new rtc::RefCountedObject<I420Buffer>(20, 10)); |
| 237 memset(buf1->MutableDataY(), 1, 200); | 301 memset(buf1->MutableDataY(), 1, 200); |
| 238 memset(buf1->MutableDataU(), 2, 50); | 302 memset(buf1->MutableDataU(), 2, 50); |
| 239 memset(buf1->MutableDataV(), 3, 50); | 303 memset(buf1->MutableDataV(), 3, 50); |
| 240 rtc::scoped_refptr<I420Buffer> buf2 = I420Buffer::Copy(buf1); | 304 rtc::scoped_refptr<I420Buffer> buf2 = I420Buffer::Copy(buf1); |
| 241 EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2)); | 305 EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2)); |
| 242 } | 306 } |
| 243 | 307 |
| 308 TEST(TestI420FrameBuffer, Scale) { | |
| 309 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); | |
| 310 | |
| 311 // Pure scaling, no cropping. | |
| 312 rtc::scoped_refptr<I420Buffer> scaled( | |
| 313 new rtc::RefCountedObject<I420Buffer>(150, 75)); | |
| 314 | |
| 315 scaled->Scale(buf); | |
|
pbos-webrtc
2016/06/03 14:53:22
scaled_buffer please
nisse-webrtc
2016/06/09 08:10:05
Done.
| |
| 316 CheckCrop(scaled, 0.0, 0.0, 1.0, 1.0); | |
| 317 } | |
| 318 | |
| 319 TEST(TestI420FrameBuffer, CropXCenter) { | |
| 320 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); | |
| 321 | |
| 322 // Pure center cropping, no scaling. | |
| 323 rtc::scoped_refptr<I420Buffer> scaled( | |
| 324 new rtc::RefCountedObject<I420Buffer>(100, 100)); | |
| 325 | |
| 326 scaled->CropAndScale(buf, 50, 0, 100, 100); | |
| 327 CheckCrop(scaled, 0.25, 0.0, 0.5, 1.0); | |
| 328 } | |
| 329 | |
| 330 TEST(TestI420FrameBuffer, CropXNotCenter) { | |
| 331 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); | |
| 332 | |
| 333 // Non-center cropping, no scaling. | |
| 334 rtc::scoped_refptr<I420Buffer> scaled( | |
| 335 new rtc::RefCountedObject<I420Buffer>(100, 100)); | |
| 336 | |
| 337 scaled->CropAndScale(buf, 25, 0, 100, 100); | |
| 338 CheckCrop(scaled, 0.125, 0.0, 0.5, 1.0); | |
| 339 } | |
| 340 | |
| 341 TEST(TestI420FrameBuffer, CropYCenter) { | |
| 342 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200); | |
| 343 | |
| 344 // Pure center cropping, no scaling. | |
| 345 rtc::scoped_refptr<I420Buffer> scaled( | |
| 346 new rtc::RefCountedObject<I420Buffer>(100, 100)); | |
| 347 | |
| 348 scaled->CropAndScale(buf, 0, 50, 100, 100); | |
| 349 CheckCrop(scaled, 0.0, 0.25, 1.0, 0.5); | |
| 350 } | |
| 351 | |
| 352 TEST(TestI420FrameBuffer, CropYNotCenter) { | |
| 353 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200); | |
| 354 | |
| 355 // Non-center cropping, no scaling. | |
| 356 rtc::scoped_refptr<I420Buffer> scaled( | |
| 357 new rtc::RefCountedObject<I420Buffer>(100, 100)); | |
| 358 | |
| 359 scaled->CropAndScale(buf, 0, 25, 100, 100); | |
| 360 CheckCrop(scaled, 0.0, 0.125, 1.0, 0.5); | |
| 361 } | |
| 362 | |
| 363 TEST(TestI420FrameBuffer, CropAndScale16x9) { | |
| 364 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(640, 480); | |
| 365 | |
| 366 // Center crop to 640 x 360 (16/9 aspect), then scale down by 2. | |
| 367 rtc::scoped_refptr<I420Buffer> scaled( | |
| 368 new rtc::RefCountedObject<I420Buffer>(320, 180)); | |
| 369 | |
| 370 scaled->CropAndScale(buf); | |
| 371 CheckCrop(scaled, 0.0, 0.125, 1.0, 0.75); | |
| 372 } | |
| 373 | |
| 244 } // namespace webrtc | 374 } // namespace webrtc |
| OLD | NEW |