blob: 0e1c0a667374206ceb71b608f42918865f5fc9b6 [file] [log] [blame]
Alex Converse9ffcb462015-12-16 11:16:32 -08001/*
Alex Converse5d33cc42016-09-28 10:58:19 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Alex Converse9ffcb462015-12-16 11:16:32 -08003 *
Alex Converse5d33cc42016-09-28 10:58:19 -07004 * 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 Converse9ffcb462015-12-16 11:16:32 -080010 */
11
12#include <assert.h>
13#include <math.h>
14#include <stdio.h>
15#include <ctime>
16#include <utility>
17#include <vector>
18
19#include "third_party/googletest/src/include/gtest/gtest.h"
20
Alex Converse080a2cc2016-09-20 16:39:01 -070021#include "test/acm_random.h"
Alex Converse7fe2ae82016-09-28 11:33:20 -070022#include "aom_dsp/ansreader.h"
Alex Conversecfa83fd2016-10-26 16:31:48 -070023#include "aom_dsp/buf_ans.h"
Alex Converse5d33cc42016-09-28 10:58:19 -070024
Alex Converse9ffcb462015-12-16 11:16:32 -080025namespace {
26typedef std::vector<std::pair<uint8_t, bool> > PvVec;
27
Alex Converse5d33cc42016-09-28 10:58:19 -070028const int kPrintStats = 0;
Alex Converseeb780e72016-12-13 12:46:41 -080029// Use a small buffer size to exercise ANS window spills or buffer growth
30const int kBufAnsSize = 1 << 8;
Alex Converse5d33cc42016-09-28 10:58:19 -070031
Alex Converse9ffcb462015-12-16 11:16:32 -080032PvVec abs_encode_build_vals(int iters) {
33 PvVec ret;
Yaowu Xuc27fc142016-08-22 16:08:15 -070034 libaom_test::ACMRandom gen(0x30317076);
Alex Converse9ffcb462015-12-16 11:16:32 -080035 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 Converse5d33cc42016-09-28 10:58:19 -070043 if (kPrintStats) {
44 double d = p / 256.;
45 entropy += -d * log2(d) - (1 - d) * log2(1 - d);
46 }
Alex Converse9ffcb462015-12-16 11:16:32 -080047 }
Alex Converse5d33cc42016-09-28 10:58:19 -070048 if (kPrintStats) printf("entropy %f\n", entropy);
Alex Converse9ffcb462015-12-16 11:16:32 -080049 return ret;
50}
51
Alex Converse9ffcb462015-12-16 11:16:32 -080052bool check_uabs(const PvVec &pv_vec, uint8_t *buf) {
Alex Conversecfa83fd2016-10-26 16:31:48 -070053 BufAnsCoder a;
Alex Converse2cdf0d82016-12-13 13:53:09 -080054 aom_buf_ans_alloc(&a, NULL, kBufAnsSize);
Alex Conversecfa83fd2016-10-26 16:31:48 -070055 buf_ans_write_init(&a, buf);
Alex Converse9ffcb462015-12-16 11:16:32 -080056
57 std::clock_t start = std::clock();
Alex Conversecfa83fd2016-10-26 16:31:48 -070058 for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) {
59 buf_uabs_write(&a, it->second, 256 - it->first);
Alex Converse9ffcb462015-12-16 11:16:32 -080060 }
Alex Converse1ecdf2b2016-11-30 15:51:12 -080061 aom_buf_ans_flush(&a);
Alex Converse9ffcb462015-12-16 11:16:32 -080062 std::clock_t enc_time = std::clock() - start;
Alex Conversecfa83fd2016-10-26 16:31:48 -070063 int offset = buf_ans_write_end(&a);
64 aom_buf_ans_free(&a);
Alex Converse9ffcb462015-12-16 11:16:32 -080065 bool okay = true;
66 AnsDecoder d;
Alex Converse2cdf0d82016-12-13 13:53:09 -080067#if ANS_MAX_SYMBOLS
Alex Converse346440b2017-01-03 13:47:37 -080068 d.window_size = kBufAnsSize;
Alex Converse2cdf0d82016-12-13 13:53:09 -080069#endif
Alex Converse346440b2017-01-03 13:47:37 -080070 if (ans_read_init(&d, buf, offset)) return false;
Alex Converse9ffcb462015-12-16 11:16:32 -080071 start = std::clock();
72 for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) {
Yaowu Xu1e487c32016-11-10 15:54:07 -080073 okay = okay && (uabs_read(&d, 256 - it->first) != 0) == it->second;
Alex Converse9ffcb462015-12-16 11:16:32 -080074 }
75 std::clock_t dec_time = std::clock() - start;
76 if (!okay) return false;
Alex Converse5d33cc42016-09-28 10:58:19 -070077 if (kPrintStats)
78 printf("uABS size %d enc_time %f dec_time %f\n", offset,
79 static_cast<float>(enc_time) / CLOCKS_PER_SEC,
80 static_cast<float>(dec_time) / CLOCKS_PER_SEC);
Yaowu Xu1e487c32016-11-10 15:54:07 -080081 return ans_read_end(&d) != 0;
Alex Converse9ffcb462015-12-16 11:16:32 -080082}
83
Alex Converse62a94a62016-09-09 10:51:48 -070084const aom_cdf_prob spareto65[] = { 8320, 6018, 4402, 3254, 4259,
85 3919, 2057, 492, 45, 2 };
Alex Converse9ffcb462015-12-16 11:16:32 -080086
Alex Conversecba3d1f2016-09-09 10:30:36 -070087const int kRansSymbols =
88 static_cast<int>(sizeof(spareto65) / sizeof(spareto65[0]));
89
90std::vector<int> ans_encode_build_vals(rans_sym *const tab, int iters) {
91 aom_cdf_prob sum = 0;
92 for (int i = 0; i < kRansSymbols; ++i) {
93 tab[i].cum_prob = sum;
94 tab[i].prob = spareto65[i];
95 sum += spareto65[i];
96 }
Alex Converse9ffcb462015-12-16 11:16:32 -080097 std::vector<int> p_to_sym;
Alex Converse9ed1a2f2016-09-04 13:30:43 +020098 for (int i = 0; i < kRansSymbols; ++i) {
Alex Converse9ffcb462015-12-16 11:16:32 -080099 p_to_sym.insert(p_to_sym.end(), tab[i].prob, i);
Alex Converse9ffcb462015-12-16 11:16:32 -0800100 }
Alex Converse5d33cc42016-09-28 10:58:19 -0700101 assert(p_to_sym.size() == RANS_PRECISION);
Alex Converse9ffcb462015-12-16 11:16:32 -0800102 std::vector<int> ret;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700103 libaom_test::ACMRandom gen(18543637);
Alex Converse9ffcb462015-12-16 11:16:32 -0800104 for (int i = 0; i < iters; ++i) {
Alex Converse62a94a62016-09-09 10:51:48 -0700105 int sym =
106 p_to_sym[((gen.Rand8() << 8) + gen.Rand8()) & (RANS_PRECISION - 1)];
Alex Converse9ffcb462015-12-16 11:16:32 -0800107 ret.push_back(sym);
108 }
109 return ret;
110}
111
Alex Converse9ed1a2f2016-09-04 13:30:43 +0200112void rans_build_dec_tab(const struct rans_sym sym_tab[],
113 aom_cdf_prob *dec_tab) {
Alex Conversee9f70f82016-08-22 12:44:16 -0700114 unsigned int sum = 0;
115 for (int i = 0; sum < RANS_PRECISION; ++i) {
116 dec_tab[i] = sum += sym_tab[i].prob;
Alex Converse9ffcb462015-12-16 11:16:32 -0800117 }
118}
119
120bool check_rans(const std::vector<int> &sym_vec, const rans_sym *const tab,
121 uint8_t *buf) {
Alex Conversecfa83fd2016-10-26 16:31:48 -0700122 BufAnsCoder a;
Alex Converse2cdf0d82016-12-13 13:53:09 -0800123 aom_buf_ans_alloc(&a, NULL, kBufAnsSize);
Alex Conversecfa83fd2016-10-26 16:31:48 -0700124 buf_ans_write_init(&a, buf);
Alex Converse9ed1a2f2016-09-04 13:30:43 +0200125 aom_cdf_prob dec_tab[kRansSymbols];
Alex Converse9ffcb462015-12-16 11:16:32 -0800126 rans_build_dec_tab(tab, dec_tab);
127
128 std::clock_t start = std::clock();
Alex Conversecfa83fd2016-10-26 16:31:48 -0700129 for (std::vector<int>::const_iterator it = sym_vec.begin();
130 it != sym_vec.end(); ++it) {
131 buf_rans_write(&a, &tab[*it]);
Alex Converse9ffcb462015-12-16 11:16:32 -0800132 }
Alex Converse1ecdf2b2016-11-30 15:51:12 -0800133 aom_buf_ans_flush(&a);
Alex Converse9ffcb462015-12-16 11:16:32 -0800134 std::clock_t enc_time = std::clock() - start;
Alex Conversecfa83fd2016-10-26 16:31:48 -0700135 int offset = buf_ans_write_end(&a);
136 aom_buf_ans_free(&a);
Alex Converse9ffcb462015-12-16 11:16:32 -0800137 bool okay = true;
138 AnsDecoder d;
Alex Converse2cdf0d82016-12-13 13:53:09 -0800139#if ANS_MAX_SYMBOLS
Alex Converse346440b2017-01-03 13:47:37 -0800140 d.window_size = kBufAnsSize;
Alex Converse2cdf0d82016-12-13 13:53:09 -0800141#endif
Alex Converse346440b2017-01-03 13:47:37 -0800142 if (ans_read_init(&d, buf, offset)) return false;
Alex Converse9ffcb462015-12-16 11:16:32 -0800143 start = std::clock();
144 for (std::vector<int>::const_iterator it = sym_vec.begin();
145 it != sym_vec.end(); ++it) {
146 okay &= rans_read(&d, dec_tab) == *it;
147 }
148 std::clock_t dec_time = std::clock() - start;
149 if (!okay) return false;
Alex Converse5d33cc42016-09-28 10:58:19 -0700150 if (kPrintStats)
151 printf("rANS size %d enc_time %f dec_time %f\n", offset,
152 static_cast<float>(enc_time) / CLOCKS_PER_SEC,
153 static_cast<float>(dec_time) / CLOCKS_PER_SEC);
Yaowu Xu1e487c32016-11-10 15:54:07 -0800154 return ans_read_end(&d) != 0;
Alex Converse9ffcb462015-12-16 11:16:32 -0800155}
156
Alex Converse0ad82c62016-09-26 12:49:00 -0700157class AbsTest : public ::testing::Test {
Alex Converse9ffcb462015-12-16 11:16:32 -0800158 protected:
159 static void SetUpTestCase() { pv_vec_ = abs_encode_build_vals(kNumBools); }
160 virtual void SetUp() { buf_ = new uint8_t[kNumBools / 8]; }
161 virtual void TearDown() { delete[] buf_; }
162 static const int kNumBools = 100000000;
163 static PvVec pv_vec_;
164 uint8_t *buf_;
165};
Alex Converse0ad82c62016-09-26 12:49:00 -0700166PvVec AbsTest::pv_vec_;
Alex Converse9ffcb462015-12-16 11:16:32 -0800167
Alex Converse0ad82c62016-09-26 12:49:00 -0700168class AnsTest : public ::testing::Test {
Alex Converse9ffcb462015-12-16 11:16:32 -0800169 protected:
170 static void SetUpTestCase() {
Alex Conversecba3d1f2016-09-09 10:30:36 -0700171 sym_vec_ = ans_encode_build_vals(rans_sym_tab_, kNumSyms);
Alex Converse9ffcb462015-12-16 11:16:32 -0800172 }
173 virtual void SetUp() { buf_ = new uint8_t[kNumSyms / 2]; }
174 virtual void TearDown() { delete[] buf_; }
175 static const int kNumSyms = 25000000;
176 static std::vector<int> sym_vec_;
Alex Conversecba3d1f2016-09-09 10:30:36 -0700177 static rans_sym rans_sym_tab_[kRansSymbols];
Alex Converse9ffcb462015-12-16 11:16:32 -0800178 uint8_t *buf_;
179};
Alex Converse0ad82c62016-09-26 12:49:00 -0700180std::vector<int> AnsTest::sym_vec_;
Alex Conversecba3d1f2016-09-09 10:30:36 -0700181rans_sym AnsTest::rans_sym_tab_[kRansSymbols];
Alex Converse9ffcb462015-12-16 11:16:32 -0800182
Alex Converse0ad82c62016-09-26 12:49:00 -0700183TEST_F(AbsTest, Uabs) { EXPECT_TRUE(check_uabs(pv_vec_, buf_)); }
Alex Conversecba3d1f2016-09-09 10:30:36 -0700184TEST_F(AnsTest, Rans) {
185 EXPECT_TRUE(check_rans(sym_vec_, rans_sym_tab_, buf_));
186}
Alex Converse9ffcb462015-12-16 11:16:32 -0800187} // namespace