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.

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