]> git.sesse.net Git - x264/commitdiff
Improve threaded frame handling
authorAnton Mitrofanov <BugMaster@narod.ru>
Wed, 23 Sep 2009 19:31:53 +0000 (12:31 -0700)
committerFiona Glaser <fiona@x264.com>
Wed, 23 Sep 2009 19:31:53 +0000 (12:31 -0700)
Avoid unnecessary cond_wait

common/common.h
common/frame.c
common/frame.h
encoder/encoder.c
encoder/lookahead.c

index a643078ef6b2bb62a0788f9ad6fddcdce05c8494..0498139b31f57123a01d55569c984542c63bd4d9 100644 (file)
@@ -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;
index 001c4fd9b7e20402cb74c0c7a6b316738e50ba64..707a99ca8889aae0175e82a368dac93d1239eecd 100644 (file)
@@ -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;
-}
index f6faa12b003dd89635dfdcdf1192fcec96b345a1..81cfe3c9b02da027e85470ed2e25984dd8c3fb7e 100644 (file)
@@ -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)
index 6bf59222c050031b5ba4cc9c0b36d4bf8438afad..78d64b3aa273e3459e094b781bea2266888453aa 100644 (file)
@@ -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;
 }
index 9c8dd17e1873cc24177ae46fbe767d32bafde501..968d0fc13746a6caa19cb30d2e55d4c344d3e35f 100644 (file)
@@ -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