1. x264_lookahead_t
typedef struct x264_lookahead_t
{volatile uint8_t b_exit_thread; // notify thread exit
uint8_t b_thread_active; // thread in active status
uint8_t b_analyse_keyframe; // analyse key frame
int i_last_keyframe; // last keyframe
int i_slicetype_length;
x264_frame_t *last_nonb; // last non-bframe
x264_pthread_t thread_handle; // thread handle
x264_sync_frame_list_t ifbuf; // buffer list
x264_sync_frame_list_t next;
x264_sync_frame_list_t ofbuf;
} x264_lookahead_t;
2. x264_lookahead_init
int x264_lookahead_init( x264_t *h, int i_slicetype_length )
{
x264_lookahead_t *look;
// malloc lookahead and zero it
CHECKED_MALLOCZERO( look, sizeof(x264_lookahead_t) );
for( int i = 0; i < h->param.i_threads; i++ ) // set x264_t's lookahead for each thread
h->thread[i]->lookahead = look;
look->i_last_keyframe = - h->param.i_keyint_max;
look->b_analyse_keyframe = (h->param.rc.b_mb_tree || (h->param.rc.i_vbv_buffer_size && h->param.rc.i_lookahead))
&& !h->param.rc.b_stat_read;
look->i_slicetype_length = i_slicetype_length;
/* init frame lists */
if( x264_sync_frame_list_init( &look->ifbuf, h->param.i_sync_lookahead+3 ) ||
x264_sync_frame_list_init( &look->next, h->frames.i_delay+3 ) ||
x264_sync_frame_list_init( &look->ofbuf, h->frames.i_delay+3 ) )
goto fail;
if( !h->param.i_sync_lookahead )
return 0;
x264_t *look_h = h->thread[h->param.i_threads];
*look_h = *h;
if( x264_macroblock_cache_allocate( look_h ) )
goto fail;
if( x264_macroblock_thread_allocate( look_h, 1 ) < 0 )
goto fail;
// create lookahead_thread
if( x264_pthread_create( &look->thread_handle, NULL, (void*)x264_lookahead_thread, look_h ) )
goto fail;
look->b_thread_active = 1;
return 0;
fail:
x264_free( look );
return -1;
}
3. x264_lookahead_shift
// shift frame from src to dst
static void x264_lookahead_shift( x264_sync_frame_list_t *dst, x264_sync_frame_list_t *src, int count )
{
int i = count;
while( i-- )
{
assert( dst->i_size < dst->i_max_size );
assert( src->i_size );
dst->list[ dst->i_size++ ] = x264_frame_shift( src->list ); // get first from src->list, and move others forward
src->i_size--;
}
if( count )
{
x264_pthread_cond_broadcast( &dst->cv_fill );
x264_pthread_cond_broadcast( &src->cv_empty );
}
}
4. x264_lookahead_update_last_nonb
// subtract 1 from h->lookahead->last_nonb->i_reference_count
// and if it == 0, insert h->lookahead->last_nonb into h->frames.unused[frame->b_fdec]
static void x264_lookahead_update_last_nonb( x264_t *h, x264_frame_t *new_nonb )
{
if( h->lookahead->last_nonb )
x264_frame_push_unused( h, h->lookahead->last_nonb );
h->lookahead->last_nonb = new_nonb;
new_nonb->i_reference_count++;
}
5. x264_frame_pop_unused
// pop unused from h->frames.unused[b_fdec] trail, and update its trail.
x264_frame_t *x264_frame_pop_unused( x264_t *h, int b_fdec )
{
x264_frame_t *frame;
if( h->frames.unused[b_fdec][0] )
frame = x264_frame_pop( h->frames.unused[b_fdec] );
else
frame = x264_frame_new( h, b_fdec );
if( !frame )
return NULL;
frame->b_last_minigop_bframe = 0;
frame->i_reference_count = 1;
frame->b_intra_calculated = 0;
frame->b_scenecut = 1;
frame->b_keyframe = 0;
frame->b_corrupt = 0;
frame->i_slice_count = h->param.b_sliced_threads ? h->param.i_threads : 1;
memset( frame->weight, 0, sizeof(frame->weight) );
memset( frame->f_weighted_cost_delta, 0, sizeof(frame->f_weighted_cost_delta) );
return frame;
}
6. x264_lookahead_slicetype_decide and x264_lookahead_thread
#if HAVE_THREAD
static void x264_lookahead_slicetype_decide( x264_t *h )
{
x264_stack_align( x264_slicetype_decide, h );
x264_lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
int shift_frames = h->lookahead->next.list[0]->i_bframes + 1;
x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
while( h->lookahead->ofbuf.i_size == h->lookahead->ofbuf.i_max_size )
x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_empty, &h->lookahead->ofbuf.mutex );
x264_pthread_mutex_lock( &h->lookahead->next.mutex );
// shift from next to ofbuf
x264_lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, shift_frames );
x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
/* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
x264_stack_align( x264_slicetype_analyse, h, shift_frames );
x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
}
static void *x264_lookahead_thread( x264_t *h )
{
int shift;
#if HAVE_MMX
if( h->param.cpu&X264_CPU_SSE_MISALIGN )
x264_cpu_mask_misalign_sse();
#endif
while( !h->lookahead->b_exit_thread )
{
x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
x264_pthread_mutex_lock( &h->lookahead->next.mutex );
// calc shift number from ifbuf to next
shift = X264_MIN( h->lookahead->next.i_max_size - h->lookahead->next.i_size, h->lookahead->ifbuf.i_size );
// shift items from ifbuf to next
x264_lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, shift );
x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
if( h->lookahead->next.i_size <= h->lookahead->i_slicetype_length + h->param.b_vfr_input )
{
while( !h->lookahead->ifbuf.i_size && !h->lookahead->b_exit_thread )
x264_pthread_cond_wait( &h->lookahead->ifbuf.cv_fill, &h->lookahead->ifbuf.mutex );
x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
}
else
{
x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
x264_lookahead_slicetype_decide( h ); // lookahead_slicetype_decide
}
} /* end of input frames */
x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
x264_pthread_mutex_lock( &h->lookahead->next.mutex );
// shift
x264_lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, h->lookahead->ifbuf.i_size );
x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
while( h->lookahead->next.i_size )
x264_lookahead_slicetype_decide( h ); // decide
x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
h->lookahead->b_thread_active = 0;
x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_fill );
x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
return NULL;
}
#endif
7. x264_lookahead_delete etc
void x264_lookahead_delete( x264_t *h )
{
if( h->param.i_sync_lookahead )
{
x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex );
h->lookahead->b_exit_thread = 1;
x264_pthread_cond_broadcast( &h->lookahead->ifbuf.cv_fill );
x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex );
x264_pthread_join( h->lookahead->thread_handle, NULL );
x264_macroblock_cache_free( h->thread[h->param.i_threads] );
x264_macroblock_thread_free( h->thread[h->param.i_threads], 1 );
x264_free( h->thread[h->param.i_threads] );
}
x264_sync_frame_list_delete( &h->lookahead->ifbuf );
x264_sync_frame_list_delete( &h->lookahead->next );
if( h->lookahead->last_nonb )
x264_frame_push_unused( h, h->lookahead->last_nonb );
x264_sync_frame_list_delete( &h->lookahead->ofbuf );
x264_free( h->lookahead );
}
void x264_lookahead_put_frame( x264_t *h, x264_frame_t *frame )
{
if( h->param.i_sync_lookahead )
x264_sync_frame_list_push( &h->lookahead->ifbuf, frame );
else
x264_sync_frame_list_push( &h->lookahead->next, frame );
}
int x264_lookahead_is_empty( x264_t *h )
{
x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
x264_pthread_mutex_lock( &h->lookahead->next.mutex );
int b_empty = !h->lookahead->next.i_size && !h->lookahead->ofbuf.i_size;
x264_pthread_mutex_unlock( &h->lookahead->next.mutex );
x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
return b_empty;
}
static void x264_lookahead_encoder_shift( x264_t *h )
{
if( !h->lookahead->ofbuf.i_size )
return;
int i_frames = h->lookahead->ofbuf.list[0]->i_bframes + 1;
while( i_frames-- )
{
x264_frame_push( h->frames.current, x264_frame_shift( h->lookahead->ofbuf.list ) );
h->lookahead->ofbuf.i_size--;
}
x264_pthread_cond_broadcast( &h->lookahead->ofbuf.cv_empty );
}
void x264_lookahead_get_frames( x264_t *h )
{
if( h->param.i_sync_lookahead )
{ /* We have a lookahead thread, so get frames from there */
x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex );
while( !h->lookahead->ofbuf.i_size && h->lookahead->b_thread_active )
x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_fill, &h->lookahead->ofbuf.mutex );
x264_lookahead_encoder_shift( h );
x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex );
}
else
{ /* We are not running a lookahead thread, so perform all the slicetype decide on the fly */
if( h->frames.current[0] || !h->lookahead->next.i_size )
return;
x264_stack_align( x264_slicetype_decide, h );
x264_lookahead_update_last_nonb( h, h->lookahead->next.list[0] );
int shift_frames = h->lookahead->next.list[0]->i_bframes + 1;
x264_lookahead_shift( &h->lookahead->ofbuf, &h->lookahead->next, shift_frames );
/* For MB-tree and VBV lookahead, we have to perform propagation analysis on I-frames too. */
if( h->lookahead->b_analyse_keyframe && IS_X264_TYPE_I( h->lookahead->last_nonb->i_type ) )
x264_stack_align( x264_slicetype_analyse, h, shift_frames );
x264_lookahead_encoder_shift( h );
}
}