📄 encoder.c
字号:
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->i_thread_num = i;
}
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;
}
x264_ratecontrol_threads_start( h );
/* dispatch */
#ifdef HAVE_PTHREAD
{
pthread_t handles[X264_SLICE_MAX];
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], NULL );
}
#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 );
/* ------------------------ Create slice header ----------------------- */
x264_slice_init( h, i_nal_type, i_slice_type, i_global_qp );
if( h->fenc->b_kept_as_ref )
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(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;
/* 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 */
i_frame_size = x264_slices_write( h );
/* restore CPU state (before using float again) */
x264_cpu_restore( h->param.cpu );
if( i_slice_type == SLICE_TYPE_P && !h->param.rc.b_stat_read
&& h->param.i_scenecut_threshold >= 0 )
{
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 )
f_thresh_min= f_thresh_max;
/* macroblock_analyse() doesn't further analyse skipped mbs,
* so we have to guess their cost */
if( i_mb_s < i_mb )
i_intra_cost = i_intra_cost * i_mb / (i_mb - i_mb_s);
if( i_gop_size < h->param.i_keyint_min / 4 )
f_bias = f_thresh_min / 4;
else if( i_gop_size <= h->param.i_keyint_min )
f_bias = f_thresh_min * i_gop_size / h->param.i_keyint_min;
else
{
f_bias = f_thresh_min
+ ( f_thresh_max - f_thresh_min )
* ( i_gop_size - h->param.i_keyint_min )
/ ( h->param.i_keyint_max - h->param.i_keyint_min );
}
f_bias = X264_MIN( f_bias, 1.0 );
/* Bad P will be reencoded as I */
if( i_mb_s < i_mb &&
i_inter_cost >= (1.0 - f_bias) * i_intra_cost )
{
int b;
x264_log( h, X264_LOG_DEBUG, "scene cut at %d Icost:%.0f Pcost:%.0f ratio:%.3f bias=%.3f lastIDR:%d (I:%d P:%d S:%d)\n",
h->fenc->i_frame,
(double)i_intra_cost, (double)i_inter_cost,
(double)i_inter_cost / i_intra_cost,
f_bias, i_gop_size,
i_mb_i, i_mb_p, i_mb_s );
/* Restore frame num */
h->i_frame_num--;
for( b = 0; h->frames.current[b] && IS_X264_TYPE_B( h->frames.current[b]->i_type ); b++ );
if( b > 0 )
{
/* If using B-frames, force GOP to be closed.
* Even if this frame is going to be I and not IDR, forcing a
* P-frame before the scenecut will probably help compression.
*
* We don't yet know exactly which frame is the scene cut, so
* we can't assign an I-frame. Instead, change the previous
* B-frame to P, and rearrange coding order. */
if( h->param.b_bframe_adaptive || b > 1 )
h->fenc->i_type = X264_TYPE_AUTO;
x264_frame_sort_pts( h->frames.current );
x264_frame_push( h->frames.next, h->fenc );
h->fenc = h->frames.current[b-1];
h->frames.current[b-1] = NULL;
h->fenc->i_type = X264_TYPE_P;
x264_frame_sort_dts( h->frames.current );
}
/* Do IDR if needed */
else if( i_gop_size >= h->param.i_keyint_min )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -