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