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 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
33 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_charset.h> /* EnsureUTF8 */
37 #include <vlc_meta.h> /* vlc_meta_t, vlc_meta_ */
38 #include <vlc_input.h>
42 #include "id3genres.h" /* for ATOM_gnre */
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
52 set_category( CAT_INPUT )
53 set_subcategory( SUBCAT_INPUT_DEMUX )
54 set_description( N_("MP4 stream demuxer") )
55 set_shortname( N_("MP4") )
56 set_capability( "demux", 240 )
57 set_callbacks( Open, Close )
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Demux ( demux_t * );
64 static int DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
65 static int DemuxFrg( demux_t * );
66 static int DemuxAsLeaf( demux_t * );
67 static int Seek ( demux_t *, mtime_t );
68 static int Control ( demux_t *, int, va_list );
72 MP4_Box_t *p_root; /* container for the whole file */
76 uint64_t i_overall_duration; /* Full duration, including all fragments */
77 uint64_t i_time; /* time position of the presentation
78 * in movie timescale */
79 uint32_t i_timescale; /* movie time scale */
80 uint64_t i_duration; /* movie duration */
81 unsigned int i_tracks; /* number of tracks */
82 mp4_track_t *track; /* array of track */
83 float f_fps; /* number of frame per seconds */
85 bool b_fragmented; /* fMP4 */
89 bool b_smooth; /* Is it Smooth Streaming? */
92 bool b_fragments_probed;
93 mp4_fragment_t moovfragment; /* moov */
94 mp4_fragment_t *p_fragments; /* known fragments (moof following moov) */
98 mp4_fragment_t *p_fragment;
99 uint32_t i_current_box_type;
100 uint32_t i_mdatbytesleft;
104 MP4_Box_t *p_tref_chap;
107 input_title_t *p_title;
110 asf_packet_sys_t asfpacketsys;
111 uint64_t i_preroll; /* foobar */
112 int64_t i_preroll_start;
113 mp4_track_t *p_current_track; /* avoids matching stream_number */
116 /*****************************************************************************
117 * Declaration of local function
118 *****************************************************************************/
119 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *, bool b_force_enable );
120 static int MP4_frg_TrackCreate( demux_t *, mp4_track_t *, MP4_Box_t *);
121 static void MP4_TrackDestroy( mp4_track_t * );
123 static block_t * MP4_Block_Read( demux_t *, const mp4_track_t *, int );
124 static void MP4_Block_Send( demux_t *, mp4_track_t *, block_t * );
126 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
127 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
129 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
131 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
132 static uint32_t MP4_TrackGetReadSize( mp4_track_t *, uint32_t * );
133 static int MP4_TrackNextSample( demux_t *, mp4_track_t *, uint32_t );
134 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
136 static void MP4_UpdateSeekpoint( demux_t * );
138 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id );
140 static bool AddFragment( demux_t *p_demux, MP4_Box_t *p_moox );
141 static int ProbeFragments( demux_t *p_demux, bool b_force );
142 static int ProbeIndex( demux_t *p_demux );
143 static mp4_fragment_t *GetFragmentByPos( demux_t *p_demux, uint64_t i_pos, bool b_exact );
144 static mp4_fragment_t *GetFragmentByTime( demux_t *p_demux, const mtime_t i_time );
146 static int LeafIndexGetMoofPosByTime( demux_t *p_demux, const mtime_t i_target_time,
147 uint64_t *pi_pos, mtime_t *pi_mooftime );
148 static mtime_t LeafGetFragmentTimeOffset( demux_t *p_demux, mp4_fragment_t * );
149 static int LeafGetTrackAndChunkByMOOVPos( demux_t *p_demux, uint64_t *pi_pos,
150 mp4_track_t **pp_tk, unsigned int *pi_chunk );
151 static int LeafMapTrafTrunContextes( demux_t *p_demux, MP4_Box_t *p_moof );
154 static asf_track_info_t * MP4ASF_GetTrackInfo( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number );
155 static void MP4ASF_Send(asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, block_t **pp_frame);
156 static void MP4ASF_ResetFrames( demux_sys_t *p_sys );
160 static uint32_t stream_ReadU32( stream_t *s, void *p_read, uint32_t i_toread )
162 uint32_t i_return = 0;
163 if ( i_toread > INT32_MAX )
165 i_return = stream_Read( s, p_read, INT32_MAX );
166 if ( i_return < INT32_MAX )
169 i_toread -= INT32_MAX;
171 i_return += stream_Read( s, (uint8_t *)p_read + i_return, (int32_t) i_toread );
175 static bool MP4_stream_Tell( stream_t *s, uint64_t *pi_pos )
177 int64_t i_pos = stream_Tell( s );
182 *pi_pos = (uint64_t) i_pos;
187 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
189 MP4_Box_t *p_trex = MP4_BoxGet( p_moov, "mvex/trex" );
192 if ( p_trex->i_type == ATOM_trex &&
193 BOXDATA(p_trex) && BOXDATA(p_trex)->i_track_ID == i_id )
196 p_trex = p_trex->p_next;
201 static MP4_Box_t * MP4_GetTrakByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
203 MP4_Box_t *p_trak = MP4_BoxGet( p_moov, "trak" );
207 if( p_trak->i_type == ATOM_trak &&
208 (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) && BOXDATA(p_tkhd) &&
209 BOXDATA(p_tkhd)->i_track_ID == i_id )
212 p_trak = p_trak->p_next;
217 /* Return time in microsecond of a track */
218 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
220 demux_sys_t *p_sys = p_demux->p_sys;
222 if( p_sys->b_fragmented )
223 chunk = *p_track->cchunk;
225 chunk = p_track->chunk[p_track->i_chunk];
227 unsigned int i_index = 0;
228 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
229 int64_t i_dts = chunk.i_first_dts;
231 while( i_sample > 0 )
233 if( i_sample > chunk.p_sample_count_dts[i_index] )
235 i_dts += chunk.p_sample_count_dts[i_index] *
236 chunk.p_sample_delta_dts[i_index];
237 i_sample -= chunk.p_sample_count_dts[i_index];
242 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
247 /* now handle elst */
248 if( p_track->p_elst )
250 demux_sys_t *p_sys = p_demux->p_sys;
251 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
253 /* convert to offset */
254 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
255 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
256 elst->i_media_time[p_track->i_elst] > 0 )
258 i_dts -= elst->i_media_time[p_track->i_elst];
261 /* add i_elst_time */
262 i_dts += p_track->i_elst_time * p_track->i_timescale /
265 if( i_dts < 0 ) i_dts = 0;
268 return CLOCK_FREQ * i_dts / p_track->i_timescale;
271 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
273 demux_sys_t *p_sys = p_demux->p_sys;
275 if( p_sys->b_fragmented )
276 ck = p_track->cchunk;
278 ck = &p_track->chunk[p_track->i_chunk];
280 unsigned int i_index = 0;
281 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
283 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
286 for( i_index = 0;; i_index++ )
288 if( i_sample < ck->p_sample_count_pts[i_index] )
289 return ck->p_sample_offset_pts[i_index] * CLOCK_FREQ /
290 (int64_t)p_track->i_timescale;
292 i_sample -= ck->p_sample_count_pts[i_index];
296 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
298 return CLOCK_FREQ * p_sys->i_time / p_sys->i_timescale;
301 static void LoadChapter( demux_t *p_demux );
303 static int LoadInitFrag( demux_t *p_demux )
305 demux_sys_t *p_sys = p_demux->p_sys;
307 if( p_sys->b_smooth ) /* Smooth Streaming */
309 if( ( p_sys->p_root = MP4_BoxGetSmooBox( p_demux->s ) ) == NULL )
311 goto LoadInitFragError;
315 MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
316 if( !p_smoo || CmpUUID( &p_smoo->i_uuid, &SmooBoxUUID ) )
317 goto LoadInitFragError;
318 /* Get number of tracks */
320 for( int i = 0; i < 3; i++ )
322 MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
323 if( p_stra && BOXDATA(p_stra) && BOXDATA(p_stra)->i_track_ID )
325 /* Get timescale and duration of the video track; */
326 if( p_sys->i_timescale == 0 )
328 if ( p_stra && BOXDATA(p_stra) )
330 p_sys->i_timescale = BOXDATA(p_stra)->i_timescale;
331 p_sys->i_duration = BOXDATA(p_stra)->i_duration;
332 p_sys->i_overall_duration = BOXDATA(p_stra)->i_duration;
334 if( p_sys->i_timescale == 0 )
336 msg_Err( p_demux, "bad timescale" );
337 goto LoadInitFragError;
345 /* Load all boxes ( except raw data ) */
346 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
348 goto LoadInitFragError;
354 msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" );
358 static int InitTracks( demux_t *p_demux )
360 demux_sys_t *p_sys = p_demux->p_sys;
362 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
363 if( p_sys->track == NULL )
366 if( p_sys->b_fragmented )
368 mp4_track_t *p_track;
369 for( uint16_t i = 0; i < p_sys->i_tracks; i++ )
371 p_track = &p_sys->track[i];
372 p_track->cchunk = calloc( 1, sizeof( mp4_chunk_t ) );
373 if( unlikely( !p_track->cchunk ) )
375 free( p_sys->track );
383 static void CreateTracksFromSmooBox( demux_t *p_demux )
385 demux_sys_t *p_sys = p_demux->p_sys;
387 MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
388 mp4_track_t *p_track;
390 for( int i = 0; i < 3; i++ )
392 MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
393 if( !p_stra || !BOXDATA(p_stra) || BOXDATA(p_stra)->i_track_ID == 0 )
397 p_track = &p_sys->track[j]; j++;
398 MP4_frg_TrackCreate( p_demux, p_track, p_stra );
399 p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
404 static block_t * MP4_EIA608_Convert( block_t * p_block )
406 /* Rebuild codec data from encap */
408 size_t i_remaining = p_block->i_buffer;
409 uint32_t i_bytes = 0;
412 if ( i_remaining < 10 ||
413 !(i_bytes = GetDWBE(p_block->p_buffer)) ||
414 (i_bytes + 8 > i_remaining) ||
415 memcmp("cdat", &p_block->p_buffer[4], 4) ||
416 !(p_newblock = block_Alloc( i_remaining * 3 - 8 )) )
418 p_block->i_buffer = 0;
422 uint8_t *p_write = p_newblock->p_buffer;
423 uint8_t *p_read = &p_block->p_buffer[8];
429 p_write[i_copied++] = 0; /* cc1 == field 0 */
430 p_write[i_copied++] = p_read[0];
431 p_write[i_copied++] = p_read[1];
435 } while( i_bytes >= 2 );
437 if ( i_remaining >= 10 &&
438 (i_bytes = GetDWBE(p_read)) &&
439 (i_bytes + 8 <= i_remaining) &&
440 !memcmp("cdt2", &p_read[4], 4) )
447 p_write[i_copied++] = 0; /* cc1 == field 0 */
448 p_write[i_copied++] = p_read[0];
449 p_write[i_copied++] = p_read[1];
452 } while( i_bytes >= 2 );
455 block_Release( p_block );
456 p_newblock->i_buffer = i_copied;
460 static block_t * MP4_Block_Read( demux_t *p_demux, const mp4_track_t *p_track, int i_size )
462 block_t *p_block = stream_Block( p_demux->s, i_size );
466 /* might have some encap */
467 if( p_track->fmt.i_cat == SPU_ES )
469 switch( p_track->fmt.i_codec )
475 case VLC_CODEC_EIA608_1:
476 p_block = MP4_EIA608_Convert( p_block );
479 p_block->i_buffer = 0;
487 static void MP4_Block_Send( demux_t *p_demux, mp4_track_t *p_track, block_t *p_block )
489 if ( p_track->b_chans_reorder && aout_BitsPerSample( p_track->fmt.i_codec ) )
491 aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
492 p_track->fmt.audio.i_channels,
493 p_track->rgi_chans_reordering,
494 p_track->fmt.i_codec );
497 /* ASF packets in mov */
500 /* Fake a new stream from MP4 block */
501 stream_t *p_stream = p_demux->s;
502 p_demux->s = stream_MemoryNew( p_demux, p_block->p_buffer, p_block->i_buffer, true );
505 p_track->i_dts_backup = p_block->i_dts;
506 p_track->i_pts_backup = p_block->i_pts;
507 /* And demux it as ASF packet */
508 DemuxASFPacket( &p_demux->p_sys->asfpacketsys, p_block->i_buffer, p_block->i_buffer );
509 stream_Delete(p_demux->s);
511 block_Release(p_block);
512 p_demux->s = p_stream;
515 es_out_Send( p_demux->out, p_track->p_es, p_block );
518 /*****************************************************************************
519 * Open: check file and initializes MP4 structures
520 *****************************************************************************/
521 static int Open( vlc_object_t * p_this )
523 demux_t *p_demux = (demux_t *)p_this;
526 const uint8_t *p_peek;
536 /* A little test to see if it could be a mp4 */
537 if( stream_Peek( p_demux->s, &p_peek, 11 ) < 11 ) return VLC_EGENERIC;
539 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
550 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
553 /* We don't yet support f4v, but avformat does. */
554 if( p_peek[8] == 'f' && p_peek[9] == '4' && p_peek[10] == 'v' )
561 /* create our structure that will contains all data */
562 p_sys = calloc( 1, sizeof( demux_sys_t ) );
567 stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
568 if( !p_sys->b_seekable )
570 msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" );
574 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable );
575 p_sys->b_seekmode = p_sys->b_fastseekable;
577 /*Set exported functions */
578 p_demux->pf_demux = Demux;
579 p_demux->pf_control = Control;
581 p_demux->p_sys = p_sys;
583 if( stream_Peek( p_demux->s, &p_peek, 24 ) < 24 ) return VLC_EGENERIC;
584 if( !CmpUUID( (UUID_t *)(p_peek + 8), &SmooBoxUUID ) )
586 p_sys->b_smooth = true;
587 p_sys->b_fragmented = true;
590 if( LoadInitFrag( p_demux ) != VLC_SUCCESS )
593 if( MP4_BoxCount( p_sys->p_root, "/moov/mvex" ) > 0 )
595 if ( p_sys->b_seekable )
597 /* Probe remaining to check if there's really fragments
598 or if that file is just ready to append fragments */
599 ProbeFragments( p_demux, false );
600 p_sys->b_fragmented = !!MP4_BoxCount( p_sys->p_root, "/moof" );
603 p_sys->b_fragmented = true;
605 if ( p_sys->b_fragmented && !p_sys->i_overall_duration )
606 ProbeFragments( p_demux, true );
609 if ( !p_sys->moovfragment.p_moox )
610 AddFragment( p_demux, MP4_BoxGet( p_sys->p_root, "/moov" ) );
612 /* we always need a moov entry, but smooth has a workaround */
613 if ( !p_sys->moovfragment.p_moox && !p_sys->b_smooth )
616 if ( p_sys->b_smooth )
618 p_demux->pf_demux = DemuxFrg;
620 else if( p_sys->b_fragmented )
622 p_demux->pf_demux = DemuxAsLeaf;
625 if( p_sys->b_smooth )
627 if( InitTracks( p_demux ) != VLC_SUCCESS )
629 CreateTracksFromSmooBox( p_demux );
633 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
635 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
637 switch( BOXDATA(p_ftyp)->i_major_brand )
641 "ISO Media file (isom) version %d.",
642 BOXDATA(p_ftyp)->i_minor_version );
645 case( VLC_FOURCC( '3', 'g', 'p', '5' ) ):
646 case( VLC_FOURCC( '3', 'g', 'p', '6' ) ):
647 case( VLC_FOURCC( '3', 'g', 'p', '7' ) ):
648 msg_Dbg( p_demux, "3GPP Media file Release: %c",
649 #ifdef WORDS_BIGENDIAN
650 BOXDATA(p_ftyp)->i_major_brand
652 BOXDATA(p_ftyp)->i_major_brand >> 24
656 case( VLC_FOURCC( 'q', 't', ' ', ' ') ):
657 msg_Dbg( p_demux, "Apple QuickTime file" );
659 case( VLC_FOURCC( 'i', 's', 'm', 'l') ):
660 msg_Dbg( p_demux, "PIFF (= isml = fMP4) file" );
664 "unrecognized major file specification (%4.4s).",
665 (char*)&BOXDATA(p_ftyp)->i_major_brand );
671 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
674 /* the file need to have one moov box */
675 p_sys->moovfragment.p_moox = MP4_BoxGet( p_sys->p_root, "/moov", 0 );
676 if( !p_sys->moovfragment.p_moox )
678 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
682 msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
685 /* we have a free box as a moov, rename it */
686 p_foov->i_type = ATOM_moov;
687 p_sys->moovfragment.p_moox = p_foov;
690 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
692 int i_count = MP4_BoxCount( p_rmra, "rmda" );
695 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
697 input_thread_t *p_input = demux_GetParentInput( p_demux );
698 input_item_t *p_current = input_GetItem( p_input );
700 input_item_node_t *p_subitems = input_item_node_Create( p_current );
702 for( i = 0; i < i_count; i++ )
704 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
708 if( !p_rdrf || !BOXDATA(p_rdrf) || !( psz_ref = strdup( BOXDATA(p_rdrf)->psz_ref ) ) )
712 i_ref_type = BOXDATA(p_rdrf)->i_ref_type;
714 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
715 psz_ref, (char*)&i_ref_type );
717 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
719 if( strstr( psz_ref, "qt5gateQT" ) )
721 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
724 if( !strncmp( psz_ref, "http://", 7 ) ||
725 !strncmp( psz_ref, "rtsp://", 7 ) )
732 char *psz_path = strdup( p_demux->psz_location );
733 char *end = strrchr( psz_path, '/' );
734 if( end ) end[1] = '\0';
735 else *psz_path = '\0';
737 if( asprintf( &psz_absolute, "%s://%s%s",
738 p_demux->psz_access, psz_path, psz_ref ) < 0 )
742 input_item_node_Delete( p_subitems );
743 vlc_object_release( p_input) ;
748 psz_ref = psz_absolute;
751 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
752 input_item_t *p_item = input_item_New( psz_ref, NULL );
753 input_item_CopyOptions( p_current, p_item );
754 input_item_node_AppendItem( p_subitems, p_item );
755 vlc_gc_decref( p_item );
759 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
760 (char*)&BOXDATA(p_rdrf)->i_ref_type );
764 input_item_node_PostAndDelete( p_subitems );
765 vlc_object_release( p_input );
768 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
772 msg_Err( p_demux, "cannot find /moov/mvhd" );
777 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
778 p_demux->pf_demux = DemuxRef;
784 p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
785 if( p_sys->i_timescale == 0 )
787 msg_Err( p_this, "bad timescale" );
792 if ( p_sys->i_overall_duration == 0 )
794 /* Try in mehd if fragmented */
795 MP4_Box_t *p_mehd = MP4_BoxGet( p_demux->p_sys->p_root, "moov/mvex/mehd");
796 if ( p_mehd && p_mehd->data.p_mehd )
797 p_sys->i_overall_duration = p_mehd->data.p_mehd->i_fragment_duration;
799 p_sys->i_overall_duration = p_sys->moovfragment.i_duration;
802 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
804 msg_Err( p_demux, "cannot find any /moov/trak" );
807 msg_Dbg( p_demux, "found %d track%c",
809 p_sys->i_tracks ? 's':' ' );
811 if( InitTracks( p_demux ) != VLC_SUCCESS )
814 /* Search the first chap reference (like quicktime) and
815 * check that at least 1 stream is enabled */
816 p_sys->p_tref_chap = NULL;
817 b_enabled_es = false;
818 for( i = 0; i < p_sys->i_tracks; i++ )
820 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
823 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
824 if( p_tkhd && BOXDATA(p_tkhd) && (BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED) )
827 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
828 if( p_chap && p_chap->data.p_tref_generic &&
829 p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
830 p_sys->p_tref_chap = p_chap;
833 /* now process each track and extract all useful information */
834 for( i = 0; i < p_sys->i_tracks; i++ )
836 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
837 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
839 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
842 switch( p_sys->track[i].fmt.i_cat )
851 psz_cat = "subtitle";
859 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
860 p_sys->track[i].i_track_ID, psz_cat,
861 p_sys->track[i].b_enable ? "enable":"disable",
862 p_sys->track[i].fmt.psz_language ?
863 p_sys->track[i].fmt.psz_language : "undef" );
865 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
867 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
868 p_sys->track[i].i_track_ID,
869 p_sys->track[i].fmt.psz_language ?
870 p_sys->track[i].fmt.psz_language : "undef" );
874 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
875 p_sys->track[i].i_track_ID );
879 mp4_fragment_t *p_fragment = &p_sys->moovfragment;
882 msg_Dbg( p_demux, "fragment offset %"PRId64", data %"PRIu64"<->%"PRIu64", duration %"PRId64,
883 p_fragment->p_moox->i_pos, p_fragment->i_chunk_range_min_offset,
884 p_fragment->i_chunk_range_max_offset, CLOCK_FREQ * p_fragment->i_duration / p_sys->i_timescale );
885 p_fragment = p_fragment->p_next;
889 LoadChapter( p_demux );
891 p_sys->asfpacketsys.p_demux = p_demux;
892 p_sys->asfpacketsys.pi_preroll = &p_sys->i_preroll;
893 p_sys->asfpacketsys.pi_preroll_start = &p_sys->i_preroll_start;
894 p_sys->asfpacketsys.pf_doskip = NULL;
895 p_sys->asfpacketsys.pf_send = MP4ASF_Send;
896 p_sys->asfpacketsys.pf_gettrackinfo = MP4ASF_GetTrackInfo;
897 p_sys->asfpacketsys.pf_updatetime = NULL;
898 p_sys->asfpacketsys.pf_setaspectratio = NULL;
905 MP4_BoxFree( p_demux->s, p_sys->p_root );
911 /*****************************************************************************
912 * Demux: read packet and send them to decoders
913 *****************************************************************************
914 * TODO check for newly selected track (ie audio upt to now )
915 *****************************************************************************/
916 static int Demux( demux_t *p_demux )
918 demux_sys_t *p_sys = p_demux->p_sys;
919 unsigned int i_track;
922 unsigned int i_track_selected;
924 /* check for newly selected/unselected track */
925 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
928 mp4_track_t *tk = &p_sys->track[i_track];
931 if( !tk->b_ok || tk->b_chapter ||
932 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
937 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
939 if( tk->b_selected && !b )
941 MP4_TrackUnselect( p_demux, tk );
943 else if( !tk->b_selected && b)
945 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
954 if( i_track_selected <= 0 )
956 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
957 if( p_sys->i_timescale > 0 )
959 int64_t i_length = CLOCK_FREQ *
960 (mtime_t)p_sys->moovfragment.i_duration /
961 (mtime_t)p_sys->i_timescale;
962 if( MP4_GetMoviePTS( p_sys ) >= i_length )
967 msg_Warn( p_demux, "no track selected, exiting..." );
972 MP4_UpdateSeekpoint( p_demux );
974 /* first wait for the good time to read a packet */
975 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
977 bool b_data_sent = false;
979 /* Find next track matching contiguous data */
980 mp4_track_t *tk = NULL;
981 uint64_t i_candidate_pos = UINT64_MAX;
982 mtime_t i_candidate_dts = INT64_MAX;
983 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
985 mp4_track_t *tk_tmp = &p_sys->track[i_track];
986 if( !tk_tmp->b_ok || tk_tmp->b_chapter || !tk_tmp->b_selected || tk_tmp->i_sample >= tk_tmp->i_sample_count )
989 if ( p_sys->b_seekmode )
991 mtime_t i_dts = MP4_TrackGetDTS( p_demux, tk_tmp );
992 if ( i_dts <= i_candidate_dts )
995 i_candidate_dts = i_dts;
996 i_candidate_pos = MP4_TrackGetPos( tk_tmp );
1001 /* Try to avoid seeking on non fastseekable. Will fail with non interleaved content */
1002 uint64_t i_pos = MP4_TrackGetPos( tk_tmp );
1003 if ( i_pos <= i_candidate_pos )
1005 i_candidate_pos = i_pos;
1013 msg_Dbg( p_demux, "Could not select track by data position" );
1016 else if ( p_sys->b_seekmode )
1018 if( stream_Seek( p_demux->s, i_candidate_pos ) )
1020 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
1022 MP4_TrackUnselect( p_demux, tk );
1028 msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, i_track,
1029 MP4_TrackGetDTS( p_demux, tk ),
1030 MP4_GetMoviePTS( p_sys ), i_candidate_pos );
1033 uint32_t i_nb_samples = 0;
1034 uint32_t i_samplessize = MP4_TrackGetReadSize( tk, &i_nb_samples );
1035 if( i_samplessize > 0 )
1039 uint64_t i_current_pos;
1042 if ( !MP4_stream_Tell( p_demux->s, &i_current_pos ) )
1045 if( i_current_pos != i_candidate_pos )
1047 if( stream_Seek( p_demux->s, i_candidate_pos ) )
1049 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1050 ": Failed to seek to %"PRIu64,
1051 tk->i_track_ID, i_candidate_pos );
1052 MP4_TrackUnselect( p_demux, tk );
1055 i_current_pos = i_candidate_pos;
1059 if( !(p_block = MP4_Block_Read( p_demux, tk, i_samplessize )) )
1061 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1062 ": Failed to read %d bytes sample at %"PRIu64,
1063 tk->i_track_ID, i_samplessize, i_current_pos );
1064 MP4_TrackUnselect( p_demux, tk );
1069 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
1071 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
1073 p_block->i_pts = p_block->i_dts + i_delta;
1074 else if( tk->fmt.i_cat != VIDEO_ES )
1075 p_block->i_pts = p_block->i_dts;
1077 p_block->i_pts = VLC_TS_INVALID;
1081 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
1084 MP4_Block_Send( p_demux, tk, p_block );
1088 if ( i_nb_samples ) /* sample size could be 0, need to go fwd. see return */
1089 MP4_TrackNextSample( p_demux, tk, i_nb_samples );
1094 p_sys->i_pcr = INT64_MAX;
1095 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1097 mp4_track_t *tk = &p_sys->track[i_track];
1098 if ( !tk->b_ok || !tk->b_selected ||
1099 (tk->fmt.i_cat != AUDIO_ES && tk->fmt.i_cat != VIDEO_ES) )
1102 mtime_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1103 p_sys->i_pcr = __MIN( i_dts, p_sys->i_pcr );
1105 if ( !p_sys->b_seekmode && i_dts > p_sys->i_pcr + 2*CLOCK_FREQ )
1107 msg_Dbg( p_demux, "that media doesn't look interleaved, will need to seek");
1108 p_sys->b_seekmode = true;
1111 p_sys->i_time = p_sys->i_pcr * p_sys->i_timescale / CLOCK_FREQ;
1115 return b_data_sent || ( i_samplessize == 0 && i_nb_samples );
1118 static void MP4_UpdateSeekpoint( demux_t *p_demux )
1120 demux_sys_t *p_sys = p_demux->p_sys;
1123 if( !p_sys->p_title )
1125 i_time = MP4_GetMoviePTS( p_sys );
1126 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
1128 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
1133 if( i != p_demux->info.i_seekpoint && i >= 0 )
1135 p_demux->info.i_seekpoint = i;
1136 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1139 /*****************************************************************************
1140 * Seek: Go to i_date
1141 ******************************************************************************/
1142 static int Seek( demux_t *p_demux, mtime_t i_date )
1144 demux_sys_t *p_sys = p_demux->p_sys;
1145 unsigned int i_track;
1147 /* First update global time */
1148 p_sys->i_time = i_date * p_sys->i_timescale / CLOCK_FREQ;
1149 p_sys->i_pcr = VLC_TS_INVALID;
1151 /* Now for each stream try to go to this time */
1152 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1154 mp4_track_t *tk = &p_sys->track[i_track];
1155 MP4_TrackSeek( p_demux, tk, i_date );
1157 MP4_UpdateSeekpoint( p_demux );
1159 MP4ASF_ResetFrames( p_sys );
1160 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1165 static int LeafSeekIntoFragment( demux_t *p_demux, mp4_fragment_t *p_fragment )
1167 demux_sys_t *p_sys = p_demux->p_sys;
1168 uint64_t i64 = p_fragment->i_chunk_range_min_offset;
1170 if ( p_fragment->p_moox->i_type == ATOM_moov )
1172 mp4_track_t *p_track;
1173 unsigned int i_chunk;
1174 int i_ret = LeafGetTrackAndChunkByMOOVPos( p_demux, &i64, &p_track, &i_chunk );
1175 if ( i_ret == VLC_EGENERIC )
1177 msg_Dbg( p_demux, "moov seek failed to identify %"PRIu64, i64 );
1180 msg_Dbg( p_demux, "moov seeking to %"PRIu64, i64 );
1184 i64 = p_fragment->i_chunk_range_min_offset;
1185 msg_Dbg( p_demux, "moof seeking to %"PRIu64, i64 );
1188 if( stream_Seek( p_demux->s, i64 ) )
1190 msg_Err( p_demux, "seek failed to %"PRIu64, i64 );
1191 return VLC_EGENERIC;
1195 p_sys->context.p_fragment = p_fragment;
1196 p_sys->context.i_current_box_type = ATOM_mdat;
1197 LeafMapTrafTrunContextes( p_demux, p_fragment->p_moox );
1198 p_sys->context.i_mdatbytesleft = p_fragment->i_chunk_range_max_offset - i64;
1200 mtime_t i_time_base = LeafGetFragmentTimeOffset( p_demux, p_fragment );
1201 for( unsigned int i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1203 p_sys->track[i_track].i_time = i_time_base * p_sys->track[i_track].i_timescale / p_sys->i_timescale;
1205 p_sys->i_time = i_time_base;
1206 p_sys->i_pcr = VLC_TS_INVALID;
1211 static int LeafSeekToTime( demux_t *p_demux, mtime_t i_nztime )
1213 demux_sys_t *p_sys = p_demux->p_sys;
1214 mp4_fragment_t *p_fragment;
1216 if ( !p_sys->i_timescale || !p_sys->i_overall_duration || !p_sys->b_seekable )
1217 return VLC_EGENERIC;
1219 if ( !p_sys->b_fragments_probed && !p_sys->b_index_probed && p_sys->b_seekable )
1221 ProbeIndex( p_demux );
1222 p_sys->b_index_probed = true;
1225 p_fragment = GetFragmentByTime( p_demux, i_nztime );
1229 msg_Dbg( p_demux, "seek can't find matching fragment for %"PRId64", trying index", i_nztime );
1230 if ( LeafIndexGetMoofPosByTime( p_demux, i_nztime, &i64, &i_mooftime ) == VLC_SUCCESS )
1232 msg_Dbg( p_demux, "seek trying to go to unknown but indexed fragment at %"PRId64, i64 );
1233 if( stream_Seek( p_demux->s, i64 ) )
1235 msg_Err( p_demux, "seek to moof failed %"PRId64, i64 );
1236 return VLC_EGENERIC;
1238 p_sys->context.i_current_box_type = 0;
1239 p_sys->context.i_mdatbytesleft = 0;
1240 p_sys->context.p_fragment = NULL;
1241 for( unsigned int i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1243 p_sys->track[i_track].i_time = i_mooftime / CLOCK_FREQ * p_sys->track[i_track].i_timescale;
1245 p_sys->i_time = i_mooftime / CLOCK_FREQ * p_sys->i_timescale;
1246 p_sys->i_pcr = VLC_TS_INVALID;
1250 msg_Warn( p_demux, "seek by index failed" );
1251 return VLC_EGENERIC;
1256 msg_Dbg( p_demux, "seeking to fragment data starting at %"PRIu64" for time %"PRId64,
1257 p_fragment->i_chunk_range_min_offset, i_nztime );
1258 if ( LeafSeekIntoFragment( p_demux, p_fragment ) != VLC_SUCCESS )
1259 return VLC_EGENERIC;
1262 MP4ASF_ResetFrames( p_sys );
1263 /* And set next display time in that trun/fragment */
1264 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TS_0 + i_nztime );
1268 static int LeafSeekToPos( demux_t *p_demux, double f )
1270 demux_sys_t *p_sys = p_demux->p_sys;
1271 if ( !p_sys->b_seekable )
1272 return VLC_EGENERIC;
1274 if ( p_sys->i_timescale && p_sys->i_overall_duration )
1276 return LeafSeekToTime( p_demux,
1277 (mtime_t)( f * CLOCK_FREQ * p_sys->i_overall_duration /
1278 p_sys->i_timescale ) );
1281 if ( !p_sys->b_fragments_probed && !p_sys->b_index_probed && p_sys->b_seekable )
1283 ProbeIndex( p_demux );
1284 p_sys->b_index_probed = true;
1287 /* Blind seek to pos only */
1288 uint64_t i64 = (uint64_t) stream_Size( p_demux->s ) * f;
1289 mp4_fragment_t *p_fragment = GetFragmentByPos( p_demux, i64, false );
1292 msg_Dbg( p_demux, "Seeking to fragment data starting at %"PRIu64" for pos %"PRIu64,
1293 p_fragment->i_chunk_range_min_offset, i64 );
1294 return LeafSeekIntoFragment( p_demux, p_fragment );
1298 msg_Dbg( p_demux, "Cant get fragment for data starting at %"PRIu64, i64 );
1299 return VLC_EGENERIC;
1303 static int MP4_frg_Seek( demux_t *p_demux, double f )
1305 demux_sys_t *p_sys = p_demux->p_sys;
1307 int64_t i64 = stream_Size( p_demux->s );
1308 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
1310 return VLC_EGENERIC;
1314 /* update global time */
1315 p_sys->i_time = (uint64_t)(f * (double)p_sys->moovfragment.i_duration);
1316 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
1318 for( unsigned i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1320 mp4_track_t *tk = &p_sys->track[i_track];
1322 /* We don't want the current chunk to be flushed */
1323 tk->cchunk->i_sample = tk->cchunk->i_sample_count;
1325 /* reset/update some values */
1326 tk->i_sample = tk->i_sample_first = 0;
1327 tk->i_first_dts = p_sys->i_time;
1329 /* We want to discard the current chunk and get the next one at once */
1330 tk->b_has_non_empty_cchunk = false;
1332 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->i_pcr );
1337 /*****************************************************************************
1339 *****************************************************************************/
1340 static int Control( demux_t *p_demux, int i_query, va_list args )
1342 demux_sys_t *p_sys = p_demux->p_sys;
1349 case DEMUX_GET_POSITION:
1350 pf = (double*)va_arg( args, double * );
1351 if( p_sys->i_overall_duration > 0 )
1353 *pf = (double)p_sys->i_time / (double)p_sys->i_overall_duration;
1361 case DEMUX_SET_POSITION:
1362 f = (double)va_arg( args, double );
1363 if ( p_demux->pf_demux == DemuxAsLeaf )
1364 return LeafSeekToPos( p_demux, f );
1365 else if ( p_demux->pf_demux == DemuxFrg )
1366 return MP4_frg_Seek( p_demux, f );
1367 else if( p_sys->i_timescale > 0 )
1369 i64 = (int64_t)( f * CLOCK_FREQ *
1370 (double)p_sys->i_overall_duration /
1371 (double)p_sys->i_timescale );
1372 return Seek( p_demux, i64 );
1374 else return VLC_EGENERIC;
1376 case DEMUX_GET_TIME:
1377 pi64 = (int64_t*)va_arg( args, int64_t * );
1378 if( p_sys->i_timescale > 0 )
1380 *pi64 = CLOCK_FREQ *
1381 (mtime_t)p_sys->i_time /
1382 (mtime_t)p_sys->i_timescale;
1387 case DEMUX_SET_TIME:
1388 i64 = (int64_t)va_arg( args, int64_t );
1389 if ( p_demux->pf_demux == DemuxAsLeaf )
1390 return LeafSeekToTime( p_demux, i64 );
1392 return Seek( p_demux, i64 );
1394 case DEMUX_GET_LENGTH:
1395 pi64 = (int64_t*)va_arg( args, int64_t * );
1396 if( p_sys->i_timescale > 0 )
1398 *pi64 = CLOCK_FREQ *
1399 (mtime_t)p_sys->i_overall_duration /
1400 (mtime_t)p_sys->i_timescale;
1406 pf = (double*)va_arg( args, double* );
1410 case DEMUX_GET_ATTACHMENTS:
1412 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1413 int *pi_int = va_arg( args, int * );
1415 MP4_Box_t *p_covr = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst/covr" );
1416 if ( !p_covr ) return VLC_EGENERIC;
1420 for( p_box = p_covr->p_first; p_box != NULL; p_box = p_box->p_next )
1422 if ( p_box->i_type == ATOM_data && p_box->data.p_data->i_blob >= 16 )
1427 return VLC_EGENERIC;
1429 *ppp_attach = (input_attachment_t**)
1430 malloc( sizeof(input_attachment_t*) * i_count );
1431 if( !(*ppp_attach) ) return VLC_ENOMEM;
1435 for( p_box = p_covr->p_first; p_box != NULL; p_box = p_box->p_next )
1437 if ( p_box->i_type != ATOM_data || p_box->data.p_data->i_blob < 16 )
1440 if ( !memcmp( p_box->data.p_data->p_blob,
1441 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8 ) )
1443 psz_mime = strdup( "image/png" );
1445 else if ( !memcmp( p_box->data.p_data->p_blob, "\xFF\xD8", 2 ) )
1447 psz_mime = strdup( "image/jpeg" );
1454 if ( asprintf( &psz_filename, "picture%u", i_count ) >= 0 )
1456 (*ppp_attach)[i_count++] =
1457 vlc_input_attachment_New( psz_filename, psz_mime, NULL,
1458 p_box->data.p_data->p_blob, p_box->data.p_data->i_blob );
1459 free( psz_filename );
1467 free( *ppp_attach );
1468 return VLC_EGENERIC;
1476 case DEMUX_GET_META:
1478 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
1479 MP4_Box_t *p_string;
1481 MP4_Box_t *p_covr = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst/covr/data[0]" );
1483 vlc_meta_SetArtURL( p_meta, "attachment://picture0" );
1485 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
1486 if( p_udta == NULL )
1488 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
1489 if( p_udta == NULL && p_covr == NULL )
1490 return VLC_EGENERIC;
1495 for( p_string = p_udta->p_first; p_string != NULL;
1496 p_string = p_string->p_next )
1499 if( !p_string || !BOXDATA(p_string) )
1502 /* FIXME FIXME: should convert from whatever the character
1503 * encoding of MP4 meta data is to UTF-8. */
1504 #define SET(fct) do { char *psz_utf = strdup( BOXDATA(p_string)->psz_text ? BOXDATA(p_string)->psz_text : "" ); \
1505 if( psz_utf ) { EnsureUTF8( psz_utf ); \
1506 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
1508 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
1509 switch( p_string->i_type )
1511 case ATOM_0xa9nam: /* Full name */
1512 SET( vlc_meta_SetTitle );
1515 SET( vlc_meta_SetArtist );
1518 SET( vlc_meta_SetArtist );
1521 SET( vlc_meta_SetCopyright );
1523 case ATOM_0xa9day: /* Creation Date */
1524 SET( vlc_meta_SetDate );
1526 case ATOM_0xa9des: /* Description */
1527 SET( vlc_meta_SetDescription );
1529 case ATOM_0xa9gen: /* Genre */
1530 SET( vlc_meta_SetGenre );
1534 if( p_string->data.p_gnre->i_genre <= NUM_GENRES )
1535 vlc_meta_SetGenre( p_meta, ppsz_genres[p_string->data.p_gnre->i_genre - 1] );
1538 case ATOM_0xa9alb: /* Album */
1539 SET( vlc_meta_SetAlbum );
1542 case ATOM_0xa9trk: /* Track */
1543 SET( vlc_meta_SetTrackNum );
1548 snprintf( psz_trck, sizeof( psz_trck ), "%i",
1549 p_string->data.p_trkn->i_track_number );
1550 vlc_meta_SetTrackNum( p_meta, psz_trck );
1551 if( p_string->data.p_trkn->i_track_total > 0 )
1553 snprintf( psz_trck, sizeof( psz_trck ), "%i",
1554 p_string->data.p_trkn->i_track_total );
1555 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, psz_trck );
1559 case ATOM_0xa9cmt: /* Commment */
1560 SET( vlc_meta_SetDescription );
1563 case ATOM_0xa9url: /* URL */
1564 SET( vlc_meta_SetURL );
1567 case ATOM_0xa9too: /* Encoder Tool */
1568 case ATOM_0xa9enc: /* Encoded By */
1569 SET( vlc_meta_SetEncodedBy );
1573 SET( vlc_meta_SetPublisher );
1577 SET( vlc_meta_SetDirector );
1584 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
1586 { ATOM_0xa9wrt, N_("Writer") },
1587 { ATOM_0xa9com, N_("Composer") },
1588 { ATOM_0xa9prd, N_("Producer") },
1589 { ATOM_0xa9inf, N_("Information") },
1590 { ATOM_0xa9dis, N_("Disclaimer") },
1591 { ATOM_0xa9req, N_("Requirements") },
1592 { ATOM_0xa9fmt, N_("Original Format") },
1593 { ATOM_0xa9dsa, N_("Display Source As") },
1594 { ATOM_0xa9hst, N_("Host Computer") },
1595 { ATOM_0xa9prf, N_("Performers") },
1596 { ATOM_0xa9ope, N_("Original Performer") },
1597 { ATOM_0xa9src, N_("Providers Source Content") },
1598 { ATOM_0xa9wrn, N_("Warning") },
1599 { ATOM_0xa9swr, N_("Software") },
1600 { ATOM_0xa9lyr, N_("Lyrics") },
1601 { ATOM_0xa9mak, N_("Record Company") },
1602 { ATOM_0xa9mod, N_("Model") },
1603 { ATOM_0xa9PRD, N_("Product") },
1604 { ATOM_0xa9grp, N_("Grouping") },
1605 { ATOM_0xa9gen, N_("Genre") },
1606 { ATOM_0xa9st3, N_("Sub-Title") },
1607 { ATOM_0xa9arg, N_("Arranger") },
1608 { ATOM_0xa9ard, N_("Art Director") },
1609 { ATOM_0xa9cak, N_("Copyright Acknowledgement") },
1610 { ATOM_0xa9con, N_("Conductor") },
1611 { ATOM_0xa9des, N_("Song Description") },
1612 { ATOM_0xa9lnt, N_("Liner Notes") },
1613 { ATOM_0xa9phg, N_("Phonogram Rights") },
1614 { ATOM_0xa9pub, N_("Publisher") },
1615 { ATOM_0xa9sne, N_("Sound Engineer") },
1616 { ATOM_0xa9sol, N_("Soloist") },
1617 { ATOM_0xa9thx, N_("Thanks") },
1618 { ATOM_0xa9xpd, N_("Executive Producer") },
1621 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
1623 if( p_string->i_type == xa9typetoextrameta[i].xa9_type )
1625 char *psz_utf = strdup( BOXDATA(p_string)->psz_text ? BOXDATA(p_string)->psz_text : "" );
1628 EnsureUTF8( psz_utf );
1629 vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
1639 case DEMUX_GET_TITLE_INFO:
1641 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1642 int *pi_int = (int*)va_arg( args, int* );
1643 int *pi_title_offset = (int*)va_arg( args, int* );
1644 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
1646 if( !p_sys->p_title )
1647 return VLC_EGENERIC;
1650 *ppp_title = malloc( sizeof( input_title_t*) );
1651 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
1652 *pi_title_offset = 0;
1653 *pi_seekpoint_offset = 0;
1656 case DEMUX_SET_TITLE:
1658 const int i_title = (int)va_arg( args, int );
1659 if( !p_sys->p_title || i_title != 0 )
1660 return VLC_EGENERIC;
1663 case DEMUX_SET_SEEKPOINT:
1665 const int i_seekpoint = (int)va_arg( args, int );
1666 if( !p_sys->p_title )
1667 return VLC_EGENERIC;
1668 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
1671 case DEMUX_SET_NEXT_DEMUX_TIME:
1672 case DEMUX_SET_GROUP:
1673 case DEMUX_HAS_UNSUPPORTED_META:
1674 case DEMUX_GET_PTS_DELAY:
1675 case DEMUX_CAN_RECORD:
1676 return VLC_EGENERIC;
1679 msg_Warn( p_demux, "control query %u unimplemented", i_query );
1680 return VLC_EGENERIC;
1684 /*****************************************************************************
1685 * Close: frees unused data
1686 *****************************************************************************/
1687 static void Close ( vlc_object_t * p_this )
1689 demux_t * p_demux = (demux_t *)p_this;
1690 demux_sys_t *p_sys = p_demux->p_sys;
1691 unsigned int i_track;
1693 msg_Dbg( p_demux, "freeing all memory" );
1695 MP4_BoxFree( p_demux->s, p_sys->p_root );
1696 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1698 MP4_TrackDestroy( &p_sys->track[i_track] );
1700 FREENULL( p_sys->track );
1702 if( p_sys->p_title )
1703 vlc_input_title_Delete( p_sys->p_title );
1705 while( p_sys->moovfragment.p_next )
1707 mp4_fragment_t *p_fragment = p_sys->moovfragment.p_next->p_next;
1708 free( p_sys->moovfragment.p_next );
1709 p_sys->moovfragment.p_next = p_fragment;
1717 /****************************************************************************
1718 * Local functions, specific to vlc
1719 ****************************************************************************/
1721 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1723 demux_sys_t *p_sys = p_demux->p_sys;
1726 p_sys->p_title = vlc_input_title_New();
1727 for( i = 0; i < BOXDATA(p_chpl)->i_chapter; i++ )
1729 seekpoint_t *s = vlc_seekpoint_New();
1731 s->psz_name = strdup( BOXDATA(p_chpl)->chapter[i].psz_name );
1732 EnsureUTF8( s->psz_name );
1733 s->i_time_offset = BOXDATA(p_chpl)->chapter[i].i_start / 10;
1734 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1737 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1739 demux_sys_t *p_sys = p_demux->p_sys;
1741 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1743 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1744 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( p_demux, tk );
1745 uint32_t i_nb_samples = 0;
1746 const uint32_t i_size = MP4_TrackGetReadSize( tk, &i_nb_samples );
1748 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1751 const uint32_t i_read = stream_ReadU32( p_demux->s, p_buffer,
1752 __MIN( sizeof(p_buffer), i_size ) );
1753 const uint32_t i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1757 seekpoint_t *s = vlc_seekpoint_New();
1759 s->psz_name = strndup( &p_buffer[2], i_len );
1760 EnsureUTF8( s->psz_name );
1762 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1764 if( !p_sys->p_title )
1765 p_sys->p_title = vlc_input_title_New();
1766 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1769 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1770 tk->chunk[tk->i_chunk].i_sample_count )
1774 static void LoadChapter( demux_t *p_demux )
1776 demux_sys_t *p_sys = p_demux->p_sys;
1779 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) &&
1780 BOXDATA(p_chpl) && BOXDATA(p_chpl)->i_chapter > 0 )
1782 LoadChapterGpac( p_demux, p_chpl );
1784 else if( p_sys->p_tref_chap )
1786 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1789 /* Load the first subtitle track like quicktime */
1790 for( i = 0; i < p_chap->i_entry_count; i++ )
1792 for( j = 0; j < p_sys->i_tracks; j++ )
1794 mp4_track_t *tk = &p_sys->track[j];
1795 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1796 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_SUBT )
1799 if( j < p_sys->i_tracks )
1801 LoadChapterApple( p_demux, &p_sys->track[j] );
1807 /* Add duration if titles are enabled */
1808 if( p_sys->p_title )
1810 p_sys->p_title->i_length = CLOCK_FREQ *
1811 (uint64_t)p_sys->i_overall_duration / (uint64_t)p_sys->i_timescale;
1815 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1816 static int TrackCreateChunksIndex( demux_t *p_demux,
1817 mp4_track_t *p_demux_track )
1819 demux_sys_t *p_sys = p_demux->p_sys;
1821 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1824 unsigned int i_chunk;
1825 unsigned int i_index, i_last;
1827 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1828 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1829 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1831 return( VLC_EGENERIC );
1834 p_demux_track->i_chunk_count = BOXDATA(p_co64)->i_entry_count;
1835 if( !p_demux_track->i_chunk_count )
1837 msg_Warn( p_demux, "no chunk defined" );
1839 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1840 sizeof( mp4_chunk_t ) );
1841 if( p_demux_track->chunk == NULL )
1846 /* first we read chunk offset */
1847 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1849 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1851 ck->i_offset = BOXDATA(p_co64)->i_chunk_offset[i_chunk];
1853 ck->i_first_dts = 0;
1854 ck->p_sample_count_dts = NULL;
1855 ck->p_sample_delta_dts = NULL;
1856 ck->p_sample_count_pts = NULL;
1857 ck->p_sample_offset_pts = NULL;
1860 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1861 to be used for the sample XXX begin to 1
1862 We construct it begining at the end */
1863 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1864 i_index = BOXDATA(p_stsc)->i_entry_count;
1866 while( i_index-- > 0 )
1868 for( i_chunk = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
1869 i_chunk < i_last; i_chunk++ )
1871 if( i_chunk >= p_demux_track->i_chunk_count )
1873 msg_Warn( p_demux, "corrupted chunk table" );
1874 return VLC_EGENERIC;
1877 p_demux_track->chunk[i_chunk].i_sample_description_index =
1878 BOXDATA(p_stsc)->i_sample_description_index[i_index];
1879 p_demux_track->chunk[i_chunk].i_sample_count =
1880 BOXDATA(p_stsc)->i_samples_per_chunk[i_index];
1882 i_last = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
1885 if ( p_demux_track->i_chunk_count )
1887 p_demux_track->chunk[0].i_sample_first = 0;
1888 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1890 p_demux_track->chunk[i_chunk].i_sample_first =
1891 p_demux_track->chunk[i_chunk-1].i_sample_first +
1892 p_demux_track->chunk[i_chunk-1].i_sample_count;
1896 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1897 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1899 if ( p_demux_track->i_chunk_count && (
1900 p_sys->moovfragment.i_chunk_range_min_offset == 0 ||
1901 p_sys->moovfragment.i_chunk_range_min_offset > p_demux_track->chunk[0].i_offset
1903 p_sys->moovfragment.i_chunk_range_min_offset = p_demux_track->chunk[0].i_offset;
1908 static int xTTS_CountEntries( demux_t *p_demux, uint32_t *pi_entry /* out */,
1909 const uint32_t i_index,
1910 uint32_t i_index_samples_left,
1911 uint32_t i_sample_count,
1912 const uint32_t *pi_index_sample_count,
1913 const uint32_t i_table_count )
1915 uint32_t i_array_offset;
1916 while( i_sample_count > 0 )
1918 if ( likely((UINT32_MAX - i_index) >= *pi_entry) )
1919 i_array_offset = i_index + *pi_entry;
1921 return VLC_EGENERIC;
1923 if ( i_array_offset >= i_table_count )
1925 msg_Err( p_demux, "invalid index counting total samples %u %u", i_array_offset, i_table_count );
1926 return VLC_EGENERIC;
1929 if ( i_index_samples_left )
1931 if ( i_index_samples_left > i_sample_count )
1933 i_index_samples_left -= i_sample_count;
1935 *pi_entry +=1; /* No samples left, go copy */
1940 i_sample_count -= i_index_samples_left;
1941 i_index_samples_left = 0;
1948 i_sample_count -= __MIN( i_sample_count, pi_index_sample_count[i_array_offset] );
1956 static int TrackCreateSamplesIndex( demux_t *p_demux,
1957 mp4_track_t *p_demux_track )
1959 demux_sys_t *p_sys = p_demux->p_sys;
1962 MP4_Box_data_stsz_t *stsz;
1963 /* TODO use also stss and stsh table for seeking */
1964 /* FIXME use edit table */
1967 * Gives the sample size for each samples. There is also a stz2 table
1968 * (compressed form) that we need to implement TODO */
1969 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1972 /* FIXME and stz2 */
1973 msg_Warn( p_demux, "cannot find STSZ box" );
1974 return VLC_EGENERIC;
1976 stsz = p_box->data.p_stsz;
1978 /* Use stsz table to create a sample number -> sample size table */
1979 p_demux_track->i_sample_count = stsz->i_sample_count;
1980 if( stsz->i_sample_size )
1982 /* 1: all sample have the same size, so no need to construct a table */
1983 p_demux_track->i_sample_size = stsz->i_sample_size;
1984 p_demux_track->p_sample_size = NULL;
1988 /* 2: each sample can have a different size */
1989 p_demux_track->i_sample_size = 0;
1990 p_demux_track->p_sample_size =
1991 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1992 if( p_demux_track->p_sample_size == NULL )
1995 for( uint32_t i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1997 p_demux_track->p_sample_size[i_sample] =
1998 stsz->i_entry_size[i_sample];
2002 if ( p_demux_track->i_chunk_count )
2004 mp4_chunk_t *lastchunk = &p_demux_track->chunk[p_demux_track->i_chunk_count - 1];
2005 uint64_t i_total_size = lastchunk->i_offset;
2007 if ( p_demux_track->i_sample_size != 0 ) /* all samples have same size */
2009 i_total_size += (uint64_t)p_demux_track->i_sample_size * lastchunk->i_sample_count;
2013 if( (uint64_t)lastchunk->i_sample_count + p_demux_track->i_chunk_count - 1 > stsz->i_sample_count )
2015 msg_Err( p_demux, "invalid samples table: stsz table is too small" );
2016 return VLC_EGENERIC;
2019 for( uint32_t i=stsz->i_sample_count - lastchunk->i_sample_count;
2020 i<stsz->i_sample_count; i++)
2022 i_total_size += stsz->i_entry_size[i];
2026 if ( i_total_size > p_sys->moovfragment.i_chunk_range_max_offset )
2027 p_sys->moovfragment.i_chunk_range_max_offset = i_total_size;
2030 /* Use stts table to create a sample number -> dts table.
2031 * XXX: if we don't want to waste too much memory, we can't expand
2032 * the box! so each chunk will contain an "extract" of this table
2033 * for fast research (problem with raw stream where a sample is sometime
2034 * just channels*bits_per_sample/8 */
2036 /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
2037 * differing in 2/2 fields and 1 signedness */
2039 mtime_t i_next_dts = 0;
2041 * Gives mapping between sample and decoding time
2043 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
2046 msg_Warn( p_demux, "cannot find STTS box" );
2047 return VLC_EGENERIC;
2051 MP4_Box_data_stts_t *stts = p_box->data.p_stts;
2053 msg_Warn( p_demux, "STTS table of %"PRIu32" entries", stts->i_entry_count );
2055 /* Create sample -> dts table per chunk */
2056 uint32_t i_index = 0;
2057 uint32_t i_current_index_samples_left = 0;
2059 for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2061 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2062 uint32_t i_entry, i_sample_count;
2064 /* save first dts */
2065 ck->i_first_dts = i_next_dts;
2066 ck->i_last_dts = i_next_dts;
2068 /* count how many entries are needed for this chunk
2069 * for p_sample_delta_dts and p_sample_count_dts */
2072 int i_ret = xTTS_CountEntries( p_demux, &i_entry, i_index,
2073 i_current_index_samples_left,
2075 stts->pi_sample_count,
2076 stts->i_entry_count );
2077 if ( i_ret != VLC_SUCCESS )
2081 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
2082 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
2083 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
2085 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, i_entry );
2090 i_sample_count = ck->i_sample_count;
2092 for( uint32_t i = 0; i < i_entry; i++ )
2094 if ( i_current_index_samples_left )
2096 if ( i_current_index_samples_left > i_sample_count )
2098 if ( i_sample_count ) ck->i_last_dts = i_next_dts;
2099 ck->p_sample_count_dts[i] = i_sample_count;
2100 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2101 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2102 i_current_index_samples_left -= i_sample_count;
2104 assert( i == i_entry - 1 );
2109 if ( i_current_index_samples_left ) ck->i_last_dts = i_next_dts;
2110 ck->p_sample_count_dts[i] = i_current_index_samples_left;
2111 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2112 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2113 i_sample_count -= i_current_index_samples_left;
2114 i_current_index_samples_left = 0;
2120 if ( stts->pi_sample_count[i_index] > i_sample_count )
2122 if ( i_sample_count ) ck->i_last_dts = i_next_dts;
2123 ck->p_sample_count_dts[i] = i_sample_count;
2124 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2125 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2126 i_current_index_samples_left = stts->pi_sample_count[i_index] - i_sample_count;
2128 assert( i == i_entry - 1 );
2129 // keep building from same index
2133 if ( stts->pi_sample_count[i_index] ) ck->i_last_dts = i_next_dts;
2134 ck->p_sample_count_dts[i] = stts->pi_sample_count[i_index];
2135 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2136 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2137 i_sample_count -= stts->pi_sample_count[i_index];
2148 * Gives the delta between decoding time (dts) and composition table (pts)
2150 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
2151 if( p_box && p_box->data.p_ctts )
2153 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
2155 msg_Warn( p_demux, "CTTS table of %"PRIu32" entries", ctts->i_entry_count );
2157 /* Create pts-dts table per chunk */
2158 uint32_t i_index = 0;
2159 uint32_t i_current_index_samples_left = 0;
2161 for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2163 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2164 uint32_t i_entry, i_sample_count;
2166 /* count how many entries are needed for this chunk
2167 * for p_sample_offset_pts and p_sample_count_pts */
2169 int i_ret = xTTS_CountEntries( p_demux, &i_entry, i_index,
2170 i_current_index_samples_left,
2172 ctts->pi_sample_count,
2173 ctts->i_entry_count );
2174 if ( i_ret != VLC_SUCCESS )
2178 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
2179 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
2180 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
2182 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, i_entry );
2187 i_sample_count = ck->i_sample_count;
2189 for( uint32_t i = 0; i < i_entry; i++ )
2191 if ( i_current_index_samples_left )
2193 if ( i_current_index_samples_left > i_sample_count )
2195 ck->p_sample_count_pts[i] = i_sample_count;
2196 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
2197 i_current_index_samples_left -= i_sample_count;
2199 assert( i == i_entry - 1 );
2204 ck->p_sample_count_pts[i] = i_current_index_samples_left;
2205 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
2206 i_sample_count -= i_current_index_samples_left;
2207 i_current_index_samples_left = 0;
2213 if ( ctts->pi_sample_count[i_index] > i_sample_count )
2215 ck->p_sample_count_pts[i] = i_sample_count;
2216 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
2217 i_current_index_samples_left = ctts->pi_sample_count[i_index] - i_sample_count;
2219 assert( i == i_entry - 1 );
2220 // keep building from same index
2224 ck->p_sample_count_pts[i] = ctts->pi_sample_count[i_index];
2225 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
2226 i_sample_count -= ctts->pi_sample_count[i_index];
2236 msg_Dbg( p_demux, "track[Id 0x%x] read %"PRIu32" samples length:%"PRId64"s",
2237 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
2238 i_next_dts / p_demux_track->i_timescale );
2245 * It computes the sample rate for a video track using the given sample
2248 static void TrackGetESSampleRate( demux_t *p_demux,
2249 unsigned *pi_num, unsigned *pi_den,
2250 const mp4_track_t *p_track,
2251 unsigned i_sd_index,
2257 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( MP4_BoxGet( p_demux->p_sys->p_root, "/moov" ),
2258 p_track->i_track_ID );
2259 MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
2260 if ( p_mdhd && BOXDATA(p_mdhd) )
2262 vlc_ureduce( pi_num, pi_den,
2263 (uint64_t) BOXDATA(p_mdhd)->i_timescale * p_track->i_sample_count,
2264 (uint64_t) BOXDATA(p_mdhd)->i_duration,
2269 if( p_track->i_chunk_count == 0 )
2273 const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
2274 while( p_chunk > &p_track->chunk[0] &&
2275 p_chunk[-1].i_sample_description_index == i_sd_index )
2280 uint64_t i_sample = 0;
2281 uint64_t i_first_dts = p_chunk->i_first_dts;
2282 uint64_t i_last_dts;
2285 i_sample += p_chunk->i_sample_count;
2286 i_last_dts = p_chunk->i_last_dts;
2289 while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
2290 p_chunk->i_sample_description_index == i_sd_index );
2292 if( i_sample > 1 && i_first_dts < i_last_dts )
2293 vlc_ureduce( pi_num, pi_den,
2294 ( i_sample - 1) * p_track->i_timescale,
2295 i_last_dts - i_first_dts,
2301 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
2303 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
2304 unsigned int i_chunk, es_out_id_t **pp_es )
2306 demux_sys_t *p_sys = p_demux->p_sys;
2307 unsigned int i_sample_description_index;
2309 if( p_sys->b_fragmented || p_track->i_chunk_count == 0 )
2310 i_sample_description_index = 1; /* XXX */
2312 i_sample_description_index =
2313 p_track->chunk[i_chunk].i_sample_description_index;
2318 if( !i_sample_description_index )
2320 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
2321 p_track->i_track_ID );
2322 return VLC_EGENERIC;
2325 MP4_Box_t *p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
2326 i_sample_description_index - 1 );
2329 ( !p_sample->data.p_payload && p_track->fmt.i_cat != SPU_ES ) )
2331 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
2332 p_track->i_track_ID );
2333 return VLC_EGENERIC;
2336 p_track->p_sample = p_sample;
2339 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) && p_frma->data.p_frma )
2341 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
2343 p_sample->i_type = p_frma->data.p_frma->i_type;
2347 switch( p_track->fmt.i_cat )
2350 if ( !p_sample->data.p_sample_vide || p_sample->i_handler != ATOM_vide )
2352 SetupVideoES( p_demux, p_track, p_sample );
2354 /* Set frame rate */
2355 TrackGetESSampleRate( p_demux,
2356 &p_track->fmt.video.i_frame_rate,
2357 &p_track->fmt.video.i_frame_rate_base,
2358 p_track, i_sample_description_index, i_chunk );
2360 p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
2361 (float)p_track->fmt.video.i_frame_rate_base;
2366 if ( !p_sample->data.p_sample_soun || p_sample->i_handler != ATOM_soun )
2368 SetupAudioES( p_demux, p_track, p_sample );
2372 if ( !p_sample->data.p_sample_text || p_sample->i_handler != ATOM_text )
2374 SetupSpuES( p_demux, p_track, p_sample );
2381 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2386 /* given a time it return sample/chunk
2387 * it also update elst field of the track
2389 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2390 int64_t i_start, uint32_t *pi_chunk,
2391 uint32_t *pi_sample )
2393 demux_sys_t *p_sys = p_demux->p_sys;
2394 MP4_Box_t *p_box_stss;
2396 unsigned int i_sample;
2397 unsigned int i_chunk;
2400 /* FIXME see if it's needed to check p_track->i_chunk_count */
2401 if( p_track->i_chunk_count == 0 )
2402 return( VLC_EGENERIC );
2404 /* handle elst (find the correct one) */
2405 MP4_TrackSetELST( p_demux, p_track, i_start );
2406 if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
2408 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
2409 int64_t i_mvt= i_start * p_sys->i_timescale / CLOCK_FREQ;
2411 /* now calculate i_start for this elst */
2413 i_start -= p_track->i_elst_time * CLOCK_FREQ / p_sys->i_timescale;
2421 /* to track time scale */
2422 i_start = i_start * p_track->i_timescale / CLOCK_FREQ;
2423 /* add elst offset */
2424 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2425 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2426 elst->i_media_time[p_track->i_elst] > 0 )
2428 i_start += elst->i_media_time[p_track->i_elst];
2431 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2432 "ms (track)", p_track->i_elst,
2433 i_mvt * 1000 / p_sys->i_timescale,
2434 i_start * 1000 / p_track->i_timescale );
2438 /* convert absolute time to in timescale unit */
2439 i_start = i_start * p_track->i_timescale / CLOCK_FREQ;
2442 /* we start from sample 0/chunk 0, hope it won't take too much time */
2443 /* *** find good chunk *** */
2444 for( i_chunk = 0; ; i_chunk++ )
2446 if( i_chunk + 1 >= p_track->i_chunk_count )
2448 /* at the end and can't check if i_start in this chunk,
2449 it will be check while searching i_sample */
2450 i_chunk = p_track->i_chunk_count - 1;
2454 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2455 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
2461 /* *** find sample in the chunk *** */
2462 i_sample = p_track->chunk[i_chunk].i_sample_first;
2463 i_dts = p_track->chunk[i_chunk].i_first_dts;
2464 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2467 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2468 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2471 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2472 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2474 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2479 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2483 i_sample += ( i_start - i_dts ) /
2484 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2489 if( i_sample >= p_track->i_sample_count )
2491 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2492 "(seeking too far) chunk=%d sample=%d",
2493 p_track->i_track_ID, i_chunk, i_sample );
2494 return( VLC_EGENERIC );
2498 /* *** Try to find nearest sync points *** */
2499 if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2501 MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2502 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2503 p_track->i_track_ID );
2504 for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2506 if( i_index >= p_stss->i_entry_count - 1 ||
2507 i_sample < p_stss->i_sample_number[i_index+1] )
2509 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2510 msg_Dbg( p_demux, "stss gives %d --> %d (sample number)",
2511 i_sample, i_sync_sample );
2513 if( i_sync_sample <= i_sample )
2515 while( i_chunk > 0 &&
2516 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2521 while( i_chunk < p_track->i_chunk_count - 1 &&
2522 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2523 p_track->chunk[i_chunk].i_sample_count )
2526 i_sample = i_sync_sample;
2533 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2534 "Sample Box (stss)", p_track->i_track_ID );
2537 *pi_chunk = i_chunk;
2538 *pi_sample = i_sample;
2543 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2544 unsigned int i_chunk, unsigned int i_sample )
2546 bool b_reselect = false;
2548 /* now see if actual es is ok */
2549 if( p_track->i_chunk >= p_track->i_chunk_count ||
2550 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2551 p_track->chunk[i_chunk].i_sample_description_index )
2553 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2554 p_track->i_track_ID );
2556 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2557 p_track->p_es, &b_reselect );
2559 es_out_Del( p_demux->out, p_track->p_es );
2561 p_track->p_es = NULL;
2563 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2565 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2566 p_track->i_track_ID );
2568 p_track->b_ok = false;
2569 p_track->b_selected = false;
2570 return VLC_EGENERIC;
2574 /* select again the new decoder */
2577 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2580 p_track->i_chunk = i_chunk;
2581 p_track->i_sample = i_sample;
2583 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2586 /****************************************************************************
2588 ****************************************************************************
2589 * Parse track information and create all needed data to run a track
2590 * If it succeed b_ok is set to 1 else to 0
2591 ****************************************************************************/
2592 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2593 MP4_Box_t *p_box_trak,
2594 bool b_force_enable )
2596 demux_sys_t *p_sys = p_demux->p_sys;
2598 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2599 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2609 char language[4] = { '\0' };
2611 /* hint track unsupported */
2613 /* set default value (-> track unusable) */
2614 p_track->b_ok = false;
2615 p_track->b_enable = false;
2616 p_track->b_selected = false;
2617 p_track->b_chapter = false;
2618 p_track->b_mac_encoding = false;
2620 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2627 /* do we launch this track by default ? */
2629 ( ( BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED ) != 0 );
2630 if( !p_track->b_enable )
2631 p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
2633 p_track->i_track_ID = BOXDATA(p_tkhd)->i_track_ID;
2635 p_track->i_width = BOXDATA(p_tkhd)->i_width / BLOCK16x16;
2636 p_track->i_height = BOXDATA(p_tkhd)->i_height / BLOCK16x16;
2637 p_track->f_rotation = BOXDATA(p_tkhd)->f_rotation;
2641 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2644 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2645 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2647 if( ( !p_mdhd )||( !p_hdlr ) )
2652 p_track->i_timescale = BOXDATA(p_mdhd)->i_timescale;
2653 if( p_track->i_timescale == 0 )
2656 memcpy( &language, BOXDATA(p_mdhd)->rgs_language, 3 );
2657 p_track->b_mac_encoding = BOXDATA(p_mdhd)->b_mac_encoding;
2659 switch( p_hdlr->data.p_hdlr->i_handler_type )
2662 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2666 p_track->fmt.i_cat = AUDIO_ES;
2670 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2674 p_track->fmt.i_cat = VIDEO_ES;
2681 p_track->fmt.i_codec = VLC_CODEC_TX3G;
2682 p_track->fmt.i_cat = SPU_ES;
2685 /* closed captions */
2687 p_track->fmt.i_cat = SPU_ES;
2694 p_track->i_elst = 0;
2695 p_track->i_elst_time = 0;
2696 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2698 MP4_Box_data_elst_t *elst = BOXDATA(p_elst);
2701 msg_Warn( p_demux, "elst box found" );
2702 for( i = 0; i < elst->i_entry_count; i++ )
2704 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2705 "ms) rate=%d.%d", i,
2706 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2707 elst->i_media_time[i] >= 0 ?
2708 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2710 elst->i_media_rate_integer[i],
2711 elst->i_media_rate_fraction[i] );
2718 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2720 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2721 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2727 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2729 p_track->fmt.psz_language = strdup( language );
2732 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2735 MP4_Box_t *p_box_iter;
2736 for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2737 p_box_iter = p_box_iter->p_next )
2739 switch( p_box_iter->i_type )
2743 p_track->fmt.psz_description =
2744 strdup( p_box_iter->data.p_string->psz_text );
2751 /* Create chunk index table and sample index table */
2752 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2753 TrackCreateSamplesIndex( p_demux, p_track ) )
2755 msg_Err( p_demux, "cannot create chunks index" );
2756 return; /* cannot create chunks index */
2759 p_track->i_chunk = 0;
2760 p_track->i_sample = 0;
2762 /* Mark chapter only track */
2763 if( p_sys->p_tref_chap )
2765 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2768 for( i = 0; i < p_chap->i_entry_count; i++ )
2770 if( p_track->i_track_ID == p_chap->i_track_ID[i] &&
2771 p_track->fmt.i_cat == UNKNOWN_ES )
2773 p_track->b_chapter = true;
2774 p_track->b_enable = false;
2781 if( b_force_enable &&
2782 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2784 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2785 p_track->i_track_ID );
2786 p_track->b_enable = true;
2787 p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN;
2790 p_track->p_es = NULL;
2791 if( TrackCreateES( p_demux,
2792 p_track, p_track->i_chunk,
2793 p_track->b_chapter ? NULL : &p_track->p_es ) )
2795 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2796 p_track->i_track_ID );
2799 p_track->b_ok = true;
2803 for( i = 0; i < p_track->i_chunk_count; i++ )
2805 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2806 i, p_track->chunk[i].i_sample_count,
2807 p_track->chunk[i].i_first_dts );
2814 static void FreeAndResetChunk( mp4_chunk_t *ck )
2816 free( ck->p_sample_count_dts );
2817 free( ck->p_sample_delta_dts );
2818 free( ck->p_sample_count_pts );
2819 free( ck->p_sample_offset_pts );
2820 free( ck->p_sample_size );
2821 for( uint32_t i = 0; i < ck->i_sample_count; i++ )
2822 free( ck->p_sample_data[i] );
2823 free( ck->p_sample_data );
2824 memset( ck, 0, sizeof( mp4_chunk_t ) );
2827 /****************************************************************************
2829 ****************************************************************************
2830 * Destroy a track created by MP4_TrackCreate.
2831 ****************************************************************************/
2832 static void MP4_TrackDestroy( mp4_track_t *p_track )
2834 unsigned int i_chunk;
2836 p_track->b_ok = false;
2837 p_track->b_enable = false;
2838 p_track->b_selected = false;
2840 es_format_Clean( &p_track->fmt );
2842 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2844 if( p_track->chunk )
2846 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2847 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2849 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2850 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2853 FREENULL( p_track->chunk );
2854 if( p_track->cchunk ) {
2855 FreeAndResetChunk( p_track->cchunk );
2856 FREENULL( p_track->cchunk );
2859 if( !p_track->i_sample_size )
2861 FREENULL( p_track->p_sample_size );
2864 if ( p_track->asfinfo.p_frame )
2865 block_ChainRelease( p_track->asfinfo.p_frame );
2868 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2871 if( !p_track->b_ok || p_track->b_chapter )
2873 return VLC_EGENERIC;
2876 if( p_track->b_selected )
2878 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2879 p_track->i_track_ID );
2883 return MP4_TrackSeek( p_demux, p_track, i_start );
2886 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2888 if( !p_track->b_ok || p_track->b_chapter )
2893 if( !p_track->b_selected )
2895 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2896 p_track->i_track_ID );
2901 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2902 p_track->p_es, false );
2905 p_track->b_selected = false;
2908 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2914 if( !p_track->b_ok || p_track->b_chapter )
2915 return VLC_EGENERIC;
2917 p_track->b_selected = false;
2919 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2920 &i_chunk, &i_sample ) )
2922 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2923 p_track->i_track_ID );
2924 return VLC_EGENERIC;
2927 p_track->b_selected = true;
2928 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2929 p_track->b_selected = true;
2931 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2936 * 3 types: for audio
2939 #define QT_V0_MAX_SAMPLES 1024
2940 static uint32_t MP4_TrackGetReadSize( mp4_track_t *p_track, uint32_t *pi_nb_samples )
2942 uint32_t i_size = 0;
2945 if ( p_track->i_sample == p_track->i_sample_count )
2948 if ( p_track->fmt.i_cat != AUDIO_ES )
2952 if( p_track->i_sample_size == 0 ) /* all sizes are different */
2953 return p_track->p_sample_size[p_track->i_sample];
2955 return p_track->i_sample_size;
2959 const MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
2960 const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
2961 uint32_t i_max_samples = p_chunk->i_sample_count - p_chunk->i_sample + 1;
2963 /* Group audio packets so we don't call demux for single sample unit */
2964 if( p_track->fmt.i_original_fourcc == VLC_CODEC_DVD_LPCM &&
2965 p_soun->i_constLPCMframesperaudiopacket &&
2966 p_soun->i_constbytesperaudiopacket )
2968 /* uncompressed case */
2969 uint32_t i_packets = i_max_samples / p_soun->i_constLPCMframesperaudiopacket;
2970 if ( UINT32_MAX / p_soun->i_constbytesperaudiopacket < i_packets )
2971 i_packets = UINT32_MAX / p_soun->i_constbytesperaudiopacket;
2972 *pi_nb_samples = i_packets * p_soun->i_constLPCMframesperaudiopacket;
2973 return i_packets * p_soun->i_constbytesperaudiopacket;
2976 /* all samples have a different size */
2977 if( p_track->i_sample_size == 0 )
2980 return p_track->p_sample_size[p_track->i_sample];
2983 if( p_soun->i_qt_version == 1 )
2985 if ( p_soun->i_compressionid != 0 || p_soun->i_bytes_per_sample > 1 ) /* compressed */
2987 /* in this case we are dealing with compressed data
2988 -2 in V1: additional fields are meaningless (VBR and such) */
2989 *pi_nb_samples = i_max_samples;//p_track->chunk[p_track->i_chunk].i_sample_count;
2990 if( p_track->fmt.audio.i_blockalign > 1 )
2991 *pi_nb_samples = p_soun->i_sample_per_packet;
2992 i_size = *pi_nb_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2995 else /* uncompressed case */
2998 if( p_track->fmt.audio.i_blockalign > 1 )
3001 i_packets = i_max_samples / p_soun->i_sample_per_packet;
3003 if ( UINT32_MAX / p_soun->i_bytes_per_frame < i_packets )
3004 i_packets = UINT32_MAX / p_soun->i_bytes_per_frame;
3006 *pi_nb_samples = i_packets * p_soun->i_sample_per_packet;
3007 i_size = i_packets * p_soun->i_bytes_per_frame;
3012 /* uncompressed v0 */
3014 for( uint32_t i=p_track->i_sample;
3015 i<p_chunk->i_sample_first+p_chunk->i_sample_count &&
3016 i<p_track->i_sample_count;
3020 if ( p_track->i_sample_size == 0 )
3021 i_size += p_track->p_sample_size[i];
3023 i_size += p_track->i_sample_size;
3024 if ( *pi_nb_samples == QT_V0_MAX_SAMPLES )
3029 //fprintf( stderr, "size=%d\n", i_size );
3033 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
3035 unsigned int i_sample;
3038 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
3040 if( p_track->i_sample_size )
3042 MP4_Box_data_sample_soun_t *p_soun =
3043 p_track->p_sample->data.p_sample_soun;
3045 if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
3047 i_pos += ( p_track->i_sample -
3048 p_track->chunk[p_track->i_chunk].i_sample_first ) *
3049 p_track->i_sample_size;
3053 /* we read chunk by chunk unless a blockalign is requested */
3054 if( p_track->fmt.audio.i_blockalign > 1 )
3055 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
3056 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
3061 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
3062 i_sample < p_track->i_sample; i_sample++ )
3064 i_pos += p_track->p_sample_size[i_sample];
3071 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track, uint32_t i_samples )
3073 if ( UINT32_MAX - p_track->i_sample < i_samples )
3075 p_track->i_sample = UINT32_MAX;
3076 return VLC_EGENERIC;
3079 p_track->i_sample += i_samples;
3081 if( p_track->i_sample >= p_track->i_sample_count )
3082 return VLC_EGENERIC;
3084 /* Have we changed chunk ? */
3085 if( p_track->i_sample >=
3086 p_track->chunk[p_track->i_chunk].i_sample_first +
3087 p_track->chunk[p_track->i_chunk].i_sample_count )
3089 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
3090 p_track->i_sample ) )
3092 msg_Warn( p_demux, "track[0x%x] will be disabled "
3093 "(cannot restart decoder)", p_track->i_track_ID );
3094 MP4_TrackUnselect( p_demux, p_track );
3095 return VLC_EGENERIC;
3099 /* Have we changed elst */
3100 if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
3102 demux_sys_t *p_sys = p_demux->p_sys;
3103 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
3104 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
3105 p_sys->i_timescale / CLOCK_FREQ;
3107 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
3108 i_mvt >= p_track->i_elst_time +
3109 elst->i_segment_duration[p_track->i_elst] )
3111 MP4_TrackSetELST( p_demux, p_track,
3112 MP4_TrackGetDTS( p_demux, p_track ) );
3119 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
3122 demux_sys_t *p_sys = p_demux->p_sys;
3123 int i_elst_last = tk->i_elst;
3125 /* handle elst (find the correct one) */
3127 tk->i_elst_time = 0;
3128 if( tk->p_elst && tk->BOXDATA(p_elst)->i_entry_count > 0 )
3130 MP4_Box_data_elst_t *elst = tk->BOXDATA(p_elst);
3131 int64_t i_mvt= i_time * p_sys->i_timescale / CLOCK_FREQ;
3133 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
3135 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
3137 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
3141 tk->i_elst_time += i_dur;
3144 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
3146 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
3147 tk->i_elst = elst->i_entry_count - 1;
3148 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
3151 if( elst->i_media_time[tk->i_elst] < 0 )
3154 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
3157 if( i_elst_last != tk->i_elst )
3159 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
3163 /******************************************************************************
3164 * Here are the functions used for fragmented MP4
3165 *****************************************************************************/
3168 * It computes the sample rate for a video track using current video chunk
3170 static void ChunkGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
3171 const mp4_track_t *p_track )
3173 if( p_track->cchunk->i_last_dts == 0 )
3180 const mp4_chunk_t *p_chunk = p_track->cchunk;
3182 uint32_t i_sample = p_chunk->i_sample_count;
3183 uint64_t i_first_dts = p_chunk->i_first_dts;
3184 uint64_t i_last_dts = p_chunk->i_last_dts;
3186 if( i_sample > 1 && i_first_dts < i_last_dts )
3187 vlc_ureduce( pi_num, pi_den,
3188 ( i_sample - 1) * p_track->i_timescale,
3189 i_last_dts - i_first_dts,
3194 * Build raw avcC box (without the 8 bytes header)
3195 * \return The size of the box.
3197 static int build_raw_avcC( uint8_t **p_extra, const uint8_t *CodecPrivateData,
3198 const unsigned cpd_len )
3201 unsigned sps_len = 0, pps_len = 0;
3202 const uint32_t mark = 0x00000001;
3204 assert( CodecPrivateData[0] == 0 );
3205 assert( CodecPrivateData[1] == 0 );
3206 assert( CodecPrivateData[2] == 0 );
3207 assert( CodecPrivateData[3] == 1 );
3209 uint32_t length = cpd_len + 3;
3210 avcC = calloc( length, 1 );
3211 if( unlikely( avcC == NULL ) )
3214 uint8_t *sps = avcC + 8;
3216 uint32_t candidate = ~mark;
3217 CodecPrivateData += 4;
3218 for( unsigned i = 0; i < cpd_len - 4; i++ )
3220 sps[i] = CodecPrivateData[i];
3221 candidate = (candidate << 8) | CodecPrivateData[i];
3222 if( candidate == mark )
3233 uint8_t *pps = sps + sps_len + 3;
3234 pps_len = cpd_len - sps_len - 4 * 2;
3235 memcpy( pps, CodecPrivateData + sps_len + 4, pps_len );
3238 uint8_t AVCProfileIndication = 0x64;
3239 uint8_t profile_compatibility = 0x40;
3240 uint8_t AVCLevelIndication = 0x1f;
3241 uint8_t lengthSizeMinusOne = 0x03;
3244 avcC[1] = AVCProfileIndication;
3245 avcC[2] = profile_compatibility;
3246 avcC[3] = AVCLevelIndication;
3247 avcC[4] = 0xfc + lengthSizeMinusOne;
3249 avcC[6] = (sps_len & 0xff00)>>8;
3250 avcC[7] = sps_len & 0xff;
3252 avcC[8+sps_len] = 1;
3253 avcC[9+sps_len] = (pps_len & 0xff00) >> 8;
3254 avcC[10+sps_len] = pps_len & 0xff;
3261 * Build a mp4_track_t from a StraBox
3264 static inline int MP4_SetCodecExtraData( es_format_t *fmt, MP4_Box_data_stra_t *p_data )
3266 fmt->i_extra = p_data->cpd_len;
3267 fmt->p_extra = malloc( p_data->cpd_len );
3268 if( unlikely( !fmt->p_extra ) )
3270 memcpy( fmt->p_extra, p_data->CodecPrivateData, p_data->cpd_len );
3274 static int MP4_frg_TrackCreate( demux_t *p_demux, mp4_track_t *p_track, MP4_Box_t *p_stra )
3276 demux_sys_t *p_sys = p_demux->p_sys;
3278 MP4_Box_data_stra_t *p_data = BOXDATA(p_stra);
3280 return VLC_EGENERIC;
3282 p_track->b_ok = true;
3283 p_track->b_selected = false;
3284 p_track->i_sample_count = UINT32_MAX;
3286 p_track->i_timescale = p_sys->i_timescale;
3287 p_track->i_width = p_data->MaxWidth;
3288 p_track->i_height = p_data->MaxHeight;
3289 p_track->i_track_ID = p_data->i_track_ID;
3291 es_format_t *fmt = &p_track->fmt;
3293 return VLC_EGENERIC;
3295 es_format_Init( fmt, p_data->i_es_cat, 0 );
3297 /* Set language FIXME */
3298 fmt->psz_language = strdup( "en" );
3300 fmt->i_original_fourcc = p_data->FourCC;
3301 fmt->i_codec = vlc_fourcc_GetCodec( fmt->i_cat, p_data->FourCC );
3303 uint8_t **p_extra = (uint8_t **)&fmt->p_extra;
3304 /* See http://msdn.microsoft.com/en-us/library/ff728116%28v=vs.90%29.aspx
3305 * for MS weird use of FourCC*/
3306 switch( fmt->i_cat )
3309 if( p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', '1' ) ||
3310 p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', 'B' ) ||
3311 p_data->FourCC == VLC_FOURCC( 'H', '2', '6', '4' ) )
3313 fmt->i_extra = build_raw_avcC( p_extra,
3314 p_data->CodecPrivateData, p_data->cpd_len );
3318 ret = MP4_SetCodecExtraData( fmt, p_data );
3319 if( ret != VLC_SUCCESS )
3323 fmt->video.i_width = p_data->MaxWidth;
3324 fmt->video.i_height = p_data->MaxHeight;
3325 fmt->video.i_bits_per_pixel = 0x18;
3326 fmt->video.i_visible_width = p_data->MaxWidth;
3327 fmt->video.i_visible_height = p_data->MaxHeight;
3330 ChunkGetESSampleRate( &fmt->video.i_frame_rate,
3331 &fmt->video.i_frame_rate_base, p_track );
3333 if( fmt->video.i_frame_rate_base != 0 )
3335 p_demux->p_sys->f_fps = (float)fmt->video.i_frame_rate /
3336 (float)fmt->video.i_frame_rate_base;
3339 p_demux->p_sys->f_fps = 24;
3344 fmt->audio.i_channels = p_data->Channels;
3345 fmt->audio.i_rate = p_data->SamplingRate;
3346 fmt->audio.i_bitspersample = p_data->BitsPerSample;
3347 fmt->audio.i_blockalign = p_data->nBlockAlign;
3349 fmt->i_bitrate = p_data->Bitrate;
3351 ret = MP4_SetCodecExtraData( fmt, p_data );
3352 if( ret != VLC_SUCCESS )
3365 * Return the track identified by tid
3367 static mp4_track_t *MP4_frg_GetTrackByID( demux_t *p_demux, const uint32_t tid )
3369 demux_sys_t *p_sys = p_demux->p_sys;
3371 mp4_track_t *ret = NULL;
3372 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
3374 ret = &p_sys->track[i];
3375 if( ret->i_track_ID == tid )
3378 msg_Err( p_demux, "MP4_frg_GetTrack: track %"PRIu32" not found!", tid );
3382 static void FlushChunk( demux_t *p_demux, mp4_track_t *tk )
3384 msg_Dbg( p_demux, "Flushing chunk for track id %u", tk->i_track_ID );
3385 mp4_chunk_t *ck = tk->cchunk;
3386 while( ck->i_sample < ck->i_sample_count )
3391 if( ck->p_sample_size == NULL || ck->p_sample_data == NULL )
3394 uint32_t sample_size = ck->p_sample_size[ck->i_sample];
3395 assert( sample_size > 0 );
3396 p_block = block_Alloc( sample_size );
3397 if( unlikely( !p_block ) )
3400 uint8_t *src = ck->p_sample_data[ck->i_sample];
3401 memcpy( p_block->p_buffer, src, sample_size );
3405 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
3407 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
3409 p_block->i_pts = p_block->i_dts + i_delta;
3410 else if( tk->fmt.i_cat != VIDEO_ES )
3411 p_block->i_pts = p_block->i_dts;
3413 p_block->i_pts = VLC_TS_INVALID;
3415 MP4_Block_Send( p_demux, tk, p_block );
3423 * \Note If we call that function too soon,
3424 * before the track has been selected by MP4_TrackSelect
3425 * (during the first execution of Demux), then the track gets disabled
3427 static int ReInitDecoder( demux_t *p_demux, mp4_track_t *p_track )
3429 demux_sys_t *p_sys = p_demux->p_sys;
3431 uint32_t i_sample = 0;
3432 bool b_smooth = false;
3433 MP4_Box_t *p_stra = NULL, *p_trak = NULL;
3435 if( !CmpUUID( &p_sys->p_root->p_first->i_uuid, &SmooBoxUUID ) )
3440 p_stra = MP4_BoxGet( p_sys->p_root, "uuid/uuid[0]" );
3441 if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
3442 return VLC_EGENERIC;
3446 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[0]" );
3448 return VLC_EGENERIC;
3451 i_sample = p_track->i_sample;
3452 es_out_Del( p_demux->out, p_track->p_es );
3453 es_format_Clean( &p_track->fmt );
3456 MP4_frg_TrackCreate( p_demux, p_track, p_stra );
3458 MP4_TrackCreate( p_demux, p_track, p_trak, true );
3460 p_track->i_sample = i_sample;
3462 /* Temporary hack until we support track selection */
3463 p_track->b_selected = true;
3464 p_track->b_ok = true;
3465 p_track->b_enable = true;
3467 p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
3468 p_track->b_codec_need_restart = false;
3474 * This function fills a mp4_chunk_t structure from a MP4_Box_t (p_chunk).
3475 * The 'i_tk_id' argument returns the ID of the track the chunk belongs to.
3476 * \note p_chunk usually contains a 'moof' and a 'mdat', and might contain a 'sidx'.
3477 * \return VLC_SUCCESS, VLC_EGENERIC or VLC_ENOMEM.
3479 static int MP4_frg_GetChunk( demux_t *p_demux, MP4_Box_t *p_chunk, unsigned *i_tk_id )
3481 MP4_Box_t *p_sidx = MP4_BoxGet( p_chunk, "sidx" );
3482 MP4_Box_t *p_moof = MP4_BoxGet( p_chunk, "moof" );
3485 msg_Warn( p_demux, "no moof box found!" );
3486 return VLC_EGENERIC;
3489 MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
3492 msg_Warn( p_demux, "no traf box found!" );
3493 return VLC_EGENERIC;
3496 MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
3499 msg_Warn( p_demux, "no tfhd box found!" );
3500 return VLC_EGENERIC;
3503 uint32_t i_track_ID = BOXDATA(p_tfhd)->i_track_ID;
3504 *i_tk_id = i_track_ID;
3505 assert( i_track_ID > 0 );
3506 msg_Dbg( p_demux, "GetChunk: track ID is %"PRIu32"", i_track_ID );
3508 mp4_track_t *p_track = MP4_frg_GetTrackByID( p_demux, i_track_ID );
3510 return VLC_EGENERIC;
3512 mp4_chunk_t *ret = p_track->cchunk;
3514 if( BOXDATA(p_tfhd)->b_empty )
3515 msg_Warn( p_demux, "No samples in this chunk!" );
3517 /* Usually we read 100 ms of each track. However, suppose we have two tracks,
3518 * Ta and Tv (audio and video). Suppose also that Ta is the first track to be
3519 * read, i.e. we read 100 ms of Ta, then 100 ms of Tv, then 100 ms of Ta,
3520 * and so on. Finally, suppose that we get the chunks the other way around,
3521 * i.e. first a chunk of Tv, then a chunk of Ta, then a chunk of Tv, and so on.
3522 * In that case, it is very likely that at some point, Ta->cchunk or Tv->cchunk
3523 * is not emptied when MP4_frg_GetChunks is called. It is therefore necessary to
3524 * flush it, i.e. send to the decoder the samples not yet sent.
3525 * Note that all the samples to be flushed should worth less than 100 ms,
3526 * (though I did not do the formal proof) and thus this flushing mechanism
3527 * should not cause A/V sync issues, or delays or whatever.
3529 if( ret->i_sample < ret->i_sample_count )
3530 FlushChunk( p_demux, p_track );
3532 if( ret->i_sample_count )
3533 FreeAndResetChunk( ret );
3535 uint32_t default_duration = 0;
3536 if( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
3537 default_duration = BOXDATA(p_tfhd)->i_default_sample_duration;
3539 uint32_t default_size = 0;
3540 if( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
3541 default_size = BOXDATA(p_tfhd)->i_default_sample_size;
3543 MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun");
3546 msg_Warn( p_demux, "no trun box found!" );
3547 return VLC_EGENERIC;
3549 MP4_Box_data_trun_t *p_trun_data = p_trun->data.p_trun;
3551 ret->i_sample_count = p_trun_data->i_sample_count;
3552 assert( ret->i_sample_count > 0 );
3553 ret->i_sample_description_index = 1; /* seems to be always 1, is it? */
3554 ret->i_sample_first = p_track->i_sample_first;
3555 p_track->i_sample_first += ret->i_sample_count;
3557 ret->i_first_dts = p_track->i_first_dts;
3559 /* XXX I already saw DASH content with no default_duration and no
3560 * MP4_TRUN_SAMPLE_DURATION flag, but a sidx box was present.
3561 * This chunk of code might be buggy with segments having
3562 * more than one subsegment. */
3563 if( !default_duration )
3565 MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_moof, i_track_ID );
3567 default_duration = BOXDATA(p_trex)->i_default_sample_duration;
3570 MP4_Box_data_sidx_t *p_sidx_data = BOXDATA(p_sidx);
3571 assert( p_sidx_data->i_reference_count == 1 );
3573 if( p_sidx_data->i_timescale == 0 )
3574 return VLC_EGENERIC;
3576 unsigned i_chunk_duration = p_sidx_data->p_items[0].i_subsegment_duration /
3577 p_sidx_data->i_timescale;
3578 default_duration = i_chunk_duration * p_track->i_timescale / ret->i_sample_count;
3583 msg_Dbg( p_demux, "Default sample duration is %"PRIu32, default_duration );
3585 ret->p_sample_count_dts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3586 ret->p_sample_delta_dts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3588 if( !ret->p_sample_count_dts || !ret->p_sample_delta_dts )
3591 ret->p_sample_count_pts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3592 if( !ret->p_sample_count_pts )
3595 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
3597 ret->p_sample_offset_pts = calloc( ret->i_sample_count, sizeof( int32_t ) );
3598 if( !ret->p_sample_offset_pts )
3602 ret->p_sample_size = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3603 if( !ret->p_sample_size )
3606 ret->p_sample_data = calloc( ret->i_sample_count, sizeof( uint8_t * ) );
3607 if( !ret->p_sample_data )
3610 uint32_t dur = 0, i_mdatlen = 0, len;
3611 uint32_t chunk_duration = 0, chunk_size = 0;
3613 /* Skip header of mdat */
3615 int i_read = stream_Read( p_demux->s, &mdat, 8 );
3616 i_mdatlen = GetDWBE( mdat );
3617 if ( i_read < 8 || i_mdatlen < 8 ||
3618 VLC_FOURCC( mdat[4], mdat[5], mdat[6], mdat[7] ) != ATOM_mdat )
3619 return VLC_EGENERIC;
3621 for( uint32_t i = 0; i < ret->i_sample_count; i++)
3623 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_DURATION )
3624 dur = p_trun_data->p_samples[i].i_duration;
3626 dur = default_duration;
3627 ret->p_sample_delta_dts[i] = dur;
3628 chunk_duration += dur;
3630 ret->p_sample_count_dts[i] = ret->p_sample_count_pts[i] = 1;
3632 if( ret->p_sample_offset_pts )
3634 if ( p_trun_data->i_version == 0 )
3635 ret->p_sample_offset_pts[i] = (int32_t) p_trun_data->p_samples[i].i_composition_time_offset;
3637 ret->p_sample_offset_pts[i] = __MIN( INT32_MAX, p_trun_data->p_samples[i].i_composition_time_offset );
3640 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_SIZE )
3641 len = ret->p_sample_size[i] = p_trun_data->p_samples[i].i_size;
3643 len = ret->p_sample_size[i] = default_size;
3645 if ( chunk_size + len > ( i_mdatlen - 8 ) )
3646 return VLC_EGENERIC;
3648 ret->p_sample_data[i] = malloc( len );
3649 if( ret->p_sample_data[i] == NULL )
3651 uint32_t i_read = stream_ReadU32( p_demux->s, ret->p_sample_data[i], len );
3653 return VLC_EGENERIC;
3656 ret->i_last_dts = ret->i_first_dts + chunk_duration - dur;
3657 p_track->i_first_dts = chunk_duration + ret->i_first_dts;
3659 if( p_track->b_codec_need_restart &&
3660 p_track->fmt.i_cat == VIDEO_ES )
3661 ReInitDecoder( p_demux, p_track );
3663 /* Skip if we didn't reach the end of mdat box */
3664 if ( chunk_size < (i_mdatlen - 8) )
3665 stream_ReadU32( p_demux->s, NULL, i_mdatlen - chunk_size - 8 );
3667 p_track->b_has_non_empty_cchunk = true;
3673 * Get the next chunk of the track identified by i_tk_id.
3674 * \Note We don't want to seek all the time, so if the first chunk given by the
3675 * input method doesn't belong to the right track, we don't throw it away,
3676 * and so, in general, this function fetch more than one chunk.
3677 * Not to mention that a new init fragment might be put everywhere
3678 * between two chunks by the input method.
3680 static int MP4_frg_GetChunks( demux_t *p_demux, const unsigned i_tk_id )
3682 demux_sys_t *p_sys = p_demux->p_sys;
3683 mp4_track_t *p_track;
3685 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
3687 MP4_Box_t *p_chunk = MP4_BoxGetNextChunk( p_demux->s );
3689 return VLC_EGENERIC;
3691 if( !p_chunk->p_first )
3692 goto MP4_frg_GetChunks_Error;
3693 uint32_t i_type = p_chunk->p_first->i_type;
3695 if( i_type == ATOM_uuid || i_type == ATOM_ftyp )
3697 MP4_BoxFree( p_demux->s, p_sys->p_root );
3698 p_sys->p_root = p_chunk;
3700 if( i_type == ATOM_ftyp ) /* DASH */
3702 MP4_Box_t *p_tkhd = MP4_BoxGet( p_chunk, "/moov/trak[0]/tkhd" );
3705 msg_Warn( p_demux, "No tkhd found!" );
3706 goto MP4_frg_GetChunks_Error;
3708 tid = p_tkhd->data.p_tkhd->i_track_ID;
3710 else /* Smooth Streaming */
3712 assert( !CmpUUID( &p_chunk->p_first->i_uuid, &SmooBoxUUID ) );
3713 MP4_Box_t *p_stra = MP4_BoxGet( p_chunk, "/uuid/uuid[0]" );
3714 if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
3716 msg_Warn( p_demux, "No StraBox found!" );
3717 goto MP4_frg_GetChunks_Error;
3719 tid = p_stra->data.p_stra->i_track_ID;
3722 p_track = MP4_frg_GetTrackByID( p_demux, tid );
3724 goto MP4_frg_GetChunks_Error;
3725 p_track->b_codec_need_restart = true;
3727 return MP4_frg_GetChunks( p_demux, i_tk_id );
3730 if( MP4_frg_GetChunk( p_demux, p_chunk, &tid ) != VLC_SUCCESS )
3731 goto MP4_frg_GetChunks_Error;
3733 MP4_BoxFree( p_demux->s, p_chunk );
3735 if( tid == i_tk_id )
3740 MP4_frg_GetChunks_Error:
3741 MP4_BoxFree( p_demux->s, p_chunk );
3742 return VLC_EGENERIC;
3748 static int MP4_frg_TrackSelect( demux_t *p_demux, mp4_track_t *p_track )
3750 if( !p_track->b_ok || p_track->b_chapter )
3752 return VLC_EGENERIC;
3755 if( p_track->b_selected )
3757 msg_Warn( p_demux, "track[Id 0x%x] already selected", p_track->i_track_ID );
3761 msg_Dbg( p_demux, "Select track id %u", p_track->i_track_ID );
3762 p_track->b_selected = true;
3767 * DemuxFrg: read packet and send them to decoders
3768 * \return 1 on success, 0 on error.
3769 * TODO check for newly selected track
3771 int DemuxFrg( demux_t *p_demux )
3773 demux_sys_t *p_sys = p_demux->p_sys;
3775 unsigned i_track_selected;
3777 /* check for newly selected/unselected track */
3778 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
3780 mp4_track_t *tk = &p_sys->track[i_track];
3783 if( !tk->b_ok || tk->b_chapter )
3786 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
3787 msg_Dbg( p_demux, "track %u %s!", tk->i_track_ID, b ? "enabled" : "disabled" );
3789 if( tk->b_selected && !b )
3791 MP4_TrackUnselect( p_demux, tk );
3793 else if( !tk->b_selected && b)
3795 MP4_frg_TrackSelect( p_demux, tk );
3798 if( tk->b_selected )
3802 if( i_track_selected <= 0 )
3804 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
3805 if( p_sys->i_timescale > 0 )
3807 int64_t i_length = CLOCK_FREQ *
3808 (mtime_t)p_sys->moovfragment.i_duration /
3809 (mtime_t)p_sys->i_timescale;
3810 if( MP4_GetMoviePTS( p_sys ) >= i_length )
3815 msg_Warn( p_demux, "no track selected, exiting..." );
3819 /* first wait for the good time to read a packet */
3820 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
3822 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
3824 /* we will read 100ms for each stream so ...*/
3825 p_sys->i_time += __MAX( p_sys->i_timescale / 10, 1 );
3827 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
3829 mp4_track_t *tk = &p_sys->track[i_track];
3831 if( !tk->b_ok || tk->b_chapter || !tk->b_selected )
3833 msg_Warn( p_demux, "Skipping track id %u", tk->i_track_ID );
3837 if( !tk->b_has_non_empty_cchunk )
3839 if( MP4_frg_GetChunks( p_demux, tk->i_track_ID ) != VLC_SUCCESS )
3841 msg_Info( p_demux, "MP4_frg_GetChunks returned error. End of stream?" );
3846 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
3851 mp4_chunk_t *ck = tk->cchunk;
3852 if( ck->i_sample >= ck->i_sample_count )
3854 msg_Err( p_demux, "sample %"PRIu32" of %"PRIu32"",
3855 ck->i_sample, ck->i_sample_count );
3859 uint32_t sample_size = ck->p_sample_size[ck->i_sample];
3860 p_block = block_Alloc( sample_size );
3861 uint8_t *src = ck->p_sample_data[ck->i_sample];
3862 memcpy( p_block->p_buffer, src, sample_size );
3865 if( ck->i_sample == ck->i_sample_count )
3866 tk->b_has_non_empty_cchunk = false;
3869 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
3871 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
3873 p_block->i_pts = p_block->i_dts + i_delta;
3874 else if( tk->fmt.i_cat != VIDEO_ES )
3875 p_block->i_pts = p_block->i_dts;
3877 p_block->i_pts = VLC_TS_INVALID;
3879 MP4_Block_Send( p_demux, tk, p_block );
3883 if( !tk->b_has_non_empty_cchunk )
3890 static MP4_Box_t * LoadNextChunk( demux_t *p_demux )
3892 /* Read Next Chunk */
3893 MP4_Box_t *p_chunk = MP4_BoxGetNextChunk( p_demux->s );
3896 msg_Warn( p_demux, "no next chunk" );
3900 if( !p_chunk->p_first )
3902 msg_Warn( p_demux, "no next chunk child" );
3909 static bool BoxExistsInRootTree( MP4_Box_t *p_root, uint32_t i_type, off_t i_pos )
3913 if ( p_root->i_pos == i_pos )
3915 assert( i_type == p_root->i_type );
3918 p_root = p_root->p_next;
3920 return (p_root != NULL);
3923 /* Keeps an ordered chain of all fragments */
3924 static bool AddFragment( demux_t *p_demux, MP4_Box_t *p_moox )
3926 demux_sys_t *p_sys = p_demux->p_sys;
3927 mp4_fragment_t *p_base_fragment = & p_sys->moovfragment;
3931 if( p_moox->i_type == ATOM_moov )
3933 if ( !p_sys->moovfragment.p_moox )
3935 p_sys->moovfragment.p_moox = p_moox;
3937 if( (p_mvhd = MP4_BoxGet( p_moox, "mvhd" )) )
3939 p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
3940 p_sys->i_overall_duration = BOXDATA(p_mvhd)->i_duration;
3943 if ( MP4_BoxCount( p_moox, "mvex" ) || !p_mvhd )
3944 { /* duration might be wrong an be set to whole duration :/ */
3945 p_sys->moovfragment.i_duration = 0;
3947 MP4_Box_t *p_trak = MP4_BoxGet( p_moox, "trak" );
3950 if ( p_trak->i_type == ATOM_trak && (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) )
3952 if ( BOXDATA(p_tkhd)->i_duration > p_sys->moovfragment.i_duration )
3953 p_sys->moovfragment.i_duration = BOXDATA(p_tkhd)->i_duration;
3955 p_trak = p_trak->p_next;
3959 msg_Dbg( p_demux, "added fragment %4.4s", (char*) &p_moox->i_type );
3964 else // p_moox->i_type == ATOM_moof
3966 assert(p_moox->i_type == ATOM_moof);
3967 mp4_fragment_t *p_fragment = p_sys->moovfragment.p_next;
3968 while ( p_fragment )
3970 if ( !p_base_fragment->p_moox || p_moox->i_pos > p_base_fragment->p_moox->i_pos )
3972 p_base_fragment = p_fragment;
3973 p_fragment = p_fragment->p_next;
3975 else if ( p_moox->i_pos == p_base_fragment->p_moox->i_pos )
3977 /* already exists */
3983 /* Add the moof fragment */
3984 mp4_fragment_t *p_new = malloc(sizeof(mp4_fragment_t));
3985 if ( !p_new ) return false;
3986 p_new->p_moox = p_moox;
3987 p_new->i_duration = 0;
3988 p_new->p_next = p_base_fragment->p_next;
3989 p_base_fragment->p_next = p_new;
3990 msg_Dbg( p_demux, "added fragment %4.4s", (char*) &p_moox->i_type );
3992 /* we have to probe all fragments :/ */
3993 uint64_t i_traf_base_data_offset = 0;
3994 uint64_t i_traf_min_offset = 0;
3995 uint32_t i_traf = 0;
3996 uint32_t i_traf_total_size = 0;
3997 uint32_t i_trafs_total_size = 0;
3999 MP4_Box_t *p_traf = MP4_BoxGet( p_new->p_moox, "traf" );
4002 if ( p_traf->i_type != ATOM_traf )
4004 p_traf = p_traf->p_next;
4007 const MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
4008 const MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun" );
4009 if ( !p_tfhd || !p_trun )
4011 p_traf = p_traf->p_next;
4015 uint32_t i_track_timescale = 0;
4016 uint32_t i_track_defaultsamplesize = 0;
4017 uint32_t i_track_defaultsampleduration = 0;
4018 if ( p_sys->b_smooth )
4020 /* stra sets identical timescales */
4021 i_track_timescale = p_sys->i_timescale;
4022 i_track_defaultsamplesize = 1;
4023 i_track_defaultsampleduration = 1;
4027 /* set trex for defaults */
4028 MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_sys->moovfragment.p_moox, BOXDATA(p_tfhd)->i_track_ID );
4031 i_track_defaultsamplesize = BOXDATA(p_trex)->i_default_sample_size;
4032 i_track_defaultsampleduration = BOXDATA(p_trex)->i_default_sample_duration;
4035 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->moovfragment.p_moox, BOXDATA(p_tfhd)->i_track_ID );
4038 MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
4039 if ( p_mdhd ) i_track_timescale = BOXDATA(p_mdhd)->i_timescale;
4043 if ( !i_track_timescale )
4045 p_traf = p_traf->p_next;
4049 if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
4051 i_traf_base_data_offset = BOXDATA(p_tfhd)->i_base_data_offset;
4053 else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DEFAULT_BASE_IS_MOOF )
4055 i_traf_base_data_offset = p_new->p_moox->i_pos /* + 8*/;
4060 i_traf_base_data_offset = p_new->p_moox->i_pos /*+ 8*/;
4062 i_traf_base_data_offset += i_traf_total_size;
4065 i_traf_total_size = 0;
4067 uint64_t i_trun_data_offset = i_traf_base_data_offset;
4068 uint64_t i_traf_duration = 0;
4069 uint32_t i_trun_size = 0;
4070 while ( p_trun && p_tfhd )
4072 if ( p_trun->i_type != ATOM_trun )
4074 p_trun = p_trun->p_next;
4077 const MP4_Box_data_trun_t *p_trundata = p_trun->data.p_trun;
4079 /* Get data offset */
4080 if ( p_trundata->i_flags & MP4_TRUN_DATA_OFFSET )
4081 i_trun_data_offset += __MAX( p_trundata->i_data_offset, 0 );
4083 i_trun_data_offset += i_trun_size;
4087 /* Sum total time */
4088 if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_DURATION )
4090 for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
4091 i_traf_duration += p_trundata->p_samples[i].i_duration;
4093 else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
4095 i_traf_duration += p_trundata->i_sample_count *
4096 BOXDATA(p_tfhd)->i_default_sample_duration;
4100 i_traf_duration += p_trundata->i_sample_count *
4101 i_track_defaultsampleduration;
4104 /* Get total traf size */
4105 if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_SIZE )
4107 for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
4108 i_trun_size += p_trundata->p_samples[i].i_size;
4110 else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
4112 i_trun_size += p_trundata->i_sample_count *
4113 BOXDATA(p_tfhd)->i_default_sample_size;
4117 i_trun_size += p_trundata->i_sample_count *
4118 i_track_defaultsamplesize;
4121 i_traf_total_size += i_trun_size;
4123 if ( i_traf_min_offset )
4124 i_traf_min_offset = __MIN( i_trun_data_offset, i_traf_min_offset );
4126 i_traf_min_offset = i_trun_data_offset;
4128 p_trun = p_trun->p_next;
4131 i_trafs_total_size += i_traf_total_size;
4132 p_new->i_duration = __MAX( p_new->i_duration, i_traf_duration * p_sys->i_timescale / i_track_timescale );
4134 p_traf = p_traf->p_next;
4138 p_new->i_chunk_range_min_offset = i_traf_min_offset;
4139 p_new->i_chunk_range_max_offset = i_traf_min_offset + i_trafs_total_size;
4141 msg_Dbg( p_demux, "new fragment is %"PRId64" %"PRId64, p_new->i_chunk_range_min_offset, p_new->i_chunk_range_max_offset );
4143 /* compute total duration with that new fragment if no overall provided */
4144 MP4_Box_t *p_mehd = MP4_BoxGet( p_sys->moovfragment.p_moox, "mvex/mehd");
4147 p_sys->i_overall_duration = 0;
4148 if ( p_sys->b_fragments_probed )
4150 p_new = &p_sys->moovfragment;
4153 p_sys->i_overall_duration += p_new->i_duration;
4154 p_new = p_new->p_next;
4159 p_sys->i_overall_duration = BOXDATA(p_mehd)->i_fragment_duration;
4161 msg_Dbg( p_demux, "total fragments duration %"PRId64, CLOCK_FREQ * p_sys->i_overall_duration / p_sys->i_timescale);
4165 static int ProbeIndex( demux_t *p_demux )
4167 demux_sys_t *p_sys = p_demux->p_sys;
4168 uint64_t i_backup_pos, i_stream_size;
4169 uint8_t mfro[MP4_MFRO_BOXSIZE];
4170 assert( p_sys->b_seekable );
4172 if ( MP4_BoxCount( p_sys->p_root, "/mfra" ) )
4175 i_stream_size = stream_Size( p_demux->s );
4176 if ( ( i_stream_size >> 62 ) ||
4177 ( i_stream_size < MP4_MFRO_BOXSIZE ) ||
4178 ( !MP4_stream_Tell( p_demux->s, &i_backup_pos ) ) ||
4179 ( stream_Seek( p_demux->s, i_stream_size - MP4_MFRO_BOXSIZE ) != VLC_SUCCESS )
4182 msg_Dbg( p_demux, "Probing tail for mfro has failed" );
4183 return VLC_EGENERIC;
4186 if ( stream_Read( p_demux->s, &mfro, MP4_MFRO_BOXSIZE ) == MP4_MFRO_BOXSIZE &&
4187 VLC_FOURCC(mfro[4],mfro[5],mfro[6],mfro[7]) == ATOM_mfro &&
4188 GetDWBE( &mfro ) == MP4_MFRO_BOXSIZE )
4190 uint32_t i_offset = GetDWBE( &mfro[12] );
4191 msg_Dbg( p_demux, "will read mfra index at %"PRIu64, i_stream_size - i_offset );
4192 if ( i_stream_size > i_offset &&
4193 stream_Seek( p_demux->s, i_stream_size - i_offset ) == VLC_SUCCESS )
4195 msg_Dbg( p_demux, "reading mfra index at %"PRIu64, i_stream_size - i_offset );
4196 MP4_ReadBoxContainerChildren( p_demux->s, p_sys->p_root, ATOM_mfra );
4200 return stream_Seek( p_demux->s, i_backup_pos );
4203 static int ProbeFragments( demux_t *p_demux, bool b_force )
4205 demux_sys_t *p_sys = p_demux->p_sys;
4206 uint64_t i_current_pos;
4208 if ( MP4_stream_Tell( p_demux->s, &i_current_pos ) )
4209 msg_Dbg( p_demux, "probing fragments from %"PRId64, i_current_pos );
4211 assert( p_sys->p_root );
4213 if ( p_sys->b_fastseekable || b_force )
4215 MP4_ReadBoxContainerChildren( p_demux->s, p_sys->p_root, 0 ); /* Get the rest of the file */
4216 p_sys->b_fragments_probed = true;
4220 /* We stop at first moof, which validates our fragmentation condition
4221 * and we'll find others while reading. */
4222 MP4_ReadBoxContainerChildren( p_demux->s, p_sys->p_root, ATOM_moof );
4225 if ( !p_sys->moovfragment.p_moox )
4227 MP4_Box_t *p_moov = MP4_BoxGet( p_sys->p_root, "/moov" );
4230 /* moov/mvex before probing should be present anyway */
4231 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
4232 return VLC_EGENERIC;
4234 AddFragment( p_demux, p_moov );
4237 MP4_Box_t *p_moof = MP4_BoxGet( p_sys->p_root, "moof" );
4240 if ( p_moof->i_type == ATOM_moof )
4241 AddFragment( p_demux, p_moof );
4242 p_moof = p_moof->p_next;
4245 MP4_Box_t *p_mdat = MP4_BoxGet( p_sys->p_root, "mdat" );
4248 stream_Seek( p_demux->s, p_mdat->i_pos );
4249 msg_Dbg( p_demux, "rewinding to mdat %"PRId64, p_mdat->i_pos );
4255 static mp4_fragment_t * GetFragmentByPos( demux_t *p_demux, uint64_t i_pos, bool b_exact )
4257 mp4_fragment_t *p_fragment = &p_demux->p_sys->moovfragment;
4258 while ( p_fragment )
4260 if ( i_pos <= p_fragment->i_chunk_range_max_offset &&
4261 ( !b_exact || i_pos >= p_fragment->i_chunk_range_min_offset ) )
4263 msg_Dbg( p_demux, "fragment matched %"PRIu64" << %"PRIu64" << %"PRIu64,
4264 p_fragment->i_chunk_range_min_offset, i_pos,
4265 p_fragment->i_chunk_range_max_offset );
4270 p_fragment = p_fragment->p_next;
4276 /* Get a matching fragment data start by clock time */
4277 static mp4_fragment_t * GetFragmentByTime( demux_t *p_demux, const mtime_t i_time )
4279 mp4_fragment_t *p_fragment = &p_demux->p_sys->moovfragment;
4280 mtime_t i_base_time = 0;
4281 while ( p_fragment )
4283 mtime_t i_length = CLOCK_FREQ * p_fragment->i_duration / p_demux->p_sys->i_timescale;
4284 if ( i_time >= i_base_time &&
4285 i_time <= i_base_time + i_length )
4291 i_base_time += i_length;
4292 p_fragment = p_fragment->p_next;
4295 if ( !p_demux->p_sys->b_fragments_probed )
4296 return NULL; /* We have no way to guess missing fragments time */
4301 /* Returns fragment scaled time offset */
4302 static mtime_t LeafGetFragmentTimeOffset( demux_t *p_demux, mp4_fragment_t *p_fragment )
4304 mtime_t i_base_scaledtime = 0;
4305 mp4_fragment_t *p_current = &p_demux->p_sys->moovfragment;
4306 while ( p_current != p_fragment )
4308 i_base_scaledtime += p_current->i_duration;
4309 p_current = p_current->p_next;
4311 return i_base_scaledtime;
4314 static int LeafParseTRUN( demux_t *p_demux, mp4_track_t *p_track,
4315 const uint32_t i_defaultduration, const uint32_t i_defaultsize,
4316 const MP4_Box_data_trun_t *p_trun, uint32_t * const pi_mdatlen )
4318 assert( p_trun->i_sample_count );
4319 msg_Dbg( p_demux, "Default sample duration %"PRIu32" size %"PRIu32" firstdts %"PRIu64,
4320 i_defaultduration, i_defaultsize, p_track->i_first_dts );
4322 uint32_t dur = 0, len;
4323 uint32_t chunk_size = 0;
4324 mtime_t i_nzdts = CLOCK_FREQ * p_track->i_time / p_track->i_timescale;
4328 for( uint32_t i = 0; i < p_trun->i_sample_count; i++)
4330 i_nzdts += CLOCK_FREQ * dur / p_track->i_timescale;
4333 if( p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
4334 dur = p_trun->p_samples[i].i_duration;
4336 dur = i_defaultduration;
4338 p_track->i_time += dur;
4340 if( p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
4342 if ( p_trun->i_version == 0 )
4343 i_nzpts += CLOCK_FREQ * (int32_t) p_trun->p_samples[i].i_composition_time_offset / p_track->i_timescale;
4345 i_nzpts += CLOCK_FREQ * p_trun->p_samples[i].i_composition_time_offset / p_track->i_timescale;
4348 if( p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
4349 len = p_trun->p_samples[i].i_size;
4351 len = i_defaultsize;
4353 assert( dur ); /* dur, dur ! */
4356 if ( chunk_size + len > *pi_mdatlen )
4358 /* update data left in mdat */
4359 *pi_mdatlen -= chunk_size;
4360 return VLC_EGENERIC;
4363 block_t *p_block = MP4_Block_Read( p_demux, p_track, __MIN( len, INT32_MAX ) );
4364 uint32_t i_read = ( p_block ) ? p_block->i_buffer : 0;
4367 /* update data left in mdat */
4368 *pi_mdatlen -= chunk_size;
4369 *pi_mdatlen -= i_read;
4371 return VLC_EGENERIC;
4374 if ( p_demux->p_sys->i_pcr < VLC_TS_0 )
4376 p_demux->p_sys->i_pcr = i_nzdts;
4377 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + i_nzdts );
4380 if ( p_track->p_es )
4382 p_block->i_dts = VLC_TS_0 + i_nzdts;
4383 p_block->i_pts = VLC_TS_0 + i_nzpts;
4384 p_block->i_length = CLOCK_FREQ * dur / p_track->i_timescale;
4385 MP4_Block_Send( p_demux, p_track, p_block );
4387 else free( p_block );
4392 /* update data left in mdat */
4393 *pi_mdatlen -= chunk_size;
4397 static int LeafGetTrackAndChunkByMOOVPos( demux_t *p_demux, uint64_t *pi_pos,
4398 mp4_track_t **pp_tk, unsigned int *pi_chunk )
4400 const demux_sys_t *p_sys = p_demux->p_sys;
4402 mp4_track_t *p_tk_closest = NULL;
4403 uint64_t i_closest = UINT64_MAX;
4404 unsigned int i_chunk_closest;
4408 for ( unsigned int i_track = 0; i_track < p_sys->i_tracks; i_track++ )
4410 for( unsigned int i_chunk = 0; i_chunk < p_sys->track[i_track].i_chunk_count; i_chunk++ )
4412 if ( p_sys->track[i_track].chunk[i_chunk].i_offset > *pi_pos )
4414 i_closest = __MIN( i_closest, p_sys->track[i_track].chunk[i_chunk].i_offset );
4415 p_tk_closest = &p_sys->track[i_track];
4416 i_chunk_closest = i_chunk;
4419 if ( *pi_pos == p_sys->track[i_track].chunk[i_chunk].i_offset )
4421 *pp_tk = &p_sys->track[i_track];
4422 *pi_chunk = i_chunk;
4428 if ( i_closest != UINT64_MAX )
4430 *pi_pos = i_closest;
4431 *pp_tk = p_tk_closest;
4432 *pi_chunk = i_chunk_closest;
4435 else return VLC_EGENERIC;
4438 static int LeafMOOVGetSamplesSize( const mp4_track_t *p_track, const uint32_t i_sample,
4439 uint32_t *pi_samplestoread, uint32_t *pi_samplessize,
4440 const uint32_t i_maxbytes, const uint32_t i_maxsamples )
4442 MP4_Box_t *p_stsz = MP4_BoxGet( p_track->p_stbl, "stsz" );
4444 return VLC_EGENERIC;
4446 if ( BOXDATA(p_stsz)->i_sample_size == 0 )
4448 uint32_t i_entry = i_sample;
4449 uint32_t i_totalbytes = 0;
4450 *pi_samplestoread = 1;
4452 if ( i_sample >= BOXDATA(p_stsz)->i_sample_count )
4453 return VLC_EGENERIC;
4455 *pi_samplessize = BOXDATA(p_stsz)->i_entry_size[i_sample];
4456 i_totalbytes += *pi_samplessize;
4458 if ( *pi_samplessize > i_maxbytes )
4459 return VLC_EGENERIC;
4462 while( i_entry < BOXDATA(p_stsz)->i_sample_count &&
4463 *pi_samplessize == BOXDATA(p_stsz)->i_entry_size[i_entry] &&
4464 i_totalbytes + *pi_samplessize < i_maxbytes &&
4465 *pi_samplestoread < i_maxsamples
4468 i_totalbytes += *pi_samplessize;
4469 (*pi_samplestoread)++;
4472 *pi_samplessize = i_totalbytes;
4476 /* all samples have same size */
4477 *pi_samplessize = BOXDATA(p_stsz)->i_sample_size;
4478 *pi_samplestoread = __MIN( i_maxsamples, BOXDATA(p_stsz)->i_sample_count );
4479 *pi_samplestoread = __MIN( i_maxbytes / *pi_samplessize, *pi_samplestoread );
4480 *pi_samplessize = *pi_samplessize * *pi_samplestoread;
4486 static inline mtime_t LeafGetMOOVTimeInChunk( const mp4_chunk_t *p_chunk, uint32_t i_sample )
4489 uint32_t i_index = 0;
4491 while( i_sample > 0 )
4493 if( i_sample > p_chunk->p_sample_count_dts[i_index] )
4495 i_time += p_chunk->p_sample_count_dts[i_index] *
4496 p_chunk->p_sample_delta_dts[i_index];
4497 i_sample -= p_chunk->p_sample_count_dts[i_index];
4502 i_time += i_sample * p_chunk->p_sample_delta_dts[i_index];
4510 static int LeafParseMDATwithMOOV( demux_t *p_demux )
4512 demux_sys_t *p_sys = p_demux->p_sys;
4514 assert( p_sys->context.i_current_box_type == ATOM_mdat );
4515 assert( p_sys->context.p_fragment->p_moox->i_type == ATOM_moov );
4517 uint64_t i_current_pos;
4518 if ( !MP4_stream_Tell( p_demux->s, &i_current_pos ) )
4519 return VLC_EGENERIC;
4521 if ( p_sys->context.i_mdatbytesleft == 0 ) /* Start parsing new mdat */
4523 /* Ready mdat section */
4525 int i_read = stream_Read( p_demux->s, &mdat, 8 );
4526 p_sys->context.i_mdatbytesleft = GetDWBE( mdat );
4527 if ( i_read < 8 || p_sys->context.i_mdatbytesleft < 8 ||
4528 VLC_FOURCC( mdat[4], mdat[5], mdat[6], mdat[7] ) != ATOM_mdat )
4531 if ( !MP4_stream_Tell( p_demux->s, &i_pos ) )
4532 msg_Err( p_demux, "No mdat atom at %"PRIu64, i_pos - __MAX( 0, i_read ) );
4533 return VLC_EGENERIC;
4536 p_sys->context.i_mdatbytesleft -= 8;
4539 while( p_sys->context.i_mdatbytesleft > 0 )
4541 mp4_track_t *p_track;
4542 unsigned int i_chunk;
4545 uint64_t i_pos = i_current_pos;
4546 int i_ret = LeafGetTrackAndChunkByMOOVPos( p_demux, &i_pos, &p_track, &i_chunk );
4547 if ( i_ret == VLC_EGENERIC )
4549 msg_Err(p_demux, "can't find referenced chunk for start at %"PRIu64, i_current_pos );
4552 else if( i_ret == VLC_ENOOBJ )
4554 assert( i_pos - i_current_pos > p_sys->context.i_mdatbytesleft );
4555 int i_read = stream_Read( p_demux->s, NULL, i_pos - i_current_pos );
4556 i_current_pos += i_read;
4557 p_sys->context.i_mdatbytesleft -= i_read;
4558 if ( i_read == 0 ) goto error;
4563 mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
4565 uint32_t i_nb_samples_at_chunk_start = p_chunk->i_sample_first;
4566 uint32_t i_nb_samples_in_chunk = p_chunk->i_sample_count;
4568 assert(i_nb_samples_in_chunk);
4570 uint32_t i_nb_samples = 0;
4571 while ( i_nb_samples < i_nb_samples_in_chunk )
4573 uint32_t i_samplessize = 0;
4574 uint32_t i_samplescounttoread = 0;
4575 i_ret = LeafMOOVGetSamplesSize( p_track,
4576 i_nb_samples_at_chunk_start + i_nb_samples,
4577 &i_samplescounttoread, &i_samplessize,
4578 p_sys->context.i_mdatbytesleft,
4579 /*i_nb_samples_in_chunk - i_nb_samples*/ 1 );
4580 if ( i_ret != VLC_SUCCESS )
4583 if( p_sys->context.i_mdatbytesleft &&
4584 p_sys->context.i_mdatbytesleft >= i_samplessize )
4590 if( !(p_block = MP4_Block_Read( p_demux, p_track, i_samplessize )) )
4593 if ( MP4_stream_Tell( p_demux->s, &i_pos ) )
4595 p_sys->context.i_mdatbytesleft -= ( i_pos - i_current_pos );
4596 msg_Err( p_demux, "stream block error %"PRId64" %"PRId64, i_pos, i_pos - i_current_pos );
4601 i_nb_samples += i_samplescounttoread;
4602 i_current_pos += i_samplessize;
4603 p_sys->context.i_mdatbytesleft -= i_samplessize;
4606 mtime_t i_time = LeafGetMOOVTimeInChunk( p_chunk, i_nb_samples );
4607 i_time += p_chunk->i_first_dts;
4608 p_track->i_time = i_time;
4609 p_block->i_dts = VLC_TS_0 + CLOCK_FREQ * i_time / p_track->i_timescale;
4612 int64_t i_delta = MP4_TrackGetPTSDelta( p_demux, p_track );
4614 p_block->i_pts = p_block->i_dts + i_delta;
4615 else if( p_track->fmt.i_cat != VIDEO_ES )
4616 p_block->i_pts = p_block->i_dts;
4618 p_block->i_pts = VLC_TS_INVALID;
4620 MP4_Block_Send( p_demux, p_track, p_block );
4622 if ( p_demux->p_sys->i_pcr < VLC_TS_0 )
4624 p_sys->i_time = p_track->i_time * p_sys->i_timescale / p_track->i_timescale;
4625 p_demux->p_sys->i_pcr = MP4_GetMoviePTS( p_demux->p_sys );
4626 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_demux->p_sys->i_pcr );
4632 // sample size > left bytes
4636 p_track->i_sample += i_samplescounttoread;
4639 /* flag end of mdat section if needed */
4640 if ( p_sys->context.i_mdatbytesleft == 0 )
4641 p_sys->context.i_current_box_type = 0;
4648 /* Skip if we didn't reach the end of mdat box */
4649 if ( p_sys->context.i_mdatbytesleft > 0 )
4651 msg_Err( p_demux, "mdat had still %"PRIu32" bytes unparsed as samples",
4652 p_sys->context.i_mdatbytesleft );
4653 stream_ReadU32( p_demux->s, NULL, p_sys->context.i_mdatbytesleft );
4655 p_sys->context.i_current_box_type = 0;
4660 static mp4_track_t * LeafGetTrackByTrunPos( demux_t *p_demux, const uint64_t i_pos, const uint64_t i_moofpos )
4662 demux_sys_t *p_sys = p_demux->p_sys;
4664 for ( uint32_t i=0; i<p_sys->i_tracks; i++ )
4666 mp4_track_t *p_track = &p_sys->track[i];
4667 if ( !p_track->context.p_trun || !p_track->context.p_tfhd )
4669 const MP4_Box_data_trun_t *p_trun_data = p_track->context.BOXDATA(p_trun);
4670 uint64_t i_offset = 0;
4672 if ( p_track->context.BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
4673 i_offset = p_track->context.BOXDATA(p_tfhd)->i_base_data_offset;
4675 i_offset = i_moofpos;
4677 //if ( p_track->context.BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DEFAULT_BASE_IS_MOOF )
4678 // i_offset += i_moofpos;
4680 if (p_trun_data->i_flags & MP4_TRUN_DATA_OFFSET)
4681 i_offset += p_trun_data->i_data_offset;
4685 if ( i_offset == i_pos )
4692 static int LeafMapTrafTrunContextes( demux_t *p_demux, MP4_Box_t *p_moof )
4694 demux_sys_t *p_sys = p_demux->p_sys;
4697 for ( uint32_t i=0; i<p_sys->i_tracks; i++ )
4699 mp4_track_t *p_track = &p_sys->track[i];
4700 p_track->context.p_tfhd = NULL;
4701 p_track->context.p_traf = NULL;
4702 p_track->context.p_trun = NULL;
4705 if ( p_moof->i_type == ATOM_moov )
4708 MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
4711 msg_Warn( p_demux, "no traf box found!" );
4712 return VLC_EGENERIC;
4718 if ( p_traf->i_type == ATOM_traf )
4720 MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
4721 for ( uint32_t i=0; p_tfhd && i<p_sys->i_tracks; i++ )
4723 mp4_track_t *p_track = &p_sys->track[i];
4724 if ( BOXDATA(p_tfhd)->i_track_ID == p_track->i_track_ID )
4726 MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun" );
4729 p_track->context.p_tfhd = p_tfhd;
4730 p_track->context.p_traf = p_traf;
4731 p_track->context.p_trun = p_trun;
4733 p_tfhd = NULL; /* break loop */
4737 p_traf = p_traf->p_next;
4743 static int LeafIndexGetMoofPosByTime( demux_t *p_demux, const mtime_t i_target_time,
4744 uint64_t *pi_pos, mtime_t *pi_mooftime )
4746 MP4_Box_t *p_tfra = MP4_BoxGet( p_demux->p_sys->p_root, "mfra/tfra" );
4749 if ( p_tfra->i_type == ATOM_tfra )
4752 const MP4_Box_data_tfra_t *p_data = BOXDATA(p_tfra);
4753 mp4_track_t *p_track = MP4_frg_GetTrackByID( p_demux, p_data->i_track_ID );
4754 if ( p_track && (p_track->fmt.i_cat == AUDIO_ES || p_track->fmt.i_cat == VIDEO_ES) )
4756 for ( uint32_t i = 0; i<p_data->i_number_of_entries; i += ( p_data->i_version == 1 ) ? 2 : 1 )
4760 if ( p_data->i_version == 1 )
4762 i_time = *((uint64_t *)(p_data->p_time + i));
4763 i_offset = *((uint64_t *)(p_data->p_moof_offset + i));
4767 i_time = p_data->p_time[i];
4768 i_offset = p_data->p_moof_offset[i];
4771 if ( CLOCK_FREQ * i_time / p_track->i_timescale >= i_target_time )
4773 if ( i_pos == -1 ) /* Not in this traf */
4776 *pi_pos = (uint64_t) i_pos;
4777 *pi_mooftime = CLOCK_FREQ * i_time / p_track->i_timescale;
4778 if ( p_track->fmt.i_cat == AUDIO_ES )
4779 *pi_mooftime -= CLOCK_FREQ / p_track->fmt.audio.i_rate * p_data->p_sample_number[i];
4781 *pi_mooftime -= CLOCK_FREQ / p_demux->p_sys->f_fps * p_data->p_sample_number[i];
4789 p_tfra = p_tfra->p_next;
4791 return VLC_EGENERIC;
4794 static int LeafParseMDATwithMOOF( demux_t *p_demux, MP4_Box_t *p_moof )
4796 demux_sys_t *p_sys = p_demux->p_sys;
4798 assert( p_moof->i_type == ATOM_moof );
4799 assert( p_sys->context.i_current_box_type == ATOM_mdat );
4801 if ( p_sys->context.i_mdatbytesleft == 0 )
4803 int i_ret = LeafMapTrafTrunContextes( p_demux, p_moof );
4804 if ( i_ret != VLC_SUCCESS )
4807 /* Ready mdat section */
4809 int i_read = stream_Read( p_demux->s, &mdat, 8 );
4810 p_sys->context.i_mdatbytesleft = GetDWBE( mdat );
4811 if ( i_read < 8 || p_sys->context.i_mdatbytesleft < 8 ||
4812 VLC_FOURCC( mdat[4], mdat[5], mdat[6], mdat[7] ) != ATOM_mdat )
4814 if ( MP4_stream_Tell( p_demux->s, &i_pos ) )
4815 msg_Err(p_demux, "No mdat atom at %"PRIu64, i_pos - i_read );
4816 return VLC_EGENERIC;
4818 p_sys->context.i_mdatbytesleft -= 8;
4821 if ( !MP4_stream_Tell( p_demux->s, &i_pos ) )
4822 return VLC_EGENERIC;
4823 if ( p_sys->b_smooth )
4824 i_pos -= p_moof->i_pos;
4825 mp4_track_t *p_track = LeafGetTrackByTrunPos( p_demux, i_pos, p_moof->i_pos );
4828 MP4_Box_data_tfhd_t *p_tfhd_data = p_track->context.BOXDATA(p_tfhd);
4829 uint32_t i_trun_sample_default_duration = 0;
4830 uint32_t i_trun_sample_default_size = 0;
4832 if ( p_track->context.p_trun )
4834 /* Get defaults for this/these RUN */
4835 if( p_tfhd_data->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
4836 i_trun_sample_default_duration = p_tfhd_data->i_default_sample_duration;
4838 if( p_tfhd_data->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
4839 i_trun_sample_default_size = p_tfhd_data->i_default_sample_size;
4841 if( !i_trun_sample_default_duration || !i_trun_sample_default_size )
4843 MP4_Box_t *p_trex = MP4_BoxGet( p_demux->p_sys->p_root, "moov/mvex/trex");
4846 while( p_trex && BOXDATA(p_trex)->i_track_ID != p_tfhd_data->i_track_ID )
4847 p_trex = p_trex->p_next;
4848 if ( p_trex && !i_trun_sample_default_duration )
4849 i_trun_sample_default_duration = BOXDATA(p_trex)->i_default_sample_duration;
4850 if ( p_trex && !i_trun_sample_default_size )
4851 i_trun_sample_default_size = BOXDATA(p_trex)->i_default_sample_size;
4855 const MP4_Box_data_trun_t *p_trun_data = p_track->context.BOXDATA(p_trun);
4857 /* NOW PARSE TRUN WITH MDAT */
4858 int i_ret = LeafParseTRUN( p_demux, p_track,
4859 i_trun_sample_default_duration, i_trun_sample_default_size,
4860 p_trun_data, & p_sys->context.i_mdatbytesleft );
4861 if ( i_ret != VLC_SUCCESS )
4864 p_track->context.p_trun = p_track->context.p_trun->p_next;
4867 if ( p_sys->context.i_mdatbytesleft == 0 )
4868 p_sys->context.i_current_box_type = 0;
4873 /* Skip if we didn't reach the end of mdat box */
4874 if ( p_sys->context.i_mdatbytesleft > 0 )
4876 msg_Warn( p_demux, "mdat had still %"PRIu32" bytes unparsed as samples", p_sys->context.i_mdatbytesleft - 8 );
4877 stream_ReadU32( p_demux->s, NULL, p_sys->context.i_mdatbytesleft );
4880 p_sys->context.i_current_box_type = 0;
4885 static int DemuxAsLeaf( demux_t *p_demux )
4887 demux_sys_t *p_sys = p_demux->p_sys;
4888 unsigned i_track_selected = 0;
4890 /* check for newly selected/unselected track */
4891 for( unsigned i_track = 0; i_track < p_sys->i_tracks; i_track++ )
4893 mp4_track_t *tk = &p_sys->track[i_track];
4896 if( !tk->b_ok || tk->b_chapter )
4899 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
4900 msg_Dbg( p_demux, "track %u %s!", tk->i_track_ID, b ? "enabled" : "disabled" );
4902 if( tk->b_selected && !b )
4903 MP4_TrackUnselect( p_demux, tk );
4904 else if( !tk->b_selected && b)
4905 MP4_frg_TrackSelect( p_demux, tk );
4907 if( tk->b_selected )
4911 if( i_track_selected <= 0 )
4913 msg_Warn( p_demux, "no track selected, exiting..." );
4917 if ( p_sys->context.i_current_box_type != ATOM_mdat )
4919 /* Othewise mdat is skipped. FIXME: mdat reading ! */
4920 const uint8_t *p_peek;
4921 int i_read = stream_Peek( p_demux->s, &p_peek, 8 );
4925 p_sys->context.i_current_box_type = VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
4927 if ( p_sys->context.i_current_box_type != ATOM_mdat )
4929 if ( ! BoxExistsInRootTree( p_sys->p_root, p_sys->context.i_current_box_type, stream_Tell( p_demux->s ) ) )
4930 {// only if !b_probed ??
4931 MP4_Box_t *p_vroot = LoadNextChunk( p_demux );
4932 switch( p_sys->context.i_current_box_type )
4936 /* create fragment */
4937 AddFragment( p_demux, p_vroot->p_first );
4943 /* Append to root */
4944 p_sys->p_root->p_last->p_next = p_vroot->p_first;
4945 p_sys->p_root->p_last = p_vroot->p_first;
4946 p_vroot->p_last = NULL;
4947 p_vroot->p_next = NULL;
4948 p_vroot->p_first = NULL;
4949 MP4_BoxFree( p_demux->s, p_vroot );
4954 msg_Err( p_demux, "skipping known chunk type %4.4s size %"PRIu32, (char*)& p_sys->context.i_current_box_type, GetDWBE( p_peek ) );
4955 stream_Read( p_demux->s, NULL, GetDWBE( p_peek ) );
4960 /* skip mdat header */
4961 p_sys->context.p_fragment = GetFragmentByPos( p_demux,
4962 stream_Tell( p_demux->s ) + 8, true );
4967 if ( p_sys->context.i_current_box_type == ATOM_mdat )
4969 assert(p_sys->context.p_fragment);
4970 if ( p_sys->context.p_fragment )
4971 switch( p_sys->context.p_fragment->p_moox->i_type )
4973 case ATOM_moov://[ftyp/moov, mdat]+ -> [moof, mdat]+
4974 LeafParseMDATwithMOOV( p_demux );
4977 LeafParseMDATwithMOOF( p_demux, p_sys->context.p_fragment->p_moox ); // BACKUP CHUNK!
4980 msg_Err( p_demux, "fragment type %4.4s", (char*) &p_sys->context.p_fragment->p_moox->i_type );
4985 /* Get current time */
4986 mtime_t i_lowest_dts = VLC_TS_INVALID;
4987 mtime_t i_lowest_time = INT64_MAX;
4988 for( unsigned int i_track = 0; i_track < p_sys->i_tracks; i_track++ )
4990 const mp4_track_t *p_track = &p_sys->track[i_track];
4991 if( !p_track->b_selected || ( p_track->fmt.i_cat != VIDEO_ES && p_track->fmt.i_cat != AUDIO_ES ) )
4994 i_lowest_time = __MIN( i_lowest_time, p_track->i_time * p_sys->i_timescale / p_track->i_timescale );
4996 if ( i_lowest_dts == VLC_TS_INVALID )
4997 i_lowest_dts = CLOCK_FREQ * p_track->i_time / p_track->i_timescale;
4999 i_lowest_dts = __MIN( i_lowest_dts, CLOCK_FREQ * p_track->i_time / p_track->i_timescale );
5002 p_sys->i_time = i_lowest_time;
5003 p_sys->i_pcr = i_lowest_dts;
5004 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
5010 inline static mp4_track_t *MP4ASF_GetTrack( asf_packet_sys_t *p_packetsys,
5011 uint8_t i_stream_number )
5013 demux_sys_t *p_sys = p_packetsys->p_demux->p_sys;
5014 for ( unsigned int i=0; i<p_sys->i_tracks; i++ )
5016 if ( p_sys->track[i].p_asf &&
5017 i_stream_number == p_sys->track[i].BOXDATA(p_asf)->i_stream_number )
5019 return &p_sys->track[i];
5025 static asf_track_info_t * MP4ASF_GetTrackInfo( asf_packet_sys_t *p_packetsys,
5026 uint8_t i_stream_number )
5028 mp4_track_t *p_track = MP4ASF_GetTrack( p_packetsys, i_stream_number );
5030 return &p_track->asfinfo;
5035 static void MP4ASF_Send( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
5036 block_t **pp_frame )
5038 mp4_track_t *p_track = MP4ASF_GetTrack( p_packetsys, i_stream_number );
5041 block_Release( *pp_frame );
5045 block_t *p_gather = block_ChainGather( *pp_frame );
5046 p_gather->i_dts = p_track->i_dts_backup;
5047 p_gather->i_pts = p_track->i_pts_backup;
5048 es_out_Send( p_packetsys->p_demux->out, p_track->p_es, p_gather );
5054 static void MP4ASF_ResetFrames( demux_sys_t *p_sys )
5056 for ( unsigned int i=0; i<p_sys->i_tracks; i++ )
5058 mp4_track_t *p_track = &p_sys->track[i];
5059 if( p_track->asfinfo.p_frame )
5061 block_ChainRelease( p_track->asfinfo.p_frame );
5062 p_track->asfinfo.p_frame = NULL;