put_bits.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "libavutil/intreadwrite.h"
  29. #include "libavutil/avassert.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. * Rebase the bit writer onto a reallocated buffer.
  58. *
  59. * @param buffer the buffer where to put bits
  60. * @param buffer_size the size in bytes of buffer,
  61. * must be larger than the previous size
  62. */
  63. static inline void rebase_put_bits(PutBitContext *s, uint8_t *buffer,
  64. int buffer_size)
  65. {
  66. av_assert0(8*buffer_size > s->size_in_bits);
  67. s->buf_end = buffer + buffer_size;
  68. s->buf_ptr = buffer + (s->buf_ptr - s->buf);
  69. s->buf = buffer;
  70. s->size_in_bits = 8 * buffer_size;
  71. }
  72. /**
  73. * @return the total number of bits written to the bitstream.
  74. */
  75. static inline int put_bits_count(PutBitContext *s)
  76. {
  77. return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
  78. }
  79. /**
  80. * @return the number of bits available in the bitstream.
  81. */
  82. static inline int put_bits_left(PutBitContext* s)
  83. {
  84. return (s->buf_end - s->buf_ptr) * 8 - 32 + s->bit_left;
  85. }
  86. /**
  87. * Pad the end of the output stream with zeros.
  88. */
  89. static inline void flush_put_bits(PutBitContext *s)
  90. {
  91. #ifndef BITSTREAM_WRITER_LE
  92. if (s->bit_left < 32)
  93. s->bit_buf <<= s->bit_left;
  94. #endif
  95. while (s->bit_left < 32) {
  96. av_assert0(s->buf_ptr < s->buf_end);
  97. #ifdef BITSTREAM_WRITER_LE
  98. *s->buf_ptr++ = s->bit_buf;
  99. s->bit_buf >>= 8;
  100. #else
  101. *s->buf_ptr++ = s->bit_buf >> 24;
  102. s->bit_buf <<= 8;
  103. #endif
  104. s->bit_left += 8;
  105. }
  106. s->bit_left = 32;
  107. s->bit_buf = 0;
  108. }
  109. static inline void flush_put_bits_le(PutBitContext *s)
  110. {
  111. while (s->bit_left < 32) {
  112. av_assert0(s->buf_ptr < s->buf_end);
  113. *s->buf_ptr++ = s->bit_buf;
  114. s->bit_buf >>= 8;
  115. s->bit_left += 8;
  116. }
  117. s->bit_left = 32;
  118. s->bit_buf = 0;
  119. }
  120. #ifdef BITSTREAM_WRITER_LE
  121. #define avpriv_align_put_bits align_put_bits_unsupported_here
  122. #define avpriv_put_string ff_put_string_unsupported_here
  123. #define avpriv_copy_bits avpriv_copy_bits_unsupported_here
  124. #else
  125. /**
  126. * Pad the bitstream with zeros up to the next byte boundary.
  127. */
  128. void avpriv_align_put_bits(PutBitContext *s);
  129. /**
  130. * Put the string string in the bitstream.
  131. *
  132. * @param terminate_string 0-terminates the written string if value is 1
  133. */
  134. void avpriv_put_string(PutBitContext *pb, const char *string,
  135. int terminate_string);
  136. /**
  137. * Copy the content of src to the bitstream.
  138. *
  139. * @param length the number of bits of src to copy
  140. */
  141. void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
  142. #endif
  143. /**
  144. * Write up to 31 bits into a bitstream.
  145. * Use put_bits32 to write 32 bits.
  146. */
  147. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  148. {
  149. unsigned int bit_buf;
  150. int bit_left;
  151. av_assert2(n <= 31 && value < (1U << n));
  152. bit_buf = s->bit_buf;
  153. bit_left = s->bit_left;
  154. /* XXX: optimize */
  155. #ifdef BITSTREAM_WRITER_LE
  156. bit_buf |= value << (32 - bit_left);
  157. if (n >= bit_left) {
  158. if (3 < s->buf_end - s->buf_ptr) {
  159. AV_WL32(s->buf_ptr, bit_buf);
  160. s->buf_ptr += 4;
  161. } else {
  162. av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
  163. av_assert2(0);
  164. }
  165. bit_buf = value >> bit_left;
  166. bit_left += 32;
  167. }
  168. bit_left -= n;
  169. #else
  170. if (n < bit_left) {
  171. bit_buf = (bit_buf << n) | value;
  172. bit_left -= n;
  173. } else {
  174. bit_buf <<= bit_left;
  175. bit_buf |= value >> (n - bit_left);
  176. if (3 < s->buf_end - s->buf_ptr) {
  177. AV_WB32(s->buf_ptr, bit_buf);
  178. s->buf_ptr += 4;
  179. } else {
  180. av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
  181. av_assert2(0);
  182. }
  183. bit_left += 32 - n;
  184. bit_buf = value;
  185. }
  186. #endif
  187. s->bit_buf = bit_buf;
  188. s->bit_left = bit_left;
  189. }
  190. static inline void put_bits_le(PutBitContext *s, int n, unsigned int value)
  191. {
  192. unsigned int bit_buf;
  193. int bit_left;
  194. av_assert2(n <= 31 && value < (1U << n));
  195. bit_buf = s->bit_buf;
  196. bit_left = s->bit_left;
  197. bit_buf |= value << (32 - bit_left);
  198. if (n >= bit_left) {
  199. if (3 < s->buf_end - s->buf_ptr) {
  200. AV_WL32(s->buf_ptr, bit_buf);
  201. s->buf_ptr += 4;
  202. } else {
  203. av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
  204. av_assert2(0);
  205. }
  206. bit_buf = value >> bit_left;
  207. bit_left += 32;
  208. }
  209. bit_left -= n;
  210. s->bit_buf = bit_buf;
  211. s->bit_left = bit_left;
  212. }
  213. static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
  214. {
  215. av_assert2(n >= 0 && n <= 31);
  216. put_bits(pb, n, av_mod_uintp2(value, n));
  217. }
  218. /**
  219. * Write exactly 32 bits into a bitstream.
  220. */
  221. static void av_unused put_bits32(PutBitContext *s, uint32_t value)
  222. {
  223. unsigned int bit_buf;
  224. int bit_left;
  225. bit_buf = s->bit_buf;
  226. bit_left = s->bit_left;
  227. #ifdef BITSTREAM_WRITER_LE
  228. bit_buf |= value << (32 - bit_left);
  229. if (3 < s->buf_end - s->buf_ptr) {
  230. AV_WL32(s->buf_ptr, bit_buf);
  231. s->buf_ptr += 4;
  232. } else {
  233. av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
  234. av_assert2(0);
  235. }
  236. bit_buf = (uint64_t)value >> bit_left;
  237. #else
  238. bit_buf = (uint64_t)bit_buf << bit_left;
  239. bit_buf |= value >> (32 - bit_left);
  240. if (3 < s->buf_end - s->buf_ptr) {
  241. AV_WB32(s->buf_ptr, bit_buf);
  242. s->buf_ptr += 4;
  243. } else {
  244. av_log(NULL, AV_LOG_ERROR, "Internal error, put_bits buffer too small\n");
  245. av_assert2(0);
  246. }
  247. bit_buf = value;
  248. #endif
  249. s->bit_buf = bit_buf;
  250. s->bit_left = bit_left;
  251. }
  252. /**
  253. * Write up to 64 bits into a bitstream.
  254. */
  255. static inline void put_bits64(PutBitContext *s, int n, uint64_t value)
  256. {
  257. av_assert2((n == 64) || (n < 64 && value < (UINT64_C(1) << n)));
  258. if (n < 32)
  259. put_bits(s, n, value);
  260. else if (n == 32)
  261. put_bits32(s, value);
  262. else if (n < 64) {
  263. uint32_t lo = value & 0xffffffff;
  264. uint32_t hi = value >> 32;
  265. #ifdef BITSTREAM_WRITER_LE
  266. put_bits32(s, lo);
  267. put_bits(s, n - 32, hi);
  268. #else
  269. put_bits(s, n - 32, hi);
  270. put_bits32(s, lo);
  271. #endif
  272. } else {
  273. uint32_t lo = value & 0xffffffff;
  274. uint32_t hi = value >> 32;
  275. #ifdef BITSTREAM_WRITER_LE
  276. put_bits32(s, lo);
  277. put_bits32(s, hi);
  278. #else
  279. put_bits32(s, hi);
  280. put_bits32(s, lo);
  281. #endif
  282. }
  283. }
  284. /**
  285. * Return the pointer to the byte where the bitstream writer will put
  286. * the next bit.
  287. */
  288. static inline uint8_t *put_bits_ptr(PutBitContext *s)
  289. {
  290. return s->buf_ptr;
  291. }
  292. /**
  293. * Skip the given number of bytes.
  294. * PutBitContext must be flushed & aligned to a byte boundary before calling this.
  295. */
  296. static inline void skip_put_bytes(PutBitContext *s, int n)
  297. {
  298. av_assert2((put_bits_count(s) & 7) == 0);
  299. av_assert2(s->bit_left == 32);
  300. av_assert0(n <= s->buf_end - s->buf_ptr);
  301. s->buf_ptr += n;
  302. }
  303. /**
  304. * Skip the given number of bits.
  305. * Must only be used if the actual values in the bitstream do not matter.
  306. * If n is 0 the behavior is undefined.
  307. */
  308. static inline void skip_put_bits(PutBitContext *s, int n)
  309. {
  310. s->bit_left -= n;
  311. s->buf_ptr -= 4 * (s->bit_left >> 5);
  312. s->bit_left &= 31;
  313. }
  314. /**
  315. * Change the end of the buffer.
  316. *
  317. * @param size the new size in bytes of the buffer where to put bits
  318. */
  319. static inline void set_put_bits_buffer_size(PutBitContext *s, int size)
  320. {
  321. av_assert0(size <= INT_MAX/8 - 32);
  322. s->buf_end = s->buf + size;
  323. s->size_in_bits = 8*size;
  324. }
  325. #endif /* AVCODEC_PUT_BITS_H */