Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 1 | /* |
Alex Converse | 5d33cc4 | 2016-09-28 10:58:19 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 3 | * |
Alex Converse | 5d33cc4 | 2016-09-28 10:58:19 -0700 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <assert.h> |
| 13 | #include <math.h> |
| 14 | #include <stdio.h> |
| 15 | #include <ctime> |
| 16 | #include <utility> |
| 17 | #include <vector> |
| 18 | |
Tom Finegan | 7a07ece | 2017-02-07 17:14:05 -0800 | [diff] [blame] | 19 | #include "third_party/googletest/src/googletest/include/gtest/gtest.h" |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 20 | |
Alex Converse | 080a2cc | 2016-09-20 16:39:01 -0700 | [diff] [blame] | 21 | #include "test/acm_random.h" |
Alex Converse | 7fe2ae8 | 2016-09-28 11:33:20 -0700 | [diff] [blame] | 22 | #include "aom_dsp/ansreader.h" |
Alex Converse | cfa83fd | 2016-10-26 16:31:48 -0700 | [diff] [blame] | 23 | #include "aom_dsp/buf_ans.h" |
Alex Converse | 5d33cc4 | 2016-09-28 10:58:19 -0700 | [diff] [blame] | 24 | |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 25 | namespace { |
| 26 | typedef std::vector<std::pair<uint8_t, bool> > PvVec; |
| 27 | |
Alex Converse | 5d33cc4 | 2016-09-28 10:58:19 -0700 | [diff] [blame] | 28 | const int kPrintStats = 0; |
Alex Converse | eb780e7 | 2016-12-13 12:46:41 -0800 | [diff] [blame] | 29 | // Use a small buffer size to exercise ANS window spills or buffer growth |
| 30 | const int kBufAnsSize = 1 << 8; |
Alex Converse | 5d33cc4 | 2016-09-28 10:58:19 -0700 | [diff] [blame] | 31 | |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 32 | PvVec abs_encode_build_vals(int iters) { |
| 33 | PvVec ret; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 34 | libaom_test::ACMRandom gen(0x30317076); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 35 | double entropy = 0; |
| 36 | for (int i = 0; i < iters; ++i) { |
| 37 | uint8_t p; |
| 38 | do { |
| 39 | p = gen.Rand8(); |
| 40 | } while (p == 0); // zero is not a valid coding probability |
| 41 | bool b = gen.Rand8() < p; |
| 42 | ret.push_back(std::make_pair(static_cast<uint8_t>(p), b)); |
Alex Converse | 5d33cc4 | 2016-09-28 10:58:19 -0700 | [diff] [blame] | 43 | if (kPrintStats) { |
| 44 | double d = p / 256.; |
| 45 | entropy += -d * log2(d) - (1 - d) * log2(1 - d); |
| 46 | } |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 47 | } |
Alex Converse | 5d33cc4 | 2016-09-28 10:58:19 -0700 | [diff] [blame] | 48 | if (kPrintStats) printf("entropy %f\n", entropy); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 49 | return ret; |
| 50 | } |
| 51 | |
Alex Converse | c54692b | 2017-01-25 16:41:05 -0800 | [diff] [blame] | 52 | bool check_rabs(const PvVec &pv_vec, uint8_t *buf) { |
Alex Converse | cfa83fd | 2016-10-26 16:31:48 -0700 | [diff] [blame] | 53 | BufAnsCoder a; |
Alex Converse | 30f0e15 | 2017-03-28 10:13:27 -0700 | [diff] [blame] | 54 | a.size = kBufAnsSize; |
| 55 | aom_buf_ans_alloc(&a, NULL); |
Alex Converse | cfa83fd | 2016-10-26 16:31:48 -0700 | [diff] [blame] | 56 | buf_ans_write_init(&a, buf); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 57 | |
| 58 | std::clock_t start = std::clock(); |
Alex Converse | cfa83fd | 2016-10-26 16:31:48 -0700 | [diff] [blame] | 59 | for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) { |
Alex Converse | c54692b | 2017-01-25 16:41:05 -0800 | [diff] [blame] | 60 | buf_rabs_write(&a, it->second, 256 - it->first); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 61 | } |
Alex Converse | 1ecdf2b | 2016-11-30 15:51:12 -0800 | [diff] [blame] | 62 | aom_buf_ans_flush(&a); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 63 | std::clock_t enc_time = std::clock() - start; |
Alex Converse | cfa83fd | 2016-10-26 16:31:48 -0700 | [diff] [blame] | 64 | int offset = buf_ans_write_end(&a); |
| 65 | aom_buf_ans_free(&a); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 66 | bool okay = true; |
| 67 | AnsDecoder d; |
Alex Converse | 2cdf0d8 | 2016-12-13 13:53:09 -0800 | [diff] [blame] | 68 | #if ANS_MAX_SYMBOLS |
Alex Converse | 346440b | 2017-01-03 13:47:37 -0800 | [diff] [blame] | 69 | d.window_size = kBufAnsSize; |
Alex Converse | 2cdf0d8 | 2016-12-13 13:53:09 -0800 | [diff] [blame] | 70 | #endif |
Alex Converse | 346440b | 2017-01-03 13:47:37 -0800 | [diff] [blame] | 71 | if (ans_read_init(&d, buf, offset)) return false; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 72 | start = std::clock(); |
| 73 | for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) { |
Alex Converse | c54692b | 2017-01-25 16:41:05 -0800 | [diff] [blame] | 74 | okay = okay && (rabs_read(&d, 256 - it->first) != 0) == it->second; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 75 | } |
| 76 | std::clock_t dec_time = std::clock() - start; |
| 77 | if (!okay) return false; |
Alex Converse | 5d33cc4 | 2016-09-28 10:58:19 -0700 | [diff] [blame] | 78 | if (kPrintStats) |
| 79 | printf("uABS size %d enc_time %f dec_time %f\n", offset, |
| 80 | static_cast<float>(enc_time) / CLOCKS_PER_SEC, |
| 81 | static_cast<float>(dec_time) / CLOCKS_PER_SEC); |
Yaowu Xu | 1e487c3 | 2016-11-10 15:54:07 -0800 | [diff] [blame] | 82 | return ans_read_end(&d) != 0; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Alex Converse | 62a94a6 | 2016-09-09 10:51:48 -0700 | [diff] [blame] | 85 | const aom_cdf_prob spareto65[] = { 8320, 6018, 4402, 3254, 4259, |
| 86 | 3919, 2057, 492, 45, 2 }; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 87 | |
Alex Converse | cba3d1f | 2016-09-09 10:30:36 -0700 | [diff] [blame] | 88 | const int kRansSymbols = |
| 89 | static_cast<int>(sizeof(spareto65) / sizeof(spareto65[0])); |
| 90 | |
Alex Converse | 8c687d2 | 2017-02-07 15:55:24 -0800 | [diff] [blame] | 91 | struct rans_sym { |
| 92 | aom_cdf_prob prob; |
| 93 | aom_cdf_prob cum_prob; // not-inclusive |
| 94 | }; |
| 95 | |
Alex Converse | cba3d1f | 2016-09-09 10:30:36 -0700 | [diff] [blame] | 96 | std::vector<int> ans_encode_build_vals(rans_sym *const tab, int iters) { |
| 97 | aom_cdf_prob sum = 0; |
| 98 | for (int i = 0; i < kRansSymbols; ++i) { |
| 99 | tab[i].cum_prob = sum; |
| 100 | tab[i].prob = spareto65[i]; |
| 101 | sum += spareto65[i]; |
| 102 | } |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 103 | std::vector<int> p_to_sym; |
Alex Converse | 9ed1a2f | 2016-09-04 13:30:43 +0200 | [diff] [blame] | 104 | for (int i = 0; i < kRansSymbols; ++i) { |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 105 | p_to_sym.insert(p_to_sym.end(), tab[i].prob, i); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 106 | } |
Alex Converse | 5d33cc4 | 2016-09-28 10:58:19 -0700 | [diff] [blame] | 107 | assert(p_to_sym.size() == RANS_PRECISION); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 108 | std::vector<int> ret; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 109 | libaom_test::ACMRandom gen(18543637); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 110 | for (int i = 0; i < iters; ++i) { |
Alex Converse | 62a94a6 | 2016-09-09 10:51:48 -0700 | [diff] [blame] | 111 | int sym = |
| 112 | p_to_sym[((gen.Rand8() << 8) + gen.Rand8()) & (RANS_PRECISION - 1)]; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 113 | ret.push_back(sym); |
| 114 | } |
| 115 | return ret; |
| 116 | } |
| 117 | |
Alex Converse | 9ed1a2f | 2016-09-04 13:30:43 +0200 | [diff] [blame] | 118 | void rans_build_dec_tab(const struct rans_sym sym_tab[], |
| 119 | aom_cdf_prob *dec_tab) { |
Alex Converse | e9f70f8 | 2016-08-22 12:44:16 -0700 | [diff] [blame] | 120 | unsigned int sum = 0; |
| 121 | for (int i = 0; sum < RANS_PRECISION; ++i) { |
| 122 | dec_tab[i] = sum += sym_tab[i].prob; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | bool check_rans(const std::vector<int> &sym_vec, const rans_sym *const tab, |
| 127 | uint8_t *buf) { |
Alex Converse | cfa83fd | 2016-10-26 16:31:48 -0700 | [diff] [blame] | 128 | BufAnsCoder a; |
Alex Converse | 30f0e15 | 2017-03-28 10:13:27 -0700 | [diff] [blame] | 129 | a.size = kBufAnsSize; |
| 130 | aom_buf_ans_alloc(&a, NULL); |
Alex Converse | cfa83fd | 2016-10-26 16:31:48 -0700 | [diff] [blame] | 131 | buf_ans_write_init(&a, buf); |
Alex Converse | 9ed1a2f | 2016-09-04 13:30:43 +0200 | [diff] [blame] | 132 | aom_cdf_prob dec_tab[kRansSymbols]; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 133 | rans_build_dec_tab(tab, dec_tab); |
| 134 | |
| 135 | std::clock_t start = std::clock(); |
Alex Converse | cfa83fd | 2016-10-26 16:31:48 -0700 | [diff] [blame] | 136 | for (std::vector<int>::const_iterator it = sym_vec.begin(); |
| 137 | it != sym_vec.end(); ++it) { |
Alex Converse | 8c687d2 | 2017-02-07 15:55:24 -0800 | [diff] [blame] | 138 | buf_rans_write(&a, tab[*it].cum_prob, tab[*it].prob); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 139 | } |
Alex Converse | 1ecdf2b | 2016-11-30 15:51:12 -0800 | [diff] [blame] | 140 | aom_buf_ans_flush(&a); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 141 | std::clock_t enc_time = std::clock() - start; |
Alex Converse | cfa83fd | 2016-10-26 16:31:48 -0700 | [diff] [blame] | 142 | int offset = buf_ans_write_end(&a); |
| 143 | aom_buf_ans_free(&a); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 144 | bool okay = true; |
| 145 | AnsDecoder d; |
Alex Converse | 2cdf0d8 | 2016-12-13 13:53:09 -0800 | [diff] [blame] | 146 | #if ANS_MAX_SYMBOLS |
Alex Converse | 346440b | 2017-01-03 13:47:37 -0800 | [diff] [blame] | 147 | d.window_size = kBufAnsSize; |
Alex Converse | 2cdf0d8 | 2016-12-13 13:53:09 -0800 | [diff] [blame] | 148 | #endif |
Alex Converse | 346440b | 2017-01-03 13:47:37 -0800 | [diff] [blame] | 149 | if (ans_read_init(&d, buf, offset)) return false; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 150 | start = std::clock(); |
| 151 | for (std::vector<int>::const_iterator it = sym_vec.begin(); |
| 152 | it != sym_vec.end(); ++it) { |
| 153 | okay &= rans_read(&d, dec_tab) == *it; |
| 154 | } |
| 155 | std::clock_t dec_time = std::clock() - start; |
| 156 | if (!okay) return false; |
Alex Converse | 5d33cc4 | 2016-09-28 10:58:19 -0700 | [diff] [blame] | 157 | if (kPrintStats) |
| 158 | printf("rANS size %d enc_time %f dec_time %f\n", offset, |
| 159 | static_cast<float>(enc_time) / CLOCKS_PER_SEC, |
| 160 | static_cast<float>(dec_time) / CLOCKS_PER_SEC); |
Yaowu Xu | 1e487c3 | 2016-11-10 15:54:07 -0800 | [diff] [blame] | 161 | return ans_read_end(&d) != 0; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Alex Converse | af0c102 | 2016-12-16 10:33:11 -0800 | [diff] [blame] | 164 | class AbsTestFix : public ::testing::Test { |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 165 | protected: |
| 166 | static void SetUpTestCase() { pv_vec_ = abs_encode_build_vals(kNumBools); } |
| 167 | virtual void SetUp() { buf_ = new uint8_t[kNumBools / 8]; } |
| 168 | virtual void TearDown() { delete[] buf_; } |
| 169 | static const int kNumBools = 100000000; |
| 170 | static PvVec pv_vec_; |
| 171 | uint8_t *buf_; |
| 172 | }; |
Alex Converse | af0c102 | 2016-12-16 10:33:11 -0800 | [diff] [blame] | 173 | PvVec AbsTestFix::pv_vec_; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 174 | |
Alex Converse | af0c102 | 2016-12-16 10:33:11 -0800 | [diff] [blame] | 175 | class AnsTestFix : public ::testing::Test { |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 176 | protected: |
| 177 | static void SetUpTestCase() { |
Alex Converse | cba3d1f | 2016-09-09 10:30:36 -0700 | [diff] [blame] | 178 | sym_vec_ = ans_encode_build_vals(rans_sym_tab_, kNumSyms); |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 179 | } |
| 180 | virtual void SetUp() { buf_ = new uint8_t[kNumSyms / 2]; } |
| 181 | virtual void TearDown() { delete[] buf_; } |
| 182 | static const int kNumSyms = 25000000; |
| 183 | static std::vector<int> sym_vec_; |
Alex Converse | cba3d1f | 2016-09-09 10:30:36 -0700 | [diff] [blame] | 184 | static rans_sym rans_sym_tab_[kRansSymbols]; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 185 | uint8_t *buf_; |
| 186 | }; |
Alex Converse | af0c102 | 2016-12-16 10:33:11 -0800 | [diff] [blame] | 187 | std::vector<int> AnsTestFix::sym_vec_; |
| 188 | rans_sym AnsTestFix::rans_sym_tab_[kRansSymbols]; |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 189 | |
Alex Converse | c54692b | 2017-01-25 16:41:05 -0800 | [diff] [blame] | 190 | TEST_F(AbsTestFix, Rabs) { EXPECT_TRUE(check_rabs(pv_vec_, buf_)); } |
Alex Converse | af0c102 | 2016-12-16 10:33:11 -0800 | [diff] [blame] | 191 | TEST_F(AnsTestFix, Rans) { |
Alex Converse | cba3d1f | 2016-09-09 10:30:36 -0700 | [diff] [blame] | 192 | EXPECT_TRUE(check_rans(sym_vec_, rans_sym_tab_, buf_)); |
| 193 | } |
Alex Converse | af0c102 | 2016-12-16 10:33:11 -0800 | [diff] [blame] | 194 | TEST(AnsTest, FinalStateSerialization) { |
| 195 | for (unsigned i = L_BASE; i < L_BASE * IO_BASE; ++i) { |
| 196 | uint8_t buf[8]; |
| 197 | AnsCoder c; |
| 198 | ans_write_init(&c, buf); |
| 199 | c.state = i; |
| 200 | const int written_size = ans_write_end(&c); |
| 201 | ASSERT_LT(static_cast<size_t>(written_size), sizeof(buf)); |
| 202 | AnsDecoder d; |
| 203 | #if ANS_MAX_SYMBOLS |
| 204 | // There is no real data window here because no symbols are sent through |
| 205 | // ans (only synthetic states), so use a dummy value |
| 206 | d.window_size = 1024; |
| 207 | #endif |
| 208 | const int read_init_status = ans_read_init(&d, buf, written_size); |
| 209 | EXPECT_EQ(read_init_status, 0); |
| 210 | EXPECT_EQ(d.state, i); |
| 211 | } |
| 212 | } |
Alex Converse | 9ffcb46 | 2015-12-16 11:16:32 -0800 | [diff] [blame] | 213 | } // namespace |