⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cabac.c

📁 H.264 source codes
💻 C
📖 第 1 页 / 共 3 页
字号:
        {            i_nza = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 1];        }        if( i_mbb_xy >= 0 && (h->mb.cbp[i_mbb_xy]&0x30) == 0x20 )        {            i_nzb = h->mb.cache.non_zero_count[x264_scan8[16+i_idx] - 8];        }    }    if( ( i_mba_xy < 0  && IS_INTRA( h->mb.i_type ) ) || i_nza > 0 )    {        ctx++;    }    if( ( i_mbb_xy < 0  && IS_INTRA( h->mb.i_type ) ) || i_nzb > 0 )    {        ctx += 2;    }    return 4 * i_cat + ctx;}static void block_residual_write_cabac( x264_t *h, int i_ctxBlockCat, int i_idx, int *l, int i_count ){    static const int significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };    static const int last_significant_coeff_flag_offset[5] = { 0, 15, 29, 44, 47 };    static const int coeff_abs_level_m1_offset[5] = { 0, 10, 20, 30, 39 };    int i_coeff_abs_m1[16];    int i_coeff_sign[16];    int i_coeff = 0;    int i_last  = 0;    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     */    //fprintf( stderr, "l[] = " );    for( i = 0; i < i_count; i++ )    {        //fprintf( stderr, "%d ", l[i] );        if( l[i] != 0 )        {            i_coeff_abs_m1[i_coeff] = abs( l[i] ) - 1;            i_coeff_sign[i_coeff]   = ( l[i] < 0 ? 1 : 0);            i_coeff++;            i_last = i;        }    }    //fprintf( stderr, "\n" );    if( i_coeff == 0 )    {        /* codec block flag */        x264_cabac_encode_decision( &h->cabac,  85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), 0 );        return;    }    /* block coded */    x264_cabac_encode_decision( &h->cabac,  85 + x264_cabac_mb_cbf_ctxidxinc( h, i_ctxBlockCat, i_idx ), 1 );    for( i = 0; i < i_count - 1; i++ )    {        int i_ctxIdxInc;        i_ctxIdxInc = X264_MIN( i, i_count - 2 );        if( l[i] != 0 )        {            x264_cabac_encode_decision( &h->cabac, 105 + significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, 1 );            x264_cabac_encode_decision( &h->cabac, 166 + last_significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, i == i_last ? 1 : 0 );        }        else        {            x264_cabac_encode_decision( &h->cabac, 105 + significant_coeff_flag_offset[i_ctxBlockCat] + i_ctxIdxInc, 0 );        }        if( i == i_last )        {            break;        }    }    for( i = i_coeff - 1; i >= 0; i-- )    {        int i_prefix;        int i_ctxIdxInc;        /* write coeff_abs - 1 */        /* prefix */        i_prefix = X264_MIN( i_coeff_abs_m1[i], 14 );        i_ctxIdxInc = (i_abslevelgt1 != 0 ? 0 : X264_MIN( 4, i_abslevel1 + 1 )) + coeff_abs_level_m1_offset[i_ctxBlockCat];        if( i_prefix == 0 )        {            x264_cabac_encode_decision( &h->cabac,  227 + i_ctxIdxInc, 0 );        }        else        {            int j;            x264_cabac_encode_decision( &h->cabac,  227 + i_ctxIdxInc, 1 );            i_ctxIdxInc = 5 + X264_MIN( 4, i_abslevelgt1 ) + coeff_abs_level_m1_offset[i_ctxBlockCat];            for( j = 0; j < i_prefix - 1; j++ )            {                x264_cabac_encode_decision( &h->cabac,  227 + i_ctxIdxInc, 1 );            }            if( i_prefix < 14 )            {                x264_cabac_encode_decision( &h->cabac,  227 + i_ctxIdxInc, 0 );            }        }        /* suffix */        if( i_coeff_abs_m1[i] >= 14 )        {            int k = 0;            int i_suffix = i_coeff_abs_m1[i] - 14;            while( i_suffix >= (1<<k) )            {                x264_cabac_encode_bypass( &h->cabac, 1 );                i_suffix -= 1 << k;                k++;            }            x264_cabac_encode_bypass( &h->cabac, 0 );            while( k-- )            {                x264_cabac_encode_bypass( &h->cabac, (i_suffix >> k)&0x01 );            }        }        /* write sign */        x264_cabac_encode_bypass( &h->cabac, i_coeff_sign[i] );        if( i_coeff_abs_m1[i] == 0 )        {            i_abslevel1++;        }        else        {            i_abslevelgt1++;        }    }}void x264_macroblock_write_cabac( x264_t *h, bs_t *s ){    const int i_mb_type = h->mb.i_type;    const int i_mb_pos_start = bs_pos( s );    int       i_mb_pos_tex;    int i_list;    int i;    /* Write the MB type */    x264_cabac_mb_type( h );    /* PCM special block type UNTESTED */    if( i_mb_type == I_PCM )    {        bs_align_0( s );    /* not sure */        /* Luma */        for( i = 0; i < 16*16; i++ )        {            const int x = 16 * h->mb.i_mb_x + (i % 16);            const int y = 16 * h->mb.i_mb_y + (i / 16);            bs_write( s, 8, h->fenc->plane[0][y*h->mb.pic.i_stride[0]+x] );        }        /* Cb */        for( i = 0; i < 8*8; i++ )        {            const int x = 8 * h->mb.i_mb_x + (i % 8);            const int y = 8 * h->mb.i_mb_y + (i / 8);            bs_write( s, 8, h->fenc->plane[1][y*h->mb.pic.i_stride[1]+x] );        }        /* Cr */        for( i = 0; i < 8*8; i++ )        {            const int x = 8 * h->mb.i_mb_x + (i % 8);            const int y = 8 * h->mb.i_mb_y + (i / 8);            bs_write( s, 8, h->fenc->plane[2][y*h->mb.pic.i_stride[2]+x] );        }        x264_cabac_encode_init( &h->cabac, s );        return;    }    if( IS_INTRA( i_mb_type ) )    {        /* Prediction */        if( i_mb_type == I_4x4 )        {            for( i = 0; i < 16; i++ )            {                const int i_pred = x264_mb_predict_intra4x4_mode( h, i );                const int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];                x264_cabac_mb_intra4x4_pred_mode( h, i_pred, i_mode );            }        }        x264_cabac_mb_intra8x8_pred_mode( h );    }    else if( i_mb_type == P_L0 )    {        if( h->mb.i_partition == D_16x16 )        {            if( h->sh.i_num_ref_idx_l0_active > 1 )            {                x264_cabac_mb_ref( h, 0, 0 );            }            x264_cabac_mb_mvd( h, 0, 0, 4, 4 );        }        else if( h->mb.i_partition == D_16x8 )        {            if( h->sh.i_num_ref_idx_l0_active > 1 )            {                x264_cabac_mb_ref( h, 0, 0 );                x264_cabac_mb_ref( h, 0, 8 );            }            x264_cabac_mb_mvd( h, 0, 0, 4, 2 );            x264_cabac_mb_mvd( h, 0, 8, 4, 2 );        }        else if( h->mb.i_partition == D_8x16 )        {            if( h->sh.i_num_ref_idx_l0_active > 1 )            {                x264_cabac_mb_ref( h, 0, 0 );                x264_cabac_mb_ref( h, 0, 4 );            }            x264_cabac_mb_mvd( h, 0, 0, 2, 4 );            x264_cabac_mb_mvd( h, 0, 4, 2, 4 );        }    }    else if( i_mb_type == P_8x8 )    {        /* sub mb type */        x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[0] );        x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[1] );        x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[2] );        x264_cabac_mb_sub_p_partition( h, h->mb.i_sub_partition[3] );        /* ref 0 */        if( h->sh.i_num_ref_idx_l0_active > 1 )        {            x264_cabac_mb_ref( h, 0, 0 );            x264_cabac_mb_ref( h, 0, 4 );            x264_cabac_mb_ref( h, 0, 8 );            x264_cabac_mb_ref( h, 0, 12 );        }        x264_cabac_mb8x8_mvd( h, 0 );    }    else if( i_mb_type == B_8x8 )    {        /* sub mb type */        x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[0] );        x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[1] );        x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[2] );        x264_cabac_mb_sub_b_partition( h, h->mb.i_sub_partition[3] );        /* ref */        for( i_list = 0; i_list < 2; i_list++ )        {            if( ( i_list ? h->sh.i_num_ref_idx_l1_active : h->sh.i_num_ref_idx_l0_active ) == 1 )                continue;            for( i = 0; i < 4; i++ )            {                if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] )                {                    x264_cabac_mb_ref( h, i_list, 4*i );                }            }        }        x264_cabac_mb8x8_mvd( h, 0 );        x264_cabac_mb8x8_mvd( h, 1 );    }    else if( i_mb_type != B_DIRECT )    {        /* All B mode */        int b_list[2][2];        /* init ref list utilisations */        for( i = 0; i < 2; i++ )        {            b_list[0][i] = x264_mb_type_list0_table[i_mb_type][i];            b_list[1][i] = x264_mb_type_list1_table[i_mb_type][i];        }        for( i_list = 0; i_list < 2; i_list++ )        {            const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active;            if( i_ref_max > 1 )            {                if( h->mb.i_partition == D_16x16 )                {                    if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );                }                else if( h->mb.i_partition == D_16x8 )                {                    if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );                    if( b_list[i_list][1] ) x264_cabac_mb_ref( h, i_list, 8 );                }                else if( h->mb.i_partition == D_8x16 )                {                    if( b_list[i_list][0] ) x264_cabac_mb_ref( h, i_list, 0 );                    if( b_list[i_list][1] ) x264_cabac_mb_ref( h, i_list, 4 );                }            }        }        for( i_list = 0; i_list < 2; i_list++ )        {            if( h->mb.i_partition == D_16x16 )            {                if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 4, 4 );            }            else if( h->mb.i_partition == D_16x8 )            {                if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 4, 2 );                if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, i_list, 8, 4, 2 );            }            else if( h->mb.i_partition == D_8x16 )            {                if( b_list[i_list][0] ) x264_cabac_mb_mvd( h, i_list, 0, 2, 4 );                if( b_list[i_list][1] ) x264_cabac_mb_mvd( h, i_list, 4, 2, 4 );            }        }    }    i_mb_pos_tex = bs_pos( s );    h->stat.frame.i_hdr_bits += i_mb_pos_tex - i_mb_pos_start;    if( i_mb_type != I_16x16 )    {        x264_cabac_mb_cbp_luma( h );        x264_cabac_mb_cbp_chroma( h );    }    if( h->mb.i_cbp_luma > 0 || h->mb.i_cbp_chroma > 0 || i_mb_type == I_16x16 )    {        x264_cabac_mb_qp_delta( h );        /* write residual */        if( i_mb_type == I_16x16 )        {            /* DC Luma */            block_residual_write_cabac( h, 0, 0, h->dct.luma16x16_dc, 16 );            if( h->mb.i_cbp_luma != 0 )            {                /* AC Luma */                for( i = 0; i < 16; i++ )                {                    block_residual_write_cabac( h, 1, i, h->dct.block[i].residual_ac, 15 );                }            }        }        else        {            for( i = 0; i < 16; i++ )            {                if( h->mb.i_cbp_luma & ( 1 << ( i / 4 ) ) )                {                    block_residual_write_cabac( h, 2, i, h->dct.block[i].luma4x4, 16 );                }            }        }        if( h->mb.i_cbp_chroma &0x03 )    /* Chroma DC residual present */        {            block_residual_write_cabac( h, 3, 0, h->dct.chroma_dc[0], 4 );            block_residual_write_cabac( h, 3, 1, h->dct.chroma_dc[1], 4 );        }        if( h->mb.i_cbp_chroma&0x02 ) /* Chroma AC residual present */        {            for( i = 0; i < 8; i++ )            {                block_residual_write_cabac( h, 4, i, h->dct.block[16+i].residual_ac, 15 );            }        }    }    if( IS_INTRA( i_mb_type ) )        h->stat.frame.i_itex_bits += bs_pos(s) - i_mb_pos_tex;    else        h->stat.frame.i_ptex_bits += bs_pos(s) - i_mb_pos_tex;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -