1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // 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.
8 // -----------------------------------------------------------------------------
10 // Bit writing and boolean coder
12 // Author: Skal (pascal.massimino@gmail.com)
14 #ifndef WEBP_UTILS_BIT_WRITER_H_
15 #define WEBP_UTILS_BIT_WRITER_H_
17 #include "../webp/types.h"
23 //------------------------------------------------------------------------------
26 typedef struct VP8BitWriter VP8BitWriter
;
28 int32_t range_
; // range-1
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.
35 int error_
; // true in case of error
38 // Initialize the object. Allocates some initial memory based on expected_size.
39 int VP8BitWriterInit(VP8BitWriter
* const bw
, size_t expected_size
);
40 // Finalize the bitstream coding. Returns a pointer to the internal buffer.
41 uint8_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!
44 void VP8BitWriterWipeOut(VP8BitWriter
* const bw
);
46 int VP8PutBit(VP8BitWriter
* const bw
, int bit
, int prob
);
47 int VP8PutBitUniform(VP8BitWriter
* const bw
, int bit
);
48 void VP8PutValue(VP8BitWriter
* const bw
, int value
, int nb_bits
);
49 void VP8PutSignedValue(VP8BitWriter
* const bw
, int value
, int nb_bits
);
51 // Appends some bytes to the internal buffer. Data is copied.
52 int VP8BitWriterAppend(VP8BitWriter
* const bw
,
53 const uint8_t* data
, size_t size
);
55 // return approximate write position (in bits)
56 static WEBP_INLINE
uint64_t VP8BitWriterPos(const VP8BitWriter
* const bw
) {
57 return (uint64_t)(bw
->pos_
+ bw
->run_
) * 8 + 8 + bw
->nb_bits_
;
60 // Returns a pointer to the internal buffer.
61 static WEBP_INLINE
uint8_t* VP8BitWriterBuf(const VP8BitWriter
* const bw
) {
64 // Returns the size of the internal buffer.
65 static WEBP_INLINE
size_t VP8BitWriterSize(const VP8BitWriter
* const bw
) {
69 //------------------------------------------------------------------------------
71 // TODO(vikasa): VP8LBitWriter is copied as-is from lossless code. There's scope
72 // of re-using VP8BitWriter. Will evaluate once basic lossless encoder is
80 // After all bits are written, the caller must observe the state of
81 // error_. A value of 1 indicates that a memory allocation failure
82 // has happened during bit writing. A value of 0 indicates successful
87 static WEBP_INLINE
size_t VP8LBitWriterNumBytes(VP8LBitWriter
* const bw
) {
88 return (bw
->bit_pos_
+ 7) >> 3;
91 static WEBP_INLINE
uint8_t* VP8LBitWriterFinish(VP8LBitWriter
* const bw
) {
95 // Returns 0 in case of memory allocation error.
96 int VP8LBitWriterInit(VP8LBitWriter
* const bw
, size_t expected_size
);
98 void VP8LBitWriterDestroy(VP8LBitWriter
* const bw
);
100 // This function writes bits into bytes in increasing addresses, and within
101 // a byte least-significant-bit first.
103 // The function can write up to 16 bits in one go with WriteBits
104 // Example: let's assume that 3 bits (Rs below) have been written already:
106 // BYTE-0 BYTE+1 BYTE+2
108 // 0000 0RRR 0000 0000 0000 0000
110 // Now, we could write 5 or less bits in MSB by just sifting by 3
111 // and OR'ing to BYTE-0.
113 // For n bits, we take the last 5 bytes, OR that with high bits in BYTE-0,
114 // and locate the rest in BYTE+1 and BYTE+2.
116 // VP8LBitWriter's error_ flag is set in case of memory allocation error.
117 void VP8LWriteBits(VP8LBitWriter
* const bw
, int n_bits
, uint32_t bits
);
119 //------------------------------------------------------------------------------
125 #endif /* WEBP_UTILS_BIT_WRITER_H_ */