blob: c80d22ae92b5e4d3ef9406f166cb3bbd77c40715 [file] [log] [blame]
Vikas Aroraa2415722012-08-09 16:18:58 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2//
Vikas Arora0406ce12013-08-09 15:57:12 -07003// Use of this source code is governed by a BSD-style license
4// that can be found in the COPYING file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
Vikas Aroraa2415722012-08-09 16:18:58 -07008// -----------------------------------------------------------------------------
9//
10// Bit writing and boolean coder
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#ifndef WEBP_UTILS_BIT_WRITER_H_
15#define WEBP_UTILS_BIT_WRITER_H_
16
James Zern9e80ee92015-03-17 18:54:21 -070017#include "../webp/types.h"
Vikas Aroraa2415722012-08-09 16:18:58 -070018
Vikas Arora8b720222014-01-02 16:48:02 -080019#ifdef __cplusplus
Vikas Aroraa2415722012-08-09 16:18:58 -070020extern "C" {
21#endif
22
23//------------------------------------------------------------------------------
24// Bit-writing
25
26typedef struct VP8BitWriter VP8BitWriter;
27struct VP8BitWriter {
28 int32_t range_; // range-1
29 int32_t value_;
30 int run_; // number of outstanding bits
31 int nb_bits_; // number of pending bits
32 uint8_t* buf_; // internal buffer. Re-allocated regularly. Not owned.
33 size_t pos_;
34 size_t max_pos_;
35 int error_; // true in case of error
36};
37
38// Initialize the object. Allocates some initial memory based on expected_size.
39int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size);
40// Finalize the bitstream coding. Returns a pointer to the internal buffer.
41uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw);
42// Release any pending memory and zeroes the object. Not a mandatory call.
43// Only useful in case of error, when the internal buffer hasn't been grabbed!
44void VP8BitWriterWipeOut(VP8BitWriter* const bw);
45
46int VP8PutBit(VP8BitWriter* const bw, int bit, int prob);
47int VP8PutBitUniform(VP8BitWriter* const bw, int bit);
48void VP8PutValue(VP8BitWriter* const bw, int value, int nb_bits);
49void VP8PutSignedValue(VP8BitWriter* const bw, int value, int nb_bits);
50
51// Appends some bytes to the internal buffer. Data is copied.
52int VP8BitWriterAppend(VP8BitWriter* const bw,
53 const uint8_t* data, size_t size);
54
55// return approximate write position (in bits)
56static WEBP_INLINE uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) {
57 return (uint64_t)(bw->pos_ + bw->run_) * 8 + 8 + bw->nb_bits_;
58}
59
60// Returns a pointer to the internal buffer.
61static WEBP_INLINE uint8_t* VP8BitWriterBuf(const VP8BitWriter* const bw) {
62 return bw->buf_;
63}
64// Returns the size of the internal buffer.
65static WEBP_INLINE size_t VP8BitWriterSize(const VP8BitWriter* const bw) {
66 return bw->pos_;
67}
68
69//------------------------------------------------------------------------------
70// VP8LBitWriter
Vikas Arora33f74da2014-07-25 13:53:32 -070071
72#if defined(__x86_64__) || defined(_M_X64) // 64bit
73typedef uint64_t vp8l_atype_t; // accumulator type
74typedef uint32_t vp8l_wtype_t; // writing type
75#define WSWAP HToLE32
76#else
77typedef uint32_t vp8l_atype_t;
78typedef uint16_t vp8l_wtype_t;
79#define WSWAP HToLE16
80#endif
Vikas Aroraa2415722012-08-09 16:18:58 -070081
82typedef struct {
Vikas Arora33f74da2014-07-25 13:53:32 -070083 vp8l_atype_t bits_; // bit accumulator
84 int used_; // number of bits used in accumulator
85 uint8_t* buf_; // start of buffer
86 uint8_t* cur_; // current write position
87 uint8_t* end_; // end of buffer
Vikas Aroraa2415722012-08-09 16:18:58 -070088
Vikas Arora33f74da2014-07-25 13:53:32 -070089 // After all bits are written (VP8LBitWriterFinish()), the caller must observe
90 // the state of error_. A value of 1 indicates that a memory allocation
91 // failure has happened during bit writing. A value of 0 indicates successful
Vikas Aroraa2415722012-08-09 16:18:58 -070092 // writing of bits.
93 int error_;
94} VP8LBitWriter;
95
96static WEBP_INLINE size_t VP8LBitWriterNumBytes(VP8LBitWriter* const bw) {
Vikas Arora33f74da2014-07-25 13:53:32 -070097 return (bw->cur_ - bw->buf_) + ((bw->used_ + 7) >> 3);
Vikas Aroraa2415722012-08-09 16:18:58 -070098}
99
Vikas Arora33f74da2014-07-25 13:53:32 -0700100uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw);
Vikas Aroraa2415722012-08-09 16:18:58 -0700101
102// Returns 0 in case of memory allocation error.
103int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size);
104
105void VP8LBitWriterDestroy(VP8LBitWriter* const bw);
106
Vikas Arora33f74da2014-07-25 13:53:32 -0700107// This function writes bits into bytes in increasing addresses (little endian),
108// and within a byte least-significant-bit first.
109// This function can write up to 32 bits in one go, but VP8LBitReader can only
110// read 24 bits max (VP8L_MAX_NUM_BIT_READ).
Vikas Aroraa2415722012-08-09 16:18:58 -0700111// VP8LBitWriter's error_ flag is set in case of memory allocation error.
112void VP8LWriteBits(VP8LBitWriter* const bw, int n_bits, uint32_t bits);
113
114//------------------------------------------------------------------------------
115
Vikas Arora8b720222014-01-02 16:48:02 -0800116#ifdef __cplusplus
Vikas Aroraa2415722012-08-09 16:18:58 -0700117} // extern "C"
118#endif
119
120#endif /* WEBP_UTILS_BIT_WRITER_H_ */