Path: blob/master/thirdparty/libwebp/src/utils/bit_writer_utils.h
21151 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])1213#ifndef WEBP_UTILS_BIT_WRITER_UTILS_H_14#define WEBP_UTILS_BIT_WRITER_UTILS_H_1516#include <stddef.h>1718#include "src/webp/types.h"1920#ifdef __cplusplus21extern "C" {22#endif2324//------------------------------------------------------------------------------25// Bit-writing2627typedef struct VP8BitWriter VP8BitWriter;28struct VP8BitWriter {29int32_t range; // range-130int32_t value;31int run; // number of outstanding bits32int nb_bits; // number of pending bits33uint8_t* buf; // internal buffer. Re-allocated regularly. Not owned.34size_t pos;35size_t max_pos;36int error; // true in case of error37};3839// Initialize the object. Allocates some initial memory based on expected_size.40int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size);41// Finalize the bitstream coding. Returns a pointer to the internal buffer.42uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw);43// Release any pending memory and zeroes the object. Not a mandatory call.44// Only useful in case of error, when the internal buffer hasn't been grabbed!45void VP8BitWriterWipeOut(VP8BitWriter* const bw);4647int VP8PutBit(VP8BitWriter* const bw, int bit, int prob);48int VP8PutBitUniform(VP8BitWriter* const bw, int bit);49void VP8PutBits(VP8BitWriter* const bw, uint32_t value, int nb_bits);50void VP8PutSignedBits(VP8BitWriter* const bw, int value, int nb_bits);5152// Appends some bytes to the internal buffer. Data is copied.53int VP8BitWriterAppend(VP8BitWriter* const bw,54const uint8_t* data, size_t size);5556// return approximate write position (in bits)57static WEBP_INLINE uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) {58const uint64_t nb_bits = 8 + bw->nb_bits; // bw->nb_bits is <= 0, note59return (bw->pos + bw->run) * 8 + nb_bits;60}6162// Returns a pointer to the internal buffer.63static WEBP_INLINE uint8_t* VP8BitWriterBuf(const VP8BitWriter* const bw) {64return bw->buf;65}66// Returns the size of the internal buffer.67static WEBP_INLINE size_t VP8BitWriterSize(const VP8BitWriter* const bw) {68return bw->pos;69}7071//------------------------------------------------------------------------------72// VP8LBitWriter7374#if defined(__x86_64__) || defined(_M_X64) // 64bit75typedef uint64_t vp8l_atype_t; // accumulator type76typedef uint32_t vp8l_wtype_t; // writing type77#define WSWAP HToLE3278#define VP8L_WRITER_BYTES 4 // sizeof(vp8l_wtype_t)79#define VP8L_WRITER_BITS 32 // 8 * sizeof(vp8l_wtype_t)80#define VP8L_WRITER_MAX_BITS 64 // 8 * sizeof(vp8l_atype_t)81#else82typedef uint32_t vp8l_atype_t;83typedef uint16_t vp8l_wtype_t;84#define WSWAP HToLE1685#define VP8L_WRITER_BYTES 286#define VP8L_WRITER_BITS 1687#define VP8L_WRITER_MAX_BITS 3288#endif8990typedef struct {91vp8l_atype_t bits; // bit accumulator92int used; // number of bits used in accumulator93uint8_t* buf; // start of buffer94uint8_t* cur; // current write position95uint8_t* end; // end of buffer9697// After all bits are written (VP8LBitWriterFinish()), the caller must observe98// the state of 'error'. A value of 1 indicates that a memory allocation99// failure has happened during bit writing. A value of 0 indicates successful100// writing of bits.101int error;102} VP8LBitWriter;103104static WEBP_INLINE size_t VP8LBitWriterNumBytes(const VP8LBitWriter* const bw) {105return (bw->cur - bw->buf) + ((bw->used + 7) >> 3);106}107108// Returns false in case of memory allocation error.109int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size);110// Returns false in case of memory allocation error.111int VP8LBitWriterClone(const VP8LBitWriter* const src,112VP8LBitWriter* const dst);113// Finalize the bitstream coding. Returns a pointer to the internal buffer.114uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw);115// Release any pending memory and zeroes the object.116void VP8LBitWriterWipeOut(VP8LBitWriter* const bw);117// Resets the cursor of the BitWriter bw to when it was like in bw_init.118void VP8LBitWriterReset(const VP8LBitWriter* const bw_init,119VP8LBitWriter* const bw);120// Swaps the memory held by two BitWriters.121void VP8LBitWriterSwap(VP8LBitWriter* const src, VP8LBitWriter* const dst);122123// Internal function for VP8LPutBits flushing 32 bits from the written state.124void VP8LPutBitsFlushBits(VP8LBitWriter* const bw);125126// PutBits internal function used in the 16 bit vp8l_wtype_t case.127void VP8LPutBitsInternal(VP8LBitWriter* const bw, uint32_t bits, int n_bits);128129// This function writes bits into bytes in increasing addresses (little endian),130// and within a byte least-significant-bit first.131// This function can write up to 32 bits in one go, but VP8LBitReader can only132// read 24 bits max (VP8L_MAX_NUM_BIT_READ).133// VP8LBitWriter's 'error' flag is set in case of memory allocation error.134static WEBP_INLINE void VP8LPutBits(VP8LBitWriter* const bw,135uint32_t bits, int n_bits) {136if (sizeof(vp8l_wtype_t) == 4) {137if (n_bits > 0) {138if (bw->used >= 32) {139VP8LPutBitsFlushBits(bw);140}141bw->bits |= (vp8l_atype_t)bits << bw->used;142bw->used += n_bits;143}144} else {145VP8LPutBitsInternal(bw, bits, n_bits);146}147}148149//------------------------------------------------------------------------------150151#ifdef __cplusplus152} // extern "C"153#endif154155#endif // WEBP_UTILS_BIT_WRITER_UTILS_H_156157158