xref: /aosp_15_r20/external/webrtc/rtc_tools/frame_analyzer/video_geometry_aligner_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 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 "rtc_tools/frame_analyzer/video_geometry_aligner.h"
12 
13 #include <vector>
14 
15 #include "api/video/i420_buffer.h"
16 #include "rtc_tools/frame_analyzer/video_quality_analysis.h"
17 #include "rtc_tools/video_file_reader.h"
18 #include "test/gtest.h"
19 #include "test/testsupport/file_utils.h"
20 
21 namespace webrtc {
22 namespace test {
23 
24 class VideoGeometryAlignerTest : public ::testing::Test {
25  protected:
SetUp()26   void SetUp() {
27     reference_video_ =
28         OpenYuvFile(ResourcePath("foreman_128x96", "yuv"), 128, 96);
29     ASSERT_TRUE(reference_video_);
30 
31     // Very simple 4x4 frame used for verying CropAndZoom.
32     const uint8_t data_y[] = {0, 1, 2,  3,  4,  5,  6,  7,
33                               8, 9, 10, 11, 12, 13, 14, 15};
34     const uint8_t data_u[] = {0, 1, 2, 3};
35     const uint8_t data_v[] = {0, 1, 2, 3};
36     test_frame_ = I420Buffer::Copy(
37         /* width= */ 4, /* height= */ 4, data_y, /* stride_y= */ 4, data_u,
38         /* stride_u= */ 2, data_v, /* stride_v= */ 2);
39   }
40 
41   rtc::scoped_refptr<Video> reference_video_;
42   rtc::scoped_refptr<I420BufferInterface> test_frame_;
43 };
44 
45 // Teach gtest how to compare CropRegions.
operator ==(const CropRegion & a,const CropRegion & b)46 bool operator==(const CropRegion& a, const CropRegion& b) {
47   return a.left == b.left && a.top == b.top && a.right == b.right &&
48          a.bottom == b.bottom;
49 }
50 
TEST_F(VideoGeometryAlignerTest,CropAndZoomIdentity)51 TEST_F(VideoGeometryAlignerTest, CropAndZoomIdentity) {
52   const rtc::scoped_refptr<I420BufferInterface> frame =
53       reference_video_->GetFrame(0);
54 
55   // Assume perfect match, i.e. SSIM == 1.
56   CropRegion identity_region;
57   EXPECT_EQ(1.0, Ssim(frame, CropAndZoom(identity_region, frame)));
58 }
59 
TEST_F(VideoGeometryAlignerTest,CropAndZoomLeft)60 TEST_F(VideoGeometryAlignerTest, CropAndZoomLeft) {
61   CropRegion region;
62   region.left = 2;
63   const rtc::scoped_refptr<I420BufferInterface> cropped_frame =
64       CropAndZoom(region, test_frame_);
65   EXPECT_EQ(std::vector<uint8_t>(
66                 {2, 2, 3, 3, 6, 6, 7, 7, 10, 10, 11, 11, 14, 14, 15, 15}),
67             std::vector<uint8_t>(cropped_frame->DataY(),
68                                  cropped_frame->DataY() + 16));
69   EXPECT_EQ(
70       std::vector<uint8_t>({1, 1, 3, 3}),
71       std::vector<uint8_t>(cropped_frame->DataU(), cropped_frame->DataU() + 4));
72   EXPECT_EQ(
73       std::vector<uint8_t>({1, 1, 3, 3}),
74       std::vector<uint8_t>(cropped_frame->DataV(), cropped_frame->DataV() + 4));
75 }
76 
77 // TODO(magjed): Re-enable when libyuv filtering is updated.
TEST_F(VideoGeometryAlignerTest,DISABLED_CropAndZoomTop)78 TEST_F(VideoGeometryAlignerTest, DISABLED_CropAndZoomTop) {
79   CropRegion region;
80   region.top = 2;
81   const rtc::scoped_refptr<I420BufferInterface> cropped_frame =
82       CropAndZoom(region, test_frame_);
83   EXPECT_EQ(std::vector<uint8_t>(
84                 {8, 9, 10, 11, 10, 11, 12, 13, 12, 13, 14, 15, 12, 13, 14, 15}),
85             std::vector<uint8_t>(cropped_frame->DataY(),
86                                  cropped_frame->DataY() + 16));
87   EXPECT_EQ(
88       std::vector<uint8_t>({2, 3, 2, 3}),
89       std::vector<uint8_t>(cropped_frame->DataU(), cropped_frame->DataU() + 4));
90   EXPECT_EQ(
91       std::vector<uint8_t>({2, 3, 2, 3}),
92       std::vector<uint8_t>(cropped_frame->DataV(), cropped_frame->DataV() + 4));
93 }
94 
TEST_F(VideoGeometryAlignerTest,CropAndZoomRight)95 TEST_F(VideoGeometryAlignerTest, CropAndZoomRight) {
96   CropRegion region;
97   region.right = 2;
98   const rtc::scoped_refptr<I420BufferInterface> cropped_frame =
99       CropAndZoom(region, test_frame_);
100   EXPECT_EQ(std::vector<uint8_t>(
101                 {0, 0, 1, 1, 4, 4, 5, 5, 8, 8, 9, 9, 12, 12, 13, 13}),
102             std::vector<uint8_t>(cropped_frame->DataY(),
103                                  cropped_frame->DataY() + 16));
104   EXPECT_EQ(
105       std::vector<uint8_t>({0, 0, 2, 2}),
106       std::vector<uint8_t>(cropped_frame->DataU(), cropped_frame->DataU() + 4));
107   EXPECT_EQ(
108       std::vector<uint8_t>({0, 0, 2, 2}),
109       std::vector<uint8_t>(cropped_frame->DataV(), cropped_frame->DataV() + 4));
110 }
111 
112 // TODO(magjed): Re-enable when libyuv filtering is updated.
TEST_F(VideoGeometryAlignerTest,DISABLED_CropAndZoomBottom)113 TEST_F(VideoGeometryAlignerTest, DISABLED_CropAndZoomBottom) {
114   CropRegion region;
115   region.bottom = 2;
116   const rtc::scoped_refptr<I420BufferInterface> cropped_frame =
117       CropAndZoom(region, test_frame_);
118   EXPECT_EQ(
119       std::vector<uint8_t>({0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 4, 5, 6, 7}),
120       std::vector<uint8_t>(cropped_frame->DataY(),
121                            cropped_frame->DataY() + 16));
122   EXPECT_EQ(
123       std::vector<uint8_t>({0, 1, 0, 1}),
124       std::vector<uint8_t>(cropped_frame->DataU(), cropped_frame->DataU() + 4));
125   EXPECT_EQ(
126       std::vector<uint8_t>({0, 1, 0, 1}),
127       std::vector<uint8_t>(cropped_frame->DataV(), cropped_frame->DataV() + 4));
128 }
129 
TEST_F(VideoGeometryAlignerTest,CalculateCropRegionIdentity)130 TEST_F(VideoGeometryAlignerTest, CalculateCropRegionIdentity) {
131   const rtc::scoped_refptr<I420BufferInterface> frame =
132       reference_video_->GetFrame(0);
133   CropRegion identity_region;
134   EXPECT_EQ(identity_region, CalculateCropRegion(frame, frame));
135 }
136 
TEST_F(VideoGeometryAlignerTest,CalculateCropRegionArbitrary)137 TEST_F(VideoGeometryAlignerTest, CalculateCropRegionArbitrary) {
138   // Arbitrary crop region.
139   CropRegion crop_region;
140   crop_region.left = 2;
141   crop_region.top = 4;
142   crop_region.right = 5;
143   crop_region.bottom = 3;
144 
145   const rtc::scoped_refptr<I420BufferInterface> frame =
146       reference_video_->GetFrame(0);
147 
148   EXPECT_EQ(crop_region,
149             CalculateCropRegion(frame, CropAndZoom(crop_region, frame)));
150 }
151 
152 }  // namespace test
153 }  // namespace webrtc
154