]> git.sesse.net Git - vlc/blobdiff - modules/demux/mp4/mp4.c
Rename vlc_input_item_* functions to input_Item* for consistency
[vlc] / modules / demux / mp4 / mp4.c
index 31d719e4682a167613b1469932bff2a4f4998219..d3af967172288a4d86a7963f65427ed449fae475 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * mp4.c : MP4 file input module for vlc
  *****************************************************************************
- * Copyright (C) 2001-2004 VideoLAN
+ * Copyright (C) 2001-2004 the VideoLAN team
  * $Id$
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
 
 #include <vlc/vlc.h>
+
+#include <stdio.h>
+#include <stdlib.h>                                      /* malloc(), free() */
+
 #include <vlc/input.h>
 #include <vlc_playlist.h>
+#include <vlc_md5.h>
+#include "charset.h"
 #include "iso_lang.h"
 #include "vlc_meta.h"
 
@@ -77,7 +82,7 @@ typedef struct
     uint32_t     *p_sample_delta_dts;   /* dts delta */
 
     uint32_t     *p_sample_count_pts;
-    uint32_t     *p_sample_offset_pts;  /* pts-dts */
+    int32_t      *p_sample_offset_pts;  /* pts-dts */
 
     /* TODO if needed add pts
         but quickly *add* support for edts and seeking */
@@ -164,7 +169,7 @@ static int      MP4_TrackSampleSize( mp4_track_t * );
 static int      MP4_TrackNextSample( demux_t *, mp4_track_t * );
 static void     MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
 
-/* Return time in ยตs of a track */
+/* Return time in s of a track */
 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
 {
 #define chunk p_track->chunk[p_track->i_chunk]
@@ -228,7 +233,8 @@ static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_tra
     for( i_index = 0;; i_index++ )
     {
         if( i_sample < ck->p_sample_count_pts[i_index] )
-            return ck->p_sample_offset_pts[i_index] * I64C(1000000) / p_track->i_timescale;
+            return ck->p_sample_offset_pts[i_index] * I64C(1000000) /
+                   (int64_t)p_track->i_timescale;
 
         i_sample -= ck->p_sample_count_pts[i_index];
     }
@@ -239,7 +245,9 @@ static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
     return I64C(1000000) * p_sys->i_time / p_sys->i_timescale;
 }
 
-#define FREE( p ) if( p ) { free( p ); (p) = NULL;}
+/* Function to lookup the currently playing item */
+static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
+                     playlist_item_t **pp_item );
 
 /*****************************************************************************
  * Open: check file and initializes MP4 structures
@@ -342,7 +350,7 @@ static int Open( vlc_object_t * p_this )
     if( ( p_rmra = MP4_BoxGet( p_sys->p_root,  "/moov/rmra" ) ) )
     {
         playlist_t *p_playlist;
-        playlist_item_t *p_item;
+        playlist_item_t *p_current, *p_item_in_category;
         int        i_count = MP4_BoxCount( p_rmra, "rmda" );
         int        i;
         vlc_bool_t b_play = VLC_FALSE;
@@ -355,9 +363,9 @@ static int Open( vlc_object_t * p_this )
                                            FIND_ANYWHERE );
         if( p_playlist )
         {
-            p_item = playlist_LockItemGetByInput( p_playlist,
-                      ((input_thread_t *)p_demux->p_parent)->input.p_item );
-            playlist_ItemToNode( p_playlist, p_item );
+            b_play = FindItem( p_demux, p_playlist, &p_current );
+            p_item_in_category = playlist_ItemToNode( p_playlist, p_current );
+            p_current->p_input->i_type = ITEM_TYPE_PLAYLIST;
 
             for( i = 0; i < i_count; i++ )
             {
@@ -365,7 +373,7 @@ static int Open( vlc_object_t * p_this )
                 char      *psz_ref;
                 uint32_t  i_ref_type;
 
-                if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
+                if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
                 {
                     continue;
                 }
@@ -384,59 +392,31 @@ static int Open( vlc_object_t * p_this )
                     if( !strncmp( psz_ref, "http://", 7 ) ||
                         !strncmp( psz_ref, "rtsp://", 7 ) )
                     {
-                        msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
-                        if( p_item )
-                        {
-                            playlist_item_t *p_child =
-                                        playlist_ItemNew( p_playlist,
-                                                          psz_ref, psz_ref );
-                            if( p_child )
-                            {
-                                playlist_NodeAddItem( p_playlist, p_child,
-                                                 p_item->pp_parents[0]->i_view,
-                                                 p_item, PLAYLIST_APPEND,
-                                                 PLAYLIST_END );
-                                playlist_CopyParents( p_item, p_child );
-                                b_play = VLC_TRUE;
-                            }
-                        }
+                        ;
                     }
                     else
                     {
-                        /* msg dbg relative ? */
-                        char *psz_absolute = alloca( strlen( p_demux->psz_access ) + 3 + strlen( p_demux->psz_path ) + strlen( psz_ref ) + 1);
-                        char *end = strrchr( p_demux->psz_path, '/' );
+                        char *psz_absolute;
+                        char *psz_path = strdup( p_demux->psz_path );
+                        char *end = strrchr( psz_path, '/' );
+                        if( end ) end[1] = '\0';
+                        else *psz_path = '\0';
 
-                        if( end )
-                        {
-                            int i_len = end + 1 - p_demux->psz_path;
+                        asprintf( &psz_absolute, "%s://%s%s",
+                                      p_demux->psz_access, psz_path, psz_ref );
 
-                            strcpy( psz_absolute, p_demux->psz_access );
-                            strcat( psz_absolute, "://" );
-                            strncat( psz_absolute, p_demux->psz_path, i_len);
-                        }
-                        else
-                        {
-                            strcpy( psz_absolute, "" );
-                        }
-                        strcat( psz_absolute, psz_ref );
-                        msg_Dbg( p_demux, "adding ref = `%s'", psz_absolute );
-                        if( p_item )
-                        {
-                            playlist_item_t *p_child =
-                                        playlist_ItemNew( p_playlist,
-                                                          psz_absolute,
-                                                          psz_absolute );
-                            if( p_child )
-                            {
-                                playlist_NodeAddItem( p_playlist, p_child,
-                                                 p_item->pp_parents[0]->i_view,
-                                                 p_item, PLAYLIST_APPEND,
-                                                 PLAYLIST_END );
-                                playlist_CopyParents( p_item, p_child );
-                                b_play = VLC_TRUE;
-                            }
-                        }
+                        psz_ref = psz_absolute;
+                        free( psz_path );
+                    }
+                    if( p_current )
+                    {
+                        input_item_t *p_input;
+                        msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
+                        p_input = input_ItemNewExt( p_playlist, psz_ref, NULL,
+                                            0, NULL, -1 );
+                        input_ItemCopyOptions( p_current->p_input, p_input );
+                        playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
+                                p_item_in_category, VLC_FALSE, PLAYLIST_APPEND );
                     }
                 }
                 else
@@ -444,12 +424,13 @@ static int Open( vlc_object_t * p_this )
                     msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
                              (char*)&p_rdrf->data.p_rdrf->i_ref_type );
                 }
+                if( psz_ref ) free( psz_ref );
             }
-            if( b_play == VLC_TRUE )
+            if( b_play && p_playlist->status.p_item &&
+                  p_playlist->status.p_item->i_children > 0)
             {
-                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
-                                   p_playlist->status.i_view,
-                                   p_playlist->status.p_item, NULL );
+                playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
+                                  p_playlist->status.p_item, NULL );
             }
             vlc_object_release( p_playlist );
         }
@@ -673,7 +654,7 @@ static int Demux( demux_t *p_demux )
                             p_block->i_buffer = i_size + 1;
 
                             /* convert \r -> \n */
-                            while( ( p = strchr( p_block->p_buffer, '\r' ) ) )
+                            while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
                             {
                                 *p = '\n';
                             }
@@ -689,7 +670,7 @@ static int Demux( demux_t *p_demux )
                 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
                 /* pts */
                 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
-                if( i_delta >= 0 )
+                if( i_delta != -1 )
                     p_block->i_pts = p_block->i_dts + i_delta;
                 else if( tk->fmt.i_cat != VIDEO_ES )
                     p_block->i_pts = p_block->i_dts;
@@ -798,47 +779,48 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
 
         case DEMUX_GET_META:
         {
-            vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
-            vlc_meta_t *meta;
+            vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
             MP4_Box_t  *p_udta   = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
             MP4_Box_t  *p_0xa9xxx;
             if( p_udta == NULL )
             {
                 return VLC_EGENERIC;
             }
-            *pp_meta = meta = vlc_meta_New();
             for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
                  p_0xa9xxx = p_0xa9xxx->p_next )
             {
+                char *psz_utf;
+                if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
+                    continue;
+                psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
+                if( psz_utf == NULL )
+                    continue;
+                /* FIXME FIXME: should convert from whatever the character
+                 * encoding of MP4 meta data is to UTF-8. */
+                EnsureUTF8( psz_utf );
+
                 switch( p_0xa9xxx->i_type )
                 {
                 case FOURCC_0xa9nam: /* Full name */
-                    vlc_meta_Add( meta, VLC_META_TITLE,
-                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
+                    vlc_meta_SetArtist( p_meta, psz_utf );
                     break;
                 case FOURCC_0xa9aut:
-                    vlc_meta_Add( meta, VLC_META_AUTHOR,
-                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
+                    vlc_meta_SetAuthor( p_meta, psz_utf );
                     break;
                 case FOURCC_0xa9ART:
-                    vlc_meta_Add( meta, VLC_META_ARTIST,
-                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
+                    vlc_meta_SetArtist( p_meta, psz_utf );
                     break;
                 case FOURCC_0xa9cpy:
-                    vlc_meta_Add( meta, VLC_META_COPYRIGHT,
-                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
+                    vlc_meta_SetCopyright( p_meta, psz_utf );
                     break;
                 case FOURCC_0xa9day: /* Creation Date */
-                    vlc_meta_Add( meta, VLC_META_DATE,
-                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
+                    vlc_meta_SetDate( p_meta, psz_utf );
                     break;
                 case FOURCC_0xa9des: /* Description */
-                    vlc_meta_Add( meta, VLC_META_DESCRIPTION,
-                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
+                    vlc_meta_SetDescription( p_meta, psz_utf );
                     break;
                 case FOURCC_0xa9gen: /* Genre */
-                    vlc_meta_Add( meta, VLC_META_GENRE,
-                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
+                    vlc_meta_SetGenre( p_meta, psz_utf );
                     break;
 
                 case FOURCC_0xa9swr:
@@ -867,6 +849,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
                 default:
                     break;
                 }
+                free( psz_utf );
             }
             return VLC_SUCCESS;
         }
@@ -877,7 +860,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             return VLC_EGENERIC;
 
         default:
-            msg_Warn( p_demux, "control query unimplemented !!!" );
+            msg_Warn( p_demux, "control query %u unimplemented", i_query );
             return VLC_EGENERIC;
     }
 }
@@ -898,7 +881,7 @@ static void Close ( vlc_object_t * p_this )
     {
         MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
     }
-    FREE( p_sys->track );
+    FREENULL( p_sys->track );
 
     free( p_sys );
 }
@@ -1145,7 +1128,7 @@ static int TrackCreateSamplesIndex( demux_t *p_demux,
 
             /* allocate them */
             ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
-            ck->p_sample_offset_pts = calloc( i_entry, sizeof( uint32_t ) );
+            ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
 
             /* now copy */
             i_sample_count = ck->i_sample_count;
@@ -1268,18 +1251,38 @@ static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
         case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
             p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
             break;
+
         case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
             p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
+
+            /* Buggy files workaround */
+            if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
+                p_sample->data.p_sample_soun->i_sampleratehi) )
+            {
+                MP4_Box_data_sample_soun_t *p_soun =
+                    p_sample->data.p_sample_soun;
+
+                msg_Warn( p_demux, "i_timescale ("I64Fu") != i_sampleratehi "
+                          "(%u), making both equal (report any problem).",
+                          p_track->i_timescale, p_soun->i_sampleratehi );
+
+                if( p_soun->i_sampleratehi )
+                    p_track->i_timescale = p_soun->i_sampleratehi;
+                else
+                    p_soun->i_sampleratehi = p_track->i_timescale;
+            }
             break;
+
         case( VLC_FOURCC( 's', '2', '6', '3' ) ):
             p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
             break;
 
         case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
+        case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
             p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
             /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
             /* FIXME UTF-8 doesn't work here ? */
-            /* p_track->fmt.subs.psz_encoding = strdup( "UTF-8" ); */
+            p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
             break;
 
         default:
@@ -1337,6 +1340,10 @@ static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
                 if( p_track->fmt.i_cat == SPU_ES )
                 {
                     p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
+                    if( p_track->i_width > 0 )
+                        p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
+                    if( p_track->i_height > 0 )
+                        p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
                     break;
                 }
             /* Fallback */
@@ -1399,12 +1406,10 @@ static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
 
                 if( p_avcC )
                 {
-                    /* Hack: use a packetizer to reecampsulate data in anexe B format */
-                    msg_Dbg( p_demux, "avcC: size=%d", p_avcC->data.p_avcC->i_avcC );
                     p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
                     p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
-                    memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC, p_track->fmt.i_extra );
-                    p_track->fmt.b_packetized = VLC_FALSE;
+                    memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
+                            p_track->fmt.i_extra );
                 }
                 else
                 {
@@ -1426,6 +1431,8 @@ static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
     case( VIDEO_ES ):
         p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
         p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
+        p_track->fmt.video.i_bits_per_pixel =
+            p_sample->data.p_sample_vide->i_depth;
 
         /* fall on display size */
         if( p_track->fmt.video.i_width <= 0 )
@@ -1440,6 +1447,10 @@ static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
             p_track->fmt.video.i_aspect =
                 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
 
+        /* Support for cropping (eg. in H263 files) */
+        p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
+        p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
+
         /* Frame rate */
         p_track->fmt.video.i_frame_rate = p_track->i_timescale;
         p_track->fmt.video.i_frame_rate_base = 1;
@@ -1545,8 +1556,8 @@ static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
             break;
         }
 
-        if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
-            i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
+        if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
+            (uint64_t)i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
         {
             break;
         }
@@ -1559,7 +1570,7 @@ static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
     {
         if( i_dts +
             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
-            p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
+            p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
         {
             i_dts    +=
                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
@@ -1714,7 +1725,7 @@ static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
     unsigned int i;
     char language[4];
 
-    /* hint track unsuported */
+    /* hint track unsupported */
 
     /* set default value (-> track unusable) */
     p_track->b_ok       = VLC_FALSE;
@@ -1777,6 +1788,7 @@ static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
 
         case( FOURCC_text ):
         case( FOURCC_subp ):
+        case( FOURCC_tx3g ):
             p_track->fmt.i_cat = SPU_ES;
             break;
 
@@ -1789,7 +1801,7 @@ static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
     if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
     {
         MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
-        int i;
+        unsigned int i;
 
         msg_Warn( p_demux, "elst box found" );
         for( i = 0; i < elst->i_entry_count; i++ )
@@ -1798,7 +1810,8 @@ static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
                      "ms) rate=%d.%d", i,
                      elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
                      elst->i_media_time[i] >= 0 ?
-                     elst->i_media_time[i] * 1000 / p_track->i_timescale : -1,
+                     (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
+                     I64C(-1),
                      elst->i_media_rate_integer[i],
                      elst->i_media_rate_fraction[i] );
         }
@@ -1843,32 +1856,6 @@ static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
         }
     }
 
-    /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
-    if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
-    {
-        MP4_Box_t *p_sample;
-
-        p_sample = MP4_BoxGet(  p_track->p_stsd, "[0]" );
-        if( p_sample && p_sample->data.p_sample_soun)
-        {
-            MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
-            if( p_soun->i_qt_version == 0 &&
-                p_track->i_timescale != p_soun->i_sampleratehi )
-            {
-                msg_Warn( p_demux,
-                          "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
-                          "qt_version == 0\n"
-                          "Making both equal. (report any problem)",
-                          p_track->i_timescale, p_soun->i_sampleratehi );
-
-                if( p_soun->i_sampleratehi )
-                    p_track->i_timescale = p_soun->i_sampleratehi;
-                else
-                    p_soun->i_sampleratehi = p_track->i_timescale;
-            }
-        }
-    }
-
     /* Create chunk index table and sample index table */
     if( TrackCreateChunksIndex( p_demux,p_track  ) ||
         TrackCreateSamplesIndex( p_demux, p_track ) )
@@ -1923,18 +1910,18 @@ static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
     {
         if( p_track->chunk )
         {
-           FREE(p_track->chunk[i_chunk].p_sample_count_dts);
-           FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
+           FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
+           FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
 
-           FREE(p_track->chunk[i_chunk].p_sample_count_pts);
-           FREE(p_track->chunk[i_chunk].p_sample_offset_pts );
+           FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
+           FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
         }
     }
-    FREE( p_track->chunk );
+    FREENULL( p_track->chunk );
 
     if( !p_track->i_sample_size )
     {
-        FREE( p_track->p_sample_size );
+        FREENULL( p_track->p_sample_size );
     }
 }
 
@@ -2005,7 +1992,11 @@ static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
         VLC_SUCCESS )
     {
         p_track->b_selected = VLC_TRUE;
+
+        es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
+                        p_track->p_es, i_start );
     }
+
     return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
 }
 
@@ -2157,10 +2148,10 @@ static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
     {
         demux_sys_t *p_sys = p_demux->p_sys;
         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
-        int64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
+        uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
                         p_sys->i_timescale / (int64_t)1000000;
 
-        if( p_track->i_elst < elst->i_entry_count &&
+        if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
             i_mvt >= p_track->i_elst_time +
                      elst->i_segment_duration[p_track->i_elst] )
         {
@@ -2186,7 +2177,7 @@ static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
         MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
         int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
 
-        for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
+        for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
         {
             mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
 
@@ -2197,7 +2188,7 @@ static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
             tk->i_elst_time += i_dur;
         }
 
-        if( tk->i_elst >= elst->i_entry_count )
+        if( (unsigned int)tk->i_elst >= elst->i_entry_count )
         {
             /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
             tk->i_elst = elst->i_entry_count - 1;
@@ -2215,3 +2206,33 @@ static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
         msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
     }
 }
+
+static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
+                     playlist_item_t **pp_item )
+{
+     vlc_bool_t b_play = var_CreateGetBool( p_demux, "playlist-autostart" );
+
+     if( b_play && p_playlist->status.p_item &&
+             p_playlist->status.p_item->p_input ==
+                ((input_thread_t *)p_demux->p_parent)->input.p_item )
+     {
+         msg_Dbg( p_playlist, "starting playlist playback" );
+         *pp_item = p_playlist->status.p_item;
+         b_play = VLC_TRUE;
+     }
+     else
+     {
+         input_item_t *p_current = ( (input_thread_t*)p_demux->p_parent)->
+                                                        input.p_item;
+         *pp_item = playlist_LockItemGetByInput( p_playlist, p_current );
+         if( !*pp_item )
+         {
+             msg_Dbg( p_playlist, "unable to find item in playlist");
+         }
+         msg_Dbg( p_playlist, "not starting playlist playback");
+         b_play = VLC_FALSE;
+     }
+     return b_play;
+}
+
+