Path: blob/master/thirdparty/libwebp/src/utils/bit_writer_utils.c
21136 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 <stdlib.h>16#include <string.h> // for memcpy()1718#include "src/utils/bit_writer_utils.h"19#include "src/webp/types.h"20#include "src/utils/endian_inl_utils.h"21#include "src/utils/utils.h"2223//------------------------------------------------------------------------------24// VP8BitWriter2526static int BitWriterResize(VP8BitWriter* const bw, size_t extra_size) {27uint8_t* new_buf;28size_t new_size;29const uint64_t needed_size_64b = (uint64_t)bw->pos + extra_size;30const size_t needed_size = (size_t)needed_size_64b;31if (needed_size_64b != needed_size) {32bw->error = 1;33return 0;34}35if (needed_size <= bw->max_pos) return 1;36// If the following line wraps over 32bit, the test just after will catch it.37new_size = 2 * bw->max_pos;38if (new_size < needed_size) new_size = needed_size;39if (new_size < 1024) new_size = 1024;40new_buf = (uint8_t*)WebPSafeMalloc(1ULL, new_size);41if (new_buf == NULL) {42bw->error = 1;43return 0;44}45if (bw->pos > 0) {46assert(bw->buf != NULL);47memcpy(new_buf, bw->buf, bw->pos);48}49WebPSafeFree(bw->buf);50bw->buf = new_buf;51bw->max_pos = new_size;52return 1;53}5455static void Flush(VP8BitWriter* const bw) {56const int s = 8 + bw->nb_bits;57const int32_t bits = bw->value >> s;58assert(bw->nb_bits >= 0);59bw->value -= bits << s;60bw->nb_bits -= 8;61if ((bits & 0xff) != 0xff) {62size_t pos = bw->pos;63if (!BitWriterResize(bw, bw->run + 1)) {64return;65}66if (bits & 0x100) { // overflow -> propagate carry over pending 0xff's67if (pos > 0) bw->buf[pos - 1]++;68}69if (bw->run > 0) {70const int value = (bits & 0x100) ? 0x00 : 0xff;71for (; bw->run > 0; --bw->run) bw->buf[pos++] = value;72}73bw->buf[pos++] = bits & 0xff;74bw->pos = pos;75} else {76bw->run++; // delay writing of bytes 0xff, pending eventual carry.77}78}7980//------------------------------------------------------------------------------81// renormalization8283static const uint8_t kNorm[128] = { // renorm_sizes[i] = 8 - log2(i)847, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,853, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,862, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,872, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,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,911, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,92093};9495// range = ((range + 1) << kVP8Log2Range[range]) - 196static const uint8_t kNewRange[128] = {97127, 127, 191, 127, 159, 191, 223, 127, 143, 159, 175, 191, 207, 223, 239,98127, 135, 143, 151, 159, 167, 175, 183, 191, 199, 207, 215, 223, 231, 239,99247, 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179,100183, 187, 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239,101243, 247, 251, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149,102151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179,103181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209,104211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239,105241, 243, 245, 247, 249, 251, 253, 127106};107108int VP8PutBit(VP8BitWriter* const bw, int bit, int prob) {109const int split = (bw->range * prob) >> 8;110if (bit) {111bw->value += split + 1;112bw->range -= split + 1;113} else {114bw->range = split;115}116if (bw->range < 127) { // emit 'shift' bits out and renormalize117const int shift = kNorm[bw->range];118bw->range = kNewRange[bw->range];119bw->value <<= shift;120bw->nb_bits += shift;121if (bw->nb_bits > 0) Flush(bw);122}123return bit;124}125126int VP8PutBitUniform(VP8BitWriter* const bw, int bit) {127const int split = bw->range >> 1;128if (bit) {129bw->value += split + 1;130bw->range -= split + 1;131} else {132bw->range = split;133}134if (bw->range < 127) {135bw->range = kNewRange[bw->range];136bw->value <<= 1;137bw->nb_bits += 1;138if (bw->nb_bits > 0) Flush(bw);139}140return bit;141}142143void VP8PutBits(VP8BitWriter* const bw, uint32_t value, int nb_bits) {144uint32_t mask;145assert(nb_bits > 0 && nb_bits < 32);146for (mask = 1u << (nb_bits - 1); mask; mask >>= 1) {147VP8PutBitUniform(bw, value & mask);148}149}150151void VP8PutSignedBits(VP8BitWriter* const bw, int value, int nb_bits) {152if (!VP8PutBitUniform(bw, value != 0)) return;153if (value < 0) {154VP8PutBits(bw, ((-value) << 1) | 1, nb_bits + 1);155} else {156VP8PutBits(bw, value << 1, nb_bits + 1);157}158}159160//------------------------------------------------------------------------------161162int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size) {163bw->range = 255 - 1;164bw->value = 0;165bw->run = 0;166bw->nb_bits = -8;167bw->pos = 0;168bw->max_pos = 0;169bw->error = 0;170bw->buf = NULL;171return (expected_size > 0) ? BitWriterResize(bw, expected_size) : 1;172}173174uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) {175VP8PutBits(bw, 0, 9 - bw->nb_bits);176bw->nb_bits = 0; // pad with zeroes177Flush(bw);178return bw->buf;179}180181int VP8BitWriterAppend(VP8BitWriter* const bw,182const uint8_t* data, size_t size) {183assert(data != NULL);184if (bw->nb_bits != -8) return 0; // Flush() must have been called185if (!BitWriterResize(bw, size)) return 0;186memcpy(bw->buf + bw->pos, data, size);187bw->pos += size;188return 1;189}190191void VP8BitWriterWipeOut(VP8BitWriter* const bw) {192if (bw != NULL) {193WebPSafeFree(bw->buf);194memset(bw, 0, sizeof(*bw));195}196}197198//------------------------------------------------------------------------------199// VP8LBitWriter200201// This is the minimum amount of size the memory buffer is guaranteed to grow202// when extra space is needed.203#define MIN_EXTRA_SIZE (32768ULL)204205// Returns 1 on success.206static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) {207uint8_t* allocated_buf;208size_t allocated_size;209const size_t max_bytes = bw->end - bw->buf;210const size_t current_size = bw->cur - bw->buf;211const uint64_t size_required_64b = (uint64_t)current_size + extra_size;212const size_t size_required = (size_t)size_required_64b;213if (size_required != size_required_64b) {214bw->error = 1;215return 0;216}217if (max_bytes > 0 && size_required <= max_bytes) return 1;218allocated_size = (3 * max_bytes) >> 1;219if (allocated_size < size_required) allocated_size = size_required;220// make allocated size multiple of 1k221allocated_size = (((allocated_size >> 10) + 1) << 10);222allocated_buf = (uint8_t*)WebPSafeMalloc(1ULL, allocated_size);223if (allocated_buf == NULL) {224bw->error = 1;225return 0;226}227if (current_size > 0) {228memcpy(allocated_buf, bw->buf, current_size);229}230WebPSafeFree(bw->buf);231bw->buf = allocated_buf;232bw->cur = bw->buf + current_size;233bw->end = bw->buf + allocated_size;234return 1;235}236237int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size) {238memset(bw, 0, sizeof(*bw));239return VP8LBitWriterResize(bw, expected_size);240}241242int VP8LBitWriterClone(const VP8LBitWriter* const src,243VP8LBitWriter* const dst) {244const size_t current_size = src->cur - src->buf;245assert(src->cur >= src->buf && src->cur <= src->end);246if (!VP8LBitWriterResize(dst, current_size)) return 0;247memcpy(dst->buf, src->buf, current_size);248dst->bits = src->bits;249dst->used = src->used;250dst->error = src->error;251dst->cur = dst->buf + current_size;252return 1;253}254255void VP8LBitWriterWipeOut(VP8LBitWriter* const bw) {256if (bw != NULL) {257WebPSafeFree(bw->buf);258memset(bw, 0, sizeof(*bw));259}260}261262void VP8LBitWriterReset(const VP8LBitWriter* const bw_init,263VP8LBitWriter* const bw) {264bw->bits = bw_init->bits;265bw->used = bw_init->used;266bw->cur = bw->buf + (bw_init->cur - bw_init->buf);267assert(bw->cur <= bw->end);268bw->error = bw_init->error;269}270271void VP8LBitWriterSwap(VP8LBitWriter* const src, VP8LBitWriter* const dst) {272const VP8LBitWriter tmp = *src;273*src = *dst;274*dst = tmp;275}276277void VP8LPutBitsFlushBits(VP8LBitWriter* const bw) {278// If needed, make some room by flushing some bits out.279if (bw->cur + VP8L_WRITER_BYTES > bw->end) {280const uint64_t extra_size = (bw->end - bw->buf) + MIN_EXTRA_SIZE;281if (!CheckSizeOverflow(extra_size) ||282!VP8LBitWriterResize(bw, (size_t)extra_size)) {283bw->cur = bw->buf;284bw->error = 1;285return;286}287}288*(vp8l_wtype_t*)bw->cur = (vp8l_wtype_t)WSWAP((vp8l_wtype_t)bw->bits);289bw->cur += VP8L_WRITER_BYTES;290bw->bits >>= VP8L_WRITER_BITS;291bw->used -= VP8L_WRITER_BITS;292}293294void VP8LPutBitsInternal(VP8LBitWriter* const bw, uint32_t bits, int n_bits) {295assert(n_bits <= 32);296// That's the max we can handle:297assert(sizeof(vp8l_wtype_t) == 2);298if (n_bits > 0) {299vp8l_atype_t lbits = bw->bits;300int used = bw->used;301// Special case of overflow handling for 32bit accumulator (2-steps flush).302#if VP8L_WRITER_BITS == 16303if (used + n_bits >= VP8L_WRITER_MAX_BITS) {304// Fill up all the VP8L_WRITER_MAX_BITS so it can be flushed out below.305const int shift = VP8L_WRITER_MAX_BITS - used;306lbits |= (vp8l_atype_t)bits << used;307used = VP8L_WRITER_MAX_BITS;308n_bits -= shift;309bits >>= shift;310assert(n_bits <= VP8L_WRITER_MAX_BITS);311}312#endif313// If needed, make some room by flushing some bits out.314while (used >= VP8L_WRITER_BITS) {315if (bw->cur + VP8L_WRITER_BYTES > bw->end) {316const uint64_t extra_size = (bw->end - bw->buf) + MIN_EXTRA_SIZE;317if (!CheckSizeOverflow(extra_size) ||318!VP8LBitWriterResize(bw, (size_t)extra_size)) {319bw->cur = bw->buf;320bw->error = 1;321return;322}323}324*(vp8l_wtype_t*)bw->cur = (vp8l_wtype_t)WSWAP((vp8l_wtype_t)lbits);325bw->cur += VP8L_WRITER_BYTES;326lbits >>= VP8L_WRITER_BITS;327used -= VP8L_WRITER_BITS;328}329bw->bits = lbits | ((vp8l_atype_t)bits << used);330bw->used = used + n_bits;331}332}333334uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw) {335// flush leftover bits336if (VP8LBitWriterResize(bw, (bw->used + 7) >> 3)) {337while (bw->used > 0) {338*bw->cur++ = (uint8_t)bw->bits;339bw->bits >>= 8;340bw->used -= 8;341}342bw->used = 0;343}344return bw->buf;345}346347//------------------------------------------------------------------------------348349350