1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
10 // Bit writing and boolean coder
12 // Author: Skal (pascal.massimino@gmail.com)
13 // Vikas Arora (vikaas.arora@gmail.com)
16 #include <string.h> // for memcpy()
18 #include "./bit_writer.h"
20 #if defined(__cplusplus) || defined(c_plusplus)
24 //------------------------------------------------------------------------------
27 static int BitWriterResize(VP8BitWriter* const bw, size_t extra_size) {
30 const uint64_t needed_size_64b = (uint64_t)bw->pos_ + extra_size;
31 const size_t needed_size = (size_t)needed_size_64b;
32 if (needed_size_64b != needed_size) {
36 if (needed_size <= bw->max_pos_) return 1;
37 // If the following line wraps over 32bit, the test just after will catch it.
38 new_size = 2 * bw->max_pos_;
39 if (new_size < needed_size) new_size = needed_size;
40 if (new_size < 1024) new_size = 1024;
41 new_buf = (uint8_t*)malloc(new_size);
42 if (new_buf == NULL) {
46 memcpy(new_buf, bw->buf_, bw->pos_);
49 bw->max_pos_ = new_size;
53 static void kFlush(VP8BitWriter* const bw) {
54 const int s = 8 + bw->nb_bits_;
55 const int32_t bits = bw->value_ >> s;
56 assert(bw->nb_bits_ >= 0);
57 bw->value_ -= bits << s;
59 if ((bits & 0xff) != 0xff) {
60 size_t pos = bw->pos_;
61 if (!BitWriterResize(bw, bw->run_ + 1)) {
64 if (bits & 0x100) { // overflow -> propagate carry over pending 0xff's
65 if (pos > 0) bw->buf_[pos - 1]++;
68 const int value = (bits & 0x100) ? 0x00 : 0xff;
69 for (; bw->run_ > 0; --bw->run_) bw->buf_[pos++] = value;
71 bw->buf_[pos++] = bits;
74 bw->run_++; // delay writing of bytes 0xff, pending eventual carry.
78 //------------------------------------------------------------------------------
81 static const uint8_t kNorm[128] = { // renorm_sizes[i] = 8 - log2(i)
82 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
83 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
84 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
85 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
86 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
87 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
88 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
89 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
93 // range = ((range + 1) << kVP8Log2Range[range]) - 1
94 static const uint8_t kNewRange[128] = {
95 127, 127, 191, 127, 159, 191, 223, 127, 143, 159, 175, 191, 207, 223, 239,
96 127, 135, 143, 151, 159, 167, 175, 183, 191, 199, 207, 215, 223, 231, 239,
97 247, 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179,
98 183, 187, 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239,
99 243, 247, 251, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149,
100 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179,
101 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209,
102 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239,
103 241, 243, 245, 247, 249, 251, 253, 127
106 int VP8PutBit(VP8BitWriter* const bw, int bit, int prob) {
107 const int split = (bw->range_ * prob) >> 8;
109 bw->value_ += split + 1;
110 bw->range_ -= split + 1;
114 if (bw->range_ < 127) { // emit 'shift' bits out and renormalize
115 const int shift = kNorm[bw->range_];
116 bw->range_ = kNewRange[bw->range_];
117 bw->value_ <<= shift;
118 bw->nb_bits_ += shift;
119 if (bw->nb_bits_ > 0) kFlush(bw);
124 int VP8PutBitUniform(VP8BitWriter* const bw, int bit) {
125 const int split = bw->range_ >> 1;
127 bw->value_ += split + 1;
128 bw->range_ -= split + 1;
132 if (bw->range_ < 127) {
133 bw->range_ = kNewRange[bw->range_];
136 if (bw->nb_bits_ > 0) kFlush(bw);
141 void VP8PutValue(VP8BitWriter* const bw, int value, int nb_bits) {
143 for (mask = 1 << (nb_bits - 1); mask; mask >>= 1)
144 VP8PutBitUniform(bw, value & mask);
147 void VP8PutSignedValue(VP8BitWriter* const bw, int value, int nb_bits) {
148 if (!VP8PutBitUniform(bw, value != 0))
151 VP8PutValue(bw, ((-value) << 1) | 1, nb_bits + 1);
153 VP8PutValue(bw, value << 1, nb_bits + 1);
157 //------------------------------------------------------------------------------
159 int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size) {
160 bw->range_ = 255 - 1;
168 return (expected_size > 0) ? BitWriterResize(bw, expected_size) : 1;
171 uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) {
172 VP8PutValue(bw, 0, 9 - bw->nb_bits_);
173 bw->nb_bits_ = 0; // pad with zeroes
178 int VP8BitWriterAppend(VP8BitWriter* const bw,
179 const uint8_t* data, size_t size) {
181 if (bw->nb_bits_ != -8) return 0; // kFlush() must have been called
182 if (!BitWriterResize(bw, size)) return 0;
183 memcpy(bw->buf_ + bw->pos_, data, size);
188 void VP8BitWriterWipeOut(VP8BitWriter* const bw) {
191 memset(bw, 0, sizeof(*bw));
195 //------------------------------------------------------------------------------
198 // Returns 1 on success.
199 static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) {
200 uint8_t* allocated_buf;
201 size_t allocated_size;
202 const size_t current_size = VP8LBitWriterNumBytes(bw);
203 const uint64_t size_required_64b = (uint64_t)current_size + extra_size;
204 const size_t size_required = (size_t)size_required_64b;
205 if (size_required != size_required_64b) {
209 if (bw->max_bytes_ > 0 && size_required <= bw->max_bytes_) return 1;
210 allocated_size = (3 * bw->max_bytes_) >> 1;
211 if (allocated_size < size_required) allocated_size = size_required;
212 // make allocated size multiple of 1k
213 allocated_size = (((allocated_size >> 10) + 1) << 10);
214 allocated_buf = (uint8_t*)malloc(allocated_size);
215 if (allocated_buf == NULL) {
219 memcpy(allocated_buf, bw->buf_, current_size);
221 bw->buf_ = allocated_buf;
222 bw->max_bytes_ = allocated_size;
223 memset(allocated_buf + current_size, 0, allocated_size - current_size);
227 int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size) {
228 memset(bw, 0, sizeof(*bw));
229 return VP8LBitWriterResize(bw, expected_size);
232 void VP8LBitWriterDestroy(VP8LBitWriter* const bw) {
235 memset(bw, 0, sizeof(*bw));
239 void VP8LWriteBits(VP8LBitWriter* const bw, int n_bits, uint32_t bits) {
240 if (n_bits < 1) return;
241 #if !defined(__BIG_ENDIAN__)
242 // Technically, this branch of the code can write up to 25 bits at a time,
243 // but in prefix encoding, the maximum number of bits written is 18 at a time.
245 uint8_t* const p = &bw->buf_[bw->bit_pos_ >> 3];
246 uint32_t v = *(const uint32_t*)p;
247 v |= bits << (bw->bit_pos_ & 7);
249 bw->bit_pos_ += n_bits;
253 uint8_t* p = &bw->buf_[bw->bit_pos_ >> 3];
254 const int bits_reserved_in_first_byte = bw->bit_pos_ & 7;
255 const int bits_left_to_write = n_bits - 8 + bits_reserved_in_first_byte;
256 // implicit & 0xff is assumed for uint8_t arithmetics
257 *p++ |= bits << bits_reserved_in_first_byte;
258 bits >>= 8 - bits_reserved_in_first_byte;
259 if (bits_left_to_write >= 1) {
262 if (bits_left_to_write >= 9) {
267 assert(n_bits <= 25);
269 bw->bit_pos_ += n_bits;
272 if ((bw->bit_pos_ >> 3) > (bw->max_bytes_ - 8)) {
273 const uint64_t extra_size = 32768ULL + bw->max_bytes_;
274 if (extra_size != (size_t)extra_size ||
275 !VP8LBitWriterResize(bw, (size_t)extra_size)) {
282 //------------------------------------------------------------------------------
284 #if defined(__cplusplus) || defined(c_plusplus)