aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/bs_encode/encode_ut.cpp
blob: 39f0ff1093787b246f5ad0dd81b9c6201a6a268d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * This file is part of AtracDEnc.
 *
 * AtracDEnc is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * AtracDEnc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with AtracDEnc; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "encode.h"
#include <gtest/gtest.h>
#include <cmath>
#include <memory>
#include <bitstream/bitstream.h>

using namespace NAtracDEnc;

static size_t SomeBitFn1(float lambda) {
    return sqrt(lambda * (-1.0f)) * 300;
}

static size_t SomeBitFn2(float lambda) {
    return 1 + (SomeBitFn1(lambda) & (~(size_t)7));
}

class TPartEncoder1 : public IBitStreamPartEncoder {
public:
    explicit TPartEncoder1(size_t expCalls)
        : ExpCalls(expCalls)
    {}

    EStatus Encode(void* frameData, TBitAllocHandler& ba) override {
        EncCalls++;
        ba.Start(1000, -15, -1);
        return EStatus::Ok;
    }

    void Dump(NBitStream::TBitStream& bs) override {
         EXPECT_EQ(EncCalls, ExpCalls);
    }

    uint32_t GetConsumption() const noexcept override {
        return 0;
    }
private:
    const size_t ExpCalls;
    size_t EncCalls = 0;
};

template<size_t (*F)(float)>
class TPartEncoder2 : public IBitStreamPartEncoder {
public:
    explicit TPartEncoder2(size_t expCalls)
        : ExpCalls(expCalls)
    {}

    EStatus Encode(void* frameData, TBitAllocHandler& ba) override {
        EncCalls++;
        auto lambda = ba.Continue();
        auto bits = F(lambda);
        ba.Submit(bits);
        Bits = bits;
        return EStatus::Ok;
    }

    void Dump(NBitStream::TBitStream& bs) override {
         EXPECT_EQ(EncCalls, ExpCalls);
         for (size_t i = 0; i < Bits; i++) {
             bs.Write(1, 1);
         }
    }

    uint32_t GetConsumption() const noexcept override {
        return 1 * Bits;
    }
private:
    const size_t ExpCalls;
    size_t EncCalls = 0;
    size_t Bits = 0;
};

class TPartEncoder3 : public IBitStreamPartEncoder {
public:
    explicit TPartEncoder3(size_t expCalls)
        : ExpCalls(expCalls)
    {}

    EStatus Encode(void* frameData, TBitAllocHandler& ba) override {
        EncCalls++;
        return EStatus::Ok;
    }

    void Dump(NBitStream::TBitStream& bs) override {
         EXPECT_EQ(EncCalls, ExpCalls);
    }

    uint32_t GetConsumption() const noexcept override {
        return 0;
    }
private:
    const size_t ExpCalls;
    size_t EncCalls = 0;
};

class TPartEncoder4 : public IBitStreamPartEncoder {
public:
    TPartEncoder4() = default;

    EStatus Encode(void* frameData, TBitAllocHandler& ba) override {
        if (EncCalls == 0) {
            EncCalls++;
            return EStatus::Repeat;
        }

        return EStatus::Ok;
    }

    void Dump(NBitStream::TBitStream& bs) override {
         EXPECT_EQ(EncCalls, 1);
    }

    uint32_t GetConsumption() const noexcept override {
        return 0;
    }
private:
    size_t EncCalls = 0;
};

TEST(BsEncode, SimpleAlloc) {
    std::vector<IBitStreamPartEncoder::TPtr> encoders;
    encoders.emplace_back(std::make_unique<TPartEncoder1>(1));
    encoders.emplace_back(std::make_unique<TPartEncoder2<SomeBitFn1>>(8));
    encoders.emplace_back(std::make_unique<TPartEncoder3>(1));

    NBitStream::TBitStream bs;
    TBitStreamEncoder encoder(std::move(encoders));
    encoder.Do(nullptr, bs);

    EXPECT_EQ(bs.GetSizeInBits(), 1000);
}

TEST(BsEncode, AllocWithRepeat) {
    std::vector<IBitStreamPartEncoder::TPtr> encoders;
    encoders.emplace_back(std::make_unique<TPartEncoder1>(2));
    encoders.emplace_back(std::make_unique<TPartEncoder2<SomeBitFn1>>(16));
    encoders.emplace_back(std::make_unique<TPartEncoder3>(2));
    encoders.emplace_back(std::make_unique<TPartEncoder4>());

    NBitStream::TBitStream bs;
    TBitStreamEncoder encoder(std::move(encoders));
    encoder.Do(nullptr, bs);

    EXPECT_EQ(bs.GetSizeInBits(), 1000);
}

TEST(BsEncode, NotExactAlloc) {
    std::vector<IBitStreamPartEncoder::TPtr> encoders;
    encoders.emplace_back(std::make_unique<TPartEncoder1>(1));
    encoders.emplace_back(std::make_unique<TPartEncoder2<SomeBitFn2>>(11));
    encoders.emplace_back(std::make_unique<TPartEncoder3>(1));

    NBitStream::TBitStream bs;
    TBitStreamEncoder encoder(std::move(encoders));
    encoder.Do(nullptr, bs);

    EXPECT_EQ(bs.GetSizeInBits(), 993);
}