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
11#include <math.h>
12#include <stddef.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/types.h>
17
18#include "third_party/googletest/src/include/gtest/gtest.h"
19
20#include "test/acm_random.h"
21#include "vp8/decoder/dboolhuff.h"
22#include "vp8/encoder/boolhuff.h"
23#include "vpx/vpx_integer.h"
24
25namespace {
26const int num_tests = 10;
27
28// In a real use the 'decrypt_state' parameter will be a pointer to a struct
29// with whatever internal state the decryptor uses. For testing we'll just
30// xor with a constant key, and decrypt_state will point to the start of
31// the original buffer.
32const uint8_t secret_key[16] = {
33  0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
34  0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0
35};
36
37void encrypt_buffer(uint8_t *buffer, size_t size) {
38  for (size_t i = 0; i < size; ++i) {
39    buffer[i] ^= secret_key[i & 15];
40  }
41}
42
43void test_decrypt_cb(void *decrypt_state, const uint8_t *input, uint8_t *output,
44                     int count) {
45  const size_t offset = input - reinterpret_cast<uint8_t *>(decrypt_state);
46  for (int i = 0; i < count; i++) {
47    output[i] = input[i] ^ secret_key[(offset + i) & 15];
48  }
49}
50
51}  // namespace
52
53using libvpx_test::ACMRandom;
54
55TEST(VP8, TestBitIO) {
56  ACMRandom rnd(ACMRandom::DeterministicSeed());
57  for (int n = 0; n < num_tests; ++n) {
58    for (int method = 0; method <= 7; ++method) {  // we generate various proba
59      const int kBitsToTest = 1000;
60      uint8_t probas[kBitsToTest];
61
62      for (int i = 0; i < kBitsToTest; ++i) {
63        const int parity = i & 1;
64        /* clang-format off */
65        probas[i] =
66            (method == 0) ? 0 : (method == 1) ? 255 :
67            (method == 2) ? 128 :
68            (method == 3) ? rnd.Rand8() :
69            (method == 4) ? (parity ? 0 : 255) :
70            // alternate between low and high proba:
71            (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
72            (method == 6) ?
73                (parity ? rnd(64) : 255 - rnd(64)) :
74                (parity ? rnd(32) : 255 - rnd(32));
75        /* clang-format on */
76      }
77      for (int bit_method = 0; bit_method <= 3; ++bit_method) {
78        const int random_seed = 6432;
79        const int kBufferSize = 10000;
80        ACMRandom bit_rnd(random_seed);
81        BOOL_CODER bw;
82        uint8_t bw_buffer[kBufferSize];
83        vp8_start_encode(&bw, bw_buffer, bw_buffer + kBufferSize);
84
85        int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
86        for (int i = 0; i < kBitsToTest; ++i) {
87          if (bit_method == 2) {
88            bit = (i & 1);
89          } else if (bit_method == 3) {
90            bit = bit_rnd(2);
91          }
92          vp8_encode_bool(&bw, bit, static_cast<int>(probas[i]));
93        }
94
95        vp8_stop_encode(&bw);
96
97        BOOL_DECODER br;
98        encrypt_buffer(bw_buffer, kBufferSize);
99        vp8dx_start_decode(&br, bw_buffer, kBufferSize, test_decrypt_cb,
100                           reinterpret_cast<void *>(bw_buffer));
101        bit_rnd.Reset(random_seed);
102        for (int i = 0; i < kBitsToTest; ++i) {
103          if (bit_method == 2) {
104            bit = (i & 1);
105          } else if (bit_method == 3) {
106            bit = bit_rnd(2);
107          }
108          GTEST_ASSERT_EQ(vp8dx_decode_bool(&br, probas[i]), bit)
109              << "pos: " << i << " / " << kBitsToTest
110              << " bit_method: " << bit_method << " method: " << method;
111        }
112      }
113    }
114  }
115}
116