cabac.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*****************************************************************************
  2. * cabac.c: arithmetic coder
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  7. * Loren Merritt <lorenm@u.washington.edu>
  8. * Fiona Glaser <fiona@x264.com>
  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. #include "common.h"
  28. static uint8_t cabac_contexts[4][QP_MAX_SPEC+1][1024];
  29. void x264_cabac_init( x264_t *h )
  30. {
  31. int ctx_count = CHROMA444 ? 1024 : 460;
  32. for( int i = 0; i < 4; i++ )
  33. {
  34. const int8_t (*cabac_context_init)[1024][2] = i == 0 ? &x264_cabac_context_init_I
  35. : &x264_cabac_context_init_PB[i-1];
  36. for( int qp = 0; qp <= QP_MAX_SPEC; qp++ )
  37. for( int j = 0; j < ctx_count; j++ )
  38. {
  39. int state = x264_clip3( (((*cabac_context_init)[j][0] * qp) >> 4) + (*cabac_context_init)[j][1], 1, 126 );
  40. cabac_contexts[i][qp][j] = (X264_MIN( state, 127-state ) << 1) | (state >> 6);
  41. }
  42. }
  43. }
  44. void x264_cabac_context_init( x264_t *h, x264_cabac_t *cb, int i_slice_type, int i_qp, int i_model )
  45. {
  46. memcpy( cb->state, cabac_contexts[i_slice_type == SLICE_TYPE_I ? 0 : i_model + 1][i_qp], CHROMA444 ? 1024 : 460 );
  47. }
  48. void x264_cabac_encode_init_core( x264_cabac_t *cb )
  49. {
  50. cb->i_low = 0;
  51. cb->i_range = 0x01FE;
  52. cb->i_queue = -9; // the first bit will be shifted away and not written
  53. cb->i_bytes_outstanding = 0;
  54. }
  55. void x264_cabac_encode_init( x264_cabac_t *cb, uint8_t *p_data, uint8_t *p_end )
  56. {
  57. x264_cabac_encode_init_core( cb );
  58. cb->p_start = p_data;
  59. cb->p = p_data;
  60. cb->p_end = p_end;
  61. }
  62. static inline void cabac_putbyte( x264_cabac_t *cb )
  63. {
  64. if( cb->i_queue >= 0 )
  65. {
  66. int out = cb->i_low >> (cb->i_queue+10);
  67. cb->i_low &= (0x400<<cb->i_queue)-1;
  68. cb->i_queue -= 8;
  69. if( (out & 0xff) == 0xff )
  70. cb->i_bytes_outstanding++;
  71. else
  72. {
  73. int carry = out >> 8;
  74. int bytes_outstanding = cb->i_bytes_outstanding;
  75. // this can't modify before the beginning of the stream because
  76. // that would correspond to a probability > 1.
  77. // it will write before the beginning of the stream, which is ok
  78. // because a slice header always comes before cabac data.
  79. // this can't carry beyond the one byte, because any 0xff bytes
  80. // are in bytes_outstanding and thus not written yet.
  81. cb->p[-1] += carry;
  82. while( bytes_outstanding > 0 )
  83. {
  84. *(cb->p++) = carry-1;
  85. bytes_outstanding--;
  86. }
  87. *(cb->p++) = out;
  88. cb->i_bytes_outstanding = 0;
  89. }
  90. }
  91. }
  92. static inline void cabac_encode_renorm( x264_cabac_t *cb )
  93. {
  94. int shift = x264_cabac_renorm_shift[cb->i_range>>3];
  95. cb->i_range <<= shift;
  96. cb->i_low <<= shift;
  97. cb->i_queue += shift;
  98. cabac_putbyte( cb );
  99. }
  100. /* Making custom versions of this function, even in asm, for the cases where
  101. * b is known to be 0 or 1, proved to be somewhat useful on x86_32 with GCC 3.4
  102. * but nearly useless with GCC 4.3 and worse than useless on x86_64. */
  103. void x264_cabac_encode_decision_c( x264_cabac_t *cb, int i_ctx, int b )
  104. {
  105. int i_state = cb->state[i_ctx];
  106. int i_range_lps = x264_cabac_range_lps[i_state>>1][(cb->i_range>>6)-4];
  107. cb->i_range -= i_range_lps;
  108. if( b != (i_state & 1) )
  109. {
  110. cb->i_low += cb->i_range;
  111. cb->i_range = i_range_lps;
  112. }
  113. cb->state[i_ctx] = x264_cabac_transition[i_state][b];
  114. cabac_encode_renorm( cb );
  115. }
  116. /* Note: b is negated for this function */
  117. void x264_cabac_encode_bypass_c( x264_cabac_t *cb, int b )
  118. {
  119. cb->i_low <<= 1;
  120. cb->i_low += b & cb->i_range;
  121. cb->i_queue += 1;
  122. cabac_putbyte( cb );
  123. }
  124. static const int bypass_lut[16] =
  125. {
  126. -1, 0x2, 0x14, 0x68, 0x1d0, 0x7a0, 0x1f40, 0x7e80,
  127. 0x1fd00, 0x7fa00, 0x1ff400, 0x7fe800, 0x1ffd000, 0x7ffa000, 0x1fff4000, 0x7ffe8000
  128. };
  129. void x264_cabac_encode_ue_bypass( x264_cabac_t *cb, int exp_bits, int val )
  130. {
  131. uint32_t v = val + (1<<exp_bits);
  132. int k = 31 - x264_clz( v );
  133. uint32_t x = (bypass_lut[k-exp_bits]<<exp_bits) + v;
  134. k = 2*k+1-exp_bits;
  135. int i = ((k-1)&7)+1;
  136. do {
  137. k -= i;
  138. cb->i_low <<= i;
  139. cb->i_low += ((x>>k)&0xff) * cb->i_range;
  140. cb->i_queue += i;
  141. cabac_putbyte( cb );
  142. i = 8;
  143. } while( k > 0 );
  144. }
  145. void x264_cabac_encode_terminal_c( x264_cabac_t *cb )
  146. {
  147. cb->i_range -= 2;
  148. cabac_encode_renorm( cb );
  149. }
  150. void x264_cabac_encode_flush( x264_t *h, x264_cabac_t *cb )
  151. {
  152. cb->i_low += cb->i_range - 2;
  153. cb->i_low |= 1;
  154. cb->i_low <<= 9;
  155. cb->i_queue += 9;
  156. cabac_putbyte( cb );
  157. cabac_putbyte( cb );
  158. cb->i_low <<= -cb->i_queue;
  159. cb->i_low |= (0x35a4e4f5 >> (h->i_frame & 31) & 1) << 10;
  160. cb->i_queue = 0;
  161. cabac_putbyte( cb );
  162. while( cb->i_bytes_outstanding > 0 )
  163. {
  164. *(cb->p++) = 0xff;
  165. cb->i_bytes_outstanding--;
  166. }
  167. }