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