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