📄 cabac.c.svn-base
字号:
} x264_cabac_encode_decision( cb, 60 + ctx, 0 );}void x264_cabac_mb_skip( x264_t *h, int b_skip ){ int ctx = 0; if( h->mb.i_mb_type_left >= 0 && !IS_SKIP( h->mb.i_mb_type_left ) ) { ctx++; } if( h->mb.i_mb_type_top >= 0 && !IS_SKIP( h->mb.i_mb_type_top ) ) { ctx++; } ctx += (h->sh.i_type == SLICE_TYPE_P) ? 11 : 24; x264_cabac_encode_decision( &h->cabac, ctx, b_skip );}static inline void x264_cabac_mb_sub_p_partition( x264_cabac_t *cb, int i_sub ){ if( i_sub == D_L0_8x8 ) { x264_cabac_encode_decision( cb, 21, 1 ); } else if( i_sub == D_L0_8x4 ) { x264_cabac_encode_decision( cb, 21, 0 ); x264_cabac_encode_decision( cb, 22, 0 ); } else if( i_sub == D_L0_4x8 ) { x264_cabac_encode_decision( cb, 21, 0 ); x264_cabac_encode_decision( cb, 22, 1 ); x264_cabac_encode_decision( cb, 23, 1 ); } else if( i_sub == D_L0_4x4 ) { x264_cabac_encode_decision( cb, 21, 0 ); x264_cabac_encode_decision( cb, 22, 1 ); x264_cabac_encode_decision( cb, 23, 0 ); }}static inline void x264_cabac_mb_sub_b_partition( x264_cabac_t *cb, int i_sub ){#define WRITE_SUB_3(a,b,c) {\ x264_cabac_encode_decision( cb, 36, a );\ x264_cabac_encode_decision( cb, 37, b );\ x264_cabac_encode_decision( cb, 39, c );\ }#define WRITE_SUB_5(a,b,c,d,e) {\ x264_cabac_encode_decision( cb, 36, a );\ x264_cabac_encode_decision( cb, 37, b );\ x264_cabac_encode_decision( cb, 38, c );\ x264_cabac_encode_decision( cb, 39, d );\ x264_cabac_encode_decision( cb, 39, e );\ }#define WRITE_SUB_6(a,b,c,d,e,f) {\ WRITE_SUB_5(a,b,c,d,e)\ x264_cabac_encode_decision( cb, 39, f );\ } switch( i_sub ) { case D_DIRECT_8x8: x264_cabac_encode_decision( cb, 36, 0 ); break; case D_L0_8x8: WRITE_SUB_3(1,0,0); break; case D_L1_8x8: WRITE_SUB_3(1,0,1); break; case D_BI_8x8: WRITE_SUB_5(1,1,0,0,0); break; case D_L0_8x4: WRITE_SUB_5(1,1,0,0,1); break; case D_L0_4x8: WRITE_SUB_5(1,1,0,1,0); break; case D_L1_8x4: WRITE_SUB_5(1,1,0,1,1); break; case D_L1_4x8: WRITE_SUB_6(1,1,1,0,0,0); break; case D_BI_8x4: WRITE_SUB_6(1,1,1,0,0,1); break; case D_BI_4x8: WRITE_SUB_6(1,1,1,0,1,0); break; case D_L0_4x4: WRITE_SUB_6(1,1,1,0,1,1); break; case D_L1_4x4: WRITE_SUB_5(1,1,1,1,0); break; case D_BI_4x4: WRITE_SUB_5(1,1,1,1,1); break; }}static inline void x264_cabac_mb_transform_size( x264_t *h, x264_cabac_t *cb ){ int ctx = 399 + h->mb.cache.i_neighbour_transform_size; x264_cabac_encode_decision( cb, ctx, h->mb.b_transform_8x8 );}static inline void x264_cabac_mb_ref( x264_t *h, x264_cabac_t *cb, int i_list, int idx ){ const int i8 = x264_scan8[idx]; const int i_refa = h->mb.cache.ref[i_list][i8 - 1]; const int i_refb = h->mb.cache.ref[i_list][i8 - 8]; int i_ref = h->mb.cache.ref[i_list][i8]; int ctx = 0; if( i_refa > 0 && !h->mb.cache.skip[i8 - 1]) ctx++; if( i_refb > 0 && !h->mb.cache.skip[i8 - 8]) ctx += 2; while( i_ref > 0 ) { x264_cabac_encode_decision( cb, 54 + ctx, 1 ); if( ctx < 4 ) ctx = 4; else ctx = 5; i_ref--; } x264_cabac_encode_decision( cb, 54 + ctx, 0 );}static inline void x264_cabac_mb_mvd_cpn( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int l, int mvd ){ const int amvd = abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 1][l] ) + abs( h->mb.cache.mvd[i_list][x264_scan8[idx] - 8][l] ); const int i_abs = abs( mvd ); const int i_prefix = X264_MIN( i_abs, 9 ); const int ctxbase = (l == 0 ? 40 : 47); int ctx; int i; if( amvd < 3 ) ctx = 0; else if( amvd > 32 ) ctx = 2; else ctx = 1; for( i = 0; i < i_prefix; i++ ) { x264_cabac_encode_decision( cb, ctxbase + ctx, 1 ); if( ctx < 3 ) ctx = 3; else if( ctx < 6 ) ctx++; } if( i_prefix < 9 ) x264_cabac_encode_decision( cb, ctxbase + ctx, 0 ); else x264_cabac_encode_ue_bypass( cb, 3, i_abs - 9 ); /* sign */ if( mvd ) x264_cabac_encode_bypass( cb, mvd < 0 );}static inline void x264_cabac_mb_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int idx, int width, int height ){ int mvp[2]; int mdx, mdy; /* Calculate mvd */ x264_mb_predict_mv( h, i_list, idx, width, mvp ); mdx = h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0]; mdy = h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1]; /* encode */ x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 0, mdx ); x264_cabac_mb_mvd_cpn( h, cb, i_list, idx, 1, mdy ); /* save value */ x264_macroblock_cache_mvd( h, block_idx_x[idx], block_idx_y[idx], width, height, i_list, mdx, mdy );}static inline void x264_cabac_mb8x8_mvd( x264_t *h, x264_cabac_t *cb, int i_list, int i ){ if( !x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] ) return; switch( h->mb.i_sub_partition[i] ) { case D_L0_8x8: case D_L1_8x8: case D_BI_8x8: x264_cabac_mb_mvd( h, cb, i_list, 4*i, 2, 2 ); break; case D_L0_8x4: case D_L1_8x4: case D_BI_8x4: x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 2, 1 ); x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 2, 1 ); break; case D_L0_4x8: case D_L1_4x8: case D_BI_4x8: x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 2 ); x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 2 ); break; case D_L0_4x4: case D_L1_4x4: case D_BI_4x4: x264_cabac_mb_mvd( h, cb, i_list, 4*i+0, 1, 1 ); x264_cabac_mb_mvd( h, cb, i_list, 4*i+1, 1, 1 ); x264_cabac_mb_mvd( h, cb, i_list, 4*i+2, 1, 1 ); x264_cabac_mb_mvd( h, cb, i_list, 4*i+3, 1, 1 ); break; }}static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx ){ int i_mba_xy = -1; int i_mbb_xy = -1; int i_nza = 0; int i_nzb = 0; int ctx; if( i_cat == DCT_LUMA_DC ) { if( h->mb.i_neighbour & MB_LEFT ) { i_mba_xy = h->mb.i_mb_xy - 1; if( h->mb.type[i_mba_xy] == I_16x16 ) i_nza = h->mb.cbp[i_mba_xy] & 0x100; } if( h->mb.i_neighbour & MB_TOP ) { i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride; if( h->mb.type[i_mbb_xy] == I_16x16 ) i_nzb = h->mb.cbp[i_mbb_xy] & 0x100; } } else if( i_cat == DCT_LUMA_AC || i_cat == DCT_LUMA_4x4 ) { if( i_idx & ~10 ) // block_idx_x > 0 i_mba_xy = h->mb.i_mb_xy; else if( h->mb.i_neighbour & MB_LEFT ) i_mba_xy = h->mb.i_mb_xy - 1; if( i_idx & ~5 ) // block_idx_y > 0 i_mbb_xy = h->mb.i_mb_xy; else if( h->mb.i_neighbour & MB_TOP ) i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride; /* no need to test for skip/pcm */ if( i_mba_xy >= 0 ) i_nza = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 1]; if( i_mbb_xy >= 0 ) i_nzb = h->mb.cache.non_zero_count[x264_scan8[i_idx] - 8]; } else if( i_cat == DCT_CHROMA_DC ) { /* no need to test skip/pcm */ if( h->mb.i_neighbour & MB_LEFT ) { i_mba_xy = h->mb.i_mb_xy - 1; i_nza = h->mb.cbp[i_mba_xy] & (0x200 << i_idx); } if( h->mb.i_neighbour & MB_TOP ) { i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride; i_nzb = h->mb.cbp[i_mbb_xy] & (0x200 << i_idx); } } else if( i_cat == DCT_CHROMA_AC ) { if( i_idx & 1 ) i_mba_xy = h->mb.i_mb_xy; else if( h->mb.i_neighbour & MB_LEFT ) i_mba_xy = h->mb.i_mb_xy - 1; if( i_idx & 2 ) i_mbb_xy = h->mb.i_mb_xy; else if( h->mb.i_neighbour & MB_TOP ) i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride; /* no need to test skip/pcm */ if( i_mba_xy >= 0 ) i_nza = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 1]; if( i_mbb_xy >= 0 ) i_nzb = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 8]; } if( IS_INTRA( h->mb.i_type ) ) { if( i_mba_xy < 0 ) i_nza = 1; if( i_mbb_xy < 0 ) i_nzb = 1; } ctx = 4 * i_cat; if( i_nza ) ctx += 1; if( i_nzb ) ctx += 2; return ctx;}static const int significant_coeff_flag_offset[6] = { 105, 120, 134, 149, 152, 402 };static const int last_coeff_flag_offset[6] = { 166, 181, 195, 210, 213, 417 };static const int coeff_abs_level_m1_offset[6] = { 227, 237, 247, 257, 266, 426 };static const int significant_coeff_flag_offset_8x8[63] = { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7, 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11, 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12};static const int last_coeff_flag_offset_8x8[63] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8};static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int *l, int i_count ){ const int i_ctx_sig = significant_coeff_flag_offset[i_ctxBlockCat]; const int i_ctx_last = last_coeff_flag_offset[i_ctxBlockCat]; const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat]; int i_coeff_abs_m1[64]; int i_coeff_sign[64]; int i_coeff = 0; int i_last = 0; int i_sigmap_size; int i_abslevel1 = 0; int i_abslevelgt1 = 0; int i; /* i_ctxBlockCat: 0-> DC 16x16 i_idx = 0 * 1-> AC 16x16 i_idx = luma4x4idx * 2-> Luma4x4 i_idx = luma4x4idx * 3-> DC Chroma i_idx = iCbCr * 4-> AC Chroma i_idx = 4 * iCbCr + chroma4x4idx * 5-> Luma8x8 i_idx = luma8x8idx */ for( i = 0; i < i_count; i++ ) { if( l[i] != 0 ) { i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1; i_coeff_sign[i_coeff] = ( l[i] < 0 ); i_coeff++; i_last = i; } } if( i_count != 64 ) { /* coded block flag */ x264_cabac_encode_decision( cb, 85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), i_coeff != 0 ); if( i_coeff == 0 ) return; } i_sigmap_size = X264_MIN( i_last+1, i_count-1 ); for( i = 0; i < i_sigmap_size; i++ ) { int i_sig_ctxIdxInc; int i_last_ctxIdxInc; if( i_ctxBlockCat == DCT_LUMA_8x8 ) { i_sig_ctxIdxInc = significant_coeff_flag_offset_8x8[i]; i_last_ctxIdxInc = last_coeff_flag_offset_8x8[i]; } else i_sig_ctxIdxInc = i_last_ctxIdxInc = i; x264_cabac_encode_decision( cb, i_ctx_sig + i_sig_ctxIdxInc, l[i] != 0 ); if( l[i] != 0 ) x264_cabac_encode_decision( cb, i_ctx_last + i_last_ctxIdxInc, i == i_last );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -