bit_writer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifndef WEBP_UTILS_BIT_WRITER_H_
  14. #define WEBP_UTILS_BIT_WRITER_H_
  15. #include "../webp/types.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. //------------------------------------------------------------------------------
  20. // Bit-writing
  21. typedef struct VP8BitWriter VP8BitWriter;
  22. struct VP8BitWriter {
  23. int32_t range_; // range-1
  24. int32_t value_;
  25. int run_; // number of outstanding bits
  26. int nb_bits_; // number of pending bits
  27. uint8_t* buf_; // internal buffer. Re-allocated regularly. Not owned.
  28. size_t pos_;
  29. size_t max_pos_;
  30. int error_; // true in case of error
  31. };
  32. // Initialize the object. Allocates some initial memory based on expected_size.
  33. int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size);
  34. // Finalize the bitstream coding. Returns a pointer to the internal buffer.
  35. uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw);
  36. // Release any pending memory and zeroes the object. Not a mandatory call.
  37. // Only useful in case of error, when the internal buffer hasn't been grabbed!
  38. void VP8BitWriterWipeOut(VP8BitWriter* const bw);
  39. int VP8PutBit(VP8BitWriter* const bw, int bit, int prob);
  40. int VP8PutBitUniform(VP8BitWriter* const bw, int bit);
  41. void VP8PutValue(VP8BitWriter* const bw, int value, int nb_bits);
  42. void VP8PutSignedValue(VP8BitWriter* const bw, int value, int nb_bits);
  43. // Appends some bytes to the internal buffer. Data is copied.
  44. int VP8BitWriterAppend(VP8BitWriter* const bw,
  45. const uint8_t* data, size_t size);
  46. // return approximate write position (in bits)
  47. static WEBP_INLINE uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) {
  48. return (uint64_t)(bw->pos_ + bw->run_) * 8 + 8 + bw->nb_bits_;
  49. }
  50. // Returns a pointer to the internal buffer.
  51. static WEBP_INLINE uint8_t* VP8BitWriterBuf(const VP8BitWriter* const bw) {
  52. return bw->buf_;
  53. }
  54. // Returns the size of the internal buffer.
  55. static WEBP_INLINE size_t VP8BitWriterSize(const VP8BitWriter* const bw) {
  56. return bw->pos_;
  57. }
  58. //------------------------------------------------------------------------------
  59. // VP8LBitWriter
  60. #if defined(__x86_64__) || defined(_M_X64) // 64bit
  61. typedef uint64_t vp8l_atype_t; // accumulator type
  62. typedef uint32_t vp8l_wtype_t; // writing type
  63. #define WSWAP HToLE32
  64. #else
  65. typedef uint32_t vp8l_atype_t;
  66. typedef uint16_t vp8l_wtype_t;
  67. #define WSWAP HToLE16
  68. #endif
  69. typedef struct {
  70. vp8l_atype_t bits_; // bit accumulator
  71. int used_; // number of bits used in accumulator
  72. uint8_t* buf_; // start of buffer
  73. uint8_t* cur_; // current write position
  74. uint8_t* end_; // end of buffer
  75. // After all bits are written (VP8LBitWriterFinish()), the caller must observe
  76. // the state of error_. A value of 1 indicates that a memory allocation
  77. // failure has happened during bit writing. A value of 0 indicates successful
  78. // writing of bits.
  79. int error_;
  80. } VP8LBitWriter;
  81. static WEBP_INLINE size_t VP8LBitWriterNumBytes(VP8LBitWriter* const bw) {
  82. return (bw->cur_ - bw->buf_) + ((bw->used_ + 7) >> 3);
  83. }
  84. uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw);
  85. // Returns 0 in case of memory allocation error.
  86. int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size);
  87. void VP8LBitWriterDestroy(VP8LBitWriter* const bw);
  88. // This function writes bits into bytes in increasing addresses (little endian),
  89. // and within a byte least-significant-bit first.
  90. // This function can write up to 32 bits in one go, but VP8LBitReader can only
  91. // read 24 bits max (VP8L_MAX_NUM_BIT_READ).
  92. // VP8LBitWriter's error_ flag is set in case of memory allocation error.
  93. void VP8LWriteBits(VP8LBitWriter* const bw, int n_bits, uint32_t bits);
  94. //------------------------------------------------------------------------------
  95. #ifdef __cplusplus
  96. } // extern "C"
  97. #endif
  98. #endif /* WEBP_UTILS_BIT_WRITER_H_ */