demux: mp4: use es_format_Change
[vlc.git] / modules / demux / mp4 / mp4.c
blobcad62a076282c7285c76bf961d64fa1b3c6c3814
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 *****************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #include "mp4.h"
31 #include <vlc_demux.h>
32 #include <vlc_charset.h> /* EnsureUTF8 */
33 #include <vlc_input.h>
34 #include <vlc_aout.h>
35 #include <vlc_plugin.h>
36 #include <vlc_dialog.h>
37 #include <assert.h>
38 #include <limits.h>
39 #include "../codec/cc.h"
41 /*****************************************************************************
42 * Module descriptor
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
47 #define CFG_PREFIX "mp4-"
49 #define MP4_M4A_TEXT "M4A audio only"
50 #define MP4_M4A_LONGTEXT "Ignore non audio tracks from iTunes audio files"
52 vlc_module_begin ()
53 set_category( CAT_INPUT )
54 set_subcategory( SUBCAT_INPUT_DEMUX )
55 set_description( N_("MP4 stream demuxer") )
56 set_shortname( N_("MP4") )
57 set_capability( "demux", 240 )
58 set_callbacks( Open, Close )
60 add_category_hint("Hacks", NULL, true)
61 add_bool( CFG_PREFIX"m4a-audioonly", false, MP4_M4A_TEXT, MP4_M4A_LONGTEXT, true )
62 vlc_module_end ()
64 /*****************************************************************************
65 * Local prototypes
66 *****************************************************************************/
67 static int Demux ( demux_t * );
68 static int DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
69 static int DemuxFrag( demux_t * );
70 static int Control ( demux_t *, int, va_list );
72 struct demux_sys_t
74 MP4_Box_t *p_root; /* container for the whole file */
76 mtime_t i_pcr;
78 uint64_t i_moov_duration;
79 uint64_t i_duration; /* Declared fragmented duration */
80 uint64_t i_cumulated_duration; /* Same as above, but not from probing */
81 uint64_t i_time; /* time position of the presentation
82 * in movie timescale */
83 uint32_t i_timescale; /* movie time scale */
84 unsigned int i_tracks; /* number of tracks */
85 mp4_track_t *track; /* array of track */
86 float f_fps; /* number of frame per seconds */
88 bool b_fragmented; /* fMP4 */
89 bool b_seekable;
90 bool b_fastseekable;
91 bool b_error; /* unrecoverable */
93 bool b_index_probed; /* mFra sync points index */
94 bool b_fragments_probed; /* moof segments index created */
96 MP4_Box_t *p_moov;
98 struct
100 uint32_t i_current_box_type;
101 MP4_Box_t *p_fragment_atom;
102 uint64_t i_post_mdat_offset;
103 uint32_t i_lastseqnumber;
104 } context;
106 /* */
107 MP4_Box_t *p_tref_chap;
109 /* */
110 input_title_t *p_title;
111 vlc_meta_t *p_meta;
113 /* ASF in MP4 */
114 asf_packet_sys_t asfpacketsys;
115 uint64_t i_preroll; /* foobar */
116 int64_t i_preroll_start;
118 struct
120 int es_cat_filters;
121 } hacks;
123 mp4_fragments_index_t *p_fragsindex;
126 #define DEMUX_INCREMENT (CLOCK_FREQ / 4) /* How far the pcr will go, each round */
127 #define DEMUX_TRACK_MAX_PRELOAD (CLOCK_FREQ * 15) /* maximum preloading, to deal with interleaving */
129 #define VLC_DEMUXER_EOS (VLC_DEMUXER_EGENERIC - 1)
131 const uint32_t rgi_pict_atoms[2] = { ATOM_PICT, ATOM_pict };
132 const char *psz_meta_roots[] = { "/moov/udta/meta/ilst",
133 "/moov/meta/ilst",
134 "/moov/udta/meta",
135 "/moov/udta",
136 "/meta/ilst",
137 "/udta",
138 NULL };
140 /*****************************************************************************
141 * Declaration of local function
142 *****************************************************************************/
143 static void MP4_TrackSetup( demux_t *, mp4_track_t *, MP4_Box_t *, bool, bool );
144 static void MP4_TrackInit( mp4_track_t * );
145 static void MP4_TrackClean( es_out_t *, mp4_track_t * );
147 static void MP4_Block_Send( demux_t *, mp4_track_t *, block_t * );
149 static void MP4_TrackSelect ( demux_t *, mp4_track_t *, bool );
150 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
152 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
153 static uint32_t MP4_TrackGetReadSize( mp4_track_t *, uint32_t * );
154 static int MP4_TrackNextSample( demux_t *, mp4_track_t *, uint32_t );
155 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
157 static void MP4_UpdateSeekpoint( demux_t *, int64_t );
159 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id );
160 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t *p_moov,
161 const MP4_Box_data_tfhd_t *p_tfhd_data,
162 uint32_t *pi_default_size,
163 uint32_t *pi_default_duration );
165 static stime_t GetMoovTrackDuration( demux_sys_t *p_sys, unsigned i_track_ID );
167 static int ProbeFragments( demux_t *p_demux, bool b_force, bool *pb_fragmented );
168 static int ProbeIndex( demux_t *p_demux );
170 static int FragCreateTrunIndex( demux_t *, MP4_Box_t *, MP4_Box_t *, stime_t, bool );
172 static int FragGetMoofBySidxIndex( demux_t *p_demux, mtime_t i_target_time,
173 uint64_t *pi_moof_pos, mtime_t *pi_sampletime );
174 static int FragGetMoofByTfraIndex( demux_t *p_demux, const mtime_t i_target_time, unsigned i_track_ID,
175 uint64_t *pi_moof_pos, mtime_t *pi_sampletime );
176 static void FragResetContext( demux_sys_t * );
178 /* ASF Handlers */
179 static asf_track_info_t * MP4ASF_GetTrackInfo( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number );
180 static void MP4ASF_Send(asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, block_t **pp_frame);
181 static void MP4ASF_ResetFrames( demux_sys_t *p_sys );
183 /* RTP Hint track */
184 static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec );
185 static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount );
187 static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta );
189 /* Helpers */
191 static int64_t MP4_rescale( int64_t i_value, uint32_t i_timescale, uint32_t i_newscale )
193 if( i_timescale == i_newscale )
194 return i_value;
196 if( i_value <= INT64_MAX / i_newscale )
197 return i_value * i_newscale / i_timescale;
199 /* overflow */
200 int64_t q = i_value / i_timescale;
201 int64_t r = i_value % i_timescale;
202 return q * i_newscale + r * i_newscale / i_timescale;
205 static uint32_t stream_ReadU32( stream_t *s, void *p_read, uint32_t i_toread )
207 ssize_t i_return = 0;
208 if ( i_toread > INT32_MAX )
210 i_return = vlc_stream_Read( s, p_read, (size_t) INT32_MAX );
211 if ( i_return < INT32_MAX )
212 return i_return;
213 else
214 i_toread -= INT32_MAX;
216 i_return += vlc_stream_Read( s, (uint8_t *)p_read + i_return, (size_t) i_toread );
217 return i_return;
220 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
222 if(!p_moov)
223 return NULL;
224 MP4_Box_t *p_trex = MP4_BoxGet( p_moov, "mvex/trex" );
225 while( p_trex )
227 if ( p_trex->i_type == ATOM_trex &&
228 BOXDATA(p_trex) && BOXDATA(p_trex)->i_track_ID == i_id )
229 break;
230 else
231 p_trex = p_trex->p_next;
233 return p_trex;
237 * Return the track identified by tid
239 static mp4_track_t *MP4_GetTrackByTrackID( demux_t *p_demux, const uint32_t tid )
241 demux_sys_t *p_sys = p_demux->p_sys;
243 mp4_track_t *ret = NULL;
244 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
246 ret = &p_sys->track[i];
247 if( ret->i_track_ID == tid )
248 return ret;
250 return NULL;
253 static MP4_Box_t * MP4_GetTrakByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
255 MP4_Box_t *p_trak = MP4_BoxGet( p_moov, "trak" );
256 MP4_Box_t *p_tkhd;
257 while( p_trak )
259 if( p_trak->i_type == ATOM_trak &&
260 (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) && BOXDATA(p_tkhd) &&
261 BOXDATA(p_tkhd)->i_track_ID == i_id )
262 break;
263 else
264 p_trak = p_trak->p_next;
266 return p_trak;
269 static MP4_Box_t * MP4_GetTrafByTrackID( MP4_Box_t *p_moof, const uint32_t i_id )
271 MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
272 MP4_Box_t *p_tfhd;
273 while( p_traf )
275 if( p_traf->i_type == ATOM_traf &&
276 (p_tfhd = MP4_BoxGet( p_traf, "tfhd" )) && BOXDATA(p_tfhd) &&
277 BOXDATA(p_tfhd)->i_track_ID == i_id )
278 break;
279 else
280 p_traf = p_traf->p_next;
282 return p_traf;
285 static es_out_id_t * MP4_AddTrackES( es_out_t *out, mp4_track_t *p_track )
287 es_out_id_t *p_es = es_out_Add( out, &p_track->fmt );
288 /* Force SPU which isn't selected/defaulted */
289 if( p_track->fmt.i_cat == SPU_ES && p_es && p_track->b_forced_spu )
290 es_out_Control( out, ES_OUT_SET_ES_DEFAULT, p_es );
292 return p_es;
295 /* Return time in microsecond of a track */
296 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
298 demux_sys_t *p_sys = p_demux->p_sys;
299 const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
301 unsigned int i_index = 0;
302 unsigned int i_sample = p_track->i_sample - p_chunk->i_sample_first;
303 int64_t i_dts = p_chunk->i_first_dts;
305 while( i_sample > 0 && i_index < p_chunk->i_entries_dts )
307 if( i_sample > p_chunk->p_sample_count_dts[i_index] )
309 i_dts += p_chunk->p_sample_count_dts[i_index] *
310 p_chunk->p_sample_delta_dts[i_index];
311 i_sample -= p_chunk->p_sample_count_dts[i_index];
312 i_index++;
314 else
316 i_dts += i_sample * p_chunk->p_sample_delta_dts[i_index];
317 break;
321 /* now handle elst */
322 if( p_track->p_elst )
324 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
326 /* convert to offset */
327 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
328 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
329 elst->i_media_time[p_track->i_elst] > 0 )
331 i_dts -= elst->i_media_time[p_track->i_elst];
334 /* add i_elst_time */
335 i_dts += MP4_rescale( p_track->i_elst_time, p_sys->i_timescale, p_track->i_timescale );
337 if( i_dts < 0 ) i_dts = 0;
340 return MP4_rescale( i_dts, p_track->i_timescale, CLOCK_FREQ );
343 static inline bool MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track,
344 int64_t *pi_delta )
346 VLC_UNUSED( p_demux );
347 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
349 unsigned int i_index = 0;
350 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
352 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
353 return false;
355 for( i_index = 0; i_index < ck->i_entries_pts ; i_index++ )
357 if( i_sample < ck->p_sample_count_pts[i_index] )
359 *pi_delta = MP4_rescale( ck->p_sample_offset_pts[i_index],
360 p_track->i_timescale, CLOCK_FREQ );
361 return true;
364 i_sample -= ck->p_sample_count_pts[i_index];
366 return false;
369 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
371 return MP4_rescale( p_sys->i_time, p_sys->i_timescale, CLOCK_FREQ );
374 static void LoadChapter( demux_t *p_demux );
376 static int LoadInitFrag( demux_t *p_demux )
378 demux_sys_t *p_sys = p_demux->p_sys;
380 /* Load all boxes ( except raw data ) */
381 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
383 goto LoadInitFragError;
386 return VLC_SUCCESS;
388 LoadInitFragError:
389 msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" );
390 return VLC_EGENERIC;
393 static int CreateTracks( demux_t *p_demux, unsigned i_tracks )
395 demux_sys_t *p_sys = p_demux->p_sys;
397 if( SIZE_MAX / i_tracks < sizeof(mp4_track_t) )
398 return VLC_EGENERIC;
400 p_sys->track = malloc( i_tracks * sizeof(mp4_track_t) );
401 if( p_sys->track == NULL )
402 return VLC_ENOMEM;
403 p_sys->i_tracks = i_tracks;
405 for( unsigned i=0; i<i_tracks; i++ )
406 MP4_TrackInit( &p_sys->track[i] );
408 return VLC_SUCCESS;
411 static block_t * MP4_WebVTT_Convert( demux_t *p_demux, block_t * p_block )
413 stream_t *p_stream =
414 vlc_stream_MemoryNew( p_demux, p_block->p_buffer,
415 p_block->i_buffer, true );
416 if( p_stream )
418 MP4_Box_t *p_vroot = MP4_BoxNew(ATOM_wvtt);
419 if( p_vroot )
421 p_vroot->i_size = p_block->i_buffer;
422 if ( MP4_ReadBoxContainerChildren( p_stream, p_vroot, NULL ) == 1 )
424 MP4_Box_t *p_payl = MP4_BoxGet( p_vroot, "vttc/payl" );
425 if( p_payl && p_payl->i_size >= 9 )
427 p_block->p_buffer += p_payl->i_pos + 8;
428 p_block->i_buffer = p_payl->i_size - 8;
429 p_block->p_buffer[p_block->i_buffer - 1] = '\0';
431 else p_block->i_buffer = 0;
432 #ifndef NDEBUG
433 MP4_BoxDumpStructure(p_stream, p_vroot);
434 #endif
436 MP4_BoxFree(p_vroot);
438 vlc_stream_Delete( p_stream );
440 return p_block;
443 static block_t * MP4_EIA608_Convert( block_t * p_block )
445 /* Rebuild codec data from encap */
446 size_t i_copied = 0;
447 size_t i_remaining = __MIN(p_block->i_buffer, INT64_MAX / 3);
448 uint32_t i_bytes = 0;
449 block_t *p_newblock;
451 /* always need at least 10 bytes (atom size+header+1pair)*/
452 if ( i_remaining < 10 ||
453 !(i_bytes = GetDWBE(p_block->p_buffer)) ||
454 (i_bytes > i_remaining) ||
455 memcmp("cdat", &p_block->p_buffer[4], 4) ||
456 !(p_newblock = block_Alloc( i_remaining * 3 - 8 )) )
458 p_block->i_buffer = 0;
459 return p_block;
462 uint8_t *p_write = p_newblock->p_buffer;
463 uint8_t *p_read = &p_block->p_buffer[8];
464 i_bytes -= 8;
465 i_remaining -= 8;
469 p_write[i_copied++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
470 p_write[i_copied++] = p_read[0];
471 p_write[i_copied++] = p_read[1];
472 p_read += 2;
473 i_bytes -= 2;
474 i_remaining -= 2;
475 } while( i_bytes >= 2 );
477 /* cdt2 is optional */
478 if ( i_remaining >= 10 &&
479 (i_bytes = GetDWBE(p_read)) &&
480 (i_bytes <= i_remaining) &&
481 !memcmp("cdt2", &p_read[4], 4) )
483 p_read += 8;
484 i_bytes -= 8;
485 i_remaining -= 8;
488 p_write[i_copied++] = CC_PKT_BYTE0(0); /* cc1 == field 0 */
489 p_write[i_copied++] = p_read[0];
490 p_write[i_copied++] = p_read[1];
491 p_read += 2;
492 i_bytes -= 2;
493 } while( i_bytes >= 2 );
496 p_newblock->i_pts = p_block->i_dts;
497 p_newblock->i_buffer = i_copied;
498 p_newblock->i_flags = BLOCK_FLAG_TYPE_P;
499 block_Release( p_block );
501 return p_newblock;
504 static uint32_t MP4_TrackGetRunSeq( mp4_track_t *p_track )
506 if( p_track->i_chunk_count > 0 )
507 return p_track->chunk[p_track->i_chunk].i_virtual_run_number;
508 return 0;
511 /* Analyzes chunks to find max interleave length
512 * sets flat flag if no interleaving is in use */
513 static void MP4_GetInterleaving( demux_t *p_demux, uint64_t *pi_max_contiguous, bool *pb_flat )
515 demux_sys_t *p_sys = p_demux->p_sys;
516 *pi_max_contiguous = 0;
517 *pb_flat = true;
519 /* Find first recorded chunk */
520 mp4_track_t *tk = NULL;
521 uint64_t i_duration = 0;
522 for( unsigned i=0; i < p_sys->i_tracks; i++ )
524 mp4_track_t *cur = &p_sys->track[i];
525 if( !cur->i_chunk_count )
526 continue;
528 if( tk == NULL || cur->chunk[0].i_offset < tk->chunk[0].i_offset )
529 tk = cur;
532 for( ; tk != NULL; )
534 i_duration += tk->chunk[tk->i_chunk].i_duration;
535 tk->i_chunk++;
537 /* Find next chunk in data order */
538 mp4_track_t *nexttk = NULL;
539 for( unsigned i=0; i < p_sys->i_tracks; i++ )
541 mp4_track_t *cur = &p_sys->track[i];
542 if( cur->i_chunk == cur->i_chunk_count )
543 continue;
545 if( nexttk == NULL ||
546 cur->chunk[cur->i_chunk].i_offset < nexttk->chunk[nexttk->i_chunk].i_offset )
547 nexttk = cur;
550 /* copy previous run */
551 if( nexttk && nexttk->i_chunk > 0 )
552 nexttk->chunk[nexttk->i_chunk].i_virtual_run_number =
553 nexttk->chunk[nexttk->i_chunk - 1].i_virtual_run_number;
555 if( tk != nexttk )
557 i_duration = MP4_rescale( i_duration, tk->i_timescale, CLOCK_FREQ );
558 if( i_duration > *pi_max_contiguous )
559 *pi_max_contiguous = i_duration;
560 i_duration = 0;
562 if( tk->i_chunk != tk->i_chunk_count )
563 *pb_flat = false;
565 if( nexttk && nexttk->i_chunk > 0 ) /* new run number */
566 nexttk->chunk[nexttk->i_chunk].i_virtual_run_number++;
569 tk = nexttk;
572 /* reset */
573 for( unsigned i=0; i < p_sys->i_tracks; i++ )
574 p_sys->track[i].i_chunk = 0;
577 static block_t * MP4_Block_Convert( demux_t *p_demux, const mp4_track_t *p_track, block_t *p_block )
579 /* might have some encap */
580 if( p_track->fmt.i_cat == SPU_ES )
582 switch( p_track->fmt.i_codec )
584 case VLC_CODEC_TTML:
585 case VLC_CODEC_TX3G:
586 case VLC_CODEC_SPU:
587 /* accept as-is */
588 break;
589 case VLC_CODEC_EIA608_1:
590 p_block = MP4_EIA608_Convert( p_block );
591 break;
592 case VLC_CODEC_SUBT:
593 if( p_track->fmt.i_original_fourcc == ATOM_wvtt )
594 p_block = MP4_WebVTT_Convert( p_demux, p_block );
595 break;
596 default:
597 p_block->i_buffer = 0;
598 break;
601 else if( p_track->fmt.i_original_fourcc == ATOM_rrtp )
603 p_block = MP4_RTPHint_Convert( p_demux, p_block, p_track->fmt.i_codec );
606 return p_block;
609 static void MP4_Block_Send( demux_t *p_demux, mp4_track_t *p_track, block_t *p_block )
611 p_block = MP4_Block_Convert( p_demux, p_track, p_block );
612 if( p_block == NULL )
613 return;
615 if ( p_track->b_chans_reorder )
617 aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
618 p_track->fmt.audio.i_channels,
619 p_track->rgi_chans_reordering,
620 p_track->fmt.i_codec );
623 p_block->i_flags |= p_track->i_block_flags;
624 if( p_track->i_next_block_flags )
626 p_block->i_flags |= p_track->i_next_block_flags;
627 p_track->i_next_block_flags = 0;
630 /* ASF packets in mov */
631 if( p_track->p_asf )
633 /* Fake a new stream from MP4 block */
634 stream_t *p_stream = p_demux->s;
635 p_demux->s = vlc_stream_MemoryNew( p_demux, p_block->p_buffer, p_block->i_buffer, true );
636 if ( p_demux->s )
638 p_track->i_dts_backup = p_block->i_dts;
639 p_track->i_pts_backup = p_block->i_pts;
640 /* And demux it as ASF packet */
641 DemuxASFPacket( &p_demux->p_sys->asfpacketsys, p_block->i_buffer, p_block->i_buffer );
642 vlc_stream_Delete(p_demux->s);
644 block_Release(p_block);
645 p_demux->s = p_stream;
647 else
648 es_out_Send( p_demux->out, p_track->p_es, p_block );
651 /*****************************************************************************
652 * Open: check file and initializes MP4 structures
653 *****************************************************************************/
654 static int Open( vlc_object_t * p_this )
656 demux_t *p_demux = (demux_t *)p_this;
657 demux_sys_t *p_sys;
659 const uint8_t *p_peek;
661 MP4_Box_t *p_ftyp;
662 MP4_Box_t *p_rmra;
663 const MP4_Box_t *p_mvhd = NULL;
664 const MP4_Box_t *p_mvex = NULL;
666 bool b_enabled_es;
668 /* A little test to see if it could be a mp4 */
669 if( vlc_stream_Peek( p_demux->s, &p_peek, 11 ) < 11 ) return VLC_EGENERIC;
671 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
673 case ATOM_moov:
674 case ATOM_foov:
675 case ATOM_moof:
676 case ATOM_mdat:
677 case ATOM_udta:
678 case ATOM_free:
679 case ATOM_skip:
680 case ATOM_wide:
681 case ATOM_uuid:
682 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
683 break;
684 case ATOM_ftyp:
685 /* We don't yet support f4v, but avformat does. */
686 if( p_peek[8] == 'f' && p_peek[9] == '4' && p_peek[10] == 'v' )
687 return VLC_EGENERIC;
688 break;
689 default:
690 return VLC_EGENERIC;
693 /* create our structure that will contains all data */
694 p_sys = calloc( 1, sizeof( demux_sys_t ) );
695 if ( !p_sys )
696 return VLC_EGENERIC;
698 /* I need to seek */
699 vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
700 if( p_sys->b_seekable )
701 vlc_stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable );
703 /*Set exported functions */
704 p_demux->pf_demux = Demux;
705 p_demux->pf_control = Control;
707 p_sys->context.i_lastseqnumber = UINT32_MAX;
709 p_demux->p_sys = p_sys;
711 if( LoadInitFrag( p_demux ) != VLC_SUCCESS )
712 goto error;
714 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
716 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
718 switch( BOXDATA(p_ftyp)->i_major_brand )
720 case MAJOR_isom:
721 msg_Dbg( p_demux,
722 "ISO Media (isom) version %d.",
723 BOXDATA(p_ftyp)->i_minor_version );
724 break;
725 case MAJOR_3gp4:
726 case MAJOR_3gp5:
727 case MAJOR_3gp6:
728 case MAJOR_3gp7:
729 msg_Dbg( p_demux, "3GPP Media Release: %4.4s",
730 (char *)&BOXDATA(p_ftyp)->i_major_brand );
731 break;
732 case MAJOR_qt__:
733 msg_Dbg( p_demux, "Apple QuickTime media" );
734 break;
735 case MAJOR_isml:
736 msg_Dbg( p_demux, "PIFF (= isml = fMP4) media" );
737 break;
738 case MAJOR_dash:
739 msg_Dbg( p_demux, "DASH Stream" );
740 break;
741 case MAJOR_M4A:
742 msg_Dbg( p_demux, "iTunes audio" );
743 if( var_InheritBool( p_demux, CFG_PREFIX"m4a-audioonly" ) )
744 p_sys->hacks.es_cat_filters = AUDIO_ES;
745 break;
746 default:
747 msg_Dbg( p_demux,
748 "unrecognized major media specification (%4.4s).",
749 (char*)&BOXDATA(p_ftyp)->i_major_brand );
750 break;
752 /* also lookup in compatibility list */
753 for(uint32_t i=0; i<BOXDATA(p_ftyp)->i_compatible_brands_count; i++)
755 if (BOXDATA(p_ftyp)->i_compatible_brands[i] == MAJOR_dash)
757 msg_Dbg( p_demux, "DASH Stream" );
759 else if (BOXDATA(p_ftyp)->i_compatible_brands[i] == VLC_FOURCC('s', 'm', 'o', 'o') )
761 msg_Dbg( p_demux, "Handling VLC Smooth Stream" );
765 else
767 msg_Dbg( p_demux, "file type box missing (assuming ISO Media)" );
770 /* the file need to have one moov box */
771 p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/moov" );
772 if( unlikely(!p_sys->p_moov) )
774 p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/foov" );
775 if( !p_sys->p_moov )
777 msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
778 goto error;
780 /* we have a free box as a moov, rename it */
781 p_sys->p_moov->i_type = ATOM_moov;
784 p_mvhd = MP4_BoxGet( p_sys->p_moov, "mvhd" );
785 if( p_mvhd && BOXDATA(p_mvhd) && BOXDATA(p_mvhd)->i_timescale )
787 p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
788 p_sys->i_moov_duration = p_sys->i_duration = BOXDATA(p_mvhd)->i_duration;
789 p_sys->i_cumulated_duration = BOXDATA(p_mvhd)->i_duration;
791 else
793 msg_Warn( p_demux, "No valid mvhd found" );
794 goto error;
797 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
799 int i_count = MP4_BoxCount( p_rmra, "rmda" );
800 int i;
802 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
804 input_thread_t *p_input = p_demux->p_input;
805 input_item_t *p_current = input_GetItem( p_input );
807 input_item_node_t *p_subitems = input_item_node_Create( p_current );
809 for( i = 0; i < i_count; i++ )
811 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
812 char *psz_ref;
813 uint32_t i_ref_type;
815 if( !p_rdrf || !BOXDATA(p_rdrf) || !( psz_ref = strdup( BOXDATA(p_rdrf)->psz_ref ) ) )
817 continue;
819 i_ref_type = BOXDATA(p_rdrf)->i_ref_type;
821 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
822 psz_ref, (char*)&i_ref_type );
824 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
826 if( strstr( psz_ref, "qt5gateQT" ) )
828 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
829 free( psz_ref );
830 continue;
832 if( !strncmp( psz_ref, "http://", 7 ) ||
833 !strncmp( psz_ref, "rtsp://", 7 ) )
837 else
839 char *psz_absolute;
840 char *psz_path = strdup( p_demux->psz_location );
841 char *end = strrchr( psz_path, '/' );
842 if( end ) end[1] = '\0';
843 else *psz_path = '\0';
845 if( asprintf( &psz_absolute, "%s://%s%s",
846 p_demux->psz_access, psz_path, psz_ref ) < 0 )
848 free( psz_ref );
849 free( psz_path );
850 input_item_node_Delete( p_subitems );
851 return VLC_ENOMEM;
854 free( psz_ref );
855 psz_ref = psz_absolute;
856 free( psz_path );
858 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
859 input_item_t *p_item = input_item_New( psz_ref, NULL );
860 input_item_CopyOptions( p_item, p_current );
861 input_item_node_AppendItem( p_subitems, p_item );
862 input_item_Release( p_item );
864 else
866 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
867 (char*)&BOXDATA(p_rdrf)->i_ref_type );
869 free( psz_ref );
872 /* FIXME: create a stream_filter sub-module for this */
873 if (es_out_Control(p_demux->out, ES_OUT_POST_SUBNODE, p_subitems))
874 input_item_node_Delete(p_subitems);
877 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
879 if( !p_rmra )
881 msg_Err( p_demux, "cannot find /moov/mvhd" );
882 goto error;
884 else
886 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
887 p_demux->pf_demux = DemuxRef;
888 return VLC_SUCCESS;
891 else
893 p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
894 if( p_sys->i_timescale == 0 )
896 msg_Err( p_this, "bad timescale" );
897 goto error;
901 const unsigned i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" );
902 if( i_tracks < 1 )
904 msg_Err( p_demux, "cannot find any /moov/trak" );
905 goto error;
907 msg_Dbg( p_demux, "found %u track%c", i_tracks, i_tracks ? 's':' ' );
909 if( CreateTracks( p_demux, i_tracks ) != VLC_SUCCESS )
910 goto error;
912 /* Search the first chap reference (like quicktime) and
913 * check that at least 1 stream is enabled */
914 p_sys->p_tref_chap = NULL;
915 b_enabled_es = false;
916 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
918 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
921 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
922 if( p_tkhd && BOXDATA(p_tkhd) && (BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED) )
923 b_enabled_es = true;
925 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
926 if( p_chap && p_chap->data.p_tref_generic &&
927 p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
928 p_sys->p_tref_chap = p_chap;
931 /* Set and store metadata */
932 if( (p_sys->p_meta = vlc_meta_New()) )
933 MP4_LoadMeta( p_sys, p_sys->p_meta );
935 /* now process each track and extract all useful information */
936 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
938 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%u]", i );
939 MP4_TrackSetup( p_demux, &p_sys->track[i], p_trak, true, !b_enabled_es );
941 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapters_source )
943 const char *psz_cat;
944 switch( p_sys->track[i].fmt.i_cat )
946 case( VIDEO_ES ):
947 psz_cat = "video";
948 break;
949 case( AUDIO_ES ):
950 psz_cat = "audio";
951 break;
952 case( SPU_ES ):
953 psz_cat = "subtitle";
954 break;
956 default:
957 psz_cat = "unknown";
958 break;
961 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
962 p_sys->track[i].i_track_ID, psz_cat,
963 p_sys->track[i].b_enable ? "enable":"disable",
964 p_sys->track[i].fmt.psz_language ?
965 p_sys->track[i].fmt.psz_language : "undef" );
967 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapters_source )
969 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
970 p_sys->track[i].i_track_ID,
971 p_sys->track[i].fmt.psz_language ?
972 p_sys->track[i].fmt.psz_language : "undef" );
974 else
976 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
977 p_sys->track[i].i_track_ID );
981 p_mvex = MP4_BoxGet( p_sys->p_moov, "mvex" );
982 if( p_mvex != NULL )
984 const MP4_Box_t *p_mehd = MP4_BoxGet( p_mvex, "mehd");
985 if ( p_mehd && BOXDATA(p_mehd) )
987 if( BOXDATA(p_mehd)->i_fragment_duration > p_sys->i_duration )
989 p_sys->b_fragmented = true;
990 p_sys->i_duration = BOXDATA(p_mehd)->i_fragment_duration;
994 const MP4_Box_t *p_sidx = MP4_BoxGet( p_sys->p_root, "sidx");
995 if( p_sidx )
996 p_sys->b_fragmented = true;
998 if ( p_sys->b_seekable )
1000 if( !p_sys->b_fragmented /* as unknown */ )
1002 /* Probe remaining to check if there's really fragments
1003 or if that file is just ready to append fragments */
1004 ProbeFragments( p_demux, (p_sys->i_duration == 0), &p_sys->b_fragmented );
1007 if( vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos ) != VLC_SUCCESS )
1008 goto error;
1010 else /* Handle as fragmented by default as we can't see moof */
1012 p_sys->context.p_fragment_atom = p_sys->p_moov;
1013 p_sys->context.i_current_box_type = ATOM_moov;
1014 p_sys->b_fragmented = true;
1018 if( p_sys->b_fragmented )
1020 p_demux->pf_demux = DemuxFrag;
1021 msg_Dbg( p_demux, "Set Fragmented demux mode" );
1024 if( !p_sys->b_seekable && p_demux->pf_demux == Demux )
1026 msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" );
1027 goto error;
1030 if( p_sys->i_tracks > 1 && !p_sys->b_fastseekable )
1032 uint64_t i_max_continuity;
1033 bool b_flat;
1034 MP4_GetInterleaving( p_demux, &i_max_continuity, &b_flat );
1035 if( b_flat )
1036 msg_Warn( p_demux, "that media doesn't look interleaved, will need to seek");
1037 else if( i_max_continuity > DEMUX_TRACK_MAX_PRELOAD )
1038 msg_Warn( p_demux, "that media doesn't look properly interleaved, will need to seek");
1041 /* */
1042 LoadChapter( p_demux );
1044 p_sys->asfpacketsys.p_demux = p_demux;
1045 p_sys->asfpacketsys.pi_preroll = &p_sys->i_preroll;
1046 p_sys->asfpacketsys.pi_preroll_start = &p_sys->i_preroll_start;
1047 p_sys->asfpacketsys.pf_doskip = NULL;
1048 p_sys->asfpacketsys.pf_send = MP4ASF_Send;
1049 p_sys->asfpacketsys.pf_gettrackinfo = MP4ASF_GetTrackInfo;
1050 p_sys->asfpacketsys.pf_updatetime = NULL;
1051 p_sys->asfpacketsys.pf_setaspectratio = NULL;
1053 return VLC_SUCCESS;
1055 error:
1056 if( vlc_stream_Tell( p_demux->s ) > 0 )
1058 if( vlc_stream_Seek( p_demux->s, 0 ) != VLC_SUCCESS )
1059 msg_Warn( p_demux, "Can't reset stream position from probing" );
1062 Close( p_this );
1064 return VLC_EGENERIC;
1067 const unsigned int SAMPLEHEADERSIZE = 4;
1068 const unsigned int RTPPACKETSIZE = 12;
1069 const unsigned int CONSTRUCTORSIZE = 16;
1071 /*******************************************************************************
1072 * MP4_RTPHintToFrame: converts RTP Reception Hint Track sample to H.264 frame
1073 *******************************************************************************/
1074 static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount )
1076 uint8_t *p_slice = p_block->p_buffer + SAMPLEHEADERSIZE;
1077 block_t *p_newblock = NULL;
1078 size_t i_payload = 0;
1080 if( p_block->i_buffer < SAMPLEHEADERSIZE + RTPPACKETSIZE + CONSTRUCTORSIZE )
1082 msg_Err( p_demux, "Sample not large enough for necessary structs");
1083 block_Release( p_block );
1084 return NULL;
1087 for( uint32_t i = 0; i < packetcount; ++i )
1089 if( (size_t)(p_slice - p_block->p_buffer) + RTPPACKETSIZE + CONSTRUCTORSIZE > p_block->i_buffer )
1090 goto error;
1092 /* skip RTP header in sample. Could be used to detect packet losses */
1093 p_slice += RTPPACKETSIZE;
1095 mp4_rtpsampleconstructor_t sample_cons;
1097 sample_cons.type = p_slice[0];
1098 sample_cons.trackrefindex = p_slice[1];
1099 sample_cons.length = GetWBE( &p_slice[2] );
1100 sample_cons.samplenumber = GetDWBE( &p_slice[4] );
1101 sample_cons.sampleoffset = GetDWBE( &p_slice[8] );
1102 sample_cons.bytesperblock = GetWBE( &p_slice[12] );
1103 sample_cons.samplesperblock = GetWBE( &p_slice[14] );
1105 /* skip packet constructor */
1106 p_slice += CONSTRUCTORSIZE;
1108 /* check that is RTPsampleconstructor, referencing itself and no weird audio stuff */
1109 if( sample_cons.type != 2||sample_cons.trackrefindex != -1
1110 ||sample_cons.samplesperblock != 1||sample_cons.bytesperblock != 1 )
1112 msg_Err(p_demux, "Unhandled constructor in RTP Reception Hint Track. Type:%u", sample_cons.type);
1113 goto error;
1116 /* slice doesn't fit in buffer */
1117 if( sample_cons.sampleoffset + sample_cons.length > p_block->i_buffer)
1119 msg_Err(p_demux, "Sample buffer is smaller than sample" );
1120 goto error;
1123 block_t *p_realloc = ( p_newblock ) ?
1124 block_Realloc( p_newblock, 0, i_payload + sample_cons.length + 4 ):
1125 block_Alloc( i_payload + sample_cons.length + 4 );
1126 if( !p_realloc )
1127 goto error;
1129 p_newblock = p_realloc;
1130 uint8_t *p_dst = &p_newblock->p_buffer[i_payload];
1132 const uint8_t* p_src = p_block->p_buffer + sample_cons.sampleoffset;
1133 uint8_t i_type = (*p_src) & ((1<<5)-1);
1135 const uint8_t synccode[4] = { 0, 0, 0, 1 };
1136 if( memcmp( p_src, synccode, 4 ) )
1138 if( i_type == 7 || i_type == 8 )
1139 *p_dst++=0;
1141 p_dst[0] = 0;
1142 p_dst[1] = 0;
1143 p_dst[2] = 1;
1144 p_dst += 3;
1147 memcpy( p_dst, p_src, sample_cons.length );
1148 p_dst += sample_cons.length;
1150 i_payload = p_dst - p_newblock->p_buffer;
1153 block_Release( p_block );
1154 if( p_newblock )
1155 p_newblock->i_buffer = i_payload;
1156 return p_newblock;
1158 error:
1159 block_Release( p_block );
1160 if( p_newblock )
1161 block_Release( p_newblock );
1162 return NULL;
1165 /* RTP Reception Hint Track */
1166 static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec )
1168 block_t *p_converted = NULL;
1169 if( p_block->i_buffer < 2 )
1171 block_Release( p_block );
1172 return NULL;
1175 /* number of RTP packets contained in this sample */
1176 const uint16_t i_packets = GetWBE( p_block->p_buffer );
1177 if( i_packets <= 1 || i_codec != VLC_CODEC_H264 )
1179 const size_t i_skip = SAMPLEHEADERSIZE + i_packets * ( RTPPACKETSIZE + CONSTRUCTORSIZE );
1180 if( i_packets == 1 && i_skip < p_block->i_buffer )
1182 p_block->p_buffer += i_skip;
1183 p_converted = p_block;
1185 else
1187 block_Release( p_block );
1190 else
1192 p_converted = MP4_RTPHintToFrame( p_demux, p_block, i_packets );
1195 return p_converted;
1198 /*****************************************************************************
1199 * Demux: read packet and send them to decoders
1200 *****************************************************************************
1201 * TODO check for newly selected track (ie audio upt to now )
1202 *****************************************************************************/
1203 static int DemuxTrack( demux_t *p_demux, mp4_track_t *tk, uint64_t i_readpos,
1204 unsigned i_max_preload )
1206 uint32_t i_nb_samples = 0;
1207 uint32_t i_samplessize = 0;
1209 if( !tk->b_ok || tk->i_sample >= tk->i_sample_count )
1210 return VLC_DEMUXER_EOS;
1212 if( tk->b_chapters_source )
1213 return VLC_DEMUXER_SUCCESS;
1215 uint32_t i_run_seq = MP4_TrackGetRunSeq( tk );
1216 mtime_t i_current_nzdts = MP4_TrackGetDTS( p_demux, tk );
1217 const mtime_t i_demux_max_nzdts =(i_max_preload < UINT_MAX)
1218 ? i_current_nzdts + i_max_preload
1219 : INT64_MAX;
1221 for( ; i_demux_max_nzdts >= i_current_nzdts; )
1223 if( tk->i_sample >= tk->i_sample_count )
1224 return VLC_DEMUXER_EOS;
1226 #if 0
1227 msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, tk->i_track_ID,
1228 MP4_TrackGetDTS( p_demux, tk ),
1229 MP4_GetMoviePTS( p_demux->p_sys ), i_readpos );
1230 #endif
1232 i_samplessize = MP4_TrackGetReadSize( tk, &i_nb_samples );
1233 if( i_samplessize > 0 )
1235 block_t *p_block;
1236 int64_t i_delta;
1238 if( vlc_stream_Tell( p_demux->s ) != i_readpos )
1240 if( MP4_Seek( p_demux->s, i_readpos ) != VLC_SUCCESS )
1242 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1243 ": Failed to seek to %"PRIu64,
1244 tk->i_track_ID, i_readpos );
1245 MP4_TrackSelect( p_demux, tk, false );
1246 goto end;
1250 /* now read pes */
1251 if( !(p_block = vlc_stream_Block( p_demux->s, i_samplessize )) )
1253 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1254 ": Failed to read %d bytes sample at %"PRIu64,
1255 tk->i_track_ID, i_samplessize, i_readpos );
1256 MP4_TrackSelect( p_demux, tk, false );
1257 goto end;
1260 /* !important! Ensure clock is set before sending data */
1261 if( p_demux->p_sys->i_pcr == VLC_TS_INVALID )
1263 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + i_current_nzdts );
1264 p_demux->p_sys->i_pcr = VLC_TS_0 + i_current_nzdts;
1267 /* dts */
1268 p_block->i_dts = VLC_TS_0 + i_current_nzdts;
1269 /* pts */
1270 if( MP4_TrackGetPTSDelta( p_demux, tk, &i_delta ) )
1271 p_block->i_pts = p_block->i_dts + i_delta;
1272 else if( tk->fmt.i_cat != VIDEO_ES )
1273 p_block->i_pts = p_block->i_dts;
1274 else
1275 p_block->i_pts = VLC_TS_INVALID;
1277 MP4_Block_Send( p_demux, tk, p_block );
1280 /* Next sample */
1281 if ( i_nb_samples ) /* sample size could be 0, need to go fwd. see return */
1282 MP4_TrackNextSample( p_demux, tk, i_nb_samples );
1284 uint32_t i_next_run_seq = MP4_TrackGetRunSeq( tk );
1285 if( i_next_run_seq != i_run_seq )
1286 break;
1288 i_current_nzdts = MP4_TrackGetDTS( p_demux, tk );
1289 i_readpos = MP4_TrackGetPos( tk );
1292 return VLC_DEMUXER_SUCCESS;
1294 end:
1295 return VLC_DEMUXER_EGENERIC;
1298 static int DemuxMoov( demux_t *p_demux )
1300 demux_sys_t *p_sys = p_demux->p_sys;
1301 unsigned int i_track;
1303 /* check for newly selected/unselected track */
1304 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1306 mp4_track_t *tk = &p_sys->track[i_track];
1307 bool b = true;
1309 if( !tk->b_ok || tk->b_chapters_source ||
1310 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
1312 continue;
1315 if( p_sys->b_seekable )
1316 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1318 if( tk->b_selected && !b )
1320 MP4_TrackSelect( p_demux, tk, false );
1322 else if( !tk->b_selected && b)
1324 MP4_TrackSeek( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
1328 const mtime_t i_nztime = MP4_GetMoviePTS( p_sys );
1330 /* We demux/set pcr, even without selected tracks, (empty edits, ...) */
1331 if( p_sys->i_pcr != VLC_TS_INVALID /* not after a seek */ )
1333 bool b_eof = true;
1334 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1336 mp4_track_t *tk = &p_sys->track[i_track];
1337 if( !tk->b_ok || tk->b_chapters_source || tk->i_sample >= tk->i_sample_count )
1338 continue;
1339 /* Test for EOF on each track (samples count, edit list) */
1340 b_eof &= ( i_nztime > MP4_TrackGetDTS( p_demux, tk ) );
1342 if( b_eof )
1343 return VLC_DEMUXER_EOS;
1346 const unsigned i_max_preload = ( p_sys->b_fastseekable ) ? 0 : ( p_sys->b_seekable ) ? DEMUX_TRACK_MAX_PRELOAD : UINT_MAX;
1347 const mtime_t i_scaledincrement = DEMUX_INCREMENT * p_sys->i_timescale / CLOCK_FREQ;
1348 int i_status;
1349 /* demux up to increment amount of data on every track, or just set pcr if empty data */
1350 for( ;; )
1352 mp4_track_t *tk = NULL;
1353 i_status = VLC_DEMUXER_EOS;
1355 /* First pass, find any track within our target increment, ordered by position */
1356 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1358 mp4_track_t *tk_tmp = &p_sys->track[i_track];
1359 if( !tk_tmp->b_ok || tk_tmp->b_chapters_source ||
1360 tk_tmp->i_sample >= tk_tmp->i_sample_count ||
1361 (!tk_tmp->b_selected && p_sys->b_seekable) )
1362 continue;
1364 /* At least still have data to demux on this or next turns */
1365 i_status = VLC_DEMUXER_SUCCESS;
1367 if ( MP4_TrackGetDTS( p_demux, tk_tmp ) <= i_nztime + DEMUX_INCREMENT )
1369 if( tk == NULL || MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) )
1370 tk = tk_tmp;
1374 if( tk )
1376 /* Second pass, refine and find any best candidate having a chunk pos closer than
1377 * current candidate (avoids seeks when increment falls between the 2) from
1378 * current position, but within extended interleave time */
1379 for( i_track = 0; i_max_preload > 0 && i_track < p_sys->i_tracks; i_track++ )
1381 mp4_track_t *tk_tmp = &p_sys->track[i_track];
1382 if( tk_tmp == tk ||
1383 !tk_tmp->b_ok || tk_tmp->b_chapters_source ||
1384 (!tk_tmp->b_selected && p_sys->b_seekable) ||
1385 tk_tmp->i_sample >= tk_tmp->i_sample_count )
1386 continue;
1388 mtime_t i_nzdts = MP4_TrackGetDTS( p_demux, tk_tmp );
1389 if ( i_nzdts <= i_nztime + DEMUX_TRACK_MAX_PRELOAD )
1391 /* Found a better candidate to avoid seeking */
1392 if( MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) )
1393 tk = tk_tmp;
1394 /* Note: previous candidate will be repicked on next loop */
1398 uint64_t i_pos = MP4_TrackGetPos( tk );
1399 int i_ret = DemuxTrack( p_demux, tk, i_pos, i_max_preload );
1401 if( i_ret == VLC_DEMUXER_SUCCESS )
1402 i_status = VLC_DEMUXER_SUCCESS;
1405 if( i_status != VLC_DEMUXER_SUCCESS || !tk )
1406 break;
1409 p_sys->i_time += __MAX(i_scaledincrement, 1);
1410 if( p_sys->i_pcr > VLC_TS_INVALID )
1412 p_sys->i_pcr = VLC_TS_0 + MP4_rescale( p_sys->i_time, p_sys->i_timescale, CLOCK_FREQ );
1413 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
1416 /* */
1417 MP4_UpdateSeekpoint( p_demux, i_nztime + DEMUX_INCREMENT );
1419 return i_status;
1422 static int Demux( demux_t *p_demux )
1424 demux_sys_t *p_sys = p_demux->p_sys;
1426 assert( ! p_sys->b_fragmented );
1428 int i_status = DemuxMoov( p_demux );
1430 if( i_status == VLC_DEMUXER_EOS )
1431 i_status = VLC_DEMUXER_EOF;
1433 return i_status;
1436 static void MP4_UpdateSeekpoint( demux_t *p_demux, int64_t i_time )
1438 demux_sys_t *p_sys = p_demux->p_sys;
1439 int i;
1440 if( !p_sys->p_title )
1441 return;
1442 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
1444 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
1445 break;
1447 i--;
1449 if( i != p_demux->info.i_seekpoint && i >= 0 )
1451 p_demux->info.i_seekpoint = i;
1452 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1455 /*****************************************************************************
1456 * Seek: Go to i_date
1457 ******************************************************************************/
1458 static int Seek( demux_t *p_demux, mtime_t i_date )
1460 demux_sys_t *p_sys = p_demux->p_sys;
1461 unsigned int i_track;
1463 /* First update global time */
1464 p_sys->i_time = MP4_rescale( i_date, CLOCK_FREQ, p_sys->i_timescale );
1465 p_sys->i_pcr = VLC_TS_INVALID;
1467 /* Now for each stream try to go to this time */
1468 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1470 mp4_track_t *tk = &p_sys->track[i_track];
1471 MP4_TrackSeek( p_demux, tk, i_date );
1473 MP4_UpdateSeekpoint( p_demux, i_date );
1475 MP4ASF_ResetFrames( p_sys );
1476 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1478 return VLC_SUCCESS;
1481 static int FragPrepareChunk( demux_t *p_demux, MP4_Box_t *p_moof,
1482 MP4_Box_t *p_sidx, stime_t i_moof_time, bool b_discontinuity )
1484 demux_sys_t *p_sys = p_demux->p_sys;
1486 if( FragCreateTrunIndex( p_demux, p_moof, p_sidx, i_moof_time, b_discontinuity ) == VLC_SUCCESS )
1488 for( unsigned i=0; i<p_sys->i_tracks; i++ )
1490 mp4_track_t *p_track = &p_sys->track[i];
1491 if( p_track->context.runs.i_count )
1493 const mp4_run_t *p_run = &p_track->context.runs.p_array[0];
1494 p_track->context.i_trun_sample_pos = p_run->i_offset;
1495 p_track->context.i_trun_sample = 0;
1496 p_track->i_time = p_run->i_first_dts;
1499 return VLC_SUCCESS;
1502 return VLC_EGENERIC;
1505 static stime_t FragGetDemuxTimeFromTracksTime( demux_sys_t *p_sys )
1507 stime_t i_time = INT64_MAX;
1508 for( unsigned int i = 0; i < p_sys->i_tracks; i++ )
1510 if( p_sys->track[i].context.runs.i_count == 0 )
1511 continue;
1512 stime_t i_ttime = MP4_rescale( p_sys->track[i].i_time,
1513 p_sys->track[i].i_timescale, p_sys->i_timescale );
1514 i_time = __MIN( i_time, i_ttime );
1516 return i_time;
1519 static uint32_t FragGetMoofSequenceNumber( MP4_Box_t *p_moof )
1521 const MP4_Box_t *p_mfhd = MP4_BoxGet( p_moof, "mfhd" );
1522 if( p_mfhd && BOXDATA(p_mfhd) )
1523 return BOXDATA(p_mfhd)->i_sequence_number;
1524 return 0;
1527 static int FragSeekLoadFragment( demux_t *p_demux, uint32_t i_moox, stime_t i_moox_time )
1529 demux_sys_t *p_sys = p_demux->p_sys;
1530 MP4_Box_t *p_moox;
1532 if( i_moox == ATOM_moov )
1534 p_moox = p_sys->p_moov;
1536 else
1538 const uint8_t *p_peek;
1539 if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) != 8 )
1540 return VLC_EGENERIC;
1542 if( ATOM_moof != VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
1543 return VLC_EGENERIC;
1545 MP4_Box_t *p_vroot = MP4_BoxGetNextChunk( p_demux->s );
1546 if(!p_vroot)
1547 return VLC_EGENERIC;
1548 p_moox = MP4_BoxExtract( &p_vroot->p_first, ATOM_moof );
1549 MP4_BoxFree( p_vroot );
1551 if(!p_moox)
1552 return VLC_EGENERIC;
1555 FragResetContext( p_sys );
1557 /* map context */
1558 p_sys->context.p_fragment_atom = p_moox;
1559 p_sys->context.i_current_box_type = i_moox;
1561 if( i_moox == ATOM_moof )
1563 FragPrepareChunk( p_demux, p_moox, NULL, i_moox_time, true );
1564 p_sys->context.i_lastseqnumber = FragGetMoofSequenceNumber( p_moox );
1566 p_sys->i_time = FragGetDemuxTimeFromTracksTime( p_sys );
1567 p_sys->i_pcr = VLC_TS_INVALID;
1570 msg_Dbg( p_demux, "seeked to %4.4s at pos %" PRIu64, (char *) &i_moox, p_moox->i_pos );
1571 return VLC_SUCCESS;
1574 static unsigned GetSeekTrackIndex( demux_sys_t *p_sys )
1576 unsigned cand = 0;
1577 for( unsigned i=0; i<p_sys->i_tracks; i++ )
1579 if( p_sys->track[i].fmt.i_cat == VIDEO_ES ||
1580 p_sys->track[i].fmt.i_cat == AUDIO_ES )
1582 if( cand != i && !p_sys->track[cand].b_selected )
1583 cand = i;
1586 return cand;
1589 static void FragTrunSeekToTime( mp4_track_t *p_track, stime_t i_target_time )
1591 if( !p_track->b_ok || p_track->context.runs.i_count < 1 )
1592 return;
1594 unsigned i_run = 0;
1595 unsigned i_sample = 0;
1596 uint64_t i_pos = p_track->context.runs.p_array[0].i_offset;
1597 stime_t i_time = p_track->context.runs.p_array[0].i_first_dts;
1599 for( unsigned r = 0; r < p_track->context.runs.i_count; r++ )
1601 const mp4_run_t *p_run = &p_track->context.runs.p_array[r];
1602 const MP4_Box_data_trun_t *p_data =
1603 p_track->context.runs.p_array[r].p_trun->data.p_trun;
1604 if( i_time > i_target_time )
1605 break;
1607 i_run = r;
1608 i_time = p_run->i_first_dts;
1609 i_pos = p_run->i_offset;
1610 i_sample = 0;
1612 uint32_t dur = p_track->context.i_default_sample_duration;
1613 uint32_t len = p_track->context.i_default_sample_size;
1614 for ( unsigned i=0; i<p_data->i_sample_count; i++ )
1616 if( p_data->i_flags & MP4_TRUN_SAMPLE_DURATION )
1617 dur = p_data->p_samples[i].i_duration;
1619 /* check condition */
1620 if( i_time + dur > i_target_time )
1621 break;
1623 if( p_data->i_flags & MP4_TRUN_SAMPLE_SIZE )
1624 len = p_data->p_samples[i].i_size;
1626 i_time += dur;
1627 i_pos += len;
1631 p_track->context.i_trun_sample = i_sample;
1632 p_track->context.i_trun_sample_pos = i_pos;
1633 p_track->context.runs.i_current = i_run;
1636 static int FragSeekToTime( demux_t *p_demux, mtime_t i_nztime )
1638 demux_sys_t *p_sys = p_demux->p_sys;
1639 uint64_t i64 = UINT64_MAX;
1640 uint32_t i_segment_type = ATOM_moof;
1641 stime_t i_segment_time = INT64_MAX;
1642 mtime_t i_sync_time = i_nztime;
1643 bool b_iframesync = false;
1645 const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1646 if ( !p_sys->i_timescale || !i_duration || !p_sys->b_seekable )
1647 return VLC_EGENERIC;
1649 uint64_t i_backup_pos = vlc_stream_Tell( p_demux->s );
1651 if ( !p_sys->b_fragments_probed && !p_sys->b_index_probed && p_sys->b_seekable )
1653 ProbeIndex( p_demux );
1654 p_sys->b_index_probed = true;
1657 const unsigned i_seek_track_index = GetSeekTrackIndex( p_sys );
1658 const unsigned i_seek_track_ID = p_sys->track[i_seek_track_index].i_track_ID;
1660 if( MP4_rescale( i_nztime, CLOCK_FREQ, p_sys->i_timescale )
1661 < GetMoovTrackDuration( p_sys, i_seek_track_ID ) )
1663 i64 = p_sys->p_moov->i_pos;
1664 i_segment_type = ATOM_moov;
1666 else if( FragGetMoofBySidxIndex( p_demux, i_nztime, &i64, &i_sync_time ) == VLC_SUCCESS )
1668 /* provides base offset */
1669 i_segment_time = i_sync_time;
1670 msg_Dbg( p_demux, "seeking to sidx moof pos %" PRId64 " %" PRId64, i64, i_sync_time );
1672 else
1674 bool b_buildindex = false;
1676 if( FragGetMoofByTfraIndex( p_demux, i_nztime, i_seek_track_ID, &i64, &i_sync_time ) == VLC_SUCCESS )
1678 /* Does only provide segment position and a sync sample time */
1679 msg_Dbg( p_demux, "seeking to sync point %" PRId64, i_sync_time );
1680 b_iframesync = true;
1682 else if( !p_sys->b_fragments_probed && !p_sys->b_fastseekable )
1684 const char *psz_msg = _(
1685 "Because this file index is broken or missing, "
1686 "seeking will not work correctly.\n"
1687 "VLC won't repair your file but can temporary fix this "
1688 "problem by building an index in memory.\n"
1689 "This step might take a long time on a large file.\n"
1690 "What do you want to do?");
1691 b_buildindex = vlc_dialog_wait_question( p_demux,
1692 VLC_DIALOG_QUESTION_NORMAL,
1693 _("Do not seek"),
1694 _("Build index"),
1695 NULL,
1696 _("Broken or missing Index"),
1697 "%s", psz_msg );
1700 if( !p_sys->b_fragments_probed && ( p_sys->b_fastseekable || b_buildindex ) )
1702 bool foo;
1703 int i_ret = vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos + p_sys->p_moov->i_size );
1704 if( i_ret == VLC_SUCCESS )
1706 i_ret = ProbeFragments( p_demux, true, &foo );
1707 p_sys->b_fragments_probed = true;
1709 if( i_ret != VLC_SUCCESS )
1711 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1712 return i_ret;
1716 if( p_sys->b_fragments_probed && p_sys->p_fragsindex )
1718 stime_t i_basetime = MP4_rescale( i_sync_time, CLOCK_FREQ, p_sys->i_timescale );
1719 if( !MP4_Fragments_Index_Lookup( p_sys->p_fragsindex, &i_basetime, &i64, i_seek_track_index ) )
1721 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1722 return VLC_EGENERIC;
1724 msg_Dbg( p_demux, "seeking to fragment index pos %" PRId64 " %" PRId64, i64,
1725 MP4_rescale( i_basetime, p_sys->i_timescale, CLOCK_FREQ ) );
1729 if( i64 == UINT64_MAX )
1731 msg_Warn( p_demux, "seek by index failed" );
1732 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1733 return VLC_EGENERIC;
1736 msg_Dbg( p_demux, "final seek to fragment at %"PRId64, i64 );
1737 if( vlc_stream_Seek( p_demux->s, i64 ) )
1739 msg_Err( p_demux, "seek failed to %"PRId64, i64 );
1740 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1741 return VLC_EGENERIC;
1744 /* Context is killed on success */
1745 if( FragSeekLoadFragment( p_demux, i_segment_type, i_segment_time ) != VLC_SUCCESS )
1747 p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
1748 return VLC_EGENERIC;
1751 p_sys->i_pcr = VLC_TS_INVALID;
1753 for( unsigned i=0; i<p_sys->i_tracks; i++ )
1755 if( i_segment_type == ATOM_moov )
1757 MP4_TrackSeek( p_demux, &p_sys->track[i], i_sync_time );
1758 p_sys->i_time = MP4_rescale( i_sync_time, CLOCK_FREQ, p_sys->i_timescale );
1759 p_sys->i_pcr = VLC_TS_INVALID;
1761 else if( b_iframesync )
1763 stime_t i_tst = MP4_rescale( i_sync_time, CLOCK_FREQ, p_sys->track[i].i_timescale );
1764 FragTrunSeekToTime( &p_sys->track[i], i_tst );
1765 p_sys->track[i].i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1769 MP4ASF_ResetFrames( p_sys );
1770 /* And set next display time in that trun/fragment */
1771 if( b_iframesync )
1772 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TS_0 + i_nztime );
1773 return VLC_SUCCESS;
1776 static int FragSeekToPos( demux_t *p_demux, double f )
1778 demux_sys_t *p_sys = p_demux->p_sys;
1779 const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1781 if ( !p_sys->b_seekable || !p_sys->i_timescale || !i_duration )
1782 return VLC_EGENERIC;
1784 return FragSeekToTime( p_demux, (mtime_t)( f *
1785 MP4_rescale( i_duration, p_sys->i_timescale, CLOCK_FREQ ) ) );
1788 static bool imageTypeCompatible( const MP4_Box_data_data_t *p_data )
1790 return p_data && (
1791 p_data->e_wellknowntype == DATA_WKT_PNG ||
1792 p_data->e_wellknowntype == DATA_WKT_JPEG ||
1793 p_data->e_wellknowntype == DATA_WKT_BMP );
1796 static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta )
1798 MP4_Box_t *p_data = NULL;
1799 MP4_Box_t *p_udta = NULL;
1800 bool b_attachment_set = false;
1802 if( !p_meta )
1803 return VLC_EGENERIC;
1805 for( int i_index = 0; psz_meta_roots[i_index] && !p_udta; i_index++ )
1807 p_udta = MP4_BoxGet( p_sys->p_root, psz_meta_roots[i_index] );
1808 if ( p_udta )
1810 p_data = MP4_BoxGet( p_udta, "covr/data" );
1811 if ( p_data && imageTypeCompatible( BOXDATA(p_data) ) )
1813 char *psz_attachment;
1814 if ( -1 != asprintf( &psz_attachment, "attachment://%s/covr/data[0]",
1815 psz_meta_roots[i_index] ) )
1817 vlc_meta_SetArtURL( p_meta, psz_attachment );
1818 b_attachment_set = true;
1819 free( psz_attachment );
1825 const MP4_Box_t *p_pnot;
1826 if ( !b_attachment_set && (p_pnot = MP4_BoxGet( p_sys->p_root, "pnot" )) )
1828 for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms) && !b_attachment_set; i++ )
1830 if ( rgi_pict_atoms[i] == BOXDATA(p_pnot)->i_type )
1832 char rgsz_path[26];
1833 snprintf( rgsz_path, 26, "attachment://%4.4s[%"PRIu16"]",
1834 (char*)&rgi_pict_atoms[i], BOXDATA(p_pnot)->i_index - 1 );
1835 vlc_meta_SetArtURL( p_meta, rgsz_path );
1836 b_attachment_set = true;
1841 if( p_udta == NULL )
1843 if( !b_attachment_set )
1844 return VLC_EGENERIC;
1846 else SetupMeta( p_meta, p_udta );
1848 return VLC_SUCCESS;
1851 /*****************************************************************************
1852 * Control:
1853 *****************************************************************************/
1854 static int Control( demux_t *p_demux, int i_query, va_list args )
1856 demux_sys_t *p_sys = p_demux->p_sys;
1858 double f, *pf;
1859 int64_t i64, *pi64;
1861 const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
1863 switch( i_query )
1865 case DEMUX_CAN_SEEK:
1866 *va_arg( args, bool * ) = p_sys->b_seekable;
1867 return VLC_SUCCESS;
1869 case DEMUX_GET_POSITION:
1870 pf = va_arg( args, double * );
1871 if( i_duration > 0 )
1873 *pf = (double)p_sys->i_time / (double)i_duration;
1875 else
1877 *pf = 0.0;
1879 return VLC_SUCCESS;
1881 case DEMUX_SET_POSITION:
1882 f = va_arg( args, double );
1883 if ( p_demux->pf_demux == DemuxFrag )
1884 return FragSeekToPos( p_demux, f );
1885 else if( p_sys->i_timescale > 0 )
1887 i64 = (int64_t)( f * MP4_rescale( p_sys->i_duration,
1888 p_sys->i_timescale, CLOCK_FREQ ) );
1889 return Seek( p_demux, i64 );
1891 else return VLC_EGENERIC;
1893 case DEMUX_GET_TIME:
1894 pi64 = va_arg( args, int64_t * );
1895 if( p_sys->i_timescale > 0 )
1897 *pi64 = MP4_rescale( p_sys->i_time,
1898 p_sys->i_timescale, CLOCK_FREQ );
1900 else *pi64 = 0;
1901 return VLC_SUCCESS;
1903 case DEMUX_SET_TIME:
1904 i64 = va_arg( args, int64_t );
1905 if ( p_demux->pf_demux == DemuxFrag )
1906 return FragSeekToTime( p_demux, i64 );
1907 else
1908 return Seek( p_demux, i64 );
1910 case DEMUX_GET_LENGTH:
1911 pi64 = va_arg( args, int64_t * );
1912 if( p_sys->i_timescale > 0 )
1914 *pi64 = MP4_rescale( i_duration,
1915 p_sys->i_timescale, CLOCK_FREQ );
1917 else *pi64 = 0;
1918 return VLC_SUCCESS;
1920 case DEMUX_GET_FPS:
1921 pf = va_arg( args, double * );
1922 *pf = p_sys->f_fps;
1923 return VLC_SUCCESS;
1925 case DEMUX_GET_ATTACHMENTS:
1927 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1928 int *pi_int = va_arg( args, int * );
1930 MP4_Box_t *p_udta = NULL;
1931 size_t i_count = 0;
1932 int i_index = 0;
1934 /* Count number of total attachments */
1935 for( ; psz_meta_roots[i_index] && !p_udta; i_index++ )
1937 p_udta = MP4_BoxGet( p_sys->p_root, psz_meta_roots[i_index] );
1938 if ( p_udta )
1939 i_count += MP4_BoxCount( p_udta, "covr/data" );
1942 for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms); i++ )
1944 char rgsz_path[5];
1945 snprintf( rgsz_path, 5, "%4.4s", (char*)&rgi_pict_atoms[i] );
1946 i_count += MP4_BoxCount( p_sys->p_root, rgsz_path );
1949 if ( i_count == 0 )
1950 return VLC_EGENERIC;
1952 *ppp_attach = (input_attachment_t**)
1953 malloc( sizeof(input_attachment_t*) * i_count );
1954 if( !(*ppp_attach) ) return VLC_ENOMEM;
1956 /* First add cover attachments */
1957 i_count = 0;
1958 size_t i_box_count = 0;
1959 if ( p_udta )
1961 const MP4_Box_t *p_data = MP4_BoxGet( p_udta, "covr/data" );
1962 for( ; p_data; p_data = p_data->p_next )
1964 char *psz_mime;
1965 char *psz_filename;
1966 i_box_count++;
1968 if ( p_data->i_type != ATOM_data || !imageTypeCompatible( BOXDATA(p_data) ) )
1969 continue;
1971 switch( BOXDATA(p_data)->e_wellknowntype )
1973 case DATA_WKT_PNG:
1974 psz_mime = strdup( "image/png" );
1975 break;
1976 case DATA_WKT_JPEG:
1977 psz_mime = strdup( "image/jpeg" );
1978 break;
1979 case DATA_WKT_BMP:
1980 psz_mime = strdup( "image/bmp" );
1981 break;
1982 default:
1983 continue;
1986 if ( asprintf( &psz_filename, "%s/covr/data[%"PRIu64"]", psz_meta_roots[i_index - 1],
1987 (uint64_t) i_box_count - 1 ) >= 0 )
1989 (*ppp_attach)[i_count++] =
1990 vlc_input_attachment_New( psz_filename, psz_mime, "Cover picture",
1991 BOXDATA(p_data)->p_blob, BOXDATA(p_data)->i_blob );
1992 msg_Dbg( p_demux, "adding attachment %s", psz_filename );
1993 free( psz_filename );
1996 free( psz_mime );
2000 /* Then quickdraw pict ones */
2001 for ( size_t i=0; i< ARRAY_SIZE(rgi_pict_atoms); i++ )
2003 char rgsz_path[5];
2004 snprintf( rgsz_path, 5, "%4.4s", (char*)&rgi_pict_atoms[i] );
2005 const MP4_Box_t *p_pict = MP4_BoxGet( p_sys->p_root, rgsz_path );
2006 i_box_count = 0;
2007 for( ; p_pict; p_pict = p_pict->p_next )
2009 if ( i_box_count++ == UINT16_MAX ) /* pnot only handles 2^16 */
2010 break;
2011 if ( p_pict->i_type != rgi_pict_atoms[i] )
2012 continue;
2013 char rgsz_location[12];
2014 snprintf( rgsz_location, 12, "%4.4s[%"PRIu16"]", (char*)&rgi_pict_atoms[i],
2015 (uint16_t) i_box_count - 1 );
2016 (*ppp_attach)[i_count] = vlc_input_attachment_New( rgsz_location, "image/x-pict",
2017 "Quickdraw image", p_pict->data.p_binary->p_blob, p_pict->data.p_binary->i_blob );
2018 if ( !(*ppp_attach)[i_count] )
2020 i_count = 0;
2021 break;
2023 i_count++;
2024 msg_Dbg( p_demux, "adding attachment %s", rgsz_location );
2028 if ( i_count == 0 )
2030 free( *ppp_attach );
2031 return VLC_EGENERIC;
2034 *pi_int = i_count;
2036 return VLC_SUCCESS;
2039 case DEMUX_GET_META:
2041 vlc_meta_t *p_meta = va_arg( args, vlc_meta_t *);
2043 if( !p_sys->p_meta )
2044 return VLC_EGENERIC;
2046 vlc_meta_Merge( p_meta, p_sys->p_meta );
2048 return VLC_SUCCESS;
2051 case DEMUX_GET_TITLE_INFO:
2053 input_title_t ***ppp_title = va_arg( args, input_title_t *** );
2054 int *pi_int = va_arg( args, int* );
2055 int *pi_title_offset = va_arg( args, int* );
2056 int *pi_seekpoint_offset = va_arg( args, int* );
2058 if( !p_sys->p_title )
2059 return VLC_EGENERIC;
2061 *pi_int = 1;
2062 *ppp_title = malloc( sizeof( input_title_t*) );
2063 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
2064 *pi_title_offset = 0;
2065 *pi_seekpoint_offset = 0;
2066 return VLC_SUCCESS;
2068 case DEMUX_SET_TITLE:
2070 const int i_title = va_arg( args, int );
2071 if( !p_sys->p_title || i_title != 0 )
2072 return VLC_EGENERIC;
2073 return VLC_SUCCESS;
2075 case DEMUX_SET_SEEKPOINT:
2077 const int i_seekpoint = va_arg( args, int );
2078 if( !p_sys->p_title )
2079 return VLC_EGENERIC;
2080 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
2082 case DEMUX_GET_PTS_DELAY:
2084 for( unsigned int i = 0; i < p_sys->i_tracks; i++ )
2086 const MP4_Box_t *p_load;
2087 if ( (p_load = MP4_BoxGet( p_sys->track[i].p_track, "load" )) &&
2088 BOXDATA(p_load)->i_duration > 0 )
2090 *va_arg(args, int64_t *) =
2091 MP4_rescale( BOXDATA(p_load)->i_duration,
2092 p_sys->track[i].i_timescale, CLOCK_FREQ );
2093 return VLC_SUCCESS;
2096 return VLC_EGENERIC;
2098 case DEMUX_SET_NEXT_DEMUX_TIME:
2099 case DEMUX_SET_GROUP:
2100 case DEMUX_HAS_UNSUPPORTED_META:
2101 case DEMUX_CAN_RECORD:
2102 default:
2103 return VLC_EGENERIC;
2107 /*****************************************************************************
2108 * Close: frees unused data
2109 *****************************************************************************/
2110 static void Close ( vlc_object_t * p_this )
2112 demux_t * p_demux = (demux_t *)p_this;
2113 demux_sys_t *p_sys = p_demux->p_sys;
2114 unsigned int i_track;
2116 msg_Dbg( p_demux, "freeing all memory" );
2118 FragResetContext( p_sys );
2120 MP4_BoxFree( p_sys->p_root );
2122 if( p_sys->p_title )
2123 vlc_input_title_Delete( p_sys->p_title );
2125 if( p_sys->p_meta )
2126 vlc_meta_Delete( p_sys->p_meta );
2128 MP4_Fragments_Index_Delete( p_sys->p_fragsindex );
2130 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
2131 MP4_TrackClean( p_demux->out, &p_sys->track[i_track] );
2132 free( p_sys->track );
2134 free( p_sys );
2139 /****************************************************************************
2140 * Local functions, specific to vlc
2141 ****************************************************************************/
2142 /* Chapters */
2143 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
2145 demux_sys_t *p_sys = p_demux->p_sys;
2147 if( BOXDATA(p_chpl)->i_chapter == 0 )
2148 return;
2150 p_sys->p_title = vlc_input_title_New();
2151 for( int i = 0; i < BOXDATA(p_chpl)->i_chapter && p_sys->p_title; i++ )
2153 seekpoint_t *s = vlc_seekpoint_New();
2154 if( s == NULL) continue;
2156 s->psz_name = strdup( BOXDATA(p_chpl)->chapter[i].psz_name );
2157 if( s->psz_name == NULL)
2159 vlc_seekpoint_Delete( s );;
2160 continue;
2163 EnsureUTF8( s->psz_name );
2164 s->i_time_offset = BOXDATA(p_chpl)->chapter[i].i_start / 10;
2165 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2168 static void LoadChapterGoPro( demux_t *p_demux, MP4_Box_t *p_hmmt )
2170 demux_sys_t *p_sys = p_demux->p_sys;
2172 p_sys->p_title = vlc_input_title_New();
2173 if( p_sys->p_title )
2174 for( unsigned i = 0; i < BOXDATA(p_hmmt)->i_chapter_count; i++ )
2176 seekpoint_t *s = vlc_seekpoint_New();
2177 if( s )
2179 if( asprintf( &s->psz_name, "HiLight tag #%u", i+1 ) != -1 )
2180 EnsureUTF8( s->psz_name );
2182 /* HiLights are stored in ms so we convert them to µs */
2183 s->i_time_offset = BOXDATA(p_hmmt)->pi_chapter_start[i] * 1000;
2184 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2188 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
2190 demux_sys_t *p_sys = p_demux->p_sys;
2192 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
2194 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
2195 int64_t i_pts_delta;
2196 if ( !MP4_TrackGetPTSDelta( p_demux, tk, &i_pts_delta ) )
2197 i_pts_delta = 0;
2198 uint32_t i_nb_samples = 0;
2199 const uint32_t i_size = MP4_TrackGetReadSize( tk, &i_nb_samples );
2201 if( i_size > 0 && !vlc_stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
2203 char p_buffer[256];
2204 const uint32_t i_read = stream_ReadU32( p_demux->s, p_buffer,
2205 __MIN( sizeof(p_buffer), i_size ) );
2206 if( i_read > 2 )
2208 const uint32_t i_string = __MIN( GetWBE(p_buffer), i_read-2 );
2209 const char *psnz_string = &p_buffer[2];
2211 seekpoint_t *s = vlc_seekpoint_New();
2212 if( s == NULL ) continue;
2214 if( i_string > 1 && !memcmp( psnz_string, "\xFF\xFE", 2 ) )
2215 s->psz_name = FromCharset( "UTF-16LE", psnz_string, i_string );
2216 else
2217 s->psz_name = strndup( psnz_string, i_string );
2219 if( s->psz_name == NULL )
2221 vlc_seekpoint_Delete( s );
2222 continue;
2225 EnsureUTF8( s->psz_name );
2226 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
2228 if( !p_sys->p_title )
2229 p_sys->p_title = vlc_input_title_New();
2230 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2233 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
2234 tk->chunk[tk->i_chunk].i_sample_count )
2235 tk->i_chunk++;
2238 static void LoadChapter( demux_t *p_demux )
2240 demux_sys_t *p_sys = p_demux->p_sys;
2241 MP4_Box_t *p_chpl;
2242 MP4_Box_t *p_hmmt;
2244 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) &&
2245 BOXDATA(p_chpl) && BOXDATA(p_chpl)->i_chapter > 0 )
2247 LoadChapterGpac( p_demux, p_chpl );
2249 else if( ( p_hmmt = MP4_BoxGet( p_sys->p_root, "/moov/udta/HMMT" ) ) &&
2250 BOXDATA(p_hmmt) && BOXDATA(p_hmmt)->pi_chapter_start && BOXDATA(p_hmmt)->i_chapter_count > 0 )
2252 LoadChapterGoPro( p_demux, p_hmmt );
2254 else if( p_sys->p_tref_chap )
2256 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2257 unsigned int i, j;
2259 /* Load the first subtitle track like quicktime */
2260 for( i = 0; i < p_chap->i_entry_count; i++ )
2262 for( j = 0; j < p_sys->i_tracks; j++ )
2264 mp4_track_t *tk = &p_sys->track[j];
2265 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
2266 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_TX3G )
2267 break;
2269 if( j < p_sys->i_tracks )
2271 LoadChapterApple( p_demux, &p_sys->track[j] );
2272 break;
2277 /* Add duration if titles are enabled */
2278 if( p_sys->p_title )
2280 const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
2281 p_sys->p_title->i_length =
2282 MP4_rescale( i_duration,
2283 p_sys->i_timescale, CLOCK_FREQ );
2287 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
2288 static int TrackCreateChunksIndex( demux_t *p_demux,
2289 mp4_track_t *p_demux_track )
2291 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
2292 MP4_Box_t *p_stsc;
2294 unsigned int i_chunk;
2295 unsigned int i_index, i_last;
2297 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
2298 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
2299 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
2301 return( VLC_EGENERIC );
2304 p_demux_track->i_chunk_count = BOXDATA(p_co64)->i_entry_count;
2305 if( !p_demux_track->i_chunk_count )
2307 msg_Warn( p_demux, "no chunk defined" );
2309 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
2310 sizeof( mp4_chunk_t ) );
2311 if( p_demux_track->chunk == NULL )
2313 return VLC_ENOMEM;
2316 /* first we read chunk offset */
2317 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2319 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2321 ck->i_offset = BOXDATA(p_co64)->i_chunk_offset[i_chunk];
2323 ck->i_first_dts = 0;
2324 ck->i_entries_dts = 0;
2325 ck->p_sample_count_dts = NULL;
2326 ck->p_sample_delta_dts = NULL;
2327 ck->i_entries_pts = 0;
2328 ck->p_sample_count_pts = NULL;
2329 ck->p_sample_offset_pts = NULL;
2332 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
2333 to be used for the sample XXX begin to 1
2334 We construct it begining at the end */
2335 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
2336 i_index = BOXDATA(p_stsc)->i_entry_count;
2338 while( i_index-- > 0 )
2340 for( i_chunk = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
2341 i_chunk < i_last; i_chunk++ )
2343 if( i_chunk >= p_demux_track->i_chunk_count )
2345 msg_Warn( p_demux, "corrupted chunk table" );
2346 return VLC_EGENERIC;
2349 p_demux_track->chunk[i_chunk].i_sample_description_index =
2350 BOXDATA(p_stsc)->i_sample_description_index[i_index];
2351 p_demux_track->chunk[i_chunk].i_sample_count =
2352 BOXDATA(p_stsc)->i_samples_per_chunk[i_index];
2354 i_last = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
2357 p_demux_track->i_sample_count = 0;
2358 bool b_broken = false;
2359 if ( p_demux_track->i_chunk_count )
2361 p_demux_track->chunk[0].i_sample_first = 0;
2362 p_demux_track->i_sample_count += p_demux_track->chunk[0].i_sample_count;
2364 const mp4_chunk_t *prev = &p_demux_track->chunk[0];
2365 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2367 mp4_chunk_t *cur = &p_demux_track->chunk[i_chunk];
2368 if( unlikely(UINT32_MAX - cur->i_sample_count < p_demux_track->i_sample_count) )
2370 b_broken = true;
2371 break;
2373 p_demux_track->i_sample_count += cur->i_sample_count;
2374 cur->i_sample_first = prev->i_sample_first + prev->i_sample_count;
2375 prev = cur;
2379 if( unlikely(b_broken) )
2381 msg_Err( p_demux, "Overflow in chunks total samples count" );
2382 return VLC_EGENERIC;
2385 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
2386 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
2388 return VLC_SUCCESS;
2391 static int xTTS_CountEntries( demux_t *p_demux, uint32_t *pi_entry /* out */,
2392 const uint32_t i_index,
2393 uint32_t i_index_samples_left,
2394 uint32_t i_sample_count,
2395 const uint32_t *pi_index_sample_count,
2396 const uint32_t i_table_count )
2398 uint32_t i_array_offset;
2399 while( i_sample_count > 0 )
2401 if ( likely((UINT32_MAX - i_index) >= *pi_entry) )
2402 i_array_offset = i_index + *pi_entry;
2403 else
2404 return VLC_EGENERIC;
2406 if ( i_array_offset >= i_table_count )
2408 msg_Err( p_demux, "invalid index counting total samples %u %u", i_array_offset, i_table_count );
2409 return VLC_ENOVAR;
2412 if ( i_index_samples_left )
2414 if ( i_index_samples_left > i_sample_count )
2416 i_index_samples_left -= i_sample_count;
2417 i_sample_count = 0;
2418 *pi_entry +=1; /* No samples left, go copy */
2419 break;
2421 else
2423 i_sample_count -= i_index_samples_left;
2424 i_index_samples_left = 0;
2425 *pi_entry += 1;
2426 continue;
2429 else
2431 i_sample_count -= __MIN( i_sample_count, pi_index_sample_count[i_array_offset] );
2432 *pi_entry += 1;
2436 return VLC_SUCCESS;
2439 static int TrackCreateSamplesIndex( demux_t *p_demux,
2440 mp4_track_t *p_demux_track )
2442 MP4_Box_t *p_box;
2443 MP4_Box_data_stsz_t *stsz;
2444 /* TODO use also stss and stsh table for seeking */
2445 /* FIXME use edit table */
2447 /* Find stsz
2448 * Gives the sample size for each samples. There is also a stz2 table
2449 * (compressed form) that we need to implement TODO */
2450 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
2451 if( !p_box )
2453 /* FIXME and stz2 */
2454 msg_Warn( p_demux, "cannot find STSZ box" );
2455 return VLC_EGENERIC;
2457 stsz = p_box->data.p_stsz;
2459 /* Use stsz table to create a sample number -> sample size table */
2460 if( p_demux_track->i_sample_count != stsz->i_sample_count )
2462 msg_Warn( p_demux, "Incorrect total samples stsc %" PRIu32 " <> stsz %"PRIu32 ", "
2463 " expect truncated media playback",
2464 p_demux_track->i_sample_count, stsz->i_sample_count );
2465 p_demux_track->i_sample_count = __MIN(p_demux_track->i_sample_count, stsz->i_sample_count);
2468 if( stsz->i_sample_size )
2470 /* 1: all sample have the same size, so no need to construct a table */
2471 p_demux_track->i_sample_size = stsz->i_sample_size;
2472 p_demux_track->p_sample_size = NULL;
2474 else
2476 /* 2: each sample can have a different size */
2477 p_demux_track->i_sample_size = 0;
2478 p_demux_track->p_sample_size =
2479 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
2480 if( p_demux_track->p_sample_size == NULL )
2481 return VLC_ENOMEM;
2483 for( uint32_t i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
2485 p_demux_track->p_sample_size[i_sample] =
2486 stsz->i_entry_size[i_sample];
2490 if ( p_demux_track->i_chunk_count && p_demux_track->i_sample_size == 0 )
2492 const mp4_chunk_t *lastchunk = &p_demux_track->chunk[p_demux_track->i_chunk_count - 1];
2493 if( (uint64_t)lastchunk->i_sample_count + p_demux_track->i_chunk_count - 1 > stsz->i_sample_count )
2495 msg_Err( p_demux, "invalid samples table: stsz table is too small" );
2496 return VLC_EGENERIC;
2500 /* Use stts table to create a sample number -> dts table.
2501 * XXX: if we don't want to waste too much memory, we can't expand
2502 * the box! so each chunk will contain an "extract" of this table
2503 * for fast research (problem with raw stream where a sample is sometime
2504 * just channels*bits_per_sample/8 */
2506 /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
2507 * differing in 2/2 fields and 1 signedness */
2509 mtime_t i_next_dts = 0;
2510 /* Find stts
2511 * Gives mapping between sample and decoding time
2513 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
2514 if( !p_box )
2516 msg_Warn( p_demux, "cannot find STTS box" );
2517 return VLC_EGENERIC;
2519 else
2521 MP4_Box_data_stts_t *stts = p_box->data.p_stts;
2523 msg_Warn( p_demux, "STTS table of %"PRIu32" entries", stts->i_entry_count );
2525 /* Create sample -> dts table per chunk */
2526 uint32_t i_index = 0;
2527 uint32_t i_current_index_samples_left = 0;
2529 for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2531 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2532 uint32_t i_sample_count;
2534 /* save first dts */
2535 ck->i_first_dts = i_next_dts;
2537 /* count how many entries are needed for this chunk
2538 * for p_sample_delta_dts and p_sample_count_dts */
2539 ck->i_entries_dts = 0;
2541 int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_dts, i_index,
2542 i_current_index_samples_left,
2543 ck->i_sample_count,
2544 stts->pi_sample_count,
2545 stts->i_entry_count );
2546 if ( i_ret == VLC_EGENERIC )
2547 return i_ret;
2549 /* allocate them */
2550 ck->p_sample_count_dts = calloc( ck->i_entries_dts, sizeof( uint32_t ) );
2551 ck->p_sample_delta_dts = calloc( ck->i_entries_dts, sizeof( uint32_t ) );
2552 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
2554 free( ck->p_sample_count_dts );
2555 free( ck->p_sample_delta_dts );
2556 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_dts );
2557 ck->i_entries_dts = 0;
2558 return VLC_ENOMEM;
2561 /* now copy */
2562 i_sample_count = ck->i_sample_count;
2564 for( uint32_t i = 0; i < ck->i_entries_dts; i++ )
2566 if ( i_current_index_samples_left )
2568 if ( i_current_index_samples_left > i_sample_count )
2570 ck->p_sample_count_dts[i] = i_sample_count;
2571 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2572 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2573 if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts;
2574 i_current_index_samples_left -= i_sample_count;
2575 i_sample_count = 0;
2576 assert( i == ck->i_entries_dts - 1 );
2577 break;
2579 else
2581 ck->p_sample_count_dts[i] = i_current_index_samples_left;
2582 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2583 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2584 if ( i_current_index_samples_left ) ck->i_duration = i_next_dts - ck->i_first_dts;
2585 i_sample_count -= i_current_index_samples_left;
2586 i_current_index_samples_left = 0;
2587 i_index++;
2590 else
2592 if ( stts->pi_sample_count[i_index] > i_sample_count )
2594 ck->p_sample_count_dts[i] = i_sample_count;
2595 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2596 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2597 if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts;
2598 i_current_index_samples_left = stts->pi_sample_count[i_index] - i_sample_count;
2599 i_sample_count = 0;
2600 assert( i == ck->i_entries_dts - 1 );
2601 // keep building from same index
2603 else
2605 ck->p_sample_count_dts[i] = stts->pi_sample_count[i_index];
2606 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2607 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2608 if ( stts->pi_sample_count[i_index] ) ck->i_duration = i_next_dts - ck->i_first_dts;
2609 i_sample_count -= stts->pi_sample_count[i_index];
2610 i_index++;
2619 /* Find ctts
2620 * Gives the delta between decoding time (dts) and composition table (pts)
2622 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
2623 if( p_box && p_box->data.p_ctts )
2625 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
2627 msg_Warn( p_demux, "CTTS table of %"PRIu32" entries", ctts->i_entry_count );
2629 int64_t i_cts_shift = 0;
2630 const MP4_Box_t *p_cslg = MP4_BoxGet( p_demux_track->p_stbl, "cslg" );
2631 if( p_cslg && BOXDATA(p_cslg) )
2632 i_cts_shift = BOXDATA(p_cslg)->ct_to_dts_shift;
2634 /* Create pts-dts table per chunk */
2635 uint32_t i_index = 0;
2636 uint32_t i_current_index_samples_left = 0;
2638 for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2640 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2641 uint32_t i_sample_count;
2643 /* count how many entries are needed for this chunk
2644 * for p_sample_offset_pts and p_sample_count_pts */
2645 ck->i_entries_pts = 0;
2646 int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_pts, i_index,
2647 i_current_index_samples_left,
2648 ck->i_sample_count,
2649 ctts->pi_sample_count,
2650 ctts->i_entry_count );
2651 if ( i_ret == VLC_EGENERIC )
2652 return i_ret;
2654 /* allocate them */
2655 ck->p_sample_count_pts = calloc( ck->i_entries_pts, sizeof( uint32_t ) );
2656 ck->p_sample_offset_pts = calloc( ck->i_entries_pts, sizeof( int32_t ) );
2657 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
2659 free( ck->p_sample_count_pts );
2660 free( ck->p_sample_offset_pts );
2661 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_pts );
2662 ck->i_entries_pts = 0;
2663 return VLC_ENOMEM;
2666 /* now copy */
2667 i_sample_count = ck->i_sample_count;
2669 for( uint32_t i = 0; i < ck->i_entries_pts; i++ )
2671 if ( i_current_index_samples_left )
2673 if ( i_current_index_samples_left > i_sample_count )
2675 ck->p_sample_count_pts[i] = i_sample_count;
2676 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2677 i_current_index_samples_left -= i_sample_count;
2678 i_sample_count = 0;
2679 assert( i == ck->i_entries_pts - 1 );
2680 break;
2682 else
2684 ck->p_sample_count_pts[i] = i_current_index_samples_left;
2685 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2686 i_sample_count -= i_current_index_samples_left;
2687 i_current_index_samples_left = 0;
2688 i_index++;
2691 else
2693 if ( ctts->pi_sample_count[i_index] > i_sample_count )
2695 ck->p_sample_count_pts[i] = i_sample_count;
2696 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2697 i_current_index_samples_left = ctts->pi_sample_count[i_index] - i_sample_count;
2698 i_sample_count = 0;
2699 assert( i == ck->i_entries_pts - 1 );
2700 // keep building from same index
2702 else
2704 ck->p_sample_count_pts[i] = ctts->pi_sample_count[i_index];
2705 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index] + i_cts_shift;
2706 i_sample_count -= ctts->pi_sample_count[i_index];
2707 i_index++;
2716 msg_Dbg( p_demux, "track[Id 0x%x] read %"PRIu32" samples length:%"PRId64"s",
2717 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
2718 i_next_dts / p_demux_track->i_timescale );
2720 return VLC_SUCCESS;
2725 * It computes the sample rate for a video track using the given sample
2726 * description index
2728 static void TrackGetESSampleRate( demux_t *p_demux,
2729 unsigned *pi_num, unsigned *pi_den,
2730 const mp4_track_t *p_track,
2731 unsigned i_sd_index,
2732 unsigned i_chunk )
2734 *pi_num = 0;
2735 *pi_den = 0;
2737 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( MP4_BoxGet( p_demux->p_sys->p_root, "/moov" ),
2738 p_track->i_track_ID );
2739 MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
2740 if ( p_mdhd && BOXDATA(p_mdhd) )
2742 vlc_ureduce( pi_num, pi_den,
2743 (uint64_t) BOXDATA(p_mdhd)->i_timescale * p_track->i_sample_count,
2744 (uint64_t) BOXDATA(p_mdhd)->i_duration,
2745 UINT16_MAX );
2746 return;
2749 if( p_track->i_chunk_count == 0 )
2750 return;
2752 /* */
2753 const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
2754 while( p_chunk > &p_track->chunk[0] &&
2755 p_chunk[-1].i_sample_description_index == i_sd_index )
2757 p_chunk--;
2760 uint64_t i_sample = 0;
2761 uint64_t i_total_duration = 0;
2764 i_sample += p_chunk->i_sample_count;
2765 i_total_duration += p_chunk->i_duration;
2766 p_chunk++;
2768 while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
2769 p_chunk->i_sample_description_index == i_sd_index );
2771 if( i_sample > 0 && i_total_duration )
2772 vlc_ureduce( pi_num, pi_den,
2773 i_sample * p_track->i_timescale,
2774 i_total_duration,
2775 UINT16_MAX);
2779 * TrackCreateES:
2780 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
2782 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
2783 unsigned int i_chunk, es_out_id_t **pp_es )
2785 demux_sys_t *p_sys = p_demux->p_sys;
2786 unsigned int i_sample_description_index;
2788 if( p_sys->b_fragmented || p_track->i_chunk_count == 0 )
2789 i_sample_description_index = 1; /* XXX */
2790 else
2791 i_sample_description_index =
2792 p_track->chunk[i_chunk].i_sample_description_index;
2794 if( pp_es )
2795 *pp_es = NULL;
2797 if( !i_sample_description_index )
2799 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
2800 p_track->i_track_ID );
2801 return VLC_EGENERIC;
2804 MP4_Box_t *p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
2805 i_sample_description_index - 1 );
2807 if( !p_sample ||
2808 ( !p_sample->data.p_payload && p_track->fmt.i_cat != SPU_ES ) )
2810 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
2811 p_track->i_track_ID );
2812 return VLC_EGENERIC;
2815 p_track->p_sample = p_sample;
2817 MP4_Box_t *p_frma;
2818 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) && p_frma->data.p_frma )
2820 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
2822 p_sample->i_type = p_frma->data.p_frma->i_type;
2825 /* */
2826 switch( p_track->fmt.i_cat )
2828 case VIDEO_ES:
2829 if ( !SetupVideoES( p_demux, p_track, p_sample ) )
2830 return VLC_EGENERIC;
2832 /* Set frame rate */
2833 TrackGetESSampleRate( p_demux,
2834 &p_track->fmt.video.i_frame_rate,
2835 &p_track->fmt.video.i_frame_rate_base,
2836 p_track, i_sample_description_index, i_chunk );
2838 p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
2839 (float)p_track->fmt.video.i_frame_rate_base;
2841 break;
2843 case AUDIO_ES:
2844 if ( !SetupAudioES( p_demux, p_track, p_sample ) )
2845 return VLC_EGENERIC;
2846 if( p_sys->p_meta )
2848 audio_replay_gain_t *p_arg = &p_track->fmt.audio_replay_gain;
2849 const char *psz_meta = vlc_meta_GetExtra( p_sys->p_meta, "replaygain_track_gain" );
2850 if( psz_meta )
2852 double f_gain = us_atof( psz_meta );
2853 p_arg->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = f_gain;
2854 p_arg->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = f_gain != 0;
2856 psz_meta = vlc_meta_GetExtra( p_sys->p_meta, "replaygain_track_peak" );
2857 if( psz_meta )
2859 double f_gain = us_atof( psz_meta );
2860 p_arg->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = f_gain;
2861 p_arg->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = f_gain > 0;
2864 break;
2866 case SPU_ES:
2867 if ( !SetupSpuES( p_demux, p_track, p_sample ) )
2868 return VLC_EGENERIC;
2869 break;
2871 default:
2872 break;
2875 if( pp_es )
2876 *pp_es = MP4_AddTrackES( p_demux->out, p_track );
2878 return ( !pp_es || *pp_es ) ? VLC_SUCCESS : VLC_EGENERIC;
2881 /* *** Try to find nearest sync points *** */
2882 static int TrackGetNearestSeekPoint( demux_t *p_demux, mp4_track_t *p_track,
2883 uint32_t i_sample, uint32_t *pi_sync_sample )
2885 int i_ret = VLC_EGENERIC;
2886 *pi_sync_sample = 0;
2888 const MP4_Box_t *p_stss;
2889 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2891 const MP4_Box_data_stss_t *p_stss_data = BOXDATA(p_stss);
2892 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2893 p_track->i_track_ID );
2894 for( unsigned i_index = 0; i_index < p_stss_data->i_entry_count; i_index++ )
2896 if( i_index >= p_stss_data->i_entry_count - 1 ||
2897 i_sample < p_stss_data->i_sample_number[i_index+1] )
2899 *pi_sync_sample = p_stss_data->i_sample_number[i_index];
2900 msg_Dbg( p_demux, "stss gives %d --> %" PRIu32 " (sample number)",
2901 i_sample, *pi_sync_sample );
2902 i_ret = VLC_SUCCESS;
2903 break;
2908 /* try rap samples groups */
2909 const MP4_Box_t *p_sbgp = MP4_BoxGet( p_track->p_stbl, "sbgp" );
2910 for( ; p_sbgp; p_sbgp = p_sbgp->p_next )
2912 const MP4_Box_data_sbgp_t *p_sbgp_data = BOXDATA(p_sbgp);
2913 if( p_sbgp->i_type != ATOM_sbgp || !p_sbgp_data )
2914 continue;
2916 if( p_sbgp_data->i_grouping_type == SAMPLEGROUP_rap )
2918 uint32_t i_group_sample = 0;
2919 for ( uint32_t i=0; i<p_sbgp_data->i_entry_count; i++ )
2921 /* Sample belongs to rap group ? */
2922 if( p_sbgp_data->entries.pi_group_description_index[i] != 0 )
2924 if( i_sample < i_group_sample )
2926 msg_Dbg( p_demux, "sbgp lookup failed %" PRIu32 " (sample number)",
2927 i_sample );
2928 break;
2930 else if ( i_sample >= i_group_sample &&
2931 *pi_sync_sample < i_group_sample )
2933 *pi_sync_sample = i_group_sample;
2934 i_ret = VLC_SUCCESS;
2937 i_group_sample += p_sbgp_data->entries.pi_sample_count[i];
2940 if( i_ret == VLC_SUCCESS && *pi_sync_sample )
2942 msg_Dbg( p_demux, "sbgp gives %d --> %" PRIu32 " (sample number)",
2943 i_sample, *pi_sync_sample );
2948 return i_ret;
2951 /* given a time it return sample/chunk
2952 * it also update elst field of the track
2954 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2955 int64_t i_start, uint32_t *pi_chunk,
2956 uint32_t *pi_sample )
2958 demux_sys_t *p_sys = p_demux->p_sys;
2959 uint64_t i_dts;
2960 unsigned int i_sample;
2961 unsigned int i_chunk;
2962 int i_index;
2964 /* FIXME see if it's needed to check p_track->i_chunk_count */
2965 if( p_track->i_chunk_count == 0 )
2966 return( VLC_EGENERIC );
2968 /* handle elst (find the correct one) */
2969 MP4_TrackSetELST( p_demux, p_track, i_start );
2970 if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
2972 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
2973 int64_t i_mvt= MP4_rescale( i_start, CLOCK_FREQ, p_sys->i_timescale );
2975 /* now calculate i_start for this elst */
2976 /* offset */
2977 i_start -= MP4_rescale( p_track->i_elst_time, p_sys->i_timescale, CLOCK_FREQ );
2978 if( i_start < 0 )
2980 *pi_chunk = 0;
2981 *pi_sample= 0;
2983 return VLC_SUCCESS;
2985 /* to track time scale */
2986 i_start = MP4_rescale( i_start, CLOCK_FREQ, p_track->i_timescale );
2987 /* add elst offset */
2988 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2989 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2990 elst->i_media_time[p_track->i_elst] > 0 )
2992 i_start += elst->i_media_time[p_track->i_elst];
2995 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2996 "ms (track)", p_track->i_elst,
2997 MP4_rescale( i_mvt, p_sys->i_timescale, 1000 ),
2998 MP4_rescale( i_start, p_track->i_timescale, 1000 ) );
3000 else
3002 /* convert absolute time to in timescale unit */
3003 i_start = MP4_rescale( i_start, CLOCK_FREQ, p_track->i_timescale );
3006 /* we start from sample 0/chunk 0, hope it won't take too much time */
3007 /* *** find good chunk *** */
3008 for( i_chunk = 0; ; i_chunk++ )
3010 if( i_chunk + 1 >= p_track->i_chunk_count )
3012 /* at the end and can't check if i_start in this chunk,
3013 it will be check while searching i_sample */
3014 i_chunk = p_track->i_chunk_count - 1;
3015 break;
3018 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
3019 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
3021 break;
3025 /* *** find sample in the chunk *** */
3026 i_sample = p_track->chunk[i_chunk].i_sample_first;
3027 i_dts = p_track->chunk[i_chunk].i_first_dts;
3028 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
3030 if( i_dts +
3031 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
3032 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
3034 i_dts +=
3035 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
3036 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
3038 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
3039 i_index++;
3041 else
3043 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
3045 break;
3047 i_sample += ( i_start - i_dts ) /
3048 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
3049 break;
3053 if( i_sample >= p_track->i_sample_count )
3055 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
3056 "(seeking too far) chunk=%d sample=%d",
3057 p_track->i_track_ID, i_chunk, i_sample );
3058 return( VLC_EGENERIC );
3062 /* *** Try to find nearest sync points *** */
3063 uint32_t i_sync_sample;
3064 if( VLC_SUCCESS ==
3065 TrackGetNearestSeekPoint( p_demux, p_track, i_sample, &i_sync_sample ) )
3067 /* Go to chunk */
3068 if( i_sync_sample <= i_sample )
3070 while( i_chunk > 0 &&
3071 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
3072 i_chunk--;
3074 else
3076 while( i_chunk < p_track->i_chunk_count - 1 &&
3077 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
3078 p_track->chunk[i_chunk].i_sample_count )
3079 i_chunk++;
3081 i_sample = i_sync_sample;
3084 *pi_chunk = i_chunk;
3085 *pi_sample = i_sample;
3087 return VLC_SUCCESS;
3090 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
3091 unsigned int i_chunk, unsigned int i_sample )
3093 bool b_reselect = false;
3095 /* now see if actual es is ok */
3096 if( p_track->i_chunk >= p_track->i_chunk_count ||
3097 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
3098 p_track->chunk[i_chunk].i_sample_description_index )
3100 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
3101 p_track->i_track_ID );
3103 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
3104 p_track->p_es, &b_reselect );
3106 es_out_Del( p_demux->out, p_track->p_es );
3108 p_track->p_es = NULL;
3110 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
3112 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
3113 p_track->i_track_ID );
3115 p_track->b_ok = false;
3116 p_track->b_selected = false;
3117 return VLC_EGENERIC;
3121 /* select again the new decoder */
3122 if( b_reselect )
3124 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
3127 p_track->i_chunk = i_chunk;
3128 p_track->chunk[i_chunk].i_sample = i_sample - p_track->chunk[i_chunk].i_sample_first;
3129 p_track->i_sample = i_sample;
3131 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
3133 #if 0
3134 static void MP4_TrackRestart( demux_t *p_demux, mp4_track_t *p_track,
3135 MP4_Box_t *p_params_box )
3137 bool b_reselect = false;
3138 if( p_track->p_es )
3140 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
3141 p_track->p_es, &b_reselect );
3144 /* Save previous fragmented pos */
3145 uint32_t i_sample_pos_backup = p_track->i_sample;
3146 mtime_t time_backup = p_track->i_time;
3147 uint32_t timescale_backup = p_track->i_timescale;
3149 /* Save previous format and ES */
3150 es_format_t fmtbackup;
3151 es_out_id_t *p_es_backup = p_track->p_es;
3152 p_track->p_es = NULL;
3153 es_format_Init( &fmtbackup, UNKNOWN_ES, 0 );
3154 es_format_Copy( &fmtbackup, &p_track->fmt );
3155 es_format_Clean( &p_track->fmt );
3158 /* do the cleanup and recycle track / restart */
3159 MP4_TrackDestroy( p_demux, p_track );
3160 memset( p_track, 0, sizeof(*p_track) );
3162 assert(p_params_box->i_type == ATOM_trak);
3163 MP4_TrackCreate( p_demux, p_track, p_params_box, false, true );
3165 if( p_track->b_ok )
3167 if( !es_format_IsSimilar( &fmtbackup, &p_track->fmt ) ||
3168 fmtbackup.i_extra != p_track->fmt.i_extra ||
3169 memcmp( fmtbackup.p_extra, p_track->fmt.p_extra, fmtbackup.i_extra ) )
3171 if( p_es_backup )
3172 es_out_Del( p_demux->out, p_es_backup );
3174 if( !p_track->b_chapters_source )
3176 p_track->p_es = MP4_AddTrackES( p_demux->out, p_track );
3177 p_track->b_ok = !!p_track->p_es;
3180 else
3182 p_track->p_es = p_es_backup;
3185 else if( p_es_backup )
3187 es_out_Del( p_demux->out, p_es_backup );
3190 /* select again the new decoder */
3191 if( b_reselect && p_track->p_es )
3192 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
3194 es_format_Clean( &fmtbackup );
3196 /* Restore fragmented pos */
3197 p_track->i_sample = i_sample_pos_backup;
3198 p_track->i_time = MP4_rescale( time_backup, timescale_backup, p_track->i_timescale );
3200 #endif
3201 /****************************************************************************
3202 * MP4_TrackSetup:
3203 ****************************************************************************
3204 * Parse track information and create all needed data to run a track
3205 * If it succeed b_ok is set to 1 else to 0
3206 ****************************************************************************/
3207 static void MP4_TrackSetup( demux_t *p_demux, mp4_track_t *p_track,
3208 MP4_Box_t *p_box_trak,
3209 bool b_create_es, bool b_force_enable )
3211 demux_sys_t *p_sys = p_demux->p_sys;
3213 p_track->p_track = p_box_trak;
3215 char language[4] = { '\0' };
3216 char sdp_media_type[8] = { '\0' };
3218 const MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
3219 if( !p_tkhd )
3221 return;
3224 /* do we launch this track by default ? */
3225 p_track->b_enable =
3226 ( ( BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED ) != 0 );
3227 if( !p_track->b_enable )
3228 p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
3230 p_track->i_track_ID = BOXDATA(p_tkhd)->i_track_ID;
3232 p_track->i_width = BOXDATA(p_tkhd)->i_width / BLOCK16x16;
3233 p_track->i_height = BOXDATA(p_tkhd)->i_height / BLOCK16x16;
3234 p_track->f_rotation = BOXDATA(p_tkhd)->f_rotation;
3236 /* FIXME: unhandled box: tref */
3238 const MP4_Box_t *p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
3239 const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
3241 if( ( !p_mdhd )||( !p_hdlr ) )
3243 return;
3246 if( BOXDATA(p_mdhd)->i_timescale == 0 )
3248 msg_Warn( p_demux, "Invalid track timescale " );
3249 return;
3251 p_track->i_timescale = BOXDATA(p_mdhd)->i_timescale;
3253 memcpy( &language, BOXDATA(p_mdhd)->rgs_language, 3 );
3254 p_track->b_mac_encoding = BOXDATA(p_mdhd)->b_mac_encoding;
3256 switch( p_hdlr->data.p_hdlr->i_handler_type )
3258 case( ATOM_soun ):
3259 if( !MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) )
3261 return;
3263 es_format_Change( &p_track->fmt, AUDIO_ES, 0 );
3264 break;
3266 case( ATOM_vide ):
3267 if( !MP4_BoxGet( p_box_trak, "mdia/minf/vmhd") )
3269 return;
3271 es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3272 break;
3274 case( ATOM_hint ):
3275 /* RTP Reception Hint tracks */
3276 if( !MP4_BoxGet( p_box_trak, "mdia/minf/hmhd" ) ||
3277 !MP4_BoxGet( p_box_trak, "mdia/minf/stbl/stsd/rrtp" ) )
3279 break;
3281 MP4_Box_t *p_sdp;
3283 /* parse the sdp message to find out whether the RTP stream contained audio or video */
3284 if( !( p_sdp = MP4_BoxGet( p_box_trak, "udta/hnti/sdp " ) ) )
3286 msg_Warn( p_demux, "Didn't find sdp box to determine stream type" );
3287 return;
3290 memcpy( sdp_media_type, BOXDATA(p_sdp)->psz_text, 7 );
3291 if( !strcmp(sdp_media_type, "m=audio") )
3293 msg_Dbg( p_demux, "Found audio Rtp: %s", sdp_media_type );
3294 es_format_Change( &p_track->fmt, AUDIO_ES, 0 );
3296 else if( !strcmp(sdp_media_type, "m=video") )
3298 msg_Dbg( p_demux, "Found video Rtp: %s", sdp_media_type );
3299 es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3301 else
3303 msg_Warn( p_demux, "Malformed track SDP message: %s", sdp_media_type );
3304 return;
3306 p_track->p_sdp = p_sdp;
3307 break;
3309 case( ATOM_tx3g ):
3310 case( ATOM_text ):
3311 case( ATOM_subp ):
3312 case( ATOM_subt ): /* ttml */
3313 case( ATOM_sbtl ):
3314 case( ATOM_clcp ): /* closed captions */
3315 es_format_Change( &p_track->fmt, SPU_ES, 0 );
3316 break;
3318 default:
3319 return;
3322 p_track->asfinfo.i_cat = p_track->fmt.i_cat;
3324 const MP4_Box_t *p_elst;
3325 p_track->i_elst = 0;
3326 p_track->i_elst_time = 0;
3327 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
3329 MP4_Box_data_elst_t *elst = BOXDATA(p_elst);
3330 unsigned int i;
3332 msg_Warn( p_demux, "elst box found" );
3333 for( i = 0; i < elst->i_entry_count; i++ )
3335 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
3336 "ms) rate=%d.%d", i,
3337 MP4_rescale( elst->i_segment_duration[i], p_sys->i_timescale, 1000 ),
3338 elst->i_media_time[i] >= 0 ?
3339 MP4_rescale( elst->i_media_time[i], p_track->i_timescale, 1000 ) :
3340 INT64_C(-1),
3341 elst->i_media_rate_integer[i],
3342 elst->i_media_rate_fraction[i] );
3347 /* TODO
3348 add support for:
3349 p_dinf = MP4_BoxGet( p_minf, "dinf" );
3351 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
3352 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
3354 return;
3357 /* Set language */
3358 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
3360 p_track->fmt.psz_language = strdup( language );
3363 const MP4_Box_t *p_udta = MP4_BoxGet( p_box_trak, "udta" );
3364 if( p_udta )
3366 const MP4_Box_t *p_box_iter;
3367 for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
3368 p_box_iter = p_box_iter->p_next )
3370 switch( p_box_iter->i_type )
3372 case ATOM_0xa9nam:
3373 case ATOM_name:
3374 p_track->fmt.psz_description =
3375 strndup( p_box_iter->data.p_binary->p_blob,
3376 p_box_iter->data.p_binary->i_blob );
3377 default:
3378 break;
3383 /* Create chunk index table and sample index table */
3384 if( TrackCreateChunksIndex( p_demux,p_track ) ||
3385 TrackCreateSamplesIndex( p_demux, p_track ) )
3387 msg_Err( p_demux, "cannot create chunks index" );
3388 return; /* cannot create chunks index */
3391 p_track->i_chunk = 0;
3392 p_track->i_sample = 0;
3394 /* Mark chapter only track */
3395 if( p_sys->p_tref_chap )
3397 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
3398 unsigned int i;
3400 for( i = 0; i < p_chap->i_entry_count; i++ )
3402 if( p_track->i_track_ID == p_chap->i_track_ID[i] &&
3403 p_track->fmt.i_cat == UNKNOWN_ES )
3405 p_track->b_chapters_source = true;
3406 p_track->b_enable = false;
3407 break;
3412 const MP4_Box_t *p_tsel;
3413 /* now create es */
3414 if( b_force_enable &&
3415 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
3417 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
3418 p_track->i_track_ID );
3419 p_track->b_enable = true;
3420 p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN;
3422 else if ( (p_tsel = MP4_BoxGet( p_box_trak, "udta/tsel" )) )
3424 if ( BOXDATA(p_tsel) && BOXDATA(p_tsel)->i_switch_group )
3426 p_track->i_switch_group = BOXDATA(p_tsel)->i_switch_group;
3427 int i_priority = ES_PRIORITY_SELECTABLE_MIN;
3428 for ( unsigned int i = 0; i < p_sys->i_tracks; i++ )
3430 const mp4_track_t *p_other = &p_sys->track[i];
3431 if( p_other && p_other != p_track &&
3432 p_other->fmt.i_cat == p_track->fmt.i_cat &&
3433 p_track->i_switch_group == p_other->i_switch_group )
3434 i_priority = __MAX( i_priority, p_other->fmt.i_priority + 1 );
3436 /* VLC only support ES priority for AUDIO_ES and SPU_ES.
3437 If there's another VIDEO_ES in the same group, we need to unselect it then */
3438 if ( p_track->fmt.i_cat == VIDEO_ES && i_priority > ES_PRIORITY_SELECTABLE_MIN )
3439 p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
3440 else
3441 p_track->fmt.i_priority = i_priority;
3444 /* If there's no tsel, try to enable the track coming first in edit list */
3445 else if ( p_track->p_elst && p_track->fmt.i_priority == ES_PRIORITY_SELECTABLE_MIN )
3447 #define MAX_SELECTABLE (INT_MAX - ES_PRIORITY_SELECTABLE_MIN)
3448 for ( uint32_t i=0; i<p_track->BOXDATA(p_elst)->i_entry_count; i++ )
3450 if ( p_track->BOXDATA(p_elst)->i_media_time[i] >= 0 &&
3451 p_track->BOXDATA(p_elst)->i_segment_duration[i] )
3453 /* We do selection by inverting start time into priority.
3454 The track with earliest edit will have the highest prio */
3455 const int i_time = __MIN( MAX_SELECTABLE, p_track->BOXDATA(p_elst)->i_media_time[i] );
3456 p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + MAX_SELECTABLE - i_time;
3457 break;
3462 if( p_sys->hacks.es_cat_filters && (p_sys->hacks.es_cat_filters & p_track->fmt.i_cat) == 0 )
3464 p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
3467 if( TrackCreateES( p_demux,
3468 p_track, p_track->i_chunk,
3469 (p_track->b_chapters_source || !b_create_es) ? NULL : &p_track->p_es ) )
3471 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
3472 p_track->i_track_ID );
3473 return;
3476 p_track->b_ok = true;
3479 static void DestroyChunk( mp4_chunk_t *ck )
3481 free( ck->p_sample_count_dts );
3482 free( ck->p_sample_delta_dts );
3483 free( ck->p_sample_count_pts );
3484 free( ck->p_sample_offset_pts );
3485 free( ck->p_sample_size );
3488 /****************************************************************************
3489 * MP4_TrackClean:
3490 ****************************************************************************
3491 * Cleans a track created by MP4_TrackCreate.
3492 ****************************************************************************/
3493 static void MP4_TrackClean( es_out_t *out, mp4_track_t *p_track )
3495 es_format_Clean( &p_track->fmt );
3497 if( p_track->p_es )
3498 es_out_Del( out, p_track->p_es );
3500 if( p_track->chunk )
3502 for( unsigned int i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
3503 DestroyChunk( &p_track->chunk[i_chunk] );
3505 free( p_track->chunk );
3507 if( !p_track->i_sample_size )
3508 free( p_track->p_sample_size );
3510 if ( p_track->asfinfo.p_frame )
3511 block_ChainRelease( p_track->asfinfo.p_frame );
3513 free( p_track->context.runs.p_array );
3516 static void MP4_TrackInit( mp4_track_t *p_track )
3518 memset( p_track, 0, sizeof(mp4_track_t) );
3519 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
3520 p_track->i_timescale = 1;
3523 static void MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track, bool b_select )
3525 if( !p_track->b_ok || p_track->b_chapters_source )
3526 return;
3528 if( b_select == p_track->b_selected )
3529 return;
3531 if( !b_select && p_track->p_es )
3533 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
3534 p_track->p_es, false );
3537 p_track->b_selected = b_select;
3540 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
3541 mtime_t i_start )
3543 uint32_t i_chunk;
3544 uint32_t i_sample;
3546 if( !p_track->b_ok || p_track->b_chapters_source )
3547 return VLC_EGENERIC;
3549 p_track->b_selected = false;
3551 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
3552 &i_chunk, &i_sample ) )
3554 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
3555 p_track->i_track_ID );
3556 return VLC_EGENERIC;
3559 p_track->b_selected = true;
3560 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
3561 p_track->b_selected = true;
3563 p_track->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
3565 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
3570 * 3 types: for audio
3573 static inline uint32_t MP4_GetFixedSampleSize( const mp4_track_t *p_track,
3574 const MP4_Box_data_sample_soun_t *p_soun )
3576 uint32_t i_size = p_track->i_sample_size;
3578 assert( p_track->i_sample_size != 0 );
3580 /* QuickTime "built-in" support case fixups */
3581 if( p_track->fmt.i_cat == AUDIO_ES &&
3582 p_soun->i_compressionid == 0 && p_track->i_sample_size <= 2 )
3584 switch( p_track->fmt.i_codec )
3586 case VLC_CODEC_GSM:
3587 i_size = p_soun->i_channelcount;
3588 break;
3589 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
3590 case ATOM_twos:
3591 case ATOM_sowt:
3592 case ATOM_raw:
3593 case VLC_CODEC_S24L:
3594 case VLC_CODEC_S24B:
3595 case VLC_CODEC_S32L:
3596 case VLC_CODEC_S32B:
3597 case VLC_CODEC_F32L:
3598 case VLC_CODEC_F32B:
3599 case VLC_CODEC_F64L:
3600 case VLC_CODEC_F64B:
3601 if( p_track->i_sample_size < ((p_soun->i_samplesize+7U)/8U) * p_soun->i_channelcount )
3602 i_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
3603 break;
3604 case VLC_CODEC_ALAW:
3605 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
3606 i_size = p_soun->i_channelcount;
3607 break;
3608 default:
3609 break;
3613 return i_size;
3616 static uint32_t MP4_TrackGetReadSize( mp4_track_t *p_track, uint32_t *pi_nb_samples )
3618 uint32_t i_size = 0;
3619 *pi_nb_samples = 0;
3621 if ( p_track->i_sample == p_track->i_sample_count )
3622 return 0;
3624 if ( p_track->fmt.i_cat != AUDIO_ES )
3626 *pi_nb_samples = 1;
3628 if( p_track->i_sample_size == 0 ) /* all sizes are different */
3629 return p_track->p_sample_size[p_track->i_sample];
3630 else
3631 return p_track->i_sample_size;
3633 else
3635 const MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
3636 const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
3637 uint32_t i_max_samples = p_chunk->i_sample_count - p_chunk->i_sample;
3639 /* Group audio packets so we don't call demux for single sample unit */
3640 if( p_track->fmt.i_original_fourcc == VLC_CODEC_DVD_LPCM &&
3641 p_soun->i_constLPCMframesperaudiopacket &&
3642 p_soun->i_constbytesperaudiopacket )
3644 /* uncompressed case */
3645 uint32_t i_packets = i_max_samples / p_soun->i_constLPCMframesperaudiopacket;
3646 if ( UINT32_MAX / p_soun->i_constbytesperaudiopacket < i_packets )
3647 i_packets = UINT32_MAX / p_soun->i_constbytesperaudiopacket;
3648 *pi_nb_samples = i_packets * p_soun->i_constLPCMframesperaudiopacket;
3649 return i_packets * p_soun->i_constbytesperaudiopacket;
3652 if( p_track->fmt.i_original_fourcc == VLC_FOURCC('r','r','t','p') )
3654 *pi_nb_samples = 1;
3655 return p_track->i_sample_size;
3658 /* all samples have a different size */
3659 if( p_track->i_sample_size == 0 )
3661 *pi_nb_samples = 1;
3662 return p_track->p_sample_size[p_track->i_sample];
3665 if( p_soun->i_qt_version == 1 )
3667 if ( p_soun->i_compressionid == 0xFFFE )
3669 *pi_nb_samples = 1; /* != number of audio samples */
3670 if ( p_track->i_sample_size )
3671 return p_track->i_sample_size;
3672 else
3673 return p_track->p_sample_size[p_track->i_sample];
3675 else if ( p_soun->i_compressionid != 0 || p_soun->i_bytes_per_sample > 1 ) /* compressed */
3677 /* in this case we are dealing with compressed data
3678 -2 in V1: additional fields are meaningless (VBR and such) */
3679 *pi_nb_samples = i_max_samples;//p_track->chunk[p_track->i_chunk].i_sample_count;
3680 if( p_track->fmt.audio.i_blockalign > 1 )
3681 *pi_nb_samples = p_soun->i_sample_per_packet;
3682 i_size = *pi_nb_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
3683 return i_size;
3685 else /* uncompressed case */
3687 uint32_t i_packets;
3688 if( p_track->fmt.audio.i_blockalign > 1 )
3689 i_packets = 1;
3690 else
3691 i_packets = i_max_samples / p_soun->i_sample_per_packet;
3693 if ( UINT32_MAX / p_soun->i_bytes_per_frame < i_packets )
3694 i_packets = UINT32_MAX / p_soun->i_bytes_per_frame;
3696 *pi_nb_samples = i_packets * p_soun->i_sample_per_packet;
3697 i_size = i_packets * p_soun->i_bytes_per_frame;
3698 return i_size;
3702 /* uncompressed v0 (qt) or... not (ISO) */
3704 /* Quicktime built-in support handling */
3705 if( p_soun->i_compressionid == 0 && p_track->i_sample_size == 1 )
3707 switch( p_track->fmt.i_codec )
3709 /* sample size is not integer */
3710 case VLC_CODEC_GSM:
3711 *pi_nb_samples = 160 * p_track->fmt.audio.i_channels;
3712 return 33 * p_track->fmt.audio.i_channels;
3713 default:
3714 break;
3718 /* More regular V0 cases */
3719 uint32_t i_max_v0_samples;
3720 switch( p_track->fmt.i_codec )
3722 /* Compressed samples in V0 */
3723 case VLC_CODEC_AMR_NB:
3724 case VLC_CODEC_AMR_WB:
3725 i_max_v0_samples = 16;
3726 break;
3727 case VLC_CODEC_MPGA:
3728 case VLC_CODEC_MP2:
3729 case VLC_CODEC_MP3:
3730 i_max_v0_samples = 1;
3731 break;
3732 default:
3733 /* Read 25ms of samples (uncompressed) */
3734 i_max_v0_samples = p_track->fmt.audio.i_rate / 40 *
3735 p_track->fmt.audio.i_channels;
3736 if( i_max_v0_samples < 1 )
3737 i_max_v0_samples = 1;
3738 break;
3741 *pi_nb_samples = 0;
3742 for( uint32_t i=p_track->i_sample;
3743 i<p_chunk->i_sample_first+p_chunk->i_sample_count &&
3744 i<p_track->i_sample_count;
3745 i++ )
3747 (*pi_nb_samples)++;
3748 if ( p_track->i_sample_size == 0 )
3749 i_size += p_track->p_sample_size[i];
3750 else
3751 i_size += MP4_GetFixedSampleSize( p_track, p_soun );
3753 /* Try to detect compression in ISO */
3754 if(p_soun->i_compressionid != 0)
3756 /* Return only 1 sample */
3757 break;
3760 if ( *pi_nb_samples == i_max_v0_samples )
3761 break;
3765 //fprintf( stderr, "size=%d\n", i_size );
3766 return i_size;
3769 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
3771 unsigned int i_sample;
3772 uint64_t i_pos;
3774 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
3776 if( p_track->i_sample_size )
3778 MP4_Box_data_sample_soun_t *p_soun =
3779 p_track->p_sample->data.p_sample_soun;
3781 /* Quicktime builtin support, _must_ ignore sample tables */
3782 if( p_track->fmt.i_cat == AUDIO_ES && p_soun->i_compressionid == 0 &&
3783 p_track->i_sample_size == 1 )
3785 switch( p_track->fmt.i_codec )
3787 case VLC_CODEC_GSM: /* # Samples > data size */
3788 i_pos += ( p_track->i_sample -
3789 p_track->chunk[p_track->i_chunk].i_sample_first ) / 160 * 33;
3790 return i_pos;
3791 default:
3792 break;
3796 if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 ||
3797 p_track->fmt.audio.i_blockalign <= 1 ||
3798 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame == 0 )
3800 i_pos += ( p_track->i_sample -
3801 p_track->chunk[p_track->i_chunk].i_sample_first ) *
3802 MP4_GetFixedSampleSize( p_track, p_soun );
3804 else
3806 /* we read chunk by chunk unless a blockalign is requested */
3807 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
3808 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
3811 else
3813 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
3814 i_sample < p_track->i_sample; i_sample++ )
3816 i_pos += p_track->p_sample_size[i_sample];
3820 return i_pos;
3823 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track, uint32_t i_samples )
3825 if ( UINT32_MAX - p_track->i_sample < i_samples )
3827 p_track->i_sample = UINT32_MAX;
3828 return VLC_EGENERIC;
3831 p_track->i_sample += i_samples;
3833 if( p_track->i_sample >= p_track->i_sample_count )
3834 return VLC_EGENERIC;
3836 /* Have we changed chunk ? */
3837 if( p_track->i_sample >=
3838 p_track->chunk[p_track->i_chunk].i_sample_first +
3839 p_track->chunk[p_track->i_chunk].i_sample_count )
3841 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
3842 p_track->i_sample ) )
3844 msg_Warn( p_demux, "track[0x%x] will be disabled "
3845 "(cannot restart decoder)", p_track->i_track_ID );
3846 MP4_TrackSelect( p_demux, p_track, false );
3847 return VLC_EGENERIC;
3851 /* Have we changed elst */
3852 if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
3854 demux_sys_t *p_sys = p_demux->p_sys;
3855 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
3856 uint64_t i_mvt = MP4_rescale( MP4_TrackGetDTS( p_demux, p_track ),
3857 CLOCK_FREQ, p_sys->i_timescale );
3858 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
3859 i_mvt >= p_track->i_elst_time +
3860 elst->i_segment_duration[p_track->i_elst] )
3862 MP4_TrackSetELST( p_demux, p_track,
3863 MP4_TrackGetDTS( p_demux, p_track ) );
3867 return VLC_SUCCESS;
3870 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
3871 int64_t i_time )
3873 demux_sys_t *p_sys = p_demux->p_sys;
3874 int i_elst_last = tk->i_elst;
3876 /* handle elst (find the correct one) */
3877 tk->i_elst = 0;
3878 tk->i_elst_time = 0;
3879 if( tk->p_elst && tk->BOXDATA(p_elst)->i_entry_count > 0 )
3881 MP4_Box_data_elst_t *elst = tk->BOXDATA(p_elst);
3882 int64_t i_mvt= MP4_rescale( i_time, CLOCK_FREQ, p_sys->i_timescale );
3884 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
3886 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
3888 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
3890 break;
3892 tk->i_elst_time += i_dur;
3895 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
3897 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
3898 tk->i_elst = elst->i_entry_count - 1;
3899 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
3902 if( elst->i_media_time[tk->i_elst] < 0 )
3904 /* track offset */
3905 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
3908 if( i_elst_last != tk->i_elst )
3910 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
3911 tk->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
3915 /******************************************************************************
3916 * Here are the functions used for fragmented MP4
3917 *****************************************************************************/
3918 #if 0
3920 * Re-init decoder.
3921 * \Note If we call that function too soon,
3922 * before the track has been selected by MP4_TrackSelect
3923 * (during the first execution of Demux), then the track gets disabled
3925 static int ReInitDecoder( demux_t *p_demux, const MP4_Box_t *p_root,
3926 mp4_track_t *p_track )
3928 MP4_Box_t *p_paramsbox = MP4_BoxGet( p_root, "/moov/trak[0]" );
3929 if( !p_paramsbox )
3930 return VLC_EGENERIC;
3932 MP4_TrackRestart( p_demux, p_track, p_paramsbox );
3934 /* Temporary hack until we support track selection */
3935 p_track->b_selected = true;
3936 p_track->b_enable = true;
3938 return VLC_SUCCESS;
3940 #endif
3942 static stime_t GetCumulatedDuration( demux_t *p_demux )
3944 demux_sys_t *p_sys = p_demux->p_sys;
3945 stime_t i_max_duration = 0;
3947 for ( unsigned int i=0; i<p_sys->i_tracks; i++ )
3949 stime_t i_track_duration = 0;
3950 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->p_moov, p_sys->track[i].i_track_ID );
3951 const MP4_Box_t *p_stsz;
3952 const MP4_Box_t *p_tkhd;
3953 if ( (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) &&
3954 (p_stsz = MP4_BoxGet( p_trak, "mdia/minf/stbl/stsz" )) &&
3955 /* duration might be wrong an be set to whole duration :/ */
3956 BOXDATA(p_stsz)->i_sample_count > 0 )
3958 i_max_duration = __MAX( (uint64_t)i_max_duration, BOXDATA(p_tkhd)->i_duration );
3961 if( p_sys->p_fragsindex )
3963 i_track_duration += MP4_Fragment_Index_GetTrackDuration( p_sys->p_fragsindex, i );
3966 i_max_duration = __MAX( i_max_duration, i_track_duration );
3969 return i_max_duration;
3972 static int ProbeIndex( demux_t *p_demux )
3974 demux_sys_t *p_sys = p_demux->p_sys;
3975 uint64_t i_stream_size;
3976 uint8_t mfro[MP4_MFRO_BOXSIZE];
3977 assert( p_sys->b_seekable );
3979 if ( MP4_BoxCount( p_sys->p_root, "/mfra" ) )
3980 return VLC_EGENERIC;
3982 i_stream_size = stream_Size( p_demux->s );
3983 if ( ( i_stream_size >> 62 ) ||
3984 ( i_stream_size < MP4_MFRO_BOXSIZE ) ||
3985 ( vlc_stream_Seek( p_demux->s, i_stream_size - MP4_MFRO_BOXSIZE ) != VLC_SUCCESS )
3988 msg_Dbg( p_demux, "Probing tail for mfro has failed" );
3989 return VLC_EGENERIC;
3992 if ( vlc_stream_Read( p_demux->s, &mfro, MP4_MFRO_BOXSIZE ) == MP4_MFRO_BOXSIZE &&
3993 VLC_FOURCC(mfro[4],mfro[5],mfro[6],mfro[7]) == ATOM_mfro &&
3994 GetDWBE( &mfro ) == MP4_MFRO_BOXSIZE )
3996 uint32_t i_offset = GetDWBE( &mfro[12] );
3997 msg_Dbg( p_demux, "will read mfra index at %"PRIu64, i_stream_size - i_offset );
3998 if ( i_stream_size > i_offset &&
3999 vlc_stream_Seek( p_demux->s, i_stream_size - i_offset ) == VLC_SUCCESS )
4001 msg_Dbg( p_demux, "reading mfra index at %"PRIu64, i_stream_size - i_offset );
4002 const uint32_t stoplist[] = { ATOM_mfra, 0 };
4003 MP4_ReadBoxContainerChildren( p_demux->s, p_sys->p_root, stoplist );
4005 return VLC_SUCCESS;
4007 return VLC_EGENERIC;
4010 static stime_t GetMoovTrackDuration( demux_sys_t *p_sys, unsigned i_track_ID )
4012 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->p_moov, i_track_ID );
4013 const MP4_Box_t *p_stsz;
4014 const MP4_Box_t *p_tkhd;
4015 if ( (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) &&
4016 (p_stsz = MP4_BoxGet( p_trak, "mdia/minf/stbl/stsz" )) &&
4017 /* duration might be wrong an be set to whole duration :/ */
4018 BOXDATA(p_stsz)->i_sample_count > 0 )
4020 return BOXDATA(p_tkhd)->i_duration; /* In movie / mvhd scale */
4022 return 0;
4025 static bool GetMoofTrackDuration( MP4_Box_t *p_moov, MP4_Box_t *p_moof,
4026 unsigned i_track_ID, stime_t *p_duration )
4028 if ( !p_moof || !p_moov )
4029 return false;
4031 MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
4032 while ( p_traf )
4034 if ( p_traf->i_type != ATOM_traf )
4036 p_traf = p_traf->p_next;
4037 continue;
4040 const MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
4041 const MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun" );
4042 if ( !p_tfhd || !p_trun || i_track_ID != BOXDATA(p_tfhd)->i_track_ID )
4044 p_traf = p_traf->p_next;
4045 continue;
4048 uint32_t i_track_timescale = 0;
4049 uint32_t i_track_defaultsampleduration = 0;
4051 /* set trex for defaults */
4052 MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_moov, BOXDATA(p_tfhd)->i_track_ID );
4053 if ( p_trex )
4055 i_track_defaultsampleduration = BOXDATA(p_trex)->i_default_sample_duration;
4058 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_moov, BOXDATA(p_tfhd)->i_track_ID );
4059 if ( p_trak )
4061 MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
4062 if ( p_mdhd )
4063 i_track_timescale = BOXDATA(p_mdhd)->i_timescale;
4066 if ( !i_track_timescale )
4068 p_traf = p_traf->p_next;
4069 continue;
4072 uint64_t i_traf_duration = 0;
4073 while ( p_trun && p_tfhd )
4075 if ( p_trun->i_type != ATOM_trun )
4077 p_trun = p_trun->p_next;
4078 continue;
4080 const MP4_Box_data_trun_t *p_trundata = p_trun->data.p_trun;
4082 /* Sum total time */
4083 if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_DURATION )
4085 for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
4086 i_traf_duration += p_trundata->p_samples[i].i_duration;
4088 else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
4090 i_traf_duration += p_trundata->i_sample_count *
4091 BOXDATA(p_tfhd)->i_default_sample_duration;
4093 else
4095 i_traf_duration += p_trundata->i_sample_count *
4096 i_track_defaultsampleduration;
4099 p_trun = p_trun->p_next;
4102 *p_duration = i_traf_duration;
4103 break;
4106 return true;
4109 static int ProbeFragments( demux_t *p_demux, bool b_force, bool *pb_fragmented )
4111 demux_sys_t *p_sys = p_demux->p_sys;
4113 msg_Dbg( p_demux, "probing fragments from %"PRId64, vlc_stream_Tell( p_demux->s ) );
4115 assert( p_sys->p_root );
4117 MP4_Box_t *p_vroot = MP4_BoxNew(ATOM_root);
4118 if( !p_vroot )
4119 return VLC_EGENERIC;
4121 if( p_sys->b_seekable && (p_sys->b_fastseekable || b_force) )
4123 MP4_ReadBoxContainerChildren( p_demux->s, p_vroot, NULL ); /* Get the rest of the file */
4124 p_sys->b_fragments_probed = true;
4126 const unsigned i_moof = MP4_BoxCount( p_vroot, "/moof" );
4127 if( i_moof )
4129 *pb_fragmented = true;
4130 p_sys->p_fragsindex = MP4_Fragments_Index_New( p_sys->i_tracks, i_moof );
4131 if( !p_sys->p_fragsindex )
4133 MP4_BoxFree( p_vroot );
4134 return VLC_EGENERIC;
4137 stime_t *pi_track_times = calloc( p_sys->i_tracks, sizeof(*pi_track_times) );
4138 if( !pi_track_times )
4140 MP4_Fragments_Index_Delete( p_sys->p_fragsindex );
4141 p_sys->p_fragsindex = NULL;
4142 MP4_BoxFree( p_vroot );
4143 return VLC_EGENERIC;
4146 unsigned index = 0;
4148 for( MP4_Box_t *p_moof = p_vroot->p_first; p_moof; p_moof = p_moof->p_next )
4150 if( p_moof->i_type != ATOM_moof )
4151 continue;
4153 for( unsigned i=0; i<p_sys->i_tracks; i++ )
4155 stime_t i_duration = 0;
4156 MP4_Box_t *p_tfdt = NULL;
4157 MP4_Box_t *p_traf = MP4_GetTrafByTrackID( p_moof, p_sys->track[i].i_track_ID );
4158 if( p_traf )
4159 p_tfdt = MP4_BoxGet( p_traf, "tfdt" );
4161 /* Set first fragment time offset from moov */
4162 if( index == 0 )
4163 pi_track_times[i] = GetMoovTrackDuration( p_sys, p_sys->track[i].i_track_ID );
4165 if( p_tfdt && BOXDATA(p_tfdt) )
4167 pi_track_times[i] = p_tfdt->data.p_tfdt->i_base_media_decode_time;
4169 else if( index == 0 ) /* Set first fragment time offset from moov */
4171 i_duration = GetMoovTrackDuration( p_sys, p_sys->track[i].i_track_ID );
4172 pi_track_times[i] = MP4_rescale( i_duration, p_sys->i_timescale, p_sys->track[i].i_timescale );
4175 stime_t i_movietime = MP4_rescale( pi_track_times[i], p_sys->track[i].i_timescale, p_sys->i_timescale );
4176 p_sys->p_fragsindex->p_times[index * p_sys->i_tracks + i] = i_movietime;
4178 if( GetMoofTrackDuration( p_sys->p_moov, p_moof, p_sys->track[i].i_track_ID, &i_duration ) )
4179 pi_track_times[i] += i_duration;
4182 p_sys->p_fragsindex->pi_pos[index++] = p_moof->i_pos;
4185 for( unsigned i=0; i<p_sys->i_tracks; i++ )
4187 stime_t i_movietime = MP4_rescale( pi_track_times[i], p_sys->track[i].i_timescale, p_sys->i_timescale );
4188 if( p_sys->p_fragsindex->i_last_time < i_movietime )
4189 p_sys->p_fragsindex->i_last_time = i_movietime;
4192 free( pi_track_times );
4193 #ifdef MP4_VERBOSE
4194 MP4_Fragments_Index_Dump( VLC_OBJECT(p_demux), p_sys->p_fragsindex, p_sys->i_timescale );
4195 #endif
4198 else
4200 /* We stop at first moof, which validates our fragmentation condition
4201 * and we'll find others while reading. */
4202 const uint32_t excllist[] = { ATOM_moof, 0 };
4203 MP4_ReadBoxContainerRestricted( p_demux->s, p_vroot, NULL, excllist );
4204 /* Peek since we stopped before restriction */
4205 const uint8_t *p_peek;
4206 if ( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) == 8 )
4207 *pb_fragmented = (VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) == ATOM_moof);
4208 else
4209 *pb_fragmented = false;
4212 MP4_BoxFree( p_vroot );
4214 MP4_Box_t *p_mehd = MP4_BoxGet( p_sys->p_moov, "mvex/mehd");
4215 if ( !p_mehd )
4216 p_sys->i_cumulated_duration = GetCumulatedDuration( p_demux );
4218 return VLC_SUCCESS;
4221 static void FragResetContext( demux_sys_t *p_sys )
4223 if( p_sys->context.p_fragment_atom )
4225 if( p_sys->context.p_fragment_atom != p_sys->p_moov )
4226 MP4_BoxFree( p_sys->context.p_fragment_atom );
4227 p_sys->context.p_fragment_atom = NULL;
4229 p_sys->context.i_current_box_type = 0;
4231 for ( uint32_t i=0; i<p_sys->i_tracks; i++ )
4233 mp4_track_t *p_track = &p_sys->track[i];
4234 p_track->context.i_default_sample_size = 0;
4235 p_track->context.i_default_sample_duration = 0;
4239 static int FragDemuxTrack( demux_t *p_demux, mp4_track_t *p_track,
4240 unsigned i_max_preload )
4242 if( !p_track->b_ok ||
4243 p_track->context.runs.i_current >= p_track->context.runs.i_count )
4244 return VLC_DEMUXER_EOS;
4246 const MP4_Box_data_trun_t *p_trun =
4247 p_track->context.runs.p_array[p_track->context.runs.i_current].p_trun->data.p_trun;
4249 if( p_track->context.i_trun_sample >= p_trun->i_sample_count )
4250 return VLC_DEMUXER_EOS;
4252 uint32_t dur = p_track->context.i_default_sample_duration,
4253 len = p_track->context.i_default_sample_size;
4255 if( vlc_stream_Tell(p_demux->s) != p_track->context.i_trun_sample_pos &&
4256 MP4_Seek( p_demux->s, p_track->context.i_trun_sample_pos ) != VLC_SUCCESS )
4257 return VLC_DEMUXER_EOF;
4259 const stime_t i_demux_max_dts = (i_max_preload < UINT_MAX) ?
4260 p_track->i_time + MP4_rescale( i_max_preload, CLOCK_FREQ, p_track->i_timescale ) :
4261 INT64_MAX;
4263 for( uint32_t i = p_track->context.i_trun_sample; i < p_trun->i_sample_count; i++ )
4265 const stime_t i_dts = p_track->i_time;
4266 stime_t i_pts = i_dts;
4268 if( p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
4269 dur = p_trun->p_samples[i].i_duration;
4271 if( i_dts > i_demux_max_dts )
4272 return VLC_DEMUXER_SUCCESS;
4274 p_track->i_time += dur;
4275 p_track->context.i_trun_sample = i + 1;
4277 if( p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
4279 if ( p_trun->i_version == 1 )
4280 i_pts += p_trun->p_samples[i].i_composition_time_offset.v1;
4281 else if( p_trun->p_samples[i].i_composition_time_offset.v0 < 0xFF000000 )
4282 i_pts += p_trun->p_samples[i].i_composition_time_offset.v0;
4283 else /* version 0 with negative */
4284 i_pts += p_trun->p_samples[i].i_composition_time_offset.v1;
4287 if( p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
4288 len = p_trun->p_samples[i].i_size;
4290 if( !dur )
4291 msg_Warn(p_demux, "Zero duration sample in trun.");
4293 if( !len )
4294 msg_Warn(p_demux, "Zero length sample in trun.");
4296 block_t *p_block = vlc_stream_Block( p_demux->s, len );
4297 uint32_t i_read = ( p_block ) ? p_block->i_buffer : 0;
4298 p_track->context.i_trun_sample_pos += i_read;
4299 if( i_read < len || p_block == NULL )
4301 if( p_block )
4302 block_Release( p_block );
4303 return VLC_DEMUXER_EOF;
4306 #if 0
4307 msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, p_track->i_track_ID,
4308 VLC_TS_0 + MP4_rescale( i_dts, p_track->i_timescale, CLOCK_FREQ ),
4309 VLC_TS_0 + MP4_rescale( i_pts, p_track->i_timescale, CLOCK_FREQ ),
4310 p_track->context.i_trun_sample_pos );
4311 #endif
4312 if ( p_track->p_es )
4314 p_block->i_dts = VLC_TS_0 + MP4_rescale( i_dts, p_track->i_timescale, CLOCK_FREQ );
4315 if( p_track->fmt.i_cat == VIDEO_ES && !( p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET ) )
4316 p_block->i_pts = VLC_TS_INVALID;
4317 else
4318 p_block->i_pts = VLC_TS_0 + MP4_rescale( i_pts, p_track->i_timescale, CLOCK_FREQ );
4319 p_block->i_length = MP4_rescale( dur, p_track->i_timescale, CLOCK_FREQ );
4320 MP4_Block_Send( p_demux, p_track, p_block );
4322 else block_Release( p_block );
4325 if( p_track->context.i_trun_sample == p_trun->i_sample_count )
4327 p_track->context.i_trun_sample = 0;
4328 if( ++p_track->context.runs.i_current < p_track->context.runs.i_count )
4330 p_track->i_time = p_track->context.runs.p_array[p_track->context.runs.i_current].i_first_dts;
4331 p_track->context.i_trun_sample_pos = p_track->context.runs.p_array[p_track->context.runs.i_current].i_offset;
4335 return VLC_DEMUXER_SUCCESS;
4338 static int DemuxMoof( demux_t *p_demux )
4340 demux_sys_t *p_sys = p_demux->p_sys;
4341 int i_status;
4343 const unsigned i_max_preload = ( p_sys->b_fastseekable ) ? 0 : ( p_sys->b_seekable ) ? DEMUX_TRACK_MAX_PRELOAD : UINT_MAX;
4344 const stime_t i_scaledincrement = MP4_rescale( DEMUX_INCREMENT, CLOCK_FREQ, p_sys->i_timescale );
4346 const mtime_t i_nztime = MP4_GetMoviePTS( p_sys );
4348 /* !important! Ensure clock is set before sending data */
4349 if( p_sys->i_pcr == VLC_TS_INVALID )
4350 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + i_nztime );
4352 /* demux up to increment amount of data on every track, or just set pcr if empty data */
4353 for( ;; )
4355 mp4_track_t *tk = NULL;
4356 i_status = VLC_DEMUXER_EOS;
4358 /* First pass, find any track within our target increment, ordered by position */
4359 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
4361 mp4_track_t *tk_tmp = &p_sys->track[i];
4363 if( !tk_tmp->b_ok || tk_tmp->b_chapters_source ||
4364 (!tk_tmp->b_selected && !p_sys->b_seekable) ||
4365 tk_tmp->context.runs.i_current >= tk_tmp->context.runs.i_count )
4366 continue;
4368 /* At least still have data to demux on this or next turns */
4369 i_status = VLC_DEMUXER_SUCCESS;
4371 if( MP4_rescale( tk_tmp->i_time, tk_tmp->i_timescale, CLOCK_FREQ ) <= i_nztime + DEMUX_INCREMENT )
4373 if( tk == NULL || tk_tmp->context.i_trun_sample_pos < tk->context.i_trun_sample_pos )
4374 tk = tk_tmp;
4378 if( tk )
4380 /* Second pass, refine and find any best candidate having a chunk pos closer than
4381 * current candidate (avoids seeks when increment falls between the 2) from
4382 * current position, but within extended interleave time */
4383 for( unsigned i = 0; i_max_preload > 0 && i < p_sys->i_tracks; i++ )
4385 mp4_track_t *tk_tmp = &p_sys->track[i];
4386 if( tk_tmp == tk ||
4387 !tk_tmp->b_ok || tk_tmp->b_chapters_source ||
4388 (!tk_tmp->b_selected && !p_sys->b_seekable) ||
4389 tk_tmp->context.runs.i_current >= tk_tmp->context.runs.i_count )
4390 continue;
4392 mtime_t i_nzdts = MP4_rescale( tk_tmp->i_time, tk_tmp->i_timescale, CLOCK_FREQ );
4393 if ( i_nzdts <= i_nztime + DEMUX_TRACK_MAX_PRELOAD )
4395 /* Found a better candidate to avoid seeking */
4396 if( tk_tmp->context.i_trun_sample_pos < tk->context.i_trun_sample_pos )
4397 tk = tk_tmp;
4398 /* Note: previous candidate will be repicked on next loop */
4402 int i_ret = FragDemuxTrack( p_demux, tk, i_max_preload );
4404 if( i_ret == VLC_DEMUXER_SUCCESS )
4405 i_status = VLC_DEMUXER_SUCCESS;
4408 if( i_status != VLC_DEMUXER_SUCCESS || !tk )
4409 break;
4412 if( i_status != VLC_DEMUXER_EOS )
4414 p_sys->i_time += __MAX(i_scaledincrement, 1);
4415 p_sys->i_pcr = VLC_TS_0 + MP4_rescale( p_sys->i_time, p_sys->i_timescale, CLOCK_FREQ );
4416 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
4418 else
4420 stime_t i_segment_end = INT64_MAX;
4421 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
4423 mp4_track_t *tk = &p_sys->track[i];
4424 if( tk->b_ok || tk->b_chapters_source ||
4425 (!tk->b_selected && !p_sys->b_seekable) )
4426 continue;
4427 stime_t i_track_end = MP4_rescale( tk->i_time, tk->i_timescale, p_sys->i_timescale );
4428 if( i_track_end < i_segment_end )
4429 i_segment_end = i_track_end;
4431 if( i_segment_end != INT64_MAX )
4433 p_sys->i_time = i_segment_end;
4434 p_sys->i_pcr = VLC_TS_0 + MP4_rescale( p_sys->i_time, p_sys->i_timescale, CLOCK_FREQ );
4435 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
4439 return i_status;
4442 static int FragCreateTrunIndex( demux_t *p_demux, MP4_Box_t *p_moof,
4443 MP4_Box_t *p_chunksidx, stime_t i_moof_time, bool b_discontinuity )
4445 demux_sys_t *p_sys = p_demux->p_sys;
4447 uint64_t i_traf_base_data_offset = p_moof->i_pos;
4448 uint32_t i_traf = 0;
4449 uint64_t i_prev_traf_end = 0;
4451 for( unsigned i=0; i<p_sys->i_tracks; i++ )
4453 mp4_track_t *p_track = &p_sys->track[i];
4454 if( p_track->context.runs.p_array )
4455 free( p_track->context.runs.p_array );
4456 p_track->context.runs.p_array = NULL;
4457 p_track->context.runs.i_count = 0;
4458 p_track->context.runs.i_current = 0;
4461 for( MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
4462 p_traf ; p_traf = p_traf->p_next )
4464 if ( p_traf->i_type != ATOM_traf )
4465 continue;
4467 const MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
4468 const uint32_t i_trun_count = MP4_BoxCount( p_traf, "trun" );
4469 if ( !p_tfhd || !i_trun_count )
4470 continue;
4472 mp4_track_t *p_track = MP4_GetTrackByTrackID( p_demux, BOXDATA(p_tfhd)->i_track_ID );
4473 if( !p_track )
4474 continue;
4476 p_track->context.runs.p_array = calloc(i_trun_count, sizeof(mp4_run_t));
4477 if(!p_track->context.runs.p_array)
4478 continue;
4480 /* Get defaults for this/these RUN */
4481 uint32_t i_track_defaultsamplesize = 0;
4482 uint32_t i_track_defaultsampleduration = 0;
4483 MP4_GetDefaultSizeAndDuration( p_sys->p_moov, BOXDATA(p_tfhd),
4484 &i_track_defaultsamplesize,
4485 &i_track_defaultsampleduration );
4486 p_track->context.i_default_sample_size = i_track_defaultsamplesize;
4487 p_track->context.i_default_sample_duration = i_track_defaultsampleduration;
4489 stime_t i_traf_start_time = p_track->i_time;
4490 bool b_has_base_media_decode_time = false;
4492 if( b_discontinuity ) /* We NEED start time offset for each track */
4494 /* Find start time */
4495 const MP4_Box_t *p_tfdt = MP4_BoxGet( p_traf, "tfdt" );
4496 if( p_tfdt )
4498 i_traf_start_time = BOXDATA(p_tfdt)->i_base_media_decode_time;
4499 b_has_base_media_decode_time = true;
4502 /* Try using Tfxd for base offset (Smooth) */
4503 if( !b_has_base_media_decode_time && p_sys->i_tracks == 1 )
4505 const MP4_Box_t *p_uuid = MP4_BoxGet( p_traf, "uuid" );
4506 for( ; p_uuid; p_uuid = p_uuid->p_next )
4508 if( p_uuid->i_type == ATOM_uuid &&
4509 !CmpUUID( &p_uuid->i_uuid, &TfxdBoxUUID ) && p_uuid->data.p_tfxd )
4511 i_traf_start_time = p_uuid->data.p_tfxd->i_fragment_abs_time;
4512 b_has_base_media_decode_time = true;
4513 break;
4518 /* After seek we should have probed fragments */
4519 if( !b_has_base_media_decode_time && p_sys->p_fragsindex )
4521 unsigned i_track_index = (p_track - p_sys->track);
4522 assert(&p_sys->track[i_track_index] == p_track);
4523 i_traf_start_time = MP4_Fragment_Index_GetTrackStartTime( p_sys->p_fragsindex,
4524 i_track_index, p_moof->i_pos );
4525 i_traf_start_time = MP4_rescale( i_traf_start_time,
4526 p_sys->i_timescale, p_track->i_timescale );
4527 b_has_base_media_decode_time = true;
4530 if( !b_has_base_media_decode_time && p_chunksidx )
4532 /* Try using SIDX as base offset.
4533 * This can not work for global sidx but only when sent within each fragment (dash) */
4534 const MP4_Box_data_sidx_t *p_data = p_chunksidx->data.p_sidx;
4535 if( p_data && p_data->i_timescale && p_data->i_reference_count == 1 )
4537 i_traf_start_time = MP4_rescale( p_data->i_earliest_presentation_time,
4538 p_data->i_timescale, p_track->i_timescale );
4539 b_has_base_media_decode_time = true;
4543 /* First contiguous segment (moov->moof) and there's no tfdt not probed index (yet) */
4544 if( !b_has_base_media_decode_time && FragGetMoofSequenceNumber( p_moof ) == 1 )
4546 i_traf_start_time = MP4_rescale( GetMoovTrackDuration( p_sys, p_track->i_track_ID ),
4547 p_sys->i_timescale, p_track->i_timescale );
4548 b_has_base_media_decode_time = true;
4551 /* Use global sidx moof time, in case moof does not carry tfdt */
4552 if( !b_has_base_media_decode_time && i_moof_time != INT64_MAX )
4553 i_traf_start_time = MP4_rescale( i_moof_time, p_sys->i_timescale, p_track->i_timescale );
4555 /* That should not happen */
4556 if( !b_has_base_media_decode_time )
4557 i_traf_start_time = MP4_rescale( p_sys->i_time, p_sys->i_timescale, p_track->i_timescale );
4560 /* Parse TRUN data */
4562 if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
4564 i_traf_base_data_offset = BOXDATA(p_tfhd)->i_base_data_offset;
4566 else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DEFAULT_BASE_IS_MOOF )
4568 i_traf_base_data_offset = p_moof->i_pos /* + 8*/;
4570 else
4572 if ( i_traf == 0 )
4573 i_traf_base_data_offset = p_moof->i_pos /*+ 8*/;
4574 else
4575 i_traf_base_data_offset = i_prev_traf_end;
4578 uint64_t i_trun_dts = i_traf_start_time;
4579 uint64_t i_trun_data_offset = i_traf_base_data_offset;
4580 uint32_t i_trun_size = 0;
4582 for( const MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun" );
4583 p_trun && p_tfhd; p_trun = p_trun->p_next )
4585 if ( p_trun->i_type != ATOM_trun )
4586 continue;
4588 const MP4_Box_data_trun_t *p_trundata = p_trun->data.p_trun;
4590 /* Get data offset */
4591 if ( p_trundata->i_flags & MP4_TRUN_DATA_OFFSET )
4593 /* Fix for broken Trun data offset relative to tfhd instead of moof, as seen in smooth */
4594 if( (BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET) == 0 &&
4595 i_traf == 0 &&
4596 i_traf_base_data_offset + p_trundata->i_data_offset < p_moof->i_pos + p_moof->i_size + 8 )
4598 i_trun_data_offset += p_moof->i_size + 8;
4600 else if( (BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET) )
4602 i_trun_data_offset = BOXDATA(p_tfhd)->i_base_data_offset + p_trundata->i_data_offset;
4604 else
4606 i_trun_data_offset += p_trundata->i_data_offset;
4609 else
4611 i_trun_data_offset += i_trun_size;
4614 i_trun_size = 0;
4615 #ifndef NDEBUG
4616 msg_Dbg( p_demux,
4617 "tk %u run %" PRIu32 " dflt dur %"PRIu32" size %"PRIu32" firstdts %"PRId64" offset %"PRIu64,
4618 p_track->i_track_ID,
4619 p_track->context.runs.i_count,
4620 i_track_defaultsampleduration,
4621 i_track_defaultsamplesize,
4622 MP4_rescale( i_trun_dts, p_track->i_timescale, CLOCK_FREQ ), i_trun_data_offset );
4623 #endif
4624 //************
4625 mp4_run_t *p_run = &p_track->context.runs.p_array[p_track->context.runs.i_count++];
4626 p_run->i_first_dts = i_trun_dts;
4627 p_run->i_offset = i_trun_data_offset;
4628 p_run->p_trun = p_trun;
4630 //************
4631 /* Sum total time */
4632 if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_DURATION )
4634 for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
4635 i_trun_dts += p_trundata->p_samples[i].i_duration;
4637 else
4639 i_trun_dts += p_trundata->i_sample_count *
4640 i_track_defaultsampleduration;
4643 /* Get total traf size */
4644 if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_SIZE )
4646 for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
4647 i_trun_size += p_trundata->p_samples[i].i_size;
4649 else
4651 i_trun_size += p_trundata->i_sample_count *
4652 i_track_defaultsamplesize;
4655 i_prev_traf_end = i_trun_data_offset + i_trun_size;
4658 i_traf++;
4661 return VLC_SUCCESS;
4664 static int FragGetMoofBySidxIndex( demux_t *p_demux, mtime_t i_target_time,
4665 uint64_t *pi_moof_pos, mtime_t *pi_sampletime )
4667 const MP4_Box_t *p_sidx = MP4_BoxGet( p_demux->p_sys->p_root, "sidx" );
4668 const MP4_Box_data_sidx_t *p_data;
4669 if( !p_sidx || !((p_data = BOXDATA(p_sidx))) || !p_data->i_timescale )
4670 return VLC_EGENERIC;
4672 i_target_time = MP4_rescale( i_target_time, CLOCK_FREQ, p_data->i_timescale );
4674 /* sidx refers to offsets from end of sidx pos in the file + first offset */
4675 uint64_t i_pos = p_data->i_first_offset + p_sidx->i_pos + p_sidx->i_size;
4676 stime_t i_time = 0;
4677 for( uint16_t i=0; i<p_data->i_reference_count; i++ )
4679 if( i_time + p_data->p_items[i].i_subsegment_duration > i_target_time )
4681 *pi_sampletime = MP4_rescale( i_time, p_data->i_timescale, CLOCK_FREQ );
4682 *pi_moof_pos = i_pos;
4683 return VLC_SUCCESS;
4685 i_pos += p_data->p_items[i].i_referenced_size;
4686 i_time += p_data->p_items[i].i_subsegment_duration;
4689 return VLC_EGENERIC;
4692 static int FragGetMoofByTfraIndex( demux_t *p_demux, const mtime_t i_target_time, unsigned i_track_ID,
4693 uint64_t *pi_moof_pos, mtime_t *pi_sampletime )
4695 MP4_Box_t *p_tfra = MP4_BoxGet( p_demux->p_sys->p_root, "mfra/tfra" );
4696 for( ; p_tfra; p_tfra = p_tfra->p_next )
4698 if ( p_tfra->i_type == ATOM_tfra )
4700 const MP4_Box_data_tfra_t *p_data = BOXDATA(p_tfra);
4701 if( !p_data || p_data->i_track_ID != i_track_ID )
4702 continue;
4704 uint64_t i_pos = 0;
4705 mp4_track_t *p_track = MP4_GetTrackByTrackID( p_demux, p_data->i_track_ID );
4706 if ( p_track )
4708 stime_t i_track_target_time = MP4_rescale( i_target_time, CLOCK_FREQ, p_track->i_timescale );
4709 for ( uint32_t i = 0; i<p_data->i_number_of_entries; i += ( p_data->i_version == 1 ) ? 2 : 1 )
4711 mtime_t i_time;
4712 uint64_t i_offset;
4713 if ( p_data->i_version == 1 )
4715 i_time = *((uint64_t *)(p_data->p_time + i));
4716 i_offset = *((uint64_t *)(p_data->p_moof_offset + i));
4718 else
4720 i_time = p_data->p_time[i];
4721 i_offset = p_data->p_moof_offset[i];
4724 if ( i_time >= i_track_target_time )
4726 if ( i_pos == 0 ) /* Not in this traf */
4727 break;
4729 *pi_moof_pos = i_pos;
4730 *pi_sampletime = MP4_rescale( i_time, p_track->i_timescale, CLOCK_FREQ );
4731 return VLC_SUCCESS;
4733 else
4734 i_pos = i_offset;
4739 return VLC_EGENERIC;
4742 static void MP4_GetDefaultSizeAndDuration( MP4_Box_t *p_moov,
4743 const MP4_Box_data_tfhd_t *p_tfhd_data,
4744 uint32_t *pi_default_size,
4745 uint32_t *pi_default_duration )
4747 if( p_tfhd_data->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
4748 *pi_default_duration = p_tfhd_data->i_default_sample_duration;
4750 if( p_tfhd_data->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
4751 *pi_default_size = p_tfhd_data->i_default_sample_size;
4753 if( !*pi_default_duration || !*pi_default_size )
4755 const MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_moov, p_tfhd_data->i_track_ID );
4756 if ( p_trex )
4758 if ( !*pi_default_duration )
4759 *pi_default_duration = BOXDATA(p_trex)->i_default_sample_duration;
4760 if ( !*pi_default_size )
4761 *pi_default_size = BOXDATA(p_trex)->i_default_sample_size;
4766 static int DemuxFrag( demux_t *p_demux )
4768 demux_sys_t *p_sys = p_demux->p_sys;
4769 unsigned i_track_selected = 0;
4770 int i_status = VLC_DEMUXER_SUCCESS;
4772 if( unlikely(p_sys->b_error) )
4774 msg_Warn( p_demux, "unrecoverable error" );
4775 i_status = VLC_DEMUXER_EOF;
4776 goto end;
4779 /* check for newly selected/unselected track */
4780 for( unsigned i_track = 0; i_track < p_sys->i_tracks; i_track++ )
4782 mp4_track_t *tk = &p_sys->track[i_track];
4783 bool b = true;
4785 if( !tk->b_ok || tk->b_chapters_source )
4786 continue;
4788 if( p_sys->b_seekable )
4789 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
4791 if(tk->b_selected != b)
4793 msg_Dbg( p_demux, "track %u %s!", tk->i_track_ID, b ? "enabled" : "disabled" );
4794 MP4_TrackSelect( p_demux, tk, b );
4797 if( tk->b_selected )
4798 i_track_selected++;
4801 if( i_track_selected <= 0 )
4803 msg_Warn( p_demux, "no track selected, exiting..." );
4804 i_status = VLC_DEMUXER_EOF;
4805 goto end;
4808 if ( p_sys->context.i_current_box_type != ATOM_mdat )
4810 /* Othewise mdat is skipped. FIXME: mdat reading ! */
4811 const uint8_t *p_peek;
4812 if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) != 8 )
4814 i_status = VLC_DEMUXER_EOF;
4815 goto end;
4818 p_sys->context.i_current_box_type = VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
4819 if( p_sys->context.i_current_box_type == ATOM_mdat )
4821 p_sys->context.i_post_mdat_offset = vlc_stream_Tell( p_demux->s ) + GetDWBE( p_peek );
4823 else
4825 MP4_Box_t *p_vroot = MP4_BoxGetNextChunk( p_demux->s );
4826 if(!p_vroot)
4828 i_status = VLC_DEMUXER_EOF;
4829 goto end;
4832 MP4_Box_t *p_box = NULL;
4833 for( p_box = p_vroot->p_first; p_box; p_box = p_box->p_next )
4835 if( p_box->i_type == ATOM_moof ||
4836 p_box->i_type == ATOM_moov )
4837 break;
4840 if( p_box )
4842 FragResetContext( p_sys );
4844 if( p_box->i_type == ATOM_moov )
4846 p_sys->context.p_fragment_atom = p_sys->p_moov;
4848 else
4850 p_sys->context.p_fragment_atom = MP4_BoxExtract( &p_vroot->p_first, p_box->i_type );
4852 /* Detect and Handle Passive Seek */
4853 const uint32_t i_sequence_number = FragGetMoofSequenceNumber( p_sys->context.p_fragment_atom );
4854 const bool b_discontinuity = ( i_sequence_number != p_sys->context.i_lastseqnumber + 1 );
4855 if( b_discontinuity )
4856 msg_Info( p_demux, "Fragment sequence discontinuity detected %"PRIu32" != %"PRIu32,
4857 i_sequence_number, p_sys->context.i_lastseqnumber + 1 );
4858 p_sys->context.i_lastseqnumber = i_sequence_number;
4860 /* Prepare chunk */
4861 if( FragPrepareChunk( p_demux, p_sys->context.p_fragment_atom,
4862 MP4_BoxGet( p_vroot, "sidx"), INT64_MAX,
4863 b_discontinuity ) != VLC_SUCCESS )
4865 MP4_BoxFree( p_vroot );
4866 i_status = VLC_DEMUXER_EOF;
4867 goto end;
4870 if( b_discontinuity )
4872 p_sys->i_time = FragGetDemuxTimeFromTracksTime( p_sys );
4873 p_sys->i_pcr = VLC_TS_INVALID;
4875 /* !Prepare chunk */
4878 p_sys->context.i_current_box_type = p_box->i_type;
4881 MP4_BoxFree( p_vroot );
4883 if( p_sys->context.p_fragment_atom == NULL )
4885 msg_Info(p_demux, "no moof or moov in current chunk");
4886 return VLC_DEMUXER_SUCCESS;
4891 if ( p_sys->context.i_current_box_type == ATOM_mdat )
4893 assert(p_sys->context.p_fragment_atom);
4895 if ( p_sys->context.p_fragment_atom )
4896 switch( p_sys->context.p_fragment_atom->i_type )
4898 case ATOM_moov://[ftyp/moov, mdat]+ -> [moof, mdat]+
4899 i_status = DemuxMoov( p_demux );
4900 break;
4901 case ATOM_moof:
4902 i_status = DemuxMoof( p_demux );
4903 break;
4904 default:
4905 msg_Err( p_demux, "fragment type %4.4s", (char*) &p_sys->context.p_fragment_atom->i_type );
4906 break;
4909 if( i_status == VLC_DEMUXER_EOS )
4911 i_status = VLC_DEMUXER_SUCCESS;
4912 /* Skip if we didn't reach the end of mdat box */
4913 uint64_t i_pos = vlc_stream_Tell( p_demux->s );
4914 if( i_pos != p_sys->context.i_post_mdat_offset && i_status != VLC_DEMUXER_EOF )
4916 if( i_pos > p_sys->context.i_post_mdat_offset )
4917 msg_Err( p_demux, " Overread mdat by %" PRIu64, i_pos - p_sys->context.i_post_mdat_offset );
4918 else
4919 msg_Warn( p_demux, "mdat had still %"PRIu64" bytes unparsed as samples",
4920 p_sys->context.i_post_mdat_offset - i_pos );
4921 if( MP4_Seek( p_demux->s, p_sys->context.i_post_mdat_offset ) != VLC_SUCCESS )
4922 i_status = VLC_DEMUXER_EGENERIC;
4924 p_sys->context.i_current_box_type = 0;
4929 end:
4930 if( i_status == VLC_DEMUXER_EOF )
4932 mtime_t i_demux_end = INT64_MIN;
4933 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
4935 const mp4_track_t *tk = &p_sys->track[i];
4936 mtime_t i_track_end = MP4_rescale( tk->i_time, tk->i_timescale, CLOCK_FREQ );
4937 if( i_track_end > i_demux_end )
4938 i_demux_end = i_track_end;
4940 if( i_demux_end != INT64_MIN )
4941 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + i_demux_end );
4944 return i_status;
4947 /* ASF Handlers */
4948 inline static mp4_track_t *MP4ASF_GetTrack( asf_packet_sys_t *p_packetsys,
4949 uint8_t i_stream_number )
4951 demux_sys_t *p_sys = p_packetsys->p_demux->p_sys;
4952 for ( unsigned int i=0; i<p_sys->i_tracks; i++ )
4954 if ( p_sys->track[i].p_asf &&
4955 i_stream_number == p_sys->track[i].BOXDATA(p_asf)->i_stream_number )
4957 return &p_sys->track[i];
4960 return NULL;
4963 static asf_track_info_t * MP4ASF_GetTrackInfo( asf_packet_sys_t *p_packetsys,
4964 uint8_t i_stream_number )
4966 mp4_track_t *p_track = MP4ASF_GetTrack( p_packetsys, i_stream_number );
4967 if ( p_track )
4968 return &p_track->asfinfo;
4969 else
4970 return NULL;
4973 static void MP4ASF_Send( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
4974 block_t **pp_frame )
4976 mp4_track_t *p_track = MP4ASF_GetTrack( p_packetsys, i_stream_number );
4977 if ( !p_track )
4979 block_Release( *pp_frame );
4981 else
4983 block_t *p_gather = block_ChainGather( *pp_frame );
4984 p_gather->i_dts = p_track->i_dts_backup;
4985 p_gather->i_pts = p_track->i_pts_backup;
4986 es_out_Send( p_packetsys->p_demux->out, p_track->p_es, p_gather );
4989 *pp_frame = NULL;
4992 static void MP4ASF_ResetFrames( demux_sys_t *p_sys )
4994 for ( unsigned int i=0; i<p_sys->i_tracks; i++ )
4996 mp4_track_t *p_track = &p_sys->track[i];
4997 if( p_track->asfinfo.p_frame )
4999 block_ChainRelease( p_track->asfinfo.p_frame );
5000 p_track->asfinfo.p_frame = NULL;
5005 #undef BOXDATA