ffmpeg解码h264宏块函数ff_h264_decode_mb_cabac注释

34 篇文章 12 订阅

一 解析是否为skip宏块,skip块,指的是没有mvd也没有残差的块。

帧间块才有这种。

const SPS *sps = h->ps.sps;
    int mb_xy;
    int mb_type, partition_count, cbp = 0;
    int dct8x8_allowed = h->ps.pps->transform_8x8_mode; 
    const int decode_chroma = sps->chroma_format_idc == 1 || sps->chroma_format_idc == 2;
    const int pixel_shift = h->pixel_shift;

    mb_xy = sl->mb_xy = sl->mb_x + sl->mb_y*h->mb_stride;

    ff_tlog(h->avctx, "pic:%d mb:%d/%d\n", h->poc.frame_num, sl->mb_x, sl->mb_y);
    if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
        int skip;
        /* a skipped mb needs the aff flag from the following mb */
        if (FRAME_MBAFF(h) && (sl->mb_y & 1) == 1 && sl->prev_mb_skipped)
            skip = sl->next_mb_skipped;
        else
            skip = decode_cabac_mb_skip(h, sl, sl->mb_x, sl->mb_y );
        /* read skip flags */
        if( skip ) {
            if (FRAME_MBAFF(h) && (sl->mb_y & 1) == 0) {
                h->cur_pic.mb_type[mb_xy] = MB_TYPE_SKIP;
                sl->next_mb_skipped = decode_cabac_mb_skip(h, sl, sl->mb_x, sl->mb_y+1 );
                if(!sl->next_mb_skipped)
                    sl->mb_mbaff = sl->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h, sl);
            }

            decode_mb_skip(h, sl);

            h->cbp_table[mb_xy] = 0;
            h->chroma_pred_mode_table[mb_xy] = 0;
            sl->last_qscale_diff = 0;
            return 0;
        }
    }
    //解析是否为skip块

然后填充相邻块信息,因为解码当前块需要用到周围块的相关信息,比如需要计算mvp等。

二 解析宏块类型mb_type,B帧中的块类型解析,B块会多一些分析,比如是否为Direct块(有残差,没有MVD)

fill_decode_neighbors(h, sl, -(MB_FIELD(sl))); //填充周围块的相关信息
    if (sl->slice_type_nos == AV_PICTURE_TYPE_B) { //如果是B帧
        int ctx = 0;
        av_assert2(sl->slice_type_nos == AV_PICTURE_TYPE_B);

        if (!IS_DIRECT(sl->left_type[LTOP] - 1))
            ctx++;
        if (!IS_DIRECT(sl->top_type - 1))
            ctx++;

        if( !get_cabac_noinline( &sl->cabac, &sl->cabac_state[27+ctx] ) ){
            mb_type= 0; /* B_Direct_16x16 */
        } else if ( !get_cabac_noinline( &sl->cabac, &sl->cabac_state[27+3] ) ) {
            mb_type= 1 + get_cabac_noinline( &sl->cabac, &sl->cabac_state[27+5] ); /* B_L[01]_16x16 */
        } else {
            int bits;
            bits = get_cabac_noinline( &sl->cabac, &sl->cabac_state[27+4] ) << 3;
            bits+= get_cabac_noinline( &sl->cabac, &sl->cabac_state[27+5] ) << 2;
            bits+= get_cabac_noinline( &sl->cabac, &sl->cabac_state[27+5] ) << 1;
            bits+= get_cabac_noinline( &sl->cabac, &sl->cabac_state[27+5] );
            if( bits < 8 ){ //解析宏块类型
                mb_type= bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
            }else if( bits == 13 ){
                mb_type = decode_cabac_intra_mb_type(sl, 32, 0);
                goto decode_intra_mb;
            }else if( bits == 14 ){
                mb_type= 11; /* B_L1_L0_8x16 */
            }else if( bits == 15 ){
                mb_type= 22; /* B_8x8 */
            }else{
                bits= ( bits<<1 ) + get_cabac_noinline( &sl->cabac, &sl->cabac_state[27+5] );
                mb_type= bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
            }
        }
            partition_count = ff_h264_b_mb_type_info[mb_type].partition_count;
            mb_type         = ff_h264_b_mb_type_info[mb_type].type;
    } else if (sl->slice_type_nos == AV_PICTURE_TYPE_P) {

三 P帧中块类型的解析

else if (sl->slice_type_nos == AV_PICTURE_TYPE_P) {
        if( get_cabac_noinline( &sl->cabac, &sl->cabac_state[14] ) == 0 ) {
            /* P-type */
            if( get_cabac_noinline( &sl->cabac, &sl->cabac_state[15] ) == 0 ) {
                /* P_L0_D16x16, P_8x8 */
                mb_type= 3 * get_cabac_noinline( &sl->cabac, &sl->cabac_state[16] );
            } else {
                /* P_L0_D8x16, P_L0_D16x8 */
                mb_type= 2 - get_cabac_noinline( &sl->cabac, &sl->cabac_state[17] );
            }
            partition_count = ff_h264_p_mb_type_info[mb_type].partition_count;
            mb_type         = ff_h264_p_mb_type_info[mb_type].type;
        } else {
            mb_type = decode_cabac_intra_mb_type(sl, 17, 0);
            goto decode_intra_mb;
        }
    } 

四 帧内块解析

else {
        mb_type = decode_cabac_intra_mb_type(sl, 3, 1);
        if (sl->slice_type == AV_PICTURE_TYPE_SI && mb_type)
            mb_type--;
        av_assert2(sl->slice_type_nos == AV_PICTURE_TYPE_I);
decode_intra_mb:
        partition_count = 0;
        cbp                      = ff_h264_i_mb_type_info[mb_type].cbp;
        sl->intra16x16_pred_mode = ff_h264_i_mb_type_info[mb_type].pred_mode;
        mb_type                  = ff_h264_i_mb_type_info[mb_type].type;
    }

五 如果是帧内块 (IPB帧都会有)

if( IS_INTRA( mb_type ) ) { //如果是帧内块
        int i, pred_mode;
        if( IS_INTRA4x4( mb_type ) ) { //如果是4x4的帧内块
            if (dct8x8_allowed /*如果8x8是允许的*/ 
            && get_cabac_noinline(&sl->cabac, &sl->cabac_state[399 + sl->neighbor_transform_size])) {
                mb_type |= MB_TYPE_8x8DCT; //宏块类型8x8 DCT
                for( i = 0; i < 16; i+=4 ) {
                    int pred = pred_intra_mode(h, sl, i); //预测模式 
                    int mode = decode_cabac_mb_intra4x4_pred_mode(sl, pred); //解码cabac intra 4x4 预测模式
                    fill_rectangle(&sl->intra4x4_pred_mode_cache[scan8[i]], 2, 2, 8, mode, 1);
                }
            } else {
                for( i = 0; i < 16; i++ ) {
                    int pred = pred_intra_mode(h, sl, i);
                    sl->intra4x4_pred_mode_cache[scan8[i]] = decode_cabac_mb_intra4x4_pred_mode(sl, pred);

                    ff_tlog(h->avctx, "i4x4 pred=%d mode=%d\n", pred,
                            sl->intra4x4_pred_mode_cache[scan8[i]]);
                }
            }
            //会写当前块的预测模式
            write_back_intra_pred_mode(h, sl);
            if (ff_h264_check_intra4x4_pred_mode(sl->intra4x4_pred_mode_cache, h->avctx,
                                                 sl->top_samples_available, sl->left_samples_available) < 0 )
                return -1;
        } else { //其它就是 16x16的模式
            sl->intra16x16_pred_mode = ff_h264_check_intra_pred_mode(h->avctx, sl->top_samples_available,
                                                                     sl->left_samples_available, sl->intra16x16_pred_mode, 0);
            if (sl->intra16x16_pred_mode < 0) return -1;
        }

        if(decode_chroma){ //如果有色度信息 , 解码色度宏块类型
            h->chroma_pred_mode_table[mb_xy] =
            pred_mode                        = decode_cabac_mb_chroma_pre_mode(h, sl);
            //获取色度预测模式

            pred_mode = ff_h264_check_intra_pred_mode(h->avctx, sl->top_samples_available,
                                                     sl->left_samples_available, pred_mode, 1 );
            if( pred_mode < 0 ) return -1;
            sl->chroma_pred_mode = pred_mode;
        } else {
            sl->chroma_pred_mode = DC_128_PRED8x8;
        }
    }

六 如果是4个8x8的帧间块

else if( partition_count == 4 ) { //4个8x8的partitions, 
        int i, j, sub_partition_count[4], list, ref[2][4];

        if (sl->slice_type_nos == AV_PICTURE_TYPE_B ) { //如果是B帧
            for( i = 0; i < 4; i++ ) { //解码4个子块的类型
                sl->sub_mb_type[i] = decode_cabac_b_mb_sub_type(sl);
                sub_partition_count[i] = ff_h264_b_sub_mb_type_info[sl->sub_mb_type[i]].partition_count;
                sl->sub_mb_type[i]     = ff_h264_b_sub_mb_type_info[sl->sub_mb_type[i]].type;
            }
            if (IS_DIRECT(sl->sub_mb_type[0] | sl->sub_mb_type[1] |
                          sl->sub_mb_type[2] | sl->sub_mb_type[3])) { //如果其中有一个是direct块
                ff_h264_pred_direct_motion(h, sl, &mb_type);
                sl->ref_cache[0][scan8[4]] =
                sl->ref_cache[1][scan8[4]] =
                sl->ref_cache[0][scan8[12]] =
                sl->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
                    for( i = 0; i < 4; i++ )
                        fill_rectangle(&sl->direct_cache[scan8[4*i]], 2, 2, 8, (sl->sub_mb_type[i] >> 1) & 0xFF, 1);
            }
        } else { //非B slice
            for( i = 0; i < 4; i++ ) {
                sl->sub_mb_type[i] = decode_cabac_p_mb_sub_type(sl); //子块类型
                sub_partition_count[i] = ff_h264_p_sub_mb_type_info[sl->sub_mb_type[i]].partition_count;
                //子块partitions
                sl->sub_mb_type[i]     = ff_h264_p_sub_mb_type_info[sl->sub_mb_type[i]].type;
            }
        }

        for( list = 0; list < sl->list_count; list++ ) {
                for( i = 0; i < 4; i++ ) {
                    if(IS_DIRECT(sl->sub_mb_type[i])) continue;
                    if(IS_DIR(sl->sub_mb_type[i], 0, list)){
                        unsigned rc = sl->ref_count[list] << MB_MBAFF(sl);
                        if (rc > 1) {
                            ref[list][i] = decode_cabac_mb_ref(sl, list, 4 * i); // 返回的是参考帧个数
                            if (ref[list][i] >= rc) {
                                av_log(h->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref[list][i], rc);
                                return -1;
                            }
                        }else
                            ref[list][i] = 0; //没有参考帧,全都是帧内块?
                    } else {
                        ref[list][i] = -1;  //IDR没有参考帧
                    }
                    sl->ref_cache[list][scan8[4 * i] + 1] =
                    sl->ref_cache[list][scan8[4 * i] + 8] = sl->ref_cache[list][scan8[4 * i] + 9] = ref[list][i];
                }
        }

        if(dct8x8_allowed) 正常都是4x4的dct,也有8x8的,更高的profile中
            dct8x8_allowed = get_dct8x8_allowed(h, sl);

七 接下来解析mvd,并且计算得到mv

 //下面解码MV
        for (list = 0; list < sl->list_count; list++) {
            for(i=0; i<4; i++){
                sl->ref_cache[list][scan8[4 * i]] = sl->ref_cache[list][scan8[4 * i] + 1]; //ref delta poc
                if(IS_DIRECT(sl->sub_mb_type[i])){ //如果是Direct块, 没有mvd,只有残差
                    fill_rectangle(sl->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 2);
                    continue;
                }

                if(IS_DIR(sl->sub_mb_type[i], 0, list) && !IS_DIRECT(sl->sub_mb_type[i])){
                    const int sub_mb_type= sl->sub_mb_type[i]; //子块类型
                    const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
                    for(j=0; j<sub_partition_count[i]; j++){
                        int mpx, mpy;
                        int mx, my;
                        const int index= 4*i + block_width*j;
                        int16_t (* mv_cache)[2] = &sl->mv_cache[list][ scan8[index] ];
                        uint8_t (* mvd_cache)[2]= &sl->mvd_cache[list][ scan8[index] ];
                        pred_motion(h, sl, index, block_width, list, sl->ref_cache[list][ scan8[index] ], &mx, &my); //计算出来mvp
                        DECODE_CABAC_MB_MVD(sl, list, index) //cabac解析mvx mvy 
                        ff_tlog(h->avctx, "final mv:%d %d\n", mx, my); //mv也给取出来了,mv = mvp + mvd ,在上面这个宏里面计算的。

                        if(IS_SUB_8X8(sub_mb_type)){
                            mv_cache[ 1 ][0]=
                            mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
                            mv_cache[ 1 ][1]=
                            mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;

                            mvd_cache[ 1 ][0]=
                            mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mpx; //这些解析到的都是mvd
                            mvd_cache[ 1 ][1]=
                            mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= mpy;
                        }else if(IS_SUB_8X4(sub_mb_type)){
                            mv_cache[ 1 ][0]= mx;
                            mv_cache[ 1 ][1]= my;

                            mvd_cache[ 1 ][0]=  mpx;
                            mvd_cache[ 1 ][1]= mpy;
                        }else if(IS_SUB_4X8(sub_mb_type)){
                            mv_cache[ 8 ][0]= mx;
                            mv_cache[ 8 ][1]= my;

                            mvd_cache[ 8 ][0]= mpx;
                            mvd_cache[ 8 ][1]= mpy;
                        }
                        mv_cache[ 0 ][0]= mx;
                        mv_cache[ 0 ][1]= my;

                        mvd_cache[ 0 ][0]= mpx;
                        mvd_cache[ 0 ][1]= mpy;
                    }
                }else{
                    fill_rectangle(sl->mv_cache [list][ scan8[4*i] ], 2, 2, 8, 0, 4); //填充当前块的mv刀缓存中
                    fill_rectangle(sl->mvd_cache[list][ scan8[4*i] ], 2, 2, 8, 0, 2); //填充当前块的mvd刀缓存中
                }
            }
        }

八 如果是direct块,没有mvd,直接mvp就是mv

else if( IS_DIRECT(mb_type) ) { //如果是direct块,B块才有,没有mvd,只有残差。
        ff_h264_pred_direct_motion(h, sl, &mb_type); //直接mvp就是mv
        fill_rectangle(sl->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 2);
        fill_rectangle(sl->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 2);
        dct8x8_allowed &= sps->direct_8x8_inference_flag;

九 其它正常的预测方式 解析运动向量mx和my

else { //其它的情况 
        int list, i;
        if(IS_16X16(mb_type)){ //如果是16x16的块
            for (list = 0; list < sl->list_count; list++) {
                if(IS_DIR(mb_type, 0, list)){
                    int ref;
                    unsigned rc = sl->ref_count[list] << MB_MBAFF(sl);
                    if (rc > 1) {
                        ref= decode_cabac_mb_ref(sl, list, 0);
                        if (ref >= rc) {
                            av_log(h->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, rc);
                            return -1;
                        }
                    }else
                        ref=0;
                    fill_rectangle(&sl->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
                } //解析参考帧列表
            }

            for (list = 0; list < sl->list_count; list++) {
                if(IS_DIR(mb_type, 0, list)){
                    int mx,my,mpx,mpy;
                    pred_motion(h, sl, 0, 4, list, sl->ref_cache[list][ scan8[0] ], &mx, &my); //计算mvp 
                    DECODE_CABAC_MB_MVD(sl, list, 0) //从码流中解析mvd 并且加到mvp上 得到mv
                    ff_tlog(h->avctx, "final mv:%d %d\n", mx, my);

                    fill_rectangle(sl->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack8to16(mpx,mpy), 2); //填充mvd 缓存
                    fill_rectangle(sl->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4); //填充mv缓存。
                }
            }
        }

十 解析参考帧

else { //其它的情况 
        int list, i;
        if(IS_16X16(mb_type)){ //如果是16x16的块
            for (list = 0; list < sl->list_count; list++) {
                if(IS_DIR(mb_type, 0, list)){
                    int ref;
                    unsigned rc = sl->ref_count[list] << MB_MBAFF(sl);
                    if (rc > 1) {
                        ref= decode_cabac_mb_ref(sl, list, 0);
                        if (ref >= rc) {
                            av_log(h->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, rc);
                            return -1;
                        }
                    }else
                        ref=0;
                    fill_rectangle(&sl->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);
                } //解析参考帧列表
            }

            for (list = 0; list < sl->list_count; list++) {
                if(IS_DIR(mb_type, 0, list)){
                    int mx,my,mpx,mpy;
                    pred_motion(h, sl, 0, 4, list, sl->ref_cache[list][ scan8[0] ], &mx, &my); //计算mvp 
                    DECODE_CABAC_MB_MVD(sl, list, 0) //从码流中解析mvd 并且加到mvp上 得到mv
                    ff_tlog(h->avctx, "final mv:%d %d\n", mx, my);

                    fill_rectangle(sl->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack8to16(mpx,mpy), 2); //填充mvd 缓存
                    fill_rectangle(sl->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4); //填充mv缓存。
                }
            }
        }
        else if(IS_16X8(mb_type)){ //如果是16x8的块,做法类似
            for (list = 0; list < sl->list_count; list++) {
                    for(i=0; i<2; i++){ //两个16x8的块 
                        if(IS_DIR(mb_type, i, list)){
                            int ref;
                            unsigned rc = sl->ref_count[list] << MB_MBAFF(sl);
                            if (rc > 1) {
                                ref= decode_cabac_mb_ref(sl, list, 8 * i); //解析得到参考帧
                                if (ref >= rc) {
                                    av_log(h->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, rc);
                                    return -1;
                                }
                            }else
                                ref=0;
                            fill_rectangle(&sl->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1); //填充参考帧缓存
                        }else
                            fill_rectangle(&sl->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);
                    }
            }

            for (list = 0; list < sl->list_count; list++) { //循环解析,mvd,计算mvp 最终得到mv 并存入缓存
                for(i=0; i<2; i++){
                    if(IS_DIR(mb_type, i, list)){
                        int mx,my,mpx,mpy;
                        pred_16x8_motion(h, sl, 8*i, list, sl->ref_cache[list][scan8[0] + 16*i], &mx, &my);
                        DECODE_CABAC_MB_MVD(sl, list, 8*i)
                        ff_tlog(h->avctx, "final mv:%d %d\n", mx, my);

                        fill_rectangle(sl->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack8to16(mpx,mpy), 2);
                        fill_rectangle(sl->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);
                    }else{
                        fill_rectangle(sl->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 2);
                        fill_rectangle(sl->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);
                    }
                }
            }
        }else{
            av_assert2(IS_8X16(mb_type));
            for (list = 0; list < sl->list_count; list++) {
                    for(i=0; i<2; i++){
                        if(IS_DIR(mb_type, i, list)){ //FIXME optimize
                            int ref;
                            unsigned rc = sl->ref_count[list] << MB_MBAFF(sl);
                            if (rc > 1) {
                                ref = decode_cabac_mb_ref(sl, list, 4 * i);
                                if (ref >= rc) {
                                    av_log(h->avctx, AV_LOG_ERROR, "Reference %d >= %d\n", ref, rc);
                                    return -1;
                                }
                            }else
                                ref=0;
                            fill_rectangle(&sl->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);
                        }else
                            fill_rectangle(&sl->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);
                    }
            }
            for (list = 0; list < sl->list_count; list++) {
                for(i=0; i<2; i++){
                    if(IS_DIR(mb_type, i, list)){
                        int mx,my,mpx,mpy;
                        pred_8x16_motion(h, sl, i*4, list, sl->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
                        DECODE_CABAC_MB_MVD(sl, list, 4*i)

                        ff_tlog(h->avctx, "final mv:%d %d\n", mx, my);
                        fill_rectangle(sl->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack8to16(mpx,mpy), 2);
                        fill_rectangle(sl->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);
                    }else{
                        fill_rectangle(sl->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 2);
                        fill_rectangle(sl->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);
                    }
                }
            }
        }
    }

十一 解析cbp code block parttern,如果全是0,就是都没有残差

//下面cabac解析cbp,codec block parttern, 如果全是0,说明没有残差。一个bit代表一个子块

    if( !IS_INTRA16x16( mb_type ) ) {
        cbp  = decode_cabac_mb_cbp_luma(sl);
        if(decode_chroma)
            cbp |= decode_cabac_mb_cbp_chroma(sl) << 4;
    } else {
        if (!decode_chroma && cbp>15) {
            av_log(h->avctx, AV_LOG_ERROR, "gray chroma\n");
            return AVERROR_INVALIDDATA;
        }
    }

    h->cbp_table[mb_xy] = sl->cbp = cbp;

    if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
        mb_type |= MB_TYPE_8x8DCT * get_cabac_noinline(&sl->cabac, &sl->cabac_state[399 + sl->neighbor_transform_size]);
    }

十二 解析qp值和色度qp

if( cbp || IS_INTRA16x16( mb_type ) ) { //cpb不为0,有块有残差,需要有QP解码 code block parttern
        const uint8_t *scan, *scan8x8;
        const uint32_t *qmul;

        // decode_cabac_mb_dqp 解码宏块的qp值
        if(get_cabac_noinline( &sl->cabac, &sl->cabac_state[60 + (sl->last_qscale_diff != 0)])){
            int val = 1;
            int ctx= 2;
            const int max_qp = 51 + 6*(sps->bit_depth_luma-8);

            while( get_cabac_noinline( &sl->cabac, &sl->cabac_state[60 + ctx] ) ) { //val为解析刀的delta qp
                ctx= 3;
                val++;
                if(val > 2*max_qp){ //prevent infinite loop
                    av_log(h->avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\n", sl->mb_x, sl->mb_y);
                    return -1;
                }
            }

            if( val&0x01 )
                val=   (val + 1)>>1 ;
            else
                val= -((val + 1)>>1);
            sl->last_qscale_diff = val; //最终缓存的delta qp存储刀last_qscale_diff
            sl->qscale += val; //当前块的qscale = 之前的qscale + diff值。 就是当前的qp值
            if (((unsigned)sl->qscale) > max_qp){
                if (sl->qscale < 0) sl->qscale += max_qp + 1;
                else                sl->qscale -= max_qp + 1;
            }
            //pps里面有对应亮度qp和色度qp的对应关系,可以通过查表获取
            sl->chroma_qp[0] = get_chroma_qp(h->ps.pps, 0, sl->qscale); 
            sl->chroma_qp[1] = get_chroma_qp(h->ps.pps, 1, sl->qscale);
        }else

十三 扫描dct系数数组

if(IS_INTERLACED(mb_type)){
            scan8x8 = sl->qscale ? h->field_scan8x8 : h->field_scan8x8_q0; //得到系数数组
            scan    = sl->qscale ? h->field_scan : h->field_scan_q0;
        }else{
            scan8x8 = sl->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;
            scan    = sl->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
        }

十四 解码dct系数,反量化,反dct

decode_cabac_luma_residual(h, sl, scan, scan8x8, pixel_shift, mb_type, cbp, 0); //解析cabac 的亮度残差
        if (CHROMA444(h)) {
            decode_cabac_luma_residual(h, sl, scan, scan8x8, pixel_shift, mb_type, cbp, 1);
            decode_cabac_luma_residual(h, sl, scan, scan8x8, pixel_shift, mb_type, cbp, 2);
        } else if (CHROMA422(h)) {
            if( cbp&0x30 ){
                int c;
                for (c = 0; c < 2; c++)
                    decode_cabac_residual_dc_422(h, sl, sl->mb + ((256 + 16*16*c) << pixel_shift), 3,
                                                 CHROMA_DC_BLOCK_INDEX + c,
                                                 ff_h264_chroma422_dc_scan, 8);
            }

            if( cbp&0x20 ) {
                int c, i, i8x8;
                for( c = 0; c < 2; c++ ) {
                    int16_t *mb = sl->mb + (16*(16 + 16*c) << pixel_shift);
                    qmul = h->ps.pps->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][sl->chroma_qp[c]]; //反量化
                    for (i8x8 = 0; i8x8 < 2; i8x8++) {
                        for (i = 0; i < 4; i++) {
                            const int index = 16 + 16 * c + 8*i8x8 + i;
                            decode_cabac_residual_nondc(h, sl, mb, 4, index, scan + 1, qmul, 15);
                            mb += 16<<pixel_shift;
                        }
                    }
                }
            }
static av_always_inline void decode_cabac_luma_residual(const H264Context *h, H264SliceContext *sl,
                                                        const uint8_t *scan, const uint8_t *scan8x8,
                                                        int pixel_shift, int mb_type, int cbp, int p)
{ //cabac解析编码残差4x4, 16个系数,左上角是DC系数,其余的是AC系数
    static const uint8_t ctx_cat[4][3] = {{0,6,10},{1,7,11},{2,8,12},{5,9,13}};
    const uint32_t *qmul;
    int i8x8, i4x4;
    int qscale = p == 0 ? sl->qscale : sl->chroma_qp[p - 1]; //选择亮度还是色度的qp值
    if( IS_INTRA16x16( mb_type ) ) {
        AV_ZERO128(sl->mb_luma_dc[p]+0);
        AV_ZERO128(sl->mb_luma_dc[p]+8);
        AV_ZERO128(sl->mb_luma_dc[p]+16);
        AV_ZERO128(sl->mb_luma_dc[p]+24);
        //p是通道编号,
        decode_cabac_residual_dc(h, sl, sl->mb_luma_dc[p], ctx_cat[0][p], LUMA_DC_BLOCK_INDEX+p, scan, 16); //解析dc系数, 最后一个参数最多16个参数

        if( cbp&15 ) {
            qmul = h->ps.pps->dequant4_coeff[p][qscale]; //反量化步长
            for( i4x4 = 0; i4x4 < 16; i4x4++ ) {
                const int index = 16*p + i4x4; //4x4的AC系数
                decode_cabac_residual_nondc(h, sl, sl->mb + (16*index << pixel_shift), ctx_cat[1][p], index, scan + 1, qmul, 15); //已经使用了一个dc系数的位置了,
                //还能最多解析15个AC系数。
            }
        } else {
            fill_rectangle(&sl->non_zero_count_cache[scan8[16*p]], 4, 4, 8, 0, 1);
        }

h->cur_pic.qscale_table[mb_xy] = sl->qscale; //当前块的qp存到缓存数组中

以上就是cabac解码h264宏块的粗略过程。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值