xref: /aosp_15_r20/external/libgav1/src/quantizer_test.cc (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1*09537850SAkhilesh Sanikop // Copyright 2021 The libgav1 Authors
2*09537850SAkhilesh Sanikop //
3*09537850SAkhilesh Sanikop // Licensed under the Apache License, Version 2.0 (the "License");
4*09537850SAkhilesh Sanikop // you may not use this file except in compliance with the License.
5*09537850SAkhilesh Sanikop // You may obtain a copy of the License at
6*09537850SAkhilesh Sanikop //
7*09537850SAkhilesh Sanikop //      http://www.apache.org/licenses/LICENSE-2.0
8*09537850SAkhilesh Sanikop //
9*09537850SAkhilesh Sanikop // Unless required by applicable law or agreed to in writing, software
10*09537850SAkhilesh Sanikop // distributed under the License is distributed on an "AS IS" BASIS,
11*09537850SAkhilesh Sanikop // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*09537850SAkhilesh Sanikop // See the License for the specific language governing permissions and
13*09537850SAkhilesh Sanikop // limitations under the License.
14*09537850SAkhilesh Sanikop 
15*09537850SAkhilesh Sanikop #include "src/quantizer.h"
16*09537850SAkhilesh Sanikop 
17*09537850SAkhilesh Sanikop #include <cstdint>
18*09537850SAkhilesh Sanikop 
19*09537850SAkhilesh Sanikop #include "gtest/gtest.h"
20*09537850SAkhilesh Sanikop #include "src/obu_parser.h"
21*09537850SAkhilesh Sanikop #include "src/utils/constants.h"
22*09537850SAkhilesh Sanikop #include "src/utils/types.h"
23*09537850SAkhilesh Sanikop 
24*09537850SAkhilesh Sanikop namespace libgav1 {
25*09537850SAkhilesh Sanikop namespace {
26*09537850SAkhilesh Sanikop 
TEST(QuantizerTest,GetQIndex)27*09537850SAkhilesh Sanikop TEST(QuantizerTest, GetQIndex) {
28*09537850SAkhilesh Sanikop   const int kBaseQIndex = 40;
29*09537850SAkhilesh Sanikop   const int kDelta = 10;
30*09537850SAkhilesh Sanikop   const int kOutOfRangeIndex = 200;
31*09537850SAkhilesh Sanikop   Segmentation segmentation = {};
32*09537850SAkhilesh Sanikop 
33*09537850SAkhilesh Sanikop   EXPECT_EQ(GetQIndex(segmentation, 0, kBaseQIndex), kBaseQIndex);
34*09537850SAkhilesh Sanikop   EXPECT_EQ(GetQIndex(segmentation, kOutOfRangeIndex, kBaseQIndex),
35*09537850SAkhilesh Sanikop             kBaseQIndex);
36*09537850SAkhilesh Sanikop 
37*09537850SAkhilesh Sanikop   segmentation.enabled = true;
38*09537850SAkhilesh Sanikop   EXPECT_EQ(GetQIndex(segmentation, 0, kBaseQIndex), kBaseQIndex);
39*09537850SAkhilesh Sanikop   EXPECT_EQ(GetQIndex(segmentation, kOutOfRangeIndex, kBaseQIndex),
40*09537850SAkhilesh Sanikop             kBaseQIndex);
41*09537850SAkhilesh Sanikop 
42*09537850SAkhilesh Sanikop   segmentation.feature_enabled[1][kSegmentFeatureQuantizer] = true;
43*09537850SAkhilesh Sanikop   segmentation.feature_data[1][kSegmentFeatureQuantizer] = kDelta;
44*09537850SAkhilesh Sanikop   EXPECT_EQ(GetQIndex(segmentation, 1, kBaseQIndex), kBaseQIndex + kDelta);
45*09537850SAkhilesh Sanikop   EXPECT_EQ(GetQIndex(segmentation, kOutOfRangeIndex, kBaseQIndex),
46*09537850SAkhilesh Sanikop             kBaseQIndex);
47*09537850SAkhilesh Sanikop 
48*09537850SAkhilesh Sanikop   segmentation.enabled = false;
49*09537850SAkhilesh Sanikop   EXPECT_EQ(GetQIndex(segmentation, 1, kBaseQIndex), kBaseQIndex);
50*09537850SAkhilesh Sanikop   EXPECT_EQ(GetQIndex(segmentation, kOutOfRangeIndex, kBaseQIndex),
51*09537850SAkhilesh Sanikop             kBaseQIndex);
52*09537850SAkhilesh Sanikop }
53*09537850SAkhilesh Sanikop 
TEST(QuantizerTest,GetDcValue)54*09537850SAkhilesh Sanikop TEST(QuantizerTest, GetDcValue) {
55*09537850SAkhilesh Sanikop   QuantizerParameters params = {};
56*09537850SAkhilesh Sanikop   params.delta_dc[kPlaneY] = 1;
57*09537850SAkhilesh Sanikop   params.delta_dc[kPlaneU] = 2;
58*09537850SAkhilesh Sanikop   params.delta_dc[kPlaneV] = 3;
59*09537850SAkhilesh Sanikop 
60*09537850SAkhilesh Sanikop   // Test lookups of Dc_Qlookup[0][0], Dc_Qlookup[0][11], Dc_Qlookup[0][12],
61*09537850SAkhilesh Sanikop   // and Dc_Qlookup[0][255] in the spec, including the clipping of qindex.
62*09537850SAkhilesh Sanikop   {
63*09537850SAkhilesh Sanikop     Quantizer quantizer(8, &params);
64*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, -2), 4);
65*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, -1), 4);
66*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 10), 16);
67*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 11), 17);
68*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 254), 1336);
69*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 255), 1336);
70*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, -3), 4);
71*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, -2), 4);
72*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 9), 16);
73*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 10), 17);
74*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 253), 1336);
75*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 254), 1336);
76*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, -4), 4);
77*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, -3), 4);
78*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 8), 16);
79*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 9), 17);
80*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 252), 1336);
81*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 253), 1336);
82*09537850SAkhilesh Sanikop   }
83*09537850SAkhilesh Sanikop 
84*09537850SAkhilesh Sanikop #if LIBGAV1_MAX_BITDEPTH >= 10
85*09537850SAkhilesh Sanikop   // Test lookups of Dc_Qlookup[1][0], Dc_Qlookup[1][11], Dc_Qlookup[1][12],
86*09537850SAkhilesh Sanikop   // and Dc_Qlookup[1][255] in the spec, including the clipping of qindex.
87*09537850SAkhilesh Sanikop   {
88*09537850SAkhilesh Sanikop     Quantizer quantizer(10, &params);
89*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, -2), 4);
90*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, -1), 4);
91*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 10), 34);
92*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 11), 37);
93*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 254), 5347);
94*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 255), 5347);
95*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, -3), 4);
96*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, -2), 4);
97*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 9), 34);
98*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 10), 37);
99*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 253), 5347);
100*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 254), 5347);
101*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, -4), 4);
102*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, -3), 4);
103*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 8), 34);
104*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 9), 37);
105*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 254), 5347);
106*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 253), 5347);
107*09537850SAkhilesh Sanikop   }
108*09537850SAkhilesh Sanikop #endif  // LIBGAV1_MAX_BITDEPTH >= 10
109*09537850SAkhilesh Sanikop 
110*09537850SAkhilesh Sanikop #if LIBGAV1_MAX_BITDEPTH == 12
111*09537850SAkhilesh Sanikop   // Test lookups of Dc_Qlookup[2][0], Dc_Qlookup[2][11], Dc_Qlookup[2][12],
112*09537850SAkhilesh Sanikop   // and Dc_Qlookup[2][255] in the spec, including the clipping of qindex.
113*09537850SAkhilesh Sanikop   {
114*09537850SAkhilesh Sanikop     Quantizer quantizer(12, &params);
115*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, -2), 4);
116*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, -1), 4);
117*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 10), 103);
118*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 11), 115);
119*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 254), 21387);
120*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneY, 255), 21387);
121*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, -3), 4);
122*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, -2), 4);
123*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 9), 103);
124*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 10), 115);
125*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 253), 21387);
126*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneU, 254), 21387);
127*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, -4), 4);
128*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, -3), 4);
129*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 8), 103);
130*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 9), 115);
131*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 254), 21387);
132*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetDcValue(kPlaneV, 253), 21387);
133*09537850SAkhilesh Sanikop   }
134*09537850SAkhilesh Sanikop #endif  // LIBGAV1_MAX_BITDEPTH == 12
135*09537850SAkhilesh Sanikop }
136*09537850SAkhilesh Sanikop 
TEST(QuantizerTest,GetAcValue)137*09537850SAkhilesh Sanikop TEST(QuantizerTest, GetAcValue) {
138*09537850SAkhilesh Sanikop   QuantizerParameters params = {};
139*09537850SAkhilesh Sanikop   params.delta_ac[kPlaneU] = 1;
140*09537850SAkhilesh Sanikop   params.delta_ac[kPlaneV] = 2;
141*09537850SAkhilesh Sanikop 
142*09537850SAkhilesh Sanikop   // Test lookups of Ac_Qlookup[0][0], Ac_Qlookup[0][11], Ac_Qlookup[0][12],
143*09537850SAkhilesh Sanikop   // and Ac_Qlookup[0][255] in the spec, including the clipping of qindex.
144*09537850SAkhilesh Sanikop   {
145*09537850SAkhilesh Sanikop     Quantizer quantizer(8, &params);
146*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, -1), 4);
147*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 0), 4);
148*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 11), 18);
149*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 12), 19);
150*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 255), 1828);
151*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 256), 1828);
152*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, -2), 4);
153*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, -1), 4);
154*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 10), 18);
155*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 11), 19);
156*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 254), 1828);
157*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 255), 1828);
158*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, -3), 4);
159*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, -2), 4);
160*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 9), 18);
161*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 10), 19);
162*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 253), 1828);
163*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 254), 1828);
164*09537850SAkhilesh Sanikop   }
165*09537850SAkhilesh Sanikop 
166*09537850SAkhilesh Sanikop #if LIBGAV1_MAX_BITDEPTH >= 10
167*09537850SAkhilesh Sanikop   // Test lookups of Ac_Qlookup[1][0], Ac_Qlookup[1][11], Ac_Qlookup[1][12],
168*09537850SAkhilesh Sanikop   // and Ac_Qlookup[1][255] in the spec, including the clipping of qindex.
169*09537850SAkhilesh Sanikop   {
170*09537850SAkhilesh Sanikop     Quantizer quantizer(10, &params);
171*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, -1), 4);
172*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 0), 4);
173*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 11), 37);
174*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 12), 40);
175*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 255), 7312);
176*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 256), 7312);
177*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, -2), 4);
178*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, -1), 4);
179*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 10), 37);
180*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 11), 40);
181*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 254), 7312);
182*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 255), 7312);
183*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, -3), 4);
184*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, -2), 4);
185*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 9), 37);
186*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 10), 40);
187*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 253), 7312);
188*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 254), 7312);
189*09537850SAkhilesh Sanikop   }
190*09537850SAkhilesh Sanikop #endif  // LIBGAV1_MAX_BITDEPTH >= 10
191*09537850SAkhilesh Sanikop 
192*09537850SAkhilesh Sanikop #if LIBGAV1_MAX_BITDEPTH == 12
193*09537850SAkhilesh Sanikop   // Test lookups of Ac_Qlookup[1][0], Ac_Qlookup[1][11], Ac_Qlookup[1][12],
194*09537850SAkhilesh Sanikop   // and Ac_Qlookup[1][255] in the spec, including the clipping of qindex.
195*09537850SAkhilesh Sanikop   {
196*09537850SAkhilesh Sanikop     Quantizer quantizer(12, &params);
197*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, -1), 4);
198*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 0), 4);
199*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 11), 112);
200*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 12), 126);
201*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 255), 29247);
202*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneY, 256), 29247);
203*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, -2), 4);
204*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, -1), 4);
205*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 10), 112);
206*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 11), 126);
207*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 254), 29247);
208*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneU, 255), 29247);
209*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, -3), 4);
210*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, -2), 4);
211*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 9), 112);
212*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 10), 126);
213*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 253), 29247);
214*09537850SAkhilesh Sanikop     EXPECT_EQ(quantizer.GetAcValue(kPlaneV, 254), 29247);
215*09537850SAkhilesh Sanikop   }
216*09537850SAkhilesh Sanikop #endif  // LIBGAV1_MAX_BITDEPTH == 12
217*09537850SAkhilesh Sanikop }
218*09537850SAkhilesh Sanikop 
219*09537850SAkhilesh Sanikop }  // namespace
220*09537850SAkhilesh Sanikop }  // namespace libgav1
221