]> git.sesse.net Git - vlc/blobdiff - src/input/es_out.c
es_out: do not start buffering when already buferring
[vlc] / src / input / es_out.c
index 0fc7176070b7af7480acb8d4b7367841e39446e4..b2326435e9923971fbc862425f8078a860a0347c 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * es_out.c: Es Out handler for input.
  *****************************************************************************
- * Copyright (C) 2003-2004 the VideoLAN team
+ * Copyright (C) 2003-2004 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -45,6 +45,7 @@
 #include "es_out.h"
 #include "event.h"
 #include "info.h"
+#include "item.h"
 
 #include "../stream_output/stream_output.h"
 
@@ -113,7 +114,6 @@ struct es_out_sys_t
     /* all programs */
     int           i_pgrm;
     es_out_pgrm_t **pgrm;
-    es_out_pgrm_t **pp_selected_pgrm; /* --programs */
     es_out_pgrm_t *p_pgrm;  /* Master program */
 
     /* all es */
@@ -193,7 +193,7 @@ static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced );
 static char *LanguageGetName( const char *psz_code );
 static char *LanguageGetCode( const char *psz_lang );
 static char **LanguageSplit( const char *psz_langs, bool b_default_any );
-static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang );
+static int LanguageArrayIndex( char **ppsz_langs, const char *psz_lang );
 
 static char *EsOutProgramGetMetaName( es_out_pgrm_t *p_pgrm );
 
@@ -227,7 +227,7 @@ es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
     if( !out )
         return NULL;
 
-    es_out_sys_t *p_sys = malloc( sizeof( *p_sys ) );
+    es_out_sys_t *p_sys = calloc( 1, sizeof( *p_sys ) );
     if( !p_sys )
     {
         free( out );
@@ -249,21 +249,12 @@ es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
 
 
     TAB_INIT( p_sys->i_pgrm, p_sys->pgrm );
-    p_sys->p_pgrm   = NULL;
-
-    p_sys->i_id    = 0;
 
     TAB_INIT( p_sys->i_es, p_sys->es );
 
-    p_sys->i_audio = 0;
-    p_sys->i_video = 0;
-    p_sys->i_sub   = 0;
-
     /* */
     p_sys->i_group_id = var_GetInteger( p_input, "program" );
-
     p_sys->i_audio_last = var_GetInteger( p_input, "audio-track" );
-
     p_sys->i_sub_last = var_GetInteger( p_input, "sub-track" );
 
     p_sys->i_default_sub_id   = -1;
@@ -292,39 +283,18 @@ es_out_t *input_EsOutNew( input_thread_t *p_input, int i_rate )
         }
         free( psz_string );
     }
-    else
-    {
-        p_sys->ppsz_sub_language = NULL;
-        p_sys->ppsz_audio_language = NULL;
-    }
 
     p_sys->i_audio_id = var_GetInteger( p_input, "audio-track-id" );
 
     p_sys->i_sub_id = var_GetInteger( p_input, "sub-track-id" );
 
-    p_sys->p_es_audio = NULL;
-    p_sys->p_es_video = NULL;
-    p_sys->p_es_sub   = NULL;
-
-    p_sys->i_audio_delay= 0;
-    p_sys->i_spu_delay  = 0;
-
-    p_sys->b_paused = false;
     p_sys->i_pause_date = -1;
 
     p_sys->i_rate = i_rate;
-    p_sys->i_pts_delay = 0;
-    p_sys->i_pts_jitter = 0;
-    p_sys->i_cr_average = 0;
 
     p_sys->b_buffering = true;
-    p_sys->i_buffering_extra_initial = 0;
-    p_sys->i_buffering_extra_stream = 0;
-    p_sys->i_buffering_extra_system = 0;
     p_sys->i_preroll_end = -1;
 
-    p_sys->p_sout_record = NULL;
-
     return out;
 }
 
@@ -405,7 +375,8 @@ static mtime_t EsOutGetWakeup( es_out_t *out )
     /* We do not have a wake up date if the input cannot have its speed
      * controlled or sout is imposing its own or while buffering
      *
-     * FIXME for !p_input->p->b_can_pace_control a wkeup time is still needed to avoid too strong buffering */
+     * FIXME for !p_input->p->b_can_pace_control a wake-up time is still needed
+     * to avoid too heavy buffering */
     if( !p_input->p->b_can_pace_control ||
         p_input->p->b_out_pace_control ||
         p_sys->b_buffering )
@@ -416,14 +387,13 @@ static mtime_t EsOutGetWakeup( es_out_t *out )
 
 static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
 {
-    int i;
     if( i_id < 0 )
     {
         /* Special HACK, -i_id is the cat of the stream */
         return (es_out_id_t*)((uint8_t*)NULL-i_id);
     }
 
-    for( i = 0; i < out->p_sys->i_es; i++ )
+    for( int i = 0; i < out->p_sys->i_es; i++ )
     {
         if( out->p_sys->es[i]->i_id == i_id )
             return out->p_sys->es[i];
@@ -434,7 +404,6 @@ static es_out_id_t *EsOutGetFromID( es_out_t *out, int i_id )
 static bool EsOutDecodersIsEmpty( es_out_t *out )
 {
     es_out_sys_t      *p_sys = out->p_sys;
-    int i;
 
     if( p_sys->b_buffering && p_sys->p_pgrm )
     {
@@ -443,7 +412,7 @@ static bool EsOutDecodersIsEmpty( es_out_t *out )
             return true;
     }
 
-    for( i = 0; i < p_sys->i_es; i++ )
+    for( int i = 0; i < p_sys->i_es; i++ )
     {
         es_out_id_t *es = p_sys->es[i];
 
@@ -492,7 +461,7 @@ static int EsOutSetRecord(  es_out_t *out, bool b_record )
 
         if( !psz_sout && psz_path )
         {
-            char *psz_file = input_CreateFilename( VLC_OBJECT(p_input), psz_path, INPUT_RECORD_PREFIX, NULL );
+            char *psz_file = input_CreateFilename( p_input, psz_path, INPUT_RECORD_PREFIX, NULL );
             if( psz_file )
             {
                 if( asprintf( &psz_sout, "#record{dst-prefix='%s'}", psz_file ) < 0 )
@@ -522,7 +491,7 @@ static int EsOutSetRecord(  es_out_t *out, bool b_record )
 
             p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
             if( p_es->p_dec_record && p_sys->b_buffering )
-                input_DecoderStartBuffering( p_es->p_dec_record );
+                input_DecoderStartWait( p_es->p_dec_record );
         }
     }
     else
@@ -604,13 +573,16 @@ static void EsOutChangePosition( es_out_t *out )
     {
         es_out_id_t *p_es = p_sys->es[i];
 
-        if( !p_es->p_dec )
-            continue;
-
-        input_DecoderStartBuffering( p_es->p_dec );
-
-        if( p_es->p_dec_record )
-            input_DecoderStartBuffering( p_es->p_dec_record );
+        if( p_es->p_dec != NULL )
+        {
+            input_DecoderFlush( p_es->p_dec );
+            if( !p_sys->b_buffering )
+            {
+                input_DecoderStartWait( p_es->p_dec );
+                if( p_es->p_dec_record != NULL )
+                    input_DecoderStartWait( p_es->p_dec_record );
+            }
+        }
     }
 
     for( int i = 0; i < p_sys->i_pgrm; i++ )
@@ -628,17 +600,14 @@ static void EsOutChangePosition( es_out_t *out )
 static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
 {
     es_out_sys_t *p_sys = out->p_sys;
-    int i_ret;
 
     mtime_t i_stream_start;
     mtime_t i_system_start;
     mtime_t i_stream_duration;
     mtime_t i_system_duration;
-    i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
+    if (input_clock_GetState( p_sys->p_pgrm->p_clock,
                                   &i_stream_start, &i_system_start,
-                                  &i_stream_duration, &i_system_duration );
-    assert( !i_ret || b_forced );
-    if( i_ret )
+                                  &i_stream_duration, &i_system_duration ))
         return;
 
     mtime_t i_preroll_duration = 0;
@@ -651,7 +620,11 @@ static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
 
     if( i_stream_duration <= i_buffering_duration && !b_forced )
     {
-        const double f_level = __MAX( (double)i_stream_duration / i_buffering_duration, 0 );
+        double f_level;
+        if (i_buffering_duration == 0)
+            f_level = 0;
+        else
+            f_level = __MAX( (double)i_stream_duration / i_buffering_duration, 0 );
         input_SendEventCache( p_sys->p_input, f_level );
 
         msg_Dbg( p_sys->p_input, "Buffering %d%%", (int)(100 * f_level) );
@@ -677,12 +650,12 @@ static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
 
         if( !p_es->p_dec || p_es->fmt.i_cat == SPU_ES )
             continue;
-        input_DecoderWaitBuffering( p_es->p_dec );
+        input_DecoderWait( p_es->p_dec );
         if( p_es->p_dec_record )
-            input_DecoderWaitBuffering( p_es->p_dec_record );
+            input_DecoderWait( p_es->p_dec_record );
     }
 
-    msg_Dbg( p_sys->p_input, "Decoder buffering done in %d ms",
+    msg_Dbg( p_sys->p_input, "Decoder wait done in %d ms",
               (int)(mdate() - i_decoder_buffering_start)/1000 );
 
     /* Here is a good place to destroy unused vout with every demuxer */
@@ -702,9 +675,9 @@ static void EsOutDecodersStopBuffering( es_out_t *out, bool b_forced )
         if( !p_es->p_dec )
             continue;
 
-        input_DecoderStopBuffering( p_es->p_dec );
+        input_DecoderStopWait( p_es->p_dec );
         if( p_es->p_dec_record )
-            input_DecoderStopBuffering( p_es->p_dec_record );
+            input_DecoderStopWait( p_es->p_dec_record );
     }
 }
 static void EsOutDecodersChangePause( es_out_t *out, bool b_paused, mtime_t i_date )
@@ -767,14 +740,13 @@ static void EsOutDecoderChangeDelay( es_out_t *out, es_out_id_t *p_es )
         i_delay = p_sys->i_audio_delay;
     else if( p_es->fmt.i_cat == SPU_ES )
         i_delay = p_sys->i_spu_delay;
+    else
+        return;
 
-    if( i_delay != 0 )
-    {
-        if( p_es->p_dec )
-            input_DecoderChangeDelay( p_es->p_dec, i_delay );
-        if( p_es->p_dec_record )
-            input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
-    }
+    if( p_es->p_dec )
+        input_DecoderChangeDelay( p_es->p_dec, i_delay );
+    if( p_es->p_dec_record )
+        input_DecoderChangeDelay( p_es->p_dec_record, i_delay );
 }
 static void EsOutProgramsChangeRate( es_out_t *out )
 {
@@ -855,21 +827,19 @@ static void EsOutFrameNext( es_out_t *out )
 static mtime_t EsOutGetBuffering( es_out_t *out )
 {
     es_out_sys_t *p_sys = out->p_sys;
+    mtime_t i_stream_duration, i_system_start;
 
     if( !p_sys->p_pgrm )
         return 0;
+    else
+    {
+        mtime_t i_stream_start, i_system_duration;
 
-    int i_ret;
-    mtime_t i_stream_start;
-    mtime_t i_system_start;
-    mtime_t i_stream_duration;
-    mtime_t i_system_duration;
-    i_ret = input_clock_GetState( p_sys->p_pgrm->p_clock,
+        if( input_clock_GetState( p_sys->p_pgrm->p_clock,
                                   &i_stream_start, &i_system_start,
-                                  &i_stream_duration, &i_system_duration );
-
-    if( i_ret )
-        return 0;
+                                  &i_stream_duration, &i_system_duration ) )
+            return 0;
+    }
 
     mtime_t i_delay;
 
@@ -880,6 +850,7 @@ static mtime_t EsOutGetBuffering( es_out_t *out )
     else
     {
         mtime_t i_system_duration;
+
         if( p_sys->b_paused )
         {
             i_system_duration = p_sys->i_pause_date  - i_system_start;
@@ -1044,7 +1015,7 @@ static void EsOutProgramSelect( es_out_t *out, es_out_pgrm_t *p_pgrm )
     }
 
     /* Update now playing */
-    input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
+    input_item_SetESNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
     input_item_SetPublisher( p_input->p->p_item, p_pgrm->psz_publisher );
 
     input_SendEventMeta( p_input );
@@ -1186,7 +1157,7 @@ static void EsOutProgramMeta( es_out_t *out, int i_group, const vlc_meta_t *p_me
 
     /* Check against empty meta data (empty for what we handle) */
     if( !vlc_meta_Get( p_meta, vlc_meta_Title) &&
-        !vlc_meta_Get( p_meta, vlc_meta_NowPlaying) &&
+        !vlc_meta_Get( p_meta, vlc_meta_ESNowPlaying) &&
         !vlc_meta_Get( p_meta, vlc_meta_Publisher) &&
         vlc_meta_GetExtraCount( p_meta ) <= 0 )
     {
@@ -1325,20 +1296,20 @@ static void EsOutProgramEpg( es_out_t *out, int i_group, const vlc_epg_t *p_epg
 
     if( p_pgrm == p_sys->p_pgrm )
     {
-        input_item_SetNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
+        input_item_SetESNowPlaying( p_input->p->p_item, p_pgrm->psz_now_playing );
         input_SendEventMeta( p_input );
     }
 
     if( p_pgrm->psz_now_playing )
     {
         input_Control( p_input, INPUT_ADD_INFO, psz_cat,
-            vlc_meta_TypeToLocalizedString(vlc_meta_NowPlaying),
+            vlc_meta_TypeToLocalizedString(vlc_meta_ESNowPlaying), "%s",
             p_pgrm->psz_now_playing );
     }
     else
     {
         input_Control( p_input, INPUT_DEL_INFO, psz_cat,
-            vlc_meta_TypeToLocalizedString(vlc_meta_NowPlaying) );
+            vlc_meta_TypeToLocalizedString(vlc_meta_ESNowPlaying) );
     }
 
     free( psz_cat );
@@ -1376,48 +1347,34 @@ static void EsOutMeta( es_out_t *p_out, const vlc_meta_t *p_meta )
 {
     es_out_sys_t    *p_sys = p_out->p_sys;
     input_thread_t  *p_input = p_sys->p_input;
-
     input_item_t *p_item = input_GetItem( p_input );
 
-    char *psz_title = NULL;
-    char *psz_arturl = input_item_GetArtURL( p_item );
+    if( vlc_meta_Get( p_meta, vlc_meta_Title ) != NULL )
+        input_item_SetName( p_item, vlc_meta_Get( p_meta, vlc_meta_Title ) );
 
-    vlc_mutex_lock( &p_item->lock );
-
-    if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
-        psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
+    char *psz_arturl = NULL;
+    if( vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL ) != NULL )
+        psz_arturl = input_item_GetArtURL( p_item ); /* save value */
 
+    vlc_mutex_lock( &p_item->lock );
     vlc_meta_Merge( p_item->p_meta, p_meta );
-
-    if( !psz_arturl || *psz_arturl == '\0' )
-    {
-        const char *psz_tmp = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
-        if( psz_tmp )
-            psz_arturl = strdup( psz_tmp );
-    }
     vlc_mutex_unlock( &p_item->lock );
 
-    if( psz_arturl && *psz_arturl )
-    {
+    if( psz_arturl != NULL ) /* restore/favor previously set item art URL */
         input_item_SetArtURL( p_item, psz_arturl );
+    else
+        psz_arturl = input_item_GetArtURL( p_item );
 
-        if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
-        {
-            /* Don't look for art cover if sout
-             * XXX It can change when sout has meta data support */
-            if( p_input->p->p_sout && !p_input->b_preparsing )
-                input_item_SetArtURL( p_item, "" );
-            else
-                input_ExtractAttachmentAndCacheArt( p_input );
-        }
+    if( psz_arturl != NULL && !strncmp( psz_arturl, "attachment://", 13 ) )
+    {   /* Clear art cover if streaming out.
+         * FIXME: Why? Remove this when sout gets meta data support. */
+        if( p_input->p->p_sout && !p_input->b_preparsing )
+            input_item_SetArtURL( p_item, NULL );
+        else
+            input_ExtractAttachmentAndCacheArt( p_input, psz_arturl + 13 );
     }
     free( psz_arturl );
 
-    if( psz_title )
-    {
-        input_item_SetName( p_item, psz_title );
-        free( psz_title );
-    }
     input_item_SetPreparsed( p_item, true );
 
     input_SendEventMeta( p_input );
@@ -1590,13 +1547,13 @@ static void EsCreateDecoder( es_out_t *out, es_out_id_t *p_es )
     if( p_es->p_dec )
     {
         if( p_sys->b_buffering )
-            input_DecoderStartBuffering( p_es->p_dec );
+            input_DecoderStartWait( p_es->p_dec );
 
         if( !p_es->p_master && p_sys->p_sout_record )
         {
             p_es->p_dec_record = input_DecoderNew( p_input, &p_es->fmt, p_es->p_pgrm->p_clock, p_sys->p_sout_record );
             if( p_es->p_dec_record && p_sys->b_buffering )
-                input_DecoderStartBuffering( p_es->p_dec_record );
+                input_DecoderStartWait( p_es->p_dec_record );
         }
     }
 
@@ -1748,7 +1705,7 @@ static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
     int i_cat = es->fmt.i_cat;
 
     if( !p_sys->b_active ||
-        ( !b_force && es->fmt.i_priority < 0 ) )
+        ( !b_force && es->fmt.i_priority < ES_PRIORITY_SELECTABLE_MIN ) )
     {
         return;
     }
@@ -1788,28 +1745,34 @@ static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
 
         if( i_cat == AUDIO_ES )
         {
-            int idx1 = LanguageArrayIndex( p_sys->ppsz_audio_language,
-                                     es->psz_language_code );
-
-            if( p_sys->p_es_audio &&
-                p_sys->p_es_audio->fmt.i_priority >= es->fmt.i_priority )
+            if( p_sys->ppsz_audio_language )
             {
-                int idx2 = LanguageArrayIndex( p_sys->ppsz_audio_language,
-                                         p_sys->p_es_audio->psz_language_code );
-
-                if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
-                    return;
-                i_wanted = es->i_channel;
+                int es_idx = LanguageArrayIndex( p_sys->ppsz_audio_language,
+                                                 es->psz_language_code );
+                if( !p_sys->p_es_audio )
+                {
+                    /* Only select the language if it's in the list */
+                    if( es_idx >= 0 )
+                        i_wanted = es->i_channel;
+                }
+                else
+                {
+                    int selected_es_idx =
+                        LanguageArrayIndex( p_sys->ppsz_audio_language,
+                                            p_sys->p_es_audio->psz_language_code );
+                    if( es_idx >= 0 &&
+                        ( selected_es_idx < 0 || es_idx < selected_es_idx ||
+                          ( es_idx == selected_es_idx &&
+                            p_sys->p_es_audio->fmt.i_priority < es->fmt.i_priority ) ) )
+                        i_wanted = es->i_channel;
+                }
             }
             else
             {
-                /* Select audio if (no audio selected yet)
-                 * - no audio-language
-                 * - no audio code for the ES
-                 * - audio code in the requested list */
-                if( idx1 >= 0 ||
-                    !strcmp( es->psz_language_code, "??" ) ||
-                    !p_sys->ppsz_audio_language )
+                /* Select the first one if there is no selected audio yet 
+                 * then choose by ES priority */
+                if( !p_sys->p_es_audio ||
+                    p_sys->p_es_audio->fmt.i_priority < es->fmt.i_priority )
                     i_wanted = es->i_channel;
             }
 
@@ -1826,35 +1789,58 @@ static void EsOutSelect( es_out_t *out, es_out_id_t *es, bool b_force )
         }
         else if( i_cat == SPU_ES )
         {
-            int idx1 = LanguageArrayIndex( p_sys->ppsz_sub_language,
-                                     es->psz_language_code );
-
-            if( p_sys->p_es_sub &&
-                p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
+            if( p_sys->ppsz_sub_language )
             {
-                int idx2 = LanguageArrayIndex( p_sys->ppsz_sub_language,
-                                         p_sys->p_es_sub->psz_language_code );
-
-                msg_Dbg( p_sys->p_input, "idx1=%d(%s) idx2=%d(%s)",
-                        idx1, es->psz_language_code, idx2,
-                        p_sys->p_es_sub->psz_language_code );
-
-                if( idx1 < 0 || ( idx2 >= 0 && idx2 <= idx1 ) )
-                    return;
-                /* We found a SPU that matches our language request */
-                i_wanted  = es->i_channel;
+                int es_idx = LanguageArrayIndex( p_sys->ppsz_sub_language,
+                                     es->psz_language_code );
+                if( !p_sys->p_es_sub )
+                {
+                    /* Select the language if it's in the list */
+                    if( es_idx >= 0 ||
+                        /*FIXME: Should default subtitle not in the list be 
+                         * displayed if not forbidden by none? */
+                        ( p_sys->i_default_sub_id >= 0 &&
+                          /* check if the subtitle isn't forbidden by none */
+                          LanguageArrayIndex( p_sys->ppsz_sub_language, "none" ) < 0 &&
+                          es->i_id == p_sys->i_default_sub_id ) )
+                        i_wanted = es->i_channel;
+                }
+                else
+                {
+                    int selected_es_idx =
+                        LanguageArrayIndex( p_sys->ppsz_sub_language,
+                                            p_sys->p_es_sub->psz_language_code );
+
+                    if( es_idx >= 0 &&
+                        ( selected_es_idx < 0 || es_idx < selected_es_idx ||
+                          ( es_idx == selected_es_idx &&
+                            p_sys->p_es_sub->fmt.i_priority < es->fmt.i_priority ) ) )
+                        i_wanted = es->i_channel;
+                }
             }
-            else if( idx1 >= 0 )
+            else if ( es->fmt.i_codec == EsOutFourccClosedCaptions[0] ||
+                      es->fmt.i_codec == EsOutFourccClosedCaptions[1] ||
+                      es->fmt.i_codec == EsOutFourccClosedCaptions[2] ||
+                      es->fmt.i_codec == EsOutFourccClosedCaptions[3])
             {
-                msg_Dbg( p_sys->p_input, "idx1=%d(%s)",
-                        idx1, es->psz_language_code );
-
-                i_wanted  = es->i_channel;
+                    /* We don't want to enable on initial create since p_master
+                       isn't set yet (otherwise we will think it's a standard
+                       ES_SUB stream and cause a resource leak) */
+                    return;
             }
-            else if( p_sys->i_default_sub_id >= 0 )
+            else
             {
-                if( es->i_id == p_sys->i_default_sub_id )
+                /* If there is no user preference, select the default subtitle 
+                 * or adapt by ES priority */
+                if( ( !p_sys->p_es_sub &&
+                      ( p_sys->i_default_sub_id >= 0 &&
+                        es->i_id == p_sys->i_default_sub_id ) ) ||
+                    ( p_sys->p_es_sub && 
+                      p_sys->p_es_sub->fmt.i_priority < es->fmt.i_priority ) )
                     i_wanted = es->i_channel;
+                else if( p_sys->p_es_sub &&
+                         p_sys->p_es_sub->fmt.i_priority >= es->fmt.i_priority )
+                    i_wanted = p_sys->p_es_sub->i_channel;
             }
 
             if( p_sys->i_sub_last >= 0 )
@@ -1920,27 +1906,25 @@ static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
 {
     es_out_sys_t   *p_sys = out->p_sys;
     input_thread_t *p_input = p_sys->p_input;
-    int i_total = 0;
 
     if( libvlc_stats( p_input ) )
     {
+        uint64_t i_total;
+
         vlc_mutex_lock( &p_input->p->counters.counters_lock );
-        stats_UpdateInteger( p_input, p_input->p->counters.p_demux_read,
-                             p_block->i_buffer, &i_total );
-        stats_UpdateFloat( p_input , p_input->p->counters.p_demux_bitrate,
-                           (float)i_total, NULL );
+        stats_Update( p_input->p->counters.p_demux_read,
+                      p_block->i_buffer, &i_total );
+        stats_Update( p_input->p->counters.p_demux_bitrate, i_total, NULL );
 
         /* Update number of corrupted data packats */
         if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
         {
-            stats_UpdateInteger( p_input, p_input->p->counters.p_demux_corrupted,
-                                 1, NULL );
+            stats_Update( p_input->p->counters.p_demux_corrupted, 1, NULL );
         }
         /* Update number of discontinuities */
         if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
         {
-            stats_UpdateInteger( p_input, p_input->p->counters.p_demux_discontinuity,
-                                 1, NULL );
+            stats_Update( p_input->p->counters.p_demux_discontinuity, 1, NULL );
         }
         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
     }
@@ -1958,8 +1942,6 @@ static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
             p_block->i_flags |= BLOCK_FLAG_PREROLL;
     }
 
-    p_block->i_rate = 0;
-
     if( !es->p_dec )
     {
         block_Release( p_block );
@@ -2030,6 +2012,10 @@ static int EsOutSend( es_out_t *out, es_out_id_t *es, block_t *p_block )
 
         /* */
         es->pb_cc_present[i] = true;
+
+        /* Enable if user specified on command line */
+        if (p_sys->i_sub_last == i)
+            EsOutSelect(out, es->pp_cc_es[i], true);
     }
 
     vlc_mutex_unlock( &p_sys->lock );
@@ -2051,7 +2037,7 @@ static void EsOutDel( es_out_t *out, es_out_id_t *es )
     /* We don't try to reselect */
     if( es->p_dec )
     {
-        while( !p_sys->p_input->b_die && !p_sys->b_buffering && es->p_dec )
+        while( vlc_object_alive(p_sys->p_input) && !p_sys->b_buffering )
         {
             if( input_DecoderIsEmpty( es->p_dec ) &&
                 ( !es->p_dec_record || input_DecoderIsEmpty( es->p_dec_record ) ))
@@ -2131,566 +2117,589 @@ static int EsOutControlLocked( es_out_t *out, int i_query, va_list args )
 
     switch( i_query )
     {
-        case ES_OUT_SET_ES_STATE:
+    case ES_OUT_SET_ES_STATE:
+    {
+        es_out_id_t *es = va_arg( args, es_out_id_t * );
+        bool b = va_arg( args, int );
+        if( b && !EsIsSelected( es ) )
         {
-            es_out_id_t *es = va_arg( args, es_out_id_t * );
-            bool b = va_arg( args, int );
-            if( b && !EsIsSelected( es ) )
-            {
-                EsSelect( out, es );
-                return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
-            }
-            else if( !b && EsIsSelected( es ) )
-            {
-                EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
-                return VLC_SUCCESS;
-            }
-            return VLC_SUCCESS;
+            EsSelect( out, es );
+            return EsIsSelected( es ) ? VLC_SUCCESS : VLC_EGENERIC;
         }
-
-        case ES_OUT_GET_ES_STATE:
+        else if( !b && EsIsSelected( es ) )
         {
-            es_out_id_t *es = va_arg( args, es_out_id_t * );
-            bool *pb = va_arg( args, bool * );
-
-            *pb = EsIsSelected( es );
+            EsUnselect( out, es, es->p_pgrm == p_sys->p_pgrm );
             return VLC_SUCCESS;
         }
+        return VLC_SUCCESS;
+    }
 
-        case ES_OUT_GET_GROUP_FORCED:
-        {
-            int *pi_group = va_arg( args, int * );
-            *pi_group = p_sys->i_group_id;
-            return VLC_SUCCESS;
-        }
+    case ES_OUT_GET_ES_STATE:
+    {
+        es_out_id_t *es = va_arg( args, es_out_id_t * );
+        bool *pb = va_arg( args, bool * );
 
-        case ES_OUT_SET_MODE:
-        {
-            const int i_mode = va_arg( args, int );
-            assert( i_mode == ES_OUT_MODE_NONE || i_mode == ES_OUT_MODE_ALL ||
-                    i_mode == ES_OUT_MODE_AUTO || i_mode == ES_OUT_MODE_PARTIAL ||
-                    i_mode == ES_OUT_MODE_END );
+        *pb = EsIsSelected( es );
+        return VLC_SUCCESS;
+    }
 
-            if( i_mode != ES_OUT_MODE_NONE && !p_sys->b_active && p_sys->i_es > 0 )
-            {
-                /* XXX Terminate vout if there are tracks but no video one.
-                 * This one is not mandatory but is he earliest place where it
-                 * can be done */
-                int i;
-                for( i = 0; i < p_sys->i_es; i++ )
-                {
-                    es_out_id_t *p_es = p_sys->es[i];
-                    if( p_es->fmt.i_cat == VIDEO_ES )
-                        break;
-                }
-                if( i >= p_sys->i_es )
-                    input_resource_TerminateVout( p_sys->p_input->p->p_resource );
-            }
-            p_sys->b_active = i_mode != ES_OUT_MODE_NONE;
-            p_sys->i_mode = i_mode;
+    case ES_OUT_GET_GROUP_FORCED:
+    {
+        int *pi_group = va_arg( args, int * );
+        *pi_group = p_sys->i_group_id;
+        return VLC_SUCCESS;
+    }
 
-            /* Reapply policy mode */
-            for( int i = 0; i < p_sys->i_es; i++ )
+    case ES_OUT_SET_MODE:
+    {
+        const int i_mode = va_arg( args, int );
+        assert( i_mode == ES_OUT_MODE_NONE || i_mode == ES_OUT_MODE_ALL ||
+                i_mode == ES_OUT_MODE_AUTO || i_mode == ES_OUT_MODE_PARTIAL ||
+                i_mode == ES_OUT_MODE_END );
+
+        if( i_mode != ES_OUT_MODE_NONE && !p_sys->b_active && p_sys->i_es > 0 )
+        {
+            /* XXX Terminate vout if there are tracks but no video one.
+             * This one is not mandatory but is he earliest place where it
+             * can be done */
+            int i;
+            for( i = 0; i < p_sys->i_es; i++ )
             {
-                if( EsIsSelected( p_sys->es[i] ) )
-                    EsUnselect( out, p_sys->es[i],
-                                p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
+                es_out_id_t *p_es = p_sys->es[i];
+                if( p_es->fmt.i_cat == VIDEO_ES )
+                    break;
             }
-            for( int i = 0; i < p_sys->i_es; i++ )
-                EsOutSelect( out, p_sys->es[i], false );
-            if( i_mode == ES_OUT_MODE_END )
-                EsOutTerminate( out );
-            return VLC_SUCCESS;
+            if( i >= p_sys->i_es )
+                input_resource_TerminateVout( p_sys->p_input->p->p_resource );
         }
+        p_sys->b_active = i_mode != ES_OUT_MODE_NONE;
+        p_sys->i_mode = i_mode;
 
-        case ES_OUT_SET_ES:
-        case ES_OUT_RESTART_ES:
+        /* Reapply policy mode */
+        for( int i = 0; i < p_sys->i_es; i++ )
         {
-            es_out_id_t *es = va_arg( args, es_out_id_t * );
+            if( EsIsSelected( p_sys->es[i] ) )
+                EsUnselect( out, p_sys->es[i],
+                            p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
+        }
+        for( int i = 0; i < p_sys->i_es; i++ )
+            EsOutSelect( out, p_sys->es[i], false );
+        if( i_mode == ES_OUT_MODE_END )
+            EsOutTerminate( out );
+        return VLC_SUCCESS;
+    }
 
-            int i_cat;
-            if( es == NULL )
-                i_cat = UNKNOWN_ES;
-            else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
-                i_cat = AUDIO_ES;
-            else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
-                i_cat = VIDEO_ES;
-            else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
-                i_cat = SPU_ES;
-            else
-                i_cat = -1;
+    case ES_OUT_SET_ES:
+    case ES_OUT_RESTART_ES:
+    {
+        es_out_id_t *es = va_arg( args, es_out_id_t * );
+
+        int i_cat;
+        if( es == NULL )
+            i_cat = UNKNOWN_ES;
+        else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
+            i_cat = AUDIO_ES;
+        else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
+            i_cat = VIDEO_ES;
+        else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
+            i_cat = SPU_ES;
+        else
+            i_cat = -1;
 
-            for( int i = 0; i < p_sys->i_es; i++ )
+        for( int i = 0; i < p_sys->i_es; i++ )
+        {
+            if( i_cat == -1 )
             {
-                if( i_cat == -1 )
+                if( es == p_sys->es[i] )
                 {
-                    if( es == p_sys->es[i] )
-                    {
-                        EsOutSelect( out, es, true );
-                        break;
-                    }
+                    EsOutSelect( out, es, true );
+                    break;
                 }
-                else
+            }
+            else
+            {
+                if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
                 {
-                    if( i_cat == UNKNOWN_ES || p_sys->es[i]->fmt.i_cat == i_cat )
+                    if( EsIsSelected( p_sys->es[i] ) )
                     {
-                        if( EsIsSelected( p_sys->es[i] ) )
+                        if( i_query == ES_OUT_RESTART_ES )
                         {
-                            if( i_query == ES_OUT_RESTART_ES )
+                            if( p_sys->es[i]->p_dec )
                             {
-                                if( p_sys->es[i]->p_dec )
-                                {
-                                    EsDestroyDecoder( out, p_sys->es[i] );
-                                    EsCreateDecoder( out, p_sys->es[i] );
-                                }
-                            }
-                            else
-                            {
-                                EsUnselect( out, p_sys->es[i],
-                                            p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
+                                EsDestroyDecoder( out, p_sys->es[i] );
+                                EsCreateDecoder( out, p_sys->es[i] );
                             }
                         }
+                        else
+                        {
+                            EsUnselect( out, p_sys->es[i],
+                                        p_sys->es[i]->p_pgrm == p_sys->p_pgrm );
+                        }
                     }
                 }
             }
-            return VLC_SUCCESS;
         }
-        case ES_OUT_SET_ES_DEFAULT:
-        {
-            es_out_id_t *es = va_arg( args, es_out_id_t * );
+        return VLC_SUCCESS;
+    }
 
-            if( es == NULL )
-            {
-                /*p_sys->i_default_video_id = -1;*/
-                /*p_sys->i_default_audio_id = -1;*/
-                p_sys->i_default_sub_id = -1;
-            }
-            else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
-            {
-                /*p_sys->i_default_video_id = -1;*/
-            }
-            else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
-            {
-                /*p_sys->i_default_audio_id = -1;*/
-            }
-            else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
-            {
-                p_sys->i_default_sub_id = -1;
-            }
+    case ES_OUT_SET_ES_DEFAULT:
+    {
+        es_out_id_t *es = va_arg( args, es_out_id_t * );
+
+        if( es == NULL )
+        {
+            /*p_sys->i_default_video_id = -1;*/
+            /*p_sys->i_default_audio_id = -1;*/
+            p_sys->i_default_sub_id = -1;
+        }
+        else if( es == (es_out_id_t*)((uint8_t*)NULL+AUDIO_ES) )
+        {
+            /*p_sys->i_default_video_id = -1;*/
+        }
+        else if( es == (es_out_id_t*)((uint8_t*)NULL+VIDEO_ES) )
+        {
+            /*p_sys->i_default_audio_id = -1;*/
+        }
+        else if( es == (es_out_id_t*)((uint8_t*)NULL+SPU_ES) )
+        {
+            p_sys->i_default_sub_id = -1;
+        }
+        else
+        {
+            /*if( es->fmt.i_cat == VIDEO_ES )
+                p_sys->i_default_video_id = es->i_id;
             else
-            {
-                /*if( es->fmt.i_cat == VIDEO_ES )
-                    p_sys->i_default_video_id = es->i_id;
-                else
-                if( es->fmt.i_cat == AUDIO_ES )
-                    p_sys->i_default_audio_id = es->i_id;
-                else*/
-                if( es->fmt.i_cat == SPU_ES )
-                    p_sys->i_default_sub_id = es->i_id;
-            }
-            return VLC_SUCCESS;
+            if( es->fmt.i_cat == AUDIO_ES )
+                p_sys->i_default_audio_id = es->i_id;
+            else*/
+            if( es->fmt.i_cat == SPU_ES )
+                p_sys->i_default_sub_id = es->i_id;
         }
+        return VLC_SUCCESS;
+    }
 
-        case ES_OUT_SET_PCR:
-        case ES_OUT_SET_GROUP_PCR:
-        {
-            es_out_pgrm_t *p_pgrm = NULL;
-            int            i_group = 0;
-            int64_t        i_pcr;
+    case ES_OUT_SET_PCR:
+    case ES_OUT_SET_GROUP_PCR:
+    {
+        es_out_pgrm_t *p_pgrm = NULL;
+        int            i_group = 0;
+        int64_t        i_pcr;
 
-            /* Search program */
-            if( i_query == ES_OUT_SET_PCR )
-            {
-                p_pgrm = p_sys->p_pgrm;
-                if( !p_pgrm )
-                    p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
-            }
-            else
-            {
-                i_group = (int)va_arg( args, int );
-                p_pgrm = EsOutProgramFind( out, i_group );
-            }
+        /* Search program */
+        if( i_query == ES_OUT_SET_PCR )
+        {
+            p_pgrm = p_sys->p_pgrm;
             if( !p_pgrm )
-                return VLC_EGENERIC;
+                p_pgrm = EsOutProgramAdd( out, i_group );   /* Create it */
+        }
+        else
+        {
+            i_group = (int)va_arg( args, int );
+            p_pgrm = EsOutProgramFind( out, i_group );
+        }
+        if( !p_pgrm )
+            return VLC_EGENERIC;
 
-            i_pcr = (int64_t)va_arg( args, int64_t );
-            if( i_pcr <= VLC_TS_INVALID )
-            {
-                msg_Err( p_sys->p_input, "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" );
-                return VLC_EGENERIC;
-            }
+        i_pcr = (int64_t)va_arg( args, int64_t );
+        if( i_pcr <= VLC_TS_INVALID )
+        {
+            msg_Err( p_sys->p_input, "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" );
+            return VLC_EGENERIC;
+        }
 
-            /* TODO do not use mdate() but proper stream acquisition date */
-            bool b_late;
-            input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
-                                &b_late,
-                                p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering,
-                                EsOutIsExtraBufferingAllowed( out ),
-                                i_pcr, mdate() );
+        /* TODO do not use mdate() but proper stream acquisition date */
+        bool b_late;
+        input_clock_Update( p_pgrm->p_clock, VLC_OBJECT(p_sys->p_input),
+                            &b_late,
+                            p_sys->p_input->p->b_can_pace_control || p_sys->b_buffering,
+                            EsOutIsExtraBufferingAllowed( out ),
+                            i_pcr, mdate() );
 
-            if( p_pgrm == p_sys->p_pgrm )
+        if( !p_sys->p_pgrm )
+            return VLC_SUCCESS;
+
+        if( p_sys->b_buffering )
+        {
+            /* Check buffering state on master clock update */
+            EsOutDecodersStopBuffering( out, false );
+        }
+        else if( p_pgrm == p_sys->p_pgrm )
+        {
+            if( b_late && ( !p_sys->p_input->p->p_sout ||
+                                 !p_sys->p_input->p->b_out_pace_control ) )
             {
-                if( p_sys->b_buffering )
+                const mtime_t i_pts_delay_base = p_sys->i_pts_delay - p_sys->i_pts_jitter;
+                mtime_t i_pts_delay = input_clock_GetJitter( p_pgrm->p_clock );
+
+                /* Avoid dangerously high value */
+                const mtime_t i_jitter_max = INT64_C(1000) * var_InheritInteger( p_sys->p_input, "clock-jitter" );
+                if( i_pts_delay > __MIN( i_pts_delay_base + i_jitter_max, INPUT_PTS_DELAY_MAX ) )
                 {
-                    /* Check buffering state on master clock update */
-                    EsOutDecodersStopBuffering( out, false );
+                    msg_Err( p_sys->p_input,
+                             "ES_OUT_SET_(GROUP_)PCR  is called too late (jitter of %d ms ignored)",
+                             (int)(i_pts_delay - i_pts_delay_base) / 1000 );
+                    i_pts_delay = p_sys->i_pts_delay;
+
+                    /* reset clock */
+                    for( int i = 0; i < p_sys->i_pgrm; i++ )
+                      input_clock_Reset( p_sys->pgrm[i]->p_clock );
                 }
-                else if( b_late && ( !p_sys->p_input->p->p_sout ||
-                                     !p_sys->p_input->p->b_out_pace_control ) )
+                else
                 {
-                    const mtime_t i_pts_delay_base = p_sys->i_pts_delay - p_sys->i_pts_jitter;
-                    mtime_t i_pts_delay = input_clock_GetJitter( p_pgrm->p_clock );
-
-                    /* Avoid dangerously high value */
-                    const mtime_t i_jitter_max = INT64_C(1000) * var_InheritInteger( p_sys->p_input, "clock-jitter" );
-                    if( i_pts_delay > __MIN( i_pts_delay_base + i_jitter_max, INPUT_PTS_DELAY_MAX ) )
-                    {
-                        msg_Err( p_sys->p_input,
-                                 "ES_OUT_SET_(GROUP_)PCR  is called too late (jitter of %d ms ignored)",
-                                 (int)(i_pts_delay - i_pts_delay_base) / 1000 );
-                        i_pts_delay = p_sys->i_pts_delay;
-                    }
-                    else
-                    {
-                        msg_Err( p_sys->p_input,
-                                 "ES_OUT_SET_(GROUP_)PCR  is called too late (pts_delay increased to %d ms)",
-                                 (int)(i_pts_delay/1000) );
-                    }
+                    msg_Err( p_sys->p_input,
+                             "ES_OUT_SET_(GROUP_)PCR  is called too late (pts_delay increased to %d ms)",
+                             (int)(i_pts_delay/1000) );
 
                     /* Force a rebufferization when we are too late */
 
                     /* It is not really good, as we throw away already buffered data
                      * TODO have a mean to correctly reenter bufferization */
                     es_out_Control( out, ES_OUT_RESET_PCR );
-
-                    es_out_SetJitter( out, i_pts_delay_base, i_pts_delay - i_pts_delay_base, p_sys->i_cr_average );
                 }
+
+                es_out_SetJitter( out, i_pts_delay_base, i_pts_delay - i_pts_delay_base, p_sys->i_cr_average );
             }
-            return VLC_SUCCESS;
         }
+        return VLC_SUCCESS;
+    }
 
-        case ES_OUT_RESET_PCR:
-            msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
-            EsOutChangePosition( out );
-            return VLC_SUCCESS;
+    case ES_OUT_RESET_PCR:
+        msg_Err( p_sys->p_input, "ES_OUT_RESET_PCR called" );
+        EsOutChangePosition( out );
+        return VLC_SUCCESS;
 
-        case ES_OUT_SET_GROUP:
+    case ES_OUT_SET_GROUP:
+    {
+        int i = va_arg( args, int );
+        for( int j = 0; j < p_sys->i_pgrm; j++ )
         {
-            int i = va_arg( args, int );
-            for( int j = 0; j < p_sys->i_pgrm; j++ )
+            es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
+            if( p_pgrm->i_id == i )
             {
-                es_out_pgrm_t *p_pgrm = p_sys->pgrm[j];
-                if( p_pgrm->i_id == i )
-                {
-                    EsOutProgramSelect( out, p_pgrm );
-                    return VLC_SUCCESS;
-                }
+                EsOutProgramSelect( out, p_pgrm );
+                return VLC_SUCCESS;
             }
-            return VLC_EGENERIC;
         }
+        return VLC_EGENERIC;
+    }
 
-        case ES_OUT_SET_ES_FMT:
-        {
-            /* This ain't pretty but is need by some demuxers (eg. Ogg )
-             * to update the p_extra data */
-            es_out_id_t *es = va_arg( args, es_out_id_t * );
-            es_format_t *p_fmt = va_arg( args, es_format_t * );
-            if( es == NULL )
-                return VLC_EGENERIC;
+    case ES_OUT_SET_ES_FMT:
+    {
+        /* This ain't pretty but is need by some demuxers (eg. Ogg )
+         * to update the p_extra data */
+        es_out_id_t *es = va_arg( args, es_out_id_t * );
+        es_format_t *p_fmt = va_arg( args, es_format_t * );
+        if( es == NULL )
+            return VLC_EGENERIC;
 
-            if( p_fmt->i_extra )
-            {
-                es->fmt.i_extra = p_fmt->i_extra;
-                es->fmt.p_extra = xrealloc( es->fmt.p_extra, p_fmt->i_extra );
-                memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
+        if( p_fmt->i_extra )
+        {
+            es->fmt.i_extra = p_fmt->i_extra;
+            es->fmt.p_extra = xrealloc( es->fmt.p_extra, p_fmt->i_extra );
+            memcpy( es->fmt.p_extra, p_fmt->p_extra, p_fmt->i_extra );
 
-                if( !es->p_dec )
-                    return VLC_SUCCESS;
+            if( !es->p_dec )
+                return VLC_SUCCESS;
 #if 1
-                EsDestroyDecoder( out, es );
+            EsDestroyDecoder( out, es );
 
-                EsCreateDecoder( out, es );
+            EsCreateDecoder( out, es );
 #else
-                es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
-                es->p_dec->fmt_in.p_extra =
-                  xrealloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
-                memcpy( es->p_dec->fmt_in.p_extra,
-                        p_fmt->p_extra, p_fmt->i_extra );
+            es->p_dec->fmt_in.i_extra = p_fmt->i_extra;
+            es->p_dec->fmt_in.p_extra =
+              xrealloc( es->p_dec->fmt_in.p_extra, p_fmt->i_extra );
+            memcpy( es->p_dec->fmt_in.p_extra,
+                    p_fmt->p_extra, p_fmt->i_extra );
 #endif
-            }
-
-            return VLC_SUCCESS;
         }
 
-        case ES_OUT_SET_ES_SCRAMBLED_STATE:
-        {
-            es_out_id_t *es = va_arg( args, es_out_id_t * );
-            bool b_scrambled = (bool)va_arg( args, int );
+        return VLC_SUCCESS;
+    }
 
-            if( !es->b_scrambled != !b_scrambled )
-            {
-                es->b_scrambled = b_scrambled;
-                EsOutProgramUpdateScrambled( out, es->p_pgrm );
-            }
-            return VLC_SUCCESS;
-        }
+    case ES_OUT_SET_ES_SCRAMBLED_STATE:
+    {
+        es_out_id_t *es = va_arg( args, es_out_id_t * );
+        bool b_scrambled = (bool)va_arg( args, int );
 
-        case ES_OUT_SET_NEXT_DISPLAY_TIME:
+        if( !es->b_scrambled != !b_scrambled )
         {
-            const int64_t i_date = (int64_t)va_arg( args, int64_t );
-
-            if( i_date < 0 )
-                return VLC_EGENERIC;
-
-            p_sys->i_preroll_end = i_date;
-
-            return VLC_SUCCESS;
+            es->b_scrambled = b_scrambled;
+            EsOutProgramUpdateScrambled( out, es->p_pgrm );
         }
-        case ES_OUT_SET_GROUP_META:
-        {
-            int i_group = (int)va_arg( args, int );
-            const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
+        return VLC_SUCCESS;
+    }
 
-            EsOutProgramMeta( out, i_group, p_meta );
-            return VLC_SUCCESS;
-        }
-        case ES_OUT_SET_GROUP_EPG:
-        {
-            int i_group = (int)va_arg( args, int );
-            const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
+    case ES_OUT_SET_NEXT_DISPLAY_TIME:
+    {
+        const int64_t i_date = (int64_t)va_arg( args, int64_t );
 
-            EsOutProgramEpg( out, i_group, p_epg );
-            return VLC_SUCCESS;
-        }
+        if( i_date < 0 )
+            return VLC_EGENERIC;
 
-        case ES_OUT_DEL_GROUP:
-        {
-            int i_group = (int)va_arg( args, int );
+        p_sys->i_preroll_end = i_date;
 
-            return EsOutProgramDel( out, i_group );
-        }
+        return VLC_SUCCESS;
+    }
+    case ES_OUT_SET_GROUP_META:
+    {
+        int i_group = (int)va_arg( args, int );
+        const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
 
-        case ES_OUT_SET_META:
-        {
-            const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
+        EsOutProgramMeta( out, i_group, p_meta );
+        return VLC_SUCCESS;
+    }
+    case ES_OUT_SET_GROUP_EPG:
+    {
+        int i_group = (int)va_arg( args, int );
+        const vlc_epg_t *p_epg = va_arg( args, const vlc_epg_t * );
 
-            EsOutMeta( out, p_meta );
-            return VLC_SUCCESS;
-        }
+        EsOutProgramEpg( out, i_group, p_epg );
+        return VLC_SUCCESS;
+    }
 
-        case ES_OUT_GET_WAKE_UP:
-        {
-            mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
-            *pi_wakeup = EsOutGetWakeup( out );
-            return VLC_SUCCESS;
-        }
+    case ES_OUT_DEL_GROUP:
+    {
+        int i_group = (int)va_arg( args, int );
 
-        case ES_OUT_SET_ES_BY_ID:
-        case ES_OUT_RESTART_ES_BY_ID:
-        case ES_OUT_SET_ES_DEFAULT_BY_ID:
-        {
-            const int i_id = (int)va_arg( args, int );
-            es_out_id_t *p_es = EsOutGetFromID( out, i_id );
-            int i_new_query;
+        return EsOutProgramDel( out, i_group );
+    }
 
-            switch( i_query )
-            {
-            case ES_OUT_SET_ES_BY_ID:         i_new_query = ES_OUT_SET_ES; break;
-            case ES_OUT_RESTART_ES_BY_ID:     i_new_query = ES_OUT_RESTART_ES; break;
-            case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
-            default:
-              assert(0);
-            }
-            /* TODO if the lock is made non recursive it should be changed */
-            int i_ret = es_out_Control( out, i_new_query, p_es );
+    case ES_OUT_SET_META:
+    {
+        const vlc_meta_t *p_meta = va_arg( args, const vlc_meta_t * );
 
-            /* Clean up vout after user action (in active mode only).
-             * FIXME it does not work well with multiple video windows */
-            if( p_sys->b_active )
-                input_resource_TerminateVout( p_sys->p_input->p->p_resource );
-            return i_ret;
-        }
+        EsOutMeta( out, p_meta );
+        return VLC_SUCCESS;
+    }
 
-        case ES_OUT_GET_ES_OBJECTS_BY_ID:
-        {
-            const int i_id = va_arg( args, int );
-            es_out_id_t *p_es = EsOutGetFromID( out, i_id );
-            if( !p_es )
-                return VLC_EGENERIC;
+    case ES_OUT_GET_WAKE_UP:
+    {
+        mtime_t *pi_wakeup = (mtime_t*)va_arg( args, mtime_t* );
+        *pi_wakeup = EsOutGetWakeup( out );
+        return VLC_SUCCESS;
+    }
 
-            vlc_object_t    **pp_decoder = va_arg( args, vlc_object_t ** );
-            vout_thread_t   **pp_vout    = va_arg( args, vout_thread_t ** );
-            aout_instance_t **pp_aout    = va_arg( args, aout_instance_t ** );
-            if( p_es->p_dec )
-            {
-                if( pp_decoder )
-                    *pp_decoder = vlc_object_hold( p_es->p_dec );
-                input_DecoderGetObjects( p_es->p_dec, pp_vout, pp_aout );
-            }
-            else
-            {
-                if( pp_decoder )
-                    *pp_decoder = NULL;
-                if( pp_vout )
-                    *pp_vout = NULL;
-                if( pp_aout )
-                    *pp_aout = NULL;
-            }
-            return VLC_SUCCESS;
-        }
+    case ES_OUT_SET_ES_BY_ID:
+    case ES_OUT_RESTART_ES_BY_ID:
+    case ES_OUT_SET_ES_DEFAULT_BY_ID:
+    {
+        const int i_id = (int)va_arg( args, int );
+        es_out_id_t *p_es = EsOutGetFromID( out, i_id );
+        int i_new_query = 0;
 
-        case ES_OUT_GET_BUFFERING:
+        switch( i_query )
         {
-            bool *pb = va_arg( args, bool* );
-            *pb = p_sys->b_buffering;
-            return VLC_SUCCESS;
+        case ES_OUT_SET_ES_BY_ID:         i_new_query = ES_OUT_SET_ES; break;
+        case ES_OUT_RESTART_ES_BY_ID:     i_new_query = ES_OUT_RESTART_ES; break;
+        case ES_OUT_SET_ES_DEFAULT_BY_ID: i_new_query = ES_OUT_SET_ES_DEFAULT; break;
+        default:
+          vlc_assert_unreachable();
         }
+        /* TODO if the lock is made non recursive it should be changed */
+        int i_ret = es_out_Control( out, i_new_query, p_es );
 
-        case ES_OUT_GET_EMPTY:
-        {
-            bool *pb = va_arg( args, bool* );
-            *pb = EsOutDecodersIsEmpty( out );
-            return VLC_SUCCESS;
-        }
+        /* Clean up vout after user action (in active mode only).
+         * FIXME it does not work well with multiple video windows */
+        if( p_sys->b_active )
+            input_resource_TerminateVout( p_sys->p_input->p->p_resource );
+        return i_ret;
+    }
+
+    case ES_OUT_GET_ES_OBJECTS_BY_ID:
+    {
+        const int i_id = va_arg( args, int );
+        es_out_id_t *p_es = EsOutGetFromID( out, i_id );
+        if( !p_es )
+            return VLC_EGENERIC;
 
-        case ES_OUT_SET_DELAY:
+        vlc_object_t    **pp_decoder = va_arg( args, vlc_object_t ** );
+        vout_thread_t   **pp_vout    = va_arg( args, vout_thread_t ** );
+        audio_output_t **pp_aout    = va_arg( args, audio_output_t ** );
+        if( p_es->p_dec )
         {
-            const int i_cat = (int)va_arg( args, int );
-            const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
-            EsOutSetDelay( out, i_cat, i_delay );
-            return VLC_SUCCESS;
+            if( pp_decoder )
+                *pp_decoder = vlc_object_hold( p_es->p_dec );
+            input_DecoderGetObjects( p_es->p_dec, pp_vout, pp_aout );
         }
-
-        case ES_OUT_SET_RECORD_STATE:
+        else
         {
-            bool b = va_arg( args, int );
-            return EsOutSetRecord( out, b );
+            if( pp_decoder )
+                *pp_decoder = NULL;
+            if( pp_vout )
+                *pp_vout = NULL;
+            if( pp_aout )
+                *pp_aout = NULL;
         }
+        return VLC_SUCCESS;
+    }
 
-        case ES_OUT_SET_PAUSE_STATE:
-        {
-            const bool b_source_paused = (bool)va_arg( args, int );
-            const bool b_paused = (bool)va_arg( args, int );
-            const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
+    case ES_OUT_GET_BUFFERING:
+    {
+        bool *pb = va_arg( args, bool* );
+        *pb = p_sys->b_buffering;
+        return VLC_SUCCESS;
+    }
 
-            assert( !b_source_paused == !b_paused );
-            EsOutChangePause( out, b_paused, i_date );
+    case ES_OUT_GET_EMPTY:
+    {
+        bool *pb = va_arg( args, bool* );
+        *pb = EsOutDecodersIsEmpty( out );
+        return VLC_SUCCESS;
+    }
 
-            return VLC_SUCCESS;
-        }
+    case ES_OUT_SET_DELAY:
+    {
+        const int i_cat = (int)va_arg( args, int );
+        const mtime_t i_delay = (mtime_t)va_arg( args, mtime_t );
+        EsOutSetDelay( out, i_cat, i_delay );
+        return VLC_SUCCESS;
+    }
 
-        case ES_OUT_SET_RATE:
-        {
-            const int i_src_rate = (int)va_arg( args, int );
-            const int i_rate = (int)va_arg( args, int );
+    case ES_OUT_SET_RECORD_STATE:
+    {
+        bool b = va_arg( args, int );
+        return EsOutSetRecord( out, b );
+    }
 
-            assert( i_src_rate == i_rate );
-            EsOutChangeRate( out, i_rate );
+    case ES_OUT_SET_PAUSE_STATE:
+    {
+        const bool b_source_paused = (bool)va_arg( args, int );
+        const bool b_paused = (bool)va_arg( args, int );
+        const mtime_t i_date = (mtime_t) va_arg( args, mtime_t );
 
-            return VLC_SUCCESS;
-        }
+        assert( !b_source_paused == !b_paused );
+        EsOutChangePause( out, b_paused, i_date );
 
-        case ES_OUT_SET_TIME:
-        {
-            const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
+        return VLC_SUCCESS;
+    }
 
-            assert( i_date == -1 );
-            EsOutChangePosition( out );
+    case ES_OUT_SET_RATE:
+    {
+        const int i_src_rate = (int)va_arg( args, int );
+        const int i_rate = (int)va_arg( args, int );
 
-            return VLC_SUCCESS;
-        }
+        assert( i_src_rate == i_rate );
+        EsOutChangeRate( out, i_rate );
 
-        case ES_OUT_SET_FRAME_NEXT:
-            EsOutFrameNext( out );
-            return VLC_SUCCESS;
+        return VLC_SUCCESS;
+    }
 
-        case ES_OUT_SET_TIMES:
-        {
-            double f_position = (double)va_arg( args, double );
-            mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
-            mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
+    case ES_OUT_SET_TIME:
+    {
+        const mtime_t i_date = (mtime_t)va_arg( args, mtime_t );
 
-            input_SendEventLength( p_sys->p_input, i_length );
+        assert( i_date == -1 );
+        EsOutChangePosition( out );
 
-            if( !p_sys->b_buffering )
-            {
-                mtime_t i_delay;
+        return VLC_SUCCESS;
+    }
 
-                /* Fix for buffering delay */
-                if( !p_sys->p_input->p->p_sout ||
-                    !p_sys->p_input->p->b_out_pace_control )
-                    i_delay = EsOutGetBuffering( out );
-                else
-                    i_delay = 0;
+    case ES_OUT_SET_FRAME_NEXT:
+        EsOutFrameNext( out );
+        return VLC_SUCCESS;
 
-                i_time -= i_delay;
-                if( i_time < 0 )
-                    i_time = 0;
+    case ES_OUT_SET_TIMES:
+    {
+        double f_position = (double)va_arg( args, double );
+        mtime_t i_time = (mtime_t)va_arg( args, mtime_t );
+        mtime_t i_length = (mtime_t)va_arg( args, mtime_t );
 
-                if( i_length > 0 )
-                    f_position -= (double)i_delay / i_length;
-                if( f_position < 0 )
-                    f_position = 0;
+        input_SendEventLength( p_sys->p_input, i_length );
 
-                input_SendEventPosition( p_sys->p_input, f_position, i_time );
-            }
-            return VLC_SUCCESS;
-        }
-        case ES_OUT_SET_JITTER:
+        if( !p_sys->b_buffering )
         {
-            mtime_t i_pts_delay  = (mtime_t)va_arg( args, mtime_t );
-            mtime_t i_pts_jitter = (mtime_t)va_arg( args, mtime_t );
-            int     i_cr_average = (int)va_arg( args, int );
+            mtime_t i_delay;
 
-            bool b_change_clock =
-                i_pts_delay + i_pts_jitter != p_sys->i_pts_delay ||
-                i_cr_average != p_sys->i_cr_average;
+            /* Fix for buffering delay */
+            if( !p_sys->p_input->p->p_sout ||
+                !p_sys->p_input->p->b_out_pace_control )
+                i_delay = EsOutGetBuffering( out );
+            else
+                i_delay = 0;
 
-            assert( i_pts_jitter >= 0 );
-            p_sys->i_pts_delay  = i_pts_delay + i_pts_jitter;
-            p_sys->i_pts_jitter = i_pts_jitter;
-            p_sys->i_cr_average = i_cr_average;
+            i_time -= i_delay;
+            if( i_time < 0 )
+                i_time = 0;
 
-            for( int i = 0; i < p_sys->i_pgrm && b_change_clock; i++ )
-                input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
-                                       i_pts_delay + i_pts_jitter, i_cr_average );
-            return VLC_SUCCESS;
+            if( i_length > 0 )
+                f_position -= (double)i_delay / i_length;
+            if( f_position < 0 )
+                f_position = 0;
+
+            input_SendEventPosition( p_sys->p_input, f_position, i_time );
         }
+        return VLC_SUCCESS;
+    }
+    case ES_OUT_SET_JITTER:
+    {
+        mtime_t i_pts_delay  = (mtime_t)va_arg( args, mtime_t );
+        mtime_t i_pts_jitter = (mtime_t)va_arg( args, mtime_t );
+        int     i_cr_average = (int)va_arg( args, int );
 
-        case ES_OUT_GET_PCR_SYSTEM:
-        {
-            if( p_sys->b_buffering )
-                return VLC_EGENERIC;
+        bool b_change_clock =
+            i_pts_delay + i_pts_jitter != p_sys->i_pts_delay ||
+            i_cr_average != p_sys->i_cr_average;
 
-            es_out_pgrm_t *p_pgrm = p_sys->p_pgrm;
-            if( !p_pgrm )
-                return VLC_EGENERIC;
+        assert( i_pts_jitter >= 0 );
+        p_sys->i_pts_delay  = i_pts_delay + i_pts_jitter;
+        p_sys->i_pts_jitter = i_pts_jitter;
+        p_sys->i_cr_average = i_cr_average;
 
-            mtime_t *pi_system = va_arg( args, mtime_t *);
-            mtime_t *pi_delay  = va_arg( args, mtime_t *);
-            input_clock_GetSystemOrigin( p_pgrm->p_clock, pi_system, pi_delay );
-            return VLC_SUCCESS;
-        }
+        for( int i = 0; i < p_sys->i_pgrm && b_change_clock; i++ )
+            input_clock_SetJitter( p_sys->pgrm[i]->p_clock,
+                                   i_pts_delay + i_pts_jitter, i_cr_average );
+        return VLC_SUCCESS;
+    }
 
-        case ES_OUT_MODIFY_PCR_SYSTEM:
-        {
-            if( p_sys->b_buffering )
-                return VLC_EGENERIC;
+    case ES_OUT_GET_PCR_SYSTEM:
+    {
+        if( p_sys->b_buffering )
+            return VLC_EGENERIC;
 
-            es_out_pgrm_t *p_pgrm = p_sys->p_pgrm;
-            if( !p_pgrm )
-                return VLC_EGENERIC;
+        es_out_pgrm_t *p_pgrm = p_sys->p_pgrm;
+        if( !p_pgrm )
+            return VLC_EGENERIC;
 
-            const bool    b_absolute = va_arg( args, int );
-            const mtime_t i_system   = va_arg( args, mtime_t );
-            input_clock_ChangeSystemOrigin( p_pgrm->p_clock, b_absolute, i_system );
-            return VLC_SUCCESS;
-        }
+        mtime_t *pi_system = va_arg( args, mtime_t *);
+        mtime_t *pi_delay  = va_arg( args, mtime_t *);
+        input_clock_GetSystemOrigin( p_pgrm->p_clock, pi_system, pi_delay );
+        return VLC_SUCCESS;
+    }
 
-        default:
-            msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
+    case ES_OUT_MODIFY_PCR_SYSTEM:
+    {
+        if( p_sys->b_buffering )
+            return VLC_EGENERIC;
+
+        es_out_pgrm_t *p_pgrm = p_sys->p_pgrm;
+        if( !p_pgrm )
             return VLC_EGENERIC;
+
+        const bool    b_absolute = va_arg( args, int );
+        const mtime_t i_system   = va_arg( args, mtime_t );
+        input_clock_ChangeSystemOrigin( p_pgrm->p_clock, b_absolute, i_system );
+        return VLC_SUCCESS;
+    }
+    case ES_OUT_SET_EOS:
+    {
+        for (int i = 0; i < p_sys->i_es; i++) {
+            es_out_id_t *id = p_sys->es[i];
+            decoder_t *p_dec = id->p_dec;
+            if (!p_dec)
+                continue;
+            block_t *p_block = block_Alloc(0);
+            if( !p_block )
+                break;
+
+            p_block->i_flags |= BLOCK_FLAG_CORE_EOS;
+            input_DecoderDecode(p_dec, p_block, false);
+        }
+        return VLC_SUCCESS;
+    }
+
+    default:
+        msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
+        return VLC_EGENERIC;
     }
 }
 static int EsOutControl( es_out_t *out, int i_query, va_list args )
@@ -2712,7 +2721,7 @@ static char *LanguageGetName( const char *psz_code )
 {
     const iso639_lang_t *pl;
 
-    if( psz_code == NULL )
+    if( psz_code == NULL || !strcmp( psz_code, "und" ) )
     {
         return strdup( "" );
     }
@@ -2740,11 +2749,7 @@ static char *LanguageGetName( const char *psz_code )
     }
     else
     {
-        if( *pl->psz_native_name )
-        {
-            return strdup( pl->psz_native_name );
-        }
-        return strdup( pl->psz_eng_name );
+        return strdup( vlc_gettext(pl->psz_eng_name) );
     }
 }
 
@@ -2759,16 +2764,12 @@ static char *LanguageGetCode( const char *psz_lang )
     for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
     {
         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
-            !strcasecmp( pl->psz_native_name, psz_lang ) ||
             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
-            break;
+            return strdup( pl->psz_iso639_1 );
     }
 
-    if( pl->psz_eng_name != NULL )
-        return strdup( pl->psz_iso639_1 );
-
     return strdup("??");
 }
 
@@ -2826,13 +2827,12 @@ static char **LanguageSplit( const char *psz_langs, bool b_default_any )
     return ppsz;
 }
 
-static int LanguageArrayIndex( char **ppsz_langs, char *psz_lang )
+static int LanguageArrayIndex( char **ppsz_langs, const char *psz_lang )
 {
-    int i;
+    if( !ppsz_langs || !psz_lang )
+        return -1;
 
-    if( !ppsz_langs || !psz_lang ) return -1;
-
-    for( i = 0; ppsz_langs[i]; i++ )
+    for( int i = 0; ppsz_langs[i]; i++ )
     {
         if( !strcasecmp( ppsz_langs[i], psz_lang ) ||
             !strcasecmp( ppsz_langs[i], "any" ) )
@@ -2855,6 +2855,15 @@ static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *
     const es_format_t *p_fmt_es = &es->fmt;
     lldiv_t         div;
 
+    if( es->fmt.i_cat == fmt->i_cat )
+    {
+        es_format_t update = *fmt;
+        update.i_id = es->i_meta_id;
+        update.i_codec = es->fmt.i_codec;
+        update.i_original_fourcc = es->fmt.i_original_fourcc;
+        input_item_UpdateTracksInfo(input_GetItem(p_input), &update);
+    }
+
     /* Create category */
     char psz_cat[128];
     snprintf( psz_cat, sizeof(psz_cat),_("Stream %d"), es->i_meta_id );
@@ -2862,7 +2871,7 @@ static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *
     if( !p_cat )
         return;
 
-    /* Add informations */
+    /* Add information */
     const char *psz_type;
     switch( fmt->i_cat )
     {
@@ -2891,10 +2900,10 @@ static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *
         vlc_fourcc_GetDescription( p_fmt_es->i_cat, p_fmt_es->i_codec );
     const vlc_fourcc_t i_codec_fourcc = ( p_fmt_es->i_original_fourcc )?
                                p_fmt_es->i_original_fourcc : p_fmt_es->i_codec;
-    if( psz_codec_description )
+    if( psz_codec_description && *psz_codec_description )
         info_category_AddInfo( p_cat, _("Codec"), "%s (%.4s)",
                                psz_codec_description, (char*)&i_codec_fourcc );
-    else
+    else if ( i_codec_fourcc != VLC_FOURCC(0,0,0,0) )
         info_category_AddInfo( p_cat, _("Codec"), "%.4s",
                                (char*)&i_codec_fourcc );
 
@@ -2910,12 +2919,9 @@ static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *
     case AUDIO_ES:
         info_category_AddInfo( p_cat, _("Type"), _("Audio") );
 
-        if( fmt->audio.i_physical_channels & AOUT_CHAN_PHYSMASK )
+        if( fmt->audio.i_physical_channels )
             info_category_AddInfo( p_cat, _("Channels"), "%s",
                                    _( aout_FormatPrintChannels( &fmt->audio ) ) );
-        else if( fmt->audio.i_channels > 0 )
-            info_category_AddInfo( p_cat, _("Channels"), "%u",
-                                   fmt->audio.i_channels );
 
         if( fmt->audio.i_rate > 0 )
         {
@@ -2979,6 +2985,15 @@ static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *
                info_category_AddInfo( p_cat, _("Frame rate"), "%"PRId64,
                                       div.quot );
        }
+       if( fmt->i_codec != p_fmt_es->i_codec )
+       {
+           const char *psz_chroma_description =
+                vlc_fourcc_GetDescription( VIDEO_ES, fmt->i_codec );
+           if( psz_chroma_description )
+               info_category_AddInfo( p_cat, _("Decoded format"), "%s",
+                                      psz_chroma_description );
+       }
+
        break;
 
     case SPU_ES:
@@ -3008,4 +3023,3 @@ static void EsOutUpdateInfo( es_out_t *out, es_out_id_t *es, const es_format_t *
     /* */
     input_Control( p_input, INPUT_REPLACE_INFOS, p_cat );
 }
-