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/base/scoped_ptr.h" | 16 #include "webrtc/base/scoped_ptr.h" |
17 #include "webrtc/test/fake_texture_frame.h" | 17 #include "webrtc/test/fake_texture_frame.h" |
18 #include "webrtc/video_frame.h" | 18 #include "webrtc/video_frame.h" |
19 | 19 |
20 namespace webrtc { | 20 namespace { |
21 | |
22 bool EqualPlane(const uint8_t* data1, | 21 bool EqualPlane(const uint8_t* data1, |
23 const uint8_t* data2, | 22 const uint8_t* data2, |
24 int stride, | 23 int stride, |
25 int width, | 24 int width, |
26 int height); | 25 int height) { |
| 26 for (int y = 0; y < height; ++y) { |
| 27 if (memcmp(data1, data2, width) != 0) |
| 28 return false; |
| 29 data1 += stride; |
| 30 data2 += stride; |
| 31 } |
| 32 return true; |
| 33 } |
| 34 bool FramesEqual(const webrtc::VideoFrame& f1, const webrtc::VideoFrame& f2) { |
| 35 if (f1.width() != f2.width() || f1.height() != f2.height() || |
| 36 f1.stride(webrtc::kYPlane) != f2.stride(webrtc::kYPlane) || |
| 37 f1.stride(webrtc::kUPlane) != f2.stride(webrtc::kUPlane) || |
| 38 f1.stride(webrtc::kVPlane) != f2.stride(webrtc::kVPlane) || |
| 39 f1.timestamp() != f2.timestamp() || |
| 40 f1.ntp_time_ms() != f2.ntp_time_ms() || |
| 41 f1.render_time_ms() != f2.render_time_ms()) { |
| 42 return false; |
| 43 } |
| 44 const int half_width = (f1.width() + 1) / 2; |
| 45 const int half_height = (f1.height() + 1) / 2; |
| 46 return EqualPlane(f1.buffer(webrtc::kYPlane), f2.buffer(webrtc::kYPlane), |
| 47 f1.stride(webrtc::kYPlane), f1.width(), f1.height()) && |
| 48 EqualPlane(f1.buffer(webrtc::kUPlane), f2.buffer(webrtc::kUPlane), |
| 49 f1.stride(webrtc::kUPlane), half_width, half_height) && |
| 50 EqualPlane(f1.buffer(webrtc::kVPlane), f2.buffer(webrtc::kVPlane), |
| 51 f1.stride(webrtc::kVPlane), half_width, half_height); |
| 52 } |
| 53 |
| 54 } |
| 55 |
| 56 namespace webrtc { |
| 57 |
27 int ExpectedSize(int plane_stride, int image_height, PlaneType type); | 58 int ExpectedSize(int plane_stride, int image_height, PlaneType type); |
28 | 59 |
29 TEST(TestVideoFrame, InitialValues) { | 60 TEST(TestVideoFrame, InitialValues) { |
30 VideoFrame frame; | 61 VideoFrame frame; |
31 EXPECT_TRUE(frame.IsZeroSize()); | 62 EXPECT_TRUE(frame.IsZeroSize()); |
32 EXPECT_EQ(kVideoRotation_0, frame.rotation()); | 63 EXPECT_EQ(kVideoRotation_0, frame.rotation()); |
33 } | 64 } |
34 | 65 |
35 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { | 66 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { |
36 VideoFrame frame; | 67 VideoFrame frame; |
37 VideoFrame frame2; | 68 VideoFrame frame2; |
38 frame2.CopyFrame(frame); | 69 frame2.CopyFrame(frame); |
39 } | 70 } |
40 | 71 |
41 TEST(TestVideoFrame, WidthHeightValues) { | 72 TEST(TestVideoFrame, WidthHeightValues) { |
42 VideoFrame frame; | 73 VideoFrame frame; |
43 const int valid_value = 10; | 74 const int valid_value = 10; |
44 EXPECT_EQ(0, frame.CreateEmptyFrame(10, 10, 10, 14, 90)); | 75 frame.CreateEmptyFrame(10, 10, 10, 14, 90); |
45 EXPECT_EQ(valid_value, frame.width()); | 76 EXPECT_EQ(valid_value, frame.width()); |
46 EXPECT_EQ(valid_value, frame.height()); | 77 EXPECT_EQ(valid_value, frame.height()); |
47 frame.set_timestamp(123u); | 78 frame.set_timestamp(123u); |
48 EXPECT_EQ(123u, frame.timestamp()); | 79 EXPECT_EQ(123u, frame.timestamp()); |
49 frame.set_ntp_time_ms(456); | 80 frame.set_ntp_time_ms(456); |
50 EXPECT_EQ(456, frame.ntp_time_ms()); | 81 EXPECT_EQ(456, frame.ntp_time_ms()); |
51 frame.set_render_time_ms(789); | 82 frame.set_render_time_ms(789); |
52 EXPECT_EQ(789, frame.render_time_ms()); | 83 EXPECT_EQ(789, frame.render_time_ms()); |
53 } | 84 } |
54 | 85 |
55 TEST(TestVideoFrame, SizeAllocation) { | 86 TEST(TestVideoFrame, SizeAllocation) { |
56 VideoFrame frame; | 87 VideoFrame frame; |
57 EXPECT_EQ(0, frame. CreateEmptyFrame(10, 10, 12, 14, 220)); | 88 frame. CreateEmptyFrame(10, 10, 12, 14, 220); |
58 int height = frame.height(); | 89 int height = frame.height(); |
59 int stride_y = frame.stride(kYPlane); | 90 int stride_y = frame.stride(kYPlane); |
60 int stride_u = frame.stride(kUPlane); | 91 int stride_u = frame.stride(kUPlane); |
61 int stride_v = frame.stride(kVPlane); | 92 int stride_v = frame.stride(kVPlane); |
62 // Verify that allocated size was computed correctly. | 93 // Verify that allocated size was computed correctly. |
63 EXPECT_EQ(ExpectedSize(stride_y, height, kYPlane), | 94 EXPECT_EQ(ExpectedSize(stride_y, height, kYPlane), |
64 frame.allocated_size(kYPlane)); | 95 frame.allocated_size(kYPlane)); |
65 EXPECT_EQ(ExpectedSize(stride_u, height, kUPlane), | 96 EXPECT_EQ(ExpectedSize(stride_u, height, kUPlane), |
66 frame.allocated_size(kUPlane)); | 97 frame.allocated_size(kUPlane)); |
67 EXPECT_EQ(ExpectedSize(stride_v, height, kVPlane), | 98 EXPECT_EQ(ExpectedSize(stride_v, height, kVPlane), |
68 frame.allocated_size(kVPlane)); | 99 frame.allocated_size(kVPlane)); |
69 } | 100 } |
70 | 101 |
71 TEST(TestVideoFrame, CopyFrame) { | 102 TEST(TestVideoFrame, CopyFrame) { |
72 uint32_t timestamp = 1; | 103 uint32_t timestamp = 1; |
73 int64_t ntp_time_ms = 2; | 104 int64_t ntp_time_ms = 2; |
74 int64_t render_time_ms = 3; | 105 int64_t render_time_ms = 3; |
75 int stride_y = 15; | 106 int stride_y = 15; |
76 int stride_u = 10; | 107 int stride_u = 10; |
77 int stride_v = 10; | 108 int stride_v = 10; |
78 int width = 15; | 109 int width = 15; |
79 int height = 15; | 110 int height = 15; |
80 // Copy frame. | 111 // Copy frame. |
81 VideoFrame small_frame; | 112 VideoFrame small_frame; |
82 EXPECT_EQ(0, small_frame.CreateEmptyFrame(width, height, | 113 small_frame.CreateEmptyFrame(width, height, |
83 stride_y, stride_u, stride_v)); | 114 stride_y, stride_u, stride_v); |
84 small_frame.set_timestamp(timestamp); | 115 small_frame.set_timestamp(timestamp); |
85 small_frame.set_ntp_time_ms(ntp_time_ms); | 116 small_frame.set_ntp_time_ms(ntp_time_ms); |
86 small_frame.set_render_time_ms(render_time_ms); | 117 small_frame.set_render_time_ms(render_time_ms); |
87 const int kSizeY = 400; | 118 const int kSizeY = 400; |
88 const int kSizeU = 100; | 119 const int kSizeU = 100; |
89 const int kSizeV = 100; | 120 const int kSizeV = 100; |
90 const VideoRotation kRotation = kVideoRotation_270; | 121 const VideoRotation kRotation = kVideoRotation_270; |
91 uint8_t buffer_y[kSizeY]; | 122 uint8_t buffer_y[kSizeY]; |
92 uint8_t buffer_u[kSizeU]; | 123 uint8_t buffer_u[kSizeU]; |
93 uint8_t buffer_v[kSizeV]; | 124 uint8_t buffer_v[kSizeV]; |
94 memset(buffer_y, 16, kSizeY); | 125 memset(buffer_y, 16, kSizeY); |
95 memset(buffer_u, 8, kSizeU); | 126 memset(buffer_u, 8, kSizeU); |
96 memset(buffer_v, 4, kSizeV); | 127 memset(buffer_v, 4, kSizeV); |
97 VideoFrame big_frame; | 128 VideoFrame big_frame; |
98 EXPECT_EQ(0, | 129 big_frame.CreateFrame(buffer_y, buffer_u, buffer_v, |
99 big_frame.CreateFrame(buffer_y, buffer_u, buffer_v, | 130 width + 5, height + 5, stride_y + 5, |
100 width + 5, height + 5, stride_y + 5, | 131 stride_u, stride_v, kRotation); |
101 stride_u, stride_v, kRotation)); | |
102 // Frame of smaller dimensions. | 132 // Frame of smaller dimensions. |
103 EXPECT_EQ(0, small_frame.CopyFrame(big_frame)); | 133 small_frame.CopyFrame(big_frame); |
104 EXPECT_TRUE(small_frame.EqualsFrame(big_frame)); | 134 EXPECT_TRUE(FramesEqual(small_frame, big_frame)); |
105 EXPECT_EQ(kRotation, small_frame.rotation()); | 135 EXPECT_EQ(kRotation, small_frame.rotation()); |
106 | 136 |
107 // Frame of larger dimensions. | 137 // Frame of larger dimensions. |
108 EXPECT_EQ(0, small_frame.CreateEmptyFrame(width, height, | 138 small_frame.CreateEmptyFrame(width, height, |
109 stride_y, stride_u, stride_v)); | 139 stride_y, stride_u, stride_v); |
110 memset(small_frame.buffer(kYPlane), 1, small_frame.allocated_size(kYPlane)); | 140 memset(small_frame.buffer(kYPlane), 1, small_frame.allocated_size(kYPlane)); |
111 memset(small_frame.buffer(kUPlane), 2, small_frame.allocated_size(kUPlane)); | 141 memset(small_frame.buffer(kUPlane), 2, small_frame.allocated_size(kUPlane)); |
112 memset(small_frame.buffer(kVPlane), 3, small_frame.allocated_size(kVPlane)); | 142 memset(small_frame.buffer(kVPlane), 3, small_frame.allocated_size(kVPlane)); |
113 EXPECT_EQ(0, big_frame.CopyFrame(small_frame)); | 143 big_frame.CopyFrame(small_frame); |
114 EXPECT_TRUE(small_frame.EqualsFrame(big_frame)); | 144 EXPECT_TRUE(FramesEqual(small_frame, big_frame)); |
115 } | 145 } |
116 | 146 |
117 TEST(TestVideoFrame, ShallowCopy) { | 147 TEST(TestVideoFrame, ShallowCopy) { |
118 uint32_t timestamp = 1; | 148 uint32_t timestamp = 1; |
119 int64_t ntp_time_ms = 2; | 149 int64_t ntp_time_ms = 2; |
120 int64_t render_time_ms = 3; | 150 int64_t render_time_ms = 3; |
121 int stride_y = 15; | 151 int stride_y = 15; |
122 int stride_u = 10; | 152 int stride_u = 10; |
123 int stride_v = 10; | 153 int stride_v = 10; |
124 int width = 15; | 154 int width = 15; |
125 int height = 15; | 155 int height = 15; |
126 | 156 |
127 const int kSizeY = 400; | 157 const int kSizeY = 400; |
128 const int kSizeU = 100; | 158 const int kSizeU = 100; |
129 const int kSizeV = 100; | 159 const int kSizeV = 100; |
130 const VideoRotation kRotation = kVideoRotation_270; | 160 const VideoRotation kRotation = kVideoRotation_270; |
131 uint8_t buffer_y[kSizeY]; | 161 uint8_t buffer_y[kSizeY]; |
132 uint8_t buffer_u[kSizeU]; | 162 uint8_t buffer_u[kSizeU]; |
133 uint8_t buffer_v[kSizeV]; | 163 uint8_t buffer_v[kSizeV]; |
134 memset(buffer_y, 16, kSizeY); | 164 memset(buffer_y, 16, kSizeY); |
135 memset(buffer_u, 8, kSizeU); | 165 memset(buffer_u, 8, kSizeU); |
136 memset(buffer_v, 4, kSizeV); | 166 memset(buffer_v, 4, kSizeV); |
137 VideoFrame frame1; | 167 VideoFrame frame1; |
138 EXPECT_EQ(0, frame1.CreateFrame(buffer_y, buffer_u, buffer_v, width, height, | 168 frame1.CreateFrame(buffer_y, buffer_u, buffer_v, width, height, |
139 stride_y, stride_u, stride_v, kRotation)); | 169 stride_y, stride_u, stride_v, kRotation); |
140 frame1.set_timestamp(timestamp); | 170 frame1.set_timestamp(timestamp); |
141 frame1.set_ntp_time_ms(ntp_time_ms); | 171 frame1.set_ntp_time_ms(ntp_time_ms); |
142 frame1.set_render_time_ms(render_time_ms); | 172 frame1.set_render_time_ms(render_time_ms); |
143 VideoFrame frame2; | 173 VideoFrame frame2; |
144 frame2.ShallowCopy(frame1); | 174 frame2.ShallowCopy(frame1); |
145 | 175 |
146 // To be able to access the buffers, we need const pointers to the frames. | 176 // To be able to access the buffers, we need const pointers to the frames. |
147 const VideoFrame* const_frame1_ptr = &frame1; | 177 const VideoFrame* const_frame1_ptr = &frame1; |
148 const VideoFrame* const_frame2_ptr = &frame2; | 178 const VideoFrame* const_frame2_ptr = &frame2; |
149 | 179 |
(...skipping 15 matching lines...) Expand all Loading... |
165 frame2.set_rotation(kVideoRotation_90); | 195 frame2.set_rotation(kVideoRotation_90); |
166 | 196 |
167 EXPECT_NE(frame2.timestamp(), frame1.timestamp()); | 197 EXPECT_NE(frame2.timestamp(), frame1.timestamp()); |
168 EXPECT_NE(frame2.ntp_time_ms(), frame1.ntp_time_ms()); | 198 EXPECT_NE(frame2.ntp_time_ms(), frame1.ntp_time_ms()); |
169 EXPECT_NE(frame2.render_time_ms(), frame1.render_time_ms()); | 199 EXPECT_NE(frame2.render_time_ms(), frame1.render_time_ms()); |
170 EXPECT_NE(frame2.rotation(), frame1.rotation()); | 200 EXPECT_NE(frame2.rotation(), frame1.rotation()); |
171 } | 201 } |
172 | 202 |
173 TEST(TestVideoFrame, Reset) { | 203 TEST(TestVideoFrame, Reset) { |
174 VideoFrame frame; | 204 VideoFrame frame; |
175 ASSERT_EQ(frame.CreateEmptyFrame(5, 5, 5, 5, 5), 0); | 205 frame.CreateEmptyFrame(5, 5, 5, 5, 5); |
176 frame.set_ntp_time_ms(1); | 206 frame.set_ntp_time_ms(1); |
177 frame.set_timestamp(2); | 207 frame.set_timestamp(2); |
178 frame.set_render_time_ms(3); | 208 frame.set_render_time_ms(3); |
179 ASSERT_TRUE(frame.video_frame_buffer() != NULL); | 209 ASSERT_TRUE(frame.video_frame_buffer() != NULL); |
180 | 210 |
181 frame.Reset(); | 211 frame.Reset(); |
182 EXPECT_EQ(0u, frame.ntp_time_ms()); | 212 EXPECT_EQ(0u, frame.ntp_time_ms()); |
183 EXPECT_EQ(0u, frame.render_time_ms()); | 213 EXPECT_EQ(0u, frame.render_time_ms()); |
184 EXPECT_EQ(0u, frame.timestamp()); | 214 EXPECT_EQ(0u, frame.timestamp()); |
185 EXPECT_TRUE(frame.video_frame_buffer() == NULL); | 215 EXPECT_TRUE(frame.video_frame_buffer() == NULL); |
186 } | 216 } |
187 | 217 |
188 TEST(TestVideoFrame, CopyBuffer) { | 218 TEST(TestVideoFrame, CopyBuffer) { |
189 VideoFrame frame1, frame2; | 219 VideoFrame frame1, frame2; |
190 int width = 15; | 220 int width = 15; |
191 int height = 15; | 221 int height = 15; |
192 int stride_y = 15; | 222 int stride_y = 15; |
193 int stride_uv = 10; | 223 int stride_uv = 10; |
194 const int kSizeY = 225; | 224 const int kSizeY = 225; |
195 const int kSizeUv = 80; | 225 const int kSizeUv = 80; |
196 EXPECT_EQ(0, frame2.CreateEmptyFrame(width, height, | 226 frame2.CreateEmptyFrame(width, height, |
197 stride_y, stride_uv, stride_uv)); | 227 stride_y, stride_uv, stride_uv); |
198 uint8_t buffer_y[kSizeY]; | 228 uint8_t buffer_y[kSizeY]; |
199 uint8_t buffer_u[kSizeUv]; | 229 uint8_t buffer_u[kSizeUv]; |
200 uint8_t buffer_v[kSizeUv]; | 230 uint8_t buffer_v[kSizeUv]; |
201 memset(buffer_y, 16, kSizeY); | 231 memset(buffer_y, 16, kSizeY); |
202 memset(buffer_u, 8, kSizeUv); | 232 memset(buffer_u, 8, kSizeUv); |
203 memset(buffer_v, 4, kSizeUv); | 233 memset(buffer_v, 4, kSizeUv); |
204 frame2.CreateFrame(buffer_y, buffer_u, buffer_v, | 234 frame2.CreateFrame(buffer_y, buffer_u, buffer_v, |
205 width, height, stride_y, stride_uv, stride_uv); | 235 width, height, stride_y, stride_uv, stride_uv, |
| 236 kVideoRotation_0); |
206 // Expect exactly the same pixel data. | 237 // Expect exactly the same pixel data. |
207 EXPECT_TRUE(EqualPlane(buffer_y, frame2.buffer(kYPlane), stride_y, 15, 15)); | 238 EXPECT_TRUE(EqualPlane(buffer_y, frame2.buffer(kYPlane), stride_y, 15, 15)); |
208 EXPECT_TRUE(EqualPlane(buffer_u, frame2.buffer(kUPlane), stride_uv, 8, 8)); | 239 EXPECT_TRUE(EqualPlane(buffer_u, frame2.buffer(kUPlane), stride_uv, 8, 8)); |
209 EXPECT_TRUE(EqualPlane(buffer_v, frame2.buffer(kVPlane), stride_uv, 8, 8)); | 240 EXPECT_TRUE(EqualPlane(buffer_v, frame2.buffer(kVPlane), stride_uv, 8, 8)); |
210 | 241 |
211 // Compare size. | 242 // Compare size. |
212 EXPECT_LE(kSizeY, frame2.allocated_size(kYPlane)); | 243 EXPECT_LE(kSizeY, frame2.allocated_size(kYPlane)); |
213 EXPECT_LE(kSizeUv, frame2.allocated_size(kUPlane)); | 244 EXPECT_LE(kSizeUv, frame2.allocated_size(kUPlane)); |
214 EXPECT_LE(kSizeUv, frame2.allocated_size(kVPlane)); | 245 EXPECT_LE(kSizeUv, frame2.allocated_size(kVPlane)); |
215 } | 246 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 EXPECT_EQ(10, frame.render_time_ms()); | 281 EXPECT_EQ(10, frame.render_time_ms()); |
251 EXPECT_EQ(handle, frame.native_handle()); | 282 EXPECT_EQ(handle, frame.native_handle()); |
252 | 283 |
253 frame.set_timestamp(200); | 284 frame.set_timestamp(200); |
254 EXPECT_EQ(200u, frame.timestamp()); | 285 EXPECT_EQ(200u, frame.timestamp()); |
255 frame.set_render_time_ms(20); | 286 frame.set_render_time_ms(20); |
256 EXPECT_EQ(20, frame.render_time_ms()); | 287 EXPECT_EQ(20, frame.render_time_ms()); |
257 } | 288 } |
258 | 289 |
259 } // namespace webrtc | 290 } // namespace webrtc |
OLD | NEW |