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

📄 encoder.c.svn-base

📁 现在关于h.264的源码很多
💻 SVN-BASE
📖 第 1 页 / 共 4 页
字号:
    h->fdec->i_frame_num = h->sh.i_frame_num;    if( h->sps->i_poc_type == 0 )    {        h->sh.i_poc_lsb = h->fdec->i_poc & ( (1 << h->sps->i_log2_max_poc_lsb) - 1 );        h->sh.i_delta_poc_bottom = 0;   /* XXX won't work for field */    }    else if( h->sps->i_poc_type == 1 )    {        /* FIXME TODO FIXME */    }    else    {        /* Nothing to do ? */    }    x264_macroblock_slice_init( h );}static int x264_slice_write( x264_t *h ){    int i_skip;    int mb_xy;    int i;    /* init stats */    memset( &h->stat.frame, 0, sizeof(h->stat.frame) );    /* Slice */    x264_nal_start( h, h->i_nal_type, h->i_nal_ref_idc );    /* Slice header */    x264_slice_header_write( &h->out.bs, &h->sh, h->i_nal_ref_idc );    if( h->param.b_cabac )    {        /* alignment needed */        bs_align_1( &h->out.bs );        /* init cabac */        x264_cabac_context_init( &h->cabac, h->sh.i_type, h->sh.i_qp, h->sh.i_cabac_init_idc );        x264_cabac_encode_init ( &h->cabac, &h->out.bs );    }    h->mb.i_last_qp = h->sh.i_qp;    h->mb.i_last_dqp = 0;    for( mb_xy = h->sh.i_first_mb, i_skip = 0; mb_xy < h->sh.i_last_mb; mb_xy++ )    {        const int i_mb_y = mb_xy / h->sps->i_mb_width;        const int i_mb_x = mb_xy % h->sps->i_mb_width;        int mb_spos = bs_pos(&h->out.bs);        /* load cache */        x264_macroblock_cache_load( h, i_mb_x, i_mb_y );        /* analyse parameters         * Slice I: choose I_4x4 or I_16x16 mode         * Slice P: choose between using P mode or intra (4x4 or 16x16)         * */        TIMER_START( i_mtime_analyse );        x264_macroblock_analyse( h );        TIMER_STOP( i_mtime_analyse );        /* encode this macrobock -> be carefull it can change the mb type to P_SKIP if needed */        TIMER_START( i_mtime_encode );        x264_macroblock_encode( h );        TIMER_STOP( i_mtime_encode );        TIMER_START( i_mtime_write );        if( h->param.b_cabac )        {            if( mb_xy > h->sh.i_first_mb )                x264_cabac_encode_terminal( &h->cabac, 0 );            if( IS_SKIP( h->mb.i_type ) )                x264_cabac_mb_skip( h, 1 );            else            {                if( h->sh.i_type != SLICE_TYPE_I )                    x264_cabac_mb_skip( h, 0 );                x264_macroblock_write_cabac( h, &h->cabac );            }        }        else        {            if( IS_SKIP( h->mb.i_type ) )                i_skip++;            else            {                if( h->sh.i_type != SLICE_TYPE_I )                {                    bs_write_ue( &h->out.bs, i_skip );  /* skip run */                    i_skip = 0;                }                x264_macroblock_write_cavlc( h, &h->out.bs );            }        }        TIMER_STOP( i_mtime_write );#if VISUALIZE        if( h->param.b_visualize )            x264_visualize_mb( h );#endif        /* save cache */        x264_macroblock_cache_save( h );        /* accumulate mb stats */        h->stat.frame.i_mb_count[h->mb.i_type]++;        if( !IS_SKIP(h->mb.i_type) && !IS_INTRA(h->mb.i_type) && !IS_DIRECT(h->mb.i_type) )        {            if( h->mb.i_partition != D_8x8 )                h->stat.frame.i_mb_count_size[ x264_mb_partition_pixel_table[ h->mb.i_partition ] ] += 4;            else                for( i = 0; i < 4; i++ )                    h->stat.frame.i_mb_count_size[ x264_mb_partition_pixel_table[ h->mb.i_sub_partition[i] ] ] ++;            if( h->param.i_frame_reference > 1 )            {                for( i = 0; i < 4; i++ )                {                    int i_ref = h->mb.cache.ref[0][ x264_scan8[4*i] ];                    if( i_ref >= 0 )                        h->stat.frame.i_mb_count_ref[i_ref] ++;                }            }        }        if( h->mb.i_cbp_luma && !IS_INTRA(h->mb.i_type) )        {            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) - mb_spos);    }    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 );    }    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;    return 0;}static inline int x264_slices_write( x264_t *h ){    int i_frame_size;#if VISUALIZE    if( h->param.b_visualize )        x264_visualize_init( h );#endif    if( h->param.i_threads == 1 )    {        x264_slice_write( h );        i_frame_size = h->out.nal[h->out.i_nal-1].i_payload;    }    else    {        int i_nal = h->out.i_nal;        int i_bs_size = h->out.i_bitstream / h->param.i_threads;        int i;        /* duplicate contexts */        for( i = 0; i < h->param.i_threads; i++ )        {            x264_t *t = h->thread[i];            if( i > 0 )            {                memcpy( t, h, sizeof(x264_t) );                t->out.p_bitstream += i*i_bs_size;                bs_init( &t->out.bs, t->out.p_bitstream, i_bs_size );            }            t->sh.i_first_mb = (i    * h->sps->i_mb_height / h->param.i_threads) * h->sps->i_mb_width;            t->sh.i_last_mb = ((i+1) * h->sps->i_mb_height / h->param.i_threads) * h->sps->i_mb_width;            t->out.i_nal = i_nal + i;        }        /* dispatch */#if HAVE_PTHREAD        {            pthread_t handles[X264_SLICE_MAX];            void *status;            for( i = 0; i < h->param.i_threads; i++ )                pthread_create( &handles[i], NULL, (void*)x264_slice_write, (void*)h->thread[i] );            for( i = 0; i < h->param.i_threads; i++ )                pthread_join( handles[i], &status );        }#else        for( i = 0; i < h->param.i_threads; i++ )            x264_slice_write( h->thread[i] );#endif        /* merge contexts */        i_frame_size = h->out.nal[i_nal].i_payload;        for( i = 1; i < h->param.i_threads; i++ )        {            int j;            x264_t *t = h->thread[i];            h->out.nal[i_nal+i] = t->out.nal[i_nal+i];            i_frame_size += t->out.nal[i_nal+i].i_payload;            // all entries in stat.frame are ints            for( j = 0; j < sizeof(h->stat.frame) / sizeof(int); j++ )                ((int*)&h->stat.frame)[j] += ((int*)&t->stat.frame)[j];        }        h->out.i_nal = i_nal + h->param.i_threads;    }#if VISUALIZE    if( h->param.b_visualize )    {        x264_visualize_show( h );        x264_visualize_close( h );    }#endif    return i_frame_size;}/**************************************************************************** * 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_frame_t   *frame_psnr = h->fdec; /* just to keep the current decoded frame for psnr calculation */    int     i_nal_type;    int     i_nal_ref_idc;    int     i_slice_type;    int     i_frame_size;    int i;    int   i_global_qp;    char psz_message[80];    /* 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_get( h->frames.unused );        x264_frame_copy_picture( h, fenc, pic_in );        if( h->param.i_width % 16 || h->param.i_height % 16 )            x264_frame_expand_border_mod16( h, fenc );        fenc->i_frame = h->frames.i_input++;        x264_frame_put( 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 )        {            /* 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 )            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_put( h->frames.current, x264_frame_get( &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_get( &h->frames.next[bframes/2] );            mid->i_type = X264_TYPE_BREF;            x264_frame_put( h->frames.current, mid );            bframes--;        }        while( bframes-- )            x264_frame_put( h->frames.current, x264_frame_get( h->frames.next ) );    }    TIMER_STOP( i_mtime_encode_frame );    /* ------------------- Get frame to be encoded ------------------------- */    /* 4: get picture to encode */    h->fenc = x264_frame_get( 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 dependant 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;        i_slice_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)*/        i_slice_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)*/        i_slice_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 */        i_slice_type = SLICE_TYPE_B;    }    else    /* B frame */    {        i_nal_type    = NAL_SLICE;        i_nal_ref_idc = NAL_PRIORITY_DISPOSABLE;        i_slice_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;    /* ------------------- Init                ----------------------------- */    /* build ref list 0/1 */    x264_reference_build_list( h, h->fdec->i_poc, i_slice_type );    /* Init the rate control */    x264_ratecontrol_start( h, i_slice_type, h->fenc->i_qpplus1 );    i_global_qp = x264_ratecontrol_qp( h );    pic_out->i_qpplus1 =    h->fdec->i_qpplus1 = i_global_qp + 1;    if( i_slice_type == SLICE_TYPE_B )        x264_macroblock_bipred_init( h );    if( h->fenc->b_kept_as_ref )        h->i_frame_num++;    /* ------------------------ Create slice header  ----------------------- */    x264_slice_init( h, i_nal_type, i_slice_type, i_global_qp );    /* ---------------------- 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(i_slice_type == SLICE_TYPE_I)            pic_type = 0;        else if(i_slice_type == SLICE_TYPE_P)            pic_type = 1;        else if(i_slice_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;

⌨️ 快捷键说明

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