]> git.sesse.net Git - vlc/blob - modules/demux/mp4/mp4.c
738cdb0d24cf3e59bde238441988f006d4ae4e54
[vlc] / modules / demux / mp4 / mp4.c
1 /*****************************************************************************
2  * mp4.c : MP4 file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004, 2010 the VideoLAN team
5  *
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 #include <vlc_demux.h>
35 #include <vlc_charset.h>                           /* EnsureUTF8 */
36 #include <vlc_meta.h>                              /* vlc_meta_t, vlc_meta_ */
37 #include <vlc_input.h>
38
39 #include "libmp4.h"
40 #include "id3genres.h"                             /* for ATOM_gnre */
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( N_("MP4 stream demuxer") )
52     set_shortname( N_("MP4") )
53     set_capability( "demux", 240 )
54     set_callbacks( Open, Close )
55 vlc_module_end ()
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 static int   Demux   ( demux_t * );
61 static int   DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
62 static int   Seek    ( demux_t *, mtime_t );
63 static int   Control ( demux_t *, int, va_list );
64
65 /* Contain all information about a chunk */
66 typedef struct
67 {
68     uint64_t     i_offset; /* absolute position of this chunk in the file */
69     uint32_t     i_sample_description_index; /* index for SampleEntry to use */
70     uint32_t     i_sample_count; /* how many samples in this chunk */
71     uint32_t     i_sample_first; /* index of the first sample in this chunk */
72
73     /* now provide way to calculate pts, dts, and offset without too
74         much memory and with fast access */
75
76     /* with this we can calculate dts/pts without waste memory */
77     uint64_t     i_first_dts;   /* DTS of the first sample */
78     uint64_t     i_last_dts;    /* DTS of the last sample */
79     uint32_t     *p_sample_count_dts;
80     uint32_t     *p_sample_delta_dts;   /* dts delta */
81
82     uint32_t     *p_sample_count_pts;
83     int32_t      *p_sample_offset_pts;  /* pts-dts */
84
85     /* TODO if needed add pts
86         but quickly *add* support for edts and seeking */
87
88 } mp4_chunk_t;
89
90  /* Contain all needed information for read all track with vlc */
91 typedef struct
92 {
93     unsigned int i_track_ID;/* this should be unique */
94
95     int b_ok;               /* The track is usable */
96     int b_enable;           /* is the trak enable by default */
97     bool b_selected;  /* is the trak being played */
98     bool b_chapter;   /* True when used for chapter only */
99
100     bool b_mac_encoding;
101
102     es_format_t fmt;
103     es_out_id_t *p_es;
104
105     /* display size only ! */
106     int i_width;
107     int i_height;
108
109     /* more internal data */
110     uint64_t        i_timescale;    /* time scale for this track only */
111
112     /* elst */
113     int             i_elst;         /* current elst */
114     int64_t         i_elst_time;    /* current elst start time (in movie time scale)*/
115     MP4_Box_t       *p_elst;        /* elst (could be NULL) */
116
117     /* give the next sample to read, i_chunk is to find quickly where
118       the sample is located */
119     uint32_t         i_sample;       /* next sample to read */
120     uint32_t         i_chunk;        /* chunk where next sample is stored */
121     /* total count of chunk and sample */
122     uint32_t         i_chunk_count;
123     uint32_t         i_sample_count;
124
125     mp4_chunk_t    *chunk; /* always defined  for each chunk */
126
127     /* sample size, p_sample_size defined only if i_sample_size == 0
128         else i_sample_size is size for all sample */
129     uint32_t         i_sample_size;
130     uint32_t         *p_sample_size; /* XXX perhaps add file offset if take
131                                     too much time to do sumations each time*/
132
133     MP4_Box_t *p_stbl;  /* will contain all timing information */
134     MP4_Box_t *p_stsd;  /* will contain all data to initialize decoder */
135     MP4_Box_t *p_sample;/* point on actual sdsd */
136
137     MP4_Box_t *p_skcr;
138
139 } mp4_track_t;
140
141
142 struct demux_sys_t
143 {
144     MP4_Box_t    *p_root;      /* container for the whole file */
145
146     mtime_t      i_pcr;
147
148     uint64_t     i_time;         /* time position of the presentation
149                                   * in movie timescale */
150     uint64_t     i_timescale;    /* movie time scale */
151     uint64_t     i_duration;     /* movie duration */
152     unsigned int i_tracks;       /* number of tracks */
153     mp4_track_t  *track;         /* array of track */
154     float        f_fps;          /* number of frame per seconds */
155
156     /* */
157     MP4_Box_t    *p_tref_chap;
158
159     /* */
160     input_title_t *p_title;
161 };
162
163 /*****************************************************************************
164  * Declaration of local function
165  *****************************************************************************/
166 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t  *, bool b_force_enable );
167 static void MP4_TrackDestroy(  mp4_track_t * );
168
169 static int  MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
170 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
171
172 static int  MP4_TrackSeek   ( demux_t *, mp4_track_t *, mtime_t );
173
174 static uint64_t MP4_TrackGetPos    ( mp4_track_t * );
175 static int      MP4_TrackSampleSize( mp4_track_t * );
176 static int      MP4_TrackNextSample( demux_t *, mp4_track_t * );
177 static void     MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
178
179 static void     MP4_UpdateSeekpoint( demux_t * );
180 static const char *MP4_ConvertMacCode( uint16_t );
181
182 /* Return time in s of a track */
183 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
184 {
185 #define chunk p_track->chunk[p_track->i_chunk]
186
187     unsigned int i_index = 0;
188     unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
189     int64_t i_dts = chunk.i_first_dts;
190
191     while( i_sample > 0 )
192     {
193         if( i_sample > chunk.p_sample_count_dts[i_index] )
194         {
195             i_dts += chunk.p_sample_count_dts[i_index] *
196                 chunk.p_sample_delta_dts[i_index];
197             i_sample -= chunk.p_sample_count_dts[i_index];
198             i_index++;
199         }
200         else
201         {
202             i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
203             break;
204         }
205     }
206
207 #undef chunk
208
209     /* now handle elst */
210     if( p_track->p_elst )
211     {
212         demux_sys_t         *p_sys = p_demux->p_sys;
213         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
214
215         /* convert to offset */
216         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
217               elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
218             elst->i_media_time[p_track->i_elst] > 0 )
219         {
220             i_dts -= elst->i_media_time[p_track->i_elst];
221         }
222
223         /* add i_elst_time */
224         i_dts += p_track->i_elst_time * p_track->i_timescale /
225             p_sys->i_timescale;
226
227         if( i_dts < 0 ) i_dts = 0;
228     }
229
230     return INT64_C(1000000) * i_dts / p_track->i_timescale;
231 }
232
233 static inline int64_t MP4_TrackGetPTSDelta( mp4_track_t *p_track )
234 {
235     mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
236     unsigned int i_index = 0;
237     unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
238
239     if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
240         return -1;
241
242     for( i_index = 0;; i_index++ )
243     {
244         if( i_sample < ck->p_sample_count_pts[i_index] )
245             return ck->p_sample_offset_pts[i_index] * INT64_C(1000000) /
246                    (int64_t)p_track->i_timescale;
247
248         i_sample -= ck->p_sample_count_pts[i_index];
249     }
250 }
251
252 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
253 {
254     return INT64_C(1000000) * p_sys->i_time / p_sys->i_timescale;
255 }
256
257 static void LoadChapter( demux_t  *p_demux );
258
259 /*****************************************************************************
260  * Open: check file and initializes MP4 structures
261  *****************************************************************************/
262 static int Open( vlc_object_t * p_this )
263 {
264     demux_t  *p_demux = (demux_t *)p_this;
265     demux_sys_t     *p_sys;
266
267     const uint8_t   *p_peek;
268
269     MP4_Box_t       *p_ftyp;
270     MP4_Box_t       *p_rmra;
271     MP4_Box_t       *p_mvhd;
272     MP4_Box_t       *p_trak;
273
274     unsigned int    i;
275     bool      b_seekable;
276     bool      b_enabled_es;
277
278     /* A little test to see if it could be a mp4 */
279     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
280
281     switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
282     {
283         case ATOM_ftyp:
284         case ATOM_moov:
285         case ATOM_foov:
286         case ATOM_moof:
287         case ATOM_mdat:
288         case ATOM_udta:
289         case ATOM_free:
290         case ATOM_skip:
291         case ATOM_wide:
292         case VLC_FOURCC( 'p', 'n', 'o', 't' ):
293             break;
294          default:
295             return VLC_EGENERIC;
296     }
297
298     /* I need to seek */
299     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
300     if( !b_seekable )
301     {
302         msg_Warn( p_demux, "MP4 plugin discarded (not fastseekable)" );
303         return VLC_EGENERIC;
304     }
305
306     /*Set exported functions */
307     p_demux->pf_demux = Demux;
308     p_demux->pf_control = Control;
309
310     /* create our structure that will contains all data */
311     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
312
313     /* Now load all boxes ( except raw data ) */
314     if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
315     {
316         msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
317         goto error;
318     }
319
320     MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
321
322     if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
323     {
324         switch( p_ftyp->data.p_ftyp->i_major_brand )
325         {
326             case( ATOM_isom ):
327                 msg_Dbg( p_demux,
328                          "ISO Media file (isom) version %d.",
329                          p_ftyp->data.p_ftyp->i_minor_version );
330                 break;
331             case( ATOM_3gp4 ):
332             case( VLC_FOURCC( '3', 'g', 'p', '5' ) ):
333             case( VLC_FOURCC( '3', 'g', 'p', '6' ) ):
334             case( VLC_FOURCC( '3', 'g', 'p', '7' ) ):
335                 msg_Dbg( p_demux, "3GPP Media file Release: %c",
336 #ifdef WORDS_BIGENDIAN
337                         p_ftyp->data.p_ftyp->i_major_brand
338 #else
339                         p_ftyp->data.p_ftyp->i_major_brand >> 24
340 #endif
341                         );
342                 break;
343             case( VLC_FOURCC( 'q', 't', ' ', ' ') ):
344                 msg_Dbg( p_demux, "Apple QuickTime file" );
345                 break;
346             case( VLC_FOURCC( 'i', 's', 'm', 'l') ):
347                 msg_Dbg( p_demux, "PIFF (= isml = fMP4) file" );
348                 break;
349             default:
350                 msg_Dbg( p_demux,
351                          "unrecognized major file specification (%4.4s).",
352                           (char*)&p_ftyp->data.p_ftyp->i_major_brand );
353                 break;
354         }
355     }
356     else
357     {
358         msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
359     }
360
361     /* the file need to have one moov box */
362     if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
363     {
364         MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
365
366         if( !p_foov )
367         {
368             /* search also for moof box used by smoothstreaming */
369             p_foov = MP4_BoxGet( p_sys->p_root, "/moof" );
370             if( !p_foov )
371             {
372                 msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
373                 goto error;
374             }
375         }
376         /* we have a free box as a moov, rename it */
377         p_foov->i_type = ATOM_moov;
378     }
379
380     if( ( p_rmra = MP4_BoxGet( p_sys->p_root,  "/moov/rmra" ) ) )
381     {
382         int        i_count = MP4_BoxCount( p_rmra, "rmda" );
383         int        i;
384
385         msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
386
387         input_thread_t *p_input = demux_GetParentInput( p_demux );
388         input_item_t *p_current = input_GetItem( p_input );
389
390         input_item_node_t *p_subitems = input_item_node_Create( p_current );
391
392         for( i = 0; i < i_count; i++ )
393         {
394             MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
395             char      *psz_ref;
396             uint32_t  i_ref_type;
397
398             if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
399             {
400                 continue;
401             }
402             i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
403
404             msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
405                      psz_ref, (char*)&i_ref_type );
406
407             if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
408             {
409                 if( strstr( psz_ref, "qt5gateQT" ) )
410                 {
411                     msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
412                     continue;
413                 }
414                 if( !strncmp( psz_ref, "http://", 7 ) ||
415                     !strncmp( psz_ref, "rtsp://", 7 ) )
416                 {
417                     ;
418                 }
419                 else
420                 {
421                     char *psz_absolute;
422                     char *psz_path = strdup( p_demux->psz_location );
423                     char *end = strrchr( psz_path, '/' );
424                     if( end ) end[1] = '\0';
425                     else *psz_path = '\0';
426
427                     if( asprintf( &psz_absolute, "%s://%s%s",
428                                   p_demux->psz_access, psz_path, psz_ref ) < 0 )
429                     {
430                         free( psz_ref );
431                         free( psz_path );
432                         vlc_object_release( p_input) ;
433                         return VLC_ENOMEM;
434                     }
435
436                     free( psz_ref );
437                     psz_ref = psz_absolute;
438                     free( psz_path );
439                 }
440                 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
441                 input_item_t *p_item = input_item_New( psz_ref, NULL );
442                 input_item_CopyOptions( p_current, p_item );
443                 input_item_node_AppendItem( p_subitems, p_item );
444                 vlc_gc_decref( p_item );
445             }
446             else
447             {
448                 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
449                          (char*)&p_rdrf->data.p_rdrf->i_ref_type );
450             }
451             free( psz_ref );
452         }
453         input_item_node_PostAndDelete( p_subitems );
454         vlc_object_release( p_input );
455     }
456
457     if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
458     {
459         if( !p_rmra )
460         {
461             msg_Err( p_demux, "cannot find /moov/mvhd" );
462             goto error;
463         }
464         else
465         {
466             msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
467             p_demux->pf_demux = DemuxRef;
468             return VLC_SUCCESS;
469         }
470     }
471     else
472     {
473         p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
474         if( p_sys->i_timescale == 0 )
475         {
476             msg_Err( p_this, "bad timescale" );
477             goto error;
478         }
479         p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
480     }
481
482     if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
483     {
484         msg_Err( p_demux, "cannot find any /moov/trak" );
485         goto error;
486     }
487     msg_Dbg( p_demux, "found %d track%c",
488                         p_sys->i_tracks,
489                         p_sys->i_tracks ? 's':' ' );
490
491     /* allocate memory */
492     p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
493     if( p_sys->track == NULL )
494         goto error;
495
496     /* Search the first chap reference (like quicktime) and
497      * check that at least 1 stream is enabled */
498     p_sys->p_tref_chap = NULL;
499     b_enabled_es = false;
500     for( i = 0; i < p_sys->i_tracks; i++ )
501     {
502         MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
503
504
505         MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
506         if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
507             b_enabled_es = true;
508
509         MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
510         if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
511             p_sys->p_tref_chap = p_chap;
512     }
513
514     /* now process each track and extract all useful information */
515     for( i = 0; i < p_sys->i_tracks; i++ )
516     {
517         p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
518         MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
519
520         if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
521         {
522             const char *psz_cat;
523             switch( p_sys->track[i].fmt.i_cat )
524             {
525                 case( VIDEO_ES ):
526                     psz_cat = "video";
527                     break;
528                 case( AUDIO_ES ):
529                     psz_cat = "audio";
530                     break;
531                 case( SPU_ES ):
532                     psz_cat = "subtitle";
533                     break;
534
535                 default:
536                     psz_cat = "unknown";
537                     break;
538             }
539
540             msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
541                      p_sys->track[i].i_track_ID, psz_cat,
542                      p_sys->track[i].b_enable ? "enable":"disable",
543                      p_sys->track[i].fmt.psz_language ?
544                      p_sys->track[i].fmt.psz_language : "undef" );
545         }
546         else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
547         {
548             msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
549                      p_sys->track[i].i_track_ID,
550                      p_sys->track[i].fmt.psz_language ?
551                      p_sys->track[i].fmt.psz_language : "undef" );
552         }
553         else
554         {
555             msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
556                      p_sys->track[i].i_track_ID );
557         }
558     }
559
560     /* */
561     LoadChapter( p_demux );
562
563     return VLC_SUCCESS;
564
565 error:
566     if( p_sys->p_root )
567     {
568         MP4_BoxFree( p_demux->s, p_sys->p_root );
569     }
570     free( p_sys );
571     return VLC_EGENERIC;
572 }
573
574 /*****************************************************************************
575  * Demux: read packet and send them to decoders
576  *****************************************************************************
577  * TODO check for newly selected track (ie audio upt to now )
578  *****************************************************************************/
579 static int Demux( demux_t *p_demux )
580 {
581     demux_sys_t *p_sys = p_demux->p_sys;
582     unsigned int i_track;
583
584
585     unsigned int i_track_selected;
586
587     /* check for newly selected/unselected track */
588     for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
589          i_track++ )
590     {
591         mp4_track_t *tk = &p_sys->track[i_track];
592         bool b;
593
594         if( !tk->b_ok || tk->b_chapter ||
595             ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
596         {
597             continue;
598         }
599
600         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
601
602         if( tk->b_selected && !b )
603         {
604             MP4_TrackUnselect( p_demux, tk );
605         }
606         else if( !tk->b_selected && b)
607         {
608             MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
609         }
610
611         if( tk->b_selected )
612         {
613             i_track_selected++;
614         }
615     }
616
617     if( i_track_selected <= 0 )
618     {
619         p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
620         if( p_sys->i_timescale > 0 )
621         {
622             int64_t i_length = (mtime_t)1000000 *
623                                (mtime_t)p_sys->i_duration /
624                                (mtime_t)p_sys->i_timescale;
625             if( MP4_GetMoviePTS( p_sys ) >= i_length )
626                 return 0;
627             return 1;
628         }
629
630         msg_Warn( p_demux, "no track selected, exiting..." );
631         return 0;
632     }
633
634     /* */
635     MP4_UpdateSeekpoint( p_demux );
636
637     /* first wait for the good time to read a packet */
638     es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
639
640     p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
641
642     /* we will read 100ms for each stream so ...*/
643     p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
644
645     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
646     {
647         mp4_track_t *tk = &p_sys->track[i_track];
648
649         if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
650             continue;
651
652         while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
653         {
654 #if 0
655             msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
656                      MP4_TrackGetDTS( p_demux, tk ),
657                      MP4_GetMoviePTS( p_sys ) );
658 #endif
659
660             if( MP4_TrackSampleSize( tk ) > 0 )
661             {
662                 block_t *p_block;
663                 int64_t i_delta;
664
665                 /* go,go go ! */
666                 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
667                 {
668                     msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
669                               tk->i_track_ID );
670                     MP4_TrackUnselect( p_demux, tk );
671                     break;
672                 }
673
674                 /* now read pes */
675                 if( !(p_block =
676                          stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
677                 {
678                     msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
679                               tk->i_track_ID );
680                     MP4_TrackUnselect( p_demux, tk );
681                     break;
682                 }
683
684                 else if( tk->fmt.i_cat == SPU_ES )
685                 {
686                     if( tk->fmt.i_codec == VLC_CODEC_SUBT &&
687                         p_block->i_buffer >= 2 )
688                     {
689                         size_t i_size = GetWBE( p_block->p_buffer );
690
691                         if( i_size + 2 <= p_block->i_buffer )
692                         {
693                             char *p;
694                             /* remove the length field, and append a '\0' */
695                             memmove( &p_block->p_buffer[0],
696                                      &p_block->p_buffer[2], i_size );
697                             p_block->p_buffer[i_size] = '\0';
698                             p_block->i_buffer = i_size + 1;
699
700                             /* convert \r -> \n */
701                             while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
702                             {
703                                 *p = '\n';
704                             }
705                         }
706                         else
707                         {
708                             /* Invalid */
709                             p_block->i_buffer = 0;
710                         }
711                     }
712                 }
713                 /* dts */
714                 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
715                 /* pts */
716                 i_delta = MP4_TrackGetPTSDelta( tk );
717                 if( i_delta != -1 )
718                     p_block->i_pts = p_block->i_dts + i_delta;
719                 else if( tk->fmt.i_cat != VIDEO_ES )
720                     p_block->i_pts = p_block->i_dts;
721                 else
722                     p_block->i_pts = VLC_TS_INVALID;
723
724                 es_out_Send( p_demux->out, tk->p_es, p_block );
725             }
726
727             /* Next sample */
728             if( MP4_TrackNextSample( p_demux, tk ) )
729                 break;
730         }
731     }
732
733     return 1;
734 }
735
736 static void MP4_UpdateSeekpoint( demux_t *p_demux )
737 {
738     demux_sys_t *p_sys = p_demux->p_sys;
739     int64_t i_time;
740     int i;
741     if( !p_sys->p_title )
742         return;
743     i_time = MP4_GetMoviePTS( p_sys );
744     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
745     {
746         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
747             break;
748     }
749     i--;
750
751     if( i != p_demux->info.i_seekpoint && i >= 0 )
752     {
753         p_demux->info.i_seekpoint = i;
754         p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
755     }
756 }
757 /*****************************************************************************
758  * Seek: Go to i_date
759 ******************************************************************************/
760 static int Seek( demux_t *p_demux, mtime_t i_date )
761 {
762     demux_sys_t *p_sys = p_demux->p_sys;
763     unsigned int i_track;
764
765     /* First update update global time */
766     p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
767     p_sys->i_pcr  = i_date;
768
769     /* Now for each stream try to go to this time */
770     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
771     {
772         mp4_track_t *tk = &p_sys->track[i_track];
773         MP4_TrackSeek( p_demux, tk, i_date );
774     }
775     MP4_UpdateSeekpoint( p_demux );
776
777     es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
778
779     return VLC_SUCCESS;
780 }
781
782 /*****************************************************************************
783  * Control:
784  *****************************************************************************/
785 static int Control( demux_t *p_demux, int i_query, va_list args )
786 {
787     demux_sys_t *p_sys = p_demux->p_sys;
788
789     double f, *pf;
790     int64_t i64, *pi64;
791
792     switch( i_query )
793     {
794         case DEMUX_GET_POSITION:
795             pf = (double*)va_arg( args, double * );
796             if( p_sys->i_duration > 0 )
797             {
798                 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
799             }
800             else
801             {
802                 *pf = 0.0;
803             }
804             return VLC_SUCCESS;
805
806         case DEMUX_SET_POSITION:
807             f = (double)va_arg( args, double );
808             if( p_sys->i_timescale > 0 )
809             {
810                 i64 = (int64_t)( f * (double)1000000 *
811                                  (double)p_sys->i_duration /
812                                  (double)p_sys->i_timescale );
813                 return Seek( p_demux, i64 );
814             }
815             else return VLC_SUCCESS;
816
817         case DEMUX_GET_TIME:
818             pi64 = (int64_t*)va_arg( args, int64_t * );
819             if( p_sys->i_timescale > 0 )
820             {
821                 *pi64 = (mtime_t)1000000 *
822                         (mtime_t)p_sys->i_time /
823                         (mtime_t)p_sys->i_timescale;
824             }
825             else *pi64 = 0;
826             return VLC_SUCCESS;
827
828         case DEMUX_SET_TIME:
829             i64 = (int64_t)va_arg( args, int64_t );
830             return Seek( p_demux, i64 );
831
832         case DEMUX_GET_LENGTH:
833             pi64 = (int64_t*)va_arg( args, int64_t * );
834             if( p_sys->i_timescale > 0 )
835             {
836                 *pi64 = (mtime_t)1000000 *
837                         (mtime_t)p_sys->i_duration /
838                         (mtime_t)p_sys->i_timescale;
839             }
840             else *pi64 = 0;
841             return VLC_SUCCESS;
842
843         case DEMUX_GET_FPS:
844             pf = (double*)va_arg( args, double* );
845             *pf = p_sys->f_fps;
846             return VLC_SUCCESS;
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 ATOM_0xa9nam: /* Full name */
880                     SET( vlc_meta_SetTitle );
881                     break;
882                 case ATOM_0xa9aut:
883                     SET( vlc_meta_SetArtist );
884                     break;
885                 case ATOM_0xa9ART:
886                     SET( vlc_meta_SetArtist );
887                     break;
888                 case ATOM_0xa9cpy:
889                     SET( vlc_meta_SetCopyright );
890                     break;
891                 case ATOM_0xa9day: /* Creation Date */
892                     SET( vlc_meta_SetDate );
893                     break;
894                 case ATOM_0xa9des: /* Description */
895                     SET( vlc_meta_SetDescription );
896                     break;
897                 case ATOM_0xa9gen: /* Genre */
898                     SET( vlc_meta_SetGenre );
899                     break;
900
901                 case ATOM_gnre:
902                     if( p_0xa9xxx->data.p_gnre->i_genre <= NUM_GENRES )
903                         vlc_meta_SetGenre( p_meta, ppsz_genres[p_0xa9xxx->data.p_gnre->i_genre - 1] );
904                     break;
905
906                 case ATOM_0xa9alb: /* Album */
907                     SET( vlc_meta_SetAlbum );
908                     break;
909
910                 case ATOM_0xa9trk: /* Track */
911                     SET( vlc_meta_SetTrackNum );
912                     break;
913                 case ATOM_trkn:
914                 {
915                     char psz_trck[11];
916                     snprintf( psz_trck, sizeof( psz_trck ), "%i",
917                               p_0xa9xxx->data.p_trkn->i_track_number );
918                     vlc_meta_SetTrackNum( p_meta, psz_trck );
919                     break;
920                 }
921                 case ATOM_0xa9cmt: /* Commment */
922                     SET( vlc_meta_SetDescription );
923                     break;
924
925                 case ATOM_0xa9url: /* URL */
926                     SET( vlc_meta_SetURL );
927                     break;
928
929                 case ATOM_0xa9too: /* Encoder Tool */
930                 case ATOM_0xa9enc: /* Encoded By */
931                     SET( vlc_meta_SetEncodedBy );
932                     break;
933
934                 default:
935                     break;
936                 }
937 #undef SET
938                 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
939                 {
940                     { ATOM_0xa9wrt, N_("Writer") },
941                     { ATOM_0xa9com, N_("Composer") },
942                     { ATOM_0xa9prd, N_("Producer") },
943                     { ATOM_0xa9inf, N_("Information") },
944                     { ATOM_0xa9dir, N_("Director") },
945                     { ATOM_0xa9dis, N_("Disclaimer") },
946                     { ATOM_0xa9req, N_("Requirements") },
947                     { ATOM_0xa9fmt, N_("Original Format") },
948                     { ATOM_0xa9dsa, N_("Display Source As") },
949                     { ATOM_0xa9hst, N_("Host Computer") },
950                     { ATOM_0xa9prf, N_("Performers") },
951                     { ATOM_0xa9ope, N_("Original Performer") },
952                     { ATOM_0xa9src, N_("Providers Source Content") },
953                     { ATOM_0xa9wrn, N_("Warning") },
954                     { ATOM_0xa9swr, N_("Software") },
955                     { ATOM_0xa9lyr, N_("Lyrics") },
956                     { ATOM_0xa9mak, N_("Make") },
957                     { ATOM_0xa9mod, N_("Model") },
958                     { ATOM_0xa9PRD, N_("Product") },
959                     { ATOM_0xa9grp, N_("Grouping") },
960                     { 0, "" },
961                 };
962                 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
963                 {
964                     if( p_0xa9xxx->i_type == xa9typetoextrameta[i].xa9_type )
965                     {
966                         char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" );
967                         if( psz_utf )
968                         {
969                              EnsureUTF8( psz_utf );
970                              vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
971                              free( psz_utf );
972                         }
973                         break;
974                     }
975                 }
976             }
977             return VLC_SUCCESS;
978         }
979
980         case DEMUX_GET_TITLE_INFO:
981         {
982             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
983             int *pi_int    = (int*)va_arg( args, int* );
984             int *pi_title_offset = (int*)va_arg( args, int* );
985             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
986
987             if( !p_sys->p_title )
988                 return VLC_EGENERIC;
989
990             *pi_int = 1;
991             *ppp_title = malloc( sizeof( input_title_t**) );
992             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
993             *pi_title_offset = 0;
994             *pi_seekpoint_offset = 0;
995             return VLC_SUCCESS;
996         }
997         case DEMUX_SET_TITLE:
998         {
999             const int i_title = (int)va_arg( args, int );
1000             if( !p_sys->p_title || i_title != 0 )
1001                 return VLC_EGENERIC;
1002             return VLC_SUCCESS;
1003         }
1004         case DEMUX_SET_SEEKPOINT:
1005         {
1006             const int i_seekpoint = (int)va_arg( args, int );
1007             if( !p_sys->p_title )
1008                 return VLC_EGENERIC;
1009             return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
1010         }
1011
1012         case DEMUX_SET_NEXT_DEMUX_TIME:
1013         case DEMUX_SET_GROUP:
1014         case DEMUX_HAS_UNSUPPORTED_META:
1015         case DEMUX_GET_ATTACHMENTS:
1016         case DEMUX_GET_PTS_DELAY:
1017         case DEMUX_CAN_RECORD:
1018             return VLC_EGENERIC;
1019
1020         default:
1021             msg_Warn( p_demux, "control query %u unimplemented", i_query );
1022             return VLC_EGENERIC;
1023     }
1024 }
1025
1026 /*****************************************************************************
1027  * Close: frees unused data
1028  *****************************************************************************/
1029 static void Close ( vlc_object_t * p_this )
1030 {
1031     demux_t *  p_demux = (demux_t *)p_this;
1032     demux_sys_t *p_sys = p_demux->p_sys;
1033     unsigned int i_track;
1034
1035     msg_Dbg( p_demux, "freeing all memory" );
1036
1037     MP4_BoxFree( p_demux->s, p_sys->p_root );
1038     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1039     {
1040         MP4_TrackDestroy(  &p_sys->track[i_track] );
1041     }
1042     FREENULL( p_sys->track );
1043
1044     if( p_sys->p_title )
1045         vlc_input_title_Delete( p_sys->p_title );
1046
1047     free( p_sys );
1048 }
1049
1050
1051
1052 /****************************************************************************
1053  * Local functions, specific to vlc
1054  ****************************************************************************/
1055 /* Chapters */
1056 static void LoadChapterGpac( demux_t  *p_demux, MP4_Box_t *p_chpl )
1057 {
1058     demux_sys_t *p_sys = p_demux->p_sys;
1059     int i;
1060
1061     p_sys->p_title = vlc_input_title_New();
1062     for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1063     {
1064         seekpoint_t *s = vlc_seekpoint_New();
1065
1066         s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1067         EnsureUTF8( s->psz_name );
1068         s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1069         TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1070     }
1071 }
1072 static void LoadChapterApple( demux_t  *p_demux, mp4_track_t *tk )
1073 {
1074     demux_sys_t *p_sys = p_demux->p_sys;
1075
1076     for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1077     {
1078         const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1079         const int64_t i_pts_delta = MP4_TrackGetPTSDelta( tk );
1080         const unsigned int i_size = MP4_TrackSampleSize( tk );
1081
1082         if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1083         {
1084             char p_buffer[256];
1085             const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1086             const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1087
1088             if( i_len > 0 )
1089             {
1090                 seekpoint_t *s = vlc_seekpoint_New();
1091
1092                 s->psz_name = strndup( &p_buffer[2], i_len );
1093                 EnsureUTF8( s->psz_name );
1094
1095                 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1096
1097                 if( !p_sys->p_title )
1098                     p_sys->p_title = vlc_input_title_New();
1099                 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1100             }
1101         }
1102         if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1103                               tk->chunk[tk->i_chunk].i_sample_count )
1104             tk->i_chunk++;
1105     }
1106 }
1107 static void LoadChapter( demux_t  *p_demux )
1108 {
1109     demux_sys_t *p_sys = p_demux->p_sys;
1110     MP4_Box_t *p_chpl;
1111
1112     if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1113     {
1114         LoadChapterGpac( p_demux, p_chpl );
1115     }
1116     else if( p_sys->p_tref_chap )
1117     {
1118         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1119         unsigned int i, j;
1120
1121         /* Load the first subtitle track like quicktime */
1122         for( i = 0; i < p_chap->i_entry_count; i++ )
1123         {
1124             for( j = 0; j < p_sys->i_tracks; j++ )
1125             {
1126                 mp4_track_t *tk = &p_sys->track[j];
1127                 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1128                     tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_SUBT )
1129                     break;
1130             }
1131             if( j < p_sys->i_tracks )
1132             {
1133                 LoadChapterApple( p_demux, &p_sys->track[j] );
1134                 break;
1135             }
1136         }
1137     }
1138
1139     /* Add duration if titles are enabled */
1140     if( p_sys->p_title )
1141     {
1142         p_sys->p_title->i_length = (uint64_t)1000000 *
1143                        (uint64_t)p_sys->i_duration / (uint64_t)p_sys->i_timescale;
1144     }
1145 }
1146
1147 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1148 static int TrackCreateChunksIndex( demux_t *p_demux,
1149                                    mp4_track_t *p_demux_track )
1150 {
1151     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1152     MP4_Box_t *p_stsc;
1153
1154     unsigned int i_chunk;
1155     unsigned int i_index, i_last;
1156
1157     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1158           !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1159         ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1160     {
1161         return( VLC_EGENERIC );
1162     }
1163
1164     p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1165     if( !p_demux_track->i_chunk_count )
1166     {
1167         msg_Warn( p_demux, "no chunk defined" );
1168         return( VLC_EGENERIC );
1169     }
1170     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1171                                    sizeof( mp4_chunk_t ) );
1172     if( p_demux_track->chunk == NULL )
1173     {
1174         return VLC_ENOMEM;
1175     }
1176
1177     /* first we read chunk offset */
1178     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1179     {
1180         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1181
1182         ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1183
1184         ck->i_first_dts = 0;
1185         ck->p_sample_count_dts = NULL;
1186         ck->p_sample_delta_dts = NULL;
1187         ck->p_sample_count_pts = NULL;
1188         ck->p_sample_offset_pts = NULL;
1189     }
1190
1191     /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1192         to be used for the sample XXX begin to 1
1193         We construct it begining at the end */
1194     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1195     i_index = p_stsc->data.p_stsc->i_entry_count;
1196     if( !i_index )
1197     {
1198         msg_Warn( p_demux, "cannot read chunk table or table empty" );
1199         return( VLC_EGENERIC );
1200     }
1201
1202     while( i_index-- )
1203     {
1204         for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1205              i_chunk < i_last; i_chunk++ )
1206         {
1207             if( i_chunk >= p_demux_track->i_chunk_count )
1208             {
1209                 msg_Warn( p_demux, "corrupted chunk table" );
1210                 return VLC_EGENERIC;
1211             }
1212
1213             p_demux_track->chunk[i_chunk].i_sample_description_index =
1214                     p_stsc->data.p_stsc->i_sample_description_index[i_index];
1215             p_demux_track->chunk[i_chunk].i_sample_count =
1216                     p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1217         }
1218         i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1219     }
1220
1221     p_demux_track->chunk[0].i_sample_first = 0;
1222     for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1223     {
1224         p_demux_track->chunk[i_chunk].i_sample_first =
1225             p_demux_track->chunk[i_chunk-1].i_sample_first +
1226                 p_demux_track->chunk[i_chunk-1].i_sample_count;
1227     }
1228
1229     msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1230              p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1231
1232     return VLC_SUCCESS;
1233 }
1234
1235 static int TrackCreateSamplesIndex( demux_t *p_demux,
1236                                     mp4_track_t *p_demux_track )
1237 {
1238     MP4_Box_t *p_box;
1239     MP4_Box_data_stsz_t *stsz;
1240     MP4_Box_data_stts_t *stts;
1241     /* TODO use also stss and stsh table for seeking */
1242     /* FIXME use edit table */
1243     int64_t i_sample;
1244     int64_t i_chunk;
1245
1246     int64_t i_index;
1247     int64_t i_index_sample_used;
1248
1249     int64_t i_next_dts;
1250
1251     /* Find stsz
1252      *  Gives the sample size for each samples. There is also a stz2 table
1253      *  (compressed form) that we need to implement TODO */
1254     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1255     if( !p_box )
1256     {
1257         /* FIXME and stz2 */
1258         msg_Warn( p_demux, "cannot find STSZ box" );
1259         return VLC_EGENERIC;
1260     }
1261     stsz = p_box->data.p_stsz;
1262
1263     /* Find stts
1264      *  Gives mapping between sample and decoding time
1265      */
1266     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1267     if( !p_box )
1268     {
1269         msg_Warn( p_demux, "cannot find STTS box" );
1270         return VLC_EGENERIC;
1271     }
1272     stts = p_box->data.p_stts;
1273
1274     /* Use stsz table to create a sample number -> sample size table */
1275     p_demux_track->i_sample_count = stsz->i_sample_count;
1276     if( stsz->i_sample_size )
1277     {
1278         /* 1: all sample have the same size, so no need to construct a table */
1279         p_demux_track->i_sample_size = stsz->i_sample_size;
1280         p_demux_track->p_sample_size = NULL;
1281     }
1282     else
1283     {
1284         /* 2: each sample can have a different size */
1285         p_demux_track->i_sample_size = 0;
1286         p_demux_track->p_sample_size =
1287             calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1288         if( p_demux_track->p_sample_size == NULL )
1289             return VLC_ENOMEM;
1290
1291         for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1292         {
1293             p_demux_track->p_sample_size[i_sample] =
1294                     stsz->i_entry_size[i_sample];
1295         }
1296     }
1297
1298     /* Use stts table to create a sample number -> dts table.
1299      * XXX: if we don't want to waste too much memory, we can't expand
1300      *  the box! so each chunk will contain an "extract" of this table
1301      *  for fast research (problem with raw stream where a sample is sometime
1302      *  just channels*bits_per_sample/8 */
1303
1304     i_next_dts = 0;
1305     i_index = 0; i_index_sample_used = 0;
1306     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1307     {
1308         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1309         int64_t i_entry, i_sample_count, i;
1310
1311         /* save first dts */
1312         ck->i_first_dts = i_next_dts;
1313         ck->i_last_dts  = i_next_dts;
1314
1315         /* count how many entries are needed for this chunk
1316          * for p_sample_delta_dts and p_sample_count_dts */
1317         i_sample_count = ck->i_sample_count;
1318
1319         i_entry = 0;
1320         while( i_sample_count > 0 )
1321         {
1322             i_sample_count -= stts->i_sample_count[i_index+i_entry];
1323             /* don't count already used sample in this entry */
1324             if( i_entry == 0 )
1325                 i_sample_count += i_index_sample_used;
1326
1327             i_entry++;
1328         }
1329
1330         /* allocate them */
1331         ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1332         ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1333
1334         if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1335             return VLC_ENOMEM;
1336
1337         /* now copy */
1338         i_sample_count = ck->i_sample_count;
1339         for( i = 0; i < i_entry; i++ )
1340         {
1341             int64_t i_used;
1342             int64_t i_rest;
1343
1344             i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1345
1346             i_used = __MIN( i_rest, i_sample_count );
1347
1348             i_index_sample_used += i_used;
1349             i_sample_count -= i_used;
1350             i_next_dts += i_used * stts->i_sample_delta[i_index];
1351
1352             ck->p_sample_count_dts[i] = i_used;
1353             ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1354             if( i_used > 0 )
1355                 ck->i_last_dts = i_next_dts - ck->p_sample_delta_dts[i];
1356
1357             if( i_index_sample_used >= stts->i_sample_count[i_index] )
1358             {
1359                 i_index++;
1360                 i_index_sample_used = 0;
1361             }
1362         }
1363     }
1364
1365     /* Find ctts
1366      *  Gives the delta between decoding time (dts) and composition table (pts)
1367      */
1368     p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1369     if( p_box )
1370     {
1371         MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1372
1373         msg_Warn( p_demux, "CTTS table" );
1374
1375         /* Create pts-dts table per chunk */
1376         i_index = 0; i_index_sample_used = 0;
1377         for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1378         {
1379             mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1380             int64_t i_entry, i_sample_count, i;
1381
1382             /* count how many entries are needed for this chunk
1383              * for p_sample_delta_dts and p_sample_count_dts */
1384             i_sample_count = ck->i_sample_count;
1385
1386             i_entry = 0;
1387             while( i_sample_count > 0 )
1388             {
1389                 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1390
1391                 /* don't count already used sample in this entry */
1392                 if( i_entry == 0 )
1393                     i_sample_count += i_index_sample_used;
1394
1395                 i_entry++;
1396             }
1397
1398             /* allocate them */
1399             ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1400             ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1401             if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1402                 return VLC_ENOMEM;
1403
1404             /* now copy */
1405             i_sample_count = ck->i_sample_count;
1406             for( i = 0; i < i_entry; i++ )
1407             {
1408                 int64_t i_used;
1409                 int64_t i_rest;
1410
1411                 i_rest = ctts->i_sample_count[i_index] -
1412                     i_index_sample_used;
1413
1414                 i_used = __MIN( i_rest, i_sample_count );
1415
1416                 i_index_sample_used += i_used;
1417                 i_sample_count -= i_used;
1418
1419                 ck->p_sample_count_pts[i] = i_used;
1420                 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1421
1422                 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1423                 {
1424                     i_index++;
1425                     i_index_sample_used = 0;
1426                 }
1427             }
1428         }
1429     }
1430
1431     msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1432              p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1433              i_next_dts / p_demux_track->i_timescale );
1434
1435     return VLC_SUCCESS;
1436 }
1437
1438 /**
1439  * It computes the sample rate for a video track using the given sample
1440  * description index
1441  */
1442 static void TrackGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
1443                                   const mp4_track_t *p_track,
1444                                   unsigned i_sd_index,
1445                                   unsigned i_chunk )
1446 {
1447     *pi_num = 0;
1448     *pi_den = 0;
1449
1450     if( p_track->i_chunk_count <= 0 )
1451         return;
1452
1453     /* */
1454     const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
1455     while( p_chunk > &p_track->chunk[0] &&
1456            p_chunk[-1].i_sample_description_index == i_sd_index )
1457     {
1458         p_chunk--;
1459     }
1460
1461     uint64_t i_sample = 0;
1462     uint64_t i_first_dts = p_chunk->i_first_dts;
1463     uint64_t i_last_dts;
1464     do
1465     {
1466         i_sample += p_chunk->i_sample_count;
1467         i_last_dts = p_chunk->i_last_dts;
1468         p_chunk++;
1469     }
1470     while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
1471            p_chunk->i_sample_description_index == i_sd_index );
1472
1473     if( i_sample > 1 && i_first_dts < i_last_dts )
1474         vlc_ureduce( pi_num, pi_den,
1475                      ( i_sample - 1) *  p_track->i_timescale,
1476                      i_last_dts - i_first_dts,
1477                      UINT16_MAX);
1478 }
1479
1480 /*
1481  * TrackCreateES:
1482  * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1483  */
1484 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1485                           unsigned int i_chunk, es_out_id_t **pp_es )
1486 {
1487     const unsigned i_sample_description_index =
1488         p_track->chunk[i_chunk].i_sample_description_index;
1489     MP4_Box_t   *p_sample;
1490     MP4_Box_t   *p_esds;
1491     MP4_Box_t   *p_frma;
1492     MP4_Box_t   *p_enda;
1493     MP4_Box_t   *p_pasp;
1494
1495     if( pp_es )
1496         *pp_es = NULL;
1497
1498     if( !i_sample_description_index )
1499     {
1500         msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1501                   p_track->i_track_ID );
1502         return VLC_EGENERIC;
1503     }
1504
1505     p_sample = MP4_BoxGet(  p_track->p_stsd, "[%d]",
1506                             i_sample_description_index - 1 );
1507
1508     if( !p_sample ||
1509         ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1510     {
1511         msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1512                   p_track->i_track_ID );
1513         return VLC_EGENERIC;
1514     }
1515
1516     p_track->p_sample = p_sample;
1517
1518     if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
1519     {
1520         msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
1521
1522         p_sample->i_type = p_frma->data.p_frma->i_type;
1523     }
1524
1525     p_enda = MP4_BoxGet( p_sample, "wave/enda" );
1526     if( !p_enda )
1527         p_enda = MP4_BoxGet( p_sample, "enda" );
1528
1529     p_pasp = MP4_BoxGet( p_sample, "pasp" );
1530
1531     if( p_track->fmt.i_cat == AUDIO_ES && ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1532     {
1533         MP4_Box_data_sample_soun_t *p_soun;
1534
1535         p_soun = p_sample->data.p_sample_soun;
1536
1537         if( p_soun->i_qt_version == 0 )
1538         {
1539             switch( p_sample->i_type )
1540             {
1541                 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1542                     p_soun->i_qt_version = 1;
1543                     p_soun->i_sample_per_packet = 64;
1544                     p_soun->i_bytes_per_packet  = 34;
1545                     p_soun->i_bytes_per_frame   = 34 * p_soun->i_channelcount;
1546                     p_soun->i_bytes_per_sample  = 2;
1547                     break;
1548                 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1549                     p_soun->i_qt_version = 1;
1550                     p_soun->i_sample_per_packet = 6;
1551                     p_soun->i_bytes_per_packet  = 2;
1552                     p_soun->i_bytes_per_frame   = 2 * p_soun->i_channelcount;
1553                     p_soun->i_bytes_per_sample  = 2;
1554                     break;
1555                 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1556                     p_soun->i_qt_version = 1;
1557                     p_soun->i_sample_per_packet = 12;
1558                     p_soun->i_bytes_per_packet  = 2;
1559                     p_soun->i_bytes_per_frame   = 2 * p_soun->i_channelcount;
1560                     p_soun->i_bytes_per_sample  = 2;
1561                     break;
1562                 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1563                 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1564                     p_soun->i_samplesize = 8;
1565                     p_track->i_sample_size = p_soun->i_channelcount;
1566                     break;
1567                 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1568                 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1569                 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1570                 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1571                     /* What would be the fun if you could trust the .mov */
1572                     p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1573                     break;
1574                 default:
1575                     break;
1576             }
1577
1578         }
1579         else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1580         {
1581             p_soun->i_qt_version = 0;
1582         }
1583     }
1584     else if( p_track->fmt.i_cat == AUDIO_ES && p_sample->data.p_sample_soun->i_qt_version == 1 )
1585     {
1586         MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1587
1588         switch( p_sample->i_type )
1589         {
1590             case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1591             case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1592             {
1593                 if( p_track->i_sample_size > 1 )
1594                     p_soun->i_qt_version = 0;
1595                 break;
1596             }
1597             case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1598             case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1599             case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
1600                 p_soun->i_qt_version = 0;
1601                 break;
1602             default:
1603                 break;
1604         }
1605     }
1606
1607     /* */
1608     switch( p_track->fmt.i_cat )
1609     {
1610     case VIDEO_ES:
1611         p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1612         p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1613         p_track->fmt.video.i_bits_per_pixel =
1614             p_sample->data.p_sample_vide->i_depth;
1615
1616         /* fall on display size */
1617         if( p_track->fmt.video.i_width <= 0 )
1618             p_track->fmt.video.i_width = p_track->i_width;
1619         if( p_track->fmt.video.i_height <= 0 )
1620             p_track->fmt.video.i_height = p_track->i_height;
1621
1622         /* Find out apect ratio from display size */
1623         if( p_track->i_width > 0 && p_track->i_height > 0 &&
1624             /* Work-around buggy muxed files */
1625             p_sample->data.p_sample_vide->i_width != p_track->i_width )
1626         {
1627             p_track->fmt.video.i_sar_num = p_track->i_width  * p_track->fmt.video.i_height;
1628             p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
1629         }
1630         if( p_pasp && p_pasp->data.p_pasp->i_horizontal_spacing > 0 &&
1631                       p_pasp->data.p_pasp->i_vertical_spacing > 0 )
1632         {
1633             p_track->fmt.video.i_sar_num = p_pasp->data.p_pasp->i_horizontal_spacing;
1634             p_track->fmt.video.i_sar_den = p_pasp->data.p_pasp->i_vertical_spacing;
1635         }
1636
1637         /* Support for cropping (eg. in H263 files) */
1638         p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1639         p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1640
1641         /* Frame rate */
1642         TrackGetESSampleRate( &p_track->fmt.video.i_frame_rate,
1643                               &p_track->fmt.video.i_frame_rate_base,
1644                               p_track, i_sample_description_index, i_chunk );
1645         p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
1646                                 (float)p_track->fmt.video.i_frame_rate_base;
1647         break;
1648
1649     case AUDIO_ES:
1650         p_track->fmt.audio.i_channels =
1651             p_sample->data.p_sample_soun->i_channelcount;
1652         p_track->fmt.audio.i_rate =
1653             p_sample->data.p_sample_soun->i_sampleratehi;
1654         p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1655             p_sample->data.p_sample_soun->i_sampleratehi *
1656                 p_sample->data.p_sample_soun->i_samplesize;
1657         p_track->fmt.audio.i_bitspersample =
1658             p_sample->data.p_sample_soun->i_samplesize;
1659
1660         if( p_track->i_sample_size != 0 &&
1661             p_sample->data.p_sample_soun->i_qt_version == 1 && p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1662         {
1663             msg_Err( p_demux, "Invalid sample per packet value for qt_version 1" );
1664             return VLC_EGENERIC;
1665         }
1666         break;
1667
1668     default:
1669         break;
1670     }
1671
1672
1673     /* It's a little ugly but .. there are special cases */
1674     switch( p_sample->i_type )
1675     {
1676         case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1677         case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1678         {
1679             p_track->fmt.i_codec = VLC_CODEC_MPGA;
1680             break;
1681         }
1682         case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1683         {
1684             MP4_Box_t *p_dac3_box = MP4_BoxGet(  p_sample, "dac3", 0 );
1685
1686             p_track->fmt.i_codec = VLC_CODEC_A52;
1687             if( p_dac3_box )
1688             {
1689                 static const int pi_bitrate[] = {
1690                      32,  40,  48,  56,
1691                      64,  80,  96, 112,
1692                     128, 160, 192, 224,
1693                     256, 320, 384, 448,
1694                     512, 576, 640,
1695                 };
1696                 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
1697                 p_track->fmt.audio.i_channels = 0;
1698                 p_track->fmt.i_bitrate = 0;
1699                 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
1700                     p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
1701                 p_track->fmt.audio.i_bitspersample = 0;
1702             }
1703             break;
1704         }
1705         case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1706         {
1707             p_track->fmt.i_codec = VLC_CODEC_EAC3;
1708             break;
1709         }
1710
1711         case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1712         case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1713         {
1714             MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1715
1716             if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1717                 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1718             else
1719                 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1720
1721             /* Buggy files workaround */
1722             if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1723                 p_sample->data.p_sample_soun->i_sampleratehi) )
1724             {
1725                 MP4_Box_data_sample_soun_t *p_soun =
1726                     p_sample->data.p_sample_soun;
1727
1728                 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1729                           "(%u), making both equal (report any problem).",
1730                           p_track->i_timescale, p_soun->i_sampleratehi );
1731
1732                 if( p_soun->i_sampleratehi )
1733                     p_track->i_timescale = p_soun->i_sampleratehi;
1734                 else
1735                     p_soun->i_sampleratehi = p_track->i_timescale;
1736             }
1737             break;
1738         }
1739
1740         case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1741             p_track->fmt.i_codec = VLC_CODEC_H263;
1742             break;
1743
1744         case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1745         case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1746             p_track->fmt.i_codec = VLC_CODEC_SUBT;
1747             /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1748             /* FIXME UTF-8 doesn't work here ? */
1749             if( p_track->b_mac_encoding )
1750                 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1751             else
1752                 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1753             break;
1754
1755         case VLC_FOURCC('y','v','1','2'):
1756             p_track->fmt.i_codec = VLC_CODEC_YV12;
1757             break;
1758         case VLC_FOURCC('y','u','v','2'):
1759             p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1760             break;
1761
1762         case VLC_FOURCC('i','n','2','4'):
1763             p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1764                                     VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
1765             break;
1766         case VLC_FOURCC('f','l','3','2'):
1767             p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1768                                     VLC_CODEC_F32L : VLC_CODEC_F32B;
1769             break;
1770         case VLC_FOURCC('f','l','6','4'):
1771             p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1772                                     VLC_CODEC_F64L : VLC_CODEC_F64B;
1773             break;
1774         case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
1775         {
1776             MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1777             if( p_soun->i_qt_version == 2 &&
1778                 p_soun->i_qt_description > 20 + 28 )
1779             {
1780                 /* Flags:
1781                  *  0x01: IsFloat
1782                  *  0x02: IsBigEndian
1783                  *  0x04: IsSigned
1784                  */
1785                 static const struct {
1786                     unsigned     i_flags;
1787                     unsigned     i_mask;
1788                     unsigned     i_bits;
1789                     vlc_fourcc_t i_codec;
1790                 } p_formats[] = {
1791                     { 0x01,           0x03, 32, VLC_CODEC_F32L },
1792                     { 0x01,           0x03, 64, VLC_CODEC_F64L },
1793                     { 0x01|0x02,      0x03, 32, VLC_CODEC_F32B },
1794                     { 0x01|0x02,      0x03, 64, VLC_CODEC_F64B },
1795
1796                     { 0x00,           0x05,  8, VLC_CODEC_U8 },
1797                     { 0x00|     0x04, 0x05,  8, VLC_CODEC_S8 },
1798
1799                     { 0x00,           0x07, 16, VLC_CODEC_U16L },
1800                     { 0x00|0x02,      0x07, 16, VLC_CODEC_U16B },
1801                     { 0x00     |0x04, 0x07, 16, VLC_CODEC_S16L },
1802                     { 0x00|0x02|0x04, 0x07, 16, VLC_CODEC_S16B },
1803
1804                     { 0x00,           0x07, 24, VLC_CODEC_U24L },
1805                     { 0x00|0x02,      0x07, 24, VLC_CODEC_U24B },
1806                     { 0x00     |0x04, 0x07, 24, VLC_CODEC_S24L },
1807                     { 0x00|0x02|0x04, 0x07, 24, VLC_CODEC_S24B },
1808
1809                     { 0x00,           0x07, 32, VLC_CODEC_U32L },
1810                     { 0x00|0x02,      0x07, 32, VLC_CODEC_U32B },
1811                     { 0x00     |0x04, 0x07, 32, VLC_CODEC_S32L },
1812                     { 0x00|0x02|0x04, 0x07, 32, VLC_CODEC_S32B },
1813
1814                     {0, 0, 0, 0}
1815                 };
1816                 uint32_t i_bits  = GetDWBE(&p_soun->p_qt_description[20 + 20]);
1817                 uint32_t i_flags = GetDWBE(&p_soun->p_qt_description[20 + 24]);
1818
1819                 for( int i = 0; p_formats[i].i_codec; i++ )
1820                 {
1821                     if( p_formats[i].i_bits == i_bits &&
1822                         (i_flags & p_formats[i].i_mask) == p_formats[i].i_flags )
1823                     {
1824                         p_track->fmt.i_codec = p_formats[i].i_codec;
1825                         p_track->fmt.audio.i_bitspersample = i_bits;
1826                         p_track->fmt.audio.i_blockalign = p_soun->i_channelcount * i_bits / 8;
1827                         p_track->i_sample_size = p_track->fmt.audio.i_blockalign;
1828
1829                         p_soun->i_qt_version = 0;
1830                         break;
1831                     }
1832                 }
1833             }
1834             break;
1835         }
1836         default:
1837             p_track->fmt.i_codec = p_sample->i_type;
1838             break;
1839     }
1840
1841     /* now see if esds is present and if so create a data packet
1842         with decoder_specific_info  */
1843 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1844     if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1845           ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1846         ( p_esds->data.p_esds )&&
1847         ( p_decconfig ) )
1848     {
1849         /* First update information based on i_objectTypeIndication */
1850         switch( p_decconfig->i_objectTypeIndication )
1851         {
1852             case( 0x20 ): /* MPEG4 VIDEO */
1853                 p_track->fmt.i_codec = VLC_CODEC_MP4V;
1854                 break;
1855             case( 0x21 ): /* H.264 */
1856                 p_track->fmt.i_codec = VLC_CODEC_H264;
1857                 break;
1858             case( 0x40):
1859                 p_track->fmt.i_codec = VLC_CODEC_MP4A;
1860                 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
1861                      p_decconfig->p_decoder_specific_info[0]       == 0xF8 &&
1862                     (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
1863                 {
1864                     p_track->fmt.i_codec = VLC_CODEC_ALS;
1865                 }
1866                 break;
1867             case( 0x60):
1868             case( 0x61):
1869             case( 0x62):
1870             case( 0x63):
1871             case( 0x64):
1872             case( 0x65): /* MPEG2 video */
1873                 p_track->fmt.i_codec = VLC_CODEC_MPGV;
1874                 break;
1875             /* Theses are MPEG2-AAC */
1876             case( 0x66): /* main profile */
1877             case( 0x67): /* Low complexity profile */
1878             case( 0x68): /* Scaleable Sampling rate profile */
1879                 p_track->fmt.i_codec = VLC_CODEC_MP4A;
1880                 break;
1881             /* True MPEG 2 audio */
1882             case( 0x69):
1883                 p_track->fmt.i_codec = VLC_CODEC_MPGA;
1884                 break;
1885             case( 0x6a): /* MPEG1 video */
1886                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1887                 break;
1888             case( 0x6b): /* MPEG1 audio */
1889                 p_track->fmt.i_codec = VLC_CODEC_MPGA;
1890                 break;
1891             case( 0x6c ): /* jpeg */
1892                 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1893                 break;
1894             case( 0x6d ): /* png */
1895                 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
1896                 break;
1897             case( 0x6e ): /* jpeg2000 */
1898                 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
1899                 break;
1900             case( 0xa3 ): /* vc1 */
1901                 p_track->fmt.i_codec = VLC_FOURCC( 'W','V','C','1' );
1902                 break;
1903             case( 0xa4 ):
1904                 p_track->fmt.i_codec = VLC_CODEC_DIRAC;
1905                 break;
1906             case( 0xa5 ):
1907                 p_track->fmt.i_codec = VLC_CODEC_A52;
1908                 break;
1909             case( 0xa6 ):
1910                 p_track->fmt.i_codec = VLC_CODEC_EAC3;
1911                 break;
1912             case( 0xa9 ): /* dts */
1913             case( 0xaa ): /* DTS-HD HRA */
1914             case( 0xab ): /* DTS-HD Master Audio */
1915                 p_track->fmt.i_codec = VLC_CODEC_DTS;
1916                 break;
1917             case( 0xDD ):
1918                 p_track->fmt.i_codec = VLC_CODEC_VORBIS;
1919                 break;
1920
1921             /* Private ID */
1922             case( 0xe0 ): /* NeroDigital: dvd subs */
1923                 if( p_track->fmt.i_cat == SPU_ES )
1924                 {
1925                     p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1926                     if( p_track->i_width > 0 )
1927                         p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1928                     if( p_track->i_height > 0 )
1929                         p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1930                     break;
1931                 }
1932             case( 0xe1 ): /* QCelp for 3gp */
1933                 if( p_track->fmt.i_cat == AUDIO_ES )
1934                 {
1935                     p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
1936                 }
1937                 break;
1938
1939             /* Fallback */
1940             default:
1941                 /* Unknown entry, but don't touch i_fourcc */
1942                 msg_Warn( p_demux,
1943                           "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1944                           p_decconfig->i_objectTypeIndication,
1945                           p_track->i_track_ID );
1946                 break;
1947         }
1948         p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1949         if( p_track->fmt.i_extra > 0 )
1950         {
1951             p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1952             memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1953                     p_track->fmt.i_extra );
1954         }
1955     }
1956     else
1957     {
1958         switch( p_sample->i_type )
1959         {
1960             /* qt decoder, send the complete chunk */
1961             case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1962             case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1963             case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1964             case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1965             case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1966             case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1967             case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1968             case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1969             case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1970             case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1971             case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1972             case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1973                 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1974                 break;
1975             /* qt decoder, send the complete chunk */
1976             case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1977             case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1978             case VLC_FOURCC( 'V', 'P', '3', '1' ):
1979             case VLC_FOURCC( '3', 'I', 'V', '1' ):
1980             case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1981                 p_track->fmt.i_extra =
1982                     p_sample->data.p_sample_vide->i_qt_image_description;
1983                 if( p_track->fmt.i_extra > 0 )
1984                 {
1985                     p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1986                     memcpy( p_track->fmt.p_extra,
1987                             p_sample->data.p_sample_vide->p_qt_image_description,
1988                             p_track->fmt.i_extra);
1989                 }
1990                 break;
1991
1992             case VLC_CODEC_AMR_NB:
1993                 p_track->fmt.audio.i_rate = 8000;
1994             case VLC_CODEC_AMR_WB:
1995                 p_track->fmt.audio.i_rate = 16000;
1996             case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1997             case VLC_CODEC_QDM2:
1998             case VLC_CODEC_ALAC:
1999                 p_track->fmt.i_extra =
2000                     p_sample->data.p_sample_soun->i_qt_description;
2001                 if( p_track->fmt.i_extra > 0 )
2002                 {
2003                     p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2004                     memcpy( p_track->fmt.p_extra,
2005                             p_sample->data.p_sample_soun->p_qt_description,
2006                             p_track->fmt.i_extra);
2007                 }
2008                 if( p_track->fmt.i_extra >= 56 && p_sample->i_type == VLC_CODEC_ALAC )
2009                 {
2010                     p_track->fmt.audio.i_channels = *((uint8_t*)p_track->fmt.p_extra + 41);
2011                     p_track->fmt.audio.i_rate = GetDWBE((uint8_t*)p_track->fmt.p_extra + 52);
2012                 }
2013                 break;
2014
2015             /* avc1: send avcC (h264 without annexe B, ie without start code)*/
2016             case VLC_FOURCC( 'a', 'v', 'c', '1' ):
2017             {
2018                 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
2019
2020                 if( p_avcC )
2021                 {
2022                     p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
2023                     if( p_track->fmt.i_extra > 0 )
2024                     {
2025                         p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
2026                         memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
2027                                 p_track->fmt.i_extra );
2028                     }
2029                 }
2030                 else
2031                 {
2032                     msg_Err( p_demux, "missing avcC" );
2033                 }
2034                 break;
2035             }
2036
2037             case VLC_CODEC_ADPCM_MS:
2038             case VLC_CODEC_ADPCM_IMA_WAV:
2039             case VLC_CODEC_QCELP:
2040                 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
2041                 break;
2042
2043             default:
2044                 break;
2045         }
2046     }
2047
2048 #undef p_decconfig
2049
2050     if( pp_es )
2051         *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2052
2053     return VLC_SUCCESS;
2054 }
2055
2056 /* given a time it return sample/chunk
2057  * it also update elst field of the track
2058  */
2059 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2060                                    int64_t i_start, uint32_t *pi_chunk,
2061                                    uint32_t *pi_sample )
2062 {
2063     demux_sys_t *p_sys = p_demux->p_sys;
2064     MP4_Box_t   *p_box_stss;
2065     uint64_t     i_dts;
2066     unsigned int i_sample;
2067     unsigned int i_chunk;
2068     int          i_index;
2069
2070     /* FIXME see if it's needed to check p_track->i_chunk_count */
2071     if( p_track->i_chunk_count == 0 )
2072         return( VLC_EGENERIC );
2073
2074     /* handle elst (find the correct one) */
2075     MP4_TrackSetELST( p_demux, p_track, i_start );
2076     if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2077     {
2078         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2079         int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
2080
2081         /* now calculate i_start for this elst */
2082         /* offset */
2083         i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
2084         if( i_start < 0 )
2085         {
2086             *pi_chunk = 0;
2087             *pi_sample= 0;
2088
2089             return VLC_SUCCESS;
2090         }
2091         /* to track time scale */
2092         i_start  = i_start * p_track->i_timescale / (int64_t)1000000;
2093         /* add elst offset */
2094         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2095              elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2096             elst->i_media_time[p_track->i_elst] > 0 )
2097         {
2098             i_start += elst->i_media_time[p_track->i_elst];
2099         }
2100
2101         msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2102                  "ms (track)", p_track->i_elst,
2103                  i_mvt * 1000 / p_sys->i_timescale,
2104                  i_start * 1000 / p_track->i_timescale );
2105     }
2106     else
2107     {
2108         /* convert absolute time to in timescale unit */
2109         i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2110     }
2111
2112     /* we start from sample 0/chunk 0, hope it won't take too much time */
2113     /* *** find good chunk *** */
2114     for( i_chunk = 0; ; i_chunk++ )
2115     {
2116         if( i_chunk + 1 >= p_track->i_chunk_count )
2117         {
2118             /* at the end and can't check if i_start in this chunk,
2119                it will be check while searching i_sample */
2120             i_chunk = p_track->i_chunk_count - 1;
2121             break;
2122         }
2123
2124         if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2125             (uint64_t)i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
2126         {
2127             break;
2128         }
2129     }
2130
2131     /* *** find sample in the chunk *** */
2132     i_sample = p_track->chunk[i_chunk].i_sample_first;
2133     i_dts    = p_track->chunk[i_chunk].i_first_dts;
2134     for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2135     {
2136         if( i_dts +
2137             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2138             p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2139         {
2140             i_dts    +=
2141                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2142                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2143
2144             i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2145             i_index++;
2146         }
2147         else
2148         {
2149             if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2150             {
2151                 break;
2152             }
2153             i_sample += ( i_start - i_dts ) /
2154                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2155             break;
2156         }
2157     }
2158
2159     if( i_sample >= p_track->i_sample_count )
2160     {
2161         msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2162                   "(seeking too far) chunk=%d sample=%d",
2163                   p_track->i_track_ID, i_chunk, i_sample );
2164         return( VLC_EGENERIC );
2165     }
2166
2167
2168     /* *** Try to find nearest sync points *** */
2169     if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2170     {
2171         MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2172         msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2173                  p_track->i_track_ID );
2174         for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2175         {
2176             if( i_index >= p_stss->i_entry_count - 1 ||
2177                 i_sample < p_stss->i_sample_number[i_index+1] )
2178             {
2179                 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2180                 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
2181                          i_sample, i_sync_sample );
2182
2183                 if( i_sync_sample <= i_sample )
2184                 {
2185                     while( i_chunk > 0 &&
2186                            i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2187                         i_chunk--;
2188                 }
2189                 else
2190                 {
2191                     while( i_chunk < p_track->i_chunk_count - 1 &&
2192                            i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2193                                             p_track->chunk[i_chunk].i_sample_count )
2194                         i_chunk++;
2195                 }
2196                 i_sample = i_sync_sample;
2197                 break;
2198             }
2199         }
2200     }
2201     else
2202     {
2203         msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2204                  "Sample Box (stss)", p_track->i_track_ID );
2205     }
2206
2207     *pi_chunk  = i_chunk;
2208     *pi_sample = i_sample;
2209
2210     return VLC_SUCCESS;
2211 }
2212
2213 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2214                                  unsigned int i_chunk, unsigned int i_sample )
2215 {
2216     bool b_reselect = false;
2217
2218     /* now see if actual es is ok */
2219     if( p_track->i_chunk >= p_track->i_chunk_count ||
2220         p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2221             p_track->chunk[i_chunk].i_sample_description_index )
2222     {
2223         msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2224                   p_track->i_track_ID );
2225
2226         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2227                         p_track->p_es, &b_reselect );
2228
2229         es_out_Del( p_demux->out, p_track->p_es );
2230
2231         p_track->p_es = NULL;
2232
2233         if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2234         {
2235             msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2236                      p_track->i_track_ID );
2237
2238             p_track->b_ok       = false;
2239             p_track->b_selected = false;
2240             return VLC_EGENERIC;
2241         }
2242     }
2243
2244     /* select again the new decoder */
2245     if( b_reselect )
2246     {
2247         es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2248     }
2249
2250     p_track->i_chunk    = i_chunk;
2251     p_track->i_sample   = i_sample;
2252
2253     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2254 }
2255
2256 /****************************************************************************
2257  * MP4_TrackCreate:
2258  ****************************************************************************
2259  * Parse track information and create all needed data to run a track
2260  * If it succeed b_ok is set to 1 else to 0
2261  ****************************************************************************/
2262 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2263                              MP4_Box_t *p_box_trak,
2264                              bool b_force_enable )
2265 {
2266     demux_sys_t *p_sys = p_demux->p_sys;
2267
2268     MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2269     MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2270     MP4_Box_t *p_elst;
2271
2272     MP4_Box_t *p_mdhd;
2273     MP4_Box_t *p_udta;
2274     MP4_Box_t *p_hdlr;
2275
2276     MP4_Box_t *p_vmhd;
2277     MP4_Box_t *p_smhd;
2278
2279     unsigned int i;
2280     char language[4];
2281
2282     /* hint track unsupported */
2283
2284     /* set default value (-> track unusable) */
2285     p_track->b_ok       = false;
2286     p_track->b_enable   = false;
2287     p_track->b_selected = false;
2288     p_track->b_chapter  = false;
2289     p_track->b_mac_encoding = false;
2290
2291     es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2292
2293     if( !p_tkhd )
2294     {
2295         return;
2296     }
2297
2298     /* do we launch this track by default ? */
2299     p_track->b_enable =
2300         ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2301     if( !p_track->b_enable )
2302         p_track->fmt.i_priority = -1;
2303
2304     p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2305     p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2306     p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2307
2308     if( p_tref )
2309     {
2310 /*        msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2311     }
2312
2313     p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2314     p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2315
2316     if( ( !p_mdhd )||( !p_hdlr ) )
2317     {
2318         return;
2319     }
2320
2321     p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2322     if( !p_track->i_timescale )
2323         return;
2324
2325     if( p_mdhd->data.p_mdhd->i_language_code < 0x800 )
2326     {
2327         /* We can convert i_language_code into iso 639 code,
2328          * I won't */
2329         strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2330         p_track->b_mac_encoding = true;
2331     }
2332     else
2333     {
2334         for( i = 0; i < 3; i++ )
2335             language[i] = p_mdhd->data.p_mdhd->i_language[i];
2336         language[3] = '\0';
2337     }
2338
2339     switch( p_hdlr->data.p_hdlr->i_handler_type )
2340     {
2341         case( ATOM_soun ):
2342             if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2343             {
2344                 return;
2345             }
2346             p_track->fmt.i_cat = AUDIO_ES;
2347             break;
2348
2349         case( ATOM_vide ):
2350             if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2351             {
2352                 return;
2353             }
2354             p_track->fmt.i_cat = VIDEO_ES;
2355             break;
2356
2357         case( ATOM_text ):
2358         case( ATOM_subp ):
2359         case( ATOM_tx3g ):
2360         case( ATOM_sbtl ):
2361             p_track->fmt.i_cat = SPU_ES;
2362             break;
2363
2364         default:
2365             return;
2366     }
2367
2368     p_track->i_elst = 0;
2369     p_track->i_elst_time = 0;
2370     if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2371     {
2372         MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2373         unsigned int i;
2374
2375         msg_Warn( p_demux, "elst box found" );
2376         for( i = 0; i < elst->i_entry_count; i++ )
2377         {
2378             msg_Dbg( p_demux, "   - [%d] duration=%"PRId64"ms media time=%"PRId64
2379                      "ms) rate=%d.%d", i,
2380                      elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2381                      elst->i_media_time[i] >= 0 ?
2382                      (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2383                      INT64_C(-1),
2384                      elst->i_media_rate_integer[i],
2385                      elst->i_media_rate_fraction[i] );
2386         }
2387     }
2388
2389
2390 /*  TODO
2391     add support for:
2392     p_dinf = MP4_BoxGet( p_minf, "dinf" );
2393 */
2394     if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2395         !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2396     {
2397         return;
2398     }
2399
2400     /* Set language */
2401     if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2402     {
2403         p_track->fmt.psz_language = strdup( language );
2404     }
2405
2406     p_udta = MP4_BoxGet( p_box_trak, "udta" );
2407     if( p_udta )
2408     {
2409         MP4_Box_t *p_box_iter;
2410         for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2411                  p_box_iter = p_box_iter->p_next )
2412         {
2413             switch( p_box_iter->i_type )
2414             {
2415                 case ATOM_0xa9nam:
2416                     p_track->fmt.psz_description =
2417                         strdup( p_box_iter->data.p_0xa9xxx->psz_text );
2418                     break;
2419                 case ATOM_name:
2420                     p_track->fmt.psz_description =
2421                         strdup( p_box_iter->data.p_name->psz_text );
2422                     break;
2423             }
2424         }
2425     }
2426
2427     /* Create chunk index table and sample index table */
2428     if( TrackCreateChunksIndex( p_demux,p_track  ) ||
2429         TrackCreateSamplesIndex( p_demux, p_track ) )
2430     {
2431         return; /* cannot create chunks index */
2432     }
2433
2434     p_track->i_chunk  = 0;
2435     p_track->i_sample = 0;
2436
2437     /* Mark chapter only track */
2438     if( p_sys->p_tref_chap )
2439     {
2440         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2441         unsigned int i;
2442
2443         for( i = 0; i < p_chap->i_entry_count; i++ )
2444         {
2445             if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2446             {
2447                 p_track->b_chapter = true;
2448                 p_track->b_enable = false;
2449                 break;
2450             }
2451         }
2452     }
2453
2454     /* now create es */
2455     if( b_force_enable &&
2456         ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2457     {
2458         msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2459                   p_track->i_track_ID );
2460         p_track->b_enable = true;
2461         p_track->fmt.i_priority = 0;
2462     }
2463
2464     p_track->p_es = NULL;
2465     if( TrackCreateES( p_demux,
2466                        p_track, p_track->i_chunk,
2467                        p_track->b_chapter ? NULL : &p_track->p_es ) )
2468     {
2469         msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2470                  p_track->i_track_ID );
2471         return;
2472     }
2473     p_track->b_ok = true;
2474 #if 0
2475     {
2476         int i;
2477         for( i = 0; i < p_track->i_chunk_count; i++ )
2478         {
2479             fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2480                      i, p_track->chunk[i].i_sample_count,
2481                      p_track->chunk[i].i_first_dts );
2482
2483         }
2484     }
2485 #endif
2486 }
2487
2488 /****************************************************************************
2489  * MP4_TrackDestroy:
2490  ****************************************************************************
2491  * Destroy a track created by MP4_TrackCreate.
2492  ****************************************************************************/
2493 static void MP4_TrackDestroy( mp4_track_t *p_track )
2494 {
2495     unsigned int i_chunk;
2496
2497     p_track->b_ok = false;
2498     p_track->b_enable   = false;
2499     p_track->b_selected = false;
2500
2501     es_format_Clean( &p_track->fmt );
2502
2503     for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2504     {
2505         if( p_track->chunk )
2506         {
2507            FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2508            FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2509
2510            FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2511            FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2512         }
2513     }
2514     FREENULL( p_track->chunk );
2515
2516     if( !p_track->i_sample_size )
2517     {
2518         FREENULL( p_track->p_sample_size );
2519     }
2520 }
2521
2522 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2523                             mtime_t i_start )
2524 {
2525     if( !p_track->b_ok || p_track->b_chapter )
2526     {
2527         return VLC_EGENERIC;
2528     }
2529
2530     if( p_track->b_selected )
2531     {
2532         msg_Warn( p_demux, "track[Id 0x%x] already selected",
2533                   p_track->i_track_ID );
2534         return VLC_SUCCESS;
2535     }
2536
2537     return MP4_TrackSeek( p_demux, p_track, i_start );
2538 }
2539
2540 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2541 {
2542     if( !p_track->b_ok || p_track->b_chapter )
2543     {
2544         return;
2545     }
2546
2547     if( !p_track->b_selected )
2548     {
2549         msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2550                   p_track->i_track_ID );
2551         return;
2552     }
2553     if( p_track->p_es )
2554     {
2555         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2556                         p_track->p_es, false );
2557     }
2558
2559     p_track->b_selected = false;
2560 }
2561
2562 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2563                           mtime_t i_start )
2564 {
2565     uint32_t i_chunk;
2566     uint32_t i_sample;
2567
2568     if( !p_track->b_ok || p_track->b_chapter )
2569         return VLC_EGENERIC;
2570
2571     p_track->b_selected = false;
2572
2573     if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2574                                 &i_chunk, &i_sample ) )
2575     {
2576         msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2577                   p_track->i_track_ID );
2578         return VLC_EGENERIC;
2579     }
2580
2581     p_track->b_selected = true;
2582
2583     if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2584         p_track->b_selected = true;
2585
2586     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2587 }
2588
2589
2590 /*
2591  * 3 types: for audio
2592  *
2593  */
2594 #define QT_V0_MAX_SAMPLES 1024
2595 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2596 {
2597     int i_size;
2598     MP4_Box_data_sample_soun_t *p_soun;
2599
2600     if( p_track->i_sample_size == 0 )
2601     {
2602         /* most simple case */
2603         return p_track->p_sample_size[p_track->i_sample];
2604     }
2605     if( p_track->fmt.i_cat != AUDIO_ES )
2606     {
2607         return p_track->i_sample_size;
2608     }
2609
2610     p_soun = p_track->p_sample->data.p_sample_soun;
2611
2612     if( p_soun->i_qt_version == 1 )
2613     {
2614         int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
2615         if( p_track->fmt.audio.i_blockalign > 1 )
2616             i_samples = p_soun->i_sample_per_packet;
2617
2618         i_size = i_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2619     }
2620     else if( p_track->i_sample_size > 256 )
2621     {
2622         /* We do that so we don't read too much data
2623          * (in this case we are likely dealing with compressed data) */
2624         i_size = p_track->i_sample_size;
2625     }
2626     else
2627     {
2628         /* Read a bunch of samples at once */
2629         int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2630             ( p_track->i_sample -
2631               p_track->chunk[p_track->i_chunk].i_sample_first );
2632
2633         i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2634         i_size = i_samples * p_track->i_sample_size;
2635     }
2636
2637     //fprintf( stderr, "size=%d\n", i_size );
2638     return i_size;
2639 }
2640
2641 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2642 {
2643     unsigned int i_sample;
2644     uint64_t i_pos;
2645
2646     i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2647
2648     if( p_track->i_sample_size )
2649     {
2650         MP4_Box_data_sample_soun_t *p_soun =
2651             p_track->p_sample->data.p_sample_soun;
2652
2653         if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
2654         {
2655             i_pos += ( p_track->i_sample -
2656                        p_track->chunk[p_track->i_chunk].i_sample_first ) *
2657                      p_track->i_sample_size;
2658         }
2659         else
2660         {
2661             /* we read chunk by chunk unless a blockalign is requested */
2662             if( p_track->fmt.audio.i_blockalign > 1 )
2663                 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
2664                                 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2665         }
2666     }
2667     else
2668     {
2669         for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2670              i_sample < p_track->i_sample; i_sample++ )
2671         {
2672             i_pos += p_track->p_sample_size[i_sample];
2673         }
2674     }
2675
2676     return i_pos;
2677 }
2678
2679 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2680 {
2681     if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2682     {
2683         MP4_Box_data_sample_soun_t *p_soun;
2684
2685         p_soun = p_track->p_sample->data.p_sample_soun;
2686
2687         if( p_soun->i_qt_version == 1 )
2688         {
2689             /* we read chunk by chunk unless a blockalign is requested */
2690             if( p_track->fmt.audio.i_blockalign > 1 )
2691                 p_track->i_sample += p_soun->i_sample_per_packet;
2692             else
2693                 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
2694         }
2695         else if( p_track->i_sample_size > 256 )
2696         {
2697             /* We do that so we don't read too much data
2698              * (in this case we are likely dealing with compressed data) */
2699             p_track->i_sample += 1;
2700         }
2701         else
2702         {
2703             /* FIXME */
2704             p_track->i_sample += QT_V0_MAX_SAMPLES;
2705             if( p_track->i_sample >
2706                 p_track->chunk[p_track->i_chunk].i_sample_first +
2707                 p_track->chunk[p_track->i_chunk].i_sample_count )
2708             {
2709                 p_track->i_sample =
2710                     p_track->chunk[p_track->i_chunk].i_sample_first +
2711                     p_track->chunk[p_track->i_chunk].i_sample_count;
2712             }
2713         }
2714     }
2715     else
2716     {
2717         p_track->i_sample++;
2718     }
2719
2720     if( p_track->i_sample >= p_track->i_sample_count )
2721         return VLC_EGENERIC;
2722
2723     /* Have we changed chunk ? */
2724     if( p_track->i_sample >=
2725             p_track->chunk[p_track->i_chunk].i_sample_first +
2726             p_track->chunk[p_track->i_chunk].i_sample_count )
2727     {
2728         if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2729                                   p_track->i_sample ) )
2730         {
2731             msg_Warn( p_demux, "track[0x%x] will be disabled "
2732                       "(cannot restart decoder)", p_track->i_track_ID );
2733             MP4_TrackUnselect( p_demux, p_track );
2734             return VLC_EGENERIC;
2735         }
2736     }
2737
2738     /* Have we changed elst */
2739     if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2740     {
2741         demux_sys_t *p_sys = p_demux->p_sys;
2742         MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2743         uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2744                         p_sys->i_timescale / (int64_t)1000000;
2745
2746         if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2747             i_mvt >= p_track->i_elst_time +
2748                      elst->i_segment_duration[p_track->i_elst] )
2749         {
2750             MP4_TrackSetELST( p_demux, p_track,
2751                               MP4_TrackGetDTS( p_demux, p_track ) );
2752         }
2753     }
2754
2755     return VLC_SUCCESS;
2756 }
2757
2758 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2759                               int64_t i_time )
2760 {
2761     demux_sys_t *p_sys = p_demux->p_sys;
2762     int         i_elst_last = tk->i_elst;
2763
2764     /* handle elst (find the correct one) */
2765     tk->i_elst      = 0;
2766     tk->i_elst_time = 0;
2767     if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2768     {
2769         MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2770         int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2771
2772         for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2773         {
2774             mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2775
2776             if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2777             {
2778                 break;
2779             }
2780             tk->i_elst_time += i_dur;
2781         }
2782
2783         if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2784         {
2785             /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2786             tk->i_elst = elst->i_entry_count - 1;
2787             tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2788         }
2789
2790         if( elst->i_media_time[tk->i_elst] < 0 )
2791         {
2792             /* track offset */
2793             tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2794         }
2795     }
2796     if( i_elst_last != tk->i_elst )
2797     {
2798         msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2799     }
2800 }
2801
2802 /* */
2803 static const char *MP4_ConvertMacCode( uint16_t i_code )
2804 {
2805     static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
2806         { "en",   0 }, { "fr",   1 }, { "de",   2 }, { "it",   3 }, { "nl",   4 },
2807         { "sv",   5 }, { "es",   6 }, { "da",   7 }, { "pt",   8 }, { "no",   9 },
2808         { "he",  10 }, { "ja",  11 }, { "ar",  12 }, { "fi",  13 }, { "el",  14 },
2809         { "is",  15 }, { "mt",  16 }, { "tr",  17 }, { "hr",  18 }, { "zh",  19 },
2810         { "ur",  20 }, { "hi",  21 }, { "th",  22 }, { "ko",  23 }, { "lt",  24 },
2811         { "pl",  25 }, { "hu",  26 }, { "et",  27 }, { "lv",  28 }, //{ "??",  29 },
2812         { "fo",  30 }, { "fa",  31 }, { "ru",  32 }, { "zh",  33 }, { "nl",  34 },
2813         { "ga",  35 }, { "sq",  36 }, { "ro",  37 }, { "cs",  38 }, { "sk",  39 },
2814         { "sl",  40 }, { "yi",  41 }, { "sr",  42 }, { "mk",  43 }, { "bg",  44 },
2815         { "uk",  45 }, { "be",  46 }, { "uz",  47 }, { "az",  48 }, { "kk",  48 },
2816         { "az",  50 }, { "hy",  51 }, { "ka",  52 }, { "mo",  53 }, { "ky",  54 },
2817         { "tg",  55 }, { "tk",  56 }, { "mn",  57 }, { "mn",  58 }, { "ps",  59 },
2818         { "ku",  60 }, { "ks",  61 }, { "sd",  62 }, { "bo",  63 }, { "ne",  64 },
2819         { "sa",  65 }, { "mr",  66 }, { "bn",  67 }, { "as",  68 }, { "gu",  69 },
2820         { "pa",  70 }, { "or",  71 }, { "ml",  72 }, { "kn",  73 }, { "ta",  74 },
2821         { "te",  75 }, { "si",  76 }, { "my",  77 }, { "km",  78 }, { "lo",  79 },
2822         { "vi",  80 }, { "id",  81 }, { "tl",  82 }, { "ms",  83 }, { "ms",  84 },
2823         { "am",  85 }, { "ti",  86 }, { "om",  87 }, { "so",  88 }, { "sw",  89 },
2824         { "rw",  90 }, { "rn",  91 }, { "ny",  92 }, { "mg",  93 }, { "eo",  94 },
2825
2826                                                      { "cy", 128 }, { "eu", 129 },
2827         { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
2828         { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
2829         { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
2830         { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
2831         /* */
2832         { "", 0 }
2833     };
2834     int i;
2835     for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
2836     {
2837         if( p_cvt[i].i_code == i_code )
2838             return p_cvt[i].psz_iso639_1;
2839     }
2840     return "";
2841 }