blob: fd460f409d0398ab5754efdfe98991ae4c924fc3 [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
Tom Finegan7a07ece2017-02-07 17:14:05 -080019#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
Alex Converse9ffcb462015-12-16 11:16:32 -080020
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 Conversec54692b2017-01-25 16:41:05 -080052bool check_rabs(const PvVec &pv_vec, uint8_t *buf) {
Alex Conversecfa83fd2016-10-26 16:31:48 -070053 BufAnsCoder a;
Alex Converse30f0e152017-03-28 10:13:27 -070054 a.size = kBufAnsSize;
55 aom_buf_ans_alloc(&a, NULL);
Alex Conversecfa83fd2016-10-26 16:31:48 -070056 buf_ans_write_init(&a, buf);
Alex Converse9ffcb462015-12-16 11:16:32 -080057
58 std::clock_t start = std::clock();
Alex Conversecfa83fd2016-10-26 16:31:48 -070059 for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) {
Alex Conversec54692b2017-01-25 16:41:05 -080060 buf_rabs_write(&a, it->second, 256 - it->first);
Alex Converse9ffcb462015-12-16 11:16:32 -080061 }
Alex Converse1ecdf2b2016-11-30 15:51:12 -080062 aom_buf_ans_flush(&a);
Alex Converse9ffcb462015-12-16 11:16:32 -080063 std::clock_t enc_time = std::clock() - start;
Alex Conversecfa83fd2016-10-26 16:31:48 -070064 int offset = buf_ans_write_end(&a);
65 aom_buf_ans_free(&a);
Alex Converse9ffcb462015-12-16 11:16:32 -080066 bool okay = true;
67 AnsDecoder d;
Alex Converse2cdf0d82016-12-13 13:53:09 -080068#if ANS_MAX_SYMBOLS
Alex Converse346440b2017-01-03 13:47:37 -080069 d.window_size = kBufAnsSize;
Alex Converse2cdf0d82016-12-13 13:53:09 -080070#endif
Alex Converse346440b2017-01-03 13:47:37 -080071 if (ans_read_init(&d, buf, offset)) return false;
Alex Converse9ffcb462015-12-16 11:16:32 -080072 start = std::clock();
73 for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) {
Alex Conversec54692b2017-01-25 16:41:05 -080074 okay = okay && (rabs_read(&d, 256 - it->first) != 0) == it->second;
Alex Converse9ffcb462015-12-16 11:16:32 -080075 }
76 std::clock_t dec_time = std::clock() - start;
77 if (!okay) return false;
Alex Converse5d33cc42016-09-28 10:58:19 -070078 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 Xu1e487c32016-11-10 15:54:07 -080082 return ans_read_end(&d) != 0;
Alex Converse9ffcb462015-12-16 11:16:32 -080083}
84
Alex Converse62a94a62016-09-09 10:51:48 -070085const aom_cdf_prob spareto65[] = { 8320, 6018, 4402, 3254, 4259,
86 3919, 2057, 492, 45, 2 };
Alex Converse9ffcb462015-12-16 11:16:32 -080087
Alex Conversecba3d1f2016-09-09 10:30:36 -070088const int kRansSymbols =
89 static_cast<int>(sizeof(spareto65) / sizeof(spareto65[0]));
90
Alex Converse8c687d22017-02-07 15:55:24 -080091struct rans_sym {
92 aom_cdf_prob prob;
93 aom_cdf_prob cum_prob; // not-inclusive
94};
95
Alex Conversecba3d1f2016-09-09 10:30:36 -070096std::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 Converse9ffcb462015-12-16 11:16:32 -0800103 std::vector<int> p_to_sym;
Alex Converse9ed1a2f2016-09-04 13:30:43 +0200104 for (int i = 0; i < kRansSymbols; ++i) {
Alex Converse9ffcb462015-12-16 11:16:32 -0800105 p_to_sym.insert(p_to_sym.end(), tab[i].prob, i);
Alex Converse9ffcb462015-12-16 11:16:32 -0800106 }
Alex Converse5d33cc42016-09-28 10:58:19 -0700107 assert(p_to_sym.size() == RANS_PRECISION);
Alex Converse9ffcb462015-12-16 11:16:32 -0800108 std::vector<int> ret;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700109 libaom_test::ACMRandom gen(18543637);
Alex Converse9ffcb462015-12-16 11:16:32 -0800110 for (int i = 0; i < iters; ++i) {
Alex Converse62a94a62016-09-09 10:51:48 -0700111 int sym =
112 p_to_sym[((gen.Rand8() << 8) + gen.Rand8()) & (RANS_PRECISION - 1)];
Alex Converse9ffcb462015-12-16 11:16:32 -0800113 ret.push_back(sym);
114 }
115 return ret;
116}
117
Alex Converse9ed1a2f2016-09-04 13:30:43 +0200118void rans_build_dec_tab(const struct rans_sym sym_tab[],
119 aom_cdf_prob *dec_tab) {
Alex Conversee9f70f82016-08-22 12:44:16 -0700120 unsigned int sum = 0;
121 for (int i = 0; sum < RANS_PRECISION; ++i) {
122 dec_tab[i] = sum += sym_tab[i].prob;
Alex Converse9ffcb462015-12-16 11:16:32 -0800123 }
124}
125
126bool check_rans(const std::vector<int> &sym_vec, const rans_sym *const tab,
127 uint8_t *buf) {
Alex Conversecfa83fd2016-10-26 16:31:48 -0700128 BufAnsCoder a;
Alex Converse30f0e152017-03-28 10:13:27 -0700129 a.size = kBufAnsSize;
130 aom_buf_ans_alloc(&a, NULL);
Alex Conversecfa83fd2016-10-26 16:31:48 -0700131 buf_ans_write_init(&a, buf);
Alex Converse9ed1a2f2016-09-04 13:30:43 +0200132 aom_cdf_prob dec_tab[kRansSymbols];
Alex Converse9ffcb462015-12-16 11:16:32 -0800133 rans_build_dec_tab(tab, dec_tab);
134
135 std::clock_t start = std::clock();
Alex Conversecfa83fd2016-10-26 16:31:48 -0700136 for (std::vector<int>::const_iterator it = sym_vec.begin();
137 it != sym_vec.end(); ++it) {
Alex Converse8c687d22017-02-07 15:55:24 -0800138 buf_rans_write(&a, tab[*it].cum_prob, tab[*it].prob);
Alex Converse9ffcb462015-12-16 11:16:32 -0800139 }
Alex Converse1ecdf2b2016-11-30 15:51:12 -0800140 aom_buf_ans_flush(&a);
Alex Converse9ffcb462015-12-16 11:16:32 -0800141 std::clock_t enc_time = std::clock() - start;
Alex Conversecfa83fd2016-10-26 16:31:48 -0700142 int offset = buf_ans_write_end(&a);
143 aom_buf_ans_free(&a);
Alex Converse9ffcb462015-12-16 11:16:32 -0800144 bool okay = true;
145 AnsDecoder d;
Alex Converse2cdf0d82016-12-13 13:53:09 -0800146#if ANS_MAX_SYMBOLS
Alex Converse346440b2017-01-03 13:47:37 -0800147 d.window_size = kBufAnsSize;
Alex Converse2cdf0d82016-12-13 13:53:09 -0800148#endif
Alex Converse346440b2017-01-03 13:47:37 -0800149 if (ans_read_init(&d, buf, offset)) return false;
Alex Converse9ffcb462015-12-16 11:16:32 -0800150 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 Converse5d33cc42016-09-28 10:58:19 -0700157 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 Xu1e487c32016-11-10 15:54:07 -0800161 return ans_read_end(&d) != 0;
Alex Converse9ffcb462015-12-16 11:16:32 -0800162}
163
Alex Converseaf0c1022016-12-16 10:33:11 -0800164class AbsTestFix : public ::testing::Test {
Alex Converse9ffcb462015-12-16 11:16:32 -0800165 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 Converseaf0c1022016-12-16 10:33:11 -0800173PvVec AbsTestFix::pv_vec_;
Alex Converse9ffcb462015-12-16 11:16:32 -0800174
Alex Converseaf0c1022016-12-16 10:33:11 -0800175class AnsTestFix : public ::testing::Test {
Alex Converse9ffcb462015-12-16 11:16:32 -0800176 protected:
177 static void SetUpTestCase() {
Alex Conversecba3d1f2016-09-09 10:30:36 -0700178 sym_vec_ = ans_encode_build_vals(rans_sym_tab_, kNumSyms);
Alex Converse9ffcb462015-12-16 11:16:32 -0800179 }
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 Conversecba3d1f2016-09-09 10:30:36 -0700184 static rans_sym rans_sym_tab_[kRansSymbols];
Alex Converse9ffcb462015-12-16 11:16:32 -0800185 uint8_t *buf_;
186};
Alex Converseaf0c1022016-12-16 10:33:11 -0800187std::vector<int> AnsTestFix::sym_vec_;
188rans_sym AnsTestFix::rans_sym_tab_[kRansSymbols];
Alex Converse9ffcb462015-12-16 11:16:32 -0800189
Alex Conversec54692b2017-01-25 16:41:05 -0800190TEST_F(AbsTestFix, Rabs) { EXPECT_TRUE(check_rabs(pv_vec_, buf_)); }
Alex Converseaf0c1022016-12-16 10:33:11 -0800191TEST_F(AnsTestFix, Rans) {
Alex Conversecba3d1f2016-09-09 10:30:36 -0700192 EXPECT_TRUE(check_rans(sym_vec_, rans_sym_tab_, buf_));
193}
Alex Converseaf0c1022016-12-16 10:33:11 -0800194TEST(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 Converse9ffcb462015-12-16 11:16:32 -0800213} // namespace