1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <vlc_demux.h>
32 #include <vlc_charset.h> /* EnsureUTF8 */
33 #include <vlc_input.h>
35 #include <vlc_plugin.h>
36 #include <vlc_dialog.h>
40 #include "../codec/cc.h"
42 #include "../av1_unpack.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t
* );
48 static void Close( vlc_object_t
* );
50 #define CFG_PREFIX "mp4-"
52 #define MP4_M4A_TEXT N_("M4A audio only")
53 #define MP4_M4A_LONGTEXT N_("Ignore non audio tracks from iTunes audio files")
55 #define HEIF_DURATION_TEXT N_("Duration in seconds")
56 #define HEIF_DURATION_LONGTEXT N_( \
57 "Duration in seconds before simulating an end of file. " \
58 "A negative value means an unlimited play time.")
61 set_category( CAT_INPUT
)
62 set_subcategory( SUBCAT_INPUT_DEMUX
)
63 set_description( N_("MP4 stream demuxer") )
64 set_shortname( N_("MP4") )
65 set_capability( "demux", 240 )
66 set_callbacks( Open
, Close
)
68 add_category_hint("Hacks", NULL
)
69 add_bool( CFG_PREFIX
"m4a-audioonly", false, MP4_M4A_TEXT
, MP4_M4A_LONGTEXT
, true )
72 set_category( CAT_INPUT
)
73 set_subcategory( SUBCAT_INPUT_DEMUX
)
74 set_description( N_("HEIF demuxer") )
75 set_shortname( "heif" )
76 set_capability( "demux", 239 )
77 set_callbacks( OpenHEIF
, CloseHEIF
)
78 set_section( N_("HEIF demuxer"), NULL
)
79 add_float( "heif-image-duration", HEIF_DEFAULT_DURATION
,
80 HEIF_DURATION_TEXT
, HEIF_DURATION_LONGTEXT
, false )
84 /*****************************************************************************
86 *****************************************************************************/
87 static int Demux ( demux_t
* );
88 static int DemuxRef( demux_t
*p_demux
){ (void)p_demux
; return 0;}
89 static int DemuxFrag( demux_t
* );
90 static int Control ( demux_t
*, int, va_list );
94 MP4_Box_t
*p_root
; /* container for the whole file */
98 uint64_t i_moov_duration
;
99 uint64_t i_duration
; /* Declared fragmented duration (movie time scale) */
100 uint64_t i_cumulated_duration
; /* Same as above, but not from probing, (movie time scale) */
101 uint32_t i_timescale
; /* movie time scale */
102 vlc_tick_t i_nztime
; /* time position of the presentation (CLOCK_FREQ timescale) */
103 unsigned int i_tracks
; /* number of tracks */
104 mp4_track_t
*track
; /* array of track */
105 float f_fps
; /* number of frame per seconds */
107 bool b_fragmented
; /* fMP4 */
110 bool b_error
; /* unrecoverable */
112 bool b_index_probed
; /* mFra sync points index */
113 bool b_fragments_probed
; /* moof segments index created */
119 uint32_t i_current_box_type
;
120 MP4_Box_t
*p_fragment_atom
;
121 uint64_t i_post_mdat_offset
;
122 uint32_t i_lastseqnumber
;
126 MP4_Box_t
*p_tref_chap
;
129 bool seekpoint_changed
;
131 input_title_t
*p_title
;
135 asf_packet_sys_t asfpacketsys
;
136 vlc_tick_t i_preroll
; /* foobar */
137 vlc_tick_t i_preroll_start
;
144 mp4_fragments_index_t
*p_fragsindex
;
147 #define DEMUX_INCREMENT VLC_TICK_FROM_MS(250) /* How far the pcr will go, each round */
148 #define DEMUX_TRACK_MAX_PRELOAD VLC_TICK_FROM_SEC(15) /* maximum preloading, to deal with interleaving */
150 #define INVALID_PRELOAD UINT_MAX
152 #define VLC_DEMUXER_EOS (VLC_DEMUXER_EGENERIC - 1)
153 #define VLC_DEMUXER_FATAL (VLC_DEMUXER_EGENERIC - 2)
155 const uint32_t rgi_pict_atoms
[2] = { ATOM_PICT
, ATOM_pict
};
156 const char *psz_meta_roots
[] = { "/moov/udta/meta/ilst",
164 /*****************************************************************************
165 * Declaration of local function
166 *****************************************************************************/
167 static void MP4_TrackSetup( demux_t
*, mp4_track_t
*, MP4_Box_t
*, bool, bool );
168 static void MP4_TrackInit( mp4_track_t
* );
169 static void MP4_TrackClean( es_out_t
*, mp4_track_t
* );
171 static void MP4_Block_Send( demux_t
*, mp4_track_t
*, block_t
* );
173 static void MP4_TrackSelect ( demux_t
*, mp4_track_t
*, bool );
174 static int MP4_TrackSeek ( demux_t
*, mp4_track_t
*, vlc_tick_t
);
176 static uint64_t MP4_TrackGetPos ( mp4_track_t
* );
177 static uint32_t MP4_TrackGetReadSize( mp4_track_t
*, uint32_t * );
178 static int MP4_TrackNextSample( demux_t
*, mp4_track_t
*, uint32_t );
179 static void MP4_TrackSetELST( demux_t
*, mp4_track_t
*, vlc_tick_t
);
181 static void MP4_UpdateSeekpoint( demux_t
*, vlc_tick_t
);
183 static MP4_Box_t
* MP4_GetTrexByTrackID( MP4_Box_t
*p_moov
, const uint32_t i_id
);
184 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t
*p_moov
,
185 const MP4_Box_data_tfhd_t
*p_tfhd_data
,
186 uint32_t *pi_default_size
,
187 uint32_t *pi_default_duration
);
189 static stime_t
GetMoovTrackDuration( demux_sys_t
*p_sys
, unsigned i_track_ID
);
191 static int ProbeFragments( demux_t
*p_demux
, bool b_force
, bool *pb_fragmented
);
192 static int ProbeIndex( demux_t
*p_demux
);
194 static int FragCreateTrunIndex( demux_t
*, MP4_Box_t
*, MP4_Box_t
*, stime_t
);
196 static int FragGetMoofBySidxIndex( demux_t
*p_demux
, vlc_tick_t i_target_time
,
197 uint64_t *pi_moof_pos
, vlc_tick_t
*pi_sampletime
);
198 static int FragGetMoofByTfraIndex( demux_t
*p_demux
, const vlc_tick_t i_target_time
, unsigned i_track_ID
,
199 uint64_t *pi_moof_pos
, vlc_tick_t
*pi_sampletime
);
200 static void FragResetContext( demux_sys_t
* );
203 static asf_track_info_t
* MP4ASF_GetTrackInfo( asf_packet_sys_t
*p_packetsys
, uint8_t i_stream_number
);
204 static void MP4ASF_Send(asf_packet_sys_t
*p_packetsys
, uint8_t i_stream_number
, block_t
**pp_frame
);
205 static void MP4ASF_ResetFrames( demux_sys_t
*p_sys
);
208 static block_t
* MP4_RTPHint_Convert( demux_t
*p_demux
, block_t
*p_block
, vlc_fourcc_t i_codec
);
209 static block_t
* MP4_RTPHintToFrame( demux_t
*p_demux
, block_t
*p_block
, uint32_t packetcount
);
211 static int MP4_LoadMeta( demux_sys_t
*p_sys
, vlc_meta_t
*p_meta
);
215 static int64_t MP4_rescale( int64_t i_value
, uint32_t i_timescale
, uint32_t i_newscale
)
217 if( i_timescale
== i_newscale
)
220 if( i_value
<= INT64_MAX
/ i_newscale
)
221 return i_value
* i_newscale
/ i_timescale
;
224 int64_t q
= i_value
/ i_timescale
;
225 int64_t r
= i_value
% i_timescale
;
226 return q
* i_newscale
+ r
* i_newscale
/ i_timescale
;
229 static vlc_tick_t
MP4_rescale_mtime( int64_t i_value
, uint32_t i_timescale
)
231 return MP4_rescale(i_value
, i_timescale
, CLOCK_FREQ
);
234 static int64_t MP4_rescale_qtime( vlc_tick_t i_value
, uint32_t i_timescale
)
236 return MP4_rescale(i_value
, CLOCK_FREQ
, i_timescale
);
239 static uint32_t stream_ReadU32( stream_t
*s
, void *p_read
, uint32_t i_toread
)
241 ssize_t i_return
= 0;
242 if ( i_toread
> INT32_MAX
)
244 i_return
= vlc_stream_Read( s
, p_read
, (size_t) INT32_MAX
);
245 if ( i_return
< INT32_MAX
)
248 i_toread
-= INT32_MAX
;
250 i_return
+= vlc_stream_Read( s
, (uint8_t *)p_read
+ i_return
, (size_t) i_toread
);
254 static MP4_Box_t
* MP4_GetTrexByTrackID( MP4_Box_t
*p_moov
, const uint32_t i_id
)
258 MP4_Box_t
*p_trex
= MP4_BoxGet( p_moov
, "mvex/trex" );
261 if ( p_trex
->i_type
== ATOM_trex
&&
262 BOXDATA(p_trex
) && BOXDATA(p_trex
)->i_track_ID
== i_id
)
265 p_trex
= p_trex
->p_next
;
271 * Return the track identified by tid
273 static mp4_track_t
*MP4_GetTrackByTrackID( demux_t
*p_demux
, const uint32_t tid
)
275 demux_sys_t
*p_sys
= p_demux
->p_sys
;
277 mp4_track_t
*ret
= NULL
;
278 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
280 ret
= &p_sys
->track
[i
];
281 if( ret
->i_track_ID
== tid
)
287 static MP4_Box_t
* MP4_GetTrakByTrackID( MP4_Box_t
*p_moov
, const uint32_t i_id
)
289 MP4_Box_t
*p_trak
= MP4_BoxGet( p_moov
, "trak" );
293 if( p_trak
->i_type
== ATOM_trak
&&
294 (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) && BOXDATA(p_tkhd
) &&
295 BOXDATA(p_tkhd
)->i_track_ID
== i_id
)
298 p_trak
= p_trak
->p_next
;
303 static MP4_Box_t
* MP4_GetTrafByTrackID( MP4_Box_t
*p_moof
, const uint32_t i_id
)
305 MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
309 if( p_traf
->i_type
== ATOM_traf
&&
310 (p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" )) && BOXDATA(p_tfhd
) &&
311 BOXDATA(p_tfhd
)->i_track_ID
== i_id
)
314 p_traf
= p_traf
->p_next
;
319 static es_out_id_t
* MP4_AddTrackES( es_out_t
*out
, mp4_track_t
*p_track
)
321 es_out_id_t
*p_es
= es_out_Add( out
, &p_track
->fmt
);
322 /* Force SPU which isn't selected/defaulted */
323 if( p_track
->fmt
.i_cat
== SPU_ES
&& p_es
&& p_track
->b_forced_spu
)
324 es_out_Control( out
, ES_OUT_SET_ES_DEFAULT
, p_es
);
329 /* Return time in microsecond of a track */
330 static inline vlc_tick_t
MP4_TrackGetDTS( demux_t
*p_demux
, mp4_track_t
*p_track
)
332 demux_sys_t
*p_sys
= p_demux
->p_sys
;
333 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[p_track
->i_chunk
];
335 unsigned int i_index
= 0;
336 unsigned int i_sample
= p_track
->i_sample
- p_chunk
->i_sample_first
;
337 int64_t sdts
= p_chunk
->i_first_dts
;
339 while( i_sample
> 0 && i_index
< p_chunk
->i_entries_dts
)
341 if( i_sample
> p_chunk
->p_sample_count_dts
[i_index
] )
343 sdts
+= p_chunk
->p_sample_count_dts
[i_index
] *
344 p_chunk
->p_sample_delta_dts
[i_index
];
345 i_sample
-= p_chunk
->p_sample_count_dts
[i_index
];
350 sdts
+= i_sample
* p_chunk
->p_sample_delta_dts
[i_index
];
355 vlc_tick_t i_dts
= MP4_rescale_mtime( sdts
, p_track
->i_timescale
);
357 /* now handle elst */
358 if( p_track
->p_elst
&& p_track
->BOXDATA(p_elst
)->i_entry_count
)
360 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
362 /* convert to offset */
363 if( ( elst
->i_media_rate_integer
[p_track
->i_elst
] > 0 ||
364 elst
->i_media_rate_fraction
[p_track
->i_elst
] > 0 ) &&
365 elst
->i_media_time
[p_track
->i_elst
] > 0 )
367 i_dts
-= MP4_rescale_mtime( elst
->i_media_time
[p_track
->i_elst
], p_track
->i_timescale
);
370 /* add i_elst_time */
371 i_dts
+= MP4_rescale_mtime( p_track
->i_elst_time
, p_sys
->i_timescale
);
373 if( i_dts
< 0 ) i_dts
= 0;
379 static inline bool MP4_TrackGetPTSDelta( demux_t
*p_demux
, mp4_track_t
*p_track
,
380 vlc_tick_t
*pi_delta
)
382 VLC_UNUSED( p_demux
);
383 mp4_chunk_t
*ck
= &p_track
->chunk
[p_track
->i_chunk
];
385 unsigned int i_index
= 0;
386 unsigned int i_sample
= p_track
->i_sample
- ck
->i_sample_first
;
388 if( ck
->p_sample_count_pts
== NULL
|| ck
->p_sample_offset_pts
== NULL
)
391 for( i_index
= 0; i_index
< ck
->i_entries_pts
; i_index
++ )
393 if( i_sample
< ck
->p_sample_count_pts
[i_index
] )
395 *pi_delta
= MP4_rescale_mtime( ck
->p_sample_offset_pts
[i_index
],
396 p_track
->i_timescale
);
400 i_sample
-= ck
->p_sample_count_pts
[i_index
];
405 static inline vlc_tick_t
MP4_GetSamplesDuration( demux_t
*p_demux
, mp4_track_t
*p_track
,
406 unsigned i_nb_samples
)
408 VLC_UNUSED( p_demux
);
410 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[p_track
->i_chunk
];
411 stime_t i_duration
= 0;
413 /* Forward to right index, and set remaining count in that index */
414 unsigned i_index
= 0;
415 unsigned i_remain
= 0;
416 for( unsigned i
= p_chunk
->i_sample_first
;
417 i
<p_track
->i_sample
&& i_index
< p_chunk
->i_entries_dts
; )
419 if( p_track
->i_sample
- i
>= p_chunk
->p_sample_count_dts
[i_index
] )
421 i
+= p_chunk
->p_sample_count_dts
[i_index
];
426 i_remain
= p_track
->i_sample
- i
;
431 /* Compute total duration from all samples from index */
432 while( i_nb_samples
> 0 && i_index
< p_chunk
->i_entries_dts
)
434 if( i_nb_samples
>= p_chunk
->p_sample_count_dts
[i_index
] - i_remain
)
436 i_duration
+= (p_chunk
->p_sample_count_dts
[i_index
] - i_remain
) *
437 (int64_t) p_chunk
->p_sample_delta_dts
[i_index
];
438 i_nb_samples
-= (p_chunk
->p_sample_count_dts
[i_index
] - i_remain
);
444 i_duration
+= i_nb_samples
* p_chunk
->p_sample_delta_dts
[i_index
];
449 return MP4_rescale_mtime( i_duration
, p_track
->i_timescale
);
452 static inline vlc_tick_t
MP4_GetMoviePTS(demux_sys_t
*p_sys
)
454 return p_sys
->i_nztime
;
457 static void LoadChapter( demux_t
*p_demux
);
459 static int LoadInitFrag( demux_t
*p_demux
)
461 demux_sys_t
*p_sys
= p_demux
->p_sys
;
463 /* Load all boxes ( except raw data ) */
464 if( ( p_sys
->p_root
= MP4_BoxGetRoot( p_demux
->s
) ) == NULL
)
466 goto LoadInitFragError
;
472 msg_Warn( p_demux
, "MP4 plugin discarded (not a valid initialization chunk)" );
476 static int CreateTracks( demux_t
*p_demux
, unsigned i_tracks
)
478 demux_sys_t
*p_sys
= p_demux
->p_sys
;
480 if( SIZE_MAX
/ i_tracks
< sizeof(mp4_track_t
) )
483 p_sys
->track
= vlc_alloc( i_tracks
, sizeof(mp4_track_t
) );
484 if( p_sys
->track
== NULL
)
486 p_sys
->i_tracks
= i_tracks
;
488 for( unsigned i
=0; i
<i_tracks
; i
++ )
489 MP4_TrackInit( &p_sys
->track
[i
] );
494 static block_t
* MP4_EIA608_Convert( block_t
* p_block
)
496 /* Rebuild codec data from encap */
498 size_t i_remaining
= __MIN(p_block
->i_buffer
, INT64_MAX
/ 3);
499 uint32_t i_bytes
= 0;
502 /* always need at least 10 bytes (atom size+header+1pair)*/
503 if ( i_remaining
< 10 ||
504 !(i_bytes
= GetDWBE(p_block
->p_buffer
)) ||
505 (i_bytes
> i_remaining
) ||
506 memcmp("cdat", &p_block
->p_buffer
[4], 4) ||
507 !(p_newblock
= block_Alloc( i_remaining
* 3 - 8 )) )
509 p_block
->i_buffer
= 0;
513 uint8_t *p_write
= p_newblock
->p_buffer
;
514 uint8_t *p_read
= &p_block
->p_buffer
[8];
520 p_write
[i_copied
++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
521 p_write
[i_copied
++] = p_read
[0];
522 p_write
[i_copied
++] = p_read
[1];
526 } while( i_bytes
>= 2 );
528 /* cdt2 is optional */
529 if ( i_remaining
>= 10 &&
530 (i_bytes
= GetDWBE(p_read
)) &&
531 (i_bytes
<= i_remaining
) &&
532 !memcmp("cdt2", &p_read
[4], 4) )
539 p_write
[i_copied
++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
540 p_write
[i_copied
++] = p_read
[0];
541 p_write
[i_copied
++] = p_read
[1];
544 } while( i_bytes
>= 2 );
547 p_newblock
->i_pts
= p_block
->i_dts
;
548 p_newblock
->i_buffer
= i_copied
;
549 p_newblock
->i_flags
= BLOCK_FLAG_TYPE_P
;
550 block_Release( p_block
);
555 static uint32_t MP4_TrackGetRunSeq( mp4_track_t
*p_track
)
557 if( p_track
->i_chunk_count
> 0 )
558 return p_track
->chunk
[p_track
->i_chunk
].i_virtual_run_number
;
562 /* Analyzes chunks to find max interleave length
563 * sets flat flag if no interleaving is in use */
564 static void MP4_GetInterleaving( demux_t
*p_demux
, vlc_tick_t
*pi_max_contiguous
, bool *pb_flat
)
566 demux_sys_t
*p_sys
= p_demux
->p_sys
;
567 *pi_max_contiguous
= 0;
570 /* Find first recorded chunk */
571 mp4_track_t
*tk
= NULL
;
572 uint64_t i_duration
= 0;
573 for( unsigned i
=0; i
< p_sys
->i_tracks
; i
++ )
575 mp4_track_t
*cur
= &p_sys
->track
[i
];
576 if( !cur
->i_chunk_count
)
579 if( tk
== NULL
|| cur
->chunk
[0].i_offset
< tk
->chunk
[0].i_offset
)
585 i_duration
+= tk
->chunk
[tk
->i_chunk
].i_duration
;
588 /* Find next chunk in data order */
589 mp4_track_t
*nexttk
= NULL
;
590 for( unsigned i
=0; i
< p_sys
->i_tracks
; i
++ )
592 mp4_track_t
*cur
= &p_sys
->track
[i
];
593 if( cur
->i_chunk
== cur
->i_chunk_count
)
596 if( nexttk
== NULL
||
597 cur
->chunk
[cur
->i_chunk
].i_offset
< nexttk
->chunk
[nexttk
->i_chunk
].i_offset
)
601 /* copy previous run */
602 if( nexttk
&& nexttk
->i_chunk
> 0 )
603 nexttk
->chunk
[nexttk
->i_chunk
].i_virtual_run_number
=
604 nexttk
->chunk
[nexttk
->i_chunk
- 1].i_virtual_run_number
;
608 vlc_tick_t i_dur
= MP4_rescale_mtime( i_duration
, tk
->i_timescale
);
609 if( i_dur
> *pi_max_contiguous
)
610 *pi_max_contiguous
= i_dur
;
613 if( tk
->i_chunk
!= tk
->i_chunk_count
)
616 if( nexttk
&& nexttk
->i_chunk
> 0 ) /* new run number */
617 nexttk
->chunk
[nexttk
->i_chunk
].i_virtual_run_number
++;
624 for( unsigned i
=0; i
< p_sys
->i_tracks
; i
++ )
625 p_sys
->track
[i
].i_chunk
= 0;
628 static block_t
* MP4_Block_Convert( demux_t
*p_demux
, const mp4_track_t
*p_track
, block_t
*p_block
)
630 /* might have some encap */
631 if( p_track
->fmt
.i_cat
== SPU_ES
)
633 switch( p_track
->fmt
.i_codec
)
635 case VLC_CODEC_WEBVTT
:
642 case VLC_CODEC_CEA608
:
643 p_block
= MP4_EIA608_Convert( p_block
);
646 p_block
->i_buffer
= 0;
650 else if( p_track
->fmt
.i_codec
== VLC_CODEC_AV1
)
652 p_block
= AV1_Unpack_Sample( p_block
);
654 else if( p_track
->fmt
.i_original_fourcc
== ATOM_rrtp
)
656 p_block
= MP4_RTPHint_Convert( p_demux
, p_block
, p_track
->fmt
.i_codec
);
662 static void MP4_Block_Send( demux_t
*p_demux
, mp4_track_t
*p_track
, block_t
*p_block
)
664 demux_sys_t
*p_sys
= p_demux
->p_sys
;
666 p_block
= MP4_Block_Convert( p_demux
, p_track
, p_block
);
667 if( p_block
== NULL
)
670 if ( p_track
->b_chans_reorder
)
672 aout_ChannelReorder( p_block
->p_buffer
, p_block
->i_buffer
,
673 p_track
->fmt
.audio
.i_channels
,
674 p_track
->rgi_chans_reordering
,
675 p_track
->fmt
.i_codec
);
678 p_block
->i_flags
|= p_track
->i_block_flags
;
679 if( p_track
->i_next_block_flags
)
681 p_block
->i_flags
|= p_track
->i_next_block_flags
;
682 p_track
->i_next_block_flags
= 0;
685 /* ASF packets in mov */
688 /* Fake a new stream from MP4 block */
689 stream_t
*p_stream
= p_demux
->s
;
690 p_demux
->s
= vlc_stream_MemoryNew( p_demux
, p_block
->p_buffer
, p_block
->i_buffer
, true );
693 p_track
->i_dts_backup
= p_block
->i_dts
;
694 p_track
->i_pts_backup
= p_block
->i_pts
;
695 /* And demux it as ASF packet */
696 DemuxASFPacket( &p_sys
->asfpacketsys
, p_block
->i_buffer
, p_block
->i_buffer
);
697 vlc_stream_Delete(p_demux
->s
);
699 block_Release(p_block
);
700 p_demux
->s
= p_stream
;
703 es_out_Send( p_demux
->out
, p_track
->p_es
, p_block
);
706 int OpenHEIF ( vlc_object_t
* );
707 void CloseHEIF( vlc_object_t
* );
709 /*****************************************************************************
710 * Open: check file and initializes MP4 structures
711 *****************************************************************************/
712 static int Open( vlc_object_t
* p_this
)
714 demux_t
*p_demux
= (demux_t
*)p_this
;
717 const uint8_t *p_peek
;
720 const MP4_Box_t
*p_mvhd
= NULL
;
721 const MP4_Box_t
*p_mvex
= NULL
;
725 /* A little test to see if it could be a mp4 */
726 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 12 ) < 12 ) return VLC_EGENERIC
;
728 switch( VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) )
739 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
743 /* Early handle some brands */
744 switch( VLC_FOURCC(p_peek
[8], p_peek
[9], p_peek
[10], p_peek
[11]) )
746 /* HEIF pictures goes to heif demux */
752 /* We don't yet support f4v, but avformat does. */
764 /* create our structure that will contains all data */
765 p_sys
= calloc( 1, sizeof( demux_sys_t
) );
770 vlc_stream_Control( p_demux
->s
, STREAM_CAN_SEEK
, &p_sys
->b_seekable
);
771 if( p_sys
->b_seekable
)
772 vlc_stream_Control( p_demux
->s
, STREAM_CAN_FASTSEEK
, &p_sys
->b_fastseekable
);
774 /*Set exported functions */
775 p_demux
->pf_demux
= Demux
;
776 p_demux
->pf_control
= Control
;
778 p_sys
->context
.i_lastseqnumber
= UINT32_MAX
;
780 p_demux
->p_sys
= p_sys
;
782 if( LoadInitFrag( p_demux
) != VLC_SUCCESS
)
785 MP4_BoxDumpStructure( p_demux
->s
, p_sys
->p_root
);
787 if( ( p_ftyp
= MP4_BoxGet( p_sys
->p_root
, "/ftyp" ) ) )
789 switch( BOXDATA(p_ftyp
)->i_major_brand
)
793 "ISO Media (isom) version %d.",
794 BOXDATA(p_ftyp
)->i_minor_version
);
800 msg_Dbg( p_demux
, "3GPP Media Release: %4.4s",
801 (char *)&BOXDATA(p_ftyp
)->i_major_brand
);
804 msg_Dbg( p_demux
, "Apple QuickTime media" );
807 msg_Dbg( p_demux
, "PIFF (= isml = fMP4) media" );
810 msg_Dbg( p_demux
, "DASH Stream" );
813 msg_Dbg( p_demux
, "iTunes audio" );
814 if( var_InheritBool( p_demux
, CFG_PREFIX
"m4a-audioonly" ) )
815 p_sys
->hacks
.es_cat_filters
= AUDIO_ES
;
819 "unrecognized major media specification (%4.4s).",
820 (char*)&BOXDATA(p_ftyp
)->i_major_brand
);
823 /* also lookup in compatibility list */
824 for(uint32_t i
=0; i
<BOXDATA(p_ftyp
)->i_compatible_brands_count
; i
++)
826 if (BOXDATA(p_ftyp
)->i_compatible_brands
[i
] == MAJOR_dash
)
828 msg_Dbg( p_demux
, "DASH Stream" );
830 else if (BOXDATA(p_ftyp
)->i_compatible_brands
[i
] == VLC_FOURCC('s', 'm', 'o', 'o') )
832 msg_Dbg( p_demux
, "Handling VLC Smooth Stream" );
838 msg_Dbg( p_demux
, "file type box missing (assuming ISO Media)" );
841 /* the file need to have one moov box */
842 p_sys
->p_moov
= MP4_BoxGet( p_sys
->p_root
, "/moov" );
843 if( unlikely(!p_sys
->p_moov
) )
845 p_sys
->p_moov
= MP4_BoxGet( p_sys
->p_root
, "/foov" );
848 msg_Err( p_demux
, "MP4 plugin discarded (no moov,foov,moof box)" );
851 /* we have a free box as a moov, rename it */
852 p_sys
->p_moov
->i_type
= ATOM_moov
;
855 p_mvhd
= MP4_BoxGet( p_sys
->p_moov
, "mvhd" );
856 if( p_mvhd
&& BOXDATA(p_mvhd
) && BOXDATA(p_mvhd
)->i_timescale
)
858 p_sys
->i_timescale
= BOXDATA(p_mvhd
)->i_timescale
;
859 p_sys
->i_moov_duration
= p_sys
->i_duration
= BOXDATA(p_mvhd
)->i_duration
;
860 p_sys
->i_cumulated_duration
= BOXDATA(p_mvhd
)->i_duration
;
864 msg_Warn( p_demux
, "No valid mvhd found" );
868 MP4_Box_t
*p_rmra
= MP4_BoxGet( p_sys
->p_root
, "/moov/rmra" );
869 if( p_rmra
!= NULL
&& p_demux
->p_input_item
!= NULL
)
871 int i_count
= MP4_BoxCount( p_rmra
, "rmda" );
874 msg_Dbg( p_demux
, "detected playlist mov file (%d ref)", i_count
);
876 input_item_t
*p_current
= p_demux
->p_input_item
;
878 input_item_node_t
*p_subitems
= input_item_node_Create( p_current
);
880 for( i
= 0; i
< i_count
; i
++ )
882 MP4_Box_t
*p_rdrf
= MP4_BoxGet( p_rmra
, "rmda[%d]/rdrf", i
);
886 if( !p_rdrf
|| !BOXDATA(p_rdrf
) || !( psz_ref
= strdup( BOXDATA(p_rdrf
)->psz_ref
) ) )
890 i_ref_type
= BOXDATA(p_rdrf
)->i_ref_type
;
892 msg_Dbg( p_demux
, "new ref=`%s' type=%4.4s",
893 psz_ref
, (char*)&i_ref_type
);
895 if( i_ref_type
== VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
897 if( strstr( psz_ref
, "qt5gateQT" ) )
899 msg_Dbg( p_demux
, "ignoring pseudo ref =`%s'", psz_ref
);
903 if( !strncmp( psz_ref
, "http://", 7 ) ||
904 !strncmp( psz_ref
, "rtsp://", 7 ) )
910 char *psz_absolute
= vlc_uri_resolve( p_demux
->psz_url
,
913 if( psz_absolute
== NULL
)
915 input_item_node_Delete( p_subitems
);
918 psz_ref
= psz_absolute
;
920 msg_Dbg( p_demux
, "adding ref = `%s'", psz_ref
);
921 input_item_t
*p_item
= input_item_New( psz_ref
, NULL
);
922 input_item_CopyOptions( p_item
, p_current
);
923 input_item_node_AppendItem( p_subitems
, p_item
);
924 input_item_Release( p_item
);
928 msg_Err( p_demux
, "unknown ref type=%4.4s FIXME (send a bug report)",
929 (char*)&BOXDATA(p_rdrf
)->i_ref_type
);
934 /* FIXME: create a stream_filter sub-module for this */
935 if (es_out_Control(p_demux
->out
, ES_OUT_POST_SUBNODE
, p_subitems
))
936 input_item_node_Delete(p_subitems
);
939 if( !(p_mvhd
= MP4_BoxGet( p_sys
->p_root
, "/moov/mvhd" ) ) )
943 msg_Err( p_demux
, "cannot find /moov/mvhd" );
948 msg_Warn( p_demux
, "cannot find /moov/mvhd (pure ref file)" );
949 p_demux
->pf_demux
= DemuxRef
;
955 p_sys
->i_timescale
= BOXDATA(p_mvhd
)->i_timescale
;
956 if( p_sys
->i_timescale
== 0 )
958 msg_Err( p_this
, "bad timescale" );
963 const unsigned i_tracks
= MP4_BoxCount( p_sys
->p_root
, "/moov/trak" );
966 msg_Err( p_demux
, "cannot find any /moov/trak" );
969 msg_Dbg( p_demux
, "found %u track%c", i_tracks
, i_tracks
? 's':' ' );
971 if( CreateTracks( p_demux
, i_tracks
) != VLC_SUCCESS
)
974 /* Search the first chap reference (like quicktime) and
975 * check that at least 1 stream is enabled */
976 p_sys
->p_tref_chap
= NULL
;
977 b_enabled_es
= false;
978 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
980 MP4_Box_t
*p_trak
= MP4_BoxGet( p_sys
->p_root
, "/moov/trak[%d]", i
);
983 MP4_Box_t
*p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" );
984 if( p_tkhd
&& BOXDATA(p_tkhd
) && (BOXDATA(p_tkhd
)->i_flags
&MP4_TRACK_ENABLED
) )
987 MP4_Box_t
*p_chap
= MP4_BoxGet( p_trak
, "tref/chap", i
);
988 if( p_chap
&& p_chap
->data
.p_tref_generic
&&
989 p_chap
->data
.p_tref_generic
->i_entry_count
> 0 && !p_sys
->p_tref_chap
)
990 p_sys
->p_tref_chap
= p_chap
;
993 /* Set and store metadata */
994 if( (p_sys
->p_meta
= vlc_meta_New()) )
995 MP4_LoadMeta( p_sys
, p_sys
->p_meta
);
997 /* now process each track and extract all useful information */
998 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
1000 MP4_Box_t
*p_trak
= MP4_BoxGet( p_sys
->p_root
, "/moov/trak[%u]", i
);
1001 MP4_TrackSetup( p_demux
, &p_sys
->track
[i
], p_trak
, true, !b_enabled_es
);
1003 if( p_sys
->track
[i
].b_ok
&& !p_sys
->track
[i
].b_chapters_source
)
1005 const char *psz_cat
;
1006 switch( p_sys
->track
[i
].fmt
.i_cat
)
1015 psz_cat
= "subtitle";
1019 psz_cat
= "unknown";
1023 msg_Dbg( p_demux
, "adding track[Id 0x%x] %s (%s) language %s",
1024 p_sys
->track
[i
].i_track_ID
, psz_cat
,
1025 p_sys
->track
[i
].b_enable
? "enable":"disable",
1026 p_sys
->track
[i
].fmt
.psz_language
?
1027 p_sys
->track
[i
].fmt
.psz_language
: "undef" );
1029 else if( p_sys
->track
[i
].b_ok
&& p_sys
->track
[i
].b_chapters_source
)
1031 msg_Dbg( p_demux
, "using track[Id 0x%x] for chapter language %s",
1032 p_sys
->track
[i
].i_track_ID
,
1033 p_sys
->track
[i
].fmt
.psz_language
?
1034 p_sys
->track
[i
].fmt
.psz_language
: "undef" );
1038 msg_Dbg( p_demux
, "ignoring track[Id 0x%x]",
1039 p_sys
->track
[i
].i_track_ID
);
1043 p_mvex
= MP4_BoxGet( p_sys
->p_moov
, "mvex" );
1044 if( p_mvex
!= NULL
)
1046 const MP4_Box_t
*p_mehd
= MP4_BoxGet( p_mvex
, "mehd");
1047 if ( p_mehd
&& BOXDATA(p_mehd
) )
1049 if( BOXDATA(p_mehd
)->i_fragment_duration
> p_sys
->i_duration
)
1051 p_sys
->b_fragmented
= true;
1052 p_sys
->i_duration
= BOXDATA(p_mehd
)->i_fragment_duration
;
1056 const MP4_Box_t
*p_sidx
= MP4_BoxGet( p_sys
->p_root
, "sidx");
1058 p_sys
->b_fragmented
= true;
1060 if ( p_sys
->b_seekable
)
1062 if( !p_sys
->b_fragmented
/* as unknown */ )
1064 /* Probe remaining to check if there's really fragments
1065 or if that file is just ready to append fragments */
1066 ProbeFragments( p_demux
, (p_sys
->i_duration
== 0), &p_sys
->b_fragmented
);
1069 if( vlc_stream_Seek( p_demux
->s
, p_sys
->p_moov
->i_pos
) != VLC_SUCCESS
)
1072 else /* Handle as fragmented by default as we can't see moof */
1074 p_sys
->context
.p_fragment_atom
= p_sys
->p_moov
;
1075 p_sys
->context
.i_current_box_type
= ATOM_moov
;
1076 p_sys
->b_fragmented
= true;
1080 if( p_sys
->b_fragmented
)
1082 p_demux
->pf_demux
= DemuxFrag
;
1083 msg_Dbg( p_demux
, "Set Fragmented demux mode" );
1086 if( !p_sys
->b_seekable
&& p_demux
->pf_demux
== Demux
)
1088 msg_Warn( p_demux
, "MP4 plugin discarded (not seekable)" );
1092 if( p_sys
->i_tracks
> 1 && !p_sys
->b_fastseekable
)
1094 vlc_tick_t i_max_continuity
;
1096 MP4_GetInterleaving( p_demux
, &i_max_continuity
, &b_flat
);
1098 msg_Warn( p_demux
, "that media doesn't look interleaved, will need to seek");
1099 else if( i_max_continuity
> DEMUX_TRACK_MAX_PRELOAD
)
1100 msg_Warn( p_demux
, "that media doesn't look properly interleaved, will need to seek");
1104 LoadChapter( p_demux
);
1106 p_sys
->asfpacketsys
.p_demux
= p_demux
;
1107 p_sys
->asfpacketsys
.pi_preroll
= &p_sys
->i_preroll
;
1108 p_sys
->asfpacketsys
.pi_preroll_start
= &p_sys
->i_preroll_start
;
1109 p_sys
->asfpacketsys
.pf_doskip
= NULL
;
1110 p_sys
->asfpacketsys
.pf_send
= MP4ASF_Send
;
1111 p_sys
->asfpacketsys
.pf_gettrackinfo
= MP4ASF_GetTrackInfo
;
1112 p_sys
->asfpacketsys
.pf_updatetime
= NULL
;
1113 p_sys
->asfpacketsys
.pf_setaspectratio
= NULL
;
1118 if( vlc_stream_Tell( p_demux
->s
) > 0 )
1120 if( vlc_stream_Seek( p_demux
->s
, 0 ) != VLC_SUCCESS
)
1121 msg_Warn( p_demux
, "Can't reset stream position from probing" );
1126 return VLC_EGENERIC
;
1129 const unsigned int SAMPLEHEADERSIZE
= 4;
1130 const unsigned int RTPPACKETSIZE
= 12;
1131 const unsigned int CONSTRUCTORSIZE
= 16;
1133 /*******************************************************************************
1134 * MP4_RTPHintToFrame: converts RTP Reception Hint Track sample to H.264 frame
1135 *******************************************************************************/
1136 static block_t
* MP4_RTPHintToFrame( demux_t
*p_demux
, block_t
*p_block
, uint32_t packetcount
)
1138 uint8_t *p_slice
= p_block
->p_buffer
+ SAMPLEHEADERSIZE
;
1139 block_t
*p_newblock
= NULL
;
1140 size_t i_payload
= 0;
1142 if( p_block
->i_buffer
< SAMPLEHEADERSIZE
+ RTPPACKETSIZE
+ CONSTRUCTORSIZE
)
1144 msg_Err( p_demux
, "Sample not large enough for necessary structs");
1145 block_Release( p_block
);
1149 for( uint32_t i
= 0; i
< packetcount
; ++i
)
1151 if( (size_t)(p_slice
- p_block
->p_buffer
) + RTPPACKETSIZE
+ CONSTRUCTORSIZE
> p_block
->i_buffer
)
1154 /* skip RTP header in sample. Could be used to detect packet losses */
1155 p_slice
+= RTPPACKETSIZE
;
1157 mp4_rtpsampleconstructor_t sample_cons
;
1159 sample_cons
.type
= p_slice
[0];
1160 sample_cons
.trackrefindex
= p_slice
[1];
1161 sample_cons
.length
= GetWBE( &p_slice
[2] );
1162 sample_cons
.samplenumber
= GetDWBE( &p_slice
[4] );
1163 sample_cons
.sampleoffset
= GetDWBE( &p_slice
[8] );
1164 sample_cons
.bytesperblock
= GetWBE( &p_slice
[12] );
1165 sample_cons
.samplesperblock
= GetWBE( &p_slice
[14] );
1167 /* skip packet constructor */
1168 p_slice
+= CONSTRUCTORSIZE
;
1170 /* check that is RTPsampleconstructor, referencing itself and no weird audio stuff */
1171 if( sample_cons
.type
!= 2||sample_cons
.trackrefindex
!= -1
1172 ||sample_cons
.samplesperblock
!= 1||sample_cons
.bytesperblock
!= 1 )
1174 msg_Err(p_demux
, "Unhandled constructor in RTP Reception Hint Track. Type:%u", sample_cons
.type
);
1178 /* slice doesn't fit in buffer */
1179 if( sample_cons
.sampleoffset
+ sample_cons
.length
> p_block
->i_buffer
)
1181 msg_Err(p_demux
, "Sample buffer is smaller than sample" );
1185 block_t
*p_realloc
= ( p_newblock
) ?
1186 block_Realloc( p_newblock
, 0, i_payload
+ sample_cons
.length
+ 4 ):
1187 block_Alloc( i_payload
+ sample_cons
.length
+ 4 );
1191 p_newblock
= p_realloc
;
1192 uint8_t *p_dst
= &p_newblock
->p_buffer
[i_payload
];
1194 const uint8_t* p_src
= p_block
->p_buffer
+ sample_cons
.sampleoffset
;
1195 uint8_t i_type
= (*p_src
) & ((1<<5)-1);
1197 const uint8_t synccode
[4] = { 0, 0, 0, 1 };
1198 if( memcmp( p_src
, synccode
, 4 ) )
1200 if( i_type
== 7 || i_type
== 8 )
1209 memcpy( p_dst
, p_src
, sample_cons
.length
);
1210 p_dst
+= sample_cons
.length
;
1212 i_payload
= p_dst
- p_newblock
->p_buffer
;
1215 block_Release( p_block
);
1217 p_newblock
->i_buffer
= i_payload
;
1221 block_Release( p_block
);
1223 block_Release( p_newblock
);
1227 /* RTP Reception Hint Track */
1228 static block_t
* MP4_RTPHint_Convert( demux_t
*p_demux
, block_t
*p_block
, vlc_fourcc_t i_codec
)
1230 block_t
*p_converted
= NULL
;
1231 if( p_block
->i_buffer
< 2 )
1233 block_Release( p_block
);
1237 /* number of RTP packets contained in this sample */
1238 const uint16_t i_packets
= GetWBE( p_block
->p_buffer
);
1239 if( i_packets
<= 1 || i_codec
!= VLC_CODEC_H264
)
1241 const size_t i_skip
= SAMPLEHEADERSIZE
+ i_packets
* ( RTPPACKETSIZE
+ CONSTRUCTORSIZE
);
1242 if( i_packets
== 1 && i_skip
< p_block
->i_buffer
)
1244 p_block
->p_buffer
+= i_skip
;
1245 p_converted
= p_block
;
1249 block_Release( p_block
);
1254 p_converted
= MP4_RTPHintToFrame( p_demux
, p_block
, i_packets
);
1260 /*****************************************************************************
1261 * Demux: read packet and send them to decoders
1262 *****************************************************************************
1263 * TODO check for newly selected track (ie audio upt to now )
1264 *****************************************************************************/
1265 static int DemuxTrack( demux_t
*p_demux
, mp4_track_t
*tk
, uint64_t i_readpos
,
1266 vlc_tick_t i_max_preload
)
1268 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1269 uint32_t i_nb_samples
= 0;
1270 uint32_t i_samplessize
= 0;
1272 if( !tk
->b_ok
|| tk
->i_sample
>= tk
->i_sample_count
)
1273 return VLC_DEMUXER_EOS
;
1275 if( tk
->b_chapters_source
)
1276 return VLC_DEMUXER_SUCCESS
;
1278 uint32_t i_run_seq
= MP4_TrackGetRunSeq( tk
);
1279 vlc_tick_t i_current_nzdts
= MP4_TrackGetDTS( p_demux
, tk
);
1280 const vlc_tick_t i_demux_max_nzdts
=i_max_preload
< INVALID_PRELOAD
1281 ? i_current_nzdts
+ i_max_preload
1284 for( ; i_demux_max_nzdts
>= i_current_nzdts
; )
1286 if( tk
->i_sample
>= tk
->i_sample_count
)
1287 return VLC_DEMUXER_EOS
;
1290 msg_Dbg( p_demux
, "tk(%i)=%"PRId64
" mv=%"PRId64
" pos=%"PRIu64
, tk
->i_track_ID
,
1291 MP4_TrackGetDTS( p_demux
, tk
),
1292 MP4_GetMoviePTS( p_demux
->p_sys
), i_readpos
);
1295 i_samplessize
= MP4_TrackGetReadSize( tk
, &i_nb_samples
);
1296 if( i_samplessize
> 0 )
1301 if( vlc_stream_Tell( p_demux
->s
) != i_readpos
)
1303 if( MP4_Seek( p_demux
->s
, i_readpos
) != VLC_SUCCESS
)
1305 msg_Warn( p_demux
, "track[0x%x] will be disabled (eof?)"
1306 ": Failed to seek to %"PRIu64
,
1307 tk
->i_track_ID
, i_readpos
);
1308 MP4_TrackSelect( p_demux
, tk
, false );
1314 if( !(p_block
= vlc_stream_Block( p_demux
->s
, i_samplessize
)) )
1316 msg_Warn( p_demux
, "track[0x%x] will be disabled (eof?)"
1317 ": Failed to read %d bytes sample at %"PRIu64
,
1318 tk
->i_track_ID
, i_samplessize
, i_readpos
);
1319 MP4_TrackSelect( p_demux
, tk
, false );
1323 /* !important! Ensure clock is set before sending data */
1324 if( p_sys
->i_pcr
== VLC_TICK_INVALID
)
1326 es_out_SetPCR( p_demux
->out
, VLC_TICK_0
+ i_current_nzdts
);
1327 p_sys
->i_pcr
= VLC_TICK_0
+ i_current_nzdts
;
1331 p_block
->i_dts
= VLC_TICK_0
+ i_current_nzdts
;
1333 if( MP4_TrackGetPTSDelta( p_demux
, tk
, &i_delta
) )
1334 p_block
->i_pts
= p_block
->i_dts
+ i_delta
;
1335 else if( tk
->fmt
.i_cat
!= VIDEO_ES
)
1336 p_block
->i_pts
= p_block
->i_dts
;
1338 p_block
->i_pts
= VLC_TICK_INVALID
;
1340 p_block
->i_length
= MP4_GetSamplesDuration( p_demux
, tk
, i_nb_samples
);
1342 MP4_Block_Send( p_demux
, tk
, p_block
);
1346 if ( i_nb_samples
) /* sample size could be 0, need to go fwd. see return */
1347 MP4_TrackNextSample( p_demux
, tk
, i_nb_samples
);
1349 uint32_t i_next_run_seq
= MP4_TrackGetRunSeq( tk
);
1350 if( i_next_run_seq
!= i_run_seq
)
1353 i_current_nzdts
= MP4_TrackGetDTS( p_demux
, tk
);
1354 i_readpos
= MP4_TrackGetPos( tk
);
1357 return VLC_DEMUXER_SUCCESS
;
1360 return VLC_DEMUXER_EGENERIC
;
1363 static int DemuxMoov( demux_t
*p_demux
)
1365 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1366 unsigned int i_track
;
1368 /* check for newly selected/unselected track */
1369 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1371 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1374 if( !tk
->b_ok
|| tk
->b_chapters_source
||
1375 ( tk
->b_selected
&& tk
->i_sample
>= tk
->i_sample_count
) )
1380 if( p_sys
->b_seekable
)
1381 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
, tk
->p_es
, &b
);
1383 if( tk
->b_selected
&& !b
)
1385 MP4_TrackSelect( p_demux
, tk
, false );
1387 else if( !tk
->b_selected
&& b
)
1389 MP4_TrackSeek( p_demux
, tk
, MP4_GetMoviePTS( p_sys
) );
1393 const vlc_tick_t i_nztime
= MP4_GetMoviePTS( p_sys
);
1395 /* We demux/set pcr, even without selected tracks, (empty edits, ...) */
1396 if( p_sys
->i_pcr
!= VLC_TICK_INVALID
/* not after a seek */ )
1399 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1401 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1402 if( !tk
->b_ok
|| tk
->b_chapters_source
|| tk
->i_sample
>= tk
->i_sample_count
)
1404 /* Test for EOF on each track (samples count, edit list) */
1405 b_eof
&= ( i_nztime
> MP4_TrackGetDTS( p_demux
, tk
) );
1408 return VLC_DEMUXER_EOS
;
1411 const vlc_tick_t i_max_preload
= ( p_sys
->b_fastseekable
) ? 0 : ( p_sys
->b_seekable
) ? DEMUX_TRACK_MAX_PRELOAD
: INVALID_PRELOAD
;
1413 /* demux up to increment amount of data on every track, or just set pcr if empty data */
1416 mp4_track_t
*tk
= NULL
;
1417 i_status
= VLC_DEMUXER_EOS
;
1419 /* First pass, find any track within our target increment, ordered by position */
1420 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1422 mp4_track_t
*tk_tmp
= &p_sys
->track
[i_track
];
1423 if( !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
1424 tk_tmp
->i_sample
>= tk_tmp
->i_sample_count
||
1425 (!tk_tmp
->b_selected
&& p_sys
->b_seekable
) )
1428 /* At least still have data to demux on this or next turns */
1429 i_status
= VLC_DEMUXER_SUCCESS
;
1431 if ( MP4_TrackGetDTS( p_demux
, tk_tmp
) <= i_nztime
+ DEMUX_INCREMENT
)
1433 if( tk
== NULL
|| MP4_TrackGetPos( tk_tmp
) < MP4_TrackGetPos( tk
) )
1440 /* Second pass, refine and find any best candidate having a chunk pos closer than
1441 * current candidate (avoids seeks when increment falls between the 2) from
1442 * current position, but within extended interleave time */
1443 for( i_track
= 0; i_max_preload
!= 0 && i_track
< p_sys
->i_tracks
; i_track
++ )
1445 mp4_track_t
*tk_tmp
= &p_sys
->track
[i_track
];
1447 !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
1448 (!tk_tmp
->b_selected
&& p_sys
->b_seekable
) ||
1449 tk_tmp
->i_sample
>= tk_tmp
->i_sample_count
)
1452 vlc_tick_t i_nzdts
= MP4_TrackGetDTS( p_demux
, tk_tmp
);
1453 if ( i_nzdts
<= i_nztime
+ DEMUX_TRACK_MAX_PRELOAD
)
1455 /* Found a better candidate to avoid seeking */
1456 if( MP4_TrackGetPos( tk_tmp
) < MP4_TrackGetPos( tk
) )
1458 /* Note: previous candidate will be repicked on next loop */
1462 uint64_t i_pos
= MP4_TrackGetPos( tk
);
1463 int i_ret
= DemuxTrack( p_demux
, tk
, i_pos
, i_max_preload
);
1465 if( i_ret
== VLC_DEMUXER_SUCCESS
)
1466 i_status
= VLC_DEMUXER_SUCCESS
;
1469 if( i_status
!= VLC_DEMUXER_SUCCESS
|| !tk
)
1473 p_sys
->i_nztime
+= DEMUX_INCREMENT
;
1474 if( p_sys
->i_pcr
!= VLC_TICK_INVALID
)
1476 p_sys
->i_pcr
= VLC_TICK_0
+ p_sys
->i_nztime
;
1477 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
1481 MP4_UpdateSeekpoint( p_demux
, i_nztime
+ DEMUX_INCREMENT
);
1486 static int Demux( demux_t
*p_demux
)
1488 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1490 assert( ! p_sys
->b_fragmented
);
1492 int i_status
= DemuxMoov( p_demux
);
1494 if( i_status
== VLC_DEMUXER_EOS
)
1495 i_status
= VLC_DEMUXER_EOF
;
1500 static void MP4_UpdateSeekpoint( demux_t
*p_demux
, vlc_tick_t i_time
)
1502 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1504 if( !p_sys
->p_title
)
1506 for( i
= 0; i
< p_sys
->p_title
->i_seekpoint
; i
++ )
1508 if( i_time
< p_sys
->p_title
->seekpoint
[i
]->i_time_offset
)
1513 if( i
!= p_sys
->i_seekpoint
&& i
>= 0 )
1515 p_sys
->i_seekpoint
= i
;
1516 p_sys
->seekpoint_changed
= true;
1519 /*****************************************************************************
1520 * Seek: Go to i_date
1521 ******************************************************************************/
1522 static int Seek( demux_t
*p_demux
, vlc_tick_t i_date
, bool b_accurate
)
1524 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1525 unsigned int i_track
;
1527 /* Now for each stream try to go to this time */
1528 vlc_tick_t i_start
= i_date
;
1529 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1531 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1532 /* FIXME: we should find the lowest time from tracks with indexes.
1533 considering only video for now */
1534 if( tk
->fmt
.i_cat
!= VIDEO_ES
)
1536 if( MP4_TrackSeek( p_demux
, tk
, i_date
) == VLC_SUCCESS
)
1538 vlc_tick_t i_seeked
= MP4_TrackGetDTS( p_demux
, tk
);
1539 if( i_seeked
< i_start
)
1544 msg_Dbg( p_demux
, "seeking with %"PRId64
"ms %s", MS_FROM_VLC_TICK(i_date
- i_start
),
1545 !b_accurate
? "alignment" : "preroll (use input-fast-seek to avoid)" );
1547 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
1549 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
1550 tk
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
1551 if( tk
->fmt
.i_cat
== VIDEO_ES
)
1553 MP4_TrackSeek( p_demux
, tk
, i_start
);
1556 MP4_UpdateSeekpoint( p_demux
, i_date
);
1557 MP4ASF_ResetFrames( p_sys
);
1558 /* update global time */
1559 p_sys
->i_nztime
= i_start
;
1560 p_sys
->i_pcr
= VLC_TICK_INVALID
;
1563 es_out_Control( p_demux
->out
, ES_OUT_SET_NEXT_DISPLAY_TIME
, i_date
);
1568 static int FragPrepareChunk( demux_t
*p_demux
, MP4_Box_t
*p_moof
,
1569 MP4_Box_t
*p_sidx
, stime_t i_moof_time
, bool b_discontinuity
)
1571 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1573 if( b_discontinuity
)
1575 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1576 p_sys
->track
[i
].context
.b_resync_time_offset
= true;
1579 if( FragCreateTrunIndex( p_demux
, p_moof
, p_sidx
, i_moof_time
) == VLC_SUCCESS
)
1581 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1583 mp4_track_t
*p_track
= &p_sys
->track
[i
];
1584 if( p_track
->context
.runs
.i_count
)
1586 const mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[0];
1587 p_track
->context
.i_trun_sample_pos
= p_run
->i_offset
;
1588 p_track
->context
.i_trun_sample
= 0;
1589 p_track
->i_time
= p_run
->i_first_dts
;
1595 return VLC_EGENERIC
;
1598 static vlc_tick_t
FragGetDemuxTimeFromTracksTime( demux_sys_t
*p_sys
)
1600 vlc_tick_t i_time
= INT64_MAX
;
1601 for( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
1603 if( p_sys
->track
[i
].context
.runs
.i_count
== 0 )
1605 vlc_tick_t i_ttime
= MP4_rescale_mtime( p_sys
->track
[i
].i_time
,
1606 p_sys
->track
[i
].i_timescale
);
1607 i_time
= __MIN( i_time
, i_ttime
);
1612 static uint32_t FragGetMoofSequenceNumber( MP4_Box_t
*p_moof
)
1614 const MP4_Box_t
*p_mfhd
= MP4_BoxGet( p_moof
, "mfhd" );
1615 if( p_mfhd
&& BOXDATA(p_mfhd
) )
1616 return BOXDATA(p_mfhd
)->i_sequence_number
;
1620 static int FragSeekLoadFragment( demux_t
*p_demux
, uint32_t i_moox
, stime_t i_moox_time
)
1622 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1625 if( i_moox
== ATOM_moov
)
1627 p_moox
= p_sys
->p_moov
;
1631 const uint8_t *p_peek
;
1632 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) != 8 )
1633 return VLC_EGENERIC
;
1635 if( ATOM_moof
!= VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) )
1636 return VLC_EGENERIC
;
1638 MP4_Box_t
*p_vroot
= MP4_BoxGetNextChunk( p_demux
->s
);
1640 return VLC_EGENERIC
;
1641 p_moox
= MP4_BoxExtract( &p_vroot
->p_first
, ATOM_moof
);
1642 MP4_BoxFree( p_vroot
);
1645 return VLC_EGENERIC
;
1648 FragResetContext( p_sys
);
1651 p_sys
->context
.p_fragment_atom
= p_moox
;
1652 p_sys
->context
.i_current_box_type
= i_moox
;
1654 if( i_moox
== ATOM_moof
)
1656 FragPrepareChunk( p_demux
, p_moox
, NULL
, i_moox_time
, true );
1657 p_sys
->context
.i_lastseqnumber
= FragGetMoofSequenceNumber( p_moox
);
1659 p_sys
->i_nztime
= FragGetDemuxTimeFromTracksTime( p_sys
);
1660 p_sys
->i_pcr
= VLC_TICK_INVALID
;
1663 msg_Dbg( p_demux
, "seeked to %4.4s at pos %" PRIu64
, (char *) &i_moox
, p_moox
->i_pos
);
1667 static unsigned GetSeekTrackIndex( demux_sys_t
*p_sys
)
1670 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1672 if( p_sys
->track
[i
].fmt
.i_cat
== VIDEO_ES
||
1673 p_sys
->track
[i
].fmt
.i_cat
== AUDIO_ES
)
1675 if( cand
!= i
&& !p_sys
->track
[cand
].b_selected
)
1682 static void FragTrunSeekToTime( mp4_track_t
*p_track
, stime_t i_target_time
)
1684 if( !p_track
->b_ok
|| p_track
->context
.runs
.i_count
< 1 )
1688 unsigned i_sample
= 0;
1689 uint64_t i_pos
= p_track
->context
.runs
.p_array
[0].i_offset
;
1690 stime_t i_time
= p_track
->context
.runs
.p_array
[0].i_first_dts
;
1692 for( unsigned r
= 0; r
< p_track
->context
.runs
.i_count
; r
++ )
1694 const mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[r
];
1695 const MP4_Box_data_trun_t
*p_data
=
1696 p_track
->context
.runs
.p_array
[r
].p_trun
->data
.p_trun
;
1697 if( i_time
> i_target_time
)
1701 i_time
= p_run
->i_first_dts
;
1702 i_pos
= p_run
->i_offset
;
1705 uint32_t dur
= p_track
->context
.i_default_sample_duration
;
1706 uint32_t len
= p_track
->context
.i_default_sample_size
;
1707 for ( unsigned i
=0; i
<p_data
->i_sample_count
; i
++ )
1709 if( p_data
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
1710 dur
= p_data
->p_samples
[i
].i_duration
;
1712 /* check condition */
1713 if( i_time
+ dur
> i_target_time
)
1716 if( p_data
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
1717 len
= p_data
->p_samples
[i
].i_size
;
1724 p_track
->context
.i_trun_sample
= i_sample
;
1725 p_track
->context
.i_trun_sample_pos
= i_pos
;
1726 p_track
->context
.runs
.i_current
= i_run
;
1729 #define INVALID_SEGMENT_TIME INT64_MAX
1731 static int FragSeekToTime( demux_t
*p_demux
, vlc_tick_t i_nztime
, bool b_accurate
)
1733 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1734 uint64_t i64
= UINT64_MAX
;
1735 uint32_t i_segment_type
= ATOM_moof
;
1736 stime_t i_segment_time
= INVALID_SEGMENT_TIME
;
1737 vlc_tick_t i_sync_time
= i_nztime
;
1738 bool b_iframesync
= false;
1740 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1741 if ( !p_sys
->i_timescale
|| !i_duration
|| !p_sys
->b_seekable
)
1742 return VLC_EGENERIC
;
1744 uint64_t i_backup_pos
= vlc_stream_Tell( p_demux
->s
);
1746 if ( !p_sys
->b_fragments_probed
&& !p_sys
->b_index_probed
&& p_sys
->b_seekable
)
1748 ProbeIndex( p_demux
);
1749 p_sys
->b_index_probed
= true;
1752 const unsigned i_seek_track_index
= GetSeekTrackIndex( p_sys
);
1753 const unsigned i_seek_track_ID
= p_sys
->track
[i_seek_track_index
].i_track_ID
;
1755 if( MP4_rescale_qtime( i_nztime
, p_sys
->i_timescale
)
1756 < GetMoovTrackDuration( p_sys
, i_seek_track_ID
) )
1758 i64
= p_sys
->p_moov
->i_pos
;
1759 i_segment_type
= ATOM_moov
;
1761 else if( FragGetMoofBySidxIndex( p_demux
, i_nztime
, &i64
, &i_sync_time
) == VLC_SUCCESS
)
1763 /* provides base offset */
1764 i_segment_time
= i_sync_time
;
1765 msg_Dbg( p_demux
, "seeking to sidx moof pos %" PRId64
" %" PRId64
, i64
, i_sync_time
);
1769 bool b_buildindex
= false;
1771 if( FragGetMoofByTfraIndex( p_demux
, i_nztime
, i_seek_track_ID
, &i64
, &i_sync_time
) == VLC_SUCCESS
)
1773 /* Does only provide segment position and a sync sample time */
1774 msg_Dbg( p_demux
, "seeking to sync point %" PRId64
, i_sync_time
);
1775 b_iframesync
= true;
1777 else if( !p_sys
->b_fragments_probed
&& !p_sys
->b_fastseekable
)
1779 const char *psz_msg
= _(
1780 "Because this file index is broken or missing, "
1781 "seeking will not work correctly.\n"
1782 "VLC won't repair your file but can temporary fix this "
1783 "problem by building an index in memory.\n"
1784 "This step might take a long time on a large file.\n"
1785 "What do you want to do?");
1786 b_buildindex
= vlc_dialog_wait_question( p_demux
,
1787 VLC_DIALOG_QUESTION_NORMAL
,
1791 _("Broken or missing Index"),
1795 if( !p_sys
->b_fragments_probed
&& ( p_sys
->b_fastseekable
|| b_buildindex
) )
1798 int i_ret
= vlc_stream_Seek( p_demux
->s
, p_sys
->p_moov
->i_pos
+ p_sys
->p_moov
->i_size
);
1799 if( i_ret
== VLC_SUCCESS
)
1801 i_ret
= ProbeFragments( p_demux
, true, &foo
);
1802 p_sys
->b_fragments_probed
= true;
1804 if( i_ret
!= VLC_SUCCESS
)
1806 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1811 if( p_sys
->b_fragments_probed
&& p_sys
->p_fragsindex
)
1813 stime_t i_basetime
= MP4_rescale_qtime( i_sync_time
, p_sys
->i_timescale
);
1814 if( !MP4_Fragments_Index_Lookup( p_sys
->p_fragsindex
, &i_basetime
, &i64
, i_seek_track_index
) )
1816 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1817 return VLC_EGENERIC
;
1819 msg_Dbg( p_demux
, "seeking to fragment index pos %" PRId64
" %" PRId64
, i64
,
1820 MP4_rescale_mtime( i_basetime
, p_sys
->i_timescale
) );
1824 if( i64
== UINT64_MAX
)
1826 msg_Warn( p_demux
, "seek by index failed" );
1827 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1828 return VLC_EGENERIC
;
1831 msg_Dbg( p_demux
, "final seek to fragment at %"PRId64
, i64
);
1832 if( vlc_stream_Seek( p_demux
->s
, i64
) )
1834 msg_Err( p_demux
, "seek failed to %"PRId64
, i64
);
1835 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1836 return VLC_EGENERIC
;
1839 /* Context is killed on success */
1840 if( FragSeekLoadFragment( p_demux
, i_segment_type
, i_segment_time
) != VLC_SUCCESS
)
1842 p_sys
->b_error
= (vlc_stream_Seek( p_demux
->s
, i_backup_pos
) != VLC_SUCCESS
);
1843 return VLC_EGENERIC
;
1846 p_sys
->i_pcr
= VLC_TICK_INVALID
;
1848 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
1850 if( i_segment_type
== ATOM_moov
)
1852 MP4_TrackSeek( p_demux
, &p_sys
->track
[i
], i_sync_time
);
1853 p_sys
->i_nztime
= i_sync_time
;
1854 p_sys
->i_pcr
= VLC_TICK_INVALID
;
1856 else if( b_iframesync
)
1858 stime_t i_tst
= MP4_rescale_qtime( i_sync_time
, p_sys
->track
[i
].i_timescale
);
1859 FragTrunSeekToTime( &p_sys
->track
[i
], i_tst
);
1860 p_sys
->track
[i
].i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
1864 MP4ASF_ResetFrames( p_sys
);
1865 /* And set next display time in that trun/fragment */
1866 if( b_iframesync
&& b_accurate
)
1867 es_out_Control( p_demux
->out
, ES_OUT_SET_NEXT_DISPLAY_TIME
, VLC_TICK_0
+ i_nztime
);
1871 static int FragSeekToPos( demux_t
*p_demux
, double f
, bool b_accurate
)
1873 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1874 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1876 if ( !p_sys
->b_seekable
|| !p_sys
->i_timescale
|| !i_duration
)
1877 return VLC_EGENERIC
;
1879 return FragSeekToTime( p_demux
, (vlc_tick_t
)( f
*
1880 MP4_rescale_mtime( i_duration
, p_sys
->i_timescale
) ), b_accurate
);
1883 static bool imageTypeCompatible( const MP4_Box_data_data_t
*p_data
)
1886 p_data
->e_wellknowntype
== DATA_WKT_PNG
||
1887 p_data
->e_wellknowntype
== DATA_WKT_JPEG
||
1888 p_data
->e_wellknowntype
== DATA_WKT_BMP
);
1891 static int MP4_LoadMeta( demux_sys_t
*p_sys
, vlc_meta_t
*p_meta
)
1893 MP4_Box_t
*p_data
= NULL
;
1894 MP4_Box_t
*p_udta
= NULL
;
1895 bool b_attachment_set
= false;
1898 return VLC_EGENERIC
;
1900 for( int i_index
= 0; psz_meta_roots
[i_index
] && !p_udta
; i_index
++ )
1902 p_udta
= MP4_BoxGet( p_sys
->p_root
, psz_meta_roots
[i_index
] );
1905 p_data
= MP4_BoxGet( p_udta
, "covr/data" );
1906 if ( p_data
&& imageTypeCompatible( BOXDATA(p_data
) ) )
1908 char *psz_attachment
;
1909 if ( -1 != asprintf( &psz_attachment
, "attachment://%s/covr/data[0]",
1910 psz_meta_roots
[i_index
] ) )
1912 vlc_meta_SetArtURL( p_meta
, psz_attachment
);
1913 b_attachment_set
= true;
1914 free( psz_attachment
);
1920 const MP4_Box_t
*p_pnot
;
1921 if ( !b_attachment_set
&& (p_pnot
= MP4_BoxGet( p_sys
->p_root
, "pnot" )) )
1923 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
) && !b_attachment_set
; i
++ )
1925 if ( rgi_pict_atoms
[i
] == BOXDATA(p_pnot
)->i_type
)
1928 snprintf( rgsz_path
, 26, "attachment://%4.4s[%"PRIu16
"]",
1929 (char*)&rgi_pict_atoms
[i
], BOXDATA(p_pnot
)->i_index
- 1 );
1930 vlc_meta_SetArtURL( p_meta
, rgsz_path
);
1931 b_attachment_set
= true;
1936 if( p_udta
== NULL
)
1938 if( !b_attachment_set
)
1939 return VLC_EGENERIC
;
1941 else SetupMeta( p_meta
, p_udta
);
1946 /*****************************************************************************
1948 *****************************************************************************/
1949 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
1951 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1957 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
1961 case DEMUX_CAN_SEEK
:
1962 *va_arg( args
, bool * ) = p_sys
->b_seekable
;
1965 case DEMUX_GET_POSITION
:
1966 pf
= va_arg( args
, double * );
1967 if( i_duration
> 0 )
1969 *pf
= (double)p_sys
->i_nztime
/
1970 MP4_rescale_mtime( i_duration
, p_sys
->i_timescale
);
1978 case DEMUX_GET_SEEKPOINT
:
1979 *va_arg( args
, int * ) = p_sys
->i_seekpoint
;
1982 case DEMUX_SET_POSITION
:
1983 f
= va_arg( args
, double );
1984 b
= va_arg( args
, int );
1985 if ( p_demux
->pf_demux
== DemuxFrag
)
1986 return FragSeekToPos( p_demux
, f
, b
);
1987 else if( p_sys
->i_timescale
> 0 )
1989 i64
= (vlc_tick_t
)( f
* MP4_rescale_mtime( p_sys
->i_duration
,
1990 p_sys
->i_timescale
) );
1991 return Seek( p_demux
, i64
, b
);
1993 else return VLC_EGENERIC
;
1995 case DEMUX_GET_TIME
:
1996 *va_arg( args
, vlc_tick_t
* ) = p_sys
->i_timescale
> 0 ? p_sys
->i_nztime
: 0;
1999 case DEMUX_SET_TIME
:
2000 i64
= va_arg( args
, vlc_tick_t
);
2001 b
= va_arg( args
, int );
2002 if ( p_demux
->pf_demux
== DemuxFrag
)
2003 return FragSeekToTime( p_demux
, i64
, b
);
2005 return Seek( p_demux
, i64
, b
);
2007 case DEMUX_GET_LENGTH
:
2008 if( p_sys
->i_timescale
> 0 )
2010 *va_arg( args
, vlc_tick_t
* ) = MP4_rescale_mtime( i_duration
,
2011 p_sys
->i_timescale
);
2013 else *va_arg( args
, vlc_tick_t
* ) = 0;
2017 pf
= va_arg( args
, double * );
2021 case DEMUX_GET_ATTACHMENTS
:
2023 input_attachment_t
***ppp_attach
= va_arg( args
, input_attachment_t
*** );
2024 int *pi_int
= va_arg( args
, int * );
2026 MP4_Box_t
*p_udta
= NULL
;
2030 /* Count number of total attachments */
2031 for( ; psz_meta_roots
[i_index
] && !p_udta
; i_index
++ )
2033 p_udta
= MP4_BoxGet( p_sys
->p_root
, psz_meta_roots
[i_index
] );
2035 i_count
+= MP4_BoxCount( p_udta
, "covr/data" );
2038 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
); i
++ )
2041 snprintf( rgsz_path
, 5, "%4.4s", (char*)&rgi_pict_atoms
[i
] );
2042 i_count
+= MP4_BoxCount( p_sys
->p_root
, rgsz_path
);
2046 return VLC_EGENERIC
;
2048 *ppp_attach
= (input_attachment_t
**)
2049 vlc_alloc( i_count
, sizeof(input_attachment_t
*) );
2050 if( !(*ppp_attach
) ) return VLC_ENOMEM
;
2052 /* First add cover attachments */
2054 size_t i_box_count
= 0;
2057 const MP4_Box_t
*p_data
= MP4_BoxGet( p_udta
, "covr/data" );
2058 for( ; p_data
; p_data
= p_data
->p_next
)
2064 if ( p_data
->i_type
!= ATOM_data
|| !imageTypeCompatible( BOXDATA(p_data
) ) )
2067 switch( BOXDATA(p_data
)->e_wellknowntype
)
2070 psz_mime
= strdup( "image/png" );
2073 psz_mime
= strdup( "image/jpeg" );
2076 psz_mime
= strdup( "image/bmp" );
2082 if ( asprintf( &psz_filename
, "%s/covr/data[%"PRIu64
"]", psz_meta_roots
[i_index
- 1],
2083 (uint64_t) i_box_count
- 1 ) >= 0 )
2085 (*ppp_attach
)[i_count
++] =
2086 vlc_input_attachment_New( psz_filename
, psz_mime
, "Cover picture",
2087 BOXDATA(p_data
)->p_blob
, BOXDATA(p_data
)->i_blob
);
2088 msg_Dbg( p_demux
, "adding attachment %s", psz_filename
);
2089 free( psz_filename
);
2096 /* Then quickdraw pict ones */
2097 for ( size_t i
=0; i
< ARRAY_SIZE(rgi_pict_atoms
); i
++ )
2100 snprintf( rgsz_path
, 5, "%4.4s", (char*)&rgi_pict_atoms
[i
] );
2101 const MP4_Box_t
*p_pict
= MP4_BoxGet( p_sys
->p_root
, rgsz_path
);
2103 for( ; p_pict
; p_pict
= p_pict
->p_next
)
2105 if ( i_box_count
++ == UINT16_MAX
) /* pnot only handles 2^16 */
2107 if ( p_pict
->i_type
!= rgi_pict_atoms
[i
] )
2109 char rgsz_location
[12];
2110 snprintf( rgsz_location
, 12, "%4.4s[%"PRIu16
"]", (char*)&rgi_pict_atoms
[i
],
2111 (uint16_t) i_box_count
- 1 );
2112 (*ppp_attach
)[i_count
] = vlc_input_attachment_New( rgsz_location
, "image/x-pict",
2113 "Quickdraw image", p_pict
->data
.p_binary
->p_blob
, p_pict
->data
.p_binary
->i_blob
);
2114 if ( !(*ppp_attach
)[i_count
] )
2120 msg_Dbg( p_demux
, "adding attachment %s", rgsz_location
);
2126 free( *ppp_attach
);
2127 return VLC_EGENERIC
;
2135 case DEMUX_GET_META
:
2137 vlc_meta_t
*p_meta
= va_arg( args
, vlc_meta_t
*);
2139 if( !p_sys
->p_meta
)
2140 return VLC_EGENERIC
;
2142 vlc_meta_Merge( p_meta
, p_sys
->p_meta
);
2147 case DEMUX_GET_TITLE_INFO
:
2149 input_title_t
***ppp_title
= va_arg( args
, input_title_t
*** );
2150 int *pi_int
= va_arg( args
, int* );
2151 int *pi_title_offset
= va_arg( args
, int* );
2152 int *pi_seekpoint_offset
= va_arg( args
, int* );
2154 if( !p_sys
->p_title
)
2155 return VLC_EGENERIC
;
2158 *ppp_title
= malloc( sizeof( input_title_t
*) );
2159 (*ppp_title
)[0] = vlc_input_title_Duplicate( p_sys
->p_title
);
2160 *pi_title_offset
= 0;
2161 *pi_seekpoint_offset
= 0;
2164 case DEMUX_SET_TITLE
:
2166 const int i_title
= va_arg( args
, int );
2167 if( !p_sys
->p_title
|| i_title
!= 0 )
2168 return VLC_EGENERIC
;
2171 case DEMUX_SET_SEEKPOINT
:
2173 const int i_seekpoint
= va_arg( args
, int );
2174 if( !p_sys
->p_title
)
2175 return VLC_EGENERIC
;
2176 return Seek( p_demux
, p_sys
->p_title
->seekpoint
[i_seekpoint
]->i_time_offset
, true );
2178 case DEMUX_TEST_AND_CLEAR_FLAGS
:
2180 unsigned *restrict flags
= va_arg( args
, unsigned * );
2182 if ((*flags
& INPUT_UPDATE_SEEKPOINT
) && p_sys
->seekpoint_changed
)
2184 *flags
= INPUT_UPDATE_SEEKPOINT
;
2185 p_sys
->seekpoint_changed
= false;
2192 case DEMUX_GET_PTS_DELAY
:
2194 for( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
2196 const MP4_Box_t
*p_load
;
2197 if ( (p_load
= MP4_BoxGet( p_sys
->track
[i
].p_track
, "load" )) &&
2198 BOXDATA(p_load
)->i_duration
> 0 )
2200 *va_arg(args
, vlc_tick_t
*) =
2201 MP4_rescale_mtime( BOXDATA(p_load
)->i_duration
,
2202 p_sys
->track
[i
].i_timescale
);
2206 return demux_vaControlHelper( p_demux
->s
, 0, -1, 0, 1, i_query
, args
);
2208 case DEMUX_SET_NEXT_DEMUX_TIME
:
2209 case DEMUX_SET_GROUP_DEFAULT
:
2210 case DEMUX_SET_GROUP_ALL
:
2211 case DEMUX_SET_GROUP_LIST
:
2212 case DEMUX_HAS_UNSUPPORTED_META
:
2213 case DEMUX_CAN_RECORD
:
2214 return VLC_EGENERIC
;
2216 case DEMUX_CAN_PAUSE
:
2217 case DEMUX_SET_PAUSE_STATE
:
2218 case DEMUX_CAN_CONTROL_PACE
:
2219 return demux_vaControlHelper( p_demux
->s
, 0, -1, 0, 1, i_query
, args
);
2222 return VLC_EGENERIC
;
2226 /*****************************************************************************
2227 * Close: frees unused data
2228 *****************************************************************************/
2229 static void Close ( vlc_object_t
* p_this
)
2231 demux_t
* p_demux
= (demux_t
*)p_this
;
2232 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2233 unsigned int i_track
;
2235 msg_Dbg( p_demux
, "freeing all memory" );
2237 FragResetContext( p_sys
);
2239 MP4_BoxFree( p_sys
->p_root
);
2241 if( p_sys
->p_title
)
2242 vlc_input_title_Delete( p_sys
->p_title
);
2245 vlc_meta_Delete( p_sys
->p_meta
);
2247 MP4_Fragments_Index_Delete( p_sys
->p_fragsindex
);
2249 for( i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
2250 MP4_TrackClean( p_demux
->out
, &p_sys
->track
[i_track
] );
2251 free( p_sys
->track
);
2258 /****************************************************************************
2259 * Local functions, specific to vlc
2260 ****************************************************************************/
2262 static void LoadChapterGpac( demux_t
*p_demux
, MP4_Box_t
*p_chpl
)
2264 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2266 if( BOXDATA(p_chpl
)->i_chapter
== 0 )
2269 p_sys
->p_title
= vlc_input_title_New();
2270 for( int i
= 0; i
< BOXDATA(p_chpl
)->i_chapter
&& p_sys
->p_title
; i
++ )
2272 seekpoint_t
*s
= vlc_seekpoint_New();
2273 if( s
== NULL
) continue;
2275 s
->psz_name
= strdup( BOXDATA(p_chpl
)->chapter
[i
].psz_name
);
2276 if( s
->psz_name
== NULL
)
2278 vlc_seekpoint_Delete( s
);;
2282 EnsureUTF8( s
->psz_name
);
2283 msftime_t offset
= BOXDATA(p_chpl
)->chapter
[i
].i_start
;
2284 s
->i_time_offset
= VLC_TICK_FROM_MSFTIME(offset
);
2285 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2288 static void LoadChapterGoPro( demux_t
*p_demux
, MP4_Box_t
*p_hmmt
)
2290 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2292 p_sys
->p_title
= vlc_input_title_New();
2293 if( p_sys
->p_title
)
2294 for( unsigned i
= 0; i
< BOXDATA(p_hmmt
)->i_chapter_count
; i
++ )
2296 seekpoint_t
*s
= vlc_seekpoint_New();
2299 if( asprintf( &s
->psz_name
, "HiLight tag #%u", i
+1 ) != -1 )
2300 EnsureUTF8( s
->psz_name
);
2302 /* HiLights are stored in ms so we convert them to µs */
2303 s
->i_time_offset
= VLC_TICK_FROM_MS( BOXDATA(p_hmmt
)->pi_chapter_start
[i
] );
2304 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2308 static void LoadChapterApple( demux_t
*p_demux
, mp4_track_t
*tk
)
2310 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2312 for( tk
->i_sample
= 0; tk
->i_sample
< tk
->i_sample_count
; tk
->i_sample
++ )
2314 const vlc_tick_t i_dts
= MP4_TrackGetDTS( p_demux
, tk
);
2315 vlc_tick_t i_pts_delta
;
2316 if ( !MP4_TrackGetPTSDelta( p_demux
, tk
, &i_pts_delta
) )
2318 uint32_t i_nb_samples
= 0;
2319 const uint32_t i_size
= MP4_TrackGetReadSize( tk
, &i_nb_samples
);
2321 if( i_size
> 0 && !vlc_stream_Seek( p_demux
->s
, MP4_TrackGetPos( tk
) ) )
2324 const uint32_t i_read
= stream_ReadU32( p_demux
->s
, p_buffer
,
2325 __MIN( sizeof(p_buffer
), i_size
) );
2328 const uint32_t i_string
= __MIN( GetWBE(p_buffer
), i_read
-2 );
2329 const char *psnz_string
= &p_buffer
[2];
2331 seekpoint_t
*s
= vlc_seekpoint_New();
2332 if( s
== NULL
) continue;
2334 if( i_string
> 1 && !memcmp( psnz_string
, "\xFF\xFE", 2 ) )
2335 s
->psz_name
= FromCharset( "UTF-16LE", psnz_string
, i_string
);
2337 s
->psz_name
= strndup( psnz_string
, i_string
);
2339 if( s
->psz_name
== NULL
)
2341 vlc_seekpoint_Delete( s
);
2345 EnsureUTF8( s
->psz_name
);
2346 s
->i_time_offset
= i_dts
+ __MAX( i_pts_delta
, 0 );
2348 if( !p_sys
->p_title
)
2349 p_sys
->p_title
= vlc_input_title_New();
2350 TAB_APPEND( p_sys
->p_title
->i_seekpoint
, p_sys
->p_title
->seekpoint
, s
);
2353 if( tk
->i_sample
+1 >= tk
->chunk
[tk
->i_chunk
].i_sample_first
+
2354 tk
->chunk
[tk
->i_chunk
].i_sample_count
)
2358 static void LoadChapter( demux_t
*p_demux
)
2360 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2364 if( ( p_chpl
= MP4_BoxGet( p_sys
->p_root
, "/moov/udta/chpl" ) ) &&
2365 BOXDATA(p_chpl
) && BOXDATA(p_chpl
)->i_chapter
> 0 )
2367 LoadChapterGpac( p_demux
, p_chpl
);
2369 else if( ( p_hmmt
= MP4_BoxGet( p_sys
->p_root
, "/moov/udta/HMMT" ) ) &&
2370 BOXDATA(p_hmmt
) && BOXDATA(p_hmmt
)->pi_chapter_start
&& BOXDATA(p_hmmt
)->i_chapter_count
> 0 )
2372 LoadChapterGoPro( p_demux
, p_hmmt
);
2374 else if( p_sys
->p_tref_chap
)
2376 MP4_Box_data_tref_generic_t
*p_chap
= p_sys
->p_tref_chap
->data
.p_tref_generic
;
2379 /* Load the first subtitle track like quicktime */
2380 for( i
= 0; i
< p_chap
->i_entry_count
; i
++ )
2382 for( j
= 0; j
< p_sys
->i_tracks
; j
++ )
2384 mp4_track_t
*tk
= &p_sys
->track
[j
];
2385 if( tk
->b_ok
&& tk
->i_track_ID
== p_chap
->i_track_ID
[i
] &&
2386 tk
->fmt
.i_cat
== SPU_ES
&& tk
->fmt
.i_codec
== VLC_CODEC_TX3G
)
2389 if( j
< p_sys
->i_tracks
)
2391 LoadChapterApple( p_demux
, &p_sys
->track
[j
] );
2397 /* Add duration if titles are enabled */
2398 if( p_sys
->p_title
)
2400 const uint64_t i_duration
= __MAX(p_sys
->i_duration
, p_sys
->i_cumulated_duration
);
2401 p_sys
->p_title
->i_length
=
2402 MP4_rescale_mtime( i_duration
, p_sys
->i_timescale
);
2406 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
2407 static int TrackCreateChunksIndex( demux_t
*p_demux
,
2408 mp4_track_t
*p_demux_track
)
2410 MP4_Box_t
*p_co64
; /* give offset for each chunk, same for stco and co64 */
2413 unsigned int i_chunk
;
2414 unsigned int i_index
, i_last
;
2416 if( ( !(p_co64
= MP4_BoxGet( p_demux_track
->p_stbl
, "stco" ) )&&
2417 !(p_co64
= MP4_BoxGet( p_demux_track
->p_stbl
, "co64" ) ) )||
2418 ( !(p_stsc
= MP4_BoxGet( p_demux_track
->p_stbl
, "stsc" ) ) ))
2420 return( VLC_EGENERIC
);
2423 p_demux_track
->i_chunk_count
= BOXDATA(p_co64
)->i_entry_count
;
2424 if( !p_demux_track
->i_chunk_count
)
2426 msg_Warn( p_demux
, "no chunk defined" );
2428 p_demux_track
->chunk
= calloc( p_demux_track
->i_chunk_count
,
2429 sizeof( mp4_chunk_t
) );
2430 if( p_demux_track
->chunk
== NULL
)
2435 /* first we read chunk offset */
2436 for( i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2438 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2440 ck
->i_offset
= BOXDATA(p_co64
)->i_chunk_offset
[i_chunk
];
2442 ck
->i_first_dts
= 0;
2443 ck
->i_entries_dts
= 0;
2444 ck
->p_sample_count_dts
= NULL
;
2445 ck
->p_sample_delta_dts
= NULL
;
2446 ck
->i_entries_pts
= 0;
2447 ck
->p_sample_count_pts
= NULL
;
2448 ck
->p_sample_offset_pts
= NULL
;
2451 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
2452 to be used for the sample XXX begin to 1
2453 We construct it begining at the end */
2454 i_last
= p_demux_track
->i_chunk_count
; /* last chunk proceded */
2455 i_index
= BOXDATA(p_stsc
)->i_entry_count
;
2457 while( i_index
-- > 0 )
2459 for( i_chunk
= BOXDATA(p_stsc
)->i_first_chunk
[i_index
] - 1;
2460 i_chunk
< i_last
; i_chunk
++ )
2462 if( i_chunk
>= p_demux_track
->i_chunk_count
)
2464 msg_Warn( p_demux
, "corrupted chunk table" );
2465 return VLC_EGENERIC
;
2468 p_demux_track
->chunk
[i_chunk
].i_sample_description_index
=
2469 BOXDATA(p_stsc
)->i_sample_description_index
[i_index
];
2470 p_demux_track
->chunk
[i_chunk
].i_sample_count
=
2471 BOXDATA(p_stsc
)->i_samples_per_chunk
[i_index
];
2473 i_last
= BOXDATA(p_stsc
)->i_first_chunk
[i_index
] - 1;
2476 p_demux_track
->i_sample_count
= 0;
2477 bool b_broken
= false;
2478 if ( p_demux_track
->i_chunk_count
)
2480 p_demux_track
->chunk
[0].i_sample_first
= 0;
2481 p_demux_track
->i_sample_count
+= p_demux_track
->chunk
[0].i_sample_count
;
2483 const mp4_chunk_t
*prev
= &p_demux_track
->chunk
[0];
2484 for( i_chunk
= 1; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2486 mp4_chunk_t
*cur
= &p_demux_track
->chunk
[i_chunk
];
2487 if( unlikely(UINT32_MAX
- cur
->i_sample_count
< p_demux_track
->i_sample_count
) )
2492 p_demux_track
->i_sample_count
+= cur
->i_sample_count
;
2493 cur
->i_sample_first
= prev
->i_sample_first
+ prev
->i_sample_count
;
2498 if( unlikely(b_broken
) )
2500 msg_Err( p_demux
, "Overflow in chunks total samples count" );
2501 return VLC_EGENERIC
;
2504 msg_Dbg( p_demux
, "track[Id 0x%x] read %d chunk",
2505 p_demux_track
->i_track_ID
, p_demux_track
->i_chunk_count
);
2510 static int xTTS_CountEntries( demux_t
*p_demux
, uint32_t *pi_entry
/* out */,
2511 const uint32_t i_index
,
2512 uint32_t i_index_samples_left
,
2513 uint32_t i_sample_count
,
2514 const uint32_t *pi_index_sample_count
,
2515 const uint32_t i_table_count
)
2517 uint32_t i_array_offset
;
2518 while( i_sample_count
> 0 )
2520 if ( likely((UINT32_MAX
- i_index
) >= *pi_entry
) )
2521 i_array_offset
= i_index
+ *pi_entry
;
2523 return VLC_EGENERIC
;
2525 if ( i_array_offset
>= i_table_count
)
2527 msg_Err( p_demux
, "invalid index counting total samples %u %u", i_array_offset
, i_table_count
);
2531 if ( i_index_samples_left
)
2533 if ( i_index_samples_left
> i_sample_count
)
2535 i_index_samples_left
-= i_sample_count
;
2537 *pi_entry
+=1; /* No samples left, go copy */
2542 i_sample_count
-= i_index_samples_left
;
2543 i_index_samples_left
= 0;
2550 i_sample_count
-= __MIN( i_sample_count
, pi_index_sample_count
[i_array_offset
] );
2558 static int TrackCreateSamplesIndex( demux_t
*p_demux
,
2559 mp4_track_t
*p_demux_track
)
2562 MP4_Box_data_stsz_t
*stsz
;
2563 /* TODO use also stss and stsh table for seeking */
2564 /* FIXME use edit table */
2567 * Gives the sample size for each samples. There is also a stz2 table
2568 * (compressed form) that we need to implement TODO */
2569 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "stsz" );
2572 /* FIXME and stz2 */
2573 msg_Warn( p_demux
, "cannot find STSZ box" );
2574 return VLC_EGENERIC
;
2576 stsz
= p_box
->data
.p_stsz
;
2578 /* Use stsz table to create a sample number -> sample size table */
2579 if( p_demux_track
->i_sample_count
!= stsz
->i_sample_count
)
2581 msg_Warn( p_demux
, "Incorrect total samples stsc %" PRIu32
" <> stsz %"PRIu32
", "
2582 " expect truncated media playback",
2583 p_demux_track
->i_sample_count
, stsz
->i_sample_count
);
2584 p_demux_track
->i_sample_count
= __MIN(p_demux_track
->i_sample_count
, stsz
->i_sample_count
);
2587 if( stsz
->i_sample_size
)
2589 /* 1: all sample have the same size, so no need to construct a table */
2590 p_demux_track
->i_sample_size
= stsz
->i_sample_size
;
2591 p_demux_track
->p_sample_size
= NULL
;
2595 /* 2: each sample can have a different size */
2596 p_demux_track
->i_sample_size
= 0;
2597 p_demux_track
->p_sample_size
=
2598 calloc( p_demux_track
->i_sample_count
, sizeof( uint32_t ) );
2599 if( p_demux_track
->p_sample_size
== NULL
)
2602 for( uint32_t i_sample
= 0; i_sample
< p_demux_track
->i_sample_count
; i_sample
++ )
2604 p_demux_track
->p_sample_size
[i_sample
] =
2605 stsz
->i_entry_size
[i_sample
];
2609 if ( p_demux_track
->i_chunk_count
&& p_demux_track
->i_sample_size
== 0 )
2611 const mp4_chunk_t
*lastchunk
= &p_demux_track
->chunk
[p_demux_track
->i_chunk_count
- 1];
2612 if( (uint64_t)lastchunk
->i_sample_count
+ p_demux_track
->i_chunk_count
- 1 > stsz
->i_sample_count
)
2614 msg_Err( p_demux
, "invalid samples table: stsz table is too small" );
2615 return VLC_EGENERIC
;
2619 /* Use stts table to create a sample number -> dts table.
2620 * XXX: if we don't want to waste too much memory, we can't expand
2621 * the box! so each chunk will contain an "extract" of this table
2622 * for fast research (problem with raw stream where a sample is sometime
2623 * just channels*bits_per_sample/8 */
2625 /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
2626 * differing in 2/2 fields and 1 signedness */
2628 int64_t i_next_dts
= 0;
2630 * Gives mapping between sample and decoding time
2632 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "stts" );
2635 msg_Warn( p_demux
, "cannot find STTS box" );
2636 return VLC_EGENERIC
;
2640 MP4_Box_data_stts_t
*stts
= p_box
->data
.p_stts
;
2642 msg_Warn( p_demux
, "STTS table of %"PRIu32
" entries", stts
->i_entry_count
);
2644 /* Create sample -> dts table per chunk */
2645 uint32_t i_index
= 0;
2646 uint32_t i_current_index_samples_left
= 0;
2648 for( uint32_t i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2650 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2651 uint32_t i_sample_count
;
2653 /* save first dts */
2654 ck
->i_first_dts
= i_next_dts
;
2656 /* count how many entries are needed for this chunk
2657 * for p_sample_delta_dts and p_sample_count_dts */
2658 ck
->i_entries_dts
= 0;
2660 int i_ret
= xTTS_CountEntries( p_demux
, &ck
->i_entries_dts
, i_index
,
2661 i_current_index_samples_left
,
2663 stts
->pi_sample_count
,
2664 stts
->i_entry_count
);
2665 if ( i_ret
== VLC_EGENERIC
)
2669 ck
->p_sample_count_dts
= calloc( ck
->i_entries_dts
, sizeof( uint32_t ) );
2670 ck
->p_sample_delta_dts
= calloc( ck
->i_entries_dts
, sizeof( uint32_t ) );
2671 if( !ck
->p_sample_count_dts
|| !ck
->p_sample_delta_dts
)
2673 free( ck
->p_sample_count_dts
);
2674 free( ck
->p_sample_delta_dts
);
2675 msg_Err( p_demux
, "can't allocate memory for i_entry=%"PRIu32
, ck
->i_entries_dts
);
2676 ck
->i_entries_dts
= 0;
2681 i_sample_count
= ck
->i_sample_count
;
2683 for( uint32_t i
= 0; i
< ck
->i_entries_dts
; i
++ )
2685 if ( i_current_index_samples_left
)
2687 if ( i_current_index_samples_left
> i_sample_count
)
2689 ck
->p_sample_count_dts
[i
] = i_sample_count
;
2690 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2691 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2692 if ( i_sample_count
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2693 i_current_index_samples_left
-= i_sample_count
;
2695 assert( i
== ck
->i_entries_dts
- 1 );
2700 ck
->p_sample_count_dts
[i
] = i_current_index_samples_left
;
2701 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2702 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2703 if ( i_current_index_samples_left
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2704 i_sample_count
-= i_current_index_samples_left
;
2705 i_current_index_samples_left
= 0;
2711 if ( stts
->pi_sample_count
[i_index
] > i_sample_count
)
2713 ck
->p_sample_count_dts
[i
] = i_sample_count
;
2714 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2715 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2716 if ( i_sample_count
) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2717 i_current_index_samples_left
= stts
->pi_sample_count
[i_index
] - i_sample_count
;
2719 assert( i
== ck
->i_entries_dts
- 1 );
2720 // keep building from same index
2724 ck
->p_sample_count_dts
[i
] = stts
->pi_sample_count
[i_index
];
2725 ck
->p_sample_delta_dts
[i
] = stts
->pi_sample_delta
[i_index
];
2726 i_next_dts
+= ck
->p_sample_count_dts
[i
] * stts
->pi_sample_delta
[i_index
];
2727 if ( stts
->pi_sample_count
[i_index
] ) ck
->i_duration
= i_next_dts
- ck
->i_first_dts
;
2728 i_sample_count
-= stts
->pi_sample_count
[i_index
];
2739 * Gives the delta between decoding time (dts) and composition table (pts)
2741 p_box
= MP4_BoxGet( p_demux_track
->p_stbl
, "ctts" );
2742 if( p_box
&& p_box
->data
.p_ctts
)
2744 MP4_Box_data_ctts_t
*ctts
= p_box
->data
.p_ctts
;
2746 msg_Warn( p_demux
, "CTTS table of %"PRIu32
" entries", ctts
->i_entry_count
);
2748 int64_t i_cts_shift
= 0;
2749 const MP4_Box_t
*p_cslg
= MP4_BoxGet( p_demux_track
->p_stbl
, "cslg" );
2750 if( p_cslg
&& BOXDATA(p_cslg
) )
2751 i_cts_shift
= BOXDATA(p_cslg
)->ct_to_dts_shift
;
2753 /* Create pts-dts table per chunk */
2754 uint32_t i_index
= 0;
2755 uint32_t i_current_index_samples_left
= 0;
2757 for( uint32_t i_chunk
= 0; i_chunk
< p_demux_track
->i_chunk_count
; i_chunk
++ )
2759 mp4_chunk_t
*ck
= &p_demux_track
->chunk
[i_chunk
];
2760 uint32_t i_sample_count
;
2762 /* count how many entries are needed for this chunk
2763 * for p_sample_offset_pts and p_sample_count_pts */
2764 ck
->i_entries_pts
= 0;
2765 int i_ret
= xTTS_CountEntries( p_demux
, &ck
->i_entries_pts
, i_index
,
2766 i_current_index_samples_left
,
2768 ctts
->pi_sample_count
,
2769 ctts
->i_entry_count
);
2770 if ( i_ret
== VLC_EGENERIC
)
2774 ck
->p_sample_count_pts
= calloc( ck
->i_entries_pts
, sizeof( uint32_t ) );
2775 ck
->p_sample_offset_pts
= calloc( ck
->i_entries_pts
, sizeof( int32_t ) );
2776 if( !ck
->p_sample_count_pts
|| !ck
->p_sample_offset_pts
)
2778 free( ck
->p_sample_count_pts
);
2779 free( ck
->p_sample_offset_pts
);
2780 msg_Err( p_demux
, "can't allocate memory for i_entry=%"PRIu32
, ck
->i_entries_pts
);
2781 ck
->i_entries_pts
= 0;
2786 i_sample_count
= ck
->i_sample_count
;
2788 for( uint32_t i
= 0; i
< ck
->i_entries_pts
; i
++ )
2790 if ( i_current_index_samples_left
)
2792 if ( i_current_index_samples_left
> i_sample_count
)
2794 ck
->p_sample_count_pts
[i
] = i_sample_count
;
2795 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2796 i_current_index_samples_left
-= i_sample_count
;
2798 assert( i
== ck
->i_entries_pts
- 1 );
2803 ck
->p_sample_count_pts
[i
] = i_current_index_samples_left
;
2804 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2805 i_sample_count
-= i_current_index_samples_left
;
2806 i_current_index_samples_left
= 0;
2812 if ( ctts
->pi_sample_count
[i_index
] > i_sample_count
)
2814 ck
->p_sample_count_pts
[i
] = i_sample_count
;
2815 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2816 i_current_index_samples_left
= ctts
->pi_sample_count
[i_index
] - i_sample_count
;
2818 assert( i
== ck
->i_entries_pts
- 1 );
2819 // keep building from same index
2823 ck
->p_sample_count_pts
[i
] = ctts
->pi_sample_count
[i_index
];
2824 ck
->p_sample_offset_pts
[i
] = ctts
->pi_sample_offset
[i_index
] + i_cts_shift
;
2825 i_sample_count
-= ctts
->pi_sample_count
[i_index
];
2835 msg_Dbg( p_demux
, "track[Id 0x%x] read %"PRIu32
" samples length:%"PRId64
"s",
2836 p_demux_track
->i_track_ID
, p_demux_track
->i_sample_count
,
2837 i_next_dts
/ p_demux_track
->i_timescale
);
2844 * It computes the sample rate for a video track using the given sample
2847 static void TrackGetESSampleRate( demux_t
*p_demux
,
2848 unsigned *pi_num
, unsigned *pi_den
,
2849 const mp4_track_t
*p_track
,
2850 unsigned i_sd_index
,
2853 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2857 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( MP4_BoxGet( p_sys
->p_root
,
2859 p_track
->i_track_ID
);
2860 MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_trak
, "mdia/mdhd" );
2861 if ( p_mdhd
&& BOXDATA(p_mdhd
) )
2863 vlc_ureduce( pi_num
, pi_den
,
2864 (uint64_t) BOXDATA(p_mdhd
)->i_timescale
* p_track
->i_sample_count
,
2865 (uint64_t) BOXDATA(p_mdhd
)->i_duration
,
2870 if( p_track
->i_chunk_count
== 0 )
2874 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[i_chunk
];
2875 while( p_chunk
> &p_track
->chunk
[0] &&
2876 p_chunk
[-1].i_sample_description_index
== i_sd_index
)
2881 uint64_t i_sample
= 0;
2882 uint64_t i_total_duration
= 0;
2885 i_sample
+= p_chunk
->i_sample_count
;
2886 i_total_duration
+= p_chunk
->i_duration
;
2889 while( p_chunk
< &p_track
->chunk
[p_track
->i_chunk_count
] &&
2890 p_chunk
->i_sample_description_index
== i_sd_index
);
2892 if( i_sample
> 0 && i_total_duration
)
2893 vlc_ureduce( pi_num
, pi_den
,
2894 i_sample
* p_track
->i_timescale
,
2901 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
2903 static int TrackCreateES( demux_t
*p_demux
, mp4_track_t
*p_track
,
2904 unsigned int i_chunk
, es_out_id_t
**pp_es
)
2906 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2907 unsigned int i_sample_description_index
;
2909 if( p_sys
->b_fragmented
|| p_track
->i_chunk_count
== 0 )
2910 i_sample_description_index
= 1; /* XXX */
2912 i_sample_description_index
=
2913 p_track
->chunk
[i_chunk
].i_sample_description_index
;
2918 if( !i_sample_description_index
)
2920 msg_Warn( p_demux
, "invalid SampleEntry index (track[Id 0x%x])",
2921 p_track
->i_track_ID
);
2922 return VLC_EGENERIC
;
2925 MP4_Box_t
*p_sample
= MP4_BoxGet( p_track
->p_stsd
, "[%d]",
2926 i_sample_description_index
- 1 );
2929 ( !p_sample
->data
.p_payload
&& p_track
->fmt
.i_cat
!= SPU_ES
) )
2931 msg_Warn( p_demux
, "cannot find SampleEntry (track[Id 0x%x])",
2932 p_track
->i_track_ID
);
2933 return VLC_EGENERIC
;
2936 p_track
->p_sample
= p_sample
;
2939 if( ( p_frma
= MP4_BoxGet( p_track
->p_sample
, "sinf/frma" ) ) && p_frma
->data
.p_frma
)
2941 msg_Warn( p_demux
, "Original Format Box: %4.4s", (char *)&p_frma
->data
.p_frma
->i_type
);
2943 p_sample
->i_type
= p_frma
->data
.p_frma
->i_type
;
2947 switch( p_track
->fmt
.i_cat
)
2950 if ( p_sample
->i_handler
!= ATOM_vide
||
2951 !SetupVideoES( p_demux
, p_track
, p_sample
) )
2952 return VLC_EGENERIC
;
2954 /* Set frame rate */
2955 TrackGetESSampleRate( p_demux
,
2956 &p_track
->fmt
.video
.i_frame_rate
,
2957 &p_track
->fmt
.video
.i_frame_rate_base
,
2958 p_track
, i_sample_description_index
, i_chunk
);
2960 p_sys
->f_fps
= (float)p_track
->fmt
.video
.i_frame_rate
/
2961 (float)p_track
->fmt
.video
.i_frame_rate_base
;
2966 if ( p_sample
->i_handler
!= ATOM_soun
||
2967 !SetupAudioES( p_demux
, p_track
, p_sample
) )
2968 return VLC_EGENERIC
;
2971 audio_replay_gain_t
*p_arg
= &p_track
->fmt
.audio_replay_gain
;
2972 const char *psz_meta
= vlc_meta_GetExtra( p_sys
->p_meta
, "replaygain_track_gain" );
2975 double f_gain
= us_atof( psz_meta
);
2976 p_arg
->pf_gain
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
;
2977 p_arg
->pb_gain
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
!= 0;
2979 psz_meta
= vlc_meta_GetExtra( p_sys
->p_meta
, "replaygain_track_peak" );
2982 double f_gain
= us_atof( psz_meta
);
2983 p_arg
->pf_peak
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
;
2984 p_arg
->pb_peak
[AUDIO_REPLAY_GAIN_TRACK
] = f_gain
> 0;
2990 if ( ( p_sample
->i_handler
!= ATOM_text
&&
2991 p_sample
->i_handler
!= ATOM_subt
&&
2992 p_sample
->i_handler
!= ATOM_sbtl
) ||
2993 !SetupSpuES( p_demux
, p_track
, p_sample
) )
2994 return VLC_EGENERIC
;
3002 *pp_es
= MP4_AddTrackES( p_demux
->out
, p_track
);
3004 return ( !pp_es
|| *pp_es
) ? VLC_SUCCESS
: VLC_EGENERIC
;
3007 /* *** Try to find nearest sync points *** */
3008 static int TrackGetNearestSeekPoint( demux_t
*p_demux
, mp4_track_t
*p_track
,
3009 uint32_t i_sample
, uint32_t *pi_sync_sample
)
3011 int i_ret
= VLC_EGENERIC
;
3012 *pi_sync_sample
= 0;
3014 const MP4_Box_t
*p_stss
;
3015 if( ( p_stss
= MP4_BoxGet( p_track
->p_stbl
, "stss" ) ) )
3017 const MP4_Box_data_stss_t
*p_stss_data
= BOXDATA(p_stss
);
3018 msg_Dbg( p_demux
, "track[Id 0x%x] using Sync Sample Box (stss)",
3019 p_track
->i_track_ID
);
3020 for( unsigned i_index
= 0; i_index
< p_stss_data
->i_entry_count
; i_index
++ )
3022 if( i_index
>= p_stss_data
->i_entry_count
- 1 ||
3023 i_sample
< p_stss_data
->i_sample_number
[i_index
+1] )
3025 *pi_sync_sample
= p_stss_data
->i_sample_number
[i_index
];
3026 msg_Dbg( p_demux
, "stss gives %d --> %" PRIu32
" (sample number)",
3027 i_sample
, *pi_sync_sample
);
3028 i_ret
= VLC_SUCCESS
;
3034 /* try rap samples groups */
3035 const MP4_Box_t
*p_sbgp
= MP4_BoxGet( p_track
->p_stbl
, "sbgp" );
3036 for( ; p_sbgp
; p_sbgp
= p_sbgp
->p_next
)
3038 const MP4_Box_data_sbgp_t
*p_sbgp_data
= BOXDATA(p_sbgp
);
3039 if( p_sbgp
->i_type
!= ATOM_sbgp
|| !p_sbgp_data
)
3042 if( p_sbgp_data
->i_grouping_type
== SAMPLEGROUP_rap
)
3044 uint32_t i_group_sample
= 0;
3045 for ( uint32_t i
=0; i
<p_sbgp_data
->i_entry_count
; i
++ )
3047 /* Sample belongs to rap group ? */
3048 if( p_sbgp_data
->entries
.pi_group_description_index
[i
] != 0 )
3050 if( i_sample
< i_group_sample
)
3052 msg_Dbg( p_demux
, "sbgp lookup failed %" PRIu32
" (sample number)",
3056 else if ( i_sample
>= i_group_sample
&&
3057 *pi_sync_sample
< i_group_sample
)
3059 *pi_sync_sample
= i_group_sample
;
3060 i_ret
= VLC_SUCCESS
;
3063 i_group_sample
+= p_sbgp_data
->entries
.pi_sample_count
[i
];
3066 if( i_ret
== VLC_SUCCESS
&& *pi_sync_sample
)
3068 msg_Dbg( p_demux
, "sbgp gives %d --> %" PRIu32
" (sample number)",
3069 i_sample
, *pi_sync_sample
);
3077 /* given a time it return sample/chunk
3078 * it also update elst field of the track
3080 static int TrackTimeToSampleChunk( demux_t
*p_demux
, mp4_track_t
*p_track
,
3081 vlc_tick_t start
, uint32_t *pi_chunk
,
3082 uint32_t *pi_sample
)
3084 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3086 unsigned int i_sample
;
3087 unsigned int i_chunk
;
3091 /* FIXME see if it's needed to check p_track->i_chunk_count */
3092 if( p_track
->i_chunk_count
== 0 )
3093 return( VLC_EGENERIC
);
3095 /* handle elst (find the correct one) */
3096 MP4_TrackSetELST( p_demux
, p_track
, start
);
3097 if( p_track
->p_elst
&& p_track
->BOXDATA(p_elst
)->i_entry_count
> 0 )
3099 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
3100 int64_t i_mvt
= MP4_rescale_qtime( start
, p_sys
->i_timescale
);
3102 /* now calculate i_start for this elst */
3104 if( start
< MP4_rescale_mtime( p_track
->i_elst_time
, p_sys
->i_timescale
) )
3111 /* to track time scale */
3112 i_start
= MP4_rescale_qtime( start
, p_track
->i_timescale
);
3113 /* add elst offset */
3114 if( ( elst
->i_media_rate_integer
[p_track
->i_elst
] > 0 ||
3115 elst
->i_media_rate_fraction
[p_track
->i_elst
] > 0 ) &&
3116 elst
->i_media_time
[p_track
->i_elst
] > 0 )
3118 i_start
+= elst
->i_media_time
[p_track
->i_elst
];
3121 msg_Dbg( p_demux
, "elst (%d) gives %"PRId64
"ms (movie)-> %"PRId64
3122 "ms (track)", p_track
->i_elst
,
3123 MP4_rescale( i_mvt
, p_sys
->i_timescale
, 1000 ),
3124 MP4_rescale( i_start
, p_track
->i_timescale
, 1000 ) );
3128 /* convert absolute time to in timescale unit */
3129 i_start
= MP4_rescale_qtime( start
, p_track
->i_timescale
);
3132 /* we start from sample 0/chunk 0, hope it won't take too much time */
3133 /* *** find good chunk *** */
3134 for( i_chunk
= 0; ; i_chunk
++ )
3136 if( i_chunk
+ 1 >= p_track
->i_chunk_count
)
3138 /* at the end and can't check if i_start in this chunk,
3139 it will be check while searching i_sample */
3140 i_chunk
= p_track
->i_chunk_count
- 1;
3144 if( (uint64_t)i_start
>= p_track
->chunk
[i_chunk
].i_first_dts
&&
3145 (uint64_t)i_start
< p_track
->chunk
[i_chunk
+ 1].i_first_dts
)
3151 /* *** find sample in the chunk *** */
3152 i_sample
= p_track
->chunk
[i_chunk
].i_sample_first
;
3153 i_dts
= p_track
->chunk
[i_chunk
].i_first_dts
;
3154 for( i_index
= 0; i_sample
< p_track
->chunk
[i_chunk
].i_sample_count
; )
3157 p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
] *
3158 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
] < (uint64_t)i_start
)
3161 p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
] *
3162 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
];
3164 i_sample
+= p_track
->chunk
[i_chunk
].p_sample_count_dts
[i_index
];
3169 if( p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
] <= 0 )
3173 i_sample
+= ( i_start
- i_dts
) /
3174 p_track
->chunk
[i_chunk
].p_sample_delta_dts
[i_index
];
3179 if( i_sample
>= p_track
->i_sample_count
)
3181 msg_Warn( p_demux
, "track[Id 0x%x] will be disabled "
3182 "(seeking too far) chunk=%d sample=%d",
3183 p_track
->i_track_ID
, i_chunk
, i_sample
);
3184 return( VLC_EGENERIC
);
3188 /* *** Try to find nearest sync points *** */
3189 uint32_t i_sync_sample
;
3191 TrackGetNearestSeekPoint( p_demux
, p_track
, i_sample
, &i_sync_sample
) )
3194 if( i_sync_sample
<= i_sample
)
3196 while( i_chunk
> 0 &&
3197 i_sync_sample
< p_track
->chunk
[i_chunk
].i_sample_first
)
3202 while( i_chunk
< p_track
->i_chunk_count
- 1 &&
3203 i_sync_sample
>= p_track
->chunk
[i_chunk
].i_sample_first
+
3204 p_track
->chunk
[i_chunk
].i_sample_count
)
3207 i_sample
= i_sync_sample
;
3210 *pi_chunk
= i_chunk
;
3211 *pi_sample
= i_sample
;
3216 static int TrackGotoChunkSample( demux_t
*p_demux
, mp4_track_t
*p_track
,
3217 unsigned int i_chunk
, unsigned int i_sample
)
3219 bool b_reselect
= false;
3221 /* now see if actual es is ok */
3222 if( p_track
->i_chunk
>= p_track
->i_chunk_count
||
3223 p_track
->chunk
[p_track
->i_chunk
].i_sample_description_index
!=
3224 p_track
->chunk
[i_chunk
].i_sample_description_index
)
3226 msg_Warn( p_demux
, "recreate ES for track[Id 0x%x]",
3227 p_track
->i_track_ID
);
3229 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
,
3230 p_track
->p_es
, &b_reselect
);
3232 es_out_Del( p_demux
->out
, p_track
->p_es
);
3234 p_track
->p_es
= NULL
;
3236 if( TrackCreateES( p_demux
, p_track
, i_chunk
, &p_track
->p_es
) )
3238 msg_Err( p_demux
, "cannot create es for track[Id 0x%x]",
3239 p_track
->i_track_ID
);
3241 p_track
->b_ok
= false;
3242 p_track
->b_selected
= false;
3243 return VLC_EGENERIC
;
3247 /* select again the new decoder */
3250 es_out_Control( p_demux
->out
, ES_OUT_SET_ES
, p_track
->p_es
);
3253 p_track
->i_chunk
= i_chunk
;
3254 p_track
->chunk
[i_chunk
].i_sample
= i_sample
- p_track
->chunk
[i_chunk
].i_sample_first
;
3255 p_track
->i_sample
= i_sample
;
3257 return p_track
->b_selected
? VLC_SUCCESS
: VLC_EGENERIC
;
3260 static void MP4_TrackRestart( demux_t
*p_demux
, mp4_track_t
*p_track
,
3261 MP4_Box_t
*p_params_box
)
3263 bool b_reselect
= false;
3266 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
,
3267 p_track
->p_es
, &b_reselect
);
3270 /* Save previous fragmented pos */
3271 uint32_t i_sample_pos_backup
= p_track
->i_sample
;
3272 vlc_tick_t time_backup
= p_track
->i_time
;
3273 uint32_t timescale_backup
= p_track
->i_timescale
;
3275 /* Save previous format and ES */
3276 es_format_t fmtbackup
;
3277 es_out_id_t
*p_es_backup
= p_track
->p_es
;
3278 p_track
->p_es
= NULL
;
3279 es_format_Copy( &fmtbackup
, &p_track
->fmt
);
3280 es_format_Clean( &p_track
->fmt
);
3283 /* do the cleanup and recycle track / restart */
3284 MP4_TrackDestroy( p_demux
, p_track
);
3285 memset( p_track
, 0, sizeof(*p_track
) );
3287 assert(p_params_box
->i_type
== ATOM_trak
);
3288 MP4_TrackCreate( p_demux
, p_track
, p_params_box
, false, true );
3292 if( !es_format_IsSimilar( &fmtbackup
, &p_track
->fmt
) ||
3293 fmtbackup
.i_extra
!= p_track
->fmt
.i_extra
||
3294 memcmp( fmtbackup
.p_extra
, p_track
->fmt
.p_extra
, fmtbackup
.i_extra
) )
3297 es_out_Del( p_demux
->out
, p_es_backup
);
3299 if( !p_track
->b_chapters_source
)
3301 p_track
->p_es
= MP4_AddTrackES( p_demux
->out
, p_track
);
3302 p_track
->b_ok
= !!p_track
->p_es
;
3307 p_track
->p_es
= p_es_backup
;
3310 else if( p_es_backup
)
3312 es_out_Del( p_demux
->out
, p_es_backup
);
3315 /* select again the new decoder */
3316 if( b_reselect
&& p_track
->p_es
)
3317 es_out_Control( p_demux
->out
, ES_OUT_SET_ES
, p_track
->p_es
);
3319 es_format_Clean( &fmtbackup
);
3321 /* Restore fragmented pos */
3322 p_track
->i_sample
= i_sample_pos_backup
;
3323 p_track
->i_time
= MP4_rescale( time_backup
, timescale_backup
, p_track
->i_timescale
);
3326 /****************************************************************************
3328 ****************************************************************************
3329 * Parse track information and create all needed data to run a track
3330 * If it succeed b_ok is set to 1 else to 0
3331 ****************************************************************************/
3332 static void MP4_TrackSetup( demux_t
*p_demux
, mp4_track_t
*p_track
,
3333 MP4_Box_t
*p_box_trak
,
3334 bool b_create_es
, bool b_force_enable
)
3336 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3338 p_track
->p_track
= p_box_trak
;
3340 char language
[4] = { '\0' };
3341 char sdp_media_type
[8] = { '\0' };
3343 const MP4_Box_t
*p_tkhd
= MP4_BoxGet( p_box_trak
, "tkhd" );
3349 /* do we launch this track by default ? */
3351 ( ( BOXDATA(p_tkhd
)->i_flags
&MP4_TRACK_ENABLED
) != 0 );
3353 p_track
->i_track_ID
= BOXDATA(p_tkhd
)->i_track_ID
;
3355 p_track
->i_width
= BOXDATA(p_tkhd
)->i_width
/ BLOCK16x16
;
3356 p_track
->i_height
= BOXDATA(p_tkhd
)->i_height
/ BLOCK16x16
;
3357 p_track
->f_rotation
= BOXDATA(p_tkhd
)->f_rotation
;
3359 /* FIXME: unhandled box: tref */
3361 const MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_box_trak
, "mdia/mdhd" );
3362 const MP4_Box_t
*p_hdlr
= MP4_BoxGet( p_box_trak
, "mdia/hdlr" );
3364 if( ( !p_mdhd
)||( !p_hdlr
) )
3369 if( BOXDATA(p_mdhd
)->i_timescale
== 0 )
3371 msg_Warn( p_demux
, "Invalid track timescale " );
3374 p_track
->i_timescale
= BOXDATA(p_mdhd
)->i_timescale
;
3376 memcpy( &language
, BOXDATA(p_mdhd
)->rgs_language
, 3 );
3377 p_track
->b_mac_encoding
= BOXDATA(p_mdhd
)->b_mac_encoding
;
3379 switch( p_hdlr
->data
.p_hdlr
->i_handler_type
)
3382 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/smhd" ) )
3386 es_format_Change( &p_track
->fmt
, AUDIO_ES
, 0 );
3389 case( ATOM_pict
): /* heif */
3390 es_format_Change( &p_track
->fmt
, VIDEO_ES
, 0 );
3394 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/vmhd") )
3398 es_format_Change( &p_track
->fmt
, VIDEO_ES
, 0 );
3402 /* RTP Reception Hint tracks */
3403 if( !MP4_BoxGet( p_box_trak
, "mdia/minf/hmhd" ) ||
3404 !MP4_BoxGet( p_box_trak
, "mdia/minf/stbl/stsd/rrtp" ) )
3410 /* parse the sdp message to find out whether the RTP stream contained audio or video */
3411 if( !( p_sdp
= MP4_BoxGet( p_box_trak
, "udta/hnti/sdp " ) ) )
3413 msg_Warn( p_demux
, "Didn't find sdp box to determine stream type" );
3417 memcpy( sdp_media_type
, BOXDATA(p_sdp
)->psz_text
, 7 );
3418 if( !strcmp(sdp_media_type
, "m=audio") )
3420 msg_Dbg( p_demux
, "Found audio Rtp: %s", sdp_media_type
);
3421 es_format_Change( &p_track
->fmt
, AUDIO_ES
, 0 );
3423 else if( !strcmp(sdp_media_type
, "m=video") )
3425 msg_Dbg( p_demux
, "Found video Rtp: %s", sdp_media_type
);
3426 es_format_Change( &p_track
->fmt
, VIDEO_ES
, 0 );
3430 msg_Warn( p_demux
, "Malformed track SDP message: %s", sdp_media_type
);
3433 p_track
->p_sdp
= p_sdp
;
3439 case( ATOM_subt
): /* ttml */
3441 case( ATOM_clcp
): /* closed captions */
3442 es_format_Change( &p_track
->fmt
, SPU_ES
, 0 );
3449 p_track
->asfinfo
.i_cat
= p_track
->fmt
.i_cat
;
3451 const MP4_Box_t
*p_elst
;
3452 p_track
->i_elst
= 0;
3453 p_track
->i_elst_time
= 0;
3454 if( ( p_track
->p_elst
= p_elst
= MP4_BoxGet( p_box_trak
, "edts/elst" ) ) )
3456 MP4_Box_data_elst_t
*elst
= BOXDATA(p_elst
);
3459 msg_Warn( p_demux
, "elst box found" );
3460 for( i
= 0; i
< elst
->i_entry_count
; i
++ )
3462 msg_Dbg( p_demux
, " - [%d] duration=%"PRId64
"ms media time=%"PRId64
3463 "ms) rate=%d.%d", i
,
3464 MP4_rescale( elst
->i_segment_duration
[i
], p_sys
->i_timescale
, 1000 ),
3465 elst
->i_media_time
[i
] >= 0 ?
3466 MP4_rescale( elst
->i_media_time
[i
], p_track
->i_timescale
, 1000 ) :
3468 elst
->i_media_rate_integer
[i
],
3469 elst
->i_media_rate_fraction
[i
] );
3476 p_dinf = MP4_BoxGet( p_minf, "dinf" );
3478 if( !( p_track
->p_stbl
= MP4_BoxGet( p_box_trak
,"mdia/minf/stbl" ) ) ||
3479 !( p_track
->p_stsd
= MP4_BoxGet( p_box_trak
,"mdia/minf/stbl/stsd") ) )
3485 if( *language
&& strcmp( language
, "```" ) && strcmp( language
, "und" ) )
3487 p_track
->fmt
.psz_language
= strdup( language
);
3490 const MP4_Box_t
*p_udta
= MP4_BoxGet( p_box_trak
, "udta" );
3493 const MP4_Box_t
*p_box_iter
;
3494 for( p_box_iter
= p_udta
->p_first
; p_box_iter
!= NULL
;
3495 p_box_iter
= p_box_iter
->p_next
)
3497 switch( p_box_iter
->i_type
)
3501 p_track
->fmt
.psz_description
=
3502 strndup( p_box_iter
->data
.p_binary
->p_blob
,
3503 p_box_iter
->data
.p_binary
->i_blob
);
3510 /* Create chunk index table and sample index table */
3511 if( TrackCreateChunksIndex( p_demux
,p_track
) ||
3512 TrackCreateSamplesIndex( p_demux
, p_track
) )
3514 msg_Err( p_demux
, "cannot create chunks index" );
3515 return; /* cannot create chunks index */
3518 p_track
->i_chunk
= 0;
3519 p_track
->i_sample
= 0;
3521 /* Mark chapter only track */
3522 if( p_sys
->p_tref_chap
)
3524 MP4_Box_data_tref_generic_t
*p_chap
= p_sys
->p_tref_chap
->data
.p_tref_generic
;
3527 for( i
= 0; i
< p_chap
->i_entry_count
; i
++ )
3529 if( p_track
->i_track_ID
== p_chap
->i_track_ID
[i
] &&
3530 p_track
->fmt
.i_cat
== UNKNOWN_ES
)
3532 p_track
->b_chapters_source
= true;
3533 p_track
->b_enable
= false;
3539 const MP4_Box_t
*p_tsel
;
3541 if( b_force_enable
&&
3542 ( p_track
->fmt
.i_cat
== VIDEO_ES
|| p_track
->fmt
.i_cat
== AUDIO_ES
) )
3544 msg_Warn( p_demux
, "Enabling track[Id 0x%x] (buggy file without enabled track)",
3545 p_track
->i_track_ID
);
3546 p_track
->b_enable
= true;
3547 p_track
->fmt
.i_priority
= ES_PRIORITY_SELECTABLE_MIN
;
3549 else if ( (p_tsel
= MP4_BoxGet( p_box_trak
, "udta/tsel" )) )
3551 if ( BOXDATA(p_tsel
) && BOXDATA(p_tsel
)->i_switch_group
)
3553 p_track
->i_switch_group
= BOXDATA(p_tsel
)->i_switch_group
;
3554 int i_priority
= ES_PRIORITY_SELECTABLE_MIN
;
3555 for ( unsigned int i
= 0; i
< p_sys
->i_tracks
; i
++ )
3557 const mp4_track_t
*p_other
= &p_sys
->track
[i
];
3558 if( p_other
&& p_other
!= p_track
&&
3559 p_other
->fmt
.i_cat
== p_track
->fmt
.i_cat
&&
3560 p_track
->i_switch_group
== p_other
->i_switch_group
)
3561 i_priority
= __MAX( i_priority
, p_other
->fmt
.i_priority
+ 1 );
3563 /* VLC only support ES priority for AUDIO_ES and SPU_ES.
3564 If there's another VIDEO_ES in the same group, we need to unselect it then */
3565 if ( p_track
->fmt
.i_cat
== VIDEO_ES
&& i_priority
> ES_PRIORITY_SELECTABLE_MIN
)
3566 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3568 p_track
->fmt
.i_priority
= i_priority
;
3571 /* If there's no tsel, try to enable the track coming first in edit list */
3572 else if ( p_track
->p_elst
&& p_track
->fmt
.i_priority
== ES_PRIORITY_SELECTABLE_MIN
)
3574 #define MAX_SELECTABLE (INT_MAX - ES_PRIORITY_SELECTABLE_MIN)
3575 for ( uint32_t i
=0; i
<p_track
->BOXDATA(p_elst
)->i_entry_count
; i
++ )
3577 if ( p_track
->BOXDATA(p_elst
)->i_media_time
[i
] >= 0 &&
3578 p_track
->BOXDATA(p_elst
)->i_segment_duration
[i
] )
3580 /* We do selection by inverting start time into priority.
3581 The track with earliest edit will have the highest prio */
3582 const int i_time
= __MIN( MAX_SELECTABLE
, p_track
->BOXDATA(p_elst
)->i_media_time
[i
] );
3583 p_track
->fmt
.i_priority
= ES_PRIORITY_SELECTABLE_MIN
+ MAX_SELECTABLE
- i_time
;
3589 if( p_sys
->hacks
.es_cat_filters
&& (p_sys
->hacks
.es_cat_filters
& p_track
->fmt
.i_cat
) == 0 )
3591 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3594 if( !p_track
->b_enable
)
3595 p_track
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
3597 if( TrackCreateES( p_demux
,
3598 p_track
, p_track
->i_chunk
,
3599 (p_track
->b_chapters_source
|| !b_create_es
) ? NULL
: &p_track
->p_es
) )
3601 msg_Err( p_demux
, "cannot create es for track[Id 0x%x]",
3602 p_track
->i_track_ID
);
3606 p_track
->b_ok
= true;
3609 static void DestroyChunk( mp4_chunk_t
*ck
)
3611 free( ck
->p_sample_count_dts
);
3612 free( ck
->p_sample_delta_dts
);
3613 free( ck
->p_sample_count_pts
);
3614 free( ck
->p_sample_offset_pts
);
3615 free( ck
->p_sample_size
);
3618 /****************************************************************************
3620 ****************************************************************************
3621 * Cleans a track created by MP4_TrackCreate.
3622 ****************************************************************************/
3623 static void MP4_TrackClean( es_out_t
*out
, mp4_track_t
*p_track
)
3625 es_format_Clean( &p_track
->fmt
);
3628 es_out_Del( out
, p_track
->p_es
);
3630 if( p_track
->chunk
)
3632 for( unsigned int i_chunk
= 0; i_chunk
< p_track
->i_chunk_count
; i_chunk
++ )
3633 DestroyChunk( &p_track
->chunk
[i_chunk
] );
3635 free( p_track
->chunk
);
3637 if( !p_track
->i_sample_size
)
3638 free( p_track
->p_sample_size
);
3640 if ( p_track
->asfinfo
.p_frame
)
3641 block_ChainRelease( p_track
->asfinfo
.p_frame
);
3643 free( p_track
->context
.runs
.p_array
);
3646 static void MP4_TrackInit( mp4_track_t
*p_track
)
3648 memset( p_track
, 0, sizeof(mp4_track_t
) );
3649 es_format_Init( &p_track
->fmt
, UNKNOWN_ES
, 0 );
3650 p_track
->i_timescale
= 1;
3653 static void MP4_TrackSelect( demux_t
*p_demux
, mp4_track_t
*p_track
, bool b_select
)
3655 if( !p_track
->b_ok
|| p_track
->b_chapters_source
)
3658 if( b_select
== p_track
->b_selected
)
3661 if( !b_select
&& p_track
->p_es
)
3663 es_out_Control( p_demux
->out
, ES_OUT_SET_ES_STATE
,
3664 p_track
->p_es
, false );
3667 p_track
->b_selected
= b_select
;
3670 static int MP4_TrackSeek( demux_t
*p_demux
, mp4_track_t
*p_track
,
3671 vlc_tick_t i_start
)
3676 if( !p_track
->b_ok
|| p_track
->b_chapters_source
)
3677 return VLC_EGENERIC
;
3679 p_track
->b_selected
= false;
3681 if( TrackTimeToSampleChunk( p_demux
, p_track
, i_start
,
3682 &i_chunk
, &i_sample
) )
3684 msg_Warn( p_demux
, "cannot select track[Id 0x%x]",
3685 p_track
->i_track_ID
);
3686 return VLC_EGENERIC
;
3689 p_track
->b_selected
= true;
3690 if( !TrackGotoChunkSample( p_demux
, p_track
, i_chunk
, i_sample
) )
3691 p_track
->b_selected
= true;
3693 return p_track
->b_selected
? VLC_SUCCESS
: VLC_EGENERIC
;
3698 * 3 types: for audio
3701 static inline uint32_t MP4_GetFixedSampleSize( const mp4_track_t
*p_track
,
3702 const MP4_Box_data_sample_soun_t
*p_soun
)
3704 uint32_t i_size
= p_track
->i_sample_size
;
3706 assert( p_track
->i_sample_size
!= 0 );
3708 /* QuickTime "built-in" support case fixups */
3709 if( p_track
->fmt
.i_cat
== AUDIO_ES
&&
3710 p_soun
->i_compressionid
== 0 && p_track
->i_sample_size
<= 2 )
3712 switch( p_track
->fmt
.i_codec
)
3715 i_size
= p_soun
->i_channelcount
;
3717 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
3721 case VLC_CODEC_S24L
:
3722 case VLC_CODEC_S24B
:
3723 case VLC_CODEC_S32L
:
3724 case VLC_CODEC_S32B
:
3725 case VLC_CODEC_F32L
:
3726 case VLC_CODEC_F32B
:
3727 case VLC_CODEC_F64L
:
3728 case VLC_CODEC_F64B
:
3729 if( p_track
->i_sample_size
< ((p_soun
->i_samplesize
+7U)/8U) * p_soun
->i_channelcount
)
3730 i_size
= ((p_soun
->i_samplesize
+7)/8) * p_soun
->i_channelcount
;
3732 case VLC_CODEC_ALAW
:
3733 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
3734 i_size
= p_soun
->i_channelcount
;
3744 static uint32_t MP4_TrackGetReadSize( mp4_track_t
*p_track
, uint32_t *pi_nb_samples
)
3746 uint32_t i_size
= 0;
3749 if ( p_track
->i_sample
== p_track
->i_sample_count
)
3752 if ( p_track
->fmt
.i_cat
!= AUDIO_ES
)
3756 if( p_track
->i_sample_size
== 0 ) /* all sizes are different */
3757 return p_track
->p_sample_size
[p_track
->i_sample
];
3759 return p_track
->i_sample_size
;
3763 const MP4_Box_data_sample_soun_t
*p_soun
= p_track
->p_sample
->data
.p_sample_soun
;
3764 const mp4_chunk_t
*p_chunk
= &p_track
->chunk
[p_track
->i_chunk
];
3765 uint32_t i_max_samples
= p_chunk
->i_sample_count
- p_chunk
->i_sample
;
3767 /* Group audio packets so we don't call demux for single sample unit */
3768 if( p_track
->fmt
.i_original_fourcc
== VLC_CODEC_DVD_LPCM
&&
3769 p_soun
->i_constLPCMframesperaudiopacket
&&
3770 p_soun
->i_constbytesperaudiopacket
)
3772 /* uncompressed case */
3773 uint32_t i_packets
= i_max_samples
/ p_soun
->i_constLPCMframesperaudiopacket
;
3774 if ( UINT32_MAX
/ p_soun
->i_constbytesperaudiopacket
< i_packets
)
3775 i_packets
= UINT32_MAX
/ p_soun
->i_constbytesperaudiopacket
;
3776 *pi_nb_samples
= i_packets
* p_soun
->i_constLPCMframesperaudiopacket
;
3777 return i_packets
* p_soun
->i_constbytesperaudiopacket
;
3780 if( p_track
->fmt
.i_original_fourcc
== VLC_FOURCC('r','r','t','p') )
3783 return p_track
->i_sample_size
;
3786 /* all samples have a different size */
3787 if( p_track
->i_sample_size
== 0 )
3790 return p_track
->p_sample_size
[p_track
->i_sample
];
3793 if( p_soun
->i_qt_version
== 1 )
3795 if ( p_soun
->i_compressionid
== 0xFFFE )
3797 *pi_nb_samples
= 1; /* != number of audio samples */
3798 if ( p_track
->i_sample_size
)
3799 return p_track
->i_sample_size
;
3801 return p_track
->p_sample_size
[p_track
->i_sample
];
3803 else if ( p_soun
->i_compressionid
!= 0 || p_soun
->i_bytes_per_sample
> 1 ) /* compressed */
3805 /* in this case we are dealing with compressed data
3806 -2 in V1: additional fields are meaningless (VBR and such) */
3807 *pi_nb_samples
= i_max_samples
;//p_track->chunk[p_track->i_chunk].i_sample_count;
3808 if( p_track
->fmt
.audio
.i_blockalign
> 1 )
3809 *pi_nb_samples
= p_soun
->i_sample_per_packet
;
3810 i_size
= *pi_nb_samples
/ p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
;
3813 else /* uncompressed case */
3816 if( p_track
->fmt
.audio
.i_blockalign
> 1 )
3819 i_packets
= i_max_samples
/ p_soun
->i_sample_per_packet
;
3821 if ( UINT32_MAX
/ p_soun
->i_bytes_per_frame
< i_packets
)
3822 i_packets
= UINT32_MAX
/ p_soun
->i_bytes_per_frame
;
3824 *pi_nb_samples
= i_packets
* p_soun
->i_sample_per_packet
;
3825 i_size
= i_packets
* p_soun
->i_bytes_per_frame
;
3830 /* uncompressed v0 (qt) or... not (ISO) */
3832 /* Quicktime built-in support handling */
3833 if( p_soun
->i_compressionid
== 0 && p_track
->i_sample_size
== 1 )
3835 switch( p_track
->fmt
.i_codec
)
3837 /* sample size is not integer */
3839 *pi_nb_samples
= 160 * p_track
->fmt
.audio
.i_channels
;
3840 return 33 * p_track
->fmt
.audio
.i_channels
;
3846 /* More regular V0 cases */
3847 uint32_t i_max_v0_samples
;
3848 switch( p_track
->fmt
.i_codec
)
3850 /* Compressed samples in V0 */
3851 case VLC_CODEC_AMR_NB
:
3852 case VLC_CODEC_AMR_WB
:
3853 i_max_v0_samples
= 16;
3855 case VLC_CODEC_MPGA
:
3859 case VLC_CODEC_MP4A
:
3861 i_max_v0_samples
= 1;
3863 /* fixme, reverse using a list of uncompressed codecs */
3865 /* Read 25ms of samples (uncompressed) */
3866 i_max_v0_samples
= p_track
->fmt
.audio
.i_rate
/ 40 *
3867 p_track
->fmt
.audio
.i_channels
;
3868 if( i_max_v0_samples
< 1 )
3869 i_max_v0_samples
= 1;
3874 for( uint32_t i
=p_track
->i_sample
;
3875 i
<p_chunk
->i_sample_first
+p_chunk
->i_sample_count
&&
3876 i
<p_track
->i_sample_count
;
3880 if ( p_track
->i_sample_size
== 0 )
3881 i_size
+= p_track
->p_sample_size
[i
];
3883 i_size
+= MP4_GetFixedSampleSize( p_track
, p_soun
);
3885 /* Try to detect compression in ISO */
3886 if(p_soun
->i_compressionid
!= 0)
3888 /* Return only 1 sample */
3892 if ( *pi_nb_samples
== i_max_v0_samples
)
3897 //fprintf( stderr, "size=%d\n", i_size );
3901 static uint64_t MP4_TrackGetPos( mp4_track_t
*p_track
)
3903 unsigned int i_sample
;
3906 i_pos
= p_track
->chunk
[p_track
->i_chunk
].i_offset
;
3908 if( p_track
->i_sample_size
)
3910 MP4_Box_data_sample_soun_t
*p_soun
=
3911 p_track
->p_sample
->data
.p_sample_soun
;
3913 /* Quicktime builtin support, _must_ ignore sample tables */
3914 if( p_track
->fmt
.i_cat
== AUDIO_ES
&& p_soun
->i_compressionid
== 0 &&
3915 p_track
->i_sample_size
== 1 )
3917 switch( p_track
->fmt
.i_codec
)
3919 case VLC_CODEC_GSM
: /* # Samples > data size */
3920 i_pos
+= ( p_track
->i_sample
-
3921 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) / 160 * 33;
3928 if( p_track
->fmt
.i_cat
!= AUDIO_ES
|| p_soun
->i_qt_version
== 0 ||
3929 p_track
->fmt
.audio
.i_blockalign
<= 1 ||
3930 p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
== 0 )
3932 i_pos
+= ( p_track
->i_sample
-
3933 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) *
3934 MP4_GetFixedSampleSize( p_track
, p_soun
);
3938 /* we read chunk by chunk unless a blockalign is requested */
3939 i_pos
+= ( p_track
->i_sample
- p_track
->chunk
[p_track
->i_chunk
].i_sample_first
) /
3940 p_soun
->i_sample_per_packet
* p_soun
->i_bytes_per_frame
;
3945 for( i_sample
= p_track
->chunk
[p_track
->i_chunk
].i_sample_first
;
3946 i_sample
< p_track
->i_sample
; i_sample
++ )
3948 i_pos
+= p_track
->p_sample_size
[i_sample
];
3955 static int MP4_TrackNextSample( demux_t
*p_demux
, mp4_track_t
*p_track
, uint32_t i_samples
)
3957 if ( UINT32_MAX
- p_track
->i_sample
< i_samples
)
3959 p_track
->i_sample
= UINT32_MAX
;
3960 return VLC_EGENERIC
;
3963 p_track
->i_sample
+= i_samples
;
3965 if( p_track
->i_sample
>= p_track
->i_sample_count
)
3966 return VLC_EGENERIC
;
3968 /* Have we changed chunk ? */
3969 if( p_track
->i_sample
>=
3970 p_track
->chunk
[p_track
->i_chunk
].i_sample_first
+
3971 p_track
->chunk
[p_track
->i_chunk
].i_sample_count
)
3973 if( TrackGotoChunkSample( p_demux
, p_track
, p_track
->i_chunk
+ 1,
3974 p_track
->i_sample
) )
3976 msg_Warn( p_demux
, "track[0x%x] will be disabled "
3977 "(cannot restart decoder)", p_track
->i_track_ID
);
3978 MP4_TrackSelect( p_demux
, p_track
, false );
3979 return VLC_EGENERIC
;
3983 /* Have we changed elst */
3984 if( p_track
->p_elst
&& p_track
->BOXDATA(p_elst
)->i_entry_count
> 0 )
3986 demux_sys_t
*p_sys
= p_demux
->p_sys
;
3987 MP4_Box_data_elst_t
*elst
= p_track
->BOXDATA(p_elst
);
3988 uint64_t i_mvt
= MP4_rescale_qtime( MP4_TrackGetDTS( p_demux
, p_track
),
3989 p_sys
->i_timescale
);
3990 if( (unsigned int)p_track
->i_elst
< elst
->i_entry_count
&&
3991 i_mvt
>= p_track
->i_elst_time
+
3992 elst
->i_segment_duration
[p_track
->i_elst
] )
3994 MP4_TrackSetELST( p_demux
, p_track
,
3995 MP4_TrackGetDTS( p_demux
, p_track
) );
4002 static void MP4_TrackSetELST( demux_t
*p_demux
, mp4_track_t
*tk
,
4005 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4006 int i_elst_last
= tk
->i_elst
;
4008 /* handle elst (find the correct one) */
4010 tk
->i_elst_time
= 0;
4011 if( tk
->p_elst
&& tk
->BOXDATA(p_elst
)->i_entry_count
> 0 )
4013 MP4_Box_data_elst_t
*elst
= tk
->BOXDATA(p_elst
);
4014 int64_t i_mvt
= MP4_rescale_qtime( i_time
, p_sys
->i_timescale
);
4016 for( tk
->i_elst
= 0; (unsigned int)tk
->i_elst
< elst
->i_entry_count
; tk
->i_elst
++ )
4018 uint64_t i_dur
= elst
->i_segment_duration
[tk
->i_elst
];
4020 if( tk
->i_elst_time
<= i_mvt
&& i_mvt
< tk
->i_elst_time
+ i_dur
)
4024 tk
->i_elst_time
+= i_dur
;
4027 if( (unsigned int)tk
->i_elst
>= elst
->i_entry_count
)
4029 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
4030 tk
->i_elst
= elst
->i_entry_count
- 1;
4031 tk
->i_elst_time
-= elst
->i_segment_duration
[tk
->i_elst
];
4034 if( elst
->i_media_time
[tk
->i_elst
] < 0 )
4037 tk
->i_elst_time
+= elst
->i_segment_duration
[tk
->i_elst
];
4040 if( i_elst_last
!= tk
->i_elst
)
4042 msg_Warn( p_demux
, "elst old=%d new=%d", i_elst_last
, tk
->i_elst
);
4043 tk
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
4047 /******************************************************************************
4048 * Here are the functions used for fragmented MP4
4049 *****************************************************************************/
4053 * \Note If we call that function too soon,
4054 * before the track has been selected by MP4_TrackSelect
4055 * (during the first execution of Demux), then the track gets disabled
4057 static int ReInitDecoder( demux_t
*p_demux
, const MP4_Box_t
*p_root
,
4058 mp4_track_t
*p_track
)
4060 MP4_Box_t
*p_paramsbox
= MP4_BoxGet( p_root
, "/moov/trak[0]" );
4062 return VLC_EGENERIC
;
4064 MP4_TrackRestart( p_demux
, p_track
, p_paramsbox
);
4066 /* Temporary hack until we support track selection */
4067 p_track
->b_selected
= true;
4068 p_track
->b_enable
= true;
4074 static stime_t
GetCumulatedDuration( demux_t
*p_demux
)
4076 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4077 stime_t i_max_duration
= 0;
4079 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
4081 stime_t i_track_duration
= 0;
4082 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_sys
->p_moov
, p_sys
->track
[i
].i_track_ID
);
4083 const MP4_Box_t
*p_stsz
;
4084 const MP4_Box_t
*p_tkhd
;
4085 if ( (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) &&
4086 (p_stsz
= MP4_BoxGet( p_trak
, "mdia/minf/stbl/stsz" )) &&
4087 /* duration might be wrong an be set to whole duration :/ */
4088 BOXDATA(p_stsz
)->i_sample_count
> 0 )
4090 i_max_duration
= __MAX( (uint64_t)i_max_duration
, BOXDATA(p_tkhd
)->i_duration
);
4093 if( p_sys
->p_fragsindex
)
4095 i_track_duration
+= MP4_Fragment_Index_GetTrackDuration( p_sys
->p_fragsindex
, i
);
4098 i_max_duration
= __MAX( i_max_duration
, i_track_duration
);
4101 return i_max_duration
;
4104 static int ProbeIndex( demux_t
*p_demux
)
4106 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4107 uint64_t i_stream_size
;
4108 uint8_t mfro
[MP4_MFRO_BOXSIZE
];
4109 assert( p_sys
->b_seekable
);
4111 if ( MP4_BoxCount( p_sys
->p_root
, "/mfra" ) )
4112 return VLC_EGENERIC
;
4114 i_stream_size
= stream_Size( p_demux
->s
);
4115 if ( ( i_stream_size
>> 62 ) ||
4116 ( i_stream_size
< MP4_MFRO_BOXSIZE
) ||
4117 ( vlc_stream_Seek( p_demux
->s
, i_stream_size
- MP4_MFRO_BOXSIZE
) != VLC_SUCCESS
)
4120 msg_Dbg( p_demux
, "Probing tail for mfro has failed" );
4121 return VLC_EGENERIC
;
4124 if ( vlc_stream_Read( p_demux
->s
, &mfro
, MP4_MFRO_BOXSIZE
) == MP4_MFRO_BOXSIZE
&&
4125 VLC_FOURCC(mfro
[4],mfro
[5],mfro
[6],mfro
[7]) == ATOM_mfro
&&
4126 GetDWBE( &mfro
) == MP4_MFRO_BOXSIZE
)
4128 uint32_t i_offset
= GetDWBE( &mfro
[12] );
4129 msg_Dbg( p_demux
, "will read mfra index at %"PRIu64
, i_stream_size
- i_offset
);
4130 if ( i_stream_size
> i_offset
&&
4131 vlc_stream_Seek( p_demux
->s
, i_stream_size
- i_offset
) == VLC_SUCCESS
)
4133 msg_Dbg( p_demux
, "reading mfra index at %"PRIu64
, i_stream_size
- i_offset
);
4134 const uint32_t stoplist
[] = { ATOM_mfra
, 0 };
4135 MP4_ReadBoxContainerChildren( p_demux
->s
, p_sys
->p_root
, stoplist
);
4139 return VLC_EGENERIC
;
4142 static stime_t
GetMoovTrackDuration( demux_sys_t
*p_sys
, unsigned i_track_ID
)
4144 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_sys
->p_moov
, i_track_ID
);
4145 const MP4_Box_t
*p_stsz
;
4146 const MP4_Box_t
*p_tkhd
;
4147 if ( (p_tkhd
= MP4_BoxGet( p_trak
, "tkhd" )) &&
4148 (p_stsz
= MP4_BoxGet( p_trak
, "mdia/minf/stbl/stsz" )) &&
4149 /* duration might be wrong an be set to whole duration :/ */
4150 BOXDATA(p_stsz
)->i_sample_count
> 0 )
4152 if( BOXDATA(p_tkhd
)->i_duration
<= p_sys
->i_moov_duration
)
4153 return BOXDATA(p_tkhd
)->i_duration
; /* In movie / mvhd scale */
4155 return p_sys
->i_moov_duration
;
4160 static bool GetMoofTrackDuration( MP4_Box_t
*p_moov
, MP4_Box_t
*p_moof
,
4161 unsigned i_track_ID
, stime_t
*p_duration
)
4163 if ( !p_moof
|| !p_moov
)
4166 MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
4169 if ( p_traf
->i_type
!= ATOM_traf
)
4171 p_traf
= p_traf
->p_next
;
4175 const MP4_Box_t
*p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" );
4176 const MP4_Box_t
*p_trun
= MP4_BoxGet( p_traf
, "trun" );
4177 if ( !p_tfhd
|| !p_trun
|| i_track_ID
!= BOXDATA(p_tfhd
)->i_track_ID
)
4179 p_traf
= p_traf
->p_next
;
4183 uint32_t i_track_timescale
= 0;
4184 uint32_t i_track_defaultsampleduration
= 0;
4186 /* set trex for defaults */
4187 MP4_Box_t
*p_trex
= MP4_GetTrexByTrackID( p_moov
, BOXDATA(p_tfhd
)->i_track_ID
);
4190 i_track_defaultsampleduration
= BOXDATA(p_trex
)->i_default_sample_duration
;
4193 MP4_Box_t
*p_trak
= MP4_GetTrakByTrackID( p_moov
, BOXDATA(p_tfhd
)->i_track_ID
);
4196 MP4_Box_t
*p_mdhd
= MP4_BoxGet( p_trak
, "mdia/mdhd" );
4198 i_track_timescale
= BOXDATA(p_mdhd
)->i_timescale
;
4201 if ( !i_track_timescale
)
4203 p_traf
= p_traf
->p_next
;
4207 uint64_t i_traf_duration
= 0;
4208 while ( p_trun
&& p_tfhd
)
4210 if ( p_trun
->i_type
!= ATOM_trun
)
4212 p_trun
= p_trun
->p_next
;
4215 const MP4_Box_data_trun_t
*p_trundata
= p_trun
->data
.p_trun
;
4217 /* Sum total time */
4218 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4220 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4221 i_traf_duration
+= p_trundata
->p_samples
[i
].i_duration
;
4223 else if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
4225 i_traf_duration
+= p_trundata
->i_sample_count
*
4226 BOXDATA(p_tfhd
)->i_default_sample_duration
;
4230 i_traf_duration
+= p_trundata
->i_sample_count
*
4231 i_track_defaultsampleduration
;
4234 p_trun
= p_trun
->p_next
;
4237 *p_duration
= i_traf_duration
;
4244 static int ProbeFragments( demux_t
*p_demux
, bool b_force
, bool *pb_fragmented
)
4246 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4248 msg_Dbg( p_demux
, "probing fragments from %"PRId64
, vlc_stream_Tell( p_demux
->s
) );
4250 assert( p_sys
->p_root
);
4252 MP4_Box_t
*p_vroot
= MP4_BoxNew(ATOM_root
);
4254 return VLC_EGENERIC
;
4256 if( p_sys
->b_seekable
&& (p_sys
->b_fastseekable
|| b_force
) )
4258 MP4_ReadBoxContainerChildren( p_demux
->s
, p_vroot
, NULL
); /* Get the rest of the file */
4259 p_sys
->b_fragments_probed
= true;
4261 const unsigned i_moof
= MP4_BoxCount( p_vroot
, "/moof" );
4264 *pb_fragmented
= true;
4265 p_sys
->p_fragsindex
= MP4_Fragments_Index_New( p_sys
->i_tracks
, i_moof
);
4266 if( !p_sys
->p_fragsindex
)
4268 MP4_BoxFree( p_vroot
);
4269 return VLC_EGENERIC
;
4272 stime_t
*pi_track_times
= calloc( p_sys
->i_tracks
, sizeof(*pi_track_times
) );
4273 if( !pi_track_times
)
4275 MP4_Fragments_Index_Delete( p_sys
->p_fragsindex
);
4276 p_sys
->p_fragsindex
= NULL
;
4277 MP4_BoxFree( p_vroot
);
4278 return VLC_EGENERIC
;
4283 for( MP4_Box_t
*p_moof
= p_vroot
->p_first
; p_moof
; p_moof
= p_moof
->p_next
)
4285 if( p_moof
->i_type
!= ATOM_moof
)
4288 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4290 MP4_Box_t
*p_tfdt
= NULL
;
4291 MP4_Box_t
*p_traf
= MP4_GetTrafByTrackID( p_moof
, p_sys
->track
[i
].i_track_ID
);
4293 p_tfdt
= MP4_BoxGet( p_traf
, "tfdt" );
4295 if( p_tfdt
&& BOXDATA(p_tfdt
) )
4297 pi_track_times
[i
] = p_tfdt
->data
.p_tfdt
->i_base_media_decode_time
;
4299 else if( index
== 0 ) /* Set first fragment time offset from moov */
4301 stime_t i_duration
= GetMoovTrackDuration( p_sys
, p_sys
->track
[i
].i_track_ID
);
4302 pi_track_times
[i
] = MP4_rescale( i_duration
, p_sys
->i_timescale
, p_sys
->track
[i
].i_timescale
);
4305 stime_t i_movietime
= MP4_rescale( pi_track_times
[i
], p_sys
->track
[i
].i_timescale
, p_sys
->i_timescale
);
4306 p_sys
->p_fragsindex
->p_times
[index
* p_sys
->i_tracks
+ i
] = i_movietime
;
4308 stime_t i_duration
= 0;
4309 if( GetMoofTrackDuration( p_sys
->p_moov
, p_moof
, p_sys
->track
[i
].i_track_ID
, &i_duration
) )
4310 pi_track_times
[i
] += i_duration
;
4313 p_sys
->p_fragsindex
->pi_pos
[index
++] = p_moof
->i_pos
;
4316 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4318 stime_t i_movietime
= MP4_rescale( pi_track_times
[i
], p_sys
->track
[i
].i_timescale
, p_sys
->i_timescale
);
4319 if( p_sys
->p_fragsindex
->i_last_time
< i_movietime
)
4320 p_sys
->p_fragsindex
->i_last_time
= i_movietime
;
4323 free( pi_track_times
);
4325 MP4_Fragments_Index_Dump( VLC_OBJECT(p_demux
), p_sys
->p_fragsindex
, p_sys
->i_timescale
);
4331 /* We stop at first moof, which validates our fragmentation condition
4332 * and we'll find others while reading. */
4333 const uint32_t excllist
[] = { ATOM_moof
, 0 };
4334 MP4_ReadBoxContainerRestricted( p_demux
->s
, p_vroot
, NULL
, excllist
);
4335 /* Peek since we stopped before restriction */
4336 const uint8_t *p_peek
;
4337 if ( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) == 8 )
4338 *pb_fragmented
= (VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] ) == ATOM_moof
);
4340 *pb_fragmented
= false;
4343 MP4_BoxFree( p_vroot
);
4345 MP4_Box_t
*p_mehd
= MP4_BoxGet( p_sys
->p_moov
, "mvex/mehd");
4347 p_sys
->i_cumulated_duration
= GetCumulatedDuration( p_demux
);
4352 static void FragResetContext( demux_sys_t
*p_sys
)
4354 if( p_sys
->context
.p_fragment_atom
)
4356 if( p_sys
->context
.p_fragment_atom
!= p_sys
->p_moov
)
4357 MP4_BoxFree( p_sys
->context
.p_fragment_atom
);
4358 p_sys
->context
.p_fragment_atom
= NULL
;
4360 p_sys
->context
.i_current_box_type
= 0;
4362 for ( uint32_t i
=0; i
<p_sys
->i_tracks
; i
++ )
4364 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4365 p_track
->context
.i_default_sample_size
= 0;
4366 p_track
->context
.i_default_sample_duration
= 0;
4370 static int FragDemuxTrack( demux_t
*p_demux
, mp4_track_t
*p_track
,
4371 vlc_tick_t i_max_preload
)
4373 if( !p_track
->b_ok
||
4374 p_track
->context
.runs
.i_current
>= p_track
->context
.runs
.i_count
)
4375 return VLC_DEMUXER_EOS
;
4377 const MP4_Box_data_trun_t
*p_trun
=
4378 p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].p_trun
->data
.p_trun
;
4380 if( p_track
->context
.i_trun_sample
>= p_trun
->i_sample_count
)
4381 return VLC_DEMUXER_EOS
;
4383 uint32_t dur
= p_track
->context
.i_default_sample_duration
,
4384 len
= p_track
->context
.i_default_sample_size
;
4386 if( vlc_stream_Tell(p_demux
->s
) != p_track
->context
.i_trun_sample_pos
&&
4387 MP4_Seek( p_demux
->s
, p_track
->context
.i_trun_sample_pos
) != VLC_SUCCESS
)
4388 return VLC_DEMUXER_EOF
;
4390 const stime_t i_demux_max_dts
= (i_max_preload
< INVALID_PRELOAD
) ?
4391 p_track
->i_time
+ MP4_rescale_qtime( i_max_preload
, p_track
->i_timescale
) :
4394 for( uint32_t i
= p_track
->context
.i_trun_sample
; i
< p_trun
->i_sample_count
; i
++ )
4396 const stime_t i_dts
= p_track
->i_time
;
4397 stime_t i_pts
= i_dts
;
4399 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4400 dur
= p_trun
->p_samples
[i
].i_duration
;
4402 if( i_dts
> i_demux_max_dts
)
4403 return VLC_DEMUXER_SUCCESS
;
4405 p_track
->i_time
+= dur
;
4406 p_track
->context
.i_trun_sample
= i
+ 1;
4408 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
)
4410 if ( p_trun
->i_version
== 1 )
4411 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v1
;
4412 else if( p_trun
->p_samples
[i
].i_composition_time_offset
.v0
< 0xFF000000 )
4413 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v0
;
4414 else /* version 0 with negative */
4415 i_pts
+= p_trun
->p_samples
[i
].i_composition_time_offset
.v1
;
4418 if( p_trun
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
4419 len
= p_trun
->p_samples
[i
].i_size
;
4422 msg_Warn(p_demux
, "Zero duration sample in trun.");
4425 msg_Warn(p_demux
, "Zero length sample in trun.");
4427 block_t
*p_block
= vlc_stream_Block( p_demux
->s
, len
);
4428 uint32_t i_read
= ( p_block
) ? p_block
->i_buffer
: 0;
4429 p_track
->context
.i_trun_sample_pos
+= i_read
;
4430 if( i_read
< len
|| p_block
== NULL
)
4433 block_Release( p_block
);
4434 return VLC_DEMUXER_FATAL
;
4438 msg_Dbg( p_demux
, "tk(%i)=%"PRId64
" mv=%"PRId64
" pos=%"PRIu64
, p_track
->i_track_ID
,
4439 VLC_TICK_0
+ MP4_rescale_mtime( i_dts
, p_track
->i_timescale
),
4440 VLC_TICK_0
+ MP4_rescale_mtime( i_pts
, p_track
->i_timescale
),
4441 p_track
->context
.i_trun_sample_pos
);
4443 if ( p_track
->p_es
)
4445 p_block
->i_dts
= VLC_TICK_0
+ MP4_rescale_mtime( i_dts
, p_track
->i_timescale
);
4446 if( p_track
->fmt
.i_cat
== VIDEO_ES
&& !( p_trun
->i_flags
& MP4_TRUN_SAMPLE_TIME_OFFSET
) )
4447 p_block
->i_pts
= VLC_TICK_INVALID
;
4449 p_block
->i_pts
= VLC_TICK_0
+ MP4_rescale_mtime( i_pts
, p_track
->i_timescale
);
4450 p_block
->i_length
= MP4_rescale_mtime( dur
, p_track
->i_timescale
);
4451 MP4_Block_Send( p_demux
, p_track
, p_block
);
4453 else block_Release( p_block
);
4456 if( p_track
->context
.i_trun_sample
== p_trun
->i_sample_count
)
4458 p_track
->context
.i_trun_sample
= 0;
4459 if( ++p_track
->context
.runs
.i_current
< p_track
->context
.runs
.i_count
)
4461 p_track
->i_time
= p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].i_first_dts
;
4462 p_track
->context
.i_trun_sample_pos
= p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_current
].i_offset
;
4466 return VLC_DEMUXER_SUCCESS
;
4469 static int DemuxMoof( demux_t
*p_demux
)
4471 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4474 const vlc_tick_t i_max_preload
= ( p_sys
->b_fastseekable
) ? 0 : ( p_sys
->b_seekable
) ? DEMUX_TRACK_MAX_PRELOAD
: INVALID_PRELOAD
;
4476 const vlc_tick_t i_nztime
= MP4_GetMoviePTS( p_sys
);
4478 /* !important! Ensure clock is set before sending data */
4479 if( p_sys
->i_pcr
== VLC_TICK_INVALID
)
4480 es_out_SetPCR( p_demux
->out
, VLC_TICK_0
+ i_nztime
);
4482 /* demux up to increment amount of data on every track, or just set pcr if empty data */
4485 mp4_track_t
*tk
= NULL
;
4486 i_status
= VLC_DEMUXER_EOS
;
4488 /* First pass, find any track within our target increment, ordered by position */
4489 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4491 mp4_track_t
*tk_tmp
= &p_sys
->track
[i
];
4493 if( !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
4494 (!tk_tmp
->b_selected
&& !p_sys
->b_seekable
) ||
4495 tk_tmp
->context
.runs
.i_current
>= tk_tmp
->context
.runs
.i_count
)
4498 /* At least still have data to demux on this or next turns */
4499 i_status
= VLC_DEMUXER_SUCCESS
;
4501 if( MP4_rescale_mtime( tk_tmp
->i_time
, tk_tmp
->i_timescale
) <= i_nztime
+ DEMUX_INCREMENT
)
4503 if( tk
== NULL
|| tk_tmp
->context
.i_trun_sample_pos
< tk
->context
.i_trun_sample_pos
)
4510 /* Second pass, refine and find any best candidate having a chunk pos closer than
4511 * current candidate (avoids seeks when increment falls between the 2) from
4512 * current position, but within extended interleave time */
4513 for( unsigned i
= 0; i_max_preload
!= 0 && i
< p_sys
->i_tracks
; i
++ )
4515 mp4_track_t
*tk_tmp
= &p_sys
->track
[i
];
4517 !tk_tmp
->b_ok
|| tk_tmp
->b_chapters_source
||
4518 (!tk_tmp
->b_selected
&& !p_sys
->b_seekable
) ||
4519 tk_tmp
->context
.runs
.i_current
>= tk_tmp
->context
.runs
.i_count
)
4522 vlc_tick_t i_nzdts
= MP4_rescale_mtime( tk_tmp
->i_time
, tk_tmp
->i_timescale
);
4523 if ( i_nzdts
<= i_nztime
+ DEMUX_TRACK_MAX_PRELOAD
)
4525 /* Found a better candidate to avoid seeking */
4526 if( tk_tmp
->context
.i_trun_sample_pos
< tk
->context
.i_trun_sample_pos
)
4528 /* Note: previous candidate will be repicked on next loop */
4532 int i_ret
= FragDemuxTrack( p_demux
, tk
, i_max_preload
);
4534 if( i_ret
== VLC_DEMUXER_SUCCESS
)
4535 i_status
= VLC_DEMUXER_SUCCESS
;
4536 else if( i_ret
== VLC_DEMUXER_FATAL
)
4537 i_status
= VLC_DEMUXER_EOF
;
4540 if( i_status
!= VLC_DEMUXER_SUCCESS
|| !tk
)
4544 if( i_status
!= VLC_DEMUXER_EOS
)
4546 p_sys
->i_nztime
+= DEMUX_INCREMENT
;
4547 p_sys
->i_pcr
= VLC_TICK_0
+ p_sys
->i_nztime
;
4548 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
4552 vlc_tick_t i_segment_end
= INT64_MAX
;
4553 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
4555 mp4_track_t
*tk
= &p_sys
->track
[i
];
4556 if( tk
->b_ok
|| tk
->b_chapters_source
||
4557 (!tk
->b_selected
&& !p_sys
->b_seekable
) )
4559 vlc_tick_t i_track_end
= MP4_rescale_mtime( tk
->i_time
, tk
->i_timescale
);
4560 if( i_track_end
< i_segment_end
)
4561 i_segment_end
= i_track_end
;
4563 if( i_segment_end
!= INT64_MAX
)
4565 p_sys
->i_nztime
= i_segment_end
;
4566 p_sys
->i_pcr
= VLC_TICK_0
+ p_sys
->i_nztime
;
4567 es_out_SetPCR( p_demux
->out
, p_sys
->i_pcr
);
4574 static int FragCreateTrunIndex( demux_t
*p_demux
, MP4_Box_t
*p_moof
,
4575 MP4_Box_t
*p_chunksidx
, stime_t i_moof_time
)
4577 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4579 uint64_t i_traf_base_data_offset
= p_moof
->i_pos
;
4580 uint32_t i_traf
= 0;
4581 uint64_t i_prev_traf_end
= 0;
4583 for( unsigned i
=0; i
<p_sys
->i_tracks
; i
++ )
4585 mp4_track_t
*p_track
= &p_sys
->track
[i
];
4586 if( p_track
->context
.runs
.p_array
)
4587 free( p_track
->context
.runs
.p_array
);
4588 p_track
->context
.runs
.p_array
= NULL
;
4589 p_track
->context
.runs
.i_count
= 0;
4590 p_track
->context
.runs
.i_current
= 0;
4593 for( MP4_Box_t
*p_traf
= MP4_BoxGet( p_moof
, "traf" );
4594 p_traf
; p_traf
= p_traf
->p_next
)
4596 if ( p_traf
->i_type
!= ATOM_traf
)
4599 const MP4_Box_t
*p_tfhd
= MP4_BoxGet( p_traf
, "tfhd" );
4600 const uint32_t i_trun_count
= MP4_BoxCount( p_traf
, "trun" );
4601 if ( !p_tfhd
|| !i_trun_count
)
4604 mp4_track_t
*p_track
= MP4_GetTrackByTrackID( p_demux
, BOXDATA(p_tfhd
)->i_track_ID
);
4608 p_track
->context
.runs
.p_array
= calloc(i_trun_count
, sizeof(mp4_run_t
));
4609 if(!p_track
->context
.runs
.p_array
)
4612 /* Get defaults for this/these RUN */
4613 uint32_t i_track_defaultsamplesize
= 0;
4614 uint32_t i_track_defaultsampleduration
= 0;
4615 MP4_GetDefaultSizeAndDuration( p_sys
->p_moov
, BOXDATA(p_tfhd
),
4616 &i_track_defaultsamplesize
,
4617 &i_track_defaultsampleduration
);
4618 p_track
->context
.i_default_sample_size
= i_track_defaultsamplesize
;
4619 p_track
->context
.i_default_sample_duration
= i_track_defaultsampleduration
;
4621 stime_t i_traf_start_time
= p_track
->i_time
;
4622 bool b_has_base_media_decode_time
= false;
4624 if( p_track
->context
.b_resync_time_offset
) /* We NEED start time offset for each track */
4626 p_track
->context
.b_resync_time_offset
= false;
4628 /* Find start time */
4629 const MP4_Box_t
*p_tfdt
= MP4_BoxGet( p_traf
, "tfdt" );
4632 i_traf_start_time
= BOXDATA(p_tfdt
)->i_base_media_decode_time
;
4633 b_has_base_media_decode_time
= true;
4636 /* Try using Tfxd for base offset (Smooth) */
4637 if( !b_has_base_media_decode_time
&& p_sys
->i_tracks
== 1 )
4639 const MP4_Box_t
*p_uuid
= MP4_BoxGet( p_traf
, "uuid" );
4640 for( ; p_uuid
; p_uuid
= p_uuid
->p_next
)
4642 if( p_uuid
->i_type
== ATOM_uuid
&&
4643 !CmpUUID( &p_uuid
->i_uuid
, &TfxdBoxUUID
) && p_uuid
->data
.p_tfxd
)
4645 i_traf_start_time
= p_uuid
->data
.p_tfxd
->i_fragment_abs_time
;
4646 b_has_base_media_decode_time
= true;
4652 /* After seek we should have probed fragments */
4653 if( !b_has_base_media_decode_time
&& p_sys
->p_fragsindex
)
4655 unsigned i_track_index
= (p_track
- p_sys
->track
);
4656 assert(&p_sys
->track
[i_track_index
] == p_track
);
4657 i_traf_start_time
= MP4_Fragment_Index_GetTrackStartTime( p_sys
->p_fragsindex
,
4658 i_track_index
, p_moof
->i_pos
);
4659 i_traf_start_time
= MP4_rescale( i_traf_start_time
,
4660 p_sys
->i_timescale
, p_track
->i_timescale
);
4661 b_has_base_media_decode_time
= true;
4664 if( !b_has_base_media_decode_time
&& p_chunksidx
)
4666 /* Try using SIDX as base offset.
4667 * This can not work for global sidx but only when sent within each fragment (dash) */
4668 const MP4_Box_data_sidx_t
*p_data
= p_chunksidx
->data
.p_sidx
;
4669 if( p_data
&& p_data
->i_timescale
&& p_data
->i_reference_count
== 1 )
4671 i_traf_start_time
= MP4_rescale( p_data
->i_earliest_presentation_time
,
4672 p_data
->i_timescale
, p_track
->i_timescale
);
4673 b_has_base_media_decode_time
= true;
4677 /* First contiguous segment (moov->moof) and there's no tfdt not probed index (yet) */
4678 if( !b_has_base_media_decode_time
&& FragGetMoofSequenceNumber( p_moof
) == 1 )
4680 i_traf_start_time
= MP4_rescale( GetMoovTrackDuration( p_sys
, p_track
->i_track_ID
),
4681 p_sys
->i_timescale
, p_track
->i_timescale
);
4682 b_has_base_media_decode_time
= true;
4685 /* Use global sidx moof time, in case moof does not carry tfdt */
4686 if( !b_has_base_media_decode_time
&& i_moof_time
!= INVALID_SEGMENT_TIME
)
4687 i_traf_start_time
= MP4_rescale( i_moof_time
, p_sys
->i_timescale
, p_track
->i_timescale
);
4689 /* That should not happen */
4690 if( !b_has_base_media_decode_time
)
4691 i_traf_start_time
= MP4_rescale_qtime( p_sys
->i_nztime
, p_track
->i_timescale
);
4694 /* Parse TRUN data */
4696 if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
)
4698 i_traf_base_data_offset
= BOXDATA(p_tfhd
)->i_base_data_offset
;
4700 /* ignored if MP4_TFHD_BASE_DATA_OFFSET */
4701 else if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_DEFAULT_BASE_IS_MOOF
)
4703 i_traf_base_data_offset
= p_moof
->i_pos
/* + 8*/;
4708 i_traf_base_data_offset
= p_moof
->i_pos
/*+ 8*/;
4710 i_traf_base_data_offset
= i_prev_traf_end
;
4713 uint64_t i_trun_dts
= i_traf_start_time
;
4714 uint64_t i_trun_data_offset
= i_traf_base_data_offset
;
4715 uint32_t i_trun_size
= 0;
4717 for( const MP4_Box_t
*p_trun
= MP4_BoxGet( p_traf
, "trun" );
4718 p_trun
&& p_tfhd
; p_trun
= p_trun
->p_next
)
4720 if ( p_trun
->i_type
!= ATOM_trun
)
4723 const MP4_Box_data_trun_t
*p_trundata
= p_trun
->data
.p_trun
;
4725 /* Get data offset */
4726 if ( p_trundata
->i_flags
& MP4_TRUN_DATA_OFFSET
)
4728 /* Fix for broken Trun data offset relative to tfhd instead of moof, as seen in smooth */
4729 if( (BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
) == 0 &&
4731 i_traf_base_data_offset
+ p_trundata
->i_data_offset
< p_moof
->i_pos
+ p_moof
->i_size
+ 8 )
4733 i_trun_data_offset
+= p_moof
->i_size
+ 8;
4735 else if( (BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_BASE_DATA_OFFSET
) )
4737 i_trun_data_offset
= BOXDATA(p_tfhd
)->i_base_data_offset
+ p_trundata
->i_data_offset
;
4739 /* ignored if MP4_TFHD_BASE_DATA_OFFSET */
4740 else if ( BOXDATA(p_tfhd
)->i_flags
& MP4_TFHD_DEFAULT_BASE_IS_MOOF
)
4742 i_trun_data_offset
= p_moof
->i_pos
+ p_trundata
->i_data_offset
;
4746 i_trun_data_offset
+= p_trundata
->i_data_offset
;
4751 i_trun_data_offset
+= i_trun_size
;
4757 "tk %u run %" PRIu32
" dflt dur %"PRIu32
" size %"PRIu32
" firstdts %"PRId64
" offset %"PRIu64
,
4758 p_track
->i_track_ID
,
4759 p_track
->context
.runs
.i_count
,
4760 i_track_defaultsampleduration
,
4761 i_track_defaultsamplesize
,
4762 MP4_rescale_mtime( i_trun_dts
, p_track
->i_timescale
), i_trun_data_offset
);
4765 mp4_run_t
*p_run
= &p_track
->context
.runs
.p_array
[p_track
->context
.runs
.i_count
++];
4766 p_run
->i_first_dts
= i_trun_dts
;
4767 p_run
->i_offset
= i_trun_data_offset
;
4768 p_run
->p_trun
= p_trun
;
4771 /* Sum total time */
4772 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_DURATION
)
4774 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4775 i_trun_dts
+= p_trundata
->p_samples
[i
].i_duration
;
4779 i_trun_dts
+= p_trundata
->i_sample_count
*
4780 i_track_defaultsampleduration
;
4783 /* Get total traf size */
4784 if ( p_trundata
->i_flags
& MP4_TRUN_SAMPLE_SIZE
)
4786 for( uint32_t i
=0; i
< p_trundata
->i_sample_count
; i
++ )
4787 i_trun_size
+= p_trundata
->p_samples
[i
].i_size
;
4791 i_trun_size
+= p_trundata
->i_sample_count
*
4792 i_track_defaultsamplesize
;
4795 i_prev_traf_end
= i_trun_data_offset
+ i_trun_size
;
4804 static int FragGetMoofBySidxIndex( demux_t
*p_demux
, vlc_tick_t target_time
,
4805 uint64_t *pi_moof_pos
, vlc_tick_t
*pi_sampletime
)
4807 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4808 const MP4_Box_t
*p_sidx
= MP4_BoxGet( p_sys
->p_root
, "sidx" );
4809 const MP4_Box_data_sidx_t
*p_data
;
4810 if( !p_sidx
|| !((p_data
= BOXDATA(p_sidx
))) || !p_data
->i_timescale
)
4811 return VLC_EGENERIC
;
4813 stime_t i_target_time
= MP4_rescale_qtime( target_time
, p_data
->i_timescale
);
4815 /* sidx refers to offsets from end of sidx pos in the file + first offset */
4816 uint64_t i_pos
= p_data
->i_first_offset
+ p_sidx
->i_pos
+ p_sidx
->i_size
;
4818 for( uint16_t i
=0; i
<p_data
->i_reference_count
; i
++ )
4820 if( i_time
+ p_data
->p_items
[i
].i_subsegment_duration
> i_target_time
)
4822 *pi_sampletime
= MP4_rescale_mtime( i_time
, p_data
->i_timescale
);
4823 *pi_moof_pos
= i_pos
;
4826 i_pos
+= p_data
->p_items
[i
].i_referenced_size
;
4827 i_time
+= p_data
->p_items
[i
].i_subsegment_duration
;
4830 return VLC_EGENERIC
;
4833 static int FragGetMoofByTfraIndex( demux_t
*p_demux
, const vlc_tick_t i_target_time
, unsigned i_track_ID
,
4834 uint64_t *pi_moof_pos
, vlc_tick_t
*pi_sampletime
)
4836 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4837 MP4_Box_t
*p_tfra
= MP4_BoxGet( p_sys
->p_root
, "mfra/tfra" );
4838 for( ; p_tfra
; p_tfra
= p_tfra
->p_next
)
4840 if ( p_tfra
->i_type
== ATOM_tfra
)
4842 const MP4_Box_data_tfra_t
*p_data
= BOXDATA(p_tfra
);
4843 if( !p_data
|| p_data
->i_track_ID
!= i_track_ID
)
4847 mp4_track_t
*p_track
= MP4_GetTrackByTrackID( p_demux
, p_data
->i_track_ID
);
4850 stime_t i_track_target_time
= MP4_rescale_qtime( i_target_time
, p_track
->i_timescale
);
4851 for ( uint32_t i
= 0; i
<p_data
->i_number_of_entries
; i
+= ( p_data
->i_version
== 1 ) ? 2 : 1 )
4855 if ( p_data
->i_version
== 1 )
4857 i_time
= *((uint64_t *)(p_data
->p_time
+ i
));
4858 i_offset
= *((uint64_t *)(p_data
->p_moof_offset
+ i
));
4862 i_time
= p_data
->p_time
[i
];
4863 i_offset
= p_data
->p_moof_offset
[i
];
4866 if ( i_time
>= i_track_target_time
)
4868 if ( i_pos
== 0 ) /* Not in this traf */
4871 *pi_moof_pos
= i_pos
;
4872 *pi_sampletime
= MP4_rescale_mtime( i_time
, p_track
->i_timescale
);
4881 return VLC_EGENERIC
;
4884 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t
*p_moov
,
4885 const MP4_Box_data_tfhd_t
*p_tfhd_data
,
4886 uint32_t *pi_default_size
,
4887 uint32_t *pi_default_duration
)
4889 if( p_tfhd_data
->i_flags
& MP4_TFHD_DFLT_SAMPLE_DURATION
)
4890 *pi_default_duration
= p_tfhd_data
->i_default_sample_duration
;
4892 if( p_tfhd_data
->i_flags
& MP4_TFHD_DFLT_SAMPLE_SIZE
)
4893 *pi_default_size
= p_tfhd_data
->i_default_sample_size
;
4895 if( !*pi_default_duration
|| !*pi_default_size
)
4897 const MP4_Box_t
*p_trex
= MP4_GetTrexByTrackID( p_moov
, p_tfhd_data
->i_track_ID
);
4900 if ( !*pi_default_duration
)
4901 *pi_default_duration
= BOXDATA(p_trex
)->i_default_sample_duration
;
4902 if ( !*pi_default_size
)
4903 *pi_default_size
= BOXDATA(p_trex
)->i_default_sample_size
;
4908 static int DemuxFrag( demux_t
*p_demux
)
4910 demux_sys_t
*p_sys
= p_demux
->p_sys
;
4911 unsigned i_track_selected
= 0;
4912 int i_status
= VLC_DEMUXER_SUCCESS
;
4914 if( unlikely(p_sys
->b_error
) )
4916 msg_Warn( p_demux
, "unrecoverable error" );
4917 i_status
= VLC_DEMUXER_EOF
;
4921 /* check for newly selected/unselected track */
4922 for( unsigned i_track
= 0; i_track
< p_sys
->i_tracks
; i_track
++ )
4924 mp4_track_t
*tk
= &p_sys
->track
[i_track
];
4927 if( !tk
->b_ok
|| tk
->b_chapters_source
)
4930 if( p_sys
->b_seekable
)
4931 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
, tk
->p_es
, &b
);
4933 if(tk
->b_selected
!= b
)
4935 msg_Dbg( p_demux
, "track %u %s!", tk
->i_track_ID
, b
? "enabled" : "disabled" );
4936 MP4_TrackSelect( p_demux
, tk
, b
);
4939 if( tk
->b_selected
)
4943 if( i_track_selected
<= 0 )
4945 msg_Warn( p_demux
, "no track selected, exiting..." );
4946 i_status
= VLC_DEMUXER_EOF
;
4950 if ( p_sys
->context
.i_current_box_type
!= ATOM_mdat
)
4952 /* Othewise mdat is skipped. FIXME: mdat reading ! */
4953 const uint8_t *p_peek
;
4954 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 8 ) != 8 )
4956 i_status
= VLC_DEMUXER_EOF
;
4960 p_sys
->context
.i_current_box_type
= VLC_FOURCC( p_peek
[4], p_peek
[5], p_peek
[6], p_peek
[7] );
4961 if( p_sys
->context
.i_current_box_type
!= ATOM_moof
&&
4962 p_sys
->context
.i_current_box_type
!= ATOM_moov
)
4964 uint64_t i_pos
= vlc_stream_Tell( p_demux
->s
);
4965 uint64_t i_size
= GetDWBE( p_peek
);
4968 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 16 ) != 16 )
4970 i_status
= VLC_DEMUXER_EOF
;
4973 i_size
= GetQWBE( p_peek
+ 8 );
4976 if( UINT64_MAX
- i_pos
< i_size
)
4978 i_status
= VLC_DEMUXER_EOF
;
4982 if( p_sys
->context
.i_current_box_type
== ATOM_mdat
)
4984 /* We'll now read mdat using context atom,
4985 * but we'll need post mdat offset, as we'll never seek backward */
4986 p_sys
->context
.i_post_mdat_offset
= i_pos
+ i_size
;
4988 else if( MP4_Seek( p_demux
->s
, i_pos
+ i_size
) != VLC_SUCCESS
) /* skip other atoms */
4990 i_status
= VLC_DEMUXER_EOF
;
4996 MP4_Box_t
*p_vroot
= MP4_BoxGetNextChunk( p_demux
->s
);
4999 i_status
= VLC_DEMUXER_EOF
;
5003 MP4_Box_t
*p_box
= NULL
;
5004 for( p_box
= p_vroot
->p_first
; p_box
; p_box
= p_box
->p_next
)
5006 if( p_box
->i_type
== ATOM_moof
||
5007 p_box
->i_type
== ATOM_moov
)
5013 FragResetContext( p_sys
);
5015 if( p_box
->i_type
== ATOM_moov
)
5017 p_sys
->context
.p_fragment_atom
= p_sys
->p_moov
;
5021 p_sys
->context
.p_fragment_atom
= MP4_BoxExtract( &p_vroot
->p_first
, p_box
->i_type
);
5023 /* Detect and Handle Passive Seek */
5024 const uint32_t i_sequence_number
= FragGetMoofSequenceNumber( p_sys
->context
.p_fragment_atom
);
5025 const bool b_discontinuity
= ( i_sequence_number
!= p_sys
->context
.i_lastseqnumber
+ 1 );
5026 if( b_discontinuity
)
5027 msg_Info( p_demux
, "Fragment sequence discontinuity detected %"PRIu32
" != %"PRIu32
,
5028 i_sequence_number
, p_sys
->context
.i_lastseqnumber
+ 1 );
5029 p_sys
->context
.i_lastseqnumber
= i_sequence_number
;
5032 if( FragPrepareChunk( p_demux
, p_sys
->context
.p_fragment_atom
,
5033 MP4_BoxGet( p_vroot
, "sidx"), INVALID_SEGMENT_TIME
,
5034 b_discontinuity
) != VLC_SUCCESS
)
5036 MP4_BoxFree( p_vroot
);
5037 i_status
= VLC_DEMUXER_EOF
;
5041 if( b_discontinuity
)
5043 p_sys
->i_nztime
= FragGetDemuxTimeFromTracksTime( p_sys
);
5044 p_sys
->i_pcr
= VLC_TICK_INVALID
;
5046 /* !Prepare chunk */
5049 p_sys
->context
.i_current_box_type
= p_box
->i_type
;
5052 MP4_BoxFree( p_vroot
);
5054 if( p_sys
->context
.p_fragment_atom
== NULL
)
5056 msg_Info(p_demux
, "no moof or moov in current chunk");
5057 return VLC_DEMUXER_SUCCESS
;
5062 if ( p_sys
->context
.i_current_box_type
== ATOM_mdat
)
5064 assert(p_sys
->context
.p_fragment_atom
);
5066 if ( p_sys
->context
.p_fragment_atom
)
5067 switch( p_sys
->context
.p_fragment_atom
->i_type
)
5069 case ATOM_moov
://[ftyp/moov, mdat]+ -> [moof, mdat]+
5070 i_status
= DemuxMoov( p_demux
);
5073 i_status
= DemuxMoof( p_demux
);
5076 msg_Err( p_demux
, "fragment type %4.4s", (char*) &p_sys
->context
.p_fragment_atom
->i_type
);
5080 if( i_status
== VLC_DEMUXER_EOS
)
5082 i_status
= VLC_DEMUXER_SUCCESS
;
5083 /* Skip if we didn't reach the end of mdat box */
5084 uint64_t i_pos
= vlc_stream_Tell( p_demux
->s
);
5085 if( i_pos
!= p_sys
->context
.i_post_mdat_offset
&& i_status
!= VLC_DEMUXER_EOF
)
5087 if( i_pos
> p_sys
->context
.i_post_mdat_offset
)
5088 msg_Err( p_demux
, " Overread mdat by %" PRIu64
, i_pos
- p_sys
->context
.i_post_mdat_offset
);
5090 msg_Warn( p_demux
, "mdat had still %"PRIu64
" bytes unparsed as samples",
5091 p_sys
->context
.i_post_mdat_offset
- i_pos
);
5092 if( MP4_Seek( p_demux
->s
, p_sys
->context
.i_post_mdat_offset
) != VLC_SUCCESS
)
5093 i_status
= VLC_DEMUXER_EGENERIC
;
5095 p_sys
->context
.i_current_box_type
= 0;
5101 if( i_status
== VLC_DEMUXER_EOF
)
5103 vlc_tick_t i_demux_end
= INT64_MIN
;
5104 for( unsigned i
= 0; i
< p_sys
->i_tracks
; i
++ )
5106 const mp4_track_t
*tk
= &p_sys
->track
[i
];
5107 vlc_tick_t i_track_end
= MP4_rescale_mtime( tk
->i_time
, tk
->i_timescale
);
5108 if( i_track_end
> i_demux_end
)
5109 i_demux_end
= i_track_end
;
5111 if( i_demux_end
!= INT64_MIN
)
5112 es_out_SetPCR( p_demux
->out
, VLC_TICK_0
+ i_demux_end
);
5119 inline static mp4_track_t
*MP4ASF_GetTrack( asf_packet_sys_t
*p_packetsys
,
5120 uint8_t i_stream_number
)
5122 demux_sys_t
*p_sys
= p_packetsys
->p_demux
->p_sys
;
5123 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
5125 if ( p_sys
->track
[i
].p_asf
&&
5126 i_stream_number
== p_sys
->track
[i
].BOXDATA(p_asf
)->i_stream_number
)
5128 return &p_sys
->track
[i
];
5134 static asf_track_info_t
* MP4ASF_GetTrackInfo( asf_packet_sys_t
*p_packetsys
,
5135 uint8_t i_stream_number
)
5137 mp4_track_t
*p_track
= MP4ASF_GetTrack( p_packetsys
, i_stream_number
);
5139 return &p_track
->asfinfo
;
5144 static void MP4ASF_Send( asf_packet_sys_t
*p_packetsys
, uint8_t i_stream_number
,
5145 block_t
**pp_frame
)
5147 mp4_track_t
*p_track
= MP4ASF_GetTrack( p_packetsys
, i_stream_number
);
5150 block_Release( *pp_frame
);
5154 block_t
*p_gather
= block_ChainGather( *pp_frame
);
5155 p_gather
->i_dts
= p_track
->i_dts_backup
;
5156 p_gather
->i_pts
= p_track
->i_pts_backup
;
5157 es_out_Send( p_packetsys
->p_demux
->out
, p_track
->p_es
, p_gather
);
5163 static void MP4ASF_ResetFrames( demux_sys_t
*p_sys
)
5165 for ( unsigned int i
=0; i
<p_sys
->i_tracks
; i
++ )
5167 mp4_track_t
*p_track
= &p_sys
->track
[i
];
5168 if( p_track
->asfinfo
.p_frame
)
5170 block_ChainRelease( p_track
->asfinfo
.p_frame
);
5171 p_track
->asfinfo
.p_frame
= NULL
;