diff --git a/common/common.h b/common/common.h index 81c7b00..0f51b78 100644 --- a/common/common.h +++ b/common/common.h @@ -241,8 +241,8 @@ typedef struct typedef struct x264_lookahead_t { + volatile uint8_t b_exit_thread; uint8_t b_thread_active; - uint8_t b_exit_thread; uint8_t b_analyse_keyframe; int i_last_idr; int i_slicetype_length; diff --git a/common/frame.c b/common/frame.c index be60dd8..8f42cde 100644 --- a/common/frame.c +++ b/common/frame.c @@ -1055,12 +1055,3 @@ void x264_synch_frame_list_push( x264_synch_frame_list_t *slist, x264_frame_t *f x264_pthread_mutex_unlock( &slist->mutex ); x264_pthread_cond_broadcast( &slist->cv_fill ); } - -int x264_synch_frame_list_get_size( x264_synch_frame_list_t *slist ) -{ - int size; - x264_pthread_mutex_lock( &slist->mutex ); - size = slist->i_size; - x264_pthread_mutex_unlock( &slist->mutex ); - return size; -} diff --git a/common/frame.h b/common/frame.h index f6faa12..81cfe3c 100644 --- a/common/frame.h +++ b/common/frame.h @@ -165,7 +165,6 @@ void x264_frame_delete_list( x264_frame_t **list ); int x264_synch_frame_list_init( x264_synch_frame_list_t *slist, int nelem ); void x264_synch_frame_list_delete( x264_synch_frame_list_t *slist ); void x264_synch_frame_list_push( x264_synch_frame_list_t *slist, x264_frame_t *frame ); -int x264_synch_frame_list_get_size( x264_synch_frame_list_t *slist ); #define x264_frame_sort_dts(list) x264_frame_sort(list, 1) #define x264_frame_sort_pts(list) x264_frame_sort(list, 0) diff --git a/encoder/encoder.c b/encoder/encoder.c index 56e5d5b..56e15bc 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -2276,8 +2276,12 @@ int x264_encoder_delayed_frames( x264_t *h ) h = h->thread[ h->i_thread_phase % h->param.i_threads ]; for( i=0; h->frames.current[i]; i++ ) delayed_frames++; - delayed_frames += x264_synch_frame_list_get_size( &h->lookahead->ifbuf ); - delayed_frames += x264_synch_frame_list_get_size( &h->lookahead->next ); - delayed_frames += x264_synch_frame_list_get_size( &h->lookahead->ofbuf ); + x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex ); + x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex ); + x264_pthread_mutex_lock( &h->lookahead->next.mutex ); + delayed_frames += h->lookahead->ifbuf.i_size + h->lookahead->next.i_size + h->lookahead->ofbuf.i_size; + x264_pthread_mutex_unlock( &h->lookahead->next.mutex ); + x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex ); + x264_pthread_mutex_unlock( &h->lookahead->ofbuf.mutex ); return delayed_frames; } diff --git a/encoder/lookahead.c b/encoder/lookahead.c index 35ed729..c984869 100644 --- a/encoder/lookahead.c +++ b/encoder/lookahead.c @@ -43,7 +43,7 @@ static void x264_lookahead_shift( x264_synch_frame_list_t *dst, x264_synch_frame int i = count; while( i-- ) { - assert( dst->i_size != dst->i_max_size ); + assert( dst->i_size < dst->i_max_size ); assert( src->i_size ); dst->list[ dst->i_size++ ] = x264_frame_shift( src->list ); src->i_size--; @@ -95,7 +95,6 @@ static void x264_lookahead_thread( x264_t *h ) if( h->param.cpu&X264_CPU_SSE_MISALIGN ) x264_cpu_mask_misalign_sse(); #endif - h->lookahead->b_thread_active = 1; while( !h->lookahead->b_exit_thread ) { x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex ); @@ -115,14 +114,17 @@ static void x264_lookahead_thread( x264_t *h ) x264_lookahead_slicetype_decide( h ); } } /* end of input frames */ - x264_pthread_mutex_lock( &h->lookahead->next.mutex ); x264_pthread_mutex_lock( &h->lookahead->ifbuf.mutex ); + x264_pthread_mutex_lock( &h->lookahead->next.mutex ); x264_lookahead_shift( &h->lookahead->next, &h->lookahead->ifbuf, h->lookahead->ifbuf.i_size ); - x264_pthread_mutex_unlock( &h->lookahead->ifbuf.mutex ); 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 ); + 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 ); } #endif @@ -155,6 +157,7 @@ int x264_lookahead_init( x264_t *h, int i_slicetype_length ) if( x264_pthread_create( &look_h->thread_handle, NULL, (void *)x264_lookahead_thread, look_h ) ) goto fail; + look->b_thread_active = 1; return 0; fail: @@ -185,13 +188,21 @@ void x264_lookahead_put_frame( x264_t *h, x264_frame_t *frame ) if( h->param.i_sync_lookahead ) x264_synch_frame_list_push( &h->lookahead->ifbuf, frame ); else + { + assert( h->lookahead->next.i_size < h->lookahead->next.i_max_size ); x264_synch_frame_list_push( &h->lookahead->next, frame ); + } } int x264_lookahead_is_empty( x264_t *h ) { - return !x264_synch_frame_list_get_size( &h->lookahead->ofbuf ) && - !x264_synch_frame_list_get_size( &h->lookahead->next ); + int b_empty; + x264_pthread_mutex_lock( &h->lookahead->ofbuf.mutex ); + x264_pthread_mutex_lock( &h->lookahead->next.mutex ); + 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 ) @@ -201,8 +212,6 @@ static void x264_lookahead_encoder_shift( x264_t *h ) while( h->lookahead->ofbuf.list[i_frames] ) { - while( h->lookahead->b_thread_active && !h->lookahead->ofbuf.i_size ) - x264_pthread_cond_wait( &h->lookahead->ofbuf.cv_fill, &h->lookahead->ofbuf.mutex ); if( IS_X264_TYPE_B( h->lookahead->ofbuf.list[bframes]->i_type ) ) bframes++; else