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 (movie time scale) */
81 uint64_t i_cumulated_duration
; /* Same as above, but not from probing, (movie time scale) */
82 uint32_t i_timescale
; /* movie time scale */
83 uint64_t i_nztime
; /* time position of the presentation (CLOCK_FREQ timescale) */
84 unsigned int i_tracks
; /* number of tracks */
85 mp4_track_t
*track
; /* array of track */
86 float f_fps
; /* number of frame per seconds */
88 bool b_fragmented
; /* fMP4 */
91 bool b_error
; /* unrecoverable */
93 bool b_index_probed
; /* mFra sync points index */
94 bool b_fragments_probed
; /* moof segments index created */
100 uint32_t i_current_box_type
;
101 MP4_Box_t
*p_fragment_atom
;
102 uint64_t i_post_mdat_offset
;
103 uint32_t i_lastseqnumber
;
107 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
&& p_track
->BOXDATA(p_elst
)->i_entry_count
)
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 p_sys
->i_nztime
;
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 demux_sys_t
*p_sys
= p_demux
->p_sys
;
580 p_block
= MP4_Block_Convert( p_demux
, p_track
, p_block
);
581 if( p_block
== NULL
)
584 if ( p_track
->b_chans_reorder
)
586 aout_ChannelReorder( p_block
->p_buffer
, p_block
->i_buffer
,
587 p_track
->fmt
.audio
.i_channels
,
588 p_track
->rgi_chans_reordering
,
589 p_track
->fmt
.i_codec
);
592 p_block
->i_flags
|= p_track
->i_block_flags
;
593 if( p_track
->i_next_block_flags
)
595 p_block
->i_flags
|= p_track
->i_next_block_flags
;
596 p_track
->i_next_block_flags
= 0;
599 /* ASF packets in mov */
602 /* Fake a new stream from MP4 block */
603 stream_t
*p_stream
= p_demux
->s
;
604 p_demux
->s
= vlc_stream_MemoryNew( p_demux
, p_block
->p_buffer
, p_block
->i_buffer
, true );
607 p_track
->i_dts_backup
= p_block
->i_dts
;
608 p_track
->i_pts_backup
= p_block
->i_pts
;
609 /* And demux it as ASF packet */
610 DemuxASFPacket( &p_sys
->asfpacketsys
, p_block
->i_buffer
, p_block
->i_buffer
);
611 vlc_stream_Delete(p_demux
->s
);
613 block_Release(p_block
);
614 p_demux
->s
= p_stream
;
617 es_out_Send( p_demux
->out
, p_track
->p_es
, p_block
);
620 /*****************************************************************************
621 * Open: check file and initializes MP4 structures
622 *****************************************************************************/
623 static int Open( vlc_object_t
* p_this
)
625 demux_t
*p_demux
= (demux_t
*)p_this
;
628 const uint8_t *p_peek
;
631 const MP4_Box_t
*p_mvhd
= NULL
;
632 const MP4_Box_t
*p_mvex
= NULL
;
636 /* A little test to see if it could be a mp4 */
637 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 11 ) < 11 ) return VLC_EGENERIC
;
639 switch( VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) )
650 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
653 /* We don't yet support f4v, but avformat does. */
654 if( p_peek
[8] == 'f' && p_peek
[9] == '4' && p_peek
[10] == 'v' )
661 /* create our structure that will contains all data */
662 p_sys
= calloc( 1, sizeof( demux_sys_t
) );
667 vlc_stream_Control( p_demux
->s
, STREAM_CAN_SEEK
, &p_sys
->b_seekable
);
668 if( p_sys
->b_seekable
)
669 vlc_stream_Control( p_demux
->s
, STREAM_CAN_FASTSEEK
, &p_sys
->b_fastseekable
);
671 /*Set exported functions */
672 p_demux
->pf_demux
= Demux
;
673 p_demux
->pf_control
= Control
;
675 p_sys
->context
.i_lastseqnumber
= UINT32_MAX
;
677 p_demux
->p_sys
= p_sys
;
679 if( LoadInitFrag( p_demux
) != VLC_SUCCESS
)
682 MP4_BoxDumpStructure( p_demux
->s
, p_sys
->p_root
);
684 if( ( p_ftyp
= MP4_BoxGet( p_sys
->p_root
, "/ftyp" ) ) )
686 switch( BOXDATA(p_ftyp
)->i_major_brand
)
690 "ISO Media (isom) version %d.",
691 BOXDATA(p_ftyp
)->i_minor_version
);
697 msg_Dbg( p_demux
, "3GPP Media Release: %4.4s",
698 (char *)&BOXDATA(p_ftyp
)->i_major_brand
);
701 msg_Dbg( p_demux
, "Apple QuickTime media" );
704 msg_Dbg( p_demux
, "PIFF (= isml = fMP4) media" );
707 msg_Dbg( p_demux
, "DASH Stream" );
710 msg_Dbg( p_demux
, "iTunes audio" );
711 if( var_InheritBool( p_demux
, CFG_PREFIX
"m4a-audioonly" ) )
712 p_sys
->hacks
.es_cat_filters
= AUDIO_ES
;
716 "unrecognized major media specification (%4.4s).",
717 (char*)&BOXDATA(p_ftyp
)->i_major_brand
);
720 /* also lookup in compatibility list */
721 for(uint32_t i
=0; i
<BOXDATA(p_ftyp
)->i_compatible_brands_count
; i
++)
723 if (BOXDATA(p_ftyp
)->i_compatible_brands
[i
] == MAJOR_dash
)
725 msg_Dbg( p_demux
, "DASH Stream" );
727 else if (BOXDATA(p_ftyp
)->i_compatible_brands
[i
] == VLC_FOURCC('s', 'm', 'o', 'o') )
729 msg_Dbg( p_demux
, "Handling VLC Smooth Stream" );
735 msg_Dbg( p_demux
, "file type box missing (assuming ISO Media)" );
738 /* the file need to have one moov box */
739 p_sys
->p_moov
= MP4_BoxGet( p_sys
->p_root
, "/moov" );
740 if( unlikely(!p_sys
->p_moov
) )
742 p_sys
->p_moov
= MP4_BoxGet( p_sys
->p_root
, "/foov" );
745 msg_Err( p_demux
, "MP4 plugin discarded (no moov,foov,moof box)" );
748 /* we have a free box as a moov, rename it */
749 p_sys
->p_moov
->i_type
= ATOM_moov
;
752 p_mvhd
= MP4_BoxGet( p_sys
->p_moov
, "mvhd" );
753 if( p_mvhd
&& BOXDATA(p_mvhd
) && BOXDATA(p_mvhd
)->i_timescale
)
755 p_sys
->i_timescale
= BOXDATA(p_mvhd
)->i_timescale
;
756 p_sys
->i_moov_duration
= p_sys
->i_duration
= BOXDATA(p_mvhd
)->i_duration
;
757 p_sys
->i_cumulated_duration
= BOXDATA(p_mvhd
)->i_duration
;
761 msg_Warn( p_demux
, "No valid mvhd found" );
765 MP4_Box_t
*p_rmra
= MP4_BoxGet( p_sys
->p_root
, "/moov/rmra" );
766 if( p_rmra
!= NULL
&& p_demux
->p_input
!= NULL
)
768 int i_count
= MP4_BoxCount( p_rmra
, "rmda" );
771 msg_Dbg( p_demux
, "detected playlist mov file (%d ref)", i_count
);
773 input_thread_t
*p_input
= p_demux
->p_input
;
774 input_item_t
*p_current
= input_GetItem( p_input
);
776 input_item_node_t
*p_subitems
= input_item_node_Create( p_current
);
778 for( i
= 0; i
< i_count
; i
++ )
780 MP4_Box_t
*p_rdrf
= MP4_BoxGet( p_rmra
, "rmda[%d]/rdrf", i
);
784 if( !p_rdrf
|| !BOXDATA(p_rdrf
) || !( psz_ref
= strdup( BOXDATA(p_rdrf
)->psz_ref
) ) )
788 i_ref_type
= BOXDATA(p_rdrf
)->i_ref_type
;
790 msg_Dbg( p_demux
, "new ref=`%s' type=%4.4s",
791 psz_ref
, (char*)&i_ref_type
);
793 if( i_ref_type
== VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
795 if( strstr( psz_ref
, "qt5gateQT" ) )
797 msg_Dbg( p_demux
, "ignoring pseudo ref =`%s'", psz_ref
);
801 if( !strncmp( psz_ref
, "http://", 7 ) ||
802 !strncmp( psz_ref
, "rtsp://", 7 ) )
808 char *psz_absolute
= vlc_uri_resolve( p_demux
->psz_url
,
811 if( psz_absolute
== NULL
)
813 input_item_node_Delete( p_subitems
);
816 psz_ref
= psz_absolute
;
818 msg_Dbg( p_demux
, "adding ref = `%s'", psz_ref
);
819 input_item_t
*p_item
= input_item_New( psz_ref
, NULL
);
820 input_item_CopyOptions( p_item
, p_current
);
821 input_item_node_AppendItem( p_subitems
, p_item
);
822 input_item_Release( p_item
);
826 msg_Err( p_demux
, "unknown ref type=%4.4s FIXME (send a bug report)",
827 (char*)&BOXDATA(p_rdrf
)->i_ref_type
);
832 /* FIXME: create a stream_filter sub-module for this */
833 if (es_out_Control(p_demux
->out
, ES_OUT_POST_SUBNODE
, p_subitems
))
834 input_item_node_Delete(p_subitems
);
837 if( !(p_mvhd
= MP4_BoxGet( p_sys
->p_root
, "/moov/mvhd" ) ) )
841 msg_Err( p_demux
, "cannot find /moov/mvhd" );
846 msg_Warn( p_demux
, "cannot find /moov/mvhd (pure ref file)" );
847 p_demux
->pf_demux
= DemuxRef
;
853 p_sys
->i_timescale
= BOXDATA(p_mvhd
)->i_timescale
;
854 if( p_sys
->i_timescale
== 0 )
856 msg_Err( p_this
, "bad timescale" );
861 const unsigned i_tracks
= MP4_BoxCount( p_sys
->p_root
, "/moov/trak" );
864 msg_Err( p_demux
, "cannot find any /moov/trak" );
867 msg_Dbg( p_demux
, "found %u track%c", i_tracks
, i_tracks
? 's':' ' );
869 if( CreateTracks( p_demux
, i_tracks
) != VLC_SUCCESS
)
872 /* Search the first chap reference (like quicktime) and
873 * check that at least 1 stream is enabled */
874 p_sys
->p_tref_chap
= NULL
;
875 b_enabled_es
= false;
876 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
878 MP4_Box_t
*p_trak
= MP4_BoxGet( p_sys
->p_root
, "/moov/trak[%d]", i
);
881 MP4_Box_t
*p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" );
882 if( p_tkhd
&& BOXDATA(p_tkhd
) && (BOXDATA(p_tkhd
)->i_flags
&MP4_TRACK_ENABLED
) )
885 MP4_Box_t
*p_chap
= MP4_BoxGet( p_trak
, "tref/chap", i
);
886 if( p_chap
&& p_chap
->data
.p_tref_generic
&&
887 p_chap
->data
.p_tref_generic
->i_entry_count
> 0 && !p_sys
->p_tref_chap
)
888 p_sys
->p_tref_chap
= p_chap
;
891 /* Set and store metadata */
892 if( (p_sys
->p_meta
= vlc_meta_New()) )
893 MP4_LoadMeta( p_sys
, p_sys
->p_meta
);
895 /* now process each track and extract all useful information */
896 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
898 MP4_Box_t
*p_trak
= MP4_BoxGet( p_sys
->p_root
, "/moov/trak[%u]", i
);
899 MP4_TrackSetup( p_demux
, &p_sys
->track
[i
], p_trak
, true, !b_enabled_es
);
901 if( p_sys
->track
[i
].b_ok
&& !p_sys
->track
[i
].b_chapters_source
)
904 switch( p_sys
->track
[i
].fmt
.i_cat
)
913 psz_cat
= "subtitle";
921 msg_Dbg( p_demux
, "adding track[Id 0x%x] %s (%s) language %s",
922 p_sys
->track
[i
].i_track_ID
, psz_cat
,
923 p_sys
->track
[i
].b_enable
? "enable":"disable",
924 p_sys
->track
[i
].fmt
.psz_language
?
925 p_sys
->track
[i
].fmt
.psz_language
: "undef" );
927 else if( p_sys
->track
[i
].b_ok
&& p_sys
->track
[i
].b_chapters_source
)
929 msg_Dbg( p_demux
, "using track[Id 0x%x] for chapter language %s",
930 p_sys
->track
[i
].i_track_ID
,
931 p_sys
->track
[i
].fmt
.psz_language
?
932 p_sys
->track
[i
].fmt
.psz_language
: "undef" );
936 msg_Dbg( p_demux
, "ignoring track[Id 0x%x]",
937 p_sys
->track
[i
].i_track_ID
);
941 p_mvex
= MP4_BoxGet( p_sys
->p_moov
, "mvex" );
944 const MP4_Box_t
*p_mehd
= MP4_BoxGet( p_mvex
, "mehd");
945 if ( p_mehd
&& BOXDATA(p_mehd
) )
947 if( BOXDATA(p_mehd
)->i_fragment_duration
> p_sys
->i_duration
)
949 p_sys
->b_fragmented
= true;
950 p_sys
->i_duration
= BOXDATA(p_mehd
)->i_fragment_duration
;
954 const MP4_Box_t
*p_sidx
= MP4_BoxGet( p_sys
->p_root
, "sidx");
956 p_sys
->b_fragmented
= true;
958 if ( p_sys
->b_seekable
)
960 if( !p_sys
->b_fragmented
/* as unknown */ )
962 /* Probe remaining to check if there's really fragments
963 or if that file is just ready to append fragments */
964 ProbeFragments( p_demux
, (p_sys
->i_duration
== 0), &p_sys
->b_fragmented
);
967 if( vlc_stream_Seek( p_demux
->s
, p_sys
->p_moov
->i_pos
) != VLC_SUCCESS
)
970 else /* Handle as fragmented by default as we can't see moof */
972 p_sys
->context
.p_fragment_atom
= p_sys
->p_moov
;
973 p_sys
->context
.i_current_box_type
= ATOM_moov
;
974 p_sys
->b_fragmented
= true;
978 if( p_sys
->b_fragmented
)
980 p_demux
->pf_demux
= DemuxFrag
;
981 msg_Dbg( p_demux
, "Set Fragmented demux mode" );
984 if( !p_sys
->b_seekable
&& p_demux
->pf_demux
== Demux
)
986 msg_Warn( p_demux
, "MP4 plugin discarded (not seekable)" );
990 if( p_sys
->i_tracks
> 1 && !p_sys
->b_fastseekable
)
992 uint64_t i_max_continuity
;
994 MP4_GetInterleaving( p_demux
, &i_max_continuity
, &b_flat
);
996 msg_Warn( p_demux
, "that media doesn't look interleaved, will need to seek");
997 else if( i_max_continuity
> DEMUX_TRACK_MAX_PRELOAD
)
998 msg_Warn( p_demux
, "that media doesn't look properly interleaved, will need to seek");
1002 LoadChapter( p_demux
);
1004 p_sys
->asfpacketsys
.p_demux
= p_demux
;
1005 p_sys
->asfpacketsys
.pi_preroll
= &p_sys
->i_preroll
;
1006 p_sys
->asfpacketsys
.pi_preroll_start
= &p_sys
->i_preroll_start
;
1007 p_sys
->asfpacketsys
.pf_doskip
= NULL
;
1008 p_sys
->asfpacketsys
.pf_send
= MP4ASF_Send
;
1009 p_sys
->asfpacketsys
.pf_gettrackinfo
= MP4ASF_GetTrackInfo
;
1010 p_sys
->asfpacketsys
.pf_updatetime
= NULL
;
1011 p_sys
->asfpacketsys
.pf_setaspectratio
= NULL
;
1016 if( vlc_stream_Tell( p_demux
->s
) > 0 )
1018 if( vlc_stream_Seek( p_demux
->s
, 0 ) != VLC_SUCCESS
)
1019 msg_Warn( p_demux
, "Can't reset stream position from probing" );
1024 return VLC_EGENERIC
;
1027 const unsigned int SAMPLEHEADERSIZE
= 4;
1028 const unsigned int RTPPACKETSIZE
= 12;
1029 const unsigned int CONSTRUCTORSIZE
= 16;
1031 /*******************************************************************************
1032 * MP4_RTPHintToFrame: converts RTP Reception Hint Track sample to H.264 frame
1033 *******************************************************************************/
1034 static block_t
* MP4_RTPHintToFrame( demux_t
*p_demux
, block_t
*p_block
, uint32_t packetcount
)
1036 uint8_t *p_slice
= p_block
->p_buffer
+ SAMPLEHEADERSIZE
;
1037 block_t
*p_newblock
= NULL
;
1038 size_t i_payload
= 0;
1040 if( p_block
->i_buffer
< SAMPLEHEADERSIZE
+ RTPPACKETSIZE
+ CONSTRUCTORSIZE
)
1042 msg_Err( p_demux
, "Sample not large enough for necessary structs");
1043 block_Release( p_block
);
1047 for( uint32_t i
= 0; i
< packetcount
; ++i
)
1049 if( (size_t)(p_slice
- p_block
->p_buffer
) + RTPPACKETSIZE
+ CONSTRUCTORSIZE
> p_block
->i_buffer
)
1052 /* skip RTP header in sample. Could be used to detect packet losses */
1053 p_slice
+= RTPPACKETSIZE
;
1055 mp4_rtpsampleconstructor_t sample_cons
;
1057 sample_cons
.type
= p_slice
[0];
1058 sample_cons
.trackrefindex
= p_slice
[1];
1059 sample_cons
.length
= GetWBE( &p_slice
[2] );
1060 sample_cons
.samplenumber
= GetDWBE( &p_slice
[4] );
1061 sample_cons
.sampleoffset
= GetDWBE( &p_slice
[8] );
1062 sample_cons
.bytesperblock
= GetWBE( &p_slice
[12] );
1063 sample_cons
.samplesperblock
= GetWBE( &p_slice
[14] );
1065 /* skip packet constructor */
1066 p_slice
+= CONSTRUCTORSIZE
;
1068 /* check that is RTPsampleconstructor, referencing itself and no weird audio stuff */
1069 if( sample_cons
.type
!= 2||sample_cons
.trackrefindex
!= -1
1070 ||sample_cons
.samplesperblock
!= 1||sample_cons
.bytesperblock
!= 1 )
1072 msg_Err(p_demux
, "Unhandled constructor in RTP Reception Hint Track. Type:%u", sample_cons
.type
);
1076 /* slice doesn't fit in buffer */
1077 if( sample_cons
.sampleoffset
+ sample_cons
.length
> p_block
->i_buffer
)
1079 msg_Err(p_demux
, "Sample buffer is smaller than sample" );
1083 block_t
*p_realloc
= ( p_newblock
) ?
1084 block_Realloc( p_newblock
, 0, i_payload
+ sample_cons
.length
+ 4 ):
1085 block_Alloc( i_payload
+ sample_cons
.length
+ 4 );
1089 p_newblock
= p_realloc
;
1090 uint8_t *p_dst
= &p_newblock
->p_buffer
[i_payload
];
1092 const uint8_t* p_src
= p_block
->p_buffer
+ sample_cons
.sampleoffset
;
1093 uint8_t i_type
= (*p_src
) & ((1<<5)-1);
1095 const uint8_t synccode
[4] = { 0, 0, 0, 1 };
1096 if( memcmp( p_src
, synccode
, 4 ) )
1098 if( i_type
== 7 || i_type
== 8 )
1107 memcpy( p_dst
, p_src
, sample_cons
.length
);
1108 p_dst
+= sample_cons
.length
;
1110 i_payload
= p_dst
- p_newblock
->p_buffer
;
1113 block_Release( p_block
);
1115 p_newblock
->i_buffer
= i_payload
;
1119 block_Release( p_block
);
1121 block_Release( p_newblock
);
1125 /* RTP Reception Hint Track */
1126 static block_t
* MP4_RTPHint_Convert( demux_t
*p_demux
, block_t
*p_block
, vlc_fourcc_t i_codec
)
1128 block_t
*p_converted
= NULL
;
1129 if( p_block
->i_buffer
< 2 )
1131 block_Release( p_block
);
1135 /* number of RTP packets contained in this sample */
1136 const uint16_t i_packets
= GetWBE( p_block
->p_buffer
);
1137 if( i_packets
<= 1 || i_codec
!= VLC_CODEC_H264
)
1139 const size_t i_skip
= SAMPLEHEADERSIZE
+ i_packets
* ( RTPPACKETSIZE
+ CONSTRUCTORSIZE
);
1140 if( i_packets
== 1 && i_skip
< p_block
->i_buffer
)
1142 p_block
->p_buffer
+= i_skip
;
1143 p_converted
= p_block
;
1147 block_Release( p_block
);
1152 p_converted
= MP4_RTPHintToFrame( p_demux
, p_block
, i_packets
);
1158 /*****************************************************************************
1159 * Demux: read packet and send them to decoders
1160 *****************************************************************************
1161 * TODO check for newly selected track (ie audio upt to now )
1162 *****************************************************************************/
1163 static int DemuxTrack( demux_t
*p_demux
, mp4_track_t
*tk
, uint64_t i_readpos
,
1164 unsigned i_max_preload
)
1166 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1167 uint32_t i_nb_samples
= 0;
1168 uint32_t i_samplessize
= 0;
1170 if( !tk
->b_ok
|| tk
->i_sample
>= tk
->i_sample_count
)
1171 return VLC_DEMUXER_EOS
;
1173 if( tk
->b_chapters_source
)
1174 return VLC_DEMUXER_SUCCESS
;
1176 uint32_t i_run_seq
= MP4_TrackGetRunSeq( tk
);
1177 mtime_t i_current_nzdts
= MP4_TrackGetDTS( p_demux
, tk
);
1178 const mtime_t i_demux_max_nzdts
=(i_max_preload
< UINT_MAX
)
1179 ? i_current_nzdts
+ i_max_preload
1182 for( ; i_demux_max_nzdts
>= i_current_nzdts
; )
1184 if( tk
->i_sample
>= tk
->i_sample_count
)
1185 return VLC_DEMUXER_EOS
;
1188 msg_Dbg( p_demux
, "tk(%i)=%"PRId64
" mv=%"PRId64
" pos=%"PRIu64
, tk
->i_track_ID
,
1189 MP4_TrackGetDTS( p_demux
, tk
),
1190 MP4_GetMoviePTS( p_demux
->p_sys
), i_readpos
);
1193 i_samplessize
= MP4_TrackGetReadSize( tk
, &i_nb_samples
);
1194 if( i_samplessize
> 0 )
1199 if( vlc_stream_Tell( p_demux
->s
) != i_readpos
)
1201 if( MP4_Seek( p_demux
->s
, i_readpos
) != VLC_SUCCESS
)
1203 msg_Warn( p_demux
, "track[0x%x] will be disabled (eof?)"
1204 ": Failed to seek to %"PRIu64
,
1205 tk
->i_track_ID
, i_readpos
);
1206 MP4_TrackSelect( p_demux
, tk
, false );
1212 if( !(p_block
= vlc_stream_Block( p_demux
->s
, i_samplessize
)) )
1214 msg_Warn( p_demux
, "track[0x%x] will be disabled (eof?)"
1215 ": Failed to read %d bytes sample at %"PRIu64
,
1216 tk
->i_track_ID
, i_samplessize
, i_readpos
);
1217 MP4_TrackSelect( p_demux
, tk
, false );
1221 /* !important! Ensure clock is set before sending data */
1222 if( p_sys
->i_pcr
== VLC_TS_INVALID
)
1224 es_out_SetPCR( p_demux
->out
, VLC_TS_0
+ i_current_nzdts
);
1225 p_sys
->i_pcr
= VLC_TS_0
+ i_current_nzdts
;
1229 p_block
->i_dts
= VLC_TS_0
+ i_current_nzdts
;
1231 if( MP4_TrackGetPTSDelta( p_demux
, tk
, &i_delta
) )
1232 p_block
->i_pts
= p_block
->i_dts
+ i_delta
;
1233 else if( tk
->fmt
.i_cat
!= VIDEO_ES
)
1234 p_block
->i_pts
= p_block
->i_dts
;
1236 p_block
->i_pts
= VLC_TS_INVALID
;
1238 MP4_Block_Send( p_demux
, tk
, p_block
);
1242 if ( i_nb_samples
) /* sample size could be 0, need to go fwd. see return */
1243 MP4_TrackNextSample( p_demux
, tk
, i_nb_samples
);
1245 uint32_t i_next_run_seq
= MP4_TrackGetRunSeq( tk
);
1246 if( i_next_run_seq
!= i_run_seq
)
1249 i_current_nzdts
= MP4_TrackGetDTS( p_demux
, tk
);
1250 i_readpos
= MP4_TrackGetPos( tk
);
1253 return VLC_DEMUXER_SUCCESS
;
1256 return VLC_DEMUXER_EGENERIC
;
1259 static int DemuxMoov( demux_t
*p_demux
)
1261 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1262 unsigned int i_track
;
1264 /* check for newly selected/unselected track */
1265 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1267 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1270 if( !tk
->b_ok
|| tk
->b_chapters_source
||
1271 ( tk
->b_selected
&& tk
->i_sample
>= tk
->i_sample_count
) )
1276 if( p_sys
->b_seekable
)
1277 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
, tk
->p_es
, &b
);
1279 if( tk
->b_selected
&& !b
)
1281 MP4_TrackSelect( p_demux
, tk
, false );
1283 else if( !tk
->b_selected
&& b
)
1285 MP4_TrackSeek( p_demux
, tk
, MP4_GetMoviePTS( p_sys
) );
1289 const mtime_t i_nztime
= MP4_GetMoviePTS( p_sys
);
1291 /* We demux/set pcr, even without selected tracks, (empty edits, ...) */
1292 if( p_sys
->i_pcr
!= VLC_TS_INVALID
/* not after a seek */ )
1295 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1297 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1298 if( !tk
->b_ok
|| tk
->b_chapters_source
|| tk
->i_sample
>= tk
->i_sample_count
)
1300 /* Test for EOF on each track (samples count, edit list) */
1301 b_eof
&= ( i_nztime
> MP4_TrackGetDTS( p_demux
, tk
) );
1304 return VLC_DEMUXER_EOS
;
1307 const unsigned i_max_preload
= ( p_sys
->b_fastseekable
) ? 0 : ( p_sys
->b_seekable
) ? DEMUX_TRACK_MAX_PRELOAD
: UINT_MAX
;
1309 /* demux up to increment amount of data on every track, or just set pcr if empty data */
1312 mp4_track_t
*tk
= NULL
;
1313 i_status
= VLC_DEMUXER_EOS
;
1315 /* First pass, find any track within our target increment, ordered by position */
1316 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1318 mp4_track_t
*tk_tmp
= &p_sys
->track
[i_track
];
1319 if( !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
1320 tk_tmp
->i_sample
>= tk_tmp
->i_sample_count
||
1321 (!tk_tmp
->b_selected
&& p_sys
->b_seekable
) )
1324 /* At least still have data to demux on this or next turns */
1325 i_status
= VLC_DEMUXER_SUCCESS
;
1327 if ( MP4_TrackGetDTS( p_demux
, tk_tmp
) <= i_nztime
+ DEMUX_INCREMENT
)
1329 if( tk
== NULL
|| MP4_TrackGetPos( tk_tmp
) < MP4_TrackGetPos( tk
) )
1336 /* Second pass, refine and find any best candidate having a chunk pos closer than
1337 * current candidate (avoids seeks when increment falls between the 2) from
1338 * current position, but within extended interleave time */
1339 for( i_track
= 0; i_max_preload
> 0 && i_track
< p_sys
->i_tracks
; i_track
++ )
1341 mp4_track_t
*tk_tmp
= &p_sys
->track
[i_track
];
1343 !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
1344 (!tk_tmp
->b_selected
&& p_sys
->b_seekable
) ||
1345 tk_tmp
->i_sample
>= tk_tmp
->i_sample_count
)
1348 mtime_t i_nzdts
= MP4_TrackGetDTS( p_demux
, tk_tmp
);
1349 if ( i_nzdts
<= i_nztime
+ DEMUX_TRACK_MAX_PRELOAD
)
1351 /* Found a better candidate to avoid seeking */
1352 if( MP4_TrackGetPos( tk_tmp
) < MP4_TrackGetPos( tk
) )
1354 /* Note: previous candidate will be repicked on next loop */
1358 uint64_t i_pos
= MP4_TrackGetPos( tk
);
1359 int i_ret
= DemuxTrack( p_demux
, tk
, i_pos
, i_max_preload
);
1361 if( i_ret
== VLC_DEMUXER_SUCCESS
)
1362 i_status
= VLC_DEMUXER_SUCCESS
;
1365 if( i_status
!= VLC_DEMUXER_SUCCESS
|| !tk
)
1369 p_sys
->i_nztime
+= DEMUX_INCREMENT
;
1370 if( p_sys
->i_pcr
> VLC_TS_INVALID
)
1372 p_sys
->i_pcr
= VLC_TS_0
+ p_sys
->i_nztime
;
1373 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
1377 MP4_UpdateSeekpoint( p_demux
, i_nztime
+ DEMUX_INCREMENT
);
1382 static int Demux( demux_t
*p_demux
)
1384 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1386 assert( ! p_sys
->b_fragmented
);
1388 int i_status
= DemuxMoov( p_demux
);
1390 if( i_status
== VLC_DEMUXER_EOS
)
1391 i_status
= VLC_DEMUXER_EOF
;
1396 static void MP4_UpdateSeekpoint( demux_t
*p_demux
, int64_t i_time
)
1398 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1400 if( !p_sys
->p_title
)
1402 for( i
= 0; i
< p_sys
->p_title
->i_seekpoint
; i
++ )
1404 if( i_time
< p_sys
->p_title
->seekpoint
[i
]->i_time_offset
)
1409 if( i
!= p_sys
->i_seekpoint
&& i
>= 0 )
1411 p_sys
->i_seekpoint
= i
;
1412 p_demux
->info
.i_update
|= INPUT_UPDATE_SEEKPOINT
;
1415 /*****************************************************************************
1416 * Seek: Go to i_date
1417 ******************************************************************************/
1418 static int Seek( demux_t
*p_demux
, mtime_t i_date
, bool b_accurate
)
1420 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1421 unsigned int i_track
;
1423 /* Now for each stream try to go to this time */
1424 mtime_t i_start
= i_date
;
1425 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1427 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1428 /* FIXME: we should find the lowest time from tracks with indexes.
1429 considering only video for now */
1430 if( tk
->fmt
.i_cat
!= VIDEO_ES
)
1432 if( MP4_TrackSeek( p_demux
, tk
, i_date
) == VLC_SUCCESS
)
1434 mtime_t i_seeked
= MP4_TrackGetDTS( p_demux
, tk
);
1435 if( i_seeked
< i_start
)
1440 msg_Dbg( p_demux
, "seeking with %"PRId64
"ms %s", (i_date
- i_start
) / 1000,
1441 !b_accurate
? "alignment" : "preroll (use input-fast-seek to avoid)" );
1443 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1445 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1446 if( tk
->fmt
.i_cat
== VIDEO_ES
)
1448 MP4_TrackSeek( p_demux
, tk
, i_start
);
1451 MP4_UpdateSeekpoint( p_demux
, i_date
);
1452 MP4ASF_ResetFrames( p_sys
);
1453 /* update global time */
1454 p_sys
->i_nztime
= i_start
;
1455 p_sys
->i_pcr
= VLC_TS_INVALID
;
1458 es_out_Control( p_demux
->out
, ES_OUT_SET_NEXT_DISPLAY_TIME
, i_date
);
1463 static int FragPrepareChunk( demux_t
*p_demux
, MP4_Box_t
*p_moof
,
1464 MP4_Box_t
*p_sidx
, stime_t i_moof_time
, bool b_discontinuity
)
1466 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1468 if( FragCreateTrunIndex( p_demux
, p_moof
, p_sidx
, i_moof_time
, b_discontinuity
) == VLC_SUCCESS
)
1470 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1472 mp4_track_t
*p_track
= &p_sys
->track
[i
];
1473 if( p_track
->context
.runs
.i_count
)
1475 const mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[0];
1476 p_track
->context
.i_trun_sample_pos
= p_run
->i_offset
;
1477 p_track
->context
.i_trun_sample
= 0;
1478 p_track
->i_time
= p_run
->i_first_dts
;
1484 return VLC_EGENERIC
;
1487 static mtime_t
FragGetDemuxTimeFromTracksTime( demux_sys_t
*p_sys
)
1489 mtime_t i_time
= INT64_MAX
;
1490 for( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
1492 if( p_sys
->track
[i
].context
.runs
.i_count
== 0 )
1494 mtime_t i_ttime
= MP4_rescale( p_sys
->track
[i
].i_time
,
1495 p_sys
->track
[i
].i_timescale
, CLOCK_FREQ
);
1496 i_time
= __MIN( i_time
, i_ttime
);
1501 static uint32_t FragGetMoofSequenceNumber( MP4_Box_t
*p_moof
)
1503 const MP4_Box_t
*p_mfhd
= MP4_BoxGet( p_moof
, "mfhd" );
1504 if( p_mfhd
&& BOXDATA(p_mfhd
) )
1505 return BOXDATA(p_mfhd
)->i_sequence_number
;
1509 static int FragSeekLoadFragment( demux_t
*p_demux
, uint32_t i_moox
, stime_t i_moox_time
)
1511 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1514 if( i_moox
== ATOM_moov
)
1516 p_moox
= p_sys
->p_moov
;
1520 const uint8_t *p_peek
;
1521 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) != 8 )
1522 return VLC_EGENERIC
;
1524 if( ATOM_moof
!= VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) )
1525 return VLC_EGENERIC
;
1527 MP4_Box_t
*p_vroot
= MP4_BoxGetNextChunk( p_demux
->s
);
1529 return VLC_EGENERIC
;
1530 p_moox
= MP4_BoxExtract( &p_vroot
->p_first
, ATOM_moof
);
1531 MP4_BoxFree( p_vroot
);
1534 return VLC_EGENERIC
;
1537 FragResetContext( p_sys
);
1540 p_sys
->context
.p_fragment_atom
= p_moox
;
1541 p_sys
->context
.i_current_box_type
= i_moox
;
1543 if( i_moox
== ATOM_moof
)
1545 FragPrepareChunk( p_demux
, p_moox
, NULL
, i_moox_time
, true );
1546 p_sys
->context
.i_lastseqnumber
= FragGetMoofSequenceNumber( p_moox
);
1548 p_sys
->i_nztime
= FragGetDemuxTimeFromTracksTime( p_sys
);
1549 p_sys
->i_pcr
= VLC_TS_INVALID
;
1552 msg_Dbg( p_demux
, "seeked to %4.4s at pos %" PRIu64
, (char *) &i_moox
, p_moox
->i_pos
);
1556 static unsigned GetSeekTrackIndex( demux_sys_t
*p_sys
)
1559 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1561 if( p_sys
->track
[i
].fmt
.i_cat
== VIDEO_ES
||
1562 p_sys
->track
[i
].fmt
.i_cat
== AUDIO_ES
)
1564 if( cand
!= i
&& !p_sys
->track
[cand
].b_selected
)
1571 static void FragTrunSeekToTime( mp4_track_t
*p_track
, stime_t i_target_time
)
1573 if( !p_track
->b_ok
|| p_track
->context
.runs
.i_count
< 1 )
1577 unsigned i_sample
= 0;
1578 uint64_t i_pos
= p_track
->context
.runs
.p_array
[0].i_offset
;
1579 stime_t i_time
= p_track
->context
.runs
.p_array
[0].i_first_dts
;
1581 for( unsigned r
= 0; r
< p_track
->context
.runs
.i_count
; r
++ )
1583 const mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[r
];
1584 const MP4_Box_data_trun_t
*p_data
=
1585 p_track
->context
.runs
.p_array
[r
].p_trun
->data
.p_trun
;
1586 if( i_time
> i_target_time
)
1590 i_time
= p_run
->i_first_dts
;
1591 i_pos
= p_run
->i_offset
;
1594 uint32_t dur
= p_track
->context
.i_default_sample_duration
;
1595 uint32_t len
= p_track
->context
.i_default_sample_size
;
1596 for ( unsigned i
=0; i
<p_data
->i_sample_count
; i
++ )
1598 if( p_data
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
1599 dur
= p_data
->p_samples
[i
].i_duration
;
1601 /* check condition */
1602 if( i_time
+ dur
> i_target_time
)
1605 if( p_data
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
1606 len
= p_data
->p_samples
[i
].i_size
;
1613 p_track
->context
.i_trun_sample
= i_sample
;
1614 p_track
->context
.i_trun_sample_pos
= i_pos
;
1615 p_track
->context
.runs
.i_current
= i_run
;
1618 static int FragSeekToTime( demux_t
*p_demux
, mtime_t i_nztime
, bool b_accurate
)
1620 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1621 uint64_t i64
= UINT64_MAX
;
1622 uint32_t i_segment_type
= ATOM_moof
;
1623 stime_t i_segment_time
= INT64_MAX
;
1624 mtime_t i_sync_time
= i_nztime
;
1625 bool b_iframesync
= false;
1627 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1628 if ( !p_sys
->i_timescale
|| !i_duration
|| !p_sys
->b_seekable
)
1629 return VLC_EGENERIC
;
1631 uint64_t i_backup_pos
= vlc_stream_Tell( p_demux
->s
);
1633 if ( !p_sys
->b_fragments_probed
&& !p_sys
->b_index_probed
&& p_sys
->b_seekable
)
1635 ProbeIndex( p_demux
);
1636 p_sys
->b_index_probed
= true;
1639 const unsigned i_seek_track_index
= GetSeekTrackIndex( p_sys
);
1640 const unsigned i_seek_track_ID
= p_sys
->track
[i_seek_track_index
].i_track_ID
;
1642 if( MP4_rescale( i_nztime
, CLOCK_FREQ
, p_sys
->i_timescale
)
1643 < GetMoovTrackDuration( p_sys
, i_seek_track_ID
) )
1645 i64
= p_sys
->p_moov
->i_pos
;
1646 i_segment_type
= ATOM_moov
;
1648 else if( FragGetMoofBySidxIndex( p_demux
, i_nztime
, &i64
, &i_sync_time
) == VLC_SUCCESS
)
1650 /* provides base offset */
1651 i_segment_time
= i_sync_time
;
1652 msg_Dbg( p_demux
, "seeking to sidx moof pos %" PRId64
" %" PRId64
, i64
, i_sync_time
);
1656 bool b_buildindex
= false;
1658 if( FragGetMoofByTfraIndex( p_demux
, i_nztime
, i_seek_track_ID
, &i64
, &i_sync_time
) == VLC_SUCCESS
)
1660 /* Does only provide segment position and a sync sample time */
1661 msg_Dbg( p_demux
, "seeking to sync point %" PRId64
, i_sync_time
);
1662 b_iframesync
= true;
1664 else if( !p_sys
->b_fragments_probed
&& !p_sys
->b_fastseekable
)
1666 const char *psz_msg
= _(
1667 "Because this file index is broken or missing, "
1668 "seeking will not work correctly.\n"
1669 "VLC won't repair your file but can temporary fix this "
1670 "problem by building an index in memory.\n"
1671 "This step might take a long time on a large file.\n"
1672 "What do you want to do?");
1673 b_buildindex
= vlc_dialog_wait_question( p_demux
,
1674 VLC_DIALOG_QUESTION_NORMAL
,
1678 _("Broken or missing Index"),
1682 if( !p_sys
->b_fragments_probed
&& ( p_sys
->b_fastseekable
|| b_buildindex
) )
1685 int i_ret
= vlc_stream_Seek( p_demux
->s
, p_sys
->p_moov
->i_pos
+ p_sys
->p_moov
->i_size
);
1686 if( i_ret
== VLC_SUCCESS
)
1688 i_ret
= ProbeFragments( p_demux
, true, &foo
);
1689 p_sys
->b_fragments_probed
= true;
1691 if( i_ret
!= VLC_SUCCESS
)
1693 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1698 if( p_sys
->b_fragments_probed
&& p_sys
->p_fragsindex
)
1700 stime_t i_basetime
= MP4_rescale( i_sync_time
, CLOCK_FREQ
, p_sys
->i_timescale
);
1701 if( !MP4_Fragments_Index_Lookup( p_sys
->p_fragsindex
, &i_basetime
, &i64
, i_seek_track_index
) )
1703 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1704 return VLC_EGENERIC
;
1706 msg_Dbg( p_demux
, "seeking to fragment index pos %" PRId64
" %" PRId64
, i64
,
1707 MP4_rescale( i_basetime
, p_sys
->i_timescale
, CLOCK_FREQ
) );
1711 if( i64
== UINT64_MAX
)
1713 msg_Warn( p_demux
, "seek by index failed" );
1714 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1715 return VLC_EGENERIC
;
1718 msg_Dbg( p_demux
, "final seek to fragment at %"PRId64
, i64
);
1719 if( vlc_stream_Seek( p_demux
->s
, i64
) )
1721 msg_Err( p_demux
, "seek failed to %"PRId64
, i64
);
1722 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1723 return VLC_EGENERIC
;
1726 /* Context is killed on success */
1727 if( FragSeekLoadFragment( p_demux
, i_segment_type
, i_segment_time
) != VLC_SUCCESS
)
1729 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1730 return VLC_EGENERIC
;
1733 p_sys
->i_pcr
= VLC_TS_INVALID
;
1735 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1737 if( i_segment_type
== ATOM_moov
)
1739 MP4_TrackSeek( p_demux
, &p_sys
->track
[i
], i_sync_time
);
1740 p_sys
->i_nztime
= i_sync_time
;
1741 p_sys
->i_pcr
= VLC_TS_INVALID
;
1743 else if( b_iframesync
)
1745 stime_t i_tst
= MP4_rescale( i_sync_time
, CLOCK_FREQ
, p_sys
->track
[i
].i_timescale
);
1746 FragTrunSeekToTime( &p_sys
->track
[i
], i_tst
);
1747 p_sys
->track
[i
].i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
1751 MP4ASF_ResetFrames( p_sys
);
1752 /* And set next display time in that trun/fragment */
1753 if( b_iframesync
&& b_accurate
)
1754 es_out_Control( p_demux
->out
, ES_OUT_SET_NEXT_DISPLAY_TIME
, VLC_TS_0
+ i_nztime
);
1758 static int FragSeekToPos( demux_t
*p_demux
, double f
, bool b_accurate
)
1760 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1761 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1763 if ( !p_sys
->b_seekable
|| !p_sys
->i_timescale
|| !i_duration
)
1764 return VLC_EGENERIC
;
1766 return FragSeekToTime( p_demux
, (mtime_t
)( f
*
1767 MP4_rescale( i_duration
, p_sys
->i_timescale
, CLOCK_FREQ
) ), b_accurate
);
1770 static bool imageTypeCompatible( const MP4_Box_data_data_t
*p_data
)
1773 p_data
->e_wellknowntype
== DATA_WKT_PNG
||
1774 p_data
->e_wellknowntype
== DATA_WKT_JPEG
||
1775 p_data
->e_wellknowntype
== DATA_WKT_BMP
);
1778 static int MP4_LoadMeta( demux_sys_t
*p_sys
, vlc_meta_t
*p_meta
)
1780 MP4_Box_t
*p_data
= NULL
;
1781 MP4_Box_t
*p_udta
= NULL
;
1782 bool b_attachment_set
= false;
1785 return VLC_EGENERIC
;
1787 for( int i_index
= 0; psz_meta_roots
[i_index
] && !p_udta
; i_index
++ )
1789 p_udta
= MP4_BoxGet( p_sys
->p_root
, psz_meta_roots
[i_index
] );
1792 p_data
= MP4_BoxGet( p_udta
, "covr/data" );
1793 if ( p_data
&& imageTypeCompatible( BOXDATA(p_data
) ) )
1795 char *psz_attachment
;
1796 if ( -1 != asprintf( &psz_attachment
, "attachment://%s/covr/data[0]",
1797 psz_meta_roots
[i_index
] ) )
1799 vlc_meta_SetArtURL( p_meta
, psz_attachment
);
1800 b_attachment_set
= true;
1801 free( psz_attachment
);
1807 const MP4_Box_t
*p_pnot
;
1808 if ( !b_attachment_set
&& (p_pnot
= MP4_BoxGet( p_sys
->p_root
, "pnot" )) )
1810 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
) && !b_attachment_set
; i
++ )
1812 if ( rgi_pict_atoms
[i
] == BOXDATA(p_pnot
)->i_type
)
1815 snprintf( rgsz_path
, 26, "attachment://%4.4s[%"PRIu16
"]",
1816 (char*)&rgi_pict_atoms
[i
], BOXDATA(p_pnot
)->i_index
- 1 );
1817 vlc_meta_SetArtURL( p_meta
, rgsz_path
);
1818 b_attachment_set
= true;
1823 if( p_udta
== NULL
)
1825 if( !b_attachment_set
)
1826 return VLC_EGENERIC
;
1828 else SetupMeta( p_meta
, p_udta
);
1833 /*****************************************************************************
1835 *****************************************************************************/
1836 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
1838 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1844 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1848 case DEMUX_CAN_SEEK
:
1849 *va_arg( args
, bool * ) = p_sys
->b_seekable
;
1852 case DEMUX_GET_POSITION
:
1853 pf
= va_arg( args
, double * );
1854 if( i_duration
> 0 )
1856 *pf
= (double)p_sys
->i_nztime
/
1857 MP4_rescale( i_duration
, p_sys
->i_timescale
, CLOCK_FREQ
);
1865 case DEMUX_GET_SEEKPOINT
:
1866 *va_arg( args
, int * ) = p_sys
->i_seekpoint
;
1869 case DEMUX_SET_POSITION
:
1870 f
= va_arg( args
, double );
1871 b
= va_arg( args
, int );
1872 if ( p_demux
->pf_demux
== DemuxFrag
)
1873 return FragSeekToPos( p_demux
, f
, b
);
1874 else if( p_sys
->i_timescale
> 0 )
1876 i64
= (int64_t)( f
* MP4_rescale( p_sys
->i_duration
,
1877 p_sys
->i_timescale
, CLOCK_FREQ
) );
1878 return Seek( p_demux
, i64
, b
);
1880 else return VLC_EGENERIC
;
1882 case DEMUX_GET_TIME
:
1883 pi64
= va_arg( args
, int64_t * );
1884 if( p_sys
->i_timescale
> 0 )
1885 *pi64
= p_sys
->i_nztime
;
1890 case DEMUX_SET_TIME
:
1891 i64
= va_arg( args
, int64_t );
1892 b
= va_arg( args
, int );
1893 if ( p_demux
->pf_demux
== DemuxFrag
)
1894 return FragSeekToTime( p_demux
, i64
, b
);
1896 return Seek( p_demux
, i64
, b
);
1898 case DEMUX_GET_LENGTH
:
1899 pi64
= va_arg( args
, int64_t * );
1900 if( p_sys
->i_timescale
> 0 )
1902 *pi64
= MP4_rescale( i_duration
,
1903 p_sys
->i_timescale
, CLOCK_FREQ
);
1909 pf
= va_arg( args
, double * );
1913 case DEMUX_GET_ATTACHMENTS
:
1915 input_attachment_t
***ppp_attach
= va_arg( args
, input_attachment_t
*** );
1916 int *pi_int
= va_arg( args
, int * );
1918 MP4_Box_t
*p_udta
= NULL
;
1922 /* Count number of total attachments */
1923 for( ; psz_meta_roots
[i_index
] && !p_udta
; i_index
++ )
1925 p_udta
= MP4_BoxGet( p_sys
->p_root
, psz_meta_roots
[i_index
] );
1927 i_count
+= MP4_BoxCount( p_udta
, "covr/data" );
1930 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
); i
++ )
1933 snprintf( rgsz_path
, 5, "%4.4s", (char*)&rgi_pict_atoms
[i
] );
1934 i_count
+= MP4_BoxCount( p_sys
->p_root
, rgsz_path
);
1938 return VLC_EGENERIC
;
1940 *ppp_attach
= (input_attachment_t
**)
1941 vlc_alloc( i_count
, sizeof(input_attachment_t
*) );
1942 if( !(*ppp_attach
) ) return VLC_ENOMEM
;
1944 /* First add cover attachments */
1946 size_t i_box_count
= 0;
1949 const MP4_Box_t
*p_data
= MP4_BoxGet( p_udta
, "covr/data" );
1950 for( ; p_data
; p_data
= p_data
->p_next
)
1956 if ( p_data
->i_type
!= ATOM_data
|| !imageTypeCompatible( BOXDATA(p_data
) ) )
1959 switch( BOXDATA(p_data
)->e_wellknowntype
)
1962 psz_mime
= strdup( "image/png" );
1965 psz_mime
= strdup( "image/jpeg" );
1968 psz_mime
= strdup( "image/bmp" );
1974 if ( asprintf( &psz_filename
, "%s/covr/data[%"PRIu64
"]", psz_meta_roots
[i_index
- 1],
1975 (uint64_t) i_box_count
- 1 ) >= 0 )
1977 (*ppp_attach
)[i_count
++] =
1978 vlc_input_attachment_New( psz_filename
, psz_mime
, "Cover picture",
1979 BOXDATA(p_data
)->p_blob
, BOXDATA(p_data
)->i_blob
);
1980 msg_Dbg( p_demux
, "adding attachment %s", psz_filename
);
1981 free( psz_filename
);
1988 /* Then quickdraw pict ones */
1989 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
); i
++ )
1992 snprintf( rgsz_path
, 5, "%4.4s", (char*)&rgi_pict_atoms
[i
] );
1993 const MP4_Box_t
*p_pict
= MP4_BoxGet( p_sys
->p_root
, rgsz_path
);
1995 for( ; p_pict
; p_pict
= p_pict
->p_next
)
1997 if ( i_box_count
++ == UINT16_MAX
) /* pnot only handles 2^16 */
1999 if ( p_pict
->i_type
!= rgi_pict_atoms
[i
] )
2001 char rgsz_location
[12];
2002 snprintf( rgsz_location
, 12, "%4.4s[%"PRIu16
"]", (char*)&rgi_pict_atoms
[i
],
2003 (uint16_t) i_box_count
- 1 );
2004 (*ppp_attach
)[i_count
] = vlc_input_attachment_New( rgsz_location
, "image/x-pict",
2005 "Quickdraw image", p_pict
->data
.p_binary
->p_blob
, p_pict
->data
.p_binary
->i_blob
);
2006 if ( !(*ppp_attach
)[i_count
] )
2012 msg_Dbg( p_demux
, "adding attachment %s", rgsz_location
);
2018 free( *ppp_attach
);
2019 return VLC_EGENERIC
;
2027 case DEMUX_GET_META
:
2029 vlc_meta_t
*p_meta
= va_arg( args
, vlc_meta_t
*);
2031 if( !p_sys
->p_meta
)
2032 return VLC_EGENERIC
;
2034 vlc_meta_Merge( p_meta
, p_sys
->p_meta
);
2039 case DEMUX_GET_TITLE_INFO
:
2041 input_title_t
***ppp_title
= va_arg( args
, input_title_t
*** );
2042 int *pi_int
= va_arg( args
, int* );
2043 int *pi_title_offset
= va_arg( args
, int* );
2044 int *pi_seekpoint_offset
= va_arg( args
, int* );
2046 if( !p_sys
->p_title
)
2047 return VLC_EGENERIC
;
2050 *ppp_title
= malloc( sizeof( input_title_t
*) );
2051 (*ppp_title
)[0] = vlc_input_title_Duplicate( p_sys
->p_title
);
2052 *pi_title_offset
= 0;
2053 *pi_seekpoint_offset
= 0;
2056 case DEMUX_SET_TITLE
:
2058 const int i_title
= va_arg( args
, int );
2059 if( !p_sys
->p_title
|| i_title
!= 0 )
2060 return VLC_EGENERIC
;
2063 case DEMUX_SET_SEEKPOINT
:
2065 const int i_seekpoint
= va_arg( args
, int );
2066 if( !p_sys
->p_title
)
2067 return VLC_EGENERIC
;
2068 return Seek( p_demux
, p_sys
->p_title
->seekpoint
[i_seekpoint
]->i_time_offset
, true );
2070 case DEMUX_GET_PTS_DELAY
:
2072 for( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
2074 const MP4_Box_t
*p_load
;
2075 if ( (p_load
= MP4_BoxGet( p_sys
->track
[i
].p_track
, "load" )) &&
2076 BOXDATA(p_load
)->i_duration
> 0 )
2078 *va_arg(args
, int64_t *) =
2079 MP4_rescale( BOXDATA(p_load
)->i_duration
,
2080 p_sys
->track
[i
].i_timescale
, CLOCK_FREQ
);
2084 return VLC_EGENERIC
;
2086 case DEMUX_SET_NEXT_DEMUX_TIME
:
2087 case DEMUX_SET_GROUP
:
2088 case DEMUX_HAS_UNSUPPORTED_META
:
2089 case DEMUX_CAN_RECORD
:
2091 return VLC_EGENERIC
;
2095 /*****************************************************************************
2096 * Close: frees unused data
2097 *****************************************************************************/
2098 static void Close ( vlc_object_t
* p_this
)
2100 demux_t
* p_demux
= (demux_t
*)p_this
;
2101 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2102 unsigned int i_track
;
2104 msg_Dbg( p_demux
, "freeing all memory" );
2106 FragResetContext( p_sys
);
2108 MP4_BoxFree( p_sys
->p_root
);
2110 if( p_sys
->p_title
)
2111 vlc_input_title_Delete( p_sys
->p_title
);
2114 vlc_meta_Delete( p_sys
->p_meta
);
2116 MP4_Fragments_Index_Delete( p_sys
->p_fragsindex
);
2118 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
2119 MP4_TrackClean( p_demux
->out
, &p_sys
->track
[i_track
] );
2120 free( p_sys
->track
);
2127 /****************************************************************************
2128 * Local functions, specific to vlc
2129 ****************************************************************************/
2131 static void LoadChapterGpac( demux_t
*p_demux
, MP4_Box_t
*p_chpl
)
2133 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2135 if( BOXDATA(p_chpl
)->i_chapter
== 0 )
2138 p_sys
->p_title
= vlc_input_title_New();
2139 for( int i
= 0; i
< BOXDATA(p_chpl
)->i_chapter
&& p_sys
->p_title
; i
++ )
2141 seekpoint_t
*s
= vlc_seekpoint_New();
2142 if( s
== NULL
) continue;
2144 s
->psz_name
= strdup( BOXDATA(p_chpl
)->chapter
[i
].psz_name
);
2145 if( s
->psz_name
== NULL
)
2147 vlc_seekpoint_Delete( s
);;
2151 EnsureUTF8( s
->psz_name
);
2152 s
->i_time_offset
= BOXDATA(p_chpl
)->chapter
[i
].i_start
/ 10;
2153 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2156 static void LoadChapterGoPro( demux_t
*p_demux
, MP4_Box_t
*p_hmmt
)
2158 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2160 p_sys
->p_title
= vlc_input_title_New();
2161 if( p_sys
->p_title
)
2162 for( unsigned i
= 0; i
< BOXDATA(p_hmmt
)->i_chapter_count
; i
++ )
2164 seekpoint_t
*s
= vlc_seekpoint_New();
2167 if( asprintf( &s
->psz_name
, "HiLight tag #%u", i
+1 ) != -1 )
2168 EnsureUTF8( s
->psz_name
);
2170 /* HiLights are stored in ms so we convert them to µs */
2171 s
->i_time_offset
= BOXDATA(p_hmmt
)->pi_chapter_start
[i
] * 1000;
2172 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2176 static void LoadChapterApple( demux_t
*p_demux
, mp4_track_t
*tk
)
2178 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2180 for( tk
->i_sample
= 0; tk
->i_sample
< tk
->i_sample_count
; tk
->i_sample
++ )
2182 const int64_t i_dts
= MP4_TrackGetDTS( p_demux
, tk
);
2183 int64_t i_pts_delta
;
2184 if ( !MP4_TrackGetPTSDelta( p_demux
, tk
, &i_pts_delta
) )
2186 uint32_t i_nb_samples
= 0;
2187 const uint32_t i_size
= MP4_TrackGetReadSize( tk
, &i_nb_samples
);
2189 if( i_size
> 0 && !vlc_stream_Seek( p_demux
->s
, MP4_TrackGetPos( tk
) ) )
2192 const uint32_t i_read
= stream_ReadU32( p_demux
->s
, p_buffer
,
2193 __MIN( sizeof(p_buffer
), i_size
) );
2196 const uint32_t i_string
= __MIN( GetWBE(p_buffer
), i_read
-2 );
2197 const char *psnz_string
= &p_buffer
[2];
2199 seekpoint_t
*s
= vlc_seekpoint_New();
2200 if( s
== NULL
) continue;
2202 if( i_string
> 1 && !memcmp( psnz_string
, "\xFF\xFE", 2 ) )
2203 s
->psz_name
= FromCharset( "UTF-16LE", psnz_string
, i_string
);
2205 s
->psz_name
= strndup( psnz_string
, i_string
);
2207 if( s
->psz_name
== NULL
)
2209 vlc_seekpoint_Delete( s
);
2213 EnsureUTF8( s
->psz_name
);
2214 s
->i_time_offset
= i_dts
+ __MAX( i_pts_delta
, 0 );
2216 if( !p_sys
->p_title
)
2217 p_sys
->p_title
= vlc_input_title_New();
2218 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2221 if( tk
->i_sample
+1 >= tk
->chunk
[tk
->i_chunk
].i_sample_first
+
2222 tk
->chunk
[tk
->i_chunk
].i_sample_count
)
2226 static void LoadChapter( demux_t
*p_demux
)
2228 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2232 if( ( p_chpl
= MP4_BoxGet( p_sys
->p_root
, "/moov/udta/chpl" ) ) &&
2233 BOXDATA(p_chpl
) && BOXDATA(p_chpl
)->i_chapter
> 0 )
2235 LoadChapterGpac( p_demux
, p_chpl
);
2237 else if( ( p_hmmt
= MP4_BoxGet( p_sys
->p_root
, "/moov/udta/HMMT" ) ) &&
2238 BOXDATA(p_hmmt
) && BOXDATA(p_hmmt
)->pi_chapter_start
&& BOXDATA(p_hmmt
)->i_chapter_count
> 0 )
2240 LoadChapterGoPro( p_demux
, p_hmmt
);
2242 else if( p_sys
->p_tref_chap
)
2244 MP4_Box_data_tref_generic_t
*p_chap
= p_sys
->p_tref_chap
->data
.p_tref_generic
;
2247 /* Load the first subtitle track like quicktime */
2248 for( i
= 0; i
< p_chap
->i_entry_count
; i
++ )
2250 for( j
= 0; j
< p_sys
->i_tracks
; j
++ )
2252 mp4_track_t
*tk
= &p_sys
->track
[j
];
2253 if( tk
->b_ok
&& tk
->i_track_ID
== p_chap
->i_track_ID
[i
] &&
2254 tk
->fmt
.i_cat
== SPU_ES
&& tk
->fmt
.i_codec
== VLC_CODEC_TX3G
)
2257 if( j
< p_sys
->i_tracks
)
2259 LoadChapterApple( p_demux
, &p_sys
->track
[j
] );
2265 /* Add duration if titles are enabled */
2266 if( p_sys
->p_title
)
2268 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
2269 p_sys
->p_title
->i_length
=
2270 MP4_rescale( i_duration
,
2271 p_sys
->i_timescale
, CLOCK_FREQ
);
2275 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
2276 static int TrackCreateChunksIndex( demux_t
*p_demux
,
2277 mp4_track_t
*p_demux_track
)
2279 MP4_Box_t
*p_co64
; /* give offset for each chunk, same for stco and co64 */
2282 unsigned int i_chunk
;
2283 unsigned int i_index
, i_last
;
2285 if( ( !(p_co64
= MP4_BoxGet( p_demux_track
->p_stbl
, "stco" ) )&&
2286 !(p_co64
= MP4_BoxGet( p_demux_track
->p_stbl
, "co64" ) ) )||
2287 ( !(p_stsc
= MP4_BoxGet( p_demux_track
->p_stbl
, "stsc" ) ) ))
2289 return( VLC_EGENERIC
);
2292 p_demux_track
->i_chunk_count
= BOXDATA(p_co64
)->i_entry_count
;
2293 if( !p_demux_track
->i_chunk_count
)
2295 msg_Warn( p_demux
, "no chunk defined" );
2297 p_demux_track
->chunk
= calloc( p_demux_track
->i_chunk_count
,
2298 sizeof( mp4_chunk_t
) );
2299 if( p_demux_track
->chunk
== NULL
)
2304 /* first we read chunk offset */
2305 for( i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2307 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2309 ck
->i_offset
= BOXDATA(p_co64
)->i_chunk_offset
[i_chunk
];
2311 ck
->i_first_dts
= 0;
2312 ck
->i_entries_dts
= 0;
2313 ck
->p_sample_count_dts
= NULL
;
2314 ck
->p_sample_delta_dts
= NULL
;
2315 ck
->i_entries_pts
= 0;
2316 ck
->p_sample_count_pts
= NULL
;
2317 ck
->p_sample_offset_pts
= NULL
;
2320 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
2321 to be used for the sample XXX begin to 1
2322 We construct it begining at the end */
2323 i_last
= p_demux_track
->i_chunk_count
; /* last chunk proceded */
2324 i_index
= BOXDATA(p_stsc
)->i_entry_count
;
2326 while( i_index
-- > 0 )
2328 for( i_chunk
= BOXDATA(p_stsc
)->i_first_chunk
[i_index
] - 1;
2329 i_chunk
< i_last
; i_chunk
++ )
2331 if( i_chunk
>= p_demux_track
->i_chunk_count
)
2333 msg_Warn( p_demux
, "corrupted chunk table" );
2334 return VLC_EGENERIC
;
2337 p_demux_track
->chunk
[i_chunk
].i_sample_description_index
=
2338 BOXDATA(p_stsc
)->i_sample_description_index
[i_index
];
2339 p_demux_track
->chunk
[i_chunk
].i_sample_count
=
2340 BOXDATA(p_stsc
)->i_samples_per_chunk
[i_index
];
2342 i_last
= BOXDATA(p_stsc
)->i_first_chunk
[i_index
] - 1;
2345 p_demux_track
->i_sample_count
= 0;
2346 bool b_broken
= false;
2347 if ( p_demux_track
->i_chunk_count
)
2349 p_demux_track
->chunk
[0].i_sample_first
= 0;
2350 p_demux_track
->i_sample_count
+= p_demux_track
->chunk
[0].i_sample_count
;
2352 const mp4_chunk_t
*prev
= &p_demux_track
->chunk
[0];
2353 for( i_chunk
= 1; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2355 mp4_chunk_t
*cur
= &p_demux_track
->chunk
[i_chunk
];
2356 if( unlikely(UINT32_MAX
- cur
->i_sample_count
< p_demux_track
->i_sample_count
) )
2361 p_demux_track
->i_sample_count
+= cur
->i_sample_count
;
2362 cur
->i_sample_first
= prev
->i_sample_first
+ prev
->i_sample_count
;
2367 if( unlikely(b_broken
) )
2369 msg_Err( p_demux
, "Overflow in chunks total samples count" );
2370 return VLC_EGENERIC
;
2373 msg_Dbg( p_demux
, "track[Id 0x%x] read %d chunk",
2374 p_demux_track
->i_track_ID
, p_demux_track
->i_chunk_count
);
2379 static int xTTS_CountEntries( demux_t
*p_demux
, uint32_t *pi_entry
/* out */,
2380 const uint32_t i_index
,
2381 uint32_t i_index_samples_left
,
2382 uint32_t i_sample_count
,
2383 const uint32_t *pi_index_sample_count
,
2384 const uint32_t i_table_count
)
2386 uint32_t i_array_offset
;
2387 while( i_sample_count
> 0 )
2389 if ( likely((UINT32_MAX
- i_index
) >= *pi_entry
) )
2390 i_array_offset
= i_index
+ *pi_entry
;
2392 return VLC_EGENERIC
;
2394 if ( i_array_offset
>= i_table_count
)
2396 msg_Err( p_demux
, "invalid index counting total samples %u %u", i_array_offset
, i_table_count
);
2400 if ( i_index_samples_left
)
2402 if ( i_index_samples_left
> i_sample_count
)
2404 i_index_samples_left
-= i_sample_count
;
2406 *pi_entry
+=1; /* No samples left, go copy */
2411 i_sample_count
-= i_index_samples_left
;
2412 i_index_samples_left
= 0;
2419 i_sample_count
-= __MIN( i_sample_count
, pi_index_sample_count
[i_array_offset
] );
2427 static int TrackCreateSamplesIndex( demux_t
*p_demux
,
2428 mp4_track_t
*p_demux_track
)
2431 MP4_Box_data_stsz_t
*stsz
;
2432 /* TODO use also stss and stsh table for seeking */
2433 /* FIXME use edit table */
2436 * Gives the sample size for each samples. There is also a stz2 table
2437 * (compressed form) that we need to implement TODO */
2438 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "stsz" );
2441 /* FIXME and stz2 */
2442 msg_Warn( p_demux
, "cannot find STSZ box" );
2443 return VLC_EGENERIC
;
2445 stsz
= p_box
->data
.p_stsz
;
2447 /* Use stsz table to create a sample number -> sample size table */
2448 if( p_demux_track
->i_sample_count
!= stsz
->i_sample_count
)
2450 msg_Warn( p_demux
, "Incorrect total samples stsc %" PRIu32
" <> stsz %"PRIu32
", "
2451 " expect truncated media playback",
2452 p_demux_track
->i_sample_count
, stsz
->i_sample_count
);
2453 p_demux_track
->i_sample_count
= __MIN(p_demux_track
->i_sample_count
, stsz
->i_sample_count
);
2456 if( stsz
->i_sample_size
)
2458 /* 1: all sample have the same size, so no need to construct a table */
2459 p_demux_track
->i_sample_size
= stsz
->i_sample_size
;
2460 p_demux_track
->p_sample_size
= NULL
;
2464 /* 2: each sample can have a different size */
2465 p_demux_track
->i_sample_size
= 0;
2466 p_demux_track
->p_sample_size
=
2467 calloc( p_demux_track
->i_sample_count
, sizeof( uint32_t ) );
2468 if( p_demux_track
->p_sample_size
== NULL
)
2471 for( uint32_t i_sample
= 0; i_sample
< p_demux_track
->i_sample_count
; i_sample
++ )
2473 p_demux_track
->p_sample_size
[i_sample
] =
2474 stsz
->i_entry_size
[i_sample
];
2478 if ( p_demux_track
->i_chunk_count
&& p_demux_track
->i_sample_size
== 0 )
2480 const mp4_chunk_t
*lastchunk
= &p_demux_track
->chunk
[p_demux_track
->i_chunk_count
- 1];
2481 if( (uint64_t)lastchunk
->i_sample_count
+ p_demux_track
->i_chunk_count
- 1 > stsz
->i_sample_count
)
2483 msg_Err( p_demux
, "invalid samples table: stsz table is too small" );
2484 return VLC_EGENERIC
;
2488 /* Use stts table to create a sample number -> dts table.
2489 * XXX: if we don't want to waste too much memory, we can't expand
2490 * the box! so each chunk will contain an "extract" of this table
2491 * for fast research (problem with raw stream where a sample is sometime
2492 * just channels*bits_per_sample/8 */
2494 /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
2495 * differing in 2/2 fields and 1 signedness */
2497 mtime_t i_next_dts
= 0;
2499 * Gives mapping between sample and decoding time
2501 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "stts" );
2504 msg_Warn( p_demux
, "cannot find STTS box" );
2505 return VLC_EGENERIC
;
2509 MP4_Box_data_stts_t
*stts
= p_box
->data
.p_stts
;
2511 msg_Warn( p_demux
, "STTS table of %"PRIu32
" entries", stts
->i_entry_count
);
2513 /* Create sample -> dts table per chunk */
2514 uint32_t i_index
= 0;
2515 uint32_t i_current_index_samples_left
= 0;
2517 for( uint32_t i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2519 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2520 uint32_t i_sample_count
;
2522 /* save first dts */
2523 ck
->i_first_dts
= i_next_dts
;
2525 /* count how many entries are needed for this chunk
2526 * for p_sample_delta_dts and p_sample_count_dts */
2527 ck
->i_entries_dts
= 0;
2529 int i_ret
= xTTS_CountEntries( p_demux
, &ck
->i_entries_dts
, i_index
,
2530 i_current_index_samples_left
,
2532 stts
->pi_sample_count
,
2533 stts
->i_entry_count
);
2534 if ( i_ret
== VLC_EGENERIC
)
2538 ck
->p_sample_count_dts
= calloc( ck
->i_entries_dts
, sizeof( uint32_t ) );
2539 ck
->p_sample_delta_dts
= calloc( ck
->i_entries_dts
, sizeof( uint32_t ) );
2540 if( !ck
->p_sample_count_dts
|| !ck
->p_sample_delta_dts
)
2542 free( ck
->p_sample_count_dts
);
2543 free( ck
->p_sample_delta_dts
);
2544 msg_Err( p_demux
, "can't allocate memory for i_entry=%"PRIu32
, ck
->i_entries_dts
);
2545 ck
->i_entries_dts
= 0;
2550 i_sample_count
= ck
->i_sample_count
;
2552 for( uint32_t i
= 0; i
< ck
->i_entries_dts
; i
++ )
2554 if ( i_current_index_samples_left
)
2556 if ( i_current_index_samples_left
> i_sample_count
)
2558 ck
->p_sample_count_dts
[i
] = i_sample_count
;
2559 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2560 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2561 if ( i_sample_count
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2562 i_current_index_samples_left
-= i_sample_count
;
2564 assert( i
== ck
->i_entries_dts
- 1 );
2569 ck
->p_sample_count_dts
[i
] = i_current_index_samples_left
;
2570 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2571 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2572 if ( i_current_index_samples_left
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2573 i_sample_count
-= i_current_index_samples_left
;
2574 i_current_index_samples_left
= 0;
2580 if ( stts
->pi_sample_count
[i_index
] > i_sample_count
)
2582 ck
->p_sample_count_dts
[i
] = i_sample_count
;
2583 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2584 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2585 if ( i_sample_count
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2586 i_current_index_samples_left
= stts
->pi_sample_count
[i_index
] - i_sample_count
;
2588 assert( i
== ck
->i_entries_dts
- 1 );
2589 // keep building from same index
2593 ck
->p_sample_count_dts
[i
] = stts
->pi_sample_count
[i_index
];
2594 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2595 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2596 if ( stts
->pi_sample_count
[i_index
] ) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2597 i_sample_count
-= stts
->pi_sample_count
[i_index
];
2608 * Gives the delta between decoding time (dts) and composition table (pts)
2610 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "ctts" );
2611 if( p_box
&& p_box
->data
.p_ctts
)
2613 MP4_Box_data_ctts_t
*ctts
= p_box
->data
.p_ctts
;
2615 msg_Warn( p_demux
, "CTTS table of %"PRIu32
" entries", ctts
->i_entry_count
);
2617 int64_t i_cts_shift
= 0;
2618 const MP4_Box_t
*p_cslg
= MP4_BoxGet( p_demux_track
->p_stbl
, "cslg" );
2619 if( p_cslg
&& BOXDATA(p_cslg
) )
2620 i_cts_shift
= BOXDATA(p_cslg
)->ct_to_dts_shift
;
2622 /* Create pts-dts table per chunk */
2623 uint32_t i_index
= 0;
2624 uint32_t i_current_index_samples_left
= 0;
2626 for( uint32_t i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2628 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2629 uint32_t i_sample_count
;
2631 /* count how many entries are needed for this chunk
2632 * for p_sample_offset_pts and p_sample_count_pts */
2633 ck
->i_entries_pts
= 0;
2634 int i_ret
= xTTS_CountEntries( p_demux
, &ck
->i_entries_pts
, i_index
,
2635 i_current_index_samples_left
,
2637 ctts
->pi_sample_count
,
2638 ctts
->i_entry_count
);
2639 if ( i_ret
== VLC_EGENERIC
)
2643 ck
->p_sample_count_pts
= calloc( ck
->i_entries_pts
, sizeof( uint32_t ) );
2644 ck
->p_sample_offset_pts
= calloc( ck
->i_entries_pts
, sizeof( int32_t ) );
2645 if( !ck
->p_sample_count_pts
|| !ck
->p_sample_offset_pts
)
2647 free( ck
->p_sample_count_pts
);
2648 free( ck
->p_sample_offset_pts
);
2649 msg_Err( p_demux
, "can't allocate memory for i_entry=%"PRIu32
, ck
->i_entries_pts
);
2650 ck
->i_entries_pts
= 0;
2655 i_sample_count
= ck
->i_sample_count
;
2657 for( uint32_t i
= 0; i
< ck
->i_entries_pts
; i
++ )
2659 if ( i_current_index_samples_left
)
2661 if ( i_current_index_samples_left
> i_sample_count
)
2663 ck
->p_sample_count_pts
[i
] = i_sample_count
;
2664 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2665 i_current_index_samples_left
-= i_sample_count
;
2667 assert( i
== ck
->i_entries_pts
- 1 );
2672 ck
->p_sample_count_pts
[i
] = i_current_index_samples_left
;
2673 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2674 i_sample_count
-= i_current_index_samples_left
;
2675 i_current_index_samples_left
= 0;
2681 if ( ctts
->pi_sample_count
[i_index
] > i_sample_count
)
2683 ck
->p_sample_count_pts
[i
] = i_sample_count
;
2684 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2685 i_current_index_samples_left
= ctts
->pi_sample_count
[i_index
] - i_sample_count
;
2687 assert( i
== ck
->i_entries_pts
- 1 );
2688 // keep building from same index
2692 ck
->p_sample_count_pts
[i
] = ctts
->pi_sample_count
[i_index
];
2693 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2694 i_sample_count
-= ctts
->pi_sample_count
[i_index
];
2704 msg_Dbg( p_demux
, "track[Id 0x%x] read %"PRIu32
" samples length:%"PRId64
"s",
2705 p_demux_track
->i_track_ID
, p_demux_track
->i_sample_count
,
2706 i_next_dts
/ p_demux_track
->i_timescale
);
2713 * It computes the sample rate for a video track using the given sample
2716 static void TrackGetESSampleRate( demux_t
*p_demux
,
2717 unsigned *pi_num
, unsigned *pi_den
,
2718 const mp4_track_t
*p_track
,
2719 unsigned i_sd_index
,
2722 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2726 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( MP4_BoxGet( p_sys
->p_root
,
2728 p_track
->i_track_ID
);
2729 MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_trak
, "mdia/mdhd" );
2730 if ( p_mdhd
&& BOXDATA(p_mdhd
) )
2732 vlc_ureduce( pi_num
, pi_den
,
2733 (uint64_t) BOXDATA(p_mdhd
)->i_timescale
* p_track
->i_sample_count
,
2734 (uint64_t) BOXDATA(p_mdhd
)->i_duration
,
2739 if( p_track
->i_chunk_count
== 0 )
2743 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[i_chunk
];
2744 while( p_chunk
> &p_track
->chunk
[0] &&
2745 p_chunk
[-1].i_sample_description_index
== i_sd_index
)
2750 uint64_t i_sample
= 0;
2751 uint64_t i_total_duration
= 0;
2754 i_sample
+= p_chunk
->i_sample_count
;
2755 i_total_duration
+= p_chunk
->i_duration
;
2758 while( p_chunk
< &p_track
->chunk
[p_track
->i_chunk_count
] &&
2759 p_chunk
->i_sample_description_index
== i_sd_index
);
2761 if( i_sample
> 0 && i_total_duration
)
2762 vlc_ureduce( pi_num
, pi_den
,
2763 i_sample
* p_track
->i_timescale
,
2770 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
2772 static int TrackCreateES( demux_t
*p_demux
, mp4_track_t
*p_track
,
2773 unsigned int i_chunk
, es_out_id_t
**pp_es
)
2775 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2776 unsigned int i_sample_description_index
;
2778 if( p_sys
->b_fragmented
|| p_track
->i_chunk_count
== 0 )
2779 i_sample_description_index
= 1; /* XXX */
2781 i_sample_description_index
=
2782 p_track
->chunk
[i_chunk
].i_sample_description_index
;
2787 if( !i_sample_description_index
)
2789 msg_Warn( p_demux
, "invalid SampleEntry index (track[Id 0x%x])",
2790 p_track
->i_track_ID
);
2791 return VLC_EGENERIC
;
2794 MP4_Box_t
*p_sample
= MP4_BoxGet( p_track
->p_stsd
, "[%d]",
2795 i_sample_description_index
- 1 );
2798 ( !p_sample
->data
.p_payload
&& p_track
->fmt
.i_cat
!= SPU_ES
) )
2800 msg_Warn( p_demux
, "cannot find SampleEntry (track[Id 0x%x])",
2801 p_track
->i_track_ID
);
2802 return VLC_EGENERIC
;
2805 p_track
->p_sample
= p_sample
;
2808 if( ( p_frma
= MP4_BoxGet( p_track
->p_sample
, "sinf/frma" ) ) && p_frma
->data
.p_frma
)
2810 msg_Warn( p_demux
, "Original Format Box: %4.4s", (char *)&p_frma
->data
.p_frma
->i_type
);
2812 p_sample
->i_type
= p_frma
->data
.p_frma
->i_type
;
2816 switch( p_track
->fmt
.i_cat
)
2819 if ( p_sample
->i_handler
!= ATOM_vide
||
2820 !SetupVideoES( p_demux
, p_track
, p_sample
) )
2821 return VLC_EGENERIC
;
2823 /* Set frame rate */
2824 TrackGetESSampleRate( p_demux
,
2825 &p_track
->fmt
.video
.i_frame_rate
,
2826 &p_track
->fmt
.video
.i_frame_rate_base
,
2827 p_track
, i_sample_description_index
, i_chunk
);
2829 p_sys
->f_fps
= (float)p_track
->fmt
.video
.i_frame_rate
/
2830 (float)p_track
->fmt
.video
.i_frame_rate_base
;
2835 if ( p_sample
->i_handler
!= ATOM_soun
||
2836 !SetupAudioES( p_demux
, p_track
, p_sample
) )
2837 return VLC_EGENERIC
;
2840 audio_replay_gain_t
*p_arg
= &p_track
->fmt
.audio_replay_gain
;
2841 const char *psz_meta
= vlc_meta_GetExtra( p_sys
->p_meta
, "replaygain_track_gain" );
2844 double f_gain
= us_atof( psz_meta
);
2845 p_arg
->pf_gain
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
;
2846 p_arg
->pb_gain
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
!= 0;
2848 psz_meta
= vlc_meta_GetExtra( p_sys
->p_meta
, "replaygain_track_peak" );
2851 double f_gain
= us_atof( psz_meta
);
2852 p_arg
->pf_peak
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
;
2853 p_arg
->pb_peak
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
> 0;
2859 if ( p_sample
->i_handler
!= ATOM_text
||
2860 !SetupSpuES( p_demux
, p_track
, p_sample
) )
2861 return VLC_EGENERIC
;
2869 *pp_es
= MP4_AddTrackES( p_demux
->out
, p_track
);
2871 return ( !pp_es
|| *pp_es
) ? VLC_SUCCESS
: VLC_EGENERIC
;
2874 /* *** Try to find nearest sync points *** */
2875 static int TrackGetNearestSeekPoint( demux_t
*p_demux
, mp4_track_t
*p_track
,
2876 uint32_t i_sample
, uint32_t *pi_sync_sample
)
2878 int i_ret
= VLC_EGENERIC
;
2879 *pi_sync_sample
= 0;
2881 const MP4_Box_t
*p_stss
;
2882 if( ( p_stss
= MP4_BoxGet( p_track
->p_stbl
, "stss" ) ) )
2884 const MP4_Box_data_stss_t
*p_stss_data
= BOXDATA(p_stss
);
2885 msg_Dbg( p_demux
, "track[Id 0x%x] using Sync Sample Box (stss)",
2886 p_track
->i_track_ID
);
2887 for( unsigned i_index
= 0; i_index
< p_stss_data
->i_entry_count
; i_index
++ )
2889 if( i_index
>= p_stss_data
->i_entry_count
- 1 ||
2890 i_sample
< p_stss_data
->i_sample_number
[i_index
+1] )
2892 *pi_sync_sample
= p_stss_data
->i_sample_number
[i_index
];
2893 msg_Dbg( p_demux
, "stss gives %d --> %" PRIu32
" (sample number)",
2894 i_sample
, *pi_sync_sample
);
2895 i_ret
= VLC_SUCCESS
;
2901 /* try rap samples groups */
2902 const MP4_Box_t
*p_sbgp
= MP4_BoxGet( p_track
->p_stbl
, "sbgp" );
2903 for( ; p_sbgp
; p_sbgp
= p_sbgp
->p_next
)
2905 const MP4_Box_data_sbgp_t
*p_sbgp_data
= BOXDATA(p_sbgp
);
2906 if( p_sbgp
->i_type
!= ATOM_sbgp
|| !p_sbgp_data
)
2909 if( p_sbgp_data
->i_grouping_type
== SAMPLEGROUP_rap
)
2911 uint32_t i_group_sample
= 0;
2912 for ( uint32_t i
=0; i
<p_sbgp_data
->i_entry_count
; i
++ )
2914 /* Sample belongs to rap group ? */
2915 if( p_sbgp_data
->entries
.pi_group_description_index
[i
] != 0 )
2917 if( i_sample
< i_group_sample
)
2919 msg_Dbg( p_demux
, "sbgp lookup failed %" PRIu32
" (sample number)",
2923 else if ( i_sample
>= i_group_sample
&&
2924 *pi_sync_sample
< i_group_sample
)
2926 *pi_sync_sample
= i_group_sample
;
2927 i_ret
= VLC_SUCCESS
;
2930 i_group_sample
+= p_sbgp_data
->entries
.pi_sample_count
[i
];
2933 if( i_ret
== VLC_SUCCESS
&& *pi_sync_sample
)
2935 msg_Dbg( p_demux
, "sbgp gives %d --> %" PRIu32
" (sample number)",
2936 i_sample
, *pi_sync_sample
);
2944 /* given a time it return sample/chunk
2945 * it also update elst field of the track
2947 static int TrackTimeToSampleChunk( demux_t
*p_demux
, mp4_track_t
*p_track
,
2948 int64_t i_start
, uint32_t *pi_chunk
,
2949 uint32_t *pi_sample
)
2951 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2953 unsigned int i_sample
;
2954 unsigned int i_chunk
;
2957 /* FIXME see if it's needed to check p_track->i_chunk_count */
2958 if( p_track
->i_chunk_count
== 0 )
2959 return( VLC_EGENERIC
);
2961 /* handle elst (find the correct one) */
2962 MP4_TrackSetELST( p_demux
, p_track
, i_start
);
2963 if( p_track
->p_elst
&& p_track
->BOXDATA(p_elst
)->i_entry_count
> 0 )
2965 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
2966 int64_t i_mvt
= MP4_rescale( i_start
, CLOCK_FREQ
, p_sys
->i_timescale
);
2968 /* now calculate i_start for this elst */
2970 i_start
-= MP4_rescale( p_track
->i_elst_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
2978 /* to track time scale */
2979 i_start
= MP4_rescale( i_start
, CLOCK_FREQ
, p_track
->i_timescale
);
2980 /* add elst offset */
2981 if( ( elst
->i_media_rate_integer
[p_track
->i_elst
] > 0 ||
2982 elst
->i_media_rate_fraction
[p_track
->i_elst
] > 0 ) &&
2983 elst
->i_media_time
[p_track
->i_elst
] > 0 )
2985 i_start
+= elst
->i_media_time
[p_track
->i_elst
];
2988 msg_Dbg( p_demux
, "elst (%d) gives %"PRId64
"ms (movie)-> %"PRId64
2989 "ms (track)", p_track
->i_elst
,
2990 MP4_rescale( i_mvt
, p_sys
->i_timescale
, 1000 ),
2991 MP4_rescale( i_start
, p_track
->i_timescale
, 1000 ) );
2995 /* convert absolute time to in timescale unit */
2996 i_start
= MP4_rescale( i_start
, CLOCK_FREQ
, p_track
->i_timescale
);
2999 /* we start from sample 0/chunk 0, hope it won't take too much time */
3000 /* *** find good chunk *** */
3001 for( i_chunk
= 0; ; i_chunk
++ )
3003 if( i_chunk
+ 1 >= p_track
->i_chunk_count
)
3005 /* at the end and can't check if i_start in this chunk,
3006 it will be check while searching i_sample */
3007 i_chunk
= p_track
->i_chunk_count
- 1;
3011 if( (uint64_t)i_start
>= p_track
->chunk
[i_chunk
].i_first_dts
&&
3012 (uint64_t)i_start
< p_track
->chunk
[i_chunk
+ 1].i_first_dts
)
3018 /* *** find sample in the chunk *** */
3019 i_sample
= p_track
->chunk
[i_chunk
].i_sample_first
;
3020 i_dts
= p_track
->chunk
[i_chunk
].i_first_dts
;
3021 for( i_index
= 0; i_sample
< p_track
->chunk
[i_chunk
].i_sample_count
; )
3024 p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
] *
3025 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
] < (uint64_t)i_start
)
3028 p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
] *
3029 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
];
3031 i_sample
+= p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
];
3036 if( p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
] <= 0 )
3040 i_sample
+= ( i_start
- i_dts
) /
3041 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
];
3046 if( i_sample
>= p_track
->i_sample_count
)
3048 msg_Warn( p_demux
, "track[Id 0x%x] will be disabled "
3049 "(seeking too far) chunk=%d sample=%d",
3050 p_track
->i_track_ID
, i_chunk
, i_sample
);
3051 return( VLC_EGENERIC
);
3055 /* *** Try to find nearest sync points *** */
3056 uint32_t i_sync_sample
;
3058 TrackGetNearestSeekPoint( p_demux
, p_track
, i_sample
, &i_sync_sample
) )
3061 if( i_sync_sample
<= i_sample
)
3063 while( i_chunk
> 0 &&
3064 i_sync_sample
< p_track
->chunk
[i_chunk
].i_sample_first
)
3069 while( i_chunk
< p_track
->i_chunk_count
- 1 &&
3070 i_sync_sample
>= p_track
->chunk
[i_chunk
].i_sample_first
+
3071 p_track
->chunk
[i_chunk
].i_sample_count
)
3074 i_sample
= i_sync_sample
;
3077 *pi_chunk
= i_chunk
;
3078 *pi_sample
= i_sample
;
3083 static int TrackGotoChunkSample( demux_t
*p_demux
, mp4_track_t
*p_track
,
3084 unsigned int i_chunk
, unsigned int i_sample
)
3086 bool b_reselect
= false;
3088 /* now see if actual es is ok */
3089 if( p_track
->i_chunk
>= p_track
->i_chunk_count
||
3090 p_track
->chunk
[p_track
->i_chunk
].i_sample_description_index
!=
3091 p_track
->chunk
[i_chunk
].i_sample_description_index
)
3093 msg_Warn( p_demux
, "recreate ES for track[Id 0x%x]",
3094 p_track
->i_track_ID
);
3096 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
,
3097 p_track
->p_es
, &b_reselect
);
3099 es_out_Del( p_demux
->out
, p_track
->p_es
);
3101 p_track
->p_es
= NULL
;
3103 if( TrackCreateES( p_demux
, p_track
, i_chunk
, &p_track
->p_es
) )
3105 msg_Err( p_demux
, "cannot create es for track[Id 0x%x]",
3106 p_track
->i_track_ID
);
3108 p_track
->b_ok
= false;
3109 p_track
->b_selected
= false;
3110 return VLC_EGENERIC
;
3114 /* select again the new decoder */
3117 es_out_Control( p_demux
->out
, ES_OUT_SET_ES
, p_track
->p_es
);
3120 p_track
->i_chunk
= i_chunk
;
3121 p_track
->chunk
[i_chunk
].i_sample
= i_sample
- p_track
->chunk
[i_chunk
].i_sample_first
;
3122 p_track
->i_sample
= i_sample
;
3124 return p_track
->b_selected
? VLC_SUCCESS
: VLC_EGENERIC
;
3127 static void MP4_TrackRestart( demux_t
*p_demux
, mp4_track_t
*p_track
,
3128 MP4_Box_t
*p_params_box
)
3130 bool b_reselect
= false;
3133 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
,
3134 p_track
->p_es
, &b_reselect
);
3137 /* Save previous fragmented pos */
3138 uint32_t i_sample_pos_backup
= p_track
->i_sample
;
3139 mtime_t time_backup
= p_track
->i_time
;
3140 uint32_t timescale_backup
= p_track
->i_timescale
;
3142 /* Save previous format and ES */
3143 es_format_t fmtbackup
;
3144 es_out_id_t
*p_es_backup
= p_track
->p_es
;
3145 p_track
->p_es
= NULL
;
3146 es_format_Copy( &fmtbackup
, &p_track
->fmt
);
3147 es_format_Clean( &p_track
->fmt
);
3150 /* do the cleanup and recycle track / restart */
3151 MP4_TrackDestroy( p_demux
, p_track
);
3152 memset( p_track
, 0, sizeof(*p_track
) );
3154 assert(p_params_box
->i_type
== ATOM_trak
);
3155 MP4_TrackCreate( p_demux
, p_track
, p_params_box
, false, true );
3159 if( !es_format_IsSimilar( &fmtbackup
, &p_track
->fmt
) ||
3160 fmtbackup
.i_extra
!= p_track
->fmt
.i_extra
||
3161 memcmp( fmtbackup
.p_extra
, p_track
->fmt
.p_extra
, fmtbackup
.i_extra
) )
3164 es_out_Del( p_demux
->out
, p_es_backup
);
3166 if( !p_track
->b_chapters_source
)
3168 p_track
->p_es
= MP4_AddTrackES( p_demux
->out
, p_track
);
3169 p_track
->b_ok
= !!p_track
->p_es
;
3174 p_track
->p_es
= p_es_backup
;
3177 else if( p_es_backup
)
3179 es_out_Del( p_demux
->out
, p_es_backup
);
3182 /* select again the new decoder */
3183 if( b_reselect
&& p_track
->p_es
)
3184 es_out_Control( p_demux
->out
, ES_OUT_SET_ES
, p_track
->p_es
);
3186 es_format_Clean( &fmtbackup
);
3188 /* Restore fragmented pos */
3189 p_track
->i_sample
= i_sample_pos_backup
;
3190 p_track
->i_time
= MP4_rescale( time_backup
, timescale_backup
, p_track
->i_timescale
);
3193 /****************************************************************************
3195 ****************************************************************************
3196 * Parse track information and create all needed data to run a track
3197 * If it succeed b_ok is set to 1 else to 0
3198 ****************************************************************************/
3199 static void MP4_TrackSetup( demux_t
*p_demux
, mp4_track_t
*p_track
,
3200 MP4_Box_t
*p_box_trak
,
3201 bool b_create_es
, bool b_force_enable
)
3203 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3205 p_track
->p_track
= p_box_trak
;
3207 char language
[4] = { '\0' };
3208 char sdp_media_type
[8] = { '\0' };
3210 const MP4_Box_t
*p_tkhd
= MP4_BoxGet( p_box_trak
, "tkhd" );
3216 /* do we launch this track by default ? */
3218 ( ( BOXDATA(p_tkhd
)->i_flags
&MP4_TRACK_ENABLED
) != 0 );
3219 if( !p_track
->b_enable
)
3220 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3222 p_track
->i_track_ID
= BOXDATA(p_tkhd
)->i_track_ID
;
3224 p_track
->i_width
= BOXDATA(p_tkhd
)->i_width
/ BLOCK16x16
;
3225 p_track
->i_height
= BOXDATA(p_tkhd
)->i_height
/ BLOCK16x16
;
3226 p_track
->f_rotation
= BOXDATA(p_tkhd
)->f_rotation
;
3228 /* FIXME: unhandled box: tref */
3230 const MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_box_trak
, "mdia/mdhd" );
3231 const MP4_Box_t
*p_hdlr
= MP4_BoxGet( p_box_trak
, "mdia/hdlr" );
3233 if( ( !p_mdhd
)||( !p_hdlr
) )
3238 if( BOXDATA(p_mdhd
)->i_timescale
== 0 )
3240 msg_Warn( p_demux
, "Invalid track timescale " );
3243 p_track
->i_timescale
= BOXDATA(p_mdhd
)->i_timescale
;
3245 memcpy( &language
, BOXDATA(p_mdhd
)->rgs_language
, 3 );
3246 p_track
->b_mac_encoding
= BOXDATA(p_mdhd
)->b_mac_encoding
;
3248 switch( p_hdlr
->data
.p_hdlr
->i_handler_type
)
3251 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/smhd" ) )
3255 es_format_Change( &p_track
->fmt
, AUDIO_ES
, 0 );
3259 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/vmhd") )
3263 es_format_Change( &p_track
->fmt
, VIDEO_ES
, 0 );
3267 /* RTP Reception Hint tracks */
3268 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/hmhd" ) ||
3269 !MP4_BoxGet( p_box_trak
, "mdia/minf/stbl/stsd/rrtp" ) )
3275 /* parse the sdp message to find out whether the RTP stream contained audio or video */
3276 if( !( p_sdp
= MP4_BoxGet( p_box_trak
, "udta/hnti/sdp " ) ) )
3278 msg_Warn( p_demux
, "Didn't find sdp box to determine stream type" );
3282 memcpy( sdp_media_type
, BOXDATA(p_sdp
)->psz_text
, 7 );
3283 if( !strcmp(sdp_media_type
, "m=audio") )
3285 msg_Dbg( p_demux
, "Found audio Rtp: %s", sdp_media_type
);
3286 es_format_Change( &p_track
->fmt
, AUDIO_ES
, 0 );
3288 else if( !strcmp(sdp_media_type
, "m=video") )
3290 msg_Dbg( p_demux
, "Found video Rtp: %s", sdp_media_type
);
3291 es_format_Change( &p_track
->fmt
, VIDEO_ES
, 0 );
3295 msg_Warn( p_demux
, "Malformed track SDP message: %s", sdp_media_type
);
3298 p_track
->p_sdp
= p_sdp
;
3304 case( ATOM_subt
): /* ttml */
3306 case( ATOM_clcp
): /* closed captions */
3307 es_format_Change( &p_track
->fmt
, SPU_ES
, 0 );
3314 p_track
->asfinfo
.i_cat
= p_track
->fmt
.i_cat
;
3316 const MP4_Box_t
*p_elst
;
3317 p_track
->i_elst
= 0;
3318 p_track
->i_elst_time
= 0;
3319 if( ( p_track
->p_elst
= p_elst
= MP4_BoxGet( p_box_trak
, "edts/elst" ) ) )
3321 MP4_Box_data_elst_t
*elst
= BOXDATA(p_elst
);
3324 msg_Warn( p_demux
, "elst box found" );
3325 for( i
= 0; i
< elst
->i_entry_count
; i
++ )
3327 msg_Dbg( p_demux
, " - [%d] duration=%"PRId64
"ms media time=%"PRId64
3328 "ms) rate=%d.%d", i
,
3329 MP4_rescale( elst
->i_segment_duration
[i
], p_sys
->i_timescale
, 1000 ),
3330 elst
->i_media_time
[i
] >= 0 ?
3331 MP4_rescale( elst
->i_media_time
[i
], p_track
->i_timescale
, 1000 ) :
3333 elst
->i_media_rate_integer
[i
],
3334 elst
->i_media_rate_fraction
[i
] );
3341 p_dinf = MP4_BoxGet( p_minf, "dinf" );
3343 if( !( p_track
->p_stbl
= MP4_BoxGet( p_box_trak
,"mdia/minf/stbl" ) ) ||
3344 !( p_track
->p_stsd
= MP4_BoxGet( p_box_trak
,"mdia/minf/stbl/stsd") ) )
3350 if( *language
&& strcmp( language
, "```" ) && strcmp( language
, "und" ) )
3352 p_track
->fmt
.psz_language
= strdup( language
);
3355 const MP4_Box_t
*p_udta
= MP4_BoxGet( p_box_trak
, "udta" );
3358 const MP4_Box_t
*p_box_iter
;
3359 for( p_box_iter
= p_udta
->p_first
; p_box_iter
!= NULL
;
3360 p_box_iter
= p_box_iter
->p_next
)
3362 switch( p_box_iter
->i_type
)
3366 p_track
->fmt
.psz_description
=
3367 strndup( p_box_iter
->data
.p_binary
->p_blob
,
3368 p_box_iter
->data
.p_binary
->i_blob
);
3375 /* Create chunk index table and sample index table */
3376 if( TrackCreateChunksIndex( p_demux
,p_track
) ||
3377 TrackCreateSamplesIndex( p_demux
, p_track
) )
3379 msg_Err( p_demux
, "cannot create chunks index" );
3380 return; /* cannot create chunks index */
3383 p_track
->i_chunk
= 0;
3384 p_track
->i_sample
= 0;
3386 /* Mark chapter only track */
3387 if( p_sys
->p_tref_chap
)
3389 MP4_Box_data_tref_generic_t
*p_chap
= p_sys
->p_tref_chap
->data
.p_tref_generic
;
3392 for( i
= 0; i
< p_chap
->i_entry_count
; i
++ )
3394 if( p_track
->i_track_ID
== p_chap
->i_track_ID
[i
] &&
3395 p_track
->fmt
.i_cat
== UNKNOWN_ES
)
3397 p_track
->b_chapters_source
= true;
3398 p_track
->b_enable
= false;
3404 const MP4_Box_t
*p_tsel
;
3406 if( b_force_enable
&&
3407 ( p_track
->fmt
.i_cat
== VIDEO_ES
|| p_track
->fmt
.i_cat
== AUDIO_ES
) )
3409 msg_Warn( p_demux
, "Enabling track[Id 0x%x] (buggy file without enabled track)",
3410 p_track
->i_track_ID
);
3411 p_track
->b_enable
= true;
3412 p_track
->fmt
.i_priority
= ES_PRIORITY_SELECTABLE_MIN
;
3414 else if ( (p_tsel
= MP4_BoxGet( p_box_trak
, "udta/tsel" )) )
3416 if ( BOXDATA(p_tsel
) && BOXDATA(p_tsel
)->i_switch_group
)
3418 p_track
->i_switch_group
= BOXDATA(p_tsel
)->i_switch_group
;
3419 int i_priority
= ES_PRIORITY_SELECTABLE_MIN
;
3420 for ( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
3422 const mp4_track_t
*p_other
= &p_sys
->track
[i
];
3423 if( p_other
&& p_other
!= p_track
&&
3424 p_other
->fmt
.i_cat
== p_track
->fmt
.i_cat
&&
3425 p_track
->i_switch_group
== p_other
->i_switch_group
)
3426 i_priority
= __MAX( i_priority
, p_other
->fmt
.i_priority
+ 1 );
3428 /* VLC only support ES priority for AUDIO_ES and SPU_ES.
3429 If there's another VIDEO_ES in the same group, we need to unselect it then */
3430 if ( p_track
->fmt
.i_cat
== VIDEO_ES
&& i_priority
> ES_PRIORITY_SELECTABLE_MIN
)
3431 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3433 p_track
->fmt
.i_priority
= i_priority
;
3436 /* If there's no tsel, try to enable the track coming first in edit list */
3437 else if ( p_track
->p_elst
&& p_track
->fmt
.i_priority
== ES_PRIORITY_SELECTABLE_MIN
)
3439 #define MAX_SELECTABLE (INT_MAX - ES_PRIORITY_SELECTABLE_MIN)
3440 for ( uint32_t i
=0; i
<p_track
->BOXDATA(p_elst
)->i_entry_count
; i
++ )
3442 if ( p_track
->BOXDATA(p_elst
)->i_media_time
[i
] >= 0 &&
3443 p_track
->BOXDATA(p_elst
)->i_segment_duration
[i
] )
3445 /* We do selection by inverting start time into priority.
3446 The track with earliest edit will have the highest prio */
3447 const int i_time
= __MIN( MAX_SELECTABLE
, p_track
->BOXDATA(p_elst
)->i_media_time
[i
] );
3448 p_track
->fmt
.i_priority
= ES_PRIORITY_SELECTABLE_MIN
+ MAX_SELECTABLE
- i_time
;
3454 if( p_sys
->hacks
.es_cat_filters
&& (p_sys
->hacks
.es_cat_filters
& p_track
->fmt
.i_cat
) == 0 )
3456 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3459 if( TrackCreateES( p_demux
,
3460 p_track
, p_track
->i_chunk
,
3461 (p_track
->b_chapters_source
|| !b_create_es
) ? NULL
: &p_track
->p_es
) )
3463 msg_Err( p_demux
, "cannot create es for track[Id 0x%x]",
3464 p_track
->i_track_ID
);
3468 p_track
->b_ok
= true;
3471 static void DestroyChunk( mp4_chunk_t
*ck
)
3473 free( ck
->p_sample_count_dts
);
3474 free( ck
->p_sample_delta_dts
);
3475 free( ck
->p_sample_count_pts
);
3476 free( ck
->p_sample_offset_pts
);
3477 free( ck
->p_sample_size
);
3480 /****************************************************************************
3482 ****************************************************************************
3483 * Cleans a track created by MP4_TrackCreate.
3484 ****************************************************************************/
3485 static void MP4_TrackClean( es_out_t
*out
, mp4_track_t
*p_track
)
3487 es_format_Clean( &p_track
->fmt
);
3490 es_out_Del( out
, p_track
->p_es
);
3492 if( p_track
->chunk
)
3494 for( unsigned int i_chunk
= 0; i_chunk
< p_track
->i_chunk_count
; i_chunk
++ )
3495 DestroyChunk( &p_track
->chunk
[i_chunk
] );
3497 free( p_track
->chunk
);
3499 if( !p_track
->i_sample_size
)
3500 free( p_track
->p_sample_size
);
3502 if ( p_track
->asfinfo
.p_frame
)
3503 block_ChainRelease( p_track
->asfinfo
.p_frame
);
3505 free( p_track
->context
.runs
.p_array
);
3508 static void MP4_TrackInit( mp4_track_t
*p_track
)
3510 memset( p_track
, 0, sizeof(mp4_track_t
) );
3511 es_format_Init( &p_track
->fmt
, UNKNOWN_ES
, 0 );
3512 p_track
->i_timescale
= 1;
3515 static void MP4_TrackSelect( demux_t
*p_demux
, mp4_track_t
*p_track
, bool b_select
)
3517 if( !p_track
->b_ok
|| p_track
->b_chapters_source
)
3520 if( b_select
== p_track
->b_selected
)
3523 if( !b_select
&& p_track
->p_es
)
3525 es_out_Control( p_demux
->out
, ES_OUT_SET_ES_STATE
,
3526 p_track
->p_es
, false );
3529 p_track
->b_selected
= b_select
;
3532 static int MP4_TrackSeek( demux_t
*p_demux
, mp4_track_t
*p_track
,
3538 if( !p_track
->b_ok
|| p_track
->b_chapters_source
)
3539 return VLC_EGENERIC
;
3541 p_track
->b_selected
= false;
3543 if( TrackTimeToSampleChunk( p_demux
, p_track
, i_start
,
3544 &i_chunk
, &i_sample
) )
3546 msg_Warn( p_demux
, "cannot select track[Id 0x%x]",
3547 p_track
->i_track_ID
);
3548 return VLC_EGENERIC
;
3551 p_track
->b_selected
= true;
3552 if( !TrackGotoChunkSample( p_demux
, p_track
, i_chunk
, i_sample
) )
3553 p_track
->b_selected
= true;
3555 p_track
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
3557 return p_track
->b_selected
? VLC_SUCCESS
: VLC_EGENERIC
;
3562 * 3 types: for audio
3565 static inline uint32_t MP4_GetFixedSampleSize( const mp4_track_t
*p_track
,
3566 const MP4_Box_data_sample_soun_t
*p_soun
)
3568 uint32_t i_size
= p_track
->i_sample_size
;
3570 assert( p_track
->i_sample_size
!= 0 );
3572 /* QuickTime "built-in" support case fixups */
3573 if( p_track
->fmt
.i_cat
== AUDIO_ES
&&
3574 p_soun
->i_compressionid
== 0 && p_track
->i_sample_size
<= 2 )
3576 switch( p_track
->fmt
.i_codec
)
3579 i_size
= p_soun
->i_channelcount
;
3581 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
3585 case VLC_CODEC_S24L
:
3586 case VLC_CODEC_S24B
:
3587 case VLC_CODEC_S32L
:
3588 case VLC_CODEC_S32B
:
3589 case VLC_CODEC_F32L
:
3590 case VLC_CODEC_F32B
:
3591 case VLC_CODEC_F64L
:
3592 case VLC_CODEC_F64B
:
3593 if( p_track
->i_sample_size
< ((p_soun
->i_samplesize
+7U)/8U) * p_soun
->i_channelcount
)
3594 i_size
= ((p_soun
->i_samplesize
+7)/8) * p_soun
->i_channelcount
;
3596 case VLC_CODEC_ALAW
:
3597 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
3598 i_size
= p_soun
->i_channelcount
;
3608 static uint32_t MP4_TrackGetReadSize( mp4_track_t
*p_track
, uint32_t *pi_nb_samples
)
3610 uint32_t i_size
= 0;
3613 if ( p_track
->i_sample
== p_track
->i_sample_count
)
3616 if ( p_track
->fmt
.i_cat
!= AUDIO_ES
)
3620 if( p_track
->i_sample_size
== 0 ) /* all sizes are different */
3621 return p_track
->p_sample_size
[p_track
->i_sample
];
3623 return p_track
->i_sample_size
;
3627 const MP4_Box_data_sample_soun_t
*p_soun
= p_track
->p_sample
->data
.p_sample_soun
;
3628 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[p_track
->i_chunk
];
3629 uint32_t i_max_samples
= p_chunk
->i_sample_count
- p_chunk
->i_sample
;
3631 /* Group audio packets so we don't call demux for single sample unit */
3632 if( p_track
->fmt
.i_original_fourcc
== VLC_CODEC_DVD_LPCM
&&
3633 p_soun
->i_constLPCMframesperaudiopacket
&&
3634 p_soun
->i_constbytesperaudiopacket
)
3636 /* uncompressed case */
3637 uint32_t i_packets
= i_max_samples
/ p_soun
->i_constLPCMframesperaudiopacket
;
3638 if ( UINT32_MAX
/ p_soun
->i_constbytesperaudiopacket
< i_packets
)
3639 i_packets
= UINT32_MAX
/ p_soun
->i_constbytesperaudiopacket
;
3640 *pi_nb_samples
= i_packets
* p_soun
->i_constLPCMframesperaudiopacket
;
3641 return i_packets
* p_soun
->i_constbytesperaudiopacket
;
3644 if( p_track
->fmt
.i_original_fourcc
== VLC_FOURCC('r','r','t','p') )
3647 return p_track
->i_sample_size
;
3650 /* all samples have a different size */
3651 if( p_track
->i_sample_size
== 0 )
3654 return p_track
->p_sample_size
[p_track
->i_sample
];
3657 if( p_soun
->i_qt_version
== 1 )
3659 if ( p_soun
->i_compressionid
== 0xFFFE )
3661 *pi_nb_samples
= 1; /* != number of audio samples */
3662 if ( p_track
->i_sample_size
)
3663 return p_track
->i_sample_size
;
3665 return p_track
->p_sample_size
[p_track
->i_sample
];
3667 else if ( p_soun
->i_compressionid
!= 0 || p_soun
->i_bytes_per_sample
> 1 ) /* compressed */
3669 /* in this case we are dealing with compressed data
3670 -2 in V1: additional fields are meaningless (VBR and such) */
3671 *pi_nb_samples
= i_max_samples
;//p_track->chunk[p_track->i_chunk].i_sample_count;
3672 if( p_track
->fmt
.audio
.i_blockalign
> 1 )
3673 *pi_nb_samples
= p_soun
->i_sample_per_packet
;
3674 i_size
= *pi_nb_samples
/ p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
;
3677 else /* uncompressed case */
3680 if( p_track
->fmt
.audio
.i_blockalign
> 1 )
3683 i_packets
= i_max_samples
/ p_soun
->i_sample_per_packet
;
3685 if ( UINT32_MAX
/ p_soun
->i_bytes_per_frame
< i_packets
)
3686 i_packets
= UINT32_MAX
/ p_soun
->i_bytes_per_frame
;
3688 *pi_nb_samples
= i_packets
* p_soun
->i_sample_per_packet
;
3689 i_size
= i_packets
* p_soun
->i_bytes_per_frame
;
3694 /* uncompressed v0 (qt) or... not (ISO) */
3696 /* Quicktime built-in support handling */
3697 if( p_soun
->i_compressionid
== 0 && p_track
->i_sample_size
== 1 )
3699 switch( p_track
->fmt
.i_codec
)
3701 /* sample size is not integer */
3703 *pi_nb_samples
= 160 * p_track
->fmt
.audio
.i_channels
;
3704 return 33 * p_track
->fmt
.audio
.i_channels
;
3710 /* More regular V0 cases */
3711 uint32_t i_max_v0_samples
;
3712 switch( p_track
->fmt
.i_codec
)
3714 /* Compressed samples in V0 */
3715 case VLC_CODEC_AMR_NB
:
3716 case VLC_CODEC_AMR_WB
:
3717 i_max_v0_samples
= 16;
3719 case VLC_CODEC_MPGA
:
3722 i_max_v0_samples
= 1;
3725 /* Read 25ms of samples (uncompressed) */
3726 i_max_v0_samples
= p_track
->fmt
.audio
.i_rate
/ 40 *
3727 p_track
->fmt
.audio
.i_channels
;
3728 if( i_max_v0_samples
< 1 )
3729 i_max_v0_samples
= 1;
3734 for( uint32_t i
=p_track
->i_sample
;
3735 i
<p_chunk
->i_sample_first
+p_chunk
->i_sample_count
&&
3736 i
<p_track
->i_sample_count
;
3740 if ( p_track
->i_sample_size
== 0 )
3741 i_size
+= p_track
->p_sample_size
[i
];
3743 i_size
+= MP4_GetFixedSampleSize( p_track
, p_soun
);
3745 /* Try to detect compression in ISO */
3746 if(p_soun
->i_compressionid
!= 0)
3748 /* Return only 1 sample */
3752 if ( *pi_nb_samples
== i_max_v0_samples
)
3757 //fprintf( stderr, "size=%d\n", i_size );
3761 static uint64_t MP4_TrackGetPos( mp4_track_t
*p_track
)
3763 unsigned int i_sample
;
3766 i_pos
= p_track
->chunk
[p_track
->i_chunk
].i_offset
;
3768 if( p_track
->i_sample_size
)
3770 MP4_Box_data_sample_soun_t
*p_soun
=
3771 p_track
->p_sample
->data
.p_sample_soun
;
3773 /* Quicktime builtin support, _must_ ignore sample tables */
3774 if( p_track
->fmt
.i_cat
== AUDIO_ES
&& p_soun
->i_compressionid
== 0 &&
3775 p_track
->i_sample_size
== 1 )
3777 switch( p_track
->fmt
.i_codec
)
3779 case VLC_CODEC_GSM
: /* # Samples > data size */
3780 i_pos
+= ( p_track
->i_sample
-
3781 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) / 160 * 33;
3788 if( p_track
->fmt
.i_cat
!= AUDIO_ES
|| p_soun
->i_qt_version
== 0 ||
3789 p_track
->fmt
.audio
.i_blockalign
<= 1 ||
3790 p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
== 0 )
3792 i_pos
+= ( p_track
->i_sample
-
3793 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) *
3794 MP4_GetFixedSampleSize( p_track
, p_soun
);
3798 /* we read chunk by chunk unless a blockalign is requested */
3799 i_pos
+= ( p_track
->i_sample
- p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) /
3800 p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
;
3805 for( i_sample
= p_track
->chunk
[p_track
->i_chunk
].i_sample_first
;
3806 i_sample
< p_track
->i_sample
; i_sample
++ )
3808 i_pos
+= p_track
->p_sample_size
[i_sample
];
3815 static int MP4_TrackNextSample( demux_t
*p_demux
, mp4_track_t
*p_track
, uint32_t i_samples
)
3817 if ( UINT32_MAX
- p_track
->i_sample
< i_samples
)
3819 p_track
->i_sample
= UINT32_MAX
;
3820 return VLC_EGENERIC
;
3823 p_track
->i_sample
+= i_samples
;
3825 if( p_track
->i_sample
>= p_track
->i_sample_count
)
3826 return VLC_EGENERIC
;
3828 /* Have we changed chunk ? */
3829 if( p_track
->i_sample
>=
3830 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
+
3831 p_track
->chunk
[p_track
->i_chunk
].i_sample_count
)
3833 if( TrackGotoChunkSample( p_demux
, p_track
, p_track
->i_chunk
+ 1,
3834 p_track
->i_sample
) )
3836 msg_Warn( p_demux
, "track[0x%x] will be disabled "
3837 "(cannot restart decoder)", p_track
->i_track_ID
);
3838 MP4_TrackSelect( p_demux
, p_track
, false );
3839 return VLC_EGENERIC
;
3843 /* Have we changed elst */
3844 if( p_track
->p_elst
&& p_track
->BOXDATA(p_elst
)->i_entry_count
> 0 )
3846 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3847 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
3848 uint64_t i_mvt
= MP4_rescale( MP4_TrackGetDTS( p_demux
, p_track
),
3849 CLOCK_FREQ
, p_sys
->i_timescale
);
3850 if( (unsigned int)p_track
->i_elst
< elst
->i_entry_count
&&
3851 i_mvt
>= p_track
->i_elst_time
+
3852 elst
->i_segment_duration
[p_track
->i_elst
] )
3854 MP4_TrackSetELST( p_demux
, p_track
,
3855 MP4_TrackGetDTS( p_demux
, p_track
) );
3862 static void MP4_TrackSetELST( demux_t
*p_demux
, mp4_track_t
*tk
,
3865 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3866 int i_elst_last
= tk
->i_elst
;
3868 /* handle elst (find the correct one) */
3870 tk
->i_elst_time
= 0;
3871 if( tk
->p_elst
&& tk
->BOXDATA(p_elst
)->i_entry_count
> 0 )
3873 MP4_Box_data_elst_t
*elst
= tk
->BOXDATA(p_elst
);
3874 int64_t i_mvt
= MP4_rescale( i_time
, CLOCK_FREQ
, p_sys
->i_timescale
);
3876 for( tk
->i_elst
= 0; (unsigned int)tk
->i_elst
< elst
->i_entry_count
; tk
->i_elst
++ )
3878 mtime_t i_dur
= elst
->i_segment_duration
[tk
->i_elst
];
3880 if( tk
->i_elst_time
<= i_mvt
&& i_mvt
< tk
->i_elst_time
+ i_dur
)
3884 tk
->i_elst_time
+= i_dur
;
3887 if( (unsigned int)tk
->i_elst
>= elst
->i_entry_count
)
3889 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
3890 tk
->i_elst
= elst
->i_entry_count
- 1;
3891 tk
->i_elst_time
-= elst
->i_segment_duration
[tk
->i_elst
];
3894 if( elst
->i_media_time
[tk
->i_elst
] < 0 )
3897 tk
->i_elst_time
+= elst
->i_segment_duration
[tk
->i_elst
];
3900 if( i_elst_last
!= tk
->i_elst
)
3902 msg_Warn( p_demux
, "elst old=%d new=%d", i_elst_last
, tk
->i_elst
);
3903 tk
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
3907 /******************************************************************************
3908 * Here are the functions used for fragmented MP4
3909 *****************************************************************************/
3913 * \Note If we call that function too soon,
3914 * before the track has been selected by MP4_TrackSelect
3915 * (during the first execution of Demux), then the track gets disabled
3917 static int ReInitDecoder( demux_t
*p_demux
, const MP4_Box_t
*p_root
,
3918 mp4_track_t
*p_track
)
3920 MP4_Box_t
*p_paramsbox
= MP4_BoxGet( p_root
, "/moov/trak[0]" );
3922 return VLC_EGENERIC
;
3924 MP4_TrackRestart( p_demux
, p_track
, p_paramsbox
);
3926 /* Temporary hack until we support track selection */
3927 p_track
->b_selected
= true;
3928 p_track
->b_enable
= true;
3934 static stime_t
GetCumulatedDuration( demux_t
*p_demux
)
3936 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3937 stime_t i_max_duration
= 0;
3939 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
3941 stime_t i_track_duration
= 0;
3942 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_sys
->p_moov
, p_sys
->track
[i
].i_track_ID
);
3943 const MP4_Box_t
*p_stsz
;
3944 const MP4_Box_t
*p_tkhd
;
3945 if ( (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) &&
3946 (p_stsz
= MP4_BoxGet( p_trak
, "mdia/minf/stbl/stsz" )) &&
3947 /* duration might be wrong an be set to whole duration :/ */
3948 BOXDATA(p_stsz
)->i_sample_count
> 0 )
3950 i_max_duration
= __MAX( (uint64_t)i_max_duration
, BOXDATA(p_tkhd
)->i_duration
);
3953 if( p_sys
->p_fragsindex
)
3955 i_track_duration
+= MP4_Fragment_Index_GetTrackDuration( p_sys
->p_fragsindex
, i
);
3958 i_max_duration
= __MAX( i_max_duration
, i_track_duration
);
3961 return i_max_duration
;
3964 static int ProbeIndex( demux_t
*p_demux
)
3966 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3967 uint64_t i_stream_size
;
3968 uint8_t mfro
[MP4_MFRO_BOXSIZE
];
3969 assert( p_sys
->b_seekable
);
3971 if ( MP4_BoxCount( p_sys
->p_root
, "/mfra" ) )
3972 return VLC_EGENERIC
;
3974 i_stream_size
= stream_Size( p_demux
->s
);
3975 if ( ( i_stream_size
>> 62 ) ||
3976 ( i_stream_size
< MP4_MFRO_BOXSIZE
) ||
3977 ( vlc_stream_Seek( p_demux
->s
, i_stream_size
- MP4_MFRO_BOXSIZE
) != VLC_SUCCESS
)
3980 msg_Dbg( p_demux
, "Probing tail for mfro has failed" );
3981 return VLC_EGENERIC
;
3984 if ( vlc_stream_Read( p_demux
->s
, &mfro
, MP4_MFRO_BOXSIZE
) == MP4_MFRO_BOXSIZE
&&
3985 VLC_FOURCC(mfro
[4],mfro
[5],mfro
[6],mfro
[7]) == ATOM_mfro
&&
3986 GetDWBE( &mfro
) == MP4_MFRO_BOXSIZE
)
3988 uint32_t i_offset
= GetDWBE( &mfro
[12] );
3989 msg_Dbg( p_demux
, "will read mfra index at %"PRIu64
, i_stream_size
- i_offset
);
3990 if ( i_stream_size
> i_offset
&&
3991 vlc_stream_Seek( p_demux
->s
, i_stream_size
- i_offset
) == VLC_SUCCESS
)
3993 msg_Dbg( p_demux
, "reading mfra index at %"PRIu64
, i_stream_size
- i_offset
);
3994 const uint32_t stoplist
[] = { ATOM_mfra
, 0 };
3995 MP4_ReadBoxContainerChildren( p_demux
->s
, p_sys
->p_root
, stoplist
);
3999 return VLC_EGENERIC
;
4002 static stime_t
GetMoovTrackDuration( demux_sys_t
*p_sys
, unsigned i_track_ID
)
4004 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_sys
->p_moov
, i_track_ID
);
4005 const MP4_Box_t
*p_stsz
;
4006 const MP4_Box_t
*p_tkhd
;
4007 if ( (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) &&
4008 (p_stsz
= MP4_BoxGet( p_trak
, "mdia/minf/stbl/stsz" )) &&
4009 /* duration might be wrong an be set to whole duration :/ */
4010 BOXDATA(p_stsz
)->i_sample_count
> 0 )
4012 return BOXDATA(p_tkhd
)->i_duration
; /* In movie / mvhd scale */
4017 static bool GetMoofTrackDuration( MP4_Box_t
*p_moov
, MP4_Box_t
*p_moof
,
4018 unsigned i_track_ID
, stime_t
*p_duration
)
4020 if ( !p_moof
|| !p_moov
)
4023 MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
4026 if ( p_traf
->i_type
!= ATOM_traf
)
4028 p_traf
= p_traf
->p_next
;
4032 const MP4_Box_t
*p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" );
4033 const MP4_Box_t
*p_trun
= MP4_BoxGet( p_traf
, "trun" );
4034 if ( !p_tfhd
|| !p_trun
|| i_track_ID
!= BOXDATA(p_tfhd
)->i_track_ID
)
4036 p_traf
= p_traf
->p_next
;
4040 uint32_t i_track_timescale
= 0;
4041 uint32_t i_track_defaultsampleduration
= 0;
4043 /* set trex for defaults */
4044 MP4_Box_t
*p_trex
= MP4_GetTrexByTrackID( p_moov
, BOXDATA(p_tfhd
)->i_track_ID
);
4047 i_track_defaultsampleduration
= BOXDATA(p_trex
)->i_default_sample_duration
;
4050 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_moov
, BOXDATA(p_tfhd
)->i_track_ID
);
4053 MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_trak
, "mdia/mdhd" );
4055 i_track_timescale
= BOXDATA(p_mdhd
)->i_timescale
;
4058 if ( !i_track_timescale
)
4060 p_traf
= p_traf
->p_next
;
4064 uint64_t i_traf_duration
= 0;
4065 while ( p_trun
&& p_tfhd
)
4067 if ( p_trun
->i_type
!= ATOM_trun
)
4069 p_trun
= p_trun
->p_next
;
4072 const MP4_Box_data_trun_t
*p_trundata
= p_trun
->data
.p_trun
;
4074 /* Sum total time */
4075 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4077 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4078 i_traf_duration
+= p_trundata
->p_samples
[i
].i_duration
;
4080 else if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
4082 i_traf_duration
+= p_trundata
->i_sample_count
*
4083 BOXDATA(p_tfhd
)->i_default_sample_duration
;
4087 i_traf_duration
+= p_trundata
->i_sample_count
*
4088 i_track_defaultsampleduration
;
4091 p_trun
= p_trun
->p_next
;
4094 *p_duration
= i_traf_duration
;
4101 static int ProbeFragments( demux_t
*p_demux
, bool b_force
, bool *pb_fragmented
)
4103 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4105 msg_Dbg( p_demux
, "probing fragments from %"PRId64
, vlc_stream_Tell( p_demux
->s
) );
4107 assert( p_sys
->p_root
);
4109 MP4_Box_t
*p_vroot
= MP4_BoxNew(ATOM_root
);
4111 return VLC_EGENERIC
;
4113 if( p_sys
->b_seekable
&& (p_sys
->b_fastseekable
|| b_force
) )
4115 MP4_ReadBoxContainerChildren( p_demux
->s
, p_vroot
, NULL
); /* Get the rest of the file */
4116 p_sys
->b_fragments_probed
= true;
4118 const unsigned i_moof
= MP4_BoxCount( p_vroot
, "/moof" );
4121 *pb_fragmented
= true;
4122 p_sys
->p_fragsindex
= MP4_Fragments_Index_New( p_sys
->i_tracks
, i_moof
);
4123 if( !p_sys
->p_fragsindex
)
4125 MP4_BoxFree( p_vroot
);
4126 return VLC_EGENERIC
;
4129 stime_t
*pi_track_times
= calloc( p_sys
->i_tracks
, sizeof(*pi_track_times
) );
4130 if( !pi_track_times
)
4132 MP4_Fragments_Index_Delete( p_sys
->p_fragsindex
);
4133 p_sys
->p_fragsindex
= NULL
;
4134 MP4_BoxFree( p_vroot
);
4135 return VLC_EGENERIC
;
4140 for( MP4_Box_t
*p_moof
= p_vroot
->p_first
; p_moof
; p_moof
= p_moof
->p_next
)
4142 if( p_moof
->i_type
!= ATOM_moof
)
4145 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4147 stime_t i_duration
= 0;
4148 MP4_Box_t
*p_tfdt
= NULL
;
4149 MP4_Box_t
*p_traf
= MP4_GetTrafByTrackID( p_moof
, p_sys
->track
[i
].i_track_ID
);
4151 p_tfdt
= MP4_BoxGet( p_traf
, "tfdt" );
4153 /* Set first fragment time offset from moov */
4155 pi_track_times
[i
] = GetMoovTrackDuration( p_sys
, p_sys
->track
[i
].i_track_ID
);
4157 if( p_tfdt
&& BOXDATA(p_tfdt
) )
4159 pi_track_times
[i
] = p_tfdt
->data
.p_tfdt
->i_base_media_decode_time
;
4161 else if( index
== 0 ) /* Set first fragment time offset from moov */
4163 i_duration
= GetMoovTrackDuration( p_sys
, p_sys
->track
[i
].i_track_ID
);
4164 pi_track_times
[i
] = MP4_rescale( i_duration
, p_sys
->i_timescale
, p_sys
->track
[i
].i_timescale
);
4167 stime_t i_movietime
= MP4_rescale( pi_track_times
[i
], p_sys
->track
[i
].i_timescale
, p_sys
->i_timescale
);
4168 p_sys
->p_fragsindex
->p_times
[index
* p_sys
->i_tracks
+ i
] = i_movietime
;
4170 if( GetMoofTrackDuration( p_sys
->p_moov
, p_moof
, p_sys
->track
[i
].i_track_ID
, &i_duration
) )
4171 pi_track_times
[i
] += i_duration
;
4174 p_sys
->p_fragsindex
->pi_pos
[index
++] = p_moof
->i_pos
;
4177 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4179 stime_t i_movietime
= MP4_rescale( pi_track_times
[i
], p_sys
->track
[i
].i_timescale
, p_sys
->i_timescale
);
4180 if( p_sys
->p_fragsindex
->i_last_time
< i_movietime
)
4181 p_sys
->p_fragsindex
->i_last_time
= i_movietime
;
4184 free( pi_track_times
);
4186 MP4_Fragments_Index_Dump( VLC_OBJECT(p_demux
), p_sys
->p_fragsindex
, p_sys
->i_timescale
);
4192 /* We stop at first moof, which validates our fragmentation condition
4193 * and we'll find others while reading. */
4194 const uint32_t excllist
[] = { ATOM_moof
, 0 };
4195 MP4_ReadBoxContainerRestricted( p_demux
->s
, p_vroot
, NULL
, excllist
);
4196 /* Peek since we stopped before restriction */
4197 const uint8_t *p_peek
;
4198 if ( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) == 8 )
4199 *pb_fragmented
= (VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) == ATOM_moof
);
4201 *pb_fragmented
= false;
4204 MP4_BoxFree( p_vroot
);
4206 MP4_Box_t
*p_mehd
= MP4_BoxGet( p_sys
->p_moov
, "mvex/mehd");
4208 p_sys
->i_cumulated_duration
= GetCumulatedDuration( p_demux
);
4213 static void FragResetContext( demux_sys_t
*p_sys
)
4215 if( p_sys
->context
.p_fragment_atom
)
4217 if( p_sys
->context
.p_fragment_atom
!= p_sys
->p_moov
)
4218 MP4_BoxFree( p_sys
->context
.p_fragment_atom
);
4219 p_sys
->context
.p_fragment_atom
= NULL
;
4221 p_sys
->context
.i_current_box_type
= 0;
4223 for ( uint32_t i
=0; i
<p_sys
->i_tracks
; i
++ )
4225 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4226 p_track
->context
.i_default_sample_size
= 0;
4227 p_track
->context
.i_default_sample_duration
= 0;
4231 static int FragDemuxTrack( demux_t
*p_demux
, mp4_track_t
*p_track
,
4232 unsigned i_max_preload
)
4234 if( !p_track
->b_ok
||
4235 p_track
->context
.runs
.i_current
>= p_track
->context
.runs
.i_count
)
4236 return VLC_DEMUXER_EOS
;
4238 const MP4_Box_data_trun_t
*p_trun
=
4239 p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].p_trun
->data
.p_trun
;
4241 if( p_track
->context
.i_trun_sample
>= p_trun
->i_sample_count
)
4242 return VLC_DEMUXER_EOS
;
4244 uint32_t dur
= p_track
->context
.i_default_sample_duration
,
4245 len
= p_track
->context
.i_default_sample_size
;
4247 if( vlc_stream_Tell(p_demux
->s
) != p_track
->context
.i_trun_sample_pos
&&
4248 MP4_Seek( p_demux
->s
, p_track
->context
.i_trun_sample_pos
) != VLC_SUCCESS
)
4249 return VLC_DEMUXER_EOF
;
4251 const stime_t i_demux_max_dts
= (i_max_preload
< UINT_MAX
) ?
4252 p_track
->i_time
+ MP4_rescale( i_max_preload
, CLOCK_FREQ
, p_track
->i_timescale
) :
4255 for( uint32_t i
= p_track
->context
.i_trun_sample
; i
< p_trun
->i_sample_count
; i
++ )
4257 const stime_t i_dts
= p_track
->i_time
;
4258 stime_t i_pts
= i_dts
;
4260 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4261 dur
= p_trun
->p_samples
[i
].i_duration
;
4263 if( i_dts
> i_demux_max_dts
)
4264 return VLC_DEMUXER_SUCCESS
;
4266 p_track
->i_time
+= dur
;
4267 p_track
->context
.i_trun_sample
= i
+ 1;
4269 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
)
4271 if ( p_trun
->i_version
== 1 )
4272 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v1
;
4273 else if( p_trun
->p_samples
[i
].i_composition_time_offset
.v0
< 0xFF000000 )
4274 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v0
;
4275 else /* version 0 with negative */
4276 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v1
;
4279 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
4280 len
= p_trun
->p_samples
[i
].i_size
;
4283 msg_Warn(p_demux
, "Zero duration sample in trun.");
4286 msg_Warn(p_demux
, "Zero length sample in trun.");
4288 block_t
*p_block
= vlc_stream_Block( p_demux
->s
, len
);
4289 uint32_t i_read
= ( p_block
) ? p_block
->i_buffer
: 0;
4290 p_track
->context
.i_trun_sample_pos
+= i_read
;
4291 if( i_read
< len
|| p_block
== NULL
)
4294 block_Release( p_block
);
4295 return VLC_DEMUXER_EOF
;
4299 msg_Dbg( p_demux
, "tk(%i)=%"PRId64
" mv=%"PRId64
" pos=%"PRIu64
, p_track
->i_track_ID
,
4300 VLC_TS_0
+ MP4_rescale( i_dts
, p_track
->i_timescale
, CLOCK_FREQ
),
4301 VLC_TS_0
+ MP4_rescale( i_pts
, p_track
->i_timescale
, CLOCK_FREQ
),
4302 p_track
->context
.i_trun_sample_pos
);
4304 if ( p_track
->p_es
)
4306 p_block
->i_dts
= VLC_TS_0
+ MP4_rescale( i_dts
, p_track
->i_timescale
, CLOCK_FREQ
);
4307 if( p_track
->fmt
.i_cat
== VIDEO_ES
&& !( p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
) )
4308 p_block
->i_pts
= VLC_TS_INVALID
;
4310 p_block
->i_pts
= VLC_TS_0
+ MP4_rescale( i_pts
, p_track
->i_timescale
, CLOCK_FREQ
);
4311 p_block
->i_length
= MP4_rescale( dur
, p_track
->i_timescale
, CLOCK_FREQ
);
4312 MP4_Block_Send( p_demux
, p_track
, p_block
);
4314 else block_Release( p_block
);
4317 if( p_track
->context
.i_trun_sample
== p_trun
->i_sample_count
)
4319 p_track
->context
.i_trun_sample
= 0;
4320 if( ++p_track
->context
.runs
.i_current
< p_track
->context
.runs
.i_count
)
4322 p_track
->i_time
= p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].i_first_dts
;
4323 p_track
->context
.i_trun_sample_pos
= p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].i_offset
;
4327 return VLC_DEMUXER_SUCCESS
;
4330 static int DemuxMoof( demux_t
*p_demux
)
4332 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4335 const unsigned i_max_preload
= ( p_sys
->b_fastseekable
) ? 0 : ( p_sys
->b_seekable
) ? DEMUX_TRACK_MAX_PRELOAD
: UINT_MAX
;
4337 const mtime_t i_nztime
= MP4_GetMoviePTS( p_sys
);
4339 /* !important! Ensure clock is set before sending data */
4340 if( p_sys
->i_pcr
== VLC_TS_INVALID
)
4341 es_out_SetPCR( p_demux
->out
, VLC_TS_0
+ i_nztime
);
4343 /* demux up to increment amount of data on every track, or just set pcr if empty data */
4346 mp4_track_t
*tk
= NULL
;
4347 i_status
= VLC_DEMUXER_EOS
;
4349 /* First pass, find any track within our target increment, ordered by position */
4350 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4352 mp4_track_t
*tk_tmp
= &p_sys
->track
[i
];
4354 if( !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
4355 (!tk_tmp
->b_selected
&& !p_sys
->b_seekable
) ||
4356 tk_tmp
->context
.runs
.i_current
>= tk_tmp
->context
.runs
.i_count
)
4359 /* At least still have data to demux on this or next turns */
4360 i_status
= VLC_DEMUXER_SUCCESS
;
4362 if( MP4_rescale( tk_tmp
->i_time
, tk_tmp
->i_timescale
, CLOCK_FREQ
) <= i_nztime
+ DEMUX_INCREMENT
)
4364 if( tk
== NULL
|| tk_tmp
->context
.i_trun_sample_pos
< tk
->context
.i_trun_sample_pos
)
4371 /* Second pass, refine and find any best candidate having a chunk pos closer than
4372 * current candidate (avoids seeks when increment falls between the 2) from
4373 * current position, but within extended interleave time */
4374 for( unsigned i
= 0; i_max_preload
> 0 && i
< p_sys
->i_tracks
; i
++ )
4376 mp4_track_t
*tk_tmp
= &p_sys
->track
[i
];
4378 !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
4379 (!tk_tmp
->b_selected
&& !p_sys
->b_seekable
) ||
4380 tk_tmp
->context
.runs
.i_current
>= tk_tmp
->context
.runs
.i_count
)
4383 mtime_t i_nzdts
= MP4_rescale( tk_tmp
->i_time
, tk_tmp
->i_timescale
, CLOCK_FREQ
);
4384 if ( i_nzdts
<= i_nztime
+ DEMUX_TRACK_MAX_PRELOAD
)
4386 /* Found a better candidate to avoid seeking */
4387 if( tk_tmp
->context
.i_trun_sample_pos
< tk
->context
.i_trun_sample_pos
)
4389 /* Note: previous candidate will be repicked on next loop */
4393 int i_ret
= FragDemuxTrack( p_demux
, tk
, i_max_preload
);
4395 if( i_ret
== VLC_DEMUXER_SUCCESS
)
4396 i_status
= VLC_DEMUXER_SUCCESS
;
4399 if( i_status
!= VLC_DEMUXER_SUCCESS
|| !tk
)
4403 if( i_status
!= VLC_DEMUXER_EOS
)
4405 p_sys
->i_nztime
+= DEMUX_INCREMENT
;
4406 p_sys
->i_pcr
= VLC_TS_0
+ p_sys
->i_nztime
;
4407 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
4411 mtime_t i_segment_end
= INT64_MAX
;
4412 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4414 mp4_track_t
*tk
= &p_sys
->track
[i
];
4415 if( tk
->b_ok
|| tk
->b_chapters_source
||
4416 (!tk
->b_selected
&& !p_sys
->b_seekable
) )
4418 mtime_t i_track_end
= MP4_rescale( tk
->i_time
, tk
->i_timescale
, CLOCK_FREQ
);
4419 if( i_track_end
< i_segment_end
)
4420 i_segment_end
= i_track_end
;
4422 if( i_segment_end
!= INT64_MAX
)
4424 p_sys
->i_nztime
= i_segment_end
;
4425 p_sys
->i_pcr
= VLC_TS_0
+ p_sys
->i_nztime
;
4426 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
4433 static int FragCreateTrunIndex( demux_t
*p_demux
, MP4_Box_t
*p_moof
,
4434 MP4_Box_t
*p_chunksidx
, stime_t i_moof_time
, bool b_discontinuity
)
4436 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4438 uint64_t i_traf_base_data_offset
= p_moof
->i_pos
;
4439 uint32_t i_traf
= 0;
4440 uint64_t i_prev_traf_end
= 0;
4442 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4444 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4445 if( p_track
->context
.runs
.p_array
)
4446 free( p_track
->context
.runs
.p_array
);
4447 p_track
->context
.runs
.p_array
= NULL
;
4448 p_track
->context
.runs
.i_count
= 0;
4449 p_track
->context
.runs
.i_current
= 0;
4452 for( MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
4453 p_traf
; p_traf
= p_traf
->p_next
)
4455 if ( p_traf
->i_type
!= ATOM_traf
)
4458 const MP4_Box_t
*p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" );
4459 const uint32_t i_trun_count
= MP4_BoxCount( p_traf
, "trun" );
4460 if ( !p_tfhd
|| !i_trun_count
)
4463 mp4_track_t
*p_track
= MP4_GetTrackByTrackID( p_demux
, BOXDATA(p_tfhd
)->i_track_ID
);
4467 p_track
->context
.runs
.p_array
= calloc(i_trun_count
, sizeof(mp4_run_t
));
4468 if(!p_track
->context
.runs
.p_array
)
4471 /* Get defaults for this/these RUN */
4472 uint32_t i_track_defaultsamplesize
= 0;
4473 uint32_t i_track_defaultsampleduration
= 0;
4474 MP4_GetDefaultSizeAndDuration( p_sys
->p_moov
, BOXDATA(p_tfhd
),
4475 &i_track_defaultsamplesize
,
4476 &i_track_defaultsampleduration
);
4477 p_track
->context
.i_default_sample_size
= i_track_defaultsamplesize
;
4478 p_track
->context
.i_default_sample_duration
= i_track_defaultsampleduration
;
4480 stime_t i_traf_start_time
= p_track
->i_time
;
4481 bool b_has_base_media_decode_time
= false;
4483 if( b_discontinuity
) /* We NEED start time offset for each track */
4485 /* Find start time */
4486 const MP4_Box_t
*p_tfdt
= MP4_BoxGet( p_traf
, "tfdt" );
4489 i_traf_start_time
= BOXDATA(p_tfdt
)->i_base_media_decode_time
;
4490 b_has_base_media_decode_time
= true;
4493 /* Try using Tfxd for base offset (Smooth) */
4494 if( !b_has_base_media_decode_time
&& p_sys
->i_tracks
== 1 )
4496 const MP4_Box_t
*p_uuid
= MP4_BoxGet( p_traf
, "uuid" );
4497 for( ; p_uuid
; p_uuid
= p_uuid
->p_next
)
4499 if( p_uuid
->i_type
== ATOM_uuid
&&
4500 !CmpUUID( &p_uuid
->i_uuid
, &TfxdBoxUUID
) && p_uuid
->data
.p_tfxd
)
4502 i_traf_start_time
= p_uuid
->data
.p_tfxd
->i_fragment_abs_time
;
4503 b_has_base_media_decode_time
= true;
4509 /* After seek we should have probed fragments */
4510 if( !b_has_base_media_decode_time
&& p_sys
->p_fragsindex
)
4512 unsigned i_track_index
= (p_track
- p_sys
->track
);
4513 assert(&p_sys
->track
[i_track_index
] == p_track
);
4514 i_traf_start_time
= MP4_Fragment_Index_GetTrackStartTime( p_sys
->p_fragsindex
,
4515 i_track_index
, p_moof
->i_pos
);
4516 i_traf_start_time
= MP4_rescale( i_traf_start_time
,
4517 p_sys
->i_timescale
, p_track
->i_timescale
);
4518 b_has_base_media_decode_time
= true;
4521 if( !b_has_base_media_decode_time
&& p_chunksidx
)
4523 /* Try using SIDX as base offset.
4524 * This can not work for global sidx but only when sent within each fragment (dash) */
4525 const MP4_Box_data_sidx_t
*p_data
= p_chunksidx
->data
.p_sidx
;
4526 if( p_data
&& p_data
->i_timescale
&& p_data
->i_reference_count
== 1 )
4528 i_traf_start_time
= MP4_rescale( p_data
->i_earliest_presentation_time
,
4529 p_data
->i_timescale
, p_track
->i_timescale
);
4530 b_has_base_media_decode_time
= true;
4534 /* First contiguous segment (moov->moof) and there's no tfdt not probed index (yet) */
4535 if( !b_has_base_media_decode_time
&& FragGetMoofSequenceNumber( p_moof
) == 1 )
4537 i_traf_start_time
= MP4_rescale( GetMoovTrackDuration( p_sys
, p_track
->i_track_ID
),
4538 p_sys
->i_timescale
, p_track
->i_timescale
);
4539 b_has_base_media_decode_time
= true;
4542 /* Use global sidx moof time, in case moof does not carry tfdt */
4543 if( !b_has_base_media_decode_time
&& i_moof_time
!= INT64_MAX
)
4544 i_traf_start_time
= MP4_rescale( i_moof_time
, p_sys
->i_timescale
, p_track
->i_timescale
);
4546 /* That should not happen */
4547 if( !b_has_base_media_decode_time
)
4548 i_traf_start_time
= MP4_rescale( p_sys
->i_nztime
, CLOCK_FREQ
, p_track
->i_timescale
);
4551 /* Parse TRUN data */
4553 if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
)
4555 i_traf_base_data_offset
= BOXDATA(p_tfhd
)->i_base_data_offset
;
4557 else if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_DEFAULT_BASE_IS_MOOF
)
4559 i_traf_base_data_offset
= p_moof
->i_pos
/* + 8*/;
4564 i_traf_base_data_offset
= p_moof
->i_pos
/*+ 8*/;
4566 i_traf_base_data_offset
= i_prev_traf_end
;
4569 uint64_t i_trun_dts
= i_traf_start_time
;
4570 uint64_t i_trun_data_offset
= i_traf_base_data_offset
;
4571 uint32_t i_trun_size
= 0;
4573 for( const MP4_Box_t
*p_trun
= MP4_BoxGet( p_traf
, "trun" );
4574 p_trun
&& p_tfhd
; p_trun
= p_trun
->p_next
)
4576 if ( p_trun
->i_type
!= ATOM_trun
)
4579 const MP4_Box_data_trun_t
*p_trundata
= p_trun
->data
.p_trun
;
4581 /* Get data offset */
4582 if ( p_trundata
->i_flags
& MP4_TRUN_DATA_OFFSET
)
4584 /* Fix for broken Trun data offset relative to tfhd instead of moof, as seen in smooth */
4585 if( (BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
) == 0 &&
4587 i_traf_base_data_offset
+ p_trundata
->i_data_offset
< p_moof
->i_pos
+ p_moof
->i_size
+ 8 )
4589 i_trun_data_offset
+= p_moof
->i_size
+ 8;
4591 else if( (BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
) )
4593 i_trun_data_offset
= BOXDATA(p_tfhd
)->i_base_data_offset
+ p_trundata
->i_data_offset
;
4597 i_trun_data_offset
+= p_trundata
->i_data_offset
;
4602 i_trun_data_offset
+= i_trun_size
;
4608 "tk %u run %" PRIu32
" dflt dur %"PRIu32
" size %"PRIu32
" firstdts %"PRId64
" offset %"PRIu64
,
4609 p_track
->i_track_ID
,
4610 p_track
->context
.runs
.i_count
,
4611 i_track_defaultsampleduration
,
4612 i_track_defaultsamplesize
,
4613 MP4_rescale( i_trun_dts
, p_track
->i_timescale
, CLOCK_FREQ
), i_trun_data_offset
);
4616 mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_count
++];
4617 p_run
->i_first_dts
= i_trun_dts
;
4618 p_run
->i_offset
= i_trun_data_offset
;
4619 p_run
->p_trun
= p_trun
;
4622 /* Sum total time */
4623 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4625 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4626 i_trun_dts
+= p_trundata
->p_samples
[i
].i_duration
;
4630 i_trun_dts
+= p_trundata
->i_sample_count
*
4631 i_track_defaultsampleduration
;
4634 /* Get total traf size */
4635 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
4637 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4638 i_trun_size
+= p_trundata
->p_samples
[i
].i_size
;
4642 i_trun_size
+= p_trundata
->i_sample_count
*
4643 i_track_defaultsamplesize
;
4646 i_prev_traf_end
= i_trun_data_offset
+ i_trun_size
;
4655 static int FragGetMoofBySidxIndex( demux_t
*p_demux
, mtime_t i_target_time
,
4656 uint64_t *pi_moof_pos
, mtime_t
*pi_sampletime
)
4658 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4659 const MP4_Box_t
*p_sidx
= MP4_BoxGet( p_sys
->p_root
, "sidx" );
4660 const MP4_Box_data_sidx_t
*p_data
;
4661 if( !p_sidx
|| !((p_data
= BOXDATA(p_sidx
))) || !p_data
->i_timescale
)
4662 return VLC_EGENERIC
;
4664 i_target_time
= MP4_rescale( i_target_time
, CLOCK_FREQ
, p_data
->i_timescale
);
4666 /* sidx refers to offsets from end of sidx pos in the file + first offset */
4667 uint64_t i_pos
= p_data
->i_first_offset
+ p_sidx
->i_pos
+ p_sidx
->i_size
;
4669 for( uint16_t i
=0; i
<p_data
->i_reference_count
; i
++ )
4671 if( i_time
+ p_data
->p_items
[i
].i_subsegment_duration
> i_target_time
)
4673 *pi_sampletime
= MP4_rescale( i_time
, p_data
->i_timescale
, CLOCK_FREQ
);
4674 *pi_moof_pos
= i_pos
;
4677 i_pos
+= p_data
->p_items
[i
].i_referenced_size
;
4678 i_time
+= p_data
->p_items
[i
].i_subsegment_duration
;
4681 return VLC_EGENERIC
;
4684 static int FragGetMoofByTfraIndex( demux_t
*p_demux
, const mtime_t i_target_time
, unsigned i_track_ID
,
4685 uint64_t *pi_moof_pos
, mtime_t
*pi_sampletime
)
4687 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4688 MP4_Box_t
*p_tfra
= MP4_BoxGet( p_sys
->p_root
, "mfra/tfra" );
4689 for( ; p_tfra
; p_tfra
= p_tfra
->p_next
)
4691 if ( p_tfra
->i_type
== ATOM_tfra
)
4693 const MP4_Box_data_tfra_t
*p_data
= BOXDATA(p_tfra
);
4694 if( !p_data
|| p_data
->i_track_ID
!= i_track_ID
)
4698 mp4_track_t
*p_track
= MP4_GetTrackByTrackID( p_demux
, p_data
->i_track_ID
);
4701 stime_t i_track_target_time
= MP4_rescale( i_target_time
, CLOCK_FREQ
, p_track
->i_timescale
);
4702 for ( uint32_t i
= 0; i
<p_data
->i_number_of_entries
; i
+= ( p_data
->i_version
== 1 ) ? 2 : 1 )
4706 if ( p_data
->i_version
== 1 )
4708 i_time
= *((uint64_t *)(p_data
->p_time
+ i
));
4709 i_offset
= *((uint64_t *)(p_data
->p_moof_offset
+ i
));
4713 i_time
= p_data
->p_time
[i
];
4714 i_offset
= p_data
->p_moof_offset
[i
];
4717 if ( i_time
>= i_track_target_time
)
4719 if ( i_pos
== 0 ) /* Not in this traf */
4722 *pi_moof_pos
= i_pos
;
4723 *pi_sampletime
= MP4_rescale( i_time
, p_track
->i_timescale
, CLOCK_FREQ
);
4732 return VLC_EGENERIC
;
4735 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t
*p_moov
,
4736 const MP4_Box_data_tfhd_t
*p_tfhd_data
,
4737 uint32_t *pi_default_size
,
4738 uint32_t *pi_default_duration
)
4740 if( p_tfhd_data
->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
4741 *pi_default_duration
= p_tfhd_data
->i_default_sample_duration
;
4743 if( p_tfhd_data
->i_flags
& MP4_TFHD_DFLT_SAMPLE_SIZE
)
4744 *pi_default_size
= p_tfhd_data
->i_default_sample_size
;
4746 if( !*pi_default_duration
|| !*pi_default_size
)
4748 const MP4_Box_t
*p_trex
= MP4_GetTrexByTrackID( p_moov
, p_tfhd_data
->i_track_ID
);
4751 if ( !*pi_default_duration
)
4752 *pi_default_duration
= BOXDATA(p_trex
)->i_default_sample_duration
;
4753 if ( !*pi_default_size
)
4754 *pi_default_size
= BOXDATA(p_trex
)->i_default_sample_size
;
4759 static int DemuxFrag( demux_t
*p_demux
)
4761 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4762 unsigned i_track_selected
= 0;
4763 int i_status
= VLC_DEMUXER_SUCCESS
;
4765 if( unlikely(p_sys
->b_error
) )
4767 msg_Warn( p_demux
, "unrecoverable error" );
4768 i_status
= VLC_DEMUXER_EOF
;
4772 /* check for newly selected/unselected track */
4773 for( unsigned i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
4775 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
4778 if( !tk
->b_ok
|| tk
->b_chapters_source
)
4781 if( p_sys
->b_seekable
)
4782 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
, tk
->p_es
, &b
);
4784 if(tk
->b_selected
!= b
)
4786 msg_Dbg( p_demux
, "track %u %s!", tk
->i_track_ID
, b
? "enabled" : "disabled" );
4787 MP4_TrackSelect( p_demux
, tk
, b
);
4790 if( tk
->b_selected
)
4794 if( i_track_selected
<= 0 )
4796 msg_Warn( p_demux
, "no track selected, exiting..." );
4797 i_status
= VLC_DEMUXER_EOF
;
4801 if ( p_sys
->context
.i_current_box_type
!= ATOM_mdat
)
4803 /* Othewise mdat is skipped. FIXME: mdat reading ! */
4804 const uint8_t *p_peek
;
4805 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) != 8 )
4807 i_status
= VLC_DEMUXER_EOF
;
4811 p_sys
->context
.i_current_box_type
= VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] );
4812 if( p_sys
->context
.i_current_box_type
== ATOM_mdat
)
4814 p_sys
->context
.i_post_mdat_offset
= vlc_stream_Tell( p_demux
->s
) + GetDWBE( p_peek
);
4818 MP4_Box_t
*p_vroot
= MP4_BoxGetNextChunk( p_demux
->s
);
4821 i_status
= VLC_DEMUXER_EOF
;
4825 MP4_Box_t
*p_box
= NULL
;
4826 for( p_box
= p_vroot
->p_first
; p_box
; p_box
= p_box
->p_next
)
4828 if( p_box
->i_type
== ATOM_moof
||
4829 p_box
->i_type
== ATOM_moov
)
4835 FragResetContext( p_sys
);
4837 if( p_box
->i_type
== ATOM_moov
)
4839 p_sys
->context
.p_fragment_atom
= p_sys
->p_moov
;
4843 p_sys
->context
.p_fragment_atom
= MP4_BoxExtract( &p_vroot
->p_first
, p_box
->i_type
);
4845 /* Detect and Handle Passive Seek */
4846 const uint32_t i_sequence_number
= FragGetMoofSequenceNumber( p_sys
->context
.p_fragment_atom
);
4847 const bool b_discontinuity
= ( i_sequence_number
!= p_sys
->context
.i_lastseqnumber
+ 1 );
4848 if( b_discontinuity
)
4849 msg_Info( p_demux
, "Fragment sequence discontinuity detected %"PRIu32
" != %"PRIu32
,
4850 i_sequence_number
, p_sys
->context
.i_lastseqnumber
+ 1 );
4851 p_sys
->context
.i_lastseqnumber
= i_sequence_number
;
4854 if( FragPrepareChunk( p_demux
, p_sys
->context
.p_fragment_atom
,
4855 MP4_BoxGet( p_vroot
, "sidx"), INT64_MAX
,
4856 b_discontinuity
) != VLC_SUCCESS
)
4858 MP4_BoxFree( p_vroot
);
4859 i_status
= VLC_DEMUXER_EOF
;
4863 if( b_discontinuity
)
4865 p_sys
->i_nztime
= FragGetDemuxTimeFromTracksTime( p_sys
);
4866 p_sys
->i_pcr
= VLC_TS_INVALID
;
4868 /* !Prepare chunk */
4871 p_sys
->context
.i_current_box_type
= p_box
->i_type
;
4874 MP4_BoxFree( p_vroot
);
4876 if( p_sys
->context
.p_fragment_atom
== NULL
)
4878 msg_Info(p_demux
, "no moof or moov in current chunk");
4879 return VLC_DEMUXER_SUCCESS
;
4884 if ( p_sys
->context
.i_current_box_type
== ATOM_mdat
)
4886 assert(p_sys
->context
.p_fragment_atom
);
4888 if ( p_sys
->context
.p_fragment_atom
)
4889 switch( p_sys
->context
.p_fragment_atom
->i_type
)
4891 case ATOM_moov
://[ftyp/moov, mdat]+ -> [moof, mdat]+
4892 i_status
= DemuxMoov( p_demux
);
4895 i_status
= DemuxMoof( p_demux
);
4898 msg_Err( p_demux
, "fragment type %4.4s", (char*) &p_sys
->context
.p_fragment_atom
->i_type
);
4902 if( i_status
== VLC_DEMUXER_EOS
)
4904 i_status
= VLC_DEMUXER_SUCCESS
;
4905 /* Skip if we didn't reach the end of mdat box */
4906 uint64_t i_pos
= vlc_stream_Tell( p_demux
->s
);
4907 if( i_pos
!= p_sys
->context
.i_post_mdat_offset
&& i_status
!= VLC_DEMUXER_EOF
)
4909 if( i_pos
> p_sys
->context
.i_post_mdat_offset
)
4910 msg_Err( p_demux
, " Overread mdat by %" PRIu64
, i_pos
- p_sys
->context
.i_post_mdat_offset
);
4912 msg_Warn( p_demux
, "mdat had still %"PRIu64
" bytes unparsed as samples",
4913 p_sys
->context
.i_post_mdat_offset
- i_pos
);
4914 if( MP4_Seek( p_demux
->s
, p_sys
->context
.i_post_mdat_offset
) != VLC_SUCCESS
)
4915 i_status
= VLC_DEMUXER_EGENERIC
;
4917 p_sys
->context
.i_current_box_type
= 0;
4923 if( i_status
== VLC_DEMUXER_EOF
)
4925 mtime_t i_demux_end
= INT64_MIN
;
4926 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4928 const mp4_track_t
*tk
= &p_sys
->track
[i
];
4929 mtime_t i_track_end
= MP4_rescale( tk
->i_time
, tk
->i_timescale
, CLOCK_FREQ
);
4930 if( i_track_end
> i_demux_end
)
4931 i_demux_end
= i_track_end
;
4933 if( i_demux_end
!= INT64_MIN
)
4934 es_out_SetPCR( p_demux
->out
, VLC_TS_0
+ i_demux_end
);
4941 inline static mp4_track_t
*MP4ASF_GetTrack( asf_packet_sys_t
*p_packetsys
,
4942 uint8_t i_stream_number
)
4944 demux_sys_t
*p_sys
= p_packetsys
->p_demux
->p_sys
;
4945 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
4947 if ( p_sys
->track
[i
].p_asf
&&
4948 i_stream_number
== p_sys
->track
[i
].BOXDATA(p_asf
)->i_stream_number
)
4950 return &p_sys
->track
[i
];
4956 static asf_track_info_t
* MP4ASF_GetTrackInfo( asf_packet_sys_t
*p_packetsys
,
4957 uint8_t i_stream_number
)
4959 mp4_track_t
*p_track
= MP4ASF_GetTrack( p_packetsys
, i_stream_number
);
4961 return &p_track
->asfinfo
;
4966 static void MP4ASF_Send( asf_packet_sys_t
*p_packetsys
, uint8_t i_stream_number
,
4967 block_t
**pp_frame
)
4969 mp4_track_t
*p_track
= MP4ASF_GetTrack( p_packetsys
, i_stream_number
);
4972 block_Release( *pp_frame
);
4976 block_t
*p_gather
= block_ChainGather( *pp_frame
);
4977 p_gather
->i_dts
= p_track
->i_dts_backup
;
4978 p_gather
->i_pts
= p_track
->i_pts_backup
;
4979 es_out_Send( p_packetsys
->p_demux
->out
, p_track
->p_es
, p_gather
);
4985 static void MP4ASF_ResetFrames( demux_sys_t
*p_sys
)
4987 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
4989 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4990 if( p_track
->asfinfo
.p_frame
)
4992 block_ChainRelease( p_track
->asfinfo
.p_frame
);
4993 p_track
->asfinfo
.p_frame
= NULL
;