blob: 02d7162ac851b6ef688f5ed28a45cd5a978c23d8 [file] [log] [blame]
Jim Bankoski98514862012-05-08 07:29:19 -07001/*
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
Jim Bankoski98514862012-05-08 07:29:19 -070011
12#include <math.h>
13#include <stddef.h>
Jim Bankoski98514862012-05-08 07:29:19 -070014#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <sys/types.h>
18
19#include "third_party/googletest/src/include/gtest/gtest.h"
20
Jingning Han097d59c2015-07-29 14:51:36 -070021#include "test/acm_random.h"
Yaowu Xuafffa3d2013-09-05 08:45:56 -070022#include "vp8/decoder/dboolhuff.h"
Jingning Han097d59c2015-07-29 14:51:36 -070023#include "vp8/encoder/boolhuff.h"
24#include "vpx/vpx_integer.h"
Yaowu Xuafffa3d2013-09-05 08:45:56 -070025
Jim Bankoski9ec20f82012-05-25 09:07:32 -070026namespace {
Jim Bankoski98514862012-05-08 07:29:19 -070027const int num_tests = 10;
Dmitry Kovalev26cec5c2013-03-15 18:21:55 -070028
Jeff Petkau368c7232013-06-13 12:16:58 -070029// In a real use the 'decrypt_state' parameter will be a pointer to a struct
30// with whatever internal state the decryptor uses. For testing we'll just
31// xor with a constant key, and decrypt_state will point to the start of
32// the original buffer.
33const uint8_t secret_key[16] = {
34 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
35 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0
36};
37
Tom Finegan4aa8ea52014-03-03 18:04:35 -080038void encrypt_buffer(uint8_t *buffer, size_t size) {
39 for (size_t i = 0; i < size; ++i) {
Jeff Petkau368c7232013-06-13 12:16:58 -070040 buffer[i] ^= secret_key[i & 15];
Dmitry Kovalev26cec5c2013-03-15 18:21:55 -070041 }
42}
43
Jeff Petkau368c7232013-06-13 12:16:58 -070044void test_decrypt_cb(void *decrypt_state, const uint8_t *input,
Tom Finegan4aa8ea52014-03-03 18:04:35 -080045 uint8_t *output, int count) {
James Zern77e64d82013-12-18 17:00:40 -080046 const size_t offset = input - reinterpret_cast<uint8_t*>(decrypt_state);
Jeff Petkau368c7232013-06-13 12:16:58 -070047 for (int i = 0; i < count; i++) {
48 output[i] = input[i] ^ secret_key[(offset + i) & 15];
49 }
50}
Dmitry Kovalev26cec5c2013-03-15 18:21:55 -070051
Jim Bankoski9ec20f82012-05-25 09:07:32 -070052} // namespace
Jim Bankoski98514862012-05-08 07:29:19 -070053
Johannc0e80952012-07-20 11:51:06 -070054using libvpx_test::ACMRandom;
55
Jim Bankoski9ec20f82012-05-25 09:07:32 -070056TEST(VP8, TestBitIO) {
57 ACMRandom rnd(ACMRandom::DeterministicSeed());
58 for (int n = 0; n < num_tests; ++n) {
59 for (int method = 0; method <= 7; ++method) { // we generate various proba
Yaowu Xuafffa3d2013-09-05 08:45:56 -070060 const int kBitsToTest = 1000;
61 uint8_t probas[kBitsToTest];
Jim Bankoski98514862012-05-08 07:29:19 -070062
Yaowu Xuafffa3d2013-09-05 08:45:56 -070063 for (int i = 0; i < kBitsToTest; ++i) {
Jim Bankoski9ec20f82012-05-25 09:07:32 -070064 const int parity = i & 1;
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 }
76 for (int bit_method = 0; bit_method <= 3; ++bit_method) {
77 const int random_seed = 6432;
Yaowu Xuafffa3d2013-09-05 08:45:56 -070078 const int kBufferSize = 10000;
Jim Bankoski9ec20f82012-05-25 09:07:32 -070079 ACMRandom bit_rnd(random_seed);
80 BOOL_CODER bw;
Yaowu Xuafffa3d2013-09-05 08:45:56 -070081 uint8_t bw_buffer[kBufferSize];
82 vp8_start_encode(&bw, bw_buffer, bw_buffer + kBufferSize);
Jim Bankoski98514862012-05-08 07:29:19 -070083
Jim Bankoski9ec20f82012-05-25 09:07:32 -070084 int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
Yaowu Xuafffa3d2013-09-05 08:45:56 -070085 for (int i = 0; i < kBitsToTest; ++i) {
Jim Bankoski9ec20f82012-05-25 09:07:32 -070086 if (bit_method == 2) {
87 bit = (i & 1);
88 } else if (bit_method == 3) {
89 bit = bit_rnd(2);
90 }
91 vp8_encode_bool(&bw, bit, static_cast<int>(probas[i]));
Jim Bankoski98514862012-05-08 07:29:19 -070092 }
Jim Bankoski9ec20f82012-05-25 09:07:32 -070093
94 vp8_stop_encode(&bw);
95
96 BOOL_DECODER br;
Joey Parrish18c08602014-04-15 14:10:58 -070097 encrypt_buffer(bw_buffer, kBufferSize);
98 vp8dx_start_decode(&br, bw_buffer, kBufferSize,
Yaowu Xuafffa3d2013-09-05 08:45:56 -070099 test_decrypt_cb,
100 reinterpret_cast<void *>(bw_buffer));
Jim Bankoski9ec20f82012-05-25 09:07:32 -0700101 bit_rnd.Reset(random_seed);
Yaowu Xuafffa3d2013-09-05 08:45:56 -0700102 for (int i = 0; i < kBitsToTest; ++i) {
Jim Bankoski9ec20f82012-05-25 09:07:32 -0700103 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)
Yaowu Xuafffa3d2013-09-05 08:45:56 -0700109 << "pos: "<< i << " / " << kBitsToTest
Jim Bankoski9ec20f82012-05-25 09:07:32 -0700110 << " bit_method: " << bit_method
111 << " method: " << method;
112 }
113 }
Jim Bankoski98514862012-05-08 07:29:19 -0700114 }
Jim Bankoski9ec20f82012-05-25 09:07:32 -0700115 }
Jim Bankoski98514862012-05-08 07:29:19 -0700116}