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