]> git.sesse.net Git - vlc/blob - modules/demux/mp4/mp4.c
6ce09b06b04cde078715dc152b16651d7f199bfa
[vlc] / modules / demux / mp4 / mp4.c
1 /*****************************************************************************
2  * mp4.c : MP4 file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33
34 #include <vlc_demux.h>
35 #include <vlc_charset.h>                           /* EnsureUTF8 */
36 #include <vlc_meta.h>                              /* vlc_meta_t, vlc_meta_ */
37 #include <vlc_input.h>
38 #include <assert.h>
39
40 #include "libmp4.h"
41 #include "id3genres.h"                             /* for ATOM_gnre */
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
48
49 vlc_module_begin ()
50     set_category( CAT_INPUT )
51     set_subcategory( SUBCAT_INPUT_DEMUX )
52     set_description( N_("MP4 stream demuxer") )
53     set_shortname( N_("MP4") )
54     set_capability( "demux", 240 )
55     set_callbacks( Open, Close )
56 vlc_module_end ()
57
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 static int   Demux   ( demux_t * );
62 static int   DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
63 static int   DemuxFrg( demux_t * );
64 static int   Seek    ( demux_t *, mtime_t );
65 static int   Control ( demux_t *, int, va_list );
66
67 typedef struct mp4_fragment_t mp4_fragment_t;
68 struct mp4_fragment_t
69 {
70     uint64_t i_chunk_range_min_offset;
71     uint64_t i_chunk_range_max_offset;
72     uint64_t i_duration;
73     MP4_Box_t *p_moox;
74     mp4_fragment_t *p_next;
75 };
76
77 struct demux_sys_t
78 {
79     MP4_Box_t    *p_root;      /* container for the whole file */
80
81     mtime_t      i_pcr;
82
83     uint64_t     i_overall_duration; /* Full duration, including all fragments */
84     uint64_t     i_time;         /* time position of the presentation
85                                   * in movie timescale */
86     uint32_t     i_timescale;    /* movie time scale */
87     uint64_t     i_duration;     /* movie duration */
88     unsigned int i_tracks;       /* number of tracks */
89     mp4_track_t  *track;         /* array of track */
90     float        f_fps;          /* number of frame per seconds */
91
92     bool         b_fragmented;   /* fMP4 */
93     bool         b_seekable;
94     bool         b_fastseekable;
95     bool         b_smooth;       /* Is it Smooth Streaming? */
96
97     bool            b_fragments_probed;
98     mp4_fragment_t  moovfragment; /* moov */
99     mp4_fragment_t *p_fragments;  /* known fragments (moof following moov) */
100
101     /* */
102     MP4_Box_t    *p_tref_chap;
103
104     /* */
105     input_title_t *p_title;
106 };
107
108 #define BOXDATA(type) type->data.type
109
110 /*****************************************************************************
111  * Declaration of local function
112  *****************************************************************************/
113 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t  *, bool b_force_enable );
114 static int MP4_frg_TrackCreate( demux_t *, mp4_track_t *, MP4_Box_t *);
115 static void MP4_TrackDestroy(  mp4_track_t * );
116
117 static int  MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
118 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
119
120 static int  MP4_TrackSeek   ( demux_t *, mp4_track_t *, mtime_t );
121
122 static uint64_t MP4_TrackGetPos    ( mp4_track_t * );
123 static uint32_t MP4_TrackSampleSize( mp4_track_t *, uint32_t * );
124 static int      MP4_TrackNextSample( demux_t *, mp4_track_t *, uint32_t );
125 static void     MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
126
127 static void     MP4_UpdateSeekpoint( demux_t * );
128 static const char *MP4_ConvertMacCode( uint16_t );
129
130 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id );
131
132 static bool AddFragment( demux_t *p_demux, MP4_Box_t *p_moox );
133 static int  ProbeFragments( demux_t *p_demux );
134
135 /* Helpers */
136
137 static uint32_t stream_ReadU32( stream_t *s, void *p_read, uint32_t i_toread )
138 {
139     uint32_t i_return = 0;
140     if ( i_toread > INT32_MAX )
141     {
142         i_return = stream_Read( s, p_read, INT32_MAX );
143         if ( i_return < INT32_MAX )
144             return i_return;
145         else
146             i_toread -= INT32_MAX;
147     }
148     i_return += stream_Read( s, (uint8_t *)p_read + i_return, (int32_t) i_toread );
149     return i_return;
150 }
151
152 static bool MP4_stream_Tell( stream_t *s, uint64_t *pi_pos )
153 {
154     int64_t i_pos = stream_Tell( s );
155     if ( i_pos < 0 )
156         return false;
157     else
158     {
159         *pi_pos = (uint64_t) i_pos;
160         return true;
161     }
162 }
163
164 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
165 {
166     MP4_Box_t *p_trex = MP4_BoxGet( p_moov, "mvex/trex" );
167     while( p_trex )
168     {
169         if ( p_trex->i_type == ATOM_trex && BOXDATA(p_trex)->i_track_ID == i_id )
170                 break;
171         else
172             p_trex = p_trex->p_next;
173     }
174     return p_trex;
175 }
176
177 static MP4_Box_t * MP4_GetTrakByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
178 {
179     MP4_Box_t *p_trak = MP4_BoxGet( p_moov, "trak" );
180     MP4_Box_t *p_tkhd;
181     while( p_trak )
182     {
183         if( p_trak->i_type == ATOM_trak &&
184             (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) &&
185             BOXDATA(p_tkhd)->i_track_ID == i_id )
186                 break;
187         else
188             p_trak = p_trak->p_next;
189     }
190     return p_trak;
191 }
192
193 /* Return time in microsecond of a track */
194 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
195 {
196     demux_sys_t *p_sys = p_demux->p_sys;
197     mp4_chunk_t chunk;
198     if( p_sys->b_fragmented )
199         chunk = *p_track->cchunk;
200     else
201         chunk = p_track->chunk[p_track->i_chunk];
202
203     unsigned int i_index = 0;
204     unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
205     int64_t i_dts = chunk.i_first_dts;
206
207     while( i_sample > 0 )
208     {
209         if( i_sample > chunk.p_sample_count_dts[i_index] )
210         {
211             i_dts += chunk.p_sample_count_dts[i_index] *
212                 chunk.p_sample_delta_dts[i_index];
213             i_sample -= chunk.p_sample_count_dts[i_index];
214             i_index++;
215         }
216         else
217         {
218             i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
219             break;
220         }
221     }
222
223     /* now handle elst */
224     if( p_track->p_elst )
225     {
226         demux_sys_t         *p_sys = p_demux->p_sys;
227         MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
228
229         /* convert to offset */
230         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
231               elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
232             elst->i_media_time[p_track->i_elst] > 0 )
233         {
234             i_dts -= elst->i_media_time[p_track->i_elst];
235         }
236
237         /* add i_elst_time */
238         i_dts += p_track->i_elst_time * p_track->i_timescale /
239             p_sys->i_timescale;
240
241         if( i_dts < 0 ) i_dts = 0;
242     }
243
244     return CLOCK_FREQ * i_dts / p_track->i_timescale;
245 }
246
247 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
248 {
249     demux_sys_t *p_sys = p_demux->p_sys;
250     mp4_chunk_t *ck;
251     if( p_sys->b_fragmented )
252         ck = p_track->cchunk;
253     else
254         ck = &p_track->chunk[p_track->i_chunk];
255
256     unsigned int i_index = 0;
257     unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
258
259     if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
260         return -1;
261
262     for( i_index = 0;; i_index++ )
263     {
264         if( i_sample < ck->p_sample_count_pts[i_index] )
265             return ck->p_sample_offset_pts[i_index] * CLOCK_FREQ /
266                    (int64_t)p_track->i_timescale;
267
268         i_sample -= ck->p_sample_count_pts[i_index];
269     }
270 }
271
272 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
273 {
274     return CLOCK_FREQ * p_sys->i_time / p_sys->i_timescale;
275 }
276
277 static void LoadChapter( demux_t  *p_demux );
278
279 static int LoadInitFrag( demux_t *p_demux )
280 {
281     demux_sys_t *p_sys = p_demux->p_sys;
282
283     if( p_sys->b_smooth ) /* Smooth Streaming */
284     {
285         if( ( p_sys->p_root = MP4_BoxGetSmooBox( p_demux->s ) ) == NULL )
286         {
287             goto LoadInitFragError;
288         }
289         else
290         {
291             MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
292             if( !p_smoo || CmpUUID( &p_smoo->i_uuid, &SmooBoxUUID ) )
293                 goto LoadInitFragError;
294             /* Get number of tracks */
295             p_sys->i_tracks = 0;
296             for( int i = 0; i < 3; i++ )
297             {
298                 MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
299                 if( p_stra && BOXDATA(p_stra)->i_track_ID )
300                     p_sys->i_tracks++;
301                 /* Get timescale and duration of the video track; */
302                 if( p_sys->i_timescale == 0 )
303                 {
304                     p_sys->i_timescale = BOXDATA(p_stra)->i_timescale;
305                     p_sys->i_duration = BOXDATA(p_stra)->i_duration;
306                     if( p_sys->i_timescale == 0 )
307                     {
308                         msg_Err( p_demux, "bad timescale" );
309                         goto LoadInitFragError;
310                     }
311                 }
312             }
313         }
314     }
315     else
316     {
317         /* Load all boxes ( except raw data ) */
318         if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
319         {
320             goto LoadInitFragError;
321         }
322     }
323     return VLC_SUCCESS;
324
325 LoadInitFragError:
326     msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" );
327     return VLC_EGENERIC;
328 }
329
330 static int InitTracks( demux_t *p_demux )
331 {
332     demux_sys_t *p_sys = p_demux->p_sys;
333
334     p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
335     if( p_sys->track == NULL )
336         return VLC_EGENERIC;
337
338     if( p_sys->b_fragmented )
339     {
340         mp4_track_t *p_track;
341         for( uint16_t i = 0; i < p_sys->i_tracks; i++ )
342         {
343             p_track = &p_sys->track[i];
344             p_track->cchunk = calloc( 1, sizeof( mp4_chunk_t ) );
345             if( unlikely( !p_track->cchunk ) )
346             {
347                 free( p_sys->track );
348                 return VLC_EGENERIC;
349             }
350         }
351     }
352     return VLC_SUCCESS;
353 }
354
355 static void CreateTracksFromSmooBox( demux_t *p_demux )
356 {
357     demux_sys_t *p_sys = p_demux->p_sys;
358
359     MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
360     mp4_track_t *p_track;
361     int j = 0;
362     for( int i = 0; i < 3; i++ )
363     {
364         MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
365         if( !p_stra || BOXDATA(p_stra)->i_track_ID == 0 )
366             continue;
367         else
368         {
369             p_track = &p_sys->track[j]; j++;
370             MP4_frg_TrackCreate( p_demux, p_track, p_stra );
371             p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
372         }
373     }
374 }
375
376 /*****************************************************************************
377  * Open: check file and initializes MP4 structures
378  *****************************************************************************/
379 static int Open( vlc_object_t * p_this )
380 {
381     demux_t  *p_demux = (demux_t *)p_this;
382     demux_sys_t     *p_sys;
383
384     const uint8_t   *p_peek;
385
386     MP4_Box_t       *p_ftyp;
387     MP4_Box_t       *p_rmra;
388     MP4_Box_t       *p_mvhd;
389     MP4_Box_t       *p_trak;
390
391     unsigned int    i;
392     bool      b_enabled_es;
393
394     /* A little test to see if it could be a mp4 */
395     if( stream_Peek( p_demux->s, &p_peek, 11 ) < 11 ) return VLC_EGENERIC;
396
397     switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
398     {
399         case ATOM_moov:
400         case ATOM_foov:
401         case ATOM_moof:
402         case ATOM_mdat:
403         case ATOM_udta:
404         case ATOM_free:
405         case ATOM_skip:
406         case ATOM_wide:
407         case ATOM_uuid:
408         case VLC_FOURCC( 'p', 'n', 'o', 't' ):
409             break;
410         case ATOM_ftyp:
411             /* We don't yet support f4v, but avformat does. */
412             if( p_peek[8] == 'f' && p_peek[9] == '4' && p_peek[10] == 'v' )
413                 return VLC_EGENERIC;
414             break;
415          default:
416             return VLC_EGENERIC;
417     }
418
419     /* create our structure that will contains all data */
420     p_sys = calloc( 1, sizeof( demux_sys_t ) );
421     if ( !p_sys )
422         return VLC_EGENERIC;
423
424     /* I need to seek */
425     stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
426     if( !p_sys->b_seekable )
427     {
428         msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" );
429         free( p_sys );
430         return VLC_EGENERIC;
431     }
432     stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable );
433
434     /*Set exported functions */
435     p_demux->pf_demux = Demux;
436     p_demux->pf_control = Control;
437
438     p_demux->p_sys = p_sys;
439
440     if( stream_Peek( p_demux->s, &p_peek, 24 ) < 24 ) return VLC_EGENERIC;
441     if( !CmpUUID( (UUID_t *)(p_peek + 8), &SmooBoxUUID ) )
442     {
443         p_sys->b_smooth = true;
444         p_sys->b_fragmented = true;
445     }
446
447     if( LoadInitFrag( p_demux ) != VLC_SUCCESS )
448         goto error;
449
450     if( MP4_BoxCount( p_sys->p_root, "/moov/mvex" ) > 0 )
451     {
452         if ( p_sys->b_seekable )
453         {
454             /* Probe remaining to check if there's really fragments
455                or if that file is just ready to append fragments */
456             ProbeFragments( p_demux );
457             p_sys->b_fragmented = !!MP4_BoxCount( p_sys->p_root, "/moof" );
458         }
459         else
460             p_sys->b_fragmented = true;
461     }
462
463     if ( !p_sys->moovfragment.p_moox )
464         AddFragment( p_demux, MP4_BoxGet( p_sys->p_root, "/moov" ) );
465
466     /* we always need a moov entry, but smooth has a workaround */
467     if ( !p_sys->moovfragment.p_moox && !p_sys->b_smooth )
468     {
469         goto error;
470     }
471
472     if( p_sys->b_fragmented || p_sys->b_smooth )
473     {
474         p_demux->pf_demux = DemuxFrg;
475     }
476
477     if( p_sys->b_smooth )
478     {
479         if( InitTracks( p_demux ) != VLC_SUCCESS )
480             goto error;
481         CreateTracksFromSmooBox( p_demux );
482         return VLC_SUCCESS;
483     }
484     else if( p_sys->b_fragmented )
485     {
486         /* We are not yet able to demux a fragmented MP4 file, using the 'mfra'
487          * a file, and we let avformat do the job. */
488         msg_Warn( p_demux, "MP4 plugin discarded "\
489                 "(fragmented, let avformat demux it)" );
490         stream_Seek( p_demux->s, 0 ); /* rewind, for other demux */
491         goto error;
492     }
493
494     MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
495
496     if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
497     {
498         switch( BOXDATA(p_ftyp)->i_major_brand )
499         {
500             case( ATOM_isom ):
501                 msg_Dbg( p_demux,
502                          "ISO Media file (isom) version %d.",
503                          BOXDATA(p_ftyp)->i_minor_version );
504                 break;
505             case( ATOM_3gp4 ):
506             case( VLC_FOURCC( '3', 'g', 'p', '5' ) ):
507             case( VLC_FOURCC( '3', 'g', 'p', '6' ) ):
508             case( VLC_FOURCC( '3', 'g', 'p', '7' ) ):
509                 msg_Dbg( p_demux, "3GPP Media file Release: %c",
510 #ifdef WORDS_BIGENDIAN
511                         BOXDATA(p_ftyp)->i_major_brand
512 #else
513                         BOXDATA(p_ftyp)->i_major_brand >> 24
514 #endif
515                         );
516                 break;
517             case( VLC_FOURCC( 'q', 't', ' ', ' ') ):
518                 msg_Dbg( p_demux, "Apple QuickTime file" );
519                 break;
520             case( VLC_FOURCC( 'i', 's', 'm', 'l') ):
521                 msg_Dbg( p_demux, "PIFF (= isml = fMP4) file" );
522                 break;
523             default:
524                 msg_Dbg( p_demux,
525                          "unrecognized major file specification (%4.4s).",
526                           (char*)&BOXDATA(p_ftyp)->i_major_brand );
527                 break;
528         }
529     }
530     else
531     {
532         msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
533     }
534
535     /* the file need to have one moov box */
536     p_sys->moovfragment.p_moox = MP4_BoxGet( p_sys->p_root, "/moov", 0 );
537     if( !p_sys->moovfragment.p_moox )
538     {
539         MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
540
541         if( !p_foov )
542         {
543             msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
544             goto error;
545         }
546         /* we have a free box as a moov, rename it */
547         p_foov->i_type = ATOM_moov;
548         p_sys->moovfragment.p_moox = p_foov;
549     }
550
551     if( ( p_rmra = MP4_BoxGet( p_sys->p_root,  "/moov/rmra" ) ) )
552     {
553         int        i_count = MP4_BoxCount( p_rmra, "rmda" );
554         int        i;
555
556         msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
557
558         input_thread_t *p_input = demux_GetParentInput( p_demux );
559         input_item_t *p_current = input_GetItem( p_input );
560
561         input_item_node_t *p_subitems = input_item_node_Create( p_current );
562
563         for( i = 0; i < i_count; i++ )
564         {
565             MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
566             char      *psz_ref;
567             uint32_t  i_ref_type;
568
569             if( !p_rdrf || !( psz_ref = strdup( BOXDATA(p_rdrf)->psz_ref ) ) )
570             {
571                 continue;
572             }
573             i_ref_type = BOXDATA(p_rdrf)->i_ref_type;
574
575             msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
576                      psz_ref, (char*)&i_ref_type );
577
578             if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
579             {
580                 if( strstr( psz_ref, "qt5gateQT" ) )
581                 {
582                     msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
583                     continue;
584                 }
585                 if( !strncmp( psz_ref, "http://", 7 ) ||
586                     !strncmp( psz_ref, "rtsp://", 7 ) )
587                 {
588                     ;
589                 }
590                 else
591                 {
592                     char *psz_absolute;
593                     char *psz_path = strdup( p_demux->psz_location );
594                     char *end = strrchr( psz_path, '/' );
595                     if( end ) end[1] = '\0';
596                     else *psz_path = '\0';
597
598                     if( asprintf( &psz_absolute, "%s://%s%s",
599                                   p_demux->psz_access, psz_path, psz_ref ) < 0 )
600                     {
601                         free( psz_ref );
602                         free( psz_path );
603                         input_item_node_Delete( p_subitems );
604                         vlc_object_release( p_input) ;
605                         return VLC_ENOMEM;
606                     }
607
608                     free( psz_ref );
609                     psz_ref = psz_absolute;
610                     free( psz_path );
611                 }
612                 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
613                 input_item_t *p_item = input_item_New( psz_ref, NULL );
614                 input_item_CopyOptions( p_current, p_item );
615                 input_item_node_AppendItem( p_subitems, p_item );
616                 vlc_gc_decref( p_item );
617             }
618             else
619             {
620                 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
621                          (char*)&BOXDATA(p_rdrf)->i_ref_type );
622             }
623             free( psz_ref );
624         }
625         input_item_node_PostAndDelete( p_subitems );
626         vlc_object_release( p_input );
627     }
628
629     if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
630     {
631         if( !p_rmra )
632         {
633             msg_Err( p_demux, "cannot find /moov/mvhd" );
634             goto error;
635         }
636         else
637         {
638             msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
639             p_demux->pf_demux = DemuxRef;
640             return VLC_SUCCESS;
641         }
642     }
643     else
644     {
645         p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
646         if( p_sys->i_timescale == 0 )
647         {
648             msg_Err( p_this, "bad timescale" );
649             goto error;
650         }
651     }
652
653     if ( p_sys->i_overall_duration == 0 )
654     {
655         /* Try in mehd if fragmented */
656         MP4_Box_t *p_mehd = MP4_BoxGet( p_demux->p_sys->p_root, "moov/mvex/mehd");
657         if ( p_mehd )
658             p_sys->i_overall_duration = p_mehd->data.p_mehd->i_fragment_duration;
659         else
660             p_sys->i_overall_duration = p_sys->moovfragment.i_duration;
661     }
662
663     if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
664     {
665         msg_Err( p_demux, "cannot find any /moov/trak" );
666         goto error;
667     }
668     msg_Dbg( p_demux, "found %d track%c",
669                         p_sys->i_tracks,
670                         p_sys->i_tracks ? 's':' ' );
671
672     if( InitTracks( p_demux ) != VLC_SUCCESS )
673         goto error;
674
675     /* Search the first chap reference (like quicktime) and
676      * check that at least 1 stream is enabled */
677     p_sys->p_tref_chap = NULL;
678     b_enabled_es = false;
679     for( i = 0; i < p_sys->i_tracks; i++ )
680     {
681         MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
682
683
684         MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
685         if( p_tkhd && (BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED) )
686             b_enabled_es = true;
687
688         MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
689         if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
690             p_sys->p_tref_chap = p_chap;
691     }
692
693     /* now process each track and extract all useful information */
694     for( i = 0; i < p_sys->i_tracks; i++ )
695     {
696         p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
697         MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
698
699         if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
700         {
701             const char *psz_cat;
702             switch( p_sys->track[i].fmt.i_cat )
703             {
704                 case( VIDEO_ES ):
705                     psz_cat = "video";
706                     break;
707                 case( AUDIO_ES ):
708                     psz_cat = "audio";
709                     break;
710                 case( SPU_ES ):
711                     psz_cat = "subtitle";
712                     break;
713
714                 default:
715                     psz_cat = "unknown";
716                     break;
717             }
718
719             msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
720                      p_sys->track[i].i_track_ID, psz_cat,
721                      p_sys->track[i].b_enable ? "enable":"disable",
722                      p_sys->track[i].fmt.psz_language ?
723                      p_sys->track[i].fmt.psz_language : "undef" );
724         }
725         else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
726         {
727             msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
728                      p_sys->track[i].i_track_ID,
729                      p_sys->track[i].fmt.psz_language ?
730                      p_sys->track[i].fmt.psz_language : "undef" );
731         }
732         else
733         {
734             msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
735                      p_sys->track[i].i_track_ID );
736         }
737     }
738
739     mp4_fragment_t *p_fragment = &p_sys->moovfragment;
740     while ( p_fragment )
741     {
742         msg_Dbg( p_demux, "fragment offset %"PRId64", data %"PRId64"<->%"PRId64", time %"PRId64,
743                  p_fragment->p_moox->i_pos, p_fragment->i_chunk_range_min_offset,
744                  p_fragment->i_chunk_range_max_offset, p_fragment->i_duration );
745         p_fragment = p_fragment->p_next;
746     }
747
748     /* */
749     LoadChapter( p_demux );
750
751     return VLC_SUCCESS;
752
753 error:
754     if( p_sys->p_root )
755     {
756         MP4_BoxFree( p_demux->s, p_sys->p_root );
757     }
758     free( p_sys );
759     return VLC_EGENERIC;
760 }
761
762 /*****************************************************************************
763  * Demux: read packet and send them to decoders
764  *****************************************************************************
765  * TODO check for newly selected track (ie audio upt to now )
766  *****************************************************************************/
767 static int Demux( demux_t *p_demux )
768 {
769     demux_sys_t *p_sys = p_demux->p_sys;
770     unsigned int i_track;
771
772
773     unsigned int i_track_selected;
774
775     /* check for newly selected/unselected track */
776     for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
777          i_track++ )
778     {
779         mp4_track_t *tk = &p_sys->track[i_track];
780         bool b;
781
782         if( !tk->b_ok || tk->b_chapter ||
783             ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
784         {
785             continue;
786         }
787
788         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
789
790         if( tk->b_selected && !b )
791         {
792             MP4_TrackUnselect( p_demux, tk );
793         }
794         else if( !tk->b_selected && b)
795         {
796             MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
797         }
798
799         if( tk->b_selected )
800         {
801             i_track_selected++;
802         }
803     }
804
805     if( i_track_selected <= 0 )
806     {
807         p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
808         if( p_sys->i_timescale > 0 )
809         {
810             int64_t i_length = CLOCK_FREQ *
811                                (mtime_t)p_sys->moovfragment.i_duration /
812                                (mtime_t)p_sys->i_timescale;
813             if( MP4_GetMoviePTS( p_sys ) >= i_length )
814                 return 0;
815             return 1;
816         }
817
818         msg_Warn( p_demux, "no track selected, exiting..." );
819         return 0;
820     }
821
822     /* */
823     MP4_UpdateSeekpoint( p_demux );
824
825     /* first wait for the good time to read a packet */
826     p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
827
828     bool b_data_sent = false;
829
830     /* Find next track matching contiguous data */
831     mp4_track_t *tk = NULL;
832     uint64_t i_candidate_pos = UINT64_MAX;
833     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
834     {
835         mp4_track_t *tk_tmp = &p_sys->track[i_track];
836         if( !tk_tmp->b_ok || tk_tmp->b_chapter || !tk_tmp->b_selected || tk_tmp->i_sample >= tk_tmp->i_sample_count )
837             continue;
838
839         uint64_t i_pos = MP4_TrackGetPos( tk_tmp );
840         if ( i_pos <= i_candidate_pos )
841         {
842             i_candidate_pos = i_pos;
843             tk = tk_tmp;
844         }
845     }
846
847     if ( !tk )
848     {
849         msg_Dbg( p_demux, "Could not select track by data position" );
850         goto end;
851     }
852
853 #if 0
854     msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, i_track,
855              MP4_TrackGetDTS( p_demux, tk ),
856              MP4_GetMoviePTS( p_sys ), i_candidate_pos );
857 #endif
858
859     uint32_t i_nb_samples = 0;
860     uint32_t i_samplessize = MP4_TrackSampleSize( tk, &i_nb_samples );
861     if( i_samplessize > 0 )
862     {
863         block_t *p_block;
864         int64_t i_delta;
865         uint64_t i_current_pos;
866
867         /* go,go go ! */
868         if ( !MP4_stream_Tell( p_demux->s, &i_current_pos ) )
869             goto end;
870
871         if( i_current_pos != i_candidate_pos )
872         {
873             if( stream_Seek( p_demux->s, i_candidate_pos ) )
874             {
875                 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
876                           tk->i_track_ID );
877                 MP4_TrackUnselect( p_demux, tk );
878                 goto end;
879             }
880         }
881
882         /* now read pes */
883         if( !(p_block = stream_Block( p_demux->s, i_samplessize )) )
884         {
885             msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
886                       tk->i_track_ID );
887             MP4_TrackUnselect( p_demux, tk );
888             goto end;
889         }
890         else if( tk->fmt.i_cat == SPU_ES )
891         {
892             if ( tk->fmt.i_codec != VLC_CODEC_TX3G &&
893                  tk->fmt.i_codec != VLC_CODEC_SPU )
894                 p_block->i_buffer = 0;
895         }
896
897         /* dts */
898         p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
899         /* pts */
900         i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
901         if( i_delta != -1 )
902             p_block->i_pts = p_block->i_dts + i_delta;
903         else if( tk->fmt.i_cat != VIDEO_ES )
904             p_block->i_pts = p_block->i_dts;
905         else
906             p_block->i_pts = VLC_TS_INVALID;
907
908         if ( !b_data_sent )
909         {
910             es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
911             b_data_sent = true;
912         }
913         es_out_Send( p_demux->out, tk->p_es, p_block );
914
915         /* Next sample */
916         MP4_TrackNextSample( p_demux, tk, i_nb_samples );
917     }
918
919 end:
920     if ( b_data_sent )
921     {
922         p_sys->i_pcr = INT64_MAX;
923         for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
924         {
925             mp4_track_t *tk = &p_sys->track[i_track];
926             if ( !tk->b_ok || !tk->b_selected ) continue;
927             p_sys->i_pcr = __MIN( MP4_TrackGetDTS( p_demux, tk ), p_sys->i_pcr );
928             p_sys->i_time = p_sys->i_pcr * p_sys->i_timescale / CLOCK_FREQ;
929         }
930     }
931
932     return b_data_sent;
933 }
934
935 static void MP4_UpdateSeekpoint( demux_t *p_demux )
936 {
937     demux_sys_t *p_sys = p_demux->p_sys;
938     int64_t i_time;
939     int i;
940     if( !p_sys->p_title )
941         return;
942     i_time = MP4_GetMoviePTS( p_sys );
943     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
944     {
945         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
946             break;
947     }
948     i--;
949
950     if( i != p_demux->info.i_seekpoint && i >= 0 )
951     {
952         p_demux->info.i_seekpoint = i;
953         p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
954     }
955 }
956 /*****************************************************************************
957  * Seek: Go to i_date
958 ******************************************************************************/
959 static int Seek( demux_t *p_demux, mtime_t i_date )
960 {
961     demux_sys_t *p_sys = p_demux->p_sys;
962     unsigned int i_track;
963
964     /* First update global time */
965     p_sys->i_time = i_date * p_sys->i_timescale / CLOCK_FREQ;
966     p_sys->i_pcr  = VLC_TS_INVALID;
967
968     /* Now for each stream try to go to this time */
969     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
970     {
971         mp4_track_t *tk = &p_sys->track[i_track];
972         MP4_TrackSeek( p_demux, tk, i_date );
973     }
974     MP4_UpdateSeekpoint( p_demux );
975
976     es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
977
978     return VLC_SUCCESS;
979 }
980
981 static int MP4_frg_Seek( demux_t *p_demux, double f )
982 {
983     demux_sys_t *p_sys = p_demux->p_sys;
984
985     int64_t i64 = stream_Size( p_demux->s );
986     if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
987     {
988         return VLC_EGENERIC;
989     }
990     else
991     {
992         /* update global time */
993         p_sys->i_time = (uint64_t)(f * (double)p_sys->moovfragment.i_duration);
994         p_sys->i_pcr  = MP4_GetMoviePTS( p_sys );
995
996         for( unsigned i_track = 0; i_track < p_sys->i_tracks; i_track++ )
997         {
998             mp4_track_t *tk = &p_sys->track[i_track];
999
1000             /* We don't want the current chunk to be flushed */
1001             tk->cchunk->i_sample = tk->cchunk->i_sample_count;
1002
1003             /* reset/update some values */
1004             tk->i_sample = tk->i_sample_first = 0;
1005             tk->i_first_dts = p_sys->i_time;
1006
1007             /* We want to discard the current chunk and get the next one at once */
1008             tk->b_has_non_empty_cchunk = false;
1009         }
1010         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->i_pcr );
1011         return VLC_SUCCESS;
1012     }
1013 }
1014
1015 /*****************************************************************************
1016  * Control:
1017  *****************************************************************************/
1018 static int Control( demux_t *p_demux, int i_query, va_list args )
1019 {
1020     demux_sys_t *p_sys = p_demux->p_sys;
1021
1022     double f, *pf;
1023     int64_t i64, *pi64;
1024
1025     switch( i_query )
1026     {
1027         case DEMUX_GET_POSITION:
1028             pf = (double*)va_arg( args, double * );
1029             if( p_sys->i_overall_duration > 0 )
1030             {
1031                 *pf = (double)p_sys->i_time / (double)p_sys->i_overall_duration;
1032             }
1033             else
1034             {
1035                 *pf = 0.0;
1036             }
1037             return VLC_SUCCESS;
1038
1039         case DEMUX_SET_POSITION:
1040             f = (double)va_arg( args, double );
1041             if( p_sys->b_fragmented )
1042             {
1043                 return MP4_frg_Seek( p_demux, f );
1044             }
1045             else if( p_sys->i_timescale > 0 )
1046             {
1047                 i64 = (int64_t)( f * CLOCK_FREQ *
1048                                  (double)p_sys->i_overall_duration /
1049                                  (double)p_sys->i_timescale );
1050                 return Seek( p_demux, i64 );
1051             }
1052             else return VLC_SUCCESS;
1053
1054         case DEMUX_GET_TIME:
1055             pi64 = (int64_t*)va_arg( args, int64_t * );
1056             if( p_sys->i_timescale > 0 )
1057             {
1058                 *pi64 = CLOCK_FREQ *
1059                         (mtime_t)p_sys->i_time /
1060                         (mtime_t)p_sys->i_timescale;
1061             }
1062             else *pi64 = 0;
1063             return VLC_SUCCESS;
1064
1065         case DEMUX_SET_TIME:
1066             i64 = (int64_t)va_arg( args, int64_t );
1067             return Seek( p_demux, i64 );
1068
1069         case DEMUX_GET_LENGTH:
1070             pi64 = (int64_t*)va_arg( args, int64_t * );
1071             if( p_sys->i_timescale > 0 )
1072             {
1073                 *pi64 = CLOCK_FREQ *
1074                         (mtime_t)p_sys->i_overall_duration /
1075                         (mtime_t)p_sys->i_timescale;
1076             }
1077             else *pi64 = 0;
1078             return VLC_SUCCESS;
1079
1080         case DEMUX_GET_FPS:
1081             pf = (double*)va_arg( args, double* );
1082             *pf = p_sys->f_fps;
1083             return VLC_SUCCESS;
1084
1085         case DEMUX_GET_ATTACHMENTS:
1086         {
1087             input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1088             int *pi_int = va_arg( args, int * );
1089
1090             MP4_Box_t  *p_covr = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst/covr" );
1091             if ( !p_covr ) return VLC_EGENERIC;
1092             MP4_Box_t  *p_box;
1093             int i_count = 0;
1094
1095             for( p_box = p_covr->p_first; p_box != NULL; p_box = p_box->p_next )
1096             {
1097                 if ( p_box->i_type == ATOM_data && p_box->data.p_data->i_blob >= 16 )
1098                     i_count++;
1099             }
1100
1101             if ( i_count == 0 )
1102                 return VLC_EGENERIC;
1103
1104             *ppp_attach = (input_attachment_t**)
1105                     malloc( sizeof(input_attachment_t*) * i_count );
1106             if( !(*ppp_attach) ) return VLC_ENOMEM;
1107             char *psz_mime;
1108             char *psz_filename;
1109             i_count = 0;
1110             for( p_box = p_covr->p_first; p_box != NULL; p_box = p_box->p_next )
1111             {
1112                 if ( p_box->i_type != ATOM_data || p_box->data.p_data->i_blob < 16 )
1113                     continue;
1114
1115                 if ( !memcmp( p_box->data.p_data->p_blob,
1116                               "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8 ) )
1117                 {
1118                     psz_mime = strdup( "image/png" );
1119                 }
1120                 else if ( !memcmp( p_box->data.p_data->p_blob, "\xFF\xD8", 2 ) )
1121                 {
1122                     psz_mime = strdup( "image/jpeg" );
1123                 }
1124                 else
1125                 {
1126                     continue;
1127                 }
1128
1129                 if ( asprintf( &psz_filename, "picture%u", i_count ) >= 0 )
1130                 {
1131                     (*ppp_attach)[i_count++] =
1132                         vlc_input_attachment_New( psz_filename, psz_mime, NULL,
1133                             p_box->data.p_data->p_blob, p_box->data.p_data->i_blob );
1134                     free( psz_filename );
1135                 }
1136
1137                 free( psz_mime );
1138             }
1139
1140             if ( i_count == 0 )
1141             {
1142                 free( *ppp_attach );
1143                 return VLC_EGENERIC;
1144             }
1145
1146             *pi_int = i_count;
1147
1148             return VLC_SUCCESS;
1149         }
1150
1151         case DEMUX_GET_META:
1152         {
1153             vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
1154             MP4_Box_t  *p_0xa9xxx;
1155
1156             MP4_Box_t *p_covr = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst/covr/data[0]" );
1157             if ( p_covr )
1158                 vlc_meta_SetArtURL( p_meta, "attachment://picture0" );
1159
1160             MP4_Box_t  *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
1161             if( p_udta == NULL )
1162             {
1163                 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
1164                 if( p_udta == NULL && p_covr == NULL )
1165                     return VLC_EGENERIC;
1166                 else
1167                     return VLC_SUCCESS;
1168             }
1169
1170             for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
1171                  p_0xa9xxx = p_0xa9xxx->p_next )
1172             {
1173
1174                 if( !p_0xa9xxx || !BOXDATA(p_0xa9xxx) )
1175                     continue;
1176
1177                 /* FIXME FIXME: should convert from whatever the character
1178                  * encoding of MP4 meta data is to UTF-8. */
1179 #define SET(fct) do { char *psz_utf = strdup( BOXDATA(p_0xa9xxx)->psz_text ? BOXDATA(p_0xa9xxx)->psz_text : "" ); \
1180     if( psz_utf ) { EnsureUTF8( psz_utf );  \
1181                     fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
1182
1183                 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
1184                 switch( p_0xa9xxx->i_type )
1185                 {
1186                 case ATOM_0xa9nam: /* Full name */
1187                     SET( vlc_meta_SetTitle );
1188                     break;
1189                 case ATOM_0xa9aut:
1190                     SET( vlc_meta_SetArtist );
1191                     break;
1192                 case ATOM_0xa9ART:
1193                     SET( vlc_meta_SetArtist );
1194                     break;
1195                 case ATOM_0xa9cpy:
1196                     SET( vlc_meta_SetCopyright );
1197                     break;
1198                 case ATOM_0xa9day: /* Creation Date */
1199                     SET( vlc_meta_SetDate );
1200                     break;
1201                 case ATOM_0xa9des: /* Description */
1202                     SET( vlc_meta_SetDescription );
1203                     break;
1204                 case ATOM_0xa9gen: /* Genre */
1205                     SET( vlc_meta_SetGenre );
1206                     break;
1207
1208                 case ATOM_gnre:
1209                     if( p_0xa9xxx->data.p_gnre->i_genre <= NUM_GENRES )
1210                         vlc_meta_SetGenre( p_meta, ppsz_genres[p_0xa9xxx->data.p_gnre->i_genre - 1] );
1211                     break;
1212
1213                 case ATOM_0xa9alb: /* Album */
1214                     SET( vlc_meta_SetAlbum );
1215                     break;
1216
1217                 case ATOM_0xa9trk: /* Track */
1218                     SET( vlc_meta_SetTrackNum );
1219                     break;
1220                 case ATOM_trkn:
1221                 {
1222                     char psz_trck[11];
1223                     snprintf( psz_trck, sizeof( psz_trck ), "%i",
1224                               p_0xa9xxx->data.p_trkn->i_track_number );
1225                     vlc_meta_SetTrackNum( p_meta, psz_trck );
1226                     if( p_0xa9xxx->data.p_trkn->i_track_total > 0 )
1227                     {
1228                         snprintf( psz_trck, sizeof( psz_trck ), "%i",
1229                                   p_0xa9xxx->data.p_trkn->i_track_total );
1230                         vlc_meta_Set( p_meta, vlc_meta_TrackTotal, psz_trck );
1231                     }
1232                     break;
1233                 }
1234                 case ATOM_0xa9cmt: /* Commment */
1235                     SET( vlc_meta_SetDescription );
1236                     break;
1237
1238                 case ATOM_0xa9url: /* URL */
1239                     SET( vlc_meta_SetURL );
1240                     break;
1241
1242                 case ATOM_0xa9too: /* Encoder Tool */
1243                 case ATOM_0xa9enc: /* Encoded By */
1244                     SET( vlc_meta_SetEncodedBy );
1245                     break;
1246
1247                 case ATOM_0xa9pub:
1248                     SET( vlc_meta_SetPublisher );
1249                     break;
1250
1251                 case ATOM_0xa9dir:
1252                     SET( vlc_meta_SetDirector );
1253                     break;
1254
1255                 default:
1256                     break;
1257                 }
1258 #undef SET
1259                 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
1260                 {
1261                     { ATOM_0xa9wrt, N_("Writer") },
1262                     { ATOM_0xa9com, N_("Composer") },
1263                     { ATOM_0xa9prd, N_("Producer") },
1264                     { ATOM_0xa9inf, N_("Information") },
1265                     { ATOM_0xa9dis, N_("Disclaimer") },
1266                     { ATOM_0xa9req, N_("Requirements") },
1267                     { ATOM_0xa9fmt, N_("Original Format") },
1268                     { ATOM_0xa9dsa, N_("Display Source As") },
1269                     { ATOM_0xa9hst, N_("Host Computer") },
1270                     { ATOM_0xa9prf, N_("Performers") },
1271                     { ATOM_0xa9ope, N_("Original Performer") },
1272                     { ATOM_0xa9src, N_("Providers Source Content") },
1273                     { ATOM_0xa9wrn, N_("Warning") },
1274                     { ATOM_0xa9swr, N_("Software") },
1275                     { ATOM_0xa9lyr, N_("Lyrics") },
1276                     { ATOM_0xa9mak, N_("Record Company") },
1277                     { ATOM_0xa9mod, N_("Model") },
1278                     { ATOM_0xa9PRD, N_("Product") },
1279                     { ATOM_0xa9grp, N_("Grouping") },
1280                     { ATOM_0xa9gen, N_("Genre") },
1281                     { ATOM_0xa9st3, N_("Sub-Title") },
1282                     { ATOM_0xa9arg, N_("Arranger") },
1283                     { ATOM_0xa9ard, N_("Art Director") },
1284                     { ATOM_0xa9cak, N_("Copyright Acknowledgement") },
1285                     { ATOM_0xa9con, N_("Conductor") },
1286                     { ATOM_0xa9des, N_("Song Description") },
1287                     { ATOM_0xa9lnt, N_("Liner Notes") },
1288                     { ATOM_0xa9phg, N_("Phonogram Rights") },
1289                     { ATOM_0xa9pub, N_("Publisher") },
1290                     { ATOM_0xa9sne, N_("Sound Engineer") },
1291                     { ATOM_0xa9sol, N_("Soloist") },
1292                     { ATOM_0xa9thx, N_("Thanks") },
1293                     { ATOM_0xa9xpd, N_("Executive Producer") },
1294                     { 0, "" },
1295                 };
1296                 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
1297                 {
1298                     if( p_0xa9xxx->i_type == xa9typetoextrameta[i].xa9_type )
1299                     {
1300                         char *psz_utf = strdup( BOXDATA(p_0xa9xxx)->psz_text ? BOXDATA(p_0xa9xxx)->psz_text : "" );
1301                         if( psz_utf )
1302                         {
1303                              EnsureUTF8( psz_utf );
1304                              vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
1305                              free( psz_utf );
1306                         }
1307                         break;
1308                     }
1309                 }
1310             }
1311             return VLC_SUCCESS;
1312         }
1313
1314         case DEMUX_GET_TITLE_INFO:
1315         {
1316             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1317             int *pi_int    = (int*)va_arg( args, int* );
1318             int *pi_title_offset = (int*)va_arg( args, int* );
1319             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
1320
1321             if( !p_sys->p_title )
1322                 return VLC_EGENERIC;
1323
1324             *pi_int = 1;
1325             *ppp_title = malloc( sizeof( input_title_t*) );
1326             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
1327             *pi_title_offset = 0;
1328             *pi_seekpoint_offset = 0;
1329             return VLC_SUCCESS;
1330         }
1331         case DEMUX_SET_TITLE:
1332         {
1333             const int i_title = (int)va_arg( args, int );
1334             if( !p_sys->p_title || i_title != 0 )
1335                 return VLC_EGENERIC;
1336             return VLC_SUCCESS;
1337         }
1338         case DEMUX_SET_SEEKPOINT:
1339         {
1340             const int i_seekpoint = (int)va_arg( args, int );
1341             if( !p_sys->p_title )
1342                 return VLC_EGENERIC;
1343             return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
1344         }
1345
1346         case DEMUX_SET_NEXT_DEMUX_TIME:
1347         case DEMUX_SET_GROUP:
1348         case DEMUX_HAS_UNSUPPORTED_META:
1349         case DEMUX_GET_PTS_DELAY:
1350         case DEMUX_CAN_RECORD:
1351             return VLC_EGENERIC;
1352
1353         default:
1354             msg_Warn( p_demux, "control query %u unimplemented", i_query );
1355             return VLC_EGENERIC;
1356     }
1357 }
1358
1359 /*****************************************************************************
1360  * Close: frees unused data
1361  *****************************************************************************/
1362 static void Close ( vlc_object_t * p_this )
1363 {
1364     demux_t *  p_demux = (demux_t *)p_this;
1365     demux_sys_t *p_sys = p_demux->p_sys;
1366     unsigned int i_track;
1367
1368     msg_Dbg( p_demux, "freeing all memory" );
1369
1370     MP4_BoxFree( p_demux->s, p_sys->p_root );
1371     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1372     {
1373         MP4_TrackDestroy(  &p_sys->track[i_track] );
1374     }
1375     FREENULL( p_sys->track );
1376
1377     if( p_sys->p_title )
1378         vlc_input_title_Delete( p_sys->p_title );
1379
1380     while( p_sys->moovfragment.p_next )
1381     {
1382         mp4_fragment_t *p_fragment = p_sys->moovfragment.p_next->p_next;
1383         free( p_sys->moovfragment.p_next );
1384         p_sys->moovfragment.p_next = p_fragment;
1385     }
1386
1387     free( p_sys );
1388 }
1389
1390
1391
1392 /****************************************************************************
1393  * Local functions, specific to vlc
1394  ****************************************************************************/
1395 /* Chapters */
1396 static void LoadChapterGpac( demux_t  *p_demux, MP4_Box_t *p_chpl )
1397 {
1398     demux_sys_t *p_sys = p_demux->p_sys;
1399     int i;
1400
1401     p_sys->p_title = vlc_input_title_New();
1402     for( i = 0; i < BOXDATA(p_chpl)->i_chapter; i++ )
1403     {
1404         seekpoint_t *s = vlc_seekpoint_New();
1405
1406         s->psz_name = strdup( BOXDATA(p_chpl)->chapter[i].psz_name );
1407         EnsureUTF8( s->psz_name );
1408         s->i_time_offset = BOXDATA(p_chpl)->chapter[i].i_start / 10;
1409         TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1410     }
1411 }
1412 static void LoadChapterApple( demux_t  *p_demux, mp4_track_t *tk )
1413 {
1414     demux_sys_t *p_sys = p_demux->p_sys;
1415
1416     for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1417     {
1418         const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1419         const int64_t i_pts_delta = MP4_TrackGetPTSDelta( p_demux, tk );
1420         uint32_t i_nb_samples = 0;
1421         const uint32_t i_size = MP4_TrackSampleSize( tk, &i_nb_samples );
1422
1423         if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1424         {
1425             char p_buffer[256];
1426             const uint32_t i_read = stream_ReadU32( p_demux->s, p_buffer,
1427                                                     __MIN( sizeof(p_buffer), i_size ) );
1428             const uint32_t i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1429
1430             if( i_len > 0 )
1431             {
1432                 seekpoint_t *s = vlc_seekpoint_New();
1433
1434                 s->psz_name = strndup( &p_buffer[2], i_len );
1435                 EnsureUTF8( s->psz_name );
1436
1437                 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1438
1439                 if( !p_sys->p_title )
1440                     p_sys->p_title = vlc_input_title_New();
1441                 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1442             }
1443         }
1444         if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1445                               tk->chunk[tk->i_chunk].i_sample_count )
1446             tk->i_chunk++;
1447     }
1448 }
1449 static void LoadChapter( demux_t  *p_demux )
1450 {
1451     demux_sys_t *p_sys = p_demux->p_sys;
1452     MP4_Box_t *p_chpl;
1453
1454     if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && BOXDATA(p_chpl)->i_chapter > 0 )
1455     {
1456         LoadChapterGpac( p_demux, p_chpl );
1457     }
1458     else if( p_sys->p_tref_chap )
1459     {
1460         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1461         unsigned int i, j;
1462
1463         /* Load the first subtitle track like quicktime */
1464         for( i = 0; i < p_chap->i_entry_count; i++ )
1465         {
1466             for( j = 0; j < p_sys->i_tracks; j++ )
1467             {
1468                 mp4_track_t *tk = &p_sys->track[j];
1469                 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1470                     tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_SUBT )
1471                     break;
1472             }
1473             if( j < p_sys->i_tracks )
1474             {
1475                 LoadChapterApple( p_demux, &p_sys->track[j] );
1476                 break;
1477             }
1478         }
1479     }
1480
1481     /* Add duration if titles are enabled */
1482     if( p_sys->p_title )
1483     {
1484         p_sys->p_title->i_length = CLOCK_FREQ *
1485                        (uint64_t)p_sys->i_overall_duration / (uint64_t)p_sys->i_timescale;
1486     }
1487 }
1488
1489 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1490 static int TrackCreateChunksIndex( demux_t *p_demux,
1491                                    mp4_track_t *p_demux_track )
1492 {
1493     demux_sys_t *p_sys = p_demux->p_sys;
1494
1495     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1496     MP4_Box_t *p_stsc;
1497
1498     unsigned int i_chunk;
1499     unsigned int i_index, i_last;
1500
1501     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1502           !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1503         ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1504     {
1505         return( VLC_EGENERIC );
1506     }
1507
1508     p_demux_track->i_chunk_count = BOXDATA(p_co64)->i_entry_count;
1509     if( !p_demux_track->i_chunk_count )
1510     {
1511         msg_Warn( p_demux, "no chunk defined" );
1512     }
1513     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1514                                    sizeof( mp4_chunk_t ) );
1515     if( p_demux_track->chunk == NULL )
1516     {
1517         return VLC_ENOMEM;
1518     }
1519
1520     /* first we read chunk offset */
1521     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1522     {
1523         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1524
1525         ck->i_offset = BOXDATA(p_co64)->i_chunk_offset[i_chunk];
1526
1527         ck->i_first_dts = 0;
1528         ck->p_sample_count_dts = NULL;
1529         ck->p_sample_delta_dts = NULL;
1530         ck->p_sample_count_pts = NULL;
1531         ck->p_sample_offset_pts = NULL;
1532     }
1533
1534     /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1535         to be used for the sample XXX begin to 1
1536         We construct it begining at the end */
1537     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1538     i_index = BOXDATA(p_stsc)->i_entry_count;
1539
1540     while( i_index-- > 0 )
1541     {
1542         for( i_chunk = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
1543              i_chunk < i_last; i_chunk++ )
1544         {
1545             if( i_chunk >= p_demux_track->i_chunk_count )
1546             {
1547                 msg_Warn( p_demux, "corrupted chunk table" );
1548                 return VLC_EGENERIC;
1549             }
1550
1551             p_demux_track->chunk[i_chunk].i_sample_description_index =
1552                     BOXDATA(p_stsc)->i_sample_description_index[i_index];
1553             p_demux_track->chunk[i_chunk].i_sample_count =
1554                     BOXDATA(p_stsc)->i_samples_per_chunk[i_index];
1555         }
1556         i_last = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
1557     }
1558
1559     p_demux_track->chunk[0].i_sample_first = 0;
1560     for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1561     {
1562         p_demux_track->chunk[i_chunk].i_sample_first =
1563             p_demux_track->chunk[i_chunk-1].i_sample_first +
1564                 p_demux_track->chunk[i_chunk-1].i_sample_count;
1565     }
1566
1567     msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1568              p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1569
1570     if ( p_demux_track->i_chunk_count && (
1571              p_sys->moovfragment.i_chunk_range_min_offset == 0 ||
1572              p_sys->moovfragment.i_chunk_range_min_offset > p_demux_track->chunk[0].i_offset
1573              ) )
1574         p_sys->moovfragment.i_chunk_range_min_offset = p_demux_track->chunk[0].i_offset;
1575
1576     return VLC_SUCCESS;
1577 }
1578
1579 static int xTTS_CountEntries( demux_t *p_demux, uint32_t *pi_entry /* out */,
1580                               const uint32_t i_index,
1581                               uint32_t i_index_samples_left,
1582                               uint32_t i_sample_count,
1583                               const uint32_t *pi_index_sample_count,
1584                               const uint32_t i_table_count )
1585 {
1586     uint32_t i_array_offset;
1587     while( i_sample_count > 0 )
1588     {
1589         if ( likely((UINT32_MAX - i_index) >= *pi_entry) )
1590             i_array_offset = i_index + *pi_entry;
1591         else
1592             return VLC_EGENERIC;
1593
1594         if ( i_array_offset >= i_table_count )
1595         {
1596             msg_Err( p_demux, "invalid index counting total samples %u %u", i_array_offset,  i_table_count );
1597             return VLC_EGENERIC;
1598         }
1599
1600         if ( i_index_samples_left )
1601         {
1602             if ( i_index_samples_left > i_sample_count )
1603             {
1604                 i_index_samples_left -= i_sample_count;
1605                 i_sample_count = 0;
1606                 *pi_entry +=1; /* No samples left, go copy */
1607                 break;
1608             }
1609             else
1610             {
1611                 i_sample_count -= i_index_samples_left;
1612                 i_index_samples_left = 0;
1613                 *pi_entry += 1;
1614                 continue;
1615             }
1616         }
1617         else
1618         {
1619             i_sample_count -= __MIN( i_sample_count, pi_index_sample_count[i_array_offset] );
1620             *pi_entry += 1;
1621         }
1622     }
1623
1624     return VLC_SUCCESS;
1625 }
1626
1627 static int TrackCreateSamplesIndex( demux_t *p_demux,
1628                                     mp4_track_t *p_demux_track )
1629 {
1630     demux_sys_t *p_sys = p_demux->p_sys;
1631
1632     MP4_Box_t *p_box;
1633     MP4_Box_data_stsz_t *stsz;
1634     /* TODO use also stss and stsh table for seeking */
1635     /* FIXME use edit table */
1636
1637     /* Find stsz
1638      *  Gives the sample size for each samples. There is also a stz2 table
1639      *  (compressed form) that we need to implement TODO */
1640     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1641     if( !p_box )
1642     {
1643         /* FIXME and stz2 */
1644         msg_Warn( p_demux, "cannot find STSZ box" );
1645         return VLC_EGENERIC;
1646     }
1647     stsz = p_box->data.p_stsz;
1648
1649     /* Use stsz table to create a sample number -> sample size table */
1650     p_demux_track->i_sample_count = stsz->i_sample_count;
1651     if( stsz->i_sample_size )
1652     {
1653         /* 1: all sample have the same size, so no need to construct a table */
1654         p_demux_track->i_sample_size = stsz->i_sample_size;
1655         p_demux_track->p_sample_size = NULL;
1656     }
1657     else
1658     {
1659         /* 2: each sample can have a different size */
1660         p_demux_track->i_sample_size = 0;
1661         p_demux_track->p_sample_size =
1662             calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1663         if( p_demux_track->p_sample_size == NULL )
1664             return VLC_ENOMEM;
1665
1666         for( uint32_t i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1667         {
1668             p_demux_track->p_sample_size[i_sample] =
1669                     stsz->i_entry_size[i_sample];
1670         }
1671     }
1672
1673     if ( p_demux_track->i_chunk_count )
1674     {
1675         mp4_chunk_t *lastchunk = &p_demux_track->chunk[p_demux_track->i_chunk_count - 1];
1676         uint64_t i_total_size = lastchunk->i_offset;
1677         for( uint32_t i=0; i<lastchunk->i_sample_count; i++)
1678         {
1679             if( p_demux_track->i_sample_size == 0 )
1680                 i_total_size += stsz->i_entry_size[i];
1681             else
1682                 i_total_size += p_demux_track->i_sample_size;
1683         }
1684
1685         if ( i_total_size > p_sys->moovfragment.i_chunk_range_max_offset )
1686             p_sys->moovfragment.i_chunk_range_max_offset = i_total_size;
1687     }
1688
1689     /* Use stts table to create a sample number -> dts table.
1690      * XXX: if we don't want to waste too much memory, we can't expand
1691      *  the box! so each chunk will contain an "extract" of this table
1692      *  for fast research (problem with raw stream where a sample is sometime
1693      *  just channels*bits_per_sample/8 */
1694
1695      /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
1696       *        differing in 2/2 fields and 1 signedness */
1697
1698     mtime_t i_next_dts = 0;
1699     /* Find stts
1700      *  Gives mapping between sample and decoding time
1701      */
1702     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1703     if( !p_box )
1704     {
1705         msg_Warn( p_demux, "cannot find STTS box" );
1706         return VLC_EGENERIC;
1707     }
1708     else
1709     {
1710         MP4_Box_data_stts_t *stts = p_box->data.p_stts;
1711
1712         msg_Warn( p_demux, "STTS table of %"PRIu32" entries", stts->i_entry_count );
1713
1714         /* Create sample -> dts table per chunk */
1715         uint32_t i_index = 0;
1716         uint32_t i_current_index_samples_left = 0;
1717
1718         for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1719         {
1720             mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1721             uint32_t i_entry, i_sample_count;
1722
1723             /* save first dts */
1724             ck->i_first_dts = i_next_dts;
1725             ck->i_last_dts  = i_next_dts;
1726
1727             /* count how many entries are needed for this chunk
1728              * for p_sample_delta_dts and p_sample_count_dts */
1729             i_entry = 0;
1730
1731             int i_ret = xTTS_CountEntries( p_demux, &i_entry, i_index,
1732                                            i_current_index_samples_left,
1733                                            ck->i_sample_count,
1734                                            stts->pi_sample_count,
1735                                            stts->i_entry_count );
1736             if ( i_ret != VLC_SUCCESS )
1737                 return i_ret;
1738
1739             /* allocate them */
1740             ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1741             ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1742             if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1743             {
1744                 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, i_entry );
1745                 return VLC_ENOMEM;
1746             }
1747
1748             /* now copy */
1749             i_sample_count = ck->i_sample_count;
1750
1751             for( uint32_t i = 0; i < i_entry; i++ )
1752             {
1753                 if ( i_current_index_samples_left )
1754                 {
1755                     if ( i_current_index_samples_left > i_sample_count )
1756                     {
1757                         if ( i_sample_count ) ck->i_last_dts = i_next_dts;
1758                         ck->p_sample_count_dts[i] = i_sample_count;
1759                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
1760                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
1761                         i_current_index_samples_left -= i_sample_count;
1762                         i_sample_count = 0;
1763                         assert( i == i_entry - 1 );
1764                         break;
1765                     }
1766                     else
1767                     {
1768                         if ( i_current_index_samples_left ) ck->i_last_dts = i_next_dts;
1769                         ck->p_sample_count_dts[i] = i_current_index_samples_left;
1770                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
1771                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
1772                         i_sample_count -= i_current_index_samples_left;
1773                         i_current_index_samples_left = 0;
1774                         i_index++;
1775                     }
1776                 }
1777                 else
1778                 {
1779                     if ( stts->pi_sample_count[i_index] > i_sample_count )
1780                     {
1781                         if ( i_sample_count ) ck->i_last_dts = i_next_dts;
1782                         ck->p_sample_count_dts[i] = i_sample_count;
1783                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
1784                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
1785                         i_current_index_samples_left = stts->pi_sample_count[i_index] - i_sample_count;
1786                         i_sample_count = 0;
1787                         assert( i == i_entry - 1 );
1788                         // keep building from same index
1789                     }
1790                     else
1791                     {
1792                         if ( stts->pi_sample_count[i_index] ) ck->i_last_dts = i_next_dts;
1793                         ck->p_sample_count_dts[i] = stts->pi_sample_count[i_index];
1794                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
1795                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
1796                         i_sample_count -= stts->pi_sample_count[i_index];
1797                         i_index++;
1798                     }
1799                 }
1800
1801             }
1802         }
1803     }
1804
1805
1806     /* Find ctts
1807      *  Gives the delta between decoding time (dts) and composition table (pts)
1808      */
1809     p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1810     if( p_box )
1811     {
1812         MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1813
1814         msg_Warn( p_demux, "CTTS table of %"PRIu32" entries", ctts->i_entry_count );
1815
1816         /* Create pts-dts table per chunk */
1817         uint32_t i_index = 0;
1818         uint32_t i_current_index_samples_left = 0;
1819
1820         for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1821         {
1822             mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1823             uint32_t i_entry, i_sample_count;
1824
1825             /* count how many entries are needed for this chunk
1826              * for p_sample_offset_pts and p_sample_count_pts */
1827             i_entry = 0;
1828             int i_ret = xTTS_CountEntries( p_demux, &i_entry, i_index,
1829                                            i_current_index_samples_left,
1830                                            ck->i_sample_count,
1831                                            ctts->pi_sample_count,
1832                                            ctts->i_entry_count );
1833             if ( i_ret != VLC_SUCCESS )
1834                 return i_ret;
1835
1836             /* allocate them */
1837             ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1838             ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1839             if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1840             {
1841                 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, i_entry );
1842                 return VLC_ENOMEM;
1843             }
1844
1845             /* now copy */
1846             i_sample_count = ck->i_sample_count;
1847
1848             for( uint32_t i = 0; i < i_entry; i++ )
1849             {
1850                 if ( i_current_index_samples_left )
1851                 {
1852                     if ( i_current_index_samples_left > i_sample_count )
1853                     {
1854                         ck->p_sample_count_pts[i] = i_sample_count;
1855                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
1856                         i_current_index_samples_left -= i_sample_count;
1857                         i_sample_count = 0;
1858                         assert( i == i_entry - 1 );
1859                         break;
1860                     }
1861                     else
1862                     {
1863                         ck->p_sample_count_pts[i] = i_current_index_samples_left;
1864                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
1865                         i_sample_count -= i_current_index_samples_left;
1866                         i_current_index_samples_left = 0;
1867                         i_index++;
1868                     }
1869                 }
1870                 else
1871                 {
1872                     if ( ctts->pi_sample_count[i_index] > i_sample_count )
1873                     {
1874                         ck->p_sample_count_pts[i] = i_sample_count;
1875                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
1876                         i_current_index_samples_left = ctts->pi_sample_count[i_index] - i_sample_count;
1877                         i_sample_count = 0;
1878                         assert( i == i_entry - 1 );
1879                         // keep building from same index
1880                     }
1881                     else
1882                     {
1883                         ck->p_sample_count_pts[i] = ctts->pi_sample_count[i_index];
1884                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
1885                         i_sample_count -= ctts->pi_sample_count[i_index];
1886                         i_index++;
1887                     }
1888                 }
1889
1890
1891             }
1892         }
1893     }
1894
1895     msg_Dbg( p_demux, "track[Id 0x%x] read %"PRIu32" samples length:%"PRId64"s",
1896              p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1897              i_next_dts / p_demux_track->i_timescale );
1898
1899     return VLC_SUCCESS;
1900 }
1901
1902
1903 /**
1904  * It computes the sample rate for a video track using the given sample
1905  * description index
1906  */
1907 static void TrackGetESSampleRate( demux_t *p_demux,
1908                                   unsigned *pi_num, unsigned *pi_den,
1909                                   const mp4_track_t *p_track,
1910                                   unsigned i_sd_index,
1911                                   unsigned i_chunk )
1912 {
1913     *pi_num = 0;
1914     *pi_den = 0;
1915
1916     MP4_Box_t *p_trak = MP4_GetTrakByTrackID( MP4_BoxGet( p_demux->p_sys->p_root, "/moov" ),
1917                                               p_track->i_track_ID );
1918     MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
1919     if ( p_mdhd )
1920     {
1921         vlc_ureduce( pi_num, pi_den,
1922                      BOXDATA(p_mdhd)->i_timescale * p_track->i_sample_count,
1923                      BOXDATA(p_mdhd)->i_duration,
1924                      UINT16_MAX);
1925         return;
1926     }
1927
1928     if( p_track->i_chunk_count <= 0 )
1929         return;
1930
1931     /* */
1932     const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
1933     while( p_chunk > &p_track->chunk[0] &&
1934            p_chunk[-1].i_sample_description_index == i_sd_index )
1935     {
1936         p_chunk--;
1937     }
1938
1939     uint64_t i_sample = 0;
1940     uint64_t i_first_dts = p_chunk->i_first_dts;
1941     uint64_t i_last_dts;
1942     do
1943     {
1944         i_sample += p_chunk->i_sample_count;
1945         i_last_dts = p_chunk->i_last_dts;
1946         p_chunk++;
1947     }
1948     while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
1949            p_chunk->i_sample_description_index == i_sd_index );
1950
1951     if( i_sample > 1 && i_first_dts < i_last_dts )
1952         vlc_ureduce( pi_num, pi_den,
1953                      ( i_sample - 1) *  p_track->i_timescale,
1954                      i_last_dts - i_first_dts,
1955                      UINT16_MAX);
1956 }
1957
1958 /*
1959  * TrackCreateES:
1960  * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1961  */
1962 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1963                           unsigned int i_chunk, es_out_id_t **pp_es )
1964 {
1965     demux_sys_t *p_sys = p_demux->p_sys;
1966     unsigned int i_sample_description_index;
1967
1968     if( p_sys->b_fragmented )
1969         i_sample_description_index = 1; /* XXX */
1970     else
1971         i_sample_description_index =
1972                 p_track->chunk[i_chunk].i_sample_description_index;
1973
1974     MP4_Box_t   *p_sample;
1975     MP4_Box_t   *p_esds;
1976     MP4_Box_t   *p_frma;
1977     MP4_Box_t   *p_enda;
1978     MP4_Box_t   *p_pasp;
1979
1980     if( pp_es )
1981         *pp_es = NULL;
1982
1983     if( !i_sample_description_index )
1984     {
1985         msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1986                   p_track->i_track_ID );
1987         return VLC_EGENERIC;
1988     }
1989
1990     p_sample = MP4_BoxGet(  p_track->p_stsd, "[%d]",
1991                             i_sample_description_index - 1 );
1992
1993     if( !p_sample ||
1994         ( !p_sample->data.p_payload && p_track->fmt.i_cat != SPU_ES ) )
1995     {
1996         msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1997                   p_track->i_track_ID );
1998         return VLC_EGENERIC;
1999     }
2000
2001     p_track->p_sample = p_sample;
2002
2003     if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
2004     {
2005         msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
2006
2007         p_sample->i_type = p_frma->data.p_frma->i_type;
2008     }
2009
2010     p_enda = MP4_BoxGet( p_sample, "wave/enda" );
2011     if( !p_enda )
2012         p_enda = MP4_BoxGet( p_sample, "enda" );
2013
2014     p_pasp = MP4_BoxGet( p_sample, "pasp" );
2015
2016     /* */
2017     switch( p_track->fmt.i_cat )
2018     {
2019     case VIDEO_ES:
2020         p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
2021         p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
2022         p_track->fmt.video.i_bits_per_pixel =
2023             p_sample->data.p_sample_vide->i_depth;
2024
2025         /* fall on display size */
2026         if( p_track->fmt.video.i_width <= 0 )
2027             p_track->fmt.video.i_width = p_track->i_width;
2028         if( p_track->fmt.video.i_height <= 0 )
2029             p_track->fmt.video.i_height = p_track->i_height;
2030
2031         /* Find out apect ratio from display size */
2032         if( p_track->i_width > 0 && p_track->i_height > 0 &&
2033             /* Work-around buggy muxed files */
2034             p_sample->data.p_sample_vide->i_width != p_track->i_width )
2035         {
2036             p_track->fmt.video.i_sar_num = p_track->i_width  * p_track->fmt.video.i_height;
2037             p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
2038         }
2039         if( p_pasp && p_pasp->data.p_pasp->i_horizontal_spacing > 0 &&
2040                       p_pasp->data.p_pasp->i_vertical_spacing > 0 )
2041         {
2042             p_track->fmt.video.i_sar_num = p_pasp->data.p_pasp->i_horizontal_spacing;
2043             p_track->fmt.video.i_sar_den = p_pasp->data.p_pasp->i_vertical_spacing;
2044         }
2045
2046         /* Support for cropping (eg. in H263 files) */
2047         p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
2048         p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
2049
2050         /* Frame rate */
2051         TrackGetESSampleRate( p_demux,
2052                               &p_track->fmt.video.i_frame_rate,
2053                               &p_track->fmt.video.i_frame_rate_base,
2054                               p_track, i_sample_description_index, i_chunk );
2055
2056         p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
2057                                 (float)p_track->fmt.video.i_frame_rate_base;
2058
2059         /* Rotation */
2060         switch( (int)p_track->f_rotation ) {
2061             case 90:
2062                 p_track->fmt.video.orientation = ORIENT_ROTATED_90;
2063                 break;
2064             case 180:
2065                 p_track->fmt.video.orientation = ORIENT_ROTATED_180;
2066                 break;
2067             case 270:
2068                 p_track->fmt.video.orientation = ORIENT_ROTATED_270;
2069                 break;
2070         }
2071
2072         break;
2073
2074     case AUDIO_ES:
2075         p_track->fmt.audio.i_channels =
2076             p_sample->data.p_sample_soun->i_channelcount;
2077         p_track->fmt.audio.i_rate =
2078             p_sample->data.p_sample_soun->i_sampleratehi;
2079         p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
2080             p_sample->data.p_sample_soun->i_sampleratehi *
2081                 p_sample->data.p_sample_soun->i_samplesize;
2082         p_track->fmt.audio.i_bitspersample =
2083             p_sample->data.p_sample_soun->i_samplesize;
2084
2085         if( ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
2086         {
2087             MP4_Box_data_sample_soun_t *p_soun;
2088             p_soun = p_sample->data.p_sample_soun;
2089
2090             if( p_soun->i_qt_version == 0 )
2091             {
2092                 switch( p_sample->i_type )
2093                 {
2094                     case VLC_CODEC_ADPCM_IMA_QT:
2095                         p_soun->i_qt_version = 1;
2096                         p_soun->i_sample_per_packet = 64;
2097                         p_soun->i_bytes_per_packet  = 34;
2098                         p_soun->i_bytes_per_frame   = 34 * p_soun->i_channelcount;
2099                         p_soun->i_bytes_per_sample  = 2;
2100                         break;
2101                     case VLC_CODEC_MACE3:
2102                         p_soun->i_qt_version = 1;
2103                         p_soun->i_sample_per_packet = 6;
2104                         p_soun->i_bytes_per_packet  = 2;
2105                         p_soun->i_bytes_per_frame   = 2 * p_soun->i_channelcount;
2106                         p_soun->i_bytes_per_sample  = 2;
2107                         break;
2108                     case VLC_CODEC_MACE6:
2109                         p_soun->i_qt_version = 1;
2110                         p_soun->i_sample_per_packet = 12;
2111                         p_soun->i_bytes_per_packet  = 2;
2112                         p_soun->i_bytes_per_frame   = 2 * p_soun->i_channelcount;
2113                         p_soun->i_bytes_per_sample  = 2;
2114                         break;
2115                     case VLC_CODEC_ALAW:
2116                     case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
2117                         p_soun->i_samplesize = 8;
2118                         p_track->i_sample_size = p_soun->i_channelcount;
2119                         break;
2120                     case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
2121                     case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
2122                     case VLC_FOURCC( 't', 'w', 'o', 's' ):
2123                     case VLC_FOURCC( 's', 'o', 'w', 't' ):
2124                         /* What would be the fun if you could trust the .mov */
2125                         p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
2126                         break;
2127                     default:
2128                         break;
2129                 }
2130
2131             }
2132             else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
2133             {
2134                 p_soun->i_qt_version = 0;
2135             }
2136         }
2137         else if( p_sample->data.p_sample_soun->i_qt_version == 1 )
2138         {
2139             MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
2140
2141             switch( p_sample->i_type )
2142             {
2143                 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
2144                 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
2145                     {
2146                         if( p_track->i_sample_size > 1 )
2147                             p_soun->i_qt_version = 0;
2148                         break;
2149                     }
2150                 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
2151                 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
2152                 case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
2153                     p_soun->i_qt_version = 0;
2154                     break;
2155                 default:
2156                     break;
2157             }
2158         }
2159
2160         if( p_track->i_sample_size != 0 &&
2161                 p_sample->data.p_sample_soun->i_qt_version == 1 &&
2162                 p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
2163         {
2164             msg_Err( p_demux, "Invalid sample per packet value for qt_version 1. Broken muxer!" );
2165             p_sample->data.p_sample_soun->i_qt_version = 0;
2166         }
2167         break;
2168
2169     default:
2170         break;
2171     }
2172
2173
2174     /* It's a little ugly but .. there are special cases */
2175     switch( p_sample->i_type )
2176     {
2177         case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
2178         case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
2179         {
2180             p_track->fmt.i_codec = VLC_CODEC_MPGA;
2181             break;
2182         }
2183         case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
2184         {
2185             MP4_Box_t *p_dac3_box = MP4_BoxGet(  p_sample, "dac3", 0 );
2186
2187             p_track->fmt.i_codec = VLC_CODEC_A52;
2188             if( p_dac3_box )
2189             {
2190                 static const int pi_bitrate[] = {
2191                      32,  40,  48,  56,
2192                      64,  80,  96, 112,
2193                     128, 160, 192, 224,
2194                     256, 320, 384, 448,
2195                     512, 576, 640,
2196                 };
2197                 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
2198                 p_track->fmt.audio.i_channels = 0;
2199                 p_track->fmt.i_bitrate = 0;
2200                 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
2201                     p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
2202                 p_track->fmt.audio.i_bitspersample = 0;
2203             }
2204             break;
2205         }
2206         case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
2207         {
2208             p_track->fmt.i_codec = VLC_CODEC_EAC3;
2209             break;
2210         }
2211
2212         case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
2213         case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
2214         {
2215             MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
2216
2217             if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
2218                 p_track->fmt.i_codec = VLC_CODEC_U8;
2219             else
2220                 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
2221
2222             /* Buggy files workaround */
2223             if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
2224                 p_sample->data.p_sample_soun->i_sampleratehi) )
2225             {
2226                 MP4_Box_data_sample_soun_t *p_soun =
2227                     p_sample->data.p_sample_soun;
2228
2229                 msg_Warn( p_demux, "i_timescale (%"PRId32") != i_sampleratehi "
2230                           "(%u), making both equal (report any problem).",
2231                           p_track->i_timescale, p_soun->i_sampleratehi );
2232
2233                 if( p_soun->i_sampleratehi != 0 )
2234                     p_track->i_timescale = p_soun->i_sampleratehi;
2235                 else
2236                     p_soun->i_sampleratehi = p_track->i_timescale;
2237             }
2238             break;
2239         }
2240
2241         case( VLC_FOURCC( 's', '2', '6', '3' ) ):
2242             p_track->fmt.i_codec = VLC_CODEC_H263;
2243             break;
2244
2245         case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
2246         case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
2247         {
2248             p_track->fmt.i_codec = VLC_CODEC_TX3G;
2249             MP4_Box_data_sample_text_t *p_text = p_sample->data.p_sample_text;
2250             if ( p_text )
2251             {
2252                 text_style_t *p_style = text_style_New();
2253                 if ( p_style )
2254                 {
2255                     if ( p_text->i_font_size ) /* !WARN: % in absolute storage */
2256                         p_style->i_font_size = p_text->i_font_size;
2257                     if ( p_text->i_font_color )
2258                     {
2259                         p_style->i_font_color = p_text->i_font_color >> 8;
2260                         p_style->i_font_alpha = p_text->i_font_color & 0xFF;
2261                     }
2262                     if ( p_text->i_background_color[3] >> 8 )
2263                     {
2264                         p_style->i_background_color = p_text->i_background_color[0] >> 8;
2265                         p_style->i_background_color |= p_text->i_background_color[1] >> 8;
2266                         p_style->i_background_color |= p_text->i_background_color[2] >> 8;
2267                         p_style->i_background_alpha = p_text->i_background_color[3] >> 8;
2268                     }
2269                 }
2270                 p_track->fmt.subs.p_style = p_style;
2271             }
2272             /* FIXME UTF-8 doesn't work here ? */
2273             if( p_track->b_mac_encoding )
2274                 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
2275             else
2276                 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
2277             break;
2278         }
2279         case VLC_FOURCC('y','v','1','2'):
2280             p_track->fmt.i_codec = VLC_CODEC_YV12;
2281             break;
2282         case VLC_FOURCC('y','u','v','2'):
2283             p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
2284             break;
2285
2286         case VLC_FOURCC('i','n','2','4'):
2287             p_track->fmt.i_codec = p_enda && BOXDATA(p_enda)->i_little_endian == 1 ?
2288                                     VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
2289             break;
2290         case VLC_FOURCC('i','n','3','2'):
2291             p_track->fmt.i_codec = p_enda && BOXDATA(p_enda)->i_little_endian == 1 ?
2292                                     VLC_CODEC_S32L : VLC_CODEC_S32B;
2293             break;
2294         case VLC_FOURCC('f','l','3','2'):
2295             p_track->fmt.i_codec = p_enda && BOXDATA(p_enda)->i_little_endian == 1 ?
2296                                     VLC_CODEC_F32L : VLC_CODEC_F32B;
2297             break;
2298         case VLC_FOURCC('f','l','6','4'):
2299             p_track->fmt.i_codec = p_enda && BOXDATA(p_enda)->i_little_endian == 1 ?
2300                                     VLC_CODEC_F64L : VLC_CODEC_F64B;
2301             break;
2302         case VLC_CODEC_DVD_LPCM:
2303         {
2304             MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
2305             if( p_soun->i_qt_version == 2 &&
2306                 p_soun->i_qt_description > 20 + 28 )
2307             {
2308                 /* Flags:
2309                  *  0x01: IsFloat
2310                  *  0x02: IsBigEndian
2311                  *  0x04: IsSigned
2312                  */
2313                 static const struct {
2314                     unsigned     i_flags;
2315                     unsigned     i_mask;
2316                     unsigned     i_bits;
2317                     vlc_fourcc_t i_codec;
2318                 } p_formats[] = {
2319                     { 0x01,           0x03, 32, VLC_CODEC_F32L },
2320                     { 0x01,           0x03, 64, VLC_CODEC_F64L },
2321                     { 0x01|0x02,      0x03, 32, VLC_CODEC_F32B },
2322                     { 0x01|0x02,      0x03, 64, VLC_CODEC_F64B },
2323
2324                     { 0x00,           0x05,  8, VLC_CODEC_U8 },
2325                     { 0x00|     0x04, 0x05,  8, VLC_CODEC_S8 },
2326
2327                     { 0x00,           0x07, 16, VLC_CODEC_U16L },
2328                     { 0x00|0x02,      0x07, 16, VLC_CODEC_U16B },
2329                     { 0x00     |0x04, 0x07, 16, VLC_CODEC_S16L },
2330                     { 0x00|0x02|0x04, 0x07, 16, VLC_CODEC_S16B },
2331
2332                     { 0x00,           0x07, 24, VLC_CODEC_U24L },
2333                     { 0x00|0x02,      0x07, 24, VLC_CODEC_U24B },
2334                     { 0x00     |0x04, 0x07, 24, VLC_CODEC_S24L },
2335                     { 0x00|0x02|0x04, 0x07, 24, VLC_CODEC_S24B },
2336
2337                     { 0x00,           0x07, 32, VLC_CODEC_U32L },
2338                     { 0x00|0x02,      0x07, 32, VLC_CODEC_U32B },
2339                     { 0x00     |0x04, 0x07, 32, VLC_CODEC_S32L },
2340                     { 0x00|0x02|0x04, 0x07, 32, VLC_CODEC_S32B },
2341
2342                     {0, 0, 0, 0}
2343                 };
2344                 uint32_t i_bits  = GetDWBE(&p_soun->p_qt_description[20 + 20]);
2345                 uint32_t i_flags = GetDWBE(&p_soun->p_qt_description[20 + 24]);
2346
2347                 for( int i = 0; p_formats[i].i_codec; i++ )
2348                 {
2349                     if( p_formats[i].i_bits == i_bits &&
2350                         (i_flags & p_formats[i].i_mask) == p_formats[i].i_flags )
2351                     {
2352                         p_track->fmt.i_codec = p_formats[i].i_codec;
2353                         p_track->fmt.audio.i_bitspersample = i_bits;
2354                         p_track->fmt.audio.i_blockalign = p_soun->i_channelcount * i_bits / 8;
2355                         p_track->i_sample_size = p_track->fmt.audio.i_blockalign;
2356
2357                         p_soun->i_qt_version = 0;
2358                         break;
2359                     }
2360                 }
2361             }
2362             break;
2363         }
2364         default:
2365             p_track->fmt.i_codec = p_sample->i_type;
2366             break;
2367     }
2368
2369     /* now see if esds is present and if so create a data packet
2370         with decoder_specific_info  */
2371 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
2372     if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
2373           ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
2374         ( p_esds->data.p_esds )&&
2375         ( p_decconfig ) )
2376     {
2377         /* First update information based on i_objectTypeIndication */
2378         switch( p_decconfig->i_objectTypeIndication )
2379         {
2380             case( 0x20 ): /* MPEG4 VIDEO */
2381                 p_track->fmt.i_codec = VLC_CODEC_MP4V;
2382                 break;
2383             case( 0x21 ): /* H.264 */
2384                 p_track->fmt.i_codec = VLC_CODEC_H264;
2385                 break;
2386             case( 0x40):
2387                 p_track->fmt.i_codec = VLC_CODEC_MP4A;
2388                 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
2389                      p_decconfig->p_decoder_specific_info[0]       == 0xF8 &&
2390                     (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
2391                 {
2392                     p_track->fmt.i_codec = VLC_CODEC_ALS;
2393                 }
2394                 break;
2395             case( 0x60):
2396             case( 0x61):
2397             case( 0x62):
2398             case( 0x63):
2399             case( 0x64):
2400             case( 0x65): /* MPEG2 video */
2401                 p_track->fmt.i_codec = VLC_CODEC_MPGV;
2402                 break;
2403             /* Theses are MPEG2-AAC */
2404             case( 0x66): /* main profile */
2405             case( 0x67): /* Low complexity profile */
2406             case( 0x68): /* Scaleable Sampling rate profile */
2407                 p_track->fmt.i_codec = VLC_CODEC_MP4A;
2408                 break;
2409             /* True MPEG 2 audio */
2410             case( 0x69):
2411                 p_track->fmt.i_codec = VLC_CODEC_MPGA;
2412                 break;
2413             case( 0x6a): /* MPEG1 video */
2414                 p_track->fmt.i_codec = VLC_CODEC_MPGV;
2415                 break;
2416             case( 0x6b): /* MPEG1 audio */
2417                 p_track->fmt.i_codec = VLC_CODEC_MPGA;
2418                 break;
2419             case( 0x6c ): /* jpeg */
2420                 p_track->fmt.i_codec = VLC_CODEC_JPEG;
2421                 break;
2422             case( 0x6d ): /* png */
2423                 p_track->fmt.i_codec = VLC_CODEC_PNG;
2424                 break;
2425             case( 0x6e ): /* jpeg2000 */
2426                 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
2427                 break;
2428             case( 0xa3 ): /* vc1 */
2429                 p_track->fmt.i_codec = VLC_CODEC_VC1;
2430                 break;
2431             case( 0xa4 ):
2432                 p_track->fmt.i_codec = VLC_CODEC_DIRAC;
2433                 break;
2434             case( 0xa5 ):
2435                 p_track->fmt.i_codec = VLC_CODEC_A52;
2436                 break;
2437             case( 0xa6 ):
2438                 p_track->fmt.i_codec = VLC_CODEC_EAC3;
2439                 break;
2440             case( 0xa9 ): /* dts */
2441             case( 0xaa ): /* DTS-HD HRA */
2442             case( 0xab ): /* DTS-HD Master Audio */
2443                 p_track->fmt.i_codec = VLC_CODEC_DTS;
2444                 break;
2445             case( 0xDD ):
2446                 p_track->fmt.i_codec = VLC_CODEC_VORBIS;
2447                 break;
2448
2449             /* Private ID */
2450             case( 0xe0 ): /* NeroDigital: dvd subs */
2451                 if( p_track->fmt.i_cat == SPU_ES )
2452                 {
2453                     p_track->fmt.i_codec = VLC_CODEC_SPU;
2454                     if( p_track->i_width > 0 )
2455                         p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
2456                     if( p_track->i_height > 0 )
2457                         p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
2458                     break;
2459                 }
2460             case( 0xe1 ): /* QCelp for 3gp */
2461                 if( p_track->fmt.i_cat == AUDIO_ES )
2462                 {
2463                     p_track->fmt.i_codec = VLC_CODEC_QCELP;
2464                 }
2465                 break;
2466
2467             /* Fallback */
2468             default:
2469                 /* Unknown entry, but don't touch i_fourcc */
2470                 msg_Warn( p_demux,
2471                           "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
2472                           p_decconfig->i_objectTypeIndication,
2473                           p_track->i_track_ID );
2474                 break;
2475         }
2476         p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
2477         if( p_track->fmt.i_extra > 0 )
2478         {
2479             p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2480             memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
2481                     p_track->fmt.i_extra );
2482         }
2483         if( p_track->fmt.i_codec == VLC_CODEC_SPU &&
2484             p_track->fmt.i_extra >= 16 * 4 )
2485         {
2486             for( int i = 0; i < 16; i++ )
2487             {
2488                 p_track->fmt.subs.spu.palette[1 + i] =
2489                             GetDWBE((char*)p_track->fmt.p_extra + i * 4);
2490             }
2491             p_track->fmt.subs.spu.palette[0] = 0xBeef;
2492         }
2493     }
2494     else
2495     {
2496         switch( p_sample->i_type )
2497         {
2498             /* qt decoder, send the complete chunk */
2499             case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
2500             case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
2501             case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
2502             case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
2503             case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
2504             case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
2505             case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
2506             case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
2507             case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
2508             case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
2509             case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
2510             case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
2511                 p_track->fmt.i_codec = VLC_CODEC_MPGV;
2512                 break;
2513             /* qt decoder, send the complete chunk */
2514             case VLC_CODEC_SVQ1:
2515             case VLC_CODEC_SVQ3:
2516             case VLC_FOURCC( 'V', 'P', '3', '1' ):
2517             case VLC_FOURCC( '3', 'I', 'V', '1' ):
2518             case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
2519                 p_track->fmt.i_extra =
2520                     p_sample->data.p_sample_vide->i_qt_image_description;
2521                 if( p_track->fmt.i_extra > 0 )
2522                 {
2523                     p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2524                     memcpy( p_track->fmt.p_extra,
2525                             p_sample->data.p_sample_vide->p_qt_image_description,
2526                             p_track->fmt.i_extra);
2527                 }
2528                 break;
2529
2530             case VLC_CODEC_AMR_NB:
2531                 p_track->fmt.audio.i_rate = 8000;
2532                 break;
2533             case VLC_CODEC_AMR_WB:
2534                 p_track->fmt.audio.i_rate = 16000;
2535                 break;
2536             case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
2537             case VLC_CODEC_QDM2:
2538             case VLC_CODEC_ALAC:
2539                 p_track->fmt.i_extra =
2540                     p_sample->data.p_sample_soun->i_qt_description;
2541                 if( p_track->fmt.i_extra > 0 )
2542                 {
2543                     p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2544                     memcpy( p_track->fmt.p_extra,
2545                             p_sample->data.p_sample_soun->p_qt_description,
2546                             p_track->fmt.i_extra);
2547                 }
2548                 if( p_track->fmt.i_extra == 56 && p_sample->i_type == VLC_CODEC_ALAC )
2549                 {
2550                     p_track->fmt.audio.i_channels = *((uint8_t*)p_track->fmt.p_extra + 41);
2551                     p_track->fmt.audio.i_rate = GetDWBE((uint8_t*)p_track->fmt.p_extra + 52);
2552                 }
2553                 break;
2554
2555             case VLC_FOURCC( 'v', 'c', '-', '1' ):
2556             {
2557                 MP4_Box_t *p_dvc1 = MP4_BoxGet( p_sample, "dvc1" );
2558                 if( p_dvc1 )
2559                 {
2560                     p_track->fmt.i_extra = BOXDATA(p_dvc1)->i_vc1;
2561                     if( p_track->fmt.i_extra > 0 )
2562                     {
2563                         p_track->fmt.p_extra = malloc( BOXDATA(p_dvc1)->i_vc1 );
2564                         memcpy( p_track->fmt.p_extra, BOXDATA(p_dvc1)->p_vc1,
2565                                 p_track->fmt.i_extra );
2566                     }
2567                 }
2568                 else
2569                 {
2570                     msg_Err( p_demux, "missing dvc1" );
2571                 }
2572                 break;
2573             }
2574
2575             /* avc1: send avcC (h264 without annexe B, ie without start code)*/
2576             case VLC_FOURCC( 'a', 'v', 'c', '1' ):
2577             {
2578                 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
2579
2580                 if( p_avcC )
2581                 {
2582                     p_track->fmt.i_extra = BOXDATA(p_avcC)->i_avcC;
2583                     if( p_track->fmt.i_extra > 0 )
2584                     {
2585                         p_track->fmt.p_extra = malloc( BOXDATA(p_avcC)->i_avcC );
2586                         memcpy( p_track->fmt.p_extra, BOXDATA(p_avcC)->p_avcC,
2587                                 p_track->fmt.i_extra );
2588                     }
2589                 }
2590                 else
2591                 {
2592                     msg_Err( p_demux, "missing avcC" );
2593                 }
2594                 break;
2595             }
2596             case VLC_FOURCC( 'h', 'v', 'c', '1' ):
2597             case VLC_FOURCC( 'h', 'e', 'v', '1' ):
2598             {
2599                 MP4_Box_t *p_hvcC = MP4_BoxGet( p_sample, "hvcC" );
2600
2601                 if( p_hvcC )
2602                 {
2603                     p_track->fmt.i_extra = p_hvcC->data.p_hvcC->i_hvcC;
2604                     if( p_track->fmt.i_extra > 0 )
2605                     {
2606                         p_track->fmt.p_extra = malloc( p_hvcC->data.p_hvcC->i_hvcC );
2607                         memcpy( p_track->fmt.p_extra, p_hvcC->data.p_hvcC->p_hvcC,
2608                                 p_track->fmt.i_extra );
2609                     }
2610                     p_track->fmt.i_codec = VLC_CODEC_HEVC;
2611                 }
2612                 else
2613                 {
2614                     msg_Err( p_demux, "missing hvcC" );
2615                 }
2616                 break;
2617             }
2618
2619
2620             case VLC_CODEC_ADPCM_MS:
2621             case VLC_CODEC_ADPCM_IMA_WAV:
2622             case VLC_CODEC_QCELP:
2623                 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
2624                 break;
2625
2626             default:
2627                 msg_Dbg( p_demux, "Unrecognized FourCC %4.4s", (char *)&p_sample->i_type );
2628                 break;
2629         }
2630     }
2631
2632 #undef p_decconfig
2633
2634     if( pp_es )
2635         *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2636
2637     return VLC_SUCCESS;
2638 }
2639
2640 /* given a time it return sample/chunk
2641  * it also update elst field of the track
2642  */
2643 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2644                                    int64_t i_start, uint32_t *pi_chunk,
2645                                    uint32_t *pi_sample )
2646 {
2647     demux_sys_t *p_sys = p_demux->p_sys;
2648     MP4_Box_t   *p_box_stss;
2649     uint64_t     i_dts;
2650     unsigned int i_sample;
2651     unsigned int i_chunk;
2652     int          i_index;
2653
2654     /* FIXME see if it's needed to check p_track->i_chunk_count */
2655     if( p_track->i_chunk_count == 0 )
2656         return( VLC_EGENERIC );
2657
2658     /* handle elst (find the correct one) */
2659     MP4_TrackSetELST( p_demux, p_track, i_start );
2660     if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
2661     {
2662         MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
2663         int64_t i_mvt= i_start * p_sys->i_timescale / CLOCK_FREQ;
2664
2665         /* now calculate i_start for this elst */
2666         /* offset */
2667         i_start -= p_track->i_elst_time * CLOCK_FREQ / p_sys->i_timescale;
2668         if( i_start < 0 )
2669         {
2670             *pi_chunk = 0;
2671             *pi_sample= 0;
2672
2673             return VLC_SUCCESS;
2674         }
2675         /* to track time scale */
2676         i_start  = i_start * p_track->i_timescale / CLOCK_FREQ;
2677         /* add elst offset */
2678         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2679              elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2680             elst->i_media_time[p_track->i_elst] > 0 )
2681         {
2682             i_start += elst->i_media_time[p_track->i_elst];
2683         }
2684
2685         msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2686                  "ms (track)", p_track->i_elst,
2687                  i_mvt * 1000 / p_sys->i_timescale,
2688                  i_start * 1000 / p_track->i_timescale );
2689     }
2690     else
2691     {
2692         /* convert absolute time to in timescale unit */
2693         i_start = i_start * p_track->i_timescale / CLOCK_FREQ;
2694     }
2695
2696     /* we start from sample 0/chunk 0, hope it won't take too much time */
2697     /* *** find good chunk *** */
2698     for( i_chunk = 0; ; i_chunk++ )
2699     {
2700         if( i_chunk + 1 >= p_track->i_chunk_count )
2701         {
2702             /* at the end and can't check if i_start in this chunk,
2703                it will be check while searching i_sample */
2704             i_chunk = p_track->i_chunk_count - 1;
2705             break;
2706         }
2707
2708         if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2709             (uint64_t)i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
2710         {
2711             break;
2712         }
2713     }
2714
2715     /* *** find sample in the chunk *** */
2716     i_sample = p_track->chunk[i_chunk].i_sample_first;
2717     i_dts    = p_track->chunk[i_chunk].i_first_dts;
2718     for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2719     {
2720         if( i_dts +
2721             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2722             p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2723         {
2724             i_dts    +=
2725                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2726                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2727
2728             i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2729             i_index++;
2730         }
2731         else
2732         {
2733             if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2734             {
2735                 break;
2736             }
2737             i_sample += ( i_start - i_dts ) /
2738                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2739             break;
2740         }
2741     }
2742
2743     if( i_sample >= p_track->i_sample_count )
2744     {
2745         msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2746                   "(seeking too far) chunk=%d sample=%d",
2747                   p_track->i_track_ID, i_chunk, i_sample );
2748         return( VLC_EGENERIC );
2749     }
2750
2751
2752     /* *** Try to find nearest sync points *** */
2753     if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2754     {
2755         MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2756         msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2757                  p_track->i_track_ID );
2758         for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2759         {
2760             if( i_index >= p_stss->i_entry_count - 1 ||
2761                 i_sample < p_stss->i_sample_number[i_index+1] )
2762             {
2763                 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2764                 msg_Dbg( p_demux, "stss gives %d --> %d (sample number)",
2765                          i_sample, i_sync_sample );
2766
2767                 if( i_sync_sample <= i_sample )
2768                 {
2769                     while( i_chunk > 0 &&
2770                            i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2771                         i_chunk--;
2772                 }
2773                 else
2774                 {
2775                     while( i_chunk < p_track->i_chunk_count - 1 &&
2776                            i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2777                                             p_track->chunk[i_chunk].i_sample_count )
2778                         i_chunk++;
2779                 }
2780                 i_sample = i_sync_sample;
2781                 break;
2782             }
2783         }
2784     }
2785     else
2786     {
2787         msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2788                  "Sample Box (stss)", p_track->i_track_ID );
2789     }
2790
2791     *pi_chunk  = i_chunk;
2792     *pi_sample = i_sample;
2793
2794     return VLC_SUCCESS;
2795 }
2796
2797 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2798                                  unsigned int i_chunk, unsigned int i_sample )
2799 {
2800     bool b_reselect = false;
2801
2802     /* now see if actual es is ok */
2803     if( p_track->i_chunk >= p_track->i_chunk_count ||
2804         p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2805             p_track->chunk[i_chunk].i_sample_description_index )
2806     {
2807         msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2808                   p_track->i_track_ID );
2809
2810         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2811                         p_track->p_es, &b_reselect );
2812
2813         es_out_Del( p_demux->out, p_track->p_es );
2814
2815         p_track->p_es = NULL;
2816
2817         if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2818         {
2819             msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2820                      p_track->i_track_ID );
2821
2822             p_track->b_ok       = false;
2823             p_track->b_selected = false;
2824             return VLC_EGENERIC;
2825         }
2826     }
2827
2828     /* select again the new decoder */
2829     if( b_reselect )
2830     {
2831         es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2832     }
2833
2834     p_track->i_chunk    = i_chunk;
2835     p_track->i_sample   = i_sample;
2836
2837     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2838 }
2839
2840 /****************************************************************************
2841  * MP4_TrackCreate:
2842  ****************************************************************************
2843  * Parse track information and create all needed data to run a track
2844  * If it succeed b_ok is set to 1 else to 0
2845  ****************************************************************************/
2846 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2847                              MP4_Box_t *p_box_trak,
2848                              bool b_force_enable )
2849 {
2850     demux_sys_t *p_sys = p_demux->p_sys;
2851
2852     MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2853     MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2854     MP4_Box_t *p_elst;
2855
2856     MP4_Box_t *p_mdhd;
2857     MP4_Box_t *p_udta;
2858     MP4_Box_t *p_hdlr;
2859
2860     MP4_Box_t *p_vmhd;
2861     MP4_Box_t *p_smhd;
2862
2863     char language[4] = { '\0' };
2864
2865     /* hint track unsupported */
2866
2867     /* set default value (-> track unusable) */
2868     p_track->b_ok       = false;
2869     p_track->b_enable   = false;
2870     p_track->b_selected = false;
2871     p_track->b_chapter  = false;
2872     p_track->b_mac_encoding = false;
2873
2874     es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2875
2876     if( !p_tkhd )
2877     {
2878         return;
2879     }
2880
2881     /* do we launch this track by default ? */
2882     p_track->b_enable =
2883         ( ( BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED ) != 0 );
2884     if( !p_track->b_enable )
2885         p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
2886
2887     p_track->i_track_ID = BOXDATA(p_tkhd)->i_track_ID;
2888
2889     p_track->i_width = BOXDATA(p_tkhd)->i_width / BLOCK16x16;
2890     p_track->i_height = BOXDATA(p_tkhd)->i_height / BLOCK16x16;
2891     p_track->f_rotation = BOXDATA(p_tkhd)->f_rotation;
2892
2893     if( p_tref )
2894     {
2895 /*        msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2896     }
2897
2898     p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2899     p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2900
2901     if( ( !p_mdhd )||( !p_hdlr ) )
2902     {
2903         return;
2904     }
2905
2906     p_track->i_timescale = BOXDATA(p_mdhd)->i_timescale;
2907     if( p_track->i_timescale == 0 )
2908         return;
2909
2910     if( BOXDATA(p_mdhd)->i_language_code < 0x400 )
2911     {
2912         strcpy( language, MP4_ConvertMacCode( BOXDATA(p_mdhd)->i_language_code ) );
2913         p_track->b_mac_encoding = true;
2914     }
2915     else if( BOXDATA(p_mdhd)->i_language_code == 0x7fff )
2916         p_track->b_mac_encoding = true;
2917     else
2918     {
2919         for( unsigned i = 0; i < 3; i++ )
2920             language[i] = BOXDATA(p_mdhd)->i_language[i];
2921         language[3] = '\0';
2922     }
2923
2924     switch( p_hdlr->data.p_hdlr->i_handler_type )
2925     {
2926         case( ATOM_soun ):
2927             if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2928             {
2929                 return;
2930             }
2931             p_track->fmt.i_cat = AUDIO_ES;
2932             break;
2933
2934         case( ATOM_vide ):
2935             if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2936             {
2937                 return;
2938             }
2939             p_track->fmt.i_cat = VIDEO_ES;
2940             break;
2941
2942         case( ATOM_tx3g ):
2943         case( ATOM_text ):
2944         case( ATOM_subp ):
2945         case( ATOM_sbtl ):
2946             p_track->fmt.i_codec = VLC_CODEC_TX3G;
2947             p_track->fmt.i_cat = SPU_ES;
2948             break;
2949
2950         default:
2951             return;
2952     }
2953
2954     p_track->i_elst = 0;
2955     p_track->i_elst_time = 0;
2956     if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2957     {
2958         MP4_Box_data_elst_t *elst = BOXDATA(p_elst);
2959         unsigned int i;
2960
2961         msg_Warn( p_demux, "elst box found" );
2962         for( i = 0; i < elst->i_entry_count; i++ )
2963         {
2964             msg_Dbg( p_demux, "   - [%d] duration=%"PRId64"ms media time=%"PRId64
2965                      "ms) rate=%d.%d", i,
2966                      elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2967                      elst->i_media_time[i] >= 0 ?
2968                      (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2969                      INT64_C(-1),
2970                      elst->i_media_rate_integer[i],
2971                      elst->i_media_rate_fraction[i] );
2972         }
2973     }
2974
2975
2976 /*  TODO
2977     add support for:
2978     p_dinf = MP4_BoxGet( p_minf, "dinf" );
2979 */
2980     if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2981         !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2982     {
2983         return;
2984     }
2985
2986     /* Set language */
2987     if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2988     {
2989         p_track->fmt.psz_language = strdup( language );
2990     }
2991
2992     p_udta = MP4_BoxGet( p_box_trak, "udta" );
2993     if( p_udta )
2994     {
2995         MP4_Box_t *p_box_iter;
2996         for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2997                  p_box_iter = p_box_iter->p_next )
2998         {
2999             switch( p_box_iter->i_type )
3000             {
3001                 case ATOM_0xa9nam:
3002                     p_track->fmt.psz_description =
3003                         strdup( p_box_iter->data.p_0xa9xxx->psz_text );
3004                     break;
3005                 case ATOM_name:
3006                     p_track->fmt.psz_description =
3007                         strdup( p_box_iter->data.p_name->psz_text );
3008                     break;
3009             }
3010         }
3011     }
3012
3013     /* Create chunk index table and sample index table */
3014     if( TrackCreateChunksIndex( p_demux,p_track  ) ||
3015         TrackCreateSamplesIndex( p_demux, p_track ) )
3016     {
3017         msg_Err( p_demux, "cannot create chunks index" );
3018         return; /* cannot create chunks index */
3019     }
3020
3021     p_track->i_chunk  = 0;
3022     p_track->i_sample = 0;
3023
3024     /* Mark chapter only track */
3025     if( p_sys->p_tref_chap )
3026     {
3027         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
3028         unsigned int i;
3029
3030         for( i = 0; i < p_chap->i_entry_count; i++ )
3031         {
3032             if( p_track->i_track_ID == p_chap->i_track_ID[i] &&
3033                 p_track->fmt.i_cat == UNKNOWN_ES )
3034             {
3035                 p_track->b_chapter = true;
3036                 p_track->b_enable = false;
3037                 break;
3038             }
3039         }
3040     }
3041
3042     /* now create es */
3043     if( b_force_enable &&
3044         ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
3045     {
3046         msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
3047                   p_track->i_track_ID );
3048         p_track->b_enable = true;
3049         p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN;
3050     }
3051
3052     p_track->p_es = NULL;
3053     if( TrackCreateES( p_demux,
3054                        p_track, p_track->i_chunk,
3055                        p_track->b_chapter ? NULL : &p_track->p_es ) )
3056     {
3057         msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
3058                  p_track->i_track_ID );
3059         return;
3060     }
3061     p_track->b_ok = true;
3062 #if 0
3063     {
3064         int i;
3065         for( i = 0; i < p_track->i_chunk_count; i++ )
3066         {
3067             fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
3068                      i, p_track->chunk[i].i_sample_count,
3069                      p_track->chunk[i].i_first_dts );
3070
3071         }
3072     }
3073 #endif
3074 }
3075
3076 static void FreeAndResetChunk( mp4_chunk_t *ck )
3077 {
3078     free( ck->p_sample_count_dts );
3079     free( ck->p_sample_delta_dts );
3080     free( ck->p_sample_count_pts );
3081     free( ck->p_sample_offset_pts );
3082     free( ck->p_sample_size );
3083     for( uint32_t i = 0; i < ck->i_sample_count; i++ )
3084         free( ck->p_sample_data[i] );
3085     free( ck->p_sample_data );
3086     memset( ck, 0, sizeof( mp4_chunk_t ) );
3087 }
3088
3089 /****************************************************************************
3090  * MP4_TrackDestroy:
3091  ****************************************************************************
3092  * Destroy a track created by MP4_TrackCreate.
3093  ****************************************************************************/
3094 static void MP4_TrackDestroy( mp4_track_t *p_track )
3095 {
3096     unsigned int i_chunk;
3097
3098     p_track->b_ok = false;
3099     p_track->b_enable   = false;
3100     p_track->b_selected = false;
3101
3102     es_format_Clean( &p_track->fmt );
3103
3104     for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
3105     {
3106         if( p_track->chunk )
3107         {
3108            FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
3109            FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
3110
3111            FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
3112            FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
3113         }
3114     }
3115     FREENULL( p_track->chunk );
3116     if( p_track->cchunk ) {
3117         FreeAndResetChunk( p_track->cchunk );
3118         FREENULL( p_track->cchunk );
3119     }
3120
3121     if( !p_track->i_sample_size )
3122     {
3123         FREENULL( p_track->p_sample_size );
3124     }
3125 }
3126
3127 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
3128                             mtime_t i_start )
3129 {
3130     if( !p_track->b_ok || p_track->b_chapter )
3131     {
3132         return VLC_EGENERIC;
3133     }
3134
3135     if( p_track->b_selected )
3136     {
3137         msg_Warn( p_demux, "track[Id 0x%x] already selected",
3138                   p_track->i_track_ID );
3139         return VLC_SUCCESS;
3140     }
3141
3142     return MP4_TrackSeek( p_demux, p_track, i_start );
3143 }
3144
3145 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
3146 {
3147     if( !p_track->b_ok || p_track->b_chapter )
3148     {
3149         return;
3150     }
3151
3152     if( !p_track->b_selected )
3153     {
3154         msg_Warn( p_demux, "track[Id 0x%x] already unselected",
3155                   p_track->i_track_ID );
3156         return;
3157     }
3158     if( p_track->p_es )
3159     {
3160         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
3161                         p_track->p_es, false );
3162     }
3163
3164     p_track->b_selected = false;
3165 }
3166
3167 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
3168                           mtime_t i_start )
3169 {
3170     uint32_t i_chunk;
3171     uint32_t i_sample;
3172
3173     if( !p_track->b_ok || p_track->b_chapter )
3174         return VLC_EGENERIC;
3175
3176     p_track->b_selected = false;
3177
3178     if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
3179                                 &i_chunk, &i_sample ) )
3180     {
3181         msg_Warn( p_demux, "cannot select track[Id 0x%x]",
3182                   p_track->i_track_ID );
3183         return VLC_EGENERIC;
3184     }
3185
3186     p_track->b_selected = true;
3187
3188     if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
3189         p_track->b_selected = true;
3190
3191     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
3192 }
3193
3194
3195 /*
3196  * 3 types: for audio
3197  *
3198  */
3199 #define QT_V0_MAX_SAMPLES 1024
3200 static uint32_t MP4_TrackSampleSize( mp4_track_t *p_track, uint32_t *pi_nb_samples )
3201 {
3202     uint32_t i_size;
3203     MP4_Box_data_sample_soun_t *p_soun;
3204
3205     if( p_track->i_sample_size == 0 )
3206     {
3207         /* most simple case */
3208         *pi_nb_samples = 1;
3209         return p_track->p_sample_size[p_track->i_sample];
3210     }
3211
3212     if( p_track->fmt.i_cat != AUDIO_ES )
3213     {
3214         *pi_nb_samples = 1;
3215         return p_track->i_sample_size;
3216     }
3217
3218     p_soun = p_track->p_sample->data.p_sample_soun;
3219
3220     if( p_soun->i_qt_version == 1 )
3221     {
3222         uint32_t i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
3223         if( p_track->fmt.audio.i_blockalign > 1 )
3224             i_samples = p_soun->i_sample_per_packet;
3225         i_size = i_samples / p_soun->i_sample_per_packet;
3226         if ( UINT32_MAX / i_size >= p_soun->i_bytes_per_frame )
3227             i_size *= p_soun->i_bytes_per_frame;
3228         else
3229             i_size = UINT32_MAX;
3230         *pi_nb_samples = i_samples;
3231     }
3232     else if( p_track->i_sample_size > 256 )
3233     {
3234         /* We do that so we don't read too much data
3235          * (in this case we are likely dealing with compressed data) */
3236         i_size = p_track->i_sample_size;
3237         *pi_nb_samples = 1;
3238     }
3239     else
3240     {
3241         mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
3242         int64_t i_length = CLOCK_FREQ / 10;
3243         uint32_t i_samples = 0;
3244         uint32_t i_maxsamples = 0;
3245         for( uint32_t i=p_track->i_sample; i<p_chunk->i_sample_count; i++ )
3246         {
3247             i_length -= CLOCK_FREQ * p_chunk->p_sample_delta_dts[i] / p_track->i_timescale;
3248             if ( i_length < 0 ) break;
3249             i_maxsamples++;
3250         }
3251
3252         i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
3253         i_samples = __MIN( i_maxsamples, i_samples );
3254         if ( i_samples == 0 ) i_samples = 1;
3255         i_size = i_samples * p_track->i_sample_size;
3256         *pi_nb_samples = i_samples;
3257     }
3258
3259     //fprintf( stderr, "size=%d\n", i_size );
3260     return i_size;
3261 }
3262
3263 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
3264 {
3265     unsigned int i_sample;
3266     uint64_t i_pos;
3267
3268     i_pos = p_track->chunk[p_track->i_chunk].i_offset;
3269
3270     if( p_track->i_sample_size )
3271     {
3272         MP4_Box_data_sample_soun_t *p_soun =
3273             p_track->p_sample->data.p_sample_soun;
3274
3275         if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
3276         {
3277             i_pos += ( p_track->i_sample -
3278                        p_track->chunk[p_track->i_chunk].i_sample_first ) *
3279                      p_track->i_sample_size;
3280         }
3281         else
3282         {
3283             /* we read chunk by chunk unless a blockalign is requested */
3284             if( p_track->fmt.audio.i_blockalign > 1 )
3285                 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
3286                                 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
3287         }
3288     }
3289     else
3290     {
3291         for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
3292              i_sample < p_track->i_sample; i_sample++ )
3293         {
3294             i_pos += p_track->p_sample_size[i_sample];
3295         }
3296     }
3297
3298     return i_pos;
3299 }
3300
3301 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track, uint32_t i_samples )
3302 {
3303     if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
3304     {
3305         MP4_Box_data_sample_soun_t *p_soun;
3306
3307         p_soun = p_track->p_sample->data.p_sample_soun;
3308
3309         if( p_soun->i_qt_version == 1 )
3310         {
3311             /* we read chunk by chunk unless a blockalign is requested */
3312             if( p_track->fmt.audio.i_blockalign > 1 )
3313                 p_track->i_sample += p_soun->i_sample_per_packet;
3314             else
3315                 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
3316         }
3317         else if( p_track->i_sample_size > 256 )
3318         {
3319             /* We do that so we don't read too much data
3320              * (in this case we are likely dealing with compressed data) */
3321             p_track->i_sample += 1;
3322         }
3323         else
3324         {
3325             /* FIXME */
3326             p_track->i_sample += i_samples;
3327             if( p_track->i_sample >
3328                 p_track->chunk[p_track->i_chunk].i_sample_first +
3329                 p_track->chunk[p_track->i_chunk].i_sample_count )
3330             {
3331                 p_track->i_sample =
3332                     p_track->chunk[p_track->i_chunk].i_sample_first +
3333                     p_track->chunk[p_track->i_chunk].i_sample_count;
3334             }
3335         }
3336     }
3337     else
3338     {
3339         p_track->i_sample++;
3340     }
3341
3342     if( p_track->i_sample >= p_track->i_sample_count )
3343         return VLC_EGENERIC;
3344
3345     /* Have we changed chunk ? */
3346     if( p_track->i_sample >=
3347             p_track->chunk[p_track->i_chunk].i_sample_first +
3348             p_track->chunk[p_track->i_chunk].i_sample_count )
3349     {
3350         if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
3351                                   p_track->i_sample ) )
3352         {
3353             msg_Warn( p_demux, "track[0x%x] will be disabled "
3354                       "(cannot restart decoder)", p_track->i_track_ID );
3355             MP4_TrackUnselect( p_demux, p_track );
3356             return VLC_EGENERIC;
3357         }
3358     }
3359
3360     /* Have we changed elst */
3361     if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
3362     {
3363         demux_sys_t *p_sys = p_demux->p_sys;
3364         MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
3365         uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
3366                         p_sys->i_timescale / CLOCK_FREQ;
3367
3368         if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
3369             i_mvt >= p_track->i_elst_time +
3370                      elst->i_segment_duration[p_track->i_elst] )
3371         {
3372             MP4_TrackSetELST( p_demux, p_track,
3373                               MP4_TrackGetDTS( p_demux, p_track ) );
3374         }
3375     }
3376
3377     return VLC_SUCCESS;
3378 }
3379
3380 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
3381                               int64_t i_time )
3382 {
3383     demux_sys_t *p_sys = p_demux->p_sys;
3384     int         i_elst_last = tk->i_elst;
3385
3386     /* handle elst (find the correct one) */
3387     tk->i_elst      = 0;
3388     tk->i_elst_time = 0;
3389     if( tk->p_elst && tk->BOXDATA(p_elst)->i_entry_count > 0 )
3390     {
3391         MP4_Box_data_elst_t *elst = tk->BOXDATA(p_elst);
3392         int64_t i_mvt= i_time * p_sys->i_timescale / CLOCK_FREQ;
3393
3394         for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
3395         {
3396             mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
3397
3398             if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
3399             {
3400                 break;
3401             }
3402             tk->i_elst_time += i_dur;
3403         }
3404
3405         if( (unsigned int)tk->i_elst >= elst->i_entry_count )
3406         {
3407             /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
3408             tk->i_elst = elst->i_entry_count - 1;
3409             tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
3410         }
3411
3412         if( elst->i_media_time[tk->i_elst] < 0 )
3413         {
3414             /* track offset */
3415             tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
3416         }
3417     }
3418     if( i_elst_last != tk->i_elst )
3419     {
3420         msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
3421     }
3422 }
3423
3424 /* */
3425 static const char *MP4_ConvertMacCode( uint16_t i_code )
3426 {
3427     static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
3428         { "en",   0 }, { "fr",   1 }, { "de",   2 }, { "it",   3 }, { "nl",   4 },
3429         { "sv",   5 }, { "es",   6 }, { "da",   7 }, { "pt",   8 }, { "no",   9 },
3430         { "he",  10 }, { "ja",  11 }, { "ar",  12 }, { "fi",  13 }, { "el",  14 },
3431         { "is",  15 }, { "mt",  16 }, { "tr",  17 }, { "hr",  18 }, { "zh",  19 },
3432         { "ur",  20 }, { "hi",  21 }, { "th",  22 }, { "ko",  23 }, { "lt",  24 },
3433         { "pl",  25 }, { "hu",  26 }, { "et",  27 }, { "lv",  28 }, //{ "??",  29 },
3434         { "fo",  30 }, { "fa",  31 }, { "ru",  32 }, { "zh",  33 }, { "nl",  34 },
3435         { "ga",  35 }, { "sq",  36 }, { "ro",  37 }, { "cs",  38 }, { "sk",  39 },
3436         { "sl",  40 }, { "yi",  41 }, { "sr",  42 }, { "mk",  43 }, { "bg",  44 },
3437         { "uk",  45 }, { "be",  46 }, { "uz",  47 }, { "az",  48 }, { "kk",  48 },
3438         { "az",  50 }, { "hy",  51 }, { "ka",  52 }, { "mo",  53 }, { "ky",  54 },
3439         { "tg",  55 }, { "tk",  56 }, { "mn",  57 }, { "mn",  58 }, { "ps",  59 },
3440         { "ku",  60 }, { "ks",  61 }, { "sd",  62 }, { "bo",  63 }, { "ne",  64 },
3441         { "sa",  65 }, { "mr",  66 }, { "bn",  67 }, { "as",  68 }, { "gu",  69 },
3442         { "pa",  70 }, { "or",  71 }, { "ml",  72 }, { "kn",  73 }, { "ta",  74 },
3443         { "te",  75 }, { "si",  76 }, { "my",  77 }, { "km",  78 }, { "lo",  79 },
3444         { "vi",  80 }, { "id",  81 }, { "tl",  82 }, { "ms",  83 }, { "ms",  84 },
3445         { "am",  85 }, { "ti",  86 }, { "om",  87 }, { "so",  88 }, { "sw",  89 },
3446         { "rw",  90 }, { "rn",  91 }, { "ny",  92 }, { "mg",  93 }, { "eo",  94 },
3447
3448                                                      { "cy", 128 }, { "eu", 129 },
3449         { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
3450         { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
3451         { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
3452         { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
3453         /* */
3454         { "", 0 }
3455     };
3456     int i;
3457     for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
3458     {
3459         if( p_cvt[i].i_code == i_code )
3460             return p_cvt[i].psz_iso639_1;
3461     }
3462     return "";
3463 }
3464
3465 /******************************************************************************
3466  *     Here are the functions used for fragmented MP4
3467  *****************************************************************************/
3468
3469 /**
3470  * It computes the sample rate for a video track using current video chunk
3471  */
3472 static void ChunkGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
3473                                   const mp4_track_t *p_track )
3474 {
3475     if( p_track->cchunk->i_last_dts == 0 )
3476         return;
3477
3478     *pi_num = 0;
3479     *pi_den = 0;
3480
3481     /* */
3482     const mp4_chunk_t *p_chunk = p_track->cchunk;
3483
3484     uint32_t i_sample = p_chunk->i_sample_count;
3485     uint64_t i_first_dts = p_chunk->i_first_dts;
3486     uint64_t i_last_dts =  p_chunk->i_last_dts;
3487
3488     if( i_sample > 1 && i_first_dts < i_last_dts )
3489         vlc_ureduce( pi_num, pi_den,
3490                      ( i_sample - 1) *  p_track->i_timescale,
3491                      i_last_dts - i_first_dts,
3492                      UINT16_MAX);
3493 }
3494
3495 /**
3496  * Build raw avcC box (without the 8 bytes header)
3497  * \return The size of the box.
3498  */
3499 static int build_raw_avcC( uint8_t **p_extra, const uint8_t *CodecPrivateData,
3500                                                        const unsigned cpd_len )
3501 {
3502     uint8_t *avcC;
3503     unsigned sps_len = 0, pps_len = 0;
3504     const uint32_t mark = 0x00000001;
3505
3506     assert( CodecPrivateData[0] == 0 );
3507     assert( CodecPrivateData[1] == 0 );
3508     assert( CodecPrivateData[2] == 0 );
3509     assert( CodecPrivateData[3] == 1 );
3510
3511     uint32_t length = cpd_len + 3;
3512     avcC = calloc( length, 1 );
3513     if( unlikely( avcC == NULL ) )
3514         return 0;
3515
3516     uint8_t *sps = avcC + 8;
3517
3518     uint32_t candidate = ~mark;
3519     CodecPrivateData += 4;
3520     for( unsigned i = 0; i < cpd_len - 4; i++ )
3521     {
3522         sps[i] = CodecPrivateData[i];
3523         candidate = (candidate << 8) | CodecPrivateData[i];
3524         if( candidate == mark )
3525         {
3526             sps_len = i - 3;
3527             break;
3528         }
3529     }
3530     if( sps_len == 0 )
3531     {
3532         free( avcC );
3533         return 0;
3534     }
3535     uint8_t *pps = sps + sps_len + 3;
3536     pps_len = cpd_len - sps_len - 4 * 2;
3537     memcpy( pps, CodecPrivateData + sps_len + 4, pps_len );
3538
3539     /* XXX */
3540     uint8_t AVCProfileIndication = 0x64;
3541     uint8_t profile_compatibility = 0x40;
3542     uint8_t AVCLevelIndication = 0x1f;
3543     uint8_t lengthSizeMinusOne = 0x03;
3544
3545     avcC[0] = 1;
3546     avcC[1] = AVCProfileIndication;
3547     avcC[2] = profile_compatibility;
3548     avcC[3] = AVCLevelIndication;
3549     avcC[4] = 0xfc + lengthSizeMinusOne;
3550     avcC[5] = 0xe0 + 1;
3551     avcC[6] = (sps_len & 0xff00)>>8;
3552     avcC[7] = sps_len & 0xff;
3553
3554     avcC[8+sps_len] = 1;
3555     avcC[9+sps_len] = (pps_len & 0xff00) >> 8;
3556     avcC[10+sps_len] = pps_len & 0xff;
3557
3558     *p_extra = avcC;
3559     return length;
3560 }
3561
3562 /**
3563  * Build a mp4_track_t from a StraBox
3564  */
3565
3566 static inline int MP4_SetCodecExtraData( es_format_t *fmt, MP4_Box_data_stra_t *p_data )
3567 {
3568     fmt->i_extra = p_data->cpd_len;
3569     fmt->p_extra = malloc( p_data->cpd_len );
3570     if( unlikely( !fmt->p_extra ) )
3571         return VLC_ENOMEM;
3572     memcpy( fmt->p_extra, p_data->CodecPrivateData, p_data->cpd_len );
3573     return VLC_SUCCESS;
3574   }
3575
3576 static int MP4_frg_TrackCreate( demux_t *p_demux, mp4_track_t *p_track, MP4_Box_t *p_stra )
3577 {
3578     demux_sys_t *p_sys = p_demux->p_sys;
3579     int ret;
3580     MP4_Box_data_stra_t *p_data = BOXDATA(p_stra);
3581     if( !p_data )
3582         return VLC_EGENERIC;
3583
3584     p_track->b_ok       = true;
3585     p_track->b_selected = false;
3586     p_track->i_sample_count = UINT32_MAX;
3587
3588     p_track->i_timescale = p_sys->i_timescale;
3589     p_track->i_width = p_data->MaxWidth;
3590     p_track->i_height = p_data->MaxHeight;
3591     p_track->i_track_ID = p_data->i_track_ID;
3592
3593     es_format_t *fmt = &p_track->fmt;
3594     if( fmt == NULL )
3595         return VLC_EGENERIC;
3596
3597     es_format_Init( fmt, p_data->i_es_cat, 0 );
3598
3599     /* Set language FIXME */
3600     fmt->psz_language = strdup( "en" );
3601
3602     fmt->i_original_fourcc = p_data->FourCC;
3603     fmt->i_codec = vlc_fourcc_GetCodec( fmt->i_cat, p_data->FourCC );
3604
3605     uint8_t **p_extra = (uint8_t **)&fmt->p_extra;
3606     /* See http://msdn.microsoft.com/en-us/library/ff728116%28v=vs.90%29.aspx
3607      * for MS weird use of FourCC*/
3608     switch( fmt->i_cat )
3609     {
3610         case VIDEO_ES:
3611             if( p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', '1' ) ||
3612                 p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', 'B' ) ||
3613                 p_data->FourCC == VLC_FOURCC( 'H', '2', '6', '4' ) )
3614             {
3615                 fmt->i_extra = build_raw_avcC( p_extra,
3616                         p_data->CodecPrivateData, p_data->cpd_len );
3617             }
3618             else
3619             {
3620                 ret = MP4_SetCodecExtraData( fmt, p_data );
3621                 if( ret != VLC_SUCCESS )
3622                     return ret;
3623             }
3624
3625             fmt->video.i_width = p_data->MaxWidth;
3626             fmt->video.i_height = p_data->MaxHeight;
3627             fmt->video.i_bits_per_pixel = 0x18;
3628             fmt->video.i_visible_width = p_data->MaxWidth;
3629             fmt->video.i_visible_height = p_data->MaxHeight;
3630
3631             /* Frame rate */
3632             ChunkGetESSampleRate( &fmt->video.i_frame_rate,
3633                                   &fmt->video.i_frame_rate_base, p_track );
3634
3635             if( fmt->video.i_frame_rate_base != 0 )
3636             {
3637                 p_demux->p_sys->f_fps = (float)fmt->video.i_frame_rate /
3638                                         (float)fmt->video.i_frame_rate_base;
3639             }
3640             else
3641                 p_demux->p_sys->f_fps = 24;
3642
3643             break;
3644
3645         case AUDIO_ES:
3646             fmt->audio.i_channels = p_data->Channels;
3647             fmt->audio.i_rate = p_data->SamplingRate;
3648             fmt->audio.i_bitspersample = p_data->BitsPerSample;
3649             fmt->audio.i_blockalign = p_data->nBlockAlign;
3650
3651             fmt->i_bitrate = p_data->Bitrate;
3652
3653             ret = MP4_SetCodecExtraData( fmt, p_data );
3654             if( ret != VLC_SUCCESS )
3655                 return ret;
3656
3657             break;
3658
3659         default:
3660             break;
3661     }
3662
3663     return VLC_SUCCESS;
3664 }
3665
3666 /**
3667  * Return the track identified by tid
3668  */
3669 static mp4_track_t *MP4_frg_GetTrackByID( demux_t *p_demux, const uint32_t tid )
3670 {
3671     demux_sys_t *p_sys = p_demux->p_sys;
3672
3673     mp4_track_t *ret = NULL;
3674     for( unsigned i = 0; i < p_sys->i_tracks; i++ )
3675     {
3676         ret = &p_sys->track[i];
3677         if( ret->i_track_ID == tid )
3678             return ret;
3679     }
3680     msg_Err( p_demux, "MP4_frg_GetTrack: track %"PRIu32" not found!", tid );
3681     return NULL;
3682 }
3683
3684 static void FlushChunk( demux_t *p_demux, mp4_track_t *tk )
3685 {
3686     msg_Dbg( p_demux, "Flushing chunk for track id %u", tk->i_track_ID );
3687     mp4_chunk_t *ck = tk->cchunk;
3688     while( ck->i_sample < ck->i_sample_count )
3689     {
3690         block_t *p_block;
3691         int64_t i_delta;
3692
3693         if( ck->p_sample_size == NULL || ck->p_sample_data == NULL )
3694             return;
3695
3696         uint32_t sample_size = ck->p_sample_size[ck->i_sample];
3697         assert( sample_size > 0 );
3698         p_block = block_Alloc( sample_size );
3699         if( unlikely( !p_block ) )
3700             return;
3701
3702         uint8_t *src = ck->p_sample_data[ck->i_sample];
3703         memcpy( p_block->p_buffer, src, sample_size );
3704         ck->i_sample++;
3705
3706         /* dts */
3707         p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
3708         /* pts */
3709         i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
3710         if( i_delta != -1 )
3711             p_block->i_pts = p_block->i_dts + i_delta;
3712         else if( tk->fmt.i_cat != VIDEO_ES )
3713             p_block->i_pts = p_block->i_dts;
3714         else
3715             p_block->i_pts = VLC_TS_INVALID;
3716
3717         es_out_Send( p_demux->out, tk->p_es, p_block );
3718
3719         tk->i_sample++;
3720     }
3721 }
3722
3723 /**
3724  * Re-init decoder.
3725  * \Note If we call that function too soon,
3726  * before the track has been selected by MP4_TrackSelect
3727  * (during the first execution of Demux), then the track gets disabled
3728  */
3729 static int ReInitDecoder( demux_t *p_demux, mp4_track_t *p_track )
3730 {
3731     demux_sys_t *p_sys = p_demux->p_sys;
3732
3733     uint32_t i_sample = 0;
3734     bool b_smooth = false;
3735     MP4_Box_t *p_stra = NULL, *p_trak = NULL;
3736
3737     if( !CmpUUID( &p_sys->p_root->p_first->i_uuid, &SmooBoxUUID ) )
3738         b_smooth = true;
3739
3740     if( b_smooth )
3741     {
3742         p_stra = MP4_BoxGet( p_sys->p_root, "uuid/uuid[0]" );
3743         if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
3744             return VLC_EGENERIC;
3745     }
3746     else /* DASH */
3747     {
3748         p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[0]" );
3749         if( !p_trak )
3750             return VLC_EGENERIC;
3751     }
3752
3753     i_sample = p_track->i_sample;
3754     es_out_Del( p_demux->out, p_track->p_es );
3755     es_format_Clean( &p_track->fmt );
3756
3757     if( b_smooth )
3758         MP4_frg_TrackCreate( p_demux, p_track, p_stra );
3759     else /* DASH */
3760         MP4_TrackCreate( p_demux, p_track, p_trak, true );
3761
3762     p_track->i_sample = i_sample;
3763
3764     /* Temporary hack until we support track selection */
3765     p_track->b_selected = true;
3766     p_track->b_ok = true;
3767     p_track->b_enable = true;
3768
3769     p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
3770     p_track->b_codec_need_restart = false;
3771
3772     return VLC_SUCCESS;
3773 }
3774
3775 /**
3776  * This function fills a mp4_chunk_t structure from a MP4_Box_t (p_chunk).
3777  * The 'i_tk_id' argument returns the ID of the track the chunk belongs to.
3778  * \note p_chunk usually contains a 'moof' and a 'mdat', and might contain a 'sidx'.
3779  * \return VLC_SUCCESS, VLC_EGENERIC or VLC_ENOMEM.
3780  */
3781 static int MP4_frg_GetChunk( demux_t *p_demux, MP4_Box_t *p_chunk, unsigned *i_tk_id )
3782 {
3783     MP4_Box_t *p_sidx = MP4_BoxGet( p_chunk, "sidx" );
3784     MP4_Box_t *p_moof = MP4_BoxGet( p_chunk, "moof" );
3785     if( p_moof == NULL)
3786     {
3787         msg_Warn( p_demux, "no moof box found!" );
3788         return VLC_EGENERIC;
3789     }
3790
3791     MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
3792     if( p_traf == NULL)
3793     {
3794         msg_Warn( p_demux, "no traf box found!" );
3795         return VLC_EGENERIC;
3796     }
3797
3798     MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
3799     if( p_tfhd == NULL)
3800     {
3801         msg_Warn( p_demux, "no tfhd box found!" );
3802         return VLC_EGENERIC;
3803     }
3804
3805     uint32_t i_track_ID = BOXDATA(p_tfhd)->i_track_ID;
3806     *i_tk_id = i_track_ID;
3807     assert( i_track_ID > 0 );
3808     msg_Dbg( p_demux, "GetChunk: track ID is %"PRIu32"", i_track_ID );
3809
3810     mp4_track_t *p_track = MP4_frg_GetTrackByID( p_demux, i_track_ID );
3811     if( !p_track )
3812         return VLC_EGENERIC;
3813
3814     mp4_chunk_t *ret = p_track->cchunk;
3815
3816     if( BOXDATA(p_tfhd)->b_empty )
3817         msg_Warn( p_demux, "No samples in this chunk!" );
3818
3819     /* Usually we read 100 ms of each track. However, suppose we have two tracks,
3820      * Ta and Tv (audio and video). Suppose also that Ta is the first track to be
3821      * read, i.e. we read 100 ms of Ta, then 100 ms of Tv, then 100 ms of Ta,
3822      * and so on. Finally, suppose that we get the chunks the other way around,
3823      * i.e. first a chunk of Tv, then a chunk of Ta, then a chunk of Tv, and so on.
3824      * In that case, it is very likely that at some point, Ta->cchunk or Tv->cchunk
3825      * is not emptied when MP4_frg_GetChunks is called. It is therefore necessary to
3826      * flush it, i.e. send to the decoder the samples not yet sent.
3827      * Note that all the samples to be flushed should worth less than 100 ms,
3828      * (though I did not do the formal proof) and thus this flushing mechanism
3829      * should not cause A/V sync issues, or delays or whatever.
3830      */
3831     if( ret->i_sample < ret->i_sample_count )
3832         FlushChunk( p_demux, p_track );
3833
3834     if( ret->i_sample_count )
3835         FreeAndResetChunk( ret );
3836
3837     uint32_t default_duration = 0;
3838     if( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
3839         default_duration = BOXDATA(p_tfhd)->i_default_sample_duration;
3840
3841     uint32_t default_size = 0;
3842     if( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
3843         default_size = BOXDATA(p_tfhd)->i_default_sample_size;
3844
3845     MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun");
3846     if( p_trun == NULL)
3847     {
3848         msg_Warn( p_demux, "no trun box found!" );
3849         return VLC_EGENERIC;
3850     }
3851     MP4_Box_data_trun_t *p_trun_data = p_trun->data.p_trun;
3852
3853     ret->i_sample_count = p_trun_data->i_sample_count;
3854     assert( ret->i_sample_count > 0 );
3855     ret->i_sample_description_index = 1; /* seems to be always 1, is it? */
3856     ret->i_sample_first = p_track->i_sample_first;
3857     p_track->i_sample_first += ret->i_sample_count;
3858
3859     ret->i_first_dts = p_track->i_first_dts;
3860
3861     /* XXX I already saw DASH content with no default_duration and no
3862      * MP4_TRUN_SAMPLE_DURATION flag, but a sidx box was present.
3863      * This chunk of code might be buggy with segments having
3864      * more than one subsegment. */
3865     if( !default_duration )
3866     {
3867         MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_moof, i_track_ID );
3868         if ( p_trex )
3869             default_duration = BOXDATA(p_trex)->i_default_sample_duration;
3870         else if( p_sidx )
3871         {
3872             MP4_Box_data_sidx_t *p_sidx_data = BOXDATA(p_sidx);
3873             assert( p_sidx_data->i_reference_count == 1 );
3874
3875             if( p_sidx_data->i_timescale == 0 )
3876                 return VLC_EGENERIC;
3877
3878             unsigned i_chunk_duration = p_sidx_data->p_items[0].i_subsegment_duration /
3879                                         p_sidx_data->i_timescale;
3880             default_duration = i_chunk_duration * p_track->i_timescale / ret->i_sample_count;
3881
3882         }
3883     }
3884
3885     msg_Dbg( p_demux, "Default sample duration is %"PRIu32, default_duration );
3886
3887     ret->p_sample_count_dts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3888     ret->p_sample_delta_dts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3889
3890     if( !ret->p_sample_count_dts || !ret->p_sample_delta_dts )
3891         return VLC_ENOMEM;
3892
3893     ret->p_sample_count_pts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3894     if( !ret->p_sample_count_pts )
3895         return VLC_ENOMEM;
3896
3897     if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
3898     {
3899         ret->p_sample_offset_pts = calloc( ret->i_sample_count, sizeof( int32_t ) );
3900         if( !ret->p_sample_offset_pts )
3901             return VLC_ENOMEM;
3902     }
3903
3904     ret->p_sample_size = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3905     if( !ret->p_sample_size )
3906         return VLC_ENOMEM;
3907
3908     ret->p_sample_data = calloc( ret->i_sample_count, sizeof( uint8_t * ) );
3909     if( !ret->p_sample_data )
3910         return VLC_ENOMEM;
3911
3912     uint32_t dur = 0, i_mdatlen = 0, len;
3913     uint32_t chunk_duration = 0, chunk_size = 0;
3914
3915     /* Skip header of mdat */
3916     uint8_t mdat[8];
3917     int i_read = stream_Read( p_demux->s, &mdat, 8 );
3918     i_mdatlen = GetDWBE( mdat );
3919     if ( i_read < 8 || i_mdatlen < 8 ||
3920          VLC_FOURCC( mdat[4], mdat[5], mdat[6], mdat[7] ) != ATOM_mdat )
3921         return VLC_EGENERIC;
3922
3923     for( uint32_t i = 0; i < ret->i_sample_count; i++)
3924     {
3925         if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_DURATION )
3926             dur = p_trun_data->p_samples[i].i_duration;
3927         else
3928             dur = default_duration;
3929         ret->p_sample_delta_dts[i] = dur;
3930         chunk_duration += dur;
3931
3932         ret->p_sample_count_dts[i] = ret->p_sample_count_pts[i] = 1;
3933
3934         if( ret->p_sample_offset_pts )
3935         {
3936             ret->p_sample_offset_pts[i] =
3937                         p_trun_data->p_samples[i].i_composition_time_offset;
3938         }
3939
3940         if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_SIZE )
3941             len = ret->p_sample_size[i] = p_trun_data->p_samples[i].i_size;
3942         else
3943             len = ret->p_sample_size[i] = default_size;
3944
3945         if ( chunk_size + len > ( i_mdatlen - 8 ) )
3946             return VLC_EGENERIC;
3947
3948         ret->p_sample_data[i] = malloc( len );
3949         if( ret->p_sample_data[i] == NULL )
3950             return VLC_ENOMEM;
3951         uint32_t i_read = stream_ReadU32( p_demux->s, ret->p_sample_data[i], len );
3952         if( i_read < len )
3953             return VLC_EGENERIC;
3954         chunk_size += len;
3955     }
3956     ret->i_last_dts = ret->i_first_dts + chunk_duration - dur;
3957     p_track->i_first_dts = chunk_duration + ret->i_first_dts;
3958
3959     if( p_track->b_codec_need_restart &&
3960             p_track->fmt.i_cat == VIDEO_ES )
3961         ReInitDecoder( p_demux, p_track );
3962
3963     /* Skip if we didn't reach the end of mdat box */
3964     if ( chunk_size < (i_mdatlen - 8) )
3965         stream_ReadU32( p_demux->s, NULL, i_mdatlen - chunk_size - 8 );
3966
3967     p_track->b_has_non_empty_cchunk = true;
3968     return VLC_SUCCESS;
3969 }
3970
3971 /**
3972  * Get the next chunk of the track identified by i_tk_id.
3973  * \Note We don't want to seek all the time, so if the first chunk given by the
3974  * input method doesn't belong to the right track, we don't throw it away,
3975  * and so, in general, this function fetch more than one chunk.
3976  * Not to mention that a new init segment might be put everywhere
3977  * between two chunks by the input method.
3978  */
3979 static int MP4_frg_GetChunks( demux_t *p_demux, const unsigned i_tk_id )
3980 {
3981     demux_sys_t *p_sys = p_demux->p_sys;
3982     mp4_track_t *p_track;
3983
3984     for( unsigned i = 0; i < p_sys->i_tracks; i++ )
3985     {
3986         MP4_Box_t *p_chunk = MP4_BoxGetNextChunk( p_demux->s );
3987         if( !p_chunk )
3988             return VLC_EGENERIC;
3989
3990         if( !p_chunk->p_first )
3991             goto MP4_frg_GetChunks_Error;
3992         uint32_t i_type = p_chunk->p_first->i_type;
3993         uint32_t tid = 0;
3994         if( i_type == ATOM_uuid || i_type == ATOM_ftyp )
3995         {
3996             MP4_BoxFree( p_demux->s, p_sys->p_root );
3997             p_sys->p_root = p_chunk;
3998
3999             if( i_type == ATOM_ftyp ) /* DASH */
4000             {
4001                 MP4_Box_t *p_tkhd = MP4_BoxGet( p_chunk, "/moov/trak[0]/tkhd" );
4002                 if( !p_tkhd )
4003                 {
4004                     msg_Warn( p_demux, "No tkhd found!" );
4005                     goto MP4_frg_GetChunks_Error;
4006                 }
4007                 tid = BOXDATA(p_tkhd)->i_track_ID;
4008             }
4009             else                      /* Smooth Streaming */
4010             {
4011                 assert( !CmpUUID( &p_chunk->p_first->i_uuid, &SmooBoxUUID ) );
4012                 MP4_Box_t *p_stra = MP4_BoxGet( p_chunk, "/uuid/uuid[0]" );
4013                 if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
4014                 {
4015                     msg_Warn( p_demux, "No StraBox found!" );
4016                     goto MP4_frg_GetChunks_Error;
4017                 }
4018                 tid = BOXDATA(p_stra)->i_track_ID;
4019             }
4020
4021             p_track = MP4_frg_GetTrackByID( p_demux, tid );
4022             if( !p_track )
4023                 goto MP4_frg_GetChunks_Error;
4024             p_track->b_codec_need_restart = true;
4025
4026             return MP4_frg_GetChunks( p_demux, i_tk_id );
4027         }
4028
4029         if( MP4_frg_GetChunk( p_demux, p_chunk, &tid ) != VLC_SUCCESS )
4030             goto MP4_frg_GetChunks_Error;
4031
4032         MP4_BoxFree( p_demux->s, p_chunk );
4033
4034         if( tid == i_tk_id )
4035             break;
4036         else
4037             continue;
4038
4039 MP4_frg_GetChunks_Error:
4040         MP4_BoxFree( p_demux->s, p_chunk );
4041         return VLC_EGENERIC;
4042     }
4043
4044     return VLC_SUCCESS;
4045 }
4046
4047 static int MP4_frg_TrackSelect( demux_t *p_demux, mp4_track_t *p_track )
4048 {
4049     if( !p_track->b_ok || p_track->b_chapter )
4050     {
4051         return VLC_EGENERIC;
4052     }
4053
4054     if( p_track->b_selected )
4055     {
4056         msg_Warn( p_demux, "track[Id 0x%x] already selected", p_track->i_track_ID );
4057         return VLC_SUCCESS;
4058     }
4059
4060     msg_Dbg( p_demux, "Select track id %u", p_track->i_track_ID );
4061     p_track->b_selected = true;
4062     return VLC_SUCCESS;
4063 }
4064
4065
4066 /* Keeps an ordered chain of all fragments */
4067 static bool AddFragment( demux_t *p_demux, MP4_Box_t *p_moox )
4068 {
4069     demux_sys_t *p_sys = p_demux->p_sys;
4070     mp4_fragment_t *p_base_fragment = & p_sys->moovfragment;
4071     if ( !p_moox )
4072         return false;
4073
4074     if( p_moox->i_type == ATOM_moov )
4075     {
4076         if ( !p_sys->moovfragment.p_moox )
4077         {
4078             p_sys->moovfragment.p_moox = p_moox;
4079
4080             MP4_Box_t *p_mvhd;
4081             if( (p_mvhd = MP4_BoxGet( p_moox, "mvhd" )) )
4082             {
4083                 p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
4084                 p_sys->moovfragment.i_duration = BOXDATA(p_mvhd)->i_duration;
4085             }
4086             msg_Dbg( p_demux, "added fragment %4.4s", (char*) &p_moox->i_type );
4087             return true;
4088         }
4089         return false;
4090     }
4091     else // p_moox->i_type == ATOM_moof
4092     {
4093         assert(p_moox->i_type == ATOM_moof);
4094         mp4_fragment_t *p_fragment = p_sys->moovfragment.p_next;
4095         while ( p_fragment )
4096         {
4097             if ( !p_base_fragment->p_moox || p_moox->i_pos > p_base_fragment->p_moox->i_pos )
4098             {
4099                 p_base_fragment = p_fragment;
4100                 p_fragment = p_fragment->p_next;
4101             }
4102             else if ( p_moox->i_pos == p_base_fragment->p_moox->i_pos )
4103             {
4104                 /* already exists */
4105                 return false;
4106             }
4107         }
4108     }
4109
4110     /* Add the moof fragment */
4111     mp4_fragment_t *p_new = malloc(sizeof(mp4_fragment_t));
4112     if ( !p_new ) return false;
4113     p_new->p_moox = p_moox;
4114     p_new->i_duration = 0;
4115     p_new->p_next = p_base_fragment->p_next;
4116     p_base_fragment->p_next = p_new;
4117     msg_Dbg( p_demux, "added fragment %4.4s", (char*) &p_moox->i_type );
4118
4119     /* we have to probe all fragments :/ */
4120     uint64_t i_traf_base_data_offset = 0;
4121     uint64_t i_traf_min_offset = 0;
4122     uint32_t i_traf = 0;
4123     uint32_t i_traf_total_size = 0;
4124     uint32_t i_trafs_total_size = 0;
4125
4126     MP4_Box_t *p_traf = MP4_BoxGet( p_new->p_moox, "traf" );
4127     while ( p_traf )
4128     {
4129         if ( p_traf->i_type != ATOM_traf )
4130         {
4131            p_traf = p_traf->p_next;
4132            continue;
4133         }
4134         const MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
4135         const MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun" );
4136         if ( !p_tfhd || !p_trun )
4137         {
4138            p_traf = p_traf->p_next;
4139            continue;
4140         }
4141
4142         uint32_t i_track_timescale = 0;
4143         uint32_t i_track_defaultsamplesize = 0;
4144         uint32_t i_track_defaultsampleduration = 0;
4145         if ( p_sys->b_smooth )
4146         {
4147             /* stra sets identical timescales */
4148             i_track_timescale = p_sys->i_timescale;
4149             i_track_defaultsamplesize = 1;
4150             i_track_defaultsampleduration = 1;
4151         }
4152         else
4153         {
4154             /* set trex for defaults */
4155              MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_sys->moovfragment.p_moox, BOXDATA(p_tfhd)->i_track_ID );
4156              if ( p_trex )
4157              {
4158                 i_track_defaultsamplesize = BOXDATA(p_trex)->i_default_sample_size;
4159                 i_track_defaultsampleduration = BOXDATA(p_trex)->i_default_sample_duration;
4160              }
4161              MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->moovfragment.p_moox, BOXDATA(p_tfhd)->i_track_ID );
4162              if ( p_trak )
4163              {
4164                 MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
4165                 if ( p_mdhd ) i_track_timescale = BOXDATA(p_mdhd)->i_timescale;
4166              }
4167         }
4168
4169         if ( !i_track_timescale )
4170         {
4171            p_traf = p_traf->p_next;
4172            continue;
4173         }
4174
4175         if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
4176         {
4177             i_traf_base_data_offset = BOXDATA(p_tfhd)->i_base_data_offset;
4178         }
4179         else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DEFAULT_BASE_IS_MOOF )
4180         {
4181             i_traf_base_data_offset = p_new->p_moox->i_pos + 8;
4182         }
4183         else
4184         {
4185             if ( i_traf == 0 )
4186                 i_traf_base_data_offset = p_new->p_moox->i_pos /*+ 8*/;
4187             else
4188                 i_traf_base_data_offset += i_traf_total_size;
4189         }
4190
4191         i_traf_total_size = 0;
4192
4193         uint64_t i_trun_data_offset = i_traf_base_data_offset;
4194         uint64_t i_traf_duration = 0;
4195         uint32_t i_trun_size = 0;
4196         while ( p_trun && p_tfhd )
4197         {
4198             if ( p_trun->i_type != ATOM_trun )
4199             {
4200                p_trun = p_trun->p_next;
4201                continue;
4202             }
4203             const MP4_Box_data_trun_t *p_trundata = p_trun->data.p_trun;
4204
4205             /* Get data offset */
4206             if ( p_trundata->i_flags & MP4_TRUN_DATA_OFFSET )
4207                 i_trun_data_offset += __MAX( p_trundata->i_data_offset, 0 );
4208             else
4209                 i_trun_data_offset += i_trun_size;
4210
4211             i_trun_size = 0;
4212
4213             /* Sum total time */
4214             if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_DURATION )
4215             {
4216                 for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
4217                     i_traf_duration += p_trundata->p_samples[i].i_duration;
4218             }
4219             else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
4220             {
4221                 i_traf_duration += p_trundata->i_sample_count *
4222                         BOXDATA(p_tfhd)->i_default_sample_duration;
4223             }
4224             else
4225             {
4226                 i_traf_duration += p_trundata->i_sample_count *
4227                         i_track_defaultsampleduration;
4228             }
4229
4230             /* Get total traf size */
4231             if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_SIZE )
4232             {
4233                 for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
4234                     i_trun_size += p_trundata->p_samples[i].i_size;
4235             }
4236             else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
4237             {
4238                 i_trun_size += p_trundata->i_sample_count *
4239                         BOXDATA(p_tfhd)->i_default_sample_size;
4240             }
4241             else
4242             {
4243                 i_trun_size += p_trundata->i_sample_count *
4244                         i_track_defaultsamplesize;
4245             }
4246
4247             i_traf_total_size += i_trun_size;
4248
4249             if ( i_traf_min_offset )
4250                 i_traf_min_offset = __MIN( i_trun_data_offset, i_traf_min_offset );
4251             else
4252                 i_traf_min_offset = i_trun_data_offset;
4253
4254             p_trun = p_trun->p_next;
4255         }
4256
4257         i_trafs_total_size += i_traf_total_size;
4258         p_new->i_duration = __MAX( p_new->i_duration, i_traf_duration * p_sys->i_timescale / i_track_timescale );
4259
4260         p_traf = p_traf->p_next;
4261         i_traf++;
4262     }
4263
4264     p_new->i_chunk_range_min_offset = i_traf_min_offset;
4265     p_new->i_chunk_range_max_offset = i_traf_min_offset + i_trafs_total_size;
4266
4267     msg_Dbg( p_demux, "new fragment is %"PRId64" %"PRId64, p_new->i_chunk_range_min_offset, p_new->i_chunk_range_max_offset );
4268
4269     /* compute total duration with that new fragment if no overall provided */
4270     MP4_Box_t *p_mehd = MP4_BoxGet( p_sys->moovfragment.p_moox, "mvex/mehd");
4271     if ( !p_mehd )
4272     {
4273         p_sys->i_overall_duration = 0;
4274         p_new = &p_sys->moovfragment;
4275         while ( p_new )
4276         {
4277             p_sys->i_overall_duration += p_new->i_duration;
4278             p_new = p_new->p_next;
4279         }
4280     }
4281     else
4282         p_sys->i_overall_duration = BOXDATA(p_mehd)->i_fragment_duration;
4283
4284     msg_Dbg( p_demux, "total fragments duration %"PRId64, CLOCK_FREQ * p_sys->i_overall_duration / p_sys->i_timescale);
4285     return true;
4286 }
4287
4288 static int ProbeFragments( demux_t *p_demux )
4289 {
4290     demux_sys_t *p_sys = p_demux->p_sys;
4291     uint64_t i_current_pos;
4292
4293     if ( MP4_stream_Tell( p_demux->s, &i_current_pos ) )
4294         msg_Dbg( p_demux, "probing fragments from %"PRId64, i_current_pos );
4295
4296     assert( p_sys->p_root );
4297
4298     MP4_ReadBoxContainerRaw( p_demux->s, p_sys->p_root ); /* Get the rest of the file */
4299
4300     MP4_Box_t *p_moov = MP4_BoxGet( p_sys->p_root, "/moov" );
4301     if ( !p_moov )
4302     {
4303         MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
4304         return VLC_EGENERIC;
4305     }
4306     AddFragment( p_demux, p_moov );
4307
4308     MP4_Box_t *p_moof = MP4_BoxGet( p_sys->p_root, "moof" );
4309     while ( p_moof )
4310     {
4311         if ( p_moof->i_type == ATOM_moof )
4312             AddFragment( p_demux, p_moof );
4313         p_moof = p_moof->p_next;
4314     }
4315
4316     p_sys->b_fragments_probed = true;
4317
4318     MP4_Box_t *p_mdat = MP4_BoxGet( p_sys->p_root, "mdat" );
4319     assert( p_mdat );
4320     stream_Seek( p_demux->s, p_mdat->i_pos );
4321     msg_Dbg( p_demux, "rewinding to mdat %"PRId64, p_mdat->i_pos );
4322
4323     return VLC_SUCCESS;
4324 }
4325
4326 /**
4327  * DemuxFrg: read packet and send them to decoders
4328  * \return 1 on success, 0 on error.
4329  * TODO check for newly selected track
4330  */
4331 int DemuxFrg( demux_t *p_demux )
4332 {
4333     demux_sys_t *p_sys = p_demux->p_sys;
4334     unsigned i_track;
4335     unsigned i_track_selected;
4336
4337     /* check for newly selected/unselected track */
4338     for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
4339     {
4340         mp4_track_t *tk = &p_sys->track[i_track];
4341         bool b;
4342
4343         if( !tk->b_ok || tk->b_chapter )
4344             continue;
4345
4346         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
4347         msg_Dbg( p_demux, "track %u %s!", tk->i_track_ID, b ? "enabled" : "disabled" );
4348
4349         if( tk->b_selected && !b )
4350         {
4351             MP4_TrackUnselect( p_demux, tk );
4352         }
4353         else if( !tk->b_selected && b)
4354         {
4355             MP4_frg_TrackSelect( p_demux, tk );
4356         }
4357
4358         if( tk->b_selected )
4359             i_track_selected++;
4360     }
4361
4362     if( i_track_selected <= 0 )
4363     {
4364         p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
4365         if( p_sys->i_timescale > 0 )
4366         {
4367             int64_t i_length = CLOCK_FREQ *
4368                                (mtime_t)p_sys->moovfragment.i_duration /
4369                                (mtime_t)p_sys->i_timescale;
4370             if( MP4_GetMoviePTS( p_sys ) >= i_length )
4371                 return 0;
4372             return 1;
4373         }
4374
4375         msg_Warn( p_demux, "no track selected, exiting..." );
4376         return 0;
4377     }
4378
4379     /* first wait for the good time to read a packet */
4380     es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
4381
4382     p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
4383
4384     /* we will read 100ms for each stream so ...*/
4385     p_sys->i_time += __MAX( p_sys->i_timescale / 10, 1 );
4386
4387     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
4388     {
4389         mp4_track_t *tk = &p_sys->track[i_track];
4390
4391         if( !tk->b_ok || tk->b_chapter || !tk->b_selected )
4392         {
4393             msg_Warn( p_demux, "Skipping track id %u", tk->i_track_ID );
4394             continue;
4395         }
4396
4397         if( !tk->b_has_non_empty_cchunk )
4398         {
4399             if( MP4_frg_GetChunks( p_demux, tk->i_track_ID ) != VLC_SUCCESS )
4400             {
4401                 msg_Info( p_demux, "MP4_frg_GetChunks returned error. End of stream?" );
4402                 return 0;
4403             }
4404         }
4405
4406         while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
4407         {
4408             block_t *p_block;
4409             int64_t i_delta;
4410
4411             mp4_chunk_t *ck = tk->cchunk;
4412             if( ck->i_sample >= ck->i_sample_count )
4413             {
4414                 msg_Err( p_demux, "sample %"PRIu32" of %"PRIu32"",
4415                                     ck->i_sample, ck->i_sample_count );
4416                 return 0;
4417             }
4418
4419             uint32_t sample_size = ck->p_sample_size[ck->i_sample];
4420             p_block = block_Alloc( sample_size );
4421             uint8_t *src = ck->p_sample_data[ck->i_sample];
4422             memcpy( p_block->p_buffer, src, sample_size );
4423
4424             ck->i_sample++;
4425             if( ck->i_sample == ck->i_sample_count )
4426                 tk->b_has_non_empty_cchunk = false;
4427
4428             /* dts */
4429             p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
4430             /* pts */
4431             i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
4432             if( i_delta != -1 )
4433                 p_block->i_pts = p_block->i_dts + i_delta;
4434             else if( tk->fmt.i_cat != VIDEO_ES )
4435                 p_block->i_pts = p_block->i_dts;
4436             else
4437                 p_block->i_pts = VLC_TS_INVALID;
4438
4439             es_out_Send( p_demux->out, tk->p_es, p_block );
4440
4441             tk->i_sample++;
4442
4443             if( !tk->b_has_non_empty_cchunk )
4444                 break;
4445         }
4446     }
4447     return 1;
4448 }
4449
4450 #undef BOXDATA