11b362b15af34006e6a11974088a46d42b903418eJohann/*
21b362b15af34006e6a11974088a46d42b903418eJohann *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
31b362b15af34006e6a11974088a46d42b903418eJohann *
41b362b15af34006e6a11974088a46d42b903418eJohann *  Use of this source code is governed by a BSD-style license
51b362b15af34006e6a11974088a46d42b903418eJohann *  that can be found in the LICENSE file in the root of the source
61b362b15af34006e6a11974088a46d42b903418eJohann *  tree. An additional intellectual property rights grant can be found
71b362b15af34006e6a11974088a46d42b903418eJohann *  in the file PATENTS.  All contributing project authors may
81b362b15af34006e6a11974088a46d42b903418eJohann *  be found in the AUTHORS file in the root of the source tree.
91b362b15af34006e6a11974088a46d42b903418eJohann */
101b362b15af34006e6a11974088a46d42b903418eJohann
111b362b15af34006e6a11974088a46d42b903418eJohann#include <math.h>
121b362b15af34006e6a11974088a46d42b903418eJohann#include <stdlib.h>
131b362b15af34006e6a11974088a46d42b903418eJohann#include <string.h>
141b362b15af34006e6a11974088a46d42b903418eJohann
151b362b15af34006e6a11974088a46d42b903418eJohann#include "third_party/googletest/src/include/gtest/gtest.h"
16ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
17ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangextern "C" {
18ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/encoder/vp9_boolhuff.h"
19ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/decoder/vp9_dboolhuff.h"
20ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
21ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
221184aebb761cbeac9124c37189a80a1a58f04b6bhkuang#include "test/acm_random.h"
231b362b15af34006e6a11974088a46d42b903418eJohann#include "vpx/vpx_integer.h"
241b362b15af34006e6a11974088a46d42b903418eJohann
25ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangusing libvpx_test::ACMRandom;
26ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
271b362b15af34006e6a11974088a46d42b903418eJohannnamespace {
281b362b15af34006e6a11974088a46d42b903418eJohannconst int num_tests = 10;
291b362b15af34006e6a11974088a46d42b903418eJohann}  // namespace
301b362b15af34006e6a11974088a46d42b903418eJohann
31ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangTEST(VP9, TestBitIO) {
321b362b15af34006e6a11974088a46d42b903418eJohann  ACMRandom rnd(ACMRandom::DeterministicSeed());
331b362b15af34006e6a11974088a46d42b903418eJohann  for (int n = 0; n < num_tests; ++n) {
341b362b15af34006e6a11974088a46d42b903418eJohann    for (int method = 0; method <= 7; ++method) {   // we generate various proba
351184aebb761cbeac9124c37189a80a1a58f04b6bhkuang      const int kBitsToTest = 1000;
361184aebb761cbeac9124c37189a80a1a58f04b6bhkuang      uint8_t probas[kBitsToTest];
371b362b15af34006e6a11974088a46d42b903418eJohann
381184aebb761cbeac9124c37189a80a1a58f04b6bhkuang      for (int i = 0; i < kBitsToTest; ++i) {
391b362b15af34006e6a11974088a46d42b903418eJohann        const int parity = i & 1;
401b362b15af34006e6a11974088a46d42b903418eJohann        probas[i] =
41ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang          (method == 0) ? 0 : (method == 1) ? 255 :
42ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang          (method == 2) ? 128 :
43ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang          (method == 3) ? rnd.Rand8() :
44ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang          (method == 4) ? (parity ? 0 : 255) :
451b362b15af34006e6a11974088a46d42b903418eJohann            // alternate between low and high proba:
461b362b15af34006e6a11974088a46d42b903418eJohann            (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
471b362b15af34006e6a11974088a46d42b903418eJohann            (method == 6) ?
48ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang            (parity ? rnd(64) : 255 - rnd(64)) :
49ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang            (parity ? rnd(32) : 255 - rnd(32));
501b362b15af34006e6a11974088a46d42b903418eJohann      }
511b362b15af34006e6a11974088a46d42b903418eJohann      for (int bit_method = 0; bit_method <= 3; ++bit_method) {
521b362b15af34006e6a11974088a46d42b903418eJohann        const int random_seed = 6432;
531184aebb761cbeac9124c37189a80a1a58f04b6bhkuang        const int kBufferSize = 10000;
541b362b15af34006e6a11974088a46d42b903418eJohann        ACMRandom bit_rnd(random_seed);
55ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        vp9_writer bw;
561184aebb761cbeac9124c37189a80a1a58f04b6bhkuang        uint8_t bw_buffer[kBufferSize];
57ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        vp9_start_encode(&bw, bw_buffer);
581b362b15af34006e6a11974088a46d42b903418eJohann
591b362b15af34006e6a11974088a46d42b903418eJohann        int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
601184aebb761cbeac9124c37189a80a1a58f04b6bhkuang        for (int i = 0; i < kBitsToTest; ++i) {
611b362b15af34006e6a11974088a46d42b903418eJohann          if (bit_method == 2) {
621b362b15af34006e6a11974088a46d42b903418eJohann            bit = (i & 1);
631b362b15af34006e6a11974088a46d42b903418eJohann          } else if (bit_method == 3) {
641b362b15af34006e6a11974088a46d42b903418eJohann            bit = bit_rnd(2);
651b362b15af34006e6a11974088a46d42b903418eJohann          }
66ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang          vp9_write(&bw, bit, static_cast<int>(probas[i]));
671b362b15af34006e6a11974088a46d42b903418eJohann        }
681b362b15af34006e6a11974088a46d42b903418eJohann
69ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        vp9_stop_encode(&bw);
70ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
71ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        // First bit should be zero
72ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        GTEST_ASSERT_EQ(bw_buffer[0] & 0x80, 0);
731b362b15af34006e6a11974088a46d42b903418eJohann
74ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        vp9_reader br;
751184aebb761cbeac9124c37189a80a1a58f04b6bhkuang        vp9_reader_init(&br, bw_buffer, kBufferSize);
761b362b15af34006e6a11974088a46d42b903418eJohann        bit_rnd.Reset(random_seed);
771184aebb761cbeac9124c37189a80a1a58f04b6bhkuang        for (int i = 0; i < kBitsToTest; ++i) {
781b362b15af34006e6a11974088a46d42b903418eJohann          if (bit_method == 2) {
791b362b15af34006e6a11974088a46d42b903418eJohann            bit = (i & 1);
801b362b15af34006e6a11974088a46d42b903418eJohann          } else if (bit_method == 3) {
811b362b15af34006e6a11974088a46d42b903418eJohann            bit = bit_rnd(2);
821b362b15af34006e6a11974088a46d42b903418eJohann          }
83ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang          GTEST_ASSERT_EQ(vp9_read(&br, probas[i]), bit)
841184aebb761cbeac9124c37189a80a1a58f04b6bhkuang              << "pos: " << i << " / " << kBitsToTest
851b362b15af34006e6a11974088a46d42b903418eJohann              << " bit_method: " << bit_method
861b362b15af34006e6a11974088a46d42b903418eJohann              << " method: " << method;
871b362b15af34006e6a11974088a46d42b903418eJohann        }
881b362b15af34006e6a11974088a46d42b903418eJohann      }
891b362b15af34006e6a11974088a46d42b903418eJohann    }
901b362b15af34006e6a11974088a46d42b903418eJohann  }
911b362b15af34006e6a11974088a46d42b903418eJohann}
92