bitstream.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*****************************************************************************
  2. * bitstream.h: bitstream writing
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Loren Merritt <lorenm@u.washington.edu>
  7. * Fiona Glaser <fiona@x264.com>
  8. * Laurent Aimar <fenrir@via.ecp.fr>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  23. *
  24. * This program is also available under a commercial proprietary license.
  25. * For more information, contact us at licensing@x264.com.
  26. *****************************************************************************/
  27. #ifndef X264_BS_H
  28. #define X264_BS_H
  29. typedef struct
  30. {
  31. uint16_t i_bits;
  32. uint8_t i_size;
  33. /* Next level table to use */
  34. uint8_t i_next;
  35. } vlc_large_t;
  36. typedef struct bs_s
  37. {
  38. uint8_t *p_start;
  39. uint8_t *p;
  40. uint8_t *p_end;
  41. uintptr_t cur_bits;
  42. int i_left; /* i_count number of available bits */
  43. int i_bits_encoded; /* RD only */
  44. } bs_t;
  45. typedef struct
  46. {
  47. int32_t last;
  48. int32_t mask;
  49. ALIGNED_16( dctcoef level[18] );
  50. } x264_run_level_t;
  51. typedef struct
  52. {
  53. uint8_t *(*nal_escape)( uint8_t *dst, uint8_t *src, uint8_t *end );
  54. void (*cabac_block_residual_internal)( dctcoef *l, int b_interlaced,
  55. intptr_t ctx_block_cat, x264_cabac_t *cb );
  56. void (*cabac_block_residual_rd_internal)( dctcoef *l, int b_interlaced,
  57. intptr_t ctx_block_cat, x264_cabac_t *cb );
  58. void (*cabac_block_residual_8x8_rd_internal)( dctcoef *l, int b_interlaced,
  59. intptr_t ctx_block_cat, x264_cabac_t *cb );
  60. } x264_bitstream_function_t;
  61. #define x264_bitstream_init x264_template(bitstream_init)
  62. void x264_bitstream_init( int cpu, x264_bitstream_function_t *pf );
  63. /* A larger level table size theoretically could help a bit at extremely
  64. * high bitrates, but the cost in cache is usually too high for it to be
  65. * useful.
  66. * This size appears to be optimal for QP18 encoding on a Nehalem CPU.
  67. * FIXME: Do further testing? */
  68. #define LEVEL_TABLE_SIZE 128
  69. #define x264_level_token x264_template(level_token)
  70. extern vlc_large_t x264_level_token[7][LEVEL_TABLE_SIZE];
  71. /* The longest possible set of zero run codes sums to 25 bits. This leaves
  72. * plenty of room for both the code (25 bits) and size (5 bits) in a uint32_t. */
  73. #define x264_run_before x264_template(run_before)
  74. extern uint32_t x264_run_before[1<<16];
  75. static inline void bs_init( bs_t *s, void *p_data, int i_data )
  76. {
  77. int offset = ((intptr_t)p_data & 3);
  78. s->p = s->p_start = (uint8_t*)p_data - offset;
  79. s->p_end = (uint8_t*)p_data + i_data;
  80. s->i_left = (WORD_SIZE - offset)*8;
  81. if( offset )
  82. {
  83. s->cur_bits = endian_fix32( M32(s->p) );
  84. s->cur_bits >>= (4-offset)*8;
  85. }
  86. else
  87. s->cur_bits = 0;
  88. }
  89. static inline int bs_pos( bs_t *s )
  90. {
  91. return( 8 * (s->p - s->p_start) + (WORD_SIZE*8) - s->i_left );
  92. }
  93. /* Write the rest of cur_bits to the bitstream; results in a bitstream no longer 32-bit aligned. */
  94. static inline void bs_flush( bs_t *s )
  95. {
  96. M32( s->p ) = endian_fix32( s->cur_bits << (s->i_left&31) );
  97. s->p += WORD_SIZE - (s->i_left >> 3);
  98. s->i_left = WORD_SIZE*8;
  99. }
  100. /* The inverse of bs_flush: prepare the bitstream to be written to again. */
  101. static inline void bs_realign( bs_t *s )
  102. {
  103. int offset = ((intptr_t)s->p & 3);
  104. if( offset )
  105. {
  106. s->p = (uint8_t*)s->p - offset;
  107. s->i_left = (WORD_SIZE - offset)*8;
  108. s->cur_bits = endian_fix32( M32(s->p) );
  109. s->cur_bits >>= (4-offset)*8;
  110. }
  111. }
  112. static inline void bs_write( bs_t *s, int i_count, uint32_t i_bits )
  113. {
  114. if( WORD_SIZE == 8 )
  115. {
  116. s->cur_bits = (s->cur_bits << i_count) | i_bits;
  117. s->i_left -= i_count;
  118. if( s->i_left <= 32 )
  119. {
  120. #if WORDS_BIGENDIAN
  121. M32( s->p ) = s->cur_bits >> (32 - s->i_left);
  122. #else
  123. M32( s->p ) = endian_fix( s->cur_bits << s->i_left );
  124. #endif
  125. s->i_left += 32;
  126. s->p += 4;
  127. }
  128. }
  129. else
  130. {
  131. if( i_count < s->i_left )
  132. {
  133. s->cur_bits = (s->cur_bits << i_count) | i_bits;
  134. s->i_left -= i_count;
  135. }
  136. else
  137. {
  138. i_count -= s->i_left;
  139. s->cur_bits = (s->cur_bits << s->i_left) | (i_bits >> i_count);
  140. M32( s->p ) = endian_fix( s->cur_bits );
  141. s->p += 4;
  142. s->cur_bits = i_bits;
  143. s->i_left = 32 - i_count;
  144. }
  145. }
  146. }
  147. /* Special case to eliminate branch in normal bs_write. */
  148. /* Golomb never writes an even-size code, so this is only used in slice headers. */
  149. static inline void bs_write32( bs_t *s, uint32_t i_bits )
  150. {
  151. bs_write( s, 16, i_bits >> 16 );
  152. bs_write( s, 16, i_bits );
  153. }
  154. static inline void bs_write1( bs_t *s, uint32_t i_bit )
  155. {
  156. s->cur_bits <<= 1;
  157. s->cur_bits |= i_bit;
  158. s->i_left--;
  159. if( s->i_left == WORD_SIZE*8-32 )
  160. {
  161. M32( s->p ) = endian_fix32( s->cur_bits );
  162. s->p += 4;
  163. s->i_left = WORD_SIZE*8;
  164. }
  165. }
  166. static inline void bs_align_0( bs_t *s )
  167. {
  168. bs_write( s, s->i_left&7, 0 );
  169. bs_flush( s );
  170. }
  171. static inline void bs_align_1( bs_t *s )
  172. {
  173. bs_write( s, s->i_left&7, (1 << (s->i_left&7)) - 1 );
  174. bs_flush( s );
  175. }
  176. static inline void bs_align_10( bs_t *s )
  177. {
  178. if( s->i_left&7 )
  179. bs_write( s, s->i_left&7, 1 << ( (s->i_left&7) - 1 ) );
  180. bs_flush( s );
  181. }
  182. /* golomb functions */
  183. static const uint8_t x264_ue_size_tab[256] =
  184. {
  185. 1, 1, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7,
  186. 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  187. 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
  188. 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
  189. 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
  190. 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
  191. 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
  192. 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
  193. 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  194. 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  195. 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  196. 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  197. 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  198. 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  199. 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  200. 15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  201. };
  202. static inline void bs_write_ue_big( bs_t *s, unsigned int val )
  203. {
  204. int size = 0;
  205. int tmp = ++val;
  206. if( tmp >= 0x10000 )
  207. {
  208. size = 32;
  209. tmp >>= 16;
  210. }
  211. if( tmp >= 0x100 )
  212. {
  213. size += 16;
  214. tmp >>= 8;
  215. }
  216. size += x264_ue_size_tab[tmp];
  217. bs_write( s, size>>1, 0 );
  218. bs_write( s, (size>>1)+1, val );
  219. }
  220. /* Only works on values under 255. */
  221. static inline void bs_write_ue( bs_t *s, int val )
  222. {
  223. bs_write( s, x264_ue_size_tab[val+1], val+1 );
  224. }
  225. static inline void bs_write_se( bs_t *s, int val )
  226. {
  227. int size = 0;
  228. /* Faster than (val <= 0 ? -val*2+1 : val*2) */
  229. /* 4 instructions on x86, 3 on ARM */
  230. int tmp = 1 - val*2;
  231. if( tmp < 0 ) tmp = val*2;
  232. val = tmp;
  233. if( tmp >= 0x100 )
  234. {
  235. size = 16;
  236. tmp >>= 8;
  237. }
  238. size += x264_ue_size_tab[tmp];
  239. bs_write( s, size, val );
  240. }
  241. static inline void bs_write_te( bs_t *s, int x, int val )
  242. {
  243. if( x == 1 )
  244. bs_write1( s, 1^val );
  245. else //if( x > 1 )
  246. bs_write_ue( s, val );
  247. }
  248. static inline void bs_rbsp_trailing( bs_t *s )
  249. {
  250. bs_write1( s, 1 );
  251. bs_write( s, s->i_left&7, 0 );
  252. }
  253. static ALWAYS_INLINE int bs_size_ue( unsigned int val )
  254. {
  255. return x264_ue_size_tab[val+1];
  256. }
  257. static ALWAYS_INLINE int bs_size_ue_big( unsigned int val )
  258. {
  259. if( val < 255 )
  260. return x264_ue_size_tab[val+1];
  261. else
  262. return x264_ue_size_tab[(val+1)>>8] + 16;
  263. }
  264. static ALWAYS_INLINE int bs_size_se( int val )
  265. {
  266. int tmp = 1 - val*2;
  267. if( tmp < 0 ) tmp = val*2;
  268. if( tmp < 256 )
  269. return x264_ue_size_tab[tmp];
  270. else
  271. return x264_ue_size_tab[tmp>>8]+16;
  272. }
  273. static ALWAYS_INLINE int bs_size_te( int x, int val )
  274. {
  275. if( x == 1 )
  276. return 1;
  277. else //if( x > 1 )
  278. return x264_ue_size_tab[val+1];
  279. }
  280. #endif