boolcoder_test.cc 2.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *  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"

17
#include "test/acm_random.h"
Yaowu Xu's avatar
Yaowu Xu committed
18
#include "vpx/vpx_integer.h"
19
#include "vpx_dsp/bitreader.h"
20
#include "vpx_dsp/bitwriter.h"
21

Daniel Kang's avatar
Daniel Kang committed
22
using libvpx_test::ACMRandom;
23

Daniel Kang's avatar
Daniel Kang committed
24 25
namespace {
const int num_tests = 10;
26 27
}  // namespace

James Zern's avatar
James Zern committed
28
TEST(VP9, TestBitIO) {
29 30 31
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  for (int n = 0; n < num_tests; ++n) {
    for (int method = 0; method <= 7; ++method) {   // we generate various proba
Yaowu Xu's avatar
Yaowu Xu committed
32 33
      const int kBitsToTest = 1000;
      uint8_t probas[kBitsToTest];
34

Yaowu Xu's avatar
Yaowu Xu committed
35
      for (int i = 0; i < kBitsToTest; ++i) {
36 37
        const int parity = i & 1;
        probas[i] =
John Koleszar's avatar
John Koleszar committed
38 39 40 41
          (method == 0) ? 0 : (method == 1) ? 255 :
          (method == 2) ? 128 :
          (method == 3) ? rnd.Rand8() :
          (method == 4) ? (parity ? 0 : 255) :
42 43 44
            // alternate between low and high proba:
            (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
            (method == 6) ?
John Koleszar's avatar
John Koleszar committed
45 46
            (parity ? rnd(64) : 255 - rnd(64)) :
            (parity ? rnd(32) : 255 - rnd(32));
47 48 49
      }
      for (int bit_method = 0; bit_method <= 3; ++bit_method) {
        const int random_seed = 6432;
Yaowu Xu's avatar
Yaowu Xu committed
50
        const int kBufferSize = 10000;
51
        ACMRandom bit_rnd(random_seed);
Yaowu Xu's avatar
Yaowu Xu committed
52
        vpx_writer bw;
Yaowu Xu's avatar
Yaowu Xu committed
53
        uint8_t bw_buffer[kBufferSize];
Yaowu Xu's avatar
Yaowu Xu committed
54
        vpx_start_encode(&bw, bw_buffer);
55 56

        int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
Yaowu Xu's avatar
Yaowu Xu committed
57
        for (int i = 0; i < kBitsToTest; ++i) {
58 59 60 61 62
          if (bit_method == 2) {
            bit = (i & 1);
          } else if (bit_method == 3) {
            bit = bit_rnd(2);
          }
Yaowu Xu's avatar
Yaowu Xu committed
63
          vpx_write(&bw, bit, static_cast<int>(probas[i]));
64 65
        }

Yaowu Xu's avatar
Yaowu Xu committed
66
        vpx_stop_encode(&bw);
67

68 69 70
        // First bit should be zero
        GTEST_ASSERT_EQ(bw_buffer[0] & 0x80, 0);

Yaowu Xu's avatar
Yaowu Xu committed
71 72
        vpx_reader br;
        vpx_reader_init(&br, bw_buffer, kBufferSize, NULL, NULL);
73
        bit_rnd.Reset(random_seed);
Yaowu Xu's avatar
Yaowu Xu committed
74
        for (int i = 0; i < kBitsToTest; ++i) {
75 76 77 78 79
          if (bit_method == 2) {
            bit = (i & 1);
          } else if (bit_method == 3) {
            bit = bit_rnd(2);
          }
Yaowu Xu's avatar
Yaowu Xu committed
80
          GTEST_ASSERT_EQ(vpx_read(&br, probas[i]), bit)
Yaowu Xu's avatar
Yaowu Xu committed
81
              << "pos: " << i << " / " << kBitsToTest
82 83 84 85 86 87 88
              << " bit_method: " << bit_method
              << " method: " << method;
        }
      }
    }
  }
}