1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2022, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/cost.h"
13*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodemv.h"
14*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
15*77c1e3ccSAndroid Build Coastguard Worker
16*77c1e3ccSAndroid Build Coastguard Worker namespace {
17*77c1e3ccSAndroid Build Coastguard Worker
ReferenceBuildNmvComponentCostTable(int * mvcost,const nmv_component * const mvcomp,MvSubpelPrecision precision)18*77c1e3ccSAndroid Build Coastguard Worker void ReferenceBuildNmvComponentCostTable(int *mvcost,
19*77c1e3ccSAndroid Build Coastguard Worker const nmv_component *const mvcomp,
20*77c1e3ccSAndroid Build Coastguard Worker MvSubpelPrecision precision) {
21*77c1e3ccSAndroid Build Coastguard Worker int i, v;
22*77c1e3ccSAndroid Build Coastguard Worker int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
23*77c1e3ccSAndroid Build Coastguard Worker int bits_cost[MV_OFFSET_BITS][2];
24*77c1e3ccSAndroid Build Coastguard Worker int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
25*77c1e3ccSAndroid Build Coastguard Worker int class0_hp_cost[2], hp_cost[2];
26*77c1e3ccSAndroid Build Coastguard Worker av1_cost_tokens_from_cdf(sign_cost, mvcomp->sign_cdf, nullptr);
27*77c1e3ccSAndroid Build Coastguard Worker av1_cost_tokens_from_cdf(class_cost, mvcomp->classes_cdf, nullptr);
28*77c1e3ccSAndroid Build Coastguard Worker av1_cost_tokens_from_cdf(class0_cost, mvcomp->class0_cdf, nullptr);
29*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < MV_OFFSET_BITS; ++i) {
30*77c1e3ccSAndroid Build Coastguard Worker av1_cost_tokens_from_cdf(bits_cost[i], mvcomp->bits_cdf[i], nullptr);
31*77c1e3ccSAndroid Build Coastguard Worker }
32*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < CLASS0_SIZE; ++i)
33*77c1e3ccSAndroid Build Coastguard Worker av1_cost_tokens_from_cdf(class0_fp_cost[i], mvcomp->class0_fp_cdf[i],
34*77c1e3ccSAndroid Build Coastguard Worker nullptr);
35*77c1e3ccSAndroid Build Coastguard Worker av1_cost_tokens_from_cdf(fp_cost, mvcomp->fp_cdf, nullptr);
36*77c1e3ccSAndroid Build Coastguard Worker if (precision > MV_SUBPEL_LOW_PRECISION) {
37*77c1e3ccSAndroid Build Coastguard Worker av1_cost_tokens_from_cdf(class0_hp_cost, mvcomp->class0_hp_cdf, nullptr);
38*77c1e3ccSAndroid Build Coastguard Worker av1_cost_tokens_from_cdf(hp_cost, mvcomp->hp_cdf, nullptr);
39*77c1e3ccSAndroid Build Coastguard Worker }
40*77c1e3ccSAndroid Build Coastguard Worker mvcost[0] = 0;
41*77c1e3ccSAndroid Build Coastguard Worker for (v = 1; v <= MV_MAX; ++v) {
42*77c1e3ccSAndroid Build Coastguard Worker int z, c, o, d, e, f, cost = 0;
43*77c1e3ccSAndroid Build Coastguard Worker z = v - 1;
44*77c1e3ccSAndroid Build Coastguard Worker c = av1_get_mv_class(z, &o);
45*77c1e3ccSAndroid Build Coastguard Worker cost += class_cost[c];
46*77c1e3ccSAndroid Build Coastguard Worker d = (o >> 3); /* int mv data */
47*77c1e3ccSAndroid Build Coastguard Worker f = (o >> 1) & 3; /* fractional pel mv data */
48*77c1e3ccSAndroid Build Coastguard Worker e = (o & 1); /* high precision mv data */
49*77c1e3ccSAndroid Build Coastguard Worker if (c == MV_CLASS_0) {
50*77c1e3ccSAndroid Build Coastguard Worker cost += class0_cost[d];
51*77c1e3ccSAndroid Build Coastguard Worker } else {
52*77c1e3ccSAndroid Build Coastguard Worker const int b = c + CLASS0_BITS - 1; /* number of bits */
53*77c1e3ccSAndroid Build Coastguard Worker for (i = 0; i < b; ++i) cost += bits_cost[i][((d >> i) & 1)];
54*77c1e3ccSAndroid Build Coastguard Worker }
55*77c1e3ccSAndroid Build Coastguard Worker if (precision > MV_SUBPEL_NONE) {
56*77c1e3ccSAndroid Build Coastguard Worker if (c == MV_CLASS_0) {
57*77c1e3ccSAndroid Build Coastguard Worker cost += class0_fp_cost[d][f];
58*77c1e3ccSAndroid Build Coastguard Worker } else {
59*77c1e3ccSAndroid Build Coastguard Worker cost += fp_cost[f];
60*77c1e3ccSAndroid Build Coastguard Worker }
61*77c1e3ccSAndroid Build Coastguard Worker if (precision > MV_SUBPEL_LOW_PRECISION) {
62*77c1e3ccSAndroid Build Coastguard Worker if (c == MV_CLASS_0) {
63*77c1e3ccSAndroid Build Coastguard Worker cost += class0_hp_cost[e];
64*77c1e3ccSAndroid Build Coastguard Worker } else {
65*77c1e3ccSAndroid Build Coastguard Worker cost += hp_cost[e];
66*77c1e3ccSAndroid Build Coastguard Worker }
67*77c1e3ccSAndroid Build Coastguard Worker }
68*77c1e3ccSAndroid Build Coastguard Worker }
69*77c1e3ccSAndroid Build Coastguard Worker mvcost[v] = cost + sign_cost[0];
70*77c1e3ccSAndroid Build Coastguard Worker mvcost[-v] = cost + sign_cost[1];
71*77c1e3ccSAndroid Build Coastguard Worker }
72*77c1e3ccSAndroid Build Coastguard Worker }
73*77c1e3ccSAndroid Build Coastguard Worker
74*77c1e3ccSAndroid Build Coastguard Worker // Test using the default context, except for sign
75*77c1e3ccSAndroid Build Coastguard Worker static const nmv_component kTestComponentContext = {
76*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF11(28672, 30976, 31858, 32320, 32551, 32656, 32740, 32757, 32762,
77*77c1e3ccSAndroid Build Coastguard Worker 32767) }, // class_cdf // fp
78*77c1e3ccSAndroid Build Coastguard Worker { { AOM_CDF4(16384, 24576, 26624) },
79*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF4(12288, 21248, 24128) } }, // class0_fp_cdf
80*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF4(8192, 17408, 21248) }, // fp_cdf
81*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(70 * 128) }, // sign_cdf
82*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(160 * 128) }, // class0_hp_cdf
83*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(128 * 128) }, // hp_cdf
84*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(216 * 128) }, // class0_cdf
85*77c1e3ccSAndroid Build Coastguard Worker { { AOM_CDF2(128 * 136) },
86*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(128 * 140) },
87*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(128 * 148) },
88*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(128 * 160) },
89*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(128 * 176) },
90*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(128 * 192) },
91*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(128 * 224) },
92*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(128 * 234) },
93*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(128 * 234) },
94*77c1e3ccSAndroid Build Coastguard Worker { AOM_CDF2(128 * 240) } }, // bits_cdf
95*77c1e3ccSAndroid Build Coastguard Worker };
96*77c1e3ccSAndroid Build Coastguard Worker
TestMvComponentCostTable(MvSubpelPrecision precision)97*77c1e3ccSAndroid Build Coastguard Worker void TestMvComponentCostTable(MvSubpelPrecision precision) {
98*77c1e3ccSAndroid Build Coastguard Worker std::unique_ptr<int[]> mvcost_ref_buf(new int[MV_VALS]);
99*77c1e3ccSAndroid Build Coastguard Worker std::unique_ptr<int[]> mvcost_buf(new int[MV_VALS]);
100*77c1e3ccSAndroid Build Coastguard Worker int *mvcost_ref = mvcost_ref_buf.get() + MV_MAX;
101*77c1e3ccSAndroid Build Coastguard Worker int *mvcost = mvcost_buf.get() + MV_MAX;
102*77c1e3ccSAndroid Build Coastguard Worker
103*77c1e3ccSAndroid Build Coastguard Worker ReferenceBuildNmvComponentCostTable(mvcost_ref, &kTestComponentContext,
104*77c1e3ccSAndroid Build Coastguard Worker precision);
105*77c1e3ccSAndroid Build Coastguard Worker av1_build_nmv_component_cost_table(mvcost, &kTestComponentContext, precision);
106*77c1e3ccSAndroid Build Coastguard Worker
107*77c1e3ccSAndroid Build Coastguard Worker for (int v = 0; v <= MV_MAX; ++v) {
108*77c1e3ccSAndroid Build Coastguard Worker ASSERT_EQ(mvcost_ref[v], mvcost[v]) << "v = " << v;
109*77c1e3ccSAndroid Build Coastguard Worker ASSERT_EQ(mvcost_ref[-v], mvcost[-v]) << "v = " << v;
110*77c1e3ccSAndroid Build Coastguard Worker }
111*77c1e3ccSAndroid Build Coastguard Worker }
112*77c1e3ccSAndroid Build Coastguard Worker
TEST(MvCostTest,BuildMvComponentCostTableTest1)113*77c1e3ccSAndroid Build Coastguard Worker TEST(MvCostTest, BuildMvComponentCostTableTest1) {
114*77c1e3ccSAndroid Build Coastguard Worker TestMvComponentCostTable(MV_SUBPEL_NONE);
115*77c1e3ccSAndroid Build Coastguard Worker }
116*77c1e3ccSAndroid Build Coastguard Worker
TEST(MvCostTest,BuildMvComponentCostTableTest2)117*77c1e3ccSAndroid Build Coastguard Worker TEST(MvCostTest, BuildMvComponentCostTableTest2) {
118*77c1e3ccSAndroid Build Coastguard Worker TestMvComponentCostTable(MV_SUBPEL_LOW_PRECISION);
119*77c1e3ccSAndroid Build Coastguard Worker }
120*77c1e3ccSAndroid Build Coastguard Worker
TEST(MvCostTest,BuildMvComponentCostTableTest3)121*77c1e3ccSAndroid Build Coastguard Worker TEST(MvCostTest, BuildMvComponentCostTableTest3) {
122*77c1e3ccSAndroid Build Coastguard Worker TestMvComponentCostTable(MV_SUBPEL_HIGH_PRECISION);
123*77c1e3ccSAndroid Build Coastguard Worker }
124*77c1e3ccSAndroid Build Coastguard Worker
125*77c1e3ccSAndroid Build Coastguard Worker } // namespace