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.

346 lines
8.8KB

  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 <stdlib.h>
  28. #include <assert.h>
  29. #include "libavutil/bswap.h"
  30. #include "libavutil/common.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/log.h"
  33. #include "mathops.h"
  34. #include "config.h"
  35. //#define ALT_BITSTREAM_WRITER
  36. //#define ALIGNED_BITSTREAM_WRITER
  37. /* buf and buf_end must be present and used by every alternative writer. */
  38. typedef struct PutBitContext {
  39. #ifdef ALT_BITSTREAM_WRITER
  40. uint8_t *buf, *buf_end;
  41. int index;
  42. #else
  43. uint32_t bit_buf;
  44. int bit_left;
  45. uint8_t *buf, *buf_ptr, *buf_end;
  46. #endif
  47. int size_in_bits;
  48. } PutBitContext;
  49. /**
  50. * Initialize the PutBitContext s.
  51. *
  52. * @param buffer the buffer where to put bits
  53. * @param buffer_size the size in bytes of buffer
  54. */
  55. static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, 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. #ifdef ALT_BITSTREAM_WRITER
  65. s->index=0;
  66. ((uint32_t*)(s->buf))[0]=0;
  67. // memset(buffer, 0, buffer_size);
  68. #else
  69. s->buf_ptr = s->buf;
  70. s->bit_left=32;
  71. s->bit_buf=0;
  72. #endif
  73. }
  74. /**
  75. * @return the total number of bits written to the bitstream.
  76. */
  77. static inline int put_bits_count(PutBitContext *s)
  78. {
  79. #ifdef ALT_BITSTREAM_WRITER
  80. return s->index;
  81. #else
  82. return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
  83. #endif
  84. }
  85. /**
  86. * Pad the end of the output stream with zeros.
  87. */
  88. static inline void flush_put_bits(PutBitContext *s)
  89. {
  90. #ifdef ALT_BITSTREAM_WRITER
  91. align_put_bits(s);
  92. #else
  93. #ifndef BITSTREAM_WRITER_LE
  94. if (s->bit_left < 32)
  95. s->bit_buf<<= s->bit_left;
  96. #endif
  97. while (s->bit_left < 32) {
  98. /* XXX: should test end of buffer */
  99. #ifdef BITSTREAM_WRITER_LE
  100. *s->buf_ptr++=s->bit_buf;
  101. s->bit_buf>>=8;
  102. #else
  103. *s->buf_ptr++=s->bit_buf >> 24;
  104. s->bit_buf<<=8;
  105. #endif
  106. s->bit_left+=8;
  107. }
  108. s->bit_left=32;
  109. s->bit_buf=0;
  110. #endif
  111. }
  112. #if defined(ALT_BITSTREAM_WRITER) || defined(BITSTREAM_WRITER_LE)
  113. #define align_put_bits align_put_bits_unsupported_here
  114. #define ff_put_string ff_put_string_unsupported_here
  115. #define ff_copy_bits ff_copy_bits_unsupported_here
  116. #else
  117. /**
  118. * Pad the bitstream with zeros up to the next byte boundary.
  119. */
  120. void align_put_bits(PutBitContext *s);
  121. /**
  122. * Put the string string in the bitstream.
  123. *
  124. * @param terminate_string 0-terminates the written string if value is 1
  125. */
  126. void ff_put_string(PutBitContext *pb, const char *string, int terminate_string);
  127. /**
  128. * Copy the content of src to the bitstream.
  129. *
  130. * @param length the number of bits of src to copy
  131. */
  132. void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length);
  133. #endif
  134. /**
  135. * Write up to 31 bits into a bitstream.
  136. * Use put_bits32 to write 32 bits.
  137. */
  138. static inline void put_bits(PutBitContext *s, int n, unsigned int value)
  139. #ifndef ALT_BITSTREAM_WRITER
  140. {
  141. unsigned int bit_buf;
  142. int bit_left;
  143. // printf("put_bits=%d %x\n", n, value);
  144. assert(n <= 31 && value < (1U << n));
  145. bit_buf = s->bit_buf;
  146. bit_left = s->bit_left;
  147. // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  148. /* XXX: optimize */
  149. #ifdef BITSTREAM_WRITER_LE
  150. bit_buf |= value << (32 - bit_left);
  151. if (n >= bit_left) {
  152. #if !HAVE_FAST_UNALIGNED
  153. if (3 & (intptr_t) s->buf_ptr) {
  154. AV_WL32(s->buf_ptr, bit_buf);
  155. } else
  156. #endif
  157. *(uint32_t *)s->buf_ptr = av_le2ne32(bit_buf);
  158. s->buf_ptr+=4;
  159. bit_buf = (bit_left==32)?0:value >> bit_left;
  160. bit_left+=32;
  161. }
  162. bit_left-=n;
  163. #else
  164. if (n < bit_left) {
  165. bit_buf = (bit_buf<<n) | value;
  166. bit_left-=n;
  167. } else {
  168. bit_buf<<=bit_left;
  169. bit_buf |= value >> (n - bit_left);
  170. #if !HAVE_FAST_UNALIGNED
  171. if (3 & (intptr_t) s->buf_ptr) {
  172. AV_WB32(s->buf_ptr, bit_buf);
  173. } else
  174. #endif
  175. *(uint32_t *)s->buf_ptr = av_be2ne32(bit_buf);
  176. //printf("bitbuf = %08x\n", bit_buf);
  177. s->buf_ptr+=4;
  178. bit_left+=32 - n;
  179. bit_buf = value;
  180. }
  181. #endif
  182. s->bit_buf = bit_buf;
  183. s->bit_left = bit_left;
  184. }
  185. #else /* ALT_BITSTREAM_WRITER defined */
  186. {
  187. # ifdef ALIGNED_BITSTREAM_WRITER
  188. # if ARCH_X86
  189. __asm__ volatile(
  190. "movl %0, %%ecx \n\t"
  191. "xorl %%eax, %%eax \n\t"
  192. "shrdl %%cl, %1, %%eax \n\t"
  193. "shrl %%cl, %1 \n\t"
  194. "movl %0, %%ecx \n\t"
  195. "shrl $3, %%ecx \n\t"
  196. "andl $0xFFFFFFFC, %%ecx \n\t"
  197. "bswapl %1 \n\t"
  198. "orl %1, (%2, %%ecx) \n\t"
  199. "bswapl %%eax \n\t"
  200. "addl %3, %0 \n\t"
  201. "movl %%eax, 4(%2, %%ecx) \n\t"
  202. : "=&r" (s->index), "=&r" (value)
  203. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
  204. : "%eax", "%ecx"
  205. );
  206. # else
  207. int index= s->index;
  208. uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
  209. value<<= 32-n;
  210. ptr[0] |= av_be2ne32(value>>(index&31));
  211. ptr[1] = av_be2ne32(value<<(32-(index&31)));
  212. //if(n>24) printf("%d %d\n", n, value);
  213. index+= n;
  214. s->index= index;
  215. # endif
  216. # else //ALIGNED_BITSTREAM_WRITER
  217. # if ARCH_X86
  218. __asm__ volatile(
  219. "movl $7, %%ecx \n\t"
  220. "andl %0, %%ecx \n\t"
  221. "addl %3, %%ecx \n\t"
  222. "negl %%ecx \n\t"
  223. "shll %%cl, %1 \n\t"
  224. "bswapl %1 \n\t"
  225. "movl %0, %%ecx \n\t"
  226. "shrl $3, %%ecx \n\t"
  227. "orl %1, (%%ecx, %2) \n\t"
  228. "addl %3, %0 \n\t"
  229. "movl $0, 4(%%ecx, %2) \n\t"
  230. : "=&r" (s->index), "=&r" (value)
  231. : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
  232. : "%ecx"
  233. );
  234. # else
  235. int index= s->index;
  236. uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
  237. ptr[0] |= av_be2ne32(value<<(32-n-(index&7) ));
  238. ptr[1] = 0;
  239. //if(n>24) printf("%d %d\n", n, value);
  240. index+= n;
  241. s->index= index;
  242. # endif
  243. # endif //!ALIGNED_BITSTREAM_WRITER
  244. }
  245. #endif
  246. static inline void put_sbits(PutBitContext *pb, int n, int32_t value)
  247. {
  248. assert(n >= 0 && n <= 31);
  249. put_bits(pb, n, value & ((1<<n)-1));
  250. }
  251. /**
  252. * Write exactly 32 bits into a bitstream.
  253. */
  254. static void av_unused put_bits32(PutBitContext *s, uint32_t value)
  255. {
  256. int lo = value & 0xffff;
  257. int hi = value >> 16;
  258. #ifdef BITSTREAM_WRITER_LE
  259. put_bits(s, 16, lo);
  260. put_bits(s, 16, hi);
  261. #else
  262. put_bits(s, 16, hi);
  263. put_bits(s, 16, lo);
  264. #endif
  265. }
  266. /**
  267. * Return the pointer to the byte where the bitstream writer will put
  268. * the next bit.
  269. */
  270. static inline uint8_t* put_bits_ptr(PutBitContext *s)
  271. {
  272. #ifdef ALT_BITSTREAM_WRITER
  273. return s->buf + (s->index>>3);
  274. #else
  275. return s->buf_ptr;
  276. #endif
  277. }
  278. /**
  279. * Skip the given number of bytes.
  280. * PutBitContext must be flushed & aligned to a byte boundary before calling this.
  281. */
  282. static inline void skip_put_bytes(PutBitContext *s, int n)
  283. {
  284. assert((put_bits_count(s)&7)==0);
  285. #ifdef ALT_BITSTREAM_WRITER
  286. FIXME may need some cleaning of the buffer
  287. s->index += n<<3;
  288. #else
  289. assert(s->bit_left==32);
  290. s->buf_ptr += n;
  291. #endif
  292. }
  293. /**
  294. * Skip the given number of bits.
  295. * Must only be used if the actual values in the bitstream do not matter.
  296. * If n is 0 the behavior is undefined.
  297. */
  298. static inline void skip_put_bits(PutBitContext *s, int n)
  299. {
  300. #ifdef ALT_BITSTREAM_WRITER
  301. s->index += n;
  302. #else
  303. s->bit_left -= n;
  304. s->buf_ptr-= 4*(s->bit_left>>5);
  305. s->bit_left &= 31;
  306. #endif
  307. }
  308. /**
  309. * Change the end of the buffer.
  310. *
  311. * @param size the new size in bytes of the buffer where to put bits
  312. */
  313. static inline void set_put_bits_buffer_size(PutBitContext *s, int size)
  314. {
  315. s->buf_end= s->buf + size;
  316. }
  317. #endif /* AVCODEC_PUT_BITS_H */