]> git.sesse.net Git - vlc/blobdiff - src/input/decoder.c
Cosmetic.
[vlc] / src / input / decoder.c
index 41f79b0d09a3c324f2a889ab4b15ee83a4c29e14..15428581e211fbef08f74ec2c5476635743b3228 100644 (file)
 #include "audio_output/aout_internal.h"
 #include "stream_output/stream_output.h"
 #include "input_internal.h"
-#include "input_clock.h"
-#include "input_decoder.h"
+#include "clock.h"
+#include "decoder.h"
 
-#include "../video_output/vout_internal.h"
+#include "../video_output/vout_control.h"
 
 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
 static void       DeleteDecoder( decoder_t * );
@@ -77,8 +77,6 @@ static es_format_t null_es_format;
 
 struct decoder_owner_sys_t
 {
-    bool      b_own_thread;
-
     int64_t         i_preroll_end;
 
     input_thread_t  *p_input;
@@ -120,12 +118,14 @@ struct decoder_owner_sys_t
     struct
     {
         mtime_t i_date;
+        int     i_ignore;
     } pause;
 
     /* Buffering */
     bool b_buffering;
     struct
     {
+        bool b_first;
         bool b_full;
         int  i_count;
 
@@ -187,8 +187,14 @@ mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
 
-    if( !p_owner->p_clock )
+    vlc_mutex_lock( &p_owner->lock );
+    if( p_owner->b_buffering || p_owner->b_paused )
+        i_ts = 0;
+    vlc_mutex_unlock( &p_owner->lock );
+
+    if( !p_owner->p_clock || !i_ts )
         return i_ts;
+
     return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
 }
 /* decoder_GetDisplayRate:
@@ -212,12 +218,10 @@ int decoder_GetDisplayRate( decoder_t *p_dec )
 decoder_t *input_DecoderNew( input_thread_t *p_input,
                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
 {
-    decoder_t   *p_dec = NULL;
-    vlc_value_t val;
+    decoder_t *p_dec = NULL;
+    int i_priority;
 
-#ifndef ENABLE_SOUT
-    (void)b_force_decoder;
-#else
+#ifdef ENABLE_SOUT
     /* If we are in sout mode, search for packetizer module */
     if( p_sout )
     {
@@ -256,35 +260,20 @@ decoder_t *input_DecoderNew( input_thread_t *p_input,
 
     p_dec->p_owner->p_clock = p_clock;
 
-    if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
-    {
-        msg_Dbg( p_input, "stream out mode -> no decoder thread" );
-        p_dec->p_owner->b_own_thread = false;
-    }
+    if( fmt->i_cat == AUDIO_ES )
+        i_priority = VLC_THREAD_PRIORITY_AUDIO;
     else
-    {
-        var_Get( p_input, "minimize-threads", &val );
-        p_dec->p_owner->b_own_thread = !val.b_bool;
-    }
+        i_priority = VLC_THREAD_PRIORITY_VIDEO;
 
-    if( p_dec->p_owner->b_own_thread )
+    /* Spawn the decoder thread */
+    if( vlc_thread_create( p_dec, "decoder", DecoderThread,
+                           i_priority, false ) )
     {
-        int i_priority;
-        if( fmt->i_cat == AUDIO_ES )
-            i_priority = VLC_THREAD_PRIORITY_AUDIO;
-        else
-            i_priority = VLC_THREAD_PRIORITY_VIDEO;
-
-        /* Spawn the decoder thread */
-        if( vlc_thread_create( p_dec, "decoder", DecoderThread,
-                               i_priority, false ) )
-        {
-            msg_Err( p_dec, "cannot spawn decoder thread" );
-            module_unneed( p_dec, p_dec->p_module );
-            DeleteDecoder( p_dec );
-            vlc_object_release( p_dec );
-            return NULL;
-        }
+        msg_Err( p_dec, "cannot spawn decoder thread" );
+        module_unneed( p_dec, p_dec->p_module );
+        DeleteDecoder( p_dec );
+        vlc_object_release( p_dec );
+        return NULL;
     }
 
     return p_dec;
@@ -303,33 +292,23 @@ void input_DecoderDelete( decoder_t *p_dec )
 
     vlc_object_kill( p_dec );
 
-    if( p_owner->b_own_thread )
+    /* Make sure we aren't paused anymore */
+    vlc_mutex_lock( &p_owner->lock );
+    if( p_owner->b_paused || p_owner->b_buffering )
     {
-        /* Make sure we aren't paused anymore */
-        vlc_mutex_lock( &p_owner->lock );
-        if( p_owner->b_paused || p_owner->b_buffering )
-        {
-            p_owner->b_paused = false;
-            p_owner->b_buffering = false;
-            vlc_cond_signal( &p_owner->wait );
-        }
-        vlc_mutex_unlock( &p_owner->lock );
-
-        /* Make sure the thread leaves the function */
-        block_FifoWake( p_owner->p_fifo );
+        p_owner->b_paused = false;
+        p_owner->b_buffering = false;
+        vlc_cond_signal( &p_owner->wait );
+    }
+    vlc_mutex_unlock( &p_owner->lock );
 
-        vlc_thread_join( p_dec );
+    /* Make sure the thread leaves the function */
+    block_FifoWake( p_owner->p_fifo );
 
-        /* Don't module_unneed() here because of the dll loader that wants
-         * close() in the same thread than open()/decode() */
-    }
-    else
-    {
-        /* Flush */
-        input_DecoderDecode( p_dec, NULL );
+    vlc_thread_join( p_dec );
 
-        module_unneed( p_dec, p_dec->p_module );
-    }
+    /* Don't module_unneed() here because of the dll loader that wants
+     * close() in the same thread than open()/decode() */
 
     /* */
     if( p_dec->p_owner->cc.b_supported )
@@ -352,56 +331,37 @@ void input_DecoderDelete( decoder_t *p_dec )
  * \param p_dec the decoder object
  * \param p_block the data block
  */
-void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
+void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
 
-    if( p_owner->b_own_thread )
+    if( p_owner->p_input->p->b_out_pace_control )
     {
-        if( p_owner->p_input->p->b_out_pace_control )
+        /* FIXME !!!!! */
+        while( vlc_object_alive( p_dec ) &&
+               block_FifoCount( p_owner->p_fifo ) > 10 )
         {
-            /* FIXME !!!!! */
-            while( vlc_object_alive( p_dec ) && !p_dec->b_error &&
-                   block_FifoCount( p_owner->p_fifo ) > 10 )
-            {
-                msleep( 1000 );
-            }
+            msleep( 1000 );
         }
-        else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
-        {
-            /* FIXME: ideally we would check the time amount of data
-             * in the fifo instead of its size. */
-            msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
-                      "consumed quickly enough), resetting fifo!" );
-            block_FifoEmpty( p_owner->p_fifo );
-        }
-
-        block_FifoPut( p_owner->p_fifo, p_block );
     }
-    else
+    else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
     {
-        if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
-        {
-            if( p_block )
-                block_Release( p_block );
-        }
-        else
-        {
-            DecoderProcess( p_dec, p_block );
-        }
+        /* FIXME: ideally we would check the time amount of data
+         * in the fifo instead of its size. */
+        msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
+                  "consumed quickly enough), resetting fifo!" );
+        block_FifoEmpty( p_owner->p_fifo );
     }
+
+    block_FifoPut( p_owner->p_fifo, p_block );
 }
 
 bool input_DecoderIsEmpty( decoder_t * p_dec )
 {
-    /* FIXME it is buggy if the decoder is buffering FIXME
-     * -> "deadlock" */
-    if( p_dec->p_owner->b_own_thread &&
-        block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
-    {
-        return false;
-    }
-    return true;
+    assert( !p_dec->p_owner->b_buffering );
+
+    /* FIXME that's not really true */
+    return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
 }
 
 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
@@ -496,12 +456,14 @@ void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
     vlc_mutex_lock( &p_owner->lock );
 
     assert( !p_owner->b_paused || !b_paused );
+
     p_owner->b_paused = b_paused;
     p_owner->pause.i_date = i_date;
-    if( p_owner->b_own_thread )
-        vlc_cond_signal( &p_owner->wait );
+    p_owner->pause.i_ignore = 0;
+    vlc_cond_signal( &p_owner->wait );
 
     DecoderOutputChangePause( p_dec, b_paused, i_date );
+
     vlc_mutex_unlock( &p_owner->lock );
 }
 
@@ -522,6 +484,7 @@ void input_DecoderStartBuffering( decoder_t *p_dec )
 
     DecoderFlush( p_dec );
 
+    p_owner->buffer.b_first = true;
     p_owner->buffer.b_full = false;
     p_owner->buffer.i_count = 0;
 
@@ -570,6 +533,29 @@ void input_DecoderWaitBuffering( decoder_t *p_dec )
 
     vlc_mutex_unlock( &p_owner->lock );
 }
+void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    *pi_duration = 0;
+
+    vlc_mutex_lock( &p_owner->lock );
+    if( p_dec->fmt_in.i_cat == VIDEO_ES )
+    {
+        if( p_owner->b_paused && p_owner->p_vout )
+        {
+            vout_NextPicture( p_owner->p_vout, pi_duration );
+            p_owner->pause.i_ignore++;
+            vlc_cond_signal( &p_owner->wait );
+        }
+    }
+    else
+    {
+        /* TODO subtitle should not be flushed */
+        DecoderFlush( p_dec );
+    }
+    vlc_mutex_unlock( &p_owner->lock );
+}
 
 /*****************************************************************************
  * Internal functions
@@ -626,7 +612,6 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
         vlc_object_release( p_dec );
         return NULL;
     }
-    p_dec->p_owner->b_own_thread = true;
     p_dec->p_owner->i_preroll_end = -1;
     p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
     p_dec->p_owner->p_input = p_input;
@@ -719,8 +704,10 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
 
     p_owner->b_paused = false;
     p_owner->pause.i_date = 0;
+    p_owner->pause.i_ignore = 0;
 
     p_owner->b_buffering = false;
+    p_owner->buffer.b_first = true;
     p_owner->buffer.b_full = false;
     p_owner->buffer.i_count = 0;
     p_owner->buffer.p_picture = NULL;
@@ -761,26 +748,25 @@ static void *DecoderThread( vlc_object_t *p_this )
     int canc = vlc_savecancel();
 
     /* The decoder's main loop */
-    while( vlc_object_alive( p_dec ) && !p_dec->b_error )
-    {
-        block_t *p_block = block_FifoGet( p_owner->p_fifo );
-
-        DecoderSignalBuffering( p_dec, p_block == NULL );
-
-        if( p_block && DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
-            break;
-    }
-
     while( vlc_object_alive( p_dec ) )
     {
         block_t *p_block = block_FifoGet( p_owner->p_fifo );
 
         DecoderSignalBuffering( p_dec, p_block == NULL );
 
-        /* Trash all received PES packets */
         if( p_block )
-            block_Release( p_block );
+        {
+            if( p_dec->b_error )
+            {   /* Trash all received PES packets */
+                block_Release( p_block );
+            }
+            else if( DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
+            {
+                break;
+            }
+        }
     }
+
     DecoderSignalBuffering( p_dec, true );
 
     /* We do it here because of the dll loader that wants close() in the
@@ -795,17 +781,14 @@ static void DecoderFlush( decoder_t *p_dec )
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
     block_t *p_null;
 
-    if( p_owner->b_own_thread )
-    {
-         vlc_assert_locked( &p_owner->lock );
+    vlc_assert_locked( &p_owner->lock );
 
-        /* Empty the fifo */
-        block_FifoEmpty( p_owner->p_fifo );
+    /* Empty the fifo */
+    block_FifoEmpty( p_owner->p_fifo );
 
-        /* Monitor for flush end */
-        p_owner->b_flushing = true;
-        vlc_cond_signal( &p_owner->wait );
-    }
+    /* Monitor for flush end */
+    p_owner->b_flushing = true;
+    vlc_cond_signal( &p_owner->wait );
 
     /* Send a special block */
     p_null = block_New( p_dec, 128 );
@@ -820,11 +803,8 @@ static void DecoderFlush( decoder_t *p_dec )
     input_DecoderDecode( p_dec, p_null );
 
     /* */
-    if( p_owner->b_own_thread )
-    {
-        while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
-            vlc_cond_wait( &p_owner->wait, &p_owner->lock );
-    }
+    while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
+        vlc_cond_wait( &p_owner->wait, &p_owner->lock );
 }
 
 static void DecoderSignalFlushed( decoder_t *p_dec )
@@ -880,8 +860,21 @@ static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
 
     while( !p_owner->b_flushing )
     {
-        if( !p_owner->b_paused && ( !p_owner->b_buffering || !p_owner->buffer.b_full ) )
-            break;
+        if( p_owner->b_paused )
+        {
+            if( p_owner->b_buffering && !p_owner->buffer.b_full )
+                break;
+            if( p_owner->pause.i_ignore > 0 )
+            {
+                p_owner->pause.i_ignore--;
+                break;
+            }
+        }
+        else
+        {
+            if( !p_owner->b_buffering || !p_owner->buffer.b_full )
+                break;
+        }
         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
     }
 
@@ -975,7 +968,7 @@ static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
         if( b_telx )
         {
             *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
-            if( *pi_ts1 && *pi_ts1 <= 0 )
+            if( pi_ts1 && *pi_ts1 <= 0 )
                 *pi_ts1 = *pi_ts0;
         }
     }
@@ -1039,6 +1032,7 @@ static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
             p_audio = p_owner->buffer.p_audio;
 
             p_owner->buffer.p_audio = p_audio->p_next;
+            p_owner->buffer.i_count--;
 
             b_has_more = p_owner->buffer.p_audio != NULL;
             if( !b_has_more )
@@ -1090,7 +1084,13 @@ static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
 
         if( !b_has_more )
             break;
+
         vlc_mutex_lock( &p_owner->lock );
+        if( !p_owner->buffer.p_audio )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            break;
+        }
     }
 }
 
@@ -1192,66 +1192,26 @@ static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
     }
     vlc_mutex_unlock( &p_owner->lock );
 }
-static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
-{
-    vlc_mutex_lock( &p_vout->picture_lock );
-
-    if( p_pic->i_status == READY_PICTURE )
-    {
-        /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
-        p_pic->date = 1;
-    }
-    else if( p_pic->i_refcount > 0 )
-    {
-        p_pic->i_status = DISPLAYED_PICTURE;
-    }
-    else
-    {
-        p_pic->i_status = DESTROYED_PICTURE;
-        picture_CleanupQuant( p_pic );
-        p_vout->i_heap_size--;
-    }
-
-    vlc_mutex_unlock( &p_vout->picture_lock );
-}
-static void VoutFlushPicture( vout_thread_t *p_vout, mtime_t i_max_date )
-{
-    int i;
-    vlc_mutex_lock( &p_vout->picture_lock );
-    for( i = 0; i < p_vout->render.i_pictures; i++ )
-    {
-        picture_t *p_pic = p_vout->render.pp_picture[i];
-
-        if( p_pic->i_status == READY_PICTURE ||
-            p_pic->i_status == DISPLAYED_PICTURE )
-        {
-            /* We cannot change picture status if it is in READY_PICTURE state,
-             * Just make sure they won't be displayed */
-            if( p_pic->date > i_max_date )
-                p_pic->date = i_max_date;
-        }
-    }
-    vlc_mutex_unlock( &p_vout->picture_lock );
-}
 
 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
                               int *pi_played_sum, int *pi_lost_sum )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
     vout_thread_t  *p_vout = p_owner->p_vout;
+    bool b_first_buffered;
 
     if( p_picture->date <= 0 )
     {
         msg_Warn( p_vout, "non-dated video buffer received" );
         *pi_lost_sum += 1;
-        VoutDisplayedPicture( p_vout, p_picture );
+        vout_DropPicture( p_vout, p_picture );
         return;
     }
 
     /* */
     vlc_mutex_lock( &p_owner->lock );
 
-    if( p_owner->b_buffering || p_owner->buffer.p_picture )
+    if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
     {
         p_picture->p_next = NULL;
 
@@ -1266,19 +1226,22 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
             vlc_cond_signal( &p_owner->wait );
         }
     }
+    b_first_buffered = p_owner->buffer.p_picture != NULL;
 
-    for( ;; )
+    for( ;; b_first_buffered = false )
     {
         bool b_has_more = false;
 
         bool b_reject;
+
         DecoderWaitUnblock( p_dec, &b_reject );
 
-        if( p_owner->b_buffering )
+        if( p_owner->b_buffering && !p_owner->buffer.b_first )
         {
             vlc_mutex_unlock( &p_owner->lock );
             return;
         }
+        bool b_buffering_first = p_owner->b_buffering;
 
         /* */
         if( p_owner->buffer.p_picture )
@@ -1286,6 +1249,7 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
             p_picture = p_owner->buffer.p_picture;
 
             p_owner->buffer.p_picture = p_picture->p_next;
+            p_owner->buffer.i_count--;
 
             b_has_more = p_owner->buffer.p_picture != NULL;
             if( !b_has_more )
@@ -1296,6 +1260,17 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
         int i_rate = INPUT_RATE_DEFAULT;
         mtime_t i_delay;
 
+        if( b_buffering_first )
+        {
+            assert( p_owner->buffer.b_first );
+            assert( !p_owner->buffer.i_count );
+            msg_Dbg( p_dec, "Received first picture" );
+            p_owner->buffer.b_first = false;
+            p_picture->b_force = true;
+            i_delay = 0;
+            if( p_owner->p_clock )
+                i_rate = input_clock_GetRate( p_owner->p_clock );
+        }
         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
                       &i_rate, &i_delay, false );
 
@@ -1304,20 +1279,17 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
         /* */
         const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
 
-        if( p_picture->date <= 0 || p_picture->date >= i_max_date )
+        if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
             b_reject = true;
 
         if( !b_reject )
         {
-            if( i_rate != p_owner->i_last_rate  )
+            if( i_rate != p_owner->i_last_rate || b_first_buffered )
             {
                 /* Be sure to not display old picture after our own */
-                VoutFlushPicture( p_vout, p_picture->date );
+                vout_Flush( p_vout, p_picture->date );
                 p_owner->i_last_rate = i_rate;
             }
-
-            vout_DatePicture( p_vout, p_picture, p_picture->date );
-
             vout_DisplayPicture( p_vout, p_picture );
         }
         else
@@ -1332,7 +1304,7 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
                           p_picture->date - mdate() );
             }
             *pi_lost_sum += 1;
-            VoutDisplayedPicture( p_vout, p_picture );
+            vout_DropPicture( p_vout, p_picture );
         }
         int i_tmp_display;
         int i_tmp_lost;
@@ -1341,9 +1313,15 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
         *pi_played_sum += i_tmp_display;
         *pi_lost_sum += i_tmp_lost;
 
-        if( !b_has_more )
+        if( !b_has_more || b_buffering_first )
             break;
+
         vlc_mutex_lock( &p_owner->lock );
+        if( !p_owner->buffer.p_picture )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            break;
+        }
     }
 }
 
@@ -1362,7 +1340,7 @@ static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
         if( p_dec->b_die )
         {
             /* It prevent freezing VLC in case of broken decoder */
-            VoutDisplayedPicture( p_vout, p_pic );
+            vout_DropPicture( p_vout, p_pic );
             if( p_block )
                 block_Release( p_block );
             break;
@@ -1372,7 +1350,7 @@ static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
 
         if( p_pic->date < p_owner->i_preroll_end )
         {
-            VoutDisplayedPicture( p_vout, p_pic );
+            vout_DropPicture( p_vout, p_pic );
             continue;
         }
 
@@ -1380,7 +1358,7 @@ static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
         {
             msg_Dbg( p_dec, "End of video preroll" );
             if( p_vout )
-                VoutFlushPicture( p_vout, 1 );
+                vout_Flush( p_vout, 1 );
             /* */
             p_owner->i_preroll_end = -1;
         }
@@ -1458,6 +1436,7 @@ static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
             p_subpic = p_owner->buffer.p_subpic;
 
             p_owner->buffer.p_subpic = p_subpic->p_next;
+            p_owner->buffer.i_count--;
 
             b_has_more = p_owner->buffer.p_subpic != NULL;
             if( !b_has_more )
@@ -1478,6 +1457,11 @@ static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
         if( !b_has_more )
             break;
         vlc_mutex_lock( &p_owner->lock );
+        if( !p_owner->buffer.p_subpic )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            break;
+        }
     }
 }
 
@@ -1516,7 +1500,7 @@ static void DecoderFlushBuffering( decoder_t *p_dec )
         p_owner->buffer.i_count--;
 
         if( p_owner->p_vout )
-            VoutDisplayedPicture( p_owner->p_vout, p_picture );
+            vout_DropPicture( p_owner->p_vout, p_picture );
 
         if( !p_owner->buffer.p_picture )
             p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
@@ -1598,7 +1582,7 @@ static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
 
             p_sout_block->p_next = NULL;
 
-            DecoderPlaySout( p_dec, p_block, b_telx );
+            DecoderPlaySout( p_dec, p_sout_block, b_telx );
 
             p_sout_block = p_next;
         }
@@ -1658,7 +1642,7 @@ static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flus
     }
 
     if( b_flush && p_owner->p_vout )
-        VoutFlushPicture( p_owner->p_vout, 1 );
+        vout_Flush( p_owner->p_vout, 1 );
 }
 
 /* This function process a audio block
@@ -1862,15 +1846,7 @@ static void DeleteDecoder( decoder_t * p_dec )
     if( p_owner->p_vout )
     {
         /* Hack to make sure all the the pictures are freed by the decoder */
-        for( int i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
-        {
-            picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
-
-            if( p_pic->i_status == RESERVED_PICTURE )
-                vout_DestroyPicture( p_owner->p_vout, p_pic );
-            if( p_pic->i_refcount > 0 )
-                vout_UnlinkPicture( p_owner->p_vout, p_pic );
-        }
+        vout_FixLeaks( p_owner->p_vout, true );
 
         /* We are about to die. Reattach video output to p_vlc. */
         vout_Request( p_dec, p_owner->p_vout, NULL );
@@ -2109,7 +2085,6 @@ static picture_t *vout_new_buffer( decoder_t *p_dec )
     for( ;; )
     {
         picture_t *p_picture;
-        int i_pic, i_ready_pic;
 
         if( p_dec->b_die || p_dec->b_error )
             return NULL;
@@ -2133,55 +2108,16 @@ static picture_t *vout_new_buffer( decoder_t *p_dec )
         DecoderSignalBuffering( p_dec, true );
 
         /* Check the decoder doesn't leak pictures */
-        for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
-        {
-            const picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
-
-            if( p_pic->i_status == READY_PICTURE )
-            {
-                i_ready_pic++;
-                /* If we have at least 2 ready pictures, wait for the vout thread to
-                 * process one */
-                if( i_ready_pic >= 2 )
-                    break;
-
-                continue;
-            }
-
-            if( p_pic->i_status == DISPLAYED_PICTURE )
-            {
-                /* If at least one displayed picture is not referenced
-                 * let vout free it */
-                if( p_pic->i_refcount == 0 )
-                    break;
-            }
-        }
-
-        if( i_pic == p_owner->p_vout->render.i_pictures )
-        {
-            /* Too many pictures are still referenced, there is probably a bug
-             * with the decoder */
-            msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
-
-            /* Just free all the pictures */
-            for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
-            {
-                picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
-
-                if( p_pic->i_status == RESERVED_PICTURE )
-                    vout_DestroyPicture( p_owner->p_vout, p_pic );
-                if( p_pic->i_refcount > 0 )
-                    vout_UnlinkPicture( p_owner->p_vout, p_pic );
-            }
-        }
+        vout_FixLeaks( p_owner->p_vout, false );
 
+        /* FIXME add a vout_WaitPictureAvailable (timedwait) */
         msleep( VOUT_OUTMEM_SLEEP );
     }
 }
 
 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
 {
-    VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
+    vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
 }
 
 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )