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>
39 #include "../codec/cc.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t
* );
45 static void Close( vlc_object_t
* );
47 #define CFG_PREFIX "mp4-"
49 #define MP4_M4A_TEXT "M4A audio only"
50 #define MP4_M4A_LONGTEXT "Ignore non audio tracks from iTunes audio files"
53 set_category( CAT_INPUT
)
54 set_subcategory( SUBCAT_INPUT_DEMUX
)
55 set_description( N_("MP4 stream demuxer") )
56 set_shortname( N_("MP4") )
57 set_capability( "demux", 240 )
58 set_callbacks( Open
, Close
)
60 add_category_hint("Hacks", NULL
, true)
61 add_bool( CFG_PREFIX
"m4a-audioonly", false, MP4_M4A_TEXT
, MP4_M4A_LONGTEXT
, true )
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Demux ( demux_t
* );
68 static int DemuxRef( demux_t
*p_demux
){ (void)p_demux
; return 0;}
69 static int DemuxFrag( demux_t
* );
70 static int Control ( demux_t
*, int, va_list );
74 MP4_Box_t
*p_root
; /* container for the whole file */
78 uint64_t i_moov_duration
;
79 uint64_t i_duration
; /* Declared fragmented duration */
80 uint64_t i_cumulated_duration
; /* Same as above, but not from probing */
81 uint64_t i_time
; /* time position of the presentation
82 * in movie timescale */
83 uint32_t i_timescale
; /* movie time scale */
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
;
110 input_title_t
*p_title
;
114 asf_packet_sys_t asfpacketsys
;
115 uint64_t i_preroll
; /* foobar */
116 int64_t i_preroll_start
;
123 mp4_fragments_index_t
*p_fragsindex
;
126 #define DEMUX_INCREMENT (CLOCK_FREQ / 4) /* How far the pcr will go, each round */
127 #define DEMUX_TRACK_MAX_PRELOAD (CLOCK_FREQ * 15) /* maximum preloading, to deal with interleaving */
129 #define VLC_DEMUXER_EOS (VLC_DEMUXER_EGENERIC - 1)
131 const uint32_t rgi_pict_atoms
[2] = { ATOM_PICT
, ATOM_pict
};
132 const char *psz_meta_roots
[] = { "/moov/udta/meta/ilst",
140 /*****************************************************************************
141 * Declaration of local function
142 *****************************************************************************/
143 static void MP4_TrackSetup( demux_t
*, mp4_track_t
*, MP4_Box_t
*, bool, bool );
144 static void MP4_TrackInit( mp4_track_t
* );
145 static void MP4_TrackClean( es_out_t
*, mp4_track_t
* );
147 static void MP4_Block_Send( demux_t
*, mp4_track_t
*, block_t
* );
149 static void MP4_TrackSelect ( demux_t
*, mp4_track_t
*, bool );
150 static int MP4_TrackSeek ( demux_t
*, mp4_track_t
*, mtime_t
);
152 static uint64_t MP4_TrackGetPos ( mp4_track_t
* );
153 static uint32_t MP4_TrackGetReadSize( mp4_track_t
*, uint32_t * );
154 static int MP4_TrackNextSample( demux_t
*, mp4_track_t
*, uint32_t );
155 static void MP4_TrackSetELST( demux_t
*, mp4_track_t
*, int64_t );
157 static void MP4_UpdateSeekpoint( demux_t
*, int64_t );
159 static MP4_Box_t
* MP4_GetTrexByTrackID( MP4_Box_t
*p_moov
, const uint32_t i_id
);
160 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t
*p_moov
,
161 const MP4_Box_data_tfhd_t
*p_tfhd_data
,
162 uint32_t *pi_default_size
,
163 uint32_t *pi_default_duration
);
165 static stime_t
GetMoovTrackDuration( demux_sys_t
*p_sys
, unsigned i_track_ID
);
167 static int ProbeFragments( demux_t
*p_demux
, bool b_force
, bool *pb_fragmented
);
168 static int ProbeIndex( demux_t
*p_demux
);
170 static int FragCreateTrunIndex( demux_t
*, MP4_Box_t
*, MP4_Box_t
*, stime_t
, bool );
172 static int FragGetMoofBySidxIndex( demux_t
*p_demux
, mtime_t i_target_time
,
173 uint64_t *pi_moof_pos
, mtime_t
*pi_sampletime
);
174 static int FragGetMoofByTfraIndex( demux_t
*p_demux
, const mtime_t i_target_time
, unsigned i_track_ID
,
175 uint64_t *pi_moof_pos
, mtime_t
*pi_sampletime
);
176 static void FragResetContext( demux_sys_t
* );
179 static asf_track_info_t
* MP4ASF_GetTrackInfo( asf_packet_sys_t
*p_packetsys
, uint8_t i_stream_number
);
180 static void MP4ASF_Send(asf_packet_sys_t
*p_packetsys
, uint8_t i_stream_number
, block_t
**pp_frame
);
181 static void MP4ASF_ResetFrames( demux_sys_t
*p_sys
);
184 static block_t
* MP4_RTPHint_Convert( demux_t
*p_demux
, block_t
*p_block
, vlc_fourcc_t i_codec
);
185 static block_t
* MP4_RTPHintToFrame( demux_t
*p_demux
, block_t
*p_block
, uint32_t packetcount
);
187 static int MP4_LoadMeta( demux_sys_t
*p_sys
, vlc_meta_t
*p_meta
);
191 static int64_t MP4_rescale( int64_t i_value
, uint32_t i_timescale
, uint32_t i_newscale
)
193 if( i_timescale
== i_newscale
)
196 if( i_value
<= INT64_MAX
/ i_newscale
)
197 return i_value
* i_newscale
/ i_timescale
;
200 int64_t q
= i_value
/ i_timescale
;
201 int64_t r
= i_value
% i_timescale
;
202 return q
* i_newscale
+ r
* i_newscale
/ i_timescale
;
205 static uint32_t stream_ReadU32( stream_t
*s
, void *p_read
, uint32_t i_toread
)
207 ssize_t i_return
= 0;
208 if ( i_toread
> INT32_MAX
)
210 i_return
= vlc_stream_Read( s
, p_read
, (size_t) INT32_MAX
);
211 if ( i_return
< INT32_MAX
)
214 i_toread
-= INT32_MAX
;
216 i_return
+= vlc_stream_Read( s
, (uint8_t *)p_read
+ i_return
, (size_t) i_toread
);
220 static MP4_Box_t
* MP4_GetTrexByTrackID( MP4_Box_t
*p_moov
, const uint32_t i_id
)
224 MP4_Box_t
*p_trex
= MP4_BoxGet( p_moov
, "mvex/trex" );
227 if ( p_trex
->i_type
== ATOM_trex
&&
228 BOXDATA(p_trex
) && BOXDATA(p_trex
)->i_track_ID
== i_id
)
231 p_trex
= p_trex
->p_next
;
237 * Return the track identified by tid
239 static mp4_track_t
*MP4_GetTrackByTrackID( demux_t
*p_demux
, const uint32_t tid
)
241 demux_sys_t
*p_sys
= p_demux
->p_sys
;
243 mp4_track_t
*ret
= NULL
;
244 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
246 ret
= &p_sys
->track
[i
];
247 if( ret
->i_track_ID
== tid
)
253 static MP4_Box_t
* MP4_GetTrakByTrackID( MP4_Box_t
*p_moov
, const uint32_t i_id
)
255 MP4_Box_t
*p_trak
= MP4_BoxGet( p_moov
, "trak" );
259 if( p_trak
->i_type
== ATOM_trak
&&
260 (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) && BOXDATA(p_tkhd
) &&
261 BOXDATA(p_tkhd
)->i_track_ID
== i_id
)
264 p_trak
= p_trak
->p_next
;
269 static MP4_Box_t
* MP4_GetTrafByTrackID( MP4_Box_t
*p_moof
, const uint32_t i_id
)
271 MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
275 if( p_traf
->i_type
== ATOM_traf
&&
276 (p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" )) && BOXDATA(p_tfhd
) &&
277 BOXDATA(p_tfhd
)->i_track_ID
== i_id
)
280 p_traf
= p_traf
->p_next
;
285 static es_out_id_t
* MP4_AddTrackES( es_out_t
*out
, mp4_track_t
*p_track
)
287 es_out_id_t
*p_es
= es_out_Add( out
, &p_track
->fmt
);
288 /* Force SPU which isn't selected/defaulted */
289 if( p_track
->fmt
.i_cat
== SPU_ES
&& p_es
&& p_track
->b_forced_spu
)
290 es_out_Control( out
, ES_OUT_SET_ES_DEFAULT
, p_es
);
295 /* Return time in microsecond of a track */
296 static inline int64_t MP4_TrackGetDTS( demux_t
*p_demux
, mp4_track_t
*p_track
)
298 demux_sys_t
*p_sys
= p_demux
->p_sys
;
299 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[p_track
->i_chunk
];
301 unsigned int i_index
= 0;
302 unsigned int i_sample
= p_track
->i_sample
- p_chunk
->i_sample_first
;
303 int64_t i_dts
= p_chunk
->i_first_dts
;
305 while( i_sample
> 0 && i_index
< p_chunk
->i_entries_dts
)
307 if( i_sample
> p_chunk
->p_sample_count_dts
[i_index
] )
309 i_dts
+= p_chunk
->p_sample_count_dts
[i_index
] *
310 p_chunk
->p_sample_delta_dts
[i_index
];
311 i_sample
-= p_chunk
->p_sample_count_dts
[i_index
];
316 i_dts
+= i_sample
* p_chunk
->p_sample_delta_dts
[i_index
];
321 /* now handle elst */
322 if( p_track
->p_elst
)
324 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
326 /* convert to offset */
327 if( ( elst
->i_media_rate_integer
[p_track
->i_elst
] > 0 ||
328 elst
->i_media_rate_fraction
[p_track
->i_elst
] > 0 ) &&
329 elst
->i_media_time
[p_track
->i_elst
] > 0 )
331 i_dts
-= elst
->i_media_time
[p_track
->i_elst
];
334 /* add i_elst_time */
335 i_dts
+= MP4_rescale( p_track
->i_elst_time
, p_sys
->i_timescale
, p_track
->i_timescale
);
337 if( i_dts
< 0 ) i_dts
= 0;
340 return MP4_rescale( i_dts
, p_track
->i_timescale
, CLOCK_FREQ
);
343 static inline bool MP4_TrackGetPTSDelta( demux_t
*p_demux
, mp4_track_t
*p_track
,
346 VLC_UNUSED( p_demux
);
347 mp4_chunk_t
*ck
= &p_track
->chunk
[p_track
->i_chunk
];
349 unsigned int i_index
= 0;
350 unsigned int i_sample
= p_track
->i_sample
- ck
->i_sample_first
;
352 if( ck
->p_sample_count_pts
== NULL
|| ck
->p_sample_offset_pts
== NULL
)
355 for( i_index
= 0; i_index
< ck
->i_entries_pts
; i_index
++ )
357 if( i_sample
< ck
->p_sample_count_pts
[i_index
] )
359 *pi_delta
= MP4_rescale( ck
->p_sample_offset_pts
[i_index
],
360 p_track
->i_timescale
, CLOCK_FREQ
);
364 i_sample
-= ck
->p_sample_count_pts
[i_index
];
369 static inline int64_t MP4_GetMoviePTS(demux_sys_t
*p_sys
)
371 return MP4_rescale( p_sys
->i_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
374 static void LoadChapter( demux_t
*p_demux
);
376 static int LoadInitFrag( demux_t
*p_demux
)
378 demux_sys_t
*p_sys
= p_demux
->p_sys
;
380 /* Load all boxes ( except raw data ) */
381 if( ( p_sys
->p_root
= MP4_BoxGetRoot( p_demux
->s
) ) == NULL
)
383 goto LoadInitFragError
;
389 msg_Warn( p_demux
, "MP4 plugin discarded (not a valid initialization chunk)" );
393 static int CreateTracks( demux_t
*p_demux
, unsigned i_tracks
)
395 demux_sys_t
*p_sys
= p_demux
->p_sys
;
397 if( SIZE_MAX
/ i_tracks
< sizeof(mp4_track_t
) )
400 p_sys
->track
= vlc_alloc( i_tracks
, sizeof(mp4_track_t
) );
401 if( p_sys
->track
== NULL
)
403 p_sys
->i_tracks
= i_tracks
;
405 for( unsigned i
=0; i
<i_tracks
; i
++ )
406 MP4_TrackInit( &p_sys
->track
[i
] );
411 static block_t
* MP4_EIA608_Convert( block_t
* p_block
)
413 /* Rebuild codec data from encap */
415 size_t i_remaining
= __MIN(p_block
->i_buffer
, INT64_MAX
/ 3);
416 uint32_t i_bytes
= 0;
419 /* always need at least 10 bytes (atom size+header+1pair)*/
420 if ( i_remaining
< 10 ||
421 !(i_bytes
= GetDWBE(p_block
->p_buffer
)) ||
422 (i_bytes
> i_remaining
) ||
423 memcmp("cdat", &p_block
->p_buffer
[4], 4) ||
424 !(p_newblock
= block_Alloc( i_remaining
* 3 - 8 )) )
426 p_block
->i_buffer
= 0;
430 uint8_t *p_write
= p_newblock
->p_buffer
;
431 uint8_t *p_read
= &p_block
->p_buffer
[8];
437 p_write
[i_copied
++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
438 p_write
[i_copied
++] = p_read
[0];
439 p_write
[i_copied
++] = p_read
[1];
443 } while( i_bytes
>= 2 );
445 /* cdt2 is optional */
446 if ( i_remaining
>= 10 &&
447 (i_bytes
= GetDWBE(p_read
)) &&
448 (i_bytes
<= i_remaining
) &&
449 !memcmp("cdt2", &p_read
[4], 4) )
456 p_write
[i_copied
++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
457 p_write
[i_copied
++] = p_read
[0];
458 p_write
[i_copied
++] = p_read
[1];
461 } while( i_bytes
>= 2 );
464 p_newblock
->i_pts
= p_block
->i_dts
;
465 p_newblock
->i_buffer
= i_copied
;
466 p_newblock
->i_flags
= BLOCK_FLAG_TYPE_P
;
467 block_Release( p_block
);
472 static uint32_t MP4_TrackGetRunSeq( mp4_track_t
*p_track
)
474 if( p_track
->i_chunk_count
> 0 )
475 return p_track
->chunk
[p_track
->i_chunk
].i_virtual_run_number
;
479 /* Analyzes chunks to find max interleave length
480 * sets flat flag if no interleaving is in use */
481 static void MP4_GetInterleaving( demux_t
*p_demux
, uint64_t *pi_max_contiguous
, bool *pb_flat
)
483 demux_sys_t
*p_sys
= p_demux
->p_sys
;
484 *pi_max_contiguous
= 0;
487 /* Find first recorded chunk */
488 mp4_track_t
*tk
= NULL
;
489 uint64_t i_duration
= 0;
490 for( unsigned i
=0; i
< p_sys
->i_tracks
; i
++ )
492 mp4_track_t
*cur
= &p_sys
->track
[i
];
493 if( !cur
->i_chunk_count
)
496 if( tk
== NULL
|| cur
->chunk
[0].i_offset
< tk
->chunk
[0].i_offset
)
502 i_duration
+= tk
->chunk
[tk
->i_chunk
].i_duration
;
505 /* Find next chunk in data order */
506 mp4_track_t
*nexttk
= NULL
;
507 for( unsigned i
=0; i
< p_sys
->i_tracks
; i
++ )
509 mp4_track_t
*cur
= &p_sys
->track
[i
];
510 if( cur
->i_chunk
== cur
->i_chunk_count
)
513 if( nexttk
== NULL
||
514 cur
->chunk
[cur
->i_chunk
].i_offset
< nexttk
->chunk
[nexttk
->i_chunk
].i_offset
)
518 /* copy previous run */
519 if( nexttk
&& nexttk
->i_chunk
> 0 )
520 nexttk
->chunk
[nexttk
->i_chunk
].i_virtual_run_number
=
521 nexttk
->chunk
[nexttk
->i_chunk
- 1].i_virtual_run_number
;
525 i_duration
= MP4_rescale( i_duration
, tk
->i_timescale
, CLOCK_FREQ
);
526 if( i_duration
> *pi_max_contiguous
)
527 *pi_max_contiguous
= i_duration
;
530 if( tk
->i_chunk
!= tk
->i_chunk_count
)
533 if( nexttk
&& nexttk
->i_chunk
> 0 ) /* new run number */
534 nexttk
->chunk
[nexttk
->i_chunk
].i_virtual_run_number
++;
541 for( unsigned i
=0; i
< p_sys
->i_tracks
; i
++ )
542 p_sys
->track
[i
].i_chunk
= 0;
545 static block_t
* MP4_Block_Convert( demux_t
*p_demux
, const mp4_track_t
*p_track
, block_t
*p_block
)
547 /* might have some encap */
548 if( p_track
->fmt
.i_cat
== SPU_ES
)
550 switch( p_track
->fmt
.i_codec
)
552 case VLC_CODEC_WEBVTT
:
559 case VLC_CODEC_CEA608
:
560 p_block
= MP4_EIA608_Convert( p_block
);
563 p_block
->i_buffer
= 0;
567 else if( p_track
->fmt
.i_original_fourcc
== ATOM_rrtp
)
569 p_block
= MP4_RTPHint_Convert( p_demux
, p_block
, p_track
->fmt
.i_codec
);
575 static void MP4_Block_Send( demux_t
*p_demux
, mp4_track_t
*p_track
, block_t
*p_block
)
577 p_block
= MP4_Block_Convert( p_demux
, p_track
, p_block
);
578 if( p_block
== NULL
)
581 if ( p_track
->b_chans_reorder
)
583 aout_ChannelReorder( p_block
->p_buffer
, p_block
->i_buffer
,
584 p_track
->fmt
.audio
.i_channels
,
585 p_track
->rgi_chans_reordering
,
586 p_track
->fmt
.i_codec
);
589 p_block
->i_flags
|= p_track
->i_block_flags
;
590 if( p_track
->i_next_block_flags
)
592 p_block
->i_flags
|= p_track
->i_next_block_flags
;
593 p_track
->i_next_block_flags
= 0;
596 /* ASF packets in mov */
599 /* Fake a new stream from MP4 block */
600 stream_t
*p_stream
= p_demux
->s
;
601 p_demux
->s
= vlc_stream_MemoryNew( p_demux
, p_block
->p_buffer
, p_block
->i_buffer
, true );
604 p_track
->i_dts_backup
= p_block
->i_dts
;
605 p_track
->i_pts_backup
= p_block
->i_pts
;
606 /* And demux it as ASF packet */
607 DemuxASFPacket( &p_demux
->p_sys
->asfpacketsys
, p_block
->i_buffer
, p_block
->i_buffer
);
608 vlc_stream_Delete(p_demux
->s
);
610 block_Release(p_block
);
611 p_demux
->s
= p_stream
;
614 es_out_Send( p_demux
->out
, p_track
->p_es
, p_block
);
617 /*****************************************************************************
618 * Open: check file and initializes MP4 structures
619 *****************************************************************************/
620 static int Open( vlc_object_t
* p_this
)
622 demux_t
*p_demux
= (demux_t
*)p_this
;
625 const uint8_t *p_peek
;
628 const MP4_Box_t
*p_mvhd
= NULL
;
629 const MP4_Box_t
*p_mvex
= NULL
;
633 /* A little test to see if it could be a mp4 */
634 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 11 ) < 11 ) return VLC_EGENERIC
;
636 switch( VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) )
647 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
650 /* We don't yet support f4v, but avformat does. */
651 if( p_peek
[8] == 'f' && p_peek
[9] == '4' && p_peek
[10] == 'v' )
658 /* create our structure that will contains all data */
659 p_sys
= calloc( 1, sizeof( demux_sys_t
) );
664 vlc_stream_Control( p_demux
->s
, STREAM_CAN_SEEK
, &p_sys
->b_seekable
);
665 if( p_sys
->b_seekable
)
666 vlc_stream_Control( p_demux
->s
, STREAM_CAN_FASTSEEK
, &p_sys
->b_fastseekable
);
668 /*Set exported functions */
669 p_demux
->pf_demux
= Demux
;
670 p_demux
->pf_control
= Control
;
672 p_sys
->context
.i_lastseqnumber
= UINT32_MAX
;
674 p_demux
->p_sys
= p_sys
;
676 if( LoadInitFrag( p_demux
) != VLC_SUCCESS
)
679 MP4_BoxDumpStructure( p_demux
->s
, p_sys
->p_root
);
681 if( ( p_ftyp
= MP4_BoxGet( p_sys
->p_root
, "/ftyp" ) ) )
683 switch( BOXDATA(p_ftyp
)->i_major_brand
)
687 "ISO Media (isom) version %d.",
688 BOXDATA(p_ftyp
)->i_minor_version
);
694 msg_Dbg( p_demux
, "3GPP Media Release: %4.4s",
695 (char *)&BOXDATA(p_ftyp
)->i_major_brand
);
698 msg_Dbg( p_demux
, "Apple QuickTime media" );
701 msg_Dbg( p_demux
, "PIFF (= isml = fMP4) media" );
704 msg_Dbg( p_demux
, "DASH Stream" );
707 msg_Dbg( p_demux
, "iTunes audio" );
708 if( var_InheritBool( p_demux
, CFG_PREFIX
"m4a-audioonly" ) )
709 p_sys
->hacks
.es_cat_filters
= AUDIO_ES
;
713 "unrecognized major media specification (%4.4s).",
714 (char*)&BOXDATA(p_ftyp
)->i_major_brand
);
717 /* also lookup in compatibility list */
718 for(uint32_t i
=0; i
<BOXDATA(p_ftyp
)->i_compatible_brands_count
; i
++)
720 if (BOXDATA(p_ftyp
)->i_compatible_brands
[i
] == MAJOR_dash
)
722 msg_Dbg( p_demux
, "DASH Stream" );
724 else if (BOXDATA(p_ftyp
)->i_compatible_brands
[i
] == VLC_FOURCC('s', 'm', 'o', 'o') )
726 msg_Dbg( p_demux
, "Handling VLC Smooth Stream" );
732 msg_Dbg( p_demux
, "file type box missing (assuming ISO Media)" );
735 /* the file need to have one moov box */
736 p_sys
->p_moov
= MP4_BoxGet( p_sys
->p_root
, "/moov" );
737 if( unlikely(!p_sys
->p_moov
) )
739 p_sys
->p_moov
= MP4_BoxGet( p_sys
->p_root
, "/foov" );
742 msg_Err( p_demux
, "MP4 plugin discarded (no moov,foov,moof box)" );
745 /* we have a free box as a moov, rename it */
746 p_sys
->p_moov
->i_type
= ATOM_moov
;
749 p_mvhd
= MP4_BoxGet( p_sys
->p_moov
, "mvhd" );
750 if( p_mvhd
&& BOXDATA(p_mvhd
) && BOXDATA(p_mvhd
)->i_timescale
)
752 p_sys
->i_timescale
= BOXDATA(p_mvhd
)->i_timescale
;
753 p_sys
->i_moov_duration
= p_sys
->i_duration
= BOXDATA(p_mvhd
)->i_duration
;
754 p_sys
->i_cumulated_duration
= BOXDATA(p_mvhd
)->i_duration
;
758 msg_Warn( p_demux
, "No valid mvhd found" );
762 MP4_Box_t
*p_rmra
= MP4_BoxGet( p_sys
->p_root
, "/moov/rmra" );
763 if( p_rmra
!= NULL
&& p_demux
->p_input
!= NULL
)
765 int i_count
= MP4_BoxCount( p_rmra
, "rmda" );
768 msg_Dbg( p_demux
, "detected playlist mov file (%d ref)", i_count
);
770 input_thread_t
*p_input
= p_demux
->p_input
;
771 input_item_t
*p_current
= input_GetItem( p_input
);
773 input_item_node_t
*p_subitems
= input_item_node_Create( p_current
);
775 for( i
= 0; i
< i_count
; i
++ )
777 MP4_Box_t
*p_rdrf
= MP4_BoxGet( p_rmra
, "rmda[%d]/rdrf", i
);
781 if( !p_rdrf
|| !BOXDATA(p_rdrf
) || !( psz_ref
= strdup( BOXDATA(p_rdrf
)->psz_ref
) ) )
785 i_ref_type
= BOXDATA(p_rdrf
)->i_ref_type
;
787 msg_Dbg( p_demux
, "new ref=`%s' type=%4.4s",
788 psz_ref
, (char*)&i_ref_type
);
790 if( i_ref_type
== VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
792 if( strstr( psz_ref
, "qt5gateQT" ) )
794 msg_Dbg( p_demux
, "ignoring pseudo ref =`%s'", psz_ref
);
798 if( !strncmp( psz_ref
, "http://", 7 ) ||
799 !strncmp( psz_ref
, "rtsp://", 7 ) )
806 char *psz_path
= strdup( p_demux
->psz_location
);
807 char *end
= strrchr( psz_path
, '/' );
808 if( end
) end
[1] = '\0';
809 else *psz_path
= '\0';
811 if( asprintf( &psz_absolute
, "%s://%s%s",
812 p_demux
->psz_access
, psz_path
, psz_ref
) < 0 )
816 input_item_node_Delete( p_subitems
);
821 psz_ref
= psz_absolute
;
824 msg_Dbg( p_demux
, "adding ref = `%s'", psz_ref
);
825 input_item_t
*p_item
= input_item_New( psz_ref
, NULL
);
826 input_item_CopyOptions( p_item
, p_current
);
827 input_item_node_AppendItem( p_subitems
, p_item
);
828 input_item_Release( p_item
);
832 msg_Err( p_demux
, "unknown ref type=%4.4s FIXME (send a bug report)",
833 (char*)&BOXDATA(p_rdrf
)->i_ref_type
);
838 /* FIXME: create a stream_filter sub-module for this */
839 if (es_out_Control(p_demux
->out
, ES_OUT_POST_SUBNODE
, p_subitems
))
840 input_item_node_Delete(p_subitems
);
843 if( !(p_mvhd
= MP4_BoxGet( p_sys
->p_root
, "/moov/mvhd" ) ) )
847 msg_Err( p_demux
, "cannot find /moov/mvhd" );
852 msg_Warn( p_demux
, "cannot find /moov/mvhd (pure ref file)" );
853 p_demux
->pf_demux
= DemuxRef
;
859 p_sys
->i_timescale
= BOXDATA(p_mvhd
)->i_timescale
;
860 if( p_sys
->i_timescale
== 0 )
862 msg_Err( p_this
, "bad timescale" );
867 const unsigned i_tracks
= MP4_BoxCount( p_sys
->p_root
, "/moov/trak" );
870 msg_Err( p_demux
, "cannot find any /moov/trak" );
873 msg_Dbg( p_demux
, "found %u track%c", i_tracks
, i_tracks
? 's':' ' );
875 if( CreateTracks( p_demux
, i_tracks
) != VLC_SUCCESS
)
878 /* Search the first chap reference (like quicktime) and
879 * check that at least 1 stream is enabled */
880 p_sys
->p_tref_chap
= NULL
;
881 b_enabled_es
= false;
882 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
884 MP4_Box_t
*p_trak
= MP4_BoxGet( p_sys
->p_root
, "/moov/trak[%d]", i
);
887 MP4_Box_t
*p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" );
888 if( p_tkhd
&& BOXDATA(p_tkhd
) && (BOXDATA(p_tkhd
)->i_flags
&MP4_TRACK_ENABLED
) )
891 MP4_Box_t
*p_chap
= MP4_BoxGet( p_trak
, "tref/chap", i
);
892 if( p_chap
&& p_chap
->data
.p_tref_generic
&&
893 p_chap
->data
.p_tref_generic
->i_entry_count
> 0 && !p_sys
->p_tref_chap
)
894 p_sys
->p_tref_chap
= p_chap
;
897 /* Set and store metadata */
898 if( (p_sys
->p_meta
= vlc_meta_New()) )
899 MP4_LoadMeta( p_sys
, p_sys
->p_meta
);
901 /* now process each track and extract all useful information */
902 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
904 MP4_Box_t
*p_trak
= MP4_BoxGet( p_sys
->p_root
, "/moov/trak[%u]", i
);
905 MP4_TrackSetup( p_demux
, &p_sys
->track
[i
], p_trak
, true, !b_enabled_es
);
907 if( p_sys
->track
[i
].b_ok
&& !p_sys
->track
[i
].b_chapters_source
)
910 switch( p_sys
->track
[i
].fmt
.i_cat
)
919 psz_cat
= "subtitle";
927 msg_Dbg( p_demux
, "adding track[Id 0x%x] %s (%s) language %s",
928 p_sys
->track
[i
].i_track_ID
, psz_cat
,
929 p_sys
->track
[i
].b_enable
? "enable":"disable",
930 p_sys
->track
[i
].fmt
.psz_language
?
931 p_sys
->track
[i
].fmt
.psz_language
: "undef" );
933 else if( p_sys
->track
[i
].b_ok
&& p_sys
->track
[i
].b_chapters_source
)
935 msg_Dbg( p_demux
, "using track[Id 0x%x] for chapter language %s",
936 p_sys
->track
[i
].i_track_ID
,
937 p_sys
->track
[i
].fmt
.psz_language
?
938 p_sys
->track
[i
].fmt
.psz_language
: "undef" );
942 msg_Dbg( p_demux
, "ignoring track[Id 0x%x]",
943 p_sys
->track
[i
].i_track_ID
);
947 p_mvex
= MP4_BoxGet( p_sys
->p_moov
, "mvex" );
950 const MP4_Box_t
*p_mehd
= MP4_BoxGet( p_mvex
, "mehd");
951 if ( p_mehd
&& BOXDATA(p_mehd
) )
953 if( BOXDATA(p_mehd
)->i_fragment_duration
> p_sys
->i_duration
)
955 p_sys
->b_fragmented
= true;
956 p_sys
->i_duration
= BOXDATA(p_mehd
)->i_fragment_duration
;
960 const MP4_Box_t
*p_sidx
= MP4_BoxGet( p_sys
->p_root
, "sidx");
962 p_sys
->b_fragmented
= true;
964 if ( p_sys
->b_seekable
)
966 if( !p_sys
->b_fragmented
/* as unknown */ )
968 /* Probe remaining to check if there's really fragments
969 or if that file is just ready to append fragments */
970 ProbeFragments( p_demux
, (p_sys
->i_duration
== 0), &p_sys
->b_fragmented
);
973 if( vlc_stream_Seek( p_demux
->s
, p_sys
->p_moov
->i_pos
) != VLC_SUCCESS
)
976 else /* Handle as fragmented by default as we can't see moof */
978 p_sys
->context
.p_fragment_atom
= p_sys
->p_moov
;
979 p_sys
->context
.i_current_box_type
= ATOM_moov
;
980 p_sys
->b_fragmented
= true;
984 if( p_sys
->b_fragmented
)
986 p_demux
->pf_demux
= DemuxFrag
;
987 msg_Dbg( p_demux
, "Set Fragmented demux mode" );
990 if( !p_sys
->b_seekable
&& p_demux
->pf_demux
== Demux
)
992 msg_Warn( p_demux
, "MP4 plugin discarded (not seekable)" );
996 if( p_sys
->i_tracks
> 1 && !p_sys
->b_fastseekable
)
998 uint64_t i_max_continuity
;
1000 MP4_GetInterleaving( p_demux
, &i_max_continuity
, &b_flat
);
1002 msg_Warn( p_demux
, "that media doesn't look interleaved, will need to seek");
1003 else if( i_max_continuity
> DEMUX_TRACK_MAX_PRELOAD
)
1004 msg_Warn( p_demux
, "that media doesn't look properly interleaved, will need to seek");
1008 LoadChapter( p_demux
);
1010 p_sys
->asfpacketsys
.p_demux
= p_demux
;
1011 p_sys
->asfpacketsys
.pi_preroll
= &p_sys
->i_preroll
;
1012 p_sys
->asfpacketsys
.pi_preroll_start
= &p_sys
->i_preroll_start
;
1013 p_sys
->asfpacketsys
.pf_doskip
= NULL
;
1014 p_sys
->asfpacketsys
.pf_send
= MP4ASF_Send
;
1015 p_sys
->asfpacketsys
.pf_gettrackinfo
= MP4ASF_GetTrackInfo
;
1016 p_sys
->asfpacketsys
.pf_updatetime
= NULL
;
1017 p_sys
->asfpacketsys
.pf_setaspectratio
= NULL
;
1022 if( vlc_stream_Tell( p_demux
->s
) > 0 )
1024 if( vlc_stream_Seek( p_demux
->s
, 0 ) != VLC_SUCCESS
)
1025 msg_Warn( p_demux
, "Can't reset stream position from probing" );
1030 return VLC_EGENERIC
;
1033 const unsigned int SAMPLEHEADERSIZE
= 4;
1034 const unsigned int RTPPACKETSIZE
= 12;
1035 const unsigned int CONSTRUCTORSIZE
= 16;
1037 /*******************************************************************************
1038 * MP4_RTPHintToFrame: converts RTP Reception Hint Track sample to H.264 frame
1039 *******************************************************************************/
1040 static block_t
* MP4_RTPHintToFrame( demux_t
*p_demux
, block_t
*p_block
, uint32_t packetcount
)
1042 uint8_t *p_slice
= p_block
->p_buffer
+ SAMPLEHEADERSIZE
;
1043 block_t
*p_newblock
= NULL
;
1044 size_t i_payload
= 0;
1046 if( p_block
->i_buffer
< SAMPLEHEADERSIZE
+ RTPPACKETSIZE
+ CONSTRUCTORSIZE
)
1048 msg_Err( p_demux
, "Sample not large enough for necessary structs");
1049 block_Release( p_block
);
1053 for( uint32_t i
= 0; i
< packetcount
; ++i
)
1055 if( (size_t)(p_slice
- p_block
->p_buffer
) + RTPPACKETSIZE
+ CONSTRUCTORSIZE
> p_block
->i_buffer
)
1058 /* skip RTP header in sample. Could be used to detect packet losses */
1059 p_slice
+= RTPPACKETSIZE
;
1061 mp4_rtpsampleconstructor_t sample_cons
;
1063 sample_cons
.type
= p_slice
[0];
1064 sample_cons
.trackrefindex
= p_slice
[1];
1065 sample_cons
.length
= GetWBE( &p_slice
[2] );
1066 sample_cons
.samplenumber
= GetDWBE( &p_slice
[4] );
1067 sample_cons
.sampleoffset
= GetDWBE( &p_slice
[8] );
1068 sample_cons
.bytesperblock
= GetWBE( &p_slice
[12] );
1069 sample_cons
.samplesperblock
= GetWBE( &p_slice
[14] );
1071 /* skip packet constructor */
1072 p_slice
+= CONSTRUCTORSIZE
;
1074 /* check that is RTPsampleconstructor, referencing itself and no weird audio stuff */
1075 if( sample_cons
.type
!= 2||sample_cons
.trackrefindex
!= -1
1076 ||sample_cons
.samplesperblock
!= 1||sample_cons
.bytesperblock
!= 1 )
1078 msg_Err(p_demux
, "Unhandled constructor in RTP Reception Hint Track. Type:%u", sample_cons
.type
);
1082 /* slice doesn't fit in buffer */
1083 if( sample_cons
.sampleoffset
+ sample_cons
.length
> p_block
->i_buffer
)
1085 msg_Err(p_demux
, "Sample buffer is smaller than sample" );
1089 block_t
*p_realloc
= ( p_newblock
) ?
1090 block_Realloc( p_newblock
, 0, i_payload
+ sample_cons
.length
+ 4 ):
1091 block_Alloc( i_payload
+ sample_cons
.length
+ 4 );
1095 p_newblock
= p_realloc
;
1096 uint8_t *p_dst
= &p_newblock
->p_buffer
[i_payload
];
1098 const uint8_t* p_src
= p_block
->p_buffer
+ sample_cons
.sampleoffset
;
1099 uint8_t i_type
= (*p_src
) & ((1<<5)-1);
1101 const uint8_t synccode
[4] = { 0, 0, 0, 1 };
1102 if( memcmp( p_src
, synccode
, 4 ) )
1104 if( i_type
== 7 || i_type
== 8 )
1113 memcpy( p_dst
, p_src
, sample_cons
.length
);
1114 p_dst
+= sample_cons
.length
;
1116 i_payload
= p_dst
- p_newblock
->p_buffer
;
1119 block_Release( p_block
);
1121 p_newblock
->i_buffer
= i_payload
;
1125 block_Release( p_block
);
1127 block_Release( p_newblock
);
1131 /* RTP Reception Hint Track */
1132 static block_t
* MP4_RTPHint_Convert( demux_t
*p_demux
, block_t
*p_block
, vlc_fourcc_t i_codec
)
1134 block_t
*p_converted
= NULL
;
1135 if( p_block
->i_buffer
< 2 )
1137 block_Release( p_block
);
1141 /* number of RTP packets contained in this sample */
1142 const uint16_t i_packets
= GetWBE( p_block
->p_buffer
);
1143 if( i_packets
<= 1 || i_codec
!= VLC_CODEC_H264
)
1145 const size_t i_skip
= SAMPLEHEADERSIZE
+ i_packets
* ( RTPPACKETSIZE
+ CONSTRUCTORSIZE
);
1146 if( i_packets
== 1 && i_skip
< p_block
->i_buffer
)
1148 p_block
->p_buffer
+= i_skip
;
1149 p_converted
= p_block
;
1153 block_Release( p_block
);
1158 p_converted
= MP4_RTPHintToFrame( p_demux
, p_block
, i_packets
);
1164 /*****************************************************************************
1165 * Demux: read packet and send them to decoders
1166 *****************************************************************************
1167 * TODO check for newly selected track (ie audio upt to now )
1168 *****************************************************************************/
1169 static int DemuxTrack( demux_t
*p_demux
, mp4_track_t
*tk
, uint64_t i_readpos
,
1170 unsigned i_max_preload
)
1172 uint32_t i_nb_samples
= 0;
1173 uint32_t i_samplessize
= 0;
1175 if( !tk
->b_ok
|| tk
->i_sample
>= tk
->i_sample_count
)
1176 return VLC_DEMUXER_EOS
;
1178 if( tk
->b_chapters_source
)
1179 return VLC_DEMUXER_SUCCESS
;
1181 uint32_t i_run_seq
= MP4_TrackGetRunSeq( tk
);
1182 mtime_t i_current_nzdts
= MP4_TrackGetDTS( p_demux
, tk
);
1183 const mtime_t i_demux_max_nzdts
=(i_max_preload
< UINT_MAX
)
1184 ? i_current_nzdts
+ i_max_preload
1187 for( ; i_demux_max_nzdts
>= i_current_nzdts
; )
1189 if( tk
->i_sample
>= tk
->i_sample_count
)
1190 return VLC_DEMUXER_EOS
;
1193 msg_Dbg( p_demux
, "tk(%i)=%"PRId64
" mv=%"PRId64
" pos=%"PRIu64
, tk
->i_track_ID
,
1194 MP4_TrackGetDTS( p_demux
, tk
),
1195 MP4_GetMoviePTS( p_demux
->p_sys
), i_readpos
);
1198 i_samplessize
= MP4_TrackGetReadSize( tk
, &i_nb_samples
);
1199 if( i_samplessize
> 0 )
1204 if( vlc_stream_Tell( p_demux
->s
) != i_readpos
)
1206 if( MP4_Seek( p_demux
->s
, i_readpos
) != VLC_SUCCESS
)
1208 msg_Warn( p_demux
, "track[0x%x] will be disabled (eof?)"
1209 ": Failed to seek to %"PRIu64
,
1210 tk
->i_track_ID
, i_readpos
);
1211 MP4_TrackSelect( p_demux
, tk
, false );
1217 if( !(p_block
= vlc_stream_Block( p_demux
->s
, i_samplessize
)) )
1219 msg_Warn( p_demux
, "track[0x%x] will be disabled (eof?)"
1220 ": Failed to read %d bytes sample at %"PRIu64
,
1221 tk
->i_track_ID
, i_samplessize
, i_readpos
);
1222 MP4_TrackSelect( p_demux
, tk
, false );
1226 /* !important! Ensure clock is set before sending data */
1227 if( p_demux
->p_sys
->i_pcr
== VLC_TS_INVALID
)
1229 es_out_SetPCR( p_demux
->out
, VLC_TS_0
+ i_current_nzdts
);
1230 p_demux
->p_sys
->i_pcr
= VLC_TS_0
+ i_current_nzdts
;
1234 p_block
->i_dts
= VLC_TS_0
+ i_current_nzdts
;
1236 if( MP4_TrackGetPTSDelta( p_demux
, tk
, &i_delta
) )
1237 p_block
->i_pts
= p_block
->i_dts
+ i_delta
;
1238 else if( tk
->fmt
.i_cat
!= VIDEO_ES
)
1239 p_block
->i_pts
= p_block
->i_dts
;
1241 p_block
->i_pts
= VLC_TS_INVALID
;
1243 MP4_Block_Send( p_demux
, tk
, p_block
);
1247 if ( i_nb_samples
) /* sample size could be 0, need to go fwd. see return */
1248 MP4_TrackNextSample( p_demux
, tk
, i_nb_samples
);
1250 uint32_t i_next_run_seq
= MP4_TrackGetRunSeq( tk
);
1251 if( i_next_run_seq
!= i_run_seq
)
1254 i_current_nzdts
= MP4_TrackGetDTS( p_demux
, tk
);
1255 i_readpos
= MP4_TrackGetPos( tk
);
1258 return VLC_DEMUXER_SUCCESS
;
1261 return VLC_DEMUXER_EGENERIC
;
1264 static int DemuxMoov( demux_t
*p_demux
)
1266 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1267 unsigned int i_track
;
1269 /* check for newly selected/unselected track */
1270 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1272 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1275 if( !tk
->b_ok
|| tk
->b_chapters_source
||
1276 ( tk
->b_selected
&& tk
->i_sample
>= tk
->i_sample_count
) )
1281 if( p_sys
->b_seekable
)
1282 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
, tk
->p_es
, &b
);
1284 if( tk
->b_selected
&& !b
)
1286 MP4_TrackSelect( p_demux
, tk
, false );
1288 else if( !tk
->b_selected
&& b
)
1290 MP4_TrackSeek( p_demux
, tk
, MP4_GetMoviePTS( p_sys
) );
1294 const mtime_t i_nztime
= MP4_GetMoviePTS( p_sys
);
1296 /* We demux/set pcr, even without selected tracks, (empty edits, ...) */
1297 if( p_sys
->i_pcr
!= VLC_TS_INVALID
/* not after a seek */ )
1300 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1302 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1303 if( !tk
->b_ok
|| tk
->b_chapters_source
|| tk
->i_sample
>= tk
->i_sample_count
)
1305 /* Test for EOF on each track (samples count, edit list) */
1306 b_eof
&= ( i_nztime
> MP4_TrackGetDTS( p_demux
, tk
) );
1309 return VLC_DEMUXER_EOS
;
1312 const unsigned i_max_preload
= ( p_sys
->b_fastseekable
) ? 0 : ( p_sys
->b_seekable
) ? DEMUX_TRACK_MAX_PRELOAD
: UINT_MAX
;
1313 const mtime_t i_scaledincrement
= DEMUX_INCREMENT
* p_sys
->i_timescale
/ CLOCK_FREQ
;
1315 /* demux up to increment amount of data on every track, or just set pcr if empty data */
1318 mp4_track_t
*tk
= NULL
;
1319 i_status
= VLC_DEMUXER_EOS
;
1321 /* First pass, find any track within our target increment, ordered by position */
1322 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1324 mp4_track_t
*tk_tmp
= &p_sys
->track
[i_track
];
1325 if( !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
1326 tk_tmp
->i_sample
>= tk_tmp
->i_sample_count
||
1327 (!tk_tmp
->b_selected
&& p_sys
->b_seekable
) )
1330 /* At least still have data to demux on this or next turns */
1331 i_status
= VLC_DEMUXER_SUCCESS
;
1333 if ( MP4_TrackGetDTS( p_demux
, tk_tmp
) <= i_nztime
+ DEMUX_INCREMENT
)
1335 if( tk
== NULL
|| MP4_TrackGetPos( tk_tmp
) < MP4_TrackGetPos( tk
) )
1342 /* Second pass, refine and find any best candidate having a chunk pos closer than
1343 * current candidate (avoids seeks when increment falls between the 2) from
1344 * current position, but within extended interleave time */
1345 for( i_track
= 0; i_max_preload
> 0 && i_track
< p_sys
->i_tracks
; i_track
++ )
1347 mp4_track_t
*tk_tmp
= &p_sys
->track
[i_track
];
1349 !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
1350 (!tk_tmp
->b_selected
&& p_sys
->b_seekable
) ||
1351 tk_tmp
->i_sample
>= tk_tmp
->i_sample_count
)
1354 mtime_t i_nzdts
= MP4_TrackGetDTS( p_demux
, tk_tmp
);
1355 if ( i_nzdts
<= i_nztime
+ DEMUX_TRACK_MAX_PRELOAD
)
1357 /* Found a better candidate to avoid seeking */
1358 if( MP4_TrackGetPos( tk_tmp
) < MP4_TrackGetPos( tk
) )
1360 /* Note: previous candidate will be repicked on next loop */
1364 uint64_t i_pos
= MP4_TrackGetPos( tk
);
1365 int i_ret
= DemuxTrack( p_demux
, tk
, i_pos
, i_max_preload
);
1367 if( i_ret
== VLC_DEMUXER_SUCCESS
)
1368 i_status
= VLC_DEMUXER_SUCCESS
;
1371 if( i_status
!= VLC_DEMUXER_SUCCESS
|| !tk
)
1375 p_sys
->i_time
+= __MAX(i_scaledincrement
, 1);
1376 if( p_sys
->i_pcr
> VLC_TS_INVALID
)
1378 p_sys
->i_pcr
= VLC_TS_0
+ MP4_rescale( p_sys
->i_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
1379 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
1383 MP4_UpdateSeekpoint( p_demux
, i_nztime
+ DEMUX_INCREMENT
);
1388 static int Demux( demux_t
*p_demux
)
1390 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1392 assert( ! p_sys
->b_fragmented
);
1394 int i_status
= DemuxMoov( p_demux
);
1396 if( i_status
== VLC_DEMUXER_EOS
)
1397 i_status
= VLC_DEMUXER_EOF
;
1402 static void MP4_UpdateSeekpoint( demux_t
*p_demux
, int64_t i_time
)
1404 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1406 if( !p_sys
->p_title
)
1408 for( i
= 0; i
< p_sys
->p_title
->i_seekpoint
; i
++ )
1410 if( i_time
< p_sys
->p_title
->seekpoint
[i
]->i_time_offset
)
1415 if( i
!= p_demux
->info
.i_seekpoint
&& i
>= 0 )
1417 p_demux
->info
.i_seekpoint
= i
;
1418 p_demux
->info
.i_update
|= INPUT_UPDATE_SEEKPOINT
;
1421 /*****************************************************************************
1422 * Seek: Go to i_date
1423 ******************************************************************************/
1424 static int Seek( demux_t
*p_demux
, mtime_t i_date
, bool b_accurate
)
1426 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1427 unsigned int i_track
;
1429 /* Now for each stream try to go to this time */
1430 mtime_t i_start
= i_date
;
1431 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1433 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1434 /* FIXME: we should find the lowest time from tracks with indexes.
1435 considering only video for now */
1436 if( tk
->fmt
.i_cat
!= VIDEO_ES
)
1438 if( MP4_TrackSeek( p_demux
, tk
, i_date
) == VLC_SUCCESS
)
1440 mtime_t i_seeked
= MP4_TrackGetDTS( p_demux
, tk
);
1441 if( i_seeked
< i_start
)
1446 msg_Dbg( p_demux
, "seeking with %"PRId64
"ms %s", (i_date
- i_start
) / 1000,
1447 !b_accurate
? "alignment" : "preroll (use input-fast-seek to avoid)" );
1449 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1451 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1452 if( tk
->fmt
.i_cat
== VIDEO_ES
)
1454 MP4_TrackSeek( p_demux
, tk
, i_start
);
1457 MP4_UpdateSeekpoint( p_demux
, i_date
);
1458 MP4ASF_ResetFrames( p_sys
);
1459 /* update global time */
1460 p_sys
->i_time
= MP4_rescale( i_start
, CLOCK_FREQ
, p_sys
->i_timescale
);
1461 p_sys
->i_pcr
= VLC_TS_INVALID
;
1464 es_out_Control( p_demux
->out
, ES_OUT_SET_NEXT_DISPLAY_TIME
, i_date
);
1469 static int FragPrepareChunk( demux_t
*p_demux
, MP4_Box_t
*p_moof
,
1470 MP4_Box_t
*p_sidx
, stime_t i_moof_time
, bool b_discontinuity
)
1472 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1474 if( FragCreateTrunIndex( p_demux
, p_moof
, p_sidx
, i_moof_time
, b_discontinuity
) == VLC_SUCCESS
)
1476 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1478 mp4_track_t
*p_track
= &p_sys
->track
[i
];
1479 if( p_track
->context
.runs
.i_count
)
1481 const mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[0];
1482 p_track
->context
.i_trun_sample_pos
= p_run
->i_offset
;
1483 p_track
->context
.i_trun_sample
= 0;
1484 p_track
->i_time
= p_run
->i_first_dts
;
1490 return VLC_EGENERIC
;
1493 static stime_t
FragGetDemuxTimeFromTracksTime( demux_sys_t
*p_sys
)
1495 stime_t i_time
= INT64_MAX
;
1496 for( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
1498 if( p_sys
->track
[i
].context
.runs
.i_count
== 0 )
1500 stime_t i_ttime
= MP4_rescale( p_sys
->track
[i
].i_time
,
1501 p_sys
->track
[i
].i_timescale
, p_sys
->i_timescale
);
1502 i_time
= __MIN( i_time
, i_ttime
);
1507 static uint32_t FragGetMoofSequenceNumber( MP4_Box_t
*p_moof
)
1509 const MP4_Box_t
*p_mfhd
= MP4_BoxGet( p_moof
, "mfhd" );
1510 if( p_mfhd
&& BOXDATA(p_mfhd
) )
1511 return BOXDATA(p_mfhd
)->i_sequence_number
;
1515 static int FragSeekLoadFragment( demux_t
*p_demux
, uint32_t i_moox
, stime_t i_moox_time
)
1517 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1520 if( i_moox
== ATOM_moov
)
1522 p_moox
= p_sys
->p_moov
;
1526 const uint8_t *p_peek
;
1527 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) != 8 )
1528 return VLC_EGENERIC
;
1530 if( ATOM_moof
!= VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) )
1531 return VLC_EGENERIC
;
1533 MP4_Box_t
*p_vroot
= MP4_BoxGetNextChunk( p_demux
->s
);
1535 return VLC_EGENERIC
;
1536 p_moox
= MP4_BoxExtract( &p_vroot
->p_first
, ATOM_moof
);
1537 MP4_BoxFree( p_vroot
);
1540 return VLC_EGENERIC
;
1543 FragResetContext( p_sys
);
1546 p_sys
->context
.p_fragment_atom
= p_moox
;
1547 p_sys
->context
.i_current_box_type
= i_moox
;
1549 if( i_moox
== ATOM_moof
)
1551 FragPrepareChunk( p_demux
, p_moox
, NULL
, i_moox_time
, true );
1552 p_sys
->context
.i_lastseqnumber
= FragGetMoofSequenceNumber( p_moox
);
1554 p_sys
->i_time
= FragGetDemuxTimeFromTracksTime( p_sys
);
1555 p_sys
->i_pcr
= VLC_TS_INVALID
;
1558 msg_Dbg( p_demux
, "seeked to %4.4s at pos %" PRIu64
, (char *) &i_moox
, p_moox
->i_pos
);
1562 static unsigned GetSeekTrackIndex( demux_sys_t
*p_sys
)
1565 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1567 if( p_sys
->track
[i
].fmt
.i_cat
== VIDEO_ES
||
1568 p_sys
->track
[i
].fmt
.i_cat
== AUDIO_ES
)
1570 if( cand
!= i
&& !p_sys
->track
[cand
].b_selected
)
1577 static void FragTrunSeekToTime( mp4_track_t
*p_track
, stime_t i_target_time
)
1579 if( !p_track
->b_ok
|| p_track
->context
.runs
.i_count
< 1 )
1583 unsigned i_sample
= 0;
1584 uint64_t i_pos
= p_track
->context
.runs
.p_array
[0].i_offset
;
1585 stime_t i_time
= p_track
->context
.runs
.p_array
[0].i_first_dts
;
1587 for( unsigned r
= 0; r
< p_track
->context
.runs
.i_count
; r
++ )
1589 const mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[r
];
1590 const MP4_Box_data_trun_t
*p_data
=
1591 p_track
->context
.runs
.p_array
[r
].p_trun
->data
.p_trun
;
1592 if( i_time
> i_target_time
)
1596 i_time
= p_run
->i_first_dts
;
1597 i_pos
= p_run
->i_offset
;
1600 uint32_t dur
= p_track
->context
.i_default_sample_duration
;
1601 uint32_t len
= p_track
->context
.i_default_sample_size
;
1602 for ( unsigned i
=0; i
<p_data
->i_sample_count
; i
++ )
1604 if( p_data
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
1605 dur
= p_data
->p_samples
[i
].i_duration
;
1607 /* check condition */
1608 if( i_time
+ dur
> i_target_time
)
1611 if( p_data
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
1612 len
= p_data
->p_samples
[i
].i_size
;
1619 p_track
->context
.i_trun_sample
= i_sample
;
1620 p_track
->context
.i_trun_sample_pos
= i_pos
;
1621 p_track
->context
.runs
.i_current
= i_run
;
1624 static int FragSeekToTime( demux_t
*p_demux
, mtime_t i_nztime
, bool b_accurate
)
1626 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1627 uint64_t i64
= UINT64_MAX
;
1628 uint32_t i_segment_type
= ATOM_moof
;
1629 stime_t i_segment_time
= INT64_MAX
;
1630 mtime_t i_sync_time
= i_nztime
;
1631 bool b_iframesync
= false;
1633 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1634 if ( !p_sys
->i_timescale
|| !i_duration
|| !p_sys
->b_seekable
)
1635 return VLC_EGENERIC
;
1637 uint64_t i_backup_pos
= vlc_stream_Tell( p_demux
->s
);
1639 if ( !p_sys
->b_fragments_probed
&& !p_sys
->b_index_probed
&& p_sys
->b_seekable
)
1641 ProbeIndex( p_demux
);
1642 p_sys
->b_index_probed
= true;
1645 const unsigned i_seek_track_index
= GetSeekTrackIndex( p_sys
);
1646 const unsigned i_seek_track_ID
= p_sys
->track
[i_seek_track_index
].i_track_ID
;
1648 if( MP4_rescale( i_nztime
, CLOCK_FREQ
, p_sys
->i_timescale
)
1649 < GetMoovTrackDuration( p_sys
, i_seek_track_ID
) )
1651 i64
= p_sys
->p_moov
->i_pos
;
1652 i_segment_type
= ATOM_moov
;
1654 else if( FragGetMoofBySidxIndex( p_demux
, i_nztime
, &i64
, &i_sync_time
) == VLC_SUCCESS
)
1656 /* provides base offset */
1657 i_segment_time
= i_sync_time
;
1658 msg_Dbg( p_demux
, "seeking to sidx moof pos %" PRId64
" %" PRId64
, i64
, i_sync_time
);
1662 bool b_buildindex
= false;
1664 if( FragGetMoofByTfraIndex( p_demux
, i_nztime
, i_seek_track_ID
, &i64
, &i_sync_time
) == VLC_SUCCESS
)
1666 /* Does only provide segment position and a sync sample time */
1667 msg_Dbg( p_demux
, "seeking to sync point %" PRId64
, i_sync_time
);
1668 b_iframesync
= true;
1670 else if( !p_sys
->b_fragments_probed
&& !p_sys
->b_fastseekable
)
1672 const char *psz_msg
= _(
1673 "Because this file index is broken or missing, "
1674 "seeking will not work correctly.\n"
1675 "VLC won't repair your file but can temporary fix this "
1676 "problem by building an index in memory.\n"
1677 "This step might take a long time on a large file.\n"
1678 "What do you want to do?");
1679 b_buildindex
= vlc_dialog_wait_question( p_demux
,
1680 VLC_DIALOG_QUESTION_NORMAL
,
1684 _("Broken or missing Index"),
1688 if( !p_sys
->b_fragments_probed
&& ( p_sys
->b_fastseekable
|| b_buildindex
) )
1691 int i_ret
= vlc_stream_Seek( p_demux
->s
, p_sys
->p_moov
->i_pos
+ p_sys
->p_moov
->i_size
);
1692 if( i_ret
== VLC_SUCCESS
)
1694 i_ret
= ProbeFragments( p_demux
, true, &foo
);
1695 p_sys
->b_fragments_probed
= true;
1697 if( i_ret
!= VLC_SUCCESS
)
1699 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1704 if( p_sys
->b_fragments_probed
&& p_sys
->p_fragsindex
)
1706 stime_t i_basetime
= MP4_rescale( i_sync_time
, CLOCK_FREQ
, p_sys
->i_timescale
);
1707 if( !MP4_Fragments_Index_Lookup( p_sys
->p_fragsindex
, &i_basetime
, &i64
, i_seek_track_index
) )
1709 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1710 return VLC_EGENERIC
;
1712 msg_Dbg( p_demux
, "seeking to fragment index pos %" PRId64
" %" PRId64
, i64
,
1713 MP4_rescale( i_basetime
, p_sys
->i_timescale
, CLOCK_FREQ
) );
1717 if( i64
== UINT64_MAX
)
1719 msg_Warn( p_demux
, "seek by index failed" );
1720 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1721 return VLC_EGENERIC
;
1724 msg_Dbg( p_demux
, "final seek to fragment at %"PRId64
, i64
);
1725 if( vlc_stream_Seek( p_demux
->s
, i64
) )
1727 msg_Err( p_demux
, "seek failed to %"PRId64
, i64
);
1728 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1729 return VLC_EGENERIC
;
1732 /* Context is killed on success */
1733 if( FragSeekLoadFragment( p_demux
, i_segment_type
, i_segment_time
) != VLC_SUCCESS
)
1735 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1736 return VLC_EGENERIC
;
1739 p_sys
->i_pcr
= VLC_TS_INVALID
;
1741 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1743 if( i_segment_type
== ATOM_moov
)
1745 MP4_TrackSeek( p_demux
, &p_sys
->track
[i
], i_sync_time
);
1746 p_sys
->i_time
= MP4_rescale( i_sync_time
, CLOCK_FREQ
, p_sys
->i_timescale
);
1747 p_sys
->i_pcr
= VLC_TS_INVALID
;
1749 else if( b_iframesync
)
1751 stime_t i_tst
= MP4_rescale( i_sync_time
, CLOCK_FREQ
, p_sys
->track
[i
].i_timescale
);
1752 FragTrunSeekToTime( &p_sys
->track
[i
], i_tst
);
1753 p_sys
->track
[i
].i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
1757 MP4ASF_ResetFrames( p_sys
);
1758 /* And set next display time in that trun/fragment */
1759 if( b_iframesync
&& b_accurate
)
1760 es_out_Control( p_demux
->out
, ES_OUT_SET_NEXT_DISPLAY_TIME
, VLC_TS_0
+ i_nztime
);
1764 static int FragSeekToPos( demux_t
*p_demux
, double f
, bool b_accurate
)
1766 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1767 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1769 if ( !p_sys
->b_seekable
|| !p_sys
->i_timescale
|| !i_duration
)
1770 return VLC_EGENERIC
;
1772 return FragSeekToTime( p_demux
, (mtime_t
)( f
*
1773 MP4_rescale( i_duration
, p_sys
->i_timescale
, CLOCK_FREQ
) ), b_accurate
);
1776 static bool imageTypeCompatible( const MP4_Box_data_data_t
*p_data
)
1779 p_data
->e_wellknowntype
== DATA_WKT_PNG
||
1780 p_data
->e_wellknowntype
== DATA_WKT_JPEG
||
1781 p_data
->e_wellknowntype
== DATA_WKT_BMP
);
1784 static int MP4_LoadMeta( demux_sys_t
*p_sys
, vlc_meta_t
*p_meta
)
1786 MP4_Box_t
*p_data
= NULL
;
1787 MP4_Box_t
*p_udta
= NULL
;
1788 bool b_attachment_set
= false;
1791 return VLC_EGENERIC
;
1793 for( int i_index
= 0; psz_meta_roots
[i_index
] && !p_udta
; i_index
++ )
1795 p_udta
= MP4_BoxGet( p_sys
->p_root
, psz_meta_roots
[i_index
] );
1798 p_data
= MP4_BoxGet( p_udta
, "covr/data" );
1799 if ( p_data
&& imageTypeCompatible( BOXDATA(p_data
) ) )
1801 char *psz_attachment
;
1802 if ( -1 != asprintf( &psz_attachment
, "attachment://%s/covr/data[0]",
1803 psz_meta_roots
[i_index
] ) )
1805 vlc_meta_SetArtURL( p_meta
, psz_attachment
);
1806 b_attachment_set
= true;
1807 free( psz_attachment
);
1813 const MP4_Box_t
*p_pnot
;
1814 if ( !b_attachment_set
&& (p_pnot
= MP4_BoxGet( p_sys
->p_root
, "pnot" )) )
1816 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
) && !b_attachment_set
; i
++ )
1818 if ( rgi_pict_atoms
[i
] == BOXDATA(p_pnot
)->i_type
)
1821 snprintf( rgsz_path
, 26, "attachment://%4.4s[%"PRIu16
"]",
1822 (char*)&rgi_pict_atoms
[i
], BOXDATA(p_pnot
)->i_index
- 1 );
1823 vlc_meta_SetArtURL( p_meta
, rgsz_path
);
1824 b_attachment_set
= true;
1829 if( p_udta
== NULL
)
1831 if( !b_attachment_set
)
1832 return VLC_EGENERIC
;
1834 else SetupMeta( p_meta
, p_udta
);
1839 /*****************************************************************************
1841 *****************************************************************************/
1842 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
1844 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1850 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1854 case DEMUX_CAN_SEEK
:
1855 *va_arg( args
, bool * ) = p_sys
->b_seekable
;
1858 case DEMUX_GET_POSITION
:
1859 pf
= va_arg( args
, double * );
1860 if( i_duration
> 0 )
1862 *pf
= (double)p_sys
->i_time
/ (double)i_duration
;
1870 case DEMUX_SET_POSITION
:
1871 f
= va_arg( args
, double );
1872 b
= va_arg( args
, int );
1873 if ( p_demux
->pf_demux
== DemuxFrag
)
1874 return FragSeekToPos( p_demux
, f
, b
);
1875 else if( p_sys
->i_timescale
> 0 )
1877 i64
= (int64_t)( f
* MP4_rescale( p_sys
->i_duration
,
1878 p_sys
->i_timescale
, CLOCK_FREQ
) );
1879 return Seek( p_demux
, i64
, b
);
1881 else return VLC_EGENERIC
;
1883 case DEMUX_GET_TIME
:
1884 pi64
= va_arg( args
, int64_t * );
1885 if( p_sys
->i_timescale
> 0 )
1887 *pi64
= MP4_rescale( p_sys
->i_time
,
1888 p_sys
->i_timescale
, CLOCK_FREQ
);
1893 case DEMUX_SET_TIME
:
1894 i64
= va_arg( args
, int64_t );
1895 b
= va_arg( args
, int );
1896 if ( p_demux
->pf_demux
== DemuxFrag
)
1897 return FragSeekToTime( p_demux
, i64
, b
);
1899 return Seek( p_demux
, i64
, b
);
1901 case DEMUX_GET_LENGTH
:
1902 pi64
= va_arg( args
, int64_t * );
1903 if( p_sys
->i_timescale
> 0 )
1905 *pi64
= MP4_rescale( i_duration
,
1906 p_sys
->i_timescale
, CLOCK_FREQ
);
1912 pf
= va_arg( args
, double * );
1916 case DEMUX_GET_ATTACHMENTS
:
1918 input_attachment_t
***ppp_attach
= va_arg( args
, input_attachment_t
*** );
1919 int *pi_int
= va_arg( args
, int * );
1921 MP4_Box_t
*p_udta
= NULL
;
1925 /* Count number of total attachments */
1926 for( ; psz_meta_roots
[i_index
] && !p_udta
; i_index
++ )
1928 p_udta
= MP4_BoxGet( p_sys
->p_root
, psz_meta_roots
[i_index
] );
1930 i_count
+= MP4_BoxCount( p_udta
, "covr/data" );
1933 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
); i
++ )
1936 snprintf( rgsz_path
, 5, "%4.4s", (char*)&rgi_pict_atoms
[i
] );
1937 i_count
+= MP4_BoxCount( p_sys
->p_root
, rgsz_path
);
1941 return VLC_EGENERIC
;
1943 *ppp_attach
= (input_attachment_t
**)
1944 vlc_alloc( i_count
, sizeof(input_attachment_t
*) );
1945 if( !(*ppp_attach
) ) return VLC_ENOMEM
;
1947 /* First add cover attachments */
1949 size_t i_box_count
= 0;
1952 const MP4_Box_t
*p_data
= MP4_BoxGet( p_udta
, "covr/data" );
1953 for( ; p_data
; p_data
= p_data
->p_next
)
1959 if ( p_data
->i_type
!= ATOM_data
|| !imageTypeCompatible( BOXDATA(p_data
) ) )
1962 switch( BOXDATA(p_data
)->e_wellknowntype
)
1965 psz_mime
= strdup( "image/png" );
1968 psz_mime
= strdup( "image/jpeg" );
1971 psz_mime
= strdup( "image/bmp" );
1977 if ( asprintf( &psz_filename
, "%s/covr/data[%"PRIu64
"]", psz_meta_roots
[i_index
- 1],
1978 (uint64_t) i_box_count
- 1 ) >= 0 )
1980 (*ppp_attach
)[i_count
++] =
1981 vlc_input_attachment_New( psz_filename
, psz_mime
, "Cover picture",
1982 BOXDATA(p_data
)->p_blob
, BOXDATA(p_data
)->i_blob
);
1983 msg_Dbg( p_demux
, "adding attachment %s", psz_filename
);
1984 free( psz_filename
);
1991 /* Then quickdraw pict ones */
1992 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
); i
++ )
1995 snprintf( rgsz_path
, 5, "%4.4s", (char*)&rgi_pict_atoms
[i
] );
1996 const MP4_Box_t
*p_pict
= MP4_BoxGet( p_sys
->p_root
, rgsz_path
);
1998 for( ; p_pict
; p_pict
= p_pict
->p_next
)
2000 if ( i_box_count
++ == UINT16_MAX
) /* pnot only handles 2^16 */
2002 if ( p_pict
->i_type
!= rgi_pict_atoms
[i
] )
2004 char rgsz_location
[12];
2005 snprintf( rgsz_location
, 12, "%4.4s[%"PRIu16
"]", (char*)&rgi_pict_atoms
[i
],
2006 (uint16_t) i_box_count
- 1 );
2007 (*ppp_attach
)[i_count
] = vlc_input_attachment_New( rgsz_location
, "image/x-pict",
2008 "Quickdraw image", p_pict
->data
.p_binary
->p_blob
, p_pict
->data
.p_binary
->i_blob
);
2009 if ( !(*ppp_attach
)[i_count
] )
2015 msg_Dbg( p_demux
, "adding attachment %s", rgsz_location
);
2021 free( *ppp_attach
);
2022 return VLC_EGENERIC
;
2030 case DEMUX_GET_META
:
2032 vlc_meta_t
*p_meta
= va_arg( args
, vlc_meta_t
*);
2034 if( !p_sys
->p_meta
)
2035 return VLC_EGENERIC
;
2037 vlc_meta_Merge( p_meta
, p_sys
->p_meta
);
2042 case DEMUX_GET_TITLE_INFO
:
2044 input_title_t
***ppp_title
= va_arg( args
, input_title_t
*** );
2045 int *pi_int
= va_arg( args
, int* );
2046 int *pi_title_offset
= va_arg( args
, int* );
2047 int *pi_seekpoint_offset
= va_arg( args
, int* );
2049 if( !p_sys
->p_title
)
2050 return VLC_EGENERIC
;
2053 *ppp_title
= malloc( sizeof( input_title_t
*) );
2054 (*ppp_title
)[0] = vlc_input_title_Duplicate( p_sys
->p_title
);
2055 *pi_title_offset
= 0;
2056 *pi_seekpoint_offset
= 0;
2059 case DEMUX_SET_TITLE
:
2061 const int i_title
= va_arg( args
, int );
2062 if( !p_sys
->p_title
|| i_title
!= 0 )
2063 return VLC_EGENERIC
;
2066 case DEMUX_SET_SEEKPOINT
:
2068 const int i_seekpoint
= va_arg( args
, int );
2069 if( !p_sys
->p_title
)
2070 return VLC_EGENERIC
;
2071 return Seek( p_demux
, p_sys
->p_title
->seekpoint
[i_seekpoint
]->i_time_offset
, true );
2073 case DEMUX_GET_PTS_DELAY
:
2075 for( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
2077 const MP4_Box_t
*p_load
;
2078 if ( (p_load
= MP4_BoxGet( p_sys
->track
[i
].p_track
, "load" )) &&
2079 BOXDATA(p_load
)->i_duration
> 0 )
2081 *va_arg(args
, int64_t *) =
2082 MP4_rescale( BOXDATA(p_load
)->i_duration
,
2083 p_sys
->track
[i
].i_timescale
, CLOCK_FREQ
);
2087 return VLC_EGENERIC
;
2089 case DEMUX_SET_NEXT_DEMUX_TIME
:
2090 case DEMUX_SET_GROUP
:
2091 case DEMUX_HAS_UNSUPPORTED_META
:
2092 case DEMUX_CAN_RECORD
:
2094 return VLC_EGENERIC
;
2098 /*****************************************************************************
2099 * Close: frees unused data
2100 *****************************************************************************/
2101 static void Close ( vlc_object_t
* p_this
)
2103 demux_t
* p_demux
= (demux_t
*)p_this
;
2104 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2105 unsigned int i_track
;
2107 msg_Dbg( p_demux
, "freeing all memory" );
2109 FragResetContext( p_sys
);
2111 MP4_BoxFree( p_sys
->p_root
);
2113 if( p_sys
->p_title
)
2114 vlc_input_title_Delete( p_sys
->p_title
);
2117 vlc_meta_Delete( p_sys
->p_meta
);
2119 MP4_Fragments_Index_Delete( p_sys
->p_fragsindex
);
2121 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
2122 MP4_TrackClean( p_demux
->out
, &p_sys
->track
[i_track
] );
2123 free( p_sys
->track
);
2130 /****************************************************************************
2131 * Local functions, specific to vlc
2132 ****************************************************************************/
2134 static void LoadChapterGpac( demux_t
*p_demux
, MP4_Box_t
*p_chpl
)
2136 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2138 if( BOXDATA(p_chpl
)->i_chapter
== 0 )
2141 p_sys
->p_title
= vlc_input_title_New();
2142 for( int i
= 0; i
< BOXDATA(p_chpl
)->i_chapter
&& p_sys
->p_title
; i
++ )
2144 seekpoint_t
*s
= vlc_seekpoint_New();
2145 if( s
== NULL
) continue;
2147 s
->psz_name
= strdup( BOXDATA(p_chpl
)->chapter
[i
].psz_name
);
2148 if( s
->psz_name
== NULL
)
2150 vlc_seekpoint_Delete( s
);;
2154 EnsureUTF8( s
->psz_name
);
2155 s
->i_time_offset
= BOXDATA(p_chpl
)->chapter
[i
].i_start
/ 10;
2156 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2159 static void LoadChapterGoPro( demux_t
*p_demux
, MP4_Box_t
*p_hmmt
)
2161 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2163 p_sys
->p_title
= vlc_input_title_New();
2164 if( p_sys
->p_title
)
2165 for( unsigned i
= 0; i
< BOXDATA(p_hmmt
)->i_chapter_count
; i
++ )
2167 seekpoint_t
*s
= vlc_seekpoint_New();
2170 if( asprintf( &s
->psz_name
, "HiLight tag #%u", i
+1 ) != -1 )
2171 EnsureUTF8( s
->psz_name
);
2173 /* HiLights are stored in ms so we convert them to µs */
2174 s
->i_time_offset
= BOXDATA(p_hmmt
)->pi_chapter_start
[i
] * 1000;
2175 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2179 static void LoadChapterApple( demux_t
*p_demux
, mp4_track_t
*tk
)
2181 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2183 for( tk
->i_sample
= 0; tk
->i_sample
< tk
->i_sample_count
; tk
->i_sample
++ )
2185 const int64_t i_dts
= MP4_TrackGetDTS( p_demux
, tk
);
2186 int64_t i_pts_delta
;
2187 if ( !MP4_TrackGetPTSDelta( p_demux
, tk
, &i_pts_delta
) )
2189 uint32_t i_nb_samples
= 0;
2190 const uint32_t i_size
= MP4_TrackGetReadSize( tk
, &i_nb_samples
);
2192 if( i_size
> 0 && !vlc_stream_Seek( p_demux
->s
, MP4_TrackGetPos( tk
) ) )
2195 const uint32_t i_read
= stream_ReadU32( p_demux
->s
, p_buffer
,
2196 __MIN( sizeof(p_buffer
), i_size
) );
2199 const uint32_t i_string
= __MIN( GetWBE(p_buffer
), i_read
-2 );
2200 const char *psnz_string
= &p_buffer
[2];
2202 seekpoint_t
*s
= vlc_seekpoint_New();
2203 if( s
== NULL
) continue;
2205 if( i_string
> 1 && !memcmp( psnz_string
, "\xFF\xFE", 2 ) )
2206 s
->psz_name
= FromCharset( "UTF-16LE", psnz_string
, i_string
);
2208 s
->psz_name
= strndup( psnz_string
, i_string
);
2210 if( s
->psz_name
== NULL
)
2212 vlc_seekpoint_Delete( s
);
2216 EnsureUTF8( s
->psz_name
);
2217 s
->i_time_offset
= i_dts
+ __MAX( i_pts_delta
, 0 );
2219 if( !p_sys
->p_title
)
2220 p_sys
->p_title
= vlc_input_title_New();
2221 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2224 if( tk
->i_sample
+1 >= tk
->chunk
[tk
->i_chunk
].i_sample_first
+
2225 tk
->chunk
[tk
->i_chunk
].i_sample_count
)
2229 static void LoadChapter( demux_t
*p_demux
)
2231 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2235 if( ( p_chpl
= MP4_BoxGet( p_sys
->p_root
, "/moov/udta/chpl" ) ) &&
2236 BOXDATA(p_chpl
) && BOXDATA(p_chpl
)->i_chapter
> 0 )
2238 LoadChapterGpac( p_demux
, p_chpl
);
2240 else if( ( p_hmmt
= MP4_BoxGet( p_sys
->p_root
, "/moov/udta/HMMT" ) ) &&
2241 BOXDATA(p_hmmt
) && BOXDATA(p_hmmt
)->pi_chapter_start
&& BOXDATA(p_hmmt
)->i_chapter_count
> 0 )
2243 LoadChapterGoPro( p_demux
, p_hmmt
);
2245 else if( p_sys
->p_tref_chap
)
2247 MP4_Box_data_tref_generic_t
*p_chap
= p_sys
->p_tref_chap
->data
.p_tref_generic
;
2250 /* Load the first subtitle track like quicktime */
2251 for( i
= 0; i
< p_chap
->i_entry_count
; i
++ )
2253 for( j
= 0; j
< p_sys
->i_tracks
; j
++ )
2255 mp4_track_t
*tk
= &p_sys
->track
[j
];
2256 if( tk
->b_ok
&& tk
->i_track_ID
== p_chap
->i_track_ID
[i
] &&
2257 tk
->fmt
.i_cat
== SPU_ES
&& tk
->fmt
.i_codec
== VLC_CODEC_TX3G
)
2260 if( j
< p_sys
->i_tracks
)
2262 LoadChapterApple( p_demux
, &p_sys
->track
[j
] );
2268 /* Add duration if titles are enabled */
2269 if( p_sys
->p_title
)
2271 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
2272 p_sys
->p_title
->i_length
=
2273 MP4_rescale( i_duration
,
2274 p_sys
->i_timescale
, CLOCK_FREQ
);
2278 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
2279 static int TrackCreateChunksIndex( demux_t
*p_demux
,
2280 mp4_track_t
*p_demux_track
)
2282 MP4_Box_t
*p_co64
; /* give offset for each chunk, same for stco and co64 */
2285 unsigned int i_chunk
;
2286 unsigned int i_index
, i_last
;
2288 if( ( !(p_co64
= MP4_BoxGet( p_demux_track
->p_stbl
, "stco" ) )&&
2289 !(p_co64
= MP4_BoxGet( p_demux_track
->p_stbl
, "co64" ) ) )||
2290 ( !(p_stsc
= MP4_BoxGet( p_demux_track
->p_stbl
, "stsc" ) ) ))
2292 return( VLC_EGENERIC
);
2295 p_demux_track
->i_chunk_count
= BOXDATA(p_co64
)->i_entry_count
;
2296 if( !p_demux_track
->i_chunk_count
)
2298 msg_Warn( p_demux
, "no chunk defined" );
2300 p_demux_track
->chunk
= calloc( p_demux_track
->i_chunk_count
,
2301 sizeof( mp4_chunk_t
) );
2302 if( p_demux_track
->chunk
== NULL
)
2307 /* first we read chunk offset */
2308 for( i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2310 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2312 ck
->i_offset
= BOXDATA(p_co64
)->i_chunk_offset
[i_chunk
];
2314 ck
->i_first_dts
= 0;
2315 ck
->i_entries_dts
= 0;
2316 ck
->p_sample_count_dts
= NULL
;
2317 ck
->p_sample_delta_dts
= NULL
;
2318 ck
->i_entries_pts
= 0;
2319 ck
->p_sample_count_pts
= NULL
;
2320 ck
->p_sample_offset_pts
= NULL
;
2323 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
2324 to be used for the sample XXX begin to 1
2325 We construct it begining at the end */
2326 i_last
= p_demux_track
->i_chunk_count
; /* last chunk proceded */
2327 i_index
= BOXDATA(p_stsc
)->i_entry_count
;
2329 while( i_index
-- > 0 )
2331 for( i_chunk
= BOXDATA(p_stsc
)->i_first_chunk
[i_index
] - 1;
2332 i_chunk
< i_last
; i_chunk
++ )
2334 if( i_chunk
>= p_demux_track
->i_chunk_count
)
2336 msg_Warn( p_demux
, "corrupted chunk table" );
2337 return VLC_EGENERIC
;
2340 p_demux_track
->chunk
[i_chunk
].i_sample_description_index
=
2341 BOXDATA(p_stsc
)->i_sample_description_index
[i_index
];
2342 p_demux_track
->chunk
[i_chunk
].i_sample_count
=
2343 BOXDATA(p_stsc
)->i_samples_per_chunk
[i_index
];
2345 i_last
= BOXDATA(p_stsc
)->i_first_chunk
[i_index
] - 1;
2348 p_demux_track
->i_sample_count
= 0;
2349 bool b_broken
= false;
2350 if ( p_demux_track
->i_chunk_count
)
2352 p_demux_track
->chunk
[0].i_sample_first
= 0;
2353 p_demux_track
->i_sample_count
+= p_demux_track
->chunk
[0].i_sample_count
;
2355 const mp4_chunk_t
*prev
= &p_demux_track
->chunk
[0];
2356 for( i_chunk
= 1; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2358 mp4_chunk_t
*cur
= &p_demux_track
->chunk
[i_chunk
];
2359 if( unlikely(UINT32_MAX
- cur
->i_sample_count
< p_demux_track
->i_sample_count
) )
2364 p_demux_track
->i_sample_count
+= cur
->i_sample_count
;
2365 cur
->i_sample_first
= prev
->i_sample_first
+ prev
->i_sample_count
;
2370 if( unlikely(b_broken
) )
2372 msg_Err( p_demux
, "Overflow in chunks total samples count" );
2373 return VLC_EGENERIC
;
2376 msg_Dbg( p_demux
, "track[Id 0x%x] read %d chunk",
2377 p_demux_track
->i_track_ID
, p_demux_track
->i_chunk_count
);
2382 static int xTTS_CountEntries( demux_t
*p_demux
, uint32_t *pi_entry
/* out */,
2383 const uint32_t i_index
,
2384 uint32_t i_index_samples_left
,
2385 uint32_t i_sample_count
,
2386 const uint32_t *pi_index_sample_count
,
2387 const uint32_t i_table_count
)
2389 uint32_t i_array_offset
;
2390 while( i_sample_count
> 0 )
2392 if ( likely((UINT32_MAX
- i_index
) >= *pi_entry
) )
2393 i_array_offset
= i_index
+ *pi_entry
;
2395 return VLC_EGENERIC
;
2397 if ( i_array_offset
>= i_table_count
)
2399 msg_Err( p_demux
, "invalid index counting total samples %u %u", i_array_offset
, i_table_count
);
2403 if ( i_index_samples_left
)
2405 if ( i_index_samples_left
> i_sample_count
)
2407 i_index_samples_left
-= i_sample_count
;
2409 *pi_entry
+=1; /* No samples left, go copy */
2414 i_sample_count
-= i_index_samples_left
;
2415 i_index_samples_left
= 0;
2422 i_sample_count
-= __MIN( i_sample_count
, pi_index_sample_count
[i_array_offset
] );
2430 static int TrackCreateSamplesIndex( demux_t
*p_demux
,
2431 mp4_track_t
*p_demux_track
)
2434 MP4_Box_data_stsz_t
*stsz
;
2435 /* TODO use also stss and stsh table for seeking */
2436 /* FIXME use edit table */
2439 * Gives the sample size for each samples. There is also a stz2 table
2440 * (compressed form) that we need to implement TODO */
2441 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "stsz" );
2444 /* FIXME and stz2 */
2445 msg_Warn( p_demux
, "cannot find STSZ box" );
2446 return VLC_EGENERIC
;
2448 stsz
= p_box
->data
.p_stsz
;
2450 /* Use stsz table to create a sample number -> sample size table */
2451 if( p_demux_track
->i_sample_count
!= stsz
->i_sample_count
)
2453 msg_Warn( p_demux
, "Incorrect total samples stsc %" PRIu32
" <> stsz %"PRIu32
", "
2454 " expect truncated media playback",
2455 p_demux_track
->i_sample_count
, stsz
->i_sample_count
);
2456 p_demux_track
->i_sample_count
= __MIN(p_demux_track
->i_sample_count
, stsz
->i_sample_count
);
2459 if( stsz
->i_sample_size
)
2461 /* 1: all sample have the same size, so no need to construct a table */
2462 p_demux_track
->i_sample_size
= stsz
->i_sample_size
;
2463 p_demux_track
->p_sample_size
= NULL
;
2467 /* 2: each sample can have a different size */
2468 p_demux_track
->i_sample_size
= 0;
2469 p_demux_track
->p_sample_size
=
2470 calloc( p_demux_track
->i_sample_count
, sizeof( uint32_t ) );
2471 if( p_demux_track
->p_sample_size
== NULL
)
2474 for( uint32_t i_sample
= 0; i_sample
< p_demux_track
->i_sample_count
; i_sample
++ )
2476 p_demux_track
->p_sample_size
[i_sample
] =
2477 stsz
->i_entry_size
[i_sample
];
2481 if ( p_demux_track
->i_chunk_count
&& p_demux_track
->i_sample_size
== 0 )
2483 const mp4_chunk_t
*lastchunk
= &p_demux_track
->chunk
[p_demux_track
->i_chunk_count
- 1];
2484 if( (uint64_t)lastchunk
->i_sample_count
+ p_demux_track
->i_chunk_count
- 1 > stsz
->i_sample_count
)
2486 msg_Err( p_demux
, "invalid samples table: stsz table is too small" );
2487 return VLC_EGENERIC
;
2491 /* Use stts table to create a sample number -> dts table.
2492 * XXX: if we don't want to waste too much memory, we can't expand
2493 * the box! so each chunk will contain an "extract" of this table
2494 * for fast research (problem with raw stream where a sample is sometime
2495 * just channels*bits_per_sample/8 */
2497 /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
2498 * differing in 2/2 fields and 1 signedness */
2500 mtime_t i_next_dts
= 0;
2502 * Gives mapping between sample and decoding time
2504 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "stts" );
2507 msg_Warn( p_demux
, "cannot find STTS box" );
2508 return VLC_EGENERIC
;
2512 MP4_Box_data_stts_t
*stts
= p_box
->data
.p_stts
;
2514 msg_Warn( p_demux
, "STTS table of %"PRIu32
" entries", stts
->i_entry_count
);
2516 /* Create sample -> dts table per chunk */
2517 uint32_t i_index
= 0;
2518 uint32_t i_current_index_samples_left
= 0;
2520 for( uint32_t i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2522 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2523 uint32_t i_sample_count
;
2525 /* save first dts */
2526 ck
->i_first_dts
= i_next_dts
;
2528 /* count how many entries are needed for this chunk
2529 * for p_sample_delta_dts and p_sample_count_dts */
2530 ck
->i_entries_dts
= 0;
2532 int i_ret
= xTTS_CountEntries( p_demux
, &ck
->i_entries_dts
, i_index
,
2533 i_current_index_samples_left
,
2535 stts
->pi_sample_count
,
2536 stts
->i_entry_count
);
2537 if ( i_ret
== VLC_EGENERIC
)
2541 ck
->p_sample_count_dts
= calloc( ck
->i_entries_dts
, sizeof( uint32_t ) );
2542 ck
->p_sample_delta_dts
= calloc( ck
->i_entries_dts
, sizeof( uint32_t ) );
2543 if( !ck
->p_sample_count_dts
|| !ck
->p_sample_delta_dts
)
2545 free( ck
->p_sample_count_dts
);
2546 free( ck
->p_sample_delta_dts
);
2547 msg_Err( p_demux
, "can't allocate memory for i_entry=%"PRIu32
, ck
->i_entries_dts
);
2548 ck
->i_entries_dts
= 0;
2553 i_sample_count
= ck
->i_sample_count
;
2555 for( uint32_t i
= 0; i
< ck
->i_entries_dts
; i
++ )
2557 if ( i_current_index_samples_left
)
2559 if ( i_current_index_samples_left
> i_sample_count
)
2561 ck
->p_sample_count_dts
[i
] = i_sample_count
;
2562 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2563 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2564 if ( i_sample_count
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2565 i_current_index_samples_left
-= i_sample_count
;
2567 assert( i
== ck
->i_entries_dts
- 1 );
2572 ck
->p_sample_count_dts
[i
] = i_current_index_samples_left
;
2573 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2574 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2575 if ( i_current_index_samples_left
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2576 i_sample_count
-= i_current_index_samples_left
;
2577 i_current_index_samples_left
= 0;
2583 if ( stts
->pi_sample_count
[i_index
] > i_sample_count
)
2585 ck
->p_sample_count_dts
[i
] = i_sample_count
;
2586 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2587 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2588 if ( i_sample_count
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2589 i_current_index_samples_left
= stts
->pi_sample_count
[i_index
] - i_sample_count
;
2591 assert( i
== ck
->i_entries_dts
- 1 );
2592 // keep building from same index
2596 ck
->p_sample_count_dts
[i
] = stts
->pi_sample_count
[i_index
];
2597 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2598 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2599 if ( stts
->pi_sample_count
[i_index
] ) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2600 i_sample_count
-= stts
->pi_sample_count
[i_index
];
2611 * Gives the delta between decoding time (dts) and composition table (pts)
2613 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "ctts" );
2614 if( p_box
&& p_box
->data
.p_ctts
)
2616 MP4_Box_data_ctts_t
*ctts
= p_box
->data
.p_ctts
;
2618 msg_Warn( p_demux
, "CTTS table of %"PRIu32
" entries", ctts
->i_entry_count
);
2620 int64_t i_cts_shift
= 0;
2621 const MP4_Box_t
*p_cslg
= MP4_BoxGet( p_demux_track
->p_stbl
, "cslg" );
2622 if( p_cslg
&& BOXDATA(p_cslg
) )
2623 i_cts_shift
= BOXDATA(p_cslg
)->ct_to_dts_shift
;
2625 /* Create pts-dts table per chunk */
2626 uint32_t i_index
= 0;
2627 uint32_t i_current_index_samples_left
= 0;
2629 for( uint32_t i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2631 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2632 uint32_t i_sample_count
;
2634 /* count how many entries are needed for this chunk
2635 * for p_sample_offset_pts and p_sample_count_pts */
2636 ck
->i_entries_pts
= 0;
2637 int i_ret
= xTTS_CountEntries( p_demux
, &ck
->i_entries_pts
, i_index
,
2638 i_current_index_samples_left
,
2640 ctts
->pi_sample_count
,
2641 ctts
->i_entry_count
);
2642 if ( i_ret
== VLC_EGENERIC
)
2646 ck
->p_sample_count_pts
= calloc( ck
->i_entries_pts
, sizeof( uint32_t ) );
2647 ck
->p_sample_offset_pts
= calloc( ck
->i_entries_pts
, sizeof( int32_t ) );
2648 if( !ck
->p_sample_count_pts
|| !ck
->p_sample_offset_pts
)
2650 free( ck
->p_sample_count_pts
);
2651 free( ck
->p_sample_offset_pts
);
2652 msg_Err( p_demux
, "can't allocate memory for i_entry=%"PRIu32
, ck
->i_entries_pts
);
2653 ck
->i_entries_pts
= 0;
2658 i_sample_count
= ck
->i_sample_count
;
2660 for( uint32_t i
= 0; i
< ck
->i_entries_pts
; i
++ )
2662 if ( i_current_index_samples_left
)
2664 if ( i_current_index_samples_left
> i_sample_count
)
2666 ck
->p_sample_count_pts
[i
] = i_sample_count
;
2667 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2668 i_current_index_samples_left
-= i_sample_count
;
2670 assert( i
== ck
->i_entries_pts
- 1 );
2675 ck
->p_sample_count_pts
[i
] = i_current_index_samples_left
;
2676 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2677 i_sample_count
-= i_current_index_samples_left
;
2678 i_current_index_samples_left
= 0;
2684 if ( ctts
->pi_sample_count
[i_index
] > i_sample_count
)
2686 ck
->p_sample_count_pts
[i
] = i_sample_count
;
2687 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2688 i_current_index_samples_left
= ctts
->pi_sample_count
[i_index
] - i_sample_count
;
2690 assert( i
== ck
->i_entries_pts
- 1 );
2691 // keep building from same index
2695 ck
->p_sample_count_pts
[i
] = ctts
->pi_sample_count
[i_index
];
2696 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2697 i_sample_count
-= ctts
->pi_sample_count
[i_index
];
2707 msg_Dbg( p_demux
, "track[Id 0x%x] read %"PRIu32
" samples length:%"PRId64
"s",
2708 p_demux_track
->i_track_ID
, p_demux_track
->i_sample_count
,
2709 i_next_dts
/ p_demux_track
->i_timescale
);
2716 * It computes the sample rate for a video track using the given sample
2719 static void TrackGetESSampleRate( demux_t
*p_demux
,
2720 unsigned *pi_num
, unsigned *pi_den
,
2721 const mp4_track_t
*p_track
,
2722 unsigned i_sd_index
,
2728 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( MP4_BoxGet( p_demux
->p_sys
->p_root
, "/moov" ),
2729 p_track
->i_track_ID
);
2730 MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_trak
, "mdia/mdhd" );
2731 if ( p_mdhd
&& BOXDATA(p_mdhd
) )
2733 vlc_ureduce( pi_num
, pi_den
,
2734 (uint64_t) BOXDATA(p_mdhd
)->i_timescale
* p_track
->i_sample_count
,
2735 (uint64_t) BOXDATA(p_mdhd
)->i_duration
,
2740 if( p_track
->i_chunk_count
== 0 )
2744 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[i_chunk
];
2745 while( p_chunk
> &p_track
->chunk
[0] &&
2746 p_chunk
[-1].i_sample_description_index
== i_sd_index
)
2751 uint64_t i_sample
= 0;
2752 uint64_t i_total_duration
= 0;
2755 i_sample
+= p_chunk
->i_sample_count
;
2756 i_total_duration
+= p_chunk
->i_duration
;
2759 while( p_chunk
< &p_track
->chunk
[p_track
->i_chunk_count
] &&
2760 p_chunk
->i_sample_description_index
== i_sd_index
);
2762 if( i_sample
> 0 && i_total_duration
)
2763 vlc_ureduce( pi_num
, pi_den
,
2764 i_sample
* p_track
->i_timescale
,
2771 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
2773 static int TrackCreateES( demux_t
*p_demux
, mp4_track_t
*p_track
,
2774 unsigned int i_chunk
, es_out_id_t
**pp_es
)
2776 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2777 unsigned int i_sample_description_index
;
2779 if( p_sys
->b_fragmented
|| p_track
->i_chunk_count
== 0 )
2780 i_sample_description_index
= 1; /* XXX */
2782 i_sample_description_index
=
2783 p_track
->chunk
[i_chunk
].i_sample_description_index
;
2788 if( !i_sample_description_index
)
2790 msg_Warn( p_demux
, "invalid SampleEntry index (track[Id 0x%x])",
2791 p_track
->i_track_ID
);
2792 return VLC_EGENERIC
;
2795 MP4_Box_t
*p_sample
= MP4_BoxGet( p_track
->p_stsd
, "[%d]",
2796 i_sample_description_index
- 1 );
2799 ( !p_sample
->data
.p_payload
&& p_track
->fmt
.i_cat
!= SPU_ES
) )
2801 msg_Warn( p_demux
, "cannot find SampleEntry (track[Id 0x%x])",
2802 p_track
->i_track_ID
);
2803 return VLC_EGENERIC
;
2806 p_track
->p_sample
= p_sample
;
2809 if( ( p_frma
= MP4_BoxGet( p_track
->p_sample
, "sinf/frma" ) ) && p_frma
->data
.p_frma
)
2811 msg_Warn( p_demux
, "Original Format Box: %4.4s", (char *)&p_frma
->data
.p_frma
->i_type
);
2813 p_sample
->i_type
= p_frma
->data
.p_frma
->i_type
;
2817 switch( p_track
->fmt
.i_cat
)
2820 if ( !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_demux
->p_sys
->f_fps
= (float)p_track
->fmt
.video
.i_frame_rate
/
2830 (float)p_track
->fmt
.video
.i_frame_rate_base
;
2835 if ( !SetupAudioES( p_demux
, p_track
, p_sample
) )
2836 return VLC_EGENERIC
;
2839 audio_replay_gain_t
*p_arg
= &p_track
->fmt
.audio_replay_gain
;
2840 const char *psz_meta
= vlc_meta_GetExtra( p_sys
->p_meta
, "replaygain_track_gain" );
2843 double f_gain
= us_atof( psz_meta
);
2844 p_arg
->pf_gain
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
;
2845 p_arg
->pb_gain
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
!= 0;
2847 psz_meta
= vlc_meta_GetExtra( p_sys
->p_meta
, "replaygain_track_peak" );
2850 double f_gain
= us_atof( psz_meta
);
2851 p_arg
->pf_peak
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
;
2852 p_arg
->pb_peak
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
> 0;
2858 if ( !SetupSpuES( p_demux
, p_track
, p_sample
) )
2859 return VLC_EGENERIC
;
2867 *pp_es
= MP4_AddTrackES( p_demux
->out
, p_track
);
2869 return ( !pp_es
|| *pp_es
) ? VLC_SUCCESS
: VLC_EGENERIC
;
2872 /* *** Try to find nearest sync points *** */
2873 static int TrackGetNearestSeekPoint( demux_t
*p_demux
, mp4_track_t
*p_track
,
2874 uint32_t i_sample
, uint32_t *pi_sync_sample
)
2876 int i_ret
= VLC_EGENERIC
;
2877 *pi_sync_sample
= 0;
2879 const MP4_Box_t
*p_stss
;
2880 if( ( p_stss
= MP4_BoxGet( p_track
->p_stbl
, "stss" ) ) )
2882 const MP4_Box_data_stss_t
*p_stss_data
= BOXDATA(p_stss
);
2883 msg_Dbg( p_demux
, "track[Id 0x%x] using Sync Sample Box (stss)",
2884 p_track
->i_track_ID
);
2885 for( unsigned i_index
= 0; i_index
< p_stss_data
->i_entry_count
; i_index
++ )
2887 if( i_index
>= p_stss_data
->i_entry_count
- 1 ||
2888 i_sample
< p_stss_data
->i_sample_number
[i_index
+1] )
2890 *pi_sync_sample
= p_stss_data
->i_sample_number
[i_index
];
2891 msg_Dbg( p_demux
, "stss gives %d --> %" PRIu32
" (sample number)",
2892 i_sample
, *pi_sync_sample
);
2893 i_ret
= VLC_SUCCESS
;
2899 /* try rap samples groups */
2900 const MP4_Box_t
*p_sbgp
= MP4_BoxGet( p_track
->p_stbl
, "sbgp" );
2901 for( ; p_sbgp
; p_sbgp
= p_sbgp
->p_next
)
2903 const MP4_Box_data_sbgp_t
*p_sbgp_data
= BOXDATA(p_sbgp
);
2904 if( p_sbgp
->i_type
!= ATOM_sbgp
|| !p_sbgp_data
)
2907 if( p_sbgp_data
->i_grouping_type
== SAMPLEGROUP_rap
)
2909 uint32_t i_group_sample
= 0;
2910 for ( uint32_t i
=0; i
<p_sbgp_data
->i_entry_count
; i
++ )
2912 /* Sample belongs to rap group ? */
2913 if( p_sbgp_data
->entries
.pi_group_description_index
[i
] != 0 )
2915 if( i_sample
< i_group_sample
)
2917 msg_Dbg( p_demux
, "sbgp lookup failed %" PRIu32
" (sample number)",
2921 else if ( i_sample
>= i_group_sample
&&
2922 *pi_sync_sample
< i_group_sample
)
2924 *pi_sync_sample
= i_group_sample
;
2925 i_ret
= VLC_SUCCESS
;
2928 i_group_sample
+= p_sbgp_data
->entries
.pi_sample_count
[i
];
2931 if( i_ret
== VLC_SUCCESS
&& *pi_sync_sample
)
2933 msg_Dbg( p_demux
, "sbgp gives %d --> %" PRIu32
" (sample number)",
2934 i_sample
, *pi_sync_sample
);
2942 /* given a time it return sample/chunk
2943 * it also update elst field of the track
2945 static int TrackTimeToSampleChunk( demux_t
*p_demux
, mp4_track_t
*p_track
,
2946 int64_t i_start
, uint32_t *pi_chunk
,
2947 uint32_t *pi_sample
)
2949 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2951 unsigned int i_sample
;
2952 unsigned int i_chunk
;
2955 /* FIXME see if it's needed to check p_track->i_chunk_count */
2956 if( p_track
->i_chunk_count
== 0 )
2957 return( VLC_EGENERIC
);
2959 /* handle elst (find the correct one) */
2960 MP4_TrackSetELST( p_demux
, p_track
, i_start
);
2961 if( p_track
->p_elst
&& p_track
->BOXDATA(p_elst
)->i_entry_count
> 0 )
2963 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
2964 int64_t i_mvt
= MP4_rescale( i_start
, CLOCK_FREQ
, p_sys
->i_timescale
);
2966 /* now calculate i_start for this elst */
2968 i_start
-= MP4_rescale( p_track
->i_elst_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
2976 /* to track time scale */
2977 i_start
= MP4_rescale( i_start
, CLOCK_FREQ
, p_track
->i_timescale
);
2978 /* add elst offset */
2979 if( ( elst
->i_media_rate_integer
[p_track
->i_elst
] > 0 ||
2980 elst
->i_media_rate_fraction
[p_track
->i_elst
] > 0 ) &&
2981 elst
->i_media_time
[p_track
->i_elst
] > 0 )
2983 i_start
+= elst
->i_media_time
[p_track
->i_elst
];
2986 msg_Dbg( p_demux
, "elst (%d) gives %"PRId64
"ms (movie)-> %"PRId64
2987 "ms (track)", p_track
->i_elst
,
2988 MP4_rescale( i_mvt
, p_sys
->i_timescale
, 1000 ),
2989 MP4_rescale( i_start
, p_track
->i_timescale
, 1000 ) );
2993 /* convert absolute time to in timescale unit */
2994 i_start
= MP4_rescale( i_start
, CLOCK_FREQ
, p_track
->i_timescale
);
2997 /* we start from sample 0/chunk 0, hope it won't take too much time */
2998 /* *** find good chunk *** */
2999 for( i_chunk
= 0; ; i_chunk
++ )
3001 if( i_chunk
+ 1 >= p_track
->i_chunk_count
)
3003 /* at the end and can't check if i_start in this chunk,
3004 it will be check while searching i_sample */
3005 i_chunk
= p_track
->i_chunk_count
- 1;
3009 if( (uint64_t)i_start
>= p_track
->chunk
[i_chunk
].i_first_dts
&&
3010 (uint64_t)i_start
< p_track
->chunk
[i_chunk
+ 1].i_first_dts
)
3016 /* *** find sample in the chunk *** */
3017 i_sample
= p_track
->chunk
[i_chunk
].i_sample_first
;
3018 i_dts
= p_track
->chunk
[i_chunk
].i_first_dts
;
3019 for( i_index
= 0; i_sample
< p_track
->chunk
[i_chunk
].i_sample_count
; )
3022 p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
] *
3023 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
] < (uint64_t)i_start
)
3026 p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
] *
3027 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
];
3029 i_sample
+= p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
];
3034 if( p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
] <= 0 )
3038 i_sample
+= ( i_start
- i_dts
) /
3039 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
];
3044 if( i_sample
>= p_track
->i_sample_count
)
3046 msg_Warn( p_demux
, "track[Id 0x%x] will be disabled "
3047 "(seeking too far) chunk=%d sample=%d",
3048 p_track
->i_track_ID
, i_chunk
, i_sample
);
3049 return( VLC_EGENERIC
);
3053 /* *** Try to find nearest sync points *** */
3054 uint32_t i_sync_sample
;
3056 TrackGetNearestSeekPoint( p_demux
, p_track
, i_sample
, &i_sync_sample
) )
3059 if( i_sync_sample
<= i_sample
)
3061 while( i_chunk
> 0 &&
3062 i_sync_sample
< p_track
->chunk
[i_chunk
].i_sample_first
)
3067 while( i_chunk
< p_track
->i_chunk_count
- 1 &&
3068 i_sync_sample
>= p_track
->chunk
[i_chunk
].i_sample_first
+
3069 p_track
->chunk
[i_chunk
].i_sample_count
)
3072 i_sample
= i_sync_sample
;
3075 *pi_chunk
= i_chunk
;
3076 *pi_sample
= i_sample
;
3081 static int TrackGotoChunkSample( demux_t
*p_demux
, mp4_track_t
*p_track
,
3082 unsigned int i_chunk
, unsigned int i_sample
)
3084 bool b_reselect
= false;
3086 /* now see if actual es is ok */
3087 if( p_track
->i_chunk
>= p_track
->i_chunk_count
||
3088 p_track
->chunk
[p_track
->i_chunk
].i_sample_description_index
!=
3089 p_track
->chunk
[i_chunk
].i_sample_description_index
)
3091 msg_Warn( p_demux
, "recreate ES for track[Id 0x%x]",
3092 p_track
->i_track_ID
);
3094 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
,
3095 p_track
->p_es
, &b_reselect
);
3097 es_out_Del( p_demux
->out
, p_track
->p_es
);
3099 p_track
->p_es
= NULL
;
3101 if( TrackCreateES( p_demux
, p_track
, i_chunk
, &p_track
->p_es
) )
3103 msg_Err( p_demux
, "cannot create es for track[Id 0x%x]",
3104 p_track
->i_track_ID
);
3106 p_track
->b_ok
= false;
3107 p_track
->b_selected
= false;
3108 return VLC_EGENERIC
;
3112 /* select again the new decoder */
3115 es_out_Control( p_demux
->out
, ES_OUT_SET_ES
, p_track
->p_es
);
3118 p_track
->i_chunk
= i_chunk
;
3119 p_track
->chunk
[i_chunk
].i_sample
= i_sample
- p_track
->chunk
[i_chunk
].i_sample_first
;
3120 p_track
->i_sample
= i_sample
;
3122 return p_track
->b_selected
? VLC_SUCCESS
: VLC_EGENERIC
;
3125 static void MP4_TrackRestart( demux_t
*p_demux
, mp4_track_t
*p_track
,
3126 MP4_Box_t
*p_params_box
)
3128 bool b_reselect
= false;
3131 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
,
3132 p_track
->p_es
, &b_reselect
);
3135 /* Save previous fragmented pos */
3136 uint32_t i_sample_pos_backup
= p_track
->i_sample
;
3137 mtime_t time_backup
= p_track
->i_time
;
3138 uint32_t timescale_backup
= p_track
->i_timescale
;
3140 /* Save previous format and ES */
3141 es_format_t fmtbackup
;
3142 es_out_id_t
*p_es_backup
= p_track
->p_es
;
3143 p_track
->p_es
= NULL
;
3144 es_format_Copy( &fmtbackup
, &p_track
->fmt
);
3145 es_format_Clean( &p_track
->fmt
);
3148 /* do the cleanup and recycle track / restart */
3149 MP4_TrackDestroy( p_demux
, p_track
);
3150 memset( p_track
, 0, sizeof(*p_track
) );
3152 assert(p_params_box
->i_type
== ATOM_trak
);
3153 MP4_TrackCreate( p_demux
, p_track
, p_params_box
, false, true );
3157 if( !es_format_IsSimilar( &fmtbackup
, &p_track
->fmt
) ||
3158 fmtbackup
.i_extra
!= p_track
->fmt
.i_extra
||
3159 memcmp( fmtbackup
.p_extra
, p_track
->fmt
.p_extra
, fmtbackup
.i_extra
) )
3162 es_out_Del( p_demux
->out
, p_es_backup
);
3164 if( !p_track
->b_chapters_source
)
3166 p_track
->p_es
= MP4_AddTrackES( p_demux
->out
, p_track
);
3167 p_track
->b_ok
= !!p_track
->p_es
;
3172 p_track
->p_es
= p_es_backup
;
3175 else if( p_es_backup
)
3177 es_out_Del( p_demux
->out
, p_es_backup
);
3180 /* select again the new decoder */
3181 if( b_reselect
&& p_track
->p_es
)
3182 es_out_Control( p_demux
->out
, ES_OUT_SET_ES
, p_track
->p_es
);
3184 es_format_Clean( &fmtbackup
);
3186 /* Restore fragmented pos */
3187 p_track
->i_sample
= i_sample_pos_backup
;
3188 p_track
->i_time
= MP4_rescale( time_backup
, timescale_backup
, p_track
->i_timescale
);
3191 /****************************************************************************
3193 ****************************************************************************
3194 * Parse track information and create all needed data to run a track
3195 * If it succeed b_ok is set to 1 else to 0
3196 ****************************************************************************/
3197 static void MP4_TrackSetup( demux_t
*p_demux
, mp4_track_t
*p_track
,
3198 MP4_Box_t
*p_box_trak
,
3199 bool b_create_es
, bool b_force_enable
)
3201 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3203 p_track
->p_track
= p_box_trak
;
3205 char language
[4] = { '\0' };
3206 char sdp_media_type
[8] = { '\0' };
3208 const MP4_Box_t
*p_tkhd
= MP4_BoxGet( p_box_trak
, "tkhd" );
3214 /* do we launch this track by default ? */
3216 ( ( BOXDATA(p_tkhd
)->i_flags
&MP4_TRACK_ENABLED
) != 0 );
3217 if( !p_track
->b_enable
)
3218 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3220 p_track
->i_track_ID
= BOXDATA(p_tkhd
)->i_track_ID
;
3222 p_track
->i_width
= BOXDATA(p_tkhd
)->i_width
/ BLOCK16x16
;
3223 p_track
->i_height
= BOXDATA(p_tkhd
)->i_height
/ BLOCK16x16
;
3224 p_track
->f_rotation
= BOXDATA(p_tkhd
)->f_rotation
;
3226 /* FIXME: unhandled box: tref */
3228 const MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_box_trak
, "mdia/mdhd" );
3229 const MP4_Box_t
*p_hdlr
= MP4_BoxGet( p_box_trak
, "mdia/hdlr" );
3231 if( ( !p_mdhd
)||( !p_hdlr
) )
3236 if( BOXDATA(p_mdhd
)->i_timescale
== 0 )
3238 msg_Warn( p_demux
, "Invalid track timescale " );
3241 p_track
->i_timescale
= BOXDATA(p_mdhd
)->i_timescale
;
3243 memcpy( &language
, BOXDATA(p_mdhd
)->rgs_language
, 3 );
3244 p_track
->b_mac_encoding
= BOXDATA(p_mdhd
)->b_mac_encoding
;
3246 switch( p_hdlr
->data
.p_hdlr
->i_handler_type
)
3249 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/smhd" ) )
3253 es_format_Change( &p_track
->fmt
, AUDIO_ES
, 0 );
3257 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/vmhd") )
3261 es_format_Change( &p_track
->fmt
, VIDEO_ES
, 0 );
3265 /* RTP Reception Hint tracks */
3266 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/hmhd" ) ||
3267 !MP4_BoxGet( p_box_trak
, "mdia/minf/stbl/stsd/rrtp" ) )
3273 /* parse the sdp message to find out whether the RTP stream contained audio or video */
3274 if( !( p_sdp
= MP4_BoxGet( p_box_trak
, "udta/hnti/sdp " ) ) )
3276 msg_Warn( p_demux
, "Didn't find sdp box to determine stream type" );
3280 memcpy( sdp_media_type
, BOXDATA(p_sdp
)->psz_text
, 7 );
3281 if( !strcmp(sdp_media_type
, "m=audio") )
3283 msg_Dbg( p_demux
, "Found audio Rtp: %s", sdp_media_type
);
3284 es_format_Change( &p_track
->fmt
, AUDIO_ES
, 0 );
3286 else if( !strcmp(sdp_media_type
, "m=video") )
3288 msg_Dbg( p_demux
, "Found video Rtp: %s", sdp_media_type
);
3289 es_format_Change( &p_track
->fmt
, VIDEO_ES
, 0 );
3293 msg_Warn( p_demux
, "Malformed track SDP message: %s", sdp_media_type
);
3296 p_track
->p_sdp
= p_sdp
;
3302 case( ATOM_subt
): /* ttml */
3304 case( ATOM_clcp
): /* closed captions */
3305 es_format_Change( &p_track
->fmt
, SPU_ES
, 0 );
3312 p_track
->asfinfo
.i_cat
= p_track
->fmt
.i_cat
;
3314 const MP4_Box_t
*p_elst
;
3315 p_track
->i_elst
= 0;
3316 p_track
->i_elst_time
= 0;
3317 if( ( p_track
->p_elst
= p_elst
= MP4_BoxGet( p_box_trak
, "edts/elst" ) ) )
3319 MP4_Box_data_elst_t
*elst
= BOXDATA(p_elst
);
3322 msg_Warn( p_demux
, "elst box found" );
3323 for( i
= 0; i
< elst
->i_entry_count
; i
++ )
3325 msg_Dbg( p_demux
, " - [%d] duration=%"PRId64
"ms media time=%"PRId64
3326 "ms) rate=%d.%d", i
,
3327 MP4_rescale( elst
->i_segment_duration
[i
], p_sys
->i_timescale
, 1000 ),
3328 elst
->i_media_time
[i
] >= 0 ?
3329 MP4_rescale( elst
->i_media_time
[i
], p_track
->i_timescale
, 1000 ) :
3331 elst
->i_media_rate_integer
[i
],
3332 elst
->i_media_rate_fraction
[i
] );
3339 p_dinf = MP4_BoxGet( p_minf, "dinf" );
3341 if( !( p_track
->p_stbl
= MP4_BoxGet( p_box_trak
,"mdia/minf/stbl" ) ) ||
3342 !( p_track
->p_stsd
= MP4_BoxGet( p_box_trak
,"mdia/minf/stbl/stsd") ) )
3348 if( *language
&& strcmp( language
, "```" ) && strcmp( language
, "und" ) )
3350 p_track
->fmt
.psz_language
= strdup( language
);
3353 const MP4_Box_t
*p_udta
= MP4_BoxGet( p_box_trak
, "udta" );
3356 const MP4_Box_t
*p_box_iter
;
3357 for( p_box_iter
= p_udta
->p_first
; p_box_iter
!= NULL
;
3358 p_box_iter
= p_box_iter
->p_next
)
3360 switch( p_box_iter
->i_type
)
3364 p_track
->fmt
.psz_description
=
3365 strndup( p_box_iter
->data
.p_binary
->p_blob
,
3366 p_box_iter
->data
.p_binary
->i_blob
);
3373 /* Create chunk index table and sample index table */
3374 if( TrackCreateChunksIndex( p_demux
,p_track
) ||
3375 TrackCreateSamplesIndex( p_demux
, p_track
) )
3377 msg_Err( p_demux
, "cannot create chunks index" );
3378 return; /* cannot create chunks index */
3381 p_track
->i_chunk
= 0;
3382 p_track
->i_sample
= 0;
3384 /* Mark chapter only track */
3385 if( p_sys
->p_tref_chap
)
3387 MP4_Box_data_tref_generic_t
*p_chap
= p_sys
->p_tref_chap
->data
.p_tref_generic
;
3390 for( i
= 0; i
< p_chap
->i_entry_count
; i
++ )
3392 if( p_track
->i_track_ID
== p_chap
->i_track_ID
[i
] &&
3393 p_track
->fmt
.i_cat
== UNKNOWN_ES
)
3395 p_track
->b_chapters_source
= true;
3396 p_track
->b_enable
= false;
3402 const MP4_Box_t
*p_tsel
;
3404 if( b_force_enable
&&
3405 ( p_track
->fmt
.i_cat
== VIDEO_ES
|| p_track
->fmt
.i_cat
== AUDIO_ES
) )
3407 msg_Warn( p_demux
, "Enabling track[Id 0x%x] (buggy file without enabled track)",
3408 p_track
->i_track_ID
);
3409 p_track
->b_enable
= true;
3410 p_track
->fmt
.i_priority
= ES_PRIORITY_SELECTABLE_MIN
;
3412 else if ( (p_tsel
= MP4_BoxGet( p_box_trak
, "udta/tsel" )) )
3414 if ( BOXDATA(p_tsel
) && BOXDATA(p_tsel
)->i_switch_group
)
3416 p_track
->i_switch_group
= BOXDATA(p_tsel
)->i_switch_group
;
3417 int i_priority
= ES_PRIORITY_SELECTABLE_MIN
;
3418 for ( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
3420 const mp4_track_t
*p_other
= &p_sys
->track
[i
];
3421 if( p_other
&& p_other
!= p_track
&&
3422 p_other
->fmt
.i_cat
== p_track
->fmt
.i_cat
&&
3423 p_track
->i_switch_group
== p_other
->i_switch_group
)
3424 i_priority
= __MAX( i_priority
, p_other
->fmt
.i_priority
+ 1 );
3426 /* VLC only support ES priority for AUDIO_ES and SPU_ES.
3427 If there's another VIDEO_ES in the same group, we need to unselect it then */
3428 if ( p_track
->fmt
.i_cat
== VIDEO_ES
&& i_priority
> ES_PRIORITY_SELECTABLE_MIN
)
3429 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3431 p_track
->fmt
.i_priority
= i_priority
;
3434 /* If there's no tsel, try to enable the track coming first in edit list */
3435 else if ( p_track
->p_elst
&& p_track
->fmt
.i_priority
== ES_PRIORITY_SELECTABLE_MIN
)
3437 #define MAX_SELECTABLE (INT_MAX - ES_PRIORITY_SELECTABLE_MIN)
3438 for ( uint32_t i
=0; i
<p_track
->BOXDATA(p_elst
)->i_entry_count
; i
++ )
3440 if ( p_track
->BOXDATA(p_elst
)->i_media_time
[i
] >= 0 &&
3441 p_track
->BOXDATA(p_elst
)->i_segment_duration
[i
] )
3443 /* We do selection by inverting start time into priority.
3444 The track with earliest edit will have the highest prio */
3445 const int i_time
= __MIN( MAX_SELECTABLE
, p_track
->BOXDATA(p_elst
)->i_media_time
[i
] );
3446 p_track
->fmt
.i_priority
= ES_PRIORITY_SELECTABLE_MIN
+ MAX_SELECTABLE
- i_time
;
3452 if( p_sys
->hacks
.es_cat_filters
&& (p_sys
->hacks
.es_cat_filters
& p_track
->fmt
.i_cat
) == 0 )
3454 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3457 if( TrackCreateES( p_demux
,
3458 p_track
, p_track
->i_chunk
,
3459 (p_track
->b_chapters_source
|| !b_create_es
) ? NULL
: &p_track
->p_es
) )
3461 msg_Err( p_demux
, "cannot create es for track[Id 0x%x]",
3462 p_track
->i_track_ID
);
3466 p_track
->b_ok
= true;
3469 static void DestroyChunk( mp4_chunk_t
*ck
)
3471 free( ck
->p_sample_count_dts
);
3472 free( ck
->p_sample_delta_dts
);
3473 free( ck
->p_sample_count_pts
);
3474 free( ck
->p_sample_offset_pts
);
3475 free( ck
->p_sample_size
);
3478 /****************************************************************************
3480 ****************************************************************************
3481 * Cleans a track created by MP4_TrackCreate.
3482 ****************************************************************************/
3483 static void MP4_TrackClean( es_out_t
*out
, mp4_track_t
*p_track
)
3485 es_format_Clean( &p_track
->fmt
);
3488 es_out_Del( out
, p_track
->p_es
);
3490 if( p_track
->chunk
)
3492 for( unsigned int i_chunk
= 0; i_chunk
< p_track
->i_chunk_count
; i_chunk
++ )
3493 DestroyChunk( &p_track
->chunk
[i_chunk
] );
3495 free( p_track
->chunk
);
3497 if( !p_track
->i_sample_size
)
3498 free( p_track
->p_sample_size
);
3500 if ( p_track
->asfinfo
.p_frame
)
3501 block_ChainRelease( p_track
->asfinfo
.p_frame
);
3503 free( p_track
->context
.runs
.p_array
);
3506 static void MP4_TrackInit( mp4_track_t
*p_track
)
3508 memset( p_track
, 0, sizeof(mp4_track_t
) );
3509 es_format_Init( &p_track
->fmt
, UNKNOWN_ES
, 0 );
3510 p_track
->i_timescale
= 1;
3513 static void MP4_TrackSelect( demux_t
*p_demux
, mp4_track_t
*p_track
, bool b_select
)
3515 if( !p_track
->b_ok
|| p_track
->b_chapters_source
)
3518 if( b_select
== p_track
->b_selected
)
3521 if( !b_select
&& p_track
->p_es
)
3523 es_out_Control( p_demux
->out
, ES_OUT_SET_ES_STATE
,
3524 p_track
->p_es
, false );
3527 p_track
->b_selected
= b_select
;
3530 static int MP4_TrackSeek( demux_t
*p_demux
, mp4_track_t
*p_track
,
3536 if( !p_track
->b_ok
|| p_track
->b_chapters_source
)
3537 return VLC_EGENERIC
;
3539 p_track
->b_selected
= false;
3541 if( TrackTimeToSampleChunk( p_demux
, p_track
, i_start
,
3542 &i_chunk
, &i_sample
) )
3544 msg_Warn( p_demux
, "cannot select track[Id 0x%x]",
3545 p_track
->i_track_ID
);
3546 return VLC_EGENERIC
;
3549 p_track
->b_selected
= true;
3550 if( !TrackGotoChunkSample( p_demux
, p_track
, i_chunk
, i_sample
) )
3551 p_track
->b_selected
= true;
3553 p_track
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
3555 return p_track
->b_selected
? VLC_SUCCESS
: VLC_EGENERIC
;
3560 * 3 types: for audio
3563 static inline uint32_t MP4_GetFixedSampleSize( const mp4_track_t
*p_track
,
3564 const MP4_Box_data_sample_soun_t
*p_soun
)
3566 uint32_t i_size
= p_track
->i_sample_size
;
3568 assert( p_track
->i_sample_size
!= 0 );
3570 /* QuickTime "built-in" support case fixups */
3571 if( p_track
->fmt
.i_cat
== AUDIO_ES
&&
3572 p_soun
->i_compressionid
== 0 && p_track
->i_sample_size
<= 2 )
3574 switch( p_track
->fmt
.i_codec
)
3577 i_size
= p_soun
->i_channelcount
;
3579 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
3583 case VLC_CODEC_S24L
:
3584 case VLC_CODEC_S24B
:
3585 case VLC_CODEC_S32L
:
3586 case VLC_CODEC_S32B
:
3587 case VLC_CODEC_F32L
:
3588 case VLC_CODEC_F32B
:
3589 case VLC_CODEC_F64L
:
3590 case VLC_CODEC_F64B
:
3591 if( p_track
->i_sample_size
< ((p_soun
->i_samplesize
+7U)/8U) * p_soun
->i_channelcount
)
3592 i_size
= ((p_soun
->i_samplesize
+7)/8) * p_soun
->i_channelcount
;
3594 case VLC_CODEC_ALAW
:
3595 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
3596 i_size
= p_soun
->i_channelcount
;
3606 static uint32_t MP4_TrackGetReadSize( mp4_track_t
*p_track
, uint32_t *pi_nb_samples
)
3608 uint32_t i_size
= 0;
3611 if ( p_track
->i_sample
== p_track
->i_sample_count
)
3614 if ( p_track
->fmt
.i_cat
!= AUDIO_ES
)
3618 if( p_track
->i_sample_size
== 0 ) /* all sizes are different */
3619 return p_track
->p_sample_size
[p_track
->i_sample
];
3621 return p_track
->i_sample_size
;
3625 const MP4_Box_data_sample_soun_t
*p_soun
= p_track
->p_sample
->data
.p_sample_soun
;
3626 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[p_track
->i_chunk
];
3627 uint32_t i_max_samples
= p_chunk
->i_sample_count
- p_chunk
->i_sample
;
3629 /* Group audio packets so we don't call demux for single sample unit */
3630 if( p_track
->fmt
.i_original_fourcc
== VLC_CODEC_DVD_LPCM
&&
3631 p_soun
->i_constLPCMframesperaudiopacket
&&
3632 p_soun
->i_constbytesperaudiopacket
)
3634 /* uncompressed case */
3635 uint32_t i_packets
= i_max_samples
/ p_soun
->i_constLPCMframesperaudiopacket
;
3636 if ( UINT32_MAX
/ p_soun
->i_constbytesperaudiopacket
< i_packets
)
3637 i_packets
= UINT32_MAX
/ p_soun
->i_constbytesperaudiopacket
;
3638 *pi_nb_samples
= i_packets
* p_soun
->i_constLPCMframesperaudiopacket
;
3639 return i_packets
* p_soun
->i_constbytesperaudiopacket
;
3642 if( p_track
->fmt
.i_original_fourcc
== VLC_FOURCC('r','r','t','p') )
3645 return p_track
->i_sample_size
;
3648 /* all samples have a different size */
3649 if( p_track
->i_sample_size
== 0 )
3652 return p_track
->p_sample_size
[p_track
->i_sample
];
3655 if( p_soun
->i_qt_version
== 1 )
3657 if ( p_soun
->i_compressionid
== 0xFFFE )
3659 *pi_nb_samples
= 1; /* != number of audio samples */
3660 if ( p_track
->i_sample_size
)
3661 return p_track
->i_sample_size
;
3663 return p_track
->p_sample_size
[p_track
->i_sample
];
3665 else if ( p_soun
->i_compressionid
!= 0 || p_soun
->i_bytes_per_sample
> 1 ) /* compressed */
3667 /* in this case we are dealing with compressed data
3668 -2 in V1: additional fields are meaningless (VBR and such) */
3669 *pi_nb_samples
= i_max_samples
;//p_track->chunk[p_track->i_chunk].i_sample_count;
3670 if( p_track
->fmt
.audio
.i_blockalign
> 1 )
3671 *pi_nb_samples
= p_soun
->i_sample_per_packet
;
3672 i_size
= *pi_nb_samples
/ p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
;
3675 else /* uncompressed case */
3678 if( p_track
->fmt
.audio
.i_blockalign
> 1 )
3681 i_packets
= i_max_samples
/ p_soun
->i_sample_per_packet
;
3683 if ( UINT32_MAX
/ p_soun
->i_bytes_per_frame
< i_packets
)
3684 i_packets
= UINT32_MAX
/ p_soun
->i_bytes_per_frame
;
3686 *pi_nb_samples
= i_packets
* p_soun
->i_sample_per_packet
;
3687 i_size
= i_packets
* p_soun
->i_bytes_per_frame
;
3692 /* uncompressed v0 (qt) or... not (ISO) */
3694 /* Quicktime built-in support handling */
3695 if( p_soun
->i_compressionid
== 0 && p_track
->i_sample_size
== 1 )
3697 switch( p_track
->fmt
.i_codec
)
3699 /* sample size is not integer */
3701 *pi_nb_samples
= 160 * p_track
->fmt
.audio
.i_channels
;
3702 return 33 * p_track
->fmt
.audio
.i_channels
;
3708 /* More regular V0 cases */
3709 uint32_t i_max_v0_samples
;
3710 switch( p_track
->fmt
.i_codec
)
3712 /* Compressed samples in V0 */
3713 case VLC_CODEC_AMR_NB
:
3714 case VLC_CODEC_AMR_WB
:
3715 i_max_v0_samples
= 16;
3717 case VLC_CODEC_MPGA
:
3720 i_max_v0_samples
= 1;
3723 /* Read 25ms of samples (uncompressed) */
3724 i_max_v0_samples
= p_track
->fmt
.audio
.i_rate
/ 40 *
3725 p_track
->fmt
.audio
.i_channels
;
3726 if( i_max_v0_samples
< 1 )
3727 i_max_v0_samples
= 1;
3732 for( uint32_t i
=p_track
->i_sample
;
3733 i
<p_chunk
->i_sample_first
+p_chunk
->i_sample_count
&&
3734 i
<p_track
->i_sample_count
;
3738 if ( p_track
->i_sample_size
== 0 )
3739 i_size
+= p_track
->p_sample_size
[i
];
3741 i_size
+= MP4_GetFixedSampleSize( p_track
, p_soun
);
3743 /* Try to detect compression in ISO */
3744 if(p_soun
->i_compressionid
!= 0)
3746 /* Return only 1 sample */
3750 if ( *pi_nb_samples
== i_max_v0_samples
)
3755 //fprintf( stderr, "size=%d\n", i_size );
3759 static uint64_t MP4_TrackGetPos( mp4_track_t
*p_track
)
3761 unsigned int i_sample
;
3764 i_pos
= p_track
->chunk
[p_track
->i_chunk
].i_offset
;
3766 if( p_track
->i_sample_size
)
3768 MP4_Box_data_sample_soun_t
*p_soun
=
3769 p_track
->p_sample
->data
.p_sample_soun
;
3771 /* Quicktime builtin support, _must_ ignore sample tables */
3772 if( p_track
->fmt
.i_cat
== AUDIO_ES
&& p_soun
->i_compressionid
== 0 &&
3773 p_track
->i_sample_size
== 1 )
3775 switch( p_track
->fmt
.i_codec
)
3777 case VLC_CODEC_GSM
: /* # Samples > data size */
3778 i_pos
+= ( p_track
->i_sample
-
3779 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) / 160 * 33;
3786 if( p_track
->fmt
.i_cat
!= AUDIO_ES
|| p_soun
->i_qt_version
== 0 ||
3787 p_track
->fmt
.audio
.i_blockalign
<= 1 ||
3788 p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
== 0 )
3790 i_pos
+= ( p_track
->i_sample
-
3791 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) *
3792 MP4_GetFixedSampleSize( p_track
, p_soun
);
3796 /* we read chunk by chunk unless a blockalign is requested */
3797 i_pos
+= ( p_track
->i_sample
- p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) /
3798 p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
;
3803 for( i_sample
= p_track
->chunk
[p_track
->i_chunk
].i_sample_first
;
3804 i_sample
< p_track
->i_sample
; i_sample
++ )
3806 i_pos
+= p_track
->p_sample_size
[i_sample
];
3813 static int MP4_TrackNextSample( demux_t
*p_demux
, mp4_track_t
*p_track
, uint32_t i_samples
)
3815 if ( UINT32_MAX
- p_track
->i_sample
< i_samples
)
3817 p_track
->i_sample
= UINT32_MAX
;
3818 return VLC_EGENERIC
;
3821 p_track
->i_sample
+= i_samples
;
3823 if( p_track
->i_sample
>= p_track
->i_sample_count
)
3824 return VLC_EGENERIC
;
3826 /* Have we changed chunk ? */
3827 if( p_track
->i_sample
>=
3828 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
+
3829 p_track
->chunk
[p_track
->i_chunk
].i_sample_count
)
3831 if( TrackGotoChunkSample( p_demux
, p_track
, p_track
->i_chunk
+ 1,
3832 p_track
->i_sample
) )
3834 msg_Warn( p_demux
, "track[0x%x] will be disabled "
3835 "(cannot restart decoder)", p_track
->i_track_ID
);
3836 MP4_TrackSelect( p_demux
, p_track
, false );
3837 return VLC_EGENERIC
;
3841 /* Have we changed elst */
3842 if( p_track
->p_elst
&& p_track
->BOXDATA(p_elst
)->i_entry_count
> 0 )
3844 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3845 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
3846 uint64_t i_mvt
= MP4_rescale( MP4_TrackGetDTS( p_demux
, p_track
),
3847 CLOCK_FREQ
, p_sys
->i_timescale
);
3848 if( (unsigned int)p_track
->i_elst
< elst
->i_entry_count
&&
3849 i_mvt
>= p_track
->i_elst_time
+
3850 elst
->i_segment_duration
[p_track
->i_elst
] )
3852 MP4_TrackSetELST( p_demux
, p_track
,
3853 MP4_TrackGetDTS( p_demux
, p_track
) );
3860 static void MP4_TrackSetELST( demux_t
*p_demux
, mp4_track_t
*tk
,
3863 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3864 int i_elst_last
= tk
->i_elst
;
3866 /* handle elst (find the correct one) */
3868 tk
->i_elst_time
= 0;
3869 if( tk
->p_elst
&& tk
->BOXDATA(p_elst
)->i_entry_count
> 0 )
3871 MP4_Box_data_elst_t
*elst
= tk
->BOXDATA(p_elst
);
3872 int64_t i_mvt
= MP4_rescale( i_time
, CLOCK_FREQ
, p_sys
->i_timescale
);
3874 for( tk
->i_elst
= 0; (unsigned int)tk
->i_elst
< elst
->i_entry_count
; tk
->i_elst
++ )
3876 mtime_t i_dur
= elst
->i_segment_duration
[tk
->i_elst
];
3878 if( tk
->i_elst_time
<= i_mvt
&& i_mvt
< tk
->i_elst_time
+ i_dur
)
3882 tk
->i_elst_time
+= i_dur
;
3885 if( (unsigned int)tk
->i_elst
>= elst
->i_entry_count
)
3887 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
3888 tk
->i_elst
= elst
->i_entry_count
- 1;
3889 tk
->i_elst_time
-= elst
->i_segment_duration
[tk
->i_elst
];
3892 if( elst
->i_media_time
[tk
->i_elst
] < 0 )
3895 tk
->i_elst_time
+= elst
->i_segment_duration
[tk
->i_elst
];
3898 if( i_elst_last
!= tk
->i_elst
)
3900 msg_Warn( p_demux
, "elst old=%d new=%d", i_elst_last
, tk
->i_elst
);
3901 tk
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
3905 /******************************************************************************
3906 * Here are the functions used for fragmented MP4
3907 *****************************************************************************/
3911 * \Note If we call that function too soon,
3912 * before the track has been selected by MP4_TrackSelect
3913 * (during the first execution of Demux), then the track gets disabled
3915 static int ReInitDecoder( demux_t
*p_demux
, const MP4_Box_t
*p_root
,
3916 mp4_track_t
*p_track
)
3918 MP4_Box_t
*p_paramsbox
= MP4_BoxGet( p_root
, "/moov/trak[0]" );
3920 return VLC_EGENERIC
;
3922 MP4_TrackRestart( p_demux
, p_track
, p_paramsbox
);
3924 /* Temporary hack until we support track selection */
3925 p_track
->b_selected
= true;
3926 p_track
->b_enable
= true;
3932 static stime_t
GetCumulatedDuration( demux_t
*p_demux
)
3934 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3935 stime_t i_max_duration
= 0;
3937 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
3939 stime_t i_track_duration
= 0;
3940 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_sys
->p_moov
, p_sys
->track
[i
].i_track_ID
);
3941 const MP4_Box_t
*p_stsz
;
3942 const MP4_Box_t
*p_tkhd
;
3943 if ( (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) &&
3944 (p_stsz
= MP4_BoxGet( p_trak
, "mdia/minf/stbl/stsz" )) &&
3945 /* duration might be wrong an be set to whole duration :/ */
3946 BOXDATA(p_stsz
)->i_sample_count
> 0 )
3948 i_max_duration
= __MAX( (uint64_t)i_max_duration
, BOXDATA(p_tkhd
)->i_duration
);
3951 if( p_sys
->p_fragsindex
)
3953 i_track_duration
+= MP4_Fragment_Index_GetTrackDuration( p_sys
->p_fragsindex
, i
);
3956 i_max_duration
= __MAX( i_max_duration
, i_track_duration
);
3959 return i_max_duration
;
3962 static int ProbeIndex( demux_t
*p_demux
)
3964 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3965 uint64_t i_stream_size
;
3966 uint8_t mfro
[MP4_MFRO_BOXSIZE
];
3967 assert( p_sys
->b_seekable
);
3969 if ( MP4_BoxCount( p_sys
->p_root
, "/mfra" ) )
3970 return VLC_EGENERIC
;
3972 i_stream_size
= stream_Size( p_demux
->s
);
3973 if ( ( i_stream_size
>> 62 ) ||
3974 ( i_stream_size
< MP4_MFRO_BOXSIZE
) ||
3975 ( vlc_stream_Seek( p_demux
->s
, i_stream_size
- MP4_MFRO_BOXSIZE
) != VLC_SUCCESS
)
3978 msg_Dbg( p_demux
, "Probing tail for mfro has failed" );
3979 return VLC_EGENERIC
;
3982 if ( vlc_stream_Read( p_demux
->s
, &mfro
, MP4_MFRO_BOXSIZE
) == MP4_MFRO_BOXSIZE
&&
3983 VLC_FOURCC(mfro
[4],mfro
[5],mfro
[6],mfro
[7]) == ATOM_mfro
&&
3984 GetDWBE( &mfro
) == MP4_MFRO_BOXSIZE
)
3986 uint32_t i_offset
= GetDWBE( &mfro
[12] );
3987 msg_Dbg( p_demux
, "will read mfra index at %"PRIu64
, i_stream_size
- i_offset
);
3988 if ( i_stream_size
> i_offset
&&
3989 vlc_stream_Seek( p_demux
->s
, i_stream_size
- i_offset
) == VLC_SUCCESS
)
3991 msg_Dbg( p_demux
, "reading mfra index at %"PRIu64
, i_stream_size
- i_offset
);
3992 const uint32_t stoplist
[] = { ATOM_mfra
, 0 };
3993 MP4_ReadBoxContainerChildren( p_demux
->s
, p_sys
->p_root
, stoplist
);
3997 return VLC_EGENERIC
;
4000 static stime_t
GetMoovTrackDuration( demux_sys_t
*p_sys
, unsigned i_track_ID
)
4002 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_sys
->p_moov
, i_track_ID
);
4003 const MP4_Box_t
*p_stsz
;
4004 const MP4_Box_t
*p_tkhd
;
4005 if ( (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) &&
4006 (p_stsz
= MP4_BoxGet( p_trak
, "mdia/minf/stbl/stsz" )) &&
4007 /* duration might be wrong an be set to whole duration :/ */
4008 BOXDATA(p_stsz
)->i_sample_count
> 0 )
4010 return BOXDATA(p_tkhd
)->i_duration
; /* In movie / mvhd scale */
4015 static bool GetMoofTrackDuration( MP4_Box_t
*p_moov
, MP4_Box_t
*p_moof
,
4016 unsigned i_track_ID
, stime_t
*p_duration
)
4018 if ( !p_moof
|| !p_moov
)
4021 MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
4024 if ( p_traf
->i_type
!= ATOM_traf
)
4026 p_traf
= p_traf
->p_next
;
4030 const MP4_Box_t
*p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" );
4031 const MP4_Box_t
*p_trun
= MP4_BoxGet( p_traf
, "trun" );
4032 if ( !p_tfhd
|| !p_trun
|| i_track_ID
!= BOXDATA(p_tfhd
)->i_track_ID
)
4034 p_traf
= p_traf
->p_next
;
4038 uint32_t i_track_timescale
= 0;
4039 uint32_t i_track_defaultsampleduration
= 0;
4041 /* set trex for defaults */
4042 MP4_Box_t
*p_trex
= MP4_GetTrexByTrackID( p_moov
, BOXDATA(p_tfhd
)->i_track_ID
);
4045 i_track_defaultsampleduration
= BOXDATA(p_trex
)->i_default_sample_duration
;
4048 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_moov
, BOXDATA(p_tfhd
)->i_track_ID
);
4051 MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_trak
, "mdia/mdhd" );
4053 i_track_timescale
= BOXDATA(p_mdhd
)->i_timescale
;
4056 if ( !i_track_timescale
)
4058 p_traf
= p_traf
->p_next
;
4062 uint64_t i_traf_duration
= 0;
4063 while ( p_trun
&& p_tfhd
)
4065 if ( p_trun
->i_type
!= ATOM_trun
)
4067 p_trun
= p_trun
->p_next
;
4070 const MP4_Box_data_trun_t
*p_trundata
= p_trun
->data
.p_trun
;
4072 /* Sum total time */
4073 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4075 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4076 i_traf_duration
+= p_trundata
->p_samples
[i
].i_duration
;
4078 else if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
4080 i_traf_duration
+= p_trundata
->i_sample_count
*
4081 BOXDATA(p_tfhd
)->i_default_sample_duration
;
4085 i_traf_duration
+= p_trundata
->i_sample_count
*
4086 i_track_defaultsampleduration
;
4089 p_trun
= p_trun
->p_next
;
4092 *p_duration
= i_traf_duration
;
4099 static int ProbeFragments( demux_t
*p_demux
, bool b_force
, bool *pb_fragmented
)
4101 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4103 msg_Dbg( p_demux
, "probing fragments from %"PRId64
, vlc_stream_Tell( p_demux
->s
) );
4105 assert( p_sys
->p_root
);
4107 MP4_Box_t
*p_vroot
= MP4_BoxNew(ATOM_root
);
4109 return VLC_EGENERIC
;
4111 if( p_sys
->b_seekable
&& (p_sys
->b_fastseekable
|| b_force
) )
4113 MP4_ReadBoxContainerChildren( p_demux
->s
, p_vroot
, NULL
); /* Get the rest of the file */
4114 p_sys
->b_fragments_probed
= true;
4116 const unsigned i_moof
= MP4_BoxCount( p_vroot
, "/moof" );
4119 *pb_fragmented
= true;
4120 p_sys
->p_fragsindex
= MP4_Fragments_Index_New( p_sys
->i_tracks
, i_moof
);
4121 if( !p_sys
->p_fragsindex
)
4123 MP4_BoxFree( p_vroot
);
4124 return VLC_EGENERIC
;
4127 stime_t
*pi_track_times
= calloc( p_sys
->i_tracks
, sizeof(*pi_track_times
) );
4128 if( !pi_track_times
)
4130 MP4_Fragments_Index_Delete( p_sys
->p_fragsindex
);
4131 p_sys
->p_fragsindex
= NULL
;
4132 MP4_BoxFree( p_vroot
);
4133 return VLC_EGENERIC
;
4138 for( MP4_Box_t
*p_moof
= p_vroot
->p_first
; p_moof
; p_moof
= p_moof
->p_next
)
4140 if( p_moof
->i_type
!= ATOM_moof
)
4143 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4145 stime_t i_duration
= 0;
4146 MP4_Box_t
*p_tfdt
= NULL
;
4147 MP4_Box_t
*p_traf
= MP4_GetTrafByTrackID( p_moof
, p_sys
->track
[i
].i_track_ID
);
4149 p_tfdt
= MP4_BoxGet( p_traf
, "tfdt" );
4151 /* Set first fragment time offset from moov */
4153 pi_track_times
[i
] = GetMoovTrackDuration( p_sys
, p_sys
->track
[i
].i_track_ID
);
4155 if( p_tfdt
&& BOXDATA(p_tfdt
) )
4157 pi_track_times
[i
] = p_tfdt
->data
.p_tfdt
->i_base_media_decode_time
;
4159 else if( index
== 0 ) /* Set first fragment time offset from moov */
4161 i_duration
= GetMoovTrackDuration( p_sys
, p_sys
->track
[i
].i_track_ID
);
4162 pi_track_times
[i
] = MP4_rescale( i_duration
, p_sys
->i_timescale
, p_sys
->track
[i
].i_timescale
);
4165 stime_t i_movietime
= MP4_rescale( pi_track_times
[i
], p_sys
->track
[i
].i_timescale
, p_sys
->i_timescale
);
4166 p_sys
->p_fragsindex
->p_times
[index
* p_sys
->i_tracks
+ i
] = i_movietime
;
4168 if( GetMoofTrackDuration( p_sys
->p_moov
, p_moof
, p_sys
->track
[i
].i_track_ID
, &i_duration
) )
4169 pi_track_times
[i
] += i_duration
;
4172 p_sys
->p_fragsindex
->pi_pos
[index
++] = p_moof
->i_pos
;
4175 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4177 stime_t i_movietime
= MP4_rescale( pi_track_times
[i
], p_sys
->track
[i
].i_timescale
, p_sys
->i_timescale
);
4178 if( p_sys
->p_fragsindex
->i_last_time
< i_movietime
)
4179 p_sys
->p_fragsindex
->i_last_time
= i_movietime
;
4182 free( pi_track_times
);
4184 MP4_Fragments_Index_Dump( VLC_OBJECT(p_demux
), p_sys
->p_fragsindex
, p_sys
->i_timescale
);
4190 /* We stop at first moof, which validates our fragmentation condition
4191 * and we'll find others while reading. */
4192 const uint32_t excllist
[] = { ATOM_moof
, 0 };
4193 MP4_ReadBoxContainerRestricted( p_demux
->s
, p_vroot
, NULL
, excllist
);
4194 /* Peek since we stopped before restriction */
4195 const uint8_t *p_peek
;
4196 if ( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) == 8 )
4197 *pb_fragmented
= (VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) == ATOM_moof
);
4199 *pb_fragmented
= false;
4202 MP4_BoxFree( p_vroot
);
4204 MP4_Box_t
*p_mehd
= MP4_BoxGet( p_sys
->p_moov
, "mvex/mehd");
4206 p_sys
->i_cumulated_duration
= GetCumulatedDuration( p_demux
);
4211 static void FragResetContext( demux_sys_t
*p_sys
)
4213 if( p_sys
->context
.p_fragment_atom
)
4215 if( p_sys
->context
.p_fragment_atom
!= p_sys
->p_moov
)
4216 MP4_BoxFree( p_sys
->context
.p_fragment_atom
);
4217 p_sys
->context
.p_fragment_atom
= NULL
;
4219 p_sys
->context
.i_current_box_type
= 0;
4221 for ( uint32_t i
=0; i
<p_sys
->i_tracks
; i
++ )
4223 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4224 p_track
->context
.i_default_sample_size
= 0;
4225 p_track
->context
.i_default_sample_duration
= 0;
4229 static int FragDemuxTrack( demux_t
*p_demux
, mp4_track_t
*p_track
,
4230 unsigned i_max_preload
)
4232 if( !p_track
->b_ok
||
4233 p_track
->context
.runs
.i_current
>= p_track
->context
.runs
.i_count
)
4234 return VLC_DEMUXER_EOS
;
4236 const MP4_Box_data_trun_t
*p_trun
=
4237 p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].p_trun
->data
.p_trun
;
4239 if( p_track
->context
.i_trun_sample
>= p_trun
->i_sample_count
)
4240 return VLC_DEMUXER_EOS
;
4242 uint32_t dur
= p_track
->context
.i_default_sample_duration
,
4243 len
= p_track
->context
.i_default_sample_size
;
4245 if( vlc_stream_Tell(p_demux
->s
) != p_track
->context
.i_trun_sample_pos
&&
4246 MP4_Seek( p_demux
->s
, p_track
->context
.i_trun_sample_pos
) != VLC_SUCCESS
)
4247 return VLC_DEMUXER_EOF
;
4249 const stime_t i_demux_max_dts
= (i_max_preload
< UINT_MAX
) ?
4250 p_track
->i_time
+ MP4_rescale( i_max_preload
, CLOCK_FREQ
, p_track
->i_timescale
) :
4253 for( uint32_t i
= p_track
->context
.i_trun_sample
; i
< p_trun
->i_sample_count
; i
++ )
4255 const stime_t i_dts
= p_track
->i_time
;
4256 stime_t i_pts
= i_dts
;
4258 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4259 dur
= p_trun
->p_samples
[i
].i_duration
;
4261 if( i_dts
> i_demux_max_dts
)
4262 return VLC_DEMUXER_SUCCESS
;
4264 p_track
->i_time
+= dur
;
4265 p_track
->context
.i_trun_sample
= i
+ 1;
4267 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
)
4269 if ( p_trun
->i_version
== 1 )
4270 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v1
;
4271 else if( p_trun
->p_samples
[i
].i_composition_time_offset
.v0
< 0xFF000000 )
4272 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v0
;
4273 else /* version 0 with negative */
4274 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v1
;
4277 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
4278 len
= p_trun
->p_samples
[i
].i_size
;
4281 msg_Warn(p_demux
, "Zero duration sample in trun.");
4284 msg_Warn(p_demux
, "Zero length sample in trun.");
4286 block_t
*p_block
= vlc_stream_Block( p_demux
->s
, len
);
4287 uint32_t i_read
= ( p_block
) ? p_block
->i_buffer
: 0;
4288 p_track
->context
.i_trun_sample_pos
+= i_read
;
4289 if( i_read
< len
|| p_block
== NULL
)
4292 block_Release( p_block
);
4293 return VLC_DEMUXER_EOF
;
4297 msg_Dbg( p_demux
, "tk(%i)=%"PRId64
" mv=%"PRId64
" pos=%"PRIu64
, p_track
->i_track_ID
,
4298 VLC_TS_0
+ MP4_rescale( i_dts
, p_track
->i_timescale
, CLOCK_FREQ
),
4299 VLC_TS_0
+ MP4_rescale( i_pts
, p_track
->i_timescale
, CLOCK_FREQ
),
4300 p_track
->context
.i_trun_sample_pos
);
4302 if ( p_track
->p_es
)
4304 p_block
->i_dts
= VLC_TS_0
+ MP4_rescale( i_dts
, p_track
->i_timescale
, CLOCK_FREQ
);
4305 if( p_track
->fmt
.i_cat
== VIDEO_ES
&& !( p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
) )
4306 p_block
->i_pts
= VLC_TS_INVALID
;
4308 p_block
->i_pts
= VLC_TS_0
+ MP4_rescale( i_pts
, p_track
->i_timescale
, CLOCK_FREQ
);
4309 p_block
->i_length
= MP4_rescale( dur
, p_track
->i_timescale
, CLOCK_FREQ
);
4310 MP4_Block_Send( p_demux
, p_track
, p_block
);
4312 else block_Release( p_block
);
4315 if( p_track
->context
.i_trun_sample
== p_trun
->i_sample_count
)
4317 p_track
->context
.i_trun_sample
= 0;
4318 if( ++p_track
->context
.runs
.i_current
< p_track
->context
.runs
.i_count
)
4320 p_track
->i_time
= p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].i_first_dts
;
4321 p_track
->context
.i_trun_sample_pos
= p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].i_offset
;
4325 return VLC_DEMUXER_SUCCESS
;
4328 static int DemuxMoof( demux_t
*p_demux
)
4330 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4333 const unsigned i_max_preload
= ( p_sys
->b_fastseekable
) ? 0 : ( p_sys
->b_seekable
) ? DEMUX_TRACK_MAX_PRELOAD
: UINT_MAX
;
4334 const stime_t i_scaledincrement
= MP4_rescale( DEMUX_INCREMENT
, CLOCK_FREQ
, p_sys
->i_timescale
);
4336 const mtime_t i_nztime
= MP4_GetMoviePTS( p_sys
);
4338 /* !important! Ensure clock is set before sending data */
4339 if( p_sys
->i_pcr
== VLC_TS_INVALID
)
4340 es_out_SetPCR( p_demux
->out
, VLC_TS_0
+ i_nztime
);
4342 /* demux up to increment amount of data on every track, or just set pcr if empty data */
4345 mp4_track_t
*tk
= NULL
;
4346 i_status
= VLC_DEMUXER_EOS
;
4348 /* First pass, find any track within our target increment, ordered by position */
4349 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4351 mp4_track_t
*tk_tmp
= &p_sys
->track
[i
];
4353 if( !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
4354 (!tk_tmp
->b_selected
&& !p_sys
->b_seekable
) ||
4355 tk_tmp
->context
.runs
.i_current
>= tk_tmp
->context
.runs
.i_count
)
4358 /* At least still have data to demux on this or next turns */
4359 i_status
= VLC_DEMUXER_SUCCESS
;
4361 if( MP4_rescale( tk_tmp
->i_time
, tk_tmp
->i_timescale
, CLOCK_FREQ
) <= i_nztime
+ DEMUX_INCREMENT
)
4363 if( tk
== NULL
|| tk_tmp
->context
.i_trun_sample_pos
< tk
->context
.i_trun_sample_pos
)
4370 /* Second pass, refine and find any best candidate having a chunk pos closer than
4371 * current candidate (avoids seeks when increment falls between the 2) from
4372 * current position, but within extended interleave time */
4373 for( unsigned i
= 0; i_max_preload
> 0 && i
< p_sys
->i_tracks
; i
++ )
4375 mp4_track_t
*tk_tmp
= &p_sys
->track
[i
];
4377 !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
4378 (!tk_tmp
->b_selected
&& !p_sys
->b_seekable
) ||
4379 tk_tmp
->context
.runs
.i_current
>= tk_tmp
->context
.runs
.i_count
)
4382 mtime_t i_nzdts
= MP4_rescale( tk_tmp
->i_time
, tk_tmp
->i_timescale
, CLOCK_FREQ
);
4383 if ( i_nzdts
<= i_nztime
+ DEMUX_TRACK_MAX_PRELOAD
)
4385 /* Found a better candidate to avoid seeking */
4386 if( tk_tmp
->context
.i_trun_sample_pos
< tk
->context
.i_trun_sample_pos
)
4388 /* Note: previous candidate will be repicked on next loop */
4392 int i_ret
= FragDemuxTrack( p_demux
, tk
, i_max_preload
);
4394 if( i_ret
== VLC_DEMUXER_SUCCESS
)
4395 i_status
= VLC_DEMUXER_SUCCESS
;
4398 if( i_status
!= VLC_DEMUXER_SUCCESS
|| !tk
)
4402 if( i_status
!= VLC_DEMUXER_EOS
)
4404 p_sys
->i_time
+= __MAX(i_scaledincrement
, 1);
4405 p_sys
->i_pcr
= VLC_TS_0
+ MP4_rescale( p_sys
->i_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
4406 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
4410 stime_t i_segment_end
= INT64_MAX
;
4411 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4413 mp4_track_t
*tk
= &p_sys
->track
[i
];
4414 if( tk
->b_ok
|| tk
->b_chapters_source
||
4415 (!tk
->b_selected
&& !p_sys
->b_seekable
) )
4417 stime_t i_track_end
= MP4_rescale( tk
->i_time
, tk
->i_timescale
, p_sys
->i_timescale
);
4418 if( i_track_end
< i_segment_end
)
4419 i_segment_end
= i_track_end
;
4421 if( i_segment_end
!= INT64_MAX
)
4423 p_sys
->i_time
= i_segment_end
;
4424 p_sys
->i_pcr
= VLC_TS_0
+ MP4_rescale( p_sys
->i_time
, p_sys
->i_timescale
, CLOCK_FREQ
);
4425 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
4432 static int FragCreateTrunIndex( demux_t
*p_demux
, MP4_Box_t
*p_moof
,
4433 MP4_Box_t
*p_chunksidx
, stime_t i_moof_time
, bool b_discontinuity
)
4435 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4437 uint64_t i_traf_base_data_offset
= p_moof
->i_pos
;
4438 uint32_t i_traf
= 0;
4439 uint64_t i_prev_traf_end
= 0;
4441 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4443 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4444 if( p_track
->context
.runs
.p_array
)
4445 free( p_track
->context
.runs
.p_array
);
4446 p_track
->context
.runs
.p_array
= NULL
;
4447 p_track
->context
.runs
.i_count
= 0;
4448 p_track
->context
.runs
.i_current
= 0;
4451 for( MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
4452 p_traf
; p_traf
= p_traf
->p_next
)
4454 if ( p_traf
->i_type
!= ATOM_traf
)
4457 const MP4_Box_t
*p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" );
4458 const uint32_t i_trun_count
= MP4_BoxCount( p_traf
, "trun" );
4459 if ( !p_tfhd
|| !i_trun_count
)
4462 mp4_track_t
*p_track
= MP4_GetTrackByTrackID( p_demux
, BOXDATA(p_tfhd
)->i_track_ID
);
4466 p_track
->context
.runs
.p_array
= calloc(i_trun_count
, sizeof(mp4_run_t
));
4467 if(!p_track
->context
.runs
.p_array
)
4470 /* Get defaults for this/these RUN */
4471 uint32_t i_track_defaultsamplesize
= 0;
4472 uint32_t i_track_defaultsampleduration
= 0;
4473 MP4_GetDefaultSizeAndDuration( p_sys
->p_moov
, BOXDATA(p_tfhd
),
4474 &i_track_defaultsamplesize
,
4475 &i_track_defaultsampleduration
);
4476 p_track
->context
.i_default_sample_size
= i_track_defaultsamplesize
;
4477 p_track
->context
.i_default_sample_duration
= i_track_defaultsampleduration
;
4479 stime_t i_traf_start_time
= p_track
->i_time
;
4480 bool b_has_base_media_decode_time
= false;
4482 if( b_discontinuity
) /* We NEED start time offset for each track */
4484 /* Find start time */
4485 const MP4_Box_t
*p_tfdt
= MP4_BoxGet( p_traf
, "tfdt" );
4488 i_traf_start_time
= BOXDATA(p_tfdt
)->i_base_media_decode_time
;
4489 b_has_base_media_decode_time
= true;
4492 /* Try using Tfxd for base offset (Smooth) */
4493 if( !b_has_base_media_decode_time
&& p_sys
->i_tracks
== 1 )
4495 const MP4_Box_t
*p_uuid
= MP4_BoxGet( p_traf
, "uuid" );
4496 for( ; p_uuid
; p_uuid
= p_uuid
->p_next
)
4498 if( p_uuid
->i_type
== ATOM_uuid
&&
4499 !CmpUUID( &p_uuid
->i_uuid
, &TfxdBoxUUID
) && p_uuid
->data
.p_tfxd
)
4501 i_traf_start_time
= p_uuid
->data
.p_tfxd
->i_fragment_abs_time
;
4502 b_has_base_media_decode_time
= true;
4508 /* After seek we should have probed fragments */
4509 if( !b_has_base_media_decode_time
&& p_sys
->p_fragsindex
)
4511 unsigned i_track_index
= (p_track
- p_sys
->track
);
4512 assert(&p_sys
->track
[i_track_index
] == p_track
);
4513 i_traf_start_time
= MP4_Fragment_Index_GetTrackStartTime( p_sys
->p_fragsindex
,
4514 i_track_index
, p_moof
->i_pos
);
4515 i_traf_start_time
= MP4_rescale( i_traf_start_time
,
4516 p_sys
->i_timescale
, p_track
->i_timescale
);
4517 b_has_base_media_decode_time
= true;
4520 if( !b_has_base_media_decode_time
&& p_chunksidx
)
4522 /* Try using SIDX as base offset.
4523 * This can not work for global sidx but only when sent within each fragment (dash) */
4524 const MP4_Box_data_sidx_t
*p_data
= p_chunksidx
->data
.p_sidx
;
4525 if( p_data
&& p_data
->i_timescale
&& p_data
->i_reference_count
== 1 )
4527 i_traf_start_time
= MP4_rescale( p_data
->i_earliest_presentation_time
,
4528 p_data
->i_timescale
, p_track
->i_timescale
);
4529 b_has_base_media_decode_time
= true;
4533 /* First contiguous segment (moov->moof) and there's no tfdt not probed index (yet) */
4534 if( !b_has_base_media_decode_time
&& FragGetMoofSequenceNumber( p_moof
) == 1 )
4536 i_traf_start_time
= MP4_rescale( GetMoovTrackDuration( p_sys
, p_track
->i_track_ID
),
4537 p_sys
->i_timescale
, p_track
->i_timescale
);
4538 b_has_base_media_decode_time
= true;
4541 /* Use global sidx moof time, in case moof does not carry tfdt */
4542 if( !b_has_base_media_decode_time
&& i_moof_time
!= INT64_MAX
)
4543 i_traf_start_time
= MP4_rescale( i_moof_time
, p_sys
->i_timescale
, p_track
->i_timescale
);
4545 /* That should not happen */
4546 if( !b_has_base_media_decode_time
)
4547 i_traf_start_time
= MP4_rescale( p_sys
->i_time
, p_sys
->i_timescale
, p_track
->i_timescale
);
4550 /* Parse TRUN data */
4552 if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
)
4554 i_traf_base_data_offset
= BOXDATA(p_tfhd
)->i_base_data_offset
;
4556 else if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_DEFAULT_BASE_IS_MOOF
)
4558 i_traf_base_data_offset
= p_moof
->i_pos
/* + 8*/;
4563 i_traf_base_data_offset
= p_moof
->i_pos
/*+ 8*/;
4565 i_traf_base_data_offset
= i_prev_traf_end
;
4568 uint64_t i_trun_dts
= i_traf_start_time
;
4569 uint64_t i_trun_data_offset
= i_traf_base_data_offset
;
4570 uint32_t i_trun_size
= 0;
4572 for( const MP4_Box_t
*p_trun
= MP4_BoxGet( p_traf
, "trun" );
4573 p_trun
&& p_tfhd
; p_trun
= p_trun
->p_next
)
4575 if ( p_trun
->i_type
!= ATOM_trun
)
4578 const MP4_Box_data_trun_t
*p_trundata
= p_trun
->data
.p_trun
;
4580 /* Get data offset */
4581 if ( p_trundata
->i_flags
& MP4_TRUN_DATA_OFFSET
)
4583 /* Fix for broken Trun data offset relative to tfhd instead of moof, as seen in smooth */
4584 if( (BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
) == 0 &&
4586 i_traf_base_data_offset
+ p_trundata
->i_data_offset
< p_moof
->i_pos
+ p_moof
->i_size
+ 8 )
4588 i_trun_data_offset
+= p_moof
->i_size
+ 8;
4590 else if( (BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
) )
4592 i_trun_data_offset
= BOXDATA(p_tfhd
)->i_base_data_offset
+ p_trundata
->i_data_offset
;
4596 i_trun_data_offset
+= p_trundata
->i_data_offset
;
4601 i_trun_data_offset
+= i_trun_size
;
4607 "tk %u run %" PRIu32
" dflt dur %"PRIu32
" size %"PRIu32
" firstdts %"PRId64
" offset %"PRIu64
,
4608 p_track
->i_track_ID
,
4609 p_track
->context
.runs
.i_count
,
4610 i_track_defaultsampleduration
,
4611 i_track_defaultsamplesize
,
4612 MP4_rescale( i_trun_dts
, p_track
->i_timescale
, CLOCK_FREQ
), i_trun_data_offset
);
4615 mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_count
++];
4616 p_run
->i_first_dts
= i_trun_dts
;
4617 p_run
->i_offset
= i_trun_data_offset
;
4618 p_run
->p_trun
= p_trun
;
4621 /* Sum total time */
4622 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4624 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4625 i_trun_dts
+= p_trundata
->p_samples
[i
].i_duration
;
4629 i_trun_dts
+= p_trundata
->i_sample_count
*
4630 i_track_defaultsampleduration
;
4633 /* Get total traf size */
4634 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
4636 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4637 i_trun_size
+= p_trundata
->p_samples
[i
].i_size
;
4641 i_trun_size
+= p_trundata
->i_sample_count
*
4642 i_track_defaultsamplesize
;
4645 i_prev_traf_end
= i_trun_data_offset
+ i_trun_size
;
4654 static int FragGetMoofBySidxIndex( demux_t
*p_demux
, mtime_t i_target_time
,
4655 uint64_t *pi_moof_pos
, mtime_t
*pi_sampletime
)
4657 const MP4_Box_t
*p_sidx
= MP4_BoxGet( p_demux
->p_sys
->p_root
, "sidx" );
4658 const MP4_Box_data_sidx_t
*p_data
;
4659 if( !p_sidx
|| !((p_data
= BOXDATA(p_sidx
))) || !p_data
->i_timescale
)
4660 return VLC_EGENERIC
;
4662 i_target_time
= MP4_rescale( i_target_time
, CLOCK_FREQ
, p_data
->i_timescale
);
4664 /* sidx refers to offsets from end of sidx pos in the file + first offset */
4665 uint64_t i_pos
= p_data
->i_first_offset
+ p_sidx
->i_pos
+ p_sidx
->i_size
;
4667 for( uint16_t i
=0; i
<p_data
->i_reference_count
; i
++ )
4669 if( i_time
+ p_data
->p_items
[i
].i_subsegment_duration
> i_target_time
)
4671 *pi_sampletime
= MP4_rescale( i_time
, p_data
->i_timescale
, CLOCK_FREQ
);
4672 *pi_moof_pos
= i_pos
;
4675 i_pos
+= p_data
->p_items
[i
].i_referenced_size
;
4676 i_time
+= p_data
->p_items
[i
].i_subsegment_duration
;
4679 return VLC_EGENERIC
;
4682 static int FragGetMoofByTfraIndex( demux_t
*p_demux
, const mtime_t i_target_time
, unsigned i_track_ID
,
4683 uint64_t *pi_moof_pos
, mtime_t
*pi_sampletime
)
4685 MP4_Box_t
*p_tfra
= MP4_BoxGet( p_demux
->p_sys
->p_root
, "mfra/tfra" );
4686 for( ; p_tfra
; p_tfra
= p_tfra
->p_next
)
4688 if ( p_tfra
->i_type
== ATOM_tfra
)
4690 const MP4_Box_data_tfra_t
*p_data
= BOXDATA(p_tfra
);
4691 if( !p_data
|| p_data
->i_track_ID
!= i_track_ID
)
4695 mp4_track_t
*p_track
= MP4_GetTrackByTrackID( p_demux
, p_data
->i_track_ID
);
4698 stime_t i_track_target_time
= MP4_rescale( i_target_time
, CLOCK_FREQ
, p_track
->i_timescale
);
4699 for ( uint32_t i
= 0; i
<p_data
->i_number_of_entries
; i
+= ( p_data
->i_version
== 1 ) ? 2 : 1 )
4703 if ( p_data
->i_version
== 1 )
4705 i_time
= *((uint64_t *)(p_data
->p_time
+ i
));
4706 i_offset
= *((uint64_t *)(p_data
->p_moof_offset
+ i
));
4710 i_time
= p_data
->p_time
[i
];
4711 i_offset
= p_data
->p_moof_offset
[i
];
4714 if ( i_time
>= i_track_target_time
)
4716 if ( i_pos
== 0 ) /* Not in this traf */
4719 *pi_moof_pos
= i_pos
;
4720 *pi_sampletime
= MP4_rescale( i_time
, p_track
->i_timescale
, CLOCK_FREQ
);
4729 return VLC_EGENERIC
;
4732 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t
*p_moov
,
4733 const MP4_Box_data_tfhd_t
*p_tfhd_data
,
4734 uint32_t *pi_default_size
,
4735 uint32_t *pi_default_duration
)
4737 if( p_tfhd_data
->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
4738 *pi_default_duration
= p_tfhd_data
->i_default_sample_duration
;
4740 if( p_tfhd_data
->i_flags
& MP4_TFHD_DFLT_SAMPLE_SIZE
)
4741 *pi_default_size
= p_tfhd_data
->i_default_sample_size
;
4743 if( !*pi_default_duration
|| !*pi_default_size
)
4745 const MP4_Box_t
*p_trex
= MP4_GetTrexByTrackID( p_moov
, p_tfhd_data
->i_track_ID
);
4748 if ( !*pi_default_duration
)
4749 *pi_default_duration
= BOXDATA(p_trex
)->i_default_sample_duration
;
4750 if ( !*pi_default_size
)
4751 *pi_default_size
= BOXDATA(p_trex
)->i_default_sample_size
;
4756 static int DemuxFrag( demux_t
*p_demux
)
4758 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4759 unsigned i_track_selected
= 0;
4760 int i_status
= VLC_DEMUXER_SUCCESS
;
4762 if( unlikely(p_sys
->b_error
) )
4764 msg_Warn( p_demux
, "unrecoverable error" );
4765 i_status
= VLC_DEMUXER_EOF
;
4769 /* check for newly selected/unselected track */
4770 for( unsigned i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
4772 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
4775 if( !tk
->b_ok
|| tk
->b_chapters_source
)
4778 if( p_sys
->b_seekable
)
4779 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
, tk
->p_es
, &b
);
4781 if(tk
->b_selected
!= b
)
4783 msg_Dbg( p_demux
, "track %u %s!", tk
->i_track_ID
, b
? "enabled" : "disabled" );
4784 MP4_TrackSelect( p_demux
, tk
, b
);
4787 if( tk
->b_selected
)
4791 if( i_track_selected
<= 0 )
4793 msg_Warn( p_demux
, "no track selected, exiting..." );
4794 i_status
= VLC_DEMUXER_EOF
;
4798 if ( p_sys
->context
.i_current_box_type
!= ATOM_mdat
)
4800 /* Othewise mdat is skipped. FIXME: mdat reading ! */
4801 const uint8_t *p_peek
;
4802 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) != 8 )
4804 i_status
= VLC_DEMUXER_EOF
;
4808 p_sys
->context
.i_current_box_type
= VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] );
4809 if( p_sys
->context
.i_current_box_type
== ATOM_mdat
)
4811 p_sys
->context
.i_post_mdat_offset
= vlc_stream_Tell( p_demux
->s
) + GetDWBE( p_peek
);
4815 MP4_Box_t
*p_vroot
= MP4_BoxGetNextChunk( p_demux
->s
);
4818 i_status
= VLC_DEMUXER_EOF
;
4822 MP4_Box_t
*p_box
= NULL
;
4823 for( p_box
= p_vroot
->p_first
; p_box
; p_box
= p_box
->p_next
)
4825 if( p_box
->i_type
== ATOM_moof
||
4826 p_box
->i_type
== ATOM_moov
)
4832 FragResetContext( p_sys
);
4834 if( p_box
->i_type
== ATOM_moov
)
4836 p_sys
->context
.p_fragment_atom
= p_sys
->p_moov
;
4840 p_sys
->context
.p_fragment_atom
= MP4_BoxExtract( &p_vroot
->p_first
, p_box
->i_type
);
4842 /* Detect and Handle Passive Seek */
4843 const uint32_t i_sequence_number
= FragGetMoofSequenceNumber( p_sys
->context
.p_fragment_atom
);
4844 const bool b_discontinuity
= ( i_sequence_number
!= p_sys
->context
.i_lastseqnumber
+ 1 );
4845 if( b_discontinuity
)
4846 msg_Info( p_demux
, "Fragment sequence discontinuity detected %"PRIu32
" != %"PRIu32
,
4847 i_sequence_number
, p_sys
->context
.i_lastseqnumber
+ 1 );
4848 p_sys
->context
.i_lastseqnumber
= i_sequence_number
;
4851 if( FragPrepareChunk( p_demux
, p_sys
->context
.p_fragment_atom
,
4852 MP4_BoxGet( p_vroot
, "sidx"), INT64_MAX
,
4853 b_discontinuity
) != VLC_SUCCESS
)
4855 MP4_BoxFree( p_vroot
);
4856 i_status
= VLC_DEMUXER_EOF
;
4860 if( b_discontinuity
)
4862 p_sys
->i_time
= FragGetDemuxTimeFromTracksTime( p_sys
);
4863 p_sys
->i_pcr
= VLC_TS_INVALID
;
4865 /* !Prepare chunk */
4868 p_sys
->context
.i_current_box_type
= p_box
->i_type
;
4871 MP4_BoxFree( p_vroot
);
4873 if( p_sys
->context
.p_fragment_atom
== NULL
)
4875 msg_Info(p_demux
, "no moof or moov in current chunk");
4876 return VLC_DEMUXER_SUCCESS
;
4881 if ( p_sys
->context
.i_current_box_type
== ATOM_mdat
)
4883 assert(p_sys
->context
.p_fragment_atom
);
4885 if ( p_sys
->context
.p_fragment_atom
)
4886 switch( p_sys
->context
.p_fragment_atom
->i_type
)
4888 case ATOM_moov
://[ftyp/moov, mdat]+ -> [moof, mdat]+
4889 i_status
= DemuxMoov( p_demux
);
4892 i_status
= DemuxMoof( p_demux
);
4895 msg_Err( p_demux
, "fragment type %4.4s", (char*) &p_sys
->context
.p_fragment_atom
->i_type
);
4899 if( i_status
== VLC_DEMUXER_EOS
)
4901 i_status
= VLC_DEMUXER_SUCCESS
;
4902 /* Skip if we didn't reach the end of mdat box */
4903 uint64_t i_pos
= vlc_stream_Tell( p_demux
->s
);
4904 if( i_pos
!= p_sys
->context
.i_post_mdat_offset
&& i_status
!= VLC_DEMUXER_EOF
)
4906 if( i_pos
> p_sys
->context
.i_post_mdat_offset
)
4907 msg_Err( p_demux
, " Overread mdat by %" PRIu64
, i_pos
- p_sys
->context
.i_post_mdat_offset
);
4909 msg_Warn( p_demux
, "mdat had still %"PRIu64
" bytes unparsed as samples",
4910 p_sys
->context
.i_post_mdat_offset
- i_pos
);
4911 if( MP4_Seek( p_demux
->s
, p_sys
->context
.i_post_mdat_offset
) != VLC_SUCCESS
)
4912 i_status
= VLC_DEMUXER_EGENERIC
;
4914 p_sys
->context
.i_current_box_type
= 0;
4920 if( i_status
== VLC_DEMUXER_EOF
)
4922 mtime_t i_demux_end
= INT64_MIN
;
4923 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4925 const mp4_track_t
*tk
= &p_sys
->track
[i
];
4926 mtime_t i_track_end
= MP4_rescale( tk
->i_time
, tk
->i_timescale
, CLOCK_FREQ
);
4927 if( i_track_end
> i_demux_end
)
4928 i_demux_end
= i_track_end
;
4930 if( i_demux_end
!= INT64_MIN
)
4931 es_out_SetPCR( p_demux
->out
, VLC_TS_0
+ i_demux_end
);
4938 inline static mp4_track_t
*MP4ASF_GetTrack( asf_packet_sys_t
*p_packetsys
,
4939 uint8_t i_stream_number
)
4941 demux_sys_t
*p_sys
= p_packetsys
->p_demux
->p_sys
;
4942 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
4944 if ( p_sys
->track
[i
].p_asf
&&
4945 i_stream_number
== p_sys
->track
[i
].BOXDATA(p_asf
)->i_stream_number
)
4947 return &p_sys
->track
[i
];
4953 static asf_track_info_t
* MP4ASF_GetTrackInfo( asf_packet_sys_t
*p_packetsys
,
4954 uint8_t i_stream_number
)
4956 mp4_track_t
*p_track
= MP4ASF_GetTrack( p_packetsys
, i_stream_number
);
4958 return &p_track
->asfinfo
;
4963 static void MP4ASF_Send( asf_packet_sys_t
*p_packetsys
, uint8_t i_stream_number
,
4964 block_t
**pp_frame
)
4966 mp4_track_t
*p_track
= MP4ASF_GetTrack( p_packetsys
, i_stream_number
);
4969 block_Release( *pp_frame
);
4973 block_t
*p_gather
= block_ChainGather( *pp_frame
);
4974 p_gather
->i_dts
= p_track
->i_dts_backup
;
4975 p_gather
->i_pts
= p_track
->i_pts_backup
;
4976 es_out_Send( p_packetsys
->p_demux
->out
, p_track
->p_es
, p_gather
);
4982 static void MP4ASF_ResetFrames( demux_sys_t
*p_sys
)
4984 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
4986 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4987 if( p_track
->asfinfo
.p_frame
)
4989 block_ChainRelease( p_track
->asfinfo
.p_frame
);
4990 p_track
->asfinfo
.p_frame
= NULL
;