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 // The offsets and sizes describe the rectangle extracted from the |
| 54 // original (gradient) frame, in relative coordinates where the |
| 55 // original frame correspond to the unit square, 0.0 <= x, y < 1.0. |
| 56 void CheckCrop(webrtc::VideoFrameBuffer* frame, |
| 57 double offset_x, |
| 58 double offset_y, |
| 59 double rel_width, |
| 60 double rel_height) { |
| 61 int width = frame->width(); |
| 62 int height = frame->height(); |
| 63 // Check that pixel values in the corners match the gradient used |
| 64 // for initialization. |
| 65 for (int i = 0; i < 2; i++) { |
| 66 for (int j = 0; j < 2; j++) { |
| 67 // Pixel coordinates of the corner. |
| 68 int x = i * (width - 1); |
| 69 int y = j * (height - 1); |
| 70 // Relative coordinates, range 0.0 - 1.0 correspond to the |
| 71 // size of the uncropped input frame. |
| 72 double orig_x = offset_x + i * rel_width; |
| 73 double orig_y = offset_y + j * rel_height; |
| 74 |
| 75 EXPECT_NEAR(frame->DataY()[x + y * frame->StrideY()] / 256.0, |
| 76 (orig_x + orig_y) / 2, 0.02); |
| 77 EXPECT_NEAR(frame->DataU()[x / 2 + (y / 2) * frame->StrideU()] / 256.0, |
| 78 orig_x, 0.02); |
| 79 EXPECT_NEAR(frame->DataV()[x / 2 + (y / 2) * frame->StrideV()] / 256.0, |
| 80 orig_y, 0.02); |
| 81 } |
| 82 } |
| 83 } |
| 84 |
| 85 } // namespace |
23 | 86 |
24 TEST(TestVideoFrame, InitialValues) { | 87 TEST(TestVideoFrame, InitialValues) { |
25 VideoFrame frame; | 88 VideoFrame frame; |
26 EXPECT_TRUE(frame.IsZeroSize()); | 89 EXPECT_TRUE(frame.IsZeroSize()); |
27 EXPECT_EQ(kVideoRotation_0, frame.rotation()); | 90 EXPECT_EQ(kVideoRotation_0, frame.rotation()); |
28 } | 91 } |
29 | 92 |
30 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { | 93 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { |
31 VideoFrame frame; | 94 VideoFrame frame; |
32 VideoFrame frame2; | 95 VideoFrame frame2; |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 TEST(TestI420FrameBuffer, Copy) { | 297 TEST(TestI420FrameBuffer, Copy) { |
235 rtc::scoped_refptr<I420Buffer> buf1( | 298 rtc::scoped_refptr<I420Buffer> buf1( |
236 new rtc::RefCountedObject<I420Buffer>(20, 10)); | 299 new rtc::RefCountedObject<I420Buffer>(20, 10)); |
237 memset(buf1->MutableDataY(), 1, 200); | 300 memset(buf1->MutableDataY(), 1, 200); |
238 memset(buf1->MutableDataU(), 2, 50); | 301 memset(buf1->MutableDataU(), 2, 50); |
239 memset(buf1->MutableDataV(), 3, 50); | 302 memset(buf1->MutableDataV(), 3, 50); |
240 rtc::scoped_refptr<I420Buffer> buf2 = I420Buffer::Copy(buf1); | 303 rtc::scoped_refptr<I420Buffer> buf2 = I420Buffer::Copy(buf1); |
241 EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2)); | 304 EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2)); |
242 } | 305 } |
243 | 306 |
| 307 TEST(TestI420FrameBuffer, Scale) { |
| 308 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); |
| 309 |
| 310 // Pure scaling, no cropping. |
| 311 rtc::scoped_refptr<I420Buffer> scaled_buffer( |
| 312 new rtc::RefCountedObject<I420Buffer>(150, 75)); |
| 313 |
| 314 scaled_buffer->ScaleFrom(buf); |
| 315 CheckCrop(scaled_buffer, 0.0, 0.0, 1.0, 1.0); |
| 316 } |
| 317 |
| 318 TEST(TestI420FrameBuffer, CropXCenter) { |
| 319 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); |
| 320 |
| 321 // Pure center cropping, no scaling. |
| 322 rtc::scoped_refptr<I420Buffer> scaled_buffer( |
| 323 new rtc::RefCountedObject<I420Buffer>(100, 100)); |
| 324 |
| 325 scaled_buffer->CropAndScaleFrom(buf, 50, 0, 100, 100); |
| 326 CheckCrop(scaled_buffer, 0.25, 0.0, 0.5, 1.0); |
| 327 } |
| 328 |
| 329 TEST(TestI420FrameBuffer, CropXNotCenter) { |
| 330 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); |
| 331 |
| 332 // Non-center cropping, no scaling. |
| 333 rtc::scoped_refptr<I420Buffer> scaled_buffer( |
| 334 new rtc::RefCountedObject<I420Buffer>(100, 100)); |
| 335 |
| 336 scaled_buffer->CropAndScaleFrom(buf, 25, 0, 100, 100); |
| 337 CheckCrop(scaled_buffer, 0.125, 0.0, 0.5, 1.0); |
| 338 } |
| 339 |
| 340 TEST(TestI420FrameBuffer, CropYCenter) { |
| 341 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200); |
| 342 |
| 343 // Pure center cropping, no scaling. |
| 344 rtc::scoped_refptr<I420Buffer> scaled_buffer( |
| 345 new rtc::RefCountedObject<I420Buffer>(100, 100)); |
| 346 |
| 347 scaled_buffer->CropAndScaleFrom(buf, 0, 50, 100, 100); |
| 348 CheckCrop(scaled_buffer, 0.0, 0.25, 1.0, 0.5); |
| 349 } |
| 350 |
| 351 TEST(TestI420FrameBuffer, CropYNotCenter) { |
| 352 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200); |
| 353 |
| 354 // Non-center cropping, no scaling. |
| 355 rtc::scoped_refptr<I420Buffer> scaled_buffer( |
| 356 new rtc::RefCountedObject<I420Buffer>(100, 100)); |
| 357 |
| 358 scaled_buffer->CropAndScaleFrom(buf, 0, 25, 100, 100); |
| 359 CheckCrop(scaled_buffer, 0.0, 0.125, 1.0, 0.5); |
| 360 } |
| 361 |
| 362 TEST(TestI420FrameBuffer, CropAndScale16x9) { |
| 363 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(640, 480); |
| 364 |
| 365 // Center crop to 640 x 360 (16/9 aspect), then scale down by 2. |
| 366 rtc::scoped_refptr<I420Buffer> scaled_buffer( |
| 367 new rtc::RefCountedObject<I420Buffer>(320, 180)); |
| 368 |
| 369 scaled_buffer->CropAndScaleFrom(buf); |
| 370 CheckCrop(scaled_buffer, 0.0, 0.125, 1.0, 0.75); |
| 371 } |
| 372 |
244 } // namespace webrtc | 373 } // namespace webrtc |
OLD | NEW |