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 bool seekpoint_changed;
128 input_title_t *p_title;
132 asf_packet_sys_t asfpacketsys;
133 vlc_tick_t i_preroll; /* foobar */
134 vlc_tick_t i_preroll_start;
141 mp4_fragments_index_t *p_fragsindex;
144 #define DEMUX_INCREMENT VLC_TICK_FROM_MS(250) /* How far the pcr will go, each round */
145 #define DEMUX_TRACK_MAX_PRELOAD VLC_TICK_FROM_SEC(15) /* maximum preloading, to deal with interleaving */
147 #define INVALID_PRELOAD UINT_MAX
149 #define VLC_DEMUXER_EOS (VLC_DEMUXER_EGENERIC - 1)
150 #define VLC_DEMUXER_FATAL (VLC_DEMUXER_EGENERIC - 2)
152 const uint32_t rgi_pict_atoms[2] = { ATOM_PICT, ATOM_pict };
153 const char *psz_meta_roots[] = { "/moov/udta/meta/ilst",
161 /*****************************************************************************
162 * Declaration of local function
163 *****************************************************************************/
164 static void MP4_TrackSetup( demux_t *, mp4_track_t *, MP4_Box_t *, bool, bool );
165 static void MP4_TrackInit( mp4_track_t *, const MP4_Box_t * );
166 static void MP4_TrackClean( es_out_t *, mp4_track_t * );
168 static void MP4_Block_Send( demux_t *, mp4_track_t *, block_t * );
170 static void MP4_TrackSelect ( demux_t *, mp4_track_t *, bool );
171 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, vlc_tick_t );
173 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
174 static uint32_t MP4_TrackGetReadSize( mp4_track_t *, uint32_t * );
175 static int MP4_TrackNextSample( demux_t *, mp4_track_t *, uint32_t );
176 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, vlc_tick_t );
178 static void MP4_UpdateSeekpoint( demux_t *, vlc_tick_t );
180 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id );
181 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t *p_moov,
182 const MP4_Box_data_tfhd_t *p_tfhd_data,
183 uint32_t *pi_default_size,
184 uint32_t *pi_default_duration );
186 static stime_t GetMoovTrackDuration( demux_sys_t *p_sys, unsigned i_track_ID );
188 static int ProbeFragments( demux_t *p_demux, bool b_force, bool *pb_fragmented );
189 static int ProbeFragmentsChecked( demux_t *p_demux );
190 static int ProbeIndex( demux_t *p_demux );
192 static int FragCreateTrunIndex( demux_t *, MP4_Box_t *, MP4_Box_t *, stime_t );
194 static int FragGetMoofBySidxIndex( demux_t *p_demux, vlc_tick_t i_target_time,
195 uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime );
196 static int FragGetMoofByTfraIndex( demux_t *p_demux, const vlc_tick_t i_target_time, unsigned i_track_ID,
197 uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime );
198 static void FragResetContext( demux_sys_t * );
201 static asf_track_info_t * MP4ASF_GetTrackInfo( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number );
202 static void MP4ASF_Send(asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, block_t **pp_frame);
203 static void MP4ASF_ResetFrames( demux_sys_t *p_sys );
206 static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec );
207 static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount );
209 static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta );
213 static int64_t MP4_rescale( int64_t i_value, uint32_t i_timescale, uint32_t i_newscale )
215 if( i_timescale == i_newscale )
218 if( i_value <= INT64_MAX / i_newscale )
219 return i_value * i_newscale / i_timescale;
222 int64_t q = i_value / i_timescale;
223 int64_t r = i_value % i_timescale;
224 return q * i_newscale + r * i_newscale / i_timescale;
227 static vlc_tick_t MP4_rescale_mtime( int64_t i_value, uint32_t i_timescale )
229 return MP4_rescale(i_value, i_timescale, CLOCK_FREQ);
232 static int64_t MP4_rescale_qtime( vlc_tick_t i_value, uint32_t i_timescale )
234 return MP4_rescale(i_value, CLOCK_FREQ, i_timescale);
237 static uint32_t stream_ReadU32( stream_t *s, void *p_read, uint32_t i_toread )
239 ssize_t i_return = 0;
240 if ( i_toread > INT32_MAX )
242 i_return = vlc_stream_Read( s, p_read, (size_t) INT32_MAX );
243 if ( i_return < INT32_MAX )
246 i_toread -= INT32_MAX;
248 i_return += vlc_stream_Read( s, (uint8_t *)p_read + i_return, (size_t) i_toread );
252 static inline bool MP4_isMetadata(const mp4_track_t *tk)
254 return tk->as_reftype != 0 && tk->as_reftype != ATOM_subt;
257 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
261 MP4_Box_t *p_trex = MP4_BoxGet( p_moov, "mvex/trex" );
264 if ( p_trex->i_type == ATOM_trex &&
265 BOXDATA(p_trex) && BOXDATA(p_trex)->i_track_ID == i_id )
268 p_trex = p_trex->p_next;
274 * Return the track identified by tid
276 static mp4_track_t *MP4_GetTrackByTrackID( demux_t *p_demux, const uint32_t tid )
278 demux_sys_t *p_sys = p_demux->p_sys;
280 mp4_track_t *ret = NULL;
281 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
283 ret = &p_sys->track[i];
284 if( ret->i_track_ID == tid )
290 static MP4_Box_t * MP4_GetTrakByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
292 MP4_Box_t *p_trak = MP4_BoxGet( p_moov, "trak" );
296 if( p_trak->i_type == ATOM_trak &&
297 (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) && BOXDATA(p_tkhd) &&
298 BOXDATA(p_tkhd)->i_track_ID == i_id )
301 p_trak = p_trak->p_next;
306 static MP4_Box_t * MP4_GetTrafByTrackID( MP4_Box_t *p_moof, const uint32_t i_id )
308 MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
312 if( p_traf->i_type == ATOM_traf &&
313 (p_tfhd = MP4_BoxGet( p_traf, "tfhd" )) && BOXDATA(p_tfhd) &&
314 BOXDATA(p_tfhd)->i_track_ID == i_id )
317 p_traf = p_traf->p_next;
322 static es_out_id_t * MP4_AddTrackES( es_out_t *out, mp4_track_t *p_track )
324 es_out_id_t *p_es = es_out_Add( out, &p_track->fmt );
325 /* Force SPU which isn't selected/defaulted */
326 if( p_track->fmt.i_cat == SPU_ES && p_es && p_track->b_forced_spu )
327 es_out_Control( out, ES_OUT_SET_ES_DEFAULT, p_es );
332 /* Return time in microsecond of a track */
333 static inline vlc_tick_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
335 demux_sys_t *p_sys = p_demux->p_sys;
336 const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
338 unsigned int i_index = 0;
339 unsigned int i_sample = p_track->i_sample - p_chunk->i_sample_first;
340 int64_t sdts = p_chunk->i_first_dts;
342 while( i_sample > 0 && i_index < p_chunk->i_entries_dts )
344 if( i_sample > p_chunk->p_sample_count_dts[i_index] )
346 sdts += p_chunk->p_sample_count_dts[i_index] *
347 p_chunk->p_sample_delta_dts[i_index];
348 i_sample -= p_chunk->p_sample_count_dts[i_index];
353 sdts += i_sample * p_chunk->p_sample_delta_dts[i_index];
358 vlc_tick_t i_dts = MP4_rescale_mtime( sdts, p_track->i_timescale );
360 /* now handle elst */
361 if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count )
363 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
365 /* convert to offset */
366 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
367 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
368 elst->i_media_time[p_track->i_elst] > 0 )
370 i_dts -= MP4_rescale_mtime( elst->i_media_time[p_track->i_elst], p_track->i_timescale );
373 /* add i_elst_time */
374 i_dts += MP4_rescale_mtime( p_track->i_elst_time, p_sys->i_timescale );
376 if( i_dts < 0 ) i_dts = 0;
382 static inline bool MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track,
383 vlc_tick_t *pi_delta )
385 VLC_UNUSED( p_demux );
386 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
388 unsigned int i_index = 0;
389 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
391 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
394 for( i_index = 0; i_index < ck->i_entries_pts ; i_index++ )
396 if( i_sample < ck->p_sample_count_pts[i_index] )
398 *pi_delta = MP4_rescale_mtime( ck->p_sample_offset_pts[i_index],
399 p_track->i_timescale );
403 i_sample -= ck->p_sample_count_pts[i_index];
408 static inline vlc_tick_t MP4_GetSamplesDuration( demux_t *p_demux, mp4_track_t *p_track,
409 unsigned i_nb_samples )
411 VLC_UNUSED( p_demux );
413 const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
414 stime_t i_duration = 0;
416 /* Forward to right index, and set remaining count in that index */
417 unsigned i_index = 0;
418 unsigned i_remain = 0;
419 for( unsigned i = p_chunk->i_sample_first;
420 i<p_track->i_sample && i_index < p_chunk->i_entries_dts; )
422 if( p_track->i_sample - i >= p_chunk->p_sample_count_dts[i_index] )
424 i += p_chunk->p_sample_count_dts[i_index];
429 i_remain = p_track->i_sample - i;
434 /* Compute total duration from all samples from index */
435 while( i_nb_samples > 0 && i_index < p_chunk->i_entries_dts )
437 if( i_nb_samples >= p_chunk->p_sample_count_dts[i_index] - i_remain )
439 i_duration += (p_chunk->p_sample_count_dts[i_index] - i_remain) *
440 (int64_t) p_chunk->p_sample_delta_dts[i_index];
441 i_nb_samples -= (p_chunk->p_sample_count_dts[i_index] - i_remain);
447 i_duration += i_nb_samples * p_chunk->p_sample_delta_dts[i_index];
452 return MP4_rescale_mtime( i_duration, p_track->i_timescale );
455 static inline vlc_tick_t MP4_GetMoviePTS(demux_sys_t *p_sys )
457 return p_sys->i_nztime;
460 static void LoadChapter( demux_t *p_demux );
462 static int LoadInitFrag( demux_t *p_demux )
464 demux_sys_t *p_sys = p_demux->p_sys;
466 /* Load all boxes ( except raw data ) */
467 MP4_Box_t *p_root = MP4_BoxGetRoot( p_demux->s );
468 if( p_root == NULL || !MP4_BoxGet( p_root, "/moov" ) )
470 MP4_BoxFree( p_root );
471 goto LoadInitFragError;
474 p_sys->p_root = p_root;
479 msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" );
483 static int CreateTracks( demux_t *p_demux, unsigned i_tracks )
485 demux_sys_t *p_sys = p_demux->p_sys;
487 if( SIZE_MAX / i_tracks < sizeof(mp4_track_t) )
490 p_sys->track = vlc_alloc( i_tracks, sizeof(mp4_track_t) );
491 if( p_sys->track == NULL )
493 p_sys->i_tracks = i_tracks;
495 for( unsigned i=0; i<i_tracks; i++ )
497 MP4_TrackInit( &p_sys->track[i],
498 MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i ) );
504 static block_t * MP4_EIA608_Convert( block_t * p_block )
506 /* Rebuild codec data from encap */
508 size_t i_remaining = __MIN(p_block->i_buffer, INT64_MAX / 3);
509 uint32_t i_bytes = 0;
512 /* always need at least 10 bytes (atom size+header+1pair)*/
513 i_bytes = GetDWBE(p_block->p_buffer);
515 if (10 < i_bytes || i_bytes < i_remaining ||
516 memcmp("cdat", &p_block->p_buffer[4], 4) ||
517 (p_newblock = block_Alloc(i_remaining * 3 - 8)) == NULL)
519 p_block->i_buffer = 0;
523 uint8_t *p_write = p_newblock->p_buffer;
524 uint8_t *p_read = &p_block->p_buffer[8];
530 p_write[i_copied++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
531 p_write[i_copied++] = p_read[0];
532 p_write[i_copied++] = p_read[1];
536 } while( i_bytes >= 2 );
538 /* cdt2 is optional */
539 if ( i_remaining >= 10 &&
540 (i_bytes = GetDWBE(p_read)) &&
541 (i_bytes <= i_remaining) &&
542 !memcmp("cdt2", &p_read[4], 4) )
549 p_write[i_copied++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
550 p_write[i_copied++] = p_read[0];
551 p_write[i_copied++] = p_read[1];
554 } while( i_bytes >= 2 );
557 p_newblock->i_pts = p_block->i_dts;
558 p_newblock->i_buffer = i_copied;
559 p_newblock->i_flags = BLOCK_FLAG_TYPE_P;
560 block_Release( p_block );
565 static uint32_t MP4_TrackGetRunSeq( mp4_track_t *p_track )
567 if( p_track->i_chunk_count > 0 )
568 return p_track->chunk[p_track->i_chunk].i_virtual_run_number;
572 /* Analyzes chunks to find max interleave length
573 * sets flat flag if no interleaving is in use */
574 static void MP4_GetInterleaving( demux_t *p_demux, vlc_tick_t *pi_max_contiguous, bool *pb_flat )
576 demux_sys_t *p_sys = p_demux->p_sys;
577 *pi_max_contiguous = 0;
580 /* Find first recorded chunk */
581 mp4_track_t *tk = NULL;
582 uint64_t i_duration = 0;
583 for( unsigned i=0; i < p_sys->i_tracks; i++ )
585 mp4_track_t *cur = &p_sys->track[i];
586 if( !cur->i_chunk_count )
589 if( tk == NULL || cur->chunk[0].i_offset < tk->chunk[0].i_offset )
595 i_duration += tk->chunk[tk->i_chunk].i_duration;
598 /* Find next chunk in data order */
599 mp4_track_t *nexttk = NULL;
600 for( unsigned i=0; i < p_sys->i_tracks; i++ )
602 mp4_track_t *cur = &p_sys->track[i];
603 if( cur->i_chunk == cur->i_chunk_count )
606 if( nexttk == NULL ||
607 cur->chunk[cur->i_chunk].i_offset < nexttk->chunk[nexttk->i_chunk].i_offset )
611 /* copy previous run */
612 if( nexttk && nexttk->i_chunk > 0 )
613 nexttk->chunk[nexttk->i_chunk].i_virtual_run_number =
614 nexttk->chunk[nexttk->i_chunk - 1].i_virtual_run_number;
618 vlc_tick_t i_dur = MP4_rescale_mtime( i_duration, tk->i_timescale );
619 if( i_dur > *pi_max_contiguous )
620 *pi_max_contiguous = i_dur;
623 if( tk->i_chunk != tk->i_chunk_count )
626 if( nexttk && nexttk->i_chunk > 0 ) /* new run number */
627 nexttk->chunk[nexttk->i_chunk].i_virtual_run_number++;
634 for( unsigned i=0; i < p_sys->i_tracks; i++ )
635 p_sys->track[i].i_chunk = 0;
638 static block_t * MP4_Block_Convert( demux_t *p_demux, const mp4_track_t *p_track, block_t *p_block )
640 /* might have some encap */
641 if( p_track->fmt.i_cat == SPU_ES )
643 switch( p_track->fmt.i_codec )
645 case VLC_CODEC_WEBVTT:
653 case VLC_CODEC_CEA608:
654 case VLC_CODEC_CEA708:
655 p_block = MP4_EIA608_Convert( p_block );
658 p_block->i_buffer = 0;
662 else if( p_track->fmt.i_codec == VLC_CODEC_AV1 )
664 p_block = AV1_Unpack_Sample( p_block );
666 else if( p_track->fmt.i_original_fourcc == ATOM_rrtp )
668 p_block = MP4_RTPHint_Convert( p_demux, p_block, p_track->fmt.i_codec );
674 static void MP4_Block_Send( demux_t *p_demux, mp4_track_t *p_track, block_t *p_block )
676 demux_sys_t *p_sys = p_demux->p_sys;
678 p_block = MP4_Block_Convert( p_demux, p_track, p_block );
679 if( p_block == NULL )
682 if ( p_track->b_chans_reorder )
684 aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
685 p_track->fmt.audio.i_channels,
686 p_track->rgi_chans_reordering,
687 p_track->fmt.i_codec );
690 p_block->i_flags |= p_track->i_block_flags;
691 if( p_track->i_next_block_flags )
693 p_block->i_flags |= p_track->i_next_block_flags;
694 p_track->i_next_block_flags = 0;
697 /* ASF packets in mov */
700 /* Fake a new stream from MP4 block */
701 stream_t *p_stream = p_demux->s;
702 p_demux->s = vlc_stream_MemoryNew( p_demux, p_block->p_buffer, p_block->i_buffer, true );
705 p_track->i_dts_backup = p_block->i_dts;
706 p_track->i_pts_backup = p_block->i_pts;
707 /* And demux it as ASF packet */
708 DemuxASFPacket( &p_sys->asfpacketsys, p_block->i_buffer, p_block->i_buffer );
709 vlc_stream_Delete(p_demux->s);
711 block_Release(p_block);
712 p_demux->s = p_stream;
715 es_out_Send( p_demux->out, p_track->p_es, p_block );
718 int OpenHEIF ( vlc_object_t * );
719 void CloseHEIF( vlc_object_t * );
721 /*****************************************************************************
722 * Open: check file and initializes MP4 structures
723 *****************************************************************************/
724 static int Open( vlc_object_t * p_this )
726 demux_t *p_demux = (demux_t *)p_this;
729 const uint8_t *p_peek;
732 const MP4_Box_t *p_mvhd = NULL;
733 const MP4_Box_t *p_mvex = NULL;
737 /* A little test to see if it could be a mp4 */
738 if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 ) return VLC_EGENERIC;
740 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
751 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
755 /* Early handle some brands */
756 switch( VLC_FOURCC(p_peek[8], p_peek[9], p_peek[10], p_peek[11]) )
758 /* HEIF pictures goes to heif demux */
765 /* We don't yet support f4v, but avformat does. */
777 /* create our structure that will contains all data */
778 p_sys = calloc( 1, sizeof( demux_sys_t ) );
783 vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
784 if( p_sys->b_seekable )
785 vlc_stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable );
787 /*Set exported functions */
788 p_demux->pf_demux = Demux;
789 p_demux->pf_control = Control;
791 p_sys->context.i_lastseqnumber = UINT32_MAX;
793 p_demux->p_sys = p_sys;
795 if( LoadInitFrag( p_demux ) != VLC_SUCCESS )
798 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
800 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
802 switch( BOXDATA(p_ftyp)->i_major_brand )
806 "ISO Media (isom) version %d.",
807 BOXDATA(p_ftyp)->i_minor_version );
813 msg_Dbg( p_demux, "3GPP Media Release: %4.4s",
814 (char *)&BOXDATA(p_ftyp)->i_major_brand );
817 msg_Dbg( p_demux, "Apple QuickTime media" );
820 msg_Dbg( p_demux, "PIFF (= isml = fMP4) media" );
823 msg_Dbg( p_demux, "DASH Stream" );
826 msg_Dbg( p_demux, "iTunes audio" );
827 if( var_InheritBool( p_demux, CFG_PREFIX"m4a-audioonly" ) )
828 p_sys->hacks.es_cat_filters = AUDIO_ES;
832 "unrecognized major media specification (%4.4s).",
833 (char*)&BOXDATA(p_ftyp)->i_major_brand );
836 /* also lookup in compatibility list */
837 for(uint32_t i=0; i<BOXDATA(p_ftyp)->i_compatible_brands_count; i++)
839 if (BOXDATA(p_ftyp)->i_compatible_brands[i] == BRAND_dash)
841 msg_Dbg( p_demux, "DASH Stream" );
843 else if (BOXDATA(p_ftyp)->i_compatible_brands[i] == BRAND_smoo)
845 msg_Dbg( p_demux, "Handling VLC Smooth Stream" );
851 msg_Dbg( p_demux, "file type box missing (assuming ISO Media)" );
854 /* the file need to have one moov box */
855 p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/moov" );
856 if( unlikely(!p_sys->p_moov) )
858 p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/foov" );
861 msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
864 /* we have a free box as a moov, rename it */
865 p_sys->p_moov->i_type = ATOM_moov;
868 p_mvhd = MP4_BoxGet( p_sys->p_moov, "mvhd" );
869 if( p_mvhd && BOXDATA(p_mvhd) && BOXDATA(p_mvhd)->i_timescale )
871 p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
872 p_sys->i_moov_duration = p_sys->i_duration = BOXDATA(p_mvhd)->i_duration;
873 p_sys->i_cumulated_duration = BOXDATA(p_mvhd)->i_duration;
877 msg_Warn( p_demux, "No valid mvhd found" );
881 MP4_Box_t *p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" );
882 if( p_rmra != NULL && p_demux->p_input_item != NULL )
884 int i_count = MP4_BoxCount( p_rmra, "rmda" );
887 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
889 input_item_t *p_current = p_demux->p_input_item;
891 input_item_node_t *p_subitems = input_item_node_Create( p_current );
893 for( i = 0; i < i_count; i++ )
895 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
899 if( !p_rdrf || !BOXDATA(p_rdrf) || !( psz_ref = strdup( BOXDATA(p_rdrf)->psz_ref ) ) )
903 i_ref_type = BOXDATA(p_rdrf)->i_ref_type;
905 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
906 psz_ref, (char*)&i_ref_type );
908 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
910 if( strstr( psz_ref, "qt5gateQT" ) )
912 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
916 if( !strncmp( psz_ref, "http://", 7 ) ||
917 !strncmp( psz_ref, "rtsp://", 7 ) )
923 char *psz_absolute = vlc_uri_resolve( p_demux->psz_url,
926 if( psz_absolute == NULL )
928 input_item_node_Delete( p_subitems );
931 psz_ref = psz_absolute;
933 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
934 input_item_t *p_item = input_item_New( psz_ref, NULL );
935 input_item_node_AppendItem( p_subitems, p_item );
936 input_item_Release( p_item );
940 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
941 (char*)&BOXDATA(p_rdrf)->i_ref_type );
946 /* FIXME: create a stream_filter sub-module for this */
947 if (es_out_Control(p_demux->out, ES_OUT_POST_SUBNODE, p_subitems))
948 input_item_node_Delete(p_subitems);
951 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
955 msg_Err( p_demux, "cannot find /moov/mvhd" );
960 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
961 p_demux->pf_demux = DemuxRef;
967 p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
968 if( p_sys->i_timescale == 0 )
970 msg_Err( p_this, "bad timescale" );
975 const unsigned i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" );
978 msg_Err( p_demux, "cannot find any /moov/trak" );
981 msg_Dbg( p_demux, "found %u track%c", i_tracks, i_tracks ? 's':' ' );
983 if( CreateTracks( p_demux, i_tracks ) != VLC_SUCCESS )
986 /* set referenced tracks and
987 * check that at least 1 stream is enabled */
988 b_enabled_es = false;
989 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
991 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
993 /* Enabled check as explained above */
994 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
995 if( p_tkhd && BOXDATA(p_tkhd) && (BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED) )
998 /* Referenced tracks checks */
999 MP4_Box_t *p_tref = MP4_BoxGet( p_trak, "tref" );
1003 for( MP4_Box_t *p_refbox = p_tref->p_first; p_refbox; p_refbox = p_refbox->p_next )
1005 const MP4_Box_data_trak_reference_t *refdata = p_refbox->data.p_track_reference;
1006 if(unlikely(!refdata))
1009 /* Assign reference types */
1010 for( uint32_t j = 0; j < refdata->i_entry_count; j++ )
1012 mp4_track_t *reftk = MP4_GetTrackByTrackID(p_demux, refdata->i_track_ID[j]);
1014 reftk->as_reftype = p_refbox->i_type;
1019 /* Set and store metadata */
1020 if( (p_sys->p_meta = vlc_meta_New()) )
1021 MP4_LoadMeta( p_sys, p_sys->p_meta );
1023 /* now process each track and extract all useful information */
1024 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
1026 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%u]", i );
1027 MP4_TrackSetup( p_demux, &p_sys->track[i], p_trak, true, !b_enabled_es );
1029 if( p_sys->track[i].b_ok && !p_sys->track[i].as_reftype )
1031 const char *psz_cat;
1032 switch( p_sys->track[i].fmt.i_cat )
1041 psz_cat = "subtitle";
1045 psz_cat = "unknown";
1049 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
1050 p_sys->track[i].i_track_ID, psz_cat,
1051 p_sys->track[i].b_enable ? "enable":"disable",
1052 p_sys->track[i].fmt.psz_language ?
1053 p_sys->track[i].fmt.psz_language : "undef" );
1055 else if( p_sys->track[i].b_ok && p_sys->track[i].as_reftype == ATOM_chap )
1057 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
1058 p_sys->track[i].i_track_ID,
1059 p_sys->track[i].fmt.psz_language ?
1060 p_sys->track[i].fmt.psz_language : "undef" );
1064 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
1065 p_sys->track[i].i_track_ID );
1069 p_mvex = MP4_BoxGet( p_sys->p_moov, "mvex" );
1070 if( p_mvex != NULL )
1072 const MP4_Box_t *p_mehd = MP4_BoxGet( p_mvex, "mehd");
1073 if ( p_mehd && BOXDATA(p_mehd) )
1075 if( BOXDATA(p_mehd)->i_fragment_duration > p_sys->i_duration )
1077 p_sys->b_fragmented = true;
1078 p_sys->i_duration = BOXDATA(p_mehd)->i_fragment_duration;
1082 const MP4_Box_t *p_sidx = MP4_BoxGet( p_sys->p_root, "sidx");
1084 p_sys->b_fragmented = true;
1086 if ( p_sys->b_seekable )
1088 if( !p_sys->b_fragmented /* as unknown */ )
1090 /* Probe remaining to check if there's really fragments
1091 or if that file is just ready to append fragments */
1092 ProbeFragments( p_demux, (p_sys->i_duration == 0), &p_sys->b_fragmented );
1095 if( vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos ) != VLC_SUCCESS )
1098 else /* Handle as fragmented by default as we can't see moof */
1100 p_sys->context.p_fragment_atom = p_sys->p_moov;
1101 p_sys->context.i_current_box_type = ATOM_moov;
1102 p_sys->b_fragmented = true;
1106 if( p_sys->b_fragmented )
1108 p_demux->pf_demux = DemuxFrag;
1109 msg_Dbg( p_demux, "Set Fragmented demux mode" );
1112 if( !p_sys->b_seekable && p_demux->pf_demux == Demux )
1114 msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" );
1118 if( p_sys->i_tracks > 1 && !p_sys->b_fastseekable )
1120 vlc_tick_t i_max_continuity;
1122 MP4_GetInterleaving( p_demux, &i_max_continuity, &b_flat );
1124 msg_Warn( p_demux, "that media doesn't look interleaved, will need to seek");
1125 else if( i_max_continuity > DEMUX_TRACK_MAX_PRELOAD )
1126 msg_Warn( p_demux, "that media doesn't look properly interleaved, will need to seek");
1130 LoadChapter( p_demux );
1132 p_sys->asfpacketsys.p_demux = p_demux;
1133 p_sys->asfpacketsys.pi_preroll = &p_sys->i_preroll;
1134 p_sys->asfpacketsys.pi_preroll_start = &p_sys->i_preroll_start;
1135 p_sys->asfpacketsys.pf_doskip = NULL;
1136 p_sys->asfpacketsys.pf_send = MP4ASF_Send;
1137 p_sys->asfpacketsys.pf_gettrackinfo = MP4ASF_GetTrackInfo;
1138 p_sys->asfpacketsys.pf_updatetime = NULL;
1139 p_sys->asfpacketsys.pf_setaspectratio = NULL;
1144 if( vlc_stream_Tell( p_demux->s ) > 0 )
1146 if( vlc_stream_Seek( p_demux->s, 0 ) != VLC_SUCCESS )
1147 msg_Warn( p_demux, "Can't reset stream position from probing" );
1152 return VLC_EGENERIC;
1155 const unsigned int SAMPLEHEADERSIZE = 4;
1156 const unsigned int RTPPACKETSIZE = 12;
1157 const unsigned int CONSTRUCTORSIZE = 16;
1159 /*******************************************************************************
1160 * MP4_RTPHintToFrame: converts RTP Reception Hint Track sample to H.264 frame
1161 *******************************************************************************/
1162 static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount )
1164 uint8_t *p_slice = p_block->p_buffer + SAMPLEHEADERSIZE;
1165 block_t *p_newblock = NULL;
1166 size_t i_payload = 0;
1168 if( p_block->i_buffer < SAMPLEHEADERSIZE + RTPPACKETSIZE + CONSTRUCTORSIZE )
1170 msg_Err( p_demux, "Sample not large enough for necessary structs");
1171 block_Release( p_block );
1175 for( uint32_t i = 0; i < packetcount; ++i )
1177 if( (size_t)(p_slice - p_block->p_buffer) + RTPPACKETSIZE + CONSTRUCTORSIZE > p_block->i_buffer )
1180 /* skip RTP header in sample. Could be used to detect packet losses */
1181 p_slice += RTPPACKETSIZE;
1183 mp4_rtpsampleconstructor_t sample_cons;
1185 sample_cons.type = p_slice[0];
1186 sample_cons.trackrefindex = p_slice[1];
1187 sample_cons.length = GetWBE( &p_slice[2] );
1188 sample_cons.samplenumber = GetDWBE( &p_slice[4] );
1189 sample_cons.sampleoffset = GetDWBE( &p_slice[8] );
1190 sample_cons.bytesperblock = GetWBE( &p_slice[12] );
1191 sample_cons.samplesperblock = GetWBE( &p_slice[14] );
1193 /* skip packet constructor */
1194 p_slice += CONSTRUCTORSIZE;
1196 /* check that is RTPsampleconstructor, referencing itself and no weird audio stuff */
1197 if( sample_cons.type != 2||sample_cons.trackrefindex != -1
1198 ||sample_cons.samplesperblock != 1||sample_cons.bytesperblock != 1 )
1200 msg_Err(p_demux, "Unhandled constructor in RTP Reception Hint Track. Type:%u", sample_cons.type);
1204 /* slice doesn't fit in buffer */
1205 if( sample_cons.sampleoffset + sample_cons.length > p_block->i_buffer)
1207 msg_Err(p_demux, "Sample buffer is smaller than sample" );
1211 block_t *p_realloc = ( p_newblock ) ?
1212 block_Realloc( p_newblock, 0, i_payload + sample_cons.length + 4 ):
1213 block_Alloc( i_payload + sample_cons.length + 4 );
1217 p_newblock = p_realloc;
1218 uint8_t *p_dst = &p_newblock->p_buffer[i_payload];
1220 const uint8_t* p_src = p_block->p_buffer + sample_cons.sampleoffset;
1221 uint8_t i_type = (*p_src) & ((1<<5)-1);
1223 const uint8_t synccode[4] = { 0, 0, 0, 1 };
1224 if( memcmp( p_src, synccode, 4 ) )
1226 if( i_type == 7 || i_type == 8 )
1235 memcpy( p_dst, p_src, sample_cons.length );
1236 p_dst += sample_cons.length;
1238 i_payload = p_dst - p_newblock->p_buffer;
1241 block_Release( p_block );
1243 p_newblock->i_buffer = i_payload;
1247 block_Release( p_block );
1249 block_Release( p_newblock );
1253 /* RTP Reception Hint Track */
1254 static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec )
1256 block_t *p_converted = NULL;
1257 if( p_block->i_buffer < 2 )
1259 block_Release( p_block );
1263 /* number of RTP packets contained in this sample */
1264 const uint16_t i_packets = GetWBE( p_block->p_buffer );
1265 if( i_packets <= 1 || i_codec != VLC_CODEC_H264 )
1267 const size_t i_skip = SAMPLEHEADERSIZE + i_packets * ( RTPPACKETSIZE + CONSTRUCTORSIZE );
1268 if( i_packets == 1 && i_skip < p_block->i_buffer )
1270 p_block->p_buffer += i_skip;
1271 p_converted = p_block;
1275 block_Release( p_block );
1280 p_converted = MP4_RTPHintToFrame( p_demux, p_block, i_packets );
1286 static uint64_t OverflowCheck( demux_t *p_demux, mp4_track_t *tk,
1287 uint64_t i_readpos, uint64_t i_samplessize )
1289 demux_sys_t *p_sys = p_demux->p_sys;
1290 if( !p_sys->b_seekable && p_sys->b_fragmented &&
1291 p_sys->context.i_post_mdat_offset )
1293 /* avoid breaking non seekable demux */
1294 if( i_readpos + i_samplessize > p_sys->context.i_post_mdat_offset )
1296 msg_Err(p_demux, "Broken file. track[0x%x] "
1297 "Sample @%" PRIu64 " overflowing "
1298 "parent mdat by %" PRIu64,
1299 tk->i_track_ID, i_readpos,
1300 i_readpos + i_samplessize - p_sys->context.i_post_mdat_offset );
1301 i_samplessize = p_sys->context.i_post_mdat_offset - i_readpos;
1304 return i_samplessize;
1307 /*****************************************************************************
1308 * Demux: read packet and send them to decoders
1309 *****************************************************************************
1310 * TODO check for newly selected track (ie audio upt to now )
1311 *****************************************************************************/
1312 static int DemuxTrack( demux_t *p_demux, mp4_track_t *tk, uint64_t i_readpos,
1313 vlc_tick_t i_max_preload )
1315 demux_sys_t *p_sys = p_demux->p_sys;
1316 uint32_t i_nb_samples = 0;
1317 uint32_t i_samplessize = 0;
1319 if( !tk->b_ok || tk->i_sample >= tk->i_sample_count )
1320 return VLC_DEMUXER_EOS;
1322 if( tk->as_reftype == ATOM_chap )
1323 return VLC_DEMUXER_SUCCESS;
1325 uint32_t i_run_seq = MP4_TrackGetRunSeq( tk );
1326 vlc_tick_t i_current_nzdts = MP4_TrackGetDTS( p_demux, tk );
1327 const vlc_tick_t i_demux_max_nzdts =i_max_preload < INVALID_PRELOAD
1328 ? i_current_nzdts + i_max_preload
1331 for( ; i_demux_max_nzdts >= i_current_nzdts; )
1333 if( tk->i_sample >= tk->i_sample_count )
1334 return VLC_DEMUXER_EOS;
1337 msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, tk->i_track_ID,
1338 MP4_TrackGetDTS( p_demux, tk ),
1339 MP4_GetMoviePTS( p_demux->p_sys ), i_readpos );
1342 i_samplessize = MP4_TrackGetReadSize( tk, &i_nb_samples );
1343 if( i_samplessize > 0 )
1348 if( vlc_stream_Tell( p_demux->s ) != i_readpos )
1350 if( MP4_Seek( p_demux->s, i_readpos ) != VLC_SUCCESS )
1352 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1353 ": Failed to seek to %"PRIu64,
1354 tk->i_track_ID, i_readpos );
1355 MP4_TrackSelect( p_demux, tk, false );
1360 i_samplessize = OverflowCheck( p_demux, tk, i_readpos, i_samplessize );
1363 if( !(p_block = vlc_stream_Block( p_demux->s, i_samplessize )) )
1365 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1366 ": Failed to read %d bytes sample at %"PRIu64,
1367 tk->i_track_ID, i_samplessize, i_readpos );
1368 MP4_TrackSelect( p_demux, tk, false );
1372 /* !important! Ensure clock is set before sending data */
1373 if( p_sys->i_pcr == VLC_TICK_INVALID )
1375 es_out_SetPCR( p_demux->out, VLC_TICK_0 + i_current_nzdts );
1376 p_sys->i_pcr = VLC_TICK_0 + i_current_nzdts;
1380 p_block->i_dts = VLC_TICK_0 + i_current_nzdts;
1382 if( MP4_TrackGetPTSDelta( p_demux, tk, &i_delta ) )
1383 p_block->i_pts = p_block->i_dts + i_delta;
1384 else if( tk->fmt.i_cat != VIDEO_ES )
1385 p_block->i_pts = p_block->i_dts;
1387 p_block->i_pts = VLC_TICK_INVALID;
1389 p_block->i_length = MP4_GetSamplesDuration( p_demux, tk, i_nb_samples );
1391 MP4_Block_Send( p_demux, tk, p_block );
1395 if ( i_nb_samples && /* sample size could be 0, need to go fwd. see return */
1396 MP4_TrackNextSample( p_demux, tk, i_nb_samples ) )
1399 uint32_t i_next_run_seq = MP4_TrackGetRunSeq( tk );
1400 if( i_next_run_seq != i_run_seq )
1403 i_current_nzdts = MP4_TrackGetDTS( p_demux, tk );
1404 i_readpos = MP4_TrackGetPos( tk );
1407 return VLC_DEMUXER_SUCCESS;
1410 return VLC_DEMUXER_EGENERIC;
1413 static int DemuxMoov( demux_t *p_demux )
1415 demux_sys_t *p_sys = p_demux->p_sys;
1416 unsigned int i_track;
1418 /* check for newly selected/unselected track */
1419 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1421 mp4_track_t *tk = &p_sys->track[i_track];
1424 if( !tk->b_ok || MP4_isMetadata( tk ) ||
1425 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
1430 if( p_sys->b_seekable )
1431 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1433 if( tk->b_selected && !b )
1435 MP4_TrackSelect( p_demux, tk, false );
1437 else if( !tk->b_selected && b)
1439 MP4_TrackSeek( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
1443 const vlc_tick_t i_nztime = MP4_GetMoviePTS( p_sys );
1445 /* We demux/set pcr, even without selected tracks, (empty edits, ...) */
1446 if( p_sys->i_pcr != VLC_TICK_INVALID /* not after a seek */ )
1449 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1451 mp4_track_t *tk = &p_sys->track[i_track];
1452 if( !tk->b_ok || MP4_isMetadata( tk ) || tk->i_sample >= tk->i_sample_count )
1454 /* Test for EOF on each track (samples count, edit list) */
1455 b_eof &= ( i_nztime > MP4_TrackGetDTS( p_demux, tk ) );
1458 return VLC_DEMUXER_EOS;
1461 const vlc_tick_t i_max_preload = ( p_sys->b_fastseekable ) ? 0 : ( p_sys->b_seekable ) ? DEMUX_TRACK_MAX_PRELOAD : INVALID_PRELOAD;
1463 /* demux up to increment amount of data on every track, or just set pcr if empty data */
1466 mp4_track_t *tk = NULL;
1467 i_status = VLC_DEMUXER_EOS;
1469 /* First pass, find any track within our target increment, ordered by position */
1470 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1472 mp4_track_t *tk_tmp = &p_sys->track[i_track];
1473 if( !tk_tmp->b_ok || MP4_isMetadata( tk_tmp ) ||
1474 tk_tmp->i_sample >= tk_tmp->i_sample_count ||
1475 (!tk_tmp->b_selected && p_sys->b_seekable) )
1478 /* At least still have data to demux on this or next turns */
1479 i_status = VLC_DEMUXER_SUCCESS;
1481 if ( MP4_TrackGetDTS( p_demux, tk_tmp ) <= i_nztime + DEMUX_INCREMENT )
1483 if( tk == NULL || MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) )
1490 /* Second pass, refine and find any best candidate having a chunk pos closer than
1491 * current candidate (avoids seeks when increment falls between the 2) from
1492 * current position, but within extended interleave time */
1493 for( i_track = 0; i_max_preload != 0 && i_track < p_sys->i_tracks; i_track++ )
1495 mp4_track_t *tk_tmp = &p_sys->track[i_track];
1497 !tk_tmp->b_ok || MP4_isMetadata( tk_tmp ) ||
1498 (!tk_tmp->b_selected && p_sys->b_seekable) ||
1499 tk_tmp->i_sample >= tk_tmp->i_sample_count )
1502 vlc_tick_t i_nzdts = MP4_TrackGetDTS( p_demux, tk_tmp );
1503 if ( i_nzdts <= i_nztime + DEMUX_TRACK_MAX_PRELOAD )
1505 /* Found a better candidate to avoid seeking */
1506 if( MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) )
1508 /* Note: previous candidate will be repicked on next loop */
1512 uint64_t i_pos = MP4_TrackGetPos( tk );
1513 int i_ret = DemuxTrack( p_demux, tk, i_pos, i_max_preload );
1515 if( i_ret == VLC_DEMUXER_SUCCESS )
1516 i_status = VLC_DEMUXER_SUCCESS;
1519 if( i_status != VLC_DEMUXER_SUCCESS || !tk )
1523 p_sys->i_nztime += DEMUX_INCREMENT;
1524 if( p_sys->i_pcr != VLC_TICK_INVALID )
1526 p_sys->i_pcr = VLC_TICK_0 + p_sys->i_nztime;
1527 es_out_SetPCR( p_demux->out, p_sys->i_pcr );
1531 MP4_UpdateSeekpoint( p_demux, i_nztime + DEMUX_INCREMENT );
1536 static int Demux( demux_t *p_demux )
1538 demux_sys_t *p_sys = p_demux->p_sys;
1540 assert( ! p_sys->b_fragmented );
1542 int i_status = DemuxMoov( p_demux );
1544 if( i_status == VLC_DEMUXER_EOS )
1545 i_status = VLC_DEMUXER_EOF;
1550 static void MP4_UpdateSeekpoint( demux_t *p_demux, vlc_tick_t i_time )
1552 demux_sys_t *p_sys = p_demux->p_sys;
1554 if( !p_sys->p_title )
1556 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
1558 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
1563 if( i != p_sys->i_seekpoint && i >= 0 )
1565 p_sys->i_seekpoint = i;
1566 p_sys->seekpoint_changed = true;
1569 /*****************************************************************************
1570 * Seek: Go to i_date
1571 ******************************************************************************/
1572 static int Seek( demux_t *p_demux, vlc_tick_t i_date, bool b_accurate )
1574 demux_sys_t *p_sys = p_demux->p_sys;
1575 unsigned int i_track;
1577 /* Now for each stream try to go to this time */
1578 vlc_tick_t i_start = i_date;
1579 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1581 mp4_track_t *tk = &p_sys->track[i_track];
1582 /* FIXME: we should find the lowest time from tracks with indexes.
1583 considering only video for now */
1584 if( tk->fmt.i_cat != VIDEO_ES || MP4_isMetadata( tk ) || !tk->b_ok )
1586 if( MP4_TrackSeek( p_demux, tk, i_date ) == VLC_SUCCESS )
1588 vlc_tick_t i_seeked = MP4_TrackGetDTS( p_demux, tk );
1589 if( i_seeked < i_start )
1594 msg_Dbg( p_demux, "seeking with %"PRId64 "ms %s", MS_FROM_VLC_TICK(i_date - i_start),
1595 !b_accurate ? "alignment" : "preroll (use input-fast-seek to avoid)" );
1597 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1599 mp4_track_t *tk = &p_sys->track[i_track];
1600 tk->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1601 if( tk->fmt.i_cat == VIDEO_ES || !tk->b_ok )
1603 MP4_TrackSeek( p_demux, tk, i_start );
1606 MP4_UpdateSeekpoint( p_demux, i_date );
1607 MP4ASF_ResetFrames( p_sys );
1608 /* update global time */
1609 p_sys->i_nztime = i_start;
1610 p_sys->i_pcr = VLC_TICK_INVALID;
1613 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1618 static int FragPrepareChunk( demux_t *p_demux, MP4_Box_t *p_moof,
1619 MP4_Box_t *p_sidx, stime_t i_moof_time, bool b_discontinuity )
1621 demux_sys_t *p_sys = p_demux->p_sys;
1623 if( b_discontinuity )
1625 for( unsigned i=0; i<p_sys->i_tracks; i++ )
1626 p_sys->track[i].context.b_resync_time_offset = true;
1629 if( FragCreateTrunIndex( p_demux, p_moof, p_sidx, i_moof_time ) == VLC_SUCCESS )
1631 for( unsigned i=0; i<p_sys->i_tracks; i++ )
1633 mp4_track_t *p_track = &p_sys->track[i];
1634 if( p_track->context.runs.i_count )
1636 const mp4_run_t *p_run = &p_track->context.runs.p_array[0];
1637 p_track->context.i_trun_sample_pos = p_run->i_offset;
1638 p_track->context.i_trun_sample = 0;
1639 p_track->i_time = p_run->i_first_dts;
1645 return VLC_EGENERIC;
1648 static vlc_tick_t FragGetDemuxTimeFromTracksTime( demux_sys_t *p_sys )
1650 vlc_tick_t i_time = INT64_MAX;
1651 for( unsigned int i = 0; i < p_sys->i_tracks; i++ )
1653 if( p_sys->track[i].context.runs.i_count == 0 )
1655 vlc_tick_t i_ttime = MP4_rescale_mtime( p_sys->track[i].i_time,
1656 p_sys->track[i].i_timescale );
1657 i_time = __MIN( i_time, i_ttime );
1662 static uint32_t FragGetMoofSequenceNumber( MP4_Box_t *p_moof )
1664 const MP4_Box_t *p_mfhd = MP4_BoxGet( p_moof, "mfhd" );
1665 if( p_mfhd && BOXDATA(p_mfhd) )
1666 return BOXDATA(p_mfhd)->i_sequence_number;
1670 static int FragSeekLoadFragment( demux_t *p_demux, uint32_t i_moox, stime_t i_moox_time )
1672 demux_sys_t *p_sys = p_demux->p_sys;
1675 if( i_moox == ATOM_moov )
1677 p_moox = p_sys->p_moov;
1681 const uint8_t *p_peek;
1682 if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) != 8 )
1683 return VLC_EGENERIC;
1685 if( ATOM_moof != VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
1686 return VLC_EGENERIC;
1688 MP4_Box_t *p_vroot = MP4_BoxGetNextChunk( p_demux->s );
1690 return VLC_EGENERIC;
1691 p_moox = MP4_BoxExtract( &p_vroot->p_first, ATOM_moof );
1692 MP4_BoxFree( p_vroot );
1695 return VLC_EGENERIC;
1698 FragResetContext( p_sys );
1701 p_sys->context.p_fragment_atom = p_moox;
1702 p_sys->context.i_current_box_type = i_moox;
1704 if( i_moox == ATOM_moof )
1706 FragPrepareChunk( p_demux, p_moox, NULL, i_moox_time, true );
1707 p_sys->context.i_lastseqnumber = FragGetMoofSequenceNumber( p_moox );
1709 p_sys->i_nztime = FragGetDemuxTimeFromTracksTime( p_sys );
1710 p_sys->i_pcr = VLC_TICK_INVALID;
1713 msg_Dbg( p_demux, "seeked to %4.4s at pos %" PRIu64, (char *) &i_moox, p_moox->i_pos );
1717 static unsigned GetSeekTrackIndex( demux_sys_t *p_sys )
1720 for( unsigned i=0; i<p_sys->i_tracks; i++ )
1722 if( p_sys->track[i].fmt.i_cat == VIDEO_ES ||
1723 p_sys->track[i].fmt.i_cat == AUDIO_ES )
1725 if( cand != i && !p_sys->track[cand].b_selected )
1732 static void FragTrunSeekToTime( mp4_track_t *p_track, stime_t i_target_time )
1734 if( !p_track->b_ok || p_track->context.runs.i_count < 1 )
1738 unsigned i_sample = 0;
1739 uint64_t i_pos = p_track->context.runs.p_array[0].i_offset;
1740 stime_t i_time = p_track->context.runs.p_array[0].i_first_dts;
1742 for( unsigned r = 0; r < p_track->context.runs.i_count; r++ )
1744 const mp4_run_t *p_run = &p_track->context.runs.p_array[r];
1745 const MP4_Box_data_trun_t *p_data =
1746 p_track->context.runs.p_array[r].p_trun->data.p_trun;
1747 if( i_time > i_target_time )
1751 i_time = p_run->i_first_dts;
1752 i_pos = p_run->i_offset;
1755 uint32_t dur = p_track->context.i_default_sample_duration;
1756 uint32_t len = p_track->context.i_default_sample_size;
1757 for ( unsigned i=0; i<p_data->i_sample_count; i++ )
1759 if( p_data->i_flags & MP4_TRUN_SAMPLE_DURATION )
1760 dur = p_data->p_samples[i].i_duration;
1762 /* check condition */
1763 if( i_time + dur > i_target_time )
1766 if( p_data->i_flags & MP4_TRUN_SAMPLE_SIZE )
1767 len = p_data->p_samples[i].i_size;
1774 p_track->context.i_trun_sample = i_sample;
1775 p_track->context.i_trun_sample_pos = i_pos;
1776 p_track->context.runs.i_current = i_run;
1779 #define INVALID_SEGMENT_TIME INT64_MAX
1781 static int FragSeekToTime( demux_t *p_demux, vlc_tick_t i_nztime, bool b_accurate )
1783 demux_sys_t *p_sys = p_demux->p_sys;
1784 uint64_t i64 = UINT64_MAX;
1785 uint32_t i_segment_type = ATOM_moof;
1786 stime_t i_segment_time = INVALID_SEGMENT_TIME;
1787 vlc_tick_t i_sync_time = i_nztime;
1788 bool b_iframesync = false;
1790 const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1791 if ( !p_sys->i_timescale || !i_duration || !p_sys->b_seekable )
1792 return VLC_EGENERIC;
1794 uint64_t i_backup_pos = vlc_stream_Tell( p_demux->s );
1796 if ( !p_sys->b_fragments_probed && !p_sys->b_index_probed && p_sys->b_seekable )
1798 ProbeIndex( p_demux );
1799 p_sys->b_index_probed = true;
1802 const unsigned i_seek_track_index = GetSeekTrackIndex( p_sys );
1803 const unsigned i_seek_track_ID = p_sys->track[i_seek_track_index].i_track_ID;
1805 if( MP4_rescale_qtime( i_nztime, p_sys->i_timescale )
1806 < GetMoovTrackDuration( p_sys, i_seek_track_ID ) )
1808 i64 = p_sys->p_moov->i_pos;
1809 i_segment_type = ATOM_moov;
1811 else if( FragGetMoofBySidxIndex( p_demux, i_nztime, &i64, &i_sync_time ) == VLC_SUCCESS )
1813 /* provides base offset */
1814 i_segment_time = i_sync_time;
1815 msg_Dbg( p_demux, "seeking to sidx moof pos %" PRId64 " %" PRId64, i64, i_sync_time );
1819 if( FragGetMoofByTfraIndex( p_demux, i_nztime, i_seek_track_ID, &i64, &i_sync_time ) == VLC_SUCCESS )
1821 /* Does only provide segment position and a sync sample time */
1822 msg_Dbg( p_demux, "seeking to sync point %" PRId64, i_sync_time );
1823 b_iframesync = true;
1825 else if( !p_sys->b_fragments_probed )
1827 int i_ret = ProbeFragmentsChecked( p_demux );
1828 if( i_ret != VLC_SUCCESS )
1832 if( p_sys->b_fragments_probed && p_sys->p_fragsindex )
1834 stime_t i_basetime = MP4_rescale_qtime( i_sync_time, p_sys->i_timescale );
1835 if( !MP4_Fragments_Index_Lookup( p_sys->p_fragsindex, &i_basetime, &i64, i_seek_track_index ) )
1837 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1838 return VLC_EGENERIC;
1840 msg_Dbg( p_demux, "seeking to fragment index pos %" PRId64 " %" PRId64, i64,
1841 MP4_rescale_mtime( i_basetime, p_sys->i_timescale ) );
1845 if( i64 == UINT64_MAX )
1847 msg_Warn( p_demux, "seek by index failed" );
1848 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1849 return VLC_EGENERIC;
1852 msg_Dbg( p_demux, "final seek to fragment at %"PRId64, i64 );
1853 if( vlc_stream_Seek( p_demux->s, i64 ) )
1855 msg_Err( p_demux, "seek failed to %"PRId64, i64 );
1856 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1857 return VLC_EGENERIC;
1860 /* Context is killed on success */
1861 if( FragSeekLoadFragment( p_demux, i_segment_type, i_segment_time ) != VLC_SUCCESS )
1863 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1864 return VLC_EGENERIC;
1867 p_sys->i_pcr = VLC_TICK_INVALID;
1869 for( unsigned i=0; i<p_sys->i_tracks; i++ )
1871 if( i_segment_type == ATOM_moov )
1873 MP4_TrackSeek( p_demux, &p_sys->track[i], i_sync_time );
1874 p_sys->i_nztime = i_sync_time;
1875 p_sys->i_pcr = VLC_TICK_INVALID;
1877 else if( b_iframesync )
1879 stime_t i_tst = MP4_rescale_qtime( i_sync_time, p_sys->track[i].i_timescale );
1880 FragTrunSeekToTime( &p_sys->track[i], i_tst );
1881 p_sys->track[i].i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1885 MP4ASF_ResetFrames( p_sys );
1886 /* And set next display time in that trun/fragment */
1887 if( b_iframesync && b_accurate )
1888 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TICK_0 + i_nztime );
1892 static int FragSeekToPos( demux_t *p_demux, double f, bool b_accurate )
1894 demux_sys_t *p_sys = p_demux->p_sys;
1896 if ( !p_sys->b_seekable || !p_sys->i_timescale )
1897 return VLC_EGENERIC;
1899 uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1900 if( !i_duration && !p_sys->b_fragments_probed )
1902 int i_ret = ProbeFragmentsChecked( p_demux );
1903 if( i_ret != VLC_SUCCESS )
1905 i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1909 return VLC_EGENERIC;
1911 return FragSeekToTime( p_demux, (vlc_tick_t)( f *
1912 MP4_rescale_mtime( i_duration, p_sys->i_timescale ) ), b_accurate );
1915 static bool imageTypeCompatible( const MP4_Box_data_data_t *p_data )
1918 p_data->e_wellknowntype == DATA_WKT_PNG ||
1919 p_data->e_wellknowntype == DATA_WKT_JPEG ||
1920 p_data->e_wellknowntype == DATA_WKT_BMP );
1923 static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta )
1925 MP4_Box_t *p_data = NULL;
1926 MP4_Box_t *p_udta = NULL;
1927 bool b_attachment_set = false;
1930 return VLC_EGENERIC;
1932 for( int i_index = 0; psz_meta_roots[i_index] && !p_udta; i_index++ )
1934 p_udta = MP4_BoxGet( p_sys->p_root, psz_meta_roots[i_index] );
1937 p_data = MP4_BoxGet( p_udta, "covr/data" );
1938 if ( p_data && imageTypeCompatible( BOXDATA(p_data) ) )
1940 char *psz_attachment;
1941 if ( -1 != asprintf( &psz_attachment, "attachment://%s/covr/data[0]",
1942 psz_meta_roots[i_index] ) )
1944 vlc_meta_SetArtURL( p_meta, psz_attachment );
1945 b_attachment_set = true;
1946 free( psz_attachment );
1952 const MP4_Box_t *p_pnot;
1953 if ( !b_attachment_set && (p_pnot = MP4_BoxGet( p_sys->p_root, "pnot" )) )
1955 for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms) && !b_attachment_set; i++ )
1957 if ( rgi_pict_atoms[i] == BOXDATA(p_pnot)->i_type )
1960 snprintf( rgsz_path, 26, "attachment://%4.4s[%"PRIu16"]",
1961 (char*)&rgi_pict_atoms[i], BOXDATA(p_pnot)->i_index - 1 );
1962 vlc_meta_SetArtURL( p_meta, rgsz_path );
1963 b_attachment_set = true;
1968 if( p_udta == NULL )
1970 if( !b_attachment_set )
1971 return VLC_EGENERIC;
1973 else SetupMeta( p_meta, p_udta );
1978 /*****************************************************************************
1980 *****************************************************************************/
1981 static int Control( demux_t *p_demux, int i_query, va_list args )
1983 demux_sys_t *p_sys = p_demux->p_sys;
1989 const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1993 case DEMUX_CAN_SEEK:
1994 *va_arg( args, bool * ) = p_sys->b_seekable;
1997 case DEMUX_GET_POSITION:
1998 pf = va_arg( args, double * );
1999 if( i_duration > 0 )
2001 *pf = (double)p_sys->i_nztime /
2002 MP4_rescale_mtime( i_duration, p_sys->i_timescale );
2010 case DEMUX_GET_SEEKPOINT:
2011 *va_arg( args, int * ) = p_sys->i_seekpoint;
2014 case DEMUX_SET_POSITION:
2015 f = va_arg( args, double );
2016 b = va_arg( args, int );
2017 if ( p_demux->pf_demux == DemuxFrag )
2018 return FragSeekToPos( p_demux, f, b );
2019 else if( p_sys->i_timescale > 0 )
2021 i64 = (vlc_tick_t)( f * MP4_rescale_mtime( p_sys->i_duration,
2022 p_sys->i_timescale ) );
2023 return Seek( p_demux, i64, b );
2025 else return VLC_EGENERIC;
2027 case DEMUX_GET_TIME:
2028 *va_arg( args, vlc_tick_t * ) = p_sys->i_timescale > 0 ? p_sys->i_nztime : 0;
2031 case DEMUX_SET_TIME:
2032 i64 = va_arg( args, vlc_tick_t );
2033 b = va_arg( args, int );
2034 if ( p_demux->pf_demux == DemuxFrag )
2035 return FragSeekToTime( p_demux, i64, b );
2037 return Seek( p_demux, i64, b );
2039 case DEMUX_GET_LENGTH:
2040 if( p_sys->i_timescale > 0 )
2042 *va_arg( args, vlc_tick_t * ) = MP4_rescale_mtime( i_duration,
2043 p_sys->i_timescale );
2045 else *va_arg( args, vlc_tick_t * ) = 0;
2049 pf = va_arg( args, double * );
2053 case DEMUX_GET_ATTACHMENTS:
2055 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
2056 int *pi_int = va_arg( args, int * );
2058 MP4_Box_t *p_udta = NULL;
2062 /* Count number of total attachments */
2063 for( ; psz_meta_roots[i_index] && !p_udta; i_index++ )
2065 p_udta = MP4_BoxGet( p_sys->p_root, psz_meta_roots[i_index] );
2067 i_count += MP4_BoxCount( p_udta, "covr/data" );
2070 for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms); i++ )
2073 snprintf( rgsz_path, 5, "%4.4s", (char*)&rgi_pict_atoms[i] );
2074 i_count += MP4_BoxCount( p_sys->p_root, rgsz_path );
2078 return VLC_EGENERIC;
2080 *ppp_attach = (input_attachment_t**)
2081 vlc_alloc( i_count, sizeof(input_attachment_t*) );
2082 if( !(*ppp_attach) ) return VLC_ENOMEM;
2084 /* First add cover attachments */
2086 size_t i_box_count = 0;
2089 const MP4_Box_t *p_data = MP4_BoxGet( p_udta, "covr/data" );
2090 for( ; p_data; p_data = p_data->p_next )
2096 if ( p_data->i_type != ATOM_data || !imageTypeCompatible( BOXDATA(p_data) ) )
2099 switch( BOXDATA(p_data)->e_wellknowntype )
2102 psz_mime = strdup( "image/png" );
2105 psz_mime = strdup( "image/jpeg" );
2108 psz_mime = strdup( "image/bmp" );
2114 if ( asprintf( &psz_filename, "%s/covr/data[%"PRIu64"]", psz_meta_roots[i_index - 1],
2115 (uint64_t) i_box_count - 1 ) >= 0 )
2117 (*ppp_attach)[i_count++] =
2118 vlc_input_attachment_New( psz_filename, psz_mime, "Cover picture",
2119 BOXDATA(p_data)->p_blob, BOXDATA(p_data)->i_blob );
2120 msg_Dbg( p_demux, "adding attachment %s", psz_filename );
2121 free( psz_filename );
2128 /* Then quickdraw pict ones */
2129 for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms); i++ )
2132 snprintf( rgsz_path, 5, "%4.4s", (char*)&rgi_pict_atoms[i] );
2133 const MP4_Box_t *p_pict = MP4_BoxGet( p_sys->p_root, rgsz_path );
2135 for( ; p_pict; p_pict = p_pict->p_next )
2137 if ( i_box_count++ == UINT16_MAX ) /* pnot only handles 2^16 */
2139 if ( p_pict->i_type != rgi_pict_atoms[i] )
2141 char rgsz_location[12];
2142 snprintf( rgsz_location, 12, "%4.4s[%"PRIu16"]", (char*)&rgi_pict_atoms[i],
2143 (uint16_t) i_box_count - 1 );
2144 (*ppp_attach)[i_count] = vlc_input_attachment_New( rgsz_location, "image/x-pict",
2145 "Quickdraw image", p_pict->data.p_binary->p_blob, p_pict->data.p_binary->i_blob );
2146 if ( !(*ppp_attach)[i_count] )
2152 msg_Dbg( p_demux, "adding attachment %s", rgsz_location );
2158 free( *ppp_attach );
2159 return VLC_EGENERIC;
2167 case DEMUX_GET_META:
2169 vlc_meta_t *p_meta = va_arg( args, vlc_meta_t *);
2171 if( !p_sys->p_meta )
2172 return VLC_EGENERIC;
2174 vlc_meta_Merge( p_meta, p_sys->p_meta );
2179 case DEMUX_GET_TITLE_INFO:
2181 input_title_t ***ppp_title = va_arg( args, input_title_t *** );
2182 int *pi_int = va_arg( args, int* );
2183 int *pi_title_offset = va_arg( args, int* );
2184 int *pi_seekpoint_offset = va_arg( args, int* );
2186 if( !p_sys->p_title )
2187 return VLC_EGENERIC;
2190 *ppp_title = malloc( sizeof( input_title_t*) );
2191 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
2192 *pi_title_offset = 0;
2193 *pi_seekpoint_offset = 0;
2196 case DEMUX_SET_TITLE:
2198 const int i_title = va_arg( args, int );
2199 if( !p_sys->p_title || i_title != 0 )
2200 return VLC_EGENERIC;
2203 case DEMUX_SET_SEEKPOINT:
2205 const int i_seekpoint = va_arg( args, int );
2206 if( !p_sys->p_title )
2207 return VLC_EGENERIC;
2208 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset, true );
2210 case DEMUX_TEST_AND_CLEAR_FLAGS:
2212 unsigned *restrict flags = va_arg( args, unsigned * );
2214 if ((*flags & INPUT_UPDATE_SEEKPOINT) && p_sys->seekpoint_changed)
2216 *flags = INPUT_UPDATE_SEEKPOINT;
2217 p_sys->seekpoint_changed = false;
2224 case DEMUX_GET_PTS_DELAY:
2226 for( unsigned int i = 0; i < p_sys->i_tracks; i++ )
2228 const MP4_Box_t *p_load;
2229 if ( (p_load = MP4_BoxGet( p_sys->track[i].p_track, "load" )) &&
2230 BOXDATA(p_load)->i_duration > 0 )
2232 *va_arg(args, vlc_tick_t *) =
2233 MP4_rescale_mtime( BOXDATA(p_load)->i_duration,
2234 p_sys->track[i].i_timescale );
2238 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
2240 case DEMUX_SET_NEXT_DEMUX_TIME:
2241 case DEMUX_SET_GROUP_DEFAULT:
2242 case DEMUX_SET_GROUP_ALL:
2243 case DEMUX_SET_GROUP_LIST:
2244 case DEMUX_HAS_UNSUPPORTED_META:
2245 case DEMUX_CAN_RECORD:
2246 return VLC_EGENERIC;
2248 case DEMUX_CAN_PAUSE:
2249 case DEMUX_SET_PAUSE_STATE:
2250 case DEMUX_CAN_CONTROL_PACE:
2251 return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
2254 return VLC_EGENERIC;
2258 /*****************************************************************************
2259 * Close: frees unused data
2260 *****************************************************************************/
2261 static void Close ( vlc_object_t * p_this )
2263 demux_t * p_demux = (demux_t *)p_this;
2264 demux_sys_t *p_sys = p_demux->p_sys;
2265 unsigned int i_track;
2267 msg_Dbg( p_demux, "freeing all memory" );
2269 FragResetContext( p_sys );
2271 MP4_BoxFree( p_sys->p_root );
2273 if( p_sys->p_title )
2274 vlc_input_title_Delete( p_sys->p_title );
2277 vlc_meta_Delete( p_sys->p_meta );
2279 MP4_Fragments_Index_Delete( p_sys->p_fragsindex );
2281 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
2282 MP4_TrackClean( p_demux->out, &p_sys->track[i_track] );
2283 free( p_sys->track );
2290 /****************************************************************************
2291 * Local functions, specific to vlc
2292 ****************************************************************************/
2294 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
2296 demux_sys_t *p_sys = p_demux->p_sys;
2298 if( BOXDATA(p_chpl)->i_chapter == 0 )
2301 p_sys->p_title = vlc_input_title_New();
2302 for( int i = 0; i < BOXDATA(p_chpl)->i_chapter && p_sys->p_title; i++ )
2304 seekpoint_t *s = vlc_seekpoint_New();
2305 if( s == NULL) continue;
2307 s->psz_name = strdup( BOXDATA(p_chpl)->chapter[i].psz_name );
2308 if( s->psz_name == NULL)
2310 vlc_seekpoint_Delete( s );;
2314 EnsureUTF8( s->psz_name );
2315 msftime_t offset = BOXDATA(p_chpl)->chapter[i].i_start;
2316 s->i_time_offset = VLC_TICK_FROM_MSFTIME(offset);
2317 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2320 static void LoadChapterGoPro( demux_t *p_demux, MP4_Box_t *p_hmmt )
2322 demux_sys_t *p_sys = p_demux->p_sys;
2324 p_sys->p_title = vlc_input_title_New();
2325 if( p_sys->p_title )
2326 for( unsigned i = 0; i < BOXDATA(p_hmmt)->i_chapter_count; i++ )
2328 seekpoint_t *s = vlc_seekpoint_New();
2331 if( asprintf( &s->psz_name, "HiLight tag #%u", i+1 ) != -1 )
2332 EnsureUTF8( s->psz_name );
2334 /* HiLights are stored in ms so we convert them to µs */
2335 s->i_time_offset = VLC_TICK_FROM_MS( BOXDATA(p_hmmt)->pi_chapter_start[i] );
2336 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2340 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
2342 demux_sys_t *p_sys = p_demux->p_sys;
2344 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
2346 const vlc_tick_t i_dts = MP4_TrackGetDTS( p_demux, tk );
2347 vlc_tick_t i_pts_delta;
2348 if ( !MP4_TrackGetPTSDelta( p_demux, tk, &i_pts_delta ) )
2350 uint32_t i_nb_samples = 0;
2351 const uint32_t i_size = MP4_TrackGetReadSize( tk, &i_nb_samples );
2353 if( i_size > 0 && !vlc_stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
2356 const uint32_t i_read = stream_ReadU32( p_demux->s, p_buffer,
2357 __MIN( sizeof(p_buffer), i_size ) );
2360 const uint32_t i_string = __MIN( GetWBE(p_buffer), i_read-2 );
2361 const char *psnz_string = &p_buffer[2];
2363 seekpoint_t *s = vlc_seekpoint_New();
2364 if( s == NULL ) continue;
2366 if( i_string > 1 && !memcmp( psnz_string, "\xFF\xFE", 2 ) )
2367 s->psz_name = FromCharset( "UTF-16LE", psnz_string, i_string );
2369 s->psz_name = strndup( psnz_string, i_string );
2371 if( s->psz_name == NULL )
2373 vlc_seekpoint_Delete( s );
2377 EnsureUTF8( s->psz_name );
2378 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
2380 if( !p_sys->p_title )
2381 p_sys->p_title = vlc_input_title_New();
2382 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2385 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
2386 tk->chunk[tk->i_chunk].i_sample_count )
2390 static void LoadChapter( demux_t *p_demux )
2392 demux_sys_t *p_sys = p_demux->p_sys;
2396 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) &&
2397 BOXDATA(p_chpl) && BOXDATA(p_chpl)->i_chapter > 0 )
2399 LoadChapterGpac( p_demux, p_chpl );
2401 else if( ( p_hmmt = MP4_BoxGet( p_sys->p_root, "/moov/udta/HMMT" ) ) &&
2402 BOXDATA(p_hmmt) && BOXDATA(p_hmmt)->pi_chapter_start && BOXDATA(p_hmmt)->i_chapter_count > 0 )
2404 LoadChapterGoPro( p_demux, p_hmmt );
2408 /* Load the first subtitle track like quicktime */
2409 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
2411 mp4_track_t *tk = &p_sys->track[i];
2412 if(tk->b_ok && tk->as_reftype == ATOM_chap &&
2413 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_TX3G)
2415 LoadChapterApple( p_demux, tk );
2421 /* Add duration if titles are enabled */
2422 if( p_sys->p_title )
2424 const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
2425 p_sys->p_title->i_length =
2426 MP4_rescale_mtime( i_duration, p_sys->i_timescale );
2430 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
2431 static int TrackCreateChunksIndex( demux_t *p_demux,
2432 mp4_track_t *p_demux_track )
2434 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
2437 unsigned int i_chunk;
2438 unsigned int i_index, i_last;
2440 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
2441 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
2442 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
2444 return( VLC_EGENERIC );
2447 p_demux_track->i_chunk_count = BOXDATA(p_co64)->i_entry_count;
2448 if( !p_demux_track->i_chunk_count )
2450 msg_Warn( p_demux, "no chunk defined" );
2452 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
2453 sizeof( mp4_chunk_t ) );
2454 if( p_demux_track->chunk == NULL )
2459 /* first we read chunk offset */
2460 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2462 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2464 ck->i_offset = BOXDATA(p_co64)->i_chunk_offset[i_chunk];
2466 ck->i_first_dts = 0;
2467 ck->i_entries_dts = 0;
2468 ck->p_sample_count_dts = NULL;
2469 ck->p_sample_delta_dts = NULL;
2470 ck->i_entries_pts = 0;
2471 ck->p_sample_count_pts = NULL;
2472 ck->p_sample_offset_pts = NULL;
2475 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
2476 to be used for the sample XXX begin to 1
2477 We construct it begining at the end */
2478 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
2479 i_index = BOXDATA(p_stsc)->i_entry_count;
2481 while( i_index-- > 0 )
2483 for( i_chunk = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
2484 i_chunk < i_last; i_chunk++ )
2486 if( i_chunk >= p_demux_track->i_chunk_count )
2488 msg_Warn( p_demux, "corrupted chunk table" );
2489 return VLC_EGENERIC;
2492 p_demux_track->chunk[i_chunk].i_sample_description_index =
2493 BOXDATA(p_stsc)->i_sample_description_index[i_index];
2494 p_demux_track->chunk[i_chunk].i_sample_count =
2495 BOXDATA(p_stsc)->i_samples_per_chunk[i_index];
2497 i_last = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
2500 p_demux_track->i_sample_count = 0;
2501 bool b_broken = false;
2502 if ( p_demux_track->i_chunk_count )
2504 p_demux_track->chunk[0].i_sample_first = 0;
2505 p_demux_track->i_sample_count += p_demux_track->chunk[0].i_sample_count;
2507 const mp4_chunk_t *prev = &p_demux_track->chunk[0];
2508 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2510 mp4_chunk_t *cur = &p_demux_track->chunk[i_chunk];
2511 if( unlikely(UINT32_MAX - cur->i_sample_count < p_demux_track->i_sample_count) )
2516 p_demux_track->i_sample_count += cur->i_sample_count;
2517 cur->i_sample_first = prev->i_sample_first + prev->i_sample_count;
2522 if( unlikely(b_broken) )
2524 msg_Err( p_demux, "Overflow in chunks total samples count" );
2525 return VLC_EGENERIC;
2528 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
2529 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
2534 static int xTTS_CountEntries( demux_t *p_demux, uint32_t *pi_entry /* out */,
2535 const uint32_t i_index,
2536 uint32_t i_index_samples_left,
2537 uint32_t i_sample_count,
2538 const uint32_t *pi_index_sample_count,
2539 const uint32_t i_table_count )
2541 uint32_t i_array_offset;
2542 while( i_sample_count > 0 )
2544 if ( likely((UINT32_MAX - i_index) >= *pi_entry) )
2545 i_array_offset = i_index + *pi_entry;
2547 return VLC_EGENERIC;
2549 if ( i_array_offset >= i_table_count )
2551 msg_Err( p_demux, "invalid index counting total samples %u %u", i_array_offset, i_table_count );
2555 if ( i_index_samples_left )
2557 if ( i_index_samples_left > i_sample_count )
2559 i_index_samples_left -= i_sample_count;
2561 *pi_entry +=1; /* No samples left, go copy */
2566 i_sample_count -= i_index_samples_left;
2567 i_index_samples_left = 0;
2574 i_sample_count -= __MIN( i_sample_count, pi_index_sample_count[i_array_offset] );
2582 static int TrackCreateSamplesIndex( demux_t *p_demux,
2583 mp4_track_t *p_demux_track )
2586 MP4_Box_data_stsz_t *stsz;
2587 /* TODO use also stss and stsh table for seeking */
2588 /* FIXME use edit table */
2591 * Gives the sample size for each samples. There is also a stz2 table
2592 * (compressed form) that we need to implement TODO */
2593 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
2596 /* FIXME and stz2 */
2597 msg_Warn( p_demux, "cannot find STSZ box" );
2598 return VLC_EGENERIC;
2600 stsz = p_box->data.p_stsz;
2602 /* Use stsz table to create a sample number -> sample size table */
2603 if( p_demux_track->i_sample_count != stsz->i_sample_count )
2605 msg_Warn( p_demux, "Incorrect total samples stsc %" PRIu32 " <> stsz %"PRIu32 ", "
2606 " expect truncated media playback",
2607 p_demux_track->i_sample_count, stsz->i_sample_count );
2608 p_demux_track->i_sample_count = __MIN(p_demux_track->i_sample_count, stsz->i_sample_count);
2611 if( stsz->i_sample_size )
2613 /* 1: all sample have the same size, so no need to construct a table */
2614 p_demux_track->i_sample_size = stsz->i_sample_size;
2615 p_demux_track->p_sample_size = NULL;
2619 /* 2: each sample can have a different size */
2620 p_demux_track->i_sample_size = 0;
2621 p_demux_track->p_sample_size =
2622 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
2623 if( p_demux_track->p_sample_size == NULL )
2626 for( uint32_t i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
2628 p_demux_track->p_sample_size[i_sample] =
2629 stsz->i_entry_size[i_sample];
2633 if ( p_demux_track->i_chunk_count && p_demux_track->i_sample_size == 0 )
2635 const mp4_chunk_t *lastchunk = &p_demux_track->chunk[p_demux_track->i_chunk_count - 1];
2636 if( (uint64_t)lastchunk->i_sample_count + p_demux_track->i_chunk_count - 1 > stsz->i_sample_count )
2638 msg_Err( p_demux, "invalid samples table: stsz table is too small" );
2639 return VLC_EGENERIC;
2643 /* Use stts table to create a sample number -> dts table.
2644 * XXX: if we don't want to waste too much memory, we can't expand
2645 * the box! so each chunk will contain an "extract" of this table
2646 * for fast research (problem with raw stream where a sample is sometime
2647 * just channels*bits_per_sample/8 */
2649 /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
2650 * differing in 2/2 fields and 1 signedness */
2652 int64_t i_next_dts = 0;
2654 * Gives mapping between sample and decoding time
2656 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
2659 msg_Warn( p_demux, "cannot find STTS box" );
2660 return VLC_EGENERIC;
2664 MP4_Box_data_stts_t *stts = p_box->data.p_stts;
2666 msg_Warn( p_demux, "STTS table of %"PRIu32" entries", stts->i_entry_count );
2668 /* Create sample -> dts table per chunk */
2669 uint32_t i_index = 0;
2670 uint32_t i_current_index_samples_left = 0;
2672 for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2674 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2675 uint32_t i_sample_count;
2677 /* save first dts */
2678 ck->i_first_dts = i_next_dts;
2680 /* count how many entries are needed for this chunk
2681 * for p_sample_delta_dts and p_sample_count_dts */
2682 ck->i_entries_dts = 0;
2684 int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_dts, i_index,
2685 i_current_index_samples_left,
2687 stts->pi_sample_count,
2688 stts->i_entry_count );
2689 if ( i_ret == VLC_EGENERIC )
2693 ck->p_sample_count_dts = calloc( ck->i_entries_dts, sizeof( uint32_t ) );
2694 ck->p_sample_delta_dts = calloc( ck->i_entries_dts, sizeof( uint32_t ) );
2695 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
2697 free( ck->p_sample_count_dts );
2698 free( ck->p_sample_delta_dts );
2699 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_dts );
2700 ck->i_entries_dts = 0;
2705 i_sample_count = ck->i_sample_count;
2707 for( uint32_t i = 0; i < ck->i_entries_dts; i++ )
2709 if ( i_current_index_samples_left )
2711 if ( i_current_index_samples_left > 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 -= i_sample_count;
2719 assert( i == ck->i_entries_dts - 1 );
2724 ck->p_sample_count_dts[i] = i_current_index_samples_left;
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 ( i_current_index_samples_left ) ck->i_duration = i_next_dts - ck->i_first_dts;
2728 i_sample_count -= i_current_index_samples_left;
2729 i_current_index_samples_left = 0;
2735 if ( stts->pi_sample_count[i_index] > i_sample_count )
2737 ck->p_sample_count_dts[i] = i_sample_count;
2738 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2739 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2740 if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts;
2741 i_current_index_samples_left = stts->pi_sample_count[i_index] - i_sample_count;
2743 assert( i == ck->i_entries_dts - 1 );
2744 // keep building from same index
2748 ck->p_sample_count_dts[i] = stts->pi_sample_count[i_index];
2749 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2750 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2751 if ( stts->pi_sample_count[i_index] ) ck->i_duration = i_next_dts - ck->i_first_dts;
2752 i_sample_count -= stts->pi_sample_count[i_index];
2763 * Gives the delta between decoding time (dts) and composition table (pts)
2765 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
2766 if( p_box && p_box->data.p_ctts )
2768 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
2770 msg_Warn( p_demux, "CTTS table of %"PRIu32" entries", ctts->i_entry_count );
2772 int64_t i_cts_shift = 0;
2773 const MP4_Box_t *p_cslg = MP4_BoxGet( p_demux_track->p_stbl, "cslg" );
2774 if( p_cslg && BOXDATA(p_cslg) )
2775 i_cts_shift = BOXDATA(p_cslg)->ct_to_dts_shift;
2777 /* Create pts-dts table per chunk */
2778 uint32_t i_index = 0;
2779 uint32_t i_current_index_samples_left = 0;
2781 for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2783 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2784 uint32_t i_sample_count;
2786 /* count how many entries are needed for this chunk
2787 * for p_sample_offset_pts and p_sample_count_pts */
2788 ck->i_entries_pts = 0;
2789 int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_pts, i_index,
2790 i_current_index_samples_left,
2792 ctts->pi_sample_count,
2793 ctts->i_entry_count );
2794 if ( i_ret == VLC_EGENERIC )
2798 ck->p_sample_count_pts = calloc( ck->i_entries_pts, sizeof( uint32_t ) );
2799 ck->p_sample_offset_pts = calloc( ck->i_entries_pts, sizeof( int32_t ) );
2800 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
2802 free( ck->p_sample_count_pts );
2803 free( ck->p_sample_offset_pts );
2804 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_pts );
2805 ck->i_entries_pts = 0;
2810 i_sample_count = ck->i_sample_count;
2812 for( uint32_t i = 0; i < ck->i_entries_pts; i++ )
2814 if ( i_current_index_samples_left )
2816 if ( i_current_index_samples_left > i_sample_count )
2818 ck->p_sample_count_pts[i] = i_sample_count;
2819 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2820 i_current_index_samples_left -= i_sample_count;
2822 assert( i == ck->i_entries_pts - 1 );
2827 ck->p_sample_count_pts[i] = i_current_index_samples_left;
2828 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2829 i_sample_count -= i_current_index_samples_left;
2830 i_current_index_samples_left = 0;
2836 if ( ctts->pi_sample_count[i_index] > i_sample_count )
2838 ck->p_sample_count_pts[i] = i_sample_count;
2839 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2840 i_current_index_samples_left = ctts->pi_sample_count[i_index] - i_sample_count;
2842 assert( i == ck->i_entries_pts - 1 );
2843 // keep building from same index
2847 ck->p_sample_count_pts[i] = ctts->pi_sample_count[i_index];
2848 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2849 i_sample_count -= ctts->pi_sample_count[i_index];
2859 msg_Dbg( p_demux, "track[Id 0x%x] read %"PRIu32" samples length:%"PRId64"s",
2860 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
2861 i_next_dts / p_demux_track->i_timescale );
2868 * It computes the sample rate for a video track using the given sample
2871 static void TrackGetESSampleRate( demux_t *p_demux,
2872 unsigned *pi_num, unsigned *pi_den,
2873 const mp4_track_t *p_track,
2874 unsigned i_sd_index,
2877 demux_sys_t *p_sys = p_demux->p_sys;
2881 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( MP4_BoxGet( p_sys->p_root,
2883 p_track->i_track_ID );
2884 MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
2885 if ( p_mdhd && BOXDATA(p_mdhd) )
2887 vlc_ureduce( pi_num, pi_den,
2888 (uint64_t) BOXDATA(p_mdhd)->i_timescale * p_track->i_sample_count,
2889 (uint64_t) BOXDATA(p_mdhd)->i_duration,
2894 if( p_track->i_chunk_count == 0 )
2898 const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
2899 while( p_chunk > &p_track->chunk[0] &&
2900 p_chunk[-1].i_sample_description_index == i_sd_index )
2905 uint64_t i_sample = 0;
2906 uint64_t i_total_duration = 0;
2909 i_sample += p_chunk->i_sample_count;
2910 i_total_duration += p_chunk->i_duration;
2913 while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
2914 p_chunk->i_sample_description_index == i_sd_index );
2916 if( i_sample > 0 && i_total_duration )
2917 vlc_ureduce( pi_num, pi_den,
2918 i_sample * p_track->i_timescale,
2925 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
2927 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
2928 unsigned int i_chunk, es_out_id_t **pp_es )
2930 demux_sys_t *p_sys = p_demux->p_sys;
2931 unsigned int i_sample_description_index;
2933 if( p_sys->b_fragmented || p_track->i_chunk_count == 0 )
2934 i_sample_description_index = 1; /* XXX */
2936 i_sample_description_index =
2937 p_track->chunk[i_chunk].i_sample_description_index;
2942 if( !i_sample_description_index )
2944 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
2945 p_track->i_track_ID );
2946 return VLC_EGENERIC;
2949 MP4_Box_t *p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
2950 i_sample_description_index - 1 );
2953 ( !p_sample->data.p_payload && p_track->fmt.i_cat != SPU_ES ) )
2955 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
2956 p_track->i_track_ID );
2957 return VLC_EGENERIC;
2960 p_track->p_sample = p_sample;
2963 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) && p_frma->data.p_frma )
2965 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
2967 p_sample->i_type = p_frma->data.p_frma->i_type;
2971 switch( p_track->fmt.i_cat )
2974 if ( p_sample->i_handler != ATOM_vide ||
2975 !SetupVideoES( p_demux, p_track, p_sample ) )
2976 return VLC_EGENERIC;
2978 /* Set frame rate */
2979 TrackGetESSampleRate( p_demux,
2980 &p_track->fmt.video.i_frame_rate,
2981 &p_track->fmt.video.i_frame_rate_base,
2982 p_track, i_sample_description_index, i_chunk );
2984 p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
2985 (float)p_track->fmt.video.i_frame_rate_base;
2990 if ( p_sample->i_handler != ATOM_soun ||
2991 !SetupAudioES( p_demux, p_track, p_sample ) )
2992 return VLC_EGENERIC;
2995 audio_replay_gain_t *p_arg = &p_track->fmt.audio_replay_gain;
2996 const char *psz_meta = vlc_meta_GetExtra( p_sys->p_meta, "replaygain_track_gain" );
2999 double f_gain = us_atof( psz_meta );
3000 p_arg->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = f_gain;
3001 p_arg->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = f_gain != 0;
3003 psz_meta = vlc_meta_GetExtra( p_sys->p_meta, "replaygain_track_peak" );
3006 double f_gain = us_atof( psz_meta );
3007 p_arg->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = f_gain;
3008 p_arg->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = f_gain > 0;
3014 if ( ( p_sample->i_handler != ATOM_text &&
3015 p_sample->i_handler != ATOM_subt &&
3016 p_sample->i_handler != ATOM_sbtl &&
3017 p_sample->i_handler != ATOM_clcp ) ||
3018 !SetupSpuES( p_demux, p_track, p_sample ) )
3019 return VLC_EGENERIC;
3027 *pp_es = MP4_AddTrackES( p_demux->out, p_track );
3029 return ( !pp_es || *pp_es ) ? VLC_SUCCESS : VLC_EGENERIC;
3032 /* *** Try to find nearest sync points *** */
3033 static int TrackGetNearestSeekPoint( demux_t *p_demux, mp4_track_t *p_track,
3034 uint32_t i_sample, uint32_t *pi_sync_sample )
3036 int i_ret = VLC_EGENERIC;
3037 *pi_sync_sample = 0;
3039 const MP4_Box_t *p_stss;
3040 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
3042 const MP4_Box_data_stss_t *p_stss_data = BOXDATA(p_stss);
3043 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
3044 p_track->i_track_ID );
3045 for( unsigned i_index = 0; i_index < p_stss_data->i_entry_count; i_index++ )
3047 if( i_index >= p_stss_data->i_entry_count - 1 ||
3048 i_sample < p_stss_data->i_sample_number[i_index+1] )
3050 *pi_sync_sample = p_stss_data->i_sample_number[i_index];
3051 msg_Dbg( p_demux, "stss gives %d --> %" PRIu32 " (sample number)",
3052 i_sample, *pi_sync_sample );
3053 i_ret = VLC_SUCCESS;
3059 /* try rap samples groups */
3060 const MP4_Box_t *p_sbgp = MP4_BoxGet( p_track->p_stbl, "sbgp" );
3061 for( ; p_sbgp; p_sbgp = p_sbgp->p_next )
3063 const MP4_Box_data_sbgp_t *p_sbgp_data = BOXDATA(p_sbgp);
3064 if( p_sbgp->i_type != ATOM_sbgp || !p_sbgp_data )
3067 if( p_sbgp_data->i_grouping_type == SAMPLEGROUP_rap )
3069 uint32_t i_group_sample = 0;
3070 for ( uint32_t i=0; i<p_sbgp_data->i_entry_count; i++ )
3072 /* Sample belongs to rap group ? */
3073 if( p_sbgp_data->entries.pi_group_description_index[i] != 0 )
3075 if( i_sample < i_group_sample )
3077 msg_Dbg( p_demux, "sbgp lookup failed %" PRIu32 " (sample number)",
3081 else if ( i_sample >= i_group_sample &&
3082 *pi_sync_sample < i_group_sample )
3084 *pi_sync_sample = i_group_sample;
3085 i_ret = VLC_SUCCESS;
3088 i_group_sample += p_sbgp_data->entries.pi_sample_count[i];
3091 if( i_ret == VLC_SUCCESS && *pi_sync_sample )
3093 msg_Dbg( p_demux, "sbgp gives %d --> %" PRIu32 " (sample number)",
3094 i_sample, *pi_sync_sample );
3102 /* given a time it return sample/chunk
3103 * it also update elst field of the track
3105 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
3106 vlc_tick_t start, uint32_t *pi_chunk,
3107 uint32_t *pi_sample )
3109 demux_sys_t *p_sys = p_demux->p_sys;
3111 unsigned int i_sample;
3112 unsigned int i_chunk;
3116 /* FIXME see if it's needed to check p_track->i_chunk_count */
3117 if( p_track->i_chunk_count == 0 )
3118 return( VLC_EGENERIC );
3120 /* handle elst (find the correct one) */
3121 MP4_TrackSetELST( p_demux, p_track, start );
3122 if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
3124 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
3125 int64_t i_mvt= MP4_rescale_qtime( start, p_sys->i_timescale );
3127 /* now calculate i_start for this elst */
3129 if( start < MP4_rescale_mtime( p_track->i_elst_time, p_sys->i_timescale ) )
3136 /* to track time scale */
3137 i_start = MP4_rescale_qtime( start, p_track->i_timescale );
3138 /* add elst offset */
3139 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
3140 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
3141 elst->i_media_time[p_track->i_elst] > 0 )
3143 i_start += elst->i_media_time[p_track->i_elst];
3146 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
3147 "ms (track)", p_track->i_elst,
3148 MP4_rescale( i_mvt, p_sys->i_timescale, 1000 ),
3149 MP4_rescale( i_start, p_track->i_timescale, 1000 ) );
3153 /* convert absolute time to in timescale unit */
3154 i_start = MP4_rescale_qtime( start, p_track->i_timescale );
3157 /* we start from sample 0/chunk 0, hope it won't take too much time */
3158 /* *** find good chunk *** */
3159 for( i_chunk = 0; ; i_chunk++ )
3161 if( i_chunk + 1 >= p_track->i_chunk_count )
3163 /* at the end and can't check if i_start in this chunk,
3164 it will be check while searching i_sample */
3165 i_chunk = p_track->i_chunk_count - 1;
3169 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
3170 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
3176 /* *** find sample in the chunk *** */
3177 i_sample = p_track->chunk[i_chunk].i_sample_first;
3178 i_dts = p_track->chunk[i_chunk].i_first_dts;
3179 for( i_index = 0; i_index < p_track->chunk[i_chunk].i_entries_dts &&
3180 i_sample < p_track->chunk[i_chunk].i_sample_count; )
3183 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
3184 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
3187 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
3188 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
3190 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
3195 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
3199 i_sample += ( i_start - i_dts ) /
3200 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
3205 if( i_sample >= p_track->i_sample_count )
3207 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
3208 "(seeking too far) chunk=%d sample=%d",
3209 p_track->i_track_ID, i_chunk, i_sample );
3210 return( VLC_EGENERIC );
3214 /* *** Try to find nearest sync points *** */
3215 uint32_t i_sync_sample;
3217 TrackGetNearestSeekPoint( p_demux, p_track, i_sample, &i_sync_sample ) )
3220 if( i_sync_sample <= i_sample )
3222 while( i_chunk > 0 &&
3223 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
3228 while( i_chunk < p_track->i_chunk_count - 1 &&
3229 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
3230 p_track->chunk[i_chunk].i_sample_count )
3233 i_sample = i_sync_sample;
3236 *pi_chunk = i_chunk;
3237 *pi_sample = i_sample;
3242 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
3243 unsigned int i_chunk, unsigned int i_sample )
3245 bool b_reselect = false;
3247 /* now see if actual es is ok */
3248 if( p_track->i_chunk >= p_track->i_chunk_count ||
3249 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
3250 p_track->chunk[i_chunk].i_sample_description_index )
3252 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
3253 p_track->i_track_ID );
3255 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
3256 p_track->p_es, &b_reselect );
3258 es_out_Del( p_demux->out, p_track->p_es );
3260 p_track->p_es = NULL;
3262 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
3264 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
3265 p_track->i_track_ID );
3267 p_track->b_ok = false;
3268 p_track->b_selected = false;
3269 return VLC_EGENERIC;
3273 /* select again the new decoder */
3276 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
3279 p_track->i_chunk = i_chunk;
3280 p_track->chunk[i_chunk].i_sample = i_sample - p_track->chunk[i_chunk].i_sample_first;
3281 p_track->i_sample = i_sample;
3283 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
3286 static void MP4_TrackRestart( demux_t *p_demux, mp4_track_t *p_track,
3287 MP4_Box_t *p_params_box )
3289 bool b_reselect = false;
3292 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
3293 p_track->p_es, &b_reselect );
3296 /* Save previous fragmented pos */
3297 uint32_t i_sample_pos_backup = p_track->i_sample;
3298 vlc_tick_t time_backup = p_track->i_time;
3299 uint32_t timescale_backup = p_track->i_timescale;
3301 /* Save previous format and ES */
3302 es_format_t fmtbackup;
3303 es_out_id_t *p_es_backup = p_track->p_es;
3304 p_track->p_es = NULL;
3305 es_format_Copy( &fmtbackup, &p_track->fmt );
3306 es_format_Clean( &p_track->fmt );
3309 /* do the cleanup and recycle track / restart */
3310 MP4_TrackDestroy( p_demux, p_track );
3311 memset( p_track, 0, sizeof(*p_track) );
3313 assert(p_params_box->i_type == ATOM_trak);
3314 MP4_TrackCreate( p_demux, p_track, p_params_box, false, true );
3318 if( !es_format_IsSimilar( &fmtbackup, &p_track->fmt ) ||
3319 fmtbackup.i_extra != p_track->fmt.i_extra ||
3320 memcmp( fmtbackup.p_extra, p_track->fmt.p_extra, fmtbackup.i_extra ) )
3323 es_out_Del( p_demux->out, p_es_backup );
3325 if( !p_track->b_chapters_source )
3327 p_track->p_es = MP4_AddTrackES( p_demux->out, p_track );
3328 p_track->b_ok = !!p_track->p_es;
3333 p_track->p_es = p_es_backup;
3336 else if( p_es_backup )
3338 es_out_Del( p_demux->out, p_es_backup );
3341 /* select again the new decoder */
3342 if( b_reselect && p_track->p_es )
3343 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
3345 es_format_Clean( &fmtbackup );
3347 /* Restore fragmented pos */
3348 p_track->i_sample = i_sample_pos_backup;
3349 p_track->i_time = MP4_rescale( time_backup, timescale_backup, p_track->i_timescale );
3352 /****************************************************************************
3354 ****************************************************************************
3355 * Parse track information and create all needed data to run a track
3356 * If it succeed b_ok is set to 1 else to 0
3357 ****************************************************************************/
3358 static void MP4_TrackSetup( demux_t *p_demux, mp4_track_t *p_track,
3359 MP4_Box_t *p_box_trak,
3360 bool b_create_es, bool b_force_enable )
3362 demux_sys_t *p_sys = p_demux->p_sys;
3364 p_track->p_track = p_box_trak;
3366 char language[4] = { '\0' };
3367 char sdp_media_type[8] = { '\0' };
3369 const MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
3375 /* do we launch this track by default ? */
3377 ( ( BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED ) != 0 );
3379 p_track->i_width = BOXDATA(p_tkhd)->i_width / BLOCK16x16;
3380 p_track->i_height = BOXDATA(p_tkhd)->i_height / BLOCK16x16;
3381 p_track->f_rotation = BOXDATA(p_tkhd)->f_rotation;
3382 p_track->i_flip = BOXDATA(p_tkhd)->i_flip;
3384 /* FIXME: unhandled box: tref */
3386 const MP4_Box_t *p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
3387 const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
3389 if( ( !p_mdhd )||( !p_hdlr ) )
3394 if( BOXDATA(p_mdhd)->i_timescale == 0 )
3396 msg_Warn( p_demux, "Invalid track timescale " );
3399 p_track->i_timescale = BOXDATA(p_mdhd)->i_timescale;
3401 memcpy( &language, BOXDATA(p_mdhd)->rgs_language, 3 );
3402 p_track->b_mac_encoding = BOXDATA(p_mdhd)->b_mac_encoding;
3404 switch( p_hdlr->data.p_hdlr->i_handler_type )
3407 if( !MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) )
3411 es_format_Change( &p_track->fmt, AUDIO_ES, 0 );
3414 case( ATOM_pict ): /* heif */
3415 es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3419 if( !MP4_BoxGet( p_box_trak, "mdia/minf/vmhd") )
3423 es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3427 /* RTP Reception Hint tracks */
3428 if( !MP4_BoxGet( p_box_trak, "mdia/minf/hmhd" ) ||
3429 !MP4_BoxGet( p_box_trak, "mdia/minf/stbl/stsd/rrtp" ) )
3435 /* parse the sdp message to find out whether the RTP stream contained audio or video */
3436 if( !( p_sdp = MP4_BoxGet( p_box_trak, "udta/hnti/sdp " ) ) )
3438 msg_Warn( p_demux, "Didn't find sdp box to determine stream type" );
3442 memcpy( sdp_media_type, BOXDATA(p_sdp)->psz_text, 7 );
3443 if( !strcmp(sdp_media_type, "m=audio") )
3445 msg_Dbg( p_demux, "Found audio Rtp: %s", sdp_media_type );
3446 es_format_Change( &p_track->fmt, AUDIO_ES, 0 );
3448 else if( !strcmp(sdp_media_type, "m=video") )
3450 msg_Dbg( p_demux, "Found video Rtp: %s", sdp_media_type );
3451 es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3455 msg_Warn( p_demux, "Malformed track SDP message: %s", sdp_media_type );
3458 p_track->p_sdp = p_sdp;
3464 case( ATOM_subt ): /* ttml */
3466 case( ATOM_clcp ): /* closed captions */
3467 es_format_Change( &p_track->fmt, SPU_ES, 0 );
3474 p_track->asfinfo.i_cat = p_track->fmt.i_cat;
3476 const MP4_Box_t *p_elst;
3477 p_track->i_elst = 0;
3478 p_track->i_elst_time = 0;
3479 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
3481 MP4_Box_data_elst_t *elst = BOXDATA(p_elst);
3484 msg_Warn( p_demux, "elst box found" );
3485 for( i = 0; i < elst->i_entry_count; i++ )
3487 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
3488 "ms) rate=%d.%d", i,
3489 MP4_rescale( elst->i_segment_duration[i], p_sys->i_timescale, 1000 ),
3490 elst->i_media_time[i] >= 0 ?
3491 MP4_rescale( elst->i_media_time[i], p_track->i_timescale, 1000 ) :
3493 elst->i_media_rate_integer[i],
3494 elst->i_media_rate_fraction[i] );
3501 p_dinf = MP4_BoxGet( p_minf, "dinf" );
3503 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
3504 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
3510 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
3512 p_track->fmt.psz_language = strdup( language );
3515 const MP4_Box_t *p_udta = MP4_BoxGet( p_box_trak, "udta" );
3518 const MP4_Box_t *p_box_iter;
3519 for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
3520 p_box_iter = p_box_iter->p_next )
3522 switch( p_box_iter->i_type )
3526 p_track->fmt.psz_description =
3527 strndup( p_box_iter->data.p_binary->p_blob,
3528 p_box_iter->data.p_binary->i_blob );
3535 /* Create chunk index table and sample index table */
3536 if( TrackCreateChunksIndex( p_demux,p_track ) ||
3537 TrackCreateSamplesIndex( p_demux, p_track ) )
3539 msg_Err( p_demux, "cannot create chunks index" );
3540 return; /* cannot create chunks index */
3543 p_track->i_chunk = 0;
3544 p_track->i_sample = 0;
3546 /* Disable chapter only track */
3547 if( p_track->fmt.i_cat == UNKNOWN_ES &&
3548 p_track->as_reftype == ATOM_chap )
3549 p_track->b_enable = false;
3551 const MP4_Box_t *p_tsel;
3553 if( b_force_enable &&
3554 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
3556 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
3557 p_track->i_track_ID );
3558 p_track->b_enable = true;
3559 p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN;
3561 else if ( (p_tsel = MP4_BoxGet( p_box_trak, "udta/tsel" )) )
3563 if ( BOXDATA(p_tsel) && BOXDATA(p_tsel)->i_switch_group )
3565 p_track->i_switch_group = BOXDATA(p_tsel)->i_switch_group;
3566 int i_priority = ES_PRIORITY_SELECTABLE_MIN;
3567 for ( unsigned int i = 0; i < p_sys->i_tracks; i++ )
3569 const mp4_track_t *p_other = &p_sys->track[i];
3570 if( p_other && p_other != p_track &&
3571 p_other->fmt.i_cat == p_track->fmt.i_cat &&
3572 p_track->i_switch_group == p_other->i_switch_group )
3573 i_priority = __MAX( i_priority, p_other->fmt.i_priority + 1 );
3575 /* VLC only support ES priority for AUDIO_ES and SPU_ES.
3576 If there's another VIDEO_ES in the same group, we need to unselect it then */
3577 if ( p_track->fmt.i_cat == VIDEO_ES && i_priority > ES_PRIORITY_SELECTABLE_MIN )
3578 p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
3580 p_track->fmt.i_priority = i_priority;
3583 /* If there's no tsel, try to enable the track coming first in edit list */
3584 else if ( p_track->p_elst && p_track->fmt.i_priority == ES_PRIORITY_SELECTABLE_MIN )
3586 #define MAX_SELECTABLE (INT_MAX - ES_PRIORITY_SELECTABLE_MIN)
3587 for ( uint32_t i=0; i<p_track->BOXDATA(p_elst)->i_entry_count; i++ )
3589 if ( p_track->BOXDATA(p_elst)->i_media_time[i] >= 0 &&
3590 p_track->BOXDATA(p_elst)->i_segment_duration[i] )
3592 /* We do selection by inverting start time into priority.
3593 The track with earliest edit will have the highest prio */
3594 const int i_time = __MIN( MAX_SELECTABLE, p_track->BOXDATA(p_elst)->i_media_time[i] );
3595 p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + MAX_SELECTABLE - i_time;
3601 if( p_sys->hacks.es_cat_filters && (p_sys->hacks.es_cat_filters & p_track->fmt.i_cat) == 0 )
3603 p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
3606 if( !p_track->b_enable )
3607 p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
3609 if( TrackCreateES( p_demux,
3610 p_track, p_track->i_chunk,
3611 (MP4_isMetadata( p_track ) || !b_create_es) ? NULL : &p_track->p_es ) )
3613 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
3614 p_track->i_track_ID );
3618 p_track->b_ok = true;
3621 static void DestroyChunk( mp4_chunk_t *ck )
3623 free( ck->p_sample_count_dts );
3624 free( ck->p_sample_delta_dts );
3625 free( ck->p_sample_count_pts );
3626 free( ck->p_sample_offset_pts );
3627 free( ck->p_sample_size );
3630 /****************************************************************************
3632 ****************************************************************************
3633 * Cleans a track created by MP4_TrackCreate.
3634 ****************************************************************************/
3635 static void MP4_TrackClean( es_out_t *out, mp4_track_t *p_track )
3637 es_format_Clean( &p_track->fmt );
3640 es_out_Del( out, p_track->p_es );
3642 if( p_track->chunk )
3644 for( unsigned int i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
3645 DestroyChunk( &p_track->chunk[i_chunk] );
3647 free( p_track->chunk );
3649 if( !p_track->i_sample_size )
3650 free( p_track->p_sample_size );
3652 if ( p_track->asfinfo.p_frame )
3653 block_ChainRelease( p_track->asfinfo.p_frame );
3655 free( p_track->context.runs.p_array );
3658 static void MP4_TrackInit( mp4_track_t *p_track, const MP4_Box_t *p_trackbox )
3660 memset( p_track, 0, sizeof(mp4_track_t) );
3661 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
3662 p_track->i_timescale = 1;
3663 p_track->p_track = p_trackbox;
3664 const MP4_Box_t *p_tkhd = MP4_BoxGet( p_trackbox, "tkhd" );
3665 if(likely(p_tkhd) && BOXDATA(p_tkhd))
3666 p_track->i_track_ID = BOXDATA(p_tkhd)->i_track_ID;
3669 static void MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track, bool b_select )
3671 if( !p_track->b_ok || MP4_isMetadata(p_track) )
3674 if( b_select == p_track->b_selected )
3677 if( !b_select && p_track->p_es )
3679 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
3680 p_track->p_es, false );
3683 p_track->b_selected = b_select;
3686 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
3687 vlc_tick_t i_start )
3692 if( !p_track->b_ok || MP4_isMetadata( p_track ) )
3693 return VLC_EGENERIC;
3695 p_track->b_selected = false;
3697 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
3698 &i_chunk, &i_sample ) )
3700 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
3701 p_track->i_track_ID );
3702 return VLC_EGENERIC;
3705 p_track->b_selected = true;
3706 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
3707 p_track->b_selected = true;
3709 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
3714 * 3 types: for audio
3717 static inline uint32_t MP4_GetFixedSampleSize( const mp4_track_t *p_track,
3718 const MP4_Box_data_sample_soun_t *p_soun )
3720 uint32_t i_size = p_track->i_sample_size;
3722 assert( p_track->i_sample_size != 0 );
3724 /* QuickTime "built-in" support case fixups */
3725 if( p_track->fmt.i_cat == AUDIO_ES &&
3726 p_soun->i_compressionid == 0 && p_track->i_sample_size <= 2 )
3728 switch( p_track->fmt.i_codec )
3731 i_size = p_soun->i_channelcount;
3733 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
3737 case VLC_CODEC_S24L:
3738 case VLC_CODEC_S24B:
3739 case VLC_CODEC_S32L:
3740 case VLC_CODEC_S32B:
3741 case VLC_CODEC_F32L:
3742 case VLC_CODEC_F32B:
3743 case VLC_CODEC_F64L:
3744 case VLC_CODEC_F64B:
3745 if( p_track->i_sample_size < ((p_soun->i_samplesize+7U)/8U) * p_soun->i_channelcount )
3746 i_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
3748 case VLC_CODEC_ALAW:
3749 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
3750 i_size = p_soun->i_channelcount;
3760 static uint32_t MP4_TrackGetReadSize( mp4_track_t *p_track, uint32_t *pi_nb_samples )
3762 uint32_t i_size = 0;
3765 if ( p_track->i_sample == p_track->i_sample_count )
3768 if ( p_track->fmt.i_cat != AUDIO_ES )
3772 if( p_track->i_sample_size == 0 ) /* all sizes are different */
3773 return p_track->p_sample_size[p_track->i_sample];
3775 return p_track->i_sample_size;
3779 const MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
3780 const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
3781 uint32_t i_max_samples = p_chunk->i_sample_count - p_chunk->i_sample;
3783 /* Group audio packets so we don't call demux for single sample unit */
3784 if( p_track->fmt.i_original_fourcc == VLC_CODEC_DVD_LPCM &&
3785 p_soun->i_constLPCMframesperaudiopacket &&
3786 p_soun->i_constbytesperaudiopacket )
3788 /* uncompressed case */
3789 uint32_t i_packets = i_max_samples / p_soun->i_constLPCMframesperaudiopacket;
3790 if ( UINT32_MAX / p_soun->i_constbytesperaudiopacket < i_packets )
3791 i_packets = UINT32_MAX / p_soun->i_constbytesperaudiopacket;
3792 *pi_nb_samples = i_packets * p_soun->i_constLPCMframesperaudiopacket;
3793 return i_packets * p_soun->i_constbytesperaudiopacket;
3796 if( p_track->fmt.i_original_fourcc == VLC_FOURCC('r','r','t','p') )
3799 return p_track->i_sample_size;
3802 /* all samples have a different size */
3803 if( p_track->i_sample_size == 0 )
3806 return p_track->p_sample_size[p_track->i_sample];
3809 if( p_soun->i_qt_version == 1 )
3811 if ( p_soun->i_compressionid == 0xFFFE )
3813 *pi_nb_samples = 1; /* != number of audio samples */
3814 if ( p_track->i_sample_size )
3815 return p_track->i_sample_size;
3817 return p_track->p_sample_size[p_track->i_sample];
3819 else if ( p_soun->i_compressionid != 0 || p_soun->i_bytes_per_sample > 1 ) /* compressed */
3821 /* in this case we are dealing with compressed data
3822 -2 in V1: additional fields are meaningless (VBR and such) */
3823 *pi_nb_samples = i_max_samples;//p_track->chunk[p_track->i_chunk].i_sample_count;
3824 if( p_track->fmt.audio.i_blockalign > 1 )
3825 *pi_nb_samples = p_soun->i_sample_per_packet;
3826 i_size = *pi_nb_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
3829 else /* uncompressed case */
3832 if( p_track->fmt.audio.i_blockalign > 1 )
3835 i_packets = i_max_samples / p_soun->i_sample_per_packet;
3837 if ( UINT32_MAX / p_soun->i_bytes_per_frame < i_packets )
3838 i_packets = UINT32_MAX / p_soun->i_bytes_per_frame;
3840 *pi_nb_samples = i_packets * p_soun->i_sample_per_packet;
3841 i_size = i_packets * p_soun->i_bytes_per_frame;
3846 /* uncompressed v0 (qt) or... not (ISO) */
3848 /* Quicktime built-in support handling */
3849 if( p_soun->i_compressionid == 0 && p_track->i_sample_size == 1 )
3851 switch( p_track->fmt.i_codec )
3853 /* sample size is not integer */
3855 *pi_nb_samples = 160 * p_track->fmt.audio.i_channels;
3856 return 33 * p_track->fmt.audio.i_channels;
3857 case VLC_CODEC_ADPCM_IMA_QT:
3858 *pi_nb_samples = 64 * p_track->fmt.audio.i_channels;
3859 return 34 * p_track->fmt.audio.i_channels;
3865 /* More regular V0 cases */
3866 uint32_t i_max_v0_samples;
3867 switch( p_track->fmt.i_codec )
3869 /* Compressed samples in V0 */
3870 case VLC_CODEC_AMR_NB:
3871 case VLC_CODEC_AMR_WB:
3872 i_max_v0_samples = 16;
3874 case VLC_CODEC_MPGA:
3878 case VLC_CODEC_MP4A:
3880 i_max_v0_samples = 1;
3882 /* fixme, reverse using a list of uncompressed codecs */
3884 /* Read 25ms of samples (uncompressed) */
3885 i_max_v0_samples = p_track->fmt.audio.i_rate / 40 *
3886 p_track->fmt.audio.i_channels;
3887 if( i_max_v0_samples < 1 )
3888 i_max_v0_samples = 1;
3893 for( uint32_t i=p_track->i_sample;
3894 i<p_chunk->i_sample_first+p_chunk->i_sample_count &&
3895 i<p_track->i_sample_count;
3899 if ( p_track->i_sample_size == 0 )
3900 i_size += p_track->p_sample_size[i];
3902 i_size += MP4_GetFixedSampleSize( p_track, p_soun );
3904 /* Try to detect compression in ISO */
3905 if(p_soun->i_compressionid != 0)
3907 /* Return only 1 sample */
3911 if ( *pi_nb_samples == i_max_v0_samples )
3916 //fprintf( stderr, "size=%d\n", i_size );
3920 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
3922 unsigned int i_sample;
3925 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
3927 if( p_track->i_sample_size )
3929 MP4_Box_data_sample_soun_t *p_soun =
3930 p_track->p_sample->data.p_sample_soun;
3932 /* Quicktime builtin support, _must_ ignore sample tables */
3933 if( p_track->fmt.i_cat == AUDIO_ES && p_soun->i_compressionid == 0 &&
3934 p_track->i_sample_size == 1 )
3936 switch( p_track->fmt.i_codec )
3938 case VLC_CODEC_GSM: /* # Samples > data size */
3939 i_pos += ( p_track->i_sample -
3940 p_track->chunk[p_track->i_chunk].i_sample_first ) / 160 * 33;
3942 case VLC_CODEC_ADPCM_IMA_QT: /* # Samples > data size */
3943 i_pos += ( p_track->i_sample -
3944 p_track->chunk[p_track->i_chunk].i_sample_first ) / 64 * 34;
3951 if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 ||
3952 p_track->fmt.audio.i_blockalign <= 1 ||
3953 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame == 0 )
3955 i_pos += ( p_track->i_sample -
3956 p_track->chunk[p_track->i_chunk].i_sample_first ) *
3957 MP4_GetFixedSampleSize( p_track, p_soun );
3961 /* we read chunk by chunk unless a blockalign is requested */
3962 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
3963 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
3968 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
3969 i_sample < p_track->i_sample; i_sample++ )
3971 i_pos += p_track->p_sample_size[i_sample];
3978 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track, uint32_t i_samples )
3980 if ( UINT32_MAX - p_track->i_sample < i_samples )
3982 p_track->i_sample = UINT32_MAX;
3983 return VLC_EGENERIC;
3986 p_track->i_sample += i_samples;
3988 if( p_track->i_sample >= p_track->i_sample_count )
3989 return VLC_EGENERIC;
3991 /* Have we changed chunk ? */
3992 if( p_track->i_sample >=
3993 p_track->chunk[p_track->i_chunk].i_sample_first +
3994 p_track->chunk[p_track->i_chunk].i_sample_count )
3996 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
3997 p_track->i_sample ) )
3999 msg_Warn( p_demux, "track[0x%x] will be disabled "
4000 "(cannot restart decoder)", p_track->i_track_ID );
4001 MP4_TrackSelect( p_demux, p_track, false );
4002 return VLC_EGENERIC;
4006 /* Have we changed elst */
4007 if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
4009 demux_sys_t *p_sys = p_demux->p_sys;
4010 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
4011 uint64_t i_mvt = MP4_rescale_qtime( MP4_TrackGetDTS( p_demux, p_track ),
4012 p_sys->i_timescale );
4013 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
4014 i_mvt >= p_track->i_elst_time +
4015 elst->i_segment_duration[p_track->i_elst] )
4017 MP4_TrackSetELST( p_demux, p_track,
4018 MP4_TrackGetDTS( p_demux, p_track ) );
4025 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
4028 demux_sys_t *p_sys = p_demux->p_sys;
4029 int i_elst_last = tk->i_elst;
4031 /* handle elst (find the correct one) */
4033 tk->i_elst_time = 0;
4034 if( tk->p_elst && tk->BOXDATA(p_elst)->i_entry_count > 0 )
4036 MP4_Box_data_elst_t *elst = tk->BOXDATA(p_elst);
4037 int64_t i_mvt= MP4_rescale_qtime( i_time, p_sys->i_timescale );
4039 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
4041 uint64_t i_dur = elst->i_segment_duration[tk->i_elst];
4043 if( tk->i_elst_time <= i_mvt
4044 && i_mvt < (int64_t)(tk->i_elst_time + i_dur) )
4048 tk->i_elst_time += i_dur;
4051 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
4053 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
4054 tk->i_elst = elst->i_entry_count - 1;
4055 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
4058 if( elst->i_media_time[tk->i_elst] < 0 )
4061 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
4064 if( i_elst_last != tk->i_elst )
4066 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
4067 tk->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
4071 /******************************************************************************
4072 * Here are the functions used for fragmented MP4
4073 *****************************************************************************/
4077 * \Note If we call that function too soon,
4078 * before the track has been selected by MP4_TrackSelect
4079 * (during the first execution of Demux), then the track gets disabled
4081 static int ReInitDecoder( demux_t *p_demux, const MP4_Box_t *p_root,
4082 mp4_track_t *p_track )
4084 MP4_Box_t *p_paramsbox = MP4_BoxGet( p_root, "/moov/trak[0]" );
4086 return VLC_EGENERIC;
4088 MP4_TrackRestart( p_demux, p_track, p_paramsbox );
4090 /* Temporary hack until we support track selection */
4091 p_track->b_selected = true;
4092 p_track->b_enable = true;
4098 static stime_t GetCumulatedDuration( demux_t *p_demux )
4100 demux_sys_t *p_sys = p_demux->p_sys;
4101 stime_t i_max_duration = 0;
4103 for ( unsigned int i=0; i<p_sys->i_tracks; i++ )
4105 stime_t i_track_duration = 0;
4106 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->p_moov, p_sys->track[i].i_track_ID );
4107 const MP4_Box_t *p_stsz;
4108 const MP4_Box_t *p_tkhd;
4109 if ( (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) &&
4110 (p_stsz = MP4_BoxGet( p_trak, "mdia/minf/stbl/stsz" )) &&
4111 /* duration might be wrong an be set to whole duration :/ */
4112 BOXDATA(p_stsz)->i_sample_count > 0 )
4114 i_max_duration = __MAX( (uint64_t)i_max_duration, BOXDATA(p_tkhd)->i_duration );
4117 if( p_sys->p_fragsindex )
4119 i_track_duration += MP4_Fragment_Index_GetTrackDuration( p_sys->p_fragsindex, i );
4122 i_max_duration = __MAX( i_max_duration, i_track_duration );
4125 return i_max_duration;
4128 static int ProbeIndex( demux_t *p_demux )
4130 demux_sys_t *p_sys = p_demux->p_sys;
4131 uint64_t i_stream_size;
4132 uint8_t mfro[MP4_MFRO_BOXSIZE];
4133 assert( p_sys->b_seekable );
4135 if ( MP4_BoxCount( p_sys->p_root, "/mfra" ) )
4136 return VLC_EGENERIC;
4138 i_stream_size = stream_Size( p_demux->s );
4139 if ( ( i_stream_size >> 62 ) ||
4140 ( i_stream_size < MP4_MFRO_BOXSIZE ) ||
4141 ( vlc_stream_Seek( p_demux->s, i_stream_size - MP4_MFRO_BOXSIZE ) != VLC_SUCCESS )
4144 msg_Dbg( p_demux, "Probing tail for mfro has failed" );
4145 return VLC_EGENERIC;
4148 if ( vlc_stream_Read( p_demux->s, &mfro, MP4_MFRO_BOXSIZE ) == MP4_MFRO_BOXSIZE &&
4149 VLC_FOURCC(mfro[4],mfro[5],mfro[6],mfro[7]) == ATOM_mfro &&
4150 GetDWBE( &mfro ) == MP4_MFRO_BOXSIZE )
4152 uint32_t i_offset = GetDWBE( &mfro[12] );
4153 msg_Dbg( p_demux, "will read mfra index at %"PRIu64, i_stream_size - i_offset );
4154 if ( i_stream_size > i_offset &&
4155 vlc_stream_Seek( p_demux->s, i_stream_size - i_offset ) == VLC_SUCCESS )
4157 msg_Dbg( p_demux, "reading mfra index at %"PRIu64, i_stream_size - i_offset );
4158 const uint32_t stoplist[] = { ATOM_mfra, 0 };
4159 MP4_ReadBoxContainerChildren( p_demux->s, p_sys->p_root, stoplist );
4163 return VLC_EGENERIC;
4166 static stime_t GetMoovTrackDuration( demux_sys_t *p_sys, unsigned i_track_ID )
4168 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->p_moov, i_track_ID );
4169 const MP4_Box_t *p_stsz;
4170 const MP4_Box_t *p_tkhd;
4171 if ( (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) &&
4172 (p_stsz = MP4_BoxGet( p_trak, "mdia/minf/stbl/stsz" )) &&
4173 /* duration might be wrong an be set to whole duration :/ */
4174 BOXDATA(p_stsz)->i_sample_count > 0 )
4176 if( BOXDATA(p_tkhd)->i_duration <= p_sys->i_moov_duration )
4177 return BOXDATA(p_tkhd)->i_duration; /* In movie / mvhd scale */
4179 return p_sys->i_moov_duration;
4184 static bool GetMoofTrackDuration( MP4_Box_t *p_moov, MP4_Box_t *p_moof,