put_bits.h 10.0 KB

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