blob: 5dbfd5ca59123c2c25762af106fd69e5b2c75271 [file] [log] [blame]
Ronald S. Bultjea742a732012-06-25 09:58:09 -07001/*
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Daniel Kang26641c72012-06-28 16:26:31 -070011#include <math.h>
12#include <stdlib.h>
13#include <string.h>
14
15#include "third_party/googletest/src/include/gtest/gtest.h"
16
Jingning Han097d59c2015-07-29 14:51:36 -070017#include "test/acm_random.h"
Yaowu Xu97279ed2015-07-17 13:38:54 -070018#include "vpx/vpx_integer.h"
Yaowu Xu87d2c3c2015-07-17 14:09:05 -070019#include "vpx_dsp/bitreader.h"
Yaowu Xu1fcef812015-07-20 11:18:57 -070020#include "vpx_dsp/bitwriter.h"
Ronald S. Bultjea742a732012-06-25 09:58:09 -070021
Daniel Kang26641c72012-06-28 16:26:31 -070022using libvpx_test::ACMRandom;
23
Ronald S. Bultjea742a732012-06-25 09:58:09 -070024namespace {
25const int num_tests = 10;
Ronald S. Bultjea742a732012-06-25 09:58:09 -070026} // namespace
27
James Zern2e3e6852012-11-05 18:13:04 -080028TEST(VP9, TestBitIO) {
Ronald S. Bultjea742a732012-06-25 09:58:09 -070029 ACMRandom rnd(ACMRandom::DeterministicSeed());
30 for (int n = 0; n < num_tests; ++n) {
clang-format3a826f12016-08-11 17:46:05 -070031 for (int method = 0; method <= 7; ++method) { // we generate various proba
Yaowu Xuafffa3d2013-09-05 08:45:56 -070032 const int kBitsToTest = 1000;
33 uint8_t probas[kBitsToTest];
Ronald S. Bultjea742a732012-06-25 09:58:09 -070034
Yaowu Xuafffa3d2013-09-05 08:45:56 -070035 for (int i = 0; i < kBitsToTest; ++i) {
Ronald S. Bultjea742a732012-06-25 09:58:09 -070036 const int parity = i & 1;
clang-format3a826f12016-08-11 17:46:05 -070037 /* clang-format off */
Ronald S. Bultjea742a732012-06-25 09:58:09 -070038 probas[i] =
John Koleszarc6b90392012-07-13 15:21:29 -070039 (method == 0) ? 0 : (method == 1) ? 255 :
40 (method == 2) ? 128 :
41 (method == 3) ? rnd.Rand8() :
42 (method == 4) ? (parity ? 0 : 255) :
Ronald S. Bultjea742a732012-06-25 09:58:09 -070043 // alternate between low and high proba:
44 (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
45 (method == 6) ?
John Koleszarc6b90392012-07-13 15:21:29 -070046 (parity ? rnd(64) : 255 - rnd(64)) :
47 (parity ? rnd(32) : 255 - rnd(32));
clang-format3a826f12016-08-11 17:46:05 -070048 /* clang-format on */
Ronald S. Bultjea742a732012-06-25 09:58:09 -070049 }
50 for (int bit_method = 0; bit_method <= 3; ++bit_method) {
51 const int random_seed = 6432;
Yaowu Xuafffa3d2013-09-05 08:45:56 -070052 const int kBufferSize = 10000;
Ronald S. Bultjea742a732012-06-25 09:58:09 -070053 ACMRandom bit_rnd(random_seed);
Yaowu Xu817be1d2015-07-20 14:13:38 -070054 vpx_writer bw;
Yaowu Xuafffa3d2013-09-05 08:45:56 -070055 uint8_t bw_buffer[kBufferSize];
Yaowu Xu817be1d2015-07-20 14:13:38 -070056 vpx_start_encode(&bw, bw_buffer);
Ronald S. Bultjea742a732012-06-25 09:58:09 -070057
58 int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
Yaowu Xuafffa3d2013-09-05 08:45:56 -070059 for (int i = 0; i < kBitsToTest; ++i) {
Ronald S. Bultjea742a732012-06-25 09:58:09 -070060 if (bit_method == 2) {
61 bit = (i & 1);
62 } else if (bit_method == 3) {
63 bit = bit_rnd(2);
64 }
Yaowu Xu817be1d2015-07-20 14:13:38 -070065 vpx_write(&bw, bit, static_cast<int>(probas[i]));
Ronald S. Bultjea742a732012-06-25 09:58:09 -070066 }
67
Yaowu Xu817be1d2015-07-20 14:13:38 -070068 vpx_stop_encode(&bw);
Ronald S. Bultjea742a732012-06-25 09:58:09 -070069
John Koleszara425e2c2013-06-06 18:03:44 -070070 // First bit should be zero
71 GTEST_ASSERT_EQ(bw_buffer[0] & 0x80, 0);
72
Yaowu Xubf825142015-07-20 13:49:15 -070073 vpx_reader br;
74 vpx_reader_init(&br, bw_buffer, kBufferSize, NULL, NULL);
Ronald S. Bultjea742a732012-06-25 09:58:09 -070075 bit_rnd.Reset(random_seed);
Yaowu Xuafffa3d2013-09-05 08:45:56 -070076 for (int i = 0; i < kBitsToTest; ++i) {
Ronald S. Bultjea742a732012-06-25 09:58:09 -070077 if (bit_method == 2) {
78 bit = (i & 1);
79 } else if (bit_method == 3) {
80 bit = bit_rnd(2);
81 }
Yaowu Xubf825142015-07-20 13:49:15 -070082 GTEST_ASSERT_EQ(vpx_read(&br, probas[i]), bit)
Yaowu Xuafffa3d2013-09-05 08:45:56 -070083 << "pos: " << i << " / " << kBitsToTest
clang-format3a826f12016-08-11 17:46:05 -070084 << " bit_method: " << bit_method << " method: " << method;
Ronald S. Bultjea742a732012-06-25 09:58:09 -070085 }
86 }
87 }
88 }
89}