You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
5.7KB

  1. /*
  2. * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * bitstream writer API
  23. */
  24. #ifndef AVCODEC_PUT_BITS_H
  25. #define AVCODEC_PUT_BITS_H
  26. #include <stdint.h>
  27. #include <stddef.h>
  28. #include <assert.h>
  29. #include "libavutil/intreadwrite.h"
  30. typedef struct PutBitContext {
  31. uint32_t bit_buf;
  32. int bit_left;
  33. uint8_t *buf, *buf_ptr, *buf_end;
  34. int size_in_bits;
  35. } PutBitContext;
  36. /**
  37. * Initialize the PutBitContext s.
  38. *
  39. * @param buffer the buffer where to put bits
  40. * @param buffer_size the size in bytes of buffer
  41. */
  42. static inline void init_put_bits(PutBitContext *s, uint8_t *buffer,
  43. int buffer_size)
  44. {
  45. if (buffer_size < 0) {
  46. buffer_size = 0;
  47. buffer = NULL;
  48. }
  49. s->size_in_bits = 8 * buffer_size;
  50. s->buf = buffer;
  51. s->buf_end = s->buf + buffer_size;
  52. s->buf_ptr = s->buf;
  53. s->bit_left = 32;
  54. s->bit_buf = 0;
  55. }
  56. /**
  57. * @return the total number of bits written to the bitstream.
  58. */
  59. static inline int put_bits_count(PutBitContext *s)
  60. {
  61. return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
  62. }
  63. /**
  64. * @return the number of bits available in the bitstream.
  65. */
  66. static inline int put_bits_left(PutBitContext* s)
  67. {
  68. return (s->buf_end - s->buf_ptr) * 8 - 32 + s->bit_left;
  69. }
  70. /**
  71. * Pad the end of the output stream with zeros.
  72. */
  73. static inline void flush_put_bits(PutBitContext *s)
  74. {
  75. #ifndef BITSTREAM_WRITER_LE
  76. if (s->bit_left < 32)
  77. s->bit_buf <<= s->bit_left;
  78. #endif
  79. while (s->bit_left < 32) {
  80. /* XXX: should test end of buffer */
  81. #ifdef BITSTREAM_WRITER_LE
  82. *s->buf_ptr++ = s->bit_buf;
  83. s->bit_buf >>= 8;
  84. #else
  85. *s->buf_ptr++ = s->bit_buf >> 24;
  86. s->bit_buf <<= 8;
  87. #endif
  88. s->bit_left += 8;
  89. }
  90. s->bit_left = 32;
  91. s->bit_buf = 0;
  92. }
  93. #ifdef BITSTREAM_WRITER_LE
  94. #define avpriv_align_put_bits align_put_bits_unsupported_here
  95. #define avpriv_put_string ff_put_string_unsupported_here
  96. #define avpriv_copy_bits avpriv_copy_bits_unsupported_here
  97. #else
  98. /**
  99. * Pad the bitstream with zeros up to the next byte boundary.
  100. */
  101. void avpriv_align_put_bits(PutBitContext *s);
  102. /**
  103. * Put the string string in the bitstream.
  104. *
  105. * @param terminate_string 0-terminates the written string if value is 1
  106. */
  107. void avpriv_put_string(PutBitContext *pb, const char *string,
  108. int terminate_string);
  109. /**
  110. * Copy the content of src to the bitstream.
  111. *
  112. * @param length the number of bits of src to copy
  113. */
  114. void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
  115. #endif
  116. /**
  117. * Write up to 31 bits into a bitstream.
  118. * Use put_bits32 to write 32 bits.
  119. */
  120. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  121. {
  122. unsigned int bit_buf;
  123. int bit_left;
  124. assert(n <= 31 && value < (1U << n));
  125. bit_buf = s->bit_buf;
  126. bit_left = s->bit_left;
  127. /* XXX: optimize */
  128. #ifdef BITSTREAM_WRITER_LE
  129. bit_buf |= value << (32 - bit_left);
  130. if (n >= bit_left) {
  131. AV_WL32(s->buf_ptr, bit_buf);
  132. s->buf_ptr += 4;
  133. bit_buf = (bit_left == 32) ? 0 : value >> bit_left;
  134. bit_left += 32;
  135. }
  136. bit_left -= n;
  137. #else
  138. if (n < bit_left) {
  139. bit_buf = (bit_buf << n) | value;
  140. bit_left -= n;
  141. } else {
  142. bit_buf <<= bit_left;
  143. bit_buf |= value >> (n - bit_left);
  144. AV_WB32(s->buf_ptr, bit_buf);
  145. s->buf_ptr += 4;
  146. bit_left += 32 - n;
  147. bit_buf = value;
  148. }
  149. #endif
  150. s->bit_buf = bit_buf;
  151. s->bit_left = bit_left;
  152. }
  153. static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
  154. {
  155. assert(n >= 0 && n <= 31);
  156. put_bits(pb, n, value & ((1 << n) - 1));
  157. }
  158. /**
  159. * Write exactly 32 bits into a bitstream.
  160. */
  161. static void av_unused put_bits32(PutBitContext *s, uint32_t value)
  162. {
  163. int lo = value & 0xffff;
  164. int hi = value >> 16;
  165. #ifdef BITSTREAM_WRITER_LE
  166. put_bits(s, 16, lo);
  167. put_bits(s, 16, hi);
  168. #else
  169. put_bits(s, 16, hi);
  170. put_bits(s, 16, lo);
  171. #endif
  172. }
  173. /**
  174. * Return the pointer to the byte where the bitstream writer will put
  175. * the next bit.
  176. */
  177. static inline uint8_t *put_bits_ptr(PutBitContext *s)
  178. {
  179. return s->buf_ptr;
  180. }
  181. /**
  182. * Skip the given number of bytes.
  183. * PutBitContext must be flushed & aligned to a byte boundary before calling this.
  184. */
  185. static inline void skip_put_bytes(PutBitContext *s, int n)
  186. {
  187. assert((put_bits_count(s) & 7) == 0);
  188. assert(s->bit_left == 32);
  189. s->buf_ptr += n;
  190. }
  191. /**
  192. * Skip the given number of bits.
  193. * Must only be used if the actual values in the bitstream do not matter.
  194. * If n is 0 the behavior is undefined.
  195. */
  196. static inline void skip_put_bits(PutBitContext *s, int n)
  197. {
  198. s->bit_left -= n;
  199. s->buf_ptr -= 4 * (s->bit_left >> 5);
  200. s->bit_left &= 31;
  201. }
  202. /**
  203. * Change the end of the buffer.
  204. *
  205. * @param size the new size in bytes of the buffer where to put bits
  206. */
  207. static inline void set_put_bits_buffer_size(PutBitContext *s, int size)
  208. {
  209. s->buf_end = s->buf + size;
  210. }
  211. #endif /* AVCODEC_PUT_BITS_H */