Coverage Report

Created: 2022-08-24 06:11

/src/x265/source/common/bitstream.h
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * Copyright (C) 2013-2020 MulticoreWare, Inc
3
 *
4
 * Author: Steve Borho <steve@borho.org>
5
 *         Min Chen <chenm003@163.com>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
20
 *
21
 * This program is also available under a commercial proprietary license.
22
 * For more information, contact us at license @ x265.com.
23
 *****************************************************************************/
24
25
#ifndef X265_BITSTREAM_H
26
#define X265_BITSTREAM_H 1
27
28
namespace X265_NS {
29
// private namespace
30
31
class BitInterface
32
{
33
public:
34
35
    virtual void     write(uint32_t val, uint32_t numBits)  = 0;
36
    virtual void     writeByte(uint32_t val)                = 0;
37
    virtual void     resetBits()                            = 0;
38
    virtual uint32_t getNumberOfWrittenBits() const         = 0;
39
    virtual void     writeAlignOne()                        = 0;
40
    virtual void     writeAlignZero()                       = 0;
41
0
    virtual ~BitInterface() {}
42
};
43
44
class BitCounter : public BitInterface
45
{
46
protected:
47
48
    uint32_t  m_bitCounter;
49
50
public:
51
52
0
    BitCounter() : m_bitCounter(0) {}
53
54
0
    void     write(uint32_t, uint32_t num)  { m_bitCounter += num; }
55
0
    void     writeByte(uint32_t)            { m_bitCounter += 8;   }
56
0
    void     resetBits()                    { m_bitCounter = 0;    }
57
0
    uint32_t getNumberOfWrittenBits() const { return m_bitCounter; }
58
0
    void     writeAlignOne()                { }
59
0
    void     writeAlignZero()               { }
60
};
61
62
63
class Bitstream : public BitInterface
64
{
65
public:
66
67
    Bitstream();
68
0
    ~Bitstream()                             { X265_FREE(m_fifo); }
69
70
0
    void     resetBits()                     { m_partialByteBits = m_byteOccupancy = 0; m_partialByte = 0; }
71
0
    uint32_t getNumberOfWrittenBytes() const { return m_byteOccupancy; }
72
0
    uint32_t getNumberOfWrittenBits()  const { return m_byteOccupancy * 8 + m_partialByteBits; }
73
0
    const uint8_t* getFIFO() const           { return m_fifo; }
74
0
    void     copyBits(Bitstream* stream)     { m_partialByteBits = stream->m_partialByteBits; m_byteOccupancy = stream->m_byteOccupancy; m_partialByte = stream->m_partialByte; }
75
76
    void     write(uint32_t val, uint32_t numBits);
77
    void     writeByte(uint32_t val);
78
79
    void     writeAlignOne();      // insert one bits until the bitstream is byte-aligned
80
    void     writeAlignZero();     // insert zero bits until the bitstream is byte-aligned
81
    void     writeByteAlignment(); // insert 1 bit, then pad to byte-align with zero
82
83
private:
84
85
    uint8_t *m_fifo;
86
    uint32_t m_byteAlloc;
87
    uint32_t m_byteOccupancy;
88
    uint32_t m_partialByteBits;
89
    uint8_t  m_partialByte;
90
91
    void     push_back(uint8_t val);
92
};
93
94
static const uint8_t bitSize[256] =
95
{
96
    1, 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7,
97
    9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
98
    11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
99
    11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
100
    13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
101
    13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
102
    13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
103
    13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
104
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
105
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
106
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
107
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
108
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
109
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
110
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
111
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
112
};
113
114
static inline int bs_size_ue(unsigned int val)
115
0
{
116
0
    return bitSize[val + 1];
117
0
}
Unexecuted instantiation: api.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: bitstream.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: framefilter.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: sao.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: entropy.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: encoder.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: slicetype.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: frameencoder.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: nal.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: sei.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: ratecontrol.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: weightPrediction.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: analysis.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: search.cpp:x265::bs_size_ue(unsigned int)
Unexecuted instantiation: quant.cpp:x265::bs_size_ue(unsigned int)
118
119
static inline int bs_size_ue_big(unsigned int val)
120
0
{
121
0
    if (val < 255)
122
0
        return bitSize[val + 1];
123
0
    else
124
0
        return bitSize[(val + 1) >> 8] + 16;
125
0
}
Unexecuted instantiation: api.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: bitstream.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: framefilter.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: sao.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: entropy.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: encoder.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: slicetype.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: frameencoder.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: nal.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: sei.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: ratecontrol.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: weightPrediction.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: analysis.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: search.cpp:x265::bs_size_ue_big(unsigned int)
Unexecuted instantiation: quant.cpp:x265::bs_size_ue_big(unsigned int)
126
127
static inline int bs_size_se(int val)
128
0
{
129
0
    int tmp = 1 - val * 2;
130
131
0
    if (tmp < 0) tmp = val * 2;
132
0
    if (tmp < 256)
133
0
        return bitSize[tmp];
134
0
    else
135
0
        return bitSize[tmp >> 8] + 16;
136
0
}
Unexecuted instantiation: api.cpp:x265::bs_size_se(int)
Unexecuted instantiation: bitstream.cpp:x265::bs_size_se(int)
Unexecuted instantiation: framefilter.cpp:x265::bs_size_se(int)
Unexecuted instantiation: sao.cpp:x265::bs_size_se(int)
Unexecuted instantiation: entropy.cpp:x265::bs_size_se(int)
Unexecuted instantiation: encoder.cpp:x265::bs_size_se(int)
Unexecuted instantiation: slicetype.cpp:x265::bs_size_se(int)
Unexecuted instantiation: frameencoder.cpp:x265::bs_size_se(int)
Unexecuted instantiation: nal.cpp:x265::bs_size_se(int)
Unexecuted instantiation: sei.cpp:x265::bs_size_se(int)
Unexecuted instantiation: ratecontrol.cpp:x265::bs_size_se(int)
Unexecuted instantiation: weightPrediction.cpp:x265::bs_size_se(int)
Unexecuted instantiation: analysis.cpp:x265::bs_size_se(int)
Unexecuted instantiation: search.cpp:x265::bs_size_se(int)
Unexecuted instantiation: quant.cpp:x265::bs_size_se(int)
137
138
class SyntaxElementWriter
139
{
140
public:
141
142
    BitInterface* m_bitIf;
143
144
0
    SyntaxElementWriter() : m_bitIf(NULL) {}
145
146
    /* silently discard the name of the syntax element */
147
0
    inline void WRITE_CODE(uint32_t code, uint32_t length, const char *) { writeCode(code, length); }
148
0
    inline void WRITE_UVLC(uint32_t code,                  const char *) { writeUvlc(code); }
149
0
    inline void WRITE_SVLC(int32_t  code,                  const char *) { writeSvlc(code); }
150
0
    inline void WRITE_FLAG(bool flag,                      const char *) { writeFlag(flag); }
151
152
0
    void writeCode(uint32_t code, uint32_t length) { m_bitIf->write(code, length); }
153
    void writeUvlc(uint32_t code);
154
0
    void writeSvlc(int32_t code)                   { uint32_t ucode = (code <= 0) ? -code << 1 : (code << 1) - 1; writeUvlc(ucode); }
155
0
    void writeFlag(bool code)                      { m_bitIf->write(code, 1); }
156
};
157
158
}
159
160
#endif // ifndef X265_BITSTREAM_H