ans_test.cc 5.35 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
11
12
13
14
15
16
17
18
19
20
 */

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <ctime>
#include <utility>
#include <vector>

#include "third_party/googletest/src/include/gtest/gtest.h"

21
#include "test/acm_random.h"
22
23
#include "aom_dsp/ansreader.h"
#include "aom_dsp/answriter.h"
24

25
26
27
namespace {
typedef std::vector<std::pair<uint8_t, bool> > PvVec;

28
29
const int kPrintStats = 0;

30
31
PvVec abs_encode_build_vals(int iters) {
  PvVec ret;
32
  libaom_test::ACMRandom gen(0x30317076);
33
34
35
36
37
38
39
40
  double entropy = 0;
  for (int i = 0; i < iters; ++i) {
    uint8_t p;
    do {
      p = gen.Rand8();
    } while (p == 0);  // zero is not a valid coding probability
    bool b = gen.Rand8() < p;
    ret.push_back(std::make_pair(static_cast<uint8_t>(p), b));
41
42
43
44
    if (kPrintStats) {
      double d = p / 256.;
      entropy += -d * log2(d) - (1 - d) * log2(1 - d);
    }
45
  }
46
  if (kPrintStats) printf("entropy %f\n", entropy);
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  return ret;
}

bool check_uabs(const PvVec &pv_vec, uint8_t *buf) {
  AnsCoder a;
  ans_write_init(&a, buf);

  std::clock_t start = std::clock();
  for (PvVec::const_reverse_iterator it = pv_vec.rbegin(); it != pv_vec.rend();
       ++it) {
    uabs_write(&a, it->second, 256 - it->first);
  }
  std::clock_t enc_time = std::clock() - start;
  int offset = ans_write_end(&a);
  bool okay = true;
  AnsDecoder d;
  if (ans_read_init(&d, buf, offset)) return false;
  start = std::clock();
  for (PvVec::const_iterator it = pv_vec.begin(); it != pv_vec.end(); ++it) {
    okay &= uabs_read(&d, 256 - it->first) == it->second;
  }
  std::clock_t dec_time = std::clock() - start;
  if (!okay) return false;
70
71
72
73
  if (kPrintStats)
    printf("uABS size %d enc_time %f dec_time %f\n", offset,
           static_cast<float>(enc_time) / CLOCKS_PER_SEC,
           static_cast<float>(dec_time) / CLOCKS_PER_SEC);
74
75
76
  return ans_read_end(&d);
}

77
78
const aom_cdf_prob spareto65[] = { 8320, 6018, 4402, 3254, 4259,
                                   3919, 2057, 492,  45,   2 };
79

Alex Converse's avatar
Alex Converse committed
80
const int kRansSymbols =
81
    static_cast<int>(sizeof(spareto65) / sizeof(spareto65[0]));
82

83
84
85
86
87
88
89
std::vector<int> ans_encode_build_vals(rans_sym *const tab, int iters) {
  aom_cdf_prob sum = 0;
  for (int i = 0; i < kRansSymbols; ++i) {
    tab[i].cum_prob = sum;
    tab[i].prob = spareto65[i];
    sum += spareto65[i];
  }
90
  std::vector<int> p_to_sym;
Alex Converse's avatar
Alex Converse committed
91
  for (int i = 0; i < kRansSymbols; ++i) {
92
93
    p_to_sym.insert(p_to_sym.end(), tab[i].prob, i);
  }
94
  assert(p_to_sym.size() == RANS_PRECISION);
95
  std::vector<int> ret;
96
  libaom_test::ACMRandom gen(18543637);
97
  for (int i = 0; i < iters; ++i) {
98
99
    int sym =
        p_to_sym[((gen.Rand8() << 8) + gen.Rand8()) & (RANS_PRECISION - 1)];
100
101
102
103
104
    ret.push_back(sym);
  }
  return ret;
}

Alex Converse's avatar
Alex Converse committed
105
106
void rans_build_dec_tab(const struct rans_sym sym_tab[],
                        aom_cdf_prob *dec_tab) {
107
108
109
  unsigned int sum = 0;
  for (int i = 0; sum < RANS_PRECISION; ++i) {
    dec_tab[i] = sum += sym_tab[i].prob;
110
111
112
113
114
115
116
  }
}

bool check_rans(const std::vector<int> &sym_vec, const rans_sym *const tab,
                uint8_t *buf) {
  AnsCoder a;
  ans_write_init(&a, buf);
Alex Converse's avatar
Alex Converse committed
117
  aom_cdf_prob dec_tab[kRansSymbols];
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  rans_build_dec_tab(tab, dec_tab);

  std::clock_t start = std::clock();
  for (std::vector<int>::const_reverse_iterator it = sym_vec.rbegin();
       it != sym_vec.rend(); ++it) {
    rans_write(&a, &tab[*it]);
  }
  std::clock_t enc_time = std::clock() - start;
  int offset = ans_write_end(&a);
  bool okay = true;
  AnsDecoder d;
  if (ans_read_init(&d, buf, offset)) return false;
  start = std::clock();
  for (std::vector<int>::const_iterator it = sym_vec.begin();
       it != sym_vec.end(); ++it) {
    okay &= rans_read(&d, dec_tab) == *it;
  }
  std::clock_t dec_time = std::clock() - start;
  if (!okay) return false;
137
138
139
140
  if (kPrintStats)
    printf("rANS size %d enc_time %f dec_time %f\n", offset,
           static_cast<float>(enc_time) / CLOCKS_PER_SEC,
           static_cast<float>(dec_time) / CLOCKS_PER_SEC);
141
142
143
  return ans_read_end(&d);
}

144
class AbsTest : public ::testing::Test {
145
146
147
148
149
150
151
152
 protected:
  static void SetUpTestCase() { pv_vec_ = abs_encode_build_vals(kNumBools); }
  virtual void SetUp() { buf_ = new uint8_t[kNumBools / 8]; }
  virtual void TearDown() { delete[] buf_; }
  static const int kNumBools = 100000000;
  static PvVec pv_vec_;
  uint8_t *buf_;
};
153
PvVec AbsTest::pv_vec_;
154

155
class AnsTest : public ::testing::Test {
156
157
 protected:
  static void SetUpTestCase() {
158
    sym_vec_ = ans_encode_build_vals(rans_sym_tab_, kNumSyms);
159
160
161
162
163
  }
  virtual void SetUp() { buf_ = new uint8_t[kNumSyms / 2]; }
  virtual void TearDown() { delete[] buf_; }
  static const int kNumSyms = 25000000;
  static std::vector<int> sym_vec_;
164
  static rans_sym rans_sym_tab_[kRansSymbols];
165
166
  uint8_t *buf_;
};
167
std::vector<int> AnsTest::sym_vec_;
168
rans_sym AnsTest::rans_sym_tab_[kRansSymbols];
169

170
TEST_F(AbsTest, Uabs) { EXPECT_TRUE(check_uabs(pv_vec_, buf_)); }
171
172
173
TEST_F(AnsTest, Rans) {
  EXPECT_TRUE(check_rans(sym_vec_, rans_sym_tab_, buf_));
}
174
}  // namespace