1 /*
2 * libjingle
3 * Copyright 2011 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <string.h>
29
30 #include "talk/media/base/videoframe_unittest.h"
31 #include "talk/media/webrtc/webrtcvideoframe.h"
32 #include "webrtc/test/fake_texture_frame.h"
33
34 namespace {
35
36 class WebRtcVideoTestFrame : public cricket::WebRtcVideoFrame {
37 public:
38 using cricket::WebRtcVideoFrame::SetRotation;
39
CreateEmptyFrame(int w,int h,size_t pixel_width,size_t pixel_height,int64_t time_stamp) const40 virtual VideoFrame* CreateEmptyFrame(int w,
41 int h,
42 size_t pixel_width,
43 size_t pixel_height,
44 int64_t time_stamp) const override {
45 WebRtcVideoTestFrame* frame = new WebRtcVideoTestFrame();
46 frame->InitToBlack(w, h, pixel_width, pixel_height, time_stamp);
47 return frame;
48 }
49 };
50
51 } // namespace
52
53 class WebRtcVideoFrameTest : public VideoFrameTest<cricket::WebRtcVideoFrame> {
54 public:
WebRtcVideoFrameTest()55 WebRtcVideoFrameTest() {
56 }
57
TestInit(int cropped_width,int cropped_height,webrtc::VideoRotation frame_rotation,bool apply_rotation)58 void TestInit(int cropped_width, int cropped_height,
59 webrtc::VideoRotation frame_rotation,
60 bool apply_rotation) {
61 const int frame_width = 1920;
62 const int frame_height = 1080;
63
64 // Build the CapturedFrame.
65 cricket::CapturedFrame captured_frame;
66 captured_frame.fourcc = cricket::FOURCC_I420;
67 captured_frame.pixel_width = 1;
68 captured_frame.pixel_height = 1;
69 captured_frame.time_stamp = 5678;
70 captured_frame.rotation = frame_rotation;
71 captured_frame.width = frame_width;
72 captured_frame.height = frame_height;
73 captured_frame.data_size = (frame_width * frame_height) +
74 ((frame_width + 1) / 2) * ((frame_height + 1) / 2) * 2;
75 rtc::scoped_ptr<uint8_t[]> captured_frame_buffer(
76 new uint8_t[captured_frame.data_size]);
77 // Initialize memory to satisfy DrMemory tests.
78 memset(captured_frame_buffer.get(), 0, captured_frame.data_size);
79 captured_frame.data = captured_frame_buffer.get();
80
81 // Create the new frame from the CapturedFrame.
82 cricket::WebRtcVideoFrame frame;
83 EXPECT_TRUE(
84 frame.Init(&captured_frame, cropped_width, cropped_height,
85 apply_rotation));
86
87 // Verify the new frame.
88 EXPECT_EQ(1u, frame.GetPixelWidth());
89 EXPECT_EQ(1u, frame.GetPixelHeight());
90 EXPECT_EQ(5678, frame.GetTimeStamp());
91 if (apply_rotation)
92 EXPECT_EQ(webrtc::kVideoRotation_0, frame.GetRotation());
93 else
94 EXPECT_EQ(frame_rotation, frame.GetRotation());
95 // If |apply_rotation| and the frame rotation is 90 or 270, width and
96 // height are flipped.
97 if (apply_rotation && (frame_rotation == webrtc::kVideoRotation_90
98 || frame_rotation == webrtc::kVideoRotation_270)) {
99 EXPECT_EQ(static_cast<size_t>(cropped_width), frame.GetHeight());
100 EXPECT_EQ(static_cast<size_t>(cropped_height), frame.GetWidth());
101 } else {
102 EXPECT_EQ(static_cast<size_t>(cropped_width), frame.GetWidth());
103 EXPECT_EQ(static_cast<size_t>(cropped_height), frame.GetHeight());
104 }
105 }
106 };
107
108 #define TEST_WEBRTCVIDEOFRAME(X) TEST_F(WebRtcVideoFrameTest, X) { \
109 VideoFrameTest<cricket::WebRtcVideoFrame>::X(); \
110 }
111
112 TEST_WEBRTCVIDEOFRAME(ConstructI420)
TEST_WEBRTCVIDEOFRAME(ConstructI422)113 TEST_WEBRTCVIDEOFRAME(ConstructI422)
114 TEST_WEBRTCVIDEOFRAME(ConstructYuy2)
115 TEST_WEBRTCVIDEOFRAME(ConstructYuy2Unaligned)
116 TEST_WEBRTCVIDEOFRAME(ConstructYuy2Wide)
117 TEST_WEBRTCVIDEOFRAME(ConstructYV12)
118 TEST_WEBRTCVIDEOFRAME(ConstructUyvy)
119 TEST_WEBRTCVIDEOFRAME(ConstructM420)
120 TEST_WEBRTCVIDEOFRAME(ConstructNV21)
121 TEST_WEBRTCVIDEOFRAME(ConstructNV12)
122 TEST_WEBRTCVIDEOFRAME(ConstructABGR)
123 TEST_WEBRTCVIDEOFRAME(ConstructARGB)
124 TEST_WEBRTCVIDEOFRAME(ConstructARGBWide)
125 TEST_WEBRTCVIDEOFRAME(ConstructBGRA)
126 TEST_WEBRTCVIDEOFRAME(Construct24BG)
127 TEST_WEBRTCVIDEOFRAME(ConstructRaw)
128 TEST_WEBRTCVIDEOFRAME(ConstructRGB565)
129 TEST_WEBRTCVIDEOFRAME(ConstructARGB1555)
130 TEST_WEBRTCVIDEOFRAME(ConstructARGB4444)
131
132 TEST_WEBRTCVIDEOFRAME(ConstructI420Mirror)
133 TEST_WEBRTCVIDEOFRAME(ConstructI420Rotate0)
134 TEST_WEBRTCVIDEOFRAME(ConstructI420Rotate90)
135 TEST_WEBRTCVIDEOFRAME(ConstructI420Rotate180)
136 TEST_WEBRTCVIDEOFRAME(ConstructI420Rotate270)
137 TEST_WEBRTCVIDEOFRAME(ConstructYV12Rotate0)
138 TEST_WEBRTCVIDEOFRAME(ConstructYV12Rotate90)
139 TEST_WEBRTCVIDEOFRAME(ConstructYV12Rotate180)
140 TEST_WEBRTCVIDEOFRAME(ConstructYV12Rotate270)
141 TEST_WEBRTCVIDEOFRAME(ConstructNV12Rotate0)
142 TEST_WEBRTCVIDEOFRAME(ConstructNV12Rotate90)
143 TEST_WEBRTCVIDEOFRAME(ConstructNV12Rotate180)
144 TEST_WEBRTCVIDEOFRAME(ConstructNV12Rotate270)
145 TEST_WEBRTCVIDEOFRAME(ConstructNV21Rotate0)
146 TEST_WEBRTCVIDEOFRAME(ConstructNV21Rotate90)
147 TEST_WEBRTCVIDEOFRAME(ConstructNV21Rotate180)
148 TEST_WEBRTCVIDEOFRAME(ConstructNV21Rotate270)
149 TEST_WEBRTCVIDEOFRAME(ConstructUYVYRotate0)
150 TEST_WEBRTCVIDEOFRAME(ConstructUYVYRotate90)
151 TEST_WEBRTCVIDEOFRAME(ConstructUYVYRotate180)
152 TEST_WEBRTCVIDEOFRAME(ConstructUYVYRotate270)
153 TEST_WEBRTCVIDEOFRAME(ConstructYUY2Rotate0)
154 TEST_WEBRTCVIDEOFRAME(ConstructYUY2Rotate90)
155 TEST_WEBRTCVIDEOFRAME(ConstructYUY2Rotate180)
156 TEST_WEBRTCVIDEOFRAME(ConstructYUY2Rotate270)
157 TEST_WEBRTCVIDEOFRAME(ConstructI4201Pixel)
158 TEST_WEBRTCVIDEOFRAME(ConstructI4205Pixel)
159 // TODO(juberti): WebRtcVideoFrame does not support horizontal crop.
160 // Re-evaluate once it supports 3 independent planes, since we might want to
161 // just Init normally and then crop by adjusting pointers.
162 // TEST_WEBRTCVIDEOFRAME(ConstructI420CropHorizontal)
163 TEST_WEBRTCVIDEOFRAME(ConstructI420CropVertical)
164 // TODO(juberti): WebRtcVideoFrame is not currently refcounted.
165 // TEST_WEBRTCVIDEOFRAME(ConstructCopy)
166 // TEST_WEBRTCVIDEOFRAME(ConstructCopyIsRef)
167 TEST_WEBRTCVIDEOFRAME(ConstructBlack)
168 // TODO(fbarchard): Implement Jpeg
169 // TEST_WEBRTCVIDEOFRAME(ConstructMjpgI420)
170 TEST_WEBRTCVIDEOFRAME(ConstructMjpgI422)
171 // TEST_WEBRTCVIDEOFRAME(ConstructMjpgI444)
172 // TEST_WEBRTCVIDEOFRAME(ConstructMjpgI411)
173 // TEST_WEBRTCVIDEOFRAME(ConstructMjpgI400)
174 // TEST_WEBRTCVIDEOFRAME(ValidateMjpgI420)
175 // TEST_WEBRTCVIDEOFRAME(ValidateMjpgI422)
176 // TEST_WEBRTCVIDEOFRAME(ValidateMjpgI444)
177 // TEST_WEBRTCVIDEOFRAME(ValidateMjpgI411)
178 // TEST_WEBRTCVIDEOFRAME(ValidateMjpgI400)
179 TEST_WEBRTCVIDEOFRAME(ValidateI420)
180 TEST_WEBRTCVIDEOFRAME(ValidateI420SmallSize)
181 TEST_WEBRTCVIDEOFRAME(ValidateI420LargeSize)
182 TEST_WEBRTCVIDEOFRAME(ValidateI420HugeSize)
183 // TEST_WEBRTCVIDEOFRAME(ValidateMjpgI420InvalidSize)
184 // TEST_WEBRTCVIDEOFRAME(ValidateI420InvalidSize)
185
186 // TODO(fbarchard): WebRtcVideoFrame does not support odd sizes.
187 // Re-evaluate once WebRTC switches to libyuv
188 // TEST_WEBRTCVIDEOFRAME(ConstructYuy2AllSizes)
189 // TEST_WEBRTCVIDEOFRAME(ConstructARGBAllSizes)
190 TEST_WEBRTCVIDEOFRAME(ResetAndApplyRotation)
191 TEST_WEBRTCVIDEOFRAME(ResetAndDontApplyRotation)
192 TEST_WEBRTCVIDEOFRAME(ConvertToABGRBuffer)
193 TEST_WEBRTCVIDEOFRAME(ConvertToABGRBufferStride)
194 TEST_WEBRTCVIDEOFRAME(ConvertToABGRBufferInverted)
195 TEST_WEBRTCVIDEOFRAME(ConvertToARGB1555Buffer)
196 TEST_WEBRTCVIDEOFRAME(ConvertToARGB1555BufferStride)
197 TEST_WEBRTCVIDEOFRAME(ConvertToARGB1555BufferInverted)
198 TEST_WEBRTCVIDEOFRAME(ConvertToARGB4444Buffer)
199 TEST_WEBRTCVIDEOFRAME(ConvertToARGB4444BufferStride)
200 TEST_WEBRTCVIDEOFRAME(ConvertToARGB4444BufferInverted)
201 TEST_WEBRTCVIDEOFRAME(ConvertToARGBBuffer)
202 TEST_WEBRTCVIDEOFRAME(ConvertToARGBBufferStride)
203 TEST_WEBRTCVIDEOFRAME(ConvertToARGBBufferInverted)
204 TEST_WEBRTCVIDEOFRAME(ConvertToBGRABuffer)
205 TEST_WEBRTCVIDEOFRAME(ConvertToBGRABufferStride)
206 TEST_WEBRTCVIDEOFRAME(ConvertToBGRABufferInverted)
207 TEST_WEBRTCVIDEOFRAME(ConvertToRAWBuffer)
208 TEST_WEBRTCVIDEOFRAME(ConvertToRAWBufferStride)
209 TEST_WEBRTCVIDEOFRAME(ConvertToRAWBufferInverted)
210 TEST_WEBRTCVIDEOFRAME(ConvertToRGB24Buffer)
211 TEST_WEBRTCVIDEOFRAME(ConvertToRGB24BufferStride)
212 TEST_WEBRTCVIDEOFRAME(ConvertToRGB24BufferInverted)
213 TEST_WEBRTCVIDEOFRAME(ConvertToRGB565Buffer)
214 TEST_WEBRTCVIDEOFRAME(ConvertToRGB565BufferStride)
215 TEST_WEBRTCVIDEOFRAME(ConvertToRGB565BufferInverted)
216 TEST_WEBRTCVIDEOFRAME(ConvertToI400Buffer)
217 TEST_WEBRTCVIDEOFRAME(ConvertToI400BufferStride)
218 TEST_WEBRTCVIDEOFRAME(ConvertToI400BufferInverted)
219 TEST_WEBRTCVIDEOFRAME(ConvertToYUY2Buffer)
220 TEST_WEBRTCVIDEOFRAME(ConvertToYUY2BufferStride)
221 TEST_WEBRTCVIDEOFRAME(ConvertToYUY2BufferInverted)
222 TEST_WEBRTCVIDEOFRAME(ConvertToUYVYBuffer)
223 TEST_WEBRTCVIDEOFRAME(ConvertToUYVYBufferStride)
224 TEST_WEBRTCVIDEOFRAME(ConvertToUYVYBufferInverted)
225 TEST_WEBRTCVIDEOFRAME(ConvertFromABGRBuffer)
226 TEST_WEBRTCVIDEOFRAME(ConvertFromABGRBufferStride)
227 TEST_WEBRTCVIDEOFRAME(ConvertFromABGRBufferInverted)
228 TEST_WEBRTCVIDEOFRAME(ConvertFromARGB1555Buffer)
229 TEST_WEBRTCVIDEOFRAME(ConvertFromARGB1555BufferStride)
230 TEST_WEBRTCVIDEOFRAME(ConvertFromARGB1555BufferInverted)
231 TEST_WEBRTCVIDEOFRAME(ConvertFromARGB4444Buffer)
232 TEST_WEBRTCVIDEOFRAME(ConvertFromARGB4444BufferStride)
233 TEST_WEBRTCVIDEOFRAME(ConvertFromARGB4444BufferInverted)
234 TEST_WEBRTCVIDEOFRAME(ConvertFromARGBBuffer)
235 TEST_WEBRTCVIDEOFRAME(ConvertFromARGBBufferStride)
236 TEST_WEBRTCVIDEOFRAME(ConvertFromARGBBufferInverted)
237 TEST_WEBRTCVIDEOFRAME(ConvertFromBGRABuffer)
238 TEST_WEBRTCVIDEOFRAME(ConvertFromBGRABufferStride)
239 TEST_WEBRTCVIDEOFRAME(ConvertFromBGRABufferInverted)
240 TEST_WEBRTCVIDEOFRAME(ConvertFromRAWBuffer)
241 TEST_WEBRTCVIDEOFRAME(ConvertFromRAWBufferStride)
242 TEST_WEBRTCVIDEOFRAME(ConvertFromRAWBufferInverted)
243 TEST_WEBRTCVIDEOFRAME(ConvertFromRGB24Buffer)
244 TEST_WEBRTCVIDEOFRAME(ConvertFromRGB24BufferStride)
245 TEST_WEBRTCVIDEOFRAME(ConvertFromRGB24BufferInverted)
246 TEST_WEBRTCVIDEOFRAME(ConvertFromRGB565Buffer)
247 TEST_WEBRTCVIDEOFRAME(ConvertFromRGB565BufferStride)
248 TEST_WEBRTCVIDEOFRAME(ConvertFromRGB565BufferInverted)
249 TEST_WEBRTCVIDEOFRAME(ConvertFromI400Buffer)
250 TEST_WEBRTCVIDEOFRAME(ConvertFromI400BufferStride)
251 TEST_WEBRTCVIDEOFRAME(ConvertFromI400BufferInverted)
252 TEST_WEBRTCVIDEOFRAME(ConvertFromYUY2Buffer)
253 TEST_WEBRTCVIDEOFRAME(ConvertFromYUY2BufferStride)
254 TEST_WEBRTCVIDEOFRAME(ConvertFromYUY2BufferInverted)
255 TEST_WEBRTCVIDEOFRAME(ConvertFromUYVYBuffer)
256 TEST_WEBRTCVIDEOFRAME(ConvertFromUYVYBufferStride)
257 TEST_WEBRTCVIDEOFRAME(ConvertFromUYVYBufferInverted)
258 // TEST_WEBRTCVIDEOFRAME(ConvertToI422Buffer)
259 TEST_WEBRTCVIDEOFRAME(CopyToBuffer)
260 TEST_WEBRTCVIDEOFRAME(CopyToFrame)
261 TEST_WEBRTCVIDEOFRAME(Write)
262 TEST_WEBRTCVIDEOFRAME(CopyToBuffer1Pixel)
263 // TEST_WEBRTCVIDEOFRAME(ConstructARGBBlackWhitePixel)
264
265 TEST_WEBRTCVIDEOFRAME(StretchToFrame)
266 TEST_WEBRTCVIDEOFRAME(Copy)
267 TEST_WEBRTCVIDEOFRAME(CopyIsRef)
268 TEST_WEBRTCVIDEOFRAME(MakeExclusive)
269
270 // These functions test implementation-specific details.
271 // Tests the Init function with different cropped size.
272 TEST_F(WebRtcVideoFrameTest, InitEvenSize) {
273 TestInit(640, 360, webrtc::kVideoRotation_0, true);
274 }
275
TEST_F(WebRtcVideoFrameTest,InitOddWidth)276 TEST_F(WebRtcVideoFrameTest, InitOddWidth) {
277 TestInit(601, 480, webrtc::kVideoRotation_0, true);
278 }
279
TEST_F(WebRtcVideoFrameTest,InitOddHeight)280 TEST_F(WebRtcVideoFrameTest, InitOddHeight) {
281 TestInit(360, 765, webrtc::kVideoRotation_0, true);
282 }
283
TEST_F(WebRtcVideoFrameTest,InitOddWidthHeight)284 TEST_F(WebRtcVideoFrameTest, InitOddWidthHeight) {
285 TestInit(355, 1021, webrtc::kVideoRotation_0, true);
286 }
287
TEST_F(WebRtcVideoFrameTest,InitRotated90ApplyRotation)288 TEST_F(WebRtcVideoFrameTest, InitRotated90ApplyRotation) {
289 TestInit(640, 360, webrtc::kVideoRotation_90, true);
290 }
291
TEST_F(WebRtcVideoFrameTest,InitRotated90DontApplyRotation)292 TEST_F(WebRtcVideoFrameTest, InitRotated90DontApplyRotation) {
293 TestInit(640, 360, webrtc::kVideoRotation_90, false);
294 }
295
TEST_F(WebRtcVideoFrameTest,TextureInitialValues)296 TEST_F(WebRtcVideoFrameTest, TextureInitialValues) {
297 webrtc::test::FakeNativeHandle* dummy_handle =
298 new webrtc::test::FakeNativeHandle();
299 webrtc::NativeHandleBuffer* buffer =
300 new rtc::RefCountedObject<webrtc::test::FakeNativeHandleBuffer>(
301 dummy_handle, 640, 480);
302 cricket::WebRtcVideoFrame frame(buffer, 200, webrtc::kVideoRotation_0);
303 EXPECT_EQ(dummy_handle, frame.GetNativeHandle());
304 EXPECT_EQ(640u, frame.GetWidth());
305 EXPECT_EQ(480u, frame.GetHeight());
306 EXPECT_EQ(200, frame.GetTimeStamp());
307 frame.SetTimeStamp(400);
308 EXPECT_EQ(400, frame.GetTimeStamp());
309 }
310
TEST_F(WebRtcVideoFrameTest,CopyTextureFrame)311 TEST_F(WebRtcVideoFrameTest, CopyTextureFrame) {
312 webrtc::test::FakeNativeHandle* dummy_handle =
313 new webrtc::test::FakeNativeHandle();
314 webrtc::NativeHandleBuffer* buffer =
315 new rtc::RefCountedObject<webrtc::test::FakeNativeHandleBuffer>(
316 dummy_handle, 640, 480);
317 cricket::WebRtcVideoFrame frame1(buffer, 200, webrtc::kVideoRotation_0);
318 cricket::VideoFrame* frame2 = frame1.Copy();
319 EXPECT_EQ(frame1.GetNativeHandle(), frame2->GetNativeHandle());
320 EXPECT_EQ(frame1.GetWidth(), frame2->GetWidth());
321 EXPECT_EQ(frame1.GetHeight(), frame2->GetHeight());
322 EXPECT_EQ(frame1.GetTimeStamp(), frame2->GetTimeStamp());
323 delete frame2;
324 }
325
TEST_F(WebRtcVideoFrameTest,ApplyRotationToFrame)326 TEST_F(WebRtcVideoFrameTest, ApplyRotationToFrame) {
327 WebRtcVideoTestFrame applied0;
328 EXPECT_TRUE(IsNull(applied0));
329 rtc::scoped_ptr<rtc::MemoryStream> ms(CreateYuvSample(kWidth, kHeight, 12));
330 EXPECT_TRUE(
331 LoadFrame(ms.get(), cricket::FOURCC_I420, kWidth, kHeight, &applied0));
332
333 // Claim that this frame needs to be rotated for 90 degree.
334 applied0.SetRotation(webrtc::kVideoRotation_90);
335
336 // Apply rotation on frame 1. Output should be different from frame 1.
337 WebRtcVideoTestFrame* applied90 = const_cast<WebRtcVideoTestFrame*>(
338 static_cast<const WebRtcVideoTestFrame*>(
339 applied0.GetCopyWithRotationApplied()));
340 EXPECT_TRUE(applied90);
341 EXPECT_EQ(applied90->GetVideoRotation(), webrtc::kVideoRotation_0);
342 EXPECT_FALSE(IsEqual(applied0, *applied90, 0));
343
344 // Claim the frame 2 needs to be rotated for another 270 degree. The output
345 // from frame 2 rotation should be the same as frame 1.
346 applied90->SetRotation(webrtc::kVideoRotation_270);
347 const cricket::VideoFrame* applied360 =
348 applied90->GetCopyWithRotationApplied();
349 EXPECT_TRUE(applied360);
350 EXPECT_EQ(applied360->GetVideoRotation(), webrtc::kVideoRotation_0);
351 EXPECT_TRUE(IsEqual(applied0, *applied360, 0));
352 }
353