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