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