Lines Matching refs:bw
26 static int BitWriterResize(VP8BitWriter* const bw, size_t extra_size) { in BitWriterResize() argument
29 const uint64_t needed_size_64b = (uint64_t)bw->pos_ + extra_size; in BitWriterResize()
32 bw->error_ = 1; in BitWriterResize()
35 if (needed_size <= bw->max_pos_) return 1; in BitWriterResize()
37 new_size = 2 * bw->max_pos_; in BitWriterResize()
42 bw->error_ = 1; in BitWriterResize()
45 if (bw->pos_ > 0) { in BitWriterResize()
46 assert(bw->buf_ != NULL); in BitWriterResize()
47 memcpy(new_buf, bw->buf_, bw->pos_); in BitWriterResize()
49 WebPSafeFree(bw->buf_); in BitWriterResize()
50 bw->buf_ = new_buf; in BitWriterResize()
51 bw->max_pos_ = new_size; in BitWriterResize()
55 static void Flush(VP8BitWriter* const bw) { in Flush() argument
56 const int s = 8 + bw->nb_bits_; in Flush()
57 const int32_t bits = bw->value_ >> s; in Flush()
58 assert(bw->nb_bits_ >= 0); in Flush()
59 bw->value_ -= bits << s; in Flush()
60 bw->nb_bits_ -= 8; in Flush()
62 size_t pos = bw->pos_; in Flush()
63 if (!BitWriterResize(bw, bw->run_ + 1)) { in Flush()
67 if (pos > 0) bw->buf_[pos - 1]++; in Flush()
69 if (bw->run_ > 0) { in Flush()
71 for (; bw->run_ > 0; --bw->run_) bw->buf_[pos++] = value; in Flush()
73 bw->buf_[pos++] = bits; in Flush()
74 bw->pos_ = pos; in Flush()
76 bw->run_++; // delay writing of bytes 0xff, pending eventual carry. in Flush()
108 int VP8PutBit(VP8BitWriter* const bw, int bit, int prob) { in VP8PutBit() argument
109 const int split = (bw->range_ * prob) >> 8; in VP8PutBit()
111 bw->value_ += split + 1; in VP8PutBit()
112 bw->range_ -= split + 1; in VP8PutBit()
114 bw->range_ = split; in VP8PutBit()
116 if (bw->range_ < 127) { // emit 'shift' bits out and renormalize in VP8PutBit()
117 const int shift = kNorm[bw->range_]; in VP8PutBit()
118 bw->range_ = kNewRange[bw->range_]; in VP8PutBit()
119 bw->value_ <<= shift; in VP8PutBit()
120 bw->nb_bits_ += shift; in VP8PutBit()
121 if (bw->nb_bits_ > 0) Flush(bw); in VP8PutBit()
126 int VP8PutBitUniform(VP8BitWriter* const bw, int bit) { in VP8PutBitUniform() argument
127 const int split = bw->range_ >> 1; in VP8PutBitUniform()
129 bw->value_ += split + 1; in VP8PutBitUniform()
130 bw->range_ -= split + 1; in VP8PutBitUniform()
132 bw->range_ = split; in VP8PutBitUniform()
134 if (bw->range_ < 127) { in VP8PutBitUniform()
135 bw->range_ = kNewRange[bw->range_]; in VP8PutBitUniform()
136 bw->value_ <<= 1; in VP8PutBitUniform()
137 bw->nb_bits_ += 1; in VP8PutBitUniform()
138 if (bw->nb_bits_ > 0) Flush(bw); in VP8PutBitUniform()
143 void VP8PutBits(VP8BitWriter* const bw, uint32_t value, int nb_bits) { in VP8PutBits() argument
147 VP8PutBitUniform(bw, value & mask); in VP8PutBits()
150 void VP8PutSignedBits(VP8BitWriter* const bw, int value, int nb_bits) { in VP8PutSignedBits() argument
151 if (!VP8PutBitUniform(bw, value != 0)) in VP8PutSignedBits()
154 VP8PutBits(bw, ((-value) << 1) | 1, nb_bits + 1); in VP8PutSignedBits()
156 VP8PutBits(bw, value << 1, nb_bits + 1); in VP8PutSignedBits()
162 int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size) { in VP8BitWriterInit() argument
163 bw->range_ = 255 - 1; in VP8BitWriterInit()
164 bw->value_ = 0; in VP8BitWriterInit()
165 bw->run_ = 0; in VP8BitWriterInit()
166 bw->nb_bits_ = -8; in VP8BitWriterInit()
167 bw->pos_ = 0; in VP8BitWriterInit()
168 bw->max_pos_ = 0; in VP8BitWriterInit()
169 bw->error_ = 0; in VP8BitWriterInit()
170 bw->buf_ = NULL; in VP8BitWriterInit()
171 return (expected_size > 0) ? BitWriterResize(bw, expected_size) : 1; in VP8BitWriterInit()
174 uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) { in VP8BitWriterFinish() argument
175 VP8PutBits(bw, 0, 9 - bw->nb_bits_); in VP8BitWriterFinish()
176 bw->nb_bits_ = 0; // pad with zeroes in VP8BitWriterFinish()
177 Flush(bw); in VP8BitWriterFinish()
178 return bw->buf_; in VP8BitWriterFinish()
181 int VP8BitWriterAppend(VP8BitWriter* const bw, in VP8BitWriterAppend() argument
184 if (bw->nb_bits_ != -8) return 0; // Flush() must have been called in VP8BitWriterAppend()
185 if (!BitWriterResize(bw, size)) return 0; in VP8BitWriterAppend()
186 memcpy(bw->buf_ + bw->pos_, data, size); in VP8BitWriterAppend()
187 bw->pos_ += size; in VP8BitWriterAppend()
191 void VP8BitWriterWipeOut(VP8BitWriter* const bw) { in VP8BitWriterWipeOut() argument
192 if (bw != NULL) { in VP8BitWriterWipeOut()
193 WebPSafeFree(bw->buf_); in VP8BitWriterWipeOut()
194 memset(bw, 0, sizeof(*bw)); in VP8BitWriterWipeOut()
206 static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) { in VP8LBitWriterResize() argument
209 const size_t max_bytes = bw->end_ - bw->buf_; in VP8LBitWriterResize()
210 const size_t current_size = bw->cur_ - bw->buf_; in VP8LBitWriterResize()
214 bw->error_ = 1; in VP8LBitWriterResize()
224 bw->error_ = 1; in VP8LBitWriterResize()
228 memcpy(allocated_buf, bw->buf_, current_size); in VP8LBitWriterResize()
230 WebPSafeFree(bw->buf_); in VP8LBitWriterResize()
231 bw->buf_ = allocated_buf; in VP8LBitWriterResize()
232 bw->cur_ = bw->buf_ + current_size; in VP8LBitWriterResize()
233 bw->end_ = bw->buf_ + allocated_size; in VP8LBitWriterResize()
237 int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size) { in VP8LBitWriterInit() argument
238 memset(bw, 0, sizeof(*bw)); in VP8LBitWriterInit()
239 return VP8LBitWriterResize(bw, expected_size); in VP8LBitWriterInit()
242 void VP8LBitWriterWipeOut(VP8LBitWriter* const bw) { in VP8LBitWriterWipeOut() argument
243 if (bw != NULL) { in VP8LBitWriterWipeOut()
244 WebPSafeFree(bw->buf_); in VP8LBitWriterWipeOut()
245 memset(bw, 0, sizeof(*bw)); in VP8LBitWriterWipeOut()
249 void VP8LPutBitsFlushBits(VP8LBitWriter* const bw) { in VP8LPutBitsFlushBits() argument
251 if (bw->cur_ + VP8L_WRITER_BYTES > bw->end_) { in VP8LPutBitsFlushBits()
252 const uint64_t extra_size = (bw->end_ - bw->buf_) + MIN_EXTRA_SIZE; in VP8LPutBitsFlushBits()
254 !VP8LBitWriterResize(bw, (size_t)extra_size)) { in VP8LPutBitsFlushBits()
255 bw->cur_ = bw->buf_; in VP8LPutBitsFlushBits()
256 bw->error_ = 1; in VP8LPutBitsFlushBits()
260 *(vp8l_wtype_t*)bw->cur_ = (vp8l_wtype_t)WSWAP((vp8l_wtype_t)bw->bits_); in VP8LPutBitsFlushBits()
261 bw->cur_ += VP8L_WRITER_BYTES; in VP8LPutBitsFlushBits()
262 bw->bits_ >>= VP8L_WRITER_BITS; in VP8LPutBitsFlushBits()
263 bw->used_ -= VP8L_WRITER_BITS; in VP8LPutBitsFlushBits()
266 void VP8LPutBitsInternal(VP8LBitWriter* const bw, uint32_t bits, int n_bits) { in VP8LPutBitsInternal() argument
271 vp8l_atype_t lbits = bw->bits_; in VP8LPutBitsInternal()
272 int used = bw->used_; in VP8LPutBitsInternal()
287 if (bw->cur_ + VP8L_WRITER_BYTES > bw->end_) { in VP8LPutBitsInternal()
288 const uint64_t extra_size = (bw->end_ - bw->buf_) + MIN_EXTRA_SIZE; in VP8LPutBitsInternal()
290 !VP8LBitWriterResize(bw, (size_t)extra_size)) { in VP8LPutBitsInternal()
291 bw->cur_ = bw->buf_; in VP8LPutBitsInternal()
292 bw->error_ = 1; in VP8LPutBitsInternal()
296 *(vp8l_wtype_t*)bw->cur_ = (vp8l_wtype_t)WSWAP((vp8l_wtype_t)lbits); in VP8LPutBitsInternal()
297 bw->cur_ += VP8L_WRITER_BYTES; in VP8LPutBitsInternal()
301 bw->bits_ = lbits | ((vp8l_atype_t)bits << used); in VP8LPutBitsInternal()
302 bw->used_ = used + n_bits; in VP8LPutBitsInternal()
306 uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw) { in VP8LBitWriterFinish() argument
308 if (VP8LBitWriterResize(bw, (bw->used_ + 7) >> 3)) { in VP8LBitWriterFinish()
309 while (bw->used_ > 0) { in VP8LBitWriterFinish()
310 *bw->cur_++ = (uint8_t)bw->bits_; in VP8LBitWriterFinish()
311 bw->bits_ >>= 8; in VP8LBitWriterFinish()
312 bw->used_ -= 8; in VP8LBitWriterFinish()
314 bw->used_ = 0; in VP8LBitWriterFinish()
316 return bw->buf_; in VP8LBitWriterFinish()