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