📄 enc_cabac.c
字号:
/***************************************************************************** * cabac.c: h264 encoder library ***************************************************************************** * Copyright (C) 2003 Laurent Aimar * $Id: cabac.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************/#include <stdio.h>#include <string.h>#include "common/common.h"#include "enc_macroblock.h"static inline void x264_cabac_encode_ue_bypass( x264_cabac_t *cb, int exp_bits, int val ){#ifdef RDO_SKIP_BS cb->f8_bits_encoded += ( bs_size_ue( val + (1<<exp_bits)-1 ) - exp_bits ) << 8;#else int k; for( k = exp_bits; val >= (1<<k); k++ ) { x264_cabac_encode_bypass( cb, 1 ); val -= 1 << k; } x264_cabac_encode_bypass( cb, 0 ); while( k-- ) x264_cabac_encode_bypass( cb, (val >> k)&0x01 );#endif}static inline void x264_cabac_mb_type_intra( x264_t *h, x264_cabac_t *cb, int i_mb_type, int ctx0, int ctx1, int ctx2, int ctx3, int ctx4, int ctx5 ){ if( i_mb_type == I_4x4 || i_mb_type == I_8x8 ) { x264_cabac_encode_decision( cb, ctx0, 0 ); } else if( i_mb_type == I_PCM ) { x264_cabac_encode_decision( cb, ctx0, 1 ); x264_cabac_encode_terminal( cb, 1 ); x264_cabac_encode_flush( cb ); } else { int i_pred = x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode]; x264_cabac_encode_decision( cb, ctx0, 1 ); x264_cabac_encode_terminal( cb, 0 ); x264_cabac_encode_decision( cb, ctx1, ( h->mb.i_cbp_luma == 0 ? 0 : 1 )); if( h->mb.i_cbp_chroma == 0 ) { x264_cabac_encode_decision( cb, ctx2, 0 ); } else { x264_cabac_encode_decision( cb, ctx2, 1 ); x264_cabac_encode_decision( cb, ctx3, ( h->mb.i_cbp_chroma == 1 ? 0 : 1 ) ); } x264_cabac_encode_decision( cb, ctx4, ( (i_pred / 2) ? 1 : 0 )); x264_cabac_encode_decision( cb, ctx5, ( (i_pred % 2) ? 1 : 0 )); }}static void x264_cabac_mb_type( x264_t *h, x264_cabac_t *cb ){ const int i_mb_type = h->mb.i_type; if( h->sh.i_type == SLICE_TYPE_I ) { int ctx = 0; if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != I_4x4 ) { ctx++; } if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != I_4x4 ) { ctx++; } x264_cabac_mb_type_intra( h, cb, i_mb_type, 3+ctx, 3+3, 3+4, 3+5, 3+6, 3+7 ); } else if( h->sh.i_type == SLICE_TYPE_P ) { /* prefix: 14, suffix: 17 */ if( i_mb_type == P_L0 ) { if( h->mb.i_partition == D_16x16 ) { x264_cabac_encode_decision( cb, 14, 0 ); x264_cabac_encode_decision( cb, 15, 0 ); x264_cabac_encode_decision( cb, 16, 0 ); } else if( h->mb.i_partition == D_16x8 ) { x264_cabac_encode_decision( cb, 14, 0 ); x264_cabac_encode_decision( cb, 15, 1 ); x264_cabac_encode_decision( cb, 17, 1 ); } else if( h->mb.i_partition == D_8x16 ) { x264_cabac_encode_decision( cb, 14, 0 ); x264_cabac_encode_decision( cb, 15, 1 ); x264_cabac_encode_decision( cb, 17, 0 ); } } else if( i_mb_type == P_8x8 ) { x264_cabac_encode_decision( cb, 14, 0 ); x264_cabac_encode_decision( cb, 15, 0 ); x264_cabac_encode_decision( cb, 16, 1 ); } else /* intra */ { /* prefix */ x264_cabac_encode_decision( cb, 14, 1 ); /* suffix */ x264_cabac_mb_type_intra( h, cb, i_mb_type, 17+0, 17+1, 17+2, 17+2, 17+3, 17+3 ); } } else if( h->sh.i_type == SLICE_TYPE_B ) { int ctx = 0; if( h->mb.i_mb_type_left >= 0 && h->mb.i_mb_type_left != B_SKIP && h->mb.i_mb_type_left != B_DIRECT ) { ctx++; } if( h->mb.i_mb_type_top >= 0 && h->mb.i_mb_type_top != B_SKIP && h->mb.i_mb_type_top != B_DIRECT ) { ctx++; } if( i_mb_type == B_DIRECT ) { x264_cabac_encode_decision( cb, 27+ctx, 0 ); } else if( i_mb_type == B_8x8 ) { x264_cabac_encode_decision( cb, 27+ctx, 1 ); x264_cabac_encode_decision( cb, 27+3, 1 ); x264_cabac_encode_decision( cb, 27+4, 1 ); x264_cabac_encode_decision( cb, 27+5, 1 ); x264_cabac_encode_decision( cb, 27+5, 1 ); x264_cabac_encode_decision( cb, 27+5, 1 ); } else if( IS_INTRA( i_mb_type ) ) { /* prefix */ x264_cabac_encode_decision( cb, 27+ctx, 1 ); x264_cabac_encode_decision( cb, 27+3, 1 ); x264_cabac_encode_decision( cb, 27+4, 1 ); x264_cabac_encode_decision( cb, 27+5, 1 ); x264_cabac_encode_decision( cb, 27+5, 0 ); x264_cabac_encode_decision( cb, 27+5, 1 ); /* suffix */ x264_cabac_mb_type_intra( h, cb, i_mb_type, 32+0, 32+1, 32+2, 32+2, 32+3, 32+3 ); } else { static const int i_mb_len[9*3] = { 6, 6, 3, /* L0 L0 */ 6, 6, 0, /* L0 L1 */ 7, 7, 0, /* L0 BI */ 6, 6, 0, /* L1 L0 */ 6, 6, 3, /* L1 L1 */ 7, 7, 0, /* L1 BI */ 7, 7, 0, /* BI L0 */ 7, 7, 0, /* BI L1 */ 7, 7, 6, /* BI BI */ }; static const int i_mb_bits[9*3][7] = { { 1,1,0,0,0,1 }, { 1,1,0,0,1,0, }, { 1,0,0 }, /* L0 L0 */ { 1,1,0,1,0,1 }, { 1,1,0,1,1,0 }, {0}, /* L0 L1 */ { 1,1,1,0,0,0,0 }, { 1,1,1,0,0,0,1 }, {0}, /* L0 BI */ { 1,1,0,1,1,1 }, { 1,1,1,1,1,0 }, {0}, /* L1 L0 */ { 1,1,0,0,1,1 }, { 1,1,0,1,0,0 }, { 1,0,1 }, /* L1 L1 */ { 1,1,1,0,0,1,0 }, { 1,1,1,0,0,1,1 }, {0}, /* L1 BI */ { 1,1,1,0,1,0,0 }, { 1,1,1,0,1,0,1 }, {0}, /* BI L0 */ { 1,1,1,0,1,1,0 }, { 1,1,1,0,1,1,1 }, {0}, /* BI L1 */ { 1,1,1,1,0,0,0 }, { 1,1,1,1,0,0,1 }, { 1,1,0,0,0,0 }, /* BI BI */ }; const int idx = (i_mb_type - B_L0_L0) * 3 + (h->mb.i_partition - D_16x8); int i; x264_cabac_encode_decision( cb, 27+ctx, i_mb_bits[idx][0] ); x264_cabac_encode_decision( cb, 27+3, i_mb_bits[idx][1] ); x264_cabac_encode_decision( cb, 27+5-i_mb_bits[idx][1], i_mb_bits[idx][2] ); for( i = 3; i < i_mb_len[idx]; i++ ) x264_cabac_encode_decision( cb, 27+5, i_mb_bits[idx][i] ); } } else { x264_log(h, X264_LOG_ERROR, "unknown SLICE_TYPE unsupported in x264_macroblock_write_cabac\n" ); }}static void x264_cabac_mb_intra4x4_pred_mode( x264_cabac_t *cb, int i_pred, int i_mode ){ if( i_pred == i_mode ) { /* b_prev_intra4x4_pred_mode */ x264_cabac_encode_decision( cb, 68, 1 ); } else { /* b_prev_intra4x4_pred_mode */ x264_cabac_encode_decision( cb, 68, 0 ); if( i_mode > i_pred ) { i_mode--; } x264_cabac_encode_decision( cb, 69, (i_mode )&0x01 ); x264_cabac_encode_decision( cb, 69, (i_mode >> 1)&0x01 ); x264_cabac_encode_decision( cb, 69, (i_mode >> 2)&0x01 ); }}static void x264_cabac_mb_intra_chroma_pred_mode( x264_t *h, x264_cabac_t *cb ){ const int i_mode = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ]; int ctx = 0; /* No need to test for I4x4 or I_16x16 as cache_save handle that */ if( (h->mb.i_neighbour & MB_LEFT) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - 1] != 0 ) { ctx++; } if( (h->mb.i_neighbour & MB_TOP) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - h->mb.i_mb_stride] != 0 ) { ctx++; } x264_cabac_encode_decision( cb, 64 + ctx, i_mode > 0 ); if( i_mode > 0 ) { x264_cabac_encode_decision( cb, 64 + 3, i_mode > 1 ); if( i_mode > 1 ) { x264_cabac_encode_decision( cb, 64 + 3, i_mode > 2 ); } }}static void x264_cabac_mb_cbp_luma( x264_t *h, x264_cabac_t *cb ){ /* TODO: clean up and optimize */ int i8x8; for( i8x8 = 0; i8x8 < 4; i8x8++ ) { int i_mba_xy = -1; int i_mbb_xy = -1; int x = block_idx_x[4*i8x8]; int y = block_idx_y[4*i8x8]; int ctx = 0; if( 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( 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 PCM and SKIP */ if( i_mba_xy >= 0 ) { const int i8x8a = block_idx_xy[(x-1)&0x03][y]/4; if( ((h->mb.cbp[i_mba_xy] >> i8x8a)&0x01) == 0 ) { ctx++; } } if( i_mbb_xy >= 0 ) { const int i8x8b = block_idx_xy[x][(y-1)&0x03]/4; if( ((h->mb.cbp[i_mbb_xy] >> i8x8b)&0x01) == 0 ) { ctx += 2; } } x264_cabac_encode_decision( cb, 73 + ctx, (h->mb.i_cbp_luma >> i8x8)&0x01 ); }}static void x264_cabac_mb_cbp_chroma( x264_t *h, x264_cabac_t *cb ){ int cbp_a = -1; int cbp_b = -1; int ctx; /* No need to test for SKIP/PCM */ if( h->mb.i_neighbour & MB_LEFT ) { cbp_a = (h->mb.cbp[h->mb.i_mb_xy - 1] >> 4)&0x3; } if( h->mb.i_neighbour & MB_TOP ) { cbp_b = (h->mb.cbp[h->mb.i_mb_xy - h->mb.i_mb_stride] >> 4)&0x3; } ctx = 0; if( cbp_a > 0 ) ctx++; if( cbp_b > 0 ) ctx += 2; if( h->mb.i_cbp_chroma == 0 ) { x264_cabac_encode_decision( cb, 77 + ctx, 0 ); } else { x264_cabac_encode_decision( cb, 77 + ctx, 1 ); ctx = 4; if( cbp_a == 2 ) ctx++; if( cbp_b == 2 ) ctx += 2; x264_cabac_encode_decision( cb, 77 + ctx, h->mb.i_cbp_chroma > 1 ); }}/* TODO check it with != qp per mb */static void x264_cabac_mb_qp_delta( x264_t *h, x264_cabac_t *cb ){ int i_mbn_xy = h->mb.i_mb_xy - 1; int i_dqp = h->mb.i_qp - h->mb.i_last_qp; int ctx; /* No need to test for PCM / SKIP */ if( i_mbn_xy >= h->sh.i_first_mb && h->mb.i_last_dqp != 0 && ( h->mb.type[i_mbn_xy] == I_16x16 || (h->mb.cbp[i_mbn_xy]&0x3f) ) ) ctx = 1; else ctx = 0; if( i_dqp != 0 ) { int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1); /* dqp is interpreted modulo 52 */ if( val >= 51 && val != 52 ) val = 103 - val; while( val-- ) { x264_cabac_encode_decision( cb, 60 + ctx, 1 ); if( ctx < 2 ) ctx = 2; else ctx = 3; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -