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

📄 cabac.c

📁 H.264 source codes
💻 C
📖 第 1 页 / 共 3 页
字号:
/***************************************************************************** * 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 <stdlib.h>#include <stdio.h>#include <string.h>#include "common/common.h"#include "macroblock.h"static const uint8_t block_idx_x[16] ={    0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3};static const uint8_t block_idx_y[16] ={    0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3};static const uint8_t block_idx_xy[4][4] ={    { 0, 2, 8,  10},    { 1, 3, 9,  11},    { 4, 6, 12, 14},    { 5, 7, 13, 15}};static inline void x264_cabac_mb_type_intra( x264_t *h, int i_mb_type,                    int ctx0, int ctx1, int ctx2, int ctx3, int ctx4, int ctx5 ){    if( i_mb_type == I_4x4 )    {        x264_cabac_encode_decision( &h->cabac, ctx0, 0 );    }    else if( i_mb_type == I_PCM )    {        x264_cabac_encode_decision( &h->cabac, ctx0, 1 );        x264_cabac_encode_terminal( &h->cabac,       1 );    }    else    {        x264_cabac_encode_decision( &h->cabac, ctx0, 1 );        x264_cabac_encode_terminal( &h->cabac,       0 );        x264_cabac_encode_decision( &h->cabac, ctx1, ( h->mb.i_cbp_luma == 0 ? 0 : 1 ));        if( h->mb.i_cbp_chroma == 0 )        {            x264_cabac_encode_decision( &h->cabac, ctx2, 0 );        }        else        {            x264_cabac_encode_decision( &h->cabac, ctx2, 1 );            x264_cabac_encode_decision( &h->cabac, ctx3, ( h->mb.i_cbp_chroma == 1 ? 0 : 1 ) );        }        x264_cabac_encode_decision( &h->cabac, ctx4, ( (h->mb.i_intra16x16_pred_mode / 2) ? 1 : 0 ));        x264_cabac_encode_decision( &h->cabac, ctx5, ( (h->mb.i_intra16x16_pred_mode % 2) ? 1 : 0 ));    }}static void x264_cabac_mb_type( x264_t *h ){    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_x > 0 && h->mb.type[h->mb.i_mb_xy - 1] != I_4x4 )        {            ctx++;        }        if( h->mb.i_mb_y > 0 && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride] != I_4x4 )        {            ctx++;        }        x264_cabac_mb_type_intra( h, 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( &h->cabac, 14, 0 );                x264_cabac_encode_decision( &h->cabac, 15, 0 );                x264_cabac_encode_decision( &h->cabac, 16, 0 );            }            else if( h->mb.i_partition == D_16x8 )            {                x264_cabac_encode_decision( &h->cabac, 14, 0 );                x264_cabac_encode_decision( &h->cabac, 15, 1 );                x264_cabac_encode_decision( &h->cabac, 17, 1 );            }            else if( h->mb.i_partition == D_8x16 )            {                x264_cabac_encode_decision( &h->cabac, 14, 0 );                x264_cabac_encode_decision( &h->cabac, 15, 1 );                x264_cabac_encode_decision( &h->cabac, 17, 0 );            }        }        else if( i_mb_type == P_8x8 )        {            x264_cabac_encode_decision( &h->cabac, 14, 0 );            x264_cabac_encode_decision( &h->cabac, 15, 0 );            x264_cabac_encode_decision( &h->cabac, 16, 1 );        }        else /* intra */        {            /* prefix */            x264_cabac_encode_decision( &h->cabac, 14, 1 );            /* suffix */            x264_cabac_mb_type_intra( h, 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_x > 0 && h->mb.type[h->mb.i_mb_xy - 1] != B_SKIP && h->mb.type[h->mb.i_mb_xy - 1] != B_DIRECT )        {            ctx++;        }        if( h->mb.i_mb_y > 0 && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride] != B_SKIP && h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride] != B_DIRECT )        {            ctx++;        }        if( i_mb_type == B_DIRECT )        {            x264_cabac_encode_decision( &h->cabac, 27+ctx, 0 );        }        else if( i_mb_type == B_8x8 )        {            x264_cabac_encode_decision( &h->cabac, 27+ctx, 1 );            x264_cabac_encode_decision( &h->cabac, 27+3,   1 );            x264_cabac_encode_decision( &h->cabac, 27+4,   1 );            x264_cabac_encode_decision( &h->cabac, 27+5,   1 );            x264_cabac_encode_decision( &h->cabac, 27+5,   1 );            x264_cabac_encode_decision( &h->cabac, 27+5,   1 );        }        else if( IS_INTRA( i_mb_type ) )        {            /* prefix */            x264_cabac_encode_decision( &h->cabac, 27+ctx, 1 );            x264_cabac_encode_decision( &h->cabac, 27+3,   1 );            x264_cabac_encode_decision( &h->cabac, 27+4,   1 );            x264_cabac_encode_decision( &h->cabac, 27+5,   1 );            x264_cabac_encode_decision( &h->cabac, 27+5,   0 );            x264_cabac_encode_decision( &h->cabac, 27+5,   1 );            /* suffix */            x264_cabac_mb_type_intra( h, i_mb_type, 32+0, 32+1, 32+2, 32+2, 32+3, 32+3 );        }        else        {            static const int i_mb_len[21] =            {                3, 6, 6,    /* L0 L0 */                3, 6, 6,    /* L1 L1 */                6, 7, 7,    /* BI BI */                6, 6,       /* L0 L1 */                6, 6,       /* L1 L0 */                7, 7,       /* L0 BI */                7, 7,       /* L1 BI */                7, 7,       /* BI L0 */                7, 7,       /* BI L1 */            };            static const int i_mb_bits[21][7] =            {                { 1, 0, 0, },            { 1, 1, 0, 0, 0, 1, },    { 1, 1, 0, 0, 1, 0, },   /* L0 L0 */                { 1, 0, 1, },            { 1, 1, 0, 0, 1, 1, },    { 1, 1, 0, 1, 0, 0, },   /* L1 L1 */                { 1, 1, 0, 0, 0, 0 ,},   { 1, 1, 1, 1, 0, 0 , 0 }, { 1, 1, 1, 1, 0, 0 , 1 },/* BI BI */                { 1, 1, 0, 1, 0, 1, },   { 1, 1, 0, 1, 1, 0, },     /* L0 L1 */                { 1, 1, 0, 1, 1, 1, },   { 1, 1, 1, 1, 1, 0, },     /* L1 L0 */                { 1, 1, 1, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 1 },   /* L0 BI */                { 1, 1, 1, 0, 0, 1, 0 }, { 1, 1, 1, 0, 0, 1, 1 },   /* L1 BI */                { 1, 1, 1, 0, 1, 0, 0 }, { 1, 1, 1, 0, 1, 0, 1 },   /* BI L0 */                { 1, 1, 1, 0, 1, 1, 0 }, { 1, 1, 1, 0, 1, 1, 1 }    /* BI L1 */            };            const int i_partition = h->mb.i_partition;            int idx = 0;            int i;            switch( i_mb_type )            {                /* D_16x16, D_16x8, D_8x16 */                case B_BI_BI: idx += 3;                case B_L1_L1: idx += 3;                case B_L0_L0:                    if( i_partition == D_16x8 )                        idx += 1;                    else if( i_partition == D_8x16 )                        idx += 2;                    break;                /* D_16x8, D_8x16 */                case B_BI_L1: idx += 2;                case B_BI_L0: idx += 2;                case B_L1_BI: idx += 2;                case B_L0_BI: idx += 2;                case B_L1_L0: idx += 2;                case B_L0_L1:                    idx += 3*3;                    if( i_partition == D_8x16 )                        idx++;                    break;                default:                    x264_log(h, X264_LOG_ERROR, "error in B mb type\n" );                    return;            }            x264_cabac_encode_decision( &h->cabac, 27+ctx,                         i_mb_bits[idx][0] );            x264_cabac_encode_decision( &h->cabac, 27+3,                           i_mb_bits[idx][1] );            x264_cabac_encode_decision( &h->cabac, 27+(i_mb_bits[idx][1] != 0 ? 4 : 5), i_mb_bits[idx][2] );            for( i = 3; i < i_mb_len[idx]; i++ )            {                x264_cabac_encode_decision( &h->cabac, 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_t *h, int i_pred, int i_mode ){    if( i_pred == i_mode )    {        /* b_prev_intra4x4_pred_mode */        x264_cabac_encode_decision( &h->cabac, 68, 1 );    }    else    {        /* b_prev_intra4x4_pred_mode */        x264_cabac_encode_decision( &h->cabac, 68, 0 );        if( i_mode > i_pred  )        {            i_mode--;        }        x264_cabac_encode_decision( &h->cabac, 69, (i_mode     )&0x01 );        x264_cabac_encode_decision( &h->cabac, 69, (i_mode >> 1)&0x01 );        x264_cabac_encode_decision( &h->cabac, 69, (i_mode >> 2)&0x01 );    }}static void x264_cabac_mb_intra8x8_pred_mode( x264_t *h ){    const int i_mode  = 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_mb_x > 0 && h->mb.chroma_pred_mode[h->mb.i_mb_xy - 1] != 0 )    {        ctx++;    }    if( h->mb.i_mb_y > 0 && h->mb.chroma_pred_mode[h->mb.i_mb_xy - h->mb.i_mb_stride] != 0 )    {        ctx++;    }    if( i_mode == 0 )    {        x264_cabac_encode_decision( &h->cabac, 64 + ctx, 0 );    }    else    {        x264_cabac_encode_decision( &h->cabac, 64 + ctx, 1 );        x264_cabac_encode_decision( &h->cabac, 64 + 3, ( i_mode == 1 ? 0 : 1 ) );        if( i_mode > 1 )        {            x264_cabac_encode_decision( &h->cabac, 64 + 3, ( i_mode == 2 ? 0 : 1 ) );        }    }}static void x264_cabac_mb_cbp_luma( x264_t *h ){    /* 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_mb_x > 0 )            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_mb_y > 0 )            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( &h->cabac, 73 + ctx, (h->mb.i_cbp_luma >> i8x8)&0x01 );    }}static void x264_cabac_mb_cbp_chroma( x264_t *h ){    int cbp_a = -1;    int cbp_b = -1;    int ctx;    /* No need to test for SKIP/PCM */    if( h->mb.i_mb_x > 0 )    {        cbp_a = (h->mb.cbp[h->mb.i_mb_xy - 1] >> 4)&0x3;    }    if( h->mb.i_mb_y > 0 )    {        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( &h->cabac, 77 + ctx, 0 );    }    else    {        x264_cabac_encode_decision( &h->cabac, 77 + ctx, 1 );        ctx = 4;        if( cbp_a == 2 ) ctx++;        if( cbp_b == 2 ) ctx += 2;        x264_cabac_encode_decision( &h->cabac, 77 + ctx, h->mb.i_cbp_chroma > 1 ? 1 : 0 );    }}/* TODO check it with != qp per mb */static void x264_cabac_mb_qp_delta( x264_t *h ){    int i_mbn_xy = h->mb.i_mb_xy - 1;    int i_dqp = h->mb.qp[h->mb.i_mb_xy] - h->mb.i_last_qp;    int val = i_dqp <= 0 ? (-2*i_dqp) : (2*i_dqp - 1);    int ctx;    /* No need to test for PCM / SKIP */    if( i_mbn_xy >= 0 && 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;    while( val > 0 )    {

⌨️ 快捷键说明

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