1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  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.
9  */
10 
11 #include "modules/video_coding/utility/vp9_uncompressed_header_parser.h"
12 
13 #include "test/gmock.h"
14 #include "test/gtest.h"
15 
16 namespace webrtc {
17 namespace vp9 {
18 using ::testing::AllOf;
19 using ::testing::ElementsAre;
20 using ::testing::Eq;
21 using ::testing::Field;
22 using ::testing::Optional;
23 
TEST(Vp9UncompressedHeaderParserTest,FrameWithSegmentation)24 TEST(Vp9UncompressedHeaderParserTest, FrameWithSegmentation) {
25   // Uncompressed header from a frame generated with libvpx.
26   // Encoded QVGA frame (SL0 of a VGA frame) that includes a segmentation.
27   const uint8_t kHeader[] = {
28       0x87, 0x01, 0x00, 0x00, 0x02, 0x7e, 0x01, 0xdf, 0x02, 0x7f, 0x01, 0xdf,
29       0xc6, 0x87, 0x04, 0x83, 0x83, 0x2e, 0x46, 0x60, 0x20, 0x38, 0x0c, 0x06,
30       0x03, 0xcd, 0x80, 0xc0, 0x60, 0x9f, 0xc5, 0x46, 0x00, 0x00, 0x00, 0x00,
31       0x2e, 0x73, 0xb7, 0xee, 0x22, 0x06, 0x81, 0x82, 0xd4, 0xef, 0xc3, 0x58,
32       0x1f, 0x12, 0xd2, 0x7b, 0x28, 0x1f, 0x80, 0xfc, 0x07, 0xe0, 0x00, 0x00};
33 
34   absl::optional<Vp9UncompressedHeader> frame_info =
35       ParseUncompressedVp9Header(kHeader);
36   ASSERT_TRUE(frame_info.has_value());
37 
38   EXPECT_FALSE(frame_info->is_keyframe);
39   EXPECT_TRUE(frame_info->error_resilient);
40   EXPECT_TRUE(frame_info->show_frame);
41   EXPECT_FALSE(frame_info->show_existing_frame);
42   EXPECT_EQ(frame_info->base_qp, 185);
43   EXPECT_EQ(frame_info->frame_width, 320);
44   EXPECT_EQ(frame_info->frame_height, 240);
45   EXPECT_EQ(frame_info->render_width, 640);
46   EXPECT_EQ(frame_info->render_height, 480);
47   EXPECT_TRUE(frame_info->allow_high_precision_mv);
48   EXPECT_EQ(frame_info->frame_context_idx, 0u);
49   EXPECT_EQ(frame_info->interpolation_filter,
50             Vp9InterpolationFilter::kSwitchable);
51   EXPECT_EQ(frame_info->is_lossless, false);
52   EXPECT_EQ(frame_info->profile, 0);
53   EXPECT_THAT(frame_info->reference_buffers, ElementsAre(0, 0, 0));
54   EXPECT_THAT(frame_info->reference_buffers_sign_bias, 0b0000);
55   EXPECT_EQ(frame_info->updated_buffers, 0b10000000);
56   EXPECT_EQ(frame_info->tile_cols_log2, 0u);
57   EXPECT_EQ(frame_info->tile_rows_log2, 0u);
58   EXPECT_EQ(frame_info->render_size_offset_bits, 64u);
59   EXPECT_EQ(frame_info->compressed_header_size, 23u);
60   EXPECT_EQ(frame_info->uncompressed_header_size, 37u);
61 
62   EXPECT_TRUE(frame_info->segmentation_enabled);
63   EXPECT_FALSE(frame_info->segmentation_is_delta);
64   EXPECT_THAT(frame_info->segmentation_pred_prob,
65               Optional(ElementsAre(205, 1, 1)));
66   EXPECT_THAT(frame_info->segmentation_tree_probs,
67               Optional(ElementsAre(255, 255, 128, 1, 128, 128, 128)));
68   EXPECT_THAT(frame_info->segmentation_features[1][kVp9SegLvlAlt_Q], Eq(-63));
69   EXPECT_THAT(frame_info->segmentation_features[2][kVp9SegLvlAlt_Q], Eq(-81));
70 }
71 
TEST(Vp9UncompressedHeaderParserTest,SegmentationWithDefaultPredProbs)72 TEST(Vp9UncompressedHeaderParserTest, SegmentationWithDefaultPredProbs) {
73   const uint8_t kHeader[] = {0x90, 0x49, 0x83, 0x42, 0x80, 0x2e,
74                              0x30, 0x0,  0xb0, 0x0,  0x37, 0xff,
75                              0x06, 0x80, 0x0,  0x0,  0x0,  0x0};
76   absl::optional<Vp9UncompressedHeader> frame_info =
77       ParseUncompressedVp9Header(kHeader);
78   ASSERT_TRUE(frame_info.has_value());
79   EXPECT_THAT(frame_info->segmentation_pred_prob,
80               Optional(ElementsAre(255, 255, 255)));
81 }
82 
TEST(Vp9UncompressedHeaderParserTest,SegmentationWithSkipLevel)83 TEST(Vp9UncompressedHeaderParserTest, SegmentationWithSkipLevel) {
84   const uint8_t kHeader[] = {0x90, 0x49, 0x83, 0x42, 0x80, 0x2e, 0x30, 0x00,
85                              0xb0, 0x00, 0x37, 0xff, 0x06, 0x80, 0x01, 0x08,
86                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
87   absl::optional<Vp9UncompressedHeader> frame_info =
88       ParseUncompressedVp9Header(kHeader);
89   ASSERT_TRUE(frame_info.has_value());
90   EXPECT_THAT(frame_info->segmentation_features[0][kVp9SegLvlSkip], Eq(1));
91 }
92 
93 }  // namespace vp9
94 }  // namespace webrtc
95