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

📄 encoder.c

📁 linux下编译已经通过
💻 C
📖 第 1 页 / 共 5 页
字号:
        {            h->stat.frame.i_mb_count_8x8dct[0] ++;            h->stat.frame.i_mb_count_8x8dct[1] += h->mb.b_transform_8x8;        }        if( h->mb.b_variable_qp )            x264_ratecontrol_mb(h, bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac) - mb_spos);        if( h->sh.b_mbaff )        {            if( (i_mb_y&1) && i_mb_x == h->sps->i_mb_width - 1 )                mb_xy++;            else if( i_mb_y&1 )                mb_xy += 1 - h->sps->i_mb_width;            else                mb_xy += h->sps->i_mb_width;        }        else            mb_xy++;    }    if( h->param.b_cabac )    {        /* end of slice */        x264_cabac_encode_terminal( &h->cabac, 1 );    }    else if( i_skip > 0 )    {        bs_write_ue( &h->out.bs, i_skip );  /* last skip run */    }    if( h->param.b_cabac )    {        x264_cabac_encode_flush( &h->cabac );        h->out.bs.p = h->cabac.p;    }    else    {        /* rbsp_slice_trailing_bits */        bs_rbsp_trailing( &h->out.bs );    }    x264_nal_end( h );    /* Compute misc bits */    h->stat.frame.i_misc_bits = bs_pos( &h->out.bs )                              + NALU_OVERHEAD * 8                              - h->stat.frame.i_itex_bits                              - h->stat.frame.i_ptex_bits                              - h->stat.frame.i_hdr_bits;}static void x264_thread_sync_context( x264_t *dst, x264_t *src ){    x264_frame_t **f;    if( dst == src )        return;    // reference counting    for( f = src->frames.reference; *f; f++ )        (*f)->i_reference_count++;    for( f = dst->frames.reference; *f; f++ )        x264_frame_push_unused( src, *f );    src->fdec->i_reference_count++;    x264_frame_push_unused( src, dst->fdec );    // copy everything except the per-thread pointers and the constants.    memcpy( &dst->i_frame, &src->i_frame, offsetof(x264_t, mb.type) - offsetof(x264_t, i_frame) );    memcpy( &dst->mb.i_type, &src->mb.i_type, offsetof(x264_t, rc) - offsetof(x264_t, mb.i_type) );    dst->stat = src->stat;}static void x264_thread_sync_stat( x264_t *dst, x264_t *src ){    if( dst == src )        return;    memcpy( &dst->stat.i_slice_count, &src->stat.i_slice_count, sizeof(dst->stat) - sizeof(dst->stat.frame) );}static int x264_slices_write( x264_t *h ){    int i_frame_size;#if VISUALIZE    if( h->param.b_visualize )        x264_visualize_init( h );#endif    x264_stack_align( x264_slice_write, h );    i_frame_size = h->out.nal[h->out.i_nal-1].i_payload;    x264_fdec_filter_row( h, h->sps->i_mb_height );#if VISUALIZE    if( h->param.b_visualize )    {        x264_visualize_show( h );        x264_visualize_close( h );    }#endif    h->out.i_frame_size = i_frame_size;    return 0;}/**************************************************************************** * x264_encoder_encode: *  XXX: i_poc   : is the poc of the current given picture *       i_frame : is the number of the frame being coded *  ex:  type frame poc *       I      0   2*0 *       P      1   2*3 *       B      2   2*1 *       B      3   2*2 *       P      4   2*6 *       B      5   2*4 *       B      6   2*5 ****************************************************************************/int     x264_encoder_encode( x264_t *h,                             x264_nal_t **pp_nal, int *pi_nal,                             x264_picture_t *pic_in,                             x264_picture_t *pic_out ){    x264_t *thread_current, *thread_prev, *thread_oldest;    int     i_nal_type;    int     i_nal_ref_idc;    int   i_global_qp;    if( h->param.i_threads > 1)    {        int i = ++h->i_thread_phase;        int t = h->param.i_threads;        thread_current = h->thread[ i%t ];        thread_prev    = h->thread[ (i-1)%t ];        thread_oldest  = h->thread[ (i+1)%t ];        x264_thread_sync_context( thread_current, thread_prev );        x264_thread_sync_ratecontrol( thread_current, thread_prev, thread_oldest );        h = thread_current;//      fprintf(stderr, "current: %p  prev: %p  oldest: %p \n", thread_current, thread_prev, thread_oldest);    }    else    {        thread_current =        thread_prev    =        thread_oldest  = h;    }    // ok to call this before encoding any frames, since the initial values of fdec have b_kept_as_ref=0    x264_reference_update( h );    h->fdec->i_lines_completed = -1;    /* no data out */    *pi_nal = 0;    *pp_nal = NULL;    /* ------------------- Setup new frame from picture -------------------- */    TIMER_START( i_mtime_encode_frame );    if( pic_in != NULL )    {        /* 1: Copy the picture to a frame and move it to a buffer */        x264_frame_t *fenc = x264_frame_pop_unused( h );        x264_frame_copy_picture( h, fenc, pic_in );        if( h->param.i_width != 16 * h->sps->i_mb_width ||            h->param.i_height != 16 * h->sps->i_mb_height )            x264_frame_expand_border_mod16( h, fenc );        fenc->i_frame = h->frames.i_input++;        x264_frame_push( h->frames.next, fenc );        if( h->frames.b_have_lowres )            x264_frame_init_lowres( h->param.cpu, fenc );        if( h->frames.i_input <= h->frames.i_delay + 1 - h->param.i_threads )        {            /* Nothing yet to encode */            /* waiting for filling bframe buffer */            pic_out->i_type = X264_TYPE_AUTO;            return 0;        }    }    if( h->frames.current[0] == NULL )    {        int bframes = 0;        /* 2: Select frame types */        if( h->frames.next[0] == NULL )        {            x264_encoder_frame_end( thread_oldest, thread_current, pp_nal, pi_nal, pic_out );            return 0;        }        x264_slicetype_decide( h );        /* 3: move some B-frames and 1 non-B to encode queue */        while( IS_X264_TYPE_B( h->frames.next[bframes]->i_type ) )            bframes++;        x264_frame_push( h->frames.current, x264_frame_shift( &h->frames.next[bframes] ) );        /* FIXME: when max B-frames > 3, BREF may no longer be centered after GOP closing */        if( h->param.b_bframe_pyramid && bframes > 1 )        {            x264_frame_t *mid = x264_frame_shift( &h->frames.next[bframes/2] );            mid->i_type = X264_TYPE_BREF;            x264_frame_push( h->frames.current, mid );            bframes--;        }        while( bframes-- )            x264_frame_push( h->frames.current, x264_frame_shift( h->frames.next ) );    }    TIMER_STOP( i_mtime_encode_frame );    /* ------------------- Get frame to be encoded ------------------------- */    /* 4: get picture to encode */    h->fenc = x264_frame_shift( h->frames.current );    if( h->fenc == NULL )    {        /* Nothing yet to encode (ex: waiting for I/P with B frames) */        /* waiting for filling bframe buffer */        pic_out->i_type = X264_TYPE_AUTO;        return 0;    }do_encode:    if( h->fenc->i_type == X264_TYPE_IDR )    {        h->frames.i_last_idr = h->fenc->i_frame;    }    /* ------------------- Setup frame context ----------------------------- */    /* 5: Init data dependent of frame type */    TIMER_START( i_mtime_encode_frame );    if( h->fenc->i_type == X264_TYPE_IDR )    {        /* reset ref pictures */        x264_reference_reset( h );        i_nal_type    = NAL_SLICE_IDR;        i_nal_ref_idc = NAL_PRIORITY_HIGHEST;        h->sh.i_type = SLICE_TYPE_I;    }    else if( h->fenc->i_type == X264_TYPE_I )    {        i_nal_type    = NAL_SLICE;        i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/        h->sh.i_type = SLICE_TYPE_I;    }    else if( h->fenc->i_type == X264_TYPE_P )    {        i_nal_type    = NAL_SLICE;        i_nal_ref_idc = NAL_PRIORITY_HIGH; /* Not completely true but for now it is (as all I/P are kept as ref)*/        h->sh.i_type = SLICE_TYPE_P;    }    else if( h->fenc->i_type == X264_TYPE_BREF )    {        i_nal_type    = NAL_SLICE;        i_nal_ref_idc = NAL_PRIORITY_HIGH; /* maybe add MMCO to forget it? -> low */        h->sh.i_type = SLICE_TYPE_B;    }    else    /* B frame */    {        i_nal_type    = NAL_SLICE;        i_nal_ref_idc = NAL_PRIORITY_DISPOSABLE;        h->sh.i_type = SLICE_TYPE_B;    }    h->fdec->i_poc =    h->fenc->i_poc = 2 * (h->fenc->i_frame - h->frames.i_last_idr);    h->fdec->i_type = h->fenc->i_type;    h->fdec->i_frame = h->fenc->i_frame;    h->fenc->b_kept_as_ref =    h->fdec->b_kept_as_ref = i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE && h->param.i_keyint_max > 1;    /* ------------------- Init                ----------------------------- */    /* build ref list 0/1 */    x264_reference_build_list( h, h->fdec->i_poc );    /* Init the rate control */    x264_ratecontrol_start( h, h->fenc->i_qpplus1 );    i_global_qp = x264_ratecontrol_qp( h );    pic_out->i_qpplus1 =    h->fdec->i_qpplus1 = i_global_qp + 1;    if( h->sh.i_type == SLICE_TYPE_B )        x264_macroblock_bipred_init( h );    /* ------------------------ Create slice header  ----------------------- */    x264_slice_init( h, i_nal_type, i_global_qp );    if( i_nal_ref_idc != NAL_PRIORITY_DISPOSABLE )        h->i_frame_num++;    /* ---------------------- Write the bitstream -------------------------- */    /* Init bitstream context */    h->out.i_nal = 0;    bs_init( &h->out.bs, h->out.p_bitstream, h->out.i_bitstream );    if(h->param.b_aud){        int pic_type;        if(h->sh.i_type == SLICE_TYPE_I)            pic_type = 0;        else if(h->sh.i_type == SLICE_TYPE_P)            pic_type = 1;        else if(h->sh.i_type == SLICE_TYPE_B)            pic_type = 2;        else            pic_type = 7;        x264_nal_start(h, NAL_AUD, NAL_PRIORITY_DISPOSABLE);        bs_write(&h->out.bs, 3, pic_type);        bs_rbsp_trailing(&h->out.bs);        x264_nal_end(h);    }    h->i_nal_type = i_nal_type;    h->i_nal_ref_idc = i_nal_ref_idc;    /* Write SPS and PPS */    if( i_nal_type == NAL_SLICE_IDR && h->param.b_repeat_headers )    {        if( h->fenc->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 );    }    /* Write frame */    if( h->param.i_threads > 1 )    {        x264_pthread_create( &h->thread_handle, NULL, (void*)x264_slices_write, h );        h->b_thread_active = 1;    }    else        x264_slices_write( h );    /* restore CPU state (before using float again) */    x264_cpu_restore( h->param.cpu );    if( h->sh.i_type == SLICE_TYPE_P && !h->param.rc.b_stat_read         && h->param.i_scenecut_threshold >= 0        && !h->param.b_pre_scenecut )    {        const int *mbs = h->stat.frame.i_mb_count;        int i_mb_i = mbs[I_16x16] + mbs[I_8x8] + mbs[I_4x4];        int i_mb_p = mbs[P_L0] + mbs[P_8x8];        int i_mb_s = mbs[P_SKIP];        int i_mb   = h->sps->i_mb_width * h->sps->i_mb_height;        int64_t i_inter_cost = h->stat.frame.i_inter_cost;        int64_t i_intra_cost = h->stat.frame.i_intra_cost;        float f_bias;        int i_gop_size = h->fenc->i_frame - h->frames.i_last_idr;        float f_thresh_max = h->param.i_scenecut_threshold / 100.0;        /* magic numbers pulled out of thin air */        float f_thresh_min = f_thresh_max * h->param.i_keyint_min                             / ( h->param.i_keyint_max * 4 );        if( h->param.i_keyint_min == h->param.i_keyint_max )

⌨️ 快捷键说明

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