7 #pragma warning(disable: 4244)
10 #define MIN_FIFO_SIZE 1000
12 Bitstream::Bitstream()
14 m_fifo
= X265_MALLOC(uint8_t, MIN_FIFO_SIZE
);
15 m_byteAlloc
= MIN_FIFO_SIZE
;
19 void Bitstream::push_back(uint8_t val
)
24 if (m_byteOccupancy
>= m_byteAlloc
)
26 /** reallocate buffer with doubled size */
27 uint8_t *temp
= X265_MALLOC(uint8_t, m_byteAlloc
* 2);
30 ::memcpy(temp
, m_fifo
, m_byteOccupancy
);
37 x265_log(NULL
, X265_LOG_ERROR
, "Unable to realloc bitstream buffer");
41 m_fifo
[m_byteOccupancy
++] = val
;
44 void Bitstream::write(uint32_t val
, uint32_t numBits
)
46 X265_CHECK(numBits
<= 32, "numBits out of range\n");
47 X265_CHECK(numBits
== 32 || ((val
& (~0 << numBits
)) == 0), "numBits & val out of range\n");
49 uint32_t totalPartialBits
= m_partialByteBits
+ numBits
;
50 uint32_t nextPartialBits
= totalPartialBits
& 7;
51 uint8_t nextHeldByte
= val
<< (8 - nextPartialBits
);
52 uint32_t writeBytes
= totalPartialBits
>> 3;
56 /* topword aligns m_partialByte with the msb of val */
57 uint32_t topword
= (numBits
- nextPartialBits
) & ~7;
58 uint32_t write_bits
= (m_partialByte
<< topword
) | (val
>> nextPartialBits
);
62 case 4: push_back(write_bits
>> 24);
63 case 3: push_back(write_bits
>> 16);
64 case 2: push_back(write_bits
>> 8);
65 case 1: push_back(write_bits
);
68 m_partialByte
= nextHeldByte
;
69 m_partialByteBits
= nextPartialBits
;
73 m_partialByte
|= nextHeldByte
;
74 m_partialByteBits
= nextPartialBits
;
78 void Bitstream::writeByte(uint32_t val
)
80 // Only CABAC will call writeByte, the fifo must be byte aligned
81 X265_CHECK(!m_partialByteBits
, "expecting m_partialByteBits = 0\n");
86 void Bitstream::writeAlignOne()
88 uint32_t numBits
= (8 - m_partialByteBits
) & 0x7;
90 write((1 << numBits
) - 1, numBits
);
93 void Bitstream::writeAlignZero()
95 if (m_partialByteBits
)
97 push_back(m_partialByte
);
99 m_partialByteBits
= 0;
103 void Bitstream::writeByteAlignment()
109 void SyntaxElementWriter::writeUvlc(uint32_t code
)
112 uint32_t temp
= ++code
;
114 X265_CHECK(temp
, "writing -1 code, will cause infinite loop\n");
122 // Take care of cases where length > 32
123 m_bitIf
->write(0, length
>> 1);
124 m_bitIf
->write(code
, (length
+ 1) >> 1);