Path: blob/master/thirdparty/libwebp/src/utils/bit_writer_utils.c
9912 views
// Copyright 2011 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Bit writing and boolean coder10//11// Author: Skal ([email protected])12// Vikas Arora ([email protected])1314#include <assert.h>15#include <string.h> // for memcpy()16#include <stdlib.h>1718#include "src/utils/bit_writer_utils.h"19#include "src/utils/endian_inl_utils.h"20#include "src/utils/utils.h"2122//------------------------------------------------------------------------------23// VP8BitWriter2425static int BitWriterResize(VP8BitWriter* const bw, size_t extra_size) {26uint8_t* new_buf;27size_t new_size;28const uint64_t needed_size_64b = (uint64_t)bw->pos_ + extra_size;29const size_t needed_size = (size_t)needed_size_64b;30if (needed_size_64b != needed_size) {31bw->error_ = 1;32return 0;33}34if (needed_size <= bw->max_pos_) return 1;35// If the following line wraps over 32bit, the test just after will catch it.36new_size = 2 * bw->max_pos_;37if (new_size < needed_size) new_size = needed_size;38if (new_size < 1024) new_size = 1024;39new_buf = (uint8_t*)WebPSafeMalloc(1ULL, new_size);40if (new_buf == NULL) {41bw->error_ = 1;42return 0;43}44if (bw->pos_ > 0) {45assert(bw->buf_ != NULL);46memcpy(new_buf, bw->buf_, bw->pos_);47}48WebPSafeFree(bw->buf_);49bw->buf_ = new_buf;50bw->max_pos_ = new_size;51return 1;52}5354static void Flush(VP8BitWriter* const bw) {55const int s = 8 + bw->nb_bits_;56const int32_t bits = bw->value_ >> s;57assert(bw->nb_bits_ >= 0);58bw->value_ -= bits << s;59bw->nb_bits_ -= 8;60if ((bits & 0xff) != 0xff) {61size_t pos = bw->pos_;62if (!BitWriterResize(bw, bw->run_ + 1)) {63return;64}65if (bits & 0x100) { // overflow -> propagate carry over pending 0xff's66if (pos > 0) bw->buf_[pos - 1]++;67}68if (bw->run_ > 0) {69const int value = (bits & 0x100) ? 0x00 : 0xff;70for (; bw->run_ > 0; --bw->run_) bw->buf_[pos++] = value;71}72bw->buf_[pos++] = bits & 0xff;73bw->pos_ = pos;74} else {75bw->run_++; // delay writing of bytes 0xff, pending eventual carry.76}77}7879//------------------------------------------------------------------------------80// renormalization8182static const uint8_t kNorm[128] = { // renorm_sizes[i] = 8 - log2(i)837, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,843, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,852, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,862, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,871, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,881, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,891, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,901, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,91092};9394// range = ((range + 1) << kVP8Log2Range[range]) - 195static const uint8_t kNewRange[128] = {96127, 127, 191, 127, 159, 191, 223, 127, 143, 159, 175, 191, 207, 223, 239,97127, 135, 143, 151, 159, 167, 175, 183, 191, 199, 207, 215, 223, 231, 239,98247, 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179,99183, 187, 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239,100243, 247, 251, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149,101151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179,102181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209,103211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239,104241, 243, 245, 247, 249, 251, 253, 127105};106107int VP8PutBit(VP8BitWriter* const bw, int bit, int prob) {108const int split = (bw->range_ * prob) >> 8;109if (bit) {110bw->value_ += split + 1;111bw->range_ -= split + 1;112} else {113bw->range_ = split;114}115if (bw->range_ < 127) { // emit 'shift' bits out and renormalize116const int shift = kNorm[bw->range_];117bw->range_ = kNewRange[bw->range_];118bw->value_ <<= shift;119bw->nb_bits_ += shift;120if (bw->nb_bits_ > 0) Flush(bw);121}122return bit;123}124125int VP8PutBitUniform(VP8BitWriter* const bw, int bit) {126const int split = bw->range_ >> 1;127if (bit) {128bw->value_ += split + 1;129bw->range_ -= split + 1;130} else {131bw->range_ = split;132}133if (bw->range_ < 127) {134bw->range_ = kNewRange[bw->range_];135bw->value_ <<= 1;136bw->nb_bits_ += 1;137if (bw->nb_bits_ > 0) Flush(bw);138}139return bit;140}141142void VP8PutBits(VP8BitWriter* const bw, uint32_t value, int nb_bits) {143uint32_t mask;144assert(nb_bits > 0 && nb_bits < 32);145for (mask = 1u << (nb_bits - 1); mask; mask >>= 1) {146VP8PutBitUniform(bw, value & mask);147}148}149150void VP8PutSignedBits(VP8BitWriter* const bw, int value, int nb_bits) {151if (!VP8PutBitUniform(bw, value != 0)) return;152if (value < 0) {153VP8PutBits(bw, ((-value) << 1) | 1, nb_bits + 1);154} else {155VP8PutBits(bw, value << 1, nb_bits + 1);156}157}158159//------------------------------------------------------------------------------160161int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size) {162bw->range_ = 255 - 1;163bw->value_ = 0;164bw->run_ = 0;165bw->nb_bits_ = -8;166bw->pos_ = 0;167bw->max_pos_ = 0;168bw->error_ = 0;169bw->buf_ = NULL;170return (expected_size > 0) ? BitWriterResize(bw, expected_size) : 1;171}172173uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) {174VP8PutBits(bw, 0, 9 - bw->nb_bits_);175bw->nb_bits_ = 0; // pad with zeroes176Flush(bw);177return bw->buf_;178}179180int VP8BitWriterAppend(VP8BitWriter* const bw,181const uint8_t* data, size_t size) {182assert(data != NULL);183if (bw->nb_bits_ != -8) return 0; // Flush() must have been called184if (!BitWriterResize(bw, size)) return 0;185memcpy(bw->buf_ + bw->pos_, data, size);186bw->pos_ += size;187return 1;188}189190void VP8BitWriterWipeOut(VP8BitWriter* const bw) {191if (bw != NULL) {192WebPSafeFree(bw->buf_);193memset(bw, 0, sizeof(*bw));194}195}196197//------------------------------------------------------------------------------198// VP8LBitWriter199200// This is the minimum amount of size the memory buffer is guaranteed to grow201// when extra space is needed.202#define MIN_EXTRA_SIZE (32768ULL)203204// Returns 1 on success.205static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) {206uint8_t* allocated_buf;207size_t allocated_size;208const size_t max_bytes = bw->end_ - bw->buf_;209const size_t current_size = bw->cur_ - bw->buf_;210const uint64_t size_required_64b = (uint64_t)current_size + extra_size;211const size_t size_required = (size_t)size_required_64b;212if (size_required != size_required_64b) {213bw->error_ = 1;214return 0;215}216if (max_bytes > 0 && size_required <= max_bytes) return 1;217allocated_size = (3 * max_bytes) >> 1;218if (allocated_size < size_required) allocated_size = size_required;219// make allocated size multiple of 1k220allocated_size = (((allocated_size >> 10) + 1) << 10);221allocated_buf = (uint8_t*)WebPSafeMalloc(1ULL, allocated_size);222if (allocated_buf == NULL) {223bw->error_ = 1;224return 0;225}226if (current_size > 0) {227memcpy(allocated_buf, bw->buf_, current_size);228}229WebPSafeFree(bw->buf_);230bw->buf_ = allocated_buf;231bw->cur_ = bw->buf_ + current_size;232bw->end_ = bw->buf_ + allocated_size;233return 1;234}235236int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size) {237memset(bw, 0, sizeof(*bw));238return VP8LBitWriterResize(bw, expected_size);239}240241int VP8LBitWriterClone(const VP8LBitWriter* const src,242VP8LBitWriter* const dst) {243const size_t current_size = src->cur_ - src->buf_;244assert(src->cur_ >= src->buf_ && src->cur_ <= src->end_);245if (!VP8LBitWriterResize(dst, current_size)) return 0;246memcpy(dst->buf_, src->buf_, current_size);247dst->bits_ = src->bits_;248dst->used_ = src->used_;249dst->error_ = src->error_;250dst->cur_ = dst->buf_ + current_size;251return 1;252}253254void VP8LBitWriterWipeOut(VP8LBitWriter* const bw) {255if (bw != NULL) {256WebPSafeFree(bw->buf_);257memset(bw, 0, sizeof(*bw));258}259}260261void VP8LBitWriterReset(const VP8LBitWriter* const bw_init,262VP8LBitWriter* const bw) {263bw->bits_ = bw_init->bits_;264bw->used_ = bw_init->used_;265bw->cur_ = bw->buf_ + (bw_init->cur_ - bw_init->buf_);266assert(bw->cur_ <= bw->end_);267bw->error_ = bw_init->error_;268}269270void VP8LBitWriterSwap(VP8LBitWriter* const src, VP8LBitWriter* const dst) {271const VP8LBitWriter tmp = *src;272*src = *dst;273*dst = tmp;274}275276void VP8LPutBitsFlushBits(VP8LBitWriter* const bw) {277// If needed, make some room by flushing some bits out.278if (bw->cur_ + VP8L_WRITER_BYTES > bw->end_) {279const uint64_t extra_size = (bw->end_ - bw->buf_) + MIN_EXTRA_SIZE;280if (!CheckSizeOverflow(extra_size) ||281!VP8LBitWriterResize(bw, (size_t)extra_size)) {282bw->cur_ = bw->buf_;283bw->error_ = 1;284return;285}286}287*(vp8l_wtype_t*)bw->cur_ = (vp8l_wtype_t)WSWAP((vp8l_wtype_t)bw->bits_);288bw->cur_ += VP8L_WRITER_BYTES;289bw->bits_ >>= VP8L_WRITER_BITS;290bw->used_ -= VP8L_WRITER_BITS;291}292293void VP8LPutBitsInternal(VP8LBitWriter* const bw, uint32_t bits, int n_bits) {294assert(n_bits <= 32);295// That's the max we can handle:296assert(sizeof(vp8l_wtype_t) == 2);297if (n_bits > 0) {298vp8l_atype_t lbits = bw->bits_;299int used = bw->used_;300// Special case of overflow handling for 32bit accumulator (2-steps flush).301#if VP8L_WRITER_BITS == 16302if (used + n_bits >= VP8L_WRITER_MAX_BITS) {303// Fill up all the VP8L_WRITER_MAX_BITS so it can be flushed out below.304const int shift = VP8L_WRITER_MAX_BITS - used;305lbits |= (vp8l_atype_t)bits << used;306used = VP8L_WRITER_MAX_BITS;307n_bits -= shift;308bits >>= shift;309assert(n_bits <= VP8L_WRITER_MAX_BITS);310}311#endif312// If needed, make some room by flushing some bits out.313while (used >= VP8L_WRITER_BITS) {314if (bw->cur_ + VP8L_WRITER_BYTES > bw->end_) {315const uint64_t extra_size = (bw->end_ - bw->buf_) + MIN_EXTRA_SIZE;316if (!CheckSizeOverflow(extra_size) ||317!VP8LBitWriterResize(bw, (size_t)extra_size)) {318bw->cur_ = bw->buf_;319bw->error_ = 1;320return;321}322}323*(vp8l_wtype_t*)bw->cur_ = (vp8l_wtype_t)WSWAP((vp8l_wtype_t)lbits);324bw->cur_ += VP8L_WRITER_BYTES;325lbits >>= VP8L_WRITER_BITS;326used -= VP8L_WRITER_BITS;327}328bw->bits_ = lbits | ((vp8l_atype_t)bits << used);329bw->used_ = used + n_bits;330}331}332333uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw) {334// flush leftover bits335if (VP8LBitWriterResize(bw, (bw->used_ + 7) >> 3)) {336while (bw->used_ > 0) {337*bw->cur_++ = (uint8_t)bw->bits_;338bw->bits_ >>= 8;339bw->used_ -= 8;340}341bw->used_ = 0;342}343return bw->buf_;344}345346//------------------------------------------------------------------------------347348349