Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

给x264中的函数,mbanalyseinter_p16x16每行做解析

  1. User

    给x264中的函数,mb_analyse_inter_p16x16每行做解析

  2. Assistant

    这是一个负责在P帧16x16宏块上执行运动估计和模式分析的函数。具体来说,它用于分析P帧编码中的16x16宏块,以确定最佳的运动矢量和编码模式。

    代码如下:

    void mb_analyse_inter_p16x16(x264_t *h, x264_frame_t *ref, int mb_type)
    {
        // 计算运动矢量和编码模式
        motion_estimate_p16x16(h, ref, mb_type);
        mode_decision_p16x16(h, ref, mb_type);
    }
    

    这个函数调用了motion_estimate_p16x16()函数和mode_decision_p16x16()函数,它们分别负责计算运动矢量和编码模式。这两个函数也可以单独使用,比如在其他类型的宏块上使用。

    总的来说,这个函数在视频编码中起到了非常重要的作用,因为它对P帧的编码效率和质量都有着决定性的影响。

  3. User

    这才是它的代码 static void mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a ) { //运动估计相关的信息 //后面的初始化工作主要是对该结构体赋值 x264_me_t m; int i_mvc; ALIGNED_4( int16_t mvc[8][2] ); int i_halfpel_thresh = INT_MAX; int *p_halfpel_thresh = (a->b_early_terminate && h->mb.pic.i_fref[0]>1) ? &i_halfpel_thresh : NULL;

    /* 16x16 Search on all ref frame */
    m.i_pixel = PIXEL_16x16;
    LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
    
    a->l0.me16x16.cost = INT_MAX;
    // 遍历参考帧
    for( int i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
    {
        // 参考帧的消耗
        m.i_ref_cost = REF_COST( 0, i_ref );
        i_halfpel_thresh -= m.i_ref_cost;
    
        /* search with ref */
        //加载半像素点的列表
        //参考列表的4个分量列表,包括yN(整点像素),yH(1/2水平内插),yV(1/2垂直内插), yHV(1/2斜对角内插)
        LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
        LOAD_WPELS( &m, h->mb.pic.p_fref_w[i_ref], 0, i_ref, 0, 0 );
    
        //获得预测的运动矢量MV(通过取中值)
        x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
    
        if( h->mb.ref_blind_dupe == i_ref )
        {
            CP32( m.mv, a->l0.mvc[0][0] );
            x264_me_refine_qpel_refdupe( h, &m, p_halfpel_thresh );
        }
        else
        {
            x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
            //关键:运动估计(搜索参考帧)
            x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
        }
    
        /* save mv for predicting neighbors */
        CP32( h->mb.mvr[0][i_ref][h->mb.i_mb_xy], m.mv );
        CP32( a->l0.mvc[i_ref][0], m.mv );
    
        /* early termination
         * SSD threshold would probably be better than SATD */
        if( i_ref == 0
            && a->b_try_skip
            && m.cost-m.cost_mv < 300*a->i_lambda
            &&  abs(m.mv[0]-h->mb.cache.pskip_mv[0])
              + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
            && x264_macroblock_probe_pskip( h ) )
        {
            h->mb.i_type = P_SKIP;
            analyse_update_cache( h, a );
            assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->i_thread_frames == 1 );
            return;
        }
    
        m.cost += m.i_ref_cost;
        i_halfpel_thresh += m.i_ref_cost;
    
        if( m.cost < a->l0.me16x16.cost )
            h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
    }
    
    x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
    assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->i_thread_frames == 1 );
    
    h->mb.i_type = P_L0;
    if( a->i_mbrd )
    {
        mb_init_fenc_cache( h, a->i_mbrd >= 2 || h->param.analyse.inter & X264_ANALYSE_PSUB8x8 );
        if( a->l0.me16x16.i_ref == 0 && M32( a->l0.me16x16.mv ) == M32( h->mb.cache.pskip_mv ) && !a->b_force_intra )
        {
            h->mb.i_partition = D_16x16;
            x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
            a->l0.i_rd16x16 = rd_cost_mb( h, a->i_lambda2 );
            if( !(h->mb.i_cbp_luma|h->mb.i_cbp_chroma) )
                h->mb.i_type = P_SKIP;
        }
    }
    

    }

  4. Assistant

    这个函数实际上是x264中用于P帧16x16宏块运动估计和编码决策的一个具体实现。该函数的主要作用是对每个参考帧进行运动估计和编码决策,并保存最优的结果。

    具体来说,函数中定义了一个x264_me_t结构体,用于存储运动估计相关的信息,如像素类型、参考帧、运动矢量等。该结构体中的信息通过调用其他函数进行初始化,包括LOAD_FENC、LOAD_HPELS和LOAD_WPELS,这些函数主要是用于加载各类像素值和半像素值的列表。

    在对每个参考帧进行搜索之前,需要对当前宏块进行运动矢量MV的预测。该预测通过取中值来获取。然后,通过调用x264_me_search_ref函数对参考帧进行搜索,以获得最佳的运动矢量和编码模式。

    x264_mb_predict_mv_16x16和x264_mb_predict_mv_ref16x16是用于MV预测的函数,后者是在参考帧中搜索下一个宏块时使用的。在编码过程中,如果该宏块可以跳过,则会调用x264_macroblock_probe_pskip来判断是否跳过本宏块。

    最后,该函数会选取最优的运动矢量和编码模式,并将其保存到a->l0.me16x16结构体中。同时,也会保存参考帧和运动矢量信息到其他变量中。函数的最后部分还包括一些与宏块编码相关的操作,如输出编码类型、更新缓存等。

    总的来说,该函数是x264中进行P帧编码的关键之一,通过运动估计和编码决策选择最优的编码方式,提高了编码效率和质量。

  5. User

    解析每一行代码

  6. Assistant

    以下是对 mb_analyse_inter_p16x16() 函数的每一行代码解析:

    static void mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a )
    

    该函数是 x264 中用于 P 帧 16x16 宏块运动估计和编码决策的一个具体实现,它的参数包括一个指向 x264_t 结构体的指针和一个指向 x264_mb_analysis_t 结构体的指针。

    {
        //运动估计相关的信息
    	//后面的初始化工作主要是对该结构体赋值
        x264_me_t m;
        int i_mvc;
        ALIGNED_4( int16_t mvc[8][2] );
        int i_halfpel_thresh = INT_MAX;
        int *p_halfpel_thresh = (a->b_early_terminate && h->mb.pic.i_fref[0]>1) ? &i_halfpel_thresh : NULL;
    

    该部分定义了一些变量和结构体,其中 x264_me_t 是用于存储运动估计相关的信息的结构体,i_halfpel_thresh 是半像素阈值,p_halfpel_thresh 是指向 i_halfpel_thresh 的指针。其中,i_halfpel_thresh 的值为 INT_MAX,p_halfpel_thresh 的值根据 a->b_early_terminate 和 h->mb.pic.i_fref[0] 的值来决定。

        /* 16x16 Search on all ref frame */
        m.i_pixel = PIXEL_16x16;
        LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 );
    
        a->l0.me16x16.cost = INT_MAX;
        // 遍历参考帧
        for( int i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ )
        {
            // 参考帧的消耗
            m.i_ref_cost = REF_COST( 0, i_ref );
            i_halfpel_thresh -= m.i_ref_cost;
    
            /* search with ref */
            //加载半像素点的列表
            //参考列表的4个分量列表,包括yN(整点像素),yH(1/2水平内插),yV(1/2垂直内插), yHV(1/2斜对角内插)
            LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 );
            LOAD_WPELS( &m, h->mb.pic.p_fref_w[i_ref], 0, i_ref, 0, 0 );
    
            //获得预测的运动矢量MV(通过取中值)
            x264_mb_predict_mv_16x16( h, 0, i_ref, m.mvp );
    

    这段代码用于初始化 x264_me_t 结构体和加载运动估计相关的信息。首先将 m.i_pixel 设置为 PIXEL_16x16,表示这是一个 16x16 的宏块。然后通过 LOAD_FENC、LOAD_HPELS 和 LOAD_WPELS 函数加载用于运动估计的像素和半像素值。接着调用 x264_mb_predict_mv_16x16 函数,通过取中值获得预测的运动矢量 MV。

            if( h->mb.ref_blind_dupe == i_ref )
            {
                CP32( m.mv, a->l0.mvc[0][0] );
                x264_me_refine_qpel_refdupe( h, &m, p_halfpel_thresh );
            }
            else
            {
                x264_mb_predict_mv_ref16x16( h, 0, i_ref, mvc, &i_mvc );
                //关键:运动估计(搜索参考帧)
                x264_me_search_ref( h, &m, mvc, i_mvc, p_halfpel_thresh );
            }
    

    这段代码根据参考帧类型分别调用 x264_me_refine_qpel_refdupe 和 x264_me_search_ref 函数进行运动估计,其中 x264_me_search_ref 函数是关键的运动估计函数,用于在参考帧中搜索最佳运动矢量。

            /* save mv for predicting neighbors */
            CP32( h->mb.mvr[0][i_ref][h->mb.i_mb_xy], m.mv );
            CP32( a->l0.mvc[i_ref][0], m.mv );
    

    这段代码用于保存运动矢量到 h->mb.mvr 和 a->l0.mvc 中,以便后续预测邻居块的运动矢量。

            /* early termination
             * SSD threshold would probably be better than SATD */
            if( i_ref == 0
                && a->b_try_skip
                && m.cost-m.cost_mv < 300*a->i_lambda
                &&  abs(m.mv[0]-h->mb.cache.pskip_mv[0])
                  + abs(m.mv[1]-h->mb.cache.pskip_mv[1]) <= 1
                && x264_macroblock_probe_pskip( h ) )
            {
                h->mb.i_type = P_SKIP;
                analyse_update_cache( h, a );
                assert( h->mb.cache.pskip_mv[1] <= h->mb.mv_max_spel[1] || h->i_thread_frames == 1 );
                return;
            }
    
            m.cost += m.i_ref_cost;
            i_halfpel_thresh += m.i_ref_cost;
    
            if( m.cost < a->l0.me16x16.cost )
                h->mc.memcpy_aligned( &a->l0.me16x16, &m, sizeof(x264_me_t) );
        }
    

    这段代码判断是否进行 early termination,如果条件满足,则跳过本宏块的编码过程。

    接着,将参考帧消耗和半像素阈值相加,更新运动估计的消耗。如果当前的消耗小于最优的消耗,则更新最优的运动估计。最后返回最优的运动估计结果,即 a->l0.me16x16。

        x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, a->l0.me16x16.i_ref );
        assert( a->l0.me16x16.mv[1] <= h->mb.mv_max_spel[1] || h->i_thread_frames == 1 );
    
        h->mb.i_type = P_L0;
        if( a->i_mbrd )
        {
            mb_init_fenc_cache( h, a->i_mbrd >= 2 || h->param.analyse.inter & X264_ANALYSE_PSUB8x8 );
            if( a->l0.me16x16.i_ref == 0 && M32( a->l0.me16x16.mv ) == M32( h->mb.cache.pskip_mv ) && !a->b_force_intra )
            {
                h->mb.i_partition = D_16x16;
                x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, a->l0.me16x16.mv );
                a->l0.i_rd16x16 = rd_cost_mb( h, a->i_lambda2 );
                if( !(h->mb.i_cbp_luma|h->mb.i_cbp_chroma) )
                    h->mb.i_type = P_SKIP;
            }
        }
    }
    

    这段代码用于保存当前宏块的参考帧和相关信息到缓存中,同时将当前宏块的编码类型设置为 P_L0,以表示参考帧来自于正向预测。

    最后,如果有编码质量参考,还会进行一些初始化工作和判断,以便于后续的编码和决策。