2012-06-25 20:58:09 +04:00
|
|
|
/*
|
2016-09-02 22:04:54 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2012-06-25 20:58:09 +04:00
|
|
|
*
|
2016-09-02 22:04:54 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2012-06-25 20:58:09 +04:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "third_party/googletest/src/include/gtest/gtest.h"
|
|
|
|
|
2015-07-30 00:51:36 +03:00
|
|
|
#include "test/acm_random.h"
|
2016-08-31 00:01:10 +03:00
|
|
|
#include "aom/aom_integer.h"
|
2016-08-23 02:08:15 +03:00
|
|
|
#include "aom_dsp/bitreader.h"
|
|
|
|
#include "aom_dsp/bitwriter.h"
|
2012-06-25 20:58:09 +04:00
|
|
|
|
2016-08-23 02:08:15 +03:00
|
|
|
using libaom_test::ACMRandom;
|
2012-06-25 20:58:09 +04:00
|
|
|
|
2012-06-29 03:26:31 +04:00
|
|
|
namespace {
|
|
|
|
const int num_tests = 10;
|
2012-06-25 20:58:09 +04:00
|
|
|
} // namespace
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
TEST(AV1, TestBitIO) {
|
2012-06-25 20:58:09 +04:00
|
|
|
ACMRandom rnd(ACMRandom::DeterministicSeed());
|
|
|
|
for (int n = 0; n < num_tests; ++n) {
|
2016-08-12 03:46:05 +03:00
|
|
|
for (int method = 0; method <= 7; ++method) { // we generate various proba
|
2013-09-05 19:45:56 +04:00
|
|
|
const int kBitsToTest = 1000;
|
|
|
|
uint8_t probas[kBitsToTest];
|
2012-06-25 20:58:09 +04:00
|
|
|
|
2013-09-05 19:45:56 +04:00
|
|
|
for (int i = 0; i < kBitsToTest; ++i) {
|
2012-06-25 20:58:09 +04:00
|
|
|
const int parity = i & 1;
|
2016-08-12 03:46:05 +03:00
|
|
|
/* clang-format off */
|
2012-06-25 20:58:09 +04:00
|
|
|
probas[i] =
|
2012-07-14 02:21:29 +04:00
|
|
|
(method == 0) ? 0 : (method == 1) ? 255 :
|
|
|
|
(method == 2) ? 128 :
|
|
|
|
(method == 3) ? rnd.Rand8() :
|
|
|
|
(method == 4) ? (parity ? 0 : 255) :
|
2012-06-25 20:58:09 +04:00
|
|
|
// alternate between low and high proba:
|
|
|
|
(method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
|
|
|
|
(method == 6) ?
|
2012-07-14 02:21:29 +04:00
|
|
|
(parity ? rnd(64) : 255 - rnd(64)) :
|
|
|
|
(parity ? rnd(32) : 255 - rnd(32));
|
2016-08-12 03:46:05 +03:00
|
|
|
/* clang-format on */
|
2012-06-25 20:58:09 +04:00
|
|
|
}
|
|
|
|
for (int bit_method = 0; bit_method <= 3; ++bit_method) {
|
|
|
|
const int random_seed = 6432;
|
2013-09-05 19:45:56 +04:00
|
|
|
const int kBufferSize = 10000;
|
2012-06-25 20:58:09 +04:00
|
|
|
ACMRandom bit_rnd(random_seed);
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_writer bw;
|
2013-09-05 19:45:56 +04:00
|
|
|
uint8_t bw_buffer[kBufferSize];
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_start_encode(&bw, bw_buffer);
|
2012-06-25 20:58:09 +04:00
|
|
|
|
|
|
|
int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
|
2013-09-05 19:45:56 +04:00
|
|
|
for (int i = 0; i < kBitsToTest; ++i) {
|
2012-06-25 20:58:09 +04:00
|
|
|
if (bit_method == 2) {
|
|
|
|
bit = (i & 1);
|
|
|
|
} else if (bit_method == 3) {
|
|
|
|
bit = bit_rnd(2);
|
|
|
|
}
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_write(&bw, bit, static_cast<int>(probas[i]));
|
2012-06-25 20:58:09 +04:00
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_stop_encode(&bw);
|
2012-06-25 20:58:09 +04:00
|
|
|
|
2016-10-12 00:46:03 +03:00
|
|
|
#if !CONFIG_DAALA_EC
|
2013-06-07 05:03:44 +04:00
|
|
|
// First bit should be zero
|
|
|
|
GTEST_ASSERT_EQ(bw_buffer[0] & 0x80, 0);
|
2016-10-12 00:46:03 +03:00
|
|
|
#endif
|
2013-06-07 05:03:44 +04:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_reader br;
|
2016-10-12 00:46:03 +03:00
|
|
|
aom_reader_init(&br, bw_buffer, bw.pos, NULL, NULL);
|
2012-06-25 20:58:09 +04:00
|
|
|
bit_rnd.Reset(random_seed);
|
2013-09-05 19:45:56 +04:00
|
|
|
for (int i = 0; i < kBitsToTest; ++i) {
|
2012-06-25 20:58:09 +04:00
|
|
|
if (bit_method == 2) {
|
|
|
|
bit = (i & 1);
|
|
|
|
} else if (bit_method == 3) {
|
|
|
|
bit = bit_rnd(2);
|
|
|
|
}
|
2016-08-26 00:40:54 +03:00
|
|
|
GTEST_ASSERT_EQ(aom_read(&br, probas[i], NULL), bit)
|
2013-09-05 19:45:56 +04:00
|
|
|
<< "pos: " << i << " / " << kBitsToTest
|
2016-08-12 03:46:05 +03:00
|
|
|
<< " bit_method: " << bit_method << " method: " << method;
|
2012-06-25 20:58:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 21:27:56 +03:00
|
|
|
|
|
|
|
#if CONFIG_DAALA_EC
|
|
|
|
#define FRAC_DIFF_TOTAL_ERROR 0.07
|
|
|
|
#else
|
|
|
|
#define FRAC_DIFF_TOTAL_ERROR 0.2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST(AV1, TestTell) {
|
|
|
|
const int kBufferSize = 10000;
|
|
|
|
aom_writer bw;
|
|
|
|
uint8_t bw_buffer[kBufferSize];
|
|
|
|
const int kSymbols = 1024;
|
|
|
|
// Coders are noisier at low probabilities, so we start at p = 4.
|
|
|
|
for (int p = 4; p <= 256; p++) {
|
|
|
|
double probability = p / 256.;
|
|
|
|
aom_start_encode(&bw, bw_buffer);
|
|
|
|
for (int i = 0; i < kSymbols; i++) {
|
|
|
|
aom_write(&bw, 0, p);
|
|
|
|
}
|
|
|
|
aom_stop_encode(&bw);
|
|
|
|
aom_reader br;
|
|
|
|
aom_reader_init(&br, bw_buffer, kBufferSize, NULL, NULL);
|
2016-09-07 06:48:43 +03:00
|
|
|
uint32_t last_tell = aom_reader_tell(&br);
|
|
|
|
uint32_t last_tell_frac = aom_reader_tell_frac(&br);
|
2016-08-25 21:27:56 +03:00
|
|
|
double frac_diff_total = 0;
|
2016-09-07 06:48:43 +03:00
|
|
|
GTEST_ASSERT_GE(aom_reader_tell(&br), 0u);
|
|
|
|
GTEST_ASSERT_LE(aom_reader_tell(&br), 1u);
|
2016-08-25 21:27:56 +03:00
|
|
|
for (int i = 0; i < kSymbols; i++) {
|
2016-08-26 00:40:54 +03:00
|
|
|
aom_read(&br, p, NULL);
|
2016-09-07 06:48:43 +03:00
|
|
|
uint32_t tell = aom_reader_tell(&br);
|
|
|
|
uint32_t tell_frac = aom_reader_tell_frac(&br);
|
2016-08-25 21:27:56 +03:00
|
|
|
GTEST_ASSERT_GE(tell, last_tell) << "tell: " << tell
|
|
|
|
<< ", last_tell: " << last_tell;
|
|
|
|
GTEST_ASSERT_GE(tell_frac, last_tell_frac)
|
|
|
|
<< "tell_frac: " << tell_frac
|
|
|
|
<< ", last_tell_frac: " << last_tell_frac;
|
|
|
|
// Frac tell should round up to tell.
|
|
|
|
GTEST_ASSERT_EQ(tell, (tell_frac + 7) >> 3);
|
|
|
|
last_tell = tell;
|
|
|
|
frac_diff_total +=
|
|
|
|
fabs(((tell_frac - last_tell_frac) / 8.0) + log2(probability));
|
|
|
|
last_tell_frac = tell_frac;
|
|
|
|
}
|
2016-09-07 06:48:43 +03:00
|
|
|
const uint32_t expected = (uint32_t)(-kSymbols * log2(probability));
|
2016-08-25 21:27:56 +03:00
|
|
|
// Last tell should be close to the expected value.
|
2016-10-20 18:21:20 +03:00
|
|
|
GTEST_ASSERT_LE(last_tell, expected + 20) << " last_tell: " << last_tell;
|
2016-08-25 21:27:56 +03:00
|
|
|
// The average frac_diff error should be pretty small.
|
|
|
|
GTEST_ASSERT_LE(frac_diff_total / kSymbols, FRAC_DIFF_TOTAL_ERROR)
|
|
|
|
<< " frac_diff_total: " << frac_diff_total;
|
|
|
|
}
|
|
|
|
}
|