1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <vlc_demux.h>
32 #include <vlc_charset.h> /* EnsureUTF8 */
33 #include <vlc_input.h>
35 #include <vlc_plugin.h>
36 #include <vlc_dialog.h>
40 #include "../codec/cc.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t
* );
46 static void Close( vlc_object_t
* );
48 #define CFG_PREFIX "mp4-"
50 #define MP4_M4A_TEXT N_("M4A audio only")
51 #define MP4_M4A_LONGTEXT N_("Ignore non audio tracks from iTunes audio files")
54 set_category( CAT_INPUT
)
55 set_subcategory( SUBCAT_INPUT_DEMUX
)
56 set_description( N_("MP4 stream demuxer") )
57 set_shortname( N_("MP4") )
58 set_capability( "demux", 240 )
59 set_callbacks( Open
, Close
)
61 add_category_hint("Hacks", NULL
, true)
62 add_bool( CFG_PREFIX
"m4a-audioonly", false, MP4_M4A_TEXT
, MP4_M4A_LONGTEXT
, true )
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Demux ( demux_t
* );
69 static int DemuxRef( demux_t
*p_demux
){ (void)p_demux
; return 0;}
70 static int DemuxFrag( demux_t
* );
71 static int Control ( demux_t
*, int, va_list );
75 MP4_Box_t
*p_root
; /* container for the whole file */
79 uint64_t i_moov_duration
;
80 uint64_t i_duration
; /* Declared fragmented duration */
81 uint64_t i_cumulated_duration
; /* Same as above, but not from probing */
82 uint64_t i_time
; /* time position of the presentation
83 * in movie timescale */
84 uint32_t i_timescale
; /* movie time scale */
85 unsigned int i_tracks
; /* number of tracks */
86 mp4_track_t
*track
; /* array of track */
87 float f_fps
; /* number of frame per seconds */
89 bool b_fragmented
; /* fMP4 */
92 bool b_error
; /* unrecoverable */
94 bool b_index_probed
; /* mFra sync points index */
95 bool b_fragments_probed
; /* moof segments index created */
101 uint32_t i_current_box_type
;
102 MP4_Box_t
*p_fragment_atom
;
103 uint64_t i_post_mdat_offset
;
104 uint32_t i_lastseqnumber
;
108 MP4_Box_t
*p_tref_chap
;
111 input_title_t
*p_title
;
115 asf_packet_sys_t asfpacketsys
;
116 uint64_t i_preroll
; /* foobar */
117 int64_t i_preroll_start
;
124 mp4_fragments_index_t
*p_fragsindex
;
127 #define DEMUX_INCREMENT (CLOCK_FREQ / 4) /* How far the pcr will go, each round */
128 #define DEMUX_TRACK_MAX_PRELOAD (CLOCK_FREQ * 15) /* maximum preloading, to deal with interleaving */
130 #define VLC_DEMUXER_EOS (VLC_DEMUXER_EGENERIC - 1)
132 const uint32_t rgi_pict_atoms
[2] = { ATOM_PICT
, ATOM_pict
};
133 const char *psz_meta_roots
[] = { "/moov/udta/meta/ilst",
141 /*****************************************************************************
142 * Declaration of local function
143 *****************************************************************************/
144 static void MP4_TrackSetup( demux_t
*, mp4_track_t
*, MP4_Box_t
*, bool, bool );
145 static void MP4_TrackInit( mp4_track_t
* );
146 static void MP4_TrackClean( es_out_t
*, mp4_track_t
* );
148 static void MP4_Block_Send( demux_t
*, mp4_track_t
*, block_t
* );
150 static void MP4_TrackSelect ( demux_t
*, mp4_track_t
*, bool );
151 static int MP4_TrackSeek ( demux_t
*, mp4_track_t
*, mtime_t
);
153 static uint64_t MP4_TrackGetPos ( mp4_track_t
* );
154 static uint32_t MP4_TrackGetReadSize( mp4_track_t
*, uint32_t * );
155 static int MP4_TrackNextSample( demux_t
*, mp4_track_t
*, uint32_t );
156 static void MP4_TrackSetELST( demux_t
*, mp4_track_t
*, int64_t );
158 static void MP4_UpdateSeekpoint( demux_t
*, int64_t );
160 static MP4_Box_t
* MP4_GetTrexByTrackID( MP4_Box_t
*p_moov
, const uint32_t i_id
);
161 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t
*p_moov
,
162 const MP4_Box_data_tfhd_t
*p_tfhd_data
,
163 uint32_t *pi_default_size
,
164 uint32_t *pi_default_duration
);
166 static stime_t
GetMoovTrackDuration( demux_sys_t
*p_sys
, unsigned i_track_ID
);
168 static int ProbeFragments( demux_t
*p_demux
, bool b_force
, bool *pb_fragmented
);
169 static int ProbeIndex( demux_t
*p_demux
);
171 static int FragCreateTrunIndex( demux_t
*, MP4_Box_t
*, MP4_Box_t
*, stime_t
, bool );
173 static int FragGetMoofBySidxIndex( demux_t
*p_demux
, mtime_t i_target_time
,
174 uint64_t *pi_moof_pos
, mtime_t
*pi_sampletime
);
175 static int FragGetMoofByTfraIndex( demux_t
*p_demux
, const mtime_t i_target_time
, unsigned i_track_ID
,
176 uint64_t *pi_moof_pos
, mtime_t
*pi_sampletime
);
177 static void FragResetContext( demux_sys_t
* );
180 static asf_track_info_t
* MP4ASF_GetTrackInfo( asf_packet_sys_t
*p_packetsys
, uint8_t i_stream_number
);
181 static void MP4ASF_Send(asf_packet_sys_t
*p_packetsys
, uint8_t i_stream_number
, block_t
**pp_frame
);
182 static void MP4ASF_ResetFrames( demux_sys_t
*p_sys
);
185 static block_t
* MP4_RTPHint_Convert( demux_t
*p_demux
, block_t
*p_block
, vlc_fourcc_t i_codec
);
186 static block_t
* MP4_RTPHintToFrame( demux_t
*p_demux
, block_t
*p_block
, uint32_t packetcount
);
188 static int MP4_LoadMeta( demux_sys_t
*p_sys
, vlc_meta_t
*p_meta
);
192 static int64_t MP4_rescale( int64_t i_value
, uint32_t i_timescale
, uint32_t i_newscale
)
194 if( i_timescale
== i_newscale
)
197 if( i_value
<= INT64_MAX
/ i_newscale
)
198 return i_value
* i_newscale
/ i_timescale
;
201 int64_t q
= i_value
/ i_timescale
;
202 int64_t r
= i_value
% i_timescale
;
203 return q
* i_newscale
+ r
* i_newscale
/ i_timescale
;
206 static uint32_t stream_ReadU32( stream_t
*s
, void *p_read
, uint32_t i_toread
)
208 ssize_t i_return
= 0;
209 if ( i_toread
> INT32_MAX
)
211 i_return
= vlc_stream_Read( s
, p_read
, (size_t) INT32_MAX
);
212 if ( i_return
< INT32_MAX
)
215 i_toread
-= INT32_MAX
;
217 i_return
+= vlc_stream_Read( s
, (uint8_t *)p_read
+ i_return
, (size_t) i_toread
);
221 static MP4_Box_t
* MP4_GetTrexByTrackID( MP4_Box_t
*p_moov
, const uint32_t i_id
)
225 MP4_Box_t
*p_trex
= MP4_BoxGet( p_moov
, "mvex/trex" );
228 if ( p_trex
->i_type
== ATOM_trex
&&
229 BOXDATA(p_trex
) && BOXDATA(p_trex
)->i_track_ID
== i_id
)
232 p_trex
= p_trex
->p_next
;
238 * Return the track identified by tid
240 static mp4_track_t
*MP4_GetTrackByTrackID( demux_t
*p_demux
, const uint32_t tid
)
242 demux_sys_t
*p_sys
= p_demux
->p_sys
;
244 mp4_track_t
*ret
= NULL
;
245 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
247 ret
= &p_sys
->track
[i
];
248 if( ret
->i_track_ID
== tid
)
254 static MP4_Box_t
* MP4_GetTrakByTrackID( MP4_Box_t
*p_moov
, const uint32_t i_id
)
256 MP4_Box_t
*p_trak
= MP4_BoxGet( p_moov
, "trak" );
260 if( p_trak
->i_type
== ATOM_trak
&&
261 (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) && BOXDATA(p_tkhd
) &&
262 BOXDATA(p_tkhd
)->i_track_ID
== i_id
)
265 p_trak
= p_trak
->p_next
;
270 static MP4_Box_t
* MP4_GetTrafByTrackID( MP4_Box_t
*p_moof
, const uint32_t i_id
)
272 MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
276 if( p_traf
->i_type
== ATOM_traf
&&
277 (p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" )) && BOXDATA(p_tfhd
) &&
278 BOXDATA(p_tfhd
)->i_track_ID
== i_id
)
281 p_traf
= p_traf
->p_next
;
286 static es_out_id_t
* MP4_AddTrackES( es_out_t
*out
, mp4_track_t
*p_track
)
288 es_out_id_t
*p_es
= es_out_Add( out
, &p_track
->fmt
);
289 /* Force SPU which isn't selected/defaulted */
290 if( p_track
->fmt
.i_cat
== SPU_ES
&& p_es
&& p_track
->b_forced_spu
)
291 es_out_Control( out
, ES_OUT_SET_ES_DEFAULT
, p_es
);
296 /* Return time in microsecond of a track */
297 static inline int64_t MP4_TrackGetDTS( demux_t
*p_demux
, mp4_track_t
*p_track
)
299 demux_sys_t
*p_sys
= p_demux
->p_sys
;
300 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[p_track
->i_chunk
];
302 unsigned int i_index
= 0;
303 unsigned int i_sample
= p_track
->i_sample
- p_chunk
->i_sample_first
;
304 int64_t i_dts
= p_chunk
->i_first_dts
;
306 while( i_sample
> 0 && i_index
< p_chunk
->i_entries_dts
)
308 if( i_sample
> p_chunk
->p_sample_count_dts
[i_index
] )
310 i_dts
+= p_chunk
->p_sample_count_dts
[i_index
] *
311 p_chunk
->p_sample_delta_dts
[i_index
];
312 i_sample
-= p_chunk
->p_sample_count_dts
[i_index
];
317 i_dts
+= i_sample
* p_chunk
->p_sample_delta_dts
[i_index
];
322 /* now handle elst */
323 if( p_track
->p_elst
)
325 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
327 /* convert to offset */
328 if( ( elst
->i_media_rate_integer
[p_track
->i_elst
] > 0 ||
329 elst
->i_media_rate_fraction
[p_track
->i_elst
] > 0 ) &&
330 elst
->i_media_time
[p_track
->i_elst
] > 0 )
332 i_dts
-= elst
->i_media_time
[p_track
->i_elst
];
335 /* add i_elst_time */
336 i_dts
+= MP4_rescale( p_track
->i_elst_time
, p_sys
->i_timescale
, p_track
->i_timescale
);
338 if( i_dts
< 0 ) i_dts
= 0;
341 return MP4_rescale( i_dts
, p_track
->i_timescale
, CLOCK_FREQ
);
344 static inline bool MP4_TrackGetPTSDelta( demux_t
*p_demux
, mp4_track_t
*p_track
,
347 VLC_UNUSED( p_demux
);
348 mp4_chunk_t
*ck
= &p_track
->chunk
[p_track
->i_chunk
];
350 unsigned int i_index
= 0;
351 unsigned int i_sample
= p_track
->i_sample
- ck
->i_sample_first
;
353 if( ck
->p_sample_count_pts
== NULL
|| ck
->p_sample_offset_pts
== NULL
)
356 for( i_index
= 0; i_index
< ck
->i_entries_pts
; i_index
++ )
358 if( i_sample
< ck
->p_sample_count_pts
[i_index
] )
360 *pi_delta
= MP4_rescale( ck
->p_sample_offset_pts
[i_index
],
361 p_track
->i_timescale
, CLOCK_FREQ
);
365 i_sample
-= ck
->p_sample_count_pts
[i_index
];
370 static inline int64_t MP4_GetMoviePTS(demux_sys_t
*p_sys
)
372 return MP4_rescale( p_sys
->i_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
375 static void LoadChapter( demux_t
*p_demux
);
377 static int LoadInitFrag( demux_t
*p_demux
)
379 demux_sys_t
*p_sys
= p_demux
->p_sys
;
381 /* Load all boxes ( except raw data ) */
382 if( ( p_sys
->p_root
= MP4_BoxGetRoot( p_demux
->s
) ) == NULL
)
384 goto LoadInitFragError
;
390 msg_Warn( p_demux
, "MP4 plugin discarded (not a valid initialization chunk)" );
394 static int CreateTracks( demux_t
*p_demux
, unsigned i_tracks
)
396 demux_sys_t
*p_sys
= p_demux
->p_sys
;
398 if( SIZE_MAX
/ i_tracks
< sizeof(mp4_track_t
) )
401 p_sys
->track
= vlc_alloc( i_tracks
, sizeof(mp4_track_t
) );
402 if( p_sys
->track
== NULL
)
404 p_sys
->i_tracks
= i_tracks
;
406 for( unsigned i
=0; i
<i_tracks
; i
++ )
407 MP4_TrackInit( &p_sys
->track
[i
] );
412 static block_t
* MP4_EIA608_Convert( block_t
* p_block
)
414 /* Rebuild codec data from encap */
416 size_t i_remaining
= __MIN(p_block
->i_buffer
, INT64_MAX
/ 3);
417 uint32_t i_bytes
= 0;
420 /* always need at least 10 bytes (atom size+header+1pair)*/
421 if ( i_remaining
< 10 ||
422 !(i_bytes
= GetDWBE(p_block
->p_buffer
)) ||
423 (i_bytes
> i_remaining
) ||
424 memcmp("cdat", &p_block
->p_buffer
[4], 4) ||
425 !(p_newblock
= block_Alloc( i_remaining
* 3 - 8 )) )
427 p_block
->i_buffer
= 0;
431 uint8_t *p_write
= p_newblock
->p_buffer
;
432 uint8_t *p_read
= &p_block
->p_buffer
[8];
438 p_write
[i_copied
++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
439 p_write
[i_copied
++] = p_read
[0];
440 p_write
[i_copied
++] = p_read
[1];
444 } while( i_bytes
>= 2 );
446 /* cdt2 is optional */
447 if ( i_remaining
>= 10 &&
448 (i_bytes
= GetDWBE(p_read
)) &&
449 (i_bytes
<= i_remaining
) &&
450 !memcmp("cdt2", &p_read
[4], 4) )
457 p_write
[i_copied
++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
458 p_write
[i_copied
++] = p_read
[0];
459 p_write
[i_copied
++] = p_read
[1];
462 } while( i_bytes
>= 2 );
465 p_newblock
->i_pts
= p_block
->i_dts
;
466 p_newblock
->i_buffer
= i_copied
;
467 p_newblock
->i_flags
= BLOCK_FLAG_TYPE_P
;
468 block_Release( p_block
);
473 static uint32_t MP4_TrackGetRunSeq( mp4_track_t
*p_track
)
475 if( p_track
->i_chunk_count
> 0 )
476 return p_track
->chunk
[p_track
->i_chunk
].i_virtual_run_number
;
480 /* Analyzes chunks to find max interleave length
481 * sets flat flag if no interleaving is in use */
482 static void MP4_GetInterleaving( demux_t
*p_demux
, uint64_t *pi_max_contiguous
, bool *pb_flat
)
484 demux_sys_t
*p_sys
= p_demux
->p_sys
;
485 *pi_max_contiguous
= 0;
488 /* Find first recorded chunk */
489 mp4_track_t
*tk
= NULL
;
490 uint64_t i_duration
= 0;
491 for( unsigned i
=0; i
< p_sys
->i_tracks
; i
++ )
493 mp4_track_t
*cur
= &p_sys
->track
[i
];
494 if( !cur
->i_chunk_count
)
497 if( tk
== NULL
|| cur
->chunk
[0].i_offset
< tk
->chunk
[0].i_offset
)
503 i_duration
+= tk
->chunk
[tk
->i_chunk
].i_duration
;
506 /* Find next chunk in data order */
507 mp4_track_t
*nexttk
= NULL
;
508 for( unsigned i
=0; i
< p_sys
->i_tracks
; i
++ )
510 mp4_track_t
*cur
= &p_sys
->track
[i
];
511 if( cur
->i_chunk
== cur
->i_chunk_count
)
514 if( nexttk
== NULL
||
515 cur
->chunk
[cur
->i_chunk
].i_offset
< nexttk
->chunk
[nexttk
->i_chunk
].i_offset
)
519 /* copy previous run */
520 if( nexttk
&& nexttk
->i_chunk
> 0 )
521 nexttk
->chunk
[nexttk
->i_chunk
].i_virtual_run_number
=
522 nexttk
->chunk
[nexttk
->i_chunk
- 1].i_virtual_run_number
;
526 i_duration
= MP4_rescale( i_duration
, tk
->i_timescale
, CLOCK_FREQ
);
527 if( i_duration
> *pi_max_contiguous
)
528 *pi_max_contiguous
= i_duration
;
531 if( tk
->i_chunk
!= tk
->i_chunk_count
)
534 if( nexttk
&& nexttk
->i_chunk
> 0 ) /* new run number */
535 nexttk
->chunk
[nexttk
->i_chunk
].i_virtual_run_number
++;
542 for( unsigned i
=0; i
< p_sys
->i_tracks
; i
++ )
543 p_sys
->track
[i
].i_chunk
= 0;
546 static block_t
* MP4_Block_Convert( demux_t
*p_demux
, const mp4_track_t
*p_track
, block_t
*p_block
)
548 /* might have some encap */
549 if( p_track
->fmt
.i_cat
== SPU_ES
)
551 switch( p_track
->fmt
.i_codec
)
553 case VLC_CODEC_WEBVTT
:
560 case VLC_CODEC_CEA608
:
561 p_block
= MP4_EIA608_Convert( p_block
);
564 p_block
->i_buffer
= 0;
568 else if( p_track
->fmt
.i_original_fourcc
== ATOM_rrtp
)
570 p_block
= MP4_RTPHint_Convert( p_demux
, p_block
, p_track
->fmt
.i_codec
);
576 static void MP4_Block_Send( demux_t
*p_demux
, mp4_track_t
*p_track
, block_t
*p_block
)
578 p_block
= MP4_Block_Convert( p_demux
, p_track
, p_block
);
579 if( p_block
== NULL
)
582 if ( p_track
->b_chans_reorder
)
584 aout_ChannelReorder( p_block
->p_buffer
, p_block
->i_buffer
,
585 p_track
->fmt
.audio
.i_channels
,
586 p_track
->rgi_chans_reordering
,
587 p_track
->fmt
.i_codec
);
590 p_block
->i_flags
|= p_track
->i_block_flags
;
591 if( p_track
->i_next_block_flags
)
593 p_block
->i_flags
|= p_track
->i_next_block_flags
;
594 p_track
->i_next_block_flags
= 0;
597 /* ASF packets in mov */
600 /* Fake a new stream from MP4 block */
601 stream_t
*p_stream
= p_demux
->s
;
602 p_demux
->s
= vlc_stream_MemoryNew( p_demux
, p_block
->p_buffer
, p_block
->i_buffer
, true );
605 p_track
->i_dts_backup
= p_block
->i_dts
;
606 p_track
->i_pts_backup
= p_block
->i_pts
;
607 /* And demux it as ASF packet */
608 DemuxASFPacket( &p_demux
->p_sys
->asfpacketsys
, p_block
->i_buffer
, p_block
->i_buffer
);
609 vlc_stream_Delete(p_demux
->s
);
611 block_Release(p_block
);
612 p_demux
->s
= p_stream
;
615 es_out_Send( p_demux
->out
, p_track
->p_es
, p_block
);
618 /*****************************************************************************
619 * Open: check file and initializes MP4 structures
620 *****************************************************************************/
621 static int Open( vlc_object_t
* p_this
)
623 demux_t
*p_demux
= (demux_t
*)p_this
;
626 const uint8_t *p_peek
;
629 const MP4_Box_t
*p_mvhd
= NULL
;
630 const MP4_Box_t
*p_mvex
= NULL
;
634 /* A little test to see if it could be a mp4 */
635 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 11 ) < 11 ) return VLC_EGENERIC
;
637 switch( VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) )
648 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
651 /* We don't yet support f4v, but avformat does. */
652 if( p_peek
[8] == 'f' && p_peek
[9] == '4' && p_peek
[10] == 'v' )
659 /* create our structure that will contains all data */
660 p_sys
= calloc( 1, sizeof( demux_sys_t
) );
665 vlc_stream_Control( p_demux
->s
, STREAM_CAN_SEEK
, &p_sys
->b_seekable
);
666 if( p_sys
->b_seekable
)
667 vlc_stream_Control( p_demux
->s
, STREAM_CAN_FASTSEEK
, &p_sys
->b_fastseekable
);
669 /*Set exported functions */
670 p_demux
->pf_demux
= Demux
;
671 p_demux
->pf_control
= Control
;
673 p_sys
->context
.i_lastseqnumber
= UINT32_MAX
;
675 p_demux
->p_sys
= p_sys
;
677 if( LoadInitFrag( p_demux
) != VLC_SUCCESS
)
680 MP4_BoxDumpStructure( p_demux
->s
, p_sys
->p_root
);
682 if( ( p_ftyp
= MP4_BoxGet( p_sys
->p_root
, "/ftyp" ) ) )
684 switch( BOXDATA(p_ftyp
)->i_major_brand
)
688 "ISO Media (isom) version %d.",
689 BOXDATA(p_ftyp
)->i_minor_version
);
695 msg_Dbg( p_demux
, "3GPP Media Release: %4.4s",
696 (char *)&BOXDATA(p_ftyp
)->i_major_brand
);
699 msg_Dbg( p_demux
, "Apple QuickTime media" );
702 msg_Dbg( p_demux
, "PIFF (= isml = fMP4) media" );
705 msg_Dbg( p_demux
, "DASH Stream" );
708 msg_Dbg( p_demux
, "iTunes audio" );
709 if( var_InheritBool( p_demux
, CFG_PREFIX
"m4a-audioonly" ) )
710 p_sys
->hacks
.es_cat_filters
= AUDIO_ES
;
714 "unrecognized major media specification (%4.4s).",
715 (char*)&BOXDATA(p_ftyp
)->i_major_brand
);
718 /* also lookup in compatibility list */
719 for(uint32_t i
=0; i
<BOXDATA(p_ftyp
)->i_compatible_brands_count
; i
++)
721 if (BOXDATA(p_ftyp
)->i_compatible_brands
[i
] == MAJOR_dash
)
723 msg_Dbg( p_demux
, "DASH Stream" );
725 else if (BOXDATA(p_ftyp
)->i_compatible_brands
[i
] == VLC_FOURCC('s', 'm', 'o', 'o') )
727 msg_Dbg( p_demux
, "Handling VLC Smooth Stream" );
733 msg_Dbg( p_demux
, "file type box missing (assuming ISO Media)" );
736 /* the file need to have one moov box */
737 p_sys
->p_moov
= MP4_BoxGet( p_sys
->p_root
, "/moov" );
738 if( unlikely(!p_sys
->p_moov
) )
740 p_sys
->p_moov
= MP4_BoxGet( p_sys
->p_root
, "/foov" );
743 msg_Err( p_demux
, "MP4 plugin discarded (no moov,foov,moof box)" );
746 /* we have a free box as a moov, rename it */
747 p_sys
->p_moov
->i_type
= ATOM_moov
;
750 p_mvhd
= MP4_BoxGet( p_sys
->p_moov
, "mvhd" );
751 if( p_mvhd
&& BOXDATA(p_mvhd
) && BOXDATA(p_mvhd
)->i_timescale
)
753 p_sys
->i_timescale
= BOXDATA(p_mvhd
)->i_timescale
;
754 p_sys
->i_moov_duration
= p_sys
->i_duration
= BOXDATA(p_mvhd
)->i_duration
;
755 p_sys
->i_cumulated_duration
= BOXDATA(p_mvhd
)->i_duration
;
759 msg_Warn( p_demux
, "No valid mvhd found" );
763 MP4_Box_t
*p_rmra
= MP4_BoxGet( p_sys
->p_root
, "/moov/rmra" );
764 if( p_rmra
!= NULL
&& p_demux
->p_input
!= NULL
)
766 int i_count
= MP4_BoxCount( p_rmra
, "rmda" );
769 msg_Dbg( p_demux
, "detected playlist mov file (%d ref)", i_count
);
771 input_thread_t
*p_input
= p_demux
->p_input
;
772 input_item_t
*p_current
= input_GetItem( p_input
);
774 input_item_node_t
*p_subitems
= input_item_node_Create( p_current
);
776 for( i
= 0; i
< i_count
; i
++ )
778 MP4_Box_t
*p_rdrf
= MP4_BoxGet( p_rmra
, "rmda[%d]/rdrf", i
);
782 if( !p_rdrf
|| !BOXDATA(p_rdrf
) || !( psz_ref
= strdup( BOXDATA(p_rdrf
)->psz_ref
) ) )
786 i_ref_type
= BOXDATA(p_rdrf
)->i_ref_type
;
788 msg_Dbg( p_demux
, "new ref=`%s' type=%4.4s",
789 psz_ref
, (char*)&i_ref_type
);
791 if( i_ref_type
== VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
793 if( strstr( psz_ref
, "qt5gateQT" ) )
795 msg_Dbg( p_demux
, "ignoring pseudo ref =`%s'", psz_ref
);
799 if( !strncmp( psz_ref
, "http://", 7 ) ||
800 !strncmp( psz_ref
, "rtsp://", 7 ) )
806 char *psz_absolute
= vlc_uri_resolve( p_demux
->psz_url
,
809 if( psz_absolute
== NULL
)
811 input_item_node_Delete( p_subitems
);
814 psz_ref
= psz_absolute
;
816 msg_Dbg( p_demux
, "adding ref = `%s'", psz_ref
);
817 input_item_t
*p_item
= input_item_New( psz_ref
, NULL
);
818 input_item_CopyOptions( p_item
, p_current
);
819 input_item_node_AppendItem( p_subitems
, p_item
);
820 input_item_Release( p_item
);
824 msg_Err( p_demux
, "unknown ref type=%4.4s FIXME (send a bug report)",
825 (char*)&BOXDATA(p_rdrf
)->i_ref_type
);
830 /* FIXME: create a stream_filter sub-module for this */
831 if (es_out_Control(p_demux
->out
, ES_OUT_POST_SUBNODE
, p_subitems
))
832 input_item_node_Delete(p_subitems
);
835 if( !(p_mvhd
= MP4_BoxGet( p_sys
->p_root
, "/moov/mvhd" ) ) )
839 msg_Err( p_demux
, "cannot find /moov/mvhd" );
844 msg_Warn( p_demux
, "cannot find /moov/mvhd (pure ref file)" );
845 p_demux
->pf_demux
= DemuxRef
;
851 p_sys
->i_timescale
= BOXDATA(p_mvhd
)->i_timescale
;
852 if( p_sys
->i_timescale
== 0 )
854 msg_Err( p_this
, "bad timescale" );
859 const unsigned i_tracks
= MP4_BoxCount( p_sys
->p_root
, "/moov/trak" );
862 msg_Err( p_demux
, "cannot find any /moov/trak" );
865 msg_Dbg( p_demux
, "found %u track%c", i_tracks
, i_tracks
? 's':' ' );
867 if( CreateTracks( p_demux
, i_tracks
) != VLC_SUCCESS
)
870 /* Search the first chap reference (like quicktime) and
871 * check that at least 1 stream is enabled */
872 p_sys
->p_tref_chap
= NULL
;
873 b_enabled_es
= false;
874 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
876 MP4_Box_t
*p_trak
= MP4_BoxGet( p_sys
->p_root
, "/moov/trak[%d]", i
);
879 MP4_Box_t
*p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" );
880 if( p_tkhd
&& BOXDATA(p_tkhd
) && (BOXDATA(p_tkhd
)->i_flags
&MP4_TRACK_ENABLED
) )
883 MP4_Box_t
*p_chap
= MP4_BoxGet( p_trak
, "tref/chap", i
);
884 if( p_chap
&& p_chap
->data
.p_tref_generic
&&
885 p_chap
->data
.p_tref_generic
->i_entry_count
> 0 && !p_sys
->p_tref_chap
)
886 p_sys
->p_tref_chap
= p_chap
;
889 /* Set and store metadata */
890 if( (p_sys
->p_meta
= vlc_meta_New()) )
891 MP4_LoadMeta( p_sys
, p_sys
->p_meta
);
893 /* now process each track and extract all useful information */
894 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
896 MP4_Box_t
*p_trak
= MP4_BoxGet( p_sys
->p_root
, "/moov/trak[%u]", i
);
897 MP4_TrackSetup( p_demux
, &p_sys
->track
[i
], p_trak
, true, !b_enabled_es
);
899 if( p_sys
->track
[i
].b_ok
&& !p_sys
->track
[i
].b_chapters_source
)
902 switch( p_sys
->track
[i
].fmt
.i_cat
)
911 psz_cat
= "subtitle";
919 msg_Dbg( p_demux
, "adding track[Id 0x%x] %s (%s) language %s",
920 p_sys
->track
[i
].i_track_ID
, psz_cat
,
921 p_sys
->track
[i
].b_enable
? "enable":"disable",
922 p_sys
->track
[i
].fmt
.psz_language
?
923 p_sys
->track
[i
].fmt
.psz_language
: "undef" );
925 else if( p_sys
->track
[i
].b_ok
&& p_sys
->track
[i
].b_chapters_source
)
927 msg_Dbg( p_demux
, "using track[Id 0x%x] for chapter language %s",
928 p_sys
->track
[i
].i_track_ID
,
929 p_sys
->track
[i
].fmt
.psz_language
?
930 p_sys
->track
[i
].fmt
.psz_language
: "undef" );
934 msg_Dbg( p_demux
, "ignoring track[Id 0x%x]",
935 p_sys
->track
[i
].i_track_ID
);
939 p_mvex
= MP4_BoxGet( p_sys
->p_moov
, "mvex" );
942 const MP4_Box_t
*p_mehd
= MP4_BoxGet( p_mvex
, "mehd");
943 if ( p_mehd
&& BOXDATA(p_mehd
) )
945 if( BOXDATA(p_mehd
)->i_fragment_duration
> p_sys
->i_duration
)
947 p_sys
->b_fragmented
= true;
948 p_sys
->i_duration
= BOXDATA(p_mehd
)->i_fragment_duration
;
952 const MP4_Box_t
*p_sidx
= MP4_BoxGet( p_sys
->p_root
, "sidx");
954 p_sys
->b_fragmented
= true;
956 if ( p_sys
->b_seekable
)
958 if( !p_sys
->b_fragmented
/* as unknown */ )
960 /* Probe remaining to check if there's really fragments
961 or if that file is just ready to append fragments */
962 ProbeFragments( p_demux
, (p_sys
->i_duration
== 0), &p_sys
->b_fragmented
);
965 if( vlc_stream_Seek( p_demux
->s
, p_sys
->p_moov
->i_pos
) != VLC_SUCCESS
)
968 else /* Handle as fragmented by default as we can't see moof */
970 p_sys
->context
.p_fragment_atom
= p_sys
->p_moov
;
971 p_sys
->context
.i_current_box_type
= ATOM_moov
;
972 p_sys
->b_fragmented
= true;
976 if( p_sys
->b_fragmented
)
978 p_demux
->pf_demux
= DemuxFrag
;
979 msg_Dbg( p_demux
, "Set Fragmented demux mode" );
982 if( !p_sys
->b_seekable
&& p_demux
->pf_demux
== Demux
)
984 msg_Warn( p_demux
, "MP4 plugin discarded (not seekable)" );
988 if( p_sys
->i_tracks
> 1 && !p_sys
->b_fastseekable
)
990 uint64_t i_max_continuity
;
992 MP4_GetInterleaving( p_demux
, &i_max_continuity
, &b_flat
);
994 msg_Warn( p_demux
, "that media doesn't look interleaved, will need to seek");
995 else if( i_max_continuity
> DEMUX_TRACK_MAX_PRELOAD
)
996 msg_Warn( p_demux
, "that media doesn't look properly interleaved, will need to seek");
1000 LoadChapter( p_demux
);
1002 p_sys
->asfpacketsys
.p_demux
= p_demux
;
1003 p_sys
->asfpacketsys
.pi_preroll
= &p_sys
->i_preroll
;
1004 p_sys
->asfpacketsys
.pi_preroll_start
= &p_sys
->i_preroll_start
;
1005 p_sys
->asfpacketsys
.pf_doskip
= NULL
;
1006 p_sys
->asfpacketsys
.pf_send
= MP4ASF_Send
;
1007 p_sys
->asfpacketsys
.pf_gettrackinfo
= MP4ASF_GetTrackInfo
;
1008 p_sys
->asfpacketsys
.pf_updatetime
= NULL
;
1009 p_sys
->asfpacketsys
.pf_setaspectratio
= NULL
;
1014 if( vlc_stream_Tell( p_demux
->s
) > 0 )
1016 if( vlc_stream_Seek( p_demux
->s
, 0 ) != VLC_SUCCESS
)
1017 msg_Warn( p_demux
, "Can't reset stream position from probing" );
1022 return VLC_EGENERIC
;
1025 const unsigned int SAMPLEHEADERSIZE
= 4;
1026 const unsigned int RTPPACKETSIZE
= 12;
1027 const unsigned int CONSTRUCTORSIZE
= 16;
1029 /*******************************************************************************
1030 * MP4_RTPHintToFrame: converts RTP Reception Hint Track sample to H.264 frame
1031 *******************************************************************************/
1032 static block_t
* MP4_RTPHintToFrame( demux_t
*p_demux
, block_t
*p_block
, uint32_t packetcount
)
1034 uint8_t *p_slice
= p_block
->p_buffer
+ SAMPLEHEADERSIZE
;
1035 block_t
*p_newblock
= NULL
;
1036 size_t i_payload
= 0;
1038 if( p_block
->i_buffer
< SAMPLEHEADERSIZE
+ RTPPACKETSIZE
+ CONSTRUCTORSIZE
)
1040 msg_Err( p_demux
, "Sample not large enough for necessary structs");
1041 block_Release( p_block
);
1045 for( uint32_t i
= 0; i
< packetcount
; ++i
)
1047 if( (size_t)(p_slice
- p_block
->p_buffer
) + RTPPACKETSIZE
+ CONSTRUCTORSIZE
> p_block
->i_buffer
)
1050 /* skip RTP header in sample. Could be used to detect packet losses */
1051 p_slice
+= RTPPACKETSIZE
;
1053 mp4_rtpsampleconstructor_t sample_cons
;
1055 sample_cons
.type
= p_slice
[0];
1056 sample_cons
.trackrefindex
= p_slice
[1];
1057 sample_cons
.length
= GetWBE( &p_slice
[2] );
1058 sample_cons
.samplenumber
= GetDWBE( &p_slice
[4] );
1059 sample_cons
.sampleoffset
= GetDWBE( &p_slice
[8] );
1060 sample_cons
.bytesperblock
= GetWBE( &p_slice
[12] );
1061 sample_cons
.samplesperblock
= GetWBE( &p_slice
[14] );
1063 /* skip packet constructor */
1064 p_slice
+= CONSTRUCTORSIZE
;
1066 /* check that is RTPsampleconstructor, referencing itself and no weird audio stuff */
1067 if( sample_cons
.type
!= 2||sample_cons
.trackrefindex
!= -1
1068 ||sample_cons
.samplesperblock
!= 1||sample_cons
.bytesperblock
!= 1 )
1070 msg_Err(p_demux
, "Unhandled constructor in RTP Reception Hint Track. Type:%u", sample_cons
.type
);
1074 /* slice doesn't fit in buffer */
1075 if( sample_cons
.sampleoffset
+ sample_cons
.length
> p_block
->i_buffer
)
1077 msg_Err(p_demux
, "Sample buffer is smaller than sample" );
1081 block_t
*p_realloc
= ( p_newblock
) ?
1082 block_Realloc( p_newblock
, 0, i_payload
+ sample_cons
.length
+ 4 ):
1083 block_Alloc( i_payload
+ sample_cons
.length
+ 4 );
1087 p_newblock
= p_realloc
;
1088 uint8_t *p_dst
= &p_newblock
->p_buffer
[i_payload
];
1090 const uint8_t* p_src
= p_block
->p_buffer
+ sample_cons
.sampleoffset
;
1091 uint8_t i_type
= (*p_src
) & ((1<<5)-1);
1093 const uint8_t synccode
[4] = { 0, 0, 0, 1 };
1094 if( memcmp( p_src
, synccode
, 4 ) )
1096 if( i_type
== 7 || i_type
== 8 )
1105 memcpy( p_dst
, p_src
, sample_cons
.length
);
1106 p_dst
+= sample_cons
.length
;
1108 i_payload
= p_dst
- p_newblock
->p_buffer
;
1111 block_Release( p_block
);
1113 p_newblock
->i_buffer
= i_payload
;
1117 block_Release( p_block
);
1119 block_Release( p_newblock
);
1123 /* RTP Reception Hint Track */
1124 static block_t
* MP4_RTPHint_Convert( demux_t
*p_demux
, block_t
*p_block
, vlc_fourcc_t i_codec
)
1126 block_t
*p_converted
= NULL
;
1127 if( p_block
->i_buffer
< 2 )
1129 block_Release( p_block
);
1133 /* number of RTP packets contained in this sample */
1134 const uint16_t i_packets
= GetWBE( p_block
->p_buffer
);
1135 if( i_packets
<= 1 || i_codec
!= VLC_CODEC_H264
)
1137 const size_t i_skip
= SAMPLEHEADERSIZE
+ i_packets
* ( RTPPACKETSIZE
+ CONSTRUCTORSIZE
);
1138 if( i_packets
== 1 && i_skip
< p_block
->i_buffer
)
1140 p_block
->p_buffer
+= i_skip
;
1141 p_converted
= p_block
;
1145 block_Release( p_block
);
1150 p_converted
= MP4_RTPHintToFrame( p_demux
, p_block
, i_packets
);
1156 /*****************************************************************************
1157 * Demux: read packet and send them to decoders
1158 *****************************************************************************
1159 * TODO check for newly selected track (ie audio upt to now )
1160 *****************************************************************************/
1161 static int DemuxTrack( demux_t
*p_demux
, mp4_track_t
*tk
, uint64_t i_readpos
,
1162 unsigned i_max_preload
)
1164 uint32_t i_nb_samples
= 0;
1165 uint32_t i_samplessize
= 0;
1167 if( !tk
->b_ok
|| tk
->i_sample
>= tk
->i_sample_count
)
1168 return VLC_DEMUXER_EOS
;
1170 if( tk
->b_chapters_source
)
1171 return VLC_DEMUXER_SUCCESS
;
1173 uint32_t i_run_seq
= MP4_TrackGetRunSeq( tk
);
1174 mtime_t i_current_nzdts
= MP4_TrackGetDTS( p_demux
, tk
);
1175 const mtime_t i_demux_max_nzdts
=(i_max_preload
< UINT_MAX
)
1176 ? i_current_nzdts
+ i_max_preload
1179 for( ; i_demux_max_nzdts
>= i_current_nzdts
; )
1181 if( tk
->i_sample
>= tk
->i_sample_count
)
1182 return VLC_DEMUXER_EOS
;
1185 msg_Dbg( p_demux
, "tk(%i)=%"PRId64
" mv=%"PRId64
" pos=%"PRIu64
, tk
->i_track_ID
,
1186 MP4_TrackGetDTS( p_demux
, tk
),
1187 MP4_GetMoviePTS( p_demux
->p_sys
), i_readpos
);
1190 i_samplessize
= MP4_TrackGetReadSize( tk
, &i_nb_samples
);
1191 if( i_samplessize
> 0 )
1196 if( vlc_stream_Tell( p_demux
->s
) != i_readpos
)
1198 if( MP4_Seek( p_demux
->s
, i_readpos
) != VLC_SUCCESS
)
1200 msg_Warn( p_demux
, "track[0x%x] will be disabled (eof?)"
1201 ": Failed to seek to %"PRIu64
,
1202 tk
->i_track_ID
, i_readpos
);
1203 MP4_TrackSelect( p_demux
, tk
, false );
1209 if( !(p_block
= vlc_stream_Block( p_demux
->s
, i_samplessize
)) )
1211 msg_Warn( p_demux
, "track[0x%x] will be disabled (eof?)"
1212 ": Failed to read %d bytes sample at %"PRIu64
,
1213 tk
->i_track_ID
, i_samplessize
, i_readpos
);
1214 MP4_TrackSelect( p_demux
, tk
, false );
1218 /* !important! Ensure clock is set before sending data */
1219 if( p_demux
->p_sys
->i_pcr
== VLC_TS_INVALID
)
1221 es_out_SetPCR( p_demux
->out
, VLC_TS_0
+ i_current_nzdts
);
1222 p_demux
->p_sys
->i_pcr
= VLC_TS_0
+ i_current_nzdts
;
1226 p_block
->i_dts
= VLC_TS_0
+ i_current_nzdts
;
1228 if( MP4_TrackGetPTSDelta( p_demux
, tk
, &i_delta
) )
1229 p_block
->i_pts
= p_block
->i_dts
+ i_delta
;
1230 else if( tk
->fmt
.i_cat
!= VIDEO_ES
)
1231 p_block
->i_pts
= p_block
->i_dts
;
1233 p_block
->i_pts
= VLC_TS_INVALID
;
1235 MP4_Block_Send( p_demux
, tk
, p_block
);
1239 if ( i_nb_samples
) /* sample size could be 0, need to go fwd. see return */
1240 MP4_TrackNextSample( p_demux
, tk
, i_nb_samples
);
1242 uint32_t i_next_run_seq
= MP4_TrackGetRunSeq( tk
);
1243 if( i_next_run_seq
!= i_run_seq
)
1246 i_current_nzdts
= MP4_TrackGetDTS( p_demux
, tk
);
1247 i_readpos
= MP4_TrackGetPos( tk
);
1250 return VLC_DEMUXER_SUCCESS
;
1253 return VLC_DEMUXER_EGENERIC
;
1256 static int DemuxMoov( demux_t
*p_demux
)
1258 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1259 unsigned int i_track
;
1261 /* check for newly selected/unselected track */
1262 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1264 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1267 if( !tk
->b_ok
|| tk
->b_chapters_source
||
1268 ( tk
->b_selected
&& tk
->i_sample
>= tk
->i_sample_count
) )
1273 if( p_sys
->b_seekable
)
1274 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
, tk
->p_es
, &b
);
1276 if( tk
->b_selected
&& !b
)
1278 MP4_TrackSelect( p_demux
, tk
, false );
1280 else if( !tk
->b_selected
&& b
)
1282 MP4_TrackSeek( p_demux
, tk
, MP4_GetMoviePTS( p_sys
) );
1286 const mtime_t i_nztime
= MP4_GetMoviePTS( p_sys
);
1288 /* We demux/set pcr, even without selected tracks, (empty edits, ...) */
1289 if( p_sys
->i_pcr
!= VLC_TS_INVALID
/* not after a seek */ )
1292 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1294 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1295 if( !tk
->b_ok
|| tk
->b_chapters_source
|| tk
->i_sample
>= tk
->i_sample_count
)
1297 /* Test for EOF on each track (samples count, edit list) */
1298 b_eof
&= ( i_nztime
> MP4_TrackGetDTS( p_demux
, tk
) );
1301 return VLC_DEMUXER_EOS
;
1304 const unsigned i_max_preload
= ( p_sys
->b_fastseekable
) ? 0 : ( p_sys
->b_seekable
) ? DEMUX_TRACK_MAX_PRELOAD
: UINT_MAX
;
1305 const mtime_t i_scaledincrement
= DEMUX_INCREMENT
* p_sys
->i_timescale
/ CLOCK_FREQ
;
1307 /* demux up to increment amount of data on every track, or just set pcr if empty data */
1310 mp4_track_t
*tk
= NULL
;
1311 i_status
= VLC_DEMUXER_EOS
;
1313 /* First pass, find any track within our target increment, ordered by position */
1314 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1316 mp4_track_t
*tk_tmp
= &p_sys
->track
[i_track
];
1317 if( !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
1318 tk_tmp
->i_sample
>= tk_tmp
->i_sample_count
||
1319 (!tk_tmp
->b_selected
&& p_sys
->b_seekable
) )
1322 /* At least still have data to demux on this or next turns */
1323 i_status
= VLC_DEMUXER_SUCCESS
;
1325 if ( MP4_TrackGetDTS( p_demux
, tk_tmp
) <= i_nztime
+ DEMUX_INCREMENT
)
1327 if( tk
== NULL
|| MP4_TrackGetPos( tk_tmp
) < MP4_TrackGetPos( tk
) )
1334 /* Second pass, refine and find any best candidate having a chunk pos closer than
1335 * current candidate (avoids seeks when increment falls between the 2) from
1336 * current position, but within extended interleave time */
1337 for( i_track
= 0; i_max_preload
> 0 && i_track
< p_sys
->i_tracks
; i_track
++ )
1339 mp4_track_t
*tk_tmp
= &p_sys
->track
[i_track
];
1341 !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
1342 (!tk_tmp
->b_selected
&& p_sys
->b_seekable
) ||
1343 tk_tmp
->i_sample
>= tk_tmp
->i_sample_count
)
1346 mtime_t i_nzdts
= MP4_TrackGetDTS( p_demux
, tk_tmp
);
1347 if ( i_nzdts
<= i_nztime
+ DEMUX_TRACK_MAX_PRELOAD
)
1349 /* Found a better candidate to avoid seeking */
1350 if( MP4_TrackGetPos( tk_tmp
) < MP4_TrackGetPos( tk
) )
1352 /* Note: previous candidate will be repicked on next loop */
1356 uint64_t i_pos
= MP4_TrackGetPos( tk
);
1357 int i_ret
= DemuxTrack( p_demux
, tk
, i_pos
, i_max_preload
);
1359 if( i_ret
== VLC_DEMUXER_SUCCESS
)
1360 i_status
= VLC_DEMUXER_SUCCESS
;
1363 if( i_status
!= VLC_DEMUXER_SUCCESS
|| !tk
)
1367 p_sys
->i_time
+= __MAX(i_scaledincrement
, 1);
1368 if( p_sys
->i_pcr
> VLC_TS_INVALID
)
1370 p_sys
->i_pcr
= VLC_TS_0
+ MP4_rescale( p_sys
->i_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
1371 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
1375 MP4_UpdateSeekpoint( p_demux
, i_nztime
+ DEMUX_INCREMENT
);
1380 static int Demux( demux_t
*p_demux
)
1382 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1384 assert( ! p_sys
->b_fragmented
);
1386 int i_status
= DemuxMoov( p_demux
);
1388 if( i_status
== VLC_DEMUXER_EOS
)
1389 i_status
= VLC_DEMUXER_EOF
;
1394 static void MP4_UpdateSeekpoint( demux_t
*p_demux
, int64_t i_time
)
1396 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1398 if( !p_sys
->p_title
)
1400 for( i
= 0; i
< p_sys
->p_title
->i_seekpoint
; i
++ )
1402 if( i_time
< p_sys
->p_title
->seekpoint
[i
]->i_time_offset
)
1407 if( i
!= p_demux
->info
.i_seekpoint
&& i
>= 0 )
1409 p_demux
->info
.i_seekpoint
= i
;
1410 p_demux
->info
.i_update
|= INPUT_UPDATE_SEEKPOINT
;
1413 /*****************************************************************************
1414 * Seek: Go to i_date
1415 ******************************************************************************/
1416 static int Seek( demux_t
*p_demux
, mtime_t i_date
, bool b_accurate
)
1418 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1419 unsigned int i_track
;
1421 /* Now for each stream try to go to this time */
1422 mtime_t i_start
= i_date
;
1423 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1425 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1426 /* FIXME: we should find the lowest time from tracks with indexes.
1427 considering only video for now */
1428 if( tk
->fmt
.i_cat
!= VIDEO_ES
)
1430 if( MP4_TrackSeek( p_demux
, tk
, i_date
) == VLC_SUCCESS
)
1432 mtime_t i_seeked
= MP4_TrackGetDTS( p_demux
, tk
);
1433 if( i_seeked
< i_start
)
1438 msg_Dbg( p_demux
, "seeking with %"PRId64
"ms %s", (i_date
- i_start
) / 1000,
1439 !b_accurate
? "alignment" : "preroll (use input-fast-seek to avoid)" );
1441 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1443 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1444 if( tk
->fmt
.i_cat
== VIDEO_ES
)
1446 MP4_TrackSeek( p_demux
, tk
, i_start
);
1449 MP4_UpdateSeekpoint( p_demux
, i_date
);
1450 MP4ASF_ResetFrames( p_sys
);
1451 /* update global time */
1452 p_sys
->i_time
= MP4_rescale( i_start
, CLOCK_FREQ
, p_sys
->i_timescale
);
1453 p_sys
->i_pcr
= VLC_TS_INVALID
;
1456 es_out_Control( p_demux
->out
, ES_OUT_SET_NEXT_DISPLAY_TIME
, i_date
);
1461 static int FragPrepareChunk( demux_t
*p_demux
, MP4_Box_t
*p_moof
,
1462 MP4_Box_t
*p_sidx
, stime_t i_moof_time
, bool b_discontinuity
)
1464 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1466 if( FragCreateTrunIndex( p_demux
, p_moof
, p_sidx
, i_moof_time
, b_discontinuity
) == VLC_SUCCESS
)
1468 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1470 mp4_track_t
*p_track
= &p_sys
->track
[i
];
1471 if( p_track
->context
.runs
.i_count
)
1473 const mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[0];
1474 p_track
->context
.i_trun_sample_pos
= p_run
->i_offset
;
1475 p_track
->context
.i_trun_sample
= 0;
1476 p_track
->i_time
= p_run
->i_first_dts
;
1482 return VLC_EGENERIC
;
1485 static stime_t
FragGetDemuxTimeFromTracksTime( demux_sys_t
*p_sys
)
1487 stime_t i_time
= INT64_MAX
;
1488 for( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
1490 if( p_sys
->track
[i
].context
.runs
.i_count
== 0 )
1492 stime_t i_ttime
= MP4_rescale( p_sys
->track
[i
].i_time
,
1493 p_sys
->track
[i
].i_timescale
, p_sys
->i_timescale
);
1494 i_time
= __MIN( i_time
, i_ttime
);
1499 static uint32_t FragGetMoofSequenceNumber( MP4_Box_t
*p_moof
)
1501 const MP4_Box_t
*p_mfhd
= MP4_BoxGet( p_moof
, "mfhd" );
1502 if( p_mfhd
&& BOXDATA(p_mfhd
) )
1503 return BOXDATA(p_mfhd
)->i_sequence_number
;
1507 static int FragSeekLoadFragment( demux_t
*p_demux
, uint32_t i_moox
, stime_t i_moox_time
)
1509 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1512 if( i_moox
== ATOM_moov
)
1514 p_moox
= p_sys
->p_moov
;
1518 const uint8_t *p_peek
;
1519 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) != 8 )
1520 return VLC_EGENERIC
;
1522 if( ATOM_moof
!= VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) )
1523 return VLC_EGENERIC
;
1525 MP4_Box_t
*p_vroot
= MP4_BoxGetNextChunk( p_demux
->s
);
1527 return VLC_EGENERIC
;
1528 p_moox
= MP4_BoxExtract( &p_vroot
->p_first
, ATOM_moof
);
1529 MP4_BoxFree( p_vroot
);
1532 return VLC_EGENERIC
;
1535 FragResetContext( p_sys
);
1538 p_sys
->context
.p_fragment_atom
= p_moox
;
1539 p_sys
->context
.i_current_box_type
= i_moox
;
1541 if( i_moox
== ATOM_moof
)
1543 FragPrepareChunk( p_demux
, p_moox
, NULL
, i_moox_time
, true );
1544 p_sys
->context
.i_lastseqnumber
= FragGetMoofSequenceNumber( p_moox
);
1546 p_sys
->i_time
= FragGetDemuxTimeFromTracksTime( p_sys
);
1547 p_sys
->i_pcr
= VLC_TS_INVALID
;
1550 msg_Dbg( p_demux
, "seeked to %4.4s at pos %" PRIu64
, (char *) &i_moox
, p_moox
->i_pos
);
1554 static unsigned GetSeekTrackIndex( demux_sys_t
*p_sys
)
1557 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1559 if( p_sys
->track
[i
].fmt
.i_cat
== VIDEO_ES
||
1560 p_sys
->track
[i
].fmt
.i_cat
== AUDIO_ES
)
1562 if( cand
!= i
&& !p_sys
->track
[cand
].b_selected
)
1569 static void FragTrunSeekToTime( mp4_track_t
*p_track
, stime_t i_target_time
)
1571 if( !p_track
->b_ok
|| p_track
->context
.runs
.i_count
< 1 )
1575 unsigned i_sample
= 0;
1576 uint64_t i_pos
= p_track
->context
.runs
.p_array
[0].i_offset
;
1577 stime_t i_time
= p_track
->context
.runs
.p_array
[0].i_first_dts
;
1579 for( unsigned r
= 0; r
< p_track
->context
.runs
.i_count
; r
++ )
1581 const mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[r
];
1582 const MP4_Box_data_trun_t
*p_data
=
1583 p_track
->context
.runs
.p_array
[r
].p_trun
->data
.p_trun
;
1584 if( i_time
> i_target_time
)
1588 i_time
= p_run
->i_first_dts
;
1589 i_pos
= p_run
->i_offset
;
1592 uint32_t dur
= p_track
->context
.i_default_sample_duration
;
1593 uint32_t len
= p_track
->context
.i_default_sample_size
;
1594 for ( unsigned i
=0; i
<p_data
->i_sample_count
; i
++ )
1596 if( p_data
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
1597 dur
= p_data
->p_samples
[i
].i_duration
;
1599 /* check condition */
1600 if( i_time
+ dur
> i_target_time
)
1603 if( p_data
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
1604 len
= p_data
->p_samples
[i
].i_size
;
1611 p_track
->context
.i_trun_sample
= i_sample
;
1612 p_track
->context
.i_trun_sample_pos
= i_pos
;
1613 p_track
->context
.runs
.i_current
= i_run
;
1616 static int FragSeekToTime( demux_t
*p_demux
, mtime_t i_nztime
, bool b_accurate
)
1618 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1619 uint64_t i64
= UINT64_MAX
;
1620 uint32_t i_segment_type
= ATOM_moof
;
1621 stime_t i_segment_time
= INT64_MAX
;
1622 mtime_t i_sync_time
= i_nztime
;
1623 bool b_iframesync
= false;
1625 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1626 if ( !p_sys
->i_timescale
|| !i_duration
|| !p_sys
->b_seekable
)
1627 return VLC_EGENERIC
;
1629 uint64_t i_backup_pos
= vlc_stream_Tell( p_demux
->s
);
1631 if ( !p_sys
->b_fragments_probed
&& !p_sys
->b_index_probed
&& p_sys
->b_seekable
)
1633 ProbeIndex( p_demux
);
1634 p_sys
->b_index_probed
= true;
1637 const unsigned i_seek_track_index
= GetSeekTrackIndex( p_sys
);
1638 const unsigned i_seek_track_ID
= p_sys
->track
[i_seek_track_index
].i_track_ID
;
1640 if( MP4_rescale( i_nztime
, CLOCK_FREQ
, p_sys
->i_timescale
)
1641 < GetMoovTrackDuration( p_sys
, i_seek_track_ID
) )
1643 i64
= p_sys
->p_moov
->i_pos
;
1644 i_segment_type
= ATOM_moov
;
1646 else if( FragGetMoofBySidxIndex( p_demux
, i_nztime
, &i64
, &i_sync_time
) == VLC_SUCCESS
)
1648 /* provides base offset */
1649 i_segment_time
= i_sync_time
;
1650 msg_Dbg( p_demux
, "seeking to sidx moof pos %" PRId64
" %" PRId64
, i64
, i_sync_time
);
1654 bool b_buildindex
= false;
1656 if( FragGetMoofByTfraIndex( p_demux
, i_nztime
, i_seek_track_ID
, &i64
, &i_sync_time
) == VLC_SUCCESS
)
1658 /* Does only provide segment position and a sync sample time */
1659 msg_Dbg( p_demux
, "seeking to sync point %" PRId64
, i_sync_time
);
1660 b_iframesync
= true;
1662 else if( !p_sys
->b_fragments_probed
&& !p_sys
->b_fastseekable
)
1664 const char *psz_msg
= _(
1665 "Because this file index is broken or missing, "
1666 "seeking will not work correctly.\n"
1667 "VLC won't repair your file but can temporary fix this "
1668 "problem by building an index in memory.\n"
1669 "This step might take a long time on a large file.\n"
1670 "What do you want to do?");
1671 b_buildindex
= vlc_dialog_wait_question( p_demux
,
1672 VLC_DIALOG_QUESTION_NORMAL
,
1676 _("Broken or missing Index"),
1680 if( !p_sys
->b_fragments_probed
&& ( p_sys
->b_fastseekable
|| b_buildindex
) )
1683 int i_ret
= vlc_stream_Seek( p_demux
->s
, p_sys
->p_moov
->i_pos
+ p_sys
->p_moov
->i_size
);
1684 if( i_ret
== VLC_SUCCESS
)
1686 i_ret
= ProbeFragments( p_demux
, true, &foo
);
1687 p_sys
->b_fragments_probed
= true;
1689 if( i_ret
!= VLC_SUCCESS
)
1691 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1696 if( p_sys
->b_fragments_probed
&& p_sys
->p_fragsindex
)
1698 stime_t i_basetime
= MP4_rescale( i_sync_time
, CLOCK_FREQ
, p_sys
->i_timescale
);
1699 if( !MP4_Fragments_Index_Lookup( p_sys
->p_fragsindex
, &i_basetime
, &i64
, i_seek_track_index
) )
1701 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1702 return VLC_EGENERIC
;
1704 msg_Dbg( p_demux
, "seeking to fragment index pos %" PRId64
" %" PRId64
, i64
,
1705 MP4_rescale( i_basetime
, p_sys
->i_timescale
, CLOCK_FREQ
) );
1709 if( i64
== UINT64_MAX
)
1711 msg_Warn( p_demux
, "seek by index failed" );
1712 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1713 return VLC_EGENERIC
;
1716 msg_Dbg( p_demux
, "final seek to fragment at %"PRId64
, i64
);
1717 if( vlc_stream_Seek( p_demux
->s
, i64
) )
1719 msg_Err( p_demux
, "seek failed to %"PRId64
, i64
);
1720 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1721 return VLC_EGENERIC
;
1724 /* Context is killed on success */
1725 if( FragSeekLoadFragment( p_demux
, i_segment_type
, i_segment_time
) != VLC_SUCCESS
)
1727 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1728 return VLC_EGENERIC
;
1731 p_sys
->i_pcr
= VLC_TS_INVALID
;
1733 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1735 if( i_segment_type
== ATOM_moov
)
1737 MP4_TrackSeek( p_demux
, &p_sys
->track
[i
], i_sync_time
);
1738 p_sys
->i_time
= MP4_rescale( i_sync_time
, CLOCK_FREQ
, p_sys
->i_timescale
);
1739 p_sys
->i_pcr
= VLC_TS_INVALID
;
1741 else if( b_iframesync
)
1743 stime_t i_tst
= MP4_rescale( i_sync_time
, CLOCK_FREQ
, p_sys
->track
[i
].i_timescale
);
1744 FragTrunSeekToTime( &p_sys
->track
[i
], i_tst
);
1745 p_sys
->track
[i
].i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
1749 MP4ASF_ResetFrames( p_sys
);
1750 /* And set next display time in that trun/fragment */
1751 if( b_iframesync
&& b_accurate
)
1752 es_out_Control( p_demux
->out
, ES_OUT_SET_NEXT_DISPLAY_TIME
, VLC_TS_0
+ i_nztime
);
1756 static int FragSeekToPos( demux_t
*p_demux
, double f
, bool b_accurate
)
1758 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1759 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1761 if ( !p_sys
->b_seekable
|| !p_sys
->i_timescale
|| !i_duration
)
1762 return VLC_EGENERIC
;
1764 return FragSeekToTime( p_demux
, (mtime_t
)( f
*
1765 MP4_rescale( i_duration
, p_sys
->i_timescale
, CLOCK_FREQ
) ), b_accurate
);
1768 static bool imageTypeCompatible( const MP4_Box_data_data_t
*p_data
)
1771 p_data
->e_wellknowntype
== DATA_WKT_PNG
||
1772 p_data
->e_wellknowntype
== DATA_WKT_JPEG
||
1773 p_data
->e_wellknowntype
== DATA_WKT_BMP
);
1776 static int MP4_LoadMeta( demux_sys_t
*p_sys
, vlc_meta_t
*p_meta
)
1778 MP4_Box_t
*p_data
= NULL
;
1779 MP4_Box_t
*p_udta
= NULL
;
1780 bool b_attachment_set
= false;
1783 return VLC_EGENERIC
;
1785 for( int i_index
= 0; psz_meta_roots
[i_index
] && !p_udta
; i_index
++ )
1787 p_udta
= MP4_BoxGet( p_sys
->p_root
, psz_meta_roots
[i_index
] );
1790 p_data
= MP4_BoxGet( p_udta
, "covr/data" );
1791 if ( p_data
&& imageTypeCompatible( BOXDATA(p_data
) ) )
1793 char *psz_attachment
;
1794 if ( -1 != asprintf( &psz_attachment
, "attachment://%s/covr/data[0]",
1795 psz_meta_roots
[i_index
] ) )
1797 vlc_meta_SetArtURL( p_meta
, psz_attachment
);
1798 b_attachment_set
= true;
1799 free( psz_attachment
);
1805 const MP4_Box_t
*p_pnot
;
1806 if ( !b_attachment_set
&& (p_pnot
= MP4_BoxGet( p_sys
->p_root
, "pnot" )) )
1808 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
) && !b_attachment_set
; i
++ )
1810 if ( rgi_pict_atoms
[i
] == BOXDATA(p_pnot
)->i_type
)
1813 snprintf( rgsz_path
, 26, "attachment://%4.4s[%"PRIu16
"]",
1814 (char*)&rgi_pict_atoms
[i
], BOXDATA(p_pnot
)->i_index
- 1 );
1815 vlc_meta_SetArtURL( p_meta
, rgsz_path
);
1816 b_attachment_set
= true;
1821 if( p_udta
== NULL
)
1823 if( !b_attachment_set
)
1824 return VLC_EGENERIC
;
1826 else SetupMeta( p_meta
, p_udta
);
1831 /*****************************************************************************
1833 *****************************************************************************/
1834 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
1836 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1842 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1846 case DEMUX_CAN_SEEK
:
1847 *va_arg( args
, bool * ) = p_sys
->b_seekable
;
1850 case DEMUX_GET_POSITION
:
1851 pf
= va_arg( args
, double * );
1852 if( i_duration
> 0 )
1854 *pf
= (double)p_sys
->i_time
/ (double)i_duration
;
1862 case DEMUX_SET_POSITION
:
1863 f
= va_arg( args
, double );
1864 b
= va_arg( args
, int );
1865 if ( p_demux
->pf_demux
== DemuxFrag
)
1866 return FragSeekToPos( p_demux
, f
, b
);
1867 else if( p_sys
->i_timescale
> 0 )
1869 i64
= (int64_t)( f
* MP4_rescale( p_sys
->i_duration
,
1870 p_sys
->i_timescale
, CLOCK_FREQ
) );
1871 return Seek( p_demux
, i64
, b
);
1873 else return VLC_EGENERIC
;
1875 case DEMUX_GET_TIME
:
1876 pi64
= va_arg( args
, int64_t * );
1877 if( p_sys
->i_timescale
> 0 )
1879 *pi64
= MP4_rescale( p_sys
->i_time
,
1880 p_sys
->i_timescale
, CLOCK_FREQ
);
1885 case DEMUX_SET_TIME
:
1886 i64
= va_arg( args
, int64_t );
1887 b
= va_arg( args
, int );
1888 if ( p_demux
->pf_demux
== DemuxFrag
)
1889 return FragSeekToTime( p_demux
, i64
, b
);
1891 return Seek( p_demux
, i64
, b
);
1893 case DEMUX_GET_LENGTH
:
1894 pi64
= va_arg( args
, int64_t * );
1895 if( p_sys
->i_timescale
> 0 )
1897 *pi64
= MP4_rescale( i_duration
,
1898 p_sys
->i_timescale
, CLOCK_FREQ
);
1904 pf
= va_arg( args
, double * );
1908 case DEMUX_GET_ATTACHMENTS
:
1910 input_attachment_t
***ppp_attach
= va_arg( args
, input_attachment_t
*** );
1911 int *pi_int
= va_arg( args
, int * );
1913 MP4_Box_t
*p_udta
= NULL
;
1917 /* Count number of total attachments */
1918 for( ; psz_meta_roots
[i_index
] && !p_udta
; i_index
++ )
1920 p_udta
= MP4_BoxGet( p_sys
->p_root
, psz_meta_roots
[i_index
] );
1922 i_count
+= MP4_BoxCount( p_udta
, "covr/data" );
1925 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
); i
++ )
1928 snprintf( rgsz_path
, 5, "%4.4s", (char*)&rgi_pict_atoms
[i
] );
1929 i_count
+= MP4_BoxCount( p_sys
->p_root
, rgsz_path
);
1933 return VLC_EGENERIC
;
1935 *ppp_attach
= (input_attachment_t
**)
1936 vlc_alloc( i_count
, sizeof(input_attachment_t
*) );
1937 if( !(*ppp_attach
) ) return VLC_ENOMEM
;
1939 /* First add cover attachments */
1941 size_t i_box_count
= 0;
1944 const MP4_Box_t
*p_data
= MP4_BoxGet( p_udta
, "covr/data" );
1945 for( ; p_data
; p_data
= p_data
->p_next
)
1951 if ( p_data
->i_type
!= ATOM_data
|| !imageTypeCompatible( BOXDATA(p_data
) ) )
1954 switch( BOXDATA(p_data
)->e_wellknowntype
)
1957 psz_mime
= strdup( "image/png" );
1960 psz_mime
= strdup( "image/jpeg" );
1963 psz_mime
= strdup( "image/bmp" );
1969 if ( asprintf( &psz_filename
, "%s/covr/data[%"PRIu64
"]", psz_meta_roots
[i_index
- 1],
1970 (uint64_t) i_box_count
- 1 ) >= 0 )
1972 (*ppp_attach
)[i_count
++] =
1973 vlc_input_attachment_New( psz_filename
, psz_mime
, "Cover picture",
1974 BOXDATA(p_data
)->p_blob
, BOXDATA(p_data
)->i_blob
);
1975 msg_Dbg( p_demux
, "adding attachment %s", psz_filename
);
1976 free( psz_filename
);
1983 /* Then quickdraw pict ones */
1984 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
); i
++ )
1987 snprintf( rgsz_path
, 5, "%4.4s", (char*)&rgi_pict_atoms
[i
] );
1988 const MP4_Box_t
*p_pict
= MP4_BoxGet( p_sys
->p_root
, rgsz_path
);
1990 for( ; p_pict
; p_pict
= p_pict
->p_next
)
1992 if ( i_box_count
++ == UINT16_MAX
) /* pnot only handles 2^16 */
1994 if ( p_pict
->i_type
!= rgi_pict_atoms
[i
] )
1996 char rgsz_location
[12];
1997 snprintf( rgsz_location
, 12, "%4.4s[%"PRIu16
"]", (char*)&rgi_pict_atoms
[i
],
1998 (uint16_t) i_box_count
- 1 );
1999 (*ppp_attach
)[i_count
] = vlc_input_attachment_New( rgsz_location
, "image/x-pict",
2000 "Quickdraw image", p_pict
->data
.p_binary
->p_blob
, p_pict
->data
.p_binary
->i_blob
);
2001 if ( !(*ppp_attach
)[i_count
] )
2007 msg_Dbg( p_demux
, "adding attachment %s", rgsz_location
);
2013 free( *ppp_attach
);
2014 return VLC_EGENERIC
;
2022 case DEMUX_GET_META
:
2024 vlc_meta_t
*p_meta
= va_arg( args
, vlc_meta_t
*);
2026 if( !p_sys
->p_meta
)
2027 return VLC_EGENERIC
;
2029 vlc_meta_Merge( p_meta
, p_sys
->p_meta
);
2034 case DEMUX_GET_TITLE_INFO
:
2036 input_title_t
***ppp_title
= va_arg( args
, input_title_t
*** );
2037 int *pi_int
= va_arg( args
, int* );
2038 int *pi_title_offset
= va_arg( args
, int* );
2039 int *pi_seekpoint_offset
= va_arg( args
, int* );
2041 if( !p_sys
->p_title
)
2042 return VLC_EGENERIC
;
2045 *ppp_title
= malloc( sizeof( input_title_t
*) );
2046 (*ppp_title
)[0] = vlc_input_title_Duplicate( p_sys
->p_title
);
2047 *pi_title_offset
= 0;
2048 *pi_seekpoint_offset
= 0;
2051 case DEMUX_SET_TITLE
:
2053 const int i_title
= va_arg( args
, int );
2054 if( !p_sys
->p_title
|| i_title
!= 0 )
2055 return VLC_EGENERIC
;
2058 case DEMUX_SET_SEEKPOINT
:
2060 const int i_seekpoint
= va_arg( args
, int );
2061 if( !p_sys
->p_title
)
2062 return VLC_EGENERIC
;
2063 return Seek( p_demux
, p_sys
->p_title
->seekpoint
[i_seekpoint
]->i_time_offset
, true );
2065 case DEMUX_GET_PTS_DELAY
:
2067 for( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
2069 const MP4_Box_t
*p_load
;
2070 if ( (p_load
= MP4_BoxGet( p_sys
->track
[i
].p_track
, "load" )) &&
2071 BOXDATA(p_load
)->i_duration
> 0 )
2073 *va_arg(args
, int64_t *) =
2074 MP4_rescale( BOXDATA(p_load
)->i_duration
,
2075 p_sys
->track
[i
].i_timescale
, CLOCK_FREQ
);
2079 return VLC_EGENERIC
;
2081 case DEMUX_SET_NEXT_DEMUX_TIME
:
2082 case DEMUX_SET_GROUP
:
2083 case DEMUX_HAS_UNSUPPORTED_META
:
2084 case DEMUX_CAN_RECORD
:
2086 return VLC_EGENERIC
;
2090 /*****************************************************************************
2091 * Close: frees unused data
2092 *****************************************************************************/
2093 static void Close ( vlc_object_t
* p_this
)
2095 demux_t
* p_demux
= (demux_t
*)p_this
;
2096 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2097 unsigned int i_track
;
2099 msg_Dbg( p_demux
, "freeing all memory" );
2101 FragResetContext( p_sys
);
2103 MP4_BoxFree( p_sys
->p_root
);
2105 if( p_sys
->p_title
)
2106 vlc_input_title_Delete( p_sys
->p_title
);
2109 vlc_meta_Delete( p_sys
->p_meta
);
2111 MP4_Fragments_Index_Delete( p_sys
->p_fragsindex
);
2113 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
2114 MP4_TrackClean( p_demux
->out
, &p_sys
->track
[i_track
] );
2115 free( p_sys
->track
);
2122 /****************************************************************************
2123 * Local functions, specific to vlc
2124 ****************************************************************************/
2126 static void LoadChapterGpac( demux_t
*p_demux
, MP4_Box_t
*p_chpl
)
2128 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2130 if( BOXDATA(p_chpl
)->i_chapter
== 0 )
2133 p_sys
->p_title
= vlc_input_title_New();
2134 for( int i
= 0; i
< BOXDATA(p_chpl
)->i_chapter
&& p_sys
->p_title
; i
++ )
2136 seekpoint_t
*s
= vlc_seekpoint_New();
2137 if( s
== NULL
) continue;
2139 s
->psz_name
= strdup( BOXDATA(p_chpl
)->chapter
[i
].psz_name
);
2140 if( s
->psz_name
== NULL
)
2142 vlc_seekpoint_Delete( s
);;
2146 EnsureUTF8( s
->psz_name
);
2147 s
->i_time_offset
= BOXDATA(p_chpl
)->chapter
[i
].i_start
/ 10;
2148 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2151 static void LoadChapterGoPro( demux_t
*p_demux
, MP4_Box_t
*p_hmmt
)
2153 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2155 p_sys
->p_title
= vlc_input_title_New();
2156 if( p_sys
->p_title
)
2157 for( unsigned i
= 0; i
< BOXDATA(p_hmmt
)->i_chapter_count
; i
++ )
2159 seekpoint_t
*s
= vlc_seekpoint_New();
2162 if( asprintf( &s
->psz_name
, "HiLight tag #%u", i
+1 ) != -1 )
2163 EnsureUTF8( s
->psz_name
);
2165 /* HiLights are stored in ms so we convert them to µs */
2166 s
->i_time_offset
= BOXDATA(p_hmmt
)->pi_chapter_start
[i
] * 1000;
2167 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2171 static void LoadChapterApple( demux_t
*p_demux
, mp4_track_t
*tk
)
2173 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2175 for( tk
->i_sample
= 0; tk
->i_sample
< tk
->i_sample_count
; tk
->i_sample
++ )
2177 const int64_t i_dts
= MP4_TrackGetDTS( p_demux
, tk
);
2178 int64_t i_pts_delta
;
2179 if ( !MP4_TrackGetPTSDelta( p_demux
, tk
, &i_pts_delta
) )
2181 uint32_t i_nb_samples
= 0;
2182 const uint32_t i_size
= MP4_TrackGetReadSize( tk
, &i_nb_samples
);
2184 if( i_size
> 0 && !vlc_stream_Seek( p_demux
->s
, MP4_TrackGetPos( tk
) ) )
2187 const uint32_t i_read
= stream_ReadU32( p_demux
->s
, p_buffer
,
2188 __MIN( sizeof(p_buffer
), i_size
) );
2191 const uint32_t i_string
= __MIN( GetWBE(p_buffer
), i_read
-2 );
2192 const char *psnz_string
= &p_buffer
[2];
2194 seekpoint_t
*s
= vlc_seekpoint_New();
2195 if( s
== NULL
) continue;
2197 if( i_string
> 1 && !memcmp( psnz_string
, "\xFF\xFE", 2 ) )
2198 s
->psz_name
= FromCharset( "UTF-16LE", psnz_string
, i_string
);
2200 s
->psz_name
= strndup( psnz_string
, i_string
);
2202 if( s
->psz_name
== NULL
)
2204 vlc_seekpoint_Delete( s
);
2208 EnsureUTF8( s
->psz_name
);
2209 s
->i_time_offset
= i_dts
+ __MAX( i_pts_delta
, 0 );
2211 if( !p_sys
->p_title
)
2212 p_sys
->p_title
= vlc_input_title_New();
2213 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2216 if( tk
->i_sample
+1 >= tk
->chunk
[tk
->i_chunk
].i_sample_first
+
2217 tk
->chunk
[tk
->i_chunk
].i_sample_count
)
2221 static void LoadChapter( demux_t
*p_demux
)
2223 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2227 if( ( p_chpl
= MP4_BoxGet( p_sys
->p_root
, "/moov/udta/chpl" ) ) &&
2228 BOXDATA(p_chpl
) && BOXDATA(p_chpl
)->i_chapter
> 0 )
2230 LoadChapterGpac( p_demux
, p_chpl
);
2232 else if( ( p_hmmt
= MP4_BoxGet( p_sys
->p_root
, "/moov/udta/HMMT" ) ) &&
2233 BOXDATA(p_hmmt
) && BOXDATA(p_hmmt
)->pi_chapter_start
&& BOXDATA(p_hmmt
)->i_chapter_count
> 0 )
2235 LoadChapterGoPro( p_demux
, p_hmmt
);
2237 else if( p_sys
->p_tref_chap
)
2239 MP4_Box_data_tref_generic_t
*p_chap
= p_sys
->p_tref_chap
->data
.p_tref_generic
;
2242 /* Load the first subtitle track like quicktime */
2243 for( i
= 0; i
< p_chap
->i_entry_count
; i
++ )
2245 for( j
= 0; j
< p_sys
->i_tracks
; j
++ )
2247 mp4_track_t
*tk
= &p_sys
->track
[j
];
2248 if( tk
->b_ok
&& tk
->i_track_ID
== p_chap
->i_track_ID
[i
] &&
2249 tk
->fmt
.i_cat
== SPU_ES
&& tk
->fmt
.i_codec
== VLC_CODEC_TX3G
)
2252 if( j
< p_sys
->i_tracks
)
2254 LoadChapterApple( p_demux
, &p_sys
->track
[j
] );
2260 /* Add duration if titles are enabled */
2261 if( p_sys
->p_title
)
2263 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
2264 p_sys
->p_title
->i_length
=
2265 MP4_rescale( i_duration
,
2266 p_sys
->i_timescale
, CLOCK_FREQ
);
2270 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
2271 static int TrackCreateChunksIndex( demux_t
*p_demux
,
2272 mp4_track_t
*p_demux_track
)
2274 MP4_Box_t
*p_co64
; /* give offset for each chunk, same for stco and co64 */
2277 unsigned int i_chunk
;
2278 unsigned int i_index
, i_last
;
2280 if( ( !(p_co64
= MP4_BoxGet( p_demux_track
->p_stbl
, "stco" ) )&&
2281 !(p_co64
= MP4_BoxGet( p_demux_track
->p_stbl
, "co64" ) ) )||
2282 ( !(p_stsc
= MP4_BoxGet( p_demux_track
->p_stbl
, "stsc" ) ) ))
2284 return( VLC_EGENERIC
);
2287 p_demux_track
->i_chunk_count
= BOXDATA(p_co64
)->i_entry_count
;
2288 if( !p_demux_track
->i_chunk_count
)
2290 msg_Warn( p_demux
, "no chunk defined" );
2292 p_demux_track
->chunk
= calloc( p_demux_track
->i_chunk_count
,
2293 sizeof( mp4_chunk_t
) );
2294 if( p_demux_track
->chunk
== NULL
)
2299 /* first we read chunk offset */
2300 for( i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2302 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2304 ck
->i_offset
= BOXDATA(p_co64
)->i_chunk_offset
[i_chunk
];
2306 ck
->i_first_dts
= 0;
2307 ck
->i_entries_dts
= 0;
2308 ck
->p_sample_count_dts
= NULL
;
2309 ck
->p_sample_delta_dts
= NULL
;
2310 ck
->i_entries_pts
= 0;
2311 ck
->p_sample_count_pts
= NULL
;
2312 ck
->p_sample_offset_pts
= NULL
;
2315 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
2316 to be used for the sample XXX begin to 1
2317 We construct it begining at the end */
2318 i_last
= p_demux_track
->i_chunk_count
; /* last chunk proceded */
2319 i_index
= BOXDATA(p_stsc
)->i_entry_count
;
2321 while( i_index
-- > 0 )
2323 for( i_chunk
= BOXDATA(p_stsc
)->i_first_chunk
[i_index
] - 1;
2324 i_chunk
< i_last
; i_chunk
++ )
2326 if( i_chunk
>= p_demux_track
->i_chunk_count
)
2328 msg_Warn( p_demux
, "corrupted chunk table" );
2329 return VLC_EGENERIC
;
2332 p_demux_track
->chunk
[i_chunk
].i_sample_description_index
=
2333 BOXDATA(p_stsc
)->i_sample_description_index
[i_index
];
2334 p_demux_track
->chunk
[i_chunk
].i_sample_count
=
2335 BOXDATA(p_stsc
)->i_samples_per_chunk
[i_index
];
2337 i_last
= BOXDATA(p_stsc
)->i_first_chunk
[i_index
] - 1;
2340 p_demux_track
->i_sample_count
= 0;
2341 bool b_broken
= false;
2342 if ( p_demux_track
->i_chunk_count
)
2344 p_demux_track
->chunk
[0].i_sample_first
= 0;
2345 p_demux_track
->i_sample_count
+= p_demux_track
->chunk
[0].i_sample_count
;
2347 const mp4_chunk_t
*prev
= &p_demux_track
->chunk
[0];
2348 for( i_chunk
= 1; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2350 mp4_chunk_t
*cur
= &p_demux_track
->chunk
[i_chunk
];
2351 if( unlikely(UINT32_MAX
- cur
->i_sample_count
< p_demux_track
->i_sample_count
) )
2356 p_demux_track
->i_sample_count
+= cur
->i_sample_count
;
2357 cur
->i_sample_first
= prev
->i_sample_first
+ prev
->i_sample_count
;
2362 if( unlikely(b_broken
) )
2364 msg_Err( p_demux
, "Overflow in chunks total samples count" );
2365 return VLC_EGENERIC
;
2368 msg_Dbg( p_demux
, "track[Id 0x%x] read %d chunk",
2369 p_demux_track
->i_track_ID
, p_demux_track
->i_chunk_count
);
2374 static int xTTS_CountEntries( demux_t
*p_demux
, uint32_t *pi_entry
/* out */,
2375 const uint32_t i_index
,
2376 uint32_t i_index_samples_left
,
2377 uint32_t i_sample_count
,
2378 const uint32_t *pi_index_sample_count
,
2379 const uint32_t i_table_count
)
2381 uint32_t i_array_offset
;
2382 while( i_sample_count
> 0 )
2384 if ( likely((UINT32_MAX
- i_index
) >= *pi_entry
) )
2385 i_array_offset
= i_index
+ *pi_entry
;
2387 return VLC_EGENERIC
;
2389 if ( i_array_offset
>= i_table_count
)
2391 msg_Err( p_demux
, "invalid index counting total samples %u %u", i_array_offset
, i_table_count
);
2395 if ( i_index_samples_left
)
2397 if ( i_index_samples_left
> i_sample_count
)
2399 i_index_samples_left
-= i_sample_count
;
2401 *pi_entry
+=1; /* No samples left, go copy */
2406 i_sample_count
-= i_index_samples_left
;
2407 i_index_samples_left
= 0;
2414 i_sample_count
-= __MIN( i_sample_count
, pi_index_sample_count
[i_array_offset
] );
2422 static int TrackCreateSamplesIndex( demux_t
*p_demux
,
2423 mp4_track_t
*p_demux_track
)
2426 MP4_Box_data_stsz_t
*stsz
;
2427 /* TODO use also stss and stsh table for seeking */
2428 /* FIXME use edit table */
2431 * Gives the sample size for each samples. There is also a stz2 table
2432 * (compressed form) that we need to implement TODO */
2433 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "stsz" );
2436 /* FIXME and stz2 */
2437 msg_Warn( p_demux
, "cannot find STSZ box" );
2438 return VLC_EGENERIC
;
2440 stsz
= p_box
->data
.p_stsz
;
2442 /* Use stsz table to create a sample number -> sample size table */
2443 if( p_demux_track
->i_sample_count
!= stsz
->i_sample_count
)
2445 msg_Warn( p_demux
, "Incorrect total samples stsc %" PRIu32
" <> stsz %"PRIu32
", "
2446 " expect truncated media playback",
2447 p_demux_track
->i_sample_count
, stsz
->i_sample_count
);
2448 p_demux_track
->i_sample_count
= __MIN(p_demux_track
->i_sample_count
, stsz
->i_sample_count
);
2451 if( stsz
->i_sample_size
)
2453 /* 1: all sample have the same size, so no need to construct a table */
2454 p_demux_track
->i_sample_size
= stsz
->i_sample_size
;
2455 p_demux_track
->p_sample_size
= NULL
;
2459 /* 2: each sample can have a different size */
2460 p_demux_track
->i_sample_size
= 0;
2461 p_demux_track
->p_sample_size
=
2462 calloc( p_demux_track
->i_sample_count
, sizeof( uint32_t ) );
2463 if( p_demux_track
->p_sample_size
== NULL
)
2466 for( uint32_t i_sample
= 0; i_sample
< p_demux_track
->i_sample_count
; i_sample
++ )
2468 p_demux_track
->p_sample_size
[i_sample
] =
2469 stsz
->i_entry_size
[i_sample
];
2473 if ( p_demux_track
->i_chunk_count
&& p_demux_track
->i_sample_size
== 0 )
2475 const mp4_chunk_t
*lastchunk
= &p_demux_track
->chunk
[p_demux_track
->i_chunk_count
- 1];
2476 if( (uint64_t)lastchunk
->i_sample_count
+ p_demux_track
->i_chunk_count
- 1 > stsz
->i_sample_count
)
2478 msg_Err( p_demux
, "invalid samples table: stsz table is too small" );
2479 return VLC_EGENERIC
;
2483 /* Use stts table to create a sample number -> dts table.
2484 * XXX: if we don't want to waste too much memory, we can't expand
2485 * the box! so each chunk will contain an "extract" of this table
2486 * for fast research (problem with raw stream where a sample is sometime
2487 * just channels*bits_per_sample/8 */
2489 /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
2490 * differing in 2/2 fields and 1 signedness */
2492 mtime_t i_next_dts
= 0;
2494 * Gives mapping between sample and decoding time
2496 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "stts" );
2499 msg_Warn( p_demux
, "cannot find STTS box" );
2500 return VLC_EGENERIC
;
2504 MP4_Box_data_stts_t
*stts
= p_box
->data
.p_stts
;
2506 msg_Warn( p_demux
, "STTS table of %"PRIu32
" entries", stts
->i_entry_count
);
2508 /* Create sample -> dts table per chunk */
2509 uint32_t i_index
= 0;
2510 uint32_t i_current_index_samples_left
= 0;
2512 for( uint32_t i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2514 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2515 uint32_t i_sample_count
;
2517 /* save first dts */
2518 ck
->i_first_dts
= i_next_dts
;
2520 /* count how many entries are needed for this chunk
2521 * for p_sample_delta_dts and p_sample_count_dts */
2522 ck
->i_entries_dts
= 0;
2524 int i_ret
= xTTS_CountEntries( p_demux
, &ck
->i_entries_dts
, i_index
,
2525 i_current_index_samples_left
,
2527 stts
->pi_sample_count
,
2528 stts
->i_entry_count
);
2529 if ( i_ret
== VLC_EGENERIC
)
2533 ck
->p_sample_count_dts
= calloc( ck
->i_entries_dts
, sizeof( uint32_t ) );
2534 ck
->p_sample_delta_dts
= calloc( ck
->i_entries_dts
, sizeof( uint32_t ) );
2535 if( !ck
->p_sample_count_dts
|| !ck
->p_sample_delta_dts
)
2537 free( ck
->p_sample_count_dts
);
2538 free( ck
->p_sample_delta_dts
);
2539 msg_Err( p_demux
, "can't allocate memory for i_entry=%"PRIu32
, ck
->i_entries_dts
);
2540 ck
->i_entries_dts
= 0;
2545 i_sample_count
= ck
->i_sample_count
;
2547 for( uint32_t i
= 0; i
< ck
->i_entries_dts
; i
++ )
2549 if ( i_current_index_samples_left
)
2551 if ( i_current_index_samples_left
> i_sample_count
)
2553 ck
->p_sample_count_dts
[i
] = i_sample_count
;
2554 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2555 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2556 if ( i_sample_count
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2557 i_current_index_samples_left
-= i_sample_count
;
2559 assert( i
== ck
->i_entries_dts
- 1 );
2564 ck
->p_sample_count_dts
[i
] = i_current_index_samples_left
;
2565 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2566 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2567 if ( i_current_index_samples_left
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2568 i_sample_count
-= i_current_index_samples_left
;
2569 i_current_index_samples_left
= 0;
2575 if ( stts
->pi_sample_count
[i_index
] > i_sample_count
)
2577 ck
->p_sample_count_dts
[i
] = i_sample_count
;
2578 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2579 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2580 if ( i_sample_count
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2581 i_current_index_samples_left
= stts
->pi_sample_count
[i_index
] - i_sample_count
;
2583 assert( i
== ck
->i_entries_dts
- 1 );
2584 // keep building from same index
2588 ck
->p_sample_count_dts
[i
] = stts
->pi_sample_count
[i_index
];
2589 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2590 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2591 if ( stts
->pi_sample_count
[i_index
] ) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2592 i_sample_count
-= stts
->pi_sample_count
[i_index
];
2603 * Gives the delta between decoding time (dts) and composition table (pts)
2605 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "ctts" );
2606 if( p_box
&& p_box
->data
.p_ctts
)
2608 MP4_Box_data_ctts_t
*ctts
= p_box
->data
.p_ctts
;
2610 msg_Warn( p_demux
, "CTTS table of %"PRIu32
" entries", ctts
->i_entry_count
);
2612 int64_t i_cts_shift
= 0;
2613 const MP4_Box_t
*p_cslg
= MP4_BoxGet( p_demux_track
->p_stbl
, "cslg" );
2614 if( p_cslg
&& BOXDATA(p_cslg
) )
2615 i_cts_shift
= BOXDATA(p_cslg
)->ct_to_dts_shift
;
2617 /* Create pts-dts table per chunk */
2618 uint32_t i_index
= 0;
2619 uint32_t i_current_index_samples_left
= 0;
2621 for( uint32_t i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2623 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2624 uint32_t i_sample_count
;
2626 /* count how many entries are needed for this chunk
2627 * for p_sample_offset_pts and p_sample_count_pts */
2628 ck
->i_entries_pts
= 0;
2629 int i_ret
= xTTS_CountEntries( p_demux
, &ck
->i_entries_pts
, i_index
,
2630 i_current_index_samples_left
,
2632 ctts
->pi_sample_count
,
2633 ctts
->i_entry_count
);
2634 if ( i_ret
== VLC_EGENERIC
)
2638 ck
->p_sample_count_pts
= calloc( ck
->i_entries_pts
, sizeof( uint32_t ) );
2639 ck
->p_sample_offset_pts
= calloc( ck
->i_entries_pts
, sizeof( int32_t ) );
2640 if( !ck
->p_sample_count_pts
|| !ck
->p_sample_offset_pts
)
2642 free( ck
->p_sample_count_pts
);
2643 free( ck
->p_sample_offset_pts
);
2644 msg_Err( p_demux
, "can't allocate memory for i_entry=%"PRIu32
, ck
->i_entries_pts
);
2645 ck
->i_entries_pts
= 0;
2650 i_sample_count
= ck
->i_sample_count
;
2652 for( uint32_t i
= 0; i
< ck
->i_entries_pts
; i
++ )
2654 if ( i_current_index_samples_left
)
2656 if ( i_current_index_samples_left
> i_sample_count
)
2658 ck
->p_sample_count_pts
[i
] = i_sample_count
;
2659 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2660 i_current_index_samples_left
-= i_sample_count
;
2662 assert( i
== ck
->i_entries_pts
- 1 );
2667 ck
->p_sample_count_pts
[i
] = i_current_index_samples_left
;
2668 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2669 i_sample_count
-= i_current_index_samples_left
;
2670 i_current_index_samples_left
= 0;
2676 if ( ctts
->pi_sample_count
[i_index
] > i_sample_count
)
2678 ck
->p_sample_count_pts
[i
] = i_sample_count
;
2679 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2680 i_current_index_samples_left
= ctts
->pi_sample_count
[i_index
] - i_sample_count
;
2682 assert( i
== ck
->i_entries_pts
- 1 );
2683 // keep building from same index
2687 ck
->p_sample_count_pts
[i
] = ctts
->pi_sample_count
[i_index
];
2688 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2689 i_sample_count
-= ctts
->pi_sample_count
[i_index
];
2699 msg_Dbg( p_demux
, "track[Id 0x%x] read %"PRIu32
" samples length:%"PRId64
"s",
2700 p_demux_track
->i_track_ID
, p_demux_track
->i_sample_count
,
2701 i_next_dts
/ p_demux_track
->i_timescale
);
2708 * It computes the sample rate for a video track using the given sample
2711 static void TrackGetESSampleRate( demux_t
*p_demux
,
2712 unsigned *pi_num
, unsigned *pi_den
,
2713 const mp4_track_t
*p_track
,
2714 unsigned i_sd_index
,
2720 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( MP4_BoxGet( p_demux
->p_sys
->p_root
, "/moov" ),
2721 p_track
->i_track_ID
);
2722 MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_trak
, "mdia/mdhd" );
2723 if ( p_mdhd
&& BOXDATA(p_mdhd
) )
2725 vlc_ureduce( pi_num
, pi_den
,
2726 (uint64_t) BOXDATA(p_mdhd
)->i_timescale
* p_track
->i_sample_count
,
2727 (uint64_t) BOXDATA(p_mdhd
)->i_duration
,
2732 if( p_track
->i_chunk_count
== 0 )
2736 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[i_chunk
];
2737 while( p_chunk
> &p_track
->chunk
[0] &&
2738 p_chunk
[-1].i_sample_description_index
== i_sd_index
)
2743 uint64_t i_sample
= 0;
2744 uint64_t i_total_duration
= 0;
2747 i_sample
+= p_chunk
->i_sample_count
;
2748 i_total_duration
+= p_chunk
->i_duration
;
2751 while( p_chunk
< &p_track
->chunk
[p_track
->i_chunk_count
] &&
2752 p_chunk
->i_sample_description_index
== i_sd_index
);
2754 if( i_sample
> 0 && i_total_duration
)
2755 vlc_ureduce( pi_num
, pi_den
,
2756 i_sample
* p_track
->i_timescale
,
2763 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
2765 static int TrackCreateES( demux_t
*p_demux
, mp4_track_t
*p_track
,
2766 unsigned int i_chunk
, es_out_id_t
**pp_es
)
2768 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2769 unsigned int i_sample_description_index
;
2771 if( p_sys
->b_fragmented
|| p_track
->i_chunk_count
== 0 )
2772 i_sample_description_index
= 1; /* XXX */
2774 i_sample_description_index
=
2775 p_track
->chunk
[i_chunk
].i_sample_description_index
;
2780 if( !i_sample_description_index
)
2782 msg_Warn( p_demux
, "invalid SampleEntry index (track[Id 0x%x])",
2783 p_track
->i_track_ID
);
2784 return VLC_EGENERIC
;
2787 MP4_Box_t
*p_sample
= MP4_BoxGet( p_track
->p_stsd
, "[%d]",
2788 i_sample_description_index
- 1 );
2791 ( !p_sample
->data
.p_payload
&& p_track
->fmt
.i_cat
!= SPU_ES
) )
2793 msg_Warn( p_demux
, "cannot find SampleEntry (track[Id 0x%x])",
2794 p_track
->i_track_ID
);
2795 return VLC_EGENERIC
;
2798 p_track
->p_sample
= p_sample
;
2801 if( ( p_frma
= MP4_BoxGet( p_track
->p_sample
, "sinf/frma" ) ) && p_frma
->data
.p_frma
)
2803 msg_Warn( p_demux
, "Original Format Box: %4.4s", (char *)&p_frma
->data
.p_frma
->i_type
);
2805 p_sample
->i_type
= p_frma
->data
.p_frma
->i_type
;
2809 switch( p_track
->fmt
.i_cat
)
2812 if ( !SetupVideoES( p_demux
, p_track
, p_sample
) )
2813 return VLC_EGENERIC
;
2815 /* Set frame rate */
2816 TrackGetESSampleRate( p_demux
,
2817 &p_track
->fmt
.video
.i_frame_rate
,
2818 &p_track
->fmt
.video
.i_frame_rate_base
,
2819 p_track
, i_sample_description_index
, i_chunk
);
2821 p_demux
->p_sys
->f_fps
= (float)p_track
->fmt
.video
.i_frame_rate
/
2822 (float)p_track
->fmt
.video
.i_frame_rate_base
;
2827 if ( !SetupAudioES( p_demux
, p_track
, p_sample
) )
2828 return VLC_EGENERIC
;
2831 audio_replay_gain_t
*p_arg
= &p_track
->fmt
.audio_replay_gain
;
2832 const char *psz_meta
= vlc_meta_GetExtra( p_sys
->p_meta
, "replaygain_track_gain" );
2835 double f_gain
= us_atof( psz_meta
);
2836 p_arg
->pf_gain
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
;
2837 p_arg
->pb_gain
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
!= 0;
2839 psz_meta
= vlc_meta_GetExtra( p_sys
->p_meta
, "replaygain_track_peak" );
2842 double f_gain
= us_atof( psz_meta
);
2843 p_arg
->pf_peak
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
;
2844 p_arg
->pb_peak
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
> 0;
2850 if ( !SetupSpuES( p_demux
, p_track
, p_sample
) )
2851 return VLC_EGENERIC
;
2859 *pp_es
= MP4_AddTrackES( p_demux
->out
, p_track
);
2861 return ( !pp_es
|| *pp_es
) ? VLC_SUCCESS
: VLC_EGENERIC
;
2864 /* *** Try to find nearest sync points *** */
2865 static int TrackGetNearestSeekPoint( demux_t
*p_demux
, mp4_track_t
*p_track
,
2866 uint32_t i_sample
, uint32_t *pi_sync_sample
)
2868 int i_ret
= VLC_EGENERIC
;
2869 *pi_sync_sample
= 0;
2871 const MP4_Box_t
*p_stss
;
2872 if( ( p_stss
= MP4_BoxGet( p_track
->p_stbl
, "stss" ) ) )
2874 const MP4_Box_data_stss_t
*p_stss_data
= BOXDATA(p_stss
);
2875 msg_Dbg( p_demux
, "track[Id 0x%x] using Sync Sample Box (stss)",
2876 p_track
->i_track_ID
);
2877 for( unsigned i_index
= 0; i_index
< p_stss_data
->i_entry_count
; i_index
++ )
2879 if( i_index
>= p_stss_data
->i_entry_count
- 1 ||
2880 i_sample
< p_stss_data
->i_sample_number
[i_index
+1] )
2882 *pi_sync_sample
= p_stss_data
->i_sample_number
[i_index
];
2883 msg_Dbg( p_demux
, "stss gives %d --> %" PRIu32
" (sample number)",
2884 i_sample
, *pi_sync_sample
);
2885 i_ret
= VLC_SUCCESS
;
2891 /* try rap samples groups */
2892 const MP4_Box_t
*p_sbgp
= MP4_BoxGet( p_track
->p_stbl
, "sbgp" );
2893 for( ; p_sbgp
; p_sbgp
= p_sbgp
->p_next
)
2895 const MP4_Box_data_sbgp_t
*p_sbgp_data
= BOXDATA(p_sbgp
);
2896 if( p_sbgp
->i_type
!= ATOM_sbgp
|| !p_sbgp_data
)
2899 if( p_sbgp_data
->i_grouping_type
== SAMPLEGROUP_rap
)
2901 uint32_t i_group_sample
= 0;
2902 for ( uint32_t i
=0; i
<p_sbgp_data
->i_entry_count
; i
++ )
2904 /* Sample belongs to rap group ? */
2905 if( p_sbgp_data
->entries
.pi_group_description_index
[i
] != 0 )
2907 if( i_sample
< i_group_sample
)
2909 msg_Dbg( p_demux
, "sbgp lookup failed %" PRIu32
" (sample number)",
2913 else if ( i_sample
>= i_group_sample
&&
2914 *pi_sync_sample
< i_group_sample
)
2916 *pi_sync_sample
= i_group_sample
;
2917 i_ret
= VLC_SUCCESS
;
2920 i_group_sample
+= p_sbgp_data
->entries
.pi_sample_count
[i
];
2923 if( i_ret
== VLC_SUCCESS
&& *pi_sync_sample
)
2925 msg_Dbg( p_demux
, "sbgp gives %d --> %" PRIu32
" (sample number)",
2926 i_sample
, *pi_sync_sample
);
2934 /* given a time it return sample/chunk
2935 * it also update elst field of the track
2937 static int TrackTimeToSampleChunk( demux_t
*p_demux
, mp4_track_t
*p_track
,
2938 int64_t i_start
, uint32_t *pi_chunk
,
2939 uint32_t *pi_sample
)
2941 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2943 unsigned int i_sample
;
2944 unsigned int i_chunk
;
2947 /* FIXME see if it's needed to check p_track->i_chunk_count */
2948 if( p_track
->i_chunk_count
== 0 )
2949 return( VLC_EGENERIC
);
2951 /* handle elst (find the correct one) */
2952 MP4_TrackSetELST( p_demux
, p_track
, i_start
);
2953 if( p_track
->p_elst
&& p_track
->BOXDATA(p_elst
)->i_entry_count
> 0 )
2955 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
2956 int64_t i_mvt
= MP4_rescale( i_start
, CLOCK_FREQ
, p_sys
->i_timescale
);
2958 /* now calculate i_start for this elst */
2960 i_start
-= MP4_rescale( p_track
->i_elst_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
2968 /* to track time scale */
2969 i_start
= MP4_rescale( i_start
, CLOCK_FREQ
, p_track
->i_timescale
);
2970 /* add elst offset */
2971 if( ( elst
->i_media_rate_integer
[p_track
->i_elst
] > 0 ||
2972 elst
->i_media_rate_fraction
[p_track
->i_elst
] > 0 ) &&
2973 elst
->i_media_time
[p_track
->i_elst
] > 0 )
2975 i_start
+= elst
->i_media_time
[p_track
->i_elst
];
2978 msg_Dbg( p_demux
, "elst (%d) gives %"PRId64
"ms (movie)-> %"PRId64
2979 "ms (track)", p_track
->i_elst
,
2980 MP4_rescale( i_mvt
, p_sys
->i_timescale
, 1000 ),
2981 MP4_rescale( i_start
, p_track
->i_timescale
, 1000 ) );
2985 /* convert absolute time to in timescale unit */
2986 i_start
= MP4_rescale( i_start
, CLOCK_FREQ
, p_track
->i_timescale
);
2989 /* we start from sample 0/chunk 0, hope it won't take too much time */
2990 /* *** find good chunk *** */
2991 for( i_chunk
= 0; ; i_chunk
++ )
2993 if( i_chunk
+ 1 >= p_track
->i_chunk_count
)
2995 /* at the end and can't check if i_start in this chunk,
2996 it will be check while searching i_sample */
2997 i_chunk
= p_track
->i_chunk_count
- 1;
3001 if( (uint64_t)i_start
>= p_track
->chunk
[i_chunk
].i_first_dts
&&
3002 (uint64_t)i_start
< p_track
->chunk
[i_chunk
+ 1].i_first_dts
)
3008 /* *** find sample in the chunk *** */
3009 i_sample
= p_track
->chunk
[i_chunk
].i_sample_first
;
3010 i_dts
= p_track
->chunk
[i_chunk
].i_first_dts
;
3011 for( i_index
= 0; i_sample
< p_track
->chunk
[i_chunk
].i_sample_count
; )
3014 p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
] *
3015 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
] < (uint64_t)i_start
)
3018 p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
] *
3019 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
];
3021 i_sample
+= p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
];
3026 if( p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
] <= 0 )
3030 i_sample
+= ( i_start
- i_dts
) /
3031 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
];
3036 if( i_sample
>= p_track
->i_sample_count
)
3038 msg_Warn( p_demux
, "track[Id 0x%x] will be disabled "
3039 "(seeking too far) chunk=%d sample=%d",
3040 p_track
->i_track_ID
, i_chunk
, i_sample
);
3041 return( VLC_EGENERIC
);
3045 /* *** Try to find nearest sync points *** */
3046 uint32_t i_sync_sample
;
3048 TrackGetNearestSeekPoint( p_demux
, p_track
, i_sample
, &i_sync_sample
) )
3051 if( i_sync_sample
<= i_sample
)
3053 while( i_chunk
> 0 &&
3054 i_sync_sample
< p_track
->chunk
[i_chunk
].i_sample_first
)
3059 while( i_chunk
< p_track
->i_chunk_count
- 1 &&
3060 i_sync_sample
>= p_track
->chunk
[i_chunk
].i_sample_first
+
3061 p_track
->chunk
[i_chunk
].i_sample_count
)
3064 i_sample
= i_sync_sample
;
3067 *pi_chunk
= i_chunk
;
3068 *pi_sample
= i_sample
;
3073 static int TrackGotoChunkSample( demux_t
*p_demux
, mp4_track_t
*p_track
,
3074 unsigned int i_chunk
, unsigned int i_sample
)
3076 bool b_reselect
= false;
3078 /* now see if actual es is ok */
3079 if( p_track
->i_chunk
>= p_track
->i_chunk_count
||
3080 p_track
->chunk
[p_track
->i_chunk
].i_sample_description_index
!=
3081 p_track
->chunk
[i_chunk
].i_sample_description_index
)
3083 msg_Warn( p_demux
, "recreate ES for track[Id 0x%x]",
3084 p_track
->i_track_ID
);
3086 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
,
3087 p_track
->p_es
, &b_reselect
);
3089 es_out_Del( p_demux
->out
, p_track
->p_es
);
3091 p_track
->p_es
= NULL
;
3093 if( TrackCreateES( p_demux
, p_track
, i_chunk
, &p_track
->p_es
) )
3095 msg_Err( p_demux
, "cannot create es for track[Id 0x%x]",
3096 p_track
->i_track_ID
);
3098 p_track
->b_ok
= false;
3099 p_track
->b_selected
= false;
3100 return VLC_EGENERIC
;
3104 /* select again the new decoder */
3107 es_out_Control( p_demux
->out
, ES_OUT_SET_ES
, p_track
->p_es
);
3110 p_track
->i_chunk
= i_chunk
;
3111 p_track
->chunk
[i_chunk
].i_sample
= i_sample
- p_track
->chunk
[i_chunk
].i_sample_first
;
3112 p_track
->i_sample
= i_sample
;
3114 return p_track
->b_selected
? VLC_SUCCESS
: VLC_EGENERIC
;
3117 static void MP4_TrackRestart( demux_t
*p_demux
, mp4_track_t
*p_track
,
3118 MP4_Box_t
*p_params_box
)
3120 bool b_reselect
= false;
3123 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
,
3124 p_track
->p_es
, &b_reselect
);
3127 /* Save previous fragmented pos */
3128 uint32_t i_sample_pos_backup
= p_track
->i_sample
;
3129 mtime_t time_backup
= p_track
->i_time
;
3130 uint32_t timescale_backup
= p_track
->i_timescale
;
3132 /* Save previous format and ES */
3133 es_format_t fmtbackup
;
3134 es_out_id_t
*p_es_backup
= p_track
->p_es
;
3135 p_track
->p_es
= NULL
;
3136 es_format_Copy( &fmtbackup
, &p_track
->fmt
);
3137 es_format_Clean( &p_track
->fmt
);
3140 /* do the cleanup and recycle track / restart */
3141 MP4_TrackDestroy( p_demux
, p_track
);
3142 memset( p_track
, 0, sizeof(*p_track
) );
3144 assert(p_params_box
->i_type
== ATOM_trak
);
3145 MP4_TrackCreate( p_demux
, p_track
, p_params_box
, false, true );
3149 if( !es_format_IsSimilar( &fmtbackup
, &p_track
->fmt
) ||
3150 fmtbackup
.i_extra
!= p_track
->fmt
.i_extra
||
3151 memcmp( fmtbackup
.p_extra
, p_track
->fmt
.p_extra
, fmtbackup
.i_extra
) )
3154 es_out_Del( p_demux
->out
, p_es_backup
);
3156 if( !p_track
->b_chapters_source
)
3158 p_track
->p_es
= MP4_AddTrackES( p_demux
->out
, p_track
);
3159 p_track
->b_ok
= !!p_track
->p_es
;
3164 p_track
->p_es
= p_es_backup
;
3167 else if( p_es_backup
)
3169 es_out_Del( p_demux
->out
, p_es_backup
);
3172 /* select again the new decoder */
3173 if( b_reselect
&& p_track
->p_es
)
3174 es_out_Control( p_demux
->out
, ES_OUT_SET_ES
, p_track
->p_es
);
3176 es_format_Clean( &fmtbackup
);
3178 /* Restore fragmented pos */
3179 p_track
->i_sample
= i_sample_pos_backup
;
3180 p_track
->i_time
= MP4_rescale( time_backup
, timescale_backup
, p_track
->i_timescale
);
3183 /****************************************************************************
3185 ****************************************************************************
3186 * Parse track information and create all needed data to run a track
3187 * If it succeed b_ok is set to 1 else to 0
3188 ****************************************************************************/
3189 static void MP4_TrackSetup( demux_t
*p_demux
, mp4_track_t
*p_track
,
3190 MP4_Box_t
*p_box_trak
,
3191 bool b_create_es
, bool b_force_enable
)
3193 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3195 p_track
->p_track
= p_box_trak
;
3197 char language
[4] = { '\0' };
3198 char sdp_media_type
[8] = { '\0' };
3200 const MP4_Box_t
*p_tkhd
= MP4_BoxGet( p_box_trak
, "tkhd" );
3206 /* do we launch this track by default ? */
3208 ( ( BOXDATA(p_tkhd
)->i_flags
&MP4_TRACK_ENABLED
) != 0 );
3209 if( !p_track
->b_enable
)
3210 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3212 p_track
->i_track_ID
= BOXDATA(p_tkhd
)->i_track_ID
;
3214 p_track
->i_width
= BOXDATA(p_tkhd
)->i_width
/ BLOCK16x16
;
3215 p_track
->i_height
= BOXDATA(p_tkhd
)->i_height
/ BLOCK16x16
;
3216 p_track
->f_rotation
= BOXDATA(p_tkhd
)->f_rotation
;
3218 /* FIXME: unhandled box: tref */
3220 const MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_box_trak
, "mdia/mdhd" );
3221 const MP4_Box_t
*p_hdlr
= MP4_BoxGet( p_box_trak
, "mdia/hdlr" );
3223 if( ( !p_mdhd
)||( !p_hdlr
) )
3228 if( BOXDATA(p_mdhd
)->i_timescale
== 0 )
3230 msg_Warn( p_demux
, "Invalid track timescale " );
3233 p_track
->i_timescale
= BOXDATA(p_mdhd
)->i_timescale
;
3235 memcpy( &language
, BOXDATA(p_mdhd
)->rgs_language
, 3 );
3236 p_track
->b_mac_encoding
= BOXDATA(p_mdhd
)->b_mac_encoding
;
3238 switch( p_hdlr
->data
.p_hdlr
->i_handler_type
)
3241 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/smhd" ) )
3245 es_format_Change( &p_track
->fmt
, AUDIO_ES
, 0 );
3249 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/vmhd") )
3253 es_format_Change( &p_track
->fmt
, VIDEO_ES
, 0 );
3257 /* RTP Reception Hint tracks */
3258 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/hmhd" ) ||
3259 !MP4_BoxGet( p_box_trak
, "mdia/minf/stbl/stsd/rrtp" ) )
3265 /* parse the sdp message to find out whether the RTP stream contained audio or video */
3266 if( !( p_sdp
= MP4_BoxGet( p_box_trak
, "udta/hnti/sdp " ) ) )
3268 msg_Warn( p_demux
, "Didn't find sdp box to determine stream type" );
3272 memcpy( sdp_media_type
, BOXDATA(p_sdp
)->psz_text
, 7 );
3273 if( !strcmp(sdp_media_type
, "m=audio") )
3275 msg_Dbg( p_demux
, "Found audio Rtp: %s", sdp_media_type
);
3276 es_format_Change( &p_track
->fmt
, AUDIO_ES
, 0 );
3278 else if( !strcmp(sdp_media_type
, "m=video") )
3280 msg_Dbg( p_demux
, "Found video Rtp: %s", sdp_media_type
);
3281 es_format_Change( &p_track
->fmt
, VIDEO_ES
, 0 );
3285 msg_Warn( p_demux
, "Malformed track SDP message: %s", sdp_media_type
);
3288 p_track
->p_sdp
= p_sdp
;
3294 case( ATOM_subt
): /* ttml */
3296 case( ATOM_clcp
): /* closed captions */
3297 es_format_Change( &p_track
->fmt
, SPU_ES
, 0 );
3304 p_track
->asfinfo
.i_cat
= p_track
->fmt
.i_cat
;
3306 const MP4_Box_t
*p_elst
;
3307 p_track
->i_elst
= 0;
3308 p_track
->i_elst_time
= 0;
3309 if( ( p_track
->p_elst
= p_elst
= MP4_BoxGet( p_box_trak
, "edts/elst" ) ) )
3311 MP4_Box_data_elst_t
*elst
= BOXDATA(p_elst
);
3314 msg_Warn( p_demux
, "elst box found" );
3315 for( i
= 0; i
< elst
->i_entry_count
; i
++ )
3317 msg_Dbg( p_demux
, " - [%d] duration=%"PRId64
"ms media time=%"PRId64
3318 "ms) rate=%d.%d", i
,
3319 MP4_rescale( elst
->i_segment_duration
[i
], p_sys
->i_timescale
, 1000 ),
3320 elst
->i_media_time
[i
] >= 0 ?
3321 MP4_rescale( elst
->i_media_time
[i
], p_track
->i_timescale
, 1000 ) :
3323 elst
->i_media_rate_integer
[i
],
3324 elst
->i_media_rate_fraction
[i
] );
3331 p_dinf = MP4_BoxGet( p_minf, "dinf" );
3333 if( !( p_track
->p_stbl
= MP4_BoxGet( p_box_trak
,"mdia/minf/stbl" ) ) ||
3334 !( p_track
->p_stsd
= MP4_BoxGet( p_box_trak
,"mdia/minf/stbl/stsd") ) )
3340 if( *language
&& strcmp( language
, "```" ) && strcmp( language
, "und" ) )
3342 p_track
->fmt
.psz_language
= strdup( language
);
3345 const MP4_Box_t
*p_udta
= MP4_BoxGet( p_box_trak
, "udta" );
3348 const MP4_Box_t
*p_box_iter
;
3349 for( p_box_iter
= p_udta
->p_first
; p_box_iter
!= NULL
;
3350 p_box_iter
= p_box_iter
->p_next
)
3352 switch( p_box_iter
->i_type
)
3356 p_track
->fmt
.psz_description
=
3357 strndup( p_box_iter
->data
.p_binary
->p_blob
,
3358 p_box_iter
->data
.p_binary
->i_blob
);
3365 /* Create chunk index table and sample index table */
3366 if( TrackCreateChunksIndex( p_demux
,p_track
) ||
3367 TrackCreateSamplesIndex( p_demux
, p_track
) )
3369 msg_Err( p_demux
, "cannot create chunks index" );
3370 return; /* cannot create chunks index */
3373 p_track
->i_chunk
= 0;
3374 p_track
->i_sample
= 0;
3376 /* Mark chapter only track */
3377 if( p_sys
->p_tref_chap
)
3379 MP4_Box_data_tref_generic_t
*p_chap
= p_sys
->p_tref_chap
->data
.p_tref_generic
;
3382 for( i
= 0; i
< p_chap
->i_entry_count
; i
++ )
3384 if( p_track
->i_track_ID
== p_chap
->i_track_ID
[i
] &&
3385 p_track
->fmt
.i_cat
== UNKNOWN_ES
)
3387 p_track
->b_chapters_source
= true;
3388 p_track
->b_enable
= false;
3394 const MP4_Box_t
*p_tsel
;
3396 if( b_force_enable
&&
3397 ( p_track
->fmt
.i_cat
== VIDEO_ES
|| p_track
->fmt
.i_cat
== AUDIO_ES
) )
3399 msg_Warn( p_demux
, "Enabling track[Id 0x%x] (buggy file without enabled track)",
3400 p_track
->i_track_ID
);
3401 p_track
->b_enable
= true;
3402 p_track
->fmt
.i_priority
= ES_PRIORITY_SELECTABLE_MIN
;
3404 else if ( (p_tsel
= MP4_BoxGet( p_box_trak
, "udta/tsel" )) )
3406 if ( BOXDATA(p_tsel
) && BOXDATA(p_tsel
)->i_switch_group
)
3408 p_track
->i_switch_group
= BOXDATA(p_tsel
)->i_switch_group
;
3409 int i_priority
= ES_PRIORITY_SELECTABLE_MIN
;
3410 for ( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
3412 const mp4_track_t
*p_other
= &p_sys
->track
[i
];
3413 if( p_other
&& p_other
!= p_track
&&
3414 p_other
->fmt
.i_cat
== p_track
->fmt
.i_cat
&&
3415 p_track
->i_switch_group
== p_other
->i_switch_group
)
3416 i_priority
= __MAX( i_priority
, p_other
->fmt
.i_priority
+ 1 );
3418 /* VLC only support ES priority for AUDIO_ES and SPU_ES.
3419 If there's another VIDEO_ES in the same group, we need to unselect it then */
3420 if ( p_track
->fmt
.i_cat
== VIDEO_ES
&& i_priority
> ES_PRIORITY_SELECTABLE_MIN
)
3421 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3423 p_track
->fmt
.i_priority
= i_priority
;
3426 /* If there's no tsel, try to enable the track coming first in edit list */
3427 else if ( p_track
->p_elst
&& p_track
->fmt
.i_priority
== ES_PRIORITY_SELECTABLE_MIN
)
3429 #define MAX_SELECTABLE (INT_MAX - ES_PRIORITY_SELECTABLE_MIN)
3430 for ( uint32_t i
=0; i
<p_track
->BOXDATA(p_elst
)->i_entry_count
; i
++ )
3432 if ( p_track
->BOXDATA(p_elst
)->i_media_time
[i
] >= 0 &&
3433 p_track
->BOXDATA(p_elst
)->i_segment_duration
[i
] )
3435 /* We do selection by inverting start time into priority.
3436 The track with earliest edit will have the highest prio */
3437 const int i_time
= __MIN( MAX_SELECTABLE
, p_track
->BOXDATA(p_elst
)->i_media_time
[i
] );
3438 p_track
->fmt
.i_priority
= ES_PRIORITY_SELECTABLE_MIN
+ MAX_SELECTABLE
- i_time
;
3444 if( p_sys
->hacks
.es_cat_filters
&& (p_sys
->hacks
.es_cat_filters
& p_track
->fmt
.i_cat
) == 0 )
3446 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3449 if( TrackCreateES( p_demux
,
3450 p_track
, p_track
->i_chunk
,
3451 (p_track
->b_chapters_source
|| !b_create_es
) ? NULL
: &p_track
->p_es
) )
3453 msg_Err( p_demux
, "cannot create es for track[Id 0x%x]",
3454 p_track
->i_track_ID
);
3458 p_track
->b_ok
= true;
3461 static void DestroyChunk( mp4_chunk_t
*ck
)
3463 free( ck
->p_sample_count_dts
);
3464 free( ck
->p_sample_delta_dts
);
3465 free( ck
->p_sample_count_pts
);
3466 free( ck
->p_sample_offset_pts
);
3467 free( ck
->p_sample_size
);
3470 /****************************************************************************
3472 ****************************************************************************
3473 * Cleans a track created by MP4_TrackCreate.
3474 ****************************************************************************/
3475 static void MP4_TrackClean( es_out_t
*out
, mp4_track_t
*p_track
)
3477 es_format_Clean( &p_track
->fmt
);
3480 es_out_Del( out
, p_track
->p_es
);
3482 if( p_track
->chunk
)
3484 for( unsigned int i_chunk
= 0; i_chunk
< p_track
->i_chunk_count
; i_chunk
++ )
3485 DestroyChunk( &p_track
->chunk
[i_chunk
] );
3487 free( p_track
->chunk
);
3489 if( !p_track
->i_sample_size
)
3490 free( p_track
->p_sample_size
);
3492 if ( p_track
->asfinfo
.p_frame
)
3493 block_ChainRelease( p_track
->asfinfo
.p_frame
);
3495 free( p_track
->context
.runs
.p_array
);
3498 static void MP4_TrackInit( mp4_track_t
*p_track
)
3500 memset( p_track
, 0, sizeof(mp4_track_t
) );
3501 es_format_Init( &p_track
->fmt
, UNKNOWN_ES
, 0 );
3502 p_track
->i_timescale
= 1;
3505 static void MP4_TrackSelect( demux_t
*p_demux
, mp4_track_t
*p_track
, bool b_select
)
3507 if( !p_track
->b_ok
|| p_track
->b_chapters_source
)
3510 if( b_select
== p_track
->b_selected
)
3513 if( !b_select
&& p_track
->p_es
)
3515 es_out_Control( p_demux
->out
, ES_OUT_SET_ES_STATE
,
3516 p_track
->p_es
, false );
3519 p_track
->b_selected
= b_select
;
3522 static int MP4_TrackSeek( demux_t
*p_demux
, mp4_track_t
*p_track
,
3528 if( !p_track
->b_ok
|| p_track
->b_chapters_source
)
3529 return VLC_EGENERIC
;
3531 p_track
->b_selected
= false;
3533 if( TrackTimeToSampleChunk( p_demux
, p_track
, i_start
,
3534 &i_chunk
, &i_sample
) )
3536 msg_Warn( p_demux
, "cannot select track[Id 0x%x]",
3537 p_track
->i_track_ID
);
3538 return VLC_EGENERIC
;
3541 p_track
->b_selected
= true;
3542 if( !TrackGotoChunkSample( p_demux
, p_track
, i_chunk
, i_sample
) )
3543 p_track
->b_selected
= true;
3545 p_track
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
3547 return p_track
->b_selected
? VLC_SUCCESS
: VLC_EGENERIC
;
3552 * 3 types: for audio
3555 static inline uint32_t MP4_GetFixedSampleSize( const mp4_track_t
*p_track
,
3556 const MP4_Box_data_sample_soun_t
*p_soun
)
3558 uint32_t i_size
= p_track
->i_sample_size
;
3560 assert( p_track
->i_sample_size
!= 0 );
3562 /* QuickTime "built-in" support case fixups */
3563 if( p_track
->fmt
.i_cat
== AUDIO_ES
&&
3564 p_soun
->i_compressionid
== 0 && p_track
->i_sample_size
<= 2 )
3566 switch( p_track
->fmt
.i_codec
)
3569 i_size
= p_soun
->i_channelcount
;
3571 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
3575 case VLC_CODEC_S24L
:
3576 case VLC_CODEC_S24B
:
3577 case VLC_CODEC_S32L
:
3578 case VLC_CODEC_S32B
:
3579 case VLC_CODEC_F32L
:
3580 case VLC_CODEC_F32B
:
3581 case VLC_CODEC_F64L
:
3582 case VLC_CODEC_F64B
:
3583 if( p_track
->i_sample_size
< ((p_soun
->i_samplesize
+7U)/8U) * p_soun
->i_channelcount
)
3584 i_size
= ((p_soun
->i_samplesize
+7)/8) * p_soun
->i_channelcount
;
3586 case VLC_CODEC_ALAW
:
3587 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
3588 i_size
= p_soun
->i_channelcount
;
3598 static uint32_t MP4_TrackGetReadSize( mp4_track_t
*p_track
, uint32_t *pi_nb_samples
)
3600 uint32_t i_size
= 0;
3603 if ( p_track
->i_sample
== p_track
->i_sample_count
)
3606 if ( p_track
->fmt
.i_cat
!= AUDIO_ES
)
3610 if( p_track
->i_sample_size
== 0 ) /* all sizes are different */
3611 return p_track
->p_sample_size
[p_track
->i_sample
];
3613 return p_track
->i_sample_size
;
3617 const MP4_Box_data_sample_soun_t
*p_soun
= p_track
->p_sample
->data
.p_sample_soun
;
3618 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[p_track
->i_chunk
];
3619 uint32_t i_max_samples
= p_chunk
->i_sample_count
- p_chunk
->i_sample
;
3621 /* Group audio packets so we don't call demux for single sample unit */
3622 if( p_track
->fmt
.i_original_fourcc
== VLC_CODEC_DVD_LPCM
&&
3623 p_soun
->i_constLPCMframesperaudiopacket
&&
3624 p_soun
->i_constbytesperaudiopacket
)
3626 /* uncompressed case */
3627 uint32_t i_packets
= i_max_samples
/ p_soun
->i_constLPCMframesperaudiopacket
;
3628 if ( UINT32_MAX
/ p_soun
->i_constbytesperaudiopacket
< i_packets
)
3629 i_packets
= UINT32_MAX
/ p_soun
->i_constbytesperaudiopacket
;
3630 *pi_nb_samples
= i_packets
* p_soun
->i_constLPCMframesperaudiopacket
;
3631 return i_packets
* p_soun
->i_constbytesperaudiopacket
;
3634 if( p_track
->fmt
.i_original_fourcc
== VLC_FOURCC('r','r','t','p') )
3637 return p_track
->i_sample_size
;
3640 /* all samples have a different size */
3641 if( p_track
->i_sample_size
== 0 )
3644 return p_track
->p_sample_size
[p_track
->i_sample
];
3647 if( p_soun
->i_qt_version
== 1 )
3649 if ( p_soun
->i_compressionid
== 0xFFFE )
3651 *pi_nb_samples
= 1; /* != number of audio samples */
3652 if ( p_track
->i_sample_size
)
3653 return p_track
->i_sample_size
;
3655 return p_track
->p_sample_size
[p_track
->i_sample
];
3657 else if ( p_soun
->i_compressionid
!= 0 || p_soun
->i_bytes_per_sample
> 1 ) /* compressed */
3659 /* in this case we are dealing with compressed data
3660 -2 in V1: additional fields are meaningless (VBR and such) */
3661 *pi_nb_samples
= i_max_samples
;//p_track->chunk[p_track->i_chunk].i_sample_count;
3662 if( p_track
->fmt
.audio
.i_blockalign
> 1 )
3663 *pi_nb_samples
= p_soun
->i_sample_per_packet
;
3664 i_size
= *pi_nb_samples
/ p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
;
3667 else /* uncompressed case */
3670 if( p_track
->fmt
.audio
.i_blockalign
> 1 )
3673 i_packets
= i_max_samples
/ p_soun
->i_sample_per_packet
;
3675 if ( UINT32_MAX
/ p_soun
->i_bytes_per_frame
< i_packets
)
3676 i_packets
= UINT32_MAX
/ p_soun
->i_bytes_per_frame
;
3678 *pi_nb_samples
= i_packets
* p_soun
->i_sample_per_packet
;
3679 i_size
= i_packets
* p_soun
->i_bytes_per_frame
;
3684 /* uncompressed v0 (qt) or... not (ISO) */
3686 /* Quicktime built-in support handling */
3687 if( p_soun
->i_compressionid
== 0 && p_track
->i_sample_size
== 1 )
3689 switch( p_track
->fmt
.i_codec
)
3691 /* sample size is not integer */
3693 *pi_nb_samples
= 160 * p_track
->fmt
.audio
.i_channels
;
3694 return 33 * p_track
->fmt
.audio
.i_channels
;
3700 /* More regular V0 cases */
3701 uint32_t i_max_v0_samples
;
3702 switch( p_track
->fmt
.i_codec
)
3704 /* Compressed samples in V0 */
3705 case VLC_CODEC_AMR_NB
:
3706 case VLC_CODEC_AMR_WB
:
3707 i_max_v0_samples
= 16;
3709 case VLC_CODEC_MPGA
:
3712 i_max_v0_samples
= 1;
3715 /* Read 25ms of samples (uncompressed) */
3716 i_max_v0_samples
= p_track
->fmt
.audio
.i_rate
/ 40 *
3717 p_track
->fmt
.audio
.i_channels
;
3718 if( i_max_v0_samples
< 1 )
3719 i_max_v0_samples
= 1;
3724 for( uint32_t i
=p_track
->i_sample
;
3725 i
<p_chunk
->i_sample_first
+p_chunk
->i_sample_count
&&
3726 i
<p_track
->i_sample_count
;
3730 if ( p_track
->i_sample_size
== 0 )
3731 i_size
+= p_track
->p_sample_size
[i
];
3733 i_size
+= MP4_GetFixedSampleSize( p_track
, p_soun
);
3735 /* Try to detect compression in ISO */
3736 if(p_soun
->i_compressionid
!= 0)
3738 /* Return only 1 sample */
3742 if ( *pi_nb_samples
== i_max_v0_samples
)
3747 //fprintf( stderr, "size=%d\n", i_size );
3751 static uint64_t MP4_TrackGetPos( mp4_track_t
*p_track
)
3753 unsigned int i_sample
;
3756 i_pos
= p_track
->chunk
[p_track
->i_chunk
].i_offset
;
3758 if( p_track
->i_sample_size
)
3760 MP4_Box_data_sample_soun_t
*p_soun
=
3761 p_track
->p_sample
->data
.p_sample_soun
;
3763 /* Quicktime builtin support, _must_ ignore sample tables */
3764 if( p_track
->fmt
.i_cat
== AUDIO_ES
&& p_soun
->i_compressionid
== 0 &&
3765 p_track
->i_sample_size
== 1 )
3767 switch( p_track
->fmt
.i_codec
)
3769 case VLC_CODEC_GSM
: /* # Samples > data size */
3770 i_pos
+= ( p_track
->i_sample
-
3771 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) / 160 * 33;
3778 if( p_track
->fmt
.i_cat
!= AUDIO_ES
|| p_soun
->i_qt_version
== 0 ||
3779 p_track
->fmt
.audio
.i_blockalign
<= 1 ||
3780 p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
== 0 )
3782 i_pos
+= ( p_track
->i_sample
-
3783 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) *
3784 MP4_GetFixedSampleSize( p_track
, p_soun
);
3788 /* we read chunk by chunk unless a blockalign is requested */
3789 i_pos
+= ( p_track
->i_sample
- p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) /
3790 p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
;
3795 for( i_sample
= p_track
->chunk
[p_track
->i_chunk
].i_sample_first
;
3796 i_sample
< p_track
->i_sample
; i_sample
++ )
3798 i_pos
+= p_track
->p_sample_size
[i_sample
];
3805 static int MP4_TrackNextSample( demux_t
*p_demux
, mp4_track_t
*p_track
, uint32_t i_samples
)
3807 if ( UINT32_MAX
- p_track
->i_sample
< i_samples
)
3809 p_track
->i_sample
= UINT32_MAX
;
3810 return VLC_EGENERIC
;
3813 p_track
->i_sample
+= i_samples
;
3815 if( p_track
->i_sample
>= p_track
->i_sample_count
)
3816 return VLC_EGENERIC
;
3818 /* Have we changed chunk ? */
3819 if( p_track
->i_sample
>=
3820 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
+
3821 p_track
->chunk
[p_track
->i_chunk
].i_sample_count
)
3823 if( TrackGotoChunkSample( p_demux
, p_track
, p_track
->i_chunk
+ 1,
3824 p_track
->i_sample
) )
3826 msg_Warn( p_demux
, "track[0x%x] will be disabled "
3827 "(cannot restart decoder)", p_track
->i_track_ID
);
3828 MP4_TrackSelect( p_demux
, p_track
, false );
3829 return VLC_EGENERIC
;
3833 /* Have we changed elst */
3834 if( p_track
->p_elst
&& p_track
->BOXDATA(p_elst
)->i_entry_count
> 0 )
3836 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3837 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
3838 uint64_t i_mvt
= MP4_rescale( MP4_TrackGetDTS( p_demux
, p_track
),
3839 CLOCK_FREQ
, p_sys
->i_timescale
);
3840 if( (unsigned int)p_track
->i_elst
< elst
->i_entry_count
&&
3841 i_mvt
>= p_track
->i_elst_time
+
3842 elst
->i_segment_duration
[p_track
->i_elst
] )
3844 MP4_TrackSetELST( p_demux
, p_track
,
3845 MP4_TrackGetDTS( p_demux
, p_track
) );
3852 static void MP4_TrackSetELST( demux_t
*p_demux
, mp4_track_t
*tk
,
3855 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3856 int i_elst_last
= tk
->i_elst
;
3858 /* handle elst (find the correct one) */
3860 tk
->i_elst_time
= 0;
3861 if( tk
->p_elst
&& tk
->BOXDATA(p_elst
)->i_entry_count
> 0 )
3863 MP4_Box_data_elst_t
*elst
= tk
->BOXDATA(p_elst
);
3864 int64_t i_mvt
= MP4_rescale( i_time
, CLOCK_FREQ
, p_sys
->i_timescale
);
3866 for( tk
->i_elst
= 0; (unsigned int)tk
->i_elst
< elst
->i_entry_count
; tk
->i_elst
++ )
3868 mtime_t i_dur
= elst
->i_segment_duration
[tk
->i_elst
];
3870 if( tk
->i_elst_time
<= i_mvt
&& i_mvt
< tk
->i_elst_time
+ i_dur
)
3874 tk
->i_elst_time
+= i_dur
;
3877 if( (unsigned int)tk
->i_elst
>= elst
->i_entry_count
)
3879 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
3880 tk
->i_elst
= elst
->i_entry_count
- 1;
3881 tk
->i_elst_time
-= elst
->i_segment_duration
[tk
->i_elst
];
3884 if( elst
->i_media_time
[tk
->i_elst
] < 0 )
3887 tk
->i_elst_time
+= elst
->i_segment_duration
[tk
->i_elst
];
3890 if( i_elst_last
!= tk
->i_elst
)
3892 msg_Warn( p_demux
, "elst old=%d new=%d", i_elst_last
, tk
->i_elst
);
3893 tk
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
3897 /******************************************************************************
3898 * Here are the functions used for fragmented MP4
3899 *****************************************************************************/
3903 * \Note If we call that function too soon,
3904 * before the track has been selected by MP4_TrackSelect
3905 * (during the first execution of Demux), then the track gets disabled
3907 static int ReInitDecoder( demux_t
*p_demux
, const MP4_Box_t
*p_root
,
3908 mp4_track_t
*p_track
)
3910 MP4_Box_t
*p_paramsbox
= MP4_BoxGet( p_root
, "/moov/trak[0]" );
3912 return VLC_EGENERIC
;
3914 MP4_TrackRestart( p_demux
, p_track
, p_paramsbox
);
3916 /* Temporary hack until we support track selection */
3917 p_track
->b_selected
= true;
3918 p_track
->b_enable
= true;
3924 static stime_t
GetCumulatedDuration( demux_t
*p_demux
)
3926 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3927 stime_t i_max_duration
= 0;
3929 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
3931 stime_t i_track_duration
= 0;
3932 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_sys
->p_moov
, p_sys
->track
[i
].i_track_ID
);
3933 const MP4_Box_t
*p_stsz
;
3934 const MP4_Box_t
*p_tkhd
;
3935 if ( (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) &&
3936 (p_stsz
= MP4_BoxGet( p_trak
, "mdia/minf/stbl/stsz" )) &&
3937 /* duration might be wrong an be set to whole duration :/ */
3938 BOXDATA(p_stsz
)->i_sample_count
> 0 )
3940 i_max_duration
= __MAX( (uint64_t)i_max_duration
, BOXDATA(p_tkhd
)->i_duration
);
3943 if( p_sys
->p_fragsindex
)
3945 i_track_duration
+= MP4_Fragment_Index_GetTrackDuration( p_sys
->p_fragsindex
, i
);
3948 i_max_duration
= __MAX( i_max_duration
, i_track_duration
);
3951 return i_max_duration
;
3954 static int ProbeIndex( demux_t
*p_demux
)
3956 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3957 uint64_t i_stream_size
;
3958 uint8_t mfro
[MP4_MFRO_BOXSIZE
];
3959 assert( p_sys
->b_seekable
);
3961 if ( MP4_BoxCount( p_sys
->p_root
, "/mfra" ) )
3962 return VLC_EGENERIC
;
3964 i_stream_size
= stream_Size( p_demux
->s
);
3965 if ( ( i_stream_size
>> 62 ) ||
3966 ( i_stream_size
< MP4_MFRO_BOXSIZE
) ||
3967 ( vlc_stream_Seek( p_demux
->s
, i_stream_size
- MP4_MFRO_BOXSIZE
) != VLC_SUCCESS
)
3970 msg_Dbg( p_demux
, "Probing tail for mfro has failed" );
3971 return VLC_EGENERIC
;
3974 if ( vlc_stream_Read( p_demux
->s
, &mfro
, MP4_MFRO_BOXSIZE
) == MP4_MFRO_BOXSIZE
&&
3975 VLC_FOURCC(mfro
[4],mfro
[5],mfro
[6],mfro
[7]) == ATOM_mfro
&&
3976 GetDWBE( &mfro
) == MP4_MFRO_BOXSIZE
)
3978 uint32_t i_offset
= GetDWBE( &mfro
[12] );
3979 msg_Dbg( p_demux
, "will read mfra index at %"PRIu64
, i_stream_size
- i_offset
);
3980 if ( i_stream_size
> i_offset
&&
3981 vlc_stream_Seek( p_demux
->s
, i_stream_size
- i_offset
) == VLC_SUCCESS
)
3983 msg_Dbg( p_demux
, "reading mfra index at %"PRIu64
, i_stream_size
- i_offset
);
3984 const uint32_t stoplist
[] = { ATOM_mfra
, 0 };
3985 MP4_ReadBoxContainerChildren( p_demux
->s
, p_sys
->p_root
, stoplist
);
3989 return VLC_EGENERIC
;
3992 static stime_t
GetMoovTrackDuration( demux_sys_t
*p_sys
, unsigned i_track_ID
)
3994 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_sys
->p_moov
, i_track_ID
);
3995 const MP4_Box_t
*p_stsz
;
3996 const MP4_Box_t
*p_tkhd
;
3997 if ( (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) &&
3998 (p_stsz
= MP4_BoxGet( p_trak
, "mdia/minf/stbl/stsz" )) &&
3999 /* duration might be wrong an be set to whole duration :/ */
4000 BOXDATA(p_stsz
)->i_sample_count
> 0 )
4002 return BOXDATA(p_tkhd
)->i_duration
; /* In movie / mvhd scale */
4007 static bool GetMoofTrackDuration( MP4_Box_t
*p_moov
, MP4_Box_t
*p_moof
,
4008 unsigned i_track_ID
, stime_t
*p_duration
)
4010 if ( !p_moof
|| !p_moov
)
4013 MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
4016 if ( p_traf
->i_type
!= ATOM_traf
)
4018 p_traf
= p_traf
->p_next
;
4022 const MP4_Box_t
*p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" );
4023 const MP4_Box_t
*p_trun
= MP4_BoxGet( p_traf
, "trun" );
4024 if ( !p_tfhd
|| !p_trun
|| i_track_ID
!= BOXDATA(p_tfhd
)->i_track_ID
)
4026 p_traf
= p_traf
->p_next
;
4030 uint32_t i_track_timescale
= 0;
4031 uint32_t i_track_defaultsampleduration
= 0;
4033 /* set trex for defaults */
4034 MP4_Box_t
*p_trex
= MP4_GetTrexByTrackID( p_moov
, BOXDATA(p_tfhd
)->i_track_ID
);
4037 i_track_defaultsampleduration
= BOXDATA(p_trex
)->i_default_sample_duration
;
4040 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_moov
, BOXDATA(p_tfhd
)->i_track_ID
);
4043 MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_trak
, "mdia/mdhd" );
4045 i_track_timescale
= BOXDATA(p_mdhd
)->i_timescale
;
4048 if ( !i_track_timescale
)
4050 p_traf
= p_traf
->p_next
;
4054 uint64_t i_traf_duration
= 0;
4055 while ( p_trun
&& p_tfhd
)
4057 if ( p_trun
->i_type
!= ATOM_trun
)
4059 p_trun
= p_trun
->p_next
;
4062 const MP4_Box_data_trun_t
*p_trundata
= p_trun
->data
.p_trun
;
4064 /* Sum total time */
4065 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4067 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4068 i_traf_duration
+= p_trundata
->p_samples
[i
].i_duration
;
4070 else if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
4072 i_traf_duration
+= p_trundata
->i_sample_count
*
4073 BOXDATA(p_tfhd
)->i_default_sample_duration
;
4077 i_traf_duration
+= p_trundata
->i_sample_count
*
4078 i_track_defaultsampleduration
;
4081 p_trun
= p_trun
->p_next
;
4084 *p_duration
= i_traf_duration
;
4091 static int ProbeFragments( demux_t
*p_demux
, bool b_force
, bool *pb_fragmented
)
4093 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4095 msg_Dbg( p_demux
, "probing fragments from %"PRId64
, vlc_stream_Tell( p_demux
->s
) );
4097 assert( p_sys
->p_root
);
4099 MP4_Box_t
*p_vroot
= MP4_BoxNew(ATOM_root
);
4101 return VLC_EGENERIC
;
4103 if( p_sys
->b_seekable
&& (p_sys
->b_fastseekable
|| b_force
) )
4105 MP4_ReadBoxContainerChildren( p_demux
->s
, p_vroot
, NULL
); /* Get the rest of the file */
4106 p_sys
->b_fragments_probed
= true;
4108 const unsigned i_moof
= MP4_BoxCount( p_vroot
, "/moof" );
4111 *pb_fragmented
= true;
4112 p_sys
->p_fragsindex
= MP4_Fragments_Index_New( p_sys
->i_tracks
, i_moof
);
4113 if( !p_sys
->p_fragsindex
)
4115 MP4_BoxFree( p_vroot
);
4116 return VLC_EGENERIC
;
4119 stime_t
*pi_track_times
= calloc( p_sys
->i_tracks
, sizeof(*pi_track_times
) );
4120 if( !pi_track_times
)
4122 MP4_Fragments_Index_Delete( p_sys
->p_fragsindex
);
4123 p_sys
->p_fragsindex
= NULL
;
4124 MP4_BoxFree( p_vroot
);
4125 return VLC_EGENERIC
;
4130 for( MP4_Box_t
*p_moof
= p_vroot
->p_first
; p_moof
; p_moof
= p_moof
->p_next
)
4132 if( p_moof
->i_type
!= ATOM_moof
)
4135 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4137 stime_t i_duration
= 0;
4138 MP4_Box_t
*p_tfdt
= NULL
;
4139 MP4_Box_t
*p_traf
= MP4_GetTrafByTrackID( p_moof
, p_sys
->track
[i
].i_track_ID
);
4141 p_tfdt
= MP4_BoxGet( p_traf
, "tfdt" );
4143 /* Set first fragment time offset from moov */
4145 pi_track_times
[i
] = GetMoovTrackDuration( p_sys
, p_sys
->track
[i
].i_track_ID
);
4147 if( p_tfdt
&& BOXDATA(p_tfdt
) )
4149 pi_track_times
[i
] = p_tfdt
->data
.p_tfdt
->i_base_media_decode_time
;
4151 else if( index
== 0 ) /* Set first fragment time offset from moov */
4153 i_duration
= GetMoovTrackDuration( p_sys
, p_sys
->track
[i
].i_track_ID
);
4154 pi_track_times
[i
] = MP4_rescale( i_duration
, p_sys
->i_timescale
, p_sys
->track
[i
].i_timescale
);
4157 stime_t i_movietime
= MP4_rescale( pi_track_times
[i
], p_sys
->track
[i
].i_timescale
, p_sys
->i_timescale
);
4158 p_sys
->p_fragsindex
->p_times
[index
* p_sys
->i_tracks
+ i
] = i_movietime
;
4160 if( GetMoofTrackDuration( p_sys
->p_moov
, p_moof
, p_sys
->track
[i
].i_track_ID
, &i_duration
) )
4161 pi_track_times
[i
] += i_duration
;
4164 p_sys
->p_fragsindex
->pi_pos
[index
++] = p_moof
->i_pos
;
4167 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4169 stime_t i_movietime
= MP4_rescale( pi_track_times
[i
], p_sys
->track
[i
].i_timescale
, p_sys
->i_timescale
);
4170 if( p_sys
->p_fragsindex
->i_last_time
< i_movietime
)
4171 p_sys
->p_fragsindex
->i_last_time
= i_movietime
;
4174 free( pi_track_times
);
4176 MP4_Fragments_Index_Dump( VLC_OBJECT(p_demux
), p_sys
->p_fragsindex
, p_sys
->i_timescale
);
4182 /* We stop at first moof, which validates our fragmentation condition
4183 * and we'll find others while reading. */
4184 const uint32_t excllist
[] = { ATOM_moof
, 0 };
4185 MP4_ReadBoxContainerRestricted( p_demux
->s
, p_vroot
, NULL
, excllist
);
4186 /* Peek since we stopped before restriction */
4187 const uint8_t *p_peek
;
4188 if ( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) == 8 )
4189 *pb_fragmented
= (VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) == ATOM_moof
);
4191 *pb_fragmented
= false;
4194 MP4_BoxFree( p_vroot
);
4196 MP4_Box_t
*p_mehd
= MP4_BoxGet( p_sys
->p_moov
, "mvex/mehd");
4198 p_sys
->i_cumulated_duration
= GetCumulatedDuration( p_demux
);
4203 static void FragResetContext( demux_sys_t
*p_sys
)
4205 if( p_sys
->context
.p_fragment_atom
)
4207 if( p_sys
->context
.p_fragment_atom
!= p_sys
->p_moov
)
4208 MP4_BoxFree( p_sys
->context
.p_fragment_atom
);
4209 p_sys
->context
.p_fragment_atom
= NULL
;
4211 p_sys
->context
.i_current_box_type
= 0;
4213 for ( uint32_t i
=0; i
<p_sys
->i_tracks
; i
++ )
4215 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4216 p_track
->context
.i_default_sample_size
= 0;
4217 p_track
->context
.i_default_sample_duration
= 0;
4221 static int FragDemuxTrack( demux_t
*p_demux
, mp4_track_t
*p_track
,
4222 unsigned i_max_preload
)
4224 if( !p_track
->b_ok
||
4225 p_track
->context
.runs
.i_current
>= p_track
->context
.runs
.i_count
)
4226 return VLC_DEMUXER_EOS
;
4228 const MP4_Box_data_trun_t
*p_trun
=
4229 p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].p_trun
->data
.p_trun
;
4231 if( p_track
->context
.i_trun_sample
>= p_trun
->i_sample_count
)
4232 return VLC_DEMUXER_EOS
;
4234 uint32_t dur
= p_track
->context
.i_default_sample_duration
,
4235 len
= p_track
->context
.i_default_sample_size
;
4237 if( vlc_stream_Tell(p_demux
->s
) != p_track
->context
.i_trun_sample_pos
&&
4238 MP4_Seek( p_demux
->s
, p_track
->context
.i_trun_sample_pos
) != VLC_SUCCESS
)
4239 return VLC_DEMUXER_EOF
;
4241 const stime_t i_demux_max_dts
= (i_max_preload
< UINT_MAX
) ?
4242 p_track
->i_time
+ MP4_rescale( i_max_preload
, CLOCK_FREQ
, p_track
->i_timescale
) :
4245 for( uint32_t i
= p_track
->context
.i_trun_sample
; i
< p_trun
->i_sample_count
; i
++ )
4247 const stime_t i_dts
= p_track
->i_time
;
4248 stime_t i_pts
= i_dts
;
4250 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4251 dur
= p_trun
->p_samples
[i
].i_duration
;
4253 if( i_dts
> i_demux_max_dts
)
4254 return VLC_DEMUXER_SUCCESS
;
4256 p_track
->i_time
+= dur
;
4257 p_track
->context
.i_trun_sample
= i
+ 1;
4259 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
)
4261 if ( p_trun
->i_version
== 1 )
4262 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v1
;
4263 else if( p_trun
->p_samples
[i
].i_composition_time_offset
.v0
< 0xFF000000 )
4264 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v0
;
4265 else /* version 0 with negative */
4266 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v1
;
4269 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
4270 len
= p_trun
->p_samples
[i
].i_size
;
4273 msg_Warn(p_demux
, "Zero duration sample in trun.");
4276 msg_Warn(p_demux
, "Zero length sample in trun.");
4278 block_t
*p_block
= vlc_stream_Block( p_demux
->s
, len
);
4279 uint32_t i_read
= ( p_block
) ? p_block
->i_buffer
: 0;
4280 p_track
->context
.i_trun_sample_pos
+= i_read
;
4281 if( i_read
< len
|| p_block
== NULL
)
4284 block_Release( p_block
);
4285 return VLC_DEMUXER_EOF
;
4289 msg_Dbg( p_demux
, "tk(%i)=%"PRId64
" mv=%"PRId64
" pos=%"PRIu64
, p_track
->i_track_ID
,
4290 VLC_TS_0
+ MP4_rescale( i_dts
, p_track
->i_timescale
, CLOCK_FREQ
),
4291 VLC_TS_0
+ MP4_rescale( i_pts
, p_track
->i_timescale
, CLOCK_FREQ
),
4292 p_track
->context
.i_trun_sample_pos
);
4294 if ( p_track
->p_es
)
4296 p_block
->i_dts
= VLC_TS_0
+ MP4_rescale( i_dts
, p_track
->i_timescale
, CLOCK_FREQ
);
4297 if( p_track
->fmt
.i_cat
== VIDEO_ES
&& !( p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
) )
4298 p_block
->i_pts
= VLC_TS_INVALID
;
4300 p_block
->i_pts
= VLC_TS_0
+ MP4_rescale( i_pts
, p_track
->i_timescale
, CLOCK_FREQ
);
4301 p_block
->i_length
= MP4_rescale( dur
, p_track
->i_timescale
, CLOCK_FREQ
);
4302 MP4_Block_Send( p_demux
, p_track
, p_block
);
4304 else block_Release( p_block
);
4307 if( p_track
->context
.i_trun_sample
== p_trun
->i_sample_count
)
4309 p_track
->context
.i_trun_sample
= 0;
4310 if( ++p_track
->context
.runs
.i_current
< p_track
->context
.runs
.i_count
)
4312 p_track
->i_time
= p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].i_first_dts
;
4313 p_track
->context
.i_trun_sample_pos
= p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].i_offset
;
4317 return VLC_DEMUXER_SUCCESS
;
4320 static int DemuxMoof( demux_t
*p_demux
)
4322 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4325 const unsigned i_max_preload
= ( p_sys
->b_fastseekable
) ? 0 : ( p_sys
->b_seekable
) ? DEMUX_TRACK_MAX_PRELOAD
: UINT_MAX
;
4326 const stime_t i_scaledincrement
= MP4_rescale( DEMUX_INCREMENT
, CLOCK_FREQ
, p_sys
->i_timescale
);
4328 const mtime_t i_nztime
= MP4_GetMoviePTS( p_sys
);
4330 /* !important! Ensure clock is set before sending data */
4331 if( p_sys
->i_pcr
== VLC_TS_INVALID
)
4332 es_out_SetPCR( p_demux
->out
, VLC_TS_0
+ i_nztime
);
4334 /* demux up to increment amount of data on every track, or just set pcr if empty data */
4337 mp4_track_t
*tk
= NULL
;
4338 i_status
= VLC_DEMUXER_EOS
;
4340 /* First pass, find any track within our target increment, ordered by position */
4341 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4343 mp4_track_t
*tk_tmp
= &p_sys
->track
[i
];
4345 if( !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
4346 (!tk_tmp
->b_selected
&& !p_sys
->b_seekable
) ||
4347 tk_tmp
->context
.runs
.i_current
>= tk_tmp
->context
.runs
.i_count
)
4350 /* At least still have data to demux on this or next turns */
4351 i_status
= VLC_DEMUXER_SUCCESS
;
4353 if( MP4_rescale( tk_tmp
->i_time
, tk_tmp
->i_timescale
, CLOCK_FREQ
) <= i_nztime
+ DEMUX_INCREMENT
)
4355 if( tk
== NULL
|| tk_tmp
->context
.i_trun_sample_pos
< tk
->context
.i_trun_sample_pos
)
4362 /* Second pass, refine and find any best candidate having a chunk pos closer than
4363 * current candidate (avoids seeks when increment falls between the 2) from
4364 * current position, but within extended interleave time */
4365 for( unsigned i
= 0; i_max_preload
> 0 && i
< p_sys
->i_tracks
; i
++ )
4367 mp4_track_t
*tk_tmp
= &p_sys
->track
[i
];
4369 !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
4370 (!tk_tmp
->b_selected
&& !p_sys
->b_seekable
) ||
4371 tk_tmp
->context
.runs
.i_current
>= tk_tmp
->context
.runs
.i_count
)
4374 mtime_t i_nzdts
= MP4_rescale( tk_tmp
->i_time
, tk_tmp
->i_timescale
, CLOCK_FREQ
);
4375 if ( i_nzdts
<= i_nztime
+ DEMUX_TRACK_MAX_PRELOAD
)
4377 /* Found a better candidate to avoid seeking */
4378 if( tk_tmp
->context
.i_trun_sample_pos
< tk
->context
.i_trun_sample_pos
)
4380 /* Note: previous candidate will be repicked on next loop */
4384 int i_ret
= FragDemuxTrack( p_demux
, tk
, i_max_preload
);
4386 if( i_ret
== VLC_DEMUXER_SUCCESS
)
4387 i_status
= VLC_DEMUXER_SUCCESS
;
4390 if( i_status
!= VLC_DEMUXER_SUCCESS
|| !tk
)
4394 if( i_status
!= VLC_DEMUXER_EOS
)
4396 p_sys
->i_time
+= __MAX(i_scaledincrement
, 1);
4397 p_sys
->i_pcr
= VLC_TS_0
+ MP4_rescale( p_sys
->i_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
4398 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
4402 stime_t i_segment_end
= INT64_MAX
;
4403 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4405 mp4_track_t
*tk
= &p_sys
->track
[i
];
4406 if( tk
->b_ok
|| tk
->b_chapters_source
||
4407 (!tk
->b_selected
&& !p_sys
->b_seekable
) )
4409 stime_t i_track_end
= MP4_rescale( tk
->i_time
, tk
->i_timescale
, p_sys
->i_timescale
);
4410 if( i_track_end
< i_segment_end
)
4411 i_segment_end
= i_track_end
;
4413 if( i_segment_end
!= INT64_MAX
)
4415 p_sys
->i_time
= i_segment_end
;
4416 p_sys
->i_pcr
= VLC_TS_0
+ MP4_rescale( p_sys
->i_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
4417 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
4424 static int FragCreateTrunIndex( demux_t
*p_demux
, MP4_Box_t
*p_moof
,
4425 MP4_Box_t
*p_chunksidx
, stime_t i_moof_time
, bool b_discontinuity
)
4427 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4429 uint64_t i_traf_base_data_offset
= p_moof
->i_pos
;
4430 uint32_t i_traf
= 0;
4431 uint64_t i_prev_traf_end
= 0;
4433 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4435 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4436 if( p_track
->context
.runs
.p_array
)
4437 free( p_track
->context
.runs
.p_array
);
4438 p_track
->context
.runs
.p_array
= NULL
;
4439 p_track
->context
.runs
.i_count
= 0;
4440 p_track
->context
.runs
.i_current
= 0;
4443 for( MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
4444 p_traf
; p_traf
= p_traf
->p_next
)
4446 if ( p_traf
->i_type
!= ATOM_traf
)
4449 const MP4_Box_t
*p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" );
4450 const uint32_t i_trun_count
= MP4_BoxCount( p_traf
, "trun" );
4451 if ( !p_tfhd
|| !i_trun_count
)
4454 mp4_track_t
*p_track
= MP4_GetTrackByTrackID( p_demux
, BOXDATA(p_tfhd
)->i_track_ID
);
4458 p_track
->context
.runs
.p_array
= calloc(i_trun_count
, sizeof(mp4_run_t
));
4459 if(!p_track
->context
.runs
.p_array
)
4462 /* Get defaults for this/these RUN */
4463 uint32_t i_track_defaultsamplesize
= 0;
4464 uint32_t i_track_defaultsampleduration
= 0;
4465 MP4_GetDefaultSizeAndDuration( p_sys
->p_moov
, BOXDATA(p_tfhd
),
4466 &i_track_defaultsamplesize
,
4467 &i_track_defaultsampleduration
);
4468 p_track
->context
.i_default_sample_size
= i_track_defaultsamplesize
;
4469 p_track
->context
.i_default_sample_duration
= i_track_defaultsampleduration
;
4471 stime_t i_traf_start_time
= p_track
->i_time
;
4472 bool b_has_base_media_decode_time
= false;
4474 if( b_discontinuity
) /* We NEED start time offset for each track */
4476 /* Find start time */
4477 const MP4_Box_t
*p_tfdt
= MP4_BoxGet( p_traf
, "tfdt" );
4480 i_traf_start_time
= BOXDATA(p_tfdt
)->i_base_media_decode_time
;
4481 b_has_base_media_decode_time
= true;
4484 /* Try using Tfxd for base offset (Smooth) */
4485 if( !b_has_base_media_decode_time
&& p_sys
->i_tracks
== 1 )
4487 const MP4_Box_t
*p_uuid
= MP4_BoxGet( p_traf
, "uuid" );
4488 for( ; p_uuid
; p_uuid
= p_uuid
->p_next
)
4490 if( p_uuid
->i_type
== ATOM_uuid
&&
4491 !CmpUUID( &p_uuid
->i_uuid
, &TfxdBoxUUID
) && p_uuid
->data
.p_tfxd
)
4493 i_traf_start_time
= p_uuid
->data
.p_tfxd
->i_fragment_abs_time
;
4494 b_has_base_media_decode_time
= true;
4500 /* After seek we should have probed fragments */
4501 if( !b_has_base_media_decode_time
&& p_sys
->p_fragsindex
)
4503 unsigned i_track_index
= (p_track
- p_sys
->track
);
4504 assert(&p_sys
->track
[i_track_index
] == p_track
);
4505 i_traf_start_time
= MP4_Fragment_Index_GetTrackStartTime( p_sys
->p_fragsindex
,
4506 i_track_index
, p_moof
->i_pos
);
4507 i_traf_start_time
= MP4_rescale( i_traf_start_time
,
4508 p_sys
->i_timescale
, p_track
->i_timescale
);
4509 b_has_base_media_decode_time
= true;
4512 if( !b_has_base_media_decode_time
&& p_chunksidx
)
4514 /* Try using SIDX as base offset.
4515 * This can not work for global sidx but only when sent within each fragment (dash) */
4516 const MP4_Box_data_sidx_t
*p_data
= p_chunksidx
->data
.p_sidx
;
4517 if( p_data
&& p_data
->i_timescale
&& p_data
->i_reference_count
== 1 )
4519 i_traf_start_time
= MP4_rescale( p_data
->i_earliest_presentation_time
,
4520 p_data
->i_timescale
, p_track
->i_timescale
);
4521 b_has_base_media_decode_time
= true;
4525 /* First contiguous segment (moov->moof) and there's no tfdt not probed index (yet) */
4526 if( !b_has_base_media_decode_time
&& FragGetMoofSequenceNumber( p_moof
) == 1 )
4528 i_traf_start_time
= MP4_rescale( GetMoovTrackDuration( p_sys
, p_track
->i_track_ID
),
4529 p_sys
->i_timescale
, p_track
->i_timescale
);
4530 b_has_base_media_decode_time
= true;
4533 /* Use global sidx moof time, in case moof does not carry tfdt */
4534 if( !b_has_base_media_decode_time
&& i_moof_time
!= INT64_MAX
)
4535 i_traf_start_time
= MP4_rescale( i_moof_time
, p_sys
->i_timescale
, p_track
->i_timescale
);
4537 /* That should not happen */
4538 if( !b_has_base_media_decode_time
)
4539 i_traf_start_time
= MP4_rescale( p_sys
->i_time
, p_sys
->i_timescale
, p_track
->i_timescale
);
4542 /* Parse TRUN data */
4544 if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
)
4546 i_traf_base_data_offset
= BOXDATA(p_tfhd
)->i_base_data_offset
;
4548 else if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_DEFAULT_BASE_IS_MOOF
)
4550 i_traf_base_data_offset
= p_moof
->i_pos
/* + 8*/;
4555 i_traf_base_data_offset
= p_moof
->i_pos
/*+ 8*/;
4557 i_traf_base_data_offset
= i_prev_traf_end
;
4560 uint64_t i_trun_dts
= i_traf_start_time
;
4561 uint64_t i_trun_data_offset
= i_traf_base_data_offset
;
4562 uint32_t i_trun_size
= 0;
4564 for( const MP4_Box_t
*p_trun
= MP4_BoxGet( p_traf
, "trun" );
4565 p_trun
&& p_tfhd
; p_trun
= p_trun
->p_next
)
4567 if ( p_trun
->i_type
!= ATOM_trun
)
4570 const MP4_Box_data_trun_t
*p_trundata
= p_trun
->data
.p_trun
;
4572 /* Get data offset */
4573 if ( p_trundata
->i_flags
& MP4_TRUN_DATA_OFFSET
)
4575 /* Fix for broken Trun data offset relative to tfhd instead of moof, as seen in smooth */
4576 if( (BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
) == 0 &&
4578 i_traf_base_data_offset
+ p_trundata
->i_data_offset
< p_moof
->i_pos
+ p_moof
->i_size
+ 8 )
4580 i_trun_data_offset
+= p_moof
->i_size
+ 8;
4582 else if( (BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
) )
4584 i_trun_data_offset
= BOXDATA(p_tfhd
)->i_base_data_offset
+ p_trundata
->i_data_offset
;
4588 i_trun_data_offset
+= p_trundata
->i_data_offset
;
4593 i_trun_data_offset
+= i_trun_size
;
4599 "tk %u run %" PRIu32
" dflt dur %"PRIu32
" size %"PRIu32
" firstdts %"PRId64
" offset %"PRIu64
,
4600 p_track
->i_track_ID
,
4601 p_track
->context
.runs
.i_count
,
4602 i_track_defaultsampleduration
,
4603 i_track_defaultsamplesize
,
4604 MP4_rescale( i_trun_dts
, p_track
->i_timescale
, CLOCK_FREQ
), i_trun_data_offset
);
4607 mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_count
++];
4608 p_run
->i_first_dts
= i_trun_dts
;
4609 p_run
->i_offset
= i_trun_data_offset
;
4610 p_run
->p_trun
= p_trun
;
4613 /* Sum total time */
4614 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4616 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4617 i_trun_dts
+= p_trundata
->p_samples
[i
].i_duration
;
4621 i_trun_dts
+= p_trundata
->i_sample_count
*
4622 i_track_defaultsampleduration
;
4625 /* Get total traf size */
4626 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
4628 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4629 i_trun_size
+= p_trundata
->p_samples
[i
].i_size
;
4633 i_trun_size
+= p_trundata
->i_sample_count
*
4634 i_track_defaultsamplesize
;
4637 i_prev_traf_end
= i_trun_data_offset
+ i_trun_size
;
4646 static int FragGetMoofBySidxIndex( demux_t
*p_demux
, mtime_t i_target_time
,
4647 uint64_t *pi_moof_pos
, mtime_t
*pi_sampletime
)
4649 const MP4_Box_t
*p_sidx
= MP4_BoxGet( p_demux
->p_sys
->p_root
, "sidx" );
4650 const MP4_Box_data_sidx_t
*p_data
;
4651 if( !p_sidx
|| !((p_data
= BOXDATA(p_sidx
))) || !p_data
->i_timescale
)
4652 return VLC_EGENERIC
;
4654 i_target_time
= MP4_rescale( i_target_time
, CLOCK_FREQ
, p_data
->i_timescale
);
4656 /* sidx refers to offsets from end of sidx pos in the file + first offset */
4657 uint64_t i_pos
= p_data
->i_first_offset
+ p_sidx
->i_pos
+ p_sidx
->i_size
;
4659 for( uint16_t i
=0; i
<p_data
->i_reference_count
; i
++ )
4661 if( i_time
+ p_data
->p_items
[i
].i_subsegment_duration
> i_target_time
)
4663 *pi_sampletime
= MP4_rescale( i_time
, p_data
->i_timescale
, CLOCK_FREQ
);
4664 *pi_moof_pos
= i_pos
;
4667 i_pos
+= p_data
->p_items
[i
].i_referenced_size
;
4668 i_time
+= p_data
->p_items
[i
].i_subsegment_duration
;
4671 return VLC_EGENERIC
;
4674 static int FragGetMoofByTfraIndex( demux_t
*p_demux
, const mtime_t i_target_time
, unsigned i_track_ID
,
4675 uint64_t *pi_moof_pos
, mtime_t
*pi_sampletime
)
4677 MP4_Box_t
*p_tfra
= MP4_BoxGet( p_demux
->p_sys
->p_root
, "mfra/tfra" );
4678 for( ; p_tfra
; p_tfra
= p_tfra
->p_next
)
4680 if ( p_tfra
->i_type
== ATOM_tfra
)
4682 const MP4_Box_data_tfra_t
*p_data
= BOXDATA(p_tfra
);
4683 if( !p_data
|| p_data
->i_track_ID
!= i_track_ID
)
4687 mp4_track_t
*p_track
= MP4_GetTrackByTrackID( p_demux
, p_data
->i_track_ID
);
4690 stime_t i_track_target_time
= MP4_rescale( i_target_time
, CLOCK_FREQ
, p_track
->i_timescale
);
4691 for ( uint32_t i
= 0; i
<p_data
->i_number_of_entries
; i
+= ( p_data
->i_version
== 1 ) ? 2 : 1 )
4695 if ( p_data
->i_version
== 1 )
4697 i_time
= *((uint64_t *)(p_data
->p_time
+ i
));
4698 i_offset
= *((uint64_t *)(p_data
->p_moof_offset
+ i
));
4702 i_time
= p_data
->p_time
[i
];
4703 i_offset
= p_data
->p_moof_offset
[i
];
4706 if ( i_time
>= i_track_target_time
)
4708 if ( i_pos
== 0 ) /* Not in this traf */
4711 *pi_moof_pos
= i_pos
;
4712 *pi_sampletime
= MP4_rescale( i_time
, p_track
->i_timescale
, CLOCK_FREQ
);
4721 return VLC_EGENERIC
;
4724 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t
*p_moov
,
4725 const MP4_Box_data_tfhd_t
*p_tfhd_data
,
4726 uint32_t *pi_default_size
,
4727 uint32_t *pi_default_duration
)
4729 if( p_tfhd_data
->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
4730 *pi_default_duration
= p_tfhd_data
->i_default_sample_duration
;
4732 if( p_tfhd_data
->i_flags
& MP4_TFHD_DFLT_SAMPLE_SIZE
)
4733 *pi_default_size
= p_tfhd_data
->i_default_sample_size
;
4735 if( !*pi_default_duration
|| !*pi_default_size
)
4737 const MP4_Box_t
*p_trex
= MP4_GetTrexByTrackID( p_moov
, p_tfhd_data
->i_track_ID
);
4740 if ( !*pi_default_duration
)
4741 *pi_default_duration
= BOXDATA(p_trex
)->i_default_sample_duration
;
4742 if ( !*pi_default_size
)
4743 *pi_default_size
= BOXDATA(p_trex
)->i_default_sample_size
;
4748 static int DemuxFrag( demux_t
*p_demux
)
4750 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4751 unsigned i_track_selected
= 0;
4752 int i_status
= VLC_DEMUXER_SUCCESS
;
4754 if( unlikely(p_sys
->b_error
) )
4756 msg_Warn( p_demux
, "unrecoverable error" );
4757 i_status
= VLC_DEMUXER_EOF
;
4761 /* check for newly selected/unselected track */
4762 for( unsigned i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
4764 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
4767 if( !tk
->b_ok
|| tk
->b_chapters_source
)
4770 if( p_sys
->b_seekable
)
4771 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
, tk
->p_es
, &b
);
4773 if(tk
->b_selected
!= b
)
4775 msg_Dbg( p_demux
, "track %u %s!", tk
->i_track_ID
, b
? "enabled" : "disabled" );
4776 MP4_TrackSelect( p_demux
, tk
, b
);
4779 if( tk
->b_selected
)
4783 if( i_track_selected
<= 0 )
4785 msg_Warn( p_demux
, "no track selected, exiting..." );
4786 i_status
= VLC_DEMUXER_EOF
;
4790 if ( p_sys
->context
.i_current_box_type
!= ATOM_mdat
)
4792 /* Othewise mdat is skipped. FIXME: mdat reading ! */
4793 const uint8_t *p_peek
;
4794 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) != 8 )
4796 i_status
= VLC_DEMUXER_EOF
;
4800 p_sys
->context
.i_current_box_type
= VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] );
4801 if( p_sys
->context
.i_current_box_type
== ATOM_mdat
)
4803 p_sys
->context
.i_post_mdat_offset
= vlc_stream_Tell( p_demux
->s
) + GetDWBE( p_peek
);
4807 MP4_Box_t
*p_vroot
= MP4_BoxGetNextChunk( p_demux
->s
);
4810 i_status
= VLC_DEMUXER_EOF
;
4814 MP4_Box_t
*p_box
= NULL
;
4815 for( p_box
= p_vroot
->p_first
; p_box
; p_box
= p_box
->p_next
)
4817 if( p_box
->i_type
== ATOM_moof
||
4818 p_box
->i_type
== ATOM_moov
)
4824 FragResetContext( p_sys
);
4826 if( p_box
->i_type
== ATOM_moov
)
4828 p_sys
->context
.p_fragment_atom
= p_sys
->p_moov
;
4832 p_sys
->context
.p_fragment_atom
= MP4_BoxExtract( &p_vroot
->p_first
, p_box
->i_type
);
4834 /* Detect and Handle Passive Seek */
4835 const uint32_t i_sequence_number
= FragGetMoofSequenceNumber( p_sys
->context
.p_fragment_atom
);
4836 const bool b_discontinuity
= ( i_sequence_number
!= p_sys
->context
.i_lastseqnumber
+ 1 );
4837 if( b_discontinuity
)
4838 msg_Info( p_demux
, "Fragment sequence discontinuity detected %"PRIu32
" != %"PRIu32
,
4839 i_sequence_number
, p_sys
->context
.i_lastseqnumber
+ 1 );
4840 p_sys
->context
.i_lastseqnumber
= i_sequence_number
;
4843 if( FragPrepareChunk( p_demux
, p_sys
->context
.p_fragment_atom
,
4844 MP4_BoxGet( p_vroot
, "sidx"), INT64_MAX
,
4845 b_discontinuity
) != VLC_SUCCESS
)
4847 MP4_BoxFree( p_vroot
);
4848 i_status
= VLC_DEMUXER_EOF
;
4852 if( b_discontinuity
)
4854 p_sys
->i_time
= FragGetDemuxTimeFromTracksTime( p_sys
);
4855 p_sys
->i_pcr
= VLC_TS_INVALID
;
4857 /* !Prepare chunk */
4860 p_sys
->context
.i_current_box_type
= p_box
->i_type
;
4863 MP4_BoxFree( p_vroot
);
4865 if( p_sys
->context
.p_fragment_atom
== NULL
)
4867 msg_Info(p_demux
, "no moof or moov in current chunk");
4868 return VLC_DEMUXER_SUCCESS
;
4873 if ( p_sys
->context
.i_current_box_type
== ATOM_mdat
)
4875 assert(p_sys
->context
.p_fragment_atom
);
4877 if ( p_sys
->context
.p_fragment_atom
)
4878 switch( p_sys
->context
.p_fragment_atom
->i_type
)
4880 case ATOM_moov
://[ftyp/moov, mdat]+ -> [moof, mdat]+
4881 i_status
= DemuxMoov( p_demux
);
4884 i_status
= DemuxMoof( p_demux
);
4887 msg_Err( p_demux
, "fragment type %4.4s", (char*) &p_sys
->context
.p_fragment_atom
->i_type
);
4891 if( i_status
== VLC_DEMUXER_EOS
)
4893 i_status
= VLC_DEMUXER_SUCCESS
;
4894 /* Skip if we didn't reach the end of mdat box */
4895 uint64_t i_pos
= vlc_stream_Tell( p_demux
->s
);
4896 if( i_pos
!= p_sys
->context
.i_post_mdat_offset
&& i_status
!= VLC_DEMUXER_EOF
)
4898 if( i_pos
> p_sys
->context
.i_post_mdat_offset
)
4899 msg_Err( p_demux
, " Overread mdat by %" PRIu64
, i_pos
- p_sys
->context
.i_post_mdat_offset
);
4901 msg_Warn( p_demux
, "mdat had still %"PRIu64
" bytes unparsed as samples",
4902 p_sys
->context
.i_post_mdat_offset
- i_pos
);
4903 if( MP4_Seek( p_demux
->s
, p_sys
->context
.i_post_mdat_offset
) != VLC_SUCCESS
)
4904 i_status
= VLC_DEMUXER_EGENERIC
;
4906 p_sys
->context
.i_current_box_type
= 0;
4912 if( i_status
== VLC_DEMUXER_EOF
)
4914 mtime_t i_demux_end
= INT64_MIN
;
4915 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4917 const mp4_track_t
*tk
= &p_sys
->track
[i
];
4918 mtime_t i_track_end
= MP4_rescale( tk
->i_time
, tk
->i_timescale
, CLOCK_FREQ
);
4919 if( i_track_end
> i_demux_end
)
4920 i_demux_end
= i_track_end
;
4922 if( i_demux_end
!= INT64_MIN
)
4923 es_out_SetPCR( p_demux
->out
, VLC_TS_0
+ i_demux_end
);
4930 inline static mp4_track_t
*MP4ASF_GetTrack( asf_packet_sys_t
*p_packetsys
,
4931 uint8_t i_stream_number
)
4933 demux_sys_t
*p_sys
= p_packetsys
->p_demux
->p_sys
;
4934 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
4936 if ( p_sys
->track
[i
].p_asf
&&
4937 i_stream_number
== p_sys
->track
[i
].BOXDATA(p_asf
)->i_stream_number
)
4939 return &p_sys
->track
[i
];
4945 static asf_track_info_t
* MP4ASF_GetTrackInfo( asf_packet_sys_t
*p_packetsys
,
4946 uint8_t i_stream_number
)
4948 mp4_track_t
*p_track
= MP4ASF_GetTrack( p_packetsys
, i_stream_number
);
4950 return &p_track
->asfinfo
;
4955 static void MP4ASF_Send( asf_packet_sys_t
*p_packetsys
, uint8_t i_stream_number
,
4956 block_t
**pp_frame
)
4958 mp4_track_t
*p_track
= MP4ASF_GetTrack( p_packetsys
, i_stream_number
);
4961 block_Release( *pp_frame
);
4965 block_t
*p_gather
= block_ChainGather( *pp_frame
);
4966 p_gather
->i_dts
= p_track
->i_dts_backup
;
4967 p_gather
->i_pts
= p_track
->i_pts_backup
;
4968 es_out_Send( p_packetsys
->p_demux
->out
, p_track
->p_es
, p_gather
);
4974 static void MP4ASF_ResetFrames( demux_sys_t
*p_sys
)
4976 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
4978 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4979 if( p_track
->asfinfo
.p_frame
)
4981 block_ChainRelease( p_track
->asfinfo
.p_frame
);
4982 p_track
->asfinfo
.p_frame
= NULL
;