9117c4268675cadb9e0d182f33c88a5d17b7b661
1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Author: Steve Borho <steve@borho.org>
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.
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.
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.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
24 #ifndef X265_BITSTREAM_H
25 #define X265_BITSTREAM_H 1
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() {}
43 class BitCounter
: public BitInterface
47 uint32_t m_bitCounter
;
51 BitCounter() : m_bitCounter(0) {}
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() { }
62 class Bitstream
: public BitInterface
67 ~Bitstream() { X265_FREE(m_fifo
); }
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
; }
74 void write(uint32_t val
, uint32_t numBits
);
75 void writeByte(uint32_t val
);
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
85 uint32_t m_byteOccupancy
;
86 uint32_t m_partialByteBits
;
87 uint8_t m_partialByte
;
89 void push_back(uint8_t val
);
92 static const uint8_t bitSize
[256] =
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,
112 static inline int bs_size_ue(unsigned int val
)
114 return bitSize
[val
+ 1];
117 static inline int bs_size_ue_big(unsigned int val
)
120 return bitSize
[val
+ 1];
122 return bitSize
[(val
+ 1) >> 8] + 16;
125 static inline int bs_size_se(int val
)
127 int tmp
= 1 - val
* 2;
129 if (tmp
< 0) tmp
= val
* 2;
133 return bitSize
[tmp
>> 8] + 16;
136 class SyntaxElementWriter
140 BitInterface
* m_bitIf
;
142 SyntaxElementWriter() : m_bitIf(NULL
) {}
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
); }
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); }
158 #endif // ifndef X265_BITSTREAM_H