boolcoder_test.cc 3.08 KB
Newer Older
Jim Bankoski's avatar
Jim Bankoski committed
1
2
3
4
5
6
7
8
9
10
/*
 *  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.
 */

Jim Bankoski's avatar
Jim Bankoski committed
11
extern "C" {
Jim Bankoski's avatar
Jim Bankoski committed
12
13
14
15
16
17
18
19
20
21
22
23
#include "vp8/encoder/boolhuff.h"
#include "vp8/decoder/dboolhuff.h"
}

#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "third_party/googletest/src/include/gtest/gtest.h"
Jim Bankoski's avatar
Jim Bankoski committed
24
#include "vpx/vpx_integer.h"
Jim Bankoski's avatar
Jim Bankoski committed
25

Jim Bankoski's avatar
Jim Bankoski committed
26
namespace {
Jim Bankoski's avatar
Jim Bankoski committed
27
28
const int num_tests = 10;

Jim Bankoski's avatar
Jim Bankoski committed
29
30
31
class ACMRandom {
 public:
  explicit ACMRandom(int seed) { Reset(seed); }
Jim Bankoski's avatar
Jim Bankoski committed
32

Jim Bankoski's avatar
Jim Bankoski committed
33
  void Reset(int seed) { srand(seed); }
Jim Bankoski's avatar
Jim Bankoski committed
34

Jim Bankoski's avatar
Jim Bankoski committed
35
  uint8_t Rand8(void) { return (rand() >> 8) & 0xff; }
Jim Bankoski's avatar
Jim Bankoski committed
36

Jim Bankoski's avatar
Jim Bankoski committed
37
  int PseudoUniform(int range) { return (rand() >> 8) % range; }
Jim Bankoski's avatar
Jim Bankoski committed
38

Jim Bankoski's avatar
Jim Bankoski committed
39
  int operator()(int n) { return PseudoUniform(n); }
Jim Bankoski's avatar
Jim Bankoski committed
40

Jim Bankoski's avatar
Jim Bankoski committed
41
  static int DeterministicSeed(void) { return 0xbaba; }
Jim Bankoski's avatar
Jim Bankoski committed
42
};
Jim Bankoski's avatar
Jim Bankoski committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
}  // namespace

TEST(VP8, TestBitIO) {
  ACMRandom rnd(ACMRandom::DeterministicSeed());
  for (int n = 0; n < num_tests; ++n) {
    for (int method = 0; method <= 7; ++method) {   // we generate various proba
      const int bits_to_test = 1000;
      uint8_t probas[bits_to_test];

      for (int i = 0; i < bits_to_test; ++i) {
        const int parity = i & 1;
        probas[i] =
            (method == 0) ? 0 : (method == 1) ? 255 :
            (method == 2) ? 128 :
            (method == 3) ? rnd.Rand8() :
            (method == 4) ? (parity ? 0 : 255) :
            // alternate between low and high proba:
            (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
            (method == 6) ?
                (parity ? rnd(64) : 255 - rnd(64)) :
                (parity ? rnd(32) : 255 - rnd(32));
      }
      for (int bit_method = 0; bit_method <= 3; ++bit_method) {
        const int random_seed = 6432;
        const int buffer_size = 10000;
        ACMRandom bit_rnd(random_seed);
        BOOL_CODER bw;
        uint8_t bw_buffer[buffer_size];
        vp8_start_encode(&bw, bw_buffer, bw_buffer + buffer_size);

        int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
        for (int i = 0; i < bits_to_test; ++i) {
          if (bit_method == 2) {
            bit = (i & 1);
          } else if (bit_method == 3) {
            bit = bit_rnd(2);
          }
          vp8_encode_bool(&bw, bit, static_cast<int>(probas[i]));
        }
Jim Bankoski's avatar
Jim Bankoski committed
82

Jim Bankoski's avatar
Jim Bankoski committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        vp8_stop_encode(&bw);

        BOOL_DECODER br;
        vp8dx_start_decode(&br, bw_buffer, buffer_size);
        bit_rnd.Reset(random_seed);
        for (int i = 0; i < bits_to_test; ++i) {
          if (bit_method == 2) {
            bit = (i & 1);
          } else if (bit_method == 3) {
            bit = bit_rnd(2);
          }
          GTEST_ASSERT_EQ(vp8dx_decode_bool(&br, probas[i]), bit)
              << "pos: "<< i << " / " << bits_to_test
              << " bit_method: " << bit_method
              << " method: " << method;
Jim Bankoski's avatar
Jim Bankoski committed
98
        }
Jim Bankoski's avatar
Jim Bankoski committed
99
      }
Jim Bankoski's avatar
Jim Bankoski committed
100
    }
Jim Bankoski's avatar
Jim Bankoski committed
101
  }
Jim Bankoski's avatar
Jim Bankoski committed
102
}