]> granicus.if.org Git - libvpx/blob - test/vp9_boolcoder_test.cc
Move bit reader files to vpx_dsp
[libvpx] / test / vp9_boolcoder_test.cc
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 <stdlib.h>
13 #include <string.h>
14
15 #include "third_party/googletest/src/include/gtest/gtest.h"
16
17 #include "vpx/vpx_integer.h"
18 #include "vpx_dsp/vp9_reader.h"
19
20 #include "vp9/encoder/vp9_writer.h"
21
22 #include "test/acm_random.h"
23
24 using libvpx_test::ACMRandom;
25
26 namespace {
27 const int num_tests = 10;
28 }  // namespace
29
30 TEST(VP9, TestBitIO) {
31   ACMRandom rnd(ACMRandom::DeterministicSeed());
32   for (int n = 0; n < num_tests; ++n) {
33     for (int method = 0; method <= 7; ++method) {   // we generate various proba
34       const int kBitsToTest = 1000;
35       uint8_t probas[kBitsToTest];
36
37       for (int i = 0; i < kBitsToTest; ++i) {
38         const int parity = i & 1;
39         probas[i] =
40           (method == 0) ? 0 : (method == 1) ? 255 :
41           (method == 2) ? 128 :
42           (method == 3) ? rnd.Rand8() :
43           (method == 4) ? (parity ? 0 : 255) :
44             // alternate between low and high proba:
45             (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
46             (method == 6) ?
47             (parity ? rnd(64) : 255 - rnd(64)) :
48             (parity ? rnd(32) : 255 - rnd(32));
49       }
50       for (int bit_method = 0; bit_method <= 3; ++bit_method) {
51         const int random_seed = 6432;
52         const int kBufferSize = 10000;
53         ACMRandom bit_rnd(random_seed);
54         vp9_writer bw;
55         uint8_t bw_buffer[kBufferSize];
56         vp9_start_encode(&bw, bw_buffer);
57
58         int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
59         for (int i = 0; i < kBitsToTest; ++i) {
60           if (bit_method == 2) {
61             bit = (i & 1);
62           } else if (bit_method == 3) {
63             bit = bit_rnd(2);
64           }
65           vp9_write(&bw, bit, static_cast<int>(probas[i]));
66         }
67
68         vp9_stop_encode(&bw);
69
70         // First bit should be zero
71         GTEST_ASSERT_EQ(bw_buffer[0] & 0x80, 0);
72
73         vp9_reader br;
74         vp9_reader_init(&br, bw_buffer, kBufferSize, NULL, NULL);
75         bit_rnd.Reset(random_seed);
76         for (int i = 0; i < kBitsToTest; ++i) {
77           if (bit_method == 2) {
78             bit = (i & 1);
79           } else if (bit_method == 3) {
80             bit = bit_rnd(2);
81           }
82           GTEST_ASSERT_EQ(vp9_read(&br, probas[i]), bit)
83               << "pos: " << i << " / " << kBitsToTest
84               << " bit_method: " << bit_method
85               << " method: " << method;
86         }
87       }
88     }
89   }
90 }