Path: blob/master/thirdparty/libwebp/src/utils/bit_writer_utils.h
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])1213#ifndef WEBP_UTILS_BIT_WRITER_UTILS_H_14#define WEBP_UTILS_BIT_WRITER_UTILS_H_1516#include "src/webp/types.h"1718#ifdef __cplusplus19extern "C" {20#endif2122//------------------------------------------------------------------------------23// Bit-writing2425typedef struct VP8BitWriter VP8BitWriter;26struct VP8BitWriter {27int32_t range_; // range-128int32_t value_;29int run_; // number of outstanding bits30int nb_bits_; // number of pending bits31uint8_t* buf_; // internal buffer. Re-allocated regularly. Not owned.32size_t pos_;33size_t max_pos_;34int error_; // true in case of error35};3637// Initialize the object. Allocates some initial memory based on expected_size.38int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size);39// Finalize the bitstream coding. Returns a pointer to the internal buffer.40uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw);41// Release any pending memory and zeroes the object. Not a mandatory call.42// Only useful in case of error, when the internal buffer hasn't been grabbed!43void VP8BitWriterWipeOut(VP8BitWriter* const bw);4445int VP8PutBit(VP8BitWriter* const bw, int bit, int prob);46int VP8PutBitUniform(VP8BitWriter* const bw, int bit);47void VP8PutBits(VP8BitWriter* const bw, uint32_t value, int nb_bits);48void VP8PutSignedBits(VP8BitWriter* const bw, int value, int nb_bits);4950// Appends some bytes to the internal buffer. Data is copied.51int VP8BitWriterAppend(VP8BitWriter* const bw,52const uint8_t* data, size_t size);5354// return approximate write position (in bits)55static WEBP_INLINE uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) {56const uint64_t nb_bits = 8 + bw->nb_bits_; // bw->nb_bits_ is <= 0, note57return (bw->pos_ + bw->run_) * 8 + nb_bits;58}5960// Returns a pointer to the internal buffer.61static WEBP_INLINE uint8_t* VP8BitWriterBuf(const VP8BitWriter* const bw) {62return bw->buf_;63}64// Returns the size of the internal buffer.65static WEBP_INLINE size_t VP8BitWriterSize(const VP8BitWriter* const bw) {66return bw->pos_;67}6869//------------------------------------------------------------------------------70// VP8LBitWriter7172#if defined(__x86_64__) || defined(_M_X64) // 64bit73typedef uint64_t vp8l_atype_t; // accumulator type74typedef uint32_t vp8l_wtype_t; // writing type75#define WSWAP HToLE3276#define VP8L_WRITER_BYTES 4 // sizeof(vp8l_wtype_t)77#define VP8L_WRITER_BITS 32 // 8 * sizeof(vp8l_wtype_t)78#define VP8L_WRITER_MAX_BITS 64 // 8 * sizeof(vp8l_atype_t)79#else80typedef uint32_t vp8l_atype_t;81typedef uint16_t vp8l_wtype_t;82#define WSWAP HToLE1683#define VP8L_WRITER_BYTES 284#define VP8L_WRITER_BITS 1685#define VP8L_WRITER_MAX_BITS 3286#endif8788typedef struct {89vp8l_atype_t bits_; // bit accumulator90int used_; // number of bits used in accumulator91uint8_t* buf_; // start of buffer92uint8_t* cur_; // current write position93uint8_t* end_; // end of buffer9495// After all bits are written (VP8LBitWriterFinish()), the caller must observe96// the state of error_. A value of 1 indicates that a memory allocation97// failure has happened during bit writing. A value of 0 indicates successful98// writing of bits.99int error_;100} VP8LBitWriter;101102static WEBP_INLINE size_t VP8LBitWriterNumBytes(const VP8LBitWriter* const bw) {103return (bw->cur_ - bw->buf_) + ((bw->used_ + 7) >> 3);104}105106// Returns false in case of memory allocation error.107int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size);108// Returns false in case of memory allocation error.109int VP8LBitWriterClone(const VP8LBitWriter* const src,110VP8LBitWriter* const dst);111// Finalize the bitstream coding. Returns a pointer to the internal buffer.112uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw);113// Release any pending memory and zeroes the object.114void VP8LBitWriterWipeOut(VP8LBitWriter* const bw);115// Resets the cursor of the BitWriter bw to when it was like in bw_init.116void VP8LBitWriterReset(const VP8LBitWriter* const bw_init,117VP8LBitWriter* const bw);118// Swaps the memory held by two BitWriters.119void VP8LBitWriterSwap(VP8LBitWriter* const src, VP8LBitWriter* const dst);120121// Internal function for VP8LPutBits flushing 32 bits from the written state.122void VP8LPutBitsFlushBits(VP8LBitWriter* const bw);123124// PutBits internal function used in the 16 bit vp8l_wtype_t case.125void VP8LPutBitsInternal(VP8LBitWriter* const bw, uint32_t bits, int n_bits);126127// This function writes bits into bytes in increasing addresses (little endian),128// and within a byte least-significant-bit first.129// This function can write up to 32 bits in one go, but VP8LBitReader can only130// read 24 bits max (VP8L_MAX_NUM_BIT_READ).131// VP8LBitWriter's error_ flag is set in case of memory allocation error.132static WEBP_INLINE void VP8LPutBits(VP8LBitWriter* const bw,133uint32_t bits, int n_bits) {134if (sizeof(vp8l_wtype_t) == 4) {135if (n_bits > 0) {136if (bw->used_ >= 32) {137VP8LPutBitsFlushBits(bw);138}139bw->bits_ |= (vp8l_atype_t)bits << bw->used_;140bw->used_ += n_bits;141}142} else {143VP8LPutBitsInternal(bw, bits, n_bits);144}145}146147//------------------------------------------------------------------------------148149#ifdef __cplusplus150} // extern "C"151#endif152153#endif // WEBP_UTILS_BIT_WRITER_UTILS_H_154155156