x264动态编码参数

介绍

  1. x264下载网址:https://code.videolan.org/videolan/x264.git
  2. 在视频编码过程中,有时候需要动态调整编码参数,来实现想要的结果,有些参数在编码器创建之后是无法调整的,但有些编码参数可以在编码器创建之后实时动态的调整;
  3. x264内部对动态调整进行了实现,主要涉及到的方法是:x264_encoder_reconfig、x264_encoder_reconfig_apply、encoder_try_reconfig。

逻辑图

实现逻辑也相对比较简单,大致如下图:
在这里插入图片描述

相关源码

x264_encoder_reconfig

/****************************************************************************
 1. x264_encoder_reconfig:
 ****************************************************************************/
int x264_encoder_reconfig( x264_t *h, x264_param_t *param )
{
    
    
    h = h->thread[h->thread[0]->i_thread_phase];
    x264_param_t param_save = h->reconfig_h->param;
    h->reconfig_h->param = h->param;

    int rc_reconfig;
    int ret = encoder_try_reconfig( h->reconfig_h, param, &rc_reconfig );
    if( !ret )
        h->reconfig = 1;
    else
        h->reconfig_h->param = param_save;

    return ret;
}

x264_encoder_reconfig_apply

int x264_encoder_reconfig_apply( x264_t *h, x264_param_t *param )
{
    
    
    int rc_reconfig;
    int ret = encoder_try_reconfig( h, param, &rc_reconfig );

    mbcmp_init( h );
    if( !ret )
        x264_sps_init_reconfigurable( h->sps, &h->param );

    /* Supported reconfiguration options (1-pass only):
     * vbv-maxrate
     * vbv-bufsize
     * crf
     * bitrate (CBR only) */
    if( !ret && rc_reconfig )
        x264_ratecontrol_init_reconfigurable( h, 0 );

    return ret;
}

encoder_try_reconfig

/****************************************************************************/
static int encoder_try_reconfig( x264_t *h, x264_param_t *param, int *rc_reconfig )
{
    
    
    *rc_reconfig = 0;
    set_aspect_ratio( h, param, 0 );
#define COPY(var) h->param.var = param->var
    COPY( i_frame_reference ); // but never uses more refs than initially specified
    COPY( i_bframe_bias );
    if( h->param.i_scenecut_threshold )
        COPY( i_scenecut_threshold ); // can't turn it on or off, only vary the threshold
    COPY( b_deblocking_filter );
    COPY( i_deblocking_filter_alphac0 );
    COPY( i_deblocking_filter_beta );
    COPY( i_frame_packing );
    COPY( analyse.inter );
    COPY( analyse.intra );
    COPY( analyse.i_direct_mv_pred );
    /* Scratch buffer prevents me_range from being increased for esa/tesa */
    if( h->param.analyse.i_me_method < X264_ME_ESA || param->analyse.i_me_range < h->param.analyse.i_me_range )
        COPY( analyse.i_me_range );
    COPY( analyse.i_noise_reduction );
    /* We can't switch out of subme=0 during encoding. */
    if( h->param.analyse.i_subpel_refine )
        COPY( analyse.i_subpel_refine );
    COPY( analyse.i_trellis );
    COPY( analyse.b_chroma_me );
    COPY( analyse.b_dct_decimate );
    COPY( analyse.b_fast_pskip );
    COPY( analyse.b_mixed_references );
    COPY( analyse.f_psy_rd );
    COPY( analyse.f_psy_trellis );
    COPY( crop_rect );
    // can only twiddle these if they were enabled to begin with:
    if( h->param.analyse.i_me_method >= X264_ME_ESA || param->analyse.i_me_method < X264_ME_ESA )
        COPY( analyse.i_me_method );
    if( h->param.analyse.i_me_method >= X264_ME_ESA && !h->frames.b_have_sub8x8_esa )
        h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8;
    if( h->pps->b_transform_8x8_mode )
        COPY( analyse.b_transform_8x8 );
    if( h->frames.i_max_ref1 > 1 )
        COPY( i_bframe_pyramid );
    COPY( i_slice_max_size );
    COPY( i_slice_max_mbs );
    COPY( i_slice_min_mbs );
    COPY( i_slice_count );
    COPY( i_slice_count_max );
    COPY( b_tff );

    /* VBV can't be turned on if it wasn't on to begin with */
    if( h->param.rc.i_vbv_max_bitrate > 0 && h->param.rc.i_vbv_buffer_size > 0 &&
          param->rc.i_vbv_max_bitrate > 0 &&   param->rc.i_vbv_buffer_size > 0 )
    {
    
    
        *rc_reconfig |= h->param.rc.i_vbv_max_bitrate != param->rc.i_vbv_max_bitrate;
        *rc_reconfig |= h->param.rc.i_vbv_buffer_size != param->rc.i_vbv_buffer_size;
        *rc_reconfig |= h->param.rc.i_bitrate != param->rc.i_bitrate;
        COPY( rc.i_vbv_max_bitrate );
        COPY( rc.i_vbv_buffer_size );
        COPY( rc.i_bitrate );
    }
    *rc_reconfig |= h->param.rc.f_rf_constant != param->rc.f_rf_constant;
    *rc_reconfig |= h->param.rc.f_rf_constant_max != param->rc.f_rf_constant_max;
    COPY( rc.f_rf_constant );
    COPY( rc.f_rf_constant_max );
#undef COPY

    return validate_parameters( h, 0 );
}

参考

https://www.videolan.org/developers/x264.html

猜你喜欢

转载自blog.csdn.net/yanceyxin/article/details/127634315
今日推荐