xref: /aosp_15_r20/external/libaom/test/accounting_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, 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 <math.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #include "test/acm_random.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/bitreader.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/bitwriter.h"
22*77c1e3ccSAndroid Build Coastguard Worker 
23*77c1e3ccSAndroid Build Coastguard Worker using libaom_test::ACMRandom;
24*77c1e3ccSAndroid Build Coastguard Worker 
TEST(AV1,TestAccounting)25*77c1e3ccSAndroid Build Coastguard Worker TEST(AV1, TestAccounting) {
26*77c1e3ccSAndroid Build Coastguard Worker   const int kBufferSize = 10000;
27*77c1e3ccSAndroid Build Coastguard Worker   const int kSymbols = 1024;
28*77c1e3ccSAndroid Build Coastguard Worker   aom_writer bw;
29*77c1e3ccSAndroid Build Coastguard Worker   uint8_t bw_buffer[kBufferSize];
30*77c1e3ccSAndroid Build Coastguard Worker   aom_start_encode(&bw, bw_buffer);
31*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < kSymbols; i++) {
32*77c1e3ccSAndroid Build Coastguard Worker     aom_write(&bw, 0, 32);
33*77c1e3ccSAndroid Build Coastguard Worker     aom_write(&bw, 0, 32);
34*77c1e3ccSAndroid Build Coastguard Worker     aom_write(&bw, 0, 32);
35*77c1e3ccSAndroid Build Coastguard Worker   }
36*77c1e3ccSAndroid Build Coastguard Worker   GTEST_ASSERT_GE(aom_stop_encode(&bw), 0);
37*77c1e3ccSAndroid Build Coastguard Worker   aom_reader br;
38*77c1e3ccSAndroid Build Coastguard Worker   aom_reader_init(&br, bw_buffer, bw.pos);
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker   Accounting accounting;
41*77c1e3ccSAndroid Build Coastguard Worker   aom_accounting_init(&accounting);
42*77c1e3ccSAndroid Build Coastguard Worker   br.accounting = &accounting;
43*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < kSymbols; i++) {
44*77c1e3ccSAndroid Build Coastguard Worker     aom_read(&br, 32, "A");
45*77c1e3ccSAndroid Build Coastguard Worker   }
46*77c1e3ccSAndroid Build Coastguard Worker   // Consecutive symbols that are the same are coalesced.
47*77c1e3ccSAndroid Build Coastguard Worker   GTEST_ASSERT_EQ(accounting.syms.num_syms, 1);
48*77c1e3ccSAndroid Build Coastguard Worker   GTEST_ASSERT_EQ(accounting.syms.syms[0].samples, (unsigned int)kSymbols);
49*77c1e3ccSAndroid Build Coastguard Worker 
50*77c1e3ccSAndroid Build Coastguard Worker   aom_accounting_reset(&accounting);
51*77c1e3ccSAndroid Build Coastguard Worker   GTEST_ASSERT_EQ(accounting.syms.num_syms, 0);
52*77c1e3ccSAndroid Build Coastguard Worker 
53*77c1e3ccSAndroid Build Coastguard Worker   // Should record 2 * kSymbols accounting symbols.
54*77c1e3ccSAndroid Build Coastguard Worker   aom_reader_init(&br, bw_buffer, bw.pos);
55*77c1e3ccSAndroid Build Coastguard Worker   br.accounting = &accounting;
56*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < kSymbols; i++) {
57*77c1e3ccSAndroid Build Coastguard Worker     aom_read(&br, 32, "A");
58*77c1e3ccSAndroid Build Coastguard Worker     aom_read(&br, 32, "B");
59*77c1e3ccSAndroid Build Coastguard Worker     aom_read(&br, 32, "B");
60*77c1e3ccSAndroid Build Coastguard Worker   }
61*77c1e3ccSAndroid Build Coastguard Worker   GTEST_ASSERT_EQ(accounting.syms.num_syms, kSymbols * 2);
62*77c1e3ccSAndroid Build Coastguard Worker   uint32_t tell_frac = aom_reader_tell_frac(&br);
63*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < accounting.syms.num_syms; i++) {
64*77c1e3ccSAndroid Build Coastguard Worker     tell_frac -= accounting.syms.syms[i].bits;
65*77c1e3ccSAndroid Build Coastguard Worker   }
66*77c1e3ccSAndroid Build Coastguard Worker   GTEST_ASSERT_EQ(tell_frac, 0U);
67*77c1e3ccSAndroid Build Coastguard Worker 
68*77c1e3ccSAndroid Build Coastguard Worker   GTEST_ASSERT_EQ(aom_accounting_dictionary_lookup(&accounting, "A"),
69*77c1e3ccSAndroid Build Coastguard Worker                   aom_accounting_dictionary_lookup(&accounting, "A"));
70*77c1e3ccSAndroid Build Coastguard Worker 
71*77c1e3ccSAndroid Build Coastguard Worker   // Check for collisions. The current aom_accounting_hash function returns
72*77c1e3ccSAndroid Build Coastguard Worker   // the same hash code for AB and BA.
73*77c1e3ccSAndroid Build Coastguard Worker   GTEST_ASSERT_NE(aom_accounting_dictionary_lookup(&accounting, "AB"),
74*77c1e3ccSAndroid Build Coastguard Worker                   aom_accounting_dictionary_lookup(&accounting, "BA"));
75*77c1e3ccSAndroid Build Coastguard Worker }
76