Lines Matching refs:bw
27 static int BitWriterResize(VP8BitWriter* const bw, size_t extra_size) { in BitWriterResize() argument
30 const uint64_t needed_size_64b = (uint64_t)bw->pos_ + extra_size; in BitWriterResize()
33 bw->error_ = 1; in BitWriterResize()
36 if (needed_size <= bw->max_pos_) return 1; in BitWriterResize()
38 new_size = 2 * bw->max_pos_; in BitWriterResize()
43 bw->error_ = 1; in BitWriterResize()
46 memcpy(new_buf, bw->buf_, bw->pos_); in BitWriterResize()
47 free(bw->buf_); in BitWriterResize()
48 bw->buf_ = new_buf; in BitWriterResize()
49 bw->max_pos_ = new_size; in BitWriterResize()
53 static void kFlush(VP8BitWriter* const bw) { in kFlush() argument
54 const int s = 8 + bw->nb_bits_; in kFlush()
55 const int32_t bits = bw->value_ >> s; in kFlush()
56 assert(bw->nb_bits_ >= 0); in kFlush()
57 bw->value_ -= bits << s; in kFlush()
58 bw->nb_bits_ -= 8; in kFlush()
60 size_t pos = bw->pos_; in kFlush()
61 if (!BitWriterResize(bw, bw->run_ + 1)) { in kFlush()
65 if (pos > 0) bw->buf_[pos - 1]++; in kFlush()
67 if (bw->run_ > 0) { in kFlush()
69 for (; bw->run_ > 0; --bw->run_) bw->buf_[pos++] = value; in kFlush()
71 bw->buf_[pos++] = bits; in kFlush()
72 bw->pos_ = pos; in kFlush()
74 bw->run_++; // delay writing of bytes 0xff, pending eventual carry. in kFlush()
106 int VP8PutBit(VP8BitWriter* const bw, int bit, int prob) { in VP8PutBit() argument
107 const int split = (bw->range_ * prob) >> 8; in VP8PutBit()
109 bw->value_ += split + 1; in VP8PutBit()
110 bw->range_ -= split + 1; in VP8PutBit()
112 bw->range_ = split; in VP8PutBit()
114 if (bw->range_ < 127) { // emit 'shift' bits out and renormalize in VP8PutBit()
115 const int shift = kNorm[bw->range_]; in VP8PutBit()
116 bw->range_ = kNewRange[bw->range_]; in VP8PutBit()
117 bw->value_ <<= shift; in VP8PutBit()
118 bw->nb_bits_ += shift; in VP8PutBit()
119 if (bw->nb_bits_ > 0) kFlush(bw); in VP8PutBit()
124 int VP8PutBitUniform(VP8BitWriter* const bw, int bit) { in VP8PutBitUniform() argument
125 const int split = bw->range_ >> 1; in VP8PutBitUniform()
127 bw->value_ += split + 1; in VP8PutBitUniform()
128 bw->range_ -= split + 1; in VP8PutBitUniform()
130 bw->range_ = split; in VP8PutBitUniform()
132 if (bw->range_ < 127) { in VP8PutBitUniform()
133 bw->range_ = kNewRange[bw->range_]; in VP8PutBitUniform()
134 bw->value_ <<= 1; in VP8PutBitUniform()
135 bw->nb_bits_ += 1; in VP8PutBitUniform()
136 if (bw->nb_bits_ > 0) kFlush(bw); in VP8PutBitUniform()
141 void VP8PutValue(VP8BitWriter* const bw, int value, int nb_bits) { in VP8PutValue() argument
144 VP8PutBitUniform(bw, value & mask); in VP8PutValue()
147 void VP8PutSignedValue(VP8BitWriter* const bw, int value, int nb_bits) { in VP8PutSignedValue() argument
148 if (!VP8PutBitUniform(bw, value != 0)) in VP8PutSignedValue()
151 VP8PutValue(bw, ((-value) << 1) | 1, nb_bits + 1); in VP8PutSignedValue()
153 VP8PutValue(bw, value << 1, nb_bits + 1); in VP8PutSignedValue()
159 int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size) { in VP8BitWriterInit() argument
160 bw->range_ = 255 - 1; in VP8BitWriterInit()
161 bw->value_ = 0; in VP8BitWriterInit()
162 bw->run_ = 0; in VP8BitWriterInit()
163 bw->nb_bits_ = -8; in VP8BitWriterInit()
164 bw->pos_ = 0; in VP8BitWriterInit()
165 bw->max_pos_ = 0; in VP8BitWriterInit()
166 bw->error_ = 0; in VP8BitWriterInit()
167 bw->buf_ = NULL; in VP8BitWriterInit()
168 return (expected_size > 0) ? BitWriterResize(bw, expected_size) : 1; in VP8BitWriterInit()
171 uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) { in VP8BitWriterFinish() argument
172 VP8PutValue(bw, 0, 9 - bw->nb_bits_); in VP8BitWriterFinish()
173 bw->nb_bits_ = 0; // pad with zeroes in VP8BitWriterFinish()
174 kFlush(bw); in VP8BitWriterFinish()
175 return bw->buf_; in VP8BitWriterFinish()
178 int VP8BitWriterAppend(VP8BitWriter* const bw, in VP8BitWriterAppend() argument
181 if (bw->nb_bits_ != -8) return 0; // kFlush() must have been called in VP8BitWriterAppend()
182 if (!BitWriterResize(bw, size)) return 0; in VP8BitWriterAppend()
183 memcpy(bw->buf_ + bw->pos_, data, size); in VP8BitWriterAppend()
184 bw->pos_ += size; in VP8BitWriterAppend()
188 void VP8BitWriterWipeOut(VP8BitWriter* const bw) { in VP8BitWriterWipeOut() argument
189 if (bw) { in VP8BitWriterWipeOut()
190 free(bw->buf_); in VP8BitWriterWipeOut()
191 memset(bw, 0, sizeof(*bw)); in VP8BitWriterWipeOut()
199 static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) { in VP8LBitWriterResize() argument
202 const size_t current_size = VP8LBitWriterNumBytes(bw); in VP8LBitWriterResize()
206 bw->error_ = 1; in VP8LBitWriterResize()
209 if (bw->max_bytes_ > 0 && size_required <= bw->max_bytes_) return 1; in VP8LBitWriterResize()
210 allocated_size = (3 * bw->max_bytes_) >> 1; in VP8LBitWriterResize()
216 bw->error_ = 1; in VP8LBitWriterResize()
219 memcpy(allocated_buf, bw->buf_, current_size); in VP8LBitWriterResize()
220 free(bw->buf_); in VP8LBitWriterResize()
221 bw->buf_ = allocated_buf; in VP8LBitWriterResize()
222 bw->max_bytes_ = allocated_size; in VP8LBitWriterResize()
227 int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size) { in VP8LBitWriterInit() argument
228 memset(bw, 0, sizeof(*bw)); in VP8LBitWriterInit()
229 return VP8LBitWriterResize(bw, expected_size); in VP8LBitWriterInit()
232 void VP8LBitWriterDestroy(VP8LBitWriter* const bw) { in VP8LBitWriterDestroy() argument
233 if (bw != NULL) { in VP8LBitWriterDestroy()
234 free(bw->buf_); in VP8LBitWriterDestroy()
235 memset(bw, 0, sizeof(*bw)); in VP8LBitWriterDestroy()
239 void VP8LWriteBits(VP8LBitWriter* const bw, int n_bits, uint32_t bits) { in VP8LWriteBits() argument
245 uint8_t* const p = &bw->buf_[bw->bit_pos_ >> 3]; in VP8LWriteBits()
247 v |= bits << (bw->bit_pos_ & 7); in VP8LWriteBits()
249 bw->bit_pos_ += n_bits; in VP8LWriteBits()
253 uint8_t* p = &bw->buf_[bw->bit_pos_ >> 3]; in VP8LWriteBits()
254 const int bits_reserved_in_first_byte = bw->bit_pos_ & 7; in VP8LWriteBits()
269 bw->bit_pos_ += n_bits; in VP8LWriteBits()
272 if ((bw->bit_pos_ >> 3) > (bw->max_bytes_ - 8)) { in VP8LWriteBits()
273 const uint64_t extra_size = 32768ULL + bw->max_bytes_; in VP8LWriteBits()
275 !VP8LBitWriterResize(bw, (size_t)extra_size)) { in VP8LWriteBits()
276 bw->bit_pos_ = 0; in VP8LWriteBits()
277 bw->error_ = 1; in VP8LWriteBits()