556700f06a4fdf06e46c9505f2d038e73013ac88
[vlc.git] / 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 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "mp4.h"
30
31 #include <vlc_demux.h>
32 #include <vlc_charset.h>                           /* EnsureUTF8 */
33 #include <vlc_input.h>
34 #include <vlc_aout.h>
35 #include <vlc_plugin.h>
36 #include <vlc_dialog.h>
37 #include <vlc_url.h>
38 #include <assert.h>
39 #include <limits.h>
40 #include "../codec/cc.h"
41 #include "heif.h"
42 #include "../av1_unpack.h"
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 static int  Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
49
50 #define CFG_PREFIX "mp4-"
51
52 #define MP4_M4A_TEXT     N_("M4A audio only")
53 #define MP4_M4A_LONGTEXT N_("Ignore non audio tracks from iTunes audio files")
54
55 #define HEIF_DURATION_TEXT N_("Duration in seconds")
56 #define HEIF_DURATION_LONGTEXT N_( \
57     "Duration in seconds before simulating an end of file. " \
58     "A negative value means an unlimited play time.")
59
60 vlc_module_begin ()
61     set_category( CAT_INPUT )
62     set_subcategory( SUBCAT_INPUT_DEMUX )
63     set_description( N_("MP4 stream demuxer") )
64     set_shortname( N_("MP4") )
65     set_capability( "demux", 240 )
66     set_callbacks( Open, Close )
67
68     add_category_hint("Hacks", NULL)
69     add_bool( CFG_PREFIX"m4a-audioonly", false, MP4_M4A_TEXT, MP4_M4A_LONGTEXT, true )
70
71     add_submodule()
72         set_category( CAT_INPUT )
73         set_subcategory( SUBCAT_INPUT_DEMUX )
74         set_description( N_("HEIF demuxer") )
75         set_shortname( "heif" )
76         set_capability( "demux", 239 )
77         set_callbacks( OpenHEIF, CloseHEIF )
78         set_section( N_("HEIF demuxer"), NULL )
79         add_float( "heif-image-duration", HEIF_DEFAULT_DURATION,
80                    HEIF_DURATION_TEXT, HEIF_DURATION_LONGTEXT, false )
81             change_safe()
82 vlc_module_end ()
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 static int   Demux   ( demux_t * );
88 static int   DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
89 static int   DemuxFrag( demux_t * );
90 static int   Control ( demux_t *, int, va_list );
91
92 typedef struct
93 {
94     MP4_Box_t    *p_root;      /* container for the whole file */
95
96     vlc_tick_t   i_pcr;
97
98     uint64_t     i_moov_duration;
99     uint64_t     i_duration;           /* Declared fragmented duration (movie time scale) */
100     uint64_t     i_cumulated_duration; /* Same as above, but not from probing, (movie time scale) */
101     uint32_t     i_timescale;          /* movie time scale */
102     vlc_tick_t   i_nztime;             /* time position of the presentation (CLOCK_FREQ timescale) */
103     unsigned int i_tracks;       /* number of tracks */
104     mp4_track_t  *track;         /* array of track */
105     float        f_fps;          /* number of frame per seconds */
106
107     bool         b_fragmented;   /* fMP4 */
108     bool         b_seekable;
109     bool         b_fastseekable;
110     bool         b_error;        /* unrecoverable */
111
112     bool            b_index_probed;     /* mFra sync points index */
113     bool            b_fragments_probed; /* moof segments index created */
114
115     MP4_Box_t *p_moov;
116
117     struct
118     {
119         uint32_t        i_current_box_type;
120         MP4_Box_t      *p_fragment_atom;
121         uint64_t        i_post_mdat_offset;
122         uint32_t        i_lastseqnumber;
123     } context;
124
125     /* */
126     MP4_Box_t    *p_tref_chap;
127
128     /* */
129     bool seekpoint_changed;
130     int          i_seekpoint;
131     input_title_t *p_title;
132     vlc_meta_t    *p_meta;
133
134     /* ASF in MP4 */
135     asf_packet_sys_t asfpacketsys;
136     vlc_tick_t i_preroll;       /* foobar */
137     vlc_tick_t i_preroll_start;
138
139     struct
140     {
141         int es_cat_filters;
142     } hacks;
143
144     mp4_fragments_index_t *p_fragsindex;
145 } demux_sys_t;
146
147 #define DEMUX_INCREMENT VLC_TICK_FROM_MS(250) /* How far the pcr will go, each round */
148 #define DEMUX_TRACK_MAX_PRELOAD VLC_TICK_FROM_SEC(15) /* maximum preloading, to deal with interleaving */
149
150 #define INVALID_PRELOAD  UINT_MAX
151
152 #define VLC_DEMUXER_EOS (VLC_DEMUXER_EGENERIC - 1)
153 #define VLC_DEMUXER_FATAL (VLC_DEMUXER_EGENERIC - 2)
154
155 const uint32_t rgi_pict_atoms[2] = { ATOM_PICT, ATOM_pict };
156 const char *psz_meta_roots[] = { "/moov/udta/meta/ilst",
157                                  "/moov/meta/ilst",
158                                  "/moov/udta/meta",
159                                  "/moov/udta",
160                                  "/meta/ilst",
161                                  "/udta",
162                                  NULL };
163
164 /*****************************************************************************
165  * Declaration of local function
166  *****************************************************************************/
167 static void MP4_TrackSetup( demux_t *, mp4_track_t *, MP4_Box_t  *, bool, bool );
168 static void MP4_TrackInit( mp4_track_t * );
169 static void MP4_TrackClean( es_out_t *, mp4_track_t * );
170
171 static void MP4_Block_Send( demux_t *, mp4_track_t *, block_t * );
172
173 static void MP4_TrackSelect  ( demux_t *, mp4_track_t *, bool );
174 static int  MP4_TrackSeek   ( demux_t *, mp4_track_t *, vlc_tick_t );
175
176 static uint64_t MP4_TrackGetPos    ( mp4_track_t * );
177 static uint32_t MP4_TrackGetReadSize( mp4_track_t *, uint32_t * );
178 static int      MP4_TrackNextSample( demux_t *, mp4_track_t *, uint32_t );
179 static void     MP4_TrackSetELST( demux_t *, mp4_track_t *, vlc_tick_t );
180
181 static void     MP4_UpdateSeekpoint( demux_t *, vlc_tick_t );
182
183 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id );
184 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t *p_moov,
185                                            const MP4_Box_data_tfhd_t *p_tfhd_data,
186                                            uint32_t *pi_default_size,
187                                            uint32_t *pi_default_duration );
188
189 static stime_t GetMoovTrackDuration( demux_sys_t *p_sys, unsigned i_track_ID );
190
191 static int  ProbeFragments( demux_t *p_demux, bool b_force, bool *pb_fragmented );
192 static int  ProbeIndex( demux_t *p_demux );
193
194 static int FragCreateTrunIndex( demux_t *, MP4_Box_t *, MP4_Box_t *, stime_t );
195
196 static int FragGetMoofBySidxIndex( demux_t *p_demux, vlc_tick_t i_target_time,
197                                    uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime );
198 static int FragGetMoofByTfraIndex( demux_t *p_demux, const vlc_tick_t i_target_time, unsigned i_track_ID,
199                                    uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime );
200 static void FragResetContext( demux_sys_t * );
201
202 /* ASF Handlers */
203 static asf_track_info_t * MP4ASF_GetTrackInfo( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number );
204 static void MP4ASF_Send(asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, block_t **pp_frame);
205 static void MP4ASF_ResetFrames( demux_sys_t *p_sys );
206
207 /* RTP Hint track */
208 static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec );
209 static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount );
210
211 static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta );
212
213 /* Helpers */
214
215 static int64_t MP4_rescale( int64_t i_value, uint32_t i_timescale, uint32_t i_newscale )
216 {
217     if( i_timescale == i_newscale )
218         return i_value;
219
220     if( i_value <= INT64_MAX / i_newscale )
221         return i_value * i_newscale / i_timescale;
222
223     /* overflow */
224     int64_t q = i_value / i_timescale;
225     int64_t r = i_value % i_timescale;
226     return q * i_newscale + r * i_newscale / i_timescale;
227 }
228
229 static vlc_tick_t MP4_rescale_mtime( int64_t i_value, uint32_t i_timescale )
230 {
231     return MP4_rescale(i_value, i_timescale, CLOCK_FREQ);
232 }
233
234 static int64_t MP4_rescale_qtime( vlc_tick_t i_value, uint32_t i_timescale )
235 {
236     return MP4_rescale(i_value, CLOCK_FREQ, i_timescale);
237 }
238
239 static uint32_t stream_ReadU32( stream_t *s, void *p_read, uint32_t i_toread )
240 {
241     ssize_t i_return = 0;
242     if ( i_toread > INT32_MAX )
243     {
244         i_return = vlc_stream_Read( s, p_read, (size_t) INT32_MAX );
245         if ( i_return < INT32_MAX )
246             return i_return;
247         else
248             i_toread -= INT32_MAX;
249     }
250     i_return += vlc_stream_Read( s, (uint8_t *)p_read + i_return, (size_t) i_toread );
251     return i_return;
252 }
253
254 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
255 {
256     if(!p_moov)
257         return NULL;
258     MP4_Box_t *p_trex = MP4_BoxGet( p_moov, "mvex/trex" );
259     while( p_trex )
260     {
261         if ( p_trex->i_type == ATOM_trex &&
262              BOXDATA(p_trex) && BOXDATA(p_trex)->i_track_ID == i_id )
263                 break;
264         else
265             p_trex = p_trex->p_next;
266     }
267     return p_trex;
268 }
269
270 /**
271  * Return the track identified by tid
272  */
273 static mp4_track_t *MP4_GetTrackByTrackID( demux_t *p_demux, const uint32_t tid )
274 {
275     demux_sys_t *p_sys = p_demux->p_sys;
276
277     mp4_track_t *ret = NULL;
278     for( unsigned i = 0; i < p_sys->i_tracks; i++ )
279     {
280         ret = &p_sys->track[i];
281         if( ret->i_track_ID == tid )
282             return ret;
283     }
284     return NULL;
285 }
286
287 static MP4_Box_t * MP4_GetTrakByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
288 {
289     MP4_Box_t *p_trak = MP4_BoxGet( p_moov, "trak" );
290     MP4_Box_t *p_tkhd;
291     while( p_trak )
292     {
293         if( p_trak->i_type == ATOM_trak &&
294             (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) && BOXDATA(p_tkhd) &&
295             BOXDATA(p_tkhd)->i_track_ID == i_id )
296                 break;
297         else
298             p_trak = p_trak->p_next;
299     }
300     return p_trak;
301 }
302
303 static MP4_Box_t * MP4_GetTrafByTrackID( MP4_Box_t *p_moof, const uint32_t i_id )
304 {
305     MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
306     MP4_Box_t *p_tfhd;
307     while( p_traf )
308     {
309         if( p_traf->i_type == ATOM_traf &&
310             (p_tfhd = MP4_BoxGet( p_traf, "tfhd" )) && BOXDATA(p_tfhd) &&
311             BOXDATA(p_tfhd)->i_track_ID == i_id )
312                 break;
313         else
314             p_traf = p_traf->p_next;
315     }
316     return p_traf;
317 }
318
319 static es_out_id_t * MP4_AddTrackES( es_out_t *out, mp4_track_t *p_track )
320 {
321     es_out_id_t *p_es = es_out_Add( out, &p_track->fmt );
322     /* Force SPU which isn't selected/defaulted */
323     if( p_track->fmt.i_cat == SPU_ES && p_es && p_track->b_forced_spu )
324         es_out_Control( out, ES_OUT_SET_ES_DEFAULT, p_es );
325
326     return p_es;
327 }
328
329 /* Return time in microsecond of a track */
330 static inline vlc_tick_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
331 {
332     demux_sys_t *p_sys = p_demux->p_sys;
333     const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
334
335     unsigned int i_index = 0;
336     unsigned int i_sample = p_track->i_sample - p_chunk->i_sample_first;
337     int64_t sdts = p_chunk->i_first_dts;
338
339     while( i_sample > 0 && i_index < p_chunk->i_entries_dts )
340     {
341         if( i_sample > p_chunk->p_sample_count_dts[i_index] )
342         {
343             sdts += p_chunk->p_sample_count_dts[i_index] *
344                 p_chunk->p_sample_delta_dts[i_index];
345             i_sample -= p_chunk->p_sample_count_dts[i_index];
346             i_index++;
347         }
348         else
349         {
350             sdts += i_sample * p_chunk->p_sample_delta_dts[i_index];
351             break;
352         }
353     }
354
355     vlc_tick_t i_dts = MP4_rescale_mtime( sdts, p_track->i_timescale );
356
357     /* now handle elst */
358     if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count )
359     {
360         MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
361
362         /* convert to offset */
363         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
364               elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
365             elst->i_media_time[p_track->i_elst] > 0 )
366         {
367             i_dts -= MP4_rescale_mtime( elst->i_media_time[p_track->i_elst], p_track->i_timescale );
368         }
369
370         /* add i_elst_time */
371         i_dts += MP4_rescale_mtime( p_track->i_elst_time, p_sys->i_timescale );
372
373         if( i_dts < 0 ) i_dts = 0;
374     }
375
376     return i_dts;
377 }
378
379 static inline bool MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track,
380                                          vlc_tick_t *pi_delta )
381 {
382     VLC_UNUSED( p_demux );
383     mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
384
385     unsigned int i_index = 0;
386     unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
387
388     if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
389         return false;
390
391     for( i_index = 0; i_index < ck->i_entries_pts ; i_index++ )
392     {
393         if( i_sample < ck->p_sample_count_pts[i_index] )
394         {
395             *pi_delta = MP4_rescale_mtime( ck->p_sample_offset_pts[i_index],
396                                            p_track->i_timescale );
397             return true;
398         }
399
400         i_sample -= ck->p_sample_count_pts[i_index];
401     }
402     return false;
403 }
404
405 static inline vlc_tick_t MP4_GetSamplesDuration( demux_t *p_demux, mp4_track_t *p_track,
406                                               unsigned i_nb_samples )
407 {
408     VLC_UNUSED( p_demux );
409
410     const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
411     stime_t i_duration = 0;
412
413     /* Forward to right index, and set remaining count in that index */
414     unsigned i_index = 0;
415     unsigned i_remain = 0;
416     for( unsigned i = p_chunk->i_sample_first;
417          i<p_track->i_sample && i_index < p_chunk->i_entries_dts; )
418     {
419         if( p_track->i_sample - i >= p_chunk->p_sample_count_dts[i_index] )
420         {
421             i += p_chunk->p_sample_count_dts[i_index];
422             i_index++;
423         }
424         else
425         {
426             i_remain = p_track->i_sample - i;
427             break;
428         }
429     }
430
431     /* Compute total duration from all samples from index */
432     while( i_nb_samples > 0 && i_index < p_chunk->i_entries_dts )
433     {
434         if( i_nb_samples >= p_chunk->p_sample_count_dts[i_index] - i_remain )
435         {
436             i_duration += (p_chunk->p_sample_count_dts[i_index] - i_remain) *
437                           (int64_t) p_chunk->p_sample_delta_dts[i_index];
438             i_nb_samples -= (p_chunk->p_sample_count_dts[i_index] - i_remain);
439             i_index++;
440             i_remain = 0;
441         }
442         else
443         {
444             i_duration += i_nb_samples * p_chunk->p_sample_delta_dts[i_index];
445             break;
446         }
447     }
448
449     return MP4_rescale_mtime( i_duration, p_track->i_timescale );
450 }
451
452 static inline vlc_tick_t MP4_GetMoviePTS(demux_sys_t *p_sys )
453 {
454     return p_sys->i_nztime;
455 }
456
457 static void LoadChapter( demux_t  *p_demux );
458
459 static int LoadInitFrag( demux_t *p_demux )
460 {
461     demux_sys_t *p_sys = p_demux->p_sys;
462
463     /* Load all boxes ( except raw data ) */
464     if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
465     {
466         goto LoadInitFragError;
467     }
468
469     return VLC_SUCCESS;
470
471 LoadInitFragError:
472     msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" );
473     return VLC_EGENERIC;
474 }
475
476 static int CreateTracks( demux_t *p_demux, unsigned i_tracks )
477 {
478     demux_sys_t *p_sys = p_demux->p_sys;
479
480     if( SIZE_MAX / i_tracks < sizeof(mp4_track_t) )
481         return VLC_EGENERIC;
482
483     p_sys->track = vlc_alloc( i_tracks, sizeof(mp4_track_t)  );
484     if( p_sys->track == NULL )
485         return VLC_ENOMEM;
486     p_sys->i_tracks = i_tracks;
487
488     for( unsigned i=0; i<i_tracks; i++ )
489         MP4_TrackInit( &p_sys->track[i] );
490
491     return VLC_SUCCESS;
492 }
493
494 static block_t * MP4_EIA608_Convert( block_t * p_block )
495 {
496     /* Rebuild codec data from encap */
497     size_t i_copied = 0;
498     size_t i_remaining = __MIN(p_block->i_buffer, INT64_MAX / 3);
499     uint32_t i_bytes = 0;
500     block_t *p_newblock;
501
502     /* always need at least 10 bytes (atom size+header+1pair)*/
503     if ( i_remaining < 10 ||
504          !(i_bytes = GetDWBE(p_block->p_buffer)) ||
505          (i_bytes > i_remaining) ||
506          memcmp("cdat", &p_block->p_buffer[4], 4) ||
507          !(p_newblock = block_Alloc( i_remaining * 3 - 8 )) )
508     {
509         p_block->i_buffer = 0;
510         return p_block;
511     }
512
513     uint8_t *p_write = p_newblock->p_buffer;
514     uint8_t *p_read = &p_block->p_buffer[8];
515     i_bytes -= 8;
516     i_remaining -= 8;
517
518     do
519     {
520         p_write[i_copied++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
521         p_write[i_copied++] = p_read[0];
522         p_write[i_copied++] = p_read[1];
523         p_read += 2;
524         i_bytes -= 2;
525         i_remaining -= 2;
526     } while( i_bytes >= 2 );
527
528     /* cdt2 is optional */
529     if ( i_remaining >= 10 &&
530          (i_bytes = GetDWBE(p_read)) &&
531          (i_bytes <= i_remaining) &&
532          !memcmp("cdt2", &p_read[4], 4) )
533     {
534         p_read += 8;
535         i_bytes -= 8;
536         i_remaining -= 8;
537         do
538         {
539             p_write[i_copied++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
540             p_write[i_copied++] = p_read[0];
541             p_write[i_copied++] = p_read[1];
542             p_read += 2;
543             i_bytes -= 2;
544         } while( i_bytes >= 2 );
545     }
546
547     p_newblock->i_pts = p_block->i_dts;
548     p_newblock->i_buffer = i_copied;
549     p_newblock->i_flags = BLOCK_FLAG_TYPE_P;
550     block_Release( p_block );
551
552     return p_newblock;
553 }
554
555 static uint32_t MP4_TrackGetRunSeq( mp4_track_t *p_track )
556 {
557     if( p_track->i_chunk_count > 0 )
558         return p_track->chunk[p_track->i_chunk].i_virtual_run_number;
559     return 0;
560 }
561
562 /* Analyzes chunks to find max interleave length
563  * sets flat flag if no interleaving is in use */
564 static void MP4_GetInterleaving( demux_t *p_demux, vlc_tick_t *pi_max_contiguous, bool *pb_flat )
565 {
566     demux_sys_t *p_sys = p_demux->p_sys;
567     *pi_max_contiguous = 0;
568     *pb_flat = true;
569
570     /* Find first recorded chunk */
571     mp4_track_t *tk = NULL;
572     uint64_t i_duration = 0;
573     for( unsigned i=0; i < p_sys->i_tracks; i++ )
574     {
575         mp4_track_t *cur = &p_sys->track[i];
576         if( !cur->i_chunk_count )
577             continue;
578
579         if( tk == NULL || cur->chunk[0].i_offset < tk->chunk[0].i_offset )
580             tk = cur;
581     }
582
583     for( ; tk != NULL; )
584     {
585         i_duration += tk->chunk[tk->i_chunk].i_duration;
586         tk->i_chunk++;
587
588         /* Find next chunk in data order */
589         mp4_track_t *nexttk = NULL;
590         for( unsigned i=0; i < p_sys->i_tracks; i++ )
591         {
592             mp4_track_t *cur = &p_sys->track[i];
593             if( cur->i_chunk == cur->i_chunk_count )
594                 continue;
595
596             if( nexttk == NULL ||
597                 cur->chunk[cur->i_chunk].i_offset < nexttk->chunk[nexttk->i_chunk].i_offset )
598                 nexttk = cur;
599         }
600
601         /* copy previous run */
602         if( nexttk && nexttk->i_chunk > 0 )
603             nexttk->chunk[nexttk->i_chunk].i_virtual_run_number =
604                     nexttk->chunk[nexttk->i_chunk - 1].i_virtual_run_number;
605
606         if( tk != nexttk )
607         {
608             vlc_tick_t i_dur = MP4_rescale_mtime( i_duration, tk->i_timescale );
609             if( i_dur > *pi_max_contiguous )
610                 *pi_max_contiguous = i_dur;
611             i_duration = 0;
612
613             if( tk->i_chunk != tk->i_chunk_count )
614                 *pb_flat = false;
615
616             if( nexttk && nexttk->i_chunk > 0 ) /* new run number */
617                 nexttk->chunk[nexttk->i_chunk].i_virtual_run_number++;
618         }
619
620         tk = nexttk;
621     }
622
623     /* reset */
624     for( unsigned i=0; i < p_sys->i_tracks; i++ )
625         p_sys->track[i].i_chunk = 0;
626 }
627
628 static block_t * MP4_Block_Convert( demux_t *p_demux, const mp4_track_t *p_track, block_t *p_block )
629 {
630     /* might have some encap */
631     if( p_track->fmt.i_cat == SPU_ES )
632     {
633         switch( p_track->fmt.i_codec )
634         {
635             case VLC_CODEC_WEBVTT:
636             case VLC_CODEC_TTML:
637             case VLC_CODEC_TX3G:
638             case VLC_CODEC_SPU:
639             case VLC_CODEC_SUBT:
640             /* accept as-is */
641             break;
642             case VLC_CODEC_CEA608:
643                 p_block = MP4_EIA608_Convert( p_block );
644             break;
645         default:
646             p_block->i_buffer = 0;
647             break;
648         }
649     }
650     else if( p_track->fmt.i_codec == VLC_CODEC_AV1 )
651     {
652         p_block = AV1_Unpack_Sample( p_block );
653     }
654     else if( p_track->fmt.i_original_fourcc == ATOM_rrtp )
655     {
656         p_block = MP4_RTPHint_Convert( p_demux, p_block, p_track->fmt.i_codec );
657     }
658
659     return p_block;
660 }
661
662 static void MP4_Block_Send( demux_t *p_demux, mp4_track_t *p_track, block_t *p_block )
663 {
664     demux_sys_t *p_sys = p_demux->p_sys;
665
666     p_block = MP4_Block_Convert( p_demux, p_track, p_block );
667     if( p_block == NULL )
668         return;
669
670     if ( p_track->b_chans_reorder )
671     {
672         aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
673                              p_track->fmt.audio.i_channels,
674                              p_track->rgi_chans_reordering,
675                              p_track->fmt.i_codec );
676     }
677
678     p_block->i_flags |= p_track->i_block_flags;
679     if( p_track->i_next_block_flags )
680     {
681         p_block->i_flags |= p_track->i_next_block_flags;
682         p_track->i_next_block_flags = 0;
683     }
684
685     /* ASF packets in mov */
686     if( p_track->p_asf )
687     {
688         /* Fake a new stream from MP4 block */
689         stream_t *p_stream = p_demux->s;
690         p_demux->s = vlc_stream_MemoryNew( p_demux, p_block->p_buffer, p_block->i_buffer, true );
691         if ( p_demux->s )
692         {
693             p_track->i_dts_backup = p_block->i_dts;
694             p_track->i_pts_backup = p_block->i_pts;
695             /* And demux it as ASF packet */
696             DemuxASFPacket( &p_sys->asfpacketsys, p_block->i_buffer, p_block->i_buffer );
697             vlc_stream_Delete(p_demux->s);
698         }
699         block_Release(p_block);
700         p_demux->s = p_stream;
701     }
702     else
703         es_out_Send( p_demux->out, p_track->p_es, p_block );
704 }
705
706 int  OpenHEIF ( vlc_object_t * );
707 void CloseHEIF( vlc_object_t * );
708
709 /*****************************************************************************
710  * Open: check file and initializes MP4 structures
711  *****************************************************************************/
712 static int Open( vlc_object_t * p_this )
713 {
714     demux_t  *p_demux = (demux_t *)p_this;
715     demux_sys_t     *p_sys;
716
717     const uint8_t   *p_peek;
718
719     MP4_Box_t       *p_ftyp;
720     const MP4_Box_t *p_mvhd = NULL;
721     const MP4_Box_t *p_mvex = NULL;
722
723     bool      b_enabled_es;
724
725     /* A little test to see if it could be a mp4 */
726     if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 ) return VLC_EGENERIC;
727
728     switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
729     {
730         case ATOM_moov:
731         case ATOM_foov:
732         case ATOM_moof:
733         case ATOM_mdat:
734         case ATOM_udta:
735         case ATOM_free:
736         case ATOM_skip:
737         case ATOM_wide:
738         case ATOM_uuid:
739         case VLC_FOURCC( 'p', 'n', 'o', 't' ):
740             break;
741         case ATOM_ftyp:
742         {
743             /* Early handle some brands */
744             switch( VLC_FOURCC(p_peek[8], p_peek[9], p_peek[10], p_peek[11]) )
745             {
746                 /* HEIF pictures goes to heif demux */
747                 case BRAND_heic:
748                 case BRAND_heix:
749                 case BRAND_mif1:
750                 case BRAND_jpeg:
751                 case BRAND_avci:
752                 /* We don't yet support f4v, but avformat does. */
753                 case BRAND_f4v:
754                     return VLC_EGENERIC;
755                 default:
756                     break;
757             }
758             break;
759         }
760          default:
761             return VLC_EGENERIC;
762     }
763
764     /* create our structure that will contains all data */
765     p_sys = calloc( 1, sizeof( demux_sys_t ) );
766     if ( !p_sys )
767         return VLC_EGENERIC;
768
769     /* I need to seek */
770     vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
771     if( p_sys->b_seekable )
772         vlc_stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable );
773
774     /*Set exported functions */
775     p_demux->pf_demux = Demux;
776     p_demux->pf_control = Control;
777
778     p_sys->context.i_lastseqnumber = UINT32_MAX;
779
780     p_demux->p_sys = p_sys;
781
782     if( LoadInitFrag( p_demux ) != VLC_SUCCESS )
783         goto error;
784
785     MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
786
787     if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
788     {
789         switch( BOXDATA(p_ftyp)->i_major_brand )
790         {
791             case BRAND_isom:
792                 msg_Dbg( p_demux,
793                          "ISO Media (isom) version %d.",
794                          BOXDATA(p_ftyp)->i_minor_version );
795                 break;
796             case BRAND_3gp4:
797             case BRAND_3gp5:
798             case BRAND_3gp6:
799             case BRAND_3gp7:
800                 msg_Dbg( p_demux, "3GPP Media Release: %4.4s",
801                          (char *)&BOXDATA(p_ftyp)->i_major_brand );
802                 break;
803             case BRAND_qt__:
804                 msg_Dbg( p_demux, "Apple QuickTime media" );
805                 break;
806             case BRAND_isml:
807                 msg_Dbg( p_demux, "PIFF (= isml = fMP4) media" );
808                 break;
809             case BRAND_dash:
810                 msg_Dbg( p_demux, "DASH Stream" );
811                 break;
812             case BRAND_M4A:
813                 msg_Dbg( p_demux, "iTunes audio" );
814                 if( var_InheritBool( p_demux, CFG_PREFIX"m4a-audioonly" ) )
815                     p_sys->hacks.es_cat_filters = AUDIO_ES;
816                 break;
817             default:
818                 msg_Dbg( p_demux,
819                          "unrecognized major media specification (%4.4s).",
820                           (char*)&BOXDATA(p_ftyp)->i_major_brand );
821                 break;
822         }
823         /* also lookup in compatibility list */
824         for(uint32_t i=0; i<BOXDATA(p_ftyp)->i_compatible_brands_count; i++)
825         {
826             if (BOXDATA(p_ftyp)->i_compatible_brands[i] == BRAND_dash)
827             {
828                 msg_Dbg( p_demux, "DASH Stream" );
829             }
830             else if (BOXDATA(p_ftyp)->i_compatible_brands[i] == BRAND_smoo)
831             {
832                 msg_Dbg( p_demux, "Handling VLC Smooth Stream" );
833             }
834         }
835     }
836     else
837     {
838         msg_Dbg( p_demux, "file type box missing (assuming ISO Media)" );
839     }
840
841     /* the file need to have one moov box */
842     p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/moov" );
843     if( unlikely(!p_sys->p_moov) )
844     {
845         p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/foov" );
846         if( !p_sys->p_moov )
847         {
848             msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
849             goto error;
850         }
851         /* we have a free box as a moov, rename it */
852         p_sys->p_moov->i_type = ATOM_moov;
853     }
854
855     p_mvhd = MP4_BoxGet( p_sys->p_moov, "mvhd" );
856     if( p_mvhd && BOXDATA(p_mvhd) && BOXDATA(p_mvhd)->i_timescale )
857     {
858         p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
859         p_sys->i_moov_duration = p_sys->i_duration = BOXDATA(p_mvhd)->i_duration;
860         p_sys->i_cumulated_duration = BOXDATA(p_mvhd)->i_duration;
861     }
862     else
863     {
864         msg_Warn( p_demux, "No valid mvhd found" );
865         goto error;
866     }
867
868     MP4_Box_t *p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" );
869     if( p_rmra != NULL && p_demux->p_input_item != NULL )
870     {
871         int        i_count = MP4_BoxCount( p_rmra, "rmda" );
872         int        i;
873
874         msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
875
876         input_item_t *p_current = p_demux->p_input_item;
877
878         input_item_node_t *p_subitems = input_item_node_Create( p_current );
879
880         for( i = 0; i < i_count; i++ )
881         {
882             MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
883             char      *psz_ref;
884             uint32_t  i_ref_type;
885
886             if( !p_rdrf || !BOXDATA(p_rdrf) || !( psz_ref = strdup( BOXDATA(p_rdrf)->psz_ref ) ) )
887             {
888                 continue;
889             }
890             i_ref_type = BOXDATA(p_rdrf)->i_ref_type;
891
892             msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
893                      psz_ref, (char*)&i_ref_type );
894
895             if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
896             {
897                 if( strstr( psz_ref, "qt5gateQT" ) )
898                 {
899                     msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
900                     free( psz_ref );
901                     continue;
902                 }
903                 if( !strncmp( psz_ref, "http://", 7 ) ||
904                     !strncmp( psz_ref, "rtsp://", 7 ) )
905                 {
906                     ;
907                 }
908                 else
909                 {
910                     char *psz_absolute = vlc_uri_resolve( p_demux->psz_url,
911                                                           psz_ref );
912                     free( psz_ref );
913                     if( psz_absolute == NULL )
914                     {
915                         input_item_node_Delete( p_subitems );
916                         return VLC_ENOMEM;
917                     }
918                     psz_ref = psz_absolute;
919                 }
920                 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
921                 input_item_t *p_item = input_item_New( psz_ref, NULL );
922                 input_item_CopyOptions( p_item, p_current );
923                 input_item_node_AppendItem( p_subitems, p_item );
924                 input_item_Release( p_item );
925             }
926             else
927             {
928                 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
929                          (char*)&BOXDATA(p_rdrf)->i_ref_type );
930             }
931             free( psz_ref );
932         }
933
934         /* FIXME: create a stream_filter sub-module for this */
935         if (es_out_Control(p_demux->out, ES_OUT_POST_SUBNODE, p_subitems))
936             input_item_node_Delete(p_subitems);
937     }
938
939     if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
940     {
941         if( !p_rmra )
942         {
943             msg_Err( p_demux, "cannot find /moov/mvhd" );
944             goto error;
945         }
946         else
947         {
948             msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
949             p_demux->pf_demux = DemuxRef;
950             return VLC_SUCCESS;
951         }
952     }
953     else
954     {
955         p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
956         if( p_sys->i_timescale == 0 )
957         {
958             msg_Err( p_this, "bad timescale" );
959             goto error;
960         }
961     }
962
963     const unsigned i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" );
964     if( i_tracks < 1 )
965     {
966         msg_Err( p_demux, "cannot find any /moov/trak" );
967         goto error;
968     }
969     msg_Dbg( p_demux, "found %u track%c", i_tracks, i_tracks ? 's':' ' );
970
971     if( CreateTracks( p_demux, i_tracks ) != VLC_SUCCESS )
972         goto error;
973
974     /* Search the first chap reference (like quicktime) and
975      * check that at least 1 stream is enabled */
976     p_sys->p_tref_chap = NULL;
977     b_enabled_es = false;
978     for( unsigned i = 0; i < p_sys->i_tracks; i++ )
979     {
980         MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
981
982
983         MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
984         if( p_tkhd && BOXDATA(p_tkhd) && (BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED) )
985             b_enabled_es = true;
986
987         MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
988         if( p_chap && p_chap->data.p_tref_generic &&
989             p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
990             p_sys->p_tref_chap = p_chap;
991     }
992
993     /* Set and store metadata */
994     if( (p_sys->p_meta = vlc_meta_New()) )
995         MP4_LoadMeta( p_sys, p_sys->p_meta );
996
997     /* now process each track and extract all useful information */
998     for( unsigned i = 0; i < p_sys->i_tracks; i++ )
999     {
1000         MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%u]", i );
1001         MP4_TrackSetup( p_demux, &p_sys->track[i], p_trak, true, !b_enabled_es );
1002
1003         if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapters_source )
1004         {
1005             const char *psz_cat;
1006             switch( p_sys->track[i].fmt.i_cat )
1007             {
1008                 case( VIDEO_ES ):
1009                     psz_cat = "video";
1010                     break;
1011                 case( AUDIO_ES ):
1012                     psz_cat = "audio";
1013                     break;
1014                 case( SPU_ES ):
1015                     psz_cat = "subtitle";
1016                     break;
1017
1018                 default:
1019                     psz_cat = "unknown";
1020                     break;
1021             }
1022
1023             msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
1024                      p_sys->track[i].i_track_ID, psz_cat,
1025                      p_sys->track[i].b_enable ? "enable":"disable",
1026                      p_sys->track[i].fmt.psz_language ?
1027                      p_sys->track[i].fmt.psz_language : "undef" );
1028         }
1029         else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapters_source )
1030         {
1031             msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
1032                      p_sys->track[i].i_track_ID,
1033                      p_sys->track[i].fmt.psz_language ?
1034                      p_sys->track[i].fmt.psz_language : "undef" );
1035         }
1036         else
1037         {
1038             msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
1039                      p_sys->track[i].i_track_ID );
1040         }
1041     }
1042
1043     p_mvex = MP4_BoxGet( p_sys->p_moov, "mvex" );
1044     if( p_mvex != NULL )
1045     {
1046         const MP4_Box_t *p_mehd = MP4_BoxGet( p_mvex, "mehd");
1047         if ( p_mehd && BOXDATA(p_mehd) )
1048         {
1049             if( BOXDATA(p_mehd)->i_fragment_duration > p_sys->i_duration )
1050             {
1051                 p_sys->b_fragmented = true;
1052                 p_sys->i_duration = BOXDATA(p_mehd)->i_fragment_duration;
1053             }
1054         }
1055
1056         const MP4_Box_t *p_sidx = MP4_BoxGet( p_sys->p_root, "sidx");
1057         if( p_sidx )
1058             p_sys->b_fragmented = true;
1059
1060         if ( p_sys->b_seekable )
1061         {
1062             if( !p_sys->b_fragmented /* as unknown */ )
1063             {
1064                 /* Probe remaining to check if there's really fragments
1065                    or if that file is just ready to append fragments */
1066                 ProbeFragments( p_demux, (p_sys->i_duration == 0), &p_sys->b_fragmented );
1067             }
1068
1069             if( vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos ) != VLC_SUCCESS )
1070                 goto error;
1071         }
1072         else /* Handle as fragmented by default as we can't see moof */
1073         {
1074             p_sys->context.p_fragment_atom = p_sys->p_moov;
1075             p_sys->context.i_current_box_type = ATOM_moov;
1076             p_sys->b_fragmented = true;
1077         }
1078     }
1079
1080     if( p_sys->b_fragmented )
1081     {
1082         p_demux->pf_demux = DemuxFrag;
1083         msg_Dbg( p_demux, "Set Fragmented demux mode" );
1084     }
1085
1086     if( !p_sys->b_seekable && p_demux->pf_demux == Demux )
1087     {
1088         msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" );
1089         goto error;
1090     }
1091
1092     if( p_sys->i_tracks > 1 && !p_sys->b_fastseekable )
1093     {
1094         vlc_tick_t i_max_continuity;
1095         bool b_flat;
1096         MP4_GetInterleaving( p_demux, &i_max_continuity, &b_flat );
1097         if( b_flat )
1098             msg_Warn( p_demux, "that media doesn't look interleaved, will need to seek");
1099         else if( i_max_continuity > DEMUX_TRACK_MAX_PRELOAD )
1100             msg_Warn( p_demux, "that media doesn't look properly interleaved, will need to seek");
1101     }
1102
1103     /* */
1104     LoadChapter( p_demux );
1105
1106     p_sys->asfpacketsys.p_demux = p_demux;
1107     p_sys->asfpacketsys.pi_preroll = &p_sys->i_preroll;
1108     p_sys->asfpacketsys.pi_preroll_start = &p_sys->i_preroll_start;
1109     p_sys->asfpacketsys.pf_doskip = NULL;
1110     p_sys->asfpacketsys.pf_send = MP4ASF_Send;
1111     p_sys->asfpacketsys.pf_gettrackinfo = MP4ASF_GetTrackInfo;
1112     p_sys->asfpacketsys.pf_updatetime = NULL;
1113     p_sys->asfpacketsys.pf_setaspectratio = NULL;
1114
1115     return VLC_SUCCESS;
1116
1117 error:
1118     if( vlc_stream_Tell( p_demux->s ) > 0 )
1119     {
1120         if( vlc_stream_Seek( p_demux->s, 0 ) != VLC_SUCCESS )
1121             msg_Warn( p_demux, "Can't reset stream position from probing" );
1122     }
1123
1124     Close( p_this );
1125
1126     return VLC_EGENERIC;
1127 }
1128
1129 const unsigned int SAMPLEHEADERSIZE = 4;
1130 const unsigned int RTPPACKETSIZE = 12;
1131 const unsigned int CONSTRUCTORSIZE = 16;
1132
1133 /*******************************************************************************
1134  * MP4_RTPHintToFrame: converts RTP Reception Hint Track sample to H.264 frame
1135  *******************************************************************************/
1136 static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount )
1137 {
1138     uint8_t *p_slice = p_block->p_buffer + SAMPLEHEADERSIZE;
1139     block_t *p_newblock = NULL;
1140     size_t i_payload = 0;
1141
1142     if( p_block->i_buffer < SAMPLEHEADERSIZE + RTPPACKETSIZE + CONSTRUCTORSIZE )
1143     {
1144         msg_Err( p_demux, "Sample not large enough for necessary structs");
1145         block_Release( p_block );
1146         return NULL;
1147     }
1148
1149     for( uint32_t i = 0; i < packetcount; ++i )
1150     {
1151         if( (size_t)(p_slice - p_block->p_buffer) + RTPPACKETSIZE + CONSTRUCTORSIZE > p_block->i_buffer )
1152             goto error;
1153
1154         /* skip RTP header in sample. Could be used to detect packet losses */
1155         p_slice += RTPPACKETSIZE;
1156
1157         mp4_rtpsampleconstructor_t sample_cons;
1158
1159         sample_cons.type =                      p_slice[0];
1160         sample_cons.trackrefindex =             p_slice[1];
1161         sample_cons.length =          GetWBE(  &p_slice[2] );
1162         sample_cons.samplenumber =    GetDWBE( &p_slice[4] );
1163         sample_cons.sampleoffset =    GetDWBE( &p_slice[8] );
1164         sample_cons.bytesperblock =   GetWBE(  &p_slice[12] );
1165         sample_cons.samplesperblock = GetWBE(  &p_slice[14] );
1166
1167         /* skip packet constructor */
1168         p_slice += CONSTRUCTORSIZE;
1169
1170         /* check that is RTPsampleconstructor, referencing itself and no weird audio stuff */
1171         if( sample_cons.type != 2||sample_cons.trackrefindex != -1
1172             ||sample_cons.samplesperblock != 1||sample_cons.bytesperblock != 1 )
1173         {
1174             msg_Err(p_demux, "Unhandled constructor in RTP Reception Hint Track. Type:%u", sample_cons.type);
1175             goto error;
1176         }
1177
1178         /* slice doesn't fit in buffer */
1179         if( sample_cons.sampleoffset + sample_cons.length > p_block->i_buffer)
1180         {
1181             msg_Err(p_demux, "Sample buffer is smaller than sample" );
1182             goto error;
1183         }
1184
1185         block_t *p_realloc = ( p_newblock ) ?
1186                              block_Realloc( p_newblock, 0, i_payload + sample_cons.length + 4 ):
1187                              block_Alloc( i_payload + sample_cons.length + 4 );
1188         if( !p_realloc )
1189             goto error;
1190
1191         p_newblock = p_realloc;
1192         uint8_t *p_dst = &p_newblock->p_buffer[i_payload];
1193
1194         const uint8_t* p_src = p_block->p_buffer + sample_cons.sampleoffset;
1195         uint8_t i_type = (*p_src) & ((1<<5)-1);
1196
1197         const uint8_t synccode[4] = { 0, 0, 0, 1 };
1198         if( memcmp( p_src, synccode, 4 ) )
1199         {
1200             if( i_type == 7 || i_type == 8 )
1201                 *p_dst++=0;
1202
1203             p_dst[0] = 0;
1204             p_dst[1] = 0;
1205             p_dst[2] = 1;
1206             p_dst += 3;
1207         }
1208
1209         memcpy( p_dst, p_src, sample_cons.length );
1210         p_dst += sample_cons.length;
1211
1212         i_payload = p_dst - p_newblock->p_buffer;
1213     }
1214
1215     block_Release( p_block );
1216     if( p_newblock )
1217         p_newblock->i_buffer = i_payload;
1218     return p_newblock;
1219
1220 error:
1221     block_Release( p_block );
1222     if( p_newblock )
1223         block_Release( p_newblock );
1224     return NULL;
1225 }
1226
1227 /* RTP Reception Hint Track */
1228 static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec )
1229 {
1230     block_t *p_converted = NULL;
1231     if( p_block->i_buffer < 2 )
1232     {
1233         block_Release( p_block );
1234         return NULL;
1235     }
1236
1237     /* number of RTP packets contained in this sample */
1238     const uint16_t i_packets = GetWBE( p_block->p_buffer );
1239     if( i_packets <= 1 || i_codec != VLC_CODEC_H264 )
1240     {
1241         const size_t i_skip = SAMPLEHEADERSIZE + i_packets * ( RTPPACKETSIZE + CONSTRUCTORSIZE );
1242         if( i_packets == 1 && i_skip < p_block->i_buffer )
1243         {
1244             p_block->p_buffer += i_skip;
1245             p_converted = p_block;
1246         }
1247         else
1248         {
1249             block_Release( p_block );
1250         }
1251     }
1252     else
1253     {
1254         p_converted = MP4_RTPHintToFrame( p_demux, p_block, i_packets );
1255     }
1256
1257     return p_converted;
1258 }
1259
1260 /*****************************************************************************
1261  * Demux: read packet and send them to decoders
1262  *****************************************************************************
1263  * TODO check for newly selected track (ie audio upt to now )
1264  *****************************************************************************/
1265 static int DemuxTrack( demux_t *p_demux, mp4_track_t *tk, uint64_t i_readpos,
1266                        vlc_tick_t i_max_preload )
1267 {
1268     demux_sys_t *p_sys = p_demux->p_sys;
1269     uint32_t i_nb_samples = 0;
1270     uint32_t i_samplessize = 0;
1271
1272     if( !tk->b_ok || tk->i_sample >= tk->i_sample_count )
1273         return VLC_DEMUXER_EOS;
1274
1275     if( tk->b_chapters_source )
1276         return VLC_DEMUXER_SUCCESS;
1277
1278     uint32_t i_run_seq = MP4_TrackGetRunSeq( tk );
1279     vlc_tick_t i_current_nzdts = MP4_TrackGetDTS( p_demux, tk );
1280     const vlc_tick_t i_demux_max_nzdts =i_max_preload < INVALID_PRELOAD
1281                                     ? i_current_nzdts + i_max_preload
1282                                     : INT64_MAX;
1283
1284     for( ; i_demux_max_nzdts >= i_current_nzdts; )
1285     {
1286         if( tk->i_sample >= tk->i_sample_count )
1287             return VLC_DEMUXER_EOS;
1288
1289 #if 0
1290         msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, tk->i_track_ID,
1291                  MP4_TrackGetDTS( p_demux, tk ),
1292                  MP4_GetMoviePTS( p_demux->p_sys ), i_readpos );
1293 #endif
1294
1295         i_samplessize = MP4_TrackGetReadSize( tk, &i_nb_samples );
1296         if( i_samplessize > 0 )
1297         {
1298             block_t *p_block;
1299             vlc_tick_t i_delta;
1300
1301             if( vlc_stream_Tell( p_demux->s ) != i_readpos )
1302             {
1303                 if( MP4_Seek( p_demux->s, i_readpos ) != VLC_SUCCESS )
1304                 {
1305                     msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1306                                        ": Failed to seek to %"PRIu64,
1307                               tk->i_track_ID, i_readpos );
1308                     MP4_TrackSelect( p_demux, tk, false );
1309                     goto end;
1310                 }
1311             }
1312
1313             /* now read pes */
1314             if( !(p_block = vlc_stream_Block( p_demux->s, i_samplessize )) )
1315             {
1316                 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1317                                    ": Failed to read %d bytes sample at %"PRIu64,
1318                           tk->i_track_ID, i_samplessize, i_readpos );
1319                 MP4_TrackSelect( p_demux, tk, false );
1320                 goto end;
1321             }
1322
1323             /* !important! Ensure clock is set before sending data */
1324             if( p_sys->i_pcr == VLC_TICK_INVALID )
1325             {
1326                 es_out_SetPCR( p_demux->out, VLC_TICK_0 + i_current_nzdts );
1327                 p_sys->i_pcr = VLC_TICK_0 + i_current_nzdts;
1328             }
1329
1330             /* dts */
1331             p_block->i_dts = VLC_TICK_0 + i_current_nzdts;
1332             /* pts */
1333             if( MP4_TrackGetPTSDelta( p_demux, tk, &i_delta ) )
1334                 p_block->i_pts = p_block->i_dts + i_delta;
1335             else if( tk->fmt.i_cat != VIDEO_ES )
1336                 p_block->i_pts = p_block->i_dts;
1337             else
1338                 p_block->i_pts = VLC_TICK_INVALID;
1339
1340             p_block->i_length = MP4_GetSamplesDuration( p_demux, tk, i_nb_samples );
1341
1342             MP4_Block_Send( p_demux, tk, p_block );
1343         }
1344
1345         /* Next sample */
1346         if ( i_nb_samples ) /* sample size could be 0, need to go fwd. see return */
1347             MP4_TrackNextSample( p_demux, tk, i_nb_samples );
1348
1349         uint32_t i_next_run_seq = MP4_TrackGetRunSeq( tk );
1350         if( i_next_run_seq != i_run_seq )
1351             break;
1352
1353         i_current_nzdts = MP4_TrackGetDTS( p_demux, tk );
1354         i_readpos = MP4_TrackGetPos( tk );
1355     }
1356
1357     return VLC_DEMUXER_SUCCESS;
1358
1359 end:
1360     return VLC_DEMUXER_EGENERIC;
1361 }
1362
1363 static int DemuxMoov( demux_t *p_demux )
1364 {
1365     demux_sys_t *p_sys = p_demux->p_sys;
1366     unsigned int i_track;
1367
1368     /* check for newly selected/unselected track */
1369     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1370     {
1371         mp4_track_t *tk = &p_sys->track[i_track];
1372         bool b = true;
1373
1374         if( !tk->b_ok || tk->b_chapters_source ||
1375             ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
1376         {
1377             continue;
1378         }
1379
1380         if( p_sys->b_seekable )
1381             es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1382
1383         if( tk->b_selected && !b )
1384         {
1385             MP4_TrackSelect( p_demux, tk, false );
1386         }
1387         else if( !tk->b_selected && b)
1388         {
1389             MP4_TrackSeek( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
1390         }
1391     }
1392
1393     const vlc_tick_t i_nztime = MP4_GetMoviePTS( p_sys );
1394
1395     /* We demux/set pcr, even without selected tracks, (empty edits, ...) */
1396     if( p_sys->i_pcr != VLC_TICK_INVALID /* not after a seek */ )
1397     {
1398         bool b_eof = true;
1399         for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1400         {
1401             mp4_track_t *tk = &p_sys->track[i_track];
1402             if( !tk->b_ok || tk->b_chapters_source || tk->i_sample >= tk->i_sample_count )
1403                 continue;
1404             /* Test for EOF on each track (samples count, edit list) */
1405             b_eof &= ( i_nztime > MP4_TrackGetDTS( p_demux, tk ) );
1406         }
1407         if( b_eof )
1408             return VLC_DEMUXER_EOS;
1409     }
1410
1411     const vlc_tick_t i_max_preload = ( p_sys->b_fastseekable ) ? 0 : ( p_sys->b_seekable ) ? DEMUX_TRACK_MAX_PRELOAD : INVALID_PRELOAD;
1412     int i_status;
1413     /* demux up to increment amount of data on every track, or just set pcr if empty data */
1414     for( ;; )
1415     {
1416         mp4_track_t *tk = NULL;
1417         i_status = VLC_DEMUXER_EOS;
1418
1419         /* First pass, find any track within our target increment, ordered by position */
1420         for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1421         {
1422             mp4_track_t *tk_tmp = &p_sys->track[i_track];
1423             if( !tk_tmp->b_ok || tk_tmp->b_chapters_source ||
1424                 tk_tmp->i_sample >= tk_tmp->i_sample_count ||
1425                 (!tk_tmp->b_selected && p_sys->b_seekable) )
1426                 continue;
1427
1428             /* At least still have data to demux on this or next turns */
1429             i_status = VLC_DEMUXER_SUCCESS;
1430
1431             if ( MP4_TrackGetDTS( p_demux, tk_tmp ) <= i_nztime + DEMUX_INCREMENT )
1432             {
1433                 if( tk == NULL || MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) )
1434                     tk = tk_tmp;
1435             }
1436         }
1437
1438         if( tk )
1439         {
1440             /* Second pass, refine and find any best candidate having a chunk pos closer than
1441              * current candidate (avoids seeks when increment falls between the 2) from
1442              * current position, but within extended interleave time */
1443             for( i_track = 0; i_max_preload != 0 && i_track < p_sys->i_tracks; i_track++ )
1444             {
1445                 mp4_track_t *tk_tmp = &p_sys->track[i_track];
1446                 if( tk_tmp == tk ||
1447                     !tk_tmp->b_ok || tk_tmp->b_chapters_source ||
1448                    (!tk_tmp->b_selected && p_sys->b_seekable) ||
1449                     tk_tmp->i_sample >= tk_tmp->i_sample_count )
1450                     continue;
1451
1452                 vlc_tick_t i_nzdts = MP4_TrackGetDTS( p_demux, tk_tmp );
1453                 if ( i_nzdts <= i_nztime + DEMUX_TRACK_MAX_PRELOAD )
1454                 {
1455                     /* Found a better candidate to avoid seeking */
1456                     if( MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) )
1457                         tk = tk_tmp;
1458                     /* Note: previous candidate will be repicked on next loop */
1459                 }
1460             }
1461
1462             uint64_t i_pos = MP4_TrackGetPos( tk );
1463             int i_ret = DemuxTrack( p_demux, tk, i_pos, i_max_preload );
1464
1465             if( i_ret == VLC_DEMUXER_SUCCESS )
1466                 i_status = VLC_DEMUXER_SUCCESS;
1467         }
1468
1469         if( i_status != VLC_DEMUXER_SUCCESS || !tk )
1470             break;
1471     }
1472
1473     p_sys->i_nztime += DEMUX_INCREMENT;
1474     if( p_sys->i_pcr != VLC_TICK_INVALID )
1475     {
1476         p_sys->i_pcr = VLC_TICK_0 + p_sys->i_nztime;
1477         es_out_SetPCR( p_demux->out, p_sys->i_pcr );
1478     }
1479
1480     /* */
1481     MP4_UpdateSeekpoint( p_demux, i_nztime + DEMUX_INCREMENT );
1482
1483     return i_status;
1484 }
1485
1486 static int Demux( demux_t *p_demux )
1487 {
1488     demux_sys_t *p_sys = p_demux->p_sys;
1489
1490     assert( ! p_sys->b_fragmented );
1491
1492     int i_status = DemuxMoov( p_demux );
1493
1494     if( i_status == VLC_DEMUXER_EOS )
1495         i_status = VLC_DEMUXER_EOF;
1496
1497     return i_status;
1498 }
1499
1500 static void MP4_UpdateSeekpoint( demux_t *p_demux, vlc_tick_t i_time )
1501 {
1502     demux_sys_t *p_sys = p_demux->p_sys;
1503     int i;
1504     if( !p_sys->p_title )
1505         return;
1506     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
1507     {
1508         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
1509             break;
1510     }
1511     i--;
1512
1513     if( i != p_sys->i_seekpoint && i >= 0 )
1514     {
1515         p_sys->i_seekpoint = i;
1516         p_sys->seekpoint_changed = true;
1517     }
1518 }
1519 /*****************************************************************************
1520  * Seek: Go to i_date
1521 ******************************************************************************/
1522 static int Seek( demux_t *p_demux, vlc_tick_t i_date, bool b_accurate )
1523 {
1524     demux_sys_t *p_sys = p_demux->p_sys;
1525     unsigned int i_track;
1526
1527     /* Now for each stream try to go to this time */
1528     vlc_tick_t i_start = i_date;
1529     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1530     {
1531         mp4_track_t *tk = &p_sys->track[i_track];
1532         /* FIXME: we should find the lowest time from tracks with indexes.
1533            considering only video for now */
1534         if( tk->fmt.i_cat != VIDEO_ES )
1535             continue;
1536         if( MP4_TrackSeek( p_demux, tk, i_date ) == VLC_SUCCESS )
1537         {
1538             vlc_tick_t i_seeked = MP4_TrackGetDTS( p_demux, tk );
1539             if( i_seeked < i_start )
1540                 i_start = i_seeked;
1541         }
1542     }
1543
1544     msg_Dbg( p_demux, "seeking with %"PRId64 "ms %s", MS_FROM_VLC_TICK(i_date - i_start),
1545             !b_accurate ? "alignment" : "preroll (use input-fast-seek to avoid)" );
1546
1547     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1548     {
1549         mp4_track_t *tk = &p_sys->track[i_track];
1550         tk->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1551         if( tk->fmt.i_cat == VIDEO_ES )
1552             continue;
1553         MP4_TrackSeek( p_demux, tk, i_start );
1554     }
1555
1556     MP4_UpdateSeekpoint( p_demux, i_date );
1557     MP4ASF_ResetFrames( p_sys );
1558     /* update global time */
1559     p_sys->i_nztime = i_start;
1560     p_sys->i_pcr  = VLC_TICK_INVALID;
1561
1562     if( b_accurate )
1563         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1564
1565     return VLC_SUCCESS;
1566 }
1567
1568 static int FragPrepareChunk( demux_t *p_demux, MP4_Box_t *p_moof,
1569                              MP4_Box_t *p_sidx, stime_t i_moof_time, bool b_discontinuity )
1570 {
1571     demux_sys_t *p_sys = p_demux->p_sys;
1572
1573     if( b_discontinuity )
1574     {
1575         for( unsigned i=0; i<p_sys->i_tracks; i++ )
1576             p_sys->track[i].context.b_resync_time_offset = true;
1577     }
1578
1579     if( FragCreateTrunIndex( p_demux, p_moof, p_sidx, i_moof_time ) == VLC_SUCCESS )
1580     {
1581         for( unsigned i=0; i<p_sys->i_tracks; i++ )
1582         {
1583             mp4_track_t *p_track = &p_sys->track[i];
1584             if( p_track->context.runs.i_count )
1585             {
1586                 const mp4_run_t *p_run = &p_track->context.runs.p_array[0];
1587                 p_track->context.i_trun_sample_pos = p_run->i_offset;
1588                 p_track->context.i_trun_sample = 0;
1589                 p_track->i_time = p_run->i_first_dts;
1590             }
1591         }
1592         return VLC_SUCCESS;
1593     }
1594
1595     return VLC_EGENERIC;
1596 }
1597
1598 static vlc_tick_t FragGetDemuxTimeFromTracksTime( demux_sys_t *p_sys )
1599 {
1600     vlc_tick_t i_time = INT64_MAX;
1601     for( unsigned int i = 0; i < p_sys->i_tracks; i++ )
1602     {
1603         if( p_sys->track[i].context.runs.i_count == 0 )
1604             continue;
1605         vlc_tick_t i_ttime = MP4_rescale_mtime( p_sys->track[i].i_time,
1606                                              p_sys->track[i].i_timescale );
1607         i_time = __MIN( i_time, i_ttime );
1608     }
1609     return i_time;
1610 }
1611
1612 static uint32_t FragGetMoofSequenceNumber( MP4_Box_t *p_moof )
1613 {
1614     const MP4_Box_t *p_mfhd = MP4_BoxGet( p_moof, "mfhd" );
1615     if( p_mfhd && BOXDATA(p_mfhd) )
1616         return BOXDATA(p_mfhd)->i_sequence_number;
1617     return 0;
1618 }
1619
1620 static int FragSeekLoadFragment( demux_t *p_demux, uint32_t i_moox, stime_t i_moox_time )
1621 {
1622     demux_sys_t *p_sys = p_demux->p_sys;
1623     MP4_Box_t *p_moox;
1624
1625     if( i_moox == ATOM_moov )
1626     {
1627         p_moox = p_sys->p_moov;
1628     }
1629     else
1630     {
1631         const uint8_t *p_peek;
1632         if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) != 8 )
1633             return VLC_EGENERIC;
1634
1635         if( ATOM_moof != VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
1636             return VLC_EGENERIC;
1637
1638         MP4_Box_t *p_vroot = MP4_BoxGetNextChunk( p_demux->s );
1639         if(!p_vroot)
1640             return VLC_EGENERIC;
1641         p_moox = MP4_BoxExtract( &p_vroot->p_first, ATOM_moof );
1642         MP4_BoxFree( p_vroot );
1643
1644         if(!p_moox)
1645             return VLC_EGENERIC;
1646     }
1647
1648     FragResetContext( p_sys );
1649
1650     /* map context */
1651     p_sys->context.p_fragment_atom = p_moox;
1652     p_sys->context.i_current_box_type = i_moox;
1653
1654     if( i_moox == ATOM_moof )
1655     {
1656         FragPrepareChunk( p_demux, p_moox, NULL, i_moox_time, true );
1657         p_sys->context.i_lastseqnumber = FragGetMoofSequenceNumber( p_moox );
1658
1659         p_sys->i_nztime = FragGetDemuxTimeFromTracksTime( p_sys );
1660         p_sys->i_pcr = VLC_TICK_INVALID;
1661     }
1662
1663     msg_Dbg( p_demux, "seeked to %4.4s at pos %" PRIu64, (char *) &i_moox, p_moox->i_pos );
1664     return VLC_SUCCESS;
1665 }
1666
1667 static unsigned GetSeekTrackIndex( demux_sys_t *p_sys )
1668 {
1669     unsigned cand = 0;
1670     for( unsigned i=0; i<p_sys->i_tracks; i++ )
1671     {
1672         if( p_sys->track[i].fmt.i_cat == VIDEO_ES ||
1673             p_sys->track[i].fmt.i_cat == AUDIO_ES )
1674         {
1675             if( cand != i && !p_sys->track[cand].b_selected )
1676                 cand = i;
1677         }
1678     }
1679     return cand;
1680 }
1681
1682 static void FragTrunSeekToTime( mp4_track_t *p_track, stime_t i_target_time )
1683 {
1684     if( !p_track->b_ok || p_track->context.runs.i_count < 1 )
1685         return;
1686
1687     unsigned i_run = 0;
1688     unsigned i_sample = 0;
1689     uint64_t i_pos = p_track->context.runs.p_array[0].i_offset;
1690     stime_t  i_time = p_track->context.runs.p_array[0].i_first_dts;
1691
1692     for( unsigned r = 0; r < p_track->context.runs.i_count; r++ )
1693     {
1694         const mp4_run_t *p_run = &p_track->context.runs.p_array[r];
1695         const MP4_Box_data_trun_t *p_data =
1696                     p_track->context.runs.p_array[r].p_trun->data.p_trun;
1697         if( i_time > i_target_time )
1698             break;
1699
1700         i_run = r;
1701         i_time = p_run->i_first_dts;
1702         i_pos = p_run->i_offset;
1703         i_sample = 0;
1704
1705         uint32_t dur = p_track->context.i_default_sample_duration;
1706         uint32_t len = p_track->context.i_default_sample_size;
1707         for ( unsigned i=0; i<p_data->i_sample_count; i++ )
1708         {
1709             if( p_data->i_flags & MP4_TRUN_SAMPLE_DURATION )
1710                 dur = p_data->p_samples[i].i_duration;
1711
1712             /* check condition */
1713             if( i_time + dur > i_target_time )
1714                 break;
1715
1716             if( p_data->i_flags & MP4_TRUN_SAMPLE_SIZE )
1717                 len = p_data->p_samples[i].i_size;
1718
1719             i_time += dur;
1720             i_pos += len;
1721         }
1722     }
1723
1724     p_track->context.i_trun_sample = i_sample;
1725     p_track->context.i_trun_sample_pos = i_pos;
1726     p_track->context.runs.i_current = i_run;
1727 }
1728
1729 #define INVALID_SEGMENT_TIME  INT64_MAX
1730
1731 static int FragSeekToTime( demux_t *p_demux, vlc_tick_t i_nztime, bool b_accurate )
1732 {
1733     demux_sys_t *p_sys = p_demux->p_sys;
1734     uint64_t i64 = UINT64_MAX;
1735     uint32_t i_segment_type = ATOM_moof;
1736     stime_t  i_segment_time = INVALID_SEGMENT_TIME;
1737     vlc_tick_t i_sync_time = i_nztime;
1738     bool b_iframesync = false;
1739
1740     const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1741     if ( !p_sys->i_timescale || !i_duration || !p_sys->b_seekable )
1742          return VLC_EGENERIC;
1743
1744     uint64_t i_backup_pos = vlc_stream_Tell( p_demux->s );
1745
1746     if ( !p_sys->b_fragments_probed && !p_sys->b_index_probed && p_sys->b_seekable )
1747     {
1748         ProbeIndex( p_demux );
1749         p_sys->b_index_probed = true;
1750     }
1751
1752     const unsigned i_seek_track_index = GetSeekTrackIndex( p_sys );
1753     const unsigned i_seek_track_ID = p_sys->track[i_seek_track_index].i_track_ID;
1754
1755     if( MP4_rescale_qtime( i_nztime, p_sys->i_timescale )
1756                      < GetMoovTrackDuration( p_sys, i_seek_track_ID ) )
1757     {
1758         i64 = p_sys->p_moov->i_pos;
1759         i_segment_type = ATOM_moov;
1760     }
1761     else if( FragGetMoofBySidxIndex( p_demux, i_nztime, &i64, &i_sync_time ) == VLC_SUCCESS )
1762     {
1763         /* provides base offset */
1764         i_segment_time = i_sync_time;
1765         msg_Dbg( p_demux, "seeking to sidx moof pos %" PRId64 " %" PRId64, i64, i_sync_time );
1766     }
1767     else
1768     {
1769         bool b_buildindex = false;
1770
1771         if( FragGetMoofByTfraIndex( p_demux, i_nztime, i_seek_track_ID, &i64, &i_sync_time ) == VLC_SUCCESS )
1772         {
1773             /* Does only provide segment position and a sync sample time */
1774             msg_Dbg( p_demux, "seeking to sync point %" PRId64, i_sync_time );
1775             b_iframesync = true;
1776         }
1777         else if( !p_sys->b_fragments_probed && !p_sys->b_fastseekable )
1778         {
1779             const char *psz_msg = _(
1780                 "Because this file index is broken or missing, "
1781                 "seeking will not work correctly.\n"
1782                 "VLC won't repair your file but can temporary fix this "
1783                 "problem by building an index in memory.\n"
1784                 "This step might take a long time on a large file.\n"
1785                 "What do you want to do?");
1786             b_buildindex = vlc_dialog_wait_question( p_demux,
1787                                                      VLC_DIALOG_QUESTION_NORMAL,
1788                                                      _("Do not seek"),
1789                                                      _("Build index"),
1790                                                      NULL,
1791                                                      _("Broken or missing Index"),
1792                                                      "%s", psz_msg );
1793         }
1794
1795         if( !p_sys->b_fragments_probed && ( p_sys->b_fastseekable || b_buildindex ) )
1796         {
1797             bool foo;
1798             int i_ret = vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos + p_sys->p_moov->i_size );
1799             if( i_ret == VLC_SUCCESS )
1800             {
1801                 i_ret = ProbeFragments( p_demux, true, &foo );
1802                 p_sys->b_fragments_probed = true;
1803             }
1804             if( i_ret != VLC_SUCCESS )
1805             {
1806                 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1807                 return i_ret;
1808             }
1809         }
1810
1811         if( p_sys->b_fragments_probed && p_sys->p_fragsindex )
1812         {
1813             stime_t i_basetime = MP4_rescale_qtime( i_sync_time, p_sys->i_timescale );
1814             if( !MP4_Fragments_Index_Lookup( p_sys->p_fragsindex, &i_basetime, &i64, i_seek_track_index ) )
1815             {
1816                 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1817                 return VLC_EGENERIC;
1818             }
1819             msg_Dbg( p_demux, "seeking to fragment index pos %" PRId64 " %" PRId64, i64,
1820                      MP4_rescale_mtime( i_basetime, p_sys->i_timescale ) );
1821         }
1822     }
1823
1824     if( i64 == UINT64_MAX )
1825     {
1826         msg_Warn( p_demux, "seek by index failed" );
1827         p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1828         return VLC_EGENERIC;
1829     }
1830
1831     msg_Dbg( p_demux, "final seek to fragment at %"PRId64, i64 );
1832     if( vlc_stream_Seek( p_demux->s, i64 ) )
1833     {
1834         msg_Err( p_demux, "seek failed to %"PRId64, i64 );
1835         p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1836         return VLC_EGENERIC;
1837     }
1838
1839     /* Context is killed on success */
1840     if( FragSeekLoadFragment( p_demux, i_segment_type, i_segment_time ) != VLC_SUCCESS )
1841     {
1842         p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1843         return VLC_EGENERIC;
1844     }
1845
1846     p_sys->i_pcr  = VLC_TICK_INVALID;
1847
1848     for( unsigned i=0; i<p_sys->i_tracks; i++ )
1849     {
1850         if( i_segment_type == ATOM_moov )
1851         {
1852             MP4_TrackSeek( p_demux, &p_sys->track[i], i_sync_time );
1853             p_sys->i_nztime = i_sync_time;
1854             p_sys->i_pcr  = VLC_TICK_INVALID;
1855         }
1856         else if( b_iframesync )
1857         {
1858             stime_t i_tst = MP4_rescale_qtime( i_sync_time, p_sys->track[i].i_timescale );
1859             FragTrunSeekToTime( &p_sys->track[i], i_tst );
1860             p_sys->track[i].i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1861         }
1862     }
1863
1864     MP4ASF_ResetFrames( p_sys );
1865     /* And set next display time in that trun/fragment */
1866     if( b_iframesync && b_accurate )
1867         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TICK_0 + i_nztime );
1868     return VLC_SUCCESS;
1869 }
1870
1871 static int FragSeekToPos( demux_t *p_demux, double f, bool b_accurate )
1872 {
1873     demux_sys_t *p_sys = p_demux->p_sys;
1874     const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1875
1876     if ( !p_sys->b_seekable || !p_sys->i_timescale || !i_duration )
1877         return VLC_EGENERIC;
1878
1879     return FragSeekToTime( p_demux, (vlc_tick_t)( f *
1880                            MP4_rescale_mtime( i_duration, p_sys->i_timescale ) ), b_accurate );
1881 }
1882
1883 static bool imageTypeCompatible( const MP4_Box_data_data_t *p_data )
1884 {
1885     return p_data && (
1886     p_data->e_wellknowntype == DATA_WKT_PNG ||
1887     p_data->e_wellknowntype == DATA_WKT_JPEG ||
1888     p_data->e_wellknowntype == DATA_WKT_BMP );
1889 }
1890
1891 static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta )
1892 {
1893     MP4_Box_t *p_data = NULL;
1894     MP4_Box_t *p_udta = NULL;
1895     bool b_attachment_set = false;
1896
1897     if( !p_meta )
1898         return VLC_EGENERIC;
1899
1900     for( int i_index = 0; psz_meta_roots[i_index] && !p_udta; i_index++ )
1901     {
1902         p_udta = MP4_BoxGet( p_sys->p_root, psz_meta_roots[i_index] );
1903         if ( p_udta )
1904         {
1905             p_data = MP4_BoxGet( p_udta, "covr/data" );
1906             if ( p_data && imageTypeCompatible( BOXDATA(p_data) ) )
1907             {
1908                 char *psz_attachment;
1909                 if ( -1 != asprintf( &psz_attachment, "attachment://%s/covr/data[0]",
1910                                      psz_meta_roots[i_index] ) )
1911                 {
1912                     vlc_meta_SetArtURL( p_meta, psz_attachment );
1913                     b_attachment_set = true;
1914                     free( psz_attachment );
1915                 }
1916             }
1917         }
1918     }
1919
1920     const MP4_Box_t *p_pnot;
1921     if ( !b_attachment_set && (p_pnot = MP4_BoxGet( p_sys->p_root, "pnot" )) )
1922     {
1923         for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms) && !b_attachment_set; i++ )
1924         {
1925             if ( rgi_pict_atoms[i] == BOXDATA(p_pnot)->i_type )
1926             {
1927                 char rgsz_path[26];
1928                 snprintf( rgsz_path, 26, "attachment://%4.4s[%"PRIu16"]",
1929                           (char*)&rgi_pict_atoms[i], BOXDATA(p_pnot)->i_index - 1 );
1930                 vlc_meta_SetArtURL( p_meta, rgsz_path );
1931                 b_attachment_set = true;
1932             }
1933         }
1934     }
1935
1936     if( p_udta == NULL )
1937     {
1938         if( !b_attachment_set )
1939             return VLC_EGENERIC;
1940     }
1941     else SetupMeta( p_meta, p_udta );
1942
1943     return VLC_SUCCESS;
1944 }
1945
1946 /*****************************************************************************
1947  * Control:
1948  *****************************************************************************/
1949 static int Control( demux_t *p_demux, int i_query, va_list args )
1950 {
1951     demux_sys_t *p_sys = p_demux->p_sys;
1952
1953     double f, *pf;
1954     vlc_tick_t i64;
1955     bool b;
1956
1957     const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1958
1959     switch( i_query )
1960     {
1961         case DEMUX_CAN_SEEK:
1962             *va_arg( args, bool * ) = p_sys->b_seekable;
1963             return VLC_SUCCESS;
1964
1965         case DEMUX_GET_POSITION:
1966             pf = va_arg( args, double * );
1967             if( i_duration > 0 )
1968             {
1969                 *pf = (double)p_sys->i_nztime /
1970                       MP4_rescale_mtime( i_duration, p_sys->i_timescale );
1971             }
1972             else
1973             {
1974                 *pf = 0.0;
1975             }
1976             return VLC_SUCCESS;
1977
1978         case DEMUX_GET_SEEKPOINT:
1979             *va_arg( args, int * ) = p_sys->i_seekpoint;
1980             return VLC_SUCCESS;
1981
1982         case DEMUX_SET_POSITION:
1983             f = va_arg( args, double );
1984             b = va_arg( args, int );
1985             if ( p_demux->pf_demux == DemuxFrag )
1986                 return FragSeekToPos( p_demux, f, b );
1987             else if( p_sys->i_timescale > 0 )
1988             {
1989                 i64 = (vlc_tick_t)( f * MP4_rescale_mtime( p_sys->i_duration,
1990                                                         p_sys->i_timescale ) );
1991                 return Seek( p_demux, i64, b );
1992             }
1993             else return VLC_EGENERIC;
1994
1995         case DEMUX_GET_TIME:
1996             *va_arg( args, vlc_tick_t * ) = p_sys->i_timescale > 0 ? p_sys->i_nztime : 0;
1997             return VLC_SUCCESS;
1998
1999         case DEMUX_SET_TIME:
2000             i64 = va_arg( args, vlc_tick_t );
2001             b = va_arg( args, int );
2002             if ( p_demux->pf_demux == DemuxFrag )
2003                 return FragSeekToTime( p_demux, i64, b );
2004             else
2005                 return Seek( p_demux, i64, b );
2006
2007         case DEMUX_GET_LENGTH:
2008             if( p_sys->i_timescale > 0 )
2009             {
2010                 *va_arg( args, vlc_tick_t * ) = MP4_rescale_mtime( i_duration,
2011                                                            p_sys->i_timescale );
2012             }
2013             else *va_arg( args, vlc_tick_t * ) = 0;
2014             return VLC_SUCCESS;
2015
2016         case DEMUX_GET_FPS:
2017             pf = va_arg( args, double * );
2018             *pf = p_sys->f_fps;
2019             return VLC_SUCCESS;
2020
2021         case DEMUX_GET_ATTACHMENTS:
2022         {
2023             input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
2024             int *pi_int = va_arg( args, int * );
2025
2026             MP4_Box_t *p_udta = NULL;
2027             size_t i_count = 0;
2028             int i_index = 0;
2029
2030             /* Count number of total attachments */
2031             for( ; psz_meta_roots[i_index] && !p_udta; i_index++ )
2032             {
2033                 p_udta = MP4_BoxGet( p_sys->p_root, psz_meta_roots[i_index] );
2034                 if ( p_udta )
2035                     i_count += MP4_BoxCount( p_udta, "covr/data" );
2036             }
2037
2038             for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms); i++ )
2039             {
2040                 char rgsz_path[5];
2041                 snprintf( rgsz_path, 5, "%4.4s", (char*)&rgi_pict_atoms[i] );
2042                 i_count += MP4_BoxCount( p_sys->p_root, rgsz_path );
2043             }
2044
2045             if ( i_count == 0 )
2046                 return VLC_EGENERIC;
2047
2048             *ppp_attach = (input_attachment_t**)
2049                     vlc_alloc( i_count, sizeof(input_attachment_t*) );
2050             if( !(*ppp_attach) ) return VLC_ENOMEM;
2051
2052             /* First add cover attachments */
2053             i_count = 0;
2054             size_t i_box_count = 0;
2055             if ( p_udta )
2056             {
2057                 const MP4_Box_t *p_data = MP4_BoxGet( p_udta, "covr/data" );
2058                 for( ; p_data; p_data = p_data->p_next )
2059                 {
2060                     char *psz_mime;
2061                     char *psz_filename;
2062                     i_box_count++;
2063
2064                     if ( p_data->i_type != ATOM_data || !imageTypeCompatible( BOXDATA(p_data) ) )
2065                         continue;
2066
2067                     switch( BOXDATA(p_data)->e_wellknowntype )
2068                     {
2069                     case DATA_WKT_PNG:
2070                         psz_mime = strdup( "image/png" );
2071                         break;
2072                     case DATA_WKT_JPEG:
2073                         psz_mime = strdup( "image/jpeg" );
2074                         break;
2075                     case DATA_WKT_BMP:
2076                         psz_mime = strdup( "image/bmp" );
2077                         break;
2078                     default:
2079                         continue;
2080                     }
2081
2082                     if ( asprintf( &psz_filename, "%s/covr/data[%"PRIu64"]", psz_meta_roots[i_index - 1],
2083                                    (uint64_t) i_box_count - 1 ) >= 0 )
2084                     {
2085                         (*ppp_attach)[i_count++] =
2086                             vlc_input_attachment_New( psz_filename, psz_mime, "Cover picture",
2087                                 BOXDATA(p_data)->p_blob, BOXDATA(p_data)->i_blob );
2088                         msg_Dbg( p_demux, "adding attachment %s", psz_filename );
2089                         free( psz_filename );
2090                     }
2091
2092                     free( psz_mime );
2093                 }
2094             }
2095
2096             /* Then quickdraw pict ones */
2097             for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms); i++ )
2098             {
2099                 char rgsz_path[5];
2100                 snprintf( rgsz_path, 5, "%4.4s", (char*)&rgi_pict_atoms[i] );
2101                 const MP4_Box_t *p_pict = MP4_BoxGet( p_sys->p_root, rgsz_path );
2102                 i_box_count = 0;
2103                 for( ; p_pict; p_pict = p_pict->p_next )
2104                 {
2105                     if ( i_box_count++ == UINT16_MAX ) /* pnot only handles 2^16 */
2106                         break;
2107                     if ( p_pict->i_type != rgi_pict_atoms[i] )
2108                         continue;
2109                     char rgsz_location[12];
2110                     snprintf( rgsz_location, 12, "%4.4s[%"PRIu16"]", (char*)&rgi_pict_atoms[i],
2111                               (uint16_t) i_box_count - 1 );
2112                     (*ppp_attach)[i_count] = vlc_input_attachment_New( rgsz_location, "image/x-pict",
2113                         "Quickdraw image", p_pict->data.p_binary->p_blob, p_pict->data.p_binary->i_blob );
2114                     if ( !(*ppp_attach)[i_count] )
2115                     {
2116                         i_count = 0;
2117                         break;
2118                     }
2119                     i_count++;
2120                     msg_Dbg( p_demux, "adding attachment %s", rgsz_location );
2121                 }
2122             }
2123
2124             if ( i_count == 0 )
2125             {
2126                 free( *ppp_attach );
2127                 return VLC_EGENERIC;
2128             }
2129
2130             *pi_int = i_count;
2131
2132             return VLC_SUCCESS;
2133         }
2134
2135         case DEMUX_GET_META:
2136         {
2137             vlc_meta_t *p_meta = va_arg( args, vlc_meta_t *);
2138
2139             if( !p_sys->p_meta )
2140                 return VLC_EGENERIC;
2141
2142             vlc_meta_Merge( p_meta, p_sys->p_meta );
2143
2144             return VLC_SUCCESS;
2145         }
2146
2147         case DEMUX_GET_TITLE_INFO:
2148         {
2149             input_title_t ***ppp_title = va_arg( args, input_title_t *** );
2150             int *pi_int = va_arg( args, int* );
2151             int *pi_title_offset = va_arg( args, int* );
2152             int *pi_seekpoint_offset = va_arg( args, int* );
2153
2154             if( !p_sys->p_title )
2155                 return VLC_EGENERIC;
2156
2157             *pi_int = 1;
2158             *ppp_title = malloc( sizeof( input_title_t*) );
2159             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
2160             *pi_title_offset = 0;
2161             *pi_seekpoint_offset = 0;
2162             return VLC_SUCCESS;
2163         }
2164         case DEMUX_SET_TITLE:
2165         {
2166             const int i_title = va_arg( args, int );
2167             if( !p_sys->p_title || i_title != 0 )
2168                 return VLC_EGENERIC;
2169             return VLC_SUCCESS;
2170         }
2171         case DEMUX_SET_SEEKPOINT:
2172         {
2173             const int i_seekpoint = va_arg( args, int );
2174             if( !p_sys->p_title )
2175                 return VLC_EGENERIC;
2176             return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset, true );
2177         }
2178         case DEMUX_TEST_AND_CLEAR_FLAGS:
2179         {
2180             unsigned *restrict flags = va_arg( args, unsigned * );
2181
2182             if ((*flags & INPUT_UPDATE_SEEKPOINT) && p_sys->seekpoint_changed)
2183             {
2184                 *flags = INPUT_UPDATE_SEEKPOINT;
2185                 p_sys->seekpoint_changed = false;
2186             }
2187             else
2188                 *flags = 0;
2189             return VLC_SUCCESS;
2190         }
2191
2192         case DEMUX_GET_PTS_DELAY:
2193         {
2194             for( unsigned int i = 0; i < p_sys->i_tracks; i++ )
2195             {
2196                 const MP4_Box_t *p_load;
2197                 if ( (p_load = MP4_BoxGet( p_sys->track[i].p_track, "load" )) &&
2198                      BOXDATA(p_load)->i_duration > 0 )
2199                 {
2200                     *va_arg(args, vlc_tick_t *) =
2201                             MP4_rescale_mtime( BOXDATA(p_load)->i_duration,
2202                                                p_sys->track[i].i_timescale );
2203                     return VLC_SUCCESS;
2204                 }
2205             }
2206             return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
2207         }
2208         case DEMUX_SET_NEXT_DEMUX_TIME:
2209         case DEMUX_SET_GROUP_DEFAULT:
2210         case DEMUX_SET_GROUP_ALL:
2211         case DEMUX_SET_GROUP_LIST:
2212         case DEMUX_HAS_UNSUPPORTED_META:
2213         case DEMUX_CAN_RECORD:
2214             return VLC_EGENERIC;
2215
2216         case DEMUX_CAN_PAUSE:
2217         case DEMUX_SET_PAUSE_STATE:
2218         case DEMUX_CAN_CONTROL_PACE:
2219             return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
2220
2221         default:
2222             return VLC_EGENERIC;
2223     }
2224 }
2225
2226 /*****************************************************************************
2227  * Close: frees unused data
2228  *****************************************************************************/
2229 static void Close ( vlc_object_t * p_this )
2230 {
2231     demux_t *  p_demux = (demux_t *)p_this;
2232     demux_sys_t *p_sys = p_demux->p_sys;
2233     unsigned int i_track;
2234
2235     msg_Dbg( p_demux, "freeing all memory" );
2236
2237     FragResetContext( p_sys );
2238
2239     MP4_BoxFree( p_sys->p_root );
2240
2241     if( p_sys->p_title )
2242         vlc_input_title_Delete( p_sys->p_title );
2243
2244     if( p_sys->p_meta )
2245         vlc_meta_Delete( p_sys->p_meta );
2246
2247     MP4_Fragments_Index_Delete( p_sys->p_fragsindex );
2248
2249     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
2250         MP4_TrackClean( p_demux->out, &p_sys->track[i_track] );
2251     free( p_sys->track );
2252
2253     free( p_sys );
2254 }
2255
2256
2257
2258 /****************************************************************************
2259  * Local functions, specific to vlc
2260  ****************************************************************************/
2261 /* Chapters */
2262 static void LoadChapterGpac( demux_t  *p_demux, MP4_Box_t *p_chpl )
2263 {
2264     demux_sys_t *p_sys = p_demux->p_sys;
2265
2266     if( BOXDATA(p_chpl)->i_chapter == 0 )
2267         return;
2268
2269     p_sys->p_title = vlc_input_title_New();
2270     for( int i = 0; i < BOXDATA(p_chpl)->i_chapter && p_sys->p_title; i++ )
2271     {
2272         seekpoint_t *s = vlc_seekpoint_New();
2273         if( s == NULL) continue;
2274
2275         s->psz_name = strdup( BOXDATA(p_chpl)->chapter[i].psz_name );
2276         if( s->psz_name == NULL)
2277         {
2278             vlc_seekpoint_Delete( s );;
2279             continue;
2280         }
2281
2282         EnsureUTF8( s->psz_name );
2283         msftime_t offset = BOXDATA(p_chpl)->chapter[i].i_start;
2284         s->i_time_offset = VLC_TICK_FROM_MSFTIME(offset);
2285         TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2286     }
2287 }
2288 static void LoadChapterGoPro( demux_t *p_demux, MP4_Box_t *p_hmmt )
2289 {
2290     demux_sys_t *p_sys = p_demux->p_sys;
2291
2292     p_sys->p_title = vlc_input_title_New();
2293     if( p_sys->p_title )
2294         for( unsigned i = 0; i < BOXDATA(p_hmmt)->i_chapter_count; i++ )
2295         {
2296             seekpoint_t *s = vlc_seekpoint_New();
2297             if( s )
2298             {
2299                 if( asprintf( &s->psz_name, "HiLight tag #%u", i+1 ) != -1 )
2300                     EnsureUTF8( s->psz_name );
2301
2302                 /* HiLights are stored in ms so we convert them to ┬Ás */
2303                 s->i_time_offset = VLC_TICK_FROM_MS( BOXDATA(p_hmmt)->pi_chapter_start[i] );
2304                 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2305             }
2306         }
2307 }
2308 static void LoadChapterApple( demux_t  *p_demux, mp4_track_t *tk )
2309 {
2310     demux_sys_t *p_sys = p_demux->p_sys;
2311
2312     for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
2313     {
2314         const vlc_tick_t i_dts = MP4_TrackGetDTS( p_demux, tk );
2315         vlc_tick_t i_pts_delta;
2316         if ( !MP4_TrackGetPTSDelta( p_demux, tk, &i_pts_delta ) )
2317             i_pts_delta = 0;
2318         uint32_t i_nb_samples = 0;
2319         const uint32_t i_size = MP4_TrackGetReadSize( tk, &i_nb_samples );
2320
2321         if( i_size > 0 && !vlc_stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
2322         {
2323             char p_buffer[256];
2324             const uint32_t i_read = stream_ReadU32( p_demux->s, p_buffer,
2325                                                     __MIN( sizeof(p_buffer), i_size ) );
2326             if( i_read > 2 )
2327             {
2328                 const uint32_t i_string = __MIN( GetWBE(p_buffer), i_read-2 );
2329                 const char *psnz_string = &p_buffer[2];
2330
2331                 seekpoint_t *s = vlc_seekpoint_New();
2332                 if( s == NULL ) continue;
2333
2334                 if( i_string > 1 && !memcmp( psnz_string, "\xFF\xFE", 2 ) )
2335                     s->psz_name = FromCharset( "UTF-16LE", psnz_string, i_string );
2336                 else
2337                     s->psz_name = strndup( psnz_string, i_string );
2338
2339                 if( s->psz_name == NULL )
2340                 {
2341                     vlc_seekpoint_Delete( s );
2342                     continue;
2343                 }
2344
2345                 EnsureUTF8( s->psz_name );
2346                 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
2347
2348                 if( !p_sys->p_title )
2349                     p_sys->p_title = vlc_input_title_New();
2350                 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2351             }
2352         }
2353         if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
2354                               tk->chunk[tk->i_chunk].i_sample_count )
2355             tk->i_chunk++;
2356     }
2357 }
2358 static void LoadChapter( demux_t  *p_demux )
2359 {
2360     demux_sys_t *p_sys = p_demux->p_sys;
2361     MP4_Box_t *p_chpl;
2362     MP4_Box_t *p_hmmt;
2363
2364     if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) &&
2365           BOXDATA(p_chpl) && BOXDATA(p_chpl)->i_chapter > 0 )
2366     {
2367         LoadChapterGpac( p_demux, p_chpl );
2368     }
2369     else if( ( p_hmmt = MP4_BoxGet( p_sys->p_root, "/moov/udta/HMMT" ) ) &&
2370              BOXDATA(p_hmmt) && BOXDATA(p_hmmt)->pi_chapter_start && BOXDATA(p_hmmt)->i_chapter_count > 0 )
2371     {
2372         LoadChapterGoPro( p_demux, p_hmmt );
2373     }
2374     else if( p_sys->p_tref_chap )
2375     {
2376         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2377         unsigned int i, j;
2378
2379         /* Load the first subtitle track like quicktime */
2380         for( i = 0; i < p_chap->i_entry_count; i++ )
2381         {
2382             for( j = 0; j < p_sys->i_tracks; j++ )
2383             {
2384                 mp4_track_t *tk = &p_sys->track[j];
2385                 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
2386                     tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_TX3G )
2387                     break;
2388             }
2389             if( j < p_sys->i_tracks )
2390             {
2391                 LoadChapterApple( p_demux, &p_sys->track[j] );
2392                 break;
2393             }
2394         }
2395     }
2396
2397     /* Add duration if titles are enabled */
2398     if( p_sys->p_title )
2399     {
2400         const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
2401         p_sys->p_title->i_length =
2402                 MP4_rescale_mtime( i_duration, p_sys->i_timescale );
2403     }
2404 }
2405
2406 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
2407 static int TrackCreateChunksIndex( demux_t *p_demux,
2408                                    mp4_track_t *p_demux_track )
2409 {
2410     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
2411     MP4_Box_t *p_stsc;
2412
2413     unsigned int i_chunk;
2414     unsigned int i_index, i_last;
2415
2416     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
2417           !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
2418         ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
2419     {
2420         return( VLC_EGENERIC );
2421     }
2422
2423     p_demux_track->i_chunk_count = BOXDATA(p_co64)->i_entry_count;
2424     if( !p_demux_track->i_chunk_count )
2425     {
2426         msg_Warn( p_demux, "no chunk defined" );
2427     }
2428     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
2429                                    sizeof( mp4_chunk_t ) );
2430     if( p_demux_track->chunk == NULL )
2431     {
2432         return VLC_ENOMEM;
2433     }
2434
2435     /* first we read chunk offset */
2436     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2437     {
2438         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2439
2440         ck->i_offset = BOXDATA(p_co64)->i_chunk_offset[i_chunk];
2441
2442         ck->i_first_dts = 0;
2443         ck->i_entries_dts = 0;
2444         ck->p_sample_count_dts = NULL;
2445         ck->p_sample_delta_dts = NULL;
2446         ck->i_entries_pts = 0;
2447         ck->p_sample_count_pts = NULL;
2448         ck->p_sample_offset_pts = NULL;
2449     }
2450
2451     /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
2452         to be used for the sample XXX begin to 1
2453         We construct it begining at the end */
2454     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
2455     i_index = BOXDATA(p_stsc)->i_entry_count;
2456
2457     while( i_index-- > 0 )
2458     {
2459         for( i_chunk = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
2460              i_chunk < i_last; i_chunk++ )
2461         {
2462             if( i_chunk >= p_demux_track->i_chunk_count )
2463             {
2464                 msg_Warn( p_demux, "corrupted chunk table" );
2465                 return VLC_EGENERIC;
2466             }
2467
2468             p_demux_track->chunk[i_chunk].i_sample_description_index =
2469                     BOXDATA(p_stsc)->i_sample_description_index[i_index];
2470             p_demux_track->chunk[i_chunk].i_sample_count =
2471                     BOXDATA(p_stsc)->i_samples_per_chunk[i_index];
2472         }
2473         i_last = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
2474     }
2475
2476     p_demux_track->i_sample_count = 0;
2477     bool b_broken = false;
2478     if ( p_demux_track->i_chunk_count )
2479     {
2480         p_demux_track->chunk[0].i_sample_first = 0;
2481         p_demux_track->i_sample_count += p_demux_track->chunk[0].i_sample_count;
2482
2483         const mp4_chunk_t *prev = &p_demux_track->chunk[0];
2484         for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2485         {
2486             mp4_chunk_t *cur = &p_demux_track->chunk[i_chunk];
2487             if( unlikely(UINT32_MAX - cur->i_sample_count < p_demux_track->i_sample_count) )
2488             {
2489                 b_broken = true;
2490                 break;
2491             }
2492             p_demux_track->i_sample_count += cur->i_sample_count;
2493             cur->i_sample_first = prev->i_sample_first + prev->i_sample_count;
2494             prev = cur;
2495         }
2496     }
2497
2498     if( unlikely(b_broken) )
2499     {
2500         msg_Err( p_demux, "Overflow in chunks total samples count" );
2501         return VLC_EGENERIC;
2502     }
2503
2504     msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
2505              p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
2506
2507     return VLC_SUCCESS;
2508 }
2509
2510 static int xTTS_CountEntries( demux_t *p_demux, uint32_t *pi_entry /* out */,
2511                               const uint32_t i_index,
2512                               uint32_t i_index_samples_left,
2513                               uint32_t i_sample_count,
2514                               const uint32_t *pi_index_sample_count,
2515                               const uint32_t i_table_count )
2516 {
2517     uint32_t i_array_offset;
2518     while( i_sample_count > 0 )
2519     {
2520         if ( likely((UINT32_MAX - i_index) >= *pi_entry) )
2521             i_array_offset = i_index + *pi_entry;
2522         else
2523             return VLC_EGENERIC;
2524
2525         if ( i_array_offset >= i_table_count )
2526         {
2527             msg_Err( p_demux, "invalid index counting total samples %u %u", i_array_offset,  i_table_count );
2528             return VLC_ENOVAR;
2529         }
2530
2531         if ( i_index_samples_left )
2532         {
2533             if ( i_index_samples_left > i_sample_count )
2534             {
2535                 i_index_samples_left -= i_sample_count;
2536                 i_sample_count = 0;
2537                 *pi_entry +=1; /* No samples left, go copy */
2538                 break;
2539             }
2540             else
2541             {
2542                 i_sample_count -= i_index_samples_left;
2543                 i_index_samples_left = 0;
2544                 *pi_entry += 1;
2545                 continue;
2546             }
2547         }
2548         else
2549         {
2550             i_sample_count -= __MIN( i_sample_count, pi_index_sample_count[i_array_offset] );
2551             *pi_entry += 1;
2552         }
2553     }
2554
2555     return VLC_SUCCESS;
2556 }
2557
2558 static int TrackCreateSamplesIndex( demux_t *p_demux,
2559                                     mp4_track_t *p_demux_track )
2560 {
2561     MP4_Box_t *p_box;
2562     MP4_Box_data_stsz_t *stsz;
2563     /* TODO use also stss and stsh table for seeking */
2564     /* FIXME use edit table */
2565
2566     /* Find stsz
2567      *  Gives the sample size for each samples. There is also a stz2 table
2568      *  (compressed form) that we need to implement TODO */
2569     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
2570     if( !p_box )
2571     {
2572         /* FIXME and stz2 */
2573         msg_Warn( p_demux, "cannot find STSZ box" );
2574         return VLC_EGENERIC;
2575     }
2576     stsz = p_box->data.p_stsz;
2577
2578     /* Use stsz table to create a sample number -> sample size table */
2579     if( p_demux_track->i_sample_count != stsz->i_sample_count )
2580     {
2581         msg_Warn( p_demux, "Incorrect total samples stsc %" PRIu32 " <> stsz %"PRIu32 ", "
2582                            " expect truncated media playback",
2583                            p_demux_track->i_sample_count, stsz->i_sample_count );
2584         p_demux_track->i_sample_count = __MIN(p_demux_track->i_sample_count, stsz->i_sample_count);
2585     }
2586
2587     if( stsz->i_sample_size )
2588     {
2589         /* 1: all sample have the same size, so no need to construct a table */
2590         p_demux_track->i_sample_size = stsz->i_sample_size;
2591         p_demux_track->p_sample_size = NULL;
2592     }
2593     else
2594     {
2595         /* 2: each sample can have a different size */
2596         p_demux_track->i_sample_size = 0;
2597         p_demux_track->p_sample_size =
2598             calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
2599         if( p_demux_track->p_sample_size == NULL )
2600             return VLC_ENOMEM;
2601
2602         for( uint32_t i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
2603         {
2604             p_demux_track->p_sample_size[i_sample] =
2605                     stsz->i_entry_size[i_sample];
2606         }
2607     }
2608
2609     if ( p_demux_track->i_chunk_count && p_demux_track->i_sample_size == 0 )
2610     {
2611         const mp4_chunk_t *lastchunk = &p_demux_track->chunk[p_demux_track->i_chunk_count - 1];
2612         if( (uint64_t)lastchunk->i_sample_count + p_demux_track->i_chunk_count - 1 > stsz->i_sample_count )
2613         {
2614             msg_Err( p_demux, "invalid samples table: stsz table is too small" );
2615             return VLC_EGENERIC;
2616         }
2617     }
2618
2619     /* Use stts table to create a sample number -> dts table.
2620      * XXX: if we don't want to waste too much memory, we can't expand
2621      *  the box! so each chunk will contain an "extract" of this table
2622      *  for fast research (problem with raw stream where a sample is sometime
2623      *  just channels*bits_per_sample/8 */
2624
2625      /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
2626       *        differing in 2/2 fields and 1 signedness */
2627
2628     int64_t i_next_dts = 0;
2629     /* Find stts
2630      *  Gives mapping between sample and decoding time
2631      */
2632     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
2633     if( !p_box )
2634     {
2635         msg_Warn( p_demux, "cannot find STTS box" );
2636         return VLC_EGENERIC;
2637     }
2638     else
2639     {
2640         MP4_Box_data_stts_t *stts = p_box->data.p_stts;
2641
2642         msg_Warn( p_demux, "STTS table of %"PRIu32" entries", stts->i_entry_count );
2643
2644         /* Create sample -> dts table per chunk */
2645         uint32_t i_index = 0;
2646         uint32_t i_current_index_samples_left = 0;
2647
2648         for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2649         {
2650             mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2651             uint32_t i_sample_count;
2652
2653             /* save first dts */
2654             ck->i_first_dts = i_next_dts;
2655
2656             /* count how many entries are needed for this chunk
2657              * for p_sample_delta_dts and p_sample_count_dts */
2658             ck->i_entries_dts = 0;
2659
2660             int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_dts, i_index,
2661                                            i_current_index_samples_left,
2662                                            ck->i_sample_count,
2663                                            stts->pi_sample_count,
2664                                            stts->i_entry_count );
2665             if ( i_ret == VLC_EGENERIC )
2666                 return i_ret;
2667
2668             /* allocate them */
2669             ck->p_sample_count_dts = calloc( ck->i_entries_dts, sizeof( uint32_t ) );
2670             ck->p_sample_delta_dts = calloc( ck->i_entries_dts, sizeof( uint32_t ) );
2671             if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
2672             {
2673                 free( ck->p_sample_count_dts );
2674                 free( ck->p_sample_delta_dts );
2675                 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_dts );
2676                 ck->i_entries_dts = 0;
2677                 return VLC_ENOMEM;
2678             }
2679
2680             /* now copy */
2681             i_sample_count = ck->i_sample_count;
2682
2683             for( uint32_t i = 0; i < ck->i_entries_dts; i++ )
2684             {
2685                 if ( i_current_index_samples_left )
2686                 {
2687                     if ( i_current_index_samples_left > i_sample_count )
2688                     {
2689                         ck->p_sample_count_dts[i] = i_sample_count;
2690                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2691                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2692                         if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts;
2693                         i_current_index_samples_left -= i_sample_count;
2694                         i_sample_count = 0;
2695                         assert( i == ck->i_entries_dts - 1 );
2696                         break;
2697                     }
2698                     else
2699                     {
2700                         ck->p_sample_count_dts[i] = i_current_index_samples_left;
2701                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2702                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2703                         if ( i_current_index_samples_left ) ck->i_duration = i_next_dts - ck->i_first_dts;
2704                         i_sample_count -= i_current_index_samples_left;
2705                         i_current_index_samples_left = 0;
2706                         i_index++;
2707                     }
2708                 }
2709                 else
2710                 {
2711                     if ( stts->pi_sample_count[i_index] > i_sample_count )
2712                     {
2713                         ck->p_sample_count_dts[i] = i_sample_count;
2714                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2715                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2716                         if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts;
2717                         i_current_index_samples_left = stts->pi_sample_count[i_index] - i_sample_count;
2718                         i_sample_count = 0;
2719                         assert( i == ck->i_entries_dts - 1 );
2720                         // keep building from same index
2721                     }
2722                     else
2723                     {
2724                         ck->p_sample_count_dts[i] = stts->pi_sample_count[i_index];
2725                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2726                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2727                         if ( stts->pi_sample_count[i_index] ) ck->i_duration = i_next_dts - ck->i_first_dts;
2728                         i_sample_count -= stts->pi_sample_count[i_index];
2729                         i_index++;
2730                     }
2731                 }
2732
2733             }
2734         }
2735     }
2736
2737
2738     /* Find ctts
2739      *  Gives the delta between decoding time (dts) and composition table (pts)
2740      */
2741     p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
2742     if( p_box && p_box->data.p_ctts )
2743     {
2744         MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
2745
2746         msg_Warn( p_demux, "CTTS table of %"PRIu32" entries", ctts->i_entry_count );
2747
2748         int64_t i_cts_shift = 0;
2749         const MP4_Box_t *p_cslg = MP4_BoxGet( p_demux_track->p_stbl, "cslg" );
2750         if( p_cslg && BOXDATA(p_cslg) )
2751             i_cts_shift = BOXDATA(p_cslg)->ct_to_dts_shift;
2752
2753         /* Create pts-dts table per chunk */
2754         uint32_t i_index = 0;
2755         uint32_t i_current_index_samples_left = 0;
2756
2757         for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2758         {
2759             mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2760             uint32_t i_sample_count;
2761
2762             /* count how many entries are needed for this chunk
2763              * for p_sample_offset_pts and p_sample_count_pts */
2764             ck->i_entries_pts = 0;
2765             int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_pts, i_index,
2766                                            i_current_index_samples_left,
2767                                            ck->i_sample_count,
2768                                            ctts->pi_sample_count,
2769                                            ctts->i_entry_count );
2770             if ( i_ret == VLC_EGENERIC )
2771                 return i_ret;
2772
2773             /* allocate them */
2774             ck->p_sample_count_pts = calloc( ck->i_entries_pts, sizeof( uint32_t ) );
2775             ck->p_sample_offset_pts = calloc( ck->i_entries_pts, sizeof( int32_t ) );
2776             if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
2777             {
2778                 free( ck->p_sample_count_pts );
2779                 free( ck->p_sample_offset_pts );
2780                 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_pts );
2781                 ck->i_entries_pts = 0;
2782                 return VLC_ENOMEM;
2783             }
2784
2785             /* now copy */
2786             i_sample_count = ck->i_sample_count;
2787
2788             for( uint32_t i = 0; i < ck->i_entries_pts; i++ )
2789             {
2790                 if ( i_current_index_samples_left )
2791                 {
2792                     if ( i_current_index_samples_left > i_sample_count )
2793                     {
2794                         ck->p_sample_count_pts[i] = i_sample_count;
2795                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2796                         i_current_index_samples_left -= i_sample_count;
2797                         i_sample_count = 0;
2798                         assert( i == ck->i_entries_pts - 1 );
2799                         break;
2800                     }
2801                     else
2802                     {
2803                         ck->p_sample_count_pts[i] = i_current_index_samples_left;
2804                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2805                         i_sample_count -= i_current_index_samples_left;
2806                         i_current_index_samples_left = 0;
2807                         i_index++;
2808                     }
2809                 }
2810                 else
2811                 {
2812                     if ( ctts->pi_sample_count[i_index] > i_sample_count )
2813                     {
2814                         ck->p_sample_count_pts[i] = i_sample_count;
2815                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2816                         i_current_index_samples_left = ctts->pi_sample_count[i_index] - i_sample_count;
2817                         i_sample_count = 0;
2818                         assert( i == ck->i_entries_pts - 1 );
2819                         // keep building from same index
2820                     }
2821                     else
2822                     {
2823                         ck->p_sample_count_pts[i] = ctts->pi_sample_count[i_index];
2824                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2825                         i_sample_count -= ctts->pi_sample_count[i_index];
2826                         i_index++;
2827                     }
2828                 }
2829
2830
2831             }
2832         }
2833     }
2834
2835     msg_Dbg( p_demux, "track[Id 0x%x] read %"PRIu32" samples length:%"PRId64"s",
2836              p_demux_track->i_track_ID, p_demux_track->i_sample_count,
2837              i_next_dts / p_demux_track->i_timescale );
2838
2839     return VLC_SUCCESS;
2840 }
2841
2842
2843 /**
2844  * It computes the sample rate for a video track using the given sample
2845  * description index
2846  */
2847 static void TrackGetESSampleRate( demux_t *p_demux,
2848                                   unsigned *pi_num, unsigned *pi_den,
2849                                   const mp4_track_t *p_track,
2850                                   unsigned i_sd_index,
2851                                   unsigned i_chunk )
2852 {
2853     demux_sys_t *p_sys = p_demux->p_sys;
2854     *pi_num = 0;
2855     *pi_den = 0;
2856
2857     MP4_Box_t *p_trak = MP4_GetTrakByTrackID( MP4_BoxGet( p_sys->p_root,
2858                                                           "/moov" ),
2859                                               p_track->i_track_ID );
2860     MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
2861     if ( p_mdhd && BOXDATA(p_mdhd) )
2862     {
2863         vlc_ureduce( pi_num, pi_den,
2864                      (uint64_t) BOXDATA(p_mdhd)->i_timescale * p_track->i_sample_count,
2865                      (uint64_t) BOXDATA(p_mdhd)->i_duration,
2866                      UINT16_MAX );
2867         return;
2868     }
2869
2870     if( p_track->i_chunk_count == 0 )
2871         return;
2872
2873     /* */
2874     const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
2875     while( p_chunk > &p_track->chunk[0] &&
2876            p_chunk[-1].i_sample_description_index == i_sd_index )
2877     {
2878         p_chunk--;
2879     }
2880
2881     uint64_t i_sample = 0;
2882     uint64_t i_total_duration = 0;
2883     do
2884     {
2885         i_sample += p_chunk->i_sample_count;
2886         i_total_duration += p_chunk->i_duration;
2887         p_chunk++;
2888     }
2889     while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
2890            p_chunk->i_sample_description_index == i_sd_index );
2891
2892     if( i_sample > 0 && i_total_duration )
2893         vlc_ureduce( pi_num, pi_den,
2894                      i_sample * p_track->i_timescale,
2895                      i_total_duration,
2896                      UINT16_MAX);
2897 }
2898
2899 /*
2900  * TrackCreateES:
2901  * Create ES and PES to init decoder if needed, for a track starting at i_chunk
2902  */
2903 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
2904                           unsigned int i_chunk, es_out_id_t **pp_es )
2905 {
2906     demux_sys_t *p_sys = p_demux->p_sys;
2907     unsigned int i_sample_description_index;
2908
2909     if( p_sys->b_fragmented || p_track->i_chunk_count == 0 )
2910         i_sample_description_index = 1; /* XXX */
2911     else
2912         i_sample_description_index =
2913                 p_track->chunk[i_chunk].i_sample_description_index;
2914
2915     if( pp_es )
2916         *pp_es = NULL;
2917
2918     if( !i_sample_description_index )
2919     {
2920         msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
2921                   p_track->i_track_ID );
2922         return VLC_EGENERIC;
2923     }
2924
2925     MP4_Box_t *p_sample = MP4_BoxGet(  p_track->p_stsd, "[%d]",
2926                             i_sample_description_index - 1 );
2927
2928     if( !p_sample ||
2929         ( !p_sample->data.p_payload && p_track->fmt.i_cat != SPU_ES ) )
2930     {
2931         msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
2932                   p_track->i_track_ID );
2933         return VLC_EGENERIC;
2934     }
2935
2936     p_track->p_sample = p_sample;
2937
2938     MP4_Box_t   *p_frma;
2939     if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) && p_frma->data.p_frma )
2940     {
2941         msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
2942
2943         p_sample->i_type = p_frma->data.p_frma->i_type;
2944     }
2945
2946     /* */
2947     switch( p_track->fmt.i_cat )
2948     {
2949     case VIDEO_ES:
2950         if ( p_sample->i_handler != ATOM_vide ||
2951              !SetupVideoES( p_demux, p_track, p_sample ) )
2952             return VLC_EGENERIC;
2953
2954         /* Set frame rate */
2955         TrackGetESSampleRate( p_demux,
2956                               &p_track->fmt.video.i_frame_rate,
2957                               &p_track->fmt.video.i_frame_rate_base,
2958                               p_track, i_sample_description_index, i_chunk );
2959
2960         p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
2961                        (float)p_track->fmt.video.i_frame_rate_base;
2962
2963         break;
2964
2965     case AUDIO_ES:
2966         if ( p_sample->i_handler != ATOM_soun ||
2967              !SetupAudioES( p_demux, p_track, p_sample ) )
2968             return VLC_EGENERIC;
2969         if( p_sys->p_meta )
2970         {
2971             audio_replay_gain_t *p_arg = &p_track->fmt.audio_replay_gain;
2972             const char *psz_meta = vlc_meta_GetExtra( p_sys->p_meta, "replaygain_track_gain" );
2973             if( psz_meta )
2974             {
2975                 double f_gain = us_atof( psz_meta );
2976                 p_arg->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = f_gain;
2977                 p_arg->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = f_gain != 0;
2978             }
2979             psz_meta = vlc_meta_GetExtra( p_sys->p_meta, "replaygain_track_peak" );
2980             if( psz_meta )
2981             {
2982                 double f_gain = us_atof( psz_meta );
2983                 p_arg->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = f_gain;
2984                 p_arg->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = f_gain > 0;
2985             }
2986         }
2987         break;
2988
2989     case SPU_ES:
2990         if ( ( p_sample->i_handler != ATOM_text &&
2991                p_sample->i_handler != ATOM_subt &&
2992                p_sample->i_handler != ATOM_sbtl ) ||
2993              !SetupSpuES( p_demux, p_track, p_sample ) )
2994            return VLC_EGENERIC;
2995         break;
2996
2997     default:
2998         break;
2999     }
3000
3001     if( pp_es )
3002         *pp_es = MP4_AddTrackES( p_demux->out, p_track );
3003
3004     return ( !pp_es || *pp_es ) ? VLC_SUCCESS : VLC_EGENERIC;
3005 }
3006
3007 /* *** Try to find nearest sync points *** */
3008 static int TrackGetNearestSeekPoint( demux_t *p_demux, mp4_track_t *p_track,
3009                                      uint32_t i_sample, uint32_t *pi_sync_sample )
3010 {
3011     int i_ret = VLC_EGENERIC;
3012     *pi_sync_sample = 0;
3013
3014     const MP4_Box_t *p_stss;
3015     if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
3016     {
3017         const MP4_Box_data_stss_t *p_stss_data = BOXDATA(p_stss);
3018         msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
3019                  p_track->i_track_ID );
3020         for( unsigned i_index = 0; i_index < p_stss_data->i_entry_count; i_index++ )
3021         {
3022             if( i_index >= p_stss_data->i_entry_count - 1 ||
3023                 i_sample < p_stss_data->i_sample_number[i_index+1] )
3024             {
3025                 *pi_sync_sample = p_stss_data->i_sample_number[i_index];
3026                 msg_Dbg( p_demux, "stss gives %d --> %" PRIu32 " (sample number)",
3027                          i_sample, *pi_sync_sample );
3028                 i_ret = VLC_SUCCESS;
3029                 break;
3030             }
3031         }
3032     }
3033
3034     /* try rap samples groups */
3035     const MP4_Box_t *p_sbgp = MP4_BoxGet( p_track->p_stbl, "sbgp" );
3036     for( ; p_sbgp; p_sbgp = p_sbgp->p_next )
3037     {
3038         const MP4_Box_data_sbgp_t *p_sbgp_data = BOXDATA(p_sbgp);
3039         if( p_sbgp->i_type != ATOM_sbgp || !p_sbgp_data )
3040             continue;
3041
3042         if( p_sbgp_data->i_grouping_type == SAMPLEGROUP_rap )
3043         {
3044             uint32_t i_group_sample = 0;
3045             for ( uint32_t i=0; i<p_sbgp_data->i_entry_count; i++ )
3046             {
3047                 /* Sample belongs to rap group ? */
3048                 if( p_sbgp_data->entries.pi_group_description_index[i] != 0 )
3049                 {
3050                     if( i_sample < i_group_sample )
3051                     {
3052                         msg_Dbg( p_demux, "sbgp lookup failed %" PRIu32 " (sample number)",
3053                                  i_sample );
3054                         break;
3055                     }
3056                     else if ( i_sample >= i_group_sample &&
3057                               *pi_sync_sample < i_group_sample )
3058                     {
3059                         *pi_sync_sample = i_group_sample;
3060                         i_ret = VLC_SUCCESS;
3061                     }
3062                 }
3063                 i_group_sample += p_sbgp_data->entries.pi_sample_count[i];
3064             }
3065
3066             if( i_ret == VLC_SUCCESS && *pi_sync_sample )
3067             {
3068                 msg_Dbg( p_demux, "sbgp gives %d --> %" PRIu32 " (sample number)",
3069                          i_sample, *pi_sync_sample );
3070             }
3071         }
3072     }
3073
3074     return i_ret;
3075 }
3076
3077 /* given a time it return sample/chunk
3078  * it also update elst field of the track
3079  */
3080 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
3081                                    vlc_tick_t start, uint32_t *pi_chunk,
3082                                    uint32_t *pi_sample )
3083 {
3084     demux_sys_t *p_sys = p_demux->p_sys;
3085     uint64_t     i_dts;
3086     unsigned int i_sample;
3087     unsigned int i_chunk;
3088     int          i_index;
3089     stime_t      i_start;
3090
3091     /* FIXME see if it's needed to check p_track->i_chunk_count */
3092     if( p_track->i_chunk_count == 0 )
3093         return( VLC_EGENERIC );
3094
3095     /* handle elst (find the correct one) */
3096     MP4_TrackSetELST( p_demux, p_track, start );
3097     if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
3098     {
3099         MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
3100         int64_t i_mvt= MP4_rescale_qtime( start, p_sys->i_timescale );
3101
3102         /* now calculate i_start for this elst */
3103         /* offset */
3104         if( start < MP4_rescale_mtime( p_track->i_elst_time, p_sys->i_timescale ) )
3105         {
3106             *pi_chunk = 0;
3107             *pi_sample= 0;
3108
3109             return VLC_SUCCESS;
3110         }
3111         /* to track time scale */
3112         i_start  = MP4_rescale_qtime( start, p_track->i_timescale );
3113         /* add elst offset */
3114         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
3115              elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
3116             elst->i_media_time[p_track->i_elst] > 0 )
3117         {
3118             i_start += elst->i_media_time[p_track->i_elst];
3119         }
3120
3121         msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
3122                  "ms (track)", p_track->i_elst,
3123                  MP4_rescale( i_mvt, p_sys->i_timescale, 1000 ),
3124                  MP4_rescale( i_start, p_track->i_timescale, 1000 ) );
3125     }
3126     else
3127     {
3128         /* convert absolute time to in timescale unit */
3129         i_start = MP4_rescale_qtime( start, p_track->i_timescale );
3130     }
3131
3132     /* we start from sample 0/chunk 0, hope it won't take too much time */
3133     /* *** find good chunk *** */
3134     for( i_chunk = 0; ; i_chunk++ )
3135     {
3136         if( i_chunk + 1 >= p_track->i_chunk_count )
3137         {
3138             /* at the end and can't check if i_start in this chunk,
3139                it will be check while searching i_sample */
3140             i_chunk = p_track->i_chunk_count - 1;
3141             break;
3142         }
3143
3144         if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
3145             (uint64_t)i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
3146         {
3147             break;
3148         }
3149     }
3150
3151     /* *** find sample in the chunk *** */
3152     i_sample = p_track->chunk[i_chunk].i_sample_first;
3153     i_dts    = p_track->chunk[i_chunk].i_first_dts;
3154     for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
3155     {
3156         if( i_dts +
3157             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
3158             p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
3159         {
3160             i_dts    +=
3161                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
3162                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
3163
3164             i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
3165             i_index++;
3166         }
3167         else
3168         {
3169             if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
3170             {
3171                 break;
3172             }
3173             i_sample += ( i_start - i_dts ) /
3174                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
3175             break;
3176         }
3177     }
3178
3179     if( i_sample >= p_track->i_sample_count )
3180     {
3181         msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
3182                   "(seeking too far) chunk=%d sample=%d",
3183                   p_track->i_track_ID, i_chunk, i_sample );
3184         return( VLC_EGENERIC );
3185     }
3186
3187
3188     /* *** Try to find nearest sync points *** */
3189     uint32_t i_sync_sample;
3190     if( VLC_SUCCESS ==
3191         TrackGetNearestSeekPoint( p_demux, p_track, i_sample, &i_sync_sample ) )
3192     {
3193         /* Go to chunk */
3194         if( i_sync_sample <= i_sample )
3195         {
3196             while( i_chunk > 0 &&
3197                    i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
3198                 i_chunk--;
3199         }
3200         else
3201         {
3202             while( i_chunk < p_track->i_chunk_count - 1 &&
3203                    i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
3204                                     p_track->chunk[i_chunk].i_sample_count )
3205                 i_chunk++;
3206         }
3207         i_sample = i_sync_sample;
3208     }
3209
3210     *pi_chunk  = i_chunk;
3211     *pi_sample = i_sample;
3212
3213     return VLC_SUCCESS;
3214 }
3215
3216 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
3217                                  unsigned int i_chunk, unsigned int i_sample )
3218 {
3219     bool b_reselect = false;
3220
3221     /* now see if actual es is ok */
3222     if( p_track->i_chunk >= p_track->i_chunk_count ||
3223         p_track->chunk[p_track->i_chunk].i_sample_description_index !=
3224             p_track->chunk[i_chunk].i_sample_description_index )
3225     {
3226         msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
3227                   p_track->i_track_ID );
3228
3229         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
3230                         p_track->p_es, &b_reselect );
3231
3232         es_out_Del( p_demux->out, p_track->p_es );
3233
3234         p_track->p_es = NULL;
3235
3236         if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
3237         {
3238             msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
3239                      p_track->i_track_ID );
3240
3241             p_track->b_ok       = false;
3242             p_track->b_selected = false;
3243             return VLC_EGENERIC;
3244         }
3245     }
3246
3247     /* select again the new decoder */
3248     if( b_reselect )
3249     {
3250         es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
3251     }
3252
3253     p_track->i_chunk    = i_chunk;
3254     p_track->chunk[i_chunk].i_sample = i_sample - p_track->chunk[i_chunk].i_sample_first;
3255     p_track->i_sample   = i_sample;
3256
3257     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
3258 }
3259 #if 0
3260 static void MP4_TrackRestart( demux_t *p_demux, mp4_track_t *p_track,
3261                               MP4_Box_t *p_params_box )
3262 {
3263     bool b_reselect = false;
3264     if( p_track->p_es )
3265     {
3266         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
3267                         p_track->p_es, &b_reselect );
3268     }
3269
3270     /* Save previous fragmented pos */
3271     uint32_t i_sample_pos_backup = p_track->i_sample;
3272     vlc_tick_t time_backup = p_track->i_time;
3273     uint32_t timescale_backup = p_track->i_timescale;
3274
3275     /* Save previous format and ES */
3276     es_format_t fmtbackup;
3277     es_out_id_t *p_es_backup = p_track->p_es;
3278     p_track->p_es = NULL;
3279     es_format_Copy( &fmtbackup, &p_track->fmt );
3280     es_format_Clean( &p_track->fmt );
3281
3282
3283     /* do the cleanup and recycle track / restart */
3284     MP4_TrackDestroy( p_demux, p_track );
3285     memset( p_track, 0, sizeof(*p_track) );
3286
3287     assert(p_params_box->i_type == ATOM_trak);
3288     MP4_TrackCreate( p_demux, p_track, p_params_box, false, true );
3289
3290     if( p_track->b_ok )
3291     {
3292         if( !es_format_IsSimilar( &fmtbackup, &p_track->fmt ) ||
3293             fmtbackup.i_extra != p_track->fmt.i_extra ||
3294             memcmp( fmtbackup.p_extra, p_track->fmt.p_extra, fmtbackup.i_extra ) )
3295         {
3296             if( p_es_backup )
3297                 es_out_Del( p_demux->out, p_es_backup );
3298
3299             if( !p_track->b_chapters_source )
3300             {
3301                 p_track->p_es = MP4_AddTrackES( p_demux->out, p_track );
3302                 p_track->b_ok = !!p_track->p_es;
3303             }
3304         }
3305         else
3306         {
3307             p_track->p_es = p_es_backup;
3308         }
3309     }
3310     else if( p_es_backup )
3311     {
3312         es_out_Del( p_demux->out, p_es_backup );
3313     }
3314
3315     /* select again the new decoder */
3316     if( b_reselect && p_track->p_es )
3317         es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
3318
3319     es_format_Clean( &fmtbackup );
3320
3321     /* Restore fragmented pos */
3322     p_track->i_sample = i_sample_pos_backup;
3323     p_track->i_time = MP4_rescale( time_backup, timescale_backup, p_track->i_timescale );
3324 }
3325 #endif
3326 /****************************************************************************
3327  * MP4_TrackSetup:
3328  ****************************************************************************
3329  * Parse track information and create all needed data to run a track
3330  * If it succeed b_ok is set to 1 else to 0
3331  ****************************************************************************/
3332 static void MP4_TrackSetup( demux_t *p_demux, mp4_track_t *p_track,
3333                              MP4_Box_t *p_box_trak,
3334                              bool b_create_es, bool b_force_enable )
3335 {
3336     demux_sys_t *p_sys = p_demux->p_sys;
3337
3338     p_track->p_track = p_box_trak;
3339
3340     char language[4] = { '\0' };
3341     char sdp_media_type[8] = { '\0' };
3342
3343     const MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
3344     if( !p_tkhd )
3345     {
3346         return;
3347     }
3348
3349     /* do we launch this track by default ? */
3350     p_track->b_enable =
3351         ( ( BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED ) != 0 );
3352
3353     p_track->i_track_ID = BOXDATA(p_tkhd)->i_track_ID;
3354
3355     p_track->i_width = BOXDATA(p_tkhd)->i_width / BLOCK16x16;
3356     p_track->i_height = BOXDATA(p_tkhd)->i_height / BLOCK16x16;
3357     p_track->f_rotation = BOXDATA(p_tkhd)->f_rotation;
3358
3359     /* FIXME: unhandled box: tref */
3360
3361     const MP4_Box_t *p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
3362     const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
3363
3364     if( ( !p_mdhd )||( !p_hdlr ) )
3365     {
3366         return;
3367     }
3368
3369     if( BOXDATA(p_mdhd)->i_timescale == 0 )
3370     {
3371         msg_Warn( p_demux, "Invalid track timescale " );
3372         return;
3373     }
3374     p_track->i_timescale = BOXDATA(p_mdhd)->i_timescale;
3375
3376     memcpy( &language, BOXDATA(p_mdhd)->rgs_language, 3 );
3377     p_track->b_mac_encoding = BOXDATA(p_mdhd)->b_mac_encoding;
3378
3379     switch( p_hdlr->data.p_hdlr->i_handler_type )
3380     {
3381         case( ATOM_soun ):
3382             if( !MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) )
3383             {
3384                 return;
3385             }
3386             es_format_Change( &p_track->fmt, AUDIO_ES, 0 );
3387             break;
3388
3389         case( ATOM_pict ): /* heif */
3390             es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3391             break;
3392
3393         case( ATOM_vide ):
3394             if( !MP4_BoxGet( p_box_trak, "mdia/minf/vmhd") )
3395             {
3396                 return;
3397             }
3398             es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3399             break;
3400
3401         case( ATOM_hint ):
3402             /* RTP Reception Hint tracks */
3403             if( !MP4_BoxGet( p_box_trak, "mdia/minf/hmhd" ) ||
3404                 !MP4_BoxGet( p_box_trak, "mdia/minf/stbl/stsd/rrtp" ) )
3405             {
3406                 break;
3407             }
3408             MP4_Box_t *p_sdp;
3409
3410             /* parse the sdp message to find out whether the RTP stream contained audio or video */
3411             if( !( p_sdp  = MP4_BoxGet( p_box_trak, "udta/hnti/sdp " ) ) )
3412             {
3413                 msg_Warn( p_demux, "Didn't find sdp box to determine stream type" );
3414                 return;
3415             }
3416
3417             memcpy( sdp_media_type, BOXDATA(p_sdp)->psz_text, 7 );
3418             if( !strcmp(sdp_media_type, "m=audio") )
3419             {
3420                 msg_Dbg( p_demux, "Found audio Rtp: %s", sdp_media_type );
3421                 es_format_Change( &p_track->fmt, AUDIO_ES, 0 );
3422             }
3423             else if( !strcmp(sdp_media_type, "m=video") )
3424             {
3425                 msg_Dbg( p_demux, "Found video Rtp: %s", sdp_media_type );
3426                 es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3427             }
3428             else
3429             {
3430                 msg_Warn( p_demux, "Malformed track SDP message: %s", sdp_media_type );
3431                 return;
3432             }
3433             p_track->p_sdp = p_sdp;
3434             break;
3435
3436         case( ATOM_tx3g ):
3437         case( ATOM_text ):
3438         case( ATOM_subp ):
3439         case( ATOM_subt ): /* ttml */
3440         case( ATOM_sbtl ):
3441         case( ATOM_clcp ): /* closed captions */
3442             es_format_Change( &p_track->fmt, SPU_ES, 0 );
3443             break;
3444
3445         default:
3446             return;
3447     }
3448
3449     p_track->asfinfo.i_cat = p_track->fmt.i_cat;
3450
3451     const MP4_Box_t *p_elst;
3452     p_track->i_elst = 0;
3453     p_track->i_elst_time = 0;
3454     if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
3455     {
3456         MP4_Box_data_elst_t *elst = BOXDATA(p_elst);
3457         unsigned int i;
3458
3459         msg_Warn( p_demux, "elst box found" );
3460         for( i = 0; i < elst->i_entry_count; i++ )
3461         {
3462             msg_Dbg( p_demux, "   - [%d] duration=%"PRId64"ms media time=%"PRId64
3463                      "ms) rate=%d.%d", i,
3464                      MP4_rescale( elst->i_segment_duration[i], p_sys->i_timescale, 1000 ),
3465                      elst->i_media_time[i] >= 0 ?
3466                         MP4_rescale( elst->i_media_time[i], p_track->i_timescale, 1000 ) :
3467                         INT64_C(-1),
3468                      elst->i_media_rate_integer[i],
3469                      elst->i_media_rate_fraction[i] );
3470         }
3471     }
3472
3473
3474 /*  TODO
3475     add support for:
3476     p_dinf = MP4_BoxGet( p_minf, "dinf" );
3477 */
3478     if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
3479         !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
3480     {
3481         return;
3482     }
3483
3484     /* Set language */
3485     if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
3486     {
3487         p_track->fmt.psz_language = strdup( language );
3488     }
3489
3490     const MP4_Box_t *p_udta = MP4_BoxGet( p_box_trak, "udta" );
3491     if( p_udta )
3492     {
3493         const MP4_Box_t *p_box_iter;
3494         for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
3495                  p_box_iter = p_box_iter->p_next )
3496         {
3497             switch( p_box_iter->i_type )
3498             {
3499                 case ATOM_0xa9nam:
3500                 case ATOM_name:
3501                     p_track->fmt.psz_description =
3502                         strndup( p_box_iter->data.p_binary->p_blob,
3503                                  p_box_iter->data.p_binary->i_blob );
3504                 default:
3505                     break;
3506             }
3507         }
3508     }
3509
3510     /* Create chunk index table and sample index table */
3511     if( TrackCreateChunksIndex( p_demux,p_track  ) ||
3512         TrackCreateSamplesIndex( p_demux, p_track ) )
3513     {
3514         msg_Err( p_demux, "cannot create chunks index" );
3515         return; /* cannot create chunks index */
3516     }
3517
3518     p_track->i_chunk  = 0;
3519     p_track->i_sample = 0;
3520
3521     /* Mark chapter only track */
3522     if( p_sys->p_tref_chap )
3523     {
3524         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
3525         unsigned int i;
3526
3527         for( i = 0; i < p_chap->i_entry_count; i++ )
3528         {
3529             if( p_track->i_track_ID == p_chap->i_track_ID[i] &&
3530                 p_track->fmt.i_cat == UNKNOWN_ES )
3531             {
3532                 p_track->b_chapters_source = true;
3533                 p_track->b_enable = false;
3534                 break;
3535             }
3536         }
3537     }
3538
3539     const MP4_Box_t *p_tsel;
3540     /* now create es */
3541     if( b_force_enable &&
3542         ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
3543     {
3544         msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
3545                   p_track->i_track_ID );
3546         p_track->b_enable = true;
3547         p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN;
3548     }
3549     else if ( (p_tsel = MP4_BoxGet( p_box_trak, "udta/tsel" )) )
3550     {
3551         if ( BOXDATA(p_tsel) && BOXDATA(p_tsel)->i_switch_group )
3552         {
3553             p_track->i_switch_group = BOXDATA(p_tsel)->i_switch_group;
3554             int i_priority = ES_PRIORITY_SELECTABLE_MIN;
3555             for ( unsigned int i = 0; i < p_sys->i_tracks; i++ )
3556             {
3557                 const mp4_track_t *p_other = &p_sys->track[i];
3558                 if( p_other && p_other != p_track &&
3559                     p_other->fmt.i_cat == p_track->fmt.i_cat &&
3560                     p_track->i_switch_group == p_other->i_switch_group )
3561                         i_priority = __MAX( i_priority, p_other->fmt.i_priority + 1 );
3562             }
3563             /* VLC only support ES priority for AUDIO_ES and SPU_ES.
3564                If there's another VIDEO_ES in the same group, we need to unselect it then */
3565             if ( p_track->fmt.i_cat == VIDEO_ES && i_priority > ES_PRIORITY_SELECTABLE_MIN )
3566                 p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
3567             else
3568                 p_track->fmt.i_priority = i_priority;
3569         }
3570     }
3571     /* If there's no tsel, try to enable the track coming first in edit list */
3572     else if ( p_track->p_elst && p_track->fmt.i_priority == ES_PRIORITY_SELECTABLE_MIN )
3573     {
3574 #define MAX_SELECTABLE (INT_MAX - ES_PRIORITY_SELECTABLE_MIN)
3575         for ( uint32_t i=0; i<p_track->BOXDATA(p_elst)->i_entry_count; i++ )
3576         {
3577             if ( p_track->BOXDATA(p_elst)->i_media_time[i] >= 0 &&
3578                  p_track->BOXDATA(p_elst)->i_segment_duration[i] )
3579             {
3580                 /* We do selection by inverting start time into priority.
3581                    The track with earliest edit will have the highest prio */
3582                 const int i_time = __MIN( MAX_SELECTABLE, p_track->BOXDATA(p_elst)->i_media_time[i] );
3583                 p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + MAX_SELECTABLE - i_time;
3584                 break;
3585             }
3586         }
3587     }
3588
3589     if( p_sys->hacks.es_cat_filters && (p_sys->hacks.es_cat_filters & p_track->fmt.i_cat) == 0 )
3590     {
3591         p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
3592     }
3593
3594     if( !p_track->b_enable )
3595         p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
3596
3597     if( TrackCreateES( p_demux,
3598                        p_track, p_track->i_chunk,
3599                       (p_track->b_chapters_source || !b_create_es) ? NULL : &p_track->p_es ) )
3600     {
3601         msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
3602                  p_track->i_track_ID );
3603         return;
3604     }
3605
3606     p_track->b_ok = true;
3607 }
3608
3609 static void DestroyChunk( mp4_chunk_t *ck )
3610 {
3611     free( ck->p_sample_count_dts );
3612     free( ck->p_sample_delta_dts );
3613     free( ck->p_sample_count_pts );
3614     free( ck->p_sample_offset_pts );
3615     free( ck->p_sample_size );
3616 }
3617
3618 /****************************************************************************
3619  * MP4_TrackClean:
3620  ****************************************************************************
3621  * Cleans a track created by MP4_TrackCreate.
3622  ****************************************************************************/
3623 static void MP4_TrackClean( es_out_t *out, mp4_track_t *p_track )
3624 {
3625     es_format_Clean( &p_track->fmt );
3626
3627     if( p_track->p_es )
3628         es_out_Del( out, p_track->p_es );
3629
3630     if( p_track->chunk )
3631     {
3632         for( unsigned int i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
3633             DestroyChunk( &p_track->chunk[i_chunk] );
3634     }
3635     free( p_track->chunk );
3636
3637     if( !p_track->i_sample_size )
3638         free( p_track->p_sample_size );
3639
3640     if ( p_track->asfinfo.p_frame )
3641         block_ChainRelease( p_track->asfinfo.p_frame );
3642
3643     free( p_track->context.runs.p_array );
3644 }
3645
3646 static void MP4_TrackInit( mp4_track_t *p_track )
3647 {
3648     memset( p_track, 0, sizeof(mp4_track_t) );
3649     es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
3650     p_track->i_timescale = 1;
3651 }
3652
3653 static void MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track, bool b_select )
3654 {
3655     if( !p_track->b_ok || p_track->b_chapters_source )
3656         return;
3657
3658     if( b_select == p_track->b_selected )
3659         return;
3660
3661     if( !b_select && p_track->p_es )
3662     {
3663         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
3664                         p_track->p_es, false );
3665     }
3666
3667     p_track->b_selected = b_select;
3668 }
3669
3670 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
3671                           vlc_tick_t i_start )
3672 {
3673     uint32_t i_chunk;
3674     uint32_t i_sample;
3675
3676     if( !p_track->b_ok || p_track->b_chapters_source )
3677         return VLC_EGENERIC;
3678
3679     p_track->b_selected = false;
3680
3681     if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
3682                                 &i_chunk, &i_sample ) )
3683     {
3684         msg_Warn( p_demux, "cannot select track[Id 0x%x]",
3685                   p_track->i_track_ID );
3686         return VLC_EGENERIC;
3687     }
3688
3689     p_track->b_selected = true;
3690     if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
3691         p_track->b_selected = true;
3692
3693     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
3694 }
3695
3696
3697 /*
3698  * 3 types: for audio
3699  *
3700  */
3701 static inline uint32_t MP4_GetFixedSampleSize( const mp4_track_t *p_track,
3702                                                const MP4_Box_data_sample_soun_t *p_soun )
3703 {
3704     uint32_t i_size = p_track->i_sample_size;
3705
3706     assert( p_track->i_sample_size != 0 );
3707
3708      /* QuickTime "built-in" support case fixups */
3709     if( p_track->fmt.i_cat == AUDIO_ES &&
3710         p_soun->i_compressionid == 0 && p_track->i_sample_size <= 2 )
3711     {
3712         switch( p_track->fmt.i_codec )
3713         {
3714         case VLC_CODEC_GSM:
3715             i_size = p_soun->i_channelcount;
3716             break;
3717         case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
3718         case ATOM_twos:
3719         case ATOM_sowt:
3720         case ATOM_raw:
3721         case VLC_CODEC_S24L:
3722         case VLC_CODEC_S24B:
3723         case VLC_CODEC_S32L:
3724         case VLC_CODEC_S32B:
3725         case VLC_CODEC_F32L:
3726         case VLC_CODEC_F32B:
3727         case VLC_CODEC_F64L:
3728         case VLC_CODEC_F64B:
3729             if( p_track->i_sample_size < ((p_soun->i_samplesize+7U)/8U) * p_soun->i_channelcount )
3730                 i_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
3731             break;
3732         case VLC_CODEC_ALAW:
3733         case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
3734             i_size = p_soun->i_channelcount;
3735             break;
3736         default:
3737             break;
3738         }
3739     }
3740
3741     return i_size;
3742 }
3743
3744 static uint32_t MP4_TrackGetReadSize( mp4_track_t *p_track, uint32_t *pi_nb_samples )
3745 {
3746     uint32_t i_size = 0;
3747     *pi_nb_samples = 0;
3748
3749     if ( p_track->i_sample == p_track->i_sample_count )
3750         return 0;
3751
3752     if ( p_track->fmt.i_cat != AUDIO_ES )
3753     {
3754         *pi_nb_samples = 1;
3755
3756         if( p_track->i_sample_size == 0 ) /* all sizes are different */
3757             return p_track->p_sample_size[p_track->i_sample];
3758         else
3759             return p_track->i_sample_size;
3760     }
3761     else
3762     {
3763         const MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
3764         const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
3765         uint32_t i_max_samples = p_chunk->i_sample_count - p_chunk->i_sample;
3766
3767         /* Group audio packets so we don't call demux for single sample unit */
3768         if( p_track->fmt.i_original_fourcc == VLC_CODEC_DVD_LPCM &&
3769             p_soun->i_constLPCMframesperaudiopacket &&
3770             p_soun->i_constbytesperaudiopacket )
3771         {
3772             /* uncompressed case */
3773             uint32_t i_packets = i_max_samples / p_soun->i_constLPCMframesperaudiopacket;
3774             if ( UINT32_MAX / p_soun->i_constbytesperaudiopacket < i_packets )
3775                 i_packets = UINT32_MAX / p_soun->i_constbytesperaudiopacket;
3776             *pi_nb_samples = i_packets * p_soun->i_constLPCMframesperaudiopacket;
3777             return i_packets * p_soun->i_constbytesperaudiopacket;
3778         }
3779
3780         if( p_track->fmt.i_original_fourcc == VLC_FOURCC('r','r','t','p') )
3781         {
3782             *pi_nb_samples = 1;
3783             return p_track->i_sample_size;
3784         }
3785
3786         /* all samples have a different size */
3787         if( p_track->i_sample_size == 0 )
3788         {
3789             *pi_nb_samples = 1;
3790             return p_track->p_sample_size[p_track->i_sample];
3791         }
3792
3793         if( p_soun->i_qt_version == 1 )
3794         {
3795             if ( p_soun->i_compressionid == 0xFFFE )
3796             {
3797                 *pi_nb_samples = 1; /* != number of audio samples */
3798                 if ( p_track->i_sample_size )
3799                     return p_track->i_sample_size;
3800                 else
3801                     return p_track->p_sample_size[p_track->i_sample];
3802             }
3803             else if ( p_soun->i_compressionid != 0 || p_soun->i_bytes_per_sample > 1 ) /* compressed */
3804             {
3805                 /* in this case we are dealing with compressed data
3806                    -2 in V1: additional fields are meaningless (VBR and such) */
3807                 *pi_nb_samples = i_max_samples;//p_track->chunk[p_track->i_chunk].i_sample_count;
3808                 if( p_track->fmt.audio.i_blockalign > 1 )
3809                     *pi_nb_samples = p_soun->i_sample_per_packet;
3810                 i_size = *pi_nb_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
3811                 return i_size;
3812             }
3813             else /* uncompressed case */
3814             {
3815                 uint32_t i_packets;
3816                 if( p_track->fmt.audio.i_blockalign > 1 )
3817                     i_packets = 1;
3818                 else
3819                     i_packets = i_max_samples / p_soun->i_sample_per_packet;
3820
3821                 if ( UINT32_MAX / p_soun->i_bytes_per_frame < i_packets )
3822                     i_packets = UINT32_MAX / p_soun->i_bytes_per_frame;
3823
3824                 *pi_nb_samples = i_packets * p_soun->i_sample_per_packet;
3825                 i_size = i_packets * p_soun->i_bytes_per_frame;
3826                 return i_size;
3827             }
3828         }
3829
3830         /* uncompressed v0 (qt) or... not (ISO) */
3831
3832         /* Quicktime built-in support handling */
3833         if( p_soun->i_compressionid == 0 && p_track->i_sample_size == 1 )
3834         {
3835             switch( p_track->fmt.i_codec )
3836             {
3837                 /* sample size is not integer */
3838                 case VLC_CODEC_GSM:
3839                     *pi_nb_samples = 160 * p_track->fmt.audio.i_channels;
3840                     return 33 * p_track->fmt.audio.i_channels;
3841                 case VLC_CODEC_ADPCM_IMA_QT:
3842                     *pi_nb_samples = 64 * p_track->fmt.audio.i_channels;
3843                     return 34 * p_track->fmt.audio.i_channels;
3844                 default:
3845                     break;
3846             }
3847         }
3848
3849         /* More regular V0 cases */
3850         uint32_t i_max_v0_samples;
3851         switch( p_track->fmt.i_codec )
3852         {
3853             /* Compressed samples in V0 */
3854             case VLC_CODEC_AMR_NB:
3855             case VLC_CODEC_AMR_WB:
3856                 i_max_v0_samples = 16;
3857                 break;
3858             case VLC_CODEC_MPGA:
3859             case VLC_CODEC_MP2:
3860             case VLC_CODEC_MP3:
3861             case VLC_CODEC_DTS:
3862             case VLC_CODEC_MP4A:
3863             case VLC_CODEC_A52:
3864                 i_max_v0_samples = 1;
3865                 break;
3866                 /* fixme, reverse using a list of uncompressed codecs */
3867             default:
3868                 /* Read 25ms of samples (uncompressed) */
3869                 i_max_v0_samples = p_track->fmt.audio.i_rate / 40 *
3870                                    p_track->fmt.audio.i_channels;
3871                 if( i_max_v0_samples < 1 )
3872                     i_max_v0_samples = 1;
3873                 break;
3874         }
3875
3876         *pi_nb_samples = 0;
3877         for( uint32_t i=p_track->i_sample;
3878              i<p_chunk->i_sample_first+p_chunk->i_sample_count &&
3879              i<p_track->i_sample_count;
3880              i++ )
3881         {
3882             (*pi_nb_samples)++;
3883             if ( p_track->i_sample_size == 0 )
3884                 i_size += p_track->p_sample_size[i];
3885             else
3886                 i_size += MP4_GetFixedSampleSize( p_track, p_soun );
3887
3888             /* Try to detect compression in ISO */
3889             if(p_soun->i_compressionid != 0)
3890             {
3891                 /* Return only 1 sample */
3892                 break;
3893             }
3894
3895             if ( *pi_nb_samples == i_max_v0_samples )
3896                 break;
3897         }
3898     }
3899
3900     //fprintf( stderr, "size=%d\n", i_size );
3901     return i_size;
3902 }
3903
3904 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
3905 {
3906     unsigned int i_sample;
3907     uint64_t i_pos;
3908
3909     i_pos = p_track->chunk[p_track->i_chunk].i_offset;
3910
3911     if( p_track->i_sample_size )
3912     {
3913         MP4_Box_data_sample_soun_t *p_soun =
3914             p_track->p_sample->data.p_sample_soun;
3915
3916         /* Quicktime builtin support, _must_ ignore sample tables */
3917         if( p_track->fmt.i_cat == AUDIO_ES && p_soun->i_compressionid == 0 &&
3918             p_track->i_sample_size == 1 )
3919         {
3920             switch( p_track->fmt.i_codec )
3921             {
3922             case VLC_CODEC_GSM: /* # Samples > data size */
3923                 i_pos += ( p_track->i_sample -
3924                            p_track->chunk[p_track->i_chunk].i_sample_first ) / 160 * 33;
3925                 return i_pos;
3926             case VLC_CODEC_ADPCM_IMA_QT: /* # Samples > data size */
3927                 i_pos += ( p_track->i_sample -
3928                            p_track->chunk[p_track->i_chunk].i_sample_first ) / 64 * 34;
3929                 return i_pos;
3930             default:
3931                 break;
3932             }
3933         }
3934
3935         if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 ||
3936             p_track->fmt.audio.i_blockalign <= 1 ||
3937             p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame == 0 )
3938         {
3939             i_pos += ( p_track->i_sample -
3940                        p_track->chunk[p_track->i_chunk].i_sample_first ) *
3941                      MP4_GetFixedSampleSize( p_track, p_soun );
3942         }
3943         else
3944         {
3945             /* we read chunk by chunk unless a blockalign is requested */
3946             i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
3947                         p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
3948         }
3949     }
3950     else
3951     {
3952         for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
3953              i_sample < p_track->i_sample; i_sample++ )
3954         {
3955             i_pos += p_track->p_sample_size[i_sample];
3956         }
3957     }
3958
3959     return i_pos;
3960 }
3961
3962 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track, uint32_t i_samples )
3963 {
3964     if ( UINT32_MAX - p_track->i_sample < i_samples )
3965     {
3966         p_track->i_sample = UINT32_MAX;
3967         return VLC_EGENERIC;
3968     }
3969
3970     p_track->i_sample += i_samples;
3971
3972     if( p_track->i_sample >= p_track->i_sample_count )
3973         return VLC_EGENERIC;
3974
3975     /* Have we changed chunk ? */
3976     if( p_track->i_sample >=
3977             p_track->chunk[p_track->i_chunk].i_sample_first +
3978             p_track->chunk[p_track->i_chunk].i_sample_count )
3979     {
3980         if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
3981                                   p_track->i_sample ) )
3982         {
3983             msg_Warn( p_demux, "track[0x%x] will be disabled "
3984                       "(cannot restart decoder)", p_track->i_track_ID );
3985             MP4_TrackSelect( p_demux, p_track, false );
3986             return VLC_EGENERIC;
3987         }
3988     }
3989
3990     /* Have we changed elst */
3991     if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
3992     {
3993         demux_sys_t *p_sys = p_demux->p_sys;
3994         MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
3995         uint64_t i_mvt = MP4_rescale_qtime( MP4_TrackGetDTS( p_demux, p_track ),
3996                                             p_sys->i_timescale );
3997         if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
3998             i_mvt >= p_track->i_elst_time +
3999                      elst->i_segment_duration[p_track->i_elst] )
4000         {
4001             MP4_TrackSetELST( p_demux, p_track,
4002                               MP4_TrackGetDTS( p_demux, p_track ) );
4003         }
4004     }
4005
4006     return VLC_SUCCESS;
4007 }
4008
4009 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
4010                               vlc_tick_t i_time )
4011 {
4012     demux_sys_t *p_sys = p_demux->p_sys;
4013     int         i_elst_last = tk->i_elst;
4014
4015     /* handle elst (find the correct one) */
4016     tk->i_elst      = 0;
4017     tk->i_elst_time = 0;
4018     if( tk->p_elst && tk->BOXDATA(p_elst)->i_entry_count > 0 )
4019     {
4020         MP4_Box_data_elst_t *elst = tk->BOXDATA(p_elst);
4021         int64_t i_mvt= MP4_rescale_qtime( i_time, p_sys->i_timescale );
4022
4023         for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
4024         {
4025             uint64_t i_dur = elst->i_segment_duration[tk->i_elst];
4026
4027             if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
4028             {
4029                 break;
4030             }
4031             tk->i_elst_time += i_dur;
4032         }
4033
4034         if( (unsigned int)tk->i_elst >= elst->i_entry_count )
4035         {
4036             /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
4037             tk->i_elst = elst->i_entry_count - 1;
4038             tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
4039         }
4040
4041         if( elst->i_media_time[tk->i_elst] < 0 )
4042         {
4043             /* track offset */
4044             tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
4045         }
4046     }
4047     if( i_elst_last != tk->i_elst )
4048     {
4049         msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
4050         tk->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
4051     }
4052 }
4053
4054 /******************************************************************************
4055  *     Here are the functions used for fragmented MP4
4056  *****************************************************************************/
4057 #if 0
4058 /**
4059  * Re-init decoder.
4060  * \Note If we call that function too soon,
4061  * before the track has been selected by MP4_TrackSelect
4062  * (during the first execution of Demux), then the track gets disabled
4063  */
4064 static int ReInitDecoder( demux_t *p_demux, const MP4_Box_t *p_root,
4065                           mp4_track_t *p_track )
4066 {
4067     MP4_Box_t *p_paramsbox = MP4_BoxGet( p_root, "/moov/trak[0]" );
4068     if( !p_paramsbox )
4069         return VLC_EGENERIC;
4070
4071     MP4_TrackRestart( p_demux, p_track, p_paramsbox );
4072
4073     /* Temporary hack until we support track selection */
4074     p_track->b_selected = true;
4075     p_track->b_enable = true;
4076
4077     return VLC_SUCCESS;
4078 }
4079 #endif
4080
4081 static stime_t GetCumulatedDuration( demux_t *p_demux )
4082 {
4083     demux_sys_t *p_sys = p_demux->p_sys;
4084     stime_t i_max_duration = 0;
4085
4086     for ( unsigned int i=0; i<p_sys->i_tracks; i++ )
4087     {
4088         stime_t i_track_duration = 0;
4089         MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->p_moov, p_sys->track[i].i_track_ID );
4090         const MP4_Box_t *p_stsz;
4091         const MP4_Box_t *p_tkhd;
4092         if ( (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) &&
4093              (p_stsz = MP4_BoxGet( p_trak, "mdia/minf/stbl/stsz" )) &&
4094              /* duration might be wrong an be set to whole duration :/ */
4095              BOXDATA(p_stsz)->i_sample_count > 0 )
4096         {
4097             i_max_duration = __MAX( (uint64_t)i_max_duration, BOXDATA(p_tkhd)->i_duration );
4098         }
4099
4100         if( p_sys->p_fragsindex )
4101         {
4102             i_track_duration += MP4_Fragment_Index_GetTrackDuration( p_sys->p_fragsindex, i );
4103         }
4104
4105         i_max_duration = __MAX( i_max_duration, i_track_duration );
4106     }
4107
4108     return i_max_duration;
4109 }
4110
4111 static int ProbeIndex( demux_t *p_demux )
4112 {
4113     demux_sys_t *p_sys = p_demux->p_sys;
4114     uint64_t i_stream_size;
4115     uint8_t mfro[MP4_MFRO_BOXSIZE];
4116     assert( p_sys->b_seekable );
4117
4118     if ( MP4_BoxCount( p_sys->p_root, "/mfra" ) )
4119         return VLC_EGENERIC;
4120
4121     i_stream_size = stream_Size( p_demux->s );
4122     if ( ( i_stream_size >> 62 ) ||
4123          ( i_stream_size < MP4_MFRO_BOXSIZE ) ||
4124          ( vlc_stream_Seek( p_demux->s, i_stream_size - MP4_MFRO_BOXSIZE ) != VLC_SUCCESS )
4125        )
4126     {
4127         msg_Dbg( p_demux, "Probing tail for mfro has failed" );
4128         return VLC_EGENERIC;
4129     }
4130
4131     if ( vlc_stream_Read( p_demux->s, &mfro, MP4_MFRO_BOXSIZE ) == MP4_MFRO_BOXSIZE &&
4132          VLC_FOURCC(mfro[4],mfro[5],mfro[6],mfro[7]) == ATOM_mfro &&
4133          GetDWBE( &mfro ) == MP4_MFRO_BOXSIZE )
4134     {
4135         uint32_t i_offset = GetDWBE( &mfro[12] );
4136         msg_Dbg( p_demux, "will read mfra index at %"PRIu64, i_stream_size - i_offset );
4137         if ( i_stream_size > i_offset &&
4138              vlc_stream_Seek( p_demux->s, i_stream_size - i_offset ) == VLC_SUCCESS )
4139         {
4140             msg_Dbg( p_demux, "reading mfra index at %"PRIu64, i_stream_size - i_offset );
4141             const uint32_t stoplist[] = { ATOM_mfra, 0 };
4142             MP4_ReadBoxContainerChildren( p_demux->s, p_sys->p_root, stoplist );
4143         }
4144         return VLC_SUCCESS;
4145     }
4146     return VLC_EGENERIC;
4147 }
4148
4149 static stime_t GetMoovTrackDuration( demux_sys_t *p_sys, unsigned i_track_ID )
4150 {
4151     MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->p_moov, i_track_ID );
4152     const MP4_Box_t *p_stsz;
4153     const MP4_Box_t *p_tkhd;
4154     if ( (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) &&
4155          (p_stsz = MP4_BoxGet( p_trak, "mdia/minf/stbl/stsz" )) &&
4156          /* duration might be wrong an be set to whole duration :/ */
4157          BOXDATA(p_stsz)->i_sample_count > 0 )
4158     {
4159         if( BOXDATA(p_tkhd)->i_duration <= p_sys->i_moov_duration )
4160             return BOXDATA(p_tkhd)->i_duration; /* In movie / mvhd scale */
4161         else
4162             return p_sys->i_moov_duration;
4163     }
4164     return 0;
4165 }
4166
4167 static bool GetMoofTrackDuration( MP4_Box_t *p_moov, MP4_Box_t *p_moof,
4168                                   unsigned i_track_ID, stime_t *p_duration )
4169 {
4170     if ( !p_moof || !p_moov )
4171         return false;
4172
4173     MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
4174     while ( p_traf )
4175     {
4176         if ( p_traf->i_type != ATOM