Fixed aout visual vout recycling.
[vlc.git] / src / input / decoder.c
index 1224135..bcae5fe 100644 (file)
 #include <vlc_sout.h>
 #include <vlc_codec.h>
 #include <vlc_osd.h>
+#include <vlc_meta.h>
 
 #include <vlc_interface.h>
 #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 "event.h"
+#include "ressource.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 * );
 
 static void      *DecoderThread( vlc_object_t * );
-static int        DecoderDecode( decoder_t * p_dec, block_t *p_block );
-static void       DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date );
+static void       DecoderProcess( decoder_t *, block_t * );
+static void       DecoderError( decoder_t *p_dec, block_t *p_block );
+static void       DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
+static void       DecoderFlush( decoder_t * );
+static void       DecoderSignalBuffering( decoder_t *, bool );
+static void       DecoderFlushBuffering( decoder_t * );
+
+static void       DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
 
 /* Buffers allocation callbacks for the decoders */
 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
@@ -72,12 +81,11 @@ 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;
     input_clock_t   *p_clock;
+    int             i_last_rate;
 
     vout_thread_t   *p_spu_vout;
     int              i_spu_channel;
@@ -94,6 +102,11 @@ struct decoder_owner_sys_t
     audio_format_t audio;
     es_format_t    sout;
 
+    /* */
+    bool           b_fmt_description;
+    es_format_t    fmt_description;
+    vlc_meta_t     *p_description;
+
     /* fifo */
     block_fifo_t *p_fifo;
 
@@ -108,68 +121,137 @@ struct decoder_owner_sys_t
     vout_thread_t   *p_vout;
 
     /* -- Theses variables need locking on read *and* write -- */
+    /* */
     /* Pause */
     bool b_paused;
-    mtime_t i_pause_date;
+    struct
+    {
+        mtime_t i_date;
+        int     i_ignore;
+    } pause;
+
+    /* Buffering */
+    bool b_buffering;
+    struct
+    {
+        bool b_first;
+        bool b_full;
+        int  i_count;
+
+        picture_t     *p_picture;
+        picture_t     **pp_picture_next;
+
+        subpicture_t  *p_subpic;
+        subpicture_t  **pp_subpic_next;
+
+        aout_buffer_t *p_audio;
+        aout_buffer_t **pp_audio_next;
+
+        block_t       *p_block;
+        block_t       **pp_block_next;
+    } buffer;
+
+    /* Flushing */
+    bool b_flushing;
 
     /* CC */
-    bool b_cc_supported;
-    bool pb_cc_present[4];
-    decoder_t *pp_cc[4];
+    struct
+    {
+        bool b_supported;
+        bool pb_present[4];
+        decoder_t *pp_decoder[4];
+    } cc;
 
     /* Delay */
     mtime_t i_ts_delay;
 };
 
+#define DECODER_MAX_BUFFERING_COUNT (4)
+#define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
+#define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
+
+/* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
+ * a bogus PTS and won't be displayed */
+#define DECODER_BOGUS_VIDEO_DELAY                ((mtime_t)(DEFAULT_PTS_DELAY * 30))
+
 /* */
-static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
+#define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
+
+
+/*****************************************************************************
+ * Public functions
+ *****************************************************************************/
+picture_t *decoder_NewPicture( decoder_t *p_decoder )
 {
-    msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
-             "VLC probably does not support this sound or video format.",
-             (char*)&codec );
-    intf_UserFatal( p_dec, false, _("No suitable decoder module"), 
-                    _("VLC does not support the audio or video format \"%4.4s\". "
-                      "Unfortunately there is no way for you to fix this."), (char*)&codec );
+    picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
+    if( !p_picture )
+        msg_Warn( p_decoder, "can't get output picture" );
+    return p_picture;
+}
+void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
+{
+    p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
+}
+void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
+{
+    p_decoder->pf_picture_link( p_decoder, p_picture );
+}
+void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
+{
+    p_decoder->pf_picture_unlink( p_decoder, p_picture );
 }
 
-/* decoder_GetInputAttachment:
- */
-input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
-                                                const char *psz_name )
+aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
 {
-    input_attachment_t *p_attachment;
-    if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
+    if( !p_decoder->pf_aout_buffer_new )
         return NULL;
-    return p_attachment;
+    return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
+}
+void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
+{
+    p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
+}
+
+subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
+{
+    subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
+    if( !p_subpicture )
+        msg_Warn( p_decoder, "can't get output subpicture" );
+    return p_subpicture;
+}
+void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
+{
+    p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
 }
+
 /* decoder_GetInputAttachments:
  */
 int decoder_GetInputAttachments( decoder_t *p_dec,
                                  input_attachment_t ***ppp_attachment,
                                  int *pi_attachment )
 {
-    return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
-                          ppp_attachment, pi_attachment );
+    if( !p_dec->pf_get_attachments )
+        return VLC_EGENERIC;
+
+    return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
 }
 /* decoder_GetDisplayDate:
  */
 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
 {
-    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+    if( !p_dec->pf_get_display_date )
+        return 0;
 
-    if( !p_owner->p_clock )
-        return i_ts;
-    return input_clock_GetTS( p_owner->p_clock, p_owner->p_input->i_pts_delay, i_ts );
+    return p_dec->pf_get_display_date( p_dec, i_ts );
 }
 /* decoder_GetDisplayRate:
  */
 int decoder_GetDisplayRate( decoder_t *p_dec )
 {
-    decoder_owner_sys_t *p_owner = p_dec->p_owner;
-
-    if( !p_owner->p_clock )
+    if( !p_dec->pf_get_display_rate )
         return INPUT_RATE_DEFAULT;
-    return input_clock_GetRate( p_owner->p_clock );
+
+    return p_dec->pf_get_display_rate( p_dec );
 }
 
 /**
@@ -182,12 +264,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 )
     {
@@ -226,35 +306,19 @@ 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 ) )
     {
-        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;
@@ -273,37 +337,22 @@ 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_paused = false;
-            vlc_cond_signal( &p_owner->wait );
-        }
-        vlc_mutex_unlock( &p_owner->lock );
-
-        /* Make sure the thread leaves the function by
-         * sending it an empty block. */
-        block_t *p_block = block_New( p_dec, 0 );
-        input_DecoderDecode( p_dec, p_block );
-
-        vlc_thread_join( p_dec );
+    /* Make sure we aren't paused/buffering/waiting anymore */
+    vlc_mutex_lock( &p_owner->lock );
+    const bool b_was_paused = p_owner->b_paused;
+    p_owner->b_paused = false;
+    p_owner->b_buffering = false;
+    p_owner->b_flushing = true;
+    vlc_cond_signal( &p_owner->wait );
+    vlc_mutex_unlock( &p_owner->lock );
 
-        /* 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 );
+    p_owner->b_paused = b_was_paused;
 
-        module_unneed( p_dec, p_dec->p_module );
-    }
+    module_unneed( p_dec, p_dec->p_module );
 
     /* */
-    if( p_dec->p_owner->b_cc_supported )
+    if( p_dec->p_owner->cc.b_supported )
     {
         int i;
         for( i = 0; i < 4; i++ )
@@ -319,80 +368,42 @@ void input_DecoderDelete( decoder_t *p_dec )
 
 /**
  * Put a block_t in the decoder's fifo.
+ * Thread-safe w.r.t. the decoder. May be a cancellation point.
  *
  * \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( !p_dec->b_die && !p_dec->b_error &&
-                   block_FifoCount( p_owner->p_fifo ) > 10 )
-            {
-                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 );
+        /* The fifo is not consummed when buffering and so will
+         * deadlock vlc.
+         * There is no need to lock as b_buffering is never modify
+         * inside decoder thread. */
+        if( !p_owner->b_buffering )
+            block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
     }
-    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
-        {
-            DecoderDecode( p_dec, p_block );
-        }
-    }
-}
-
-void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
-{
-    decoder_owner_sys_t *p_owner = p_dec->p_owner;
-    block_t *p_null;
-
-    /* Empty the fifo */
-    if( p_owner->b_own_thread && b_flush )
+        /* 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 );
+    }
 
-    /* Send a special block */
-    p_null = block_New( p_dec, 128 );
-    p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
-    if( b_flush && p_dec->fmt_in.i_cat == SPU_ES )
-        p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
-    /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
-    if( p_owner->p_packetizer && b_flush )
-        p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
-    memset( p_null->p_buffer, 0, p_null->i_buffer );
-
-    input_DecoderDecode( p_dec, p_null );
+    block_FifoPut( p_owner->p_fifo, p_block );
 }
 
-bool input_DecoderEmpty( decoder_t * p_dec )
+bool input_DecoderIsEmpty( decoder_t * p_dec )
 {
-    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] )
@@ -402,7 +413,7 @@ void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
 
     vlc_mutex_lock( &p_owner->lock );
     for( i = 0; i < 4; i++ )
-        pb_present[i] =  p_owner->pb_cc_present[i];
+        pb_present[i] =  p_owner->cc.pb_present[i];
     vlc_mutex_unlock( &p_owner->lock );
 }
 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
@@ -411,7 +422,7 @@ int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
 
     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
 
-    if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
+    if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
         return VLC_EGENERIC;
 
     if( b_decode )
@@ -444,7 +455,7 @@ int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
         p_cc->p_owner->p_clock = p_owner->p_clock;
 
         vlc_mutex_lock( &p_owner->lock );
-        p_owner->pp_cc[i_channel] = p_cc;
+        p_owner->cc.pp_decoder[i_channel] = p_cc;
         vlc_mutex_unlock( &p_owner->lock );
     }
     else
@@ -452,8 +463,8 @@ int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
         decoder_t *p_cc;
 
         vlc_mutex_lock( &p_owner->lock );
-        p_cc = p_owner->pp_cc[i_channel];
-        p_owner->pp_cc[i_channel] = NULL;
+        p_cc = p_owner->cc.pp_decoder[i_channel];
+        p_owner->cc.pp_decoder[i_channel] = NULL;
         vlc_mutex_unlock( &p_owner->lock );
 
         if( p_cc )
@@ -471,11 +482,11 @@ int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
 
     *pb_decode = false;
-    if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
+    if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
         return VLC_EGENERIC;
 
     vlc_mutex_lock( &p_owner->lock );
-    *pb_decode = p_owner->pp_cc[i_channel] != NULL;
+    *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
     vlc_mutex_unlock( &p_owner->lock );
     return VLC_EGENERIC;
 }
@@ -486,13 +497,15 @@ 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) );
+    assert( !p_owner->b_paused || !b_paused );
+
     p_owner->b_paused = b_paused;
-    p_owner->i_pause_date = i_date;
-    if( p_owner->b_own_thread )
-        vlc_cond_signal( &p_owner->wait );
+    p_owner->pause.i_date = i_date;
+    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 );
 }
 
@@ -504,6 +517,170 @@ void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
     p_owner->i_ts_delay = i_delay;
     vlc_mutex_unlock( &p_owner->lock );
 }
+
+void input_DecoderStartBuffering( decoder_t *p_dec )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    vlc_mutex_lock( &p_owner->lock );
+
+    DecoderFlush( p_dec );
+
+    p_owner->buffer.b_first = true;
+    p_owner->buffer.b_full = false;
+    p_owner->buffer.i_count = 0;
+
+    assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
+            !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
+
+    p_owner->buffer.p_picture = NULL;
+    p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
+
+    p_owner->buffer.p_subpic = NULL;
+    p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
+
+    p_owner->buffer.p_audio = NULL;
+    p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
+
+    p_owner->buffer.p_block = NULL;
+    p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
+
+
+    p_owner->b_buffering = true;
+
+    vlc_cond_signal( &p_owner->wait );
+
+    vlc_mutex_unlock( &p_owner->lock );
+}
+
+void input_DecoderStopBuffering( decoder_t *p_dec )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    vlc_mutex_lock( &p_owner->lock );
+
+    p_owner->b_buffering = false;
+
+    vlc_cond_signal( &p_owner->wait );
+
+    vlc_mutex_unlock( &p_owner->lock );
+}
+
+void input_DecoderWaitBuffering( decoder_t *p_dec )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    vlc_mutex_lock( &p_owner->lock );
+
+    while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
+    {
+        block_FifoWake( p_owner->p_fifo );
+        vlc_cond_wait( &p_owner->wait, &p_owner->lock );
+    }
+
+    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 );
+}
+
+bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+    bool b_changed;
+
+    vlc_mutex_lock( &p_owner->lock );
+    b_changed = p_owner->b_fmt_description;
+    if( b_changed )
+    {
+        if( p_fmt )
+            es_format_Copy( p_fmt, &p_owner->fmt_description );
+
+        if( pp_meta )
+        {
+            *pp_meta = NULL;
+            if( p_owner->p_description )
+            {
+                *pp_meta = vlc_meta_New();
+                if( *pp_meta )
+                    vlc_meta_Merge( *pp_meta, p_owner->p_description );
+            }
+        }
+        p_owner->b_fmt_description = false;
+    }
+    vlc_mutex_unlock( &p_owner->lock );
+    return b_changed;
+}
+
+/*****************************************************************************
+ * Internal functions
+ *****************************************************************************/
+static int DecoderGetInputAttachments( decoder_t *p_dec,
+                                       input_attachment_t ***ppp_attachment,
+                                       int *pi_attachment )
+{
+    return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
+                          ppp_attachment, pi_attachment );
+}
+static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    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;
+
+    if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
+        return 0;
+
+    return i_ts;
+}
+static int DecoderGetDisplayRate( decoder_t *p_dec )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    if( !p_owner->p_clock )
+        return INPUT_RATE_DEFAULT;
+    return input_clock_GetRate( p_owner->p_clock );
+}
+
+/* */
+static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
+{
+    msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
+             "VLC probably does not support this sound or video format.",
+             (char*)&codec );
+    intf_UserFatal( p_dec, false, _("No suitable decoder module"), 
+                    _("VLC does not support the audio or video format \"%4.4s\". "
+                      "Unfortunately there is no way for you to fix this."), (char*)&codec );
+}
+
+
 /**
  * Create a decoder object
  *
@@ -529,13 +706,15 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
     p_dec->pf_get_cc = NULL;
     p_dec->pf_packetize = NULL;
 
-    /* Initialize the decoder fifo */
+    /* Initialize the decoder */
     p_dec->p_module = NULL;
 
     memset( &null_es_format, 0, sizeof(es_format_t) );
     es_format_Copy( &p_dec->fmt_in, fmt );
     es_format_Copy( &p_dec->fmt_out, &null_es_format );
 
+    p_dec->p_description = NULL;
+
     /* Allocate our private structure for the decoder */
     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
     if( p_dec->p_owner == NULL )
@@ -543,8 +722,8 @@ 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;
     p_dec->p_owner->p_aout = NULL;
     p_dec->p_owner->p_aout_input = NULL;
@@ -573,14 +752,18 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
     p_dec->pf_picture_unlink  = vout_unlink_picture;
     p_dec->pf_spu_buffer_new  = spu_new_buffer;
     p_dec->pf_spu_buffer_del  = spu_del_buffer;
+    /* */
+    p_dec->pf_get_attachments  = DecoderGetInputAttachments;
+    p_dec->pf_get_display_date = DecoderGetDisplayDate;
+    p_dec->pf_get_display_rate = DecoderGetDisplayRate;
 
     vlc_object_attach( p_dec, p_input );
 
     /* Find a suitable decoder/packetizer module */
     if( i_object_type == VLC_OBJECT_DECODER )
-        p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
+        p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
     else
-        p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
+        p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
 
     /* Check if decoder requires already packetized data */
     if( i_object_type == VLC_OBJECT_DECODER &&
@@ -600,7 +783,7 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
 
             p_dec->p_owner->p_packetizer->p_module =
                 module_need( p_dec->p_owner->p_packetizer,
-                             "packetizer", "$packetizer", 0 );
+                             "packetizer", "$packetizer", false );
 
             if( !p_dec->p_owner->p_packetizer->p_module )
             {
@@ -628,24 +811,44 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
             }
         }
     }
+
+    /* */
+    vlc_mutex_init( &p_owner->lock );
+    vlc_cond_init( &p_owner->wait );
+
+    p_owner->b_fmt_description = false;
+    es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
+    p_owner->p_description = NULL;
+
+    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;
+    p_owner->buffer.p_subpic = NULL;
+    p_owner->buffer.p_audio = NULL;
+    p_owner->buffer.p_block = NULL;
+
+    p_owner->b_flushing = false;
+
     /* */
-    p_owner->b_cc_supported = false;
+    p_owner->cc.b_supported = false;
     if( i_object_type == VLC_OBJECT_DECODER )
     {
         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
-            p_owner->b_cc_supported = true;
+            p_owner->cc.b_supported = true;
         if( p_dec->pf_get_cc )
-            p_owner->b_cc_supported = true;
+            p_owner->cc.b_supported = true;
     }
 
-    vlc_mutex_init( &p_owner->lock );
-    vlc_cond_init( &p_owner->wait );
-    p_owner->b_paused = false;
-    p_owner->i_pause_date = 0;
     for( i = 0; i < 4; i++ )
     {
-        p_owner->pb_cc_present[i] = false;
-        p_owner->pp_cc[i] = NULL;
+        p_owner->cc.pb_present[i] = false;
+        p_owner->cc.pp_decoder[i] = NULL;
     }
     p_owner->i_ts_delay = 0;
     return p_dec;
@@ -656,65 +859,139 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
  *
  * \param p_dec the decoder
  */
-static voidDecoderThread( vlc_object_t *p_this )
+static void *DecoderThread( vlc_object_t *p_this )
 {
     decoder_t *p_dec = (decoder_t *)p_this;
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
 
-    block_t *p_block;
-    int canc = vlc_savecancel();
-
     /* The decoder's main loop */
-    while( vlc_object_alive( p_dec ) && !p_dec->b_error )
+    for( ;; )
     {
-        if( ( p_block = block_FifoGet( p_owner->p_fifo ) ) == NULL )
-        {
-            p_dec->b_error = 1;
-            break;
-        }
+        block_t *p_block = block_FifoGet( p_owner->p_fifo );
 
-        if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
-            break;
-    }
+        /* Make sure there is no cancellation point other than this one^^.
+         * If you need one, be sure to push cleanup of p_block. */
+        DecoderSignalBuffering( p_dec, p_block == NULL );
 
-    while( vlc_object_alive( p_dec ) )
-    {
-        /* Trash all received PES packets */
-        p_block = block_FifoGet( p_owner->p_fifo );
         if( p_block )
-            block_Release( p_block );
-    }
+        {
+            int canc = vlc_savecancel();
 
-    /* We do it here because of the dll loader that wants close() in the
-     * same thread than open()/decode() */
-    module_unneed( p_dec, p_dec->p_module );
-    vlc_restorecancel( canc );
+            if( p_dec->b_error )
+                DecoderError( p_dec, p_block );
+            else
+                DecoderProcess( p_dec, p_block );
+
+            vlc_restorecancel( canc );
+        }
+    }
     return NULL;
 }
 
-static void DecoderWaitUnpause( decoder_t *p_dec )
+static void DecoderFlush( decoder_t *p_dec )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
+    block_t *p_null;
 
-    vlc_mutex_lock( &p_owner->lock );
+    vlc_assert_locked( &p_owner->lock );
+
+    /* Empty the fifo */
+    block_FifoEmpty( p_owner->p_fifo );
+
+    /* 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 );
+    if( !p_null )
+        return;
+    p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
+    p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
+    if( !p_dec->fmt_in.b_packetized )
+        p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
+    memset( p_null->p_buffer, 0, p_null->i_buffer );
+
+    input_DecoderDecode( p_dec, p_null );
 
-    while( p_owner->b_paused )
+    /* */
+    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 )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    vlc_mutex_lock( &p_owner->lock );
+
+    if( p_owner->b_flushing )
+    {
+        p_owner->b_flushing = false;
+        vlc_cond_signal( &p_owner->wait );
+    }
+
+    vlc_mutex_unlock( &p_owner->lock );
+}
+
+static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    vlc_mutex_lock( &p_owner->lock );
+
+    if( p_owner->b_buffering )
+    {
+        if( b_full )
+            p_owner->buffer.b_full = true;
+        vlc_cond_signal( &p_owner->wait );
+    }
 
     vlc_mutex_unlock( &p_owner->lock );
 }
 
-static mtime_t DecoderGetTotalDelay( decoder_t *p_dec )
+static bool DecoderIsFlushing( decoder_t *p_dec )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
+    bool b_flushing;
 
     vlc_mutex_lock( &p_owner->lock );
 
-    mtime_t i_delay = p_owner->i_ts_delay;
+    b_flushing = p_owner->b_flushing;
 
     vlc_mutex_unlock( &p_owner->lock );
 
-    return p_owner->p_input->i_pts_delay + i_delay;
+    return b_flushing;
+}
+
+static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    vlc_assert_locked( &p_owner->lock );
+
+    while( !p_owner->b_flushing )
+    {
+        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 );
+    }
+
+    if( pb_reject )
+        *pb_reject = p_owner->b_flushing;
 }
 
 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
@@ -749,7 +1026,7 @@ static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
 }
 
-static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
+static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
 {
     mtime_t current_date = mdate();
 
@@ -758,84 +1035,181 @@ static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
     {
         /* ETSI EN 300 472 Annex A : do not take into account the PTS
          * for teletext streams. */
-        return current_date + 400000 + i_ts_delay;
+        return current_date + 400000;
     }
     return i_ts;
 }
 
-static void DecoderSoutBufferFixTs( block_t *p_block,
-                                    input_clock_t *p_clock, mtime_t i_ts_delay,
-                                    bool b_teletext )
+static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
+                          mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
 {
-    assert( p_clock );
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+    input_clock_t   *p_clock = p_owner->p_clock;
 
-    p_block->i_rate = input_clock_GetRate( p_clock );
+    vlc_assert_locked( &p_owner->lock );
 
-    if( p_block->i_dts > 0 )
-        p_block->i_dts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_dts );
+    const mtime_t i_es_delay = p_owner->i_ts_delay;
 
-    if( p_block->i_pts > 0 )
-        p_block->i_pts = input_clock_GetTS( p_clock, i_ts_delay, p_block->i_pts );
+    if( p_clock )
+    {
+        const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
+        int i_rate;
 
-    if( p_block->i_length > 0 )
-        p_block->i_length = ( p_block->i_length * p_block->i_rate +
-                                INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
+        if( *pi_ts0 > 0 )
+        {
+            *pi_ts0 += i_es_delay;
+            if( pi_ts1 && *pi_ts1 > 0 )
+                *pi_ts1 += i_es_delay;
+            input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound );
+        }
+        else
+        {
+            i_rate = input_clock_GetRate( p_clock );
+        }
 
-    if( b_teletext )
-        p_block->i_pts = DecoderTeletextFixTs( p_block->i_pts, i_ts_delay );
-}
-static void DecoderAoutBufferFixTs( aout_buffer_t *p_buffer,
-                                    input_clock_t *p_clock, mtime_t i_ts_delay )
-{
-    /* sout display module does not set clock */
-    if( !p_clock )
-        return;
+        /* Do not create ephemere data because of rounding errors */
+        if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
+            *pi_ts1 += 1;
 
-    if( p_buffer->start_date )
-        p_buffer->start_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->start_date );
+        if( pi_duration )
+            *pi_duration = ( *pi_duration * i_rate +
+                                    INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
 
-    if( p_buffer->end_date )
-        p_buffer->end_date = input_clock_GetTS( p_clock, i_ts_delay, p_buffer->end_date );
-}
-static void DecoderVoutBufferFixTs( picture_t *p_picture,
-                                    input_clock_t *p_clock, mtime_t i_ts_delay )
-{
-    /* sout display module does not set clock */
-    if( !p_clock )
-        return;
+        if( pi_rate )
+            *pi_rate = i_rate;
 
-    if( p_picture->date )
-        p_picture->date = input_clock_GetTS( p_clock, i_ts_delay, p_picture->date );
+        if( b_telx )
+        {
+            *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
+            if( pi_ts1 && *pi_ts1 <= 0 )
+                *pi_ts1 = *pi_ts0;
+        }
+    }
 }
-static void DecoderSpuBufferFixTs( subpicture_t *p_subpic,
-                                   input_clock_t *p_clock, mtime_t i_ts_delay,
-                                   bool b_teletext )
+
+static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
+                              int *pi_played_sum, int *pi_lost_sum )
 {
-    bool b_ephemere = p_subpic->i_start == p_subpic->i_stop;
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+    aout_instance_t *p_aout = p_owner->p_aout;
+    aout_input_t    *p_aout_input = p_owner->p_aout_input;
 
-    /* sout display module does not set clock */
-    if( !p_clock )
+    /* */
+    if( p_audio->start_date <= 0 )
+    {
+        msg_Warn( p_dec, "non-dated audio buffer received" );
+        *pi_lost_sum += 1;
+        aout_BufferFree( p_audio );
         return;
+    }
+
+    /* */
+    vlc_mutex_lock( &p_owner->lock );
+
+    if( p_owner->b_buffering || p_owner->buffer.p_audio )
+    {
+        p_audio->p_next = NULL;
+
+        *p_owner->buffer.pp_audio_next = p_audio;
+        p_owner->buffer.pp_audio_next = &p_audio->p_next;
+
+        p_owner->buffer.i_count++;
+        if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
+            p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
+        {
+            p_owner->buffer.b_full = true;
+            vlc_cond_signal( &p_owner->wait );
+        }
+    }
+
+    for( ;; )
+    {
+        bool b_has_more = false;
+        bool b_reject;
+        DecoderWaitUnblock( p_dec, &b_reject );
+
+        if( p_owner->b_buffering )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            return;
+        }
+
+        /* */
+        if( p_owner->buffer.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 )
+                p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
+        }
+
+        /* */
+        const bool b_dated = p_audio->start_date > 0;
+        int i_rate = INPUT_RATE_DEFAULT;
+
+        DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
+                      &i_rate, AOUT_MAX_ADVANCE_TIME, false );
+
+        vlc_mutex_unlock( &p_owner->lock );
+
+        if( !p_aout || !p_aout_input ||
+            p_audio->start_date <= 0 ||
+            i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
+            i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
+            b_reject = true;
+
+        /* Do not wait against unprotected date */
+        const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
+        while( !b_reject && i_deadline > mdate() )
+        {
+            vlc_mutex_lock( &p_owner->lock );
+            if( p_owner->b_flushing || p_dec->b_die )
+            {
+                b_reject = true;
+                vlc_mutex_unlock( &p_owner->lock );
+                break;
+            }
+            vlc_cond_timedwait( &p_owner->wait, &p_owner->lock, i_deadline );
+            vlc_mutex_unlock( &p_owner->lock );
+        }
 
-    if( p_subpic->i_start )
-        p_subpic->i_start = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_start );
+        if( !b_reject )
+        {
+            if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
+                *pi_played_sum += 1;
+            *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
+        }
+        else
+        {
+            if( b_dated )
+                msg_Warn( p_aout, "received buffer in the future" );
+            else
+                msg_Warn( p_dec, "non-dated audio buffer received" );
 
-    if( p_subpic->i_stop )
-        p_subpic->i_stop = input_clock_GetTS( p_clock, i_ts_delay, p_subpic->i_stop );
+            *pi_lost_sum += 1;
+            aout_BufferFree( p_audio );
+        }
 
-    /* Do not create ephemere picture because of rounding errors */
-    if( !b_ephemere && p_subpic->i_start == p_subpic->i_stop )
-        p_subpic->i_stop++;
+        if( !b_has_more )
+            break;
 
-    if( b_teletext )
-        p_subpic->i_start = DecoderTeletextFixTs( p_subpic->i_start, i_ts_delay );
+        vlc_mutex_lock( &p_owner->lock );
+        if( !p_owner->buffer.p_audio )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            break;
+        }
+    }
 }
 
 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
     input_thread_t  *p_input = p_owner->p_input;
-    input_clock_t   *p_clock = p_owner->p_clock;
     aout_buffer_t   *p_aout_buf;
     int i_decoded = 0;
     int i_lost = 0;
@@ -845,9 +1219,6 @@ static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
     {
         aout_instance_t *p_aout = p_owner->p_aout;
         aout_input_t    *p_aout_input = p_owner->p_aout_input;
-        int i_rate;
-        int i_lost;
-        int i_played;
 
         if( p_dec->b_die )
         {
@@ -867,55 +1238,14 @@ static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
 
         if( p_owner->i_preroll_end > 0 )
         {
-            /* FIXME TODO flush audio output (don't know how to do that) */
             msg_Dbg( p_dec, "End of audio preroll" );
+            if( p_owner->p_aout && p_owner->p_aout_input )
+                aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
+            /* */
             p_owner->i_preroll_end = -1;
         }
 
-        DecoderWaitUnpause( p_dec );
-
-        const mtime_t i_delay = DecoderGetTotalDelay( p_dec );
-
-        DecoderAoutBufferFixTs( p_aout_buf, p_clock, i_delay );
-
-        if( p_clock )
-            i_rate = input_clock_GetRate( p_clock );
-        else if( p_block && p_block->i_rate > 0 )
-            i_rate = p_block->i_rate;
-        else
-            i_rate = INPUT_RATE_DEFAULT;
-
-        /* FIXME TODO take care of audio-delay for mdate check */
-        const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
-
-        if( p_aout_buf->start_date > 0 &&
-            p_aout_buf->start_date <= i_max_date &&
-            i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
-            i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
-        {
-            /* Wait if we are too early
-             * FIXME that's plain ugly to do it here */
-            mwait( p_aout_buf->start_date - AOUT_MAX_PREPARE_TIME );
-
-            if( !aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate ) )
-                i_played++;
-            i_lost += aout_DecGetResetLost( p_aout, p_aout_input );
-        }
-        else
-        {
-            if( p_aout_buf->start_date <= 0 )
-            {
-                msg_Warn( p_dec, "non-dated audio buffer received" );
-            }
-            else if( p_aout_buf->start_date > i_max_date )
-            {
-                msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
-                          p_aout_buf->start_date - mdate() );
-            }
-            i_lost++;
-            aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
-        }
-
+        DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
     }
 
     /* Update ugly stat */
@@ -944,7 +1274,7 @@ static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
     assert( p_dec_cc->pf_get_cc != NULL );
 
     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
-    if( !p_owner->b_cc_supported )
+    if( !p_owner->cc.b_supported )
         return;
 
     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
@@ -954,163 +1284,147 @@ static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
     vlc_mutex_lock( &p_owner->lock );
     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
     {
-        p_owner->pb_cc_present[i] |= pb_present[i];
-        if( p_owner->pp_cc[i] )
+        p_owner->cc.pb_present[i] |= pb_present[i];
+        if( p_owner->cc.pp_decoder[i] )
             i_cc_decoder++;
     }
 
     for( i = 0; i < 4; i++ )
     {
-        if( !p_owner->pp_cc[i] )
+        if( !p_owner->cc.pp_decoder[i] )
             continue;
 
         if( i_cc_decoder > 1 )
-            DecoderDecode( p_owner->pp_cc[i], block_Duplicate( p_cc ) );
+            DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
         else
-            DecoderDecode( p_owner->pp_cc[i], p_cc );
+            DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
         i_cc_decoder--;
     }
     vlc_mutex_unlock( &p_owner->lock );
 }
-static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
+
+static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
+                              int *pi_played_sum, int *pi_lost_sum )
 {
-    vlc_mutex_lock( &p_vout->picture_lock );
+    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_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
+    if( p_picture->date <= 0 )
     {
-        p_pic->i_status = DESTROYED_PICTURE;
-        picture_CleanupQuant( p_pic );
-        p_vout->i_heap_size--;
+        msg_Warn( p_vout, "non-dated video buffer received" );
+        *pi_lost_sum += 1;
+        vout_DropPicture( p_vout, p_picture );
+        return;
     }
 
-    vlc_mutex_unlock( &p_vout->picture_lock );
-}
-static void VoutFlushPicture( vout_thread_t *p_vout )
-{
-    int i;
-    vlc_mutex_lock( &p_vout->picture_lock );
-    for( i = 0; i < p_vout->render.i_pictures; i++ )
+    /* */
+    vlc_mutex_lock( &p_owner->lock );
+
+    if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
     {
-        picture_t *p_pic = p_vout->render.pp_picture[i];
+        p_picture->p_next = NULL;
 
-        if( p_pic->i_status == READY_PICTURE ||
-            p_pic->i_status == DISPLAYED_PICTURE )
+        *p_owner->buffer.pp_picture_next = p_picture;
+        p_owner->buffer.pp_picture_next = &p_picture->p_next;
+
+        p_owner->buffer.i_count++;
+        if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
+            p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
         {
-            /* We cannot change picture status if it is in READY_PICTURE state,
-             * Just make sure they won't be displayed */
-            p_pic->date = 1;
+            p_owner->buffer.b_full = true;
+            vlc_cond_signal( &p_owner->wait );
         }
     }
-    vlc_mutex_unlock( &p_vout->picture_lock );
-}
-
-#if 0
-static void DecoderOptimizePtsDelay( decoder_t *p_dec )
-{
-    input_thread_t *p_input = p_dec->p_owner->p_input;
-    vout_thread_t *p_vout = p_dec->p_owner->p_vout;
-    input_thread_private_t *p_priv = p_input->p;
-
-    picture_t *p_old = NULL;
-    picture_t *p_young = NULL;
-    int i;
-
-    /* Enable with --auto-adjust-pts-delay */
-    if( !p_priv->pts_adjust.b_auto_adjust )
-        return;
+    b_first_buffered = p_owner->buffer.p_picture != NULL;
 
-    for( i = 0; i < I_RENDERPICTURES; i++ )
+    for( ;; b_first_buffered = false )
     {
-        picture_t *p_pic = PP_RENDERPICTURE[i];
-
-        if( p_pic->i_status != READY_PICTURE )
-            continue;
+        bool b_has_more = false;
 
-        if( !p_old || p_pic->date < p_old->date )
-            p_old = p_pic;
-        if( !p_young || p_pic->date > p_young->date )
-            p_young = p_pic;
-    }
+        bool b_reject;
 
-    if( !p_young || !p_old )
-        return;
+        DecoderWaitUnblock( p_dec, &b_reject );
 
-    /* Try to find if we can reduce the pts
-     * This first draft is way too simple, and we can't say if the
-     * algo will converge. It's also full of constants.
-     * But this simple algo allows to reduce the latency
-     * to the minimum.
-     * The whole point of this, is to bypass the pts_delay set
-     * by the access but also the delay arbitraly set by
-     * the remote server.
-     * Actually the remote server's muxer may set up a
-     * pts<->dts delay in the muxed stream. That is
-     * why we may end up in having a negative pts_delay,
-     * to compensate that artificial delay. */
-    const mtime_t i_buffer_length = p_young->date - p_old->date;
-    int64_t i_pts_slide = 0;
-    if( i_buffer_length < 10000 )
-    {
-        if( p_priv->pts_adjust.i_num_faulty > 10 )
+        if( p_owner->b_buffering && !p_owner->buffer.b_first )
         {
-            i_pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
-            p_priv->pts_adjust.i_num_faulty = 0;
+            vlc_mutex_unlock( &p_owner->lock );
+            return;
         }
-        if( p_priv->pts_adjust.b_to_high )
+        bool b_buffering_first = p_owner->b_buffering;
+
+        /* */
+        if( p_owner->buffer.p_picture )
         {
-            p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
-            p_priv->pts_adjust.i_num_faulty = 0;
+            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 )
+                p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
         }
-        p_priv->pts_adjust.i_num_faulty++;
-    }
-    else if( i_buffer_length > 100000 )
-    {
-        if( p_priv->pts_adjust.i_num_faulty > 25 )
+
+        /* */
+        if( b_buffering_first )
         {
-            i_pts_slide = -i_buffer_length/2;
-            p_priv->pts_adjust.i_num_faulty = 0;
+            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;
         }
-        if( p_priv->pts_adjust.b_to_high )
+
+        const bool b_dated = p_picture->date > 0;
+        int i_rate = INPUT_RATE_DEFAULT;
+        DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
+                      &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
+
+        vlc_mutex_unlock( &p_owner->lock );
+
+        /* */
+        if( !p_picture->b_force && p_picture->date <= 0 )
+            b_reject = true;
+
+        if( !b_reject )
         {
-            p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
-            p_priv->pts_adjust.i_num_faulty = 0;
+            if( i_rate != p_owner->i_last_rate || b_first_buffered )
+            {
+                /* Be sure to not display old picture after our own */
+                vout_Flush( p_vout, p_picture->date );
+                p_owner->i_last_rate = i_rate;
+            }
+            vout_DisplayPicture( p_vout, p_picture );
         }
-        p_priv->pts_adjust.i_num_faulty++;
-    }
-    if( i_pts_slide != 0 )
-    {
-        const mtime_t i_pts_delay_org = p_input->i_pts_delay;
+        else
+        {
+            if( b_dated )
+                msg_Warn( p_vout, "early picture skipped" );
+            else
+                msg_Warn( p_vout, "non-dated video buffer received" );
 
-        p_input->i_pts_delay += i_pts_slide;
+            *pi_lost_sum += 1;
+            vout_DropPicture( p_vout, p_picture );
+        }
+        int i_tmp_display;
+        int i_tmp_lost;
+        vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
 
-        /* Don't play with the pts delay for more than -2<->3sec */
-        if( p_input->i_pts_delay < -2000000 )
-            p_input->i_pts_delay = -2000000;
-        else if( p_input->i_pts_delay > 3000000 )
-            p_input->i_pts_delay = 3000000;
-        i_pts_slide = p_input->i_pts_delay - i_pts_delay_org;
+        *pi_played_sum += i_tmp_display;
+        *pi_lost_sum += i_tmp_lost;
 
-        msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
-            (int)i_pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)i_buffer_length/1000);
+        if( !b_has_more || b_buffering_first )
+            break;
 
-        vlc_mutex_lock( &p_vout->picture_lock );
-        /* Slide all the picture */
-        for( i = 0; i < I_RENDERPICTURES; i++ )
-            PP_RENDERPICTURE[i]->date += i_pts_slide;
-        /* FIXME: slide aout/spu */
-        vlc_mutex_unlock( &p_vout->picture_lock );
+        vlc_mutex_lock( &p_owner->lock );
+        if( !p_owner->buffer.p_picture )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            break;
+        }
     }
 }
-#endif
 
 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
 {
@@ -1127,7 +1441,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;
@@ -1137,7 +1451,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;
         }
 
@@ -1145,7 +1459,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 );
+                vout_Flush( p_vout, 1 );
             /* */
             p_owner->i_preroll_end = -1;
         }
@@ -1154,295 +1468,526 @@ static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
             DecoderGetCc( p_dec, p_dec );
 
-        DecoderWaitUnpause( p_dec );
+        DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
+    }
+    if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
+    {
+        vlc_mutex_lock( &p_input->p->counters.counters_lock );
+
+        stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
+                             i_decoded, NULL );
+        stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
+                             i_lost , NULL);
+
+        stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
+                             i_displayed, NULL);
+
+        vlc_mutex_unlock( &p_input->p->counters.counters_lock );
+    }
+}
+
+static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
+                            bool b_telx )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+    vout_thread_t *p_vout = p_owner->p_spu_vout;
+
+    /* */
+    if( p_subpic->i_start <= 0 )
+    {
+        msg_Warn( p_dec, "non-dated spu buffer received" );
+        subpicture_Delete( p_subpic );
+        return;
+    }
+
+    /* */
+    vlc_mutex_lock( &p_owner->lock );
+
+    if( p_owner->b_buffering || p_owner->buffer.p_subpic )
+    {
+        p_subpic->p_next = NULL;
+
+        *p_owner->buffer.pp_subpic_next = p_subpic;
+        p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
+
+        p_owner->buffer.i_count++;
+        /* XXX it is important to be full after the first one */
+        if( p_owner->buffer.i_count > 0 )
+        {
+            p_owner->buffer.b_full = true;
+            vlc_cond_signal( &p_owner->wait );
+        }
+    }
+
+    for( ;; )
+    {
+        bool b_has_more = false;
+        bool b_reject;
+        DecoderWaitUnblock( p_dec, &b_reject );
+
+        if( p_owner->b_buffering )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            return;
+        }
+
+        /* */
+        if( p_owner->buffer.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 )
+                p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
+        }
+
+        /* */
+        DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
+                      NULL, INT64_MAX, b_telx );
+
+        vlc_mutex_unlock( &p_owner->lock );
+
+        if( !b_reject )
+            spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
+        else
+            subpicture_Delete( 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;
+        }
+    }
+}
+
+static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
+                             bool b_telx )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    assert( p_owner->p_clock );
+    assert( !p_sout_block->p_next );
+
+    vlc_mutex_lock( &p_owner->lock );
+
+    if( p_owner->b_buffering || p_owner->buffer.p_block )
+    {
+        block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
+
+        p_owner->buffer.i_count++;
+        /* XXX it is important to be full after the first one */
+        if( p_owner->buffer.i_count > 0 )
+        {
+            p_owner->buffer.b_full = true;
+            vlc_cond_signal( &p_owner->wait );
+        }
+    }
+
+    for( ;; )
+    {
+        bool b_has_more = false;
+        bool b_reject;
+        DecoderWaitUnblock( p_dec, &b_reject );
+
+        if( p_owner->b_buffering )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            return;
+        }
+
+        /* */
+        if( p_owner->buffer.p_block )
+        {
+            p_sout_block = p_owner->buffer.p_block;
+
+            p_owner->buffer.p_block = p_sout_block->p_next;
+            p_owner->buffer.i_count--;
+
+            b_has_more = p_owner->buffer.p_block != NULL;
+            if( !b_has_more )
+                p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
+        }
+        p_sout_block->p_next = NULL;
+
+        DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
+                      &p_sout_block->i_length,
+                      &p_sout_block->i_rate, INT64_MAX, b_telx );
+
+        vlc_mutex_unlock( &p_owner->lock );
+
+        if( !b_reject )
+            sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
+        else
+            block_Release( p_sout_block );
+
+        if( !b_has_more )
+            break;
+        vlc_mutex_lock( &p_owner->lock );
+        if( !p_owner->buffer.p_block )
+        {
+            vlc_mutex_unlock( &p_owner->lock );
+            break;
+        }
+    }
+}
+
+/* */
+static void DecoderFlushBuffering( decoder_t *p_dec )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    vlc_assert_locked( &p_owner->lock );
+
+    while( p_owner->buffer.p_picture )
+    {
+        picture_t *p_picture = p_owner->buffer.p_picture;
 
-        const mtime_t i_delay = DecoderGetTotalDelay( p_dec );
+        p_owner->buffer.p_picture = p_picture->p_next;
+        p_owner->buffer.i_count--;
 
-        DecoderVoutBufferFixTs( p_pic, p_owner->p_clock, i_delay );
+        if( p_owner->p_vout )
+            vout_DropPicture( p_owner->p_vout, p_picture );
 
-        /* Video is never delayed so simple */
-        const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
+        if( !p_owner->buffer.p_picture )
+            p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
+    }
+    while( p_owner->buffer.p_audio )
+    {
+        aout_buffer_t *p_audio = p_owner->buffer.p_audio;
 
-        if( p_pic->date > 0 && p_pic->date < i_max_date )
-        {
-            vout_DatePicture( p_vout, p_pic, p_pic->date );
+        p_owner->buffer.p_audio = p_audio->p_next;
+        p_owner->buffer.i_count--;
 
-            /* Re-enable it but do it right this time */
-            //DecoderOptimizePtsDelay( p_dec );
-            vout_DisplayPicture( p_vout, p_pic );
-        }
-        else
-        {
-            if( p_pic->date <= 0 )
-            {
-                msg_Warn( p_vout, "non-dated video buffer received" );
-            }
-            else
-            {
-                msg_Warn( p_vout, "early picture skipped (%"PRId64")",
-                          p_pic->date - mdate() );
-            }
-            i_lost++;
-            VoutDisplayedPicture( p_vout, p_pic );
-        }
-        int i_tmp_display;
-        int i_tmp_lost;
-        vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
+        aout_BufferFree( p_audio );
 
-        i_displayed += i_tmp_display;
-        i_lost += i_tmp_lost;
+        if( !p_owner->buffer.p_audio )
+            p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
     }
-    if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
+    while( p_owner->buffer.p_subpic )
     {
-        vlc_mutex_lock( &p_input->p->counters.counters_lock );
+        subpicture_t *p_subpic = p_owner->buffer.p_subpic;
 
-        stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
-                             i_decoded, NULL );
-        stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
-                             i_lost , NULL);
+        p_owner->buffer.p_subpic = p_subpic->p_next;
+        p_owner->buffer.i_count--;
 
-        stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
-                             i_displayed, NULL);
+        subpicture_Delete( p_subpic );
 
-        vlc_mutex_unlock( &p_input->p->counters.counters_lock );
+        if( !p_owner->buffer.p_subpic )
+            p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
+    }
+    if( p_owner->buffer.p_block )
+    {
+        block_ChainRelease( p_owner->buffer.p_block );
+
+        p_owner->buffer.i_count = 0;
+        p_owner->buffer.p_block = NULL;
+        p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
     }
 }
 
-/**
- * Decode a block
- *
- * \param p_dec the decoder object
- * \param p_block the block to decode
- * \return VLC_SUCCESS or an error code
+/* This function process a block for sout
  */
-static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
+static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
 {
     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
+    block_t *p_sout_block;
 
-    if( p_block && p_block->i_buffer <= 0 )
-    {
-        block_Release( p_block );
-        return VLC_SUCCESS;
-    }
-
-#ifdef ENABLE_SOUT
-    if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
+    while( ( p_sout_block =
+                 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
     {
-        block_t *p_sout_block;
-
-        while( ( p_sout_block =
-                     p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
+        if( !p_owner->p_sout_input )
         {
-            if( !p_owner->p_sout_input )
+            es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
+
+            p_owner->sout.i_group = p_dec->fmt_in.i_group;
+            p_owner->sout.i_id = p_dec->fmt_in.i_id;
+            if( p_dec->fmt_in.psz_language )
             {
-                es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
+                if( p_owner->sout.psz_language )
+                    free( p_owner->sout.psz_language );
+                p_owner->sout.psz_language =
+                    strdup( p_dec->fmt_in.psz_language );
+            }
 
-                p_owner->sout.i_group = p_dec->fmt_in.i_group;
-                p_owner->sout.i_id = p_dec->fmt_in.i_id;
-                if( p_dec->fmt_in.psz_language )
-                {
-                    if( p_owner->sout.psz_language )
-                        free( p_owner->sout.psz_language );
-                    p_owner->sout.psz_language =
-                        strdup( p_dec->fmt_in.psz_language );
-                }
+            p_owner->p_sout_input =
+                sout_InputNew( p_owner->p_sout,
+                               &p_owner->sout );
 
-                p_owner->p_sout_input =
-                    sout_InputNew( p_owner->p_sout,
-                                   &p_owner->sout );
+            if( p_owner->p_sout_input == NULL )
+            {
+                msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
+                         (char *)&p_owner->sout.i_codec );
+                p_dec->b_error = true;
 
-                if( p_owner->p_sout_input == NULL )
+                while( p_sout_block )
                 {
-                    msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
-                             (char *)&p_owner->sout.i_codec );
-                    p_dec->b_error = true;
-
-                    while( p_sout_block )
-                    {
-                        block_t *p_next = p_sout_block->p_next;
-                        block_Release( p_sout_block );
-                        p_sout_block = p_next;
-                    }
-                    break;
+                    block_t *p_next = p_sout_block->p_next;
+                    block_Release( p_sout_block );
+                    p_sout_block = p_next;
                 }
+                break;
             }
+        }
 
-            while( p_sout_block )
-            {
-                block_t *p_next = p_sout_block->p_next;
+        while( p_sout_block )
+        {
+            block_t *p_next = p_sout_block->p_next;
 
-                p_sout_block->p_next = NULL;
+            p_sout_block->p_next = NULL;
 
-                DecoderWaitUnpause( p_dec );
+            DecoderPlaySout( p_dec, p_sout_block, b_telx );
 
-                const mtime_t i_delay = DecoderGetTotalDelay( p_dec );
+            p_sout_block = p_next;
+        }
 
-                DecoderSoutBufferFixTs( p_sout_block,
-                                        p_owner->p_clock, i_delay, b_telx );
+        /* For now it's enough, as only sout impact on this flag */
+        if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
+            p_owner->p_input->p->b_out_pace_control )
+        {
+            msg_Dbg( p_dec, "switching to sync mode" );
+            p_owner->p_input->p->b_out_pace_control = false;
+        }
+        else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
+                 !p_owner->p_input->p->b_out_pace_control )
+        {
+            msg_Dbg( p_dec, "switching to async mode" );
+            p_owner->p_input->p->b_out_pace_control = true;
+        }
+    }
+}
 
-                sout_InputSendBuffer( p_owner->p_sout_input,
-                                      p_sout_block );
+/* This function process a video block
+ */
+static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
+{
+    decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
 
-                p_sout_block = p_next;
-            }
+    if( p_owner->p_packetizer )
+    {
+        block_t *p_packetized_block;
+        decoder_t *p_packetizer = p_owner->p_packetizer;
 
-            /* For now it's enough, as only sout impact on this flag */
-            if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
-                p_owner->p_input->p->b_out_pace_control )
+        while( (p_packetized_block =
+                p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
+        {
+            if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
             {
-                msg_Dbg( p_dec, "switching to sync mode" );
-                p_owner->p_input->p->b_out_pace_control = false;
+                es_format_Clean( &p_dec->fmt_in );
+                es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
             }
-            else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
-                     !p_owner->p_input->p->b_out_pace_control )
+            if( p_packetizer->pf_get_cc )
+                DecoderGetCc( p_dec, p_packetizer );
+
+            while( p_packetized_block )
             {
-                msg_Dbg( p_dec, "switching to async mode" );
-                p_owner->p_input->p->b_out_pace_control = true;
+                block_t *p_next = p_packetized_block->p_next;
+                p_packetized_block->p_next = NULL;
+
+                DecoderDecodeVideo( p_dec, p_packetized_block );
+
+                p_packetized_block = p_next;
             }
         }
     }
-    else
-#endif
-    if( p_dec->fmt_in.i_cat == AUDIO_ES )
+    else if( p_block )
     {
-        if( p_block )
-            DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
+        DecoderDecodeVideo( p_dec, p_block );
+    }
 
-        if( p_owner->p_packetizer )
-        {
-            block_t *p_packetized_block;
-            decoder_t *p_packetizer = p_owner->p_packetizer;
+    if( b_flush && p_owner->p_vout )
+        vout_Flush( p_owner->p_vout, 1 );
+}
+
+/* This function process a audio block
+ */
+static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
+{
+    decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
 
-            while( (p_packetized_block =
-                    p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
+    if( p_owner->p_packetizer )
+    {
+        block_t *p_packetized_block;
+        decoder_t *p_packetizer = p_owner->p_packetizer;
+
+        while( (p_packetized_block =
+                p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
+        {
+            if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
             {
-                if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
-                {
-                    es_format_Clean( &p_dec->fmt_in );
-                    es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
-                }
+                es_format_Clean( &p_dec->fmt_in );
+                es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
+            }
 
-                while( p_packetized_block )
-                {
-                    block_t *p_next = p_packetized_block->p_next;
-                    p_packetized_block->p_next = NULL;
+            while( p_packetized_block )
+            {
+                block_t *p_next = p_packetized_block->p_next;
+                p_packetized_block->p_next = NULL;
 
-                    DecoderDecodeAudio( p_dec, p_packetized_block );
+                DecoderDecodeAudio( p_dec, p_packetized_block );
 
-                    p_packetized_block = p_next;
-                }
+                p_packetized_block = p_next;
             }
         }
-        else if( p_block )
-        {
-            DecoderDecodeAudio( p_dec, p_block );
-        }
     }
-    else if( p_dec->fmt_in.i_cat == VIDEO_ES )
+    else if( p_block )
     {
-        if( p_block )
-            DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
+        DecoderDecodeAudio( p_dec, p_block );
+    }
 
-        if( p_owner->p_packetizer )
-        {
-            block_t *p_packetized_block;
-            decoder_t *p_packetizer = p_owner->p_packetizer;
+    if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
+        aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
+}
 
-            while( (p_packetized_block =
-                    p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
-            {
-                if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
-                {
-                    es_format_Clean( &p_dec->fmt_in );
-                    es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
-                }
-                if( p_packetizer->pf_get_cc )
-                    DecoderGetCc( p_dec, p_packetizer );
+/* This function process a subtitle block
+ */
+static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+    const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
 
-                while( p_packetized_block )
-                {
-                    block_t *p_next = p_packetized_block->p_next;
-                    p_packetized_block->p_next = NULL;
+    input_thread_t *p_input = p_owner->p_input;
+    vout_thread_t *p_vout;
+    subpicture_t *p_spu;
 
-                    DecoderDecodeVideo( p_dec, p_packetized_block );
+    while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
+    {
+        vlc_mutex_lock( &p_input->p->counters.counters_lock );
+        stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
+        vlc_mutex_unlock( &p_input->p->counters.counters_lock );
 
-                    p_packetized_block = p_next;
-                }
+        p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
+        if( p_vout && p_owner->p_spu_vout == p_vout )
+        {
+            /* Preroll does not work very well with subtitle */
+            if( p_spu->i_start > 0 &&
+                p_spu->i_start < p_owner->i_preroll_end &&
+                ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
+            {
+                subpicture_Delete( p_spu );
+            }
+            else
+            {
+                DecoderPlaySpu( p_dec, p_spu, b_telx );
             }
         }
-        else if( p_block )
+        else
         {
-            DecoderDecodeVideo( p_dec, p_block );
+            subpicture_Delete( p_spu );
         }
+        if( p_vout )
+            vlc_object_release( p_vout );
     }
-    else if( p_dec->fmt_in.i_cat == SPU_ES )
+
+    if( b_flush && p_owner->p_spu_vout )
+    {
+        p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
+
+        if( p_vout && p_owner->p_spu_vout == p_vout )
+            spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
+                         p_owner->i_spu_channel );
+
+        if( p_vout )
+            vlc_object_release( p_vout );
+    }
+}
+
+/* */
+static void DecoderProcessOnFlush( decoder_t *p_dec )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    vlc_mutex_lock( &p_owner->lock );
+    DecoderFlushBuffering( p_dec );
+    vlc_mutex_unlock( &p_owner->lock );
+
+    DecoderSignalFlushed( p_dec );
+}
+/**
+ * Decode a block
+ *
+ * \param p_dec the decoder object
+ * \param p_block the block to decode
+ * \return VLC_SUCCESS or an error code
+ */
+static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
+{
+    decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
+    const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
+
+    if( p_block && p_block->i_buffer <= 0 )
+    {
+        assert( !b_flush_request );
+        block_Release( p_block );
+        return;
+    }
+
+#ifdef ENABLE_SOUT
+    if( vlc_internals( p_dec )->i_object_type == VLC_OBJECT_PACKETIZER )
+    {
+        if( p_block )
+            p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
+
+        DecoderProcessSout( p_dec, p_block );
+    }
+    else
+#endif
     {
-        input_thread_t *p_input = p_owner->p_input;
-        vout_thread_t *p_vout;
-        subpicture_t *p_spu;
-        bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
         bool b_flush = false;
 
         if( p_block )
         {
+            const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
-            b_flush = (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH) != 0;
-        }
-
-        if( !b_flushing && b_flush && p_owner->p_spu_vout )
-        {
-            p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
 
-            if( p_vout && p_owner->p_spu_vout == p_vout )
-                spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
-                             p_owner->i_spu_channel );
+            b_flush = !b_flushing && b_flush_request;
 
-            if( p_vout )
-                vlc_object_release( p_vout );
+            p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
         }
 
-        while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
+        if( p_dec->fmt_in.i_cat == AUDIO_ES )
         {
-            vlc_mutex_lock( &p_input->p->counters.counters_lock );
-            stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
-            vlc_mutex_unlock( &p_input->p->counters.counters_lock );
-
-            p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
-            if( p_vout && p_owner->p_spu_vout == p_vout )
-            {
-                /* Preroll does not work very well with subtitle */
-                if( p_spu->i_start > 0 &&
-                    p_spu->i_start < p_owner->i_preroll_end &&
-                    ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
-                {
-                    subpicture_Delete( p_spu );
-                }
-                else
-                {
-                    DecoderWaitUnpause( p_dec );
-
-                    DecoderSpuBufferFixTs( p_spu, p_owner->p_clock, p_input->i_pts_delay, b_telx );
-                    spu_DisplaySubpicture( p_vout->p_spu, p_spu );
-                }
-            }
-            else
-            {
-                msg_Warn( p_dec, "no vout found, leaking subpicture" );
-            }
-            if( p_vout )
-                vlc_object_release( p_vout );
+            DecoderProcessAudio( p_dec, p_block, b_flush );
+        }
+        else if( p_dec->fmt_in.i_cat == VIDEO_ES )
+        {
+            DecoderProcessVideo( p_dec, p_block, b_flush );
+        }
+        else if( p_dec->fmt_in.i_cat == SPU_ES )
+        {
+            DecoderProcessSpu( p_dec, p_block, b_flush );
+        }
+        else
+        {
+            msg_Err( p_dec, "unknown ES format" );
+            p_dec->b_error = true;
         }
     }
-    else
-    {
-        msg_Err( p_dec, "unknown ES format" );
-        p_dec->b_error = 1;
-    }
 
-    return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
+    /* */
+    if( b_flush_request )
+        DecoderProcessOnFlush( p_dec );
+}
+
+static void DecoderError( decoder_t *p_dec, block_t *p_block )
+{
+    const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
+
+    /* */
+    if( p_block )
+        block_Release( p_block );
+
+    if( b_flush_request )
+        DecoderProcessOnFlush( p_dec );
 }
 
+
 /**
  * Destroys a decoder object
  *
@@ -1461,33 +2006,32 @@ static void DeleteDecoder( decoder_t * p_dec )
     block_FifoEmpty( p_owner->p_fifo );
     block_FifoRelease( p_owner->p_fifo );
 
+    /* */
+    vlc_mutex_lock( &p_owner->lock );
+    DecoderFlushBuffering( p_dec );
+    vlc_mutex_unlock( &p_owner->lock );
+
     /* Cleanup */
     if( p_owner->p_aout_input )
         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
     if( p_owner->p_aout )
     {
-        vlc_object_release( p_owner->p_aout );
+        input_ressource_RequestAout( p_owner->p_input->p->p_ressource,
+                                     p_owner->p_aout );
+        input_SendEventAout( p_owner->p_input );
         p_owner->p_aout = NULL;
     }
     if( p_owner->p_vout )
     {
-        int i_pic;
-
-#define p_pic p_owner->p_vout->render.pp_picture[i_pic]
-        /* Hack to make sure all the the pictures are freed by the decoder */
-        for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
-             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 );
-        }
-#undef p_pic
+        /* Hack to make sure all the the pictures are freed by the decoder
+         * and that the vout is not paused anymore */
+        vout_FixLeaks( p_owner->p_vout, true );
+        if( p_owner->b_paused )
+            vout_ChangePause( p_owner->p_vout, false, mdate() );
 
-        /* We are about to die. Reattach video output to p_vlc. */
-        vout_Request( p_dec, p_owner->p_vout, NULL );
-        var_SetBool( p_owner->p_input, "intf-change-vout", true );
+        /* */
+        input_ressource_RequestVout( p_owner->p_input->p->p_ressource, p_owner->p_vout, NULL );
+        input_SendEventVout( p_owner->p_input );
     }
 
 #ifdef ENABLE_SOUT
@@ -1502,17 +2046,22 @@ static void DeleteDecoder( decoder_t * p_dec )
     {
         vout_thread_t *p_vout;
 
-        p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
-        if( p_vout && p_owner->p_spu_vout == p_vout )
+        p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
+        if( p_vout )
         {
-            spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
-                         p_owner->i_spu_channel );
+            if( p_owner->p_spu_vout == p_vout )
+                spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
             vlc_object_release( p_vout );
         }
     }
 
     es_format_Clean( &p_dec->fmt_in );
     es_format_Clean( &p_dec->fmt_out );
+    if( p_dec->p_description )
+        vlc_meta_Delete( p_dec->p_description );
+    es_format_Clean( &p_owner->fmt_description );
+    if( p_owner->p_description )
+        vlc_meta_Delete( p_owner->p_description );
 
     if( p_owner->p_packetizer )
     {
@@ -1520,6 +2069,8 @@ static void DeleteDecoder( decoder_t * p_dec )
                        p_owner->p_packetizer->p_module );
         es_format_Clean( &p_owner->p_packetizer->fmt_in );
         es_format_Clean( &p_owner->p_packetizer->fmt_out );
+        if( p_owner->p_packetizer->p_description )
+            vlc_meta_Delete( p_owner->p_packetizer->p_description );
         vlc_object_detach( p_owner->p_packetizer );
         vlc_object_release( p_owner->p_packetizer );
     }
@@ -1535,6 +2086,40 @@ static void DeleteDecoder( decoder_t * p_dec )
 /*****************************************************************************
  * Buffers allocation callbacks for the decoders
  *****************************************************************************/
+static void DecoderUpdateFormatLocked( decoder_t *p_dec )
+{
+    decoder_owner_sys_t *p_owner = p_dec->p_owner;
+
+    vlc_assert_locked( &p_owner->lock );
+
+    p_owner->b_fmt_description = true;
+
+    /* Copy es_format */
+    es_format_Clean( &p_owner->fmt_description );
+    es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
+
+    /* Move p_description */
+    if( p_owner->p_description && p_dec->p_description )
+        vlc_meta_Delete( p_owner->p_description );
+    p_owner->p_description = p_dec->p_description;
+    p_dec->p_description = NULL;
+}
+static vout_thread_t *aout_request_vout( void *p_private,
+                                         vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
+{
+    decoder_t *p_dec = p_private;
+    input_thread_t *p_input = p_dec->p_owner->p_input;
+
+    p_vout = input_ressource_RequestVout( p_input->p->p_ressource, p_vout, p_fmt );
+    /* TODO it would be better to give b_recyle to input_ressource_RequestVout
+     * as here we are not sure of which vout we destroy */
+    if( !b_recyle )
+        input_ressource_TerminateVout( p_input->p->p_ressource );
+    input_SendEventVout( p_input );
+
+    return p_vout;
+}
+
 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
@@ -1552,6 +2137,8 @@ static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
         /* Parameters changed, restart the aout */
         vlc_mutex_lock( &p_owner->lock );
 
+        DecoderFlushBuffering( p_dec );
+
         p_owner->p_aout_input = NULL;
         aout_DecDelete( p_owner->p_aout, p_aout_input );
 
@@ -1564,15 +2151,17 @@ static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
         audio_sample_format_t format;
         aout_input_t *p_aout_input;
         aout_instance_t *p_aout;
+        aout_request_vout_t request_vout;
 
         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
         p_owner->audio = p_dec->fmt_out.audio;
 
         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
-        if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
-                                    == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
+        if( i_force_dolby &&
+            (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
+                (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
         {
-            if ( i_force_dolby == 1 )
+            if( i_force_dolby == 1 )
             {
                 format.i_original_channels = format.i_original_channels |
                                              AOUT_CHAN_DOLBYSTEREO;
@@ -1584,15 +2173,25 @@ static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
             }
         }
 
+        request_vout.pf_request_vout = aout_request_vout;
+        request_vout.p_private = p_dec;
+
         p_aout = p_owner->p_aout;
+        if( !p_aout )
+            p_aout = input_ressource_RequestAout( p_owner->p_input->p->p_ressource, NULL );
         p_aout_input = aout_DecNew( p_dec, &p_aout,
-                                    &format, &p_dec->fmt_out.audio_replay_gain );
+                                    &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
 
         vlc_mutex_lock( &p_owner->lock );
+
         p_owner->p_aout = p_aout;
         p_owner->p_aout_input = p_aout_input;
+        DecoderUpdateFormatLocked( p_dec );
+
         vlc_mutex_unlock( &p_owner->lock );
 
+        input_SendEventAout( p_owner->p_input );
+
         if( p_owner->p_aout_input == NULL )
         {
             msg_Err( p_dec, "failed to create audio output" );
@@ -1622,7 +2221,6 @@ int vout_CountPictureAvailable( vout_thread_t *p_vout );
 static picture_t *vout_new_buffer( decoder_t *p_dec )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
-    picture_t *p_pic;
 
     if( p_owner->p_vout == NULL ||
         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
@@ -1687,17 +2285,24 @@ static picture_t *vout_new_buffer( decoder_t *p_dec )
         p_owner->video = p_dec->fmt_out.video;
 
         vlc_mutex_lock( &p_owner->lock );
+
+        DecoderFlushBuffering( p_dec );
+
         p_vout = p_owner->p_vout;
         p_owner->p_vout = NULL;
         vlc_mutex_unlock( &p_owner->lock );
 
-        p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
+        p_vout = input_ressource_RequestVout( p_owner->p_input->p->p_ressource,
+                                              p_vout, &p_dec->fmt_out.video );
 
         vlc_mutex_lock( &p_owner->lock );
         p_owner->p_vout = p_vout;
+
+        DecoderUpdateFormatLocked( p_dec );
+
         vlc_mutex_unlock( &p_owner->lock );
 
-        var_SetBool( p_owner->p_input, "intf-change-vout", true );
+        input_SendEventVout( p_owner->p_input );
         if( p_vout == NULL )
         {
             msg_Err( p_dec, "failed to create video output" );
@@ -1715,9 +2320,9 @@ static picture_t *vout_new_buffer( decoder_t *p_dec )
 
     /* Get a new picture
      */
-    for( p_pic = NULL; ; )
+    for( ;; )
     {
-        int i_pic, i_ready_pic;
+        picture_t *p_picture;
 
         if( p_dec->b_die || p_dec->b_error )
             return NULL;
@@ -1729,61 +2334,28 @@ static picture_t *vout_new_buffer( decoder_t *p_dec )
          */
         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
         {
-            p_pic = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
-            if( p_pic )
-                break;
+            p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
+            if( p_picture )
+                return p_picture;
         }
 
-#define p_pic p_owner->p_vout->render.pp_picture[i_pic]
-        /* 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++ )
-        {
-            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( DecoderIsFlushing( p_dec ) )
+            return NULL;
 
-            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" );
+        /* */
+        DecoderSignalBuffering( p_dec, true );
 
-            /* Just free all the pictures */
-            for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
-                 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 );
-            }
-        }
-#undef p_pic
+        /* Check the decoder doesn't leak pictures */
+        vout_FixLeaks( p_owner->p_vout, false );
 
+        /* FIXME add a vout_WaitPictureAvailable (timedwait) */
         msleep( VOUT_OUTMEM_SLEEP );
     }
-
-    return p_pic;
 }
 
 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 )
@@ -1808,11 +2380,11 @@ static subpicture_t *spu_new_buffer( decoder_t *p_dec )
         if( p_dec->b_die || p_dec->b_error )
             break;
 
-        p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+        p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
         if( p_vout )
             break;
 
-        msleep( VOUT_DISPLAY_DELAY );
+        msleep( DECODER_SPU_VOUT_WAIT_DURATION );
     }
 
     if( !p_vout )
@@ -1823,6 +2395,12 @@ static subpicture_t *spu_new_buffer( decoder_t *p_dec )
 
     if( p_owner->p_spu_vout != p_vout )
     {
+        vlc_mutex_lock( &p_owner->lock );
+
+        DecoderFlushBuffering( p_dec );
+
+        vlc_mutex_unlock( &p_owner->lock );
+
         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
                      &p_owner->i_spu_channel );
         p_owner->i_spu_order = 0;
@@ -1847,7 +2425,7 @@ static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
     vout_thread_t *p_vout = NULL;
 
-    p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+    p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
     if( !p_vout || p_owner->p_spu_vout != p_vout )
     {
         if( p_vout )