]> git.sesse.net Git - vlc/blobdiff - src/input/input.c
* src/libvlc.h, src/input/input.c:
[vlc] / src / input / input.c
index 4cca86d8838825f726c8976adb7daaca5eba5a61..61154947f53d5d8244e6083c4fee856df43a14b3 100644 (file)
@@ -3,8 +3,8 @@
  * Read a stream, demultiplex and parse it before sending it to
  * decoders.
  *****************************************************************************
- * Copyright (C) 1998-2002 VideoLAN
- * $Id: input.c,v 1.245 2003/09/30 16:09:58 hartman Exp $
+ * Copyright (C) 1998-2004 VideoLAN
+ * $Id$
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
@@ -30,6 +30,7 @@
 
 #include <vlc/vlc.h>
 #include <vlc/input.h>
+#include <vlc/decoder.h>
 #include <vlc/vout.h>
 
 #ifdef HAVE_SYS_TIMES_H
@@ -42,7 +43,8 @@
 
 #include "vlc_interface.h"
 #include "codecs.h"
-#include "modules/demux/util/sub.h"
+#include "vlc_meta.h"
+#include "../../modules/demux/util/sub.h"
 
 /*****************************************************************************
  * Local prototypes
@@ -52,6 +54,8 @@ struct input_thread_sys_t
     /* subtitles */
     int              i_sub;
     subtitle_demux_t **sub;
+
+    int64_t          i_stop_time;
 };
 
 static  int RunThread       ( input_thread_t *p_input );
@@ -62,9 +66,6 @@ static void EndThread       ( input_thread_t *p_input );
 static void ParseOption     ( input_thread_t *p_input,
                               const char *psz_option );
 
-static es_out_t *EsOutCreate ( input_thread_t * );
-static void      EsOutRelease( es_out_t * );
-
 /*****************************************************************************
  * Callbacks
  *****************************************************************************/
@@ -83,13 +84,13 @@ static int RateCallback    ( vlc_object_t *p_this, char const *psz_cmd,
  * This function creates a new input, and returns a pointer
  * to its description. On error, it returns NULL.
  *****************************************************************************/
-input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
-                                      playlist_item_t *p_item )
+input_thread_t *__input_CreateThread( vlc_object_t *p_parent, char *psz_uri,
+                                      char **ppsz_options, int i_options )
+
 {
-    input_thread_t *    p_input;                        /* thread descriptor */
-    input_info_category_t * p_info;
-    vlc_value_t val;
-    int i;
+    input_thread_t *p_input;                        /* thread descriptor */
+    vlc_value_t     val;
+    int             i;
 
     /* Allocate descriptor */
     p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
@@ -100,9 +101,10 @@ input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
     }
 
     /* Parse input options */
-    for( i = 0; i < p_item->i_options; i++ )
+    for( i = 0; i < i_options; i++ )
     {
-        ParseOption( p_input, p_item->ppsz_options[i] );
+        msg_Dbg( p_input, "option: %s", ppsz_options[i] );
+        ParseOption( p_input, ppsz_options[i] );
     }
 
     /* Create a few object variables we'll need later on */
@@ -112,12 +114,24 @@ input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
     var_Create( p_input, "spu-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
     var_Create( p_input, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
     var_Create( p_input, "sub-autodetect-file", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
+    var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
 
     var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    var_Create( p_input, "sout-all",   VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
     var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
     var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
     var_Create( p_input, "sout-keep",  VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
 
+    /* repeat variable */
+    var_Create( p_input, "input-repeat", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
+
+    /* start/stop time */
+    var_Create( p_input, "start-time", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
+    var_Create( p_input, "stop-time", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
+
+    /* decoders */
+    var_Create( p_input, "minimize-threads", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
+
     /* play status */
 
     /* position variable */
@@ -126,6 +140,7 @@ input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
     val.f_float = 0.0;
     var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
     var_AddCallback( p_input, "position", PositionCallback, NULL );
+    var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
 
     /* time variable */
     var_Create( p_input, "time",  VLC_VAR_TIME );
@@ -133,6 +148,7 @@ input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
     val.i_time = 0;
     var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
     var_AddCallback( p_input, "time", TimeCallback, NULL );
+    var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
 
     /* length variable */
     var_Create( p_input, "length",  VLC_VAR_TIME );
@@ -155,13 +171,18 @@ input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
     var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
     var_AddCallback( p_input, "state", StateCallback, NULL );
 
+    /* state variable */
+    var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
+    val.b_bool = VLC_FALSE;
+    var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
 
     /* Initialize thread properties */
     p_input->b_eof      = 0;
+    p_input->b_out_pace_control = VLC_FALSE;
     p_input->p_sys      = NULL;
 
     /* Set target */
-    p_input->psz_source = strdup( p_item->psz_uri );
+    p_input->psz_source = strdup( psz_uri );
 
     /* Stream */
     p_input->s = NULL;
@@ -174,6 +195,7 @@ input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
     p_input->pf_demux  = NULL;
     p_input->pf_rewind = NULL;
     p_input->pf_demux_control = NULL;
+    p_input->i_cr_average = config_GetInt( p_input, "cr-average" );
 
     /* Access */
     p_input->p_access = NULL;
@@ -223,23 +245,15 @@ input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
     p_input->stream.control.i_status = INIT_S;
     p_input->stream.control.i_rate = DEFAULT_RATE;
     p_input->stream.control.b_mute = 0;
-    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
-
-    /* Initialize input info */
-    p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
-    if( !p_input->stream.p_info )
-    {
-        msg_Err( p_input, "No memory!" );
-        return NULL;
-    }
-    p_input->stream.p_info->psz_name = strdup("General") ;
-    p_input->stream.p_info->p_info = NULL;
-    p_input->stream.p_info->p_next = NULL;
+    p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale");
 
     msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
 
-    p_info = input_InfoCategory( p_input, _("General") );
-    input_AddInfo( p_info, _("Playlist Item"), p_input->psz_source );
+    /* Initialize input info */
+    p_input->stream.p_info = NULL;
+    p_input->stream.p_info = input_InfoCategory( p_input, _("General") );
+    input_AddInfo( p_input->stream.p_info, _("Playlist Item"),
+                   p_input->psz_source );
     vlc_object_attach( p_input, p_parent );
 
     /* Create thread and wait for its readiness. */
@@ -247,6 +261,7 @@ input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
                            VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
     {
         msg_Err( p_input, "cannot create input thread" );
+        input_DelInfo( p_input );
         free( p_input );
         return NULL;
     }
@@ -303,8 +318,14 @@ static int RunThread( input_thread_t *p_input )
     {
         /* If we failed, wait before we are killed, and exit */
         p_input->b_error = 1;
+
         ErrorThread( p_input );
+
+        /* Tell we're dead */
         p_input->b_dead = 1;
+
+        input_DelInfo( p_input );
+
         return 0;
     }
 
@@ -455,10 +476,28 @@ static int RunThread( input_thread_t *p_input )
 
         if( i_count == 0 )
         {
-            /* End of file - we do not set b_die because only the
-             * playlist is allowed to do so. */
-            msg_Info( p_input, "EOF reached" );
-            p_input->b_eof = 1;
+            vlc_value_t repeat;
+
+            var_Get( p_input, "input-repeat", &repeat );
+            if( repeat.i_int == 0 || p_input->stream.i_area_nb <= 0 )
+            {
+                /* End of file - we do not set b_die because only the
+                 * playlist is allowed to do so. */
+                msg_Info( p_input, "EOF reached" );
+                p_input->b_eof = 1;
+            }
+            else
+            {
+                msg_Dbg( p_input, "repeating the same input (%d)", repeat.i_int );
+                if( repeat.i_int > 0 )
+                {
+                    repeat.i_int--;
+                    var_Set( p_input, "input-repeat", repeat );
+                }
+
+                p_input->stream.p_new_area = p_input->stream.pp_areas[0];
+                p_input->stream.p_new_area->i_seek = 0;
+            }
         }
         else if( i_count < 0 )
         {
@@ -489,13 +528,20 @@ static int RunThread( input_thread_t *p_input )
                 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
             }
 
+            /* Check stop-time */
+            if( p_input->p_sys->i_stop_time > 0 && p_input->p_sys->i_stop_time < i_time )
+            {
+                msg_Warn( p_input, "EOF reached because of stop-time" );
+                p_input->b_eof = 1;
+            }
+
             /* update subs */
             for( i = 0; i < p_input->p_sys->i_sub; i++ )
             {
                 subtitle_Demux( p_input->p_sys->sub[i], i_time );
             }
 
-            i_update_next = mdate() + 150000LL;
+            i_update_next = mdate() + I64C(150000);
         }
     }
 
@@ -514,13 +560,19 @@ static int RunThread( input_thread_t *p_input )
  *****************************************************************************/
 static int InitThread( input_thread_t * p_input )
 {
+    vlc_meta_t *p_meta = NULL, *p_meta_user = NULL;
     float f_fps;
+    playlist_t *p_playlist;
+    mtime_t i_length;
+
     /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
     char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
     vlc_value_t val;
-    subtitle_demux_t *p_sub;
     int64_t i_microsecondperframe;
 
+    subtitle_demux_t *p_sub_toselect = NULL;
+    char             *psz_sub_file = NULL;
+
     /* Skip the plug-in names */
     while( *psz_parser && *psz_parser != ':' )
     {
@@ -599,6 +651,12 @@ static int InitThread( input_thread_t * p_input )
 
     if( input_AccessInit( p_input ) == -1 )
     {
+        free( p_input->psz_source );
+        if( p_input->psz_dupsource != NULL )
+        {
+            free( p_input->psz_dupsource );
+        }
+
         return VLC_EGENERIC;
     }
 
@@ -611,30 +669,42 @@ static int InitThread( input_thread_t * p_input )
         {
             msg_Err( p_input, "cannot start stream output instance, aborting" );
             free( val.psz_string );
+
+            input_AccessEnd( p_input );
+            free( p_input->psz_source );
+            if( p_input->psz_dupsource != NULL )
+            {
+                free( p_input->psz_dupsource );
+            }
             return VLC_EGENERIC;
         }
         free( val.psz_string );
     }
 
-    p_input->p_es_out = EsOutCreate( p_input );
+    p_input->p_es_out = input_EsOutNew( p_input );
+    es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
+    es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
 
     /* Find and open appropriate access module */
     p_input->p_access = module_Need( p_input, "access",
-                                     p_input->psz_access );
+                                     p_input->psz_access, VLC_TRUE );
 
+#ifndef WIN32      /* Remove this gross hack from the win32 build as colons
+                    * are forbidden in filenames on Win32. */
+
+    /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
     if ( p_input->p_access == NULL
           && (*p_input->psz_demux || *p_input->psz_access) )
     {
-        /* Maybe we got something like :
-         * /Volumes/toto:titi/gabu.mpg */
         p_input->psz_access = p_input->psz_demux = "";
         p_input->psz_name = p_input->psz_source;
         free( p_input->psz_dupsource);
         p_input->psz_dupsource = NULL;
 
         p_input->p_access = module_Need( p_input, "access",
-                                         p_input->psz_access );
+                                         p_input->psz_access, VLC_TRUE );
     }
+#endif
 
     if( p_input->p_access == NULL )
     {
@@ -644,6 +714,14 @@ static int InitThread( input_thread_t * p_input )
         {
             sout_DeleteInstance( p_input->stream.p_sout );
         }
+
+        input_AccessEnd( p_input );
+        free( p_input->psz_source );
+        if( p_input->psz_dupsource != NULL )
+        {
+            free( p_input->psz_dupsource );
+        }
+        input_EsOutDelete( p_input->p_es_out );
         return VLC_EGENERIC;
     }
 
@@ -659,8 +737,10 @@ static int InitThread( input_thread_t * p_input )
 
     /* If the desynchronisation requested by the user is < 0, we need to
      * cache more data. */
-    if( p_input->p_vlc->i_desync < 0 )
-        p_input->i_pts_delay -= p_input->p_vlc->i_desync;
+    var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
+    var_Get( p_input, "audio-desync", &val );
+    if( val.i_int < 0 )
+        p_input->i_pts_delay -= (val.i_int * 1000);
 
     if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
     {
@@ -673,40 +753,67 @@ static int InitThread( input_thread_t * p_input )
                 {
                     sout_DeleteInstance( p_input->stream.p_sout );
                 }
+                input_AccessEnd( p_input );
+                free( p_input->psz_source );
+                if( p_input->psz_dupsource != NULL )
+                {
+                    free( p_input->psz_dupsource );
+                }
+                input_EsOutDelete( p_input->p_es_out );
                 return VLC_EGENERIC;
             }
         }
     }
 
     /* Create the stream_t facilities */
-    p_input->s = stream_OpenInput( p_input );
+    p_input->s = input_StreamNew( p_input );
     if( p_input->s == NULL )
     {
-        /* should nver occur yet */
+        /* should never occur yet */
+
+        msg_Err( p_input, "cannot create stream_t" );
 
-        msg_Err( p_input, "cannot create stream_t !" );
         module_Unneed( p_input, p_input->p_access );
         if ( p_input->stream.p_sout != NULL )
         {
             sout_DeleteInstance( p_input->stream.p_sout );
         }
+        input_AccessEnd( p_input );
+        free( p_input->psz_source );
+        if( p_input->psz_dupsource != NULL )
+        {
+            free( p_input->psz_dupsource );
+        }
+        input_EsOutDelete( p_input->p_es_out );
         return VLC_EGENERIC;
     }
 
     /* Find and open appropriate demux module */
-    p_input->p_demux = module_Need( p_input, "demux",
-                                    p_input->psz_demux );
+    p_input->p_demux =
+        module_Need( p_input, "demux",
+                     (p_input->psz_demux && *p_input->psz_demux) ?
+                     p_input->psz_demux : "$demux",
+                     (p_input->psz_demux && *p_input->psz_demux) ?
+                     VLC_TRUE : VLC_FALSE );
 
     if( p_input->p_demux == NULL )
     {
         msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
                  p_input->psz_access, p_input->psz_demux, p_input->psz_name );
-        stream_Release( p_input->s );
+
+        input_StreamDelete( p_input->s );
         module_Unneed( p_input, p_input->p_access );
         if ( p_input->stream.p_sout != NULL )
         {
             sout_DeleteInstance( p_input->stream.p_sout );
         }
+        input_AccessEnd( p_input );
+        free( p_input->psz_source );
+        if( p_input->psz_dupsource != NULL )
+        {
+            free( p_input->psz_dupsource );
+        }
+        input_EsOutDelete( p_input->p_es_out );
         return VLC_EGENERIC;
     }
 
@@ -715,7 +822,214 @@ static int InitThread( input_thread_t * p_input )
     p_input->p_sys->i_sub = 0;
     p_input->p_sys->sub   = NULL;
 
-    /* get fps */
+    p_input->p_sys->i_stop_time = 0;
+
+    /* Get meta information from user */
+    var_Create( p_input, "meta-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    var_Create( p_input, "meta-author", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    var_Create( p_input, "meta-artist", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    var_Create( p_input, "meta-genre", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    var_Create( p_input, "meta-copyright", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
+    var_Create( p_input, "meta-description", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
+    var_Create( p_input, "meta-date", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    var_Create( p_input, "meta-url", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+    if( (p_meta_user = vlc_meta_New()) )
+    {
+        vlc_value_t val;
+
+        var_Get( p_input, "meta-title", &val );
+        if( val.psz_string && *val.psz_string )
+            vlc_meta_Add( p_meta_user, VLC_META_TITLE, val.psz_string );
+        if( val.psz_string ) free( val.psz_string );
+        var_Get( p_input, "meta-author", &val );
+        if( val.psz_string && *val.psz_string )
+            vlc_meta_Add( p_meta_user, VLC_META_AUTHOR, val.psz_string );
+        if( val.psz_string ) free( val.psz_string );
+        var_Get( p_input, "meta-artist", &val );
+        if( val.psz_string && *val.psz_string )
+            vlc_meta_Add( p_meta_user, VLC_META_ARTIST, val.psz_string );
+        if( val.psz_string ) free( val.psz_string );
+        var_Get( p_input, "meta-genre", &val );
+        if( val.psz_string && *val.psz_string )
+            vlc_meta_Add( p_meta_user, VLC_META_GENRE, val.psz_string );
+        if( val.psz_string ) free( val.psz_string );
+        var_Get( p_input, "meta-copyright", &val );
+        if( val.psz_string && *val.psz_string )
+            vlc_meta_Add( p_meta_user, VLC_META_COPYRIGHT, val.psz_string );
+        if( val.psz_string ) free( val.psz_string );
+        var_Get( p_input, "meta-description", &val );
+        if( val.psz_string && *val.psz_string )
+            vlc_meta_Add( p_meta_user, VLC_META_DESCRIPTION, val.psz_string );
+        if( val.psz_string ) free( val.psz_string );
+        var_Get( p_input, "meta-date", &val );
+        if( val.psz_string && *val.psz_string )
+            vlc_meta_Add( p_meta_user, VLC_META_DATE, val.psz_string );
+        if( val.psz_string ) free( val.psz_string );
+        var_Get( p_input, "meta-url", &val );
+        if( val.psz_string && *val.psz_string )
+            vlc_meta_Add( p_meta_user, VLC_META_URL, val.psz_string );
+        if( val.psz_string ) free( val.psz_string );
+    }
+
+    /* Get meta informations from demuxer */
+    if( !demux_Control( p_input, DEMUX_GET_META, &p_meta ) ||
+        ( p_meta_user && p_meta_user->i_meta ) )
+    {
+        playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_input,
+                                         VLC_OBJECT_PLAYLIST,  FIND_PARENT);
+        playlist_item_t *p_item = NULL;
+        input_info_category_t *p_cat;
+        int i;
+
+        /* Merge demux and user metadata */
+        if( !p_meta ){ p_meta = p_meta_user; p_meta_user = NULL; }
+        else if( p_meta && p_meta_user ) vlc_meta_Merge( p_meta, p_meta_user );
+
+        if( p_playlist )
+        {
+            vlc_mutex_lock( &p_playlist->object_lock );
+            p_item = playlist_ItemGetByPos( p_playlist, -1 );
+            if( p_item )
+            {
+                vlc_mutex_lock( &p_item->lock );
+            }
+            vlc_mutex_unlock( &p_playlist->object_lock );
+        }
+
+        msg_Dbg( p_input, "meta informations:" );
+        if( p_meta->i_meta > 0 )
+        {
+            p_cat = input_InfoCategory( p_input, _("File") );
+            for( i = 0; i < p_meta->i_meta; i++ )
+            {
+                msg_Dbg( p_input, "  - '%s' = '%s'", _(p_meta->name[i]),
+                         p_meta->value[i] );
+                if( !strcmp( p_meta->name[i], VLC_META_TITLE ) )
+                {
+                    playlist_ItemSetName( p_item, p_meta->value[i] );
+                }
+                if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
+                {
+                    playlist_ItemAddInfo( p_item, _("General"), _("Author"),
+                                          p_meta->value[i] );
+                }
+                input_AddInfo( p_cat, _(p_meta->name[i]), "%s",
+                               p_meta->value[i] );
+                if( p_item )
+                {
+                    playlist_ItemAddInfo( p_item, _("File"),
+                                          _(p_meta->name[i]), "%s",
+                                          p_meta->value[i] );
+                }
+            }
+        }
+        for( i = 0; i < p_meta->i_track; i++ )
+        {
+            vlc_meta_t *tk = p_meta->track[i];
+            int j;
+
+            msg_Dbg( p_input, "  - track[%d]:", i );
+            if( tk->i_meta > 0 )
+            {
+                char *psz_cat = malloc( strlen(_("Stream")) + 10 );
+                sprintf( psz_cat, "%s %d", _("Stream"), i );
+                p_cat = input_InfoCategory( p_input, psz_cat );
+
+                for( j = 0; j < tk->i_meta; j++ )
+                {
+                    msg_Dbg( p_input, "     - '%s' = '%s'", _(tk->name[j]),
+                             tk->value[j] );
+                    input_AddInfo( p_cat, _(tk->name[j]), "%s", tk->value[j] );
+                    if( p_item )
+                    {
+                        playlist_ItemAddInfo( p_item, psz_cat, _(tk->name[j]),
+                                              "%s", tk->value[j] );
+                    }
+                }
+            }
+        }
+
+        if( p_item )
+        {
+            vlc_mutex_unlock( &p_item->lock );
+        }
+        if( p_playlist ) vlc_object_release( p_playlist );
+
+        if( p_input->stream.p_sout && p_input->stream.p_sout->p_meta == NULL )
+        {
+            p_input->stream.p_sout->p_meta = p_meta;
+        }
+        else
+        {
+            vlc_meta_Delete( p_meta );
+        }
+    }
+    if( p_meta_user ) vlc_meta_Delete( p_meta_user );
+
+    /* Get length */
+    if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) &&
+        i_length > 0 )
+    {
+        input_info_category_t *p_cat =
+            input_InfoCategory( p_input, _("File") );
+        p_playlist =
+            (playlist_t*)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
+                                          FIND_PARENT );
+        if( p_playlist )
+        {
+            playlist_SetDuration( p_playlist, -1 , i_length );
+            val.b_bool = p_playlist->i_index;
+            var_Set( p_playlist, "item-change", val );
+            vlc_object_release( p_playlist );
+        }
+        if( p_cat )
+        {
+            char psz_buffer[MSTRTIME_MAX_SIZE];
+            input_AddInfo( p_cat, _("Duration"),
+                           msecstotimestr( psz_buffer, i_length / 1000 ) );
+        }
+
+        /* Set start time */
+        var_Get( p_input, "start-time", &val );
+        if(  val.i_int > 0 )
+        {
+            double f_pos = val.i_int * I64C(1000000) / (double)i_length;
+
+            if( f_pos >= 1.0 )
+            {
+                msg_Warn( p_input, "invalid start-time, ignored (start-time "
+                          ">= media length)" );
+            }
+            else
+            {
+                p_input->stream.p_selected_area->i_seek =
+                    (int64_t)( f_pos * (double)p_input->stream.p_selected_area->i_size );
+
+                msg_Dbg( p_input, "start-time %ds (%2.2f)", val.i_int, f_pos );
+            }
+        }
+    }
+
+    /* Set stop-time and check validity */
+    var_Get( p_input, "stop-time", &val );
+    if( val.i_int > 0 )
+    {
+        vlc_value_t start;
+
+        var_Get( p_input, "start-time", &start );
+        if( start.i_int >= val.i_int )
+        {
+            msg_Warn( p_input, "invalid stop-time, ignored (stop-time < "
+                      "start-time)" );
+        }
+        else
+        {
+            p_input->p_sys->i_stop_time = (int64_t)val.i_int * I64C(1000000);
+            msg_Dbg( p_input, "stop-time %ds", val.i_int );
+        }
+    }
+
+    /* Get fps */
     if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
     {
         i_microsecondperframe = 0;
@@ -729,27 +1043,68 @@ static int InitThread( input_thread_t * p_input )
     var_Get( p_input, "sub-file", &val );
     if( val.psz_string && *val.psz_string )
     {
-        if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string), i_microsecondperframe ) ) )
+        subtitle_demux_t *p_sub;
+
+        msg_Dbg( p_input, "force subtitle: %s", val.psz_string );
+        if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string),
+                                    i_microsecondperframe ) ) )
         {
+            p_sub_toselect = p_sub;
             TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
-            subtitle_Select( p_sub );
         }
     }
-    if( val.psz_string ) free( val.psz_string );
-    
+    psz_sub_file = val.psz_string;
+
     var_Get( p_input, "sub-autodetect-file", &val );
     if( val.b_bool )
     {
-        char **tmp = subtitles_Detect( p_input, "", p_input->psz_source );
+        subtitle_demux_t *p_sub;
+        int i;
+        char **tmp = subtitles_Detect( p_input, "", p_input->psz_name );
         char **tmp2 = tmp;
-        while (*tmp2)
+        for( i = 0; *tmp2 != NULL; i++ )
         {
-            if( ( p_sub = subtitle_New( p_input, strdup(*tmp2++), i_microsecondperframe ) ) )
+            if( psz_sub_file == NULL || strcmp( psz_sub_file, *tmp2 ) )
             {
-                TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
+                if( ( p_sub = subtitle_New( p_input, *tmp2,
+                                            i_microsecondperframe ) ) )
+                {
+                    TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub,
+                                p_sub );
+                }
             }
+            free( *tmp2++ );
         }
-        free(tmp);
+        free( tmp );
+    }
+    if( psz_sub_file ) free( psz_sub_file );
+
+    es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
+    val.b_bool =  VLC_FALSE;
+    if( p_input->stream.p_sout )
+    {
+        var_Get( p_input, "sout-all", &val );
+    }
+    es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
+                    val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
+    if( p_sub_toselect )
+    {
+        es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
+                        p_sub_toselect->p_es, VLC_TRUE );
+    }
+
+    if( p_input->stream.p_sout )
+    {
+        if( p_input->stream.p_sout->i_out_pace_nocontrol > 0 )
+        {
+            p_input->b_out_pace_control = VLC_FALSE;
+        }
+        else
+        {
+            p_input->b_out_pace_control = VLC_TRUE;
+        }
+        msg_Dbg( p_input, "starting in %s mode",
+                 p_input->b_out_pace_control ? "asynch" : "synch" );
     }
 
     return VLC_SUCCESS;
@@ -788,11 +1143,14 @@ static void EndThread( input_thread_t * p_input )
 
     input_DumpStream( p_input );
 
+    /* Free demultiplexer's data */
+    if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
+
     /* Free all ES and destroy all decoder threads */
     input_EndStream( p_input );
 
     /* Close optional stream output instance */
-    if ( p_input->stream.p_sout != NULL )
+    if( p_input->stream.p_sout )
     {
         vlc_object_t *p_pl =
             vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
@@ -817,29 +1175,29 @@ static void EndThread( input_thread_t * p_input )
     }
 
     /* Destroy subtitles demuxers */
-    for( i = 0; i < p_input->p_sys->i_sub; i++ )
+    if( p_input->p_sys )
     {
-        subtitle_Close( p_input->p_sys->sub[i] );
-    }
-    if( p_input->p_sys->i_sub > 0 )
-    {
-        free( p_input->p_sys->sub );
-    }
-
-    /* Free input_thread_sys_t */
-    free( p_input->p_sys );
+        for( i = 0; i < p_input->p_sys->i_sub; i++ )
+        {
+            subtitle_Close( p_input->p_sys->sub[i] );
+        }
+        if( p_input->p_sys->i_sub > 0 )
+        {
+            free( p_input->p_sys->sub );
+        }
 
-    /* Free demultiplexer's data */
-    module_Unneed( p_input, p_input->p_demux );
+        /* Free input_thread_sys_t */
+        free( p_input->p_sys );
+    }
 
     /* Destroy the stream_t facilities */
-    stream_Release( p_input->s );
+    if( p_input->s ) input_StreamDelete( p_input->s );
 
     /* Destroy es out */
-    EsOutRelease( p_input->p_es_out );
+    if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
 
     /* Close the access plug-in */
-    module_Unneed( p_input, p_input->p_access );
+    if( p_input->p_access ) module_Unneed( p_input, p_input->p_access );
 
     input_AccessEnd( p_input );
 
@@ -848,7 +1206,7 @@ static void EndThread( input_thread_t * p_input )
     input_DelInfo( p_input );
 
     free( p_input->psz_source );
-    if ( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
+    if( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
 
     /* Tell we're dead */
     p_input->b_dead = 1;
@@ -931,6 +1289,7 @@ static void ParseOption( input_thread_t *p_input, const char *psz_option )
         break;
 
     case VLC_VAR_STRING:
+    case VLC_VAR_MODULE:
     case VLC_VAR_FILE:
     case VLC_VAR_DIRECTORY:
         val.psz_string = psz_value;
@@ -950,280 +1309,31 @@ static void ParseOption( input_thread_t *p_input, const char *psz_option )
     return;
 }
 
-/*****************************************************************************
- * es_out_t input handler
- *****************************************************************************/
-struct es_out_sys_t
-{
-    input_thread_t *p_input;
-
-    int         i_id;
-    es_out_id_t **id;
-
-    vlc_bool_t  i_audio;
-    vlc_bool_t  i_video;
-};
-struct es_out_id_t
-{
-    es_descriptor_t *p_es;
-};
-
-static es_out_id_t *EsOutAdd    ( es_out_t *, es_format_t * );
-static int          EsOutSend   ( es_out_t *, es_out_id_t *, pes_packet_t * );
-static void         EsOutDel    ( es_out_t *, es_out_id_t * );
-static int          EsOutControl( es_out_t *, int i_query, va_list );
-
-static es_out_t *EsOutCreate( input_thread_t *p_input )
-{
-    es_out_t *out = malloc( sizeof( es_out_t ) );
-
-    out->pf_add     = EsOutAdd;
-    out->pf_send    = EsOutSend;
-    out->pf_del     = EsOutDel;
-    out->pf_control = EsOutControl;
-
-    out->p_sys = malloc( sizeof( es_out_sys_t ) );
-    out->p_sys->p_input = p_input;
-    out->p_sys->i_id    = 0;
-    out->p_sys->id      = NULL;
-    out->p_sys->i_audio = -1;
-    out->p_sys->i_video = -1;
-    return out;
-}
-static void      EsOutRelease( es_out_t *out )
-{
-    es_out_sys_t *p_sys = out->p_sys;
-    int i;
-
-    for( i = 0; i < p_sys->i_id; i++ )
-    {
-        free( p_sys->id[i] );
-    }
-    if( p_sys->id )
-    {
-        free( p_sys->id );
-    }
-    free( p_sys );
-    free( out );
-}
-
-static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
-{
-    es_out_sys_t      *p_sys = out->p_sys;
-    input_thread_t    *p_input = p_sys->p_input;
-    es_out_id_t       *id = malloc( sizeof( es_out_id_t ) );
-    pgrm_descriptor_t *p_prgm = NULL;
-
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    if( fmt->i_group >= 0 )
-    {
-        /* search program */
-        p_prgm = input_FindProgram( p_input, fmt->i_group );
-
-        if( p_prgm == NULL )
-        {
-            /* create it */
-            p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
-
-            /* Select the first by default */
-            if( p_input->stream.p_selected_program == NULL )
-            {
-                p_input->stream.p_selected_program = p_prgm;
-            }
-        }
-    }
-
-    id->p_es = input_AddES( p_input,
-                            p_prgm,
-                            1 + out->p_sys->i_id,
-                            fmt->i_cat,
-                            fmt->psz_description, 0 );
-    id->p_es->i_stream_id = 1 + out->p_sys->i_id;
-    id->p_es->i_fourcc = fmt->i_codec;
-
-    switch( fmt->i_cat )
-    {
-        case AUDIO_ES:
-        {
-            WAVEFORMATEX *p_wf = malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
-
-            p_wf->wFormatTag        = WAVE_FORMAT_UNKNOWN;
-            p_wf->nChannels         = fmt->audio.i_channels;
-            p_wf->nSamplesPerSec    = fmt->audio.i_samplerate;
-            p_wf->nAvgBytesPerSec   = fmt->audio.i_bitrate / 8;
-            p_wf->nBlockAlign       = fmt->audio.i_blockalign;
-            p_wf->wBitsPerSample    = fmt->audio.i_bitspersample;
-            p_wf->cbSize            = fmt->i_extra;
-            if( fmt->i_extra > 0 )
-            {
-                if( fmt->i_extra_type != ES_EXTRA_TYPE_WAVEFORMATEX )
-                {
-                    msg_Warn( p_input, "extra type != WAVEFORMATEX for audio" );
-                }
-                memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
-            }
-            id->p_es->p_waveformatex = p_wf;
-            break;
-        }
-        case VIDEO_ES:
-        {
-            BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
-                                              fmt->i_extra );
-            p_bih->biSize           = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
-            p_bih->biWidth          = fmt->video.i_width;
-            p_bih->biHeight         = fmt->video.i_height;
-            p_bih->biPlanes         = 1;
-            p_bih->biBitCount       = 24;
-            p_bih->biCompression    = fmt->i_codec;
-            p_bih->biSizeImage      = fmt->video.i_width * fmt->video.i_height;
-            p_bih->biXPelsPerMeter  = 0;
-            p_bih->biYPelsPerMeter  = 0;
-            p_bih->biClrUsed        = 0;
-            p_bih->biClrImportant   = 0;
-
-            if( fmt->i_extra > 0 )
-            {
-                if( fmt->i_extra_type != ES_EXTRA_TYPE_BITMAPINFOHEADER )
-                {
-                    msg_Warn( p_input,
-                              "extra type != BITMAPINFOHEADER for video" );
-                }
-                memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
-            }
-            id->p_es->p_bitmapinfoheader = p_bih;
-            break;
-        }
-        default:
-            break;
-    }
-
-    if( fmt->i_cat == AUDIO_ES && fmt->i_priority > out->p_sys->i_audio )
-    {
-        if( out->p_sys->i_audio >= 0 )
-        {
-            msg_Err( p_input, "FIXME unselect es in es_out_Add" );
-        }
-        input_SelectES( p_input, id->p_es );
-        if( id->p_es->p_decoder_fifo )
-        {
-            out->p_sys->i_audio = fmt->i_priority;
-        }
-    }
-    else if( fmt->i_cat == VIDEO_ES && fmt->i_priority > out->p_sys->i_video )
-    {
-        if( out->p_sys->i_video >= 0 )
-        {
-            msg_Err( p_input, "FIXME unselect es in es_out_Add" );
-        }
-        input_SelectES( p_input, id->p_es );
-        if( id->p_es->p_decoder_fifo )
-        {
-            out->p_sys->i_video = fmt->i_priority;
-        }
-    }
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-
-    TAB_APPEND( out->p_sys->i_id, out->p_sys->id, id );
-
-    return id;
-}
-static int EsOutSend( es_out_t *out, es_out_id_t *id, pes_packet_t *p_pes )
-{
-    if( id->p_es->p_decoder_fifo )
-    {
-        input_DecodePES( id->p_es->p_decoder_fifo, p_pes );
-    }
-    else
-    {
-        input_DeletePES( out->p_sys->p_input->p_method_data, p_pes );
-    }
-    return VLC_SUCCESS;
-}
-static void EsOutDel( es_out_t *out, es_out_id_t *id )
-{
-    es_out_sys_t *p_sys = out->p_sys;
-
-    TAB_REMOVE( p_sys->i_id, p_sys->id, id );
-
-    vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
-    if( id->p_es->p_decoder_fifo )
-    {
-        input_UnselectES( p_sys->p_input, id->p_es );
-    }
-    if( id->p_es->p_waveformatex )
-    {
-        free( id->p_es->p_waveformatex );
-        id->p_es->p_waveformatex = NULL;
-    }
-    if( id->p_es->p_bitmapinfoheader )
-    {
-        free( id->p_es->p_bitmapinfoheader );
-        id->p_es->p_bitmapinfoheader = NULL;
-    }
-    input_DelES( p_sys->p_input, id->p_es );
-    vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
-
-    free( id );
-}
-static int EsOutControl( es_out_t *out, int i_query, va_list args )
-{
-    es_out_sys_t *p_sys = out->p_sys;
-    vlc_bool_t  b, *pb;
-    es_out_id_t *id;
-    switch( i_query )
-    {
-        case ES_OUT_SET_SELECT:
-            id = (es_out_id_t*) va_arg( args, es_out_id_t * );
-            b = (vlc_bool_t) va_arg( args, vlc_bool_t );
-            if( b && id->p_es->p_decoder_fifo == NULL )
-            {
-                input_SelectES( p_sys->p_input, id->p_es );
-                return id->p_es->p_decoder_fifo ? VLC_SUCCESS : VLC_EGENERIC;
-            }
-            else if( !b && id->p_es->p_decoder_fifo )
-            {
-                input_UnselectES( p_sys->p_input, id->p_es );
-            }
-            return VLC_SUCCESS;
-
-        case ES_OUT_GET_SELECT:
-            id = (es_out_id_t*) va_arg( args, es_out_id_t * );
-            pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
-
-            *pb = id->p_es->p_decoder_fifo ? VLC_TRUE : VLC_FALSE;
-            return VLC_SUCCESS;
-
-        default:
-            msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
-            return VLC_EGENERIC;
-    }
-}
-
 /*****************************************************************************
  * Callbacks  (position, time, state, rate )
  *****************************************************************************/
 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
-                             vlc_value_t oldval, vlc_value_t newval, void *p_data )
+                             vlc_value_t oldval, vlc_value_t newval,
+                             void *p_data )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
-    int64_t i_offset;
 
-    msg_Warn( p_input, "cmd=%s old=%f new=%f",
-              psz_cmd,
-              oldval.f_float, newval.f_float );
+    msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
+             oldval.f_float, newval.f_float );
 
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    i_offset = (int64_t)( newval.f_float *
-                          (double)p_input->stream.p_selected_area->i_size );
     if( !strcmp( psz_cmd, "position-offset" ) )
     {
-        p_input->stream.p_selected_area->i_seek += i_offset;
-    }
-    else
-    {
-        p_input->stream.p_selected_area->i_seek = i_offset;
+        vlc_value_t val;
+        var_Get( p_input, "position", &val );
+
+        newval.f_float += val.f_float;
     }
+
+    vlc_mutex_lock( &p_input->stream.stream_lock );
+    p_input->stream.p_selected_area->i_seek =
+        (int64_t)( newval.f_float *
+                   (double)p_input->stream.p_selected_area->i_size );
+
     if( p_input->stream.p_selected_area->i_seek < 0 )
     {
         p_input->stream.p_selected_area->i_seek = 0;
@@ -1233,43 +1343,44 @@ static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
     return VLC_SUCCESS;
 }
 
-static int TimeCallback    ( vlc_object_t *p_this, char const *psz_cmd,
-                             vlc_value_t oldval, vlc_value_t newval, void *p_data )
+static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
+                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
-    int64_t i_offset;
+    vlc_value_t     val;
 
     /* FIXME TODO FIXME */
-    msg_Warn( p_input, "cmd=%s old=%lld new=%lld",
-              psz_cmd,
-              oldval.i_time, newval.i_time );
-
+    msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
+             oldval.i_time, newval.i_time );
 
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    i_offset = newval.i_time / 1000000 * 50 * p_input->stream.i_mux_rate;
-    if( !strcmp( psz_cmd, "time-offset" ) )
+    var_Get( p_input, "length", &val );
+    if( val.i_time > 0 )
     {
-        p_input->stream.p_selected_area->i_seek += i_offset;
+        val.f_float = (double)newval.i_time / (double)val.i_time;
+        if( !strcmp( psz_cmd, "time-offset" ) )
+        {
+            var_Set( p_input, "position-offset", val );
+        }
+        else
+        {
+            var_Set( p_input, "position", val );
+        }
     }
     else
     {
-        p_input->stream.p_selected_area->i_seek = i_offset;
+        msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
     }
-    if( p_input->stream.p_selected_area->i_seek < 0 )
-    {
-        p_input->stream.p_selected_area->i_seek = 0;
-    }
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
     return VLC_SUCCESS;
 }
 
-static int StateCallback   ( vlc_object_t *p_this, char const *psz_cmd,
-                             vlc_value_t oldval, vlc_value_t newval, void *p_data )
+static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
+                          vlc_value_t oldval, vlc_value_t newval,
+                          void *p_data )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
-    msg_Warn( p_input, "cmd=%s old=%d new=%d",
-              psz_cmd, oldval.i_int, newval.i_int );
+
+    msg_Dbg( p_input, "cmd=%s old=%d new=%d",
+             psz_cmd, oldval.i_int, newval.i_int );
 
     switch( newval.i_int )
     {
@@ -1288,8 +1399,8 @@ static int StateCallback   ( vlc_object_t *p_this, char const *psz_cmd,
     }
 }
 
-static int RateCallback    ( vlc_object_t *p_this, char const *psz_cmd,
-                             vlc_value_t oldval, vlc_value_t newval, void *p_data )
+static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
+                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
 
@@ -1303,8 +1414,8 @@ static int RateCallback    ( vlc_object_t *p_this, char const *psz_cmd,
     }
     else
     {
-        msg_Warn( p_input, "cmd=%s old=%d new=%d",
-                  psz_cmd, oldval.i_int, newval.i_int );
+        msg_Dbg( p_input, "cmd=%s old=%d new=%d",
+                 psz_cmd, oldval.i_int, newval.i_int );
         input_SetRate( p_input, newval.i_int );
     }
     return VLC_SUCCESS;