demux: heif: add preliminary support for AVIF
[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                 case BRAND_avif:
753                 /* We don't yet support f4v, but avformat does. */
754                 case BRAND_f4v:
755                     return VLC_EGENERIC;
756                 default:
757                     break;
758             }
759             break;
760         }
761          default:
762             return VLC_EGENERIC;
763     }
764
765     /* create our structure that will contains all data */
766     p_sys = calloc( 1, sizeof( demux_sys_t ) );
767     if ( !p_sys )
768         return VLC_EGENERIC;
769
770     /* I need to seek */
771     vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
772     if( p_sys->b_seekable )
773         vlc_stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable );
774
775     /*Set exported functions */
776     p_demux->pf_demux = Demux;
777     p_demux->pf_control = Control;
778
779     p_sys->context.i_lastseqnumber = UINT32_MAX;
780
781     p_demux->p_sys = p_sys;
782
783     if( LoadInitFrag( p_demux ) != VLC_SUCCESS )
784         goto error;
785
786     MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
787
788     if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
789     {
790         switch( BOXDATA(p_ftyp)->i_major_brand )
791         {
792             case BRAND_isom:
793                 msg_Dbg( p_demux,
794                          "ISO Media (isom) version %d.",
795                          BOXDATA(p_ftyp)->i_minor_version );
796                 break;
797             case BRAND_3gp4:
798             case BRAND_3gp5:
799             case BRAND_3gp6:
800             case BRAND_3gp7:
801                 msg_Dbg( p_demux, "3GPP Media Release: %4.4s",
802                          (char *)&BOXDATA(p_ftyp)->i_major_brand );
803                 break;
804             case BRAND_qt__:
805                 msg_Dbg( p_demux, "Apple QuickTime media" );
806                 break;
807             case BRAND_isml:
808                 msg_Dbg( p_demux, "PIFF (= isml = fMP4) media" );
809                 break;
810             case BRAND_dash:
811                 msg_Dbg( p_demux, "DASH Stream" );
812                 break;
813             case BRAND_M4A:
814                 msg_Dbg( p_demux, "iTunes audio" );
815                 if( var_InheritBool( p_demux, CFG_PREFIX"m4a-audioonly" ) )
816                     p_sys->hacks.es_cat_filters = AUDIO_ES;
817                 break;
818             default:
819                 msg_Dbg( p_demux,
820                          "unrecognized major media specification (%4.4s).",
821                           (char*)&BOXDATA(p_ftyp)->i_major_brand );
822                 break;
823         }
824         /* also lookup in compatibility list */
825         for(uint32_t i=0; i<BOXDATA(p_ftyp)->i_compatible_brands_count; i++)
826         {
827             if (BOXDATA(p_ftyp)->i_compatible_brands[i] == BRAND_dash)
828             {
829                 msg_Dbg( p_demux, "DASH Stream" );
830             }
831             else if (BOXDATA(p_ftyp)->i_compatible_brands[i] == BRAND_smoo)
832             {
833                 msg_Dbg( p_demux, "Handling VLC Smooth Stream" );
834             }
835         }
836     }
837     else
838     {
839         msg_Dbg( p_demux, "file type box missing (assuming ISO Media)" );
840     }
841
842     /* the file need to have one moov box */
843     p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/moov" );
844     if( unlikely(!p_sys->p_moov) )
845     {
846         p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/foov" );
847         if( !p_sys->p_moov )
848         {
849             msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
850             goto error;
851         }
852         /* we have a free box as a moov, rename it */
853         p_sys->p_moov->i_type = ATOM_moov;
854     }
855
856     p_mvhd = MP4_BoxGet( p_sys->p_moov, "mvhd" );
857     if( p_mvhd && BOXDATA(p_mvhd) && BOXDATA(p_mvhd)->i_timescale )
858     {
859         p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
860         p_sys->i_moov_duration = p_sys->i_duration = BOXDATA(p_mvhd)->i_duration;
861         p_sys->i_cumulated_duration = BOXDATA(p_mvhd)->i_duration;
862     }
863     else
864     {
865         msg_Warn( p_demux, "No valid mvhd found" );
866         goto error;
867     }
868
869     MP4_Box_t *p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" );
870     if( p_rmra != NULL && p_demux->p_input_item != NULL )
871     {
872         int        i_count = MP4_BoxCount( p_rmra, "rmda" );
873         int        i;
874
875         msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
876
877         input_item_t *p_current = p_demux->p_input_item;
878
879         input_item_node_t *p_subitems = input_item_node_Create( p_current );
880
881         for( i = 0; i < i_count; i++ )
882         {
883             MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
884             char      *psz_ref;
885             uint32_t  i_ref_type;
886
887             if( !p_rdrf || !BOXDATA(p_rdrf) || !( psz_ref = strdup( BOXDATA(p_rdrf)->psz_ref ) ) )
888             {
889                 continue;
890             }
891             i_ref_type = BOXDATA(p_rdrf)->i_ref_type;
892
893             msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
894                      psz_ref, (char*)&i_ref_type );
895
896             if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
897             {
898                 if( strstr( psz_ref, "qt5gateQT" ) )
899                 {
900                     msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
901                     free( psz_ref );
902                     continue;
903                 }
904                 if( !strncmp( psz_ref, "http://", 7 ) ||
905                     !strncmp( psz_ref, "rtsp://", 7 ) )
906                 {
907                     ;
908                 }
909                 else
910                 {
911                     char *psz_absolute = vlc_uri_resolve( p_demux->psz_url,
912                                                           psz_ref );
913                     free( psz_ref );
914                     if( psz_absolute == NULL )
915                     {
916                         input_item_node_Delete( p_subitems );
917                         return VLC_ENOMEM;
918                     }
919                     psz_ref = psz_absolute;
920                 }
921                 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
922                 input_item_t *p_item = input_item_New( psz_ref, NULL );
923                 input_item_CopyOptions( p_item, p_current );
924                 input_item_node_AppendItem( p_subitems, p_item );
925                 input_item_Release( p_item );
926             }
927             else
928             {
929                 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
930                          (char*)&BOXDATA(p_rdrf)->i_ref_type );
931             }
932             free( psz_ref );
933         }
934
935         /* FIXME: create a stream_filter sub-module for this */
936         if (es_out_Control(p_demux->out, ES_OUT_POST_SUBNODE, p_subitems))
937             input_item_node_Delete(p_subitems);
938     }
939
940     if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
941     {
942         if( !p_rmra )
943         {
944             msg_Err( p_demux, "cannot find /moov/mvhd" );
945             goto error;
946         }
947         else
948         {
949             msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
950             p_demux->pf_demux = DemuxRef;
951             return VLC_SUCCESS;
952         }
953     }
954     else
955     {
956         p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
957         if( p_sys->i_timescale == 0 )
958         {
959             msg_Err( p_this, "bad timescale" );
960             goto error;
961         }
962     }
963
964     const unsigned i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" );
965     if( i_tracks < 1 )
966     {
967         msg_Err( p_demux, "cannot find any /moov/trak" );
968         goto error;
969     }
970     msg_Dbg( p_demux, "found %u track%c", i_tracks, i_tracks ? 's':' ' );
971
972     if( CreateTracks( p_demux, i_tracks ) != VLC_SUCCESS )
973         goto error;
974
975     /* Search the first chap reference (like quicktime) and
976      * check that at least 1 stream is enabled */
977     p_sys->p_tref_chap = NULL;
978     b_enabled_es = false;
979     for( unsigned i = 0; i < p_sys->i_tracks; i++ )
980     {
981         MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
982
983
984         MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
985         if( p_tkhd && BOXDATA(p_tkhd) && (BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED) )
986             b_enabled_es = true;
987
988         MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
989         if( p_chap && p_chap->data.p_tref_generic &&
990             p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
991             p_sys->p_tref_chap = p_chap;
992     }
993
994     /* Set and store metadata */
995     if( (p_sys->p_meta = vlc_meta_New()) )
996         MP4_LoadMeta( p_sys, p_sys->p_meta );
997
998     /* now process each track and extract all useful information */
999     for( unsigned i = 0; i < p_sys->i_tracks; i++ )
1000     {
1001         MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%u]", i );
1002         MP4_TrackSetup( p_demux, &p_sys->track[i], p_trak, true, !b_enabled_es );
1003
1004         if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapters_source )
1005         {
1006             const char *psz_cat;
1007             switch( p_sys->track[i].fmt.i_cat )
1008             {
1009                 case( VIDEO_ES ):
1010                     psz_cat = "video";
1011                     break;
1012                 case( AUDIO_ES ):
1013                     psz_cat = "audio";
1014                     break;
1015                 case( SPU_ES ):
1016                     psz_cat = "subtitle";
1017                     break;
1018
1019                 default:
1020                     psz_cat = "unknown";
1021                     break;
1022             }
1023
1024             msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
1025                      p_sys->track[i].i_track_ID, psz_cat,
1026                      p_sys->track[i].b_enable ? "enable":"disable",
1027                      p_sys->track[i].fmt.psz_language ?
1028                      p_sys->track[i].fmt.psz_language : "undef" );
1029         }
1030         else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapters_source )
1031         {
1032             msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
1033                      p_sys->track[i].i_track_ID,
1034                      p_sys->track[i].fmt.psz_language ?
1035                      p_sys->track[i].fmt.psz_language : "undef" );
1036         }
1037         else
1038         {
1039             msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
1040                      p_sys->track[i].i_track_ID );
1041         }
1042     }
1043
1044     p_mvex = MP4_BoxGet( p_sys->p_moov, "mvex" );
1045     if( p_mvex != NULL )
1046     {
1047         const MP4_Box_t *p_mehd = MP4_BoxGet( p_mvex, "mehd");
1048         if ( p_mehd && BOXDATA(p_mehd) )
1049         {
1050             if( BOXDATA(p_mehd)->i_fragment_duration > p_sys->i_duration )
1051             {
1052                 p_sys->b_fragmented = true;
1053                 p_sys->i_duration = BOXDATA(p_mehd)->i_fragment_duration;
1054             }
1055         }
1056
1057         const MP4_Box_t *p_sidx = MP4_BoxGet( p_sys->p_root, "sidx");
1058         if( p_sidx )
1059             p_sys->b_fragmented = true;
1060
1061         if ( p_sys->b_seekable )
1062         {
1063             if( !p_sys->b_fragmented /* as unknown */ )
1064             {
1065                 /* Probe remaining to check if there's really fragments
1066                    or if that file is just ready to append fragments */
1067                 ProbeFragments( p_demux, (p_sys->i_duration == 0), &p_sys->b_fragmented );
1068             }
1069
1070             if( vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos ) != VLC_SUCCESS )
1071                 goto error;
1072         }
1073         else /* Handle as fragmented by default as we can't see moof */
1074         {
1075             p_sys->context.p_fragment_atom = p_sys->p_moov;
1076             p_sys->context.i_current_box_type = ATOM_moov;
1077             p_sys->b_fragmented = true;
1078         }
1079     }
1080
1081     if( p_sys->b_fragmented )
1082     {
1083         p_demux->pf_demux = DemuxFrag;
1084         msg_Dbg( p_demux, "Set Fragmented demux mode" );
1085     }
1086
1087     if( !p_sys->b_seekable && p_demux->pf_demux == Demux )
1088     {
1089         msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" );
1090         goto error;
1091     }
1092
1093     if( p_sys->i_tracks > 1 && !p_sys->b_fastseekable )
1094     {
1095         vlc_tick_t i_max_continuity;
1096         bool b_flat;
1097         MP4_GetInterleaving( p_demux, &i_max_continuity, &b_flat );
1098         if( b_flat )
1099             msg_Warn( p_demux, "that media doesn't look interleaved, will need to seek");
1100         else if( i_max_continuity > DEMUX_TRACK_MAX_PRELOAD )
1101             msg_Warn( p_demux, "that media doesn't look properly interleaved, will need to seek");
1102     }
1103
1104     /* */
1105     LoadChapter( p_demux );
1106
1107     p_sys->asfpacketsys.p_demux = p_demux;
1108     p_sys->asfpacketsys.pi_preroll = &p_sys->i_preroll;
1109     p_sys->asfpacketsys.pi_preroll_start = &p_sys->i_preroll_start;
1110     p_sys->asfpacketsys.pf_doskip = NULL;
1111     p_sys->asfpacketsys.pf_send = MP4ASF_Send;
1112     p_sys->asfpacketsys.pf_gettrackinfo = MP4ASF_GetTrackInfo;
1113     p_sys->asfpacketsys.pf_updatetime = NULL;
1114     p_sys->asfpacketsys.pf_setaspectratio = NULL;
1115
1116     return VLC_SUCCESS;
1117
1118 error:
1119     if( vlc_stream_Tell( p_demux->s ) > 0 )
1120     {
1121         if( vlc_stream_Seek( p_demux->s, 0 ) != VLC_SUCCESS )
1122             msg_Warn( p_demux, "Can't reset stream position from probing" );
1123     }
1124
1125     Close( p_this );
1126
1127     return VLC_EGENERIC;
1128 }
1129
1130 const unsigned int SAMPLEHEADERSIZE = 4;
1131 const unsigned int RTPPACKETSIZE = 12;
1132 const unsigned int CONSTRUCTORSIZE = 16;
1133
1134 /*******************************************************************************
1135  * MP4_RTPHintToFrame: converts RTP Reception Hint Track sample to H.264 frame
1136  *******************************************************************************/
1137 static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount )
1138 {
1139     uint8_t *p_slice = p_block->p_buffer + SAMPLEHEADERSIZE;
1140     block_t *p_newblock = NULL;
1141     size_t i_payload = 0;
1142
1143     if( p_block->i_buffer < SAMPLEHEADERSIZE + RTPPACKETSIZE + CONSTRUCTORSIZE )
1144     {
1145         msg_Err( p_demux, "Sample not large enough for necessary structs");
1146         block_Release( p_block );
1147         return NULL;
1148     }
1149
1150     for( uint32_t i = 0; i < packetcount; ++i )
1151     {
1152         if( (size_t)(p_slice - p_block->p_buffer) + RTPPACKETSIZE + CONSTRUCTORSIZE > p_block->i_buffer )
1153             goto error;
1154
1155         /* skip RTP header in sample. Could be used to detect packet losses */
1156         p_slice += RTPPACKETSIZE;
1157
1158         mp4_rtpsampleconstructor_t sample_cons;
1159
1160         sample_cons.type =                      p_slice[0];
1161         sample_cons.trackrefindex =             p_slice[1];
1162         sample_cons.length =          GetWBE(  &p_slice[2] );
1163         sample_cons.samplenumber =    GetDWBE( &p_slice[4] );
1164         sample_cons.sampleoffset =    GetDWBE( &p_slice[8] );
1165         sample_cons.bytesperblock =   GetWBE(  &p_slice[12] );
1166         sample_cons.samplesperblock = GetWBE(  &p_slice[14] );
1167
1168         /* skip packet constructor */
1169         p_slice += CONSTRUCTORSIZE;
1170
1171         /* check that is RTPsampleconstructor, referencing itself and no weird audio stuff */
1172         if( sample_cons.type != 2||sample_cons.trackrefindex != -1
1173             ||sample_cons.samplesperblock != 1||sample_cons.bytesperblock != 1 )
1174         {
1175             msg_Err(p_demux, "Unhandled constructor in RTP Reception Hint Track. Type:%u", sample_cons.type);
1176             goto error;
1177         }
1178
1179         /* slice doesn't fit in buffer */
1180         if( sample_cons.sampleoffset + sample_cons.length > p_block->i_buffer)
1181         {
1182             msg_Err(p_demux, "Sample buffer is smaller than sample" );
1183             goto error;
1184         }
1185
1186         block_t *p_realloc = ( p_newblock ) ?
1187                              block_Realloc( p_newblock, 0, i_payload + sample_cons.length + 4 ):
1188                              block_Alloc( i_payload + sample_cons.length + 4 );
1189         if( !p_realloc )
1190             goto error;
1191
1192         p_newblock = p_realloc;
1193         uint8_t *p_dst = &p_newblock->p_buffer[i_payload];
1194
1195         const uint8_t* p_src = p_block->p_buffer + sample_cons.sampleoffset;
1196         uint8_t i_type = (*p_src) & ((1<<5)-1);
1197
1198         const uint8_t synccode[4] = { 0, 0, 0, 1 };
1199         if( memcmp( p_src, synccode, 4 ) )
1200         {
1201             if( i_type == 7 || i_type == 8 )
1202                 *p_dst++=0;
1203
1204             p_dst[0] = 0;
1205             p_dst[1] = 0;
1206             p_dst[2] = 1;
1207             p_dst += 3;
1208         }
1209
1210         memcpy( p_dst, p_src, sample_cons.length );
1211         p_dst += sample_cons.length;
1212
1213         i_payload = p_dst - p_newblock->p_buffer;
1214     }
1215
1216     block_Release( p_block );
1217     if( p_newblock )
1218         p_newblock->i_buffer = i_payload;
1219     return p_newblock;
1220
1221 error:
1222     block_Release( p_block );
1223     if( p_newblock )
1224         block_Release( p_newblock );
1225     return NULL;
1226 }
1227
1228 /* RTP Reception Hint Track */
1229 static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec )
1230 {
1231     block_t *p_converted = NULL;
1232     if( p_block->i_buffer < 2 )
1233     {
1234         block_Release( p_block );
1235         return NULL;
1236     }
1237
1238     /* number of RTP packets contained in this sample */
1239     const uint16_t i_packets = GetWBE( p_block->p_buffer );
1240     if( i_packets <= 1 || i_codec != VLC_CODEC_H264 )
1241     {
1242         const size_t i_skip = SAMPLEHEADERSIZE + i_packets * ( RTPPACKETSIZE + CONSTRUCTORSIZE );
1243         if( i_packets == 1 && i_skip < p_block->i_buffer )
1244         {
1245             p_block->p_buffer += i_skip;
1246             p_converted = p_block;
1247         }
1248         else
1249         {
1250             block_Release( p_block );
1251         }
1252     }
1253     else
1254     {
1255         p_converted = MP4_RTPHintToFrame( p_demux, p_block, i_packets );
1256     }
1257
1258     return p_converted;
1259 }
1260
1261 /*****************************************************************************
1262  * Demux: read packet and send them to decoders
1263  *****************************************************************************
1264  * TODO check for newly selected track (ie audio upt to now )
1265  *****************************************************************************/
1266 static int DemuxTrack( demux_t *p_demux, mp4_track_t *tk, uint64_t i_readpos,
1267                        vlc_tick_t i_max_preload )
1268 {
1269     demux_sys_t *p_sys = p_demux->p_sys;
1270     uint32_t i_nb_samples = 0;
1271     uint32_t i_samplessize = 0;
1272
1273     if( !tk->b_ok || tk->i_sample >= tk->i_sample_count )
1274         return VLC_DEMUXER_EOS;
1275
1276     if( tk->b_chapters_source )
1277         return VLC_DEMUXER_SUCCESS;
1278
1279     uint32_t i_run_seq = MP4_TrackGetRunSeq( tk );
1280     vlc_tick_t i_current_nzdts = MP4_TrackGetDTS( p_demux, tk );
1281     const vlc_tick_t i_demux_max_nzdts =i_max_preload < INVALID_PRELOAD
1282                                     ? i_current_nzdts + i_max_preload
1283                                     : INT64_MAX;
1284
1285     for( ; i_demux_max_nzdts >= i_current_nzdts; )
1286     {
1287         if( tk->i_sample >= tk->i_sample_count )
1288             return VLC_DEMUXER_EOS;
1289
1290 #if 0
1291         msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, tk->i_track_ID,
1292                  MP4_TrackGetDTS( p_demux, tk ),
1293                  MP4_GetMoviePTS( p_demux->p_sys ), i_readpos );
1294 #endif
1295
1296         i_samplessize = MP4_TrackGetReadSize( tk, &i_nb_samples );
1297         if( i_samplessize > 0 )
1298         {
1299             block_t *p_block;
1300             vlc_tick_t i_delta;
1301
1302             if( vlc_stream_Tell( p_demux->s ) != i_readpos )
1303             {
1304                 if( MP4_Seek( p_demux->s, i_readpos ) != VLC_SUCCESS )
1305                 {
1306                     msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1307                                        ": Failed to seek to %"PRIu64,
1308                               tk->i_track_ID, i_readpos );
1309                     MP4_TrackSelect( p_demux, tk, false );
1310                     goto end;
1311                 }
1312             }
1313
1314             /* now read pes */
1315             if( !(p_block = vlc_stream_Block( p_demux->s, i_samplessize )) )
1316             {
1317                 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1318                                    ": Failed to read %d bytes sample at %"PRIu64,
1319                           tk->i_track_ID, i_samplessize, i_readpos );
1320                 MP4_TrackSelect( p_demux, tk, false );
1321                 goto end;
1322             }
1323
1324             /* !important! Ensure clock is set before sending data */
1325             if( p_sys->i_pcr == VLC_TICK_INVALID )
1326             {
1327                 es_out_SetPCR( p_demux->out, VLC_TICK_0 + i_current_nzdts );
1328                 p_sys->i_pcr = VLC_TICK_0 + i_current_nzdts;
1329             }
1330
1331             /* dts */
1332             p_block->i_dts = VLC_TICK_0 + i_current_nzdts;
1333             /* pts */
1334             if( MP4_TrackGetPTSDelta( p_demux, tk, &i_delta ) )
1335                 p_block->i_pts = p_block->i_dts + i_delta;
1336             else if( tk->fmt.i_cat != VIDEO_ES )
1337                 p_block->i_pts = p_block->i_dts;
1338             else
1339                 p_block->i_pts = VLC_TICK_INVALID;
1340
1341             p_block->i_length = MP4_GetSamplesDuration( p_demux, tk, i_nb_samples );
1342
1343             MP4_Block_Send( p_demux, tk, p_block );
1344         }
1345
1346         /* Next sample */
1347         if ( i_nb_samples ) /* sample size could be 0, need to go fwd. see return */
1348             MP4_TrackNextSample( p_demux, tk, i_nb_samples );
1349
1350         uint32_t i_next_run_seq = MP4_TrackGetRunSeq( tk );
1351         if( i_next_run_seq != i_run_seq )
1352             break;
1353
1354         i_current_nzdts = MP4_TrackGetDTS( p_demux, tk );
1355         i_readpos = MP4_TrackGetPos( tk );
1356     }
1357
1358     return VLC_DEMUXER_SUCCESS;
1359
1360 end:
1361     return VLC_DEMUXER_EGENERIC;
1362 }
1363
1364 static int DemuxMoov( demux_t *p_demux )
1365 {
1366     demux_sys_t *p_sys = p_demux->p_sys;
1367     unsigned int i_track;
1368
1369     /* check for newly selected/unselected track */
1370     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1371     {
1372         mp4_track_t *tk = &p_sys->track[i_track];
1373         bool b = true;
1374
1375         if( !tk->b_ok || tk->b_chapters_source ||
1376             ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
1377         {
1378             continue;
1379         }
1380
1381         if( p_sys->b_seekable )
1382             es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1383
1384         if( tk->b_selected && !b )
1385         {
1386             MP4_TrackSelect( p_demux, tk, false );
1387         }
1388         else if( !tk->b_selected && b)
1389         {
1390             MP4_TrackSeek( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
1391         }
1392     }
1393
1394     const vlc_tick_t i_nztime = MP4_GetMoviePTS( p_sys );
1395
1396     /* We demux/set pcr, even without selected tracks, (empty edits, ...) */
1397     if( p_sys->i_pcr != VLC_TICK_INVALID /* not after a seek */ )
1398     {
1399         bool b_eof = true;
1400         for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1401         {
1402             mp4_track_t *tk = &p_sys->track[i_track];
1403             if( !tk->b_ok || tk->b_chapters_source || tk->i_sample >= tk->i_sample_count )
1404                 continue;
1405             /* Test for EOF on each track (samples count, edit list) */
1406             b_eof &= ( i_nztime > MP4_TrackGetDTS( p_demux, tk ) );
1407         }
1408         if( b_eof )
1409             return VLC_DEMUXER_EOS;
1410     }
1411
1412     const vlc_tick_t i_max_preload = ( p_sys->b_fastseekable ) ? 0 : ( p_sys->b_seekable ) ? DEMUX_TRACK_MAX_PRELOAD : INVALID_PRELOAD;
1413     int i_status;
1414     /* demux up to increment amount of data on every track, or just set pcr if empty data */
1415     for( ;; )
1416     {
1417         mp4_track_t *tk = NULL;
1418         i_status = VLC_DEMUXER_EOS;
1419
1420         /* First pass, find any track within our target increment, ordered by position */
1421         for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1422         {
1423             mp4_track_t *tk_tmp = &p_sys->track[i_track];
1424             if( !tk_tmp->b_ok || tk_tmp->b_chapters_source ||
1425                 tk_tmp->i_sample >= tk_tmp->i_sample_count ||
1426                 (!tk_tmp->b_selected && p_sys->b_seekable) )
1427                 continue;
1428
1429             /* At least still have data to demux on this or next turns */
1430             i_status = VLC_DEMUXER_SUCCESS;
1431
1432             if ( MP4_TrackGetDTS( p_demux, tk_tmp ) <= i_nztime + DEMUX_INCREMENT )
1433             {
1434                 if( tk == NULL || MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) )
1435                     tk = tk_tmp;
1436             }
1437         }
1438
1439         if( tk )
1440         {
1441             /* Second pass, refine and find any best candidate having a chunk pos closer than
1442              * current candidate (avoids seeks when increment falls between the 2) from
1443              * current position, but within extended interleave time */
1444             for( i_track = 0; i_max_preload != 0 && i_track < p_sys->i_tracks; i_track++ )
1445             {
1446                 mp4_track_t *tk_tmp = &p_sys->track[i_track];
1447                 if( tk_tmp == tk ||
1448                     !tk_tmp->b_ok || tk_tmp->b_chapters_source ||
1449                    (!tk_tmp->b_selected && p_sys->b_seekable) ||
1450                     tk_tmp->i_sample >= tk_tmp->i_sample_count )
1451                     continue;
1452
1453                 vlc_tick_t i_nzdts = MP4_TrackGetDTS( p_demux, tk_tmp );
1454                 if ( i_nzdts <= i_nztime + DEMUX_TRACK_MAX_PRELOAD )
1455                 {
1456                     /* Found a better candidate to avoid seeking */
1457                     if( MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) )
1458                         tk = tk_tmp;
1459                     /* Note: previous candidate will be repicked on next loop */
1460                 }
1461             }
1462
1463             uint64_t i_pos = MP4_TrackGetPos( tk );
1464             int i_ret = DemuxTrack( p_demux, tk, i_pos, i_max_preload );
1465
1466             if( i_ret == VLC_DEMUXER_SUCCESS )
1467                 i_status = VLC_DEMUXER_SUCCESS;
1468         }
1469
1470         if( i_status != VLC_DEMUXER_SUCCESS || !tk )
1471             break;
1472     }
1473
1474     p_sys->i_nztime += DEMUX_INCREMENT;
1475     if( p_sys->i_pcr != VLC_TICK_INVALID )
1476     {
1477         p_sys->i_pcr = VLC_TICK_0 + p_sys->i_nztime;
1478         es_out_SetPCR( p_demux->out, p_sys->i_pcr );
1479     }
1480
1481     /* */
1482     MP4_UpdateSeekpoint( p_demux, i_nztime + DEMUX_INCREMENT );
1483
1484     return i_status;
1485 }
1486
1487 static int Demux( demux_t *p_demux )
1488 {
1489     demux_sys_t *p_sys = p_demux->p_sys;
1490
1491     assert( ! p_sys->b_fragmented );
1492
1493     int i_status = DemuxMoov( p_demux );
1494
1495     if( i_status == VLC_DEMUXER_EOS )
1496         i_status = VLC_DEMUXER_EOF;
1497
1498     return i_status;
1499 }
1500
1501 static void MP4_UpdateSeekpoint( demux_t *p_demux, vlc_tick_t i_time )
1502 {
1503     demux_sys_t *p_sys = p_demux->p_sys;
1504     int i;
1505     if( !p_sys->p_title )
1506         return;
1507     for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
1508     {
1509         if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
1510             break;
1511     }
1512     i--;
1513
1514     if( i != p_sys->i_seekpoint && i >= 0 )
1515     {
1516         p_sys->i_seekpoint = i;
1517         p_sys->seekpoint_changed = true;
1518     }
1519 }
1520 /*****************************************************************************
1521  * Seek: Go to i_date
1522 ******************************************************************************/
1523 static int Seek( demux_t *p_demux, vlc_tick_t i_date, bool b_accurate )
1524 {
1525     demux_sys_t *p_sys = p_demux->p_sys;
1526     unsigned int i_track;
1527
1528     /* Now for each stream try to go to this time */
1529     vlc_tick_t i_start = i_date;
1530     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1531     {
1532         mp4_track_t *tk = &p_sys->track[i_track];
1533         /* FIXME: we should find the lowest time from tracks with indexes.
1534            considering only video for now */
1535         if( tk->fmt.i_cat != VIDEO_ES )
1536             continue;
1537         if( MP4_TrackSeek( p_demux, tk, i_date ) == VLC_SUCCESS )
1538         {
1539             vlc_tick_t i_seeked = MP4_TrackGetDTS( p_demux, tk );
1540             if( i_seeked < i_start )
1541                 i_start = i_seeked;
1542         }
1543     }
1544
1545     msg_Dbg( p_demux, "seeking with %"PRId64 "ms %s", MS_FROM_VLC_TICK(i_date - i_start),
1546             !b_accurate ? "alignment" : "preroll (use input-fast-seek to avoid)" );
1547
1548     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1549     {
1550         mp4_track_t *tk = &p_sys->track[i_track];
1551         tk->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1552         if( tk->fmt.i_cat == VIDEO_ES )
1553             continue;
1554         MP4_TrackSeek( p_demux, tk, i_start );
1555     }
1556
1557     MP4_UpdateSeekpoint( p_demux, i_date );
1558     MP4ASF_ResetFrames( p_sys );
1559     /* update global time */
1560     p_sys->i_nztime = i_start;
1561     p_sys->i_pcr  = VLC_TICK_INVALID;
1562
1563     if( b_accurate )
1564         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1565
1566     return VLC_SUCCESS;
1567 }
1568
1569 static int FragPrepareChunk( demux_t *p_demux, MP4_Box_t *p_moof,
1570                              MP4_Box_t *p_sidx, stime_t i_moof_time, bool b_discontinuity )
1571 {
1572     demux_sys_t *p_sys = p_demux->p_sys;
1573
1574     if( b_discontinuity )
1575     {
1576         for( unsigned i=0; i<p_sys->i_tracks; i++ )
1577             p_sys->track[i].context.b_resync_time_offset = true;
1578     }
1579
1580     if( FragCreateTrunIndex( p_demux, p_moof, p_sidx, i_moof_time ) == VLC_SUCCESS )
1581     {
1582         for( unsigned i=0; i<p_sys->i_tracks; i++ )
1583         {
1584             mp4_track_t *p_track = &p_sys->track[i];
1585             if( p_track->context.runs.i_count )
1586             {
1587                 const mp4_run_t *p_run = &p_track->context.runs.p_array[0];
1588                 p_track->context.i_trun_sample_pos = p_run->i_offset;
1589                 p_track->context.i_trun_sample = 0;
1590                 p_track->i_time = p_run->i_first_dts;
1591             }
1592         }
1593         return VLC_SUCCESS;
1594     }
1595
1596     return VLC_EGENERIC;
1597 }
1598
1599 static vlc_tick_t FragGetDemuxTimeFromTracksTime( demux_sys_t *p_sys )
1600 {
1601     vlc_tick_t i_time = INT64_MAX;
1602     for( unsigned int i = 0; i < p_sys->i_tracks; i++ )
1603     {
1604         if( p_sys->track[i].context.runs.i_count == 0 )
1605             continue;
1606         vlc_tick_t i_ttime = MP4_rescale_mtime( p_sys->track[i].i_time,
1607                                              p_sys->track[i].i_timescale );
1608         i_time = __MIN( i_time, i_ttime );
1609     }
1610     return i_time;
1611 }
1612
1613 static uint32_t FragGetMoofSequenceNumber( MP4_Box_t *p_moof )
1614 {
1615     const MP4_Box_t *p_mfhd = MP4_BoxGet( p_moof, "mfhd" );
1616     if( p_mfhd && BOXDATA(p_mfhd) )
1617         return BOXDATA(p_mfhd)->i_sequence_number;
1618     return 0;
1619 }
1620
1621 static int FragSeekLoadFragment( demux_t *p_demux, uint32_t i_moox, stime_t i_moox_time )
1622 {
1623     demux_sys_t *p_sys = p_demux->p_sys;
1624     MP4_Box_t *p_moox;
1625
1626     if( i_moox == ATOM_moov )
1627     {
1628         p_moox = p_sys->p_moov;
1629     }
1630     else
1631     {
1632         const uint8_t *p_peek;
1633         if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) != 8 )
1634             return VLC_EGENERIC;
1635
1636         if( ATOM_moof != VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
1637             return VLC_EGENERIC;
1638
1639         MP4_Box_t *p_vroot = MP4_BoxGetNextChunk( p_demux->s );
1640         if(!p_vroot)
1641             return VLC_EGENERIC;
1642         p_moox = MP4_BoxExtract( &p_vroot->p_first, ATOM_moof );
1643         MP4_BoxFree( p_vroot );
1644
1645         if(!p_moox)
1646             return VLC_EGENERIC;
1647     }
1648
1649     FragResetContext( p_sys );
1650
1651     /* map context */
1652     p_sys->context.p_fragment_atom = p_moox;
1653     p_sys->context.i_current_box_type = i_moox;
1654
1655     if( i_moox == ATOM_moof )
1656     {
1657         FragPrepareChunk( p_demux, p_moox, NULL, i_moox_time, true );
1658         p_sys->context.i_lastseqnumber = FragGetMoofSequenceNumber( p_moox );
1659
1660         p_sys->i_nztime = FragGetDemuxTimeFromTracksTime( p_sys );
1661         p_sys->i_pcr = VLC_TICK_INVALID;
1662     }
1663
1664     msg_Dbg( p_demux, "seeked to %4.4s at pos %" PRIu64, (char *) &i_moox, p_moox->i_pos );
1665     return VLC_SUCCESS;
1666 }
1667
1668 static unsigned GetSeekTrackIndex( demux_sys_t *p_sys )
1669 {
1670     unsigned cand = 0;
1671     for( unsigned i=0; i<p_sys->i_tracks; i++ )
1672     {
1673         if( p_sys->track[i].fmt.i_cat == VIDEO_ES ||
1674             p_sys->track[i].fmt.i_cat == AUDIO_ES )
1675         {
1676             if( cand != i && !p_sys->track[cand].b_selected )
1677                 cand = i;
1678         }
1679     }
1680     return cand;
1681 }
1682
1683 static void FragTrunSeekToTime( mp4_track_t *p_track, stime_t i_target_time )
1684 {
1685     if( !p_track->b_ok || p_track->context.runs.i_count < 1 )
1686         return;
1687
1688     unsigned i_run = 0;
1689     unsigned i_sample = 0;
1690     uint64_t i_pos = p_track->context.runs.p_array[0].i_offset;
1691     stime_t  i_time = p_track->context.runs.p_array[0].i_first_dts;
1692
1693     for( unsigned r = 0; r < p_track->context.runs.i_count; r++ )
1694     {
1695         const mp4_run_t *p_run = &p_track->context.runs.p_array[r];
1696         const MP4_Box_data_trun_t *p_data =
1697                     p_track->context.runs.p_array[r].p_trun->data.p_trun;
1698         if( i_time > i_target_time )
1699             break;
1700
1701         i_run = r;
1702         i_time = p_run->i_first_dts;
1703         i_pos = p_run->i_offset;
1704         i_sample = 0;
1705
1706         uint32_t dur = p_track->context.i_default_sample_duration;
1707         uint32_t len = p_track->context.i_default_sample_size;
1708         for ( unsigned i=0; i<p_data->i_sample_count; i++ )
1709         {
1710             if( p_data->i_flags & MP4_TRUN_SAMPLE_DURATION )
1711                 dur = p_data->p_samples[i].i_duration;
1712
1713             /* check condition */
1714             if( i_time + dur > i_target_time )
1715                 break;
1716
1717             if( p_data->i_flags & MP4_TRUN_SAMPLE_SIZE )
1718                 len = p_data->p_samples[i].i_size;
1719
1720             i_time += dur;
1721             i_pos += len;
1722         }
1723     }
1724
1725     p_track->context.i_trun_sample = i_sample;
1726     p_track->context.i_trun_sample_pos = i_pos;
1727     p_track->context.runs.i_current = i_run;
1728 }
1729
1730 #define INVALID_SEGMENT_TIME  INT64_MAX
1731
1732 static int FragSeekToTime( demux_t *p_demux, vlc_tick_t i_nztime, bool b_accurate )
1733 {
1734     demux_sys_t *p_sys = p_demux->p_sys;
1735     uint64_t i64 = UINT64_MAX;
1736     uint32_t i_segment_type = ATOM_moof;
1737     stime_t  i_segment_time = INVALID_SEGMENT_TIME;
1738     vlc_tick_t i_sync_time = i_nztime;
1739     bool b_iframesync = false;
1740
1741     const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1742     if ( !p_sys->i_timescale || !i_duration || !p_sys->b_seekable )
1743          return VLC_EGENERIC;
1744
1745     uint64_t i_backup_pos = vlc_stream_Tell( p_demux->s );
1746
1747     if ( !p_sys->b_fragments_probed && !p_sys->b_index_probed && p_sys->b_seekable )
1748     {
1749         ProbeIndex( p_demux );
1750         p_sys->b_index_probed = true;
1751     }
1752
1753     const unsigned i_seek_track_index = GetSeekTrackIndex( p_sys );
1754     const unsigned i_seek_track_ID = p_sys->track[i_seek_track_index].i_track_ID;
1755
1756     if( MP4_rescale_qtime( i_nztime, p_sys->i_timescale )
1757                      < GetMoovTrackDuration( p_sys, i_seek_track_ID ) )
1758     {
1759         i64 = p_sys->p_moov->i_pos;
1760         i_segment_type = ATOM_moov;
1761     }
1762     else if( FragGetMoofBySidxIndex( p_demux, i_nztime, &i64, &i_sync_time ) == VLC_SUCCESS )
1763     {
1764         /* provides base offset */
1765         i_segment_time = i_sync_time;
1766         msg_Dbg( p_demux, "seeking to sidx moof pos %" PRId64 " %" PRId64, i64, i_sync_time );
1767     }
1768     else
1769     {
1770         bool b_buildindex = false;
1771
1772         if( FragGetMoofByTfraIndex( p_demux, i_nztime, i_seek_track_ID, &i64, &i_sync_time ) == VLC_SUCCESS )
1773         {
1774             /* Does only provide segment position and a sync sample time */
1775             msg_Dbg( p_demux, "seeking to sync point %" PRId64, i_sync_time );
1776             b_iframesync = true;
1777         }
1778         else if( !p_sys->b_fragments_probed && !p_sys->b_fastseekable )
1779         {
1780             const char *psz_msg = _(
1781                 "Because this file index is broken or missing, "
1782                 "seeking will not work correctly.\n"
1783                 "VLC won't repair your file but can temporary fix this "
1784                 "problem by building an index in memory.\n"
1785                 "This step might take a long time on a large file.\n"
1786                 "What do you want to do?");
1787             b_buildindex = vlc_dialog_wait_question( p_demux,
1788                                                      VLC_DIALOG_QUESTION_NORMAL,
1789                                                      _("Do not seek"),
1790                                                      _("Build index"),
1791                                                      NULL,
1792                                                      _("Broken or missing Index"),
1793                                                      "%s", psz_msg );
1794         }
1795
1796         if( !p_sys->b_fragments_probed && ( p_sys->b_fastseekable || b_buildindex ) )
1797         {
1798             bool foo;
1799             int i_ret = vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos + p_sys->p_moov->i_size );
1800             if( i_ret == VLC_SUCCESS )
1801             {
1802                 i_ret = ProbeFragments( p_demux, true, &foo );
1803                 p_sys->b_fragments_probed = true;
1804             }
1805             if( i_ret != VLC_SUCCESS )
1806             {
1807                 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1808                 return i_ret;
1809             }
1810         }
1811
1812         if( p_sys->b_fragments_probed && p_sys->p_fragsindex )
1813         {
1814             stime_t i_basetime = MP4_rescale_qtime( i_sync_time, p_sys->i_timescale );
1815             if( !MP4_Fragments_Index_Lookup( p_sys->p_fragsindex, &i_basetime, &i64, i_seek_track_index ) )
1816             {
1817                 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1818                 return VLC_EGENERIC;
1819             }
1820             msg_Dbg( p_demux, "seeking to fragment index pos %" PRId64 " %" PRId64, i64,
1821                      MP4_rescale_mtime( i_basetime, p_sys->i_timescale ) );
1822         }
1823     }
1824
1825     if( i64 == UINT64_MAX )
1826     {
1827         msg_Warn( p_demux, "seek by index failed" );
1828         p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1829         return VLC_EGENERIC;
1830     }
1831
1832     msg_Dbg( p_demux, "final seek to fragment at %"PRId64, i64 );
1833     if( vlc_stream_Seek( p_demux->s, i64 ) )
1834     {
1835         msg_Err( p_demux, "seek failed to %"PRId64, i64 );
1836         p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1837         return VLC_EGENERIC;
1838     }
1839
1840     /* Context is killed on success */
1841     if( FragSeekLoadFragment( p_demux, i_segment_type, i_segment_time ) != VLC_SUCCESS )
1842     {
1843         p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1844         return VLC_EGENERIC;
1845     }
1846
1847     p_sys->i_pcr  = VLC_TICK_INVALID;
1848
1849     for( unsigned i=0; i<p_sys->i_tracks; i++ )
1850     {
1851         if( i_segment_type == ATOM_moov )
1852         {
1853             MP4_TrackSeek( p_demux, &p_sys->track[i], i_sync_time );
1854             p_sys->i_nztime = i_sync_time;
1855             p_sys->i_pcr  = VLC_TICK_INVALID;
1856         }
1857         else if( b_iframesync )
1858         {
1859             stime_t i_tst = MP4_rescale_qtime( i_sync_time, p_sys->track[i].i_timescale );
1860             FragTrunSeekToTime( &p_sys->track[i], i_tst );
1861             p_sys->track[i].i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1862         }
1863     }
1864
1865     MP4ASF_ResetFrames( p_sys );
1866     /* And set next display time in that trun/fragment */
1867     if( b_iframesync && b_accurate )
1868         es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TICK_0 + i_nztime );
1869     return VLC_SUCCESS;
1870 }
1871
1872 static int FragSeekToPos( demux_t *p_demux, double f, bool b_accurate )
1873 {
1874     demux_sys_t *p_sys = p_demux->p_sys;
1875     const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1876
1877     if ( !p_sys->b_seekable || !p_sys->i_timescale || !i_duration )
1878         return VLC_EGENERIC;
1879
1880     return FragSeekToTime( p_demux, (vlc_tick_t)( f *
1881                            MP4_rescale_mtime( i_duration, p_sys->i_timescale ) ), b_accurate );
1882 }
1883
1884 static bool imageTypeCompatible( const MP4_Box_data_data_t *p_data )
1885 {
1886     return p_data && (
1887     p_data->e_wellknowntype == DATA_WKT_PNG ||
1888     p_data->e_wellknowntype == DATA_WKT_JPEG ||
1889     p_data->e_wellknowntype == DATA_WKT_BMP );
1890 }
1891
1892 static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta )
1893 {
1894     MP4_Box_t *p_data = NULL;
1895     MP4_Box_t *p_udta = NULL;
1896     bool b_attachment_set = false;
1897
1898     if( !p_meta )
1899         return VLC_EGENERIC;
1900
1901     for( int i_index = 0; psz_meta_roots[i_index] && !p_udta; i_index++ )
1902     {
1903         p_udta = MP4_BoxGet( p_sys->p_root, psz_meta_roots[i_index] );
1904         if ( p_udta )
1905         {
1906             p_data = MP4_BoxGet( p_udta, "covr/data" );
1907             if ( p_data && imageTypeCompatible( BOXDATA(p_data) ) )
1908             {
1909                 char *psz_attachment;
1910                 if ( -1 != asprintf( &psz_attachment, "attachment://%s/covr/data[0]",
1911                                      psz_meta_roots[i_index] ) )
1912                 {
1913                     vlc_meta_SetArtURL( p_meta, psz_attachment );
1914                     b_attachment_set = true;
1915                     free( psz_attachment );
1916                 }
1917             }
1918         }
1919     }
1920
1921     const MP4_Box_t *p_pnot;
1922     if ( !b_attachment_set && (p_pnot = MP4_BoxGet( p_sys->p_root, "pnot" )) )
1923     {
1924         for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms) && !b_attachment_set; i++ )
1925         {
1926             if ( rgi_pict_atoms[i] == BOXDATA(p_pnot)->i_type )
1927             {
1928                 char rgsz_path[26];
1929                 snprintf( rgsz_path, 26, "attachment://%4.4s[%"PRIu16"]",
1930                           (char*)&rgi_pict_atoms[i], BOXDATA(p_pnot)->i_index - 1 );
1931                 vlc_meta_SetArtURL( p_meta, rgsz_path );
1932                 b_attachment_set = true;
1933             }
1934         }
1935     }
1936
1937     if( p_udta == NULL )
1938     {
1939         if( !b_attachment_set )
1940             return VLC_EGENERIC;
1941     }
1942     else SetupMeta( p_meta, p_udta );
1943
1944     return VLC_SUCCESS;
1945 }
1946
1947 /*****************************************************************************
1948  * Control:
1949  *****************************************************************************/
1950 static int Control( demux_t *p_demux, int i_query, va_list args )
1951 {
1952     demux_sys_t *p_sys = p_demux->p_sys;
1953
1954     double f, *pf;
1955     vlc_tick_t i64;
1956     bool b;
1957
1958     const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1959
1960     switch( i_query )
1961     {
1962         case DEMUX_CAN_SEEK:
1963             *va_arg( args, bool * ) = p_sys->b_seekable;
1964             return VLC_SUCCESS;
1965
1966         case DEMUX_GET_POSITION:
1967             pf = va_arg( args, double * );
1968             if( i_duration > 0 )
1969             {
1970                 *pf = (double)p_sys->i_nztime /
1971                       MP4_rescale_mtime( i_duration, p_sys->i_timescale );
1972             }
1973             else
1974             {
1975                 *pf = 0.0;
1976             }
1977             return VLC_SUCCESS;
1978
1979         case DEMUX_GET_SEEKPOINT:
1980             *va_arg( args, int * ) = p_sys->i_seekpoint;
1981             return VLC_SUCCESS;
1982
1983         case DEMUX_SET_POSITION:
1984             f = va_arg( args, double );
1985             b = va_arg( args, int );
1986             if ( p_demux->pf_demux == DemuxFrag )
1987                 return FragSeekToPos( p_demux, f, b );
1988             else if( p_sys->i_timescale > 0 )
1989             {
1990                 i64 = (vlc_tick_t)( f * MP4_rescale_mtime( p_sys->i_duration,
1991                                                         p_sys->i_timescale ) );
1992                 return Seek( p_demux, i64, b );
1993             }
1994             else return VLC_EGENERIC;
1995
1996         case DEMUX_GET_TIME:
1997             *va_arg( args, vlc_tick_t * ) = p_sys->i_timescale > 0 ? p_sys->i_nztime : 0;
1998             return VLC_SUCCESS;
1999
2000         case DEMUX_SET_TIME:
2001             i64 = va_arg( args, vlc_tick_t );
2002             b = va_arg( args, int );
2003             if ( p_demux->pf_demux == DemuxFrag )
2004                 return FragSeekToTime( p_demux, i64, b );
2005             else
2006                 return Seek( p_demux, i64, b );
2007
2008         case DEMUX_GET_LENGTH:
2009             if( p_sys->i_timescale > 0 )
2010             {
2011                 *va_arg( args, vlc_tick_t * ) = MP4_rescale_mtime( i_duration,
2012                                                            p_sys->i_timescale );
2013             }
2014             else *va_arg( args, vlc_tick_t * ) = 0;
2015             return VLC_SUCCESS;
2016
2017         case DEMUX_GET_FPS:
2018             pf = va_arg( args, double * );
2019             *pf = p_sys->f_fps;
2020             return VLC_SUCCESS;
2021
2022         case DEMUX_GET_ATTACHMENTS:
2023         {
2024             input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
2025             int *pi_int = va_arg( args, int * );
2026
2027             MP4_Box_t *p_udta = NULL;
2028             size_t i_count = 0;
2029             int i_index = 0;
2030
2031             /* Count number of total attachments */
2032             for( ; psz_meta_roots[i_index] && !p_udta; i_index++ )
2033             {
2034                 p_udta = MP4_BoxGet( p_sys->p_root, psz_meta_roots[i_index] );
2035                 if ( p_udta )
2036                     i_count += MP4_BoxCount( p_udta, "covr/data" );
2037             }
2038
2039             for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms); i++ )
2040             {
2041                 char rgsz_path[5];
2042                 snprintf( rgsz_path, 5, "%4.4s", (char*)&rgi_pict_atoms[i] );
2043                 i_count += MP4_BoxCount( p_sys->p_root, rgsz_path );
2044             }
2045
2046             if ( i_count == 0 )
2047                 return VLC_EGENERIC;
2048
2049             *ppp_attach = (input_attachment_t**)
2050                     vlc_alloc( i_count, sizeof(input_attachment_t*) );
2051             if( !(*ppp_attach) ) return VLC_ENOMEM;
2052
2053             /* First add cover attachments */
2054             i_count = 0;
2055             size_t i_box_count = 0;
2056             if ( p_udta )
2057             {
2058                 const MP4_Box_t *p_data = MP4_BoxGet( p_udta, "covr/data" );
2059                 for( ; p_data; p_data = p_data->p_next )
2060                 {
2061                     char *psz_mime;
2062                     char *psz_filename;
2063                     i_box_count++;
2064
2065                     if ( p_data->i_type != ATOM_data || !imageTypeCompatible( BOXDATA(p_data) ) )
2066                         continue;
2067
2068                     switch( BOXDATA(p_data)->e_wellknowntype )
2069                     {
2070                     case DATA_WKT_PNG:
2071                         psz_mime = strdup( "image/png" );
2072                         break;
2073                     case DATA_WKT_JPEG:
2074                         psz_mime = strdup( "image/jpeg" );
2075                         break;
2076                     case DATA_WKT_BMP:
2077                         psz_mime = strdup( "image/bmp" );
2078                         break;
2079                     default:
2080                         continue;
2081                     }
2082
2083                     if ( asprintf( &psz_filename, "%s/covr/data[%"PRIu64"]", psz_meta_roots[i_index - 1],
2084                                    (uint64_t) i_box_count - 1 ) >= 0 )
2085                     {
2086                         (*ppp_attach)[i_count++] =
2087                             vlc_input_attachment_New( psz_filename, psz_mime, "Cover picture",
2088                                 BOXDATA(p_data)->p_blob, BOXDATA(p_data)->i_blob );
2089                         msg_Dbg( p_demux, "adding attachment %s", psz_filename );
2090                         free( psz_filename );
2091                     }
2092
2093                     free( psz_mime );
2094                 }
2095             }
2096
2097             /* Then quickdraw pict ones */
2098             for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms); i++ )
2099             {
2100                 char rgsz_path[5];
2101                 snprintf( rgsz_path, 5, "%4.4s", (char*)&rgi_pict_atoms[i] );
2102                 const MP4_Box_t *p_pict = MP4_BoxGet( p_sys->p_root, rgsz_path );
2103                 i_box_count = 0;
2104                 for( ; p_pict; p_pict = p_pict->p_next )
2105                 {
2106                     if ( i_box_count++ == UINT16_MAX ) /* pnot only handles 2^16 */
2107                         break;
2108                     if ( p_pict->i_type != rgi_pict_atoms[i] )
2109                         continue;
2110                     char rgsz_location[12];
2111                     snprintf( rgsz_location, 12, "%4.4s[%"PRIu16"]", (char*)&rgi_pict_atoms[i],
2112                               (uint16_t) i_box_count - 1 );
2113                     (*ppp_attach)[i_count] = vlc_input_attachment_New( rgsz_location, "image/x-pict",
2114                         "Quickdraw image", p_pict->data.p_binary->p_blob, p_pict->data.p_binary->i_blob );
2115                     if ( !(*ppp_attach)[i_count] )
2116                     {
2117                         i_count = 0;
2118                         break;
2119                     }
2120                     i_count++;
2121                     msg_Dbg( p_demux, "adding attachment %s", rgsz_location );
2122                 }
2123             }
2124
2125             if ( i_count == 0 )
2126             {
2127                 free( *ppp_attach );
2128                 return VLC_EGENERIC;
2129             }
2130
2131             *pi_int = i_count;
2132
2133             return VLC_SUCCESS;
2134         }
2135
2136         case DEMUX_GET_META:
2137         {
2138             vlc_meta_t *p_meta = va_arg( args, vlc_meta_t *);
2139
2140             if( !p_sys->p_meta )
2141                 return VLC_EGENERIC;
2142
2143             vlc_meta_Merge( p_meta, p_sys->p_meta );
2144
2145             return VLC_SUCCESS;
2146         }
2147
2148         case DEMUX_GET_TITLE_INFO:
2149         {
2150             input_title_t ***ppp_title = va_arg( args, input_title_t *** );
2151             int *pi_int = va_arg( args, int* );
2152             int *pi_title_offset = va_arg( args, int* );
2153             int *pi_seekpoint_offset = va_arg( args, int* );
2154
2155             if( !p_sys->p_title )
2156                 return VLC_EGENERIC;
2157
2158             *pi_int = 1;
2159             *ppp_title = malloc( sizeof( input_title_t*) );
2160             (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
2161             *pi_title_offset = 0;
2162             *pi_seekpoint_offset = 0;
2163             return VLC_SUCCESS;
2164         }
2165         case DEMUX_SET_TITLE:
2166         {
2167             const int i_title = va_arg( args, int );
2168             if( !p_sys->p_title || i_title != 0 )
2169                 return VLC_EGENERIC;
2170             return VLC_SUCCESS;
2171         }
2172         case DEMUX_SET_SEEKPOINT:
2173         {
2174             const int i_seekpoint = va_arg( args, int );
2175             if( !p_sys->p_title )
2176                 return VLC_EGENERIC;
2177             return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset, true );
2178         }
2179         case DEMUX_TEST_AND_CLEAR_FLAGS:
2180         {
2181             unsigned *restrict flags = va_arg( args, unsigned * );
2182
2183             if ((*flags & INPUT_UPDATE_SEEKPOINT) && p_sys->seekpoint_changed)
2184             {
2185                 *flags = INPUT_UPDATE_SEEKPOINT;
2186                 p_sys->seekpoint_changed = false;
2187             }
2188             else
2189                 *flags = 0;
2190             return VLC_SUCCESS;
2191         }
2192
2193         case DEMUX_GET_PTS_DELAY:
2194         {
2195             for( unsigned int i = 0; i < p_sys->i_tracks; i++ )
2196             {
2197                 const MP4_Box_t *p_load;
2198                 if ( (p_load = MP4_BoxGet( p_sys->track[i].p_track, "load" )) &&
2199                      BOXDATA(p_load)->i_duration > 0 )
2200                 {
2201                     *va_arg(args, vlc_tick_t *) =
2202                             MP4_rescale_mtime( BOXDATA(p_load)->i_duration,
2203                                                p_sys->track[i].i_timescale );
2204                     return VLC_SUCCESS;
2205                 }
2206             }
2207             return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
2208         }
2209         case DEMUX_SET_NEXT_DEMUX_TIME:
2210         case DEMUX_SET_GROUP_DEFAULT:
2211         case DEMUX_SET_GROUP_ALL:
2212         case DEMUX_SET_GROUP_LIST:
2213         case DEMUX_HAS_UNSUPPORTED_META:
2214         case DEMUX_CAN_RECORD:
2215             return VLC_EGENERIC;
2216
2217         case DEMUX_CAN_PAUSE:
2218         case DEMUX_SET_PAUSE_STATE:
2219         case DEMUX_CAN_CONTROL_PACE:
2220             return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
2221
2222         default:
2223             return VLC_EGENERIC;
2224     }
2225 }
2226
2227 /*****************************************************************************
2228  * Close: frees unused data
2229  *****************************************************************************/
2230 static void Close ( vlc_object_t * p_this )
2231 {
2232     demux_t *  p_demux = (demux_t *)p_this;
2233     demux_sys_t *p_sys = p_demux->p_sys;
2234     unsigned int i_track;
2235
2236     msg_Dbg( p_demux, "freeing all memory" );
2237
2238     FragResetContext( p_sys );
2239
2240     MP4_BoxFree( p_sys->p_root );
2241
2242     if( p_sys->p_title )
2243         vlc_input_title_Delete( p_sys->p_title );
2244
2245     if( p_sys->p_meta )
2246         vlc_meta_Delete( p_sys->p_meta );
2247
2248     MP4_Fragments_Index_Delete( p_sys->p_fragsindex );
2249
2250     for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
2251         MP4_TrackClean( p_demux->out, &p_sys->track[i_track] );
2252     free( p_sys->track );
2253
2254     free( p_sys );
2255 }
2256
2257
2258
2259 /****************************************************************************
2260  * Local functions, specific to vlc
2261  ****************************************************************************/
2262 /* Chapters */
2263 static void LoadChapterGpac( demux_t  *p_demux, MP4_Box_t *p_chpl )
2264 {
2265     demux_sys_t *p_sys = p_demux->p_sys;
2266
2267     if( BOXDATA(p_chpl)->i_chapter == 0 )
2268         return;
2269
2270     p_sys->p_title = vlc_input_title_New();
2271     for( int i = 0; i < BOXDATA(p_chpl)->i_chapter && p_sys->p_title; i++ )
2272     {
2273         seekpoint_t *s = vlc_seekpoint_New();
2274         if( s == NULL) continue;
2275
2276         s->psz_name = strdup( BOXDATA(p_chpl)->chapter[i].psz_name );
2277         if( s->psz_name == NULL)
2278         {
2279             vlc_seekpoint_Delete( s );;
2280             continue;
2281         }
2282
2283         EnsureUTF8( s->psz_name );
2284         msftime_t offset = BOXDATA(p_chpl)->chapter[i].i_start;
2285         s->i_time_offset = VLC_TICK_FROM_MSFTIME(offset);
2286         TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2287     }
2288 }
2289 static void LoadChapterGoPro( demux_t *p_demux, MP4_Box_t *p_hmmt )
2290 {
2291     demux_sys_t *p_sys = p_demux->p_sys;
2292
2293     p_sys->p_title = vlc_input_title_New();
2294     if( p_sys->p_title )
2295         for( unsigned i = 0; i < BOXDATA(p_hmmt)->i_chapter_count; i++ )
2296         {
2297             seekpoint_t *s = vlc_seekpoint_New();
2298             if( s )
2299             {
2300                 if( asprintf( &s->psz_name, "HiLight tag #%u", i+1 ) != -1 )
2301                     EnsureUTF8( s->psz_name );
2302
2303                 /* HiLights are stored in ms so we convert them to ┬Ás */
2304                 s->i_time_offset = VLC_TICK_FROM_MS( BOXDATA(p_hmmt)->pi_chapter_start[i] );
2305                 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2306             }
2307         }
2308 }
2309 static void LoadChapterApple( demux_t  *p_demux, mp4_track_t *tk )
2310 {
2311     demux_sys_t *p_sys = p_demux->p_sys;
2312
2313     for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
2314     {
2315         const vlc_tick_t i_dts = MP4_TrackGetDTS( p_demux, tk );
2316         vlc_tick_t i_pts_delta;
2317         if ( !MP4_TrackGetPTSDelta( p_demux, tk, &i_pts_delta ) )
2318             i_pts_delta = 0;
2319         uint32_t i_nb_samples = 0;
2320         const uint32_t i_size = MP4_TrackGetReadSize( tk, &i_nb_samples );
2321
2322         if( i_size > 0 && !vlc_stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
2323         {
2324             char p_buffer[256];
2325             const uint32_t i_read = stream_ReadU32( p_demux->s, p_buffer,
2326                                                     __MIN( sizeof(p_buffer), i_size ) );
2327             if( i_read > 2 )
2328             {
2329                 const uint32_t i_string = __MIN( GetWBE(p_buffer), i_read-2 );
2330                 const char *psnz_string = &p_buffer[2];
2331
2332                 seekpoint_t *s = vlc_seekpoint_New();
2333                 if( s == NULL ) continue;
2334
2335                 if( i_string > 1 && !memcmp( psnz_string, "\xFF\xFE", 2 ) )
2336                     s->psz_name = FromCharset( "UTF-16LE", psnz_string, i_string );
2337                 else
2338                     s->psz_name = strndup( psnz_string, i_string );
2339
2340                 if( s->psz_name == NULL )
2341                 {
2342                     vlc_seekpoint_Delete( s );
2343                     continue;
2344                 }
2345
2346                 EnsureUTF8( s->psz_name );
2347                 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
2348
2349                 if( !p_sys->p_title )
2350                     p_sys->p_title = vlc_input_title_New();
2351                 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2352             }
2353         }
2354         if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
2355                               tk->chunk[tk->i_chunk].i_sample_count )
2356             tk->i_chunk++;
2357     }
2358 }
2359 static void LoadChapter( demux_t  *p_demux )
2360 {
2361     demux_sys_t *p_sys = p_demux->p_sys;
2362     MP4_Box_t *p_chpl;
2363     MP4_Box_t *p_hmmt;
2364
2365     if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) &&
2366           BOXDATA(p_chpl) && BOXDATA(p_chpl)->i_chapter > 0 )
2367     {
2368         LoadChapterGpac( p_demux, p_chpl );
2369     }
2370     else if( ( p_hmmt = MP4_BoxGet( p_sys->p_root, "/moov/udta/HMMT" ) ) &&
2371              BOXDATA(p_hmmt) && BOXDATA(p_hmmt)->pi_chapter_start && BOXDATA(p_hmmt)->i_chapter_count > 0 )
2372     {
2373         LoadChapterGoPro( p_demux, p_hmmt );
2374     }
2375     else if( p_sys->p_tref_chap )
2376     {
2377         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2378         unsigned int i, j;
2379
2380         /* Load the first subtitle track like quicktime */
2381         for( i = 0; i < p_chap->i_entry_count; i++ )
2382         {
2383             for( j = 0; j < p_sys->i_tracks; j++ )
2384             {
2385                 mp4_track_t *tk = &p_sys->track[j];
2386                 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
2387                     tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_TX3G )
2388                     break;
2389             }
2390             if( j < p_sys->i_tracks )
2391             {
2392                 LoadChapterApple( p_demux, &p_sys->track[j] );
2393                 break;
2394             }
2395         }
2396     }
2397
2398     /* Add duration if titles are enabled */
2399     if( p_sys->p_title )
2400     {
2401         const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
2402         p_sys->p_title->i_length =
2403                 MP4_rescale_mtime( i_duration, p_sys->i_timescale );
2404     }
2405 }
2406
2407 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
2408 static int TrackCreateChunksIndex( demux_t *p_demux,
2409                                    mp4_track_t *p_demux_track )
2410 {
2411     MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
2412     MP4_Box_t *p_stsc;
2413
2414     unsigned int i_chunk;
2415     unsigned int i_index, i_last;
2416
2417     if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
2418           !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
2419         ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
2420     {
2421         return( VLC_EGENERIC );
2422     }
2423
2424     p_demux_track->i_chunk_count = BOXDATA(p_co64)->i_entry_count;
2425     if( !p_demux_track->i_chunk_count )
2426     {
2427         msg_Warn( p_demux, "no chunk defined" );
2428     }
2429     p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
2430                                    sizeof( mp4_chunk_t ) );
2431     if( p_demux_track->chunk == NULL )
2432     {
2433         return VLC_ENOMEM;
2434     }
2435
2436     /* first we read chunk offset */
2437     for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2438     {
2439         mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2440
2441         ck->i_offset = BOXDATA(p_co64)->i_chunk_offset[i_chunk];
2442
2443         ck->i_first_dts = 0;
2444         ck->i_entries_dts = 0;
2445         ck->p_sample_count_dts = NULL;
2446         ck->p_sample_delta_dts = NULL;
2447         ck->i_entries_pts = 0;
2448         ck->p_sample_count_pts = NULL;
2449         ck->p_sample_offset_pts = NULL;
2450     }
2451
2452     /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
2453         to be used for the sample XXX begin to 1
2454         We construct it begining at the end */
2455     i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
2456     i_index = BOXDATA(p_stsc)->i_entry_count;
2457
2458     while( i_index-- > 0 )
2459     {
2460         for( i_chunk = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
2461              i_chunk < i_last; i_chunk++ )
2462         {
2463             if( i_chunk >= p_demux_track->i_chunk_count )
2464             {
2465                 msg_Warn( p_demux, "corrupted chunk table" );
2466                 return VLC_EGENERIC;
2467             }
2468
2469             p_demux_track->chunk[i_chunk].i_sample_description_index =
2470                     BOXDATA(p_stsc)->i_sample_description_index[i_index];
2471             p_demux_track->chunk[i_chunk].i_sample_count =
2472                     BOXDATA(p_stsc)->i_samples_per_chunk[i_index];
2473         }
2474         i_last = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
2475     }
2476
2477     p_demux_track->i_sample_count = 0;
2478     bool b_broken = false;
2479     if ( p_demux_track->i_chunk_count )
2480     {
2481         p_demux_track->chunk[0].i_sample_first = 0;
2482         p_demux_track->i_sample_count += p_demux_track->chunk[0].i_sample_count;
2483
2484         const mp4_chunk_t *prev = &p_demux_track->chunk[0];
2485         for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2486         {
2487             mp4_chunk_t *cur = &p_demux_track->chunk[i_chunk];
2488             if( unlikely(UINT32_MAX - cur->i_sample_count < p_demux_track->i_sample_count) )
2489             {
2490                 b_broken = true;
2491                 break;
2492             }
2493             p_demux_track->i_sample_count += cur->i_sample_count;
2494             cur->i_sample_first = prev->i_sample_first + prev->i_sample_count;
2495             prev = cur;
2496         }
2497     }
2498
2499     if( unlikely(b_broken) )
2500     {
2501         msg_Err( p_demux, "Overflow in chunks total samples count" );
2502         return VLC_EGENERIC;
2503     }
2504
2505     msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
2506              p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
2507
2508     return VLC_SUCCESS;
2509 }
2510
2511 static int xTTS_CountEntries( demux_t *p_demux, uint32_t *pi_entry /* out */,
2512                               const uint32_t i_index,
2513                               uint32_t i_index_samples_left,
2514                               uint32_t i_sample_count,
2515                               const uint32_t *pi_index_sample_count,
2516                               const uint32_t i_table_count )
2517 {
2518     uint32_t i_array_offset;
2519     while( i_sample_count > 0 )
2520     {
2521         if ( likely((UINT32_MAX - i_index) >= *pi_entry) )
2522             i_array_offset = i_index + *pi_entry;
2523         else
2524             return VLC_EGENERIC;
2525
2526         if ( i_array_offset >= i_table_count )
2527         {
2528             msg_Err( p_demux, "invalid index counting total samples %u %u", i_array_offset,  i_table_count );
2529             return VLC_ENOVAR;
2530         }
2531
2532         if ( i_index_samples_left )
2533         {
2534             if ( i_index_samples_left > i_sample_count )
2535             {
2536                 i_index_samples_left -= i_sample_count;
2537                 i_sample_count = 0;
2538                 *pi_entry +=1; /* No samples left, go copy */
2539                 break;
2540             }
2541             else
2542             {
2543                 i_sample_count -= i_index_samples_left;
2544                 i_index_samples_left = 0;
2545                 *pi_entry += 1;
2546                 continue;
2547             }
2548         }
2549         else
2550         {
2551             i_sample_count -= __MIN( i_sample_count, pi_index_sample_count[i_array_offset] );
2552             *pi_entry += 1;
2553         }
2554     }
2555
2556     return VLC_SUCCESS;
2557 }
2558
2559 static int TrackCreateSamplesIndex( demux_t *p_demux,
2560                                     mp4_track_t *p_demux_track )
2561 {
2562     MP4_Box_t *p_box;
2563     MP4_Box_data_stsz_t *stsz;
2564     /* TODO use also stss and stsh table for seeking */
2565     /* FIXME use edit table */
2566
2567     /* Find stsz
2568      *  Gives the sample size for each samples. There is also a stz2 table
2569      *  (compressed form) that we need to implement TODO */
2570     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
2571     if( !p_box )
2572     {
2573         /* FIXME and stz2 */
2574         msg_Warn( p_demux, "cannot find STSZ box" );
2575         return VLC_EGENERIC;
2576     }
2577     stsz = p_box->data.p_stsz;
2578
2579     /* Use stsz table to create a sample number -> sample size table */
2580     if( p_demux_track->i_sample_count != stsz->i_sample_count )
2581     {
2582         msg_Warn( p_demux, "Incorrect total samples stsc %" PRIu32 " <> stsz %"PRIu32 ", "
2583                            " expect truncated media playback",
2584                            p_demux_track->i_sample_count, stsz->i_sample_count );
2585         p_demux_track->i_sample_count = __MIN(p_demux_track->i_sample_count, stsz->i_sample_count);
2586     }
2587
2588     if( stsz->i_sample_size )
2589     {
2590         /* 1: all sample have the same size, so no need to construct a table */
2591         p_demux_track->i_sample_size = stsz->i_sample_size;
2592         p_demux_track->p_sample_size = NULL;
2593     }
2594     else
2595     {
2596         /* 2: each sample can have a different size */
2597         p_demux_track->i_sample_size = 0;
2598         p_demux_track->p_sample_size =
2599             calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
2600         if( p_demux_track->p_sample_size == NULL )
2601             return VLC_ENOMEM;
2602
2603         for( uint32_t i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
2604         {
2605             p_demux_track->p_sample_size[i_sample] =
2606                     stsz->i_entry_size[i_sample];
2607         }
2608     }
2609
2610     if ( p_demux_track->i_chunk_count && p_demux_track->i_sample_size == 0 )
2611     {
2612         const mp4_chunk_t *lastchunk = &p_demux_track->chunk[p_demux_track->i_chunk_count - 1];
2613         if( (uint64_t)lastchunk->i_sample_count + p_demux_track->i_chunk_count - 1 > stsz->i_sample_count )
2614         {
2615             msg_Err( p_demux, "invalid samples table: stsz table is too small" );
2616             return VLC_EGENERIC;
2617         }
2618     }
2619
2620     /* Use stts table to create a sample number -> dts table.
2621      * XXX: if we don't want to waste too much memory, we can't expand
2622      *  the box! so each chunk will contain an "extract" of this table
2623      *  for fast research (problem with raw stream where a sample is sometime
2624      *  just channels*bits_per_sample/8 */
2625
2626      /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
2627       *        differing in 2/2 fields and 1 signedness */
2628
2629     int64_t i_next_dts = 0;
2630     /* Find stts
2631      *  Gives mapping between sample and decoding time
2632      */
2633     p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
2634     if( !p_box )
2635     {
2636         msg_Warn( p_demux, "cannot find STTS box" );
2637         return VLC_EGENERIC;
2638     }
2639     else
2640     {
2641         MP4_Box_data_stts_t *stts = p_box->data.p_stts;
2642
2643         msg_Warn( p_demux, "STTS table of %"PRIu32" entries", stts->i_entry_count );
2644
2645         /* Create sample -> dts table per chunk */
2646         uint32_t i_index = 0;
2647         uint32_t i_current_index_samples_left = 0;
2648
2649         for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2650         {
2651             mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2652             uint32_t i_sample_count;
2653
2654             /* save first dts */
2655             ck->i_first_dts = i_next_dts;
2656
2657             /* count how many entries are needed for this chunk
2658              * for p_sample_delta_dts and p_sample_count_dts */
2659             ck->i_entries_dts = 0;
2660
2661             int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_dts, i_index,
2662                                            i_current_index_samples_left,
2663                                            ck->i_sample_count,
2664                                            stts->pi_sample_count,
2665                                            stts->i_entry_count );
2666             if ( i_ret == VLC_EGENERIC )
2667                 return i_ret;
2668
2669             /* allocate them */
2670             ck->p_sample_count_dts = calloc( ck->i_entries_dts, sizeof( uint32_t ) );
2671             ck->p_sample_delta_dts = calloc( ck->i_entries_dts, sizeof( uint32_t ) );
2672             if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
2673             {
2674                 free( ck->p_sample_count_dts );
2675                 free( ck->p_sample_delta_dts );
2676                 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_dts );
2677                 ck->i_entries_dts = 0;
2678                 return VLC_ENOMEM;
2679             }
2680
2681             /* now copy */
2682             i_sample_count = ck->i_sample_count;
2683
2684             for( uint32_t i = 0; i < ck->i_entries_dts; i++ )
2685             {
2686                 if ( i_current_index_samples_left )
2687                 {
2688                     if ( i_current_index_samples_left > i_sample_count )
2689                     {
2690                         ck->p_sample_count_dts[i] = i_sample_count;
2691                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2692                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2693                         if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts;
2694                         i_current_index_samples_left -= i_sample_count;
2695                         i_sample_count = 0;
2696                         assert( i == ck->i_entries_dts - 1 );
2697                         break;
2698                     }
2699                     else
2700                     {
2701                         ck->p_sample_count_dts[i] = i_current_index_samples_left;
2702                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2703                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2704                         if ( i_current_index_samples_left ) ck->i_duration = i_next_dts - ck->i_first_dts;
2705                         i_sample_count -= i_current_index_samples_left;
2706                         i_current_index_samples_left = 0;
2707                         i_index++;
2708                     }
2709                 }
2710                 else
2711                 {
2712                     if ( stts->pi_sample_count[i_index] > i_sample_count )
2713                     {
2714                         ck->p_sample_count_dts[i] = i_sample_count;
2715                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2716                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2717                         if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts;
2718                         i_current_index_samples_left = stts->pi_sample_count[i_index] - i_sample_count;
2719                         i_sample_count = 0;
2720                         assert( i == ck->i_entries_dts - 1 );
2721                         // keep building from same index
2722                     }
2723                     else
2724                     {
2725                         ck->p_sample_count_dts[i] = stts->pi_sample_count[i_index];
2726                         ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2727                         i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2728                         if ( stts->pi_sample_count[i_index] ) ck->i_duration = i_next_dts - ck->i_first_dts;
2729                         i_sample_count -= stts->pi_sample_count[i_index];
2730                         i_index++;
2731                     }
2732                 }
2733
2734             }
2735         }
2736     }
2737
2738
2739     /* Find ctts
2740      *  Gives the delta between decoding time (dts) and composition table (pts)
2741      */
2742     p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
2743     if( p_box && p_box->data.p_ctts )
2744     {
2745         MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
2746
2747         msg_Warn( p_demux, "CTTS table of %"PRIu32" entries", ctts->i_entry_count );
2748
2749         int64_t i_cts_shift = 0;
2750         const MP4_Box_t *p_cslg = MP4_BoxGet( p_demux_track->p_stbl, "cslg" );
2751         if( p_cslg && BOXDATA(p_cslg) )
2752             i_cts_shift = BOXDATA(p_cslg)->ct_to_dts_shift;
2753
2754         /* Create pts-dts table per chunk */
2755         uint32_t i_index = 0;
2756         uint32_t i_current_index_samples_left = 0;
2757
2758         for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2759         {
2760             mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2761             uint32_t i_sample_count;
2762
2763             /* count how many entries are needed for this chunk
2764              * for p_sample_offset_pts and p_sample_count_pts */
2765             ck->i_entries_pts = 0;
2766             int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_pts, i_index,
2767                                            i_current_index_samples_left,
2768                                            ck->i_sample_count,
2769                                            ctts->pi_sample_count,
2770                                            ctts->i_entry_count );
2771             if ( i_ret == VLC_EGENERIC )
2772                 return i_ret;
2773
2774             /* allocate them */
2775             ck->p_sample_count_pts = calloc( ck->i_entries_pts, sizeof( uint32_t ) );
2776             ck->p_sample_offset_pts = calloc( ck->i_entries_pts, sizeof( int32_t ) );
2777             if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
2778             {
2779                 free( ck->p_sample_count_pts );
2780                 free( ck->p_sample_offset_pts );
2781                 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_pts );
2782                 ck->i_entries_pts = 0;
2783                 return VLC_ENOMEM;
2784             }
2785
2786             /* now copy */
2787             i_sample_count = ck->i_sample_count;
2788
2789             for( uint32_t i = 0; i < ck->i_entries_pts; i++ )
2790             {
2791                 if ( i_current_index_samples_left )
2792                 {
2793                     if ( i_current_index_samples_left > i_sample_count )
2794                     {
2795                         ck->p_sample_count_pts[i] = i_sample_count;
2796                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2797                         i_current_index_samples_left -= i_sample_count;
2798                         i_sample_count = 0;
2799                         assert( i == ck->i_entries_pts - 1 );
2800                         break;
2801                     }
2802                     else
2803                     {
2804                         ck->p_sample_count_pts[i] = i_current_index_samples_left;
2805                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2806                         i_sample_count -= i_current_index_samples_left;
2807                         i_current_index_samples_left = 0;
2808                         i_index++;
2809                     }
2810                 }
2811                 else
2812                 {
2813                     if ( ctts->pi_sample_count[i_index] > i_sample_count )
2814                     {
2815                         ck->p_sample_count_pts[i] = i_sample_count;
2816                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2817                         i_current_index_samples_left = ctts->pi_sample_count[i_index] - i_sample_count;
2818                         i_sample_count = 0;
2819                         assert( i == ck->i_entries_pts - 1 );
2820                         // keep building from same index
2821                     }
2822                     else
2823                     {
2824                         ck->p_sample_count_pts[i] = ctts->pi_sample_count[i_index];
2825                         ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2826                         i_sample_count -= ctts->pi_sample_count[i_index];
2827                         i_index++;
2828                     }
2829                 }
2830
2831
2832             }
2833         }
2834     }
2835
2836     msg_Dbg( p_demux, "track[Id 0x%x] read %"PRIu32" samples length:%"PRId64"s",
2837              p_demux_track->i_track_ID, p_demux_track->i_sample_count,
2838              i_next_dts / p_demux_track->i_timescale );
2839
2840     return VLC_SUCCESS;
2841 }
2842
2843
2844 /**
2845  * It computes the sample rate for a video track using the given sample
2846  * description index
2847  */
2848 static void TrackGetESSampleRate( demux_t *p_demux,
2849                                   unsigned *pi_num, unsigned *pi_den,
2850                                   const mp4_track_t *p_track,
2851                                   unsigned i_sd_index,
2852                                   unsigned i_chunk )
2853 {
2854     demux_sys_t *p_sys = p_demux->p_sys;
2855     *pi_num = 0;
2856     *pi_den = 0;
2857
2858     MP4_Box_t *p_trak = MP4_GetTrakByTrackID( MP4_BoxGet( p_sys->p_root,
2859                                                           "/moov" ),
2860                                               p_track->i_track_ID );
2861     MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
2862     if ( p_mdhd && BOXDATA(p_mdhd) )
2863     {
2864         vlc_ureduce( pi_num, pi_den,
2865                      (uint64_t) BOXDATA(p_mdhd)->i_timescale * p_track->i_sample_count,
2866                      (uint64_t) BOXDATA(p_mdhd)->i_duration,
2867                      UINT16_MAX );
2868         return;
2869     }
2870
2871     if( p_track->i_chunk_count == 0 )
2872         return;
2873
2874     /* */
2875     const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
2876     while( p_chunk > &p_track->chunk[0] &&
2877            p_chunk[-1].i_sample_description_index == i_sd_index )
2878     {
2879         p_chunk--;
2880     }
2881
2882     uint64_t i_sample = 0;
2883     uint64_t i_total_duration = 0;
2884     do
2885     {
2886         i_sample += p_chunk->i_sample_count;
2887         i_total_duration += p_chunk->i_duration;
2888         p_chunk++;
2889     }
2890     while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
2891            p_chunk->i_sample_description_index == i_sd_index );
2892
2893     if( i_sample > 0 && i_total_duration )
2894         vlc_ureduce( pi_num, pi_den,
2895                      i_sample * p_track->i_timescale,
2896                      i_total_duration,
2897                      UINT16_MAX);
2898 }
2899
2900 /*
2901  * TrackCreateES:
2902  * Create ES and PES to init decoder if needed, for a track starting at i_chunk
2903  */
2904 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
2905                           unsigned int i_chunk, es_out_id_t **pp_es )
2906 {
2907     demux_sys_t *p_sys = p_demux->p_sys;
2908     unsigned int i_sample_description_index;
2909
2910     if( p_sys->b_fragmented || p_track->i_chunk_count == 0 )
2911         i_sample_description_index = 1; /* XXX */
2912     else
2913         i_sample_description_index =
2914                 p_track->chunk[i_chunk].i_sample_description_index;
2915
2916     if( pp_es )
2917         *pp_es = NULL;
2918
2919     if( !i_sample_description_index )
2920     {
2921         msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
2922                   p_track->i_track_ID );
2923         return VLC_EGENERIC;
2924     }
2925
2926     MP4_Box_t *p_sample = MP4_BoxGet(  p_track->p_stsd, "[%d]",
2927                             i_sample_description_index - 1 );
2928
2929     if( !p_sample ||
2930         ( !p_sample->data.p_payload && p_track->fmt.i_cat != SPU_ES ) )
2931     {
2932         msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
2933                   p_track->i_track_ID );
2934         return VLC_EGENERIC;
2935     }
2936
2937     p_track->p_sample = p_sample;
2938
2939     MP4_Box_t   *p_frma;
2940     if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) && p_frma->data.p_frma )
2941     {
2942         msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
2943
2944         p_sample->i_type = p_frma->data.p_frma->i_type;
2945     }
2946
2947     /* */
2948     switch( p_track->fmt.i_cat )
2949     {
2950     case VIDEO_ES:
2951         if ( p_sample->i_handler != ATOM_vide ||
2952              !SetupVideoES( p_demux, p_track, p_sample ) )
2953             return VLC_EGENERIC;
2954
2955         /* Set frame rate */
2956         TrackGetESSampleRate( p_demux,
2957                               &p_track->fmt.video.i_frame_rate,
2958                               &p_track->fmt.video.i_frame_rate_base,
2959                               p_track, i_sample_description_index, i_chunk );
2960
2961         p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
2962                        (float)p_track->fmt.video.i_frame_rate_base;
2963
2964         break;
2965
2966     case AUDIO_ES:
2967         if ( p_sample->i_handler != ATOM_soun ||
2968              !SetupAudioES( p_demux, p_track, p_sample ) )
2969             return VLC_EGENERIC;
2970         if( p_sys->p_meta )
2971         {
2972             audio_replay_gain_t *p_arg = &p_track->fmt.audio_replay_gain;
2973             const char *psz_meta = vlc_meta_GetExtra( p_sys->p_meta, "replaygain_track_gain" );
2974             if( psz_meta )
2975             {
2976                 double f_gain = us_atof( psz_meta );
2977                 p_arg->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = f_gain;
2978                 p_arg->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = f_gain != 0;
2979             }
2980             psz_meta = vlc_meta_GetExtra( p_sys->p_meta, "replaygain_track_peak" );
2981             if( psz_meta )
2982             {
2983                 double f_gain = us_atof( psz_meta );
2984                 p_arg->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = f_gain;
2985                 p_arg->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = f_gain > 0;
2986             }
2987         }
2988         break;
2989
2990     case SPU_ES:
2991         if ( ( p_sample->i_handler != ATOM_text &&
2992                p_sample->i_handler != ATOM_subt &&
2993                p_sample->i_handler != ATOM_sbtl ) ||
2994              !SetupSpuES( p_demux, p_track, p_sample ) )
2995            return VLC_EGENERIC;
2996         break;
2997
2998     default:
2999         break;
3000     }
3001
3002     if( pp_es )
3003         *pp_es = MP4_AddTrackES( p_demux->out, p_track );
3004
3005     return ( !pp_es || *pp_es ) ? VLC_SUCCESS : VLC_EGENERIC;
3006 }
3007
3008 /* *** Try to find nearest sync points *** */
3009 static int TrackGetNearestSeekPoint( demux_t *p_demux, mp4_track_t *p_track,
3010                                      uint32_t i_sample, uint32_t *pi_sync_sample )
3011 {
3012     int i_ret = VLC_EGENERIC;
3013     *pi_sync_sample = 0;
3014
3015     const MP4_Box_t *p_stss;
3016     if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
3017     {
3018         const MP4_Box_data_stss_t *p_stss_data = BOXDATA(p_stss);
3019         msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
3020                  p_track->i_track_ID );
3021         for( unsigned i_index = 0; i_index < p_stss_data->i_entry_count; i_index++ )
3022         {
3023             if( i_index >= p_stss_data->i_entry_count - 1 ||
3024                 i_sample < p_stss_data->i_sample_number[i_index+1] )
3025             {
3026                 *pi_sync_sample = p_stss_data->i_sample_number[i_index];
3027                 msg_Dbg( p_demux, "stss gives %d --> %" PRIu32 " (sample number)",
3028                          i_sample, *pi_sync_sample );
3029                 i_ret = VLC_SUCCESS;
3030                 break;
3031             }
3032         }
3033     }
3034
3035     /* try rap samples groups */
3036     const MP4_Box_t *p_sbgp = MP4_BoxGet( p_track->p_stbl, "sbgp" );
3037     for( ; p_sbgp; p_sbgp = p_sbgp->p_next )
3038     {
3039         const MP4_Box_data_sbgp_t *p_sbgp_data = BOXDATA(p_sbgp);
3040         if( p_sbgp->i_type != ATOM_sbgp || !p_sbgp_data )
3041             continue;
3042
3043         if( p_sbgp_data->i_grouping_type == SAMPLEGROUP_rap )
3044         {
3045             uint32_t i_group_sample = 0;
3046             for ( uint32_t i=0; i<p_sbgp_data->i_entry_count; i++ )
3047             {
3048                 /* Sample belongs to rap group ? */
3049                 if( p_sbgp_data->entries.pi_group_description_index[i] != 0 )
3050                 {
3051                     if( i_sample < i_group_sample )
3052                     {
3053                         msg_Dbg( p_demux, "sbgp lookup failed %" PRIu32 " (sample number)",
3054                                  i_sample );
3055                         break;
3056                     }
3057                     else if ( i_sample >= i_group_sample &&
3058                               *pi_sync_sample < i_group_sample )
3059                     {
3060                         *pi_sync_sample = i_group_sample;
3061                         i_ret = VLC_SUCCESS;
3062                     }
3063                 }
3064                 i_group_sample += p_sbgp_data->entries.pi_sample_count[i];
3065             }
3066
3067             if( i_ret == VLC_SUCCESS && *pi_sync_sample )
3068             {
3069                 msg_Dbg( p_demux, "sbgp gives %d --> %" PRIu32 " (sample number)",
3070                          i_sample, *pi_sync_sample );
3071             }
3072         }
3073     }
3074
3075     return i_ret;
3076 }
3077
3078 /* given a time it return sample/chunk
3079  * it also update elst field of the track
3080  */
3081 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
3082                                    vlc_tick_t start, uint32_t *pi_chunk,
3083                                    uint32_t *pi_sample )
3084 {
3085     demux_sys_t *p_sys = p_demux->p_sys;
3086     uint64_t     i_dts;
3087     unsigned int i_sample;
3088     unsigned int i_chunk;
3089     int          i_index;
3090     stime_t      i_start;
3091
3092     /* FIXME see if it's needed to check p_track->i_chunk_count */
3093     if( p_track->i_chunk_count == 0 )
3094         return( VLC_EGENERIC );
3095
3096     /* handle elst (find the correct one) */
3097     MP4_TrackSetELST( p_demux, p_track, start );
3098     if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
3099     {
3100         MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
3101         int64_t i_mvt= MP4_rescale_qtime( start, p_sys->i_timescale );
3102
3103         /* now calculate i_start for this elst */
3104         /* offset */
3105         if( start < MP4_rescale_mtime( p_track->i_elst_time, p_sys->i_timescale ) )
3106         {
3107             *pi_chunk = 0;
3108             *pi_sample= 0;
3109
3110             return VLC_SUCCESS;
3111         }
3112         /* to track time scale */
3113         i_start  = MP4_rescale_qtime( start, p_track->i_timescale );
3114         /* add elst offset */
3115         if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
3116              elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
3117             elst->i_media_time[p_track->i_elst] > 0 )
3118         {
3119             i_start += elst->i_media_time[p_track->i_elst];
3120         }
3121
3122         msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
3123                  "ms (track)", p_track->i_elst,
3124                  MP4_rescale( i_mvt, p_sys->i_timescale, 1000 ),
3125                  MP4_rescale( i_start, p_track->i_timescale, 1000 ) );
3126     }
3127     else
3128     {
3129         /* convert absolute time to in timescale unit */
3130         i_start = MP4_rescale_qtime( start, p_track->i_timescale );
3131     }
3132
3133     /* we start from sample 0/chunk 0, hope it won't take too much time */
3134     /* *** find good chunk *** */
3135     for( i_chunk = 0; ; i_chunk++ )
3136     {
3137         if( i_chunk + 1 >= p_track->i_chunk_count )
3138         {
3139             /* at the end and can't check if i_start in this chunk,
3140                it will be check while searching i_sample */
3141             i_chunk = p_track->i_chunk_count - 1;
3142             break;
3143         }
3144
3145         if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
3146             (uint64_t)i_start <  p_track->chunk[i_chunk + 1].i_first_dts )
3147         {
3148             break;
3149         }
3150     }
3151
3152     /* *** find sample in the chunk *** */
3153     i_sample = p_track->chunk[i_chunk].i_sample_first;
3154     i_dts    = p_track->chunk[i_chunk].i_first_dts;
3155     for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
3156     {
3157         if( i_dts +
3158             p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
3159             p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
3160         {
3161             i_dts    +=
3162                 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
3163                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
3164
3165             i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
3166             i_index++;
3167         }
3168         else
3169         {
3170             if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
3171             {
3172                 break;
3173             }
3174             i_sample += ( i_start - i_dts ) /
3175                 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
3176             break;
3177         }
3178     }
3179
3180     if( i_sample >= p_track->i_sample_count )
3181     {
3182         msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
3183                   "(seeking too far) chunk=%d sample=%d",
3184                   p_track->i_track_ID, i_chunk, i_sample );
3185         return( VLC_EGENERIC );
3186     }
3187
3188
3189     /* *** Try to find nearest sync points *** */
3190     uint32_t i_sync_sample;
3191     if( VLC_SUCCESS ==
3192         TrackGetNearestSeekPoint( p_demux, p_track, i_sample, &i_sync_sample ) )
3193     {
3194         /* Go to chunk */
3195         if( i_sync_sample <= i_sample )
3196         {
3197             while( i_chunk > 0 &&
3198                    i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
3199                 i_chunk--;
3200         }
3201         else
3202         {
3203             while( i_chunk < p_track->i_chunk_count - 1 &&
3204                    i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
3205                                     p_track->chunk[i_chunk].i_sample_count )
3206                 i_chunk++;
3207         }
3208         i_sample = i_sync_sample;
3209     }
3210
3211     *pi_chunk  = i_chunk;
3212     *pi_sample = i_sample;
3213
3214     return VLC_SUCCESS;
3215 }
3216
3217 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
3218                                  unsigned int i_chunk, unsigned int i_sample )
3219 {
3220     bool b_reselect = false;
3221
3222     /* now see if actual es is ok */
3223     if( p_track->i_chunk >= p_track->i_chunk_count ||
3224         p_track->chunk[p_track->i_chunk].i_sample_description_index !=
3225             p_track->chunk[i_chunk].i_sample_description_index )
3226     {
3227         msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
3228                   p_track->i_track_ID );
3229
3230         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
3231                         p_track->p_es, &b_reselect );
3232
3233         es_out_Del( p_demux->out, p_track->p_es );
3234
3235         p_track->p_es = NULL;
3236
3237         if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
3238         {
3239             msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
3240                      p_track->i_track_ID );
3241
3242             p_track->b_ok       = false;
3243             p_track->b_selected = false;
3244             return VLC_EGENERIC;
3245         }
3246     }
3247
3248     /* select again the new decoder */
3249     if( b_reselect )
3250     {
3251         es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
3252     }
3253
3254     p_track->i_chunk    = i_chunk;
3255     p_track->chunk[i_chunk].i_sample = i_sample - p_track->chunk[i_chunk].i_sample_first;
3256     p_track->i_sample   = i_sample;
3257
3258     return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
3259 }
3260 #if 0
3261 static void MP4_TrackRestart( demux_t *p_demux, mp4_track_t *p_track,
3262                               MP4_Box_t *p_params_box )
3263 {
3264     bool b_reselect = false;
3265     if( p_track->p_es )
3266     {
3267         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
3268                         p_track->p_es, &b_reselect );
3269     }
3270
3271     /* Save previous fragmented pos */
3272     uint32_t i_sample_pos_backup = p_track->i_sample;
3273     vlc_tick_t time_backup = p_track->i_time;
3274     uint32_t timescale_backup = p_track->i_timescale;
3275
3276     /* Save previous format and ES */
3277     es_format_t fmtbackup;
3278     es_out_id_t *p_es_backup = p_track->p_es;
3279     p_track->p_es = NULL;
3280     es_format_Copy( &fmtbackup, &p_track->fmt );
3281     es_format_Clean( &p_track->fmt );
3282
3283
3284     /* do the cleanup and recycle track / restart */
3285     MP4_TrackDestroy( p_demux, p_track );
3286     memset( p_track, 0, sizeof(*p_track) );
3287
3288     assert(p_params_box->i_type == ATOM_trak);
3289     MP4_TrackCreate( p_demux, p_track, p_params_box, false, true );
3290
3291     if( p_track->b_ok )
3292     {
3293         if( !es_format_IsSimilar( &fmtbackup, &p_track->fmt ) ||
3294             fmtbackup.i_extra != p_track->fmt.i_extra ||
3295             memcmp( fmtbackup.p_extra, p_track->fmt.p_extra, fmtbackup.i_extra ) )
3296         {
3297             if( p_es_backup )
3298                 es_out_Del( p_demux->out, p_es_backup );
3299
3300             if( !p_track->b_chapters_source )
3301             {
3302                 p_track->p_es = MP4_AddTrackES( p_demux->out, p_track );
3303                 p_track->b_ok = !!p_track->p_es;
3304             }
3305         }
3306         else
3307         {
3308             p_track->p_es = p_es_backup;
3309         }
3310     }
3311     else if( p_es_backup )
3312     {
3313         es_out_Del( p_demux->out, p_es_backup );
3314     }
3315
3316     /* select again the new decoder */
3317     if( b_reselect && p_track->p_es )
3318         es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
3319
3320     es_format_Clean( &fmtbackup );
3321
3322     /* Restore fragmented pos */
3323     p_track->i_sample = i_sample_pos_backup;
3324     p_track->i_time = MP4_rescale( time_backup, timescale_backup, p_track->i_timescale );
3325 }
3326 #endif
3327 /****************************************************************************
3328  * MP4_TrackSetup:
3329  ****************************************************************************
3330  * Parse track information and create all needed data to run a track
3331  * If it succeed b_ok is set to 1 else to 0
3332  ****************************************************************************/
3333 static void MP4_TrackSetup( demux_t *p_demux, mp4_track_t *p_track,
3334                              MP4_Box_t *p_box_trak,
3335                              bool b_create_es, bool b_force_enable )
3336 {
3337     demux_sys_t *p_sys = p_demux->p_sys;
3338
3339     p_track->p_track = p_box_trak;
3340
3341     char language[4] = { '\0' };
3342     char sdp_media_type[8] = { '\0' };
3343
3344     const MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
3345     if( !p_tkhd )
3346     {
3347         return;
3348     }
3349
3350     /* do we launch this track by default ? */
3351     p_track->b_enable =
3352         ( ( BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED ) != 0 );
3353
3354     p_track->i_track_ID = BOXDATA(p_tkhd)->i_track_ID;
3355
3356     p_track->i_width = BOXDATA(p_tkhd)->i_width / BLOCK16x16;
3357     p_track->i_height = BOXDATA(p_tkhd)->i_height / BLOCK16x16;
3358     p_track->f_rotation = BOXDATA(p_tkhd)->f_rotation;
3359
3360     /* FIXME: unhandled box: tref */
3361
3362     const MP4_Box_t *p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
3363     const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
3364
3365     if( ( !p_mdhd )||( !p_hdlr ) )
3366     {
3367         return;
3368     }
3369
3370     if( BOXDATA(p_mdhd)->i_timescale == 0 )
3371     {
3372         msg_Warn( p_demux, "Invalid track timescale " );
3373         return;
3374     }
3375     p_track->i_timescale = BOXDATA(p_mdhd)->i_timescale;
3376
3377     memcpy( &language, BOXDATA(p_mdhd)->rgs_language, 3 );
3378     p_track->b_mac_encoding = BOXDATA(p_mdhd)->b_mac_encoding;
3379
3380     switch( p_hdlr->data.p_hdlr->i_handler_type )
3381     {
3382         case( ATOM_soun ):
3383             if( !MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) )
3384             {
3385                 return;
3386             }
3387             es_format_Change( &p_track->fmt, AUDIO_ES, 0 );
3388             break;
3389
3390         case( ATOM_pict ): /* heif */
3391             es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3392             break;
3393
3394         case( ATOM_vide ):
3395             if( !MP4_BoxGet( p_box_trak, "mdia/minf/vmhd") )
3396             {
3397                 return;
3398             }
3399             es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3400             break;
3401
3402         case( ATOM_hint ):
3403             /* RTP Reception Hint tracks */
3404             if( !MP4_BoxGet( p_box_trak, "mdia/minf/hmhd" ) ||
3405                 !MP4_BoxGet( p_box_trak, "mdia/minf/stbl/stsd/rrtp" ) )
3406             {
3407                 break;
3408             }
3409             MP4_Box_t *p_sdp;
3410
3411             /* parse the sdp message to find out whether the RTP stream contained audio or video */
3412             if( !( p_sdp  = MP4_BoxGet( p_box_trak, "udta/hnti/sdp " ) ) )
3413             {
3414                 msg_Warn( p_demux, "Didn't find sdp box to determine stream type" );
3415                 return;
3416             }
3417
3418             memcpy( sdp_media_type, BOXDATA(p_sdp)->psz_text, 7 );
3419             if( !strcmp(sdp_media_type, "m=audio") )
3420             {
3421                 msg_Dbg( p_demux, "Found audio Rtp: %s", sdp_media_type );
3422                 es_format_Change( &p_track->fmt, AUDIO_ES, 0 );
3423             }
3424             else if( !strcmp(sdp_media_type, "m=video") )
3425             {
3426                 msg_Dbg( p_demux, "Found video Rtp: %s", sdp_media_type );
3427                 es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3428             }
3429             else
3430             {
3431                 msg_Warn( p_demux, "Malformed track SDP message: %s", sdp_media_type );
3432                 return;
3433             }
3434             p_track->p_sdp = p_sdp;
3435             break;
3436
3437         case( ATOM_tx3g ):
3438         case( ATOM_text ):
3439         case( ATOM_subp ):
3440         case( ATOM_subt ): /* ttml */
3441         case( ATOM_sbtl ):
3442         case( ATOM_clcp ): /* closed captions */
3443             es_format_Change( &p_track->fmt, SPU_ES, 0 );
3444             break;
3445
3446         default:
3447             return;
3448     }
3449
3450     p_track->asfinfo.i_cat = p_track->fmt.i_cat;
3451
3452     const MP4_Box_t *p_elst;
3453     p_track->i_elst = 0;
3454     p_track->i_elst_time = 0;
3455     if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
3456     {
3457         MP4_Box_data_elst_t *elst = BOXDATA(p_elst);
3458         unsigned int i;
3459
3460         msg_Warn( p_demux, "elst box found" );
3461         for( i = 0; i < elst->i_entry_count; i++ )
3462         {
3463             msg_Dbg( p_demux, "   - [%d] duration=%"PRId64"ms media time=%"PRId64
3464                      "ms) rate=%d.%d", i,
3465                      MP4_rescale( elst->i_segment_duration[i], p_sys->i_timescale, 1000 ),
3466                      elst->i_media_time[i] >= 0 ?
3467                         MP4_rescale( elst->i_media_time[i], p_track->i_timescale, 1000 ) :
3468                         INT64_C(-1),
3469                      elst->i_media_rate_integer[i],
3470                      elst->i_media_rate_fraction[i] );
3471         }
3472     }
3473
3474
3475 /*  TODO
3476     add support for:
3477     p_dinf = MP4_BoxGet( p_minf, "dinf" );
3478 */
3479     if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
3480         !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
3481     {
3482         return;
3483     }
3484
3485     /* Set language */
3486     if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
3487     {
3488         p_track->fmt.psz_language = strdup( language );
3489     }
3490
3491     const MP4_Box_t *p_udta = MP4_BoxGet( p_box_trak, "udta" );
3492     if( p_udta )
3493     {
3494         const MP4_Box_t *p_box_iter;
3495         for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
3496                  p_box_iter = p_box_iter->p_next )
3497         {
3498             switch( p_box_iter->i_type )
3499             {
3500                 case ATOM_0xa9nam:
3501                 case ATOM_name:
3502                     p_track->fmt.psz_description =
3503                         strndup( p_box_iter->data.p_binary->p_blob,
3504                                  p_box_iter->data.p_binary->i_blob );
3505                 default:
3506                     break;
3507             }
3508         }
3509     }
3510
3511     /* Create chunk index table and sample index table */
3512     if( TrackCreateChunksIndex( p_demux,p_track  ) ||
3513         TrackCreateSamplesIndex( p_demux, p_track ) )
3514     {
3515         msg_Err( p_demux, "cannot create chunks index" );
3516         return; /* cannot create chunks index */
3517     }
3518
3519     p_track->i_chunk  = 0;
3520     p_track->i_sample = 0;
3521
3522     /* Mark chapter only track */
3523     if( p_sys->p_tref_chap )
3524     {
3525         MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
3526         unsigned int i;
3527
3528         for( i = 0; i < p_chap->i_entry_count; i++ )
3529         {
3530             if( p_track->i_track_ID == p_chap->i_track_ID[i] &&
3531                 p_track->fmt.i_cat == UNKNOWN_ES )
3532             {
3533                 p_track->b_chapters_source = true;
3534                 p_track->b_enable = false;
3535                 break;
3536             }
3537         }
3538     }
3539
3540     const MP4_Box_t *p_tsel;
3541     /* now create es */
3542     if( b_force_enable &&
3543         ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
3544     {
3545         msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",