bit_writer_utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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_utils.h"
  18. #include "./endian_inl_utils.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 & 0xff;
  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 VP8PutBits(VP8BitWriter* const bw, uint32_t value, int nb_bits) {
  133. uint32_t mask;
  134. assert(nb_bits > 0 && nb_bits < 32);
  135. for (mask = 1u << (nb_bits - 1); mask; mask >>= 1) {
  136. VP8PutBitUniform(bw, value & mask);
  137. }
  138. }
  139. void VP8PutSignedBits(VP8BitWriter* const bw, int value, int nb_bits) {
  140. if (!VP8PutBitUniform(bw, value != 0)) return;
  141. if (value < 0) {
  142. VP8PutBits(bw, ((-value) << 1) | 1, nb_bits + 1);
  143. } else {
  144. VP8PutBits(bw, value << 1, nb_bits + 1);
  145. }
  146. }
  147. //------------------------------------------------------------------------------
  148. int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size) {
  149. bw->range_ = 255 - 1;
  150. bw->value_ = 0;
  151. bw->run_ = 0;
  152. bw->nb_bits_ = -8;
  153. bw->pos_ = 0;
  154. bw->max_pos_ = 0;
  155. bw->error_ = 0;
  156. bw->buf_ = NULL;
  157. return (expected_size > 0) ? BitWriterResize(bw, expected_size) : 1;
  158. }
  159. uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw) {
  160. VP8PutBits(bw, 0, 9 - bw->nb_bits_);
  161. bw->nb_bits_ = 0; // pad with zeroes
  162. Flush(bw);
  163. return bw->buf_;
  164. }
  165. int VP8BitWriterAppend(VP8BitWriter* const bw,
  166. const uint8_t* data, size_t size) {
  167. assert(data != NULL);
  168. if (bw->nb_bits_ != -8) return 0; // Flush() must have been called
  169. if (!BitWriterResize(bw, size)) return 0;
  170. memcpy(bw->buf_ + bw->pos_, data, size);
  171. bw->pos_ += size;
  172. return 1;
  173. }
  174. void VP8BitWriterWipeOut(VP8BitWriter* const bw) {
  175. if (bw != NULL) {
  176. WebPSafeFree(bw->buf_);
  177. memset(bw, 0, sizeof(*bw));
  178. }
  179. }
  180. //------------------------------------------------------------------------------
  181. // VP8LBitWriter
  182. // This is the minimum amount of size the memory buffer is guaranteed to grow
  183. // when extra space is needed.
  184. #define MIN_EXTRA_SIZE (32768ULL)
  185. // Returns 1 on success.
  186. static int VP8LBitWriterResize(VP8LBitWriter* const bw, size_t extra_size) {
  187. uint8_t* allocated_buf;
  188. size_t allocated_size;
  189. const size_t max_bytes = bw->end_ - bw->buf_;
  190. const size_t current_size = bw->cur_ - bw->buf_;
  191. const uint64_t size_required_64b = (uint64_t)current_size + extra_size;
  192. const size_t size_required = (size_t)size_required_64b;
  193. if (size_required != size_required_64b) {
  194. bw->error_ = 1;
  195. return 0;
  196. }
  197. if (max_bytes > 0 && size_required <= max_bytes) return 1;
  198. allocated_size = (3 * max_bytes) >> 1;
  199. if (allocated_size < size_required) allocated_size = size_required;
  200. // make allocated size multiple of 1k
  201. allocated_size = (((allocated_size >> 10) + 1) << 10);
  202. allocated_buf = (uint8_t*)WebPSafeMalloc(1ULL, allocated_size);
  203. if (allocated_buf == NULL) {
  204. bw->error_ = 1;
  205. return 0;
  206. }
  207. if (current_size > 0) {
  208. memcpy(allocated_buf, bw->buf_, current_size);
  209. }
  210. WebPSafeFree(bw->buf_);
  211. bw->buf_ = allocated_buf;
  212. bw->cur_ = bw->buf_ + current_size;
  213. bw->end_ = bw->buf_ + allocated_size;
  214. return 1;
  215. }
  216. int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size) {
  217. memset(bw, 0, sizeof(*bw));
  218. return VP8LBitWriterResize(bw, expected_size);
  219. }
  220. int VP8LBitWriterClone(const VP8LBitWriter* const src,
  221. VP8LBitWriter* const dst) {
  222. const size_t current_size = src->cur_ - src->buf_;
  223. assert(src->cur_ >= src->buf_ && src->cur_ <= src->end_);
  224. if (!VP8LBitWriterResize(dst, current_size)) return 0;
  225. memcpy(dst->buf_, src->buf_, current_size);
  226. dst->bits_ = src->bits_;
  227. dst->used_ = src->used_;
  228. dst->error_ = src->error_;
  229. dst->cur_ = dst->buf_ + current_size;
  230. return 1;
  231. }
  232. void VP8LBitWriterWipeOut(VP8LBitWriter* const bw) {
  233. if (bw != NULL) {
  234. WebPSafeFree(bw->buf_);
  235. memset(bw, 0, sizeof(*bw));
  236. }
  237. }
  238. void VP8LBitWriterReset(const VP8LBitWriter* const bw_init,
  239. VP8LBitWriter* const bw) {
  240. bw->bits_ = bw_init->bits_;
  241. bw->used_ = bw_init->used_;
  242. bw->cur_ = bw->buf_ + (bw_init->cur_ - bw_init->buf_);
  243. assert(bw->cur_ <= bw->end_);
  244. bw->error_ = bw_init->error_;
  245. }
  246. void VP8LBitWriterSwap(VP8LBitWriter* const src, VP8LBitWriter* const dst) {
  247. const VP8LBitWriter tmp = *src;
  248. *src = *dst;
  249. *dst = tmp;
  250. }
  251. void VP8LPutBitsFlushBits(VP8LBitWriter* const bw) {
  252. // If needed, make some room by flushing some bits out.
  253. if (bw->cur_ + VP8L_WRITER_BYTES > bw->end_) {
  254. const uint64_t extra_size = (bw->end_ - bw->buf_) + MIN_EXTRA_SIZE;
  255. if (!CheckSizeOverflow(extra_size) ||
  256. !VP8LBitWriterResize(bw, (size_t)extra_size)) {
  257. bw->cur_ = bw->buf_;
  258. bw->error_ = 1;
  259. return;
  260. }
  261. }
  262. *(vp8l_wtype_t*)bw->cur_ = (vp8l_wtype_t)WSWAP((vp8l_wtype_t)bw->bits_);
  263. bw->cur_ += VP8L_WRITER_BYTES;
  264. bw->bits_ >>= VP8L_WRITER_BITS;
  265. bw->used_ -= VP8L_WRITER_BITS;
  266. }
  267. void VP8LPutBitsInternal(VP8LBitWriter* const bw, uint32_t bits, int n_bits) {
  268. assert(n_bits <= 32);
  269. // That's the max we can handle:
  270. assert(sizeof(vp8l_wtype_t) == 2);
  271. if (n_bits > 0) {
  272. vp8l_atype_t lbits = bw->bits_;
  273. int used = bw->used_;
  274. // Special case of overflow handling for 32bit accumulator (2-steps flush).
  275. #if VP8L_WRITER_BITS == 16
  276. if (used + n_bits >= VP8L_WRITER_MAX_BITS) {
  277. // Fill up all the VP8L_WRITER_MAX_BITS so it can be flushed out below.
  278. const int shift = VP8L_WRITER_MAX_BITS - used;
  279. lbits |= (vp8l_atype_t)bits << used;
  280. used = VP8L_WRITER_MAX_BITS;
  281. n_bits -= shift;
  282. bits >>= shift;
  283. assert(n_bits <= VP8L_WRITER_MAX_BITS);
  284. }
  285. #endif
  286. // If needed, make some room by flushing some bits out.
  287. while (used >= VP8L_WRITER_BITS) {
  288. if (bw->cur_ + VP8L_WRITER_BYTES > bw->end_) {
  289. const uint64_t extra_size = (bw->end_ - bw->buf_) + MIN_EXTRA_SIZE;
  290. if (!CheckSizeOverflow(extra_size) ||
  291. !VP8LBitWriterResize(bw, (size_t)extra_size)) {
  292. bw->cur_ = bw->buf_;
  293. bw->error_ = 1;
  294. return;
  295. }
  296. }
  297. *(vp8l_wtype_t*)bw->cur_ = (vp8l_wtype_t)WSWAP((vp8l_wtype_t)lbits);
  298. bw->cur_ += VP8L_WRITER_BYTES;
  299. lbits >>= VP8L_WRITER_BITS;
  300. used -= VP8L_WRITER_BITS;
  301. }
  302. bw->bits_ = lbits | ((vp8l_atype_t)bits << used);
  303. bw->used_ = used + n_bits;
  304. }
  305. }
  306. uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw) {
  307. // flush leftover bits
  308. if (VP8LBitWriterResize(bw, (bw->used_ + 7) >> 3)) {
  309. while (bw->used_ > 0) {
  310. *bw->cur_++ = (uint8_t)bw->bits_;
  311. bw->bits_ >>= 8;
  312. bw->used_ -= 8;
  313. }
  314. bw->used_ = 0;
  315. }
  316. return bw->buf_;
  317. }
  318. //------------------------------------------------------------------------------