Fixed aout visual vout recycling.
[vlc.git] / src / input / decoder.c
index 7043b4c..bcae5fe 100644 (file)
@@ -39,6 +39,7 @@
 #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"
@@ -46,6 +47,8 @@
 #include "input_internal.h"
 #include "clock.h"
 #include "decoder.h"
+#include "event.h"
+#include "ressource.h"
 
 #include "../video_output/vout_control.h"
 
@@ -53,7 +56,8 @@ static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_inst
 static void       DeleteDecoder( decoder_t * );
 
 static void      *DecoderThread( vlc_object_t * );
-static int        DecoderProcess( decoder_t *, block_t * );
+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 );
@@ -98,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;
 
@@ -137,6 +146,9 @@ struct decoder_owner_sys_t
 
         aout_buffer_t *p_audio;
         aout_buffer_t **pp_audio_next;
+
+        block_t       *p_block;
+        block_t       **pp_block_next;
     } buffer;
 
     /* Flushing */
@@ -158,9 +170,59 @@ struct decoder_owner_sys_t
 #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))
+
+/* */
+#define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
+
+
 /*****************************************************************************
  * Public functions
  *****************************************************************************/
+picture_t *decoder_NewPicture( decoder_t *p_decoder )
+{
+    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 );
+}
+
+aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
+{
+    if( !p_decoder->pf_aout_buffer_new )
+        return NULL;
+    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:
  */
@@ -250,8 +312,7 @@ decoder_t *input_DecoderNew( input_thread_t *p_input,
         i_priority = VLC_THREAD_PRIORITY_VIDEO;
 
     /* Spawn the decoder thread */
-    if( vlc_thread_create( p_dec, "decoder", DecoderThread,
-                           i_priority, false ) )
+    if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
     {
         msg_Err( p_dec, "cannot spawn decoder thread" );
         module_unneed( p_dec, p_dec->p_module );
@@ -276,17 +337,18 @@ void input_DecoderDelete( decoder_t *p_dec )
 
     vlc_object_kill( p_dec );
 
-    /* Make sure we aren't paused anymore */
+    /* Make sure we aren't paused/buffering/waiting 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 );
-    }
+    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 );
 
     vlc_thread_join( p_dec );
+    p_owner->b_paused = b_was_paused;
+
     module_unneed( p_dec, p_dec->p_module );
 
     /* */
@@ -317,7 +379,12 @@ void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
 
     if( p_owner->p_input->p->b_out_pace_control )
     {
-        block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
+        /* 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 if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
     {
@@ -463,7 +530,8 @@ void input_DecoderStartBuffering( decoder_t *p_dec )
     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 );
+    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;
@@ -474,6 +542,10 @@ void input_DecoderStartBuffering( decoder_t *p_dec )
     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 );
@@ -508,6 +580,7 @@ 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;
@@ -532,6 +605,34 @@ void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
     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
  *****************************************************************************/
@@ -554,7 +655,10 @@ static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
     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 );
+    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 )
 {
@@ -602,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 )
@@ -655,9 +761,9 @@ static decoder_t * CreateDecoder( input_thread_t *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 &&
@@ -677,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 )
             {
@@ -710,6 +816,10 @@ 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;
@@ -721,6 +831,7 @@ static decoder_t * CreateDecoder( input_thread_t *p_input,
     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;
 
@@ -754,27 +865,26 @@ static void *DecoderThread( vlc_object_t *p_this )
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
 
     /* The decoder's main loop */
-    for (;;)
+    for( ;; )
     {
         block_t *p_block = block_FifoGet( p_owner->p_fifo );
+
         /* 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 );
 
         if( p_block )
         {
+            int canc = vlc_savecancel();
+
             if( p_dec->b_error )
-            {   /* Trash all received PES packets */
-                block_Release( p_block );
-            }
-            else if( DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
-            {
-                break;
-            }
+                DecoderError( p_dec, p_block );
+            else
+                DecoderProcess( p_dec, p_block );
+
+            vlc_restorecancel( canc );
         }
     }
-
-    DecoderSignalBuffering( p_dec, true );
     return NULL;
 }
 
@@ -916,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();
 
@@ -925,41 +1035,42 @@ 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 DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
-                          mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
+                          mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
 {
     decoder_owner_sys_t *p_owner = p_dec->p_owner;
     input_clock_t   *p_clock = p_owner->p_clock;
-    int i_rate = 0;
 
     vlc_assert_locked( &p_owner->lock );
 
-    const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
     const mtime_t i_es_delay = p_owner->i_ts_delay;
 
     if( p_clock )
     {
         const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
+        int i_rate;
 
         if( *pi_ts0 > 0 )
-            *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
-                                         *pi_ts0 + i_es_delay );
-        if( pi_ts1 && *pi_ts1 > 0 )
-            *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
-                                         *pi_ts1 + i_es_delay );
+        {
+            *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 );
+        }
 
         /* Do not create ephemere data because of rounding errors */
         if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
             *pi_ts1 += 1;
 
-        if( i_rate <= 0 )
-            i_rate = input_clock_GetRate( p_clock ); 
-
         if( pi_duration )
             *pi_duration = ( *pi_duration * i_rate +
                                     INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
@@ -969,16 +1080,11 @@ 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 );
+            *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
             if( pi_ts1 && *pi_ts1 <= 0 )
                 *pi_ts1 = *pi_ts0;
         }
     }
-    if( pi_delay )
-    {
-        const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
-        *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
-    }
 }
 
 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
@@ -1042,44 +1148,48 @@ static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
         }
 
         /* */
+        const bool b_dated = p_audio->start_date > 0;
         int i_rate = INPUT_RATE_DEFAULT;
-        mtime_t i_delay;
 
         DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
-                      &i_rate, &i_delay, false );
+                      &i_rate, AOUT_MAX_ADVANCE_TIME, false );
 
         vlc_mutex_unlock( &p_owner->lock );
 
-        /* */
-        const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
-
         if( !p_aout || !p_aout_input ||
-            p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
+            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;
 
-        if( !b_reject )
+        /* 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() )
         {
-            /* Wait if we are too early
-             * FIXME that's plain ugly to do it here */
-            mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
+            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( !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( p_audio->start_date <= 0 )
-            {
+            if( b_dated )
+                msg_Warn( p_aout, "received buffer in the future" );
+            else
                 msg_Warn( p_dec, "non-dated audio buffer received" );
-            }
-            else if( p_audio->start_date > i_max_date )
-            {
-                msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
-                          p_audio->start_date - mdate() );
-            }
+
             *pi_lost_sum += 1;
             aout_BufferFree( p_audio );
         }
@@ -1109,8 +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_lost = 0;
-        int i_played;
 
         if( p_dec->b_die )
         {
@@ -1259,9 +1367,6 @@ 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 );
@@ -1269,19 +1374,17 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
             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 );
         }
+
+        const bool b_dated = p_picture->date > 0;
+        int i_rate = INPUT_RATE_DEFAULT;
         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
-                      &i_rate, &i_delay, false );
+                      &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
 
         vlc_mutex_unlock( &p_owner->lock );
 
         /* */
-        const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
-
-        if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
+        if( !p_picture->b_force && p_picture->date <= 0 )
             b_reject = true;
 
         if( !b_reject )
@@ -1296,15 +1399,11 @@ static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
         }
         else
         {
-            if( p_picture->date <= 0 )
-            {
-                msg_Warn( p_vout, "non-dated video buffer received" );
-            }
+            if( b_dated )
+                msg_Warn( p_vout, "early picture skipped" );
             else
-            {
-                msg_Warn( p_vout, "early picture skipped (%"PRId64")",
-                          p_picture->date - mdate() );
-            }
+                msg_Warn( p_vout, "non-dated video buffer received" );
+
             *pi_lost_sum += 1;
             vout_DropPicture( p_vout, p_picture );
         }
@@ -1447,7 +1546,7 @@ static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
 
         /* */
         DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
-                      NULL, NULL, b_telx );
+                      NULL, INT64_MAX, b_telx );
 
         vlc_mutex_unlock( &p_owner->lock );
 
@@ -1473,18 +1572,69 @@ static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
     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 );
 
-    bool b_reject;
-    DecoderWaitUnblock( p_dec, &b_reject );
+    if( p_owner->b_buffering || p_owner->buffer.p_block )
+    {
+        block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
 
-    DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts, &p_sout_block->i_length,
-                  &p_sout_block->i_rate, NULL, b_telx );
+        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 );
+        }
+    }
 
-    vlc_mutex_unlock( &p_owner->lock );
+    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;
+        }
 
-    sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
+        /* */
+        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;
+        }
+    }
 }
 
 /* */
@@ -1531,6 +1681,14 @@ static void DecoderFlushBuffering( decoder_t *p_dec )
         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;
+    }
 }
 
 /* This function process a block for sout
@@ -1704,7 +1862,7 @@ static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush
         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 );
+        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 */
@@ -1729,7 +1887,7 @@ static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush
 
     if( b_flush && p_owner->p_spu_vout )
     {
-        p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+        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,
@@ -1740,6 +1898,17 @@ static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush
     }
 }
 
+/* */
+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
  *
@@ -1747,7 +1916,7 @@ static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush
  * \param p_block the block to decode
  * \return VLC_SUCCESS or an error code
  */
-static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
+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);
@@ -1756,12 +1925,11 @@ static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
     {
         assert( !b_flush_request );
         block_Release( p_block );
-        return VLC_SUCCESS;
+        return;
     }
 
-    int canc = vlc_savecancel ();
 #ifdef ENABLE_SOUT
-    if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
+    if( vlc_internals( p_dec )->i_object_type == VLC_OBJECT_PACKETIZER )
     {
         if( p_block )
             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
@@ -1804,18 +1972,22 @@ static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
 
     /* */
     if( b_flush_request )
-    {
-        vlc_mutex_lock( &p_owner->lock );
-        DecoderFlushBuffering( p_dec );
-        vlc_mutex_unlock( &p_owner->lock );
+        DecoderProcessOnFlush( p_dec );
+}
 
-        DecoderSignalFlushed( p_dec );
-    }
-    vlc_restorecancel(canc);
+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);
 
-    return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
+    /* */
+    if( p_block )
+        block_Release( p_block );
+
+    if( b_flush_request )
+        DecoderProcessOnFlush( p_dec );
 }
 
+
 /**
  * Destroys a decoder object
  *
@@ -1844,17 +2016,22 @@ static void DeleteDecoder( decoder_t * p_dec )
         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 )
     {
-        /* Hack to make sure all the the pictures are freed by the decoder */
+        /* 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
@@ -1869,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 )
     {
@@ -1887,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 );
     }
@@ -1902,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;
@@ -1933,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;
@@ -1953,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" );
@@ -2062,13 +2292,17 @@ static picture_t *vout_new_buffer( decoder_t *p_dec )
         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" );
@@ -2146,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 )
@@ -2191,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 )