1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include "src/video_parser.h"
9
10 #include "gtest/gtest.h"
11
12 #include "test_utils/element_parser_test.h"
13 #include "webm/id.h"
14
15 using webm::AspectRatioType;
16 using webm::Colour;
17 using webm::DisplayUnit;
18 using webm::ElementParserTest;
19 using webm::FlagInterlaced;
20 using webm::Id;
21 using webm::Projection;
22 using webm::ProjectionType;
23 using webm::StereoMode;
24 using webm::Video;
25 using webm::VideoParser;
26
27 namespace {
28
29 class VideoParserTest : public ElementParserTest<VideoParser, Id::kVideo> {};
30
TEST_F(VideoParserTest,DefaultParse)31 TEST_F(VideoParserTest, DefaultParse) {
32 ParseAndVerify();
33
34 const Video video = parser_.value();
35
36 EXPECT_FALSE(video.interlaced.is_present());
37 EXPECT_EQ(FlagInterlaced::kUnspecified, video.interlaced.value());
38
39 EXPECT_FALSE(video.stereo_mode.is_present());
40 EXPECT_EQ(StereoMode::kMono, video.stereo_mode.value());
41
42 EXPECT_FALSE(video.alpha_mode.is_present());
43 EXPECT_EQ(static_cast<std::uint64_t>(0), video.alpha_mode.value());
44
45 EXPECT_FALSE(video.pixel_width.is_present());
46 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_width.value());
47
48 EXPECT_FALSE(video.pixel_height.is_present());
49 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_height.value());
50
51 EXPECT_FALSE(video.pixel_crop_bottom.is_present());
52 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_bottom.value());
53
54 EXPECT_FALSE(video.pixel_crop_top.is_present());
55 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_top.value());
56
57 EXPECT_FALSE(video.pixel_crop_left.is_present());
58 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_left.value());
59
60 EXPECT_FALSE(video.pixel_crop_right.is_present());
61 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_right.value());
62
63 EXPECT_FALSE(video.display_width.is_present());
64 EXPECT_EQ(video.pixel_width.value(), video.display_width.value());
65
66 EXPECT_FALSE(video.display_height.is_present());
67 EXPECT_EQ(video.pixel_height.value(), video.display_height.value());
68
69 EXPECT_FALSE(video.display_unit.is_present());
70 EXPECT_EQ(DisplayUnit::kPixels, video.display_unit.value());
71
72 EXPECT_FALSE(video.aspect_ratio_type.is_present());
73 EXPECT_EQ(AspectRatioType::kFreeResizing, video.aspect_ratio_type.value());
74
75 EXPECT_FALSE(video.frame_rate.is_present());
76 EXPECT_EQ(static_cast<std::uint64_t>(0), video.frame_rate.value());
77
78 EXPECT_FALSE(video.colour.is_present());
79 EXPECT_EQ(Colour{}, video.colour.value());
80
81 EXPECT_FALSE(video.projection.is_present());
82 EXPECT_EQ(Projection{}, video.projection.value());
83 }
84
TEST_F(VideoParserTest,DefaultValues)85 TEST_F(VideoParserTest, DefaultValues) {
86 SetReaderData({
87 0x9A, // ID = 0x9A (FlagInterlaced).
88 0x20, 0x00, 0x00, // Size = 0.
89
90 0x53, 0xB8, // ID = 0x53B8 (StereoMode).
91 0x20, 0x00, 0x00, // Size = 0.
92
93 0x53, 0xC0, // ID = 0x53C0 (AlphaMode).
94 0x20, 0x00, 0x00, // Size = 0.
95
96 0xB0, // ID = 0xB0 (PixelWidth).
97 0x20, 0x00, 0x00, // Size = 0.
98
99 0xBA, // ID = 0xBA (PixelHeight).
100 0x20, 0x00, 0x00, // Size = 0.
101
102 0x54, 0xAA, // ID = 0x54AA (PixelCropBottom).
103 0x20, 0x00, 0x00, // Size = 0.
104
105 0x54, 0xBB, // ID = 0x54BB (PixelCropTop).
106 0x20, 0x00, 0x00, // Size = 0.
107
108 0x54, 0xCC, // ID = 0x54CC (PixelCropLeft).
109 0x20, 0x00, 0x00, // Size = 0.
110
111 0x54, 0xDD, // ID = 0x54DD (PixelCropRight).
112 0x20, 0x00, 0x00, // Size = 0.
113
114 0x54, 0xB0, // ID = 0x54B0 (DisplayWidth).
115 0x20, 0x00, 0x00, // Size = 0.
116
117 0x54, 0xBA, // ID = 0x54BA (DisplayHeight).
118 0x20, 0x00, 0x00, // Size = 0.
119
120 0x54, 0xB2, // ID = 0x54B2 (DisplayUnit).
121 0x20, 0x00, 0x00, // Size = 0.
122
123 0x54, 0xB3, // ID = 0x54B3 (AspectRatioType).
124 0x20, 0x00, 0x00, // Size = 0.
125
126 0x23, 0x83, 0xE3, // ID = 0x2383E3 (FrameRate).
127 0x80, // Size = 0.
128
129 0x55, 0xB0, // ID = 0x55B0 (Colour).
130 0x20, 0x00, 0x00, // Size = 0.
131
132 0x76, 0x70, // ID = 0x7670 (Projection).
133 0x20, 0x00, 0x00, // Size = 0.
134 });
135
136 ParseAndVerify();
137
138 const Video video = parser_.value();
139
140 EXPECT_TRUE(video.interlaced.is_present());
141 EXPECT_EQ(FlagInterlaced::kUnspecified, video.interlaced.value());
142
143 EXPECT_TRUE(video.stereo_mode.is_present());
144 EXPECT_EQ(StereoMode::kMono, video.stereo_mode.value());
145
146 EXPECT_TRUE(video.alpha_mode.is_present());
147 EXPECT_EQ(static_cast<std::uint64_t>(0), video.alpha_mode.value());
148
149 EXPECT_TRUE(video.pixel_width.is_present());
150 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_width.value());
151
152 EXPECT_TRUE(video.pixel_height.is_present());
153 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_height.value());
154
155 EXPECT_TRUE(video.pixel_crop_bottom.is_present());
156 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_bottom.value());
157
158 EXPECT_TRUE(video.pixel_crop_top.is_present());
159 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_top.value());
160
161 EXPECT_TRUE(video.pixel_crop_left.is_present());
162 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_left.value());
163
164 EXPECT_TRUE(video.pixel_crop_right.is_present());
165 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_right.value());
166
167 EXPECT_TRUE(video.display_width.is_present());
168 EXPECT_EQ(video.pixel_width.value(), video.display_width.value());
169
170 EXPECT_TRUE(video.display_height.is_present());
171 EXPECT_EQ(video.pixel_height.value(), video.display_height.value());
172
173 EXPECT_TRUE(video.display_unit.is_present());
174 EXPECT_EQ(DisplayUnit::kPixels, video.display_unit.value());
175
176 EXPECT_TRUE(video.aspect_ratio_type.is_present());
177 EXPECT_EQ(AspectRatioType::kFreeResizing, video.aspect_ratio_type.value());
178
179 EXPECT_TRUE(video.frame_rate.is_present());
180 EXPECT_EQ(0, video.frame_rate.value());
181
182 EXPECT_TRUE(video.colour.is_present());
183 EXPECT_EQ(Colour{}, video.colour.value());
184
185 EXPECT_TRUE(video.projection.is_present());
186 EXPECT_EQ(Projection{}, video.projection.value());
187 }
188
TEST_F(VideoParserTest,CustomValues)189 TEST_F(VideoParserTest, CustomValues) {
190 SetReaderData({
191 0x9A, // ID = 0x9A (FlagInterlaced).
192 0x10, 0x00, 0x00, 0x01, // Size = 1.
193 0x02, // Body (value = progressive).
194
195 0x53, 0xB8, // ID = 0x53B8 (StereoMode).
196 0x10, 0x00, 0x00, 0x01, // Size = 1.
197 0x02, // Body (value = top-bottom (right eye first)).
198
199 0x53, 0xC0, // ID = 0x53C0 (AlphaMode).
200 0x10, 0x00, 0x00, 0x01, // Size = 1.
201 0x03, // Body (value = 3).
202
203 0xB0, // ID = 0xB0 (PixelWidth).
204 0x10, 0x00, 0x00, 0x01, // Size = 1.
205 0x04, // Body (value = 4).
206
207 0xBA, // ID = 0xBA (PixelHeight).
208 0x10, 0x00, 0x00, 0x01, // Size = 1.
209 0x05, // Body (value = 5).
210
211 0x54, 0xAA, // ID = 0x54AA (PixelCropBottom).
212 0x10, 0x00, 0x00, 0x01, // Size = 1.
213 0x06, // Body (value = 6).
214
215 0x54, 0xBB, // ID = 0x54BB (PixelCropTop).
216 0x10, 0x00, 0x00, 0x01, // Size = 1.
217 0x07, // Body (value = 7).
218
219 0x54, 0xCC, // ID = 0x54CC (PixelCropLeft).
220 0x10, 0x00, 0x00, 0x01, // Size = 1.
221 0x08, // Body (value = 8).
222
223 0x54, 0xDD, // ID = 0x54DD (PixelCropRight).
224 0x10, 0x00, 0x00, 0x01, // Size = 1.
225 0x09, // Body (value = 9).
226
227 0x54, 0xB0, // ID = 0x54B0 (DisplayWidth).
228 0x10, 0x00, 0x00, 0x01, // Size = 1.
229 0x0A, // Body (value = 10).
230
231 0x54, 0xBA, // ID = 0x54BA (DisplayHeight).
232 0x10, 0x00, 0x00, 0x01, // Size = 1.
233 0x0B, // Body (value = 11).
234
235 0x54, 0xB2, // ID = 0x54B2 (DisplayUnit).
236 0x10, 0x00, 0x00, 0x01, // Size = 1.
237 0x02, // Body (value = inches).
238
239 0x54, 0xB3, // ID = 0x54B3 (AspectRatioType).
240 0x10, 0x00, 0x00, 0x01, // Size = 1.
241 0x01, // Body (value = keep aspect ratio).
242
243 0x23, 0x83, 0xE3, // ID = 0x2383E3 (FrameRate).
244 0x84, // Size = 4.
245 0x40, 0x0F, 0x1B, 0xBD, // Body (value = 2.2360680103302001953125f).
246
247 0x55, 0xB0, // ID = 0x55B0 (Colour).
248 0x10, 0x00, 0x00, 0x07, // Size = 7.
249
250 0x55, 0xB2, // ID = 0x55B2 (BitsPerChannel).
251 0x10, 0x00, 0x00, 0x01, // Size = 1.
252 0x01, // Body (value = 1).
253
254 0x76, 0x70, // ID = 0x7670 (Projection).
255 0x10, 0x00, 0x00, 0x07, // Size = 7.
256
257 0x76, 0x71, // ID = 0x7671 (ProjectionType).
258 0x10, 0x00, 0x00, 0x01, // Size = 1.
259 0x02, // Body (value = cube map).
260 });
261
262 ParseAndVerify();
263
264 const Video video = parser_.value();
265
266 EXPECT_TRUE(video.interlaced.is_present());
267 EXPECT_EQ(FlagInterlaced::kProgressive, video.interlaced.value());
268
269 EXPECT_TRUE(video.stereo_mode.is_present());
270 EXPECT_EQ(StereoMode::kTopBottomRightFirst, video.stereo_mode.value());
271
272 EXPECT_TRUE(video.alpha_mode.is_present());
273 EXPECT_EQ(static_cast<std::uint64_t>(3), video.alpha_mode.value());
274
275 EXPECT_TRUE(video.pixel_width.is_present());
276 EXPECT_EQ(static_cast<std::uint64_t>(4), video.pixel_width.value());
277
278 EXPECT_TRUE(video.pixel_height.is_present());
279 EXPECT_EQ(static_cast<std::uint64_t>(5), video.pixel_height.value());
280
281 EXPECT_TRUE(video.pixel_crop_bottom.is_present());
282 EXPECT_EQ(static_cast<std::uint64_t>(6), video.pixel_crop_bottom.value());
283
284 EXPECT_TRUE(video.pixel_crop_top.is_present());
285 EXPECT_EQ(static_cast<std::uint64_t>(7), video.pixel_crop_top.value());
286
287 EXPECT_TRUE(video.pixel_crop_left.is_present());
288 EXPECT_EQ(static_cast<std::uint64_t>(8), video.pixel_crop_left.value());
289
290 EXPECT_TRUE(video.pixel_crop_right.is_present());
291 EXPECT_EQ(static_cast<std::uint64_t>(9), video.pixel_crop_right.value());
292
293 EXPECT_TRUE(video.display_width.is_present());
294 EXPECT_EQ(static_cast<std::uint64_t>(10), video.display_width.value());
295
296 EXPECT_TRUE(video.display_height.is_present());
297 EXPECT_EQ(static_cast<std::uint64_t>(11), video.display_height.value());
298
299 EXPECT_TRUE(video.display_unit.is_present());
300 EXPECT_EQ(DisplayUnit::kInches, video.display_unit.value());
301
302 EXPECT_TRUE(video.aspect_ratio_type.is_present());
303 EXPECT_EQ(AspectRatioType::kKeep, video.aspect_ratio_type.value());
304
305 EXPECT_TRUE(video.frame_rate.is_present());
306 EXPECT_EQ(2.2360680103302001953125, video.frame_rate.value());
307
308 EXPECT_TRUE(video.colour.is_present());
309 EXPECT_TRUE(video.colour.value().bits_per_channel.is_present());
310 EXPECT_EQ(static_cast<std::uint64_t>(1),
311 video.colour.value().bits_per_channel.value());
312
313 EXPECT_TRUE(video.projection.is_present());
314 EXPECT_TRUE(video.projection.value().type.is_present());
315 EXPECT_EQ(ProjectionType::kCubeMap, video.projection.value().type.value());
316 }
317
TEST_F(VideoParserTest,AbsentDisplaySize)318 TEST_F(VideoParserTest, AbsentDisplaySize) {
319 SetReaderData({
320 0xB0, // ID = 0xB0 (PixelWidth).
321 0x81, // Size = 1.
322 0x01, // Body (value = 1).
323
324 0xBA, // ID = 0xBA (PixelHeight).
325 0x81, // Size = 1.
326 0x02, // Body (value = 2).
327 });
328
329 ParseAndVerify();
330
331 const Video video = parser_.value();
332
333 EXPECT_FALSE(video.interlaced.is_present());
334 EXPECT_EQ(FlagInterlaced::kUnspecified, video.interlaced.value());
335
336 EXPECT_FALSE(video.stereo_mode.is_present());
337 EXPECT_EQ(StereoMode::kMono, video.stereo_mode.value());
338
339 EXPECT_FALSE(video.alpha_mode.is_present());
340 EXPECT_EQ(static_cast<std::uint64_t>(0), video.alpha_mode.value());
341
342 EXPECT_TRUE(video.pixel_width.is_present());
343 EXPECT_EQ(static_cast<std::uint64_t>(1), video.pixel_width.value());
344
345 EXPECT_TRUE(video.pixel_height.is_present());
346 EXPECT_EQ(static_cast<std::uint64_t>(2), video.pixel_height.value());
347
348 EXPECT_FALSE(video.pixel_crop_bottom.is_present());
349 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_bottom.value());
350
351 EXPECT_FALSE(video.pixel_crop_top.is_present());
352 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_top.value());
353
354 EXPECT_FALSE(video.pixel_crop_left.is_present());
355 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_left.value());
356
357 EXPECT_FALSE(video.pixel_crop_right.is_present());
358 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_right.value());
359
360 EXPECT_FALSE(video.display_width.is_present());
361 EXPECT_EQ(static_cast<std::uint64_t>(1), video.display_width.value());
362
363 EXPECT_FALSE(video.display_height.is_present());
364 EXPECT_EQ(static_cast<std::uint64_t>(2), video.display_height.value());
365
366 EXPECT_FALSE(video.display_unit.is_present());
367 EXPECT_EQ(DisplayUnit::kPixels, video.display_unit.value());
368
369 EXPECT_FALSE(video.aspect_ratio_type.is_present());
370 EXPECT_EQ(AspectRatioType::kFreeResizing, video.aspect_ratio_type.value());
371
372 EXPECT_FALSE(video.frame_rate.is_present());
373 EXPECT_EQ(static_cast<std::uint64_t>(0), video.frame_rate.value());
374
375 EXPECT_FALSE(video.colour.is_present());
376 EXPECT_EQ(Colour{}, video.colour.value());
377 }
378
TEST_F(VideoParserTest,DefaultDisplaySize)379 TEST_F(VideoParserTest, DefaultDisplaySize) {
380 SetReaderData({
381 0xB0, // ID = 0xB0 (PixelWidth).
382 0x40, 0x01, // Size = 1.
383 0x01, // Body (value = 1).
384
385 0xBA, // ID = 0xBA (PixelHeight).
386 0x40, 0x01, // Size = 1.
387 0x02, // Body (value = 2).
388
389 0x54, 0xB0, // ID = 0x54B0 (DisplayWidth).
390 0x40, 0x00, // Size = 0.
391
392 0x54, 0xBA, // ID = 0x54BA (DisplayHeight).
393 0x40, 0x00, // Size = 0.
394 });
395
396 ParseAndVerify();
397
398 const Video video = parser_.value();
399
400 EXPECT_FALSE(video.interlaced.is_present());
401 EXPECT_EQ(FlagInterlaced::kUnspecified, video.interlaced.value());
402
403 EXPECT_FALSE(video.stereo_mode.is_present());
404 EXPECT_EQ(StereoMode::kMono, video.stereo_mode.value());
405
406 EXPECT_FALSE(video.alpha_mode.is_present());
407 EXPECT_EQ(static_cast<std::uint64_t>(0), video.alpha_mode.value());
408
409 EXPECT_TRUE(video.pixel_width.is_present());
410 EXPECT_EQ(static_cast<std::uint64_t>(1), video.pixel_width.value());
411
412 EXPECT_TRUE(video.pixel_height.is_present());
413 EXPECT_EQ(static_cast<std::uint64_t>(2), video.pixel_height.value());
414
415 EXPECT_FALSE(video.pixel_crop_bottom.is_present());
416 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_bottom.value());
417
418 EXPECT_FALSE(video.pixel_crop_top.is_present());
419 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_top.value());
420
421 EXPECT_FALSE(video.pixel_crop_left.is_present());
422 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_left.value());
423
424 EXPECT_FALSE(video.pixel_crop_right.is_present());
425 EXPECT_EQ(static_cast<std::uint64_t>(0), video.pixel_crop_right.value());
426
427 EXPECT_TRUE(video.display_width.is_present());
428 EXPECT_EQ(static_cast<std::uint64_t>(1), video.display_width.value());
429
430 EXPECT_TRUE(video.display_height.is_present());
431 EXPECT_EQ(static_cast<std::uint64_t>(2), video.display_height.value());
432
433 EXPECT_FALSE(video.display_unit.is_present());
434 EXPECT_EQ(DisplayUnit::kPixels, video.display_unit.value());
435
436 EXPECT_FALSE(video.aspect_ratio_type.is_present());
437 EXPECT_EQ(AspectRatioType::kFreeResizing, video.aspect_ratio_type.value());
438
439 EXPECT_FALSE(video.frame_rate.is_present());
440 EXPECT_EQ(static_cast<std::uint64_t>(0), video.frame_rate.value());
441
442 EXPECT_FALSE(video.colour.is_present());
443 EXPECT_EQ(Colour{}, video.colour.value());
444 }
445
446 } // namespace
447