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++) | |
magjed_webrtc
2016/05/17 12:12:40
nit: Add {}
nisse-webrtc
2016/05/18 11:16:33
Done.
| |
35 for (int y = 0; y < height; y++) { | |
36 buffer->MutableDataY()[x + y * width] = | |
37 128 * (x * height + y * width) / (width * height); | |
38 } | |
39 int chroma_width = (width + 1) / 2; | |
40 int chroma_height = (height + 1) / 2; | |
41 for (int x = 0; x < chroma_width; x++) | |
magjed_webrtc
2016/05/17 12:12:40
nit: Add {}
nisse-webrtc
2016/05/18 11:16:33
Done.
| |
42 for (int y = 0; y < chroma_height; y++) { | |
43 buffer->MutableDataU()[x + y * chroma_width] = | |
44 255 * x / (chroma_width - 1); | |
45 buffer->MutableDataV()[x + y * chroma_width] = | |
46 255 * y / (chroma_height - 1); | |
47 } | |
48 return buffer; | |
49 } | |
50 | |
51 void CheckCrop(webrtc::VideoFrameBuffer* frame, | |
52 double offset_x, | |
53 double offset_y, | |
54 double rel_width, | |
55 double rel_height) { | |
56 int width = frame->width(); | |
57 int height = frame->height(); | |
58 // Check that pixel values in the corners match the gradient used | |
59 // for initialization. | |
60 for (int i = 0; i < 2; i++) | |
61 for (int j = 0; j < 2; j++) { | |
62 // Pixel coordinates of the corner. | |
63 int x = i * (width - 1); | |
64 int y = j * (height - 1); | |
65 // Relative coordinates, range 0.0 - 1.0 correspond to the | |
66 // size of the uncropped input frame. | |
67 double orig_x = offset_x + i * rel_width; | |
68 double orig_y = offset_y + j * rel_height; | |
69 | |
70 EXPECT_NEAR(frame->DataY()[x + y * frame->StrideY()] / 256.0, | |
71 (orig_x + orig_y) / 2, 0.02); | |
72 EXPECT_NEAR(frame->DataU()[x / 2 + (y / 2) * frame->StrideU()] / 256.0, | |
73 orig_x, 0.02); | |
74 EXPECT_NEAR(frame->DataV()[x / 2 + (y / 2) * frame->StrideV()] / 256.0, | |
75 orig_y, 0.02); | |
76 } | |
77 } | |
78 } // namespace | |
23 | 79 |
24 TEST(TestVideoFrame, InitialValues) { | 80 TEST(TestVideoFrame, InitialValues) { |
25 VideoFrame frame; | 81 VideoFrame frame; |
26 EXPECT_TRUE(frame.IsZeroSize()); | 82 EXPECT_TRUE(frame.IsZeroSize()); |
27 EXPECT_EQ(kVideoRotation_0, frame.rotation()); | 83 EXPECT_EQ(kVideoRotation_0, frame.rotation()); |
28 } | 84 } |
29 | 85 |
30 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { | 86 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { |
31 VideoFrame frame; | 87 VideoFrame frame; |
32 VideoFrame frame2; | 88 VideoFrame frame2; |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 TEST(TestI420FrameBuffer, Copy) { | 299 TEST(TestI420FrameBuffer, Copy) { |
244 rtc::scoped_refptr<I420Buffer> buf1( | 300 rtc::scoped_refptr<I420Buffer> buf1( |
245 new rtc::RefCountedObject<I420Buffer>(20, 10)); | 301 new rtc::RefCountedObject<I420Buffer>(20, 10)); |
246 memset(buf1->MutableDataY(), 1, 200); | 302 memset(buf1->MutableDataY(), 1, 200); |
247 memset(buf1->MutableDataU(), 2, 50); | 303 memset(buf1->MutableDataU(), 2, 50); |
248 memset(buf1->MutableDataV(), 3, 50); | 304 memset(buf1->MutableDataV(), 3, 50); |
249 rtc::scoped_refptr<I420Buffer> buf2 = I420Buffer::Copy(buf1); | 305 rtc::scoped_refptr<I420Buffer> buf2 = I420Buffer::Copy(buf1); |
250 EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2)); | 306 EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2)); |
251 } | 307 } |
252 | 308 |
309 TEST(TestI420FrameBuffer, Scale) { | |
310 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); | |
311 | |
312 // Pure scaling, no cropping. | |
313 rtc::scoped_refptr<I420Buffer> scaled = | |
314 I420Buffer::CropAndScale(buf, 0, 0, 200, 100, 150, 75); | |
315 CheckCrop(scaled, 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 = | |
323 I420Buffer::CropAndScale(buf, 50, 0, 100, 100, 100, 100); | |
324 CheckCrop(scaled, 0.25, 0.0, 0.5, 1.0); | |
325 } | |
326 | |
327 TEST(TestI420FrameBuffer, CropXNotCenter) { | |
328 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); | |
329 | |
330 // Non-center cropping, no scaling. | |
331 rtc::scoped_refptr<I420Buffer> scaled = | |
332 I420Buffer::CropAndScale(buf, 25, 0, 100, 100, 100, 100); | |
333 CheckCrop(scaled, 0.125, 0.0, 0.5, 1.0); | |
334 } | |
335 | |
336 TEST(TestI420FrameBuffer, CropYCenter) { | |
337 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200); | |
338 | |
339 // Pure center cropping, no scaling. | |
340 rtc::scoped_refptr<I420Buffer> scaled = | |
341 I420Buffer::CropAndScale(buf, 0, 50, 100, 100, 100, 100); | |
342 CheckCrop(scaled, 0.0, 0.25, 1.0, 0.5); | |
343 } | |
344 | |
345 TEST(TestI420FrameBuffer, CropYNotCenter) { | |
346 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200); | |
347 | |
348 // Non-center cropping, no scaling. | |
349 rtc::scoped_refptr<I420Buffer> scaled = | |
350 I420Buffer::CropAndScale(buf, 0, 25, 100, 100, 100, 100); | |
351 CheckCrop(scaled, 0.0, 0.125, 1.0, 0.5); | |
352 } | |
353 | |
354 TEST(TestI420FrameBuffer, CropAndScale16x9) { | |
355 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(640, 480); | |
356 | |
357 // Center crop to 640 x 360 (16/9 aspect), then scale down by 2. | |
358 rtc::scoped_refptr<I420Buffer> scaled = | |
359 I420Buffer::CropAndScale(buf, 0, 60, 640, 360, 320, 180); | |
360 CheckCrop(scaled, 0.0, 0.125, 1.0, 0.75); | |
361 } | |
362 | |
253 } // namespace webrtc | 363 } // namespace webrtc |
OLD | NEW |