bit_writer.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  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. // -----------------------------------------------------------------------------
  9. //
  10. // Bit writing and boolean coder
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. // Vikas Arora (vikaas.arora@gmail.com)
  14. #include <assert.h>
  15. #include <string.h> // for memcpy()
  16. #include <stdlib.h>
  17. #include "./bit_writer.h"
  18. #include "./endian_inl.h"
  19. #include "./utils.h"
  20. //------------------------------------------------------------------------------
  21. // VP8BitWriter
  22. static int BitWriterResize(VP8BitWriter* const bw, size_t extra_size) {
  23. uint8_t* new_buf;
  24. size_t new_size;
  25. const uint64_t needed_size_64b = (uint64_t)bw->pos_ + extra_size;
  26. const size_t needed_size = (size_t)needed_size_64b;
  27. if (needed_size_64b != needed_size) {
  28. bw->error_ = 1;
  29. return 0;
  30. }
  31. if (needed_size <= bw->max_pos_) return 1;
  32. // If the following line wraps over 32bit, the test just after will catch it.
  33. new_size = 2 * bw->max_pos_;
  34. if (new_size < needed_size) new_size = needed_size;
  35. if (new_size < 1024) new_size = 1024;
  36. new_buf = (uint8_t*)WebPSafeMalloc(1ULL, new_size);
  37. if (new_buf == NULL) {
  38. bw->error_ = 1;
  39. return 0;
  40. }
  41. if (bw->pos_ > 0) {
  42. assert(bw->buf_ != NULL);
  43. memcpy(new_buf, bw->buf_, bw->pos_);
  44. }
  45. WebPSafeFree(bw->buf_);
  46. bw->buf_ = new_buf;
  47. bw->max_pos_ = new_size;
  48. return 1;
  49. }
  50. static void Flush(VP8BitWriter* const bw) {
  51. const int s = 8 + bw->nb_bits_;
  52. const int32_t bits = bw->value_ >> s;
  53. assert(bw->nb_bits_ >= 0);
  54. bw->value_ -= bits << s;
  55. bw->nb_bits_ -= 8;
  56. if ((bits & 0xff) != 0xff) {
  57. size_t pos = bw->pos_;
  58. if (!BitWriterResize(bw, bw->run_ + 1)) {
  59. return;
  60. }
  61. if (bits & 0x100) { // overflow -> propagate carry over pending 0xff's
  62. if (pos > 0) bw->buf_[pos - 1]++;
  63. }
  64. if (bw->run_ > 0) {
  65. const int value = (bits & 0x100) ? 0x00 : 0xff;
  66. for (; bw->run_ > 0; --bw->run_) bw->buf_[pos++] = value;
  67. }
  68. bw->buf_[pos++] = bits;
  69. bw->pos_ = pos;
  70. } else {
  71. bw->run_++; // delay writing of bytes 0xff, pending eventual carry.
  72. }
  73. }
  74. //------------------------------------------------------------------------------
  75. // renormalization
  76. static const uint8_t kNorm[128] = { // renorm_sizes[i] = 8 - log2(i)
  77. 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  78. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  79. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  80. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  81. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  82. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  83. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  84. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  85. 0
  86. };
  87. // range = ((range + 1) << kVP8Log2Range[range]) - 1
  88. static const uint8_t kNewRange[128] = {
  89. 127, 127, 191, 127, 159, 191, 223, 127, 143, 159, 175, 191, 207, 223, 239,
  90. 127, 135, 143, 151, 159, 167, 175, 183, 191, 199, 207, 215, 223, 231, 239,
  91. 247, 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179,
  92. 183, 187, 191, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239,
  93. 243, 247, 251, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149,
  94. 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179,
  95. 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209,
  96. 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239,
  97. 241, 243, 245, 247, 249, 251, 253, 127
  98. };
  99. int VP8PutBit(VP8BitWriter* const bw, int bit, int prob) {
  100. const int split = (bw->range_ * prob) >> 8;
  101. if (bit) {
  102. bw->value_ += split + 1;
  103. bw->range_ -= split + 1;
  104. } else {
  105. bw->range_ = split;
  106. }
  107. if (bw->range_ < 127) { // emit 'shift' bits out and renormalize
  108. const int shift = kNorm[bw->range_];
  109. bw->range_ = kNewRange[bw->range_];
  110. bw->value_ <<= shift;
  111. bw->nb_bits_ += shift;
  112. if (bw->nb_bits_ > 0) Flush(bw);
  113. }
  114. return bit;
  115. }
  116. int VP8PutBitUniform(VP8BitWriter* const bw, int bit) {
  117. const int split = bw->range_ >> 1;
  118. if (bit) {
  119. bw->value_ += split + 1;
  120. bw->range_ -= split + 1;
  121. } else {
  122. bw->range_ = split;
  123. }
  124. if (bw->range_ < 127) {
  125. bw->range_ = kNewRange[bw->range_];
  126. bw->value_ <<= 1;
  127. bw->nb_bits_ += 1;
  128. if (bw->nb_bits_ > 0) Flush(bw);
  129. }
  130. return bit;
  131. }
  132. void VP8PutValue(VP8BitWriter* const bw, int value, int nb_bits) {
  133. int mask;
  134. for (mask = 1 << (nb_bits - 1); mask; mask >>= 1)
  135. VP8PutBitUniform(bw, value & mask);
  136. }
  137. void VP8PutSignedValue(VP8BitWriter* const bw, int value, int nb_bits) {
  138. if (!VP8PutBitUniform(bw, value != 0))
  139. return;
  140. if (value < 0) {
  141. VP8PutValue(bw, ((-value) << 1) | 1, nb_bits + 1);
  142. } else {
  143. VP8PutValue(bw, value << 1, nb_bits + 1);
  144. }
  145. }
  146. //------------------------------------------------------------------------------
  147. int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size) {
  148. bw->range_ = 255 - 1;
  149. bw->value_ = 0;
  150. bw->run_ = 0;
  151. bw->nb_bits_ = -8;
  152. bw->pos_ = 0;
  153. bw->max_pos_ = 0;
  154. bw->error_ = 0;
  155. bw->buf_ = NULL;
  156. return (expected_size > 0) ? BitWriterResize(bw, expected_size) : 1;
  157. }
  158. uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) {
  159. VP8PutValue(bw, 0, 9 - bw->nb_bits_);
  160. bw->nb_bits_ = 0; // pad with zeroes
  161. Flush(bw);
  162. return bw->buf_;
  163. }
  164. int VP8BitWriterAppend(VP8BitWriter* const bw,
  165. const uint8_t* data, size_t size) {
  166. assert(data != NULL);
  167. if (bw->nb_bits_ != -8) return 0; // Flush() must have been called
  168. if (!BitWriterResize(bw, size)) return 0;
  169. memcpy(bw->buf_ + bw->pos_, data, size);
  170. bw->pos_ += size;
  171. return 1;
  172. }
  173. void VP8BitWriterWipeOut(VP8BitWriter* const bw) {
  174. if (bw != NULL) {
  175. WebPSafeFree(bw->buf_);
  176. memset(bw, 0, sizeof(*bw));
  177. }
  178. }
  179. //------------------------------------------------------------------------------
  180. // VP8LBitWriter
  181. // This is the minimum amount of size the memory buffer is guaranteed to grow
  182. // when extra space is needed.
  183. #define MIN_EXTRA_SIZE (32768ULL)
  184. #define VP8L_WRITER_BYTES ((int)sizeof(vp8l_wtype_t))
  185. #define VP8L_WRITER_BITS (VP8L_WRITER_BYTES * 8)
  186. #define VP8L_WRITER_MAX_BITS (8 * (int)sizeof(vp8l_atype_t))
  187. // Returns 1 on success.
  188. static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) {
  189. uint8_t* allocated_buf;
  190. size_t allocated_size;
  191. const size_t max_bytes = bw->end_ - bw->buf_;
  192. const size_t current_size = bw->cur_ - bw->buf_;
  193. const uint64_t size_required_64b = (uint64_t)current_size + extra_size;
  194. const size_t size_required = (size_t)size_required_64b;
  195. if (size_required != size_required_64b) {
  196. bw->error_ = 1;
  197. return 0;
  198. }
  199. if (max_bytes > 0 && size_required <= max_bytes) return 1;
  200. allocated_size = (3 * max_bytes) >> 1;
  201. if (allocated_size < size_required) allocated_size = size_required;
  202. // make allocated size multiple of 1k
  203. allocated_size = (((allocated_size >> 10) + 1) << 10);
  204. allocated_buf = (uint8_t*)WebPSafeMalloc(1ULL, allocated_size);
  205. if (allocated_buf == NULL) {
  206. bw->error_ = 1;
  207. return 0;
  208. }
  209. if (current_size > 0) {
  210. memcpy(allocated_buf, bw->buf_, current_size);
  211. }
  212. WebPSafeFree(bw->buf_);
  213. bw->buf_ = allocated_buf;
  214. bw->cur_ = bw->buf_ + current_size;
  215. bw->end_ = bw->buf_ + allocated_size;
  216. return 1;
  217. }
  218. int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size) {
  219. memset(bw, 0, sizeof(*bw));
  220. return VP8LBitWriterResize(bw, expected_size);
  221. }
  222. void VP8LBitWriterDestroy(VP8LBitWriter* const bw) {
  223. if (bw != NULL) {
  224. WebPSafeFree(bw->buf_);
  225. memset(bw, 0, sizeof(*bw));
  226. }
  227. }
  228. void VP8LWriteBits(VP8LBitWriter* const bw, int n_bits, uint32_t bits) {
  229. assert(n_bits <= 32);
  230. // That's the max we can handle:
  231. assert(bw->used_ + n_bits <= 2 * VP8L_WRITER_MAX_BITS);
  232. if (n_bits > 0) {
  233. // Local field copy.
  234. vp8l_atype_t lbits = bw->bits_;
  235. int used = bw->used_;
  236. // Special case of overflow handling for 32bit accumulator (2-steps flush).
  237. if (VP8L_WRITER_BITS == 16) {
  238. if (used + n_bits >= VP8L_WRITER_MAX_BITS) {
  239. // Fill up all the VP8L_WRITER_MAX_BITS so it can be flushed out below.
  240. const int shift = VP8L_WRITER_MAX_BITS - used;
  241. lbits |= (vp8l_atype_t)bits << used;
  242. used = VP8L_WRITER_MAX_BITS;
  243. n_bits -= shift;
  244. bits >>= shift;
  245. assert(n_bits <= VP8L_WRITER_MAX_BITS);
  246. }
  247. }
  248. // If needed, make some room by flushing some bits out.
  249. while (used >= VP8L_WRITER_BITS) {
  250. if (bw->cur_ + VP8L_WRITER_BYTES > bw->end_) {
  251. const uint64_t extra_size = (bw->end_ - bw->buf_) + MIN_EXTRA_SIZE;
  252. if (extra_size != (size_t)extra_size ||
  253. !VP8LBitWriterResize(bw, (size_t)extra_size)) {
  254. bw->cur_ = bw->buf_;
  255. bw->error_ = 1;
  256. return;
  257. }
  258. }
  259. *(vp8l_wtype_t*)bw->cur_ = (vp8l_wtype_t)WSWAP((vp8l_wtype_t)lbits);
  260. bw->cur_ += VP8L_WRITER_BYTES;
  261. lbits >>= VP8L_WRITER_BITS;
  262. used -= VP8L_WRITER_BITS;
  263. }
  264. // Eventually, insert new bits.
  265. bw->bits_ = lbits | ((vp8l_atype_t)bits << used);
  266. bw->used_ = used + n_bits;
  267. }
  268. }
  269. uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw) {
  270. // flush leftover bits
  271. if (VP8LBitWriterResize(bw, (bw->used_ + 7) >> 3)) {
  272. while (bw->used_ > 0) {
  273. *bw->cur_++ = (uint8_t)bw->bits_;
  274. bw->bits_ >>= 8;
  275. bw->used_ -= 8;
  276. }
  277. bw->used_ = 0;
  278. }
  279. return bw->buf_;
  280. }
  281. //------------------------------------------------------------------------------