Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 1 | /* |
| 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 Kang | 26641c7 | 2012-06-28 16:26:31 -0700 | [diff] [blame] | 11 | #include <math.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include "third_party/googletest/src/include/gtest/gtest.h" |
| 16 | |
Jingning Han | 097d59c | 2015-07-29 14:51:36 -0700 | [diff] [blame] | 17 | #include "test/acm_random.h" |
Yaowu Xu | 97279ed | 2015-07-17 13:38:54 -0700 | [diff] [blame] | 18 | #include "vpx/vpx_integer.h" |
Yaowu Xu | 87d2c3c | 2015-07-17 14:09:05 -0700 | [diff] [blame] | 19 | #include "vpx_dsp/bitreader.h" |
Yaowu Xu | 1fcef81 | 2015-07-20 11:18:57 -0700 | [diff] [blame] | 20 | #include "vpx_dsp/bitwriter.h" |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 21 | |
Daniel Kang | 26641c7 | 2012-06-28 16:26:31 -0700 | [diff] [blame] | 22 | using libvpx_test::ACMRandom; |
| 23 | |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 24 | namespace { |
| 25 | const int num_tests = 10; |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 26 | } // namespace |
| 27 | |
James Zern | 2e3e685 | 2012-11-05 18:13:04 -0800 | [diff] [blame] | 28 | TEST(VP9, TestBitIO) { |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 29 | ACMRandom rnd(ACMRandom::DeterministicSeed()); |
| 30 | for (int n = 0; n < num_tests; ++n) { |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame^] | 31 | for (int method = 0; method <= 7; ++method) { // we generate various proba |
Yaowu Xu | afffa3d | 2013-09-05 08:45:56 -0700 | [diff] [blame] | 32 | const int kBitsToTest = 1000; |
| 33 | uint8_t probas[kBitsToTest]; |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 34 | |
Yaowu Xu | afffa3d | 2013-09-05 08:45:56 -0700 | [diff] [blame] | 35 | for (int i = 0; i < kBitsToTest; ++i) { |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 36 | const int parity = i & 1; |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame^] | 37 | /* clang-format off */ |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 38 | probas[i] = |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 39 | (method == 0) ? 0 : (method == 1) ? 255 : |
| 40 | (method == 2) ? 128 : |
| 41 | (method == 3) ? rnd.Rand8() : |
| 42 | (method == 4) ? (parity ? 0 : 255) : |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 43 | // alternate between low and high proba: |
| 44 | (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) : |
| 45 | (method == 6) ? |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 46 | (parity ? rnd(64) : 255 - rnd(64)) : |
| 47 | (parity ? rnd(32) : 255 - rnd(32)); |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame^] | 48 | /* clang-format on */ |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 49 | } |
| 50 | for (int bit_method = 0; bit_method <= 3; ++bit_method) { |
| 51 | const int random_seed = 6432; |
Yaowu Xu | afffa3d | 2013-09-05 08:45:56 -0700 | [diff] [blame] | 52 | const int kBufferSize = 10000; |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 53 | ACMRandom bit_rnd(random_seed); |
Yaowu Xu | 817be1d | 2015-07-20 14:13:38 -0700 | [diff] [blame] | 54 | vpx_writer bw; |
Yaowu Xu | afffa3d | 2013-09-05 08:45:56 -0700 | [diff] [blame] | 55 | uint8_t bw_buffer[kBufferSize]; |
Yaowu Xu | 817be1d | 2015-07-20 14:13:38 -0700 | [diff] [blame] | 56 | vpx_start_encode(&bw, bw_buffer); |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 57 | |
| 58 | int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0; |
Yaowu Xu | afffa3d | 2013-09-05 08:45:56 -0700 | [diff] [blame] | 59 | for (int i = 0; i < kBitsToTest; ++i) { |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 60 | if (bit_method == 2) { |
| 61 | bit = (i & 1); |
| 62 | } else if (bit_method == 3) { |
| 63 | bit = bit_rnd(2); |
| 64 | } |
Yaowu Xu | 817be1d | 2015-07-20 14:13:38 -0700 | [diff] [blame] | 65 | vpx_write(&bw, bit, static_cast<int>(probas[i])); |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Yaowu Xu | 817be1d | 2015-07-20 14:13:38 -0700 | [diff] [blame] | 68 | vpx_stop_encode(&bw); |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 69 | |
John Koleszar | a425e2c | 2013-06-06 18:03:44 -0700 | [diff] [blame] | 70 | // First bit should be zero |
| 71 | GTEST_ASSERT_EQ(bw_buffer[0] & 0x80, 0); |
| 72 | |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 73 | vpx_reader br; |
| 74 | vpx_reader_init(&br, bw_buffer, kBufferSize, NULL, NULL); |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 75 | bit_rnd.Reset(random_seed); |
Yaowu Xu | afffa3d | 2013-09-05 08:45:56 -0700 | [diff] [blame] | 76 | for (int i = 0; i < kBitsToTest; ++i) { |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 77 | if (bit_method == 2) { |
| 78 | bit = (i & 1); |
| 79 | } else if (bit_method == 3) { |
| 80 | bit = bit_rnd(2); |
| 81 | } |
Yaowu Xu | bf82514 | 2015-07-20 13:49:15 -0700 | [diff] [blame] | 82 | GTEST_ASSERT_EQ(vpx_read(&br, probas[i]), bit) |
Yaowu Xu | afffa3d | 2013-09-05 08:45:56 -0700 | [diff] [blame] | 83 | << "pos: " << i << " / " << kBitsToTest |
clang-format | 3a826f1 | 2016-08-11 17:46:05 -0700 | [diff] [blame^] | 84 | << " bit_method: " << bit_method << " method: " << method; |
Ronald S. Bultje | a742a73 | 2012-06-25 09:58:09 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |