OSDN Git Service

Fix PFR issue where there are different number of frames in 1st and 2nd pass.
[handbrake-jp/handbrake-jp-git.git] / libhb / fifo.c
index c756f56..fcb7b83 100644 (file)
 #include <malloc.h>
 #endif
 
+#define FIFO_TIMEOUT 200
+
 /* Fifo */
 struct hb_fifo_s
 {
     hb_lock_t    * lock;
+    hb_cond_t    * cond_full;
+    int            wait_full;
+    hb_cond_t    * cond_empty;
+    int            wait_empty;
     uint32_t       capacity;
+    uint32_t       thresh;
     uint32_t       size;
     uint32_t       buffer_size;
     hb_buffer_t  * first;
@@ -51,7 +58,7 @@ void hb_buffer_pool_init( void )
     int i;
     for ( i = 10; i < 26; ++i )
     {
-        buffers.pool[i] = hb_fifo_init(BUFFER_POOL_MAX_ELEMENTS);
+        buffers.pool[i] = hb_fifo_init(BUFFER_POOL_MAX_ELEMENTS, 1);
         buffers.pool[i]->buffer_size = 1 << i;
     }
     /* requests smaller than 2^10 are satisfied from the 2^10 pool. */
@@ -90,8 +97,8 @@ void hb_buffer_pool_free( void )
         }
     }
 
-    hb_deep_log( 2, "Allocated %lld bytes of buffers on this pass and Freed %lld bytes, "
-           "%lld bytes leaked", buffers.allocated, freed, buffers.allocated - freed);
+    hb_deep_log( 2, "Allocated %"PRId64" bytes of buffers on this pass and Freed %"PRId64" bytes, "
+           "%"PRId64" bytes leaked", buffers.allocated, freed, buffers.allocated - freed);
     buffers.allocated = 0;
     hb_unlock(buffers.lock);
 }
@@ -183,25 +190,36 @@ void hb_buffer_realloc( hb_buffer_t * b, int size )
     }
 }
 
+// Frees the specified buffer list.
 void hb_buffer_close( hb_buffer_t ** _b )
 {
     hb_buffer_t * b = *_b;
-    hb_fifo_t *buffer_pool = size_to_pool( b->alloc );
 
-    if( buffer_pool && b->data && !hb_fifo_is_full( buffer_pool ) )
-    {
-        hb_fifo_push_head( buffer_pool, b );
-        return;
-    }
-    /* either the pool is full or this size doesn't use a pool - free the buf */
-    if( b->data )
+    while( b )
     {
-        free( b->data );
-        hb_lock(buffers.lock);
-        buffers.allocated -= b->alloc;
-        hb_unlock(buffers.lock);
+        hb_buffer_t * next = b->next;
+        hb_fifo_t *buffer_pool = size_to_pool( b->alloc );
+
+        b->next = NULL;
+
+        if( buffer_pool && b->data && !hb_fifo_is_full( buffer_pool ) )
+        {
+            hb_fifo_push_head( buffer_pool, b );
+            b = next;
+            continue;
+        }
+        // either the pool is full or this size doesn't use a pool
+        // free the buf 
+        if( b->data )
+        {
+            free( b->data );
+            hb_lock(buffers.lock);
+            buffers.allocated -= b->alloc;
+            hb_unlock(buffers.lock);
+        }
+        free( b );
+        b = next;
     }
-    free( b );
     *_b = NULL;
 }
 
@@ -214,16 +232,36 @@ void hb_buffer_copy_settings( hb_buffer_t * dst, const hb_buffer_t * src )
     dst->flags     = src->flags;
 }
 
-hb_fifo_t * hb_fifo_init( int capacity )
+hb_fifo_t * hb_fifo_init( int capacity, int thresh )
 {
     hb_fifo_t * f;
-    f           = calloc( sizeof( hb_fifo_t ), 1 );
-    f->lock     = hb_lock_init();
-    f->capacity = capacity;
+    f             = calloc( sizeof( hb_fifo_t ), 1 );
+    f->lock       = hb_lock_init();
+    f->cond_full  = hb_cond_init();
+    f->cond_empty = hb_cond_init();
+    f->capacity   = capacity;
+    f->thresh     = thresh;
     f->buffer_size = 0;
     return f;
 }
 
+int hb_fifo_size_bytes( hb_fifo_t * f )
+{
+    int ret = 0;
+    hb_buffer_t * link;
+
+    hb_lock( f->lock );
+    link = f->first;
+    while ( link )
+    {
+        ret += link->size;
+        link = link->next;
+    }
+    hb_unlock( f->lock );
+
+    return ret;
+}
+
 int hb_fifo_size( hb_fifo_t * f )
 {
     int ret;
@@ -257,6 +295,38 @@ float hb_fifo_percent_full( hb_fifo_t * f )
     return ret;
 }
 
+// Pulls the first packet out of this FIFO, blocking until such a packet is available.
+// Returns NULL if this FIFO has been closed or flushed.
+hb_buffer_t * hb_fifo_get_wait( hb_fifo_t * f )
+{
+    hb_buffer_t * b;
+
+    hb_lock( f->lock );
+    if( f->size < 1 )
+    {
+        f->wait_empty = 1;
+        hb_cond_timedwait( f->cond_empty, f->lock, FIFO_TIMEOUT );
+        if( f->size < 1 )
+        {
+            hb_unlock( f->lock );
+            return NULL;
+        }
+    }
+    b         = f->first;
+    f->first  = b->next;
+    b->next   = NULL;
+    f->size  -= 1;
+    if( f->wait_full && f->size == f->capacity - f->thresh )
+    {
+        f->wait_full = 0;
+        hb_cond_signal( f->cond_full );
+    }
+    hb_unlock( f->lock );
+
+    return b;
+}
+
+// Pulls a packet out of this FIFO, or returns NULL if no packet is available.
 hb_buffer_t * hb_fifo_get( hb_fifo_t * f )
 {
     hb_buffer_t * b;
@@ -271,11 +341,39 @@ hb_buffer_t * hb_fifo_get( hb_fifo_t * f )
     f->first  = b->next;
     b->next   = NULL;
     f->size  -= 1;
+    if( f->wait_full && f->size == f->capacity - f->thresh )
+    {
+        f->wait_full = 0;
+        hb_cond_signal( f->cond_full );
+    }
     hb_unlock( f->lock );
 
     return b;
 }
 
+hb_buffer_t * hb_fifo_see_wait( hb_fifo_t * f )
+{
+    hb_buffer_t * b;
+
+    hb_lock( f->lock );
+    if( f->size < 1 )
+    {
+        f->wait_empty = 1;
+        hb_cond_timedwait( f->cond_empty, f->lock, FIFO_TIMEOUT );
+        if( f->size < 1 )
+        {
+            hb_unlock( f->lock );
+            return NULL;
+        }
+    }
+    b = f->first;
+    hb_unlock( f->lock );
+
+    return b;
+}
+
+// Returns the first packet in the specified FIFO.
+// If the FIFO is empty, returns NULL.
 hb_buffer_t * hb_fifo_see( hb_fifo_t * f )
 {
     hb_buffer_t * b;
@@ -308,6 +406,62 @@ hb_buffer_t * hb_fifo_see2( hb_fifo_t * f )
     return b;
 }
 
+// Waits until the specified FIFO is no longer full or until FIFO_TIMEOUT milliseconds have elapsed.
+// Returns whether the FIFO is non-full upon return.
+int hb_fifo_full_wait( hb_fifo_t * f )
+{
+    int result;
+
+    hb_lock( f->lock );
+    if( f->size >= f->capacity )
+    {
+        f->wait_full = 1;
+        hb_cond_timedwait( f->cond_full, f->lock, FIFO_TIMEOUT );
+    }
+    result = ( f->size < f->capacity );
+    hb_unlock( f->lock );
+    return result;
+}
+
+// Pushes the specified buffer onto the specified FIFO,
+// blocking until the FIFO has space available.
+void hb_fifo_push_wait( hb_fifo_t * f, hb_buffer_t * b )
+{
+    if( !b )
+    {
+        return;
+    }
+
+    hb_lock( f->lock );
+    if( f->size >= f->capacity )
+    {
+        f->wait_full = 1;
+        hb_cond_timedwait( f->cond_full, f->lock, FIFO_TIMEOUT );
+    }
+    if( f->size > 0 )
+    {
+        f->last->next = b;
+    }
+    else
+    {
+        f->first = b;
+    }
+    f->last  = b;
+    f->size += 1;
+    while( f->last->next )
+    {
+        f->size += 1;
+        f->last  = f->last->next;
+    }
+    if( f->wait_empty && f->size >= 1 )
+    {
+        f->wait_empty = 0;
+        hb_cond_signal( f->cond_empty );
+    }
+    hb_unlock( f->lock );
+}
+
+// Appends the specified packet list to the end of the specified FIFO.
 void hb_fifo_push( hb_fifo_t * f, hb_buffer_t * b )
 {
     if( !b )
@@ -331,9 +485,15 @@ void hb_fifo_push( hb_fifo_t * f, hb_buffer_t * b )
         f->size += 1;
         f->last  = f->last->next;
     }
+    if( f->wait_empty && f->size >= 1 )
+    {
+        f->wait_empty = 0;
+        hb_cond_signal( f->cond_empty );
+    }
     hb_unlock( f->lock );
 }
 
+// Prepends the specified packet list to the start of the specified FIFO.
 void hb_fifo_push_head( hb_fifo_t * f, hb_buffer_t * b )
 {
     hb_buffer_t * tmp;
@@ -371,6 +531,29 @@ void hb_fifo_push_head( hb_fifo_t * f, hb_buffer_t * b )
     hb_unlock( f->lock );
 }
 
+// Pushes a list of packets onto the specified FIFO as a single element.
+void hb_fifo_push_list_element( hb_fifo_t *fifo, hb_buffer_t *buffer_list )
+{
+    hb_buffer_t *container = hb_buffer_init( 0 );
+    // XXX: Using an arbitrary hb_buffer_t pointer (other than 'next')
+    //      to carry the list inside a single "container" buffer
+    container->next_subpicture = buffer_list;
+    
+    hb_fifo_push( fifo, container );
+}
+
+// Removes a list of packets from the specified FIFO that were stored as a single element.
+hb_buffer_t *hb_fifo_get_list_element( hb_fifo_t *fifo )
+{
+    hb_buffer_t *container = hb_fifo_get( fifo );
+    // XXX: Using an arbitrary hb_buffer_t pointer (other than 'next')
+    //      to carry the list inside a single "container" buffer
+    hb_buffer_t *buffer_list = container->next_subpicture;
+    hb_buffer_close( &container );
+    
+    return buffer_list;
+}
+
 void hb_fifo_close( hb_fifo_t ** _f )
 {
     hb_fifo_t   * f = *_f;
@@ -383,7 +566,25 @@ void hb_fifo_close( hb_fifo_t ** _f )
     }
 
     hb_lock_close( &f->lock );
+    hb_cond_close( &f->cond_empty );
+    hb_cond_close( &f->cond_full );
     free( f );
 
     *_f = NULL;
 }
+
+void hb_fifo_flush( hb_fifo_t * f )
+{
+    hb_buffer_t * b;
+
+    while( ( b = hb_fifo_get( f ) ) )
+    {
+        hb_buffer_close( &b );
+    }
+    hb_lock( f->lock );
+    hb_cond_signal( f->cond_empty );
+    hb_cond_signal( f->cond_full );
+    hb_unlock( f->lock );
+
+}
+