]> git.sesse.net Git - vlc/blob - modules/demux/mp4/mp4.c
Plugins: include vlc_common.h directly instead of vlc/vlc.h
[vlc] / modules / demux / mp4 / mp4.c
1 /*****************************************************************************
2  * mp4.c : MP4 file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33
34
35 #include <vlc_demux.h>
36 #include <vlc_playlist.h>
37 #include <vlc_md5.h>
38 #include <vlc_charset.h>
39 #include <vlc_iso_lang.h>
40 #include <vlc_meta.h>
41
42 #include "libmp4.h"
43 #include "drms.h"
44
45 #ifdef UNDER_CE
46 #define uint64_t int64_t
47 #endif
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52 static int  Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
54
55 vlc_module_begin();
56     set_category( CAT_INPUT );
57     set_subcategory( SUBCAT_INPUT_DEMUX );
58     set_description( N_("MP4 stream demuxer") );
59     set_capability( "demux", 242 );
60     set_callbacks( Open, Close );
61 vlc_module_end();
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static int   Demux   ( demux_t * );
67 static int   DemuxRef( demux_t *p_demux ){ return 0;}
68 static int   Seek    ( demux_t *, mtime_t );
69 static int   Control ( demux_t *, int, va_list );
70
71 /* Contain all information about a chunk */
72 typedef struct
73 {
74     uint64_t     i_offset; /* absolute position of this chunk in the file */
75     uint32_t     i_sample_description_index; /* index for SampleEntry to use */
76     uint32_t     i_sample_count; /* how many samples in this chunk */
77     uint32_t     i_sample_first; /* index of the first sample in this chunk */
78
79     /* now provide way to calculate pts, dts, and offset without to
80         much memory and with fast acces */
81
82     /* with this we can calculate dts/pts without waste memory */
83     uint64_t     i_first_dts;
84     uint32_t     *p_sample_count_dts;
85     uint32_t     *p_sample_delta_dts;   /* dts delta */
86
87     uint32_t     *p_sample_count_pts;
88     int32_t      *p_sample_offset_pts;  /* pts-dts */
89
90     /* TODO if needed add pts
91         but quickly *add* support for edts and seeking */
92
93 } mp4_chunk_t;
94
95  /* Contain all needed information for read all track with vlc */
96 typedef struct
97 {
98     unsigned int i_track_ID;/* this should be unique */
99
100     int b_ok;               /* The track is usable */
101     int b_enable;           /* is the trak enable by default */
102     bool b_selected;  /* is the trak being played */
103     bool b_chapter;   /* True when used for chapter only */
104
105     es_format_t fmt;
106     es_out_id_t *p_es;
107
108     /* display size only ! */
109     int i_width;
110     int i_height;
111
112     /* more internal data */
113     uint64_t        i_timescale;    /* time scale for this track only */
114
115     /* elst */
116     int             i_elst;         /* current elst */
117     int64_t         i_elst_time;    /* current elst start time (in movie time scale)*/
118     MP4_Box_t       *p_elst;        /* elst (could be NULL) */
119
120     /* give the next sample to read, i_chunk is to find quickly where
121       the sample is located */
122     uint32_t         i_sample;       /* next sample to read */
123     uint32_t         i_chunk;        /* chunk where next sample is stored */
124     /* total count of chunk and sample */
125     uint32_t         i_chunk_count;
126     uint32_t         i_sample_count;
127
128     mp4_chunk_t    *chunk; /* always defined  for each chunk */
129
130     /* sample size, p_sample_size defined only if i_sample_size == 0
131         else i_sample_size is size for all sample */
132     uint32_t         i_sample_size;
133     uint32_t         *p_sample_size; /* XXX perhaps add file offset if take
134                                     too much time to do sumations each time*/
135
136     MP4_Box_t *p_stbl;  /* will contain all timing information */
137     MP4_Box_t *p_stsd;  /* will contain all data to initialize decoder */
138     MP4_Box_t *p_sample;/* point on actual sdsd */
139
140     bool b_drms;
141     void      *p_drms;
142
143 } mp4_track_t;
144
145
146 struct demux_sys_t
147 {
148     MP4_Box_t    *p_root;      /* container for the whole file */
149
150     mtime_t      i_pcr;
151
152     uint64_t     i_time;         /* time position of the presentation
153                                   * in movie timescale */
154     uint64_t     i_timescale;    /* movie time scale */
155     uint64_t     i_duration;     /* movie duration */
156     unsigned int i_tracks;       /* number of tracks */
157     mp4_track_t  *track;         /* array of track */
158
159     /* */
160     MP4_Box_t    *p_tref_chap;
161
162     /* */
163     input_title_t *p_title;
164 };
165
166 /*****************************************************************************
167  * Declaration of local function
168  *****************************************************************************/
169 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t  *);
170 static void MP4_TrackDestroy( demux_t *, mp4_track_t * );
171
172 static int  MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
173 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
174
175 static int  MP4_TrackSeek   ( demux_t *, mp4_track_t *, mtime_t );
176
177 static uint64_t MP4_TrackGetPos    ( mp4_track_t * );
178 static int      MP4_TrackSampleSize( mp4_track_t * );
179 static int      MP4_TrackNextSample( demux_t *, mp4_track_t * );
180 static void     MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
181
182 static void     MP4_UpdateSeekpoint( demux_t * );
183
184 /* Return time in s of a track */
185 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
186 {
187 #define chunk p_track->chunk[p_track->i_chunk]
188
189     unsigned int i_index = 0;
190     unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
191     int64_t i_dts = chunk.i_first_dts;
192
193     while( i_sample > 0 )
194     {
195         if( i_sample > chunk.p_sample_count_dts[i_index] )
196         {
197             i_dts += chunk.p_sample_count_dts[i_index] *
198                 chunk.p_sample_delta_dts[i_index];
199             i_sample -= chunk.p_sample_count_dts[i_index];
200             i_index++;
201         }
202         else
203         {
204             i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
205             i_sample = 0;
206             break;
207         }
208     }
209
210 #undef chunk
211
212     /* now handle elst */
213     if( p_track->p_elst )
214     {
215         demux_sys_t         *p_sys = p_demux->p_sys;
216         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
217
218         /* convert to offset */
219         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
220               elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
221             elst->i_media_time[p_track->i_elst] > 0 )
222         {
223             i_dts -= elst->i_media_time[p_track->i_elst];
224         }
225
226         /* add i_elst_time */
227         i_dts += p_track->i_elst_time * p_track->i_timescale /
228             p_sys->i_timescale;
229
230         if( i_dts < 0 ) i_dts = 0;
231     }
232
233     return INT64_C(1000000) * i_dts / p_track->i_timescale;
234 }
235
236 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
237 {
238     mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
239     unsigned int i_index = 0;
240     unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
241
242     if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
243         return -1;
244
245     for( i_index = 0;; i_index++ )
246     {
247         if( i_sample < ck->p_sample_count_pts[i_index] )
248             return ck->p_sample_offset_pts[i_index] * INT64_C(1000000) /
249                    (int64_t)p_track->i_timescale;
250
251         i_sample -= ck->p_sample_count_pts[i_index];
252     }
253 }
254
255 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
256 {
257     return INT64_C(1000000) * p_sys->i_time / p_sys->i_timescale;
258 }
259
260 /* Function to lookup the currently playing item */
261 static bool FindItem( demux_t *p_demux, playlist_t *p_playlist,
262                      playlist_item_t **pp_item );
263
264 static void LoadChapter( demux_t  *p_demux );
265
266 /*****************************************************************************
267  * Open: check file and initializes MP4 structures
268  *****************************************************************************/
269 static int Open( vlc_object_t * p_this )
270 {
271     demux_t  *p_demux = (demux_t *)p_this;
272     demux_sys_t     *p_sys;
273
274     const uint8_t   *p_peek;
275
276     MP4_Box_t       *p_ftyp;
277     MP4_Box_t       *p_rmra;
278     MP4_Box_t       *p_mvhd;
279     MP4_Box_t       *p_trak;
280
281     unsigned int    i;
282     bool      b_seekable;
283
284     /* A little test to see if it could be a mp4 */
285     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
286
287     switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
288     {
289         case FOURCC_ftyp:
290         case FOURCC_moov:
291         case FOURCC_foov:
292         case FOURCC_moof:
293         case FOURCC_mdat:
294         case FOURCC_udta:
295         case FOURCC_free:
296         case FOURCC_skip:
297         case FOURCC_wide:
298         case VLC_FOURCC( 'p', 'n', 'o', 't' ):
299             break;
300          default:
301             return VLC_EGENERIC;
302     }
303
304     /* I need to seek */
305     stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
306     if( !b_seekable )
307     {
308         msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
309         return VLC_EGENERIC;
310     }
311
312     /*Set exported functions */
313     p_demux->pf_demux = Demux;
314     p_demux->pf_control = Control;
315
316     /* create our structure that will contains all data */
317     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
318     memset( p_sys, 0, sizeof( demux_sys_t ) );
319
320     /* Now load all boxes ( except raw data ) */
321     if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
322     {
323         msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
324         goto error;
325     }
326
327     MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
328
329     if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
330     {
331         switch( p_ftyp->data.p_ftyp->i_major_brand )
332         {
333             case( FOURCC_isom ):
334                 msg_Dbg( p_demux,
335                          "ISO Media file (isom) version %d.",
336                          p_ftyp->data.p_ftyp->i_minor_version );
337                 break;
338             default:
339                 msg_Dbg( p_demux,
340                          "unrecognized major file specification (%4.4s).",
341                           (char*)&p_ftyp->data.p_ftyp->i_major_brand );
342                 break;
343         }
344     }
345     else
346     {
347         msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
348     }
349
350     /* the file need to have one moov box */
351     if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
352     {
353         MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
354
355         if( !p_foov )
356         {
357             msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
358             goto error;
359         }
360         /* we have a free box as a moov, rename it */
361         p_foov->i_type = FOURCC_moov;
362     }
363
364     if( ( p_rmra = MP4_BoxGet( p_sys->p_root,  "/moov/rmra" ) ) )
365     {
366         playlist_t *p_playlist;
367         playlist_item_t *p_current, *p_item_in_category;
368         int        i_count = MP4_BoxCount( p_rmra, "rmda" );
369         int        i;
370         bool b_play = false;
371
372         msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
373
374         p_playlist = pl_Yield( p_demux );
375         if( p_playlist )
376         {
377             b_play = FindItem( p_demux, p_playlist, &p_current );
378             p_item_in_category = playlist_ItemToNode( p_playlist, p_current, true );
379             p_current->p_input->i_type = ITEM_TYPE_PLAYLIST;
380
381             for( i = 0; i < i_count; i++ )
382             {
383                 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
384                 char      *psz_ref;
385                 uint32_t  i_ref_type;
386
387                 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
388                 {
389                     continue;
390                 }
391                 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
392
393                 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
394                          psz_ref, (char*)&i_ref_type );
395
396                 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
397                 {
398                     if( strstr( psz_ref, "qt5gateQT" ) )
399                     {
400                         msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
401                         continue;
402                     }
403                     if( !strncmp( psz_ref, "http://", 7 ) ||
404                         !strncmp( psz_ref, "rtsp://", 7 ) )
405                     {
406                         ;
407                     }
408                     else
409                     {
410                         char *psz_absolute;
411                         char *psz_path = strdup( p_demux->psz_path );
412                         char *end = strrchr( psz_path, '/' );
413                         if( end ) end[1] = '\0';
414                         else *psz_path = '\0';
415
416                         asprintf( &psz_absolute, "%s://%s%s",
417                                       p_demux->psz_access, psz_path, psz_ref );
418
419                         free( psz_ref );
420                         psz_ref = psz_absolute;
421                         free( psz_path );
422                     }
423                     if( p_current )
424                     {
425                         input_item_t *p_input;
426                         msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
427                         p_input = input_ItemNewExt( p_playlist, psz_ref, NULL,
428                                             0, NULL, -1 );
429                         input_ItemCopyOptions( p_current->p_input, p_input );
430                         /* FIXME: playlist_BothAddInput() can fail */
431                         playlist_BothAddInput( p_playlist, p_input,
432                                                p_item_in_category,
433                                                PLAYLIST_APPEND, PLAYLIST_END,
434                                                NULL, NULL, false );
435                         vlc_gc_decref( p_input );
436                     }
437                 }
438                 else
439                 {
440                     msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
441                              (char*)&p_rdrf->data.p_rdrf->i_ref_type );
442                 }
443                 free( psz_ref );
444             }
445             if( b_play && p_item_in_category &&
446                 p_item_in_category->i_children > 0 )
447             {
448                 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true,
449                                   p_item_in_category, NULL );
450             }
451             vlc_object_release( p_playlist );
452         }
453         else
454         {
455             msg_Err( p_demux, "can't find playlist" );
456         }
457     }
458
459     if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
460     {
461         if( !p_rmra )
462         {
463             msg_Err( p_demux, "cannot find /moov/mvhd" );
464             goto error;
465         }
466         else
467         {
468             msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
469             p_demux->pf_demux = DemuxRef;
470             return VLC_SUCCESS;
471         }
472     }
473     else
474     {
475         p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
476         if( p_sys->i_timescale == 0 )
477         {
478             msg_Err( p_this, "bad timescale" );
479             goto error;
480         }
481         p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
482     }
483
484     if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
485     {
486         msg_Err( p_demux, "cannot find any /moov/trak" );
487         goto error;
488     }
489     msg_Dbg( p_demux, "found %d track%c",
490                         p_sys->i_tracks,
491                         p_sys->i_tracks ? 's':' ' );
492
493     /* allocate memory */
494     p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
495     if( p_sys->track == NULL )
496         goto error;
497     memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
498
499     /* Search the first chap reference (like quicktime) */
500     p_sys->p_tref_chap = NULL;
501     for( i = 0; i < p_sys->i_tracks; i++ )
502     {
503         MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
504         MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
505         if( !p_chap || p_chap->data.p_tref_generic->i_entry_count <= 0 )
506             continue;
507
508         p_sys->p_tref_chap = p_chap;
509         break;
510     }
511
512     /* now process each track and extract all usefull information */
513     for( i = 0; i < p_sys->i_tracks; i++ )
514     {
515         p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
516         MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
517
518         if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
519         {
520             const char *psz_cat;
521             switch( p_sys->track[i].fmt.i_cat )
522             {
523                 case( VIDEO_ES ):
524                     psz_cat = "video";
525                     break;
526                 case( AUDIO_ES ):
527                     psz_cat = "audio";
528                     break;
529                 case( SPU_ES ):
530                     psz_cat = "subtitle";
531                     break;
532
533                 default:
534                     psz_cat = "unknown";
535                     break;
536             }
537
538             msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
539                      p_sys->track[i].i_track_ID, psz_cat,
540                      p_sys->track[i].b_enable ? "enable":"disable",
541                      p_sys->track[i].fmt.psz_language ?
542                      p_sys->track[i].fmt.psz_language : "undef" );
543         }
544         else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
545         {
546             msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
547                      p_sys->track[i].i_track_ID,
548                      p_sys->track[i].fmt.psz_language ?
549                      p_sys->track[i].fmt.psz_language : "undef" );
550         }
551         else
552         {
553             msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
554                      p_sys->track[i].i_track_ID );
555         }
556     }
557
558     /* */
559     LoadChapter( p_demux );
560
561     return VLC_SUCCESS;
562
563 error:
564     if( p_sys->p_root )
565     {
566         MP4_BoxFree( p_demux->s, p_sys->p_root );
567     }
568     free( p_sys );
569     return VLC_EGENERIC;
570 }
571
572 /*****************************************************************************
573  * Demux: read packet and send them to decoders
574  *****************************************************************************
575  * TODO check for newly selected track (ie audio upt to now )
576  *****************************************************************************/
577 static int Demux( demux_t *p_demux )
578 {
579     demux_sys_t *p_sys = p_demux->p_sys;
580     unsigned int i_track;
581
582
583     unsigned int i_track_selected;
584
585     /* check for newly selected/unselected track */
586     for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
587          i_track++ )
588     {
589         mp4_track_t *tk = &p_sys->track[i_track];
590         bool b;
591
592         if( !tk->b_ok || tk->b_chapter ||
593             ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
594         {
595             continue;
596         }
597
598         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
599
600         if( tk->b_selected && !b )
601         {
602             MP4_TrackUnselect( p_demux, tk );
603         }
604         else if( !tk->b_selected && b)
605         {
606             MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
607         }
608
609         if( tk->b_selected )
610         {
611             i_track_selected++;
612         }
613     }
614
615     if( i_track_selected <= 0 )
616     {
617         p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
618         if( p_sys->i_timescale > 0 )
619         {
620             int64_t i_length = (mtime_t)1000000 *
621                                (mtime_t)p_sys->i_duration /
622                                (mtime_t)p_sys->i_timescale;
623             if( MP4_GetMoviePTS( p_sys ) >= i_length )
624                 return 0;
625             return 1;
626         }
627
628         msg_Warn( p_demux, "no track selected, exiting..." );
629         return 0;
630     }
631
632     /* */
633     MP4_UpdateSeekpoint( p_demux );
634
635     /* first wait for the good time to read a packet */
636     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
637
638     p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
639
640     /* we will read 100ms for each stream so ...*/
641     p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
642
643     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
644     {
645         mp4_track_t *tk = &p_sys->track[i_track];
646
647         if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
648             continue;
649
650         while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
651         {
652 #if 0
653             msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
654                      MP4_TrackGetDTS( p_demux, tk ),
655                      MP4_GetMoviePTS( p_sys ) );
656 #endif
657
658             if( MP4_TrackSampleSize( tk ) > 0 )
659             {
660                 block_t *p_block;
661                 int64_t i_delta;
662
663                 /* go,go go ! */
664                 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
665                 {
666                     msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
667                               tk->i_track_ID );
668                     MP4_TrackUnselect( p_demux, tk );
669                     break;
670                 }
671
672                 /* now read pes */
673                 if( !(p_block =
674                          stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
675                 {
676                     msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
677                               tk->i_track_ID );
678                     MP4_TrackUnselect( p_demux, tk );
679                     break;
680                 }
681
682                 if( tk->b_drms && tk->p_drms )
683                 {
684                     drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
685                                   p_block->i_buffer );
686                 }
687                 else if( tk->fmt.i_cat == SPU_ES )
688                 {
689                     if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
690                         p_block->i_buffer >= 2 )
691                     {
692                         uint16_t i_size = GetWBE( p_block->p_buffer );
693
694                         if( i_size + 2 <= p_block->i_buffer )
695                         {
696                             char *p;
697                             /* remove the length field, and append a '\0' */
698                             memmove( &p_block->p_buffer[0],
699                                      &p_block->p_buffer[2], i_size );
700                             p_block->p_buffer[i_size] = '\0';
701                             p_block->i_buffer = i_size + 1;
702
703                             /* convert \r -> \n */
704                             while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
705                             {
706                                 *p = '\n';
707                             }
708                         }
709                         else
710                         {
711                             /* Invalid */
712                             p_block->i_buffer = 0;
713                         }
714                     }
715                 }
716                 /* dts */
717                 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
718                 /* pts */
719                 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
720                 if( i_delta != -1 )
721                     p_block->i_pts = p_block->i_dts + i_delta;
722                 else if( tk->fmt.i_cat != VIDEO_ES )
723                     p_block->i_pts = p_block->i_dts;
724                 else
725                     p_block->i_pts = 0;
726
727                 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
728                     es_out_Send( p_demux->out, tk->p_es, p_block );
729             }
730
731             /* Next sample */
732             if( MP4_TrackNextSample( p_demux, tk ) )
733                 break;
734         }
735     }
736
737     return 1;
738 }
739
740 static void MP4_UpdateSeekpoint( demux_t *p_demux )
741 {
742     demux_sys_t *p_sys = p_demux->p_sys;
743     int64_t i_time;
744     int i;
745     if( !p_sys->p_title )
746         return;
747     i_time = MP4_GetMoviePTS( p_sys );
748     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
749     {
750         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
751             break;
752     }
753     i--;
754
755     if( i != p_demux->info.i_seekpoint && i >= 0 )
756     {
757         p_demux->info.i_seekpoint = i;
758         p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
759     }
760 }
761 /*****************************************************************************
762  * Seek: Got to i_date
763 ******************************************************************************/
764 static int Seek( demux_t *p_demux, mtime_t i_date )
765 {
766     demux_sys_t *p_sys = p_demux->p_sys;
767     unsigned int i_track;
768
769     /* First update update global time */
770     p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
771     p_sys->i_pcr  = i_date;
772
773     /* Now for each stream try to go to this time */
774     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
775     {
776         mp4_track_t *tk = &p_sys->track[i_track];
777         MP4_TrackSeek( p_demux, tk, i_date );
778     }
779     MP4_UpdateSeekpoint( p_demux );
780     return VLC_SUCCESS;
781 }
782
783 /*****************************************************************************
784  * Control:
785  *****************************************************************************/
786 static int Control( demux_t *p_demux, int i_query, va_list args )
787 {
788     demux_sys_t *p_sys = p_demux->p_sys;
789
790     double f, *pf;
791     int64_t i64, *pi64;
792
793     switch( i_query )
794     {
795         case DEMUX_GET_POSITION:
796             pf = (double*)va_arg( args, double * );
797             if( p_sys->i_duration > 0 )
798             {
799                 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
800             }
801             else
802             {
803                 *pf = 0.0;
804             }
805             return VLC_SUCCESS;
806
807         case DEMUX_SET_POSITION:
808             f = (double)va_arg( args, double );
809             if( p_sys->i_timescale > 0 )
810             {
811                 i64 = (int64_t)( f * (double)1000000 *
812                                  (double)p_sys->i_duration /
813                                  (double)p_sys->i_timescale );
814                 return Seek( p_demux, i64 );
815             }
816             else return VLC_SUCCESS;
817
818         case DEMUX_GET_TIME:
819             pi64 = (int64_t*)va_arg( args, int64_t * );
820             if( p_sys->i_timescale > 0 )
821             {
822                 *pi64 = (mtime_t)1000000 *
823                         (mtime_t)p_sys->i_time /
824                         (mtime_t)p_sys->i_timescale;
825             }
826             else *pi64 = 0;
827             return VLC_SUCCESS;
828
829         case DEMUX_SET_TIME:
830             i64 = (int64_t)va_arg( args, int64_t );
831             return Seek( p_demux, i64 );
832
833         case DEMUX_GET_LENGTH:
834             pi64 = (int64_t*)va_arg( args, int64_t * );
835             if( p_sys->i_timescale > 0 )
836             {
837                 *pi64 = (mtime_t)1000000 *
838                         (mtime_t)p_sys->i_duration /
839                         (mtime_t)p_sys->i_timescale;
840             }
841             else *pi64 = 0;
842             return VLC_SUCCESS;
843
844         case DEMUX_GET_FPS:
845             msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
846             return VLC_EGENERIC;
847
848         case DEMUX_GET_META:
849         {
850             vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
851             MP4_Box_t  *p_0xa9xxx;
852
853             MP4_Box_t  *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
854             if( p_udta == NULL )
855             {
856                 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
857                 if( p_udta == NULL )
858                 {
859                     return VLC_EGENERIC;
860                 }
861             }
862
863             for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
864                  p_0xa9xxx = p_0xa9xxx->p_next )
865             {
866
867                 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
868                     continue;
869
870                 /* FIXME FIXME: should convert from whatever the character
871                  * encoding of MP4 meta data is to UTF-8. */
872 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
873     if( psz_utf ) { EnsureUTF8( psz_utf );  \
874                     fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
875
876                 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
877                 switch( p_0xa9xxx->i_type )
878                 {
879                 case FOURCC_0xa9nam: /* Full name */
880                     SET( vlc_meta_SetTitle );
881                     break;
882                 case FOURCC_0xa9aut:
883                     SET( vlc_meta_SetArtist );
884                     break;
885                 case FOURCC_0xa9ART:
886                     SET( vlc_meta_SetArtist );
887                     break;
888                 case FOURCC_0xa9cpy:
889                     SET( vlc_meta_SetCopyright );
890                     break;
891                 case FOURCC_0xa9day: /* Creation Date */
892                     SET( vlc_meta_SetDate );
893                     break;
894                 case FOURCC_0xa9des: /* Description */
895                     SET( vlc_meta_SetDescription );
896                     break;
897                 case FOURCC_0xa9gen: /* Genre */
898                     SET( vlc_meta_SetGenre );
899                     break;
900
901                 case FOURCC_0xa9alb: /* Album */
902                     SET( vlc_meta_SetAlbum );
903                     break;
904
905                 case FOURCC_0xa9trk: /* Track */
906                     SET( vlc_meta_SetTracknum );
907                     break;
908
909                 case FOURCC_0xa9cmt: /* Commment */
910                     SET( vlc_meta_SetDescription );
911                     break;
912
913                 case FOURCC_0xa9url: /* URL */
914                     SET( vlc_meta_SetURL );
915                     break;
916
917                 case FOURCC_0xa9enc: /* Encoded By */
918                     SET( vlc_meta_SetEncodedBy );
919                     break;
920                     
921                 case FOURCC_0xa9swr:
922                 case FOURCC_0xa9inf: /* Information */
923                 case FOURCC_0xa9dir: /* Director */
924                 case FOURCC_0xa9dis: /* Disclaimer */
925                 case FOURCC_0xa9req: /* Requirements */
926                 case FOURCC_0xa9fmt: /* Original Format */
927                 case FOURCC_0xa9dsa: /* Display Source As */
928                 case FOURCC_0xa9hst: /* Host Computer */
929                 case FOURCC_0xa9prd: /* Producer */
930                 case FOURCC_0xa9prf: /* Performers */
931                 case FOURCC_0xa9ope: /* Original Performer */
932                 case FOURCC_0xa9src: /* Providers Source Content */
933                 case FOURCC_0xa9wrt: /* Writer */
934                 case FOURCC_0xa9com: /* Composer */
935                 case FOURCC_WLOC:    /* Window Location */
936                     /* TODO one day, but they aren't really meaningfull */
937                     break;
938 #undef SET
939
940                 default:
941                     break;
942                 }
943             }
944             return VLC_SUCCESS;
945         }
946
947         case DEMUX_GET_TITLE_INFO:
948         {
949             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
950             int *pi_int    = (int*)va_arg( args, int* );
951             int *pi_title_offset = (int*)va_arg( args, int* );
952             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
953
954             if( !p_sys->p_title )
955                 return VLC_EGENERIC;
956
957             *pi_int = 1;
958             *ppp_title = malloc( sizeof( input_title_t**) );
959             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
960             *pi_title_offset = 0;
961             *pi_seekpoint_offset = 0;
962             return VLC_SUCCESS;
963         }
964         case DEMUX_SET_TITLE:
965         {
966             const int i_title = (int)va_arg( args, int );
967             if( !p_sys->p_title || i_title != 0 )
968                 return VLC_EGENERIC;
969             return VLC_SUCCESS;
970         }
971         case DEMUX_SET_SEEKPOINT:
972         {
973             const int i_seekpoint = (int)va_arg( args, int );
974             if( !p_sys->p_title )
975                 return VLC_EGENERIC;
976             return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
977         }
978
979         case DEMUX_SET_NEXT_DEMUX_TIME:
980         case DEMUX_SET_GROUP:
981         case DEMUX_HAS_UNSUPPORTED_META:
982         case DEMUX_GET_ATTACHMENTS:
983             return VLC_EGENERIC;
984
985         default:
986             msg_Warn( p_demux, "control query %u unimplemented", i_query );
987             return VLC_EGENERIC;
988     }
989 }
990
991 /*****************************************************************************
992  * Close: frees unused data
993  *****************************************************************************/
994 static void Close ( vlc_object_t * p_this )
995 {
996     demux_t *  p_demux = (demux_t *)p_this;
997     demux_sys_t *p_sys = p_demux->p_sys;
998     unsigned int i_track;
999
1000     msg_Dbg( p_demux, "freeing all memory" );
1001
1002     MP4_BoxFree( p_demux->s, p_sys->p_root );
1003     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1004     {
1005         MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
1006     }
1007     FREENULL( p_sys->track );
1008
1009     if( p_sys->p_title )
1010         vlc_input_title_Delete( p_sys->p_title );
1011
1012     free( p_sys );
1013 }
1014
1015
1016
1017 /****************************************************************************
1018  * Local functions, specific to vlc
1019  ****************************************************************************/
1020 /* Chapters */
1021 static void LoadChapterGpac( demux_t  *p_demux, MP4_Box_t *p_chpl )
1022 {
1023     demux_sys_t *p_sys = p_demux->p_sys;
1024     int i;
1025
1026     p_sys->p_title = vlc_input_title_New();
1027     for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1028     {
1029         seekpoint_t *s = vlc_seekpoint_New();
1030
1031         s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1032         EnsureUTF8( s->psz_name );
1033         s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1034         TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1035     }
1036 }
1037 static void LoadChapterApple( demux_t  *p_demux, mp4_track_t *tk )
1038 {
1039     demux_sys_t *p_sys = p_demux->p_sys;
1040
1041     for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1042     {
1043         const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1044         const int64_t i_pts_delta = MP4_TrackGetPTSDelta( p_demux, tk );
1045         const unsigned int i_size = MP4_TrackSampleSize( tk );
1046
1047         if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1048         {
1049             char p_buffer[256];
1050             const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1051             const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1052
1053             if( i_len > 0 )
1054             {
1055                 seekpoint_t *s = vlc_seekpoint_New();
1056
1057                 s->psz_name = strndup( &p_buffer[2], i_len );
1058                 EnsureUTF8( s->psz_name );
1059
1060                 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1061
1062                 if( !p_sys->p_title )
1063                     p_sys->p_title = vlc_input_title_New();
1064                 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1065             }
1066         }
1067         if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1068                               tk->chunk[tk->i_chunk].i_sample_count )
1069             tk->i_chunk++;
1070     }
1071 }
1072 static void LoadChapter( demux_t  *p_demux )
1073 {
1074     demux_sys_t *p_sys = p_demux->p_sys;
1075     MP4_Box_t *p_chpl;
1076
1077     if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1078     {
1079         LoadChapterGpac( p_demux, p_chpl );
1080     }
1081     else if( p_sys->p_tref_chap )
1082     {
1083         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1084         unsigned int i, j;
1085
1086         /* Load the first subtitle track like quicktime */
1087         for( i = 0; i < p_chap->i_entry_count; i++ )
1088         {
1089             for( j = 0; j < p_sys->i_tracks; j++ )
1090             {
1091                 mp4_track_t *tk = &p_sys->track[j];
1092                 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1093                     tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1094                     break;
1095             }
1096             if( j < p_sys->i_tracks )
1097             {
1098                 LoadChapterApple( p_demux, &p_sys->track[i] );
1099                 break;
1100             }
1101         }
1102     }
1103 }
1104
1105 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1106 static int TrackCreateChunksIndex( demux_t *p_demux,
1107                                    mp4_track_t *p_demux_track )
1108 {
1109     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1110     MP4_Box_t *p_stsc;
1111
1112     unsigned int i_chunk;
1113     unsigned int i_index, i_last;
1114
1115     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1116           !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1117         ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1118     {
1119         return( VLC_EGENERIC );
1120     }
1121
1122     p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1123     if( !p_demux_track->i_chunk_count )
1124     {
1125         msg_Warn( p_demux, "no chunk defined" );
1126         return( VLC_EGENERIC );
1127     }
1128     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1129                                    sizeof( mp4_chunk_t ) );
1130     if( p_demux_track->chunk == NULL )
1131     {
1132         return VLC_ENOMEM;
1133     }
1134
1135     /* first we read chunk offset */
1136     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1137     {
1138         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1139
1140         ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1141
1142         ck->i_first_dts = 0;
1143         ck->p_sample_count_dts = NULL;
1144         ck->p_sample_delta_dts = NULL;
1145         ck->p_sample_count_pts = NULL;
1146         ck->p_sample_offset_pts = NULL;
1147     }
1148
1149     /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1150         to be used for the sample XXX begin to 1
1151         We construct it begining at the end */
1152     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1153     i_index = p_stsc->data.p_stsc->i_entry_count;
1154     if( !i_index )
1155     {
1156         msg_Warn( p_demux, "cannot read chunk table or table empty" );
1157         return( VLC_EGENERIC );
1158     }
1159
1160     while( i_index-- )
1161     {
1162         for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1163              i_chunk < i_last; i_chunk++ )
1164         {
1165             if( i_chunk >= p_demux_track->i_chunk_count )
1166             {
1167                 msg_Warn( p_demux, "corrupted chunk table" );
1168                 return VLC_EGENERIC;
1169             }
1170
1171             p_demux_track->chunk[i_chunk].i_sample_description_index =
1172                     p_stsc->data.p_stsc->i_sample_description_index[i_index];
1173             p_demux_track->chunk[i_chunk].i_sample_count =
1174                     p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1175         }
1176         i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1177     }
1178
1179     p_demux_track->chunk[0].i_sample_first = 0;
1180     for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1181     {
1182         p_demux_track->chunk[i_chunk].i_sample_first =
1183             p_demux_track->chunk[i_chunk-1].i_sample_first +
1184                 p_demux_track->chunk[i_chunk-1].i_sample_count;
1185     }
1186
1187     msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1188              p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1189
1190     return VLC_SUCCESS;
1191 }
1192
1193 static int TrackCreateSamplesIndex( demux_t *p_demux,
1194                                     mp4_track_t *p_demux_track )
1195 {
1196     MP4_Box_t *p_box;
1197     MP4_Box_data_stsz_t *stsz;
1198     MP4_Box_data_stts_t *stts;
1199     /* TODO use also stss and stsh table for seeking */
1200     /* FIXME use edit table */
1201     int64_t i_sample;
1202     int64_t i_chunk;
1203
1204     int64_t i_index;
1205     int64_t i_index_sample_used;
1206
1207     int64_t i_last_dts;
1208
1209     /* Find stsz
1210      *  Gives the sample size for each samples. There is also a stz2 table
1211      *  (compressed form) that we need to implement TODO */
1212     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1213     if( !p_box )
1214     {
1215         /* FIXME and stz2 */
1216         msg_Warn( p_demux, "cannot find STSZ box" );
1217         return VLC_EGENERIC;
1218     }
1219     stsz = p_box->data.p_stsz;
1220
1221     /* Find stts
1222      *  Gives mapping between sample and decoding time
1223      */
1224     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1225     if( !p_box )
1226     {
1227         msg_Warn( p_demux, "cannot find STTS box" );
1228         return VLC_EGENERIC;
1229     }
1230     stts = p_box->data.p_stts;
1231
1232     /* Use stsz table to create a sample number -> sample size table */
1233     p_demux_track->i_sample_count = stsz->i_sample_count;
1234     if( stsz->i_sample_size )
1235     {
1236         /* 1: all sample have the same size, so no need to construct a table */
1237         p_demux_track->i_sample_size = stsz->i_sample_size;
1238         p_demux_track->p_sample_size = NULL;
1239     }
1240     else
1241     {
1242         /* 2: each sample can have a different size */
1243         p_demux_track->i_sample_size = 0;
1244         p_demux_track->p_sample_size =
1245             calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1246         if( p_demux_track->p_sample_size == NULL )
1247             return VLC_ENOMEM;
1248
1249         for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1250         {
1251             p_demux_track->p_sample_size[i_sample] =
1252                     stsz->i_entry_size[i_sample];
1253         }
1254     }
1255
1256     /* Use stts table to create a sample number -> dts table.
1257      * XXX: if we don't want to waste too much memory, we can't expand
1258      *  the box! so each chunk will contain an "extract" of this table
1259      *  for fast research (problem with raw stream where a sample is sometime
1260      *  just channels*bits_per_sample/8 */
1261
1262     i_last_dts = 0;
1263     i_index = 0; i_index_sample_used = 0;
1264     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1265     {
1266         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1267         int64_t i_entry, i_sample_count, i;
1268
1269         /* save last dts */
1270         ck->i_first_dts = i_last_dts;
1271
1272         /* count how many entries are needed for this chunk
1273          * for p_sample_delta_dts and p_sample_count_dts */
1274         i_sample_count = ck->i_sample_count;
1275
1276         i_entry = 0;
1277         while( i_sample_count > 0 )
1278         {
1279             i_sample_count -= stts->i_sample_count[i_index+i_entry];
1280             /* don't count already used sample in this entry */
1281             if( i_entry == 0 )
1282                 i_sample_count += i_index_sample_used;
1283
1284             i_entry++;
1285         }
1286
1287         /* allocate them */
1288         ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1289         ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1290
1291         /* now copy */
1292         i_sample_count = ck->i_sample_count;
1293         for( i = 0; i < i_entry; i++ )
1294         {
1295             int64_t i_used;
1296             int64_t i_rest;
1297
1298             i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1299
1300             i_used = __MIN( i_rest, i_sample_count );
1301
1302             i_index_sample_used += i_used;
1303             i_sample_count -= i_used;
1304
1305             ck->p_sample_count_dts[i] = i_used;
1306             ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1307
1308             i_last_dts += i_used * ck->p_sample_delta_dts[i];
1309
1310             if( i_index_sample_used >= stts->i_sample_count[i_index] )
1311             {
1312                 i_index++;
1313                 i_index_sample_used = 0;
1314             }
1315         }
1316     }
1317
1318     /* Find ctts
1319      *  Gives the delta between decoding time (dts) and composition table (pts)
1320      */
1321     p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1322     if( p_box )
1323     {
1324         MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1325
1326         msg_Warn( p_demux, "CTTS table" );
1327
1328         /* Create pts-dts table per chunk */
1329         i_index = 0; i_index_sample_used = 0;
1330         for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1331         {
1332             mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1333             int64_t i_entry, i_sample_count, i;
1334
1335             /* count how many entries are needed for this chunk
1336              * for p_sample_delta_dts and p_sample_count_dts */
1337             i_sample_count = ck->i_sample_count;
1338
1339             i_entry = 0;
1340             while( i_sample_count > 0 )
1341             {
1342                 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1343
1344                 /* don't count already used sample in this entry */
1345                 if( i_entry == 0 )
1346                     i_sample_count += i_index_sample_used;
1347
1348                 i_entry++;
1349             }
1350
1351             /* allocate them */
1352             ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1353             ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1354
1355             /* now copy */
1356             i_sample_count = ck->i_sample_count;
1357             for( i = 0; i < i_entry; i++ )
1358             {
1359                 int64_t i_used;
1360                 int64_t i_rest;
1361
1362                 i_rest = ctts->i_sample_count[i_index] -
1363                     i_index_sample_used;
1364
1365                 i_used = __MIN( i_rest, i_sample_count );
1366
1367                 i_index_sample_used += i_used;
1368                 i_sample_count -= i_used;
1369
1370                 ck->p_sample_count_pts[i] = i_used;
1371                 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1372
1373                 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1374                 {
1375                     i_index++;
1376                     i_index_sample_used = 0;
1377                 }
1378             }
1379         }
1380     }
1381
1382     msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1383              p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1384              i_last_dts / p_demux_track->i_timescale );
1385
1386     return VLC_SUCCESS;
1387 }
1388
1389 /*
1390  * TrackCreateES:
1391  * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1392  */
1393 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1394                           unsigned int i_chunk, es_out_id_t **pp_es )
1395 {
1396     MP4_Box_t   *p_sample;
1397     MP4_Box_t   *p_esds;
1398     MP4_Box_t   *p_box;
1399
1400     if( pp_es )
1401         *pp_es = NULL;
1402
1403     if( !p_track->chunk[i_chunk].i_sample_description_index )
1404     {
1405         msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1406                   p_track->i_track_ID );
1407         return VLC_EGENERIC;
1408     }
1409
1410     p_sample = MP4_BoxGet(  p_track->p_stsd, "[%d]",
1411                 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1412
1413     if( !p_sample ||
1414         ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1415     {
1416         msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1417                   p_track->i_track_ID );
1418         return VLC_EGENERIC;
1419     }
1420
1421     p_track->p_sample = p_sample;
1422
1423     if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1424     {
1425         MP4_Box_data_sample_soun_t *p_soun;
1426
1427         p_soun = p_sample->data.p_sample_soun;
1428
1429         if( p_soun->i_qt_version == 0 )
1430         {
1431             switch( p_sample->i_type )
1432             {
1433                 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1434                     p_soun->i_qt_version = 1;
1435                     p_soun->i_sample_per_packet = 64;
1436                     p_soun->i_bytes_per_packet  = 34;
1437                     p_soun->i_bytes_per_frame   = 34 * p_soun->i_channelcount;
1438                     p_soun->i_bytes_per_sample  = 2;
1439                     break;
1440                 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1441                     p_soun->i_qt_version = 1;
1442                     p_soun->i_sample_per_packet = 6;
1443                     p_soun->i_bytes_per_packet  = 2;
1444                     p_soun->i_bytes_per_frame   = 2 * p_soun->i_channelcount;
1445                     p_soun->i_bytes_per_sample  = 2;
1446                     break;
1447                 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1448                     p_soun->i_qt_version = 1;
1449                     p_soun->i_sample_per_packet = 12;
1450                     p_soun->i_bytes_per_packet  = 2;
1451                     p_soun->i_bytes_per_frame   = 2 * p_soun->i_channelcount;
1452                     p_soun->i_bytes_per_sample  = 2;
1453                     break;
1454                 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1455                 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1456                     p_soun->i_samplesize = 8;
1457                     break;
1458                 default:
1459                     break;
1460             }
1461
1462         }
1463         else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1464         {
1465             p_soun->i_qt_version = 0;
1466         }
1467     }
1468
1469
1470     /* It's a little ugly but .. there are special cases */
1471     switch( p_sample->i_type )
1472     {
1473         case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1474         case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1475             p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1476             break;
1477
1478         case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1479             p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1480
1481             /* Buggy files workaround */
1482             if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1483                 p_sample->data.p_sample_soun->i_sampleratehi) )
1484             {
1485                 MP4_Box_data_sample_soun_t *p_soun =
1486                     p_sample->data.p_sample_soun;
1487
1488                 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1489                           "(%u), making both equal (report any problem).",
1490                           p_track->i_timescale, p_soun->i_sampleratehi );
1491
1492                 if( p_soun->i_sampleratehi )
1493                     p_track->i_timescale = p_soun->i_sampleratehi;
1494                 else
1495                     p_soun->i_sampleratehi = p_track->i_timescale;
1496             }
1497             break;
1498
1499         case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1500             p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1501             break;
1502
1503         case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1504         case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1505             p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1506             /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1507             /* FIXME UTF-8 doesn't work here ? */
1508             p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1509             break;
1510
1511         case VLC_FOURCC('y','v','1','2'):
1512             p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1513             break;
1514         case VLC_FOURCC('y','u','v','2'):
1515             p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1516             break;
1517
1518         case VLC_FOURCC('i','n','2','4'):
1519             /* in in24/in32 there's enda-atom to tell it's little-endian (if present) */
1520             if( ( MP4_BoxGet( p_sample, "wave/enda" ) ) ||
1521                 ( MP4_BoxGet( p_sample, "enda" ) ) )
1522             {
1523                 p_track->fmt.i_codec = VLC_FOURCC('4','2','n','i');
1524             } else {
1525                 p_track->fmt.i_codec = p_sample->i_type;
1526             }
1527             break;
1528
1529         default:
1530             p_track->fmt.i_codec = p_sample->i_type;
1531             break;
1532     }
1533
1534     /* now see if esds is present and if so create a data packet
1535         with decoder_specific_info  */
1536 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1537     if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1538           ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1539         ( p_esds->data.p_esds )&&
1540         ( p_decconfig ) )
1541     {
1542         /* First update information based on i_objectTypeIndication */
1543         switch( p_decconfig->i_objectTypeIndication )
1544         {
1545             case( 0x20 ): /* MPEG4 VIDEO */
1546                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1547                 break;
1548             case( 0x40):
1549                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1550                 break;
1551             case( 0x60):
1552             case( 0x61):
1553             case( 0x62):
1554             case( 0x63):
1555             case( 0x64):
1556             case( 0x65): /* MPEG2 video */
1557                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1558                 break;
1559             /* Theses are MPEG2-AAC */
1560             case( 0x66): /* main profile */
1561             case( 0x67): /* Low complexity profile */
1562             case( 0x68): /* Scaleable Sampling rate profile */
1563                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1564                 break;
1565             /* true MPEG 2 audio */
1566             case( 0x69):
1567                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1568                 break;
1569             case( 0x6a): /* MPEG1 video */
1570                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1571                 break;
1572             case( 0x6b): /* MPEG1 audio */
1573                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1574                 break;
1575             case( 0x6c ): /* jpeg */
1576                 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1577                 break;
1578
1579             /* Private ID */
1580             case( 0xe0 ): /* NeroDigital: dvd subs */
1581                 if( p_track->fmt.i_cat == SPU_ES )
1582                 {
1583                     p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1584                     if( p_track->i_width > 0 )
1585                         p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1586                     if( p_track->i_height > 0 )
1587                         p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1588                     break;
1589                 }
1590             /* Fallback */
1591             default:
1592                 /* Unknown entry, but don't touch i_fourcc */
1593                 msg_Warn( p_demux,
1594                           "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1595                           p_decconfig->i_objectTypeIndication,
1596                           p_track->i_track_ID );
1597                 break;
1598         }
1599         p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1600         if( p_track->fmt.i_extra > 0 )
1601         {
1602             p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1603             memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1604                     p_track->fmt.i_extra );
1605         }
1606     }
1607     else
1608     {
1609         switch( p_sample->i_type )
1610         {
1611             /* qt decoder, send the complete chunk */
1612             case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1613             case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1614             case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1615             case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1616             case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1617             case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1618             case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1619             case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1620             case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1621             case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1622             case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1623             case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1624                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1625                 break;
1626             /* qt decoder, send the complete chunk */
1627             case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1628             case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1629             case VLC_FOURCC( 'V', 'P', '3', '1' ):
1630             case VLC_FOURCC( '3', 'I', 'V', '1' ):
1631             case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1632                 p_track->fmt.i_extra =
1633                     p_sample->data.p_sample_vide->i_qt_image_description;
1634                 if( p_track->fmt.i_extra > 0 )
1635                 {
1636                     p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1637                     memcpy( p_track->fmt.p_extra,
1638                             p_sample->data.p_sample_vide->p_qt_image_description,
1639                             p_track->fmt.i_extra);
1640                 }
1641                 break;
1642             case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1643             case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1644             case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1645             case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1646             case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1647                 p_track->fmt.i_extra =
1648                     p_sample->data.p_sample_soun->i_qt_description;
1649                 if( p_track->fmt.i_extra > 0 )
1650                 {
1651                     p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1652                     memcpy( p_track->fmt.p_extra,
1653                             p_sample->data.p_sample_soun->p_qt_description,
1654                             p_track->fmt.i_extra);
1655                 }
1656                 break;
1657
1658             /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1659             case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1660             {
1661                 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1662
1663                 if( p_avcC )
1664                 {
1665                     p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1666                     p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1667                     memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1668                             p_track->fmt.i_extra );
1669                 }
1670                 else
1671                 {
1672                     msg_Err( p_demux, "missing avcC" );
1673                 }
1674                 break;
1675             }
1676
1677             default:
1678                 break;
1679         }
1680     }
1681
1682 #undef p_decconfig
1683
1684     /* some last initialisation */
1685     switch( p_track->fmt.i_cat )
1686     {
1687     case( VIDEO_ES ):
1688         p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1689         p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1690         p_track->fmt.video.i_bits_per_pixel =
1691             p_sample->data.p_sample_vide->i_depth;
1692
1693         /* fall on display size */
1694         if( p_track->fmt.video.i_width <= 0 )
1695             p_track->fmt.video.i_width = p_track->i_width;
1696         if( p_track->fmt.video.i_height <= 0 )
1697             p_track->fmt.video.i_height = p_track->i_height;
1698
1699         /* Find out apect ratio from display size */
1700         if( p_track->i_width > 0 && p_track->i_height > 0 &&
1701             /* Work-around buggy muxed files */
1702             p_sample->data.p_sample_vide->i_width != p_track->i_width )
1703             p_track->fmt.video.i_aspect =
1704                 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1705
1706         /* Support for cropping (eg. in H263 files) */
1707         p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1708         p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1709
1710         /* Frame rate */
1711         p_track->fmt.video.i_frame_rate = p_track->i_timescale;
1712         p_track->fmt.video.i_frame_rate_base = 1;
1713
1714         if( p_track->fmt.video.i_frame_rate &&
1715             (p_box = MP4_BoxGet( p_track->p_stbl, "stts" )) &&
1716             p_box->data.p_stts->i_entry_count >= 1 )
1717         {
1718             p_track->fmt.video.i_frame_rate_base =
1719                 p_box->data.p_stts->i_sample_delta[0];
1720         }
1721
1722         break;
1723
1724     case( AUDIO_ES ):
1725         p_track->fmt.audio.i_channels =
1726             p_sample->data.p_sample_soun->i_channelcount;
1727         p_track->fmt.audio.i_rate =
1728             p_sample->data.p_sample_soun->i_sampleratehi;
1729         p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1730             p_sample->data.p_sample_soun->i_sampleratehi *
1731                 p_sample->data.p_sample_soun->i_samplesize;
1732         p_track->fmt.audio.i_bitspersample =
1733             p_sample->data.p_sample_soun->i_samplesize;
1734         break;
1735
1736     default:
1737         break;
1738     }
1739
1740     if( pp_es )
1741         *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1742
1743     return VLC_SUCCESS;
1744 }
1745
1746 /* given a time it return sample/chunk
1747  * it also update elst field of the track
1748  */
1749 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1750                                    int64_t i_start, uint32_t *pi_chunk,
1751                                    uint32_t *pi_sample )
1752 {
1753     demux_sys_t *p_sys = p_demux->p_sys;
1754     MP4_Box_t   *p_stss;
1755     uint64_t     i_dts;
1756     unsigned int i_sample;
1757     unsigned int i_chunk;
1758     int          i_index;
1759
1760     /* FIXME see if it's needed to check p_track->i_chunk_count */
1761     if( p_track->i_chunk_count == 0 )
1762         return( VLC_EGENERIC );
1763
1764     /* handle elst (find the correct one) */
1765     MP4_TrackSetELST( p_demux, p_track, i_start );
1766     if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1767     {
1768         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1769         int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1770
1771         /* now calculate i_start for this elst */
1772         /* offset */
1773         i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
1774         if( i_start < 0 )
1775         {
1776             *pi_chunk = 0;
1777             *pi_sample= 0;
1778
1779             return VLC_SUCCESS;
1780         }
1781         /* to track time scale */
1782         i_start  = i_start * p_track->i_timescale / (int64_t)1000000;
1783         /* add elst offset */
1784         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1785              elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1786             elst->i_media_time[p_track->i_elst] > 0 )
1787         {
1788             i_start += elst->i_media_time[p_track->i_elst];
1789         }
1790
1791         msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
1792                  "ms (track)", p_track->i_elst,
1793                  i_mvt * 1000 / p_sys->i_timescale,
1794                  i_start * 1000 / p_track->i_timescale );
1795     }
1796     else
1797     {
1798         /* convert absolute time to in timescale unit */
1799         i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1800     }
1801
1802     /* we start from sample 0/chunk 0, hope it won't take too much time */
1803     /* *** find good chunk *** */
1804     for( i_chunk = 0; ; i_chunk++ )
1805     {
1806         if( i_chunk + 1 >= p_track->i_chunk_count )
1807         {
1808             /* at the end and can't check if i_start in this chunk,
1809                it will be check while searching i_sample */
1810             i_chunk = p_track->i_chunk_count - 1;
1811             break;
1812         }
1813
1814         if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
1815             (uint64_t)i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
1816         {
1817             break;
1818         }
1819     }
1820
1821     /* *** find sample in the chunk *** */
1822     i_sample = p_track->chunk[i_chunk].i_sample_first;
1823     i_dts    = p_track->chunk[i_chunk].i_first_dts;
1824     for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1825     {
1826         if( i_dts +
1827             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1828             p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
1829         {
1830             i_dts    +=
1831                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1832                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1833
1834             i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1835             i_index++;
1836         }
1837         else
1838         {
1839             if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1840             {
1841                 break;
1842             }
1843             i_sample += ( i_start - i_dts ) /
1844                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1845             break;
1846         }
1847     }
1848
1849     if( i_sample >= p_track->i_sample_count )
1850     {
1851         msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1852                   "(seeking too far) chunk=%d sample=%d",
1853                   p_track->i_track_ID, i_chunk, i_sample );
1854         return( VLC_EGENERIC );
1855     }
1856
1857
1858     /* *** Try to find nearest sync points *** */
1859     if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1860     {
1861         unsigned int i_index;
1862         msg_Dbg( p_demux,
1863                     "track[Id 0x%x] using Sync Sample Box (stss)",
1864                     p_track->i_track_ID );
1865         for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1866         {
1867             if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1868             {
1869                 if( i_index > 0 )
1870                 {
1871                     msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1872                             i_sample,
1873                             p_stss->data.p_stss->i_sample_number[i_index-1] );
1874                     i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1875                     /* new i_sample is less than old so i_chunk can only decreased */
1876                     while( i_chunk > 0 &&
1877                             i_sample < p_track->chunk[i_chunk].i_sample_first )
1878                     {
1879                         i_chunk--;
1880                     }
1881                 }
1882                 else
1883                 {
1884                     msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1885                             i_sample,
1886                             p_stss->data.p_stss->i_sample_number[i_index] );
1887                     i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1888                     /* new i_sample is more than old so i_chunk can only increased */
1889                     while( i_chunk < p_track->i_chunk_count - 1 &&
1890                            i_sample >= p_track->chunk[i_chunk].i_sample_first +
1891                              p_track->chunk[i_chunk].i_sample_count )
1892                     {
1893                         i_chunk++;
1894                     }
1895                 }
1896                 break;
1897             }
1898         }
1899     }
1900     else
1901     {
1902         msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1903                  "Sample Box (stss)", p_track->i_track_ID );
1904     }
1905
1906     *pi_chunk  = i_chunk;
1907     *pi_sample = i_sample;
1908
1909     return VLC_SUCCESS;
1910 }
1911
1912 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1913                                  unsigned int i_chunk, unsigned int i_sample )
1914 {
1915     bool b_reselect = false;
1916
1917     /* now see if actual es is ok */
1918     if( (p_track->i_chunk >= p_track->i_chunk_count - 1) ||
1919         (p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1920             p_track->chunk[i_chunk].i_sample_description_index) )
1921     {
1922         msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1923                   p_track->i_track_ID );
1924
1925         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1926                         p_track->p_es, &b_reselect );
1927
1928         es_out_Del( p_demux->out, p_track->p_es );
1929
1930         p_track->p_es = NULL;
1931
1932         if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1933         {
1934             msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1935                      p_track->i_track_ID );
1936
1937             p_track->b_ok       = false;
1938             p_track->b_selected = false;
1939             return VLC_EGENERIC;
1940         }
1941     }
1942
1943     /* select again the new decoder */
1944     if( b_reselect )
1945     {
1946         es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1947     }
1948
1949     p_track->i_chunk    = i_chunk;
1950     p_track->i_sample   = i_sample;
1951
1952     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1953 }
1954
1955 /****************************************************************************
1956  * MP4_TrackCreate:
1957  ****************************************************************************
1958  * Parse track information and create all needed data to run a track
1959  * If it succeed b_ok is set to 1 else to 0
1960  ****************************************************************************/
1961 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1962                              MP4_Box_t *p_box_trak )
1963 {
1964     demux_sys_t *p_sys = p_demux->p_sys;
1965
1966     MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1967     MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1968     MP4_Box_t *p_elst;
1969
1970     MP4_Box_t *p_mdhd;
1971     MP4_Box_t *p_udta;
1972     MP4_Box_t *p_hdlr;
1973
1974     MP4_Box_t *p_vmhd;
1975     MP4_Box_t *p_smhd;
1976
1977     MP4_Box_t *p_drms;
1978
1979     unsigned int i;
1980     char language[4];
1981
1982     /* hint track unsupported */
1983
1984     /* set default value (-> track unusable) */
1985     p_track->b_ok       = false;
1986     p_track->b_enable   = false;
1987     p_track->b_selected = false;
1988     p_track->b_chapter  = false;
1989
1990     es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1991
1992     if( !p_tkhd )
1993     {
1994         return;
1995     }
1996
1997     /* do we launch this track by default ? */
1998     p_track->b_enable =
1999         ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2000
2001     p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2002     p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2003     p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2004
2005     if( p_tref )
2006     {
2007 /*        msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2008     }
2009
2010     p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2011     p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2012
2013     if( ( !p_mdhd )||( !p_hdlr ) )
2014     {
2015         return;
2016     }
2017
2018     p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2019     if( !p_track->i_timescale )
2020         return;
2021
2022     for( i = 0; i < 3; i++ )
2023     {
2024         language[i] = p_mdhd->data.p_mdhd->i_language[i];
2025     }
2026     language[3] = '\0';
2027
2028     switch( p_hdlr->data.p_hdlr->i_handler_type )
2029     {
2030         case( FOURCC_soun ):
2031             if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2032             {
2033                 return;
2034             }
2035             p_track->fmt.i_cat = AUDIO_ES;
2036             break;
2037
2038         case( FOURCC_vide ):
2039             if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2040             {
2041                 return;
2042             }
2043             p_track->fmt.i_cat = VIDEO_ES;
2044             break;
2045
2046         case( FOURCC_text ):
2047         case( FOURCC_subp ):
2048         case( FOURCC_tx3g ):
2049             p_track->fmt.i_cat = SPU_ES;
2050             break;
2051
2052         default:
2053             return;
2054     }
2055
2056     p_track->i_elst = 0;
2057     p_track->i_elst_time = 0;
2058     if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2059     {
2060         MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2061         unsigned int i;
2062
2063         msg_Warn( p_demux, "elst box found" );
2064         for( i = 0; i < elst->i_entry_count; i++ )
2065         {
2066             msg_Dbg( p_demux, "   - [%d] duration=%"PRId64"ms media time=%"PRId64
2067                      "ms) rate=%d.%d", i,
2068                      elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2069                      elst->i_media_time[i] >= 0 ?
2070                      (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2071                      INT64_C(-1),
2072                      elst->i_media_rate_integer[i],
2073                      elst->i_media_rate_fraction[i] );
2074         }
2075     }
2076
2077
2078 /*  TODO
2079     add support for:
2080     p_dinf = MP4_BoxGet( p_minf, "dinf" );
2081 */
2082     if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2083         !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2084     {
2085         return;
2086     }
2087
2088     p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2089     p_track->b_drms = p_drms != NULL;
2090     p_track->p_drms = p_track->b_drms ?
2091         p_drms->data.p_sample_soun->p_drms : NULL;
2092
2093     /* Set language */
2094     if( strcmp( language, "```" ) && strcmp( language, "und" ) )
2095     {
2096         p_track->fmt.psz_language = strdup( language );
2097     }
2098
2099     p_udta = MP4_BoxGet( p_box_trak, "udta" );
2100     if( p_udta )
2101     {
2102         MP4_Box_t *p_0xa9xxx;
2103         for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
2104                  p_0xa9xxx = p_0xa9xxx->p_next )
2105         {
2106             switch( p_0xa9xxx->i_type )
2107             {
2108                 case FOURCC_0xa9nam:
2109                     p_track->fmt.psz_description =
2110                         strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
2111                     break;
2112             }
2113         }
2114     }
2115
2116     /* Create chunk index table and sample index table */
2117     if( TrackCreateChunksIndex( p_demux,p_track  ) ||
2118         TrackCreateSamplesIndex( p_demux, p_track ) )
2119     {
2120         return; /* cannot create chunks index */
2121     }
2122
2123     p_track->i_chunk  = 0;
2124     p_track->i_sample = 0;
2125
2126     /* Mark chapter only track */
2127     if( !p_track->b_enable && p_sys->p_tref_chap )
2128     {
2129         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2130         unsigned int i;
2131
2132         for( i = 0; i < p_chap->i_entry_count; i++ )
2133         {
2134             if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2135             {
2136                 p_track->b_chapter = true;
2137                 break;
2138             }
2139         }
2140     }
2141
2142     /* now create es */
2143     p_track->p_es = NULL;
2144     if( TrackCreateES( p_demux,
2145                        p_track, p_track->i_chunk,
2146                        p_track->b_chapter ? NULL : &p_track->p_es ) )
2147     {
2148         msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2149                  p_track->i_track_ID );
2150         return;
2151     }
2152     p_track->b_ok = true;
2153 #if 0
2154     {
2155         int i;
2156         for( i = 0; i < p_track->i_chunk_count; i++ )
2157         {
2158             fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2159                      i, p_track->chunk[i].i_sample_count,
2160                      p_track->chunk[i].i_first_dts );
2161
2162         }
2163     }
2164 #endif
2165 }
2166
2167 /****************************************************************************
2168  * MP4_TrackDestroy:
2169  ****************************************************************************
2170  * Destroy a track created by MP4_TrackCreate.
2171  ****************************************************************************/
2172 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
2173 {
2174     unsigned int i_chunk;
2175
2176     p_track->b_ok = false;
2177     p_track->b_enable   = false;
2178     p_track->b_selected = false;
2179
2180     es_format_Clean( &p_track->fmt );
2181
2182     for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2183     {
2184         if( p_track->chunk )
2185         {
2186            FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2187            FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2188
2189            FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2190            FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2191         }
2192     }
2193     FREENULL( p_track->chunk );
2194
2195     if( !p_track->i_sample_size )
2196     {
2197         FREENULL( p_track->p_sample_size );
2198     }
2199 }
2200
2201 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2202                             mtime_t i_start )
2203 {
2204     if( !p_track->b_ok || p_track->b_chapter )
2205     {
2206         return VLC_EGENERIC;
2207     }
2208
2209     if( p_track->b_selected )
2210     {
2211         msg_Warn( p_demux, "track[Id 0x%x] already selected",
2212                   p_track->i_track_ID );
2213         return VLC_SUCCESS;
2214     }
2215
2216     return MP4_TrackSeek( p_demux, p_track, i_start );
2217 }
2218
2219 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2220 {
2221     if( !p_track->b_ok || p_track->b_chapter )
2222     {
2223         return;
2224     }
2225
2226     if( !p_track->b_selected )
2227     {
2228         msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2229                   p_track->i_track_ID );
2230         return;
2231     }
2232     if( p_track->p_es )
2233     {
2234         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2235                         p_track->p_es, false );
2236     }
2237
2238     p_track->b_selected = false;
2239 }
2240
2241 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2242                           mtime_t i_start )
2243 {
2244     uint32_t i_chunk;
2245     uint32_t i_sample;
2246
2247     if( !p_track->b_ok || p_track->b_chapter )
2248         return VLC_EGENERIC;
2249
2250     p_track->b_selected = false;
2251
2252     if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2253                                 &i_chunk, &i_sample ) )
2254     {
2255         msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2256                   p_track->i_track_ID );
2257         return VLC_EGENERIC;
2258     }
2259
2260     p_track->b_selected = true;
2261
2262     if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
2263         VLC_SUCCESS )
2264     {
2265         p_track->b_selected = true;
2266
2267         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
2268                         p_track->p_es, i_start );
2269     }
2270
2271     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2272 }
2273
2274
2275 /*
2276  * 3 types: for audio
2277  *
2278  */
2279 #define QT_V0_MAX_SAMPLES 1024
2280 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2281 {
2282     int i_size;
2283     MP4_Box_data_sample_soun_t *p_soun;
2284
2285     if( p_track->i_sample_size == 0 )
2286     {
2287         /* most simple case */
2288         return p_track->p_sample_size[p_track->i_sample];
2289     }
2290     if( p_track->fmt.i_cat != AUDIO_ES )
2291     {
2292         return p_track->i_sample_size;
2293     }
2294
2295     p_soun = p_track->p_sample->data.p_sample_soun;
2296
2297     if( p_soun->i_qt_version == 1 )
2298     {
2299         i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
2300             p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2301     }
2302     else if( p_track->i_sample_size > 256 )
2303     {
2304         /* We do that so we don't read too much data
2305          * (in this case we are likely dealing with compressed data) */
2306         i_size = p_track->i_sample_size;
2307     }
2308     else
2309     {
2310         /* Read a bunch of samples at once */
2311         int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2312             ( p_track->i_sample -
2313               p_track->chunk[p_track->i_chunk].i_sample_first );
2314
2315         i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2316         i_size = i_samples * p_track->i_sample_size;
2317     }
2318
2319     //fprintf( stderr, "size=%d\n", i_size );
2320     return i_size;
2321 }
2322
2323 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2324 {
2325     unsigned int i_sample;
2326     uint64_t i_pos;
2327
2328     i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2329
2330     if( p_track->i_sample_size )
2331     {
2332         MP4_Box_data_sample_soun_t *p_soun =
2333             p_track->p_sample->data.p_sample_soun;
2334
2335         if( p_soun->i_qt_version == 0 )
2336         {
2337             i_pos += ( p_track->i_sample -
2338                        p_track->chunk[p_track->i_chunk].i_sample_first ) *
2339                      p_track->i_sample_size;
2340         }
2341         else
2342         {
2343             /* we read chunk by chunk */
2344             i_pos += 0;
2345         }
2346     }
2347     else
2348     {
2349         for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2350              i_sample < p_track->i_sample; i_sample++ )
2351         {
2352             i_pos += p_track->p_sample_size[i_sample];
2353         }
2354     }
2355
2356     return i_pos;
2357 }
2358
2359 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2360 {
2361     if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2362     {
2363         MP4_Box_data_sample_soun_t *p_soun;
2364
2365         p_soun = p_track->p_sample->data.p_sample_soun;
2366
2367         if( p_soun->i_qt_version == 1 )
2368         {
2369             /* chunk by chunk */
2370             p_track->i_sample =
2371                 p_track->chunk[p_track->i_chunk].i_sample_first +
2372                 p_track->chunk[p_track->i_chunk].i_sample_count;
2373         }
2374         else if( p_track->i_sample_size > 256 )
2375         {
2376             /* We do that so we don't read too much data
2377              * (in this case we are likely dealing with compressed data) */
2378             p_track->i_sample += 1;
2379         }
2380         else
2381         {
2382             /* FIXME */
2383             p_track->i_sample += QT_V0_MAX_SAMPLES;
2384             if( p_track->i_sample >
2385                 p_track->chunk[p_track->i_chunk].i_sample_first +
2386                 p_track->chunk[p_track->i_chunk].i_sample_count )
2387             {
2388                 p_track->i_sample =
2389                     p_track->chunk[p_track->i_chunk].i_sample_first +
2390                     p_track->chunk[p_track->i_chunk].i_sample_count;
2391             }
2392         }
2393     }
2394     else
2395     {
2396         p_track->i_sample++;
2397     }
2398
2399     if( p_track->i_sample >= p_track->i_sample_count )
2400         return VLC_EGENERIC;
2401
2402     /* Have we changed chunk ? */
2403     if( p_track->i_sample >=
2404             p_track->chunk[p_track->i_chunk].i_sample_first +
2405             p_track->chunk[p_track->i_chunk].i_sample_count )
2406     {
2407         if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2408                                   p_track->i_sample ) )
2409         {
2410             msg_Warn( p_demux, "track[0x%x] will be disabled "
2411                       "(cannot restart decoder)", p_track->i_track_ID );
2412             MP4_TrackUnselect( p_demux, p_track );
2413             return VLC_EGENERIC;
2414         }
2415     }
2416
2417     /* Have we changed elst */
2418     if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2419     {
2420         demux_sys_t *p_sys = p_demux->p_sys;
2421         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2422         uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2423                         p_sys->i_timescale / (int64_t)1000000;
2424
2425         if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2426             i_mvt >= p_track->i_elst_time +
2427                      elst->i_segment_duration[p_track->i_elst] )
2428         {
2429             MP4_TrackSetELST( p_demux, p_track,
2430                               MP4_TrackGetDTS( p_demux, p_track ) );
2431         }
2432     }
2433
2434     return VLC_SUCCESS;
2435 }
2436
2437 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2438                               int64_t i_time )
2439 {
2440     demux_sys_t *p_sys = p_demux->p_sys;
2441     int         i_elst_last = tk->i_elst;
2442
2443     /* handle elst (find the correct one) */
2444     tk->i_elst      = 0;
2445     tk->i_elst_time = 0;
2446     if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2447     {
2448         MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2449         int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2450
2451         for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2452         {
2453             mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2454
2455             if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2456             {
2457                 break;
2458             }
2459             tk->i_elst_time += i_dur;
2460         }
2461
2462         if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2463         {
2464             /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2465             tk->i_elst = elst->i_entry_count - 1;
2466             tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2467         }
2468
2469         if( elst->i_media_time[tk->i_elst] < 0 )
2470         {
2471             /* track offset */
2472             tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2473         }
2474     }
2475     if( i_elst_last != tk->i_elst )
2476     {
2477         msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2478     }
2479 }
2480
2481 static bool FindItem( demux_t *p_demux, playlist_t *p_playlist,
2482                             playlist_item_t **pp_item )
2483 {
2484     input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
2485     bool b_play = var_CreateGetBool( p_demux, "playlist-autostart" );
2486
2487     *pp_item = NULL;
2488     if( p_input )
2489     {
2490         if( b_play && p_playlist->status.p_item &&
2491             p_playlist->status.p_item->p_input == input_GetItem(p_input) )
2492         {
2493             msg_Dbg( p_playlist, "starting playlist playback" );
2494             *pp_item = p_playlist->status.p_item;
2495             b_play = true;
2496         }
2497         else
2498         {
2499             input_item_t *p_current = input_GetItem( p_input );
2500
2501             *pp_item = playlist_ItemGetByInput( p_playlist, p_current, false );
2502             if( !*pp_item )
2503                 msg_Dbg( p_playlist, "unable to find item in playlist");
2504
2505             msg_Dbg( p_playlist, "not starting playlist playback");
2506             b_play = false;
2507         }
2508         vlc_object_release( p_input );
2509     }
2510     return b_play;
2511 }
2512
2513