Michael Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 1 | /* |
Krishna Rapaka | 7319db5 | 2021-09-28 20:35:29 -0700 | [diff] [blame] | 2 | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
Michael Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 3 | * |
Vibhoothi | 41c6dd7 | 2021-10-12 18:48:26 +0000 | [diff] [blame] | 4 | * 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/. |
Johann | 123e8a6 | 2017-12-28 14:40:49 -0800 | [diff] [blame] | 11 | */ |
Michael Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 12 | |
| 13 | #include <math.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
Tom Finegan | 7a07ece | 2017-02-07 17:14:05 -0800 | [diff] [blame] | 17 | #include "third_party/googletest/src/googletest/include/gtest/gtest.h" |
Michael Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 18 | |
| 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 | |
| 24 | using libaom_test::ACMRandom; |
| 25 | |
| 26 | TEST(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 Alaiwan | 2b1ec18 | 2017-12-21 09:38:27 +0100 | [diff] [blame] | 39 | aom_reader_init(&br, bw_buffer, bw.pos); |
Michael Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 40 | |
| 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 Alaiwan | 2b1ec18 | 2017-12-21 09:38:27 +0100 | [diff] [blame] | 55 | aom_reader_init(&br, bw_buffer, bw.pos); |
Michael Bebenita | 6048d05 | 2016-08-25 14:40:54 -0700 | [diff] [blame] | 56 | 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 | } |