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

📄 encoder.c.svn-base

📁 现在关于h.264的源码很多
💻 SVN-BASE
📖 第 1 页 / 共 4 页
字号:
}/**************************************************************************** * x264_encoder_open: ****************************************************************************/x264_t *x264_encoder_open   ( x264_param_t *param ){    x264_t *h = x264_malloc( sizeof( x264_t ) );    int i;    memset( h, 0, sizeof( x264_t ) );    /* Create a copy of param */    memcpy( &h->param, param, sizeof( x264_param_t ) );    if( x264_validate_parameters( h ) < 0 )    {        x264_free( h );        return NULL;    }    if( h->param.psz_cqm_file )        if( x264_cqm_parse_file( h, h->param.psz_cqm_file ) < 0 )        {            x264_free( h );            return NULL;        }    if( h->param.rc.psz_stat_out )        h->param.rc.psz_stat_out = strdup( h->param.rc.psz_stat_out );    if( h->param.rc.psz_stat_in )        h->param.rc.psz_stat_in = strdup( h->param.rc.psz_stat_in );    if( h->param.rc.psz_rc_eq )        h->param.rc.psz_rc_eq = strdup( h->param.rc.psz_rc_eq );    /* VUI */    if( h->param.vui.i_sar_width > 0 && h->param.vui.i_sar_height > 0 )    {        int i_w = param->vui.i_sar_width;        int i_h = param->vui.i_sar_height;        int a = i_w, b = i_h;        while( b != 0 )        {            int t = a;            a = b;            b = t % b;        }        i_w /= a;        i_h /= a;        while( i_w > 65535 || i_h > 65535 )        {            i_w /= 2;            i_h /= 2;        }        h->param.vui.i_sar_width = 0;        h->param.vui.i_sar_height = 0;        if( i_w == 0 || i_h == 0 )        {            x264_log( h, X264_LOG_ERROR, "cannot create valid sample aspect ratio\n" );        }        else if( i_w == i_h )        {            x264_log( h, X264_LOG_INFO, "no need for a SAR\n" );        }        else        {            x264_log( h, X264_LOG_INFO, "using SAR=%d/%d\n", i_w, i_h );            h->param.vui.i_sar_width = i_w;            h->param.vui.i_sar_height = i_h;        }    }    /* Init x264_t */    h->out.i_nal = 0;    h->out.i_bitstream = X264_MAX( 1000000, h->param.i_width * h->param.i_height * 1.7        * ( h->param.rc.b_cbr ? pow( 0.5, h->param.rc.i_qp_min )          : pow( 0.5, h->param.rc.i_qp_constant ) * X264_MAX( 1, h->param.rc.f_ip_factor )));    h->out.p_bitstream = x264_malloc( h->out.i_bitstream );    h->i_frame = 0;    h->i_frame_num = 0;    h->i_idr_pic_id = 0;    h->sps = &h->sps_array[0];    x264_sps_init( h->sps, 0, &h->param );    h->pps = &h->pps_array[0];    x264_pps_init( h->pps, 0, &h->param, h->sps);    x264_validate_levels( h );    x264_cqm_init( h );        h->mb.i_mb_count = h->sps->i_mb_width * h->sps->i_mb_height;    /* Init frames. */    h->frames.i_delay = h->param.i_bframe;    h->frames.i_max_ref0 = h->param.i_frame_reference;    h->frames.i_max_ref1 = h->sps->vui.i_num_reorder_frames;    h->frames.i_max_dpb  = h->sps->vui.i_max_dec_frame_buffering + 1;    h->frames.b_have_lowres = !h->param.rc.b_stat_read        && ( h->param.rc.b_cbr || h->param.rc.i_rf_constant || h->param.b_bframe_adaptive );    for( i = 0; i < X264_BFRAME_MAX + 3; i++ )    {        h->frames.current[i] = NULL;        h->frames.next[i]    = NULL;        h->frames.unused[i]  = NULL;    }    for( i = 0; i < 1 + h->frames.i_delay; i++ )    {        h->frames.unused[i] =  x264_frame_new( h );    }    for( i = 0; i < h->frames.i_max_dpb; i++ )    {        h->frames.reference[i] = x264_frame_new( h );    }    h->frames.reference[h->frames.i_max_dpb] = NULL;    h->frames.i_last_idr = - h->param.i_keyint_max;    h->frames.i_input    = 0;    h->frames.last_nonb  = NULL;    h->i_ref0 = 0;    h->i_ref1 = 0;    h->fdec = h->frames.reference[0];    x264_macroblock_cache_init( h );    x264_rdo_init( );    /* init CPU functions */    x264_predict_16x16_init( h->param.cpu, h->predict_16x16 );    x264_predict_8x8c_init( h->param.cpu, h->predict_8x8c );    x264_predict_8x8_init( h->param.cpu, h->predict_8x8 );    x264_predict_4x4_init( h->param.cpu, h->predict_4x4 );    x264_pixel_init( h->param.cpu, &h->pixf );    x264_dct_init( h->param.cpu, &h->dctf );    x264_mc_init( h->param.cpu, &h->mc );    x264_csp_init( h->param.cpu, h->param.i_csp, &h->csp );    x264_quant_init( h, h->param.cpu, &h->quantf );    x264_deblock_init( h->param.cpu, &h->loopf );    memcpy( h->pixf.mbcmp,            ( h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1 ) ? h->pixf.sad : h->pixf.satd,            sizeof(h->pixf.mbcmp) );    /* rate control */    if( x264_ratecontrol_new( h ) < 0 )        return NULL;    x264_log( h, X264_LOG_INFO, "using cpu capabilities %s%s%s%s%s%s\n",             param->cpu&X264_CPU_MMX ? "MMX " : "",             param->cpu&X264_CPU_MMXEXT ? "MMXEXT " : "",             param->cpu&X264_CPU_SSE ? "SSE " : "",             param->cpu&X264_CPU_SSE2 ? "SSE2 " : "",             param->cpu&X264_CPU_3DNOW ? "3DNow! " : "",             param->cpu&X264_CPU_ALTIVEC ? "Altivec " : "" );    h->thread[0] = h;    for( i = 1; i < param->i_threads; i++ )        h->thread[i] = x264_malloc( sizeof(x264_t) );    return h;}/**************************************************************************** * x264_encoder_reconfig: ****************************************************************************/int x264_encoder_reconfig( x264_t *h, x264_param_t *param ){    h->param.i_bframe_bias = param->i_bframe_bias;    h->param.i_deblocking_filter_alphac0 = param->i_deblocking_filter_alphac0;    h->param.i_deblocking_filter_beta    = param->i_deblocking_filter_beta;    h->param.analyse.i_me_method = param->analyse.i_me_method;    h->param.analyse.i_me_range = param->analyse.i_me_range;    h->param.analyse.i_subpel_refine = param->analyse.i_subpel_refine;    h->param.analyse.i_trellis = param->analyse.i_trellis;    h->param.analyse.intra = param->analyse.intra;    h->param.analyse.inter = param->analyse.inter;    if( h->sps->b_direct8x8_inference && h->param.i_bframe        && h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_TEMPORAL )        h->param.analyse.inter &= ~X264_ANALYSE_PSUB8x8;    memcpy( h->pixf.mbcmp,            ( h->mb.b_lossless || h->param.analyse.i_subpel_refine <= 1 ) ? h->pixf.sad : h->pixf.satd,            sizeof(h->pixf.mbcmp) );    return x264_validate_parameters( h );}/* internal usage */static void x264_nal_start( x264_t *h, int i_type, int i_ref_idc ){    x264_nal_t *nal = &h->out.nal[h->out.i_nal];    nal->i_ref_idc = i_ref_idc;    nal->i_type    = i_type;    bs_align_0( &h->out.bs );   /* not needed */    nal->i_payload= 0;    nal->p_payload= &h->out.p_bitstream[bs_pos( &h->out.bs) / 8];}static void x264_nal_end( x264_t *h ){    x264_nal_t *nal = &h->out.nal[h->out.i_nal];    bs_align_0( &h->out.bs );   /* not needed */    nal->i_payload = &h->out.p_bitstream[bs_pos( &h->out.bs)/8] - nal->p_payload;    h->out.i_nal++;}/**************************************************************************** * x264_encoder_headers: ****************************************************************************/int x264_encoder_headers( x264_t *h, x264_nal_t **pp_nal, int *pi_nal ){    /* init bitstream context */    h->out.i_nal = 0;    bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );    /* Put SPS and PPS */    if( h->i_frame == 0 )    {        /* identify ourself */        x264_nal_start( h, NAL_SEI, NAL_PRIORITY_DISPOSABLE );        x264_sei_version_write( h, &h->out.bs );        x264_nal_end( h );        /* generate sequence parameters */        x264_nal_start( h, NAL_SPS, NAL_PRIORITY_HIGHEST );        x264_sps_write( &h->out.bs, h->sps );        x264_nal_end( h );        /* generate picture parameters */        x264_nal_start( h, NAL_PPS, NAL_PRIORITY_HIGHEST );        x264_pps_write( &h->out.bs, h->pps );        x264_nal_end( h );    }    /* now set output*/    *pi_nal = h->out.i_nal;    *pp_nal = &h->out.nal[0];    return 0;}static void x264_frame_put( x264_frame_t *list[X264_BFRAME_MAX], x264_frame_t *frame ){    int i = 0;    while( list[i] ) i++;    list[i] = frame;}static void x264_frame_push( x264_frame_t *list[X264_BFRAME_MAX], x264_frame_t *frame ){    int i = 0;    while( list[i] ) i++;    while( i-- )        list[i+1] = list[i];    list[0] = frame;}static x264_frame_t *x264_frame_get( x264_frame_t *list[X264_BFRAME_MAX+1] ){    x264_frame_t *frame = list[0];    int i;    for( i = 0; list[i]; i++ )        list[i] = list[i+1];    return frame;}static void x264_frame_sort( x264_frame_t *list[X264_BFRAME_MAX+1], int b_dts ){    int i, b_ok;    do {        b_ok = 1;        for( i = 0; list[i+1]; i++ )        {            int dtype = list[i]->i_type - list[i+1]->i_type;            int dtime = list[i]->i_frame - list[i+1]->i_frame;            int swap = b_dts ? dtype > 0 || ( dtype == 0 && dtime > 0 )                             : dtime > 0;            if( swap )            {                XCHG( x264_frame_t*, list[i], list[i+1] );                b_ok = 0;            }        }    } while( !b_ok );}#define x264_frame_sort_dts(list) x264_frame_sort(list, 1)#define x264_frame_sort_pts(list) x264_frame_sort(list, 0)static inline void x264_reference_build_list( x264_t *h, int i_poc, int i_slice_type ){    int i;    int b_ok;    /* build ref list 0/1 */    h->i_ref0 = 0;    h->i_ref1 = 0;    for( i = 1; i < h->frames.i_max_dpb; i++ )    {        if( h->frames.reference[i]->i_poc >= 0 )        {            if( h->frames.reference[i]->i_poc < i_poc )            {                h->fref0[h->i_ref0++] = h->frames.reference[i];            }            else if( h->frames.reference[i]->i_poc > i_poc )            {                h->fref1[h->i_ref1++] = h->frames.reference[i];            }        }    }    /* Order ref0 from higher to lower poc */    do    {        b_ok = 1;        for( i = 0; i < h->i_ref0 - 1; i++ )        {            if( h->fref0[i]->i_poc < h->fref0[i+1]->i_poc )            {                XCHG( x264_frame_t*, h->fref0[i], h->fref0[i+1] );                b_ok = 0;                break;            }        }    } while( !b_ok );    /* Order ref1 from lower to higher poc (bubble sort) for B-frame */    do    {        b_ok = 1;        for( i = 0; i < h->i_ref1 - 1; i++ )        {            if( h->fref1[i]->i_poc > h->fref1[i+1]->i_poc )            {                XCHG( x264_frame_t*, h->fref1[i], h->fref1[i+1] );                b_ok = 0;                break;            }        }    } while( !b_ok );    /* In the standard, a P-frame's ref list is sorted by frame_num.     * We use POC, but check whether explicit reordering is needed */    h->b_ref_reorder[0] =    h->b_ref_reorder[1] = 0;    if( i_slice_type == SLICE_TYPE_P )    {        for( i = 0; i < h->i_ref0 - 1; i++ )            if( h->fref0[i]->i_frame_num < h->fref0[i+1]->i_frame_num )            {                h->b_ref_reorder[0] = 1;                break;            }    }    h->i_ref1 = X264_MIN( h->i_ref1, h->frames.i_max_ref1 );    h->i_ref0 = X264_MIN( h->i_ref0, h->frames.i_max_ref0 );    h->i_ref0 = X264_MIN( h->i_ref0, 16 - h->i_ref1 );}static inline void x264_reference_update( x264_t *h ){    int i;    /* apply deblocking filter to the current decoded picture */    if( !h->sh.i_disable_deblocking_filter_idc )    {        TIMER_START( i_mtime_filter );        x264_frame_deblocking_filter( h, h->sh.i_type );        TIMER_STOP( i_mtime_filter );    }    /* expand border */    x264_frame_expand_border( h->fdec );    /* create filtered images */    x264_frame_filter( h->param.cpu, h->fdec );    /* expand border of filtered images */    x264_frame_expand_border_filtered( h->fdec );    /* move lowres copy of the image to the ref frame */    for( i = 0; i < 4; i++)        XCHG( uint8_t*, h->fdec->lowres[i], h->fenc->lowres[i] );    /* adaptive B decision needs a pointer, since it can't use the ref lists */    if( h->sh.i_type != SLICE_TYPE_B )        h->frames.last_nonb = h->fdec;    /* move frame in the buffer */    h->fdec = h->frames.reference[h->frames.i_max_dpb-1];    for( i = h->frames.i_max_dpb-1; i > 0; i-- )    {        h->frames.reference[i] = h->frames.reference[i-1];    }    h->frames.reference[0] = h->fdec;}static inline void x264_reference_reset( x264_t *h ){    int i;    /* reset ref pictures */    for( i = 1; i < h->frames.i_max_dpb; i++ )    {        h->frames.reference[i]->i_poc = -1;    }    h->frames.reference[0]->i_poc = 0;}static inline void x264_slice_init( x264_t *h, int i_nal_type, int i_slice_type, int i_global_qp ){    /* ------------------------ Create slice header  ----------------------- */    if( i_nal_type == NAL_SLICE_IDR )    {        x264_slice_header_init( h, &h->sh, h->sps, h->pps, i_slice_type, h->i_idr_pic_id, h->i_frame_num - 1, i_global_qp );        /* increment id */        h->i_idr_pic_id = ( h->i_idr_pic_id + 1 ) % 65536;    }    else    {        x264_slice_header_init( h, &h->sh, h->sps, h->pps, i_slice_type, -1, h->i_frame_num - 1, i_global_qp );        /* always set the real higher num of ref frame used */        h->sh.b_num_ref_idx_override = 1;        h->sh.i_num_ref_idx_l0_active = h->i_ref0 <= 0 ? 1 : h->i_ref0;        h->sh.i_num_ref_idx_l1_active = h->i_ref1 <= 0 ? 1 : h->i_ref1;    }

⌨️ 快捷键说明

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