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