cavlc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*****************************************************************************
  2. * cavlc.c: cavlc bitstream writing
  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/common.h"
  28. #include "macroblock.h"
  29. #ifndef RDO_SKIP_BS
  30. #define RDO_SKIP_BS 0
  31. #endif
  32. /* [400,420][inter,intra] */
  33. static const uint8_t cbp_to_golomb[2][2][48] =
  34. {
  35. {{ 0, 1, 2, 5, 3, 6, 14, 10, 4, 15, 7, 11, 8, 12, 13, 9 },
  36. { 1, 10, 11, 6, 12, 7, 14, 2, 13, 15, 8, 3, 9, 4, 5, 0 }},
  37. {{ 0, 2, 3, 7, 4, 8, 17, 13, 5, 18, 9, 14, 10, 15, 16, 11,
  38. 1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
  39. 6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12 },
  40. { 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2,
  41. 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1,
  42. 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0 }}
  43. };
  44. static const uint8_t mb_type_b_to_golomb[3][9]=
  45. {
  46. { 4, 8, 12, 10, 6, 14, 16, 18, 20 }, /* D_16x8 */
  47. { 5, 9, 13, 11, 7, 15, 17, 19, 21 }, /* D_8x16 */
  48. { 1, -1, -1, -1, 2, -1, -1, -1, 3 } /* D_16x16 */
  49. };
  50. static const uint8_t subpartition_p_to_golomb[4]=
  51. {
  52. 3, 1, 2, 0
  53. };
  54. static const uint8_t subpartition_b_to_golomb[13]=
  55. {
  56. 10, 4, 5, 1, 11, 6, 7, 2, 12, 8, 9, 3, 0
  57. };
  58. #define bs_write_vlc(s,v) bs_write( s, (v).i_size, (v).i_bits )
  59. /****************************************************************************
  60. * x264_cavlc_block_residual:
  61. ****************************************************************************/
  62. static inline int cavlc_block_residual_escape( x264_t *h, int i_suffix_length, int level )
  63. {
  64. bs_t *s = &h->out.bs;
  65. static const uint16_t next_suffix[7] = { 0, 3, 6, 12, 24, 48, 0xffff };
  66. int i_level_prefix = 15;
  67. int mask = level >> 31;
  68. int abs_level = (level^mask)-mask;
  69. int i_level_code = abs_level*2-mask-2;
  70. if( ( i_level_code >> i_suffix_length ) < 15 )
  71. {
  72. bs_write( s, (i_level_code >> i_suffix_length) + 1 + i_suffix_length,
  73. (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
  74. }
  75. else
  76. {
  77. i_level_code -= 15 << i_suffix_length;
  78. if( i_suffix_length == 0 )
  79. i_level_code -= 15;
  80. /* If the prefix size exceeds 15, High Profile is required. */
  81. if( i_level_code >= 1<<12 )
  82. {
  83. if( h->sps->i_profile_idc >= PROFILE_HIGH )
  84. {
  85. while( i_level_code > 1<<(i_level_prefix-3) )
  86. {
  87. i_level_code -= 1<<(i_level_prefix-3);
  88. i_level_prefix++;
  89. }
  90. }
  91. else
  92. {
  93. #if RDO_SKIP_BS
  94. /* Weight highly against overflows. */
  95. s->i_bits_encoded += 2000;
  96. #else
  97. /* We've had an overflow; note it down and re-encode the MB later. */
  98. h->mb.b_overflow = 1;
  99. #endif
  100. }
  101. }
  102. bs_write( s, i_level_prefix + 1, 1 );
  103. bs_write( s, i_level_prefix - 3, i_level_code & ((1<<(i_level_prefix-3))-1) );
  104. }
  105. if( i_suffix_length == 0 )
  106. i_suffix_length++;
  107. if( abs_level > next_suffix[i_suffix_length] )
  108. i_suffix_length++;
  109. return i_suffix_length;
  110. }
  111. static int cavlc_block_residual_internal( x264_t *h, int ctx_block_cat, dctcoef *l, int nC )
  112. {
  113. bs_t *s = &h->out.bs;
  114. static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
  115. static const uint8_t count_cat[14] = {16, 15, 16, 0, 15, 64, 16, 15, 16, 64, 16, 15, 16, 64};
  116. x264_run_level_t runlevel;
  117. int i_total, i_trailing, i_total_zero, i_suffix_length;
  118. unsigned int i_sign;
  119. /* level and run and total */
  120. i_total = h->quantf.coeff_level_run[ctx_block_cat]( l, &runlevel );
  121. x264_prefetch( &x264_run_before[runlevel.mask] );
  122. i_total_zero = runlevel.last + 1 - i_total;
  123. /* branchless i_trailing calculation */
  124. runlevel.level[i_total+0] = 2;
  125. runlevel.level[i_total+1] = 2;
  126. i_trailing = ((((runlevel.level[0]+1) | (1-runlevel.level[0])) >> 31) & 1) // abs(runlevel.level[0])>1
  127. | ((((runlevel.level[1]+1) | (1-runlevel.level[1])) >> 31) & 2)
  128. | ((((runlevel.level[2]+1) | (1-runlevel.level[2])) >> 31) & 4);
  129. i_trailing = ctz_index[i_trailing];
  130. i_sign = ((runlevel.level[2] >> 31) & 1)
  131. | ((runlevel.level[1] >> 31) & 2)
  132. | ((runlevel.level[0] >> 31) & 4);
  133. i_sign >>= 3-i_trailing;
  134. /* total/trailing */
  135. bs_write_vlc( s, x264_coeff_token[nC][i_total-1][i_trailing] );
  136. i_suffix_length = i_total > 10 && i_trailing < 3;
  137. bs_write( s, i_trailing, i_sign );
  138. if( i_trailing < i_total )
  139. {
  140. int val = runlevel.level[i_trailing];
  141. int val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
  142. val -= ((val>>31)|1) & -(i_trailing < 3); /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
  143. val += LEVEL_TABLE_SIZE/2;
  144. if( (unsigned)val_original < LEVEL_TABLE_SIZE )
  145. {
  146. bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
  147. i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
  148. }
  149. else
  150. i_suffix_length = cavlc_block_residual_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
  151. for( int i = i_trailing+1; i < i_total; i++ )
  152. {
  153. val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
  154. if( (unsigned)val < LEVEL_TABLE_SIZE )
  155. {
  156. bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
  157. i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
  158. }
  159. else
  160. i_suffix_length = cavlc_block_residual_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
  161. }
  162. }
  163. if( ctx_block_cat == DCT_CHROMA_DC )
  164. {
  165. if( i_total < 8>>CHROMA_V_SHIFT )
  166. {
  167. vlc_t total_zeros = CHROMA_FORMAT == CHROMA_420 ? x264_total_zeros_2x2_dc[i_total-1][i_total_zero]
  168. : x264_total_zeros_2x4_dc[i_total-1][i_total_zero];
  169. bs_write_vlc( s, total_zeros );
  170. }
  171. }
  172. else if( (uint8_t)i_total < count_cat[ctx_block_cat] )
  173. bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
  174. int zero_run_code = x264_run_before[runlevel.mask];
  175. bs_write( s, zero_run_code&0x1f, zero_run_code>>5 );
  176. return i_total;
  177. }
  178. static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};
  179. #define x264_cavlc_block_residual(h,cat,idx,l)\
  180. {\
  181. int nC = cat == DCT_CHROMA_DC ? 5 - CHROMA_V_SHIFT\
  182. : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? (idx - LUMA_DC)*16 : idx )];\
  183. uint8_t *nnz = &h->mb.cache.non_zero_count[x264_scan8[idx]];\
  184. if( !*nnz )\
  185. bs_write_vlc( &h->out.bs, x264_coeff0_token[nC] );\
  186. else\
  187. *nnz = cavlc_block_residual_internal(h,cat,l,nC);\
  188. }
  189. static void cavlc_qp_delta( x264_t *h )
  190. {
  191. bs_t *s = &h->out.bs;
  192. int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
  193. /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely
  194. * flat background area. Don't do this if it would raise the quantizer, since that could
  195. * cause unexpected deblocking artifacts. */
  196. if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
  197. && !h->mb.cache.non_zero_count[x264_scan8[LUMA_DC]]
  198. && !h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]]
  199. && !h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]]
  200. && h->mb.i_qp > h->mb.i_last_qp )
  201. {
  202. #if !RDO_SKIP_BS
  203. h->mb.i_qp = h->mb.i_last_qp;
  204. #endif
  205. i_dqp = 0;
  206. }
  207. if( i_dqp )
  208. {
  209. if( i_dqp < -(QP_MAX_SPEC+1)/2 )
  210. i_dqp += QP_MAX_SPEC+1;
  211. else if( i_dqp > QP_MAX_SPEC/2 )
  212. i_dqp -= QP_MAX_SPEC+1;
  213. }
  214. bs_write_se( s, i_dqp );
  215. }
  216. static void cavlc_mvd( x264_t *h, int i_list, int idx, int width )
  217. {
  218. bs_t *s = &h->out.bs;
  219. ALIGNED_4( int16_t mvp[2] );
  220. x264_mb_predict_mv( h, i_list, idx, width, mvp );
  221. bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
  222. bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
  223. }
  224. static inline void cavlc_8x8_mvd( x264_t *h, int i )
  225. {
  226. switch( h->mb.i_sub_partition[i] )
  227. {
  228. case D_L0_8x8:
  229. cavlc_mvd( h, 0, 4*i, 2 );
  230. break;
  231. case D_L0_8x4:
  232. cavlc_mvd( h, 0, 4*i+0, 2 );
  233. cavlc_mvd( h, 0, 4*i+2, 2 );
  234. break;
  235. case D_L0_4x8:
  236. cavlc_mvd( h, 0, 4*i+0, 1 );
  237. cavlc_mvd( h, 0, 4*i+1, 1 );
  238. break;
  239. case D_L0_4x4:
  240. cavlc_mvd( h, 0, 4*i+0, 1 );
  241. cavlc_mvd( h, 0, 4*i+1, 1 );
  242. cavlc_mvd( h, 0, 4*i+2, 1 );
  243. cavlc_mvd( h, 0, 4*i+3, 1 );
  244. break;
  245. }
  246. }
  247. static ALWAYS_INLINE void cavlc_macroblock_luma_residual( x264_t *h, int plane_count )
  248. {
  249. if( h->mb.b_transform_8x8 )
  250. {
  251. /* shuffle 8x8 dct coeffs into 4x4 lists */
  252. for( int p = 0; p < plane_count; p++ )
  253. for( int i8 = 0; i8 < 4; i8++ )
  254. if( h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4]] )
  255. h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[p*16+i8*4], h->dct.luma8x8[p*4+i8],
  256. &h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4]] );
  257. }
  258. for( int p = 0; p < plane_count; p++ )
  259. FOREACH_BIT( i8, 0, h->mb.i_cbp_luma )
  260. for( int i4 = 0; i4 < 4; i4++ )
  261. x264_cavlc_block_residual( h, DCT_LUMA_4x4, i4+i8*4+p*16, h->dct.luma4x4[i4+i8*4+p*16] );
  262. }
  263. #if RDO_SKIP_BS
  264. static ALWAYS_INLINE void cavlc_partition_luma_residual( x264_t *h, int i8, int p )
  265. {
  266. if( h->mb.b_transform_8x8 && h->mb.cache.non_zero_count[x264_scan8[i8*4+p*16]] )
  267. h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4+p*16], h->dct.luma8x8[i8+p*4],
  268. &h->mb.cache.non_zero_count[x264_scan8[i8*4+p*16]] );
  269. if( h->mb.i_cbp_luma & (1 << i8) )
  270. for( int i4 = 0; i4 < 4; i4++ )
  271. x264_cavlc_block_residual( h, DCT_LUMA_4x4, i4+i8*4+p*16, h->dct.luma4x4[i4+i8*4+p*16] );
  272. }
  273. #endif
  274. static void cavlc_mb_header_i( x264_t *h, int i_mb_type, int i_mb_i_offset, int chroma )
  275. {
  276. bs_t *s = &h->out.bs;
  277. if( i_mb_type == I_16x16 )
  278. {
  279. bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
  280. h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
  281. }
  282. else //if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
  283. {
  284. int di = i_mb_type == I_8x8 ? 4 : 1;
  285. bs_write_ue( s, i_mb_i_offset + 0 );
  286. if( h->pps->b_transform_8x8_mode )
  287. bs_write1( s, h->mb.b_transform_8x8 );
  288. /* Prediction: Luma */
  289. for( int i = 0; i < 16; i += di )
  290. {
  291. int i_pred = x264_mb_predict_intra4x4_mode( h, i );
  292. int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
  293. if( i_pred == i_mode )
  294. bs_write1( s, 1 ); /* b_prev_intra4x4_pred_mode */
  295. else
  296. bs_write( s, 4, i_mode - (i_mode > i_pred) );
  297. }
  298. }
  299. if( chroma )
  300. bs_write_ue( s, x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode] );
  301. }
  302. static ALWAYS_INLINE void cavlc_mb_header_p( x264_t *h, int i_mb_type, int chroma )
  303. {
  304. bs_t *s = &h->out.bs;
  305. if( i_mb_type == P_L0 )
  306. {
  307. if( h->mb.i_partition == D_16x16 )
  308. {
  309. bs_write1( s, 1 );
  310. if( h->mb.pic.i_fref[0] > 1 )
  311. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
  312. cavlc_mvd( h, 0, 0, 4 );
  313. }
  314. else if( h->mb.i_partition == D_16x8 )
  315. {
  316. bs_write_ue( s, 1 );
  317. if( h->mb.pic.i_fref[0] > 1 )
  318. {
  319. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
  320. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
  321. }
  322. cavlc_mvd( h, 0, 0, 4 );
  323. cavlc_mvd( h, 0, 8, 4 );
  324. }
  325. else if( h->mb.i_partition == D_8x16 )
  326. {
  327. bs_write_ue( s, 2 );
  328. if( h->mb.pic.i_fref[0] > 1 )
  329. {
  330. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
  331. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
  332. }
  333. cavlc_mvd( h, 0, 0, 2 );
  334. cavlc_mvd( h, 0, 4, 2 );
  335. }
  336. }
  337. else if( i_mb_type == P_8x8 )
  338. {
  339. int b_sub_ref;
  340. if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
  341. h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
  342. {
  343. bs_write_ue( s, 4 );
  344. b_sub_ref = 0;
  345. }
  346. else
  347. {
  348. bs_write_ue( s, 3 );
  349. b_sub_ref = 1;
  350. }
  351. /* sub mb type */
  352. if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
  353. for( int i = 0; i < 4; i++ )
  354. bs_write_ue( s, subpartition_p_to_golomb[ h->mb.i_sub_partition[i] ] );
  355. else
  356. bs_write( s, 4, 0xf );
  357. /* ref0 */
  358. if( b_sub_ref )
  359. {
  360. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
  361. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
  362. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
  363. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
  364. }
  365. for( int i = 0; i < 4; i++ )
  366. cavlc_8x8_mvd( h, i );
  367. }
  368. else //if( IS_INTRA( i_mb_type ) )
  369. cavlc_mb_header_i( h, i_mb_type, 5, chroma );
  370. }
  371. static ALWAYS_INLINE void cavlc_mb_header_b( x264_t *h, int i_mb_type, int chroma )
  372. {
  373. bs_t *s = &h->out.bs;
  374. if( i_mb_type == B_8x8 )
  375. {
  376. bs_write_ue( s, 22 );
  377. /* sub mb type */
  378. for( int i = 0; i < 4; i++ )
  379. bs_write_ue( s, subpartition_b_to_golomb[ h->mb.i_sub_partition[i] ] );
  380. /* ref */
  381. if( h->mb.pic.i_fref[0] > 1 )
  382. for( int i = 0; i < 4; i++ )
  383. if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
  384. bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
  385. if( h->mb.pic.i_fref[1] > 1 )
  386. for( int i = 0; i < 4; i++ )
  387. if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
  388. bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
  389. /* mvd */
  390. for( int i = 0; i < 4; i++ )
  391. if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
  392. cavlc_mvd( h, 0, 4*i, 2 );
  393. for( int i = 0; i < 4; i++ )
  394. if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
  395. cavlc_mvd( h, 1, 4*i, 2 );
  396. }
  397. else if( i_mb_type >= B_L0_L0 && i_mb_type <= B_BI_BI )
  398. {
  399. /* All B mode */
  400. /* Motion Vector */
  401. const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
  402. const int i_ref0_max = h->mb.pic.i_fref[0] - 1;
  403. const int i_ref1_max = h->mb.pic.i_fref[1] - 1;
  404. bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
  405. if( h->mb.i_partition == D_16x16 )
  406. {
  407. if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[0]] );
  408. if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[0]] );
  409. if( b_list[0][0] ) cavlc_mvd( h, 0, 0, 4 );
  410. if( b_list[1][0] ) cavlc_mvd( h, 1, 0, 4 );
  411. }
  412. else
  413. {
  414. if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[ 0]] );
  415. if( i_ref0_max && b_list[0][1] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[12]] );
  416. if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[ 0]] );
  417. if( i_ref1_max && b_list[1][1] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[12]] );
  418. if( h->mb.i_partition == D_16x8 )
  419. {
  420. if( b_list[0][0] ) cavlc_mvd( h, 0, 0, 4 );
  421. if( b_list[0][1] ) cavlc_mvd( h, 0, 8, 4 );
  422. if( b_list[1][0] ) cavlc_mvd( h, 1, 0, 4 );
  423. if( b_list[1][1] ) cavlc_mvd( h, 1, 8, 4 );
  424. }
  425. else //if( h->mb.i_partition == D_8x16 )
  426. {
  427. if( b_list[0][0] ) cavlc_mvd( h, 0, 0, 2 );
  428. if( b_list[0][1] ) cavlc_mvd( h, 0, 4, 2 );
  429. if( b_list[1][0] ) cavlc_mvd( h, 1, 0, 2 );
  430. if( b_list[1][1] ) cavlc_mvd( h, 1, 4, 2 );
  431. }
  432. }
  433. }
  434. else if( i_mb_type == B_DIRECT )
  435. bs_write1( s, 1 );
  436. else //if( IS_INTRA( i_mb_type ) )
  437. cavlc_mb_header_i( h, i_mb_type, 23, chroma );
  438. }
  439. /*****************************************************************************
  440. * x264_macroblock_write:
  441. *****************************************************************************/
  442. void x264_macroblock_write_cavlc( x264_t *h )
  443. {
  444. bs_t *s = &h->out.bs;
  445. const int i_mb_type = h->mb.i_type;
  446. int plane_count = CHROMA444 ? 3 : 1;
  447. int chroma = CHROMA_FORMAT == CHROMA_420 || CHROMA_FORMAT == CHROMA_422;
  448. #if RDO_SKIP_BS
  449. s->i_bits_encoded = 0;
  450. #else
  451. const int i_mb_pos_start = bs_pos( s );
  452. int i_mb_pos_tex;
  453. #endif
  454. if( SLICE_MBAFF
  455. && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
  456. {
  457. bs_write1( s, MB_INTERLACED );
  458. #if !RDO_SKIP_BS
  459. h->mb.field_decoding_flag = MB_INTERLACED;
  460. #endif
  461. }
  462. #if !RDO_SKIP_BS
  463. if( i_mb_type == I_PCM )
  464. {
  465. static const uint8_t i_offsets[3] = {5,23,0};
  466. uint8_t *p_start = s->p_start;
  467. bs_write_ue( s, i_offsets[h->sh.i_type] + 25 );
  468. i_mb_pos_tex = bs_pos( s );
  469. h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
  470. bs_align_0( s );
  471. for( int p = 0; p < plane_count; p++ )
  472. for( int i = 0; i < 256; i++ )
  473. bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[p][i] );
  474. if( chroma )
  475. for( int ch = 1; ch < 3; ch++ )
  476. for( int i = 0; i < 16>>CHROMA_V_SHIFT; i++ )
  477. for( int j = 0; j < 8; j++ )
  478. bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[ch][i*FENC_STRIDE+j] );
  479. bs_init( s, s->p, s->p_end - s->p );
  480. s->p_start = p_start;
  481. h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
  482. return;
  483. }
  484. #endif
  485. if( h->sh.i_type == SLICE_TYPE_P )
  486. cavlc_mb_header_p( h, i_mb_type, chroma );
  487. else if( h->sh.i_type == SLICE_TYPE_B )
  488. cavlc_mb_header_b( h, i_mb_type, chroma );
  489. else //if( h->sh.i_type == SLICE_TYPE_I )
  490. cavlc_mb_header_i( h, i_mb_type, 0, chroma );
  491. #if !RDO_SKIP_BS
  492. i_mb_pos_tex = bs_pos( s );
  493. h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
  494. #endif
  495. /* Coded block pattern */
  496. if( i_mb_type != I_16x16 )
  497. bs_write_ue( s, cbp_to_golomb[chroma][IS_INTRA(i_mb_type)][(h->mb.i_cbp_chroma << 4)|h->mb.i_cbp_luma] );
  498. /* transform size 8x8 flag */
  499. if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
  500. bs_write1( s, h->mb.b_transform_8x8 );
  501. if( i_mb_type == I_16x16 )
  502. {
  503. cavlc_qp_delta( h );
  504. /* DC Luma */
  505. for( int p = 0; p < plane_count; p++ )
  506. {
  507. x264_cavlc_block_residual( h, DCT_LUMA_DC, LUMA_DC+p, h->dct.luma16x16_dc[p] );
  508. /* AC Luma */
  509. if( h->mb.i_cbp_luma )
  510. for( int i = p*16; i < p*16+16; i++ )
  511. x264_cavlc_block_residual( h, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
  512. }
  513. }
  514. else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
  515. {
  516. cavlc_qp_delta( h );
  517. cavlc_macroblock_luma_residual( h, plane_count );
  518. }
  519. if( h->mb.i_cbp_chroma )
  520. {
  521. /* Chroma DC residual present */
  522. x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+0, h->dct.chroma_dc[0] );
  523. x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+1, h->dct.chroma_dc[1] );
  524. if( h->mb.i_cbp_chroma == 2 ) /* Chroma AC residual present */
  525. {
  526. int step = 8 << CHROMA_V_SHIFT;
  527. for( int i = 16; i < 3*16; i += step )
  528. for( int j = i; j < i+4; j++ )
  529. x264_cavlc_block_residual( h, DCT_CHROMA_AC, j, h->dct.luma4x4[j]+1 );
  530. }
  531. }
  532. #if !RDO_SKIP_BS
  533. h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
  534. #endif
  535. }
  536. #if RDO_SKIP_BS
  537. /*****************************************************************************
  538. * RD only; doesn't generate a valid bitstream
  539. * doesn't write cbp or chroma dc (I don't know how much this matters)
  540. * doesn't write ref (never varies between calls, so no point in doing so)
  541. * only writes subpartition for p8x8, needed for sub-8x8 mode decision RDO
  542. * works on all partition sizes except 16x16
  543. *****************************************************************************/
  544. static int partition_size_cavlc( x264_t *h, int i8, int i_pixel )
  545. {
  546. bs_t *s = &h->out.bs;
  547. const int i_mb_type = h->mb.i_type;
  548. int b_8x16 = h->mb.i_partition == D_8x16;
  549. int plane_count = CHROMA444 ? 3 : 1;
  550. int j;
  551. h->out.bs.i_bits_encoded = 0;
  552. if( i_mb_type == P_8x8 )
  553. {
  554. cavlc_8x8_mvd( h, i8 );
  555. bs_write_ue( s, subpartition_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
  556. }
  557. else if( i_mb_type == P_L0 )
  558. cavlc_mvd( h, 0, 4*i8, 4>>b_8x16 );
  559. else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
  560. {
  561. if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) cavlc_mvd( h, 0, 4*i8, 4>>b_8x16 );
  562. if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) cavlc_mvd( h, 1, 4*i8, 4>>b_8x16 );
  563. }
  564. else //if( i_mb_type == B_8x8 )
  565. {
  566. if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
  567. cavlc_mvd( h, 0, 4*i8, 2 );
  568. if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
  569. cavlc_mvd( h, 1, 4*i8, 2 );
  570. }
  571. for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
  572. {
  573. for( int p = 0; p < plane_count; p++ )
  574. cavlc_partition_luma_residual( h, i8, p );
  575. if( h->mb.i_cbp_chroma )
  576. {
  577. if( CHROMA_FORMAT == CHROMA_422 )
  578. {
  579. int offset = (5*i8) & 0x09;
  580. x264_cavlc_block_residual( h, DCT_CHROMA_AC, 16+offset, h->dct.luma4x4[16+offset]+1 );
  581. x264_cavlc_block_residual( h, DCT_CHROMA_AC, 18+offset, h->dct.luma4x4[18+offset]+1 );
  582. x264_cavlc_block_residual( h, DCT_CHROMA_AC, 32+offset, h->dct.luma4x4[32+offset]+1 );
  583. x264_cavlc_block_residual( h, DCT_CHROMA_AC, 34+offset, h->dct.luma4x4[34+offset]+1 );
  584. }
  585. else
  586. {
  587. x264_cavlc_block_residual( h, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1 );
  588. x264_cavlc_block_residual( h, DCT_CHROMA_AC, 32+i8, h->dct.luma4x4[32+i8]+1 );
  589. }
  590. }
  591. i8 += x264_pixel_size[i_pixel].h >> 3;
  592. }
  593. return h->out.bs.i_bits_encoded;
  594. }
  595. static int subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
  596. {
  597. int plane_count = CHROMA444 ? 3 : 1;
  598. int b_8x4 = i_pixel == PIXEL_8x4;
  599. h->out.bs.i_bits_encoded = 0;
  600. cavlc_mvd( h, 0, i4, 1+b_8x4 );
  601. for( int p = 0; p < plane_count; p++ )
  602. {
  603. x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4, h->dct.luma4x4[p*16+i4] );
  604. if( i_pixel != PIXEL_4x4 )
  605. x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4+2-b_8x4, h->dct.luma4x4[p*16+i4+2-b_8x4] );
  606. }
  607. return h->out.bs.i_bits_encoded;
  608. }
  609. static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
  610. {
  611. if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
  612. return 1;
  613. else
  614. return 4;
  615. }
  616. static int partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
  617. {
  618. int plane_count = CHROMA444 ? 3 : 1;
  619. h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
  620. bs_write_ue( &h->out.bs, cbp_to_golomb[!CHROMA444][1][(h->mb.i_cbp_chroma << 4)|h->mb.i_cbp_luma] );
  621. for( int p = 0; p < plane_count; p++ )
  622. cavlc_partition_luma_residual( h, i8, p );
  623. return h->out.bs.i_bits_encoded;
  624. }
  625. static int partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
  626. {
  627. int plane_count = CHROMA444 ? 3 : 1;
  628. h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
  629. for( int p = 0; p < plane_count; p++ )
  630. x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4, h->dct.luma4x4[p*16+i4] );
  631. return h->out.bs.i_bits_encoded;
  632. }
  633. static int chroma_size_cavlc( x264_t *h )
  634. {
  635. h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode] );
  636. if( h->mb.i_cbp_chroma )
  637. {
  638. x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+0, h->dct.chroma_dc[0] );
  639. x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+1, h->dct.chroma_dc[1] );
  640. if( h->mb.i_cbp_chroma == 2 )
  641. {
  642. int step = 8 << CHROMA_V_SHIFT;
  643. for( int i = 16; i < 3*16; i += step )
  644. for( int j = i; j < i+4; j++ )
  645. x264_cavlc_block_residual( h, DCT_CHROMA_AC, j, h->dct.luma4x4[j]+1 );
  646. }
  647. }
  648. return h->out.bs.i_bits_encoded;
  649. }
  650. #endif