blob: 4d9d7aaf44f93f550e3aacb3263349219e2ff048 [file] [log] [blame]
Ronald S. Bultjea742a732012-06-25 09:58:09 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Ronald S. Bultjea742a732012-06-25 09:58:09 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -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.
10*/
Ronald S. Bultjea742a732012-06-25 09:58:09 -070011
Daniel Kang26641c72012-06-28 16:26:31 -070012#include <math.h>
13#include <stdlib.h>
14#include <string.h>
15
Tom Finegan7a07ece2017-02-07 17:14:05 -080016#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
Daniel Kang26641c72012-06-28 16:26:31 -070017
Jingning Han097d59c2015-07-29 14:51:36 -070018#include "test/acm_random.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070019#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020#include "aom_dsp/bitreader.h"
21#include "aom_dsp/bitwriter.h"
Ronald S. Bultjea742a732012-06-25 09:58:09 -070022
Yaowu Xuc27fc142016-08-22 16:08:15 -070023using libaom_test::ACMRandom;
Daniel Kang26641c72012-06-28 16:26:31 -070024
Ronald S. Bultjea742a732012-06-25 09:58:09 -070025namespace {
26const int num_tests = 10;
Ronald S. Bultjea742a732012-06-25 09:58:09 -070027} // namespace
28
Yaowu Xuf883b422016-08-30 14:01:10 -070029TEST(AV1, TestBitIO) {
Ronald S. Bultjea742a732012-06-25 09:58:09 -070030 ACMRandom rnd(ACMRandom::DeterministicSeed());
31 for (int n = 0; n < num_tests; ++n) {
clang-format3a826f12016-08-11 17:46:05 -070032 for (int method = 0; method <= 7; ++method) { // we generate various proba
Yaowu Xuafffa3d2013-09-05 08:45:56 -070033 const int kBitsToTest = 1000;
34 uint8_t probas[kBitsToTest];
Ronald S. Bultjea742a732012-06-25 09:58:09 -070035
Yaowu Xuafffa3d2013-09-05 08:45:56 -070036 for (int i = 0; i < kBitsToTest; ++i) {
Ronald S. Bultjea742a732012-06-25 09:58:09 -070037 const int parity = i & 1;
clang-format3a826f12016-08-11 17:46:05 -070038 /* clang-format off */
Ronald S. Bultjea742a732012-06-25 09:58:09 -070039 probas[i] =
John Koleszarc6b90392012-07-13 15:21:29 -070040 (method == 0) ? 0 : (method == 1) ? 255 :
41 (method == 2) ? 128 :
42 (method == 3) ? rnd.Rand8() :
43 (method == 4) ? (parity ? 0 : 255) :
Ronald S. Bultjea742a732012-06-25 09:58:09 -070044 // alternate between low and high proba:
45 (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
46 (method == 6) ?
John Koleszarc6b90392012-07-13 15:21:29 -070047 (parity ? rnd(64) : 255 - rnd(64)) :
48 (parity ? rnd(32) : 255 - rnd(32));
clang-format3a826f12016-08-11 17:46:05 -070049 /* clang-format on */
Ronald S. Bultjea742a732012-06-25 09:58:09 -070050 }
51 for (int bit_method = 0; bit_method <= 3; ++bit_method) {
52 const int random_seed = 6432;
Yaowu Xuafffa3d2013-09-05 08:45:56 -070053 const int kBufferSize = 10000;
Ronald S. Bultjea742a732012-06-25 09:58:09 -070054 ACMRandom bit_rnd(random_seed);
Yaowu Xuf883b422016-08-30 14:01:10 -070055 aom_writer bw;
Yaowu Xuafffa3d2013-09-05 08:45:56 -070056 uint8_t bw_buffer[kBufferSize];
Yaowu Xuf883b422016-08-30 14:01:10 -070057 aom_start_encode(&bw, bw_buffer);
Ronald S. Bultjea742a732012-06-25 09:58:09 -070058
59 int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
Yaowu Xuafffa3d2013-09-05 08:45:56 -070060 for (int i = 0; i < kBitsToTest; ++i) {
Ronald S. Bultjea742a732012-06-25 09:58:09 -070061 if (bit_method == 2) {
62 bit = (i & 1);
63 } else if (bit_method == 3) {
64 bit = bit_rnd(2);
65 }
Yaowu Xuf883b422016-08-30 14:01:10 -070066 aom_write(&bw, bit, static_cast<int>(probas[i]));
Ronald S. Bultjea742a732012-06-25 09:58:09 -070067 }
68
Yaowu Xuf883b422016-08-30 14:01:10 -070069 aom_stop_encode(&bw);
Ronald S. Bultjea742a732012-06-25 09:58:09 -070070
Nathan E. Eggee58781d2016-10-11 17:46:03 -040071#if !CONFIG_DAALA_EC
John Koleszara425e2c2013-06-06 18:03:44 -070072 // First bit should be zero
73 GTEST_ASSERT_EQ(bw_buffer[0] & 0x80, 0);
Nathan E. Eggee58781d2016-10-11 17:46:03 -040074#endif
John Koleszara425e2c2013-06-06 18:03:44 -070075
Yaowu Xuf883b422016-08-30 14:01:10 -070076 aom_reader br;
Nathan E. Eggee58781d2016-10-11 17:46:03 -040077 aom_reader_init(&br, bw_buffer, bw.pos, NULL, NULL);
Ronald S. Bultjea742a732012-06-25 09:58:09 -070078 bit_rnd.Reset(random_seed);
Yaowu Xuafffa3d2013-09-05 08:45:56 -070079 for (int i = 0; i < kBitsToTest; ++i) {
Ronald S. Bultjea742a732012-06-25 09:58:09 -070080 if (bit_method == 2) {
81 bit = (i & 1);
82 } else if (bit_method == 3) {
83 bit = bit_rnd(2);
84 }
Michael Bebenita6048d052016-08-25 14:40:54 -070085 GTEST_ASSERT_EQ(aom_read(&br, probas[i], NULL), bit)
Yaowu Xuafffa3d2013-09-05 08:45:56 -070086 << "pos: " << i << " / " << kBitsToTest
clang-format3a826f12016-08-11 17:46:05 -070087 << " bit_method: " << bit_method << " method: " << method;
Ronald S. Bultjea742a732012-06-25 09:58:09 -070088 }
89 }
90 }
91 }
92}
Michael Bebenitad7baf452016-08-25 11:27:56 -070093
94#if CONFIG_DAALA_EC
95#define FRAC_DIFF_TOTAL_ERROR 0.07
96#else
97#define FRAC_DIFF_TOTAL_ERROR 0.2
98#endif
99
100TEST(AV1, TestTell) {
101 const int kBufferSize = 10000;
102 aom_writer bw;
103 uint8_t bw_buffer[kBufferSize];
104 const int kSymbols = 1024;
105 // Coders are noisier at low probabilities, so we start at p = 4.
Yushin Cho3e4fcb42016-11-04 15:39:55 -0700106 for (int p = 4; p < 256; p++) {
Michael Bebenitad7baf452016-08-25 11:27:56 -0700107 double probability = p / 256.;
108 aom_start_encode(&bw, bw_buffer);
109 for (int i = 0; i < kSymbols; i++) {
110 aom_write(&bw, 0, p);
111 }
112 aom_stop_encode(&bw);
113 aom_reader br;
Tristan Matthews4891ef92016-11-04 20:59:52 -0400114 aom_reader_init(&br, bw_buffer, bw.pos, NULL, NULL);
Nathan E. Eggeb244f392016-09-06 23:48:43 -0400115 uint32_t last_tell = aom_reader_tell(&br);
116 uint32_t last_tell_frac = aom_reader_tell_frac(&br);
Michael Bebenitad7baf452016-08-25 11:27:56 -0700117 double frac_diff_total = 0;
Nathan E. Eggeb244f392016-09-06 23:48:43 -0400118 GTEST_ASSERT_GE(aom_reader_tell(&br), 0u);
119 GTEST_ASSERT_LE(aom_reader_tell(&br), 1u);
Michael Bebenitad7baf452016-08-25 11:27:56 -0700120 for (int i = 0; i < kSymbols; i++) {
Michael Bebenita6048d052016-08-25 14:40:54 -0700121 aom_read(&br, p, NULL);
Nathan E. Eggeb244f392016-09-06 23:48:43 -0400122 uint32_t tell = aom_reader_tell(&br);
123 uint32_t tell_frac = aom_reader_tell_frac(&br);
Michael Bebenitad7baf452016-08-25 11:27:56 -0700124 GTEST_ASSERT_GE(tell, last_tell) << "tell: " << tell
125 << ", last_tell: " << last_tell;
126 GTEST_ASSERT_GE(tell_frac, last_tell_frac)
127 << "tell_frac: " << tell_frac
128 << ", last_tell_frac: " << last_tell_frac;
129 // Frac tell should round up to tell.
130 GTEST_ASSERT_EQ(tell, (tell_frac + 7) >> 3);
131 last_tell = tell;
132 frac_diff_total +=
133 fabs(((tell_frac - last_tell_frac) / 8.0) + log2(probability));
134 last_tell_frac = tell_frac;
135 }
Nathan E. Eggeb244f392016-09-06 23:48:43 -0400136 const uint32_t expected = (uint32_t)(-kSymbols * log2(probability));
Michael Bebenitad7baf452016-08-25 11:27:56 -0700137 // Last tell should be close to the expected value.
Peter de Rivazf9948552016-10-20 16:21:20 +0100138 GTEST_ASSERT_LE(last_tell, expected + 20) << " last_tell: " << last_tell;
Michael Bebenitad7baf452016-08-25 11:27:56 -0700139 // The average frac_diff error should be pretty small.
140 GTEST_ASSERT_LE(frac_diff_total / kSymbols, FRAC_DIFF_TOTAL_ERROR)
141 << " frac_diff_total: " << frac_diff_total;
142 }
143}