vp9_boolcoder_test.cc 2.75 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 "vp9/decoder/vp9_reader.h"
18
#include "vp9/encoder/vp9_writer.h"
19

Yaowu Xu's avatar
Yaowu Xu committed
20
#include "test/acm_random.h"
Daniel Kang's avatar
Daniel Kang committed
21
#include "vpx/vpx_integer.h"
22

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

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

James Zern's avatar
James Zern committed
29
TEST(VP9, TestBitIO) {
30 31 32
  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
33 34
      const int kBitsToTest = 1000;
      uint8_t probas[kBitsToTest];
35

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

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

James Zern's avatar
James Zern committed
67
        vp9_stop_encode(&bw);
68

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

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