2012-06-25 18:58:09 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 The WebM project authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "third_party/googletest/src/include/gtest/gtest.h"
|
|
|
|
|
2012-06-29 01:26:31 +02:00
|
|
|
extern "C" {
|
2012-11-27 22:59:17 +01:00
|
|
|
#include "vp9/encoder/vp9_boolhuff.h"
|
|
|
|
#include "vp9/decoder/vp9_dboolhuff.h"
|
2012-06-29 01:26:31 +02:00
|
|
|
}
|
2012-06-25 18:58:09 +02:00
|
|
|
|
2013-09-05 17:45:56 +02:00
|
|
|
#include "test/acm_random.h"
|
2012-06-29 01:26:31 +02:00
|
|
|
#include "vpx/vpx_integer.h"
|
2012-06-25 18:58:09 +02:00
|
|
|
|
2012-06-29 01:26:31 +02:00
|
|
|
using libvpx_test::ACMRandom;
|
2012-06-25 18:58:09 +02:00
|
|
|
|
2012-06-29 01:26:31 +02:00
|
|
|
namespace {
|
|
|
|
const int num_tests = 10;
|
2012-06-25 18:58:09 +02:00
|
|
|
} // namespace
|
|
|
|
|
2012-11-06 03:13:04 +01:00
|
|
|
TEST(VP9, TestBitIO) {
|
2012-06-25 18:58:09 +02:00
|
|
|
ACMRandom rnd(ACMRandom::DeterministicSeed());
|
|
|
|
for (int n = 0; n < num_tests; ++n) {
|
|
|
|
for (int method = 0; method <= 7; ++method) { // we generate various proba
|
2013-09-05 17:45:56 +02:00
|
|
|
const int kBitsToTest = 1000;
|
|
|
|
uint8_t probas[kBitsToTest];
|
2012-06-25 18:58:09 +02:00
|
|
|
|
2013-09-05 17:45:56 +02:00
|
|
|
for (int i = 0; i < kBitsToTest; ++i) {
|
2012-06-25 18:58:09 +02:00
|
|
|
const int parity = i & 1;
|
|
|
|
probas[i] =
|
2012-07-14 00:21:29 +02:00
|
|
|
(method == 0) ? 0 : (method == 1) ? 255 :
|
|
|
|
(method == 2) ? 128 :
|
|
|
|
(method == 3) ? rnd.Rand8() :
|
|
|
|
(method == 4) ? (parity ? 0 : 255) :
|
2012-06-25 18:58:09 +02:00
|
|
|
// alternate between low and high proba:
|
|
|
|
(method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
|
|
|
|
(method == 6) ?
|
2012-07-14 00:21:29 +02:00
|
|
|
(parity ? rnd(64) : 255 - rnd(64)) :
|
|
|
|
(parity ? rnd(32) : 255 - rnd(32));
|
2012-06-25 18:58:09 +02:00
|
|
|
}
|
|
|
|
for (int bit_method = 0; bit_method <= 3; ++bit_method) {
|
|
|
|
const int random_seed = 6432;
|
2013-09-05 17:45:56 +02:00
|
|
|
const int kBufferSize = 10000;
|
2012-06-25 18:58:09 +02:00
|
|
|
ACMRandom bit_rnd(random_seed);
|
2013-05-08 03:19:50 +02:00
|
|
|
vp9_writer bw;
|
2013-09-05 17:45:56 +02:00
|
|
|
uint8_t bw_buffer[kBufferSize];
|
2012-11-06 03:13:04 +01:00
|
|
|
vp9_start_encode(&bw, bw_buffer);
|
2012-06-25 18:58:09 +02:00
|
|
|
|
|
|
|
int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
|
2013-09-05 17:45:56 +02:00
|
|
|
for (int i = 0; i < kBitsToTest; ++i) {
|
2012-06-25 18:58:09 +02:00
|
|
|
if (bit_method == 2) {
|
|
|
|
bit = (i & 1);
|
|
|
|
} else if (bit_method == 3) {
|
|
|
|
bit = bit_rnd(2);
|
|
|
|
}
|
2013-05-08 03:19:50 +02:00
|
|
|
vp9_write(&bw, bit, static_cast<int>(probas[i]));
|
2012-06-25 18:58:09 +02:00
|
|
|
}
|
|
|
|
|
2012-11-06 03:13:04 +01:00
|
|
|
vp9_stop_encode(&bw);
|
2012-06-25 18:58:09 +02:00
|
|
|
|
2013-06-07 03:03:44 +02:00
|
|
|
// First bit should be zero
|
|
|
|
GTEST_ASSERT_EQ(bw_buffer[0] & 0x80, 0);
|
|
|
|
|
2013-04-19 19:37:24 +02:00
|
|
|
vp9_reader br;
|
2013-09-05 17:45:56 +02:00
|
|
|
vp9_reader_init(&br, bw_buffer, kBufferSize);
|
2012-06-25 18:58:09 +02:00
|
|
|
bit_rnd.Reset(random_seed);
|
2013-09-05 17:45:56 +02:00
|
|
|
for (int i = 0; i < kBitsToTest; ++i) {
|
2012-06-25 18:58:09 +02:00
|
|
|
if (bit_method == 2) {
|
|
|
|
bit = (i & 1);
|
|
|
|
} else if (bit_method == 3) {
|
|
|
|
bit = bit_rnd(2);
|
|
|
|
}
|
2013-04-15 23:54:19 +02:00
|
|
|
GTEST_ASSERT_EQ(vp9_read(&br, probas[i]), bit)
|
2013-09-05 17:45:56 +02:00
|
|
|
<< "pos: " << i << " / " << kBitsToTest
|
2012-06-25 18:58:09 +02:00
|
|
|
<< " bit_method: " << bit_method
|
|
|
|
<< " method: " << method;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|