blob: 8d8d964a2486a40a59c1b44548178aca3085144e [file] [log] [blame]
Michael Bebenita6048d052016-08-25 14:40:54 -07001/*
Krishna Rapaka7319db52021-09-28 20:35:29 -07002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Michael Bebenita6048d052016-08-25 14:40:54 -07003 *
Vibhoothi41c6dd72021-10-12 18:48:26 +00004 * This source code is subject to the terms of the BSD 3-Clause Clear License
5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6 * License was not distributed with this source code in the LICENSE file, you
7 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the
8 * Alliance for Open Media Patent License 1.0 was not distributed with this
9 * source code in the PATENTS file, you can obtain it at
10 * aomedia.org/license/patent-license/.
Johann123e8a62017-12-28 14:40:49 -080011 */
Michael Bebenita6048d052016-08-25 14:40:54 -070012
13#include <math.h>
14#include <stdlib.h>
15#include <string.h>
16
Tom Finegan7a07ece2017-02-07 17:14:05 -080017#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
Michael Bebenita6048d052016-08-25 14:40:54 -070018
19#include "test/acm_random.h"
20#include "aom/aom_integer.h"
21#include "aom_dsp/bitreader.h"
22#include "aom_dsp/bitwriter.h"
23
24using libaom_test::ACMRandom;
25
26TEST(AV1, TestAccounting) {
27 const int kBufferSize = 10000;
28 const int kSymbols = 1024;
29 aom_writer bw;
30 uint8_t bw_buffer[kBufferSize];
31 aom_start_encode(&bw, bw_buffer);
32 for (int i = 0; i < kSymbols; i++) {
33 aom_write(&bw, 0, 32);
34 aom_write(&bw, 0, 32);
35 aom_write(&bw, 0, 32);
36 }
37 aom_stop_encode(&bw);
38 aom_reader br;
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +010039 aom_reader_init(&br, bw_buffer, bw.pos);
Michael Bebenita6048d052016-08-25 14:40:54 -070040
41 Accounting accounting;
42 aom_accounting_init(&accounting);
43 br.accounting = &accounting;
44 for (int i = 0; i < kSymbols; i++) {
45 aom_read(&br, 32, "A");
46 }
47 // Consecutive symbols that are the same are coalesced.
48 GTEST_ASSERT_EQ(accounting.syms.num_syms, 1);
49 GTEST_ASSERT_EQ(accounting.syms.syms[0].samples, (unsigned int)kSymbols);
50
51 aom_accounting_reset(&accounting);
52 GTEST_ASSERT_EQ(accounting.syms.num_syms, 0);
53
54 // Should record 2 * kSymbols accounting symbols.
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +010055 aom_reader_init(&br, bw_buffer, bw.pos);
Michael Bebenita6048d052016-08-25 14:40:54 -070056 br.accounting = &accounting;
57 for (int i = 0; i < kSymbols; i++) {
58 aom_read(&br, 32, "A");
59 aom_read(&br, 32, "B");
60 aom_read(&br, 32, "B");
61 }
62 GTEST_ASSERT_EQ(accounting.syms.num_syms, kSymbols * 2);
63 uint32_t tell_frac = aom_reader_tell_frac(&br);
64 for (int i = 0; i < accounting.syms.num_syms; i++) {
65 tell_frac -= accounting.syms.syms[i].bits;
66 }
67 GTEST_ASSERT_EQ(tell_frac, 0U);
68
69 GTEST_ASSERT_EQ(aom_accounting_dictionary_lookup(&accounting, "A"),
70 aom_accounting_dictionary_lookup(&accounting, "A"));
71
72 // Check for collisions. The current aom_accounting_hash function returns
73 // the same hash code for AB and BA.
74 GTEST_ASSERT_NE(aom_accounting_dictionary_lookup(&accounting, "AB"),
75 aom_accounting_dictionary_lookup(&accounting, "BA"));
76}