summaryrefslogtreecommitdiff
path: root/chromium/third_party/libjingle/source/talk/media/base/videocommon_unittest.cc
blob: e9cd26a1729000db173e42cf23d3225d7995f76d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * libjingle
 * Copyright 2008 Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "talk/base/gunit.h"
#include "talk/media/base/videocommon.h"

namespace cricket {

TEST(VideoCommonTest, TestCanonicalFourCC) {
  // Canonical fourccs are not changed.
  EXPECT_EQ(FOURCC_I420, CanonicalFourCC(FOURCC_I420));
  // The special FOURCC_ANY value is not changed.
  EXPECT_EQ(FOURCC_ANY, CanonicalFourCC(FOURCC_ANY));
  // Aliases are translated to the canonical equivalent.
  EXPECT_EQ(FOURCC_I420, CanonicalFourCC(FOURCC_IYUV));
  EXPECT_EQ(FOURCC_I422, CanonicalFourCC(FOURCC_YU16));
  EXPECT_EQ(FOURCC_I444, CanonicalFourCC(FOURCC_YU24));
  EXPECT_EQ(FOURCC_YUY2, CanonicalFourCC(FOURCC_YUYV));
  EXPECT_EQ(FOURCC_YUY2, CanonicalFourCC(FOURCC_YUVS));
  EXPECT_EQ(FOURCC_UYVY, CanonicalFourCC(FOURCC_HDYC));
  EXPECT_EQ(FOURCC_UYVY, CanonicalFourCC(FOURCC_2VUY));
  EXPECT_EQ(FOURCC_MJPG, CanonicalFourCC(FOURCC_JPEG));
  EXPECT_EQ(FOURCC_MJPG, CanonicalFourCC(FOURCC_DMB1));
  EXPECT_EQ(FOURCC_BGGR, CanonicalFourCC(FOURCC_BA81));
  EXPECT_EQ(FOURCC_RAW, CanonicalFourCC(FOURCC_RGB3));
  EXPECT_EQ(FOURCC_24BG, CanonicalFourCC(FOURCC_BGR3));
  EXPECT_EQ(FOURCC_BGRA, CanonicalFourCC(FOURCC_CM32));
  EXPECT_EQ(FOURCC_RAW, CanonicalFourCC(FOURCC_CM24));
}

// Test conversion between interval and fps
TEST(VideoCommonTest, TestVideoFormatFps) {
  EXPECT_EQ(VideoFormat::kMinimumInterval, VideoFormat::FpsToInterval(0));
  EXPECT_EQ(talk_base::kNumNanosecsPerSec / 20, VideoFormat::FpsToInterval(20));
  EXPECT_EQ(20, VideoFormat::IntervalToFps(talk_base::kNumNanosecsPerSec / 20));
}

// Test IsSize0x0
TEST(VideoCommonTest, TestVideoFormatIsSize0x0) {
  VideoFormat format;
  EXPECT_TRUE(format.IsSize0x0());
  format.width = 320;
  EXPECT_FALSE(format.IsSize0x0());
}

// Test ToString: print fourcc when it is printable.
TEST(VideoCommonTest, TestVideoFormatToString) {
  VideoFormat format;
  EXPECT_EQ("0x0x10000", format.ToString());

  format.fourcc = FOURCC_I420;
  format.width = 640;
  format.height = 480;
  format.interval = VideoFormat::FpsToInterval(20);
  EXPECT_EQ("I420 640x480x20", format.ToString());

  format.fourcc = FOURCC_ANY;
  format.width = 640;
  format.height = 480;
  format.interval = VideoFormat::FpsToInterval(20);
  EXPECT_EQ("640x480x20", format.ToString());
}

// Test comparison.
TEST(VideoCommonTest, TestVideoFormatCompare) {
  VideoFormat format(640, 480, VideoFormat::FpsToInterval(20), FOURCC_I420);
  VideoFormat format2;
  EXPECT_NE(format, format2);

  // Same pixelrate, different fourcc.
  format2 = format;
  format2.fourcc = FOURCC_YUY2;
  EXPECT_NE(format, format2);
  EXPECT_FALSE(format.IsPixelRateLess(format2) ||
               format2.IsPixelRateLess(format2));

  format2 = format;
  format2.interval /= 2;
  EXPECT_TRUE(format.IsPixelRateLess(format2));

  format2 = format;
  format2.width *= 2;
  EXPECT_TRUE(format.IsPixelRateLess(format2));
}

TEST(VideoCommonTest, TestComputeScaleWithLowFps) {
  int scaled_width, scaled_height;

  // Request small enough. Expect no change.
  ComputeScale(2560, 1600, 5, &scaled_width, &scaled_height);
  EXPECT_EQ(2560, scaled_width);
  EXPECT_EQ(1600, scaled_height);

  // Request too many pixels. Expect 1/2 size.
  ComputeScale(4096, 2560, 5, &scaled_width, &scaled_height);
  EXPECT_EQ(2048, scaled_width);
  EXPECT_EQ(1280, scaled_height);

  // Request too many pixels and too wide and tall. Expect 1/4 size.
  ComputeScale(16000, 10000, 5, &scaled_width, &scaled_height);
  EXPECT_EQ(2000, scaled_width);
  EXPECT_EQ(1250, scaled_height);

  // Request too wide. (two 30 inch monitors). Expect 1/2 size.
  ComputeScale(5120, 1600, 5, &scaled_width, &scaled_height);
  EXPECT_EQ(2560, scaled_width);
  EXPECT_EQ(800, scaled_height);

  // Request too wide but not too many pixels. Expect 1/2 size.
  ComputeScale(8192, 1024, 5, &scaled_width, &scaled_height);
  EXPECT_EQ(4096, scaled_width);
  EXPECT_EQ(512, scaled_height);

  // Request too tall. Expect 1/4 size.
  ComputeScale(1024, 8192, 5, &scaled_width, &scaled_height);
  EXPECT_EQ(256, scaled_width);
  EXPECT_EQ(2048, scaled_height);
}

// Same as TestComputeScale but with 15 fps instead of 5 fps.
TEST(VideoCommonTest, TestComputeScaleWithHighFps) {
  int scaled_width, scaled_height;

  // Request small enough but high fps. Expect 1/2 size.
  ComputeScale(2560, 1600, 15, &scaled_width, &scaled_height);
  EXPECT_EQ(1280, scaled_width);
  EXPECT_EQ(800, scaled_height);

  // Request too many pixels. Expect 1/2 size.
  ComputeScale(4096, 2560, 15, &scaled_width, &scaled_height);
  EXPECT_EQ(2048, scaled_width);
  EXPECT_EQ(1280, scaled_height);

  // Request too many pixels and too wide and tall. Expect 1/16 size.
  ComputeScale(64000, 40000, 15, &scaled_width, &scaled_height);
  EXPECT_EQ(4000, scaled_width);
  EXPECT_EQ(2500, scaled_height);

  // Request too wide. (two 30 inch monitors). Expect 1/2 size.
  ComputeScale(5120, 1600, 15, &scaled_width, &scaled_height);
  EXPECT_EQ(2560, scaled_width);
  EXPECT_EQ(800, scaled_height);

  // Request too wide but not too many pixels. Expect 1/2 size.
  ComputeScale(8192, 1024, 15, &scaled_width, &scaled_height);
  EXPECT_EQ(4096, scaled_width);
  EXPECT_EQ(512, scaled_height);

  // Request too tall. Expect 1/4 size.
  ComputeScale(1024, 8192, 15, &scaled_width, &scaled_height);
  EXPECT_EQ(256, scaled_width);
  EXPECT_EQ(2048, scaled_height);
}

TEST(VideoCommonTest, TestComputeCrop) {
  int cropped_width, cropped_height;

  // Request 16:9 to 16:9.  Expect no cropping.
  ComputeCrop(1280, 720,  // Crop size 16:9
              640, 360,  // Frame is 4:3
              1, 1,  // Normal 1:1 pixels
              0,
              &cropped_width, &cropped_height);
  EXPECT_EQ(640, cropped_width);
  EXPECT_EQ(360, cropped_height);

  // Request 4:3 to 16:9.  Expect vertical.
  ComputeCrop(640, 360,  // Crop size 16:9
              640, 480,  // Frame is 4:3
              1, 1,  // Normal 1:1 pixels
              0,
              &cropped_width, &cropped_height);
  EXPECT_EQ(640, cropped_width);
  EXPECT_EQ(360, cropped_height);

  // Request 16:9 to 4:3.  Expect horizontal crop.
  ComputeCrop(640, 480,  // Crop size 4:3
              640, 360,  // Frame is 16:9
              1, 1,  // Normal 1:1 pixels
              0,
              &cropped_width, &cropped_height);
  EXPECT_EQ(480, cropped_width);
  EXPECT_EQ(360, cropped_height);

  // Request 16:9 but VGA has 3:8 pixel aspect ratio. Expect no crop.
  // This occurs on HP4110 on OSX 10.5/10.6/10.7
  ComputeCrop(640, 360,  // Crop size 16:9
              640, 480,  // Frame is 4:3
              3, 8,  // Pixel aspect ratio is tall
              0,
              &cropped_width, &cropped_height);
  EXPECT_EQ(640, cropped_width);
  EXPECT_EQ(480, cropped_height);

  // Request 16:9 but QVGA has 15:11 pixel aspect ratio. Expect horizontal crop.
  // This occurs on Logitech B910 on OSX 10.5/10.6/10.7 in Hangouts.
  ComputeCrop(640, 360,  // Crop size 16:9
              320, 240,  // Frame is 4:3
              15, 11,  // Pixel aspect ratio is wide
              0,
              &cropped_width, &cropped_height);
  EXPECT_EQ(312, cropped_width);
  EXPECT_EQ(240, cropped_height);

  // Request 16:10 but QVGA has 15:11 pixel aspect ratio.
  // Expect horizontal crop.
  // This occurs on Logitech B910 on OSX 10.5/10.6/10.7 in gmail.
  ComputeCrop(640, 400,  // Crop size 16:10
              320, 240,  // Frame is 4:3
              15, 11,  // Pixel aspect ratio is wide
              0,
              &cropped_width, &cropped_height);
  EXPECT_EQ(280, cropped_width);
  EXPECT_EQ(240, cropped_height);

  // Request 16:9 but VGA has 6:5 pixel aspect ratio. Expect vertical crop.
  // This occurs on Logitech QuickCam Pro C9000 on OSX
  ComputeCrop(640, 360,  // Crop size 16:9
              640, 480,  // Frame is 4:3
              6, 5,  // Pixel aspect ratio is wide
              0,
              &cropped_width, &cropped_height);
  EXPECT_EQ(640, cropped_width);
  EXPECT_EQ(432, cropped_height);

  // Request 16:10 but HD is 16:9. Expect horizontal crop.
  // This occurs in settings and local preview with HD experiment.
  ComputeCrop(1280, 800,  // Crop size 16:10
              1280, 720,  // Frame is 4:3
              1, 1,  // Pixel aspect ratio is wide
              0,
              &cropped_width, &cropped_height);
  EXPECT_EQ(1152, cropped_width);
  EXPECT_EQ(720, cropped_height);

  // Request 16:9 but HD has 3:4 pixel aspect ratio. Expect vertical crop.
  // This occurs on Logitech B910 on OSX 10.5/10.6.7 but not OSX 10.6.8 or 10.7
  ComputeCrop(1280, 720,  // Crop size 16:9
              1280, 720,  // Frame is 4:3
              3, 4,  // Pixel aspect ratio is wide
              0,
              &cropped_width, &cropped_height);
  EXPECT_EQ(1280, cropped_width);
  EXPECT_EQ(540, cropped_height);

  // Request 16:9 to 3:4 (portrait).  Expect no cropping.
  ComputeCrop(640, 360,  // Crop size 16:9
              640, 480,  // Frame is 3:4 portrait
              1, 1,  // Normal 1:1 pixels
              90,
              &cropped_width, &cropped_height);
  EXPECT_EQ(640, cropped_width);
  EXPECT_EQ(480, cropped_height);

  // Cropped size 0x0.  Expect no cropping.
  // This is used when adding multiple capturers
  ComputeCrop(0, 0,  // Crop size 0x0
              1024, 768,  // Frame is 3:4 portrait
              1, 1,  // Normal 1:1 pixels
              0,
              &cropped_width, &cropped_height);
  EXPECT_EQ(1024, cropped_width);
  EXPECT_EQ(768, cropped_height);
}

}  // namespace cricket