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