blob: 72b828c028ed7cfe347693624122864b2409fc40 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2008 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000011#include "webrtc/base/gunit.h"
kjellandera96e2d72016-02-04 23:52:28 -080012#include "webrtc/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14namespace cricket {
15
16TEST(VideoCommonTest, TestCanonicalFourCC) {
17 // Canonical fourccs are not changed.
18 EXPECT_EQ(FOURCC_I420, CanonicalFourCC(FOURCC_I420));
19 // The special FOURCC_ANY value is not changed.
20 EXPECT_EQ(FOURCC_ANY, CanonicalFourCC(FOURCC_ANY));
21 // Aliases are translated to the canonical equivalent.
22 EXPECT_EQ(FOURCC_I420, CanonicalFourCC(FOURCC_IYUV));
23 EXPECT_EQ(FOURCC_I422, CanonicalFourCC(FOURCC_YU16));
24 EXPECT_EQ(FOURCC_I444, CanonicalFourCC(FOURCC_YU24));
25 EXPECT_EQ(FOURCC_YUY2, CanonicalFourCC(FOURCC_YUYV));
26 EXPECT_EQ(FOURCC_YUY2, CanonicalFourCC(FOURCC_YUVS));
27 EXPECT_EQ(FOURCC_UYVY, CanonicalFourCC(FOURCC_HDYC));
28 EXPECT_EQ(FOURCC_UYVY, CanonicalFourCC(FOURCC_2VUY));
29 EXPECT_EQ(FOURCC_MJPG, CanonicalFourCC(FOURCC_JPEG));
30 EXPECT_EQ(FOURCC_MJPG, CanonicalFourCC(FOURCC_DMB1));
31 EXPECT_EQ(FOURCC_BGGR, CanonicalFourCC(FOURCC_BA81));
32 EXPECT_EQ(FOURCC_RAW, CanonicalFourCC(FOURCC_RGB3));
33 EXPECT_EQ(FOURCC_24BG, CanonicalFourCC(FOURCC_BGR3));
34 EXPECT_EQ(FOURCC_BGRA, CanonicalFourCC(FOURCC_CM32));
35 EXPECT_EQ(FOURCC_RAW, CanonicalFourCC(FOURCC_CM24));
36}
37
38// Test conversion between interval and fps
39TEST(VideoCommonTest, TestVideoFormatFps) {
40 EXPECT_EQ(VideoFormat::kMinimumInterval, VideoFormat::FpsToInterval(0));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000041 EXPECT_EQ(rtc::kNumNanosecsPerSec / 20, VideoFormat::FpsToInterval(20));
42 EXPECT_EQ(20, VideoFormat::IntervalToFps(rtc::kNumNanosecsPerSec / 20));
henrike@webrtc.org18e59112014-03-14 17:19:38 +000043 EXPECT_EQ(0, VideoFormat::IntervalToFps(0));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044}
45
46// Test IsSize0x0
47TEST(VideoCommonTest, TestVideoFormatIsSize0x0) {
48 VideoFormat format;
49 EXPECT_TRUE(format.IsSize0x0());
50 format.width = 320;
51 EXPECT_FALSE(format.IsSize0x0());
52}
53
54// Test ToString: print fourcc when it is printable.
55TEST(VideoCommonTest, TestVideoFormatToString) {
56 VideoFormat format;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000057 EXPECT_EQ("0x0x0", format.ToString());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
59 format.fourcc = FOURCC_I420;
60 format.width = 640;
61 format.height = 480;
62 format.interval = VideoFormat::FpsToInterval(20);
63 EXPECT_EQ("I420 640x480x20", format.ToString());
64
65 format.fourcc = FOURCC_ANY;
66 format.width = 640;
67 format.height = 480;
68 format.interval = VideoFormat::FpsToInterval(20);
69 EXPECT_EQ("640x480x20", format.ToString());
70}
71
72// Test comparison.
73TEST(VideoCommonTest, TestVideoFormatCompare) {
74 VideoFormat format(640, 480, VideoFormat::FpsToInterval(20), FOURCC_I420);
75 VideoFormat format2;
76 EXPECT_NE(format, format2);
77
78 // Same pixelrate, different fourcc.
79 format2 = format;
80 format2.fourcc = FOURCC_YUY2;
81 EXPECT_NE(format, format2);
82 EXPECT_FALSE(format.IsPixelRateLess(format2) ||
83 format2.IsPixelRateLess(format2));
84
85 format2 = format;
86 format2.interval /= 2;
87 EXPECT_TRUE(format.IsPixelRateLess(format2));
88
89 format2 = format;
90 format2.width *= 2;
91 EXPECT_TRUE(format.IsPixelRateLess(format2));
92}
93
94TEST(VideoCommonTest, TestComputeScaleWithLowFps) {
95 int scaled_width, scaled_height;
96
97 // Request small enough. Expect no change.
98 ComputeScale(2560, 1600, 5, &scaled_width, &scaled_height);
99 EXPECT_EQ(2560, scaled_width);
100 EXPECT_EQ(1600, scaled_height);
101
102 // Request too many pixels. Expect 1/2 size.
103 ComputeScale(4096, 2560, 5, &scaled_width, &scaled_height);
104 EXPECT_EQ(2048, scaled_width);
105 EXPECT_EQ(1280, scaled_height);
106
107 // Request too many pixels and too wide and tall. Expect 1/4 size.
108 ComputeScale(16000, 10000, 5, &scaled_width, &scaled_height);
109 EXPECT_EQ(2000, scaled_width);
110 EXPECT_EQ(1250, scaled_height);
111
112 // Request too wide. (two 30 inch monitors). Expect 1/2 size.
113 ComputeScale(5120, 1600, 5, &scaled_width, &scaled_height);
114 EXPECT_EQ(2560, scaled_width);
115 EXPECT_EQ(800, scaled_height);
116
117 // Request too wide but not too many pixels. Expect 1/2 size.
118 ComputeScale(8192, 1024, 5, &scaled_width, &scaled_height);
119 EXPECT_EQ(4096, scaled_width);
120 EXPECT_EQ(512, scaled_height);
121
122 // Request too tall. Expect 1/4 size.
123 ComputeScale(1024, 8192, 5, &scaled_width, &scaled_height);
124 EXPECT_EQ(256, scaled_width);
125 EXPECT_EQ(2048, scaled_height);
126}
127
128// Same as TestComputeScale but with 15 fps instead of 5 fps.
129TEST(VideoCommonTest, TestComputeScaleWithHighFps) {
130 int scaled_width, scaled_height;
131
132 // Request small enough but high fps. Expect 1/2 size.
133 ComputeScale(2560, 1600, 15, &scaled_width, &scaled_height);
134 EXPECT_EQ(1280, scaled_width);
135 EXPECT_EQ(800, scaled_height);
136
137 // Request too many pixels. Expect 1/2 size.
138 ComputeScale(4096, 2560, 15, &scaled_width, &scaled_height);
139 EXPECT_EQ(2048, scaled_width);
140 EXPECT_EQ(1280, scaled_height);
141
142 // Request too many pixels and too wide and tall. Expect 1/16 size.
143 ComputeScale(64000, 40000, 15, &scaled_width, &scaled_height);
144 EXPECT_EQ(4000, scaled_width);
145 EXPECT_EQ(2500, scaled_height);
146
147 // Request too wide. (two 30 inch monitors). Expect 1/2 size.
148 ComputeScale(5120, 1600, 15, &scaled_width, &scaled_height);
149 EXPECT_EQ(2560, scaled_width);
150 EXPECT_EQ(800, scaled_height);
151
152 // Request too wide but not too many pixels. Expect 1/2 size.
153 ComputeScale(8192, 1024, 15, &scaled_width, &scaled_height);
154 EXPECT_EQ(4096, scaled_width);
155 EXPECT_EQ(512, scaled_height);
156
157 // Request too tall. Expect 1/4 size.
158 ComputeScale(1024, 8192, 15, &scaled_width, &scaled_height);
159 EXPECT_EQ(256, scaled_width);
160 EXPECT_EQ(2048, scaled_height);
161}
162
163TEST(VideoCommonTest, TestComputeCrop) {
164 int cropped_width, cropped_height;
165
166 // Request 16:9 to 16:9. Expect no cropping.
167 ComputeCrop(1280, 720, // Crop size 16:9
168 640, 360, // Frame is 4:3
169 1, 1, // Normal 1:1 pixels
170 0,
171 &cropped_width, &cropped_height);
172 EXPECT_EQ(640, cropped_width);
173 EXPECT_EQ(360, cropped_height);
174
175 // Request 4:3 to 16:9. Expect vertical.
176 ComputeCrop(640, 360, // Crop size 16:9
177 640, 480, // Frame is 4:3
178 1, 1, // Normal 1:1 pixels
179 0,
180 &cropped_width, &cropped_height);
181 EXPECT_EQ(640, cropped_width);
182 EXPECT_EQ(360, cropped_height);
183
184 // Request 16:9 to 4:3. Expect horizontal crop.
185 ComputeCrop(640, 480, // Crop size 4:3
186 640, 360, // Frame is 16:9
187 1, 1, // Normal 1:1 pixels
188 0,
189 &cropped_width, &cropped_height);
190 EXPECT_EQ(480, cropped_width);
191 EXPECT_EQ(360, cropped_height);
192
193 // Request 16:9 but VGA has 3:8 pixel aspect ratio. Expect no crop.
194 // This occurs on HP4110 on OSX 10.5/10.6/10.7
195 ComputeCrop(640, 360, // Crop size 16:9
196 640, 480, // Frame is 4:3
197 3, 8, // Pixel aspect ratio is tall
198 0,
199 &cropped_width, &cropped_height);
200 EXPECT_EQ(640, cropped_width);
201 EXPECT_EQ(480, cropped_height);
202
203 // Request 16:9 but QVGA has 15:11 pixel aspect ratio. Expect horizontal crop.
204 // This occurs on Logitech B910 on OSX 10.5/10.6/10.7 in Hangouts.
205 ComputeCrop(640, 360, // Crop size 16:9
206 320, 240, // Frame is 4:3
207 15, 11, // Pixel aspect ratio is wide
208 0,
209 &cropped_width, &cropped_height);
210 EXPECT_EQ(312, cropped_width);
211 EXPECT_EQ(240, cropped_height);
212
213 // Request 16:10 but QVGA has 15:11 pixel aspect ratio.
214 // Expect horizontal crop.
215 // This occurs on Logitech B910 on OSX 10.5/10.6/10.7 in gmail.
216 ComputeCrop(640, 400, // Crop size 16:10
217 320, 240, // Frame is 4:3
218 15, 11, // Pixel aspect ratio is wide
219 0,
220 &cropped_width, &cropped_height);
221 EXPECT_EQ(280, cropped_width);
222 EXPECT_EQ(240, cropped_height);
223
224 // Request 16:9 but VGA has 6:5 pixel aspect ratio. Expect vertical crop.
225 // This occurs on Logitech QuickCam Pro C9000 on OSX
226 ComputeCrop(640, 360, // Crop size 16:9
227 640, 480, // Frame is 4:3
228 6, 5, // Pixel aspect ratio is wide
229 0,
230 &cropped_width, &cropped_height);
231 EXPECT_EQ(640, cropped_width);
232 EXPECT_EQ(432, cropped_height);
233
234 // Request 16:10 but HD is 16:9. Expect horizontal crop.
235 // This occurs in settings and local preview with HD experiment.
236 ComputeCrop(1280, 800, // Crop size 16:10
237 1280, 720, // Frame is 4:3
238 1, 1, // Pixel aspect ratio is wide
239 0,
240 &cropped_width, &cropped_height);
241 EXPECT_EQ(1152, cropped_width);
242 EXPECT_EQ(720, cropped_height);
243
244 // Request 16:9 but HD has 3:4 pixel aspect ratio. Expect vertical crop.
245 // This occurs on Logitech B910 on OSX 10.5/10.6.7 but not OSX 10.6.8 or 10.7
246 ComputeCrop(1280, 720, // Crop size 16:9
247 1280, 720, // Frame is 4:3
248 3, 4, // Pixel aspect ratio is wide
249 0,
250 &cropped_width, &cropped_height);
251 EXPECT_EQ(1280, cropped_width);
252 EXPECT_EQ(540, cropped_height);
253
254 // Request 16:9 to 3:4 (portrait). Expect no cropping.
255 ComputeCrop(640, 360, // Crop size 16:9
256 640, 480, // Frame is 3:4 portrait
257 1, 1, // Normal 1:1 pixels
258 90,
259 &cropped_width, &cropped_height);
260 EXPECT_EQ(640, cropped_width);
261 EXPECT_EQ(480, cropped_height);
262
wu@webrtc.org97077a32013-10-25 21:18:33 +0000263 // Request 9:16 from VGA rotated (portrait). Expect crop.
264 ComputeCrop(360, 640, // Crop size 9:16
265 640, 480, // Frame is 3:4 portrait
266 1, 1, // Normal 1:1 pixels
267 90,
268 &cropped_width, &cropped_height);
269 EXPECT_EQ(640, cropped_width);
270 EXPECT_EQ(360, cropped_height);
271
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 // Cropped size 0x0. Expect no cropping.
273 // This is used when adding multiple capturers
274 ComputeCrop(0, 0, // Crop size 0x0
275 1024, 768, // Frame is 3:4 portrait
276 1, 1, // Normal 1:1 pixels
277 0,
278 &cropped_width, &cropped_height);
279 EXPECT_EQ(1024, cropped_width);
280 EXPECT_EQ(768, cropped_height);
281}
282
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000283TEST(VideoCommonTest, TestComputeScaleToSquarePixels) {
284 int scaled_width, scaled_height;
285
286 // Pixel aspect ratio is 4:3. Logical aspect ratio is 16:9. Expect scale
287 // to square pixels with physical aspect ratio of 16:9.
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +0000288 ComputeScaleToSquarePixels(640, 480,
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000289 4, 3, // 4 x 3 pixel aspect ratio
290 &scaled_width, &scaled_height);
291 EXPECT_EQ(640, scaled_width);
292 EXPECT_EQ(360, scaled_height);
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +0000293
294 // Pixel aspect ratio is 3:8. Physical aspect ratio is 4:3. Expect scale
295 // to square pixels with logical aspect ratio of 1:2.
296 // Note that 640x1280 will be scaled down by video adapter to view request
297 // of 640*360 and will end up using 320x640.
298 ComputeScaleToSquarePixels(640, 480,
299 3, 8, // 4 x 3 pixel aspect ratio
300 &scaled_width, &scaled_height);
301 EXPECT_EQ(640, scaled_width);
302 EXPECT_EQ(1280, scaled_height);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000303}
304
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305} // namespace cricket