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