]> granicus.if.org Git - libvpx/blob - test/vp9_boolcoder_test.cc
Merge "Exclude vpx intra prediction functions in vp8-only build"
[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 "test/acm_random.h"
18 #include "vpx/vpx_integer.h"
19 #include "vpx_dsp/bitreader.h"
20 #include "vpx_dsp/bitwriter.h"
21
22 using libvpx_test::ACMRandom;
23
24 namespace {
25 const int num_tests = 10;
26 }  // namespace
27
28 TEST(VP9, TestBitIO) {
29   ACMRandom rnd(ACMRandom::DeterministicSeed());
30   for (int n = 0; n < num_tests; ++n) {
31     for (int method = 0; method <= 7; ++method) {   // we generate various proba
32       const int kBitsToTest = 1000;
33       uint8_t probas[kBitsToTest];
34
35       for (int i = 0; i < kBitsToTest; ++i) {
36         const int parity = i & 1;
37         probas[i] =
38           (method == 0) ? 0 : (method == 1) ? 255 :
39           (method == 2) ? 128 :
40           (method == 3) ? rnd.Rand8() :
41           (method == 4) ? (parity ? 0 : 255) :
42             // alternate between low and high proba:
43             (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
44             (method == 6) ?
45             (parity ? rnd(64) : 255 - rnd(64)) :
46             (parity ? rnd(32) : 255 - rnd(32));
47       }
48       for (int bit_method = 0; bit_method <= 3; ++bit_method) {
49         const int random_seed = 6432;
50         const int kBufferSize = 10000;
51         ACMRandom bit_rnd(random_seed);
52         vpx_writer bw;
53         uint8_t bw_buffer[kBufferSize];
54         vpx_start_encode(&bw, bw_buffer);
55
56         int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
57         for (int i = 0; i < kBitsToTest; ++i) {
58           if (bit_method == 2) {
59             bit = (i & 1);
60           } else if (bit_method == 3) {
61             bit = bit_rnd(2);
62           }
63           vpx_write(&bw, bit, static_cast<int>(probas[i]));
64         }
65
66         vpx_stop_encode(&bw);
67
68         // First bit should be zero
69         GTEST_ASSERT_EQ(bw_buffer[0] & 0x80, 0);
70
71         vpx_reader br;
72         vpx_reader_init(&br, bw_buffer, kBufferSize, NULL, NULL);
73         bit_rnd.Reset(random_seed);
74         for (int i = 0; i < kBitsToTest; ++i) {
75           if (bit_method == 2) {
76             bit = (i & 1);
77           } else if (bit_method == 3) {
78             bit = bit_rnd(2);
79           }
80           GTEST_ASSERT_EQ(vpx_read(&br, probas[i]), bit)
81               << "pos: " << i << " / " << kBitsToTest
82               << " bit_method: " << bit_method
83               << " method: " << method;
84         }
85       }
86     }
87   }
88 }