OLD | NEW |
1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Bit writing and boolean coder | 10 // Bit writing and boolean coder |
(...skipping 27 matching lines...) Expand all Loading... |
38 // Initialize the object. Allocates some initial memory based on expected_size. | 38 // Initialize the object. Allocates some initial memory based on expected_size. |
39 int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size); | 39 int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size); |
40 // Finalize the bitstream coding. Returns a pointer to the internal buffer. | 40 // Finalize the bitstream coding. Returns a pointer to the internal buffer. |
41 uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw); | 41 uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw); |
42 // Release any pending memory and zeroes the object. Not a mandatory call. | 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! | 43 // Only useful in case of error, when the internal buffer hasn't been grabbed! |
44 void VP8BitWriterWipeOut(VP8BitWriter* const bw); | 44 void VP8BitWriterWipeOut(VP8BitWriter* const bw); |
45 | 45 |
46 int VP8PutBit(VP8BitWriter* const bw, int bit, int prob); | 46 int VP8PutBit(VP8BitWriter* const bw, int bit, int prob); |
47 int VP8PutBitUniform(VP8BitWriter* const bw, int bit); | 47 int VP8PutBitUniform(VP8BitWriter* const bw, int bit); |
48 void VP8PutValue(VP8BitWriter* const bw, int value, int nb_bits); | 48 void VP8PutBits(VP8BitWriter* const bw, uint32_t value, int nb_bits); |
49 void VP8PutSignedValue(VP8BitWriter* const bw, int value, int nb_bits); | 49 void VP8PutSignedBits(VP8BitWriter* const bw, int value, int nb_bits); |
50 | 50 |
51 // Appends some bytes to the internal buffer. Data is copied. | 51 // Appends some bytes to the internal buffer. Data is copied. |
52 int VP8BitWriterAppend(VP8BitWriter* const bw, | 52 int VP8BitWriterAppend(VP8BitWriter* const bw, |
53 const uint8_t* data, size_t size); | 53 const uint8_t* data, size_t size); |
54 | 54 |
55 // return approximate write position (in bits) | 55 // return approximate write position (in bits) |
56 static WEBP_INLINE uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) { | 56 static WEBP_INLINE uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) { |
57 return (uint64_t)(bw->pos_ + bw->run_) * 8 + 8 + bw->nb_bits_; | 57 return (uint64_t)(bw->pos_ + bw->run_) * 8 + 8 + bw->nb_bits_; |
58 } | 58 } |
59 | 59 |
60 // Returns a pointer to the internal buffer. | 60 // Returns a pointer to the internal buffer. |
61 static WEBP_INLINE uint8_t* VP8BitWriterBuf(const VP8BitWriter* const bw) { | 61 static WEBP_INLINE uint8_t* VP8BitWriterBuf(const VP8BitWriter* const bw) { |
62 return bw->buf_; | 62 return bw->buf_; |
63 } | 63 } |
64 // Returns the size of the internal buffer. | 64 // Returns the size of the internal buffer. |
65 static WEBP_INLINE size_t VP8BitWriterSize(const VP8BitWriter* const bw) { | 65 static WEBP_INLINE size_t VP8BitWriterSize(const VP8BitWriter* const bw) { |
66 return bw->pos_; | 66 return bw->pos_; |
67 } | 67 } |
68 | 68 |
69 //------------------------------------------------------------------------------ | 69 //------------------------------------------------------------------------------ |
70 // VP8LBitWriter | 70 // VP8LBitWriter |
71 | 71 |
72 #if defined(__x86_64__) || defined(_M_X64) // 64bit | 72 #if defined(__x86_64__) || defined(_M_X64) // 64bit |
73 typedef uint64_t vp8l_atype_t; // accumulator type | 73 typedef uint64_t vp8l_atype_t; // accumulator type |
74 typedef uint32_t vp8l_wtype_t; // writing type | 74 typedef uint32_t vp8l_wtype_t; // writing type |
75 #define WSWAP HToLE32 | 75 #define WSWAP HToLE32 |
| 76 #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) |
76 #else | 79 #else |
77 typedef uint32_t vp8l_atype_t; | 80 typedef uint32_t vp8l_atype_t; |
78 typedef uint16_t vp8l_wtype_t; | 81 typedef uint16_t vp8l_wtype_t; |
79 #define WSWAP HToLE16 | 82 #define WSWAP HToLE16 |
| 83 #define VP8L_WRITER_BYTES 2 |
| 84 #define VP8L_WRITER_BITS 16 |
| 85 #define VP8L_WRITER_MAX_BITS 32 |
80 #endif | 86 #endif |
81 | 87 |
82 typedef struct { | 88 typedef struct { |
83 vp8l_atype_t bits_; // bit accumulator | 89 vp8l_atype_t bits_; // bit accumulator |
84 int used_; // number of bits used in accumulator | 90 int used_; // number of bits used in accumulator |
85 uint8_t* buf_; // start of buffer | 91 uint8_t* buf_; // start of buffer |
86 uint8_t* cur_; // current write position | 92 uint8_t* cur_; // current write position |
87 uint8_t* end_; // end of buffer | 93 uint8_t* end_; // end of buffer |
88 | 94 |
89 // After all bits are written (VP8LBitWriterFinish()), the caller must observe | 95 // After all bits are written (VP8LBitWriterFinish()), the caller must observe |
90 // the state of error_. A value of 1 indicates that a memory allocation | 96 // 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 | 97 // failure has happened during bit writing. A value of 0 indicates successful |
92 // writing of bits. | 98 // writing of bits. |
93 int error_; | 99 int error_; |
94 } VP8LBitWriter; | 100 } VP8LBitWriter; |
95 | 101 |
96 static WEBP_INLINE size_t VP8LBitWriterNumBytes(VP8LBitWriter* const bw) { | 102 static WEBP_INLINE size_t VP8LBitWriterNumBytes(VP8LBitWriter* const bw) { |
97 return (bw->cur_ - bw->buf_) + ((bw->used_ + 7) >> 3); | 103 return (bw->cur_ - bw->buf_) + ((bw->used_ + 7) >> 3); |
98 } | 104 } |
99 | 105 |
| 106 // Returns false in case of memory allocation error. |
| 107 int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size); |
| 108 // Finalize the bitstream coding. Returns a pointer to the internal buffer. |
100 uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw); | 109 uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw); |
| 110 // Release any pending memory and zeroes the object. |
| 111 void VP8LBitWriterWipeOut(VP8LBitWriter* const bw); |
101 | 112 |
102 // Returns 0 in case of memory allocation error. | 113 // Internal function for VP8LPutBits flushing 32 bits from the written state. |
103 int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size); | 114 void VP8LPutBitsFlushBits(VP8LBitWriter* const bw); |
104 | 115 |
105 void VP8LBitWriterDestroy(VP8LBitWriter* const bw); | 116 // PutBits internal function used in the 16 bit vp8l_wtype_t case. |
| 117 void VP8LPutBitsInternal(VP8LBitWriter* const bw, uint32_t bits, int n_bits); |
106 | 118 |
107 // This function writes bits into bytes in increasing addresses (little endian), | 119 // This function writes bits into bytes in increasing addresses (little endian), |
108 // and within a byte least-significant-bit first. | 120 // and within a byte least-significant-bit first. |
109 // This function can write up to 32 bits in one go, but VP8LBitReader can only | 121 // 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). | 122 // read 24 bits max (VP8L_MAX_NUM_BIT_READ). |
111 // VP8LBitWriter's error_ flag is set in case of memory allocation error. | 123 // VP8LBitWriter's error_ flag is set in case of memory allocation error. |
112 void VP8LWriteBits(VP8LBitWriter* const bw, int n_bits, uint32_t bits); | 124 static WEBP_INLINE void VP8LPutBits(VP8LBitWriter* const bw, |
| 125 uint32_t bits, int n_bits) { |
| 126 if (sizeof(vp8l_wtype_t) == 4) { |
| 127 if (n_bits > 0) { |
| 128 if (bw->used_ >= 32) { |
| 129 VP8LPutBitsFlushBits(bw); |
| 130 } |
| 131 bw->bits_ |= (vp8l_atype_t)bits << bw->used_; |
| 132 bw->used_ += n_bits; |
| 133 } |
| 134 } else { |
| 135 VP8LPutBitsInternal(bw, bits, n_bits); |
| 136 } |
| 137 } |
113 | 138 |
114 //------------------------------------------------------------------------------ | 139 //------------------------------------------------------------------------------ |
115 | 140 |
116 #ifdef __cplusplus | 141 #ifdef __cplusplus |
117 } // extern "C" | 142 } // extern "C" |
118 #endif | 143 #endif |
119 | 144 |
120 #endif /* WEBP_UTILS_BIT_WRITER_H_ */ | 145 #endif /* WEBP_UTILS_BIT_WRITER_H_ */ |
OLD | NEW |