OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/video_util.h" | 5 #include "media/base/video_util.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "media/base/video_frame.h" | 12 #include "media/base/video_frame.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace { | |
16 | |
17 // Helper function used to verify the data in the coded region after copying the | |
18 // visible region and padding the remaining area. | |
19 bool VerifyPlanCopyWithPadding(const uint8_t* src, | |
20 size_t src_stride, | |
21 // Size of visible region. | |
22 const gfx::Size& src_size, | |
23 const uint8_t* dst, | |
24 size_t dst_stride, | |
25 // Coded size of |dst|. | |
26 const gfx::Size& dst_size) { | |
27 if (!src || !dst) | |
28 return false; | |
29 | |
30 const size_t src_width = src_size.width(); | |
31 const size_t src_height = src_size.height(); | |
32 const size_t dst_width = dst_size.width(); | |
33 const size_t dst_height = dst_size.height(); | |
34 if (src_width > dst_width || src_width > src_stride || | |
35 src_height > dst_height || src_size.IsEmpty() || dst_size.IsEmpty()) | |
36 return false; | |
37 | |
38 const uint8_t *src_ptr = src, *dst_ptr = dst; | |
39 for (size_t i = 0; i < src_height; | |
40 ++i, src_ptr += src_stride, dst_ptr += dst_stride) { | |
41 if (memcmp(src_ptr, dst_ptr, src_width)) | |
42 return false; | |
43 for (size_t j = src_width; j < dst_width; ++j) { | |
44 if (src_ptr[src_width - 1] != dst_ptr[j]) | |
45 return false; | |
46 } | |
47 } | |
48 if (src_height < dst_height) { | |
49 src_ptr = dst + (src_height - 1) * dst_stride; | |
50 if (memcmp(src_ptr, dst_ptr, dst_width)) | |
51 return false; | |
52 } | |
53 return true; | |
54 } | |
55 | |
56 bool VerifyCopyWithPadding(const media::VideoFrame& src_frame, | |
57 const media::VideoFrame& dst_frame) { | |
58 if (!src_frame.IsMappable() || !dst_frame.IsMappable() || | |
59 src_frame.visible_rect().size() != dst_frame.visible_rect().size()) | |
60 return false; | |
61 | |
62 if (!VerifyPlanCopyWithPadding( | |
63 src_frame.visible_data(media::VideoFrame::kYPlane), | |
64 src_frame.stride(media::VideoFrame::kYPlane), | |
65 src_frame.visible_rect().size(), | |
66 dst_frame.data(media::VideoFrame::kYPlane), | |
67 dst_frame.stride(media::VideoFrame::kYPlane), dst_frame.coded_size())) | |
68 return false; | |
69 if (!VerifyPlanCopyWithPadding( | |
70 src_frame.visible_data(media::VideoFrame::kUPlane), | |
71 src_frame.stride(media::VideoFrame::kUPlane), | |
72 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420, | |
73 media::VideoFrame::kUPlane, | |
74 src_frame.visible_rect().size()), | |
75 dst_frame.data(media::VideoFrame::kUPlane), | |
76 dst_frame.stride(media::VideoFrame::kUPlane), | |
77 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420, | |
78 media::VideoFrame::kUPlane, | |
79 dst_frame.coded_size()))) | |
80 return false; | |
81 if (!VerifyPlanCopyWithPadding( | |
82 src_frame.visible_data(media::VideoFrame::kVPlane), | |
83 src_frame.stride(media::VideoFrame::kVPlane), | |
84 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420, | |
85 media::VideoFrame::kVPlane, | |
86 src_frame.visible_rect().size()), | |
87 dst_frame.data(media::VideoFrame::kVPlane), | |
88 dst_frame.stride(media::VideoFrame::kVPlane), | |
89 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420, | |
90 media::VideoFrame::kVPlane, | |
91 dst_frame.coded_size()))) | |
92 return false; | |
93 | |
94 return true; | |
95 } | |
96 | |
97 } // namespace | |
98 | |
99 namespace media { | 15 namespace media { |
100 | 16 |
101 class VideoUtilTest : public testing::Test { | 17 class VideoUtilTest : public testing::Test { |
102 public: | 18 public: |
103 VideoUtilTest() | 19 VideoUtilTest() |
104 : height_(0), | 20 : height_(0), |
105 y_stride_(0), | 21 y_stride_(0), |
106 u_stride_(0), | 22 u_stride_(0), |
107 v_stride_(0) { | 23 v_stride_(0) { |
108 } | 24 } |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 (y / 2) * frame->stride(VideoFrame::kVPlane) + (x / 2)], | 355 (y / 2) * frame->stride(VideoFrame::kVPlane) + (x / 2)], |
440 inside ? 0x03 : 0x80); | 356 inside ? 0x03 : 0x80); |
441 } | 357 } |
442 } | 358 } |
443 } | 359 } |
444 } | 360 } |
445 } | 361 } |
446 } | 362 } |
447 } | 363 } |
448 | 364 |
449 TEST_F(VideoUtilTest, I420CopyWithPadding) { | |
450 gfx::Size visible_size(40, 30); | |
451 scoped_refptr<VideoFrame> src_frame(VideoFrame::CreateFrame( | |
452 PIXEL_FORMAT_I420, visible_size, gfx::Rect(visible_size), visible_size, | |
453 base::TimeDelta())); | |
454 // Expect to return false when copying to an empty buffer. | |
455 EXPECT_FALSE(I420CopyWithPadding(*src_frame, nullptr)); | |
456 | |
457 scoped_refptr<VideoFrame> dst_frame(VideoFrame::CreateFrame( | |
458 PIXEL_FORMAT_I420, visible_size, gfx::Rect(visible_size), visible_size, | |
459 base::TimeDelta())); | |
460 EXPECT_TRUE(I420CopyWithPadding(*src_frame, dst_frame.get())); | |
461 EXPECT_TRUE(VerifyCopyWithPadding(*src_frame, *dst_frame)); | |
462 | |
463 gfx::Size coded_size(60, 40); | |
464 dst_frame = VideoFrame::CreateFrame(PIXEL_FORMAT_I420, coded_size, | |
465 gfx::Rect(visible_size), coded_size, | |
466 base::TimeDelta()); | |
467 EXPECT_TRUE(I420CopyWithPadding(*src_frame, dst_frame.get())); | |
468 EXPECT_TRUE(VerifyCopyWithPadding(*src_frame, *dst_frame)); | |
469 | |
470 gfx::Size odd_size(39, 31); | |
471 src_frame = | |
472 VideoFrame::CreateFrame(PIXEL_FORMAT_I420, odd_size, gfx::Rect(odd_size), | |
473 odd_size, base::TimeDelta()); | |
474 dst_frame = VideoFrame::CreateFrame(PIXEL_FORMAT_I420, coded_size, | |
475 gfx::Rect(odd_size), coded_size, | |
476 base::TimeDelta()); | |
477 EXPECT_TRUE(I420CopyWithPadding(*src_frame, dst_frame.get())); | |
478 EXPECT_TRUE(VerifyCopyWithPadding(*src_frame, *dst_frame)); | |
479 } | |
480 | |
481 } // namespace media | 365 } // namespace media |
OLD | NEW |