[x264-devel] commit: Improve threaded frame handling (Anton Mitrofanov )

git version control git at videolan.org
Thu Sep 24 17:46:40 CEST 2009


x264 | branch: master | Anton Mitrofanov <BugMaster at narod.ru> | Wed Sep 23 12:31:53 2009 -0700| [e8b9dfe479a6a945d9cd91b4aae8a3cba06b9db9] | committer: Jason Garrett-Glaser 

Improve threaded frame handling
Avoid unnecessary cond_wait

> http://git.videolan.org/gitweb.cgi/x264.git/?a=commit;h=e8b9dfe479a6a945d9cd91b4aae8a3cba06b9db9
---

 common/common.h     |    2 +-
 common/frame.c      |    9 ---------
 common/frame.h      |    1 -
 encoder/encoder.c   |   10 +++++++---
 encoder/lookahead.c |   22 ++++++++++++++--------
 5 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/common/common.h b/common/common.h
index a643078..0498139 100644
--- a/common/common.h
+++ b/common/common.h
@@ -245,8 +245,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 1b0cd03..872436c 100644
--- a/encoder/encoder.c
+++ b/encoder/encoder.c
@@ -2369,8 +2369,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 9c8dd17..968d0fc 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:
@@ -190,8 +193,13 @@ void x264_lookahead_put_frame( x264_t *h, x264_frame_t *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 +209,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



More information about the x264-devel mailing list