1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 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 General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
44 set_description( _("MP4 stream demuxer") );
45 set_capability( "demux2", 242 );
46 set_callbacks( Open, Close );
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Demux ( demux_t * );
53 static int DemuxRef( demux_t *p_demux )
57 static int Seek ( demux_t *, mtime_t );
58 static int Control ( demux_t *, int, va_list );
60 /* Contain all information about a chunk */
63 uint64_t i_offset; /* absolute position of this chunk in the file */
64 uint32_t i_sample_description_index; /* index for SampleEntry to use */
65 uint32_t i_sample_count; /* how many samples in this chunk */
66 uint32_t i_sample_first; /* index of the first sample in this chunk */
68 /* now provide way to calculate pts, dts, and offset without to
69 much memory and with fast acces */
71 /* with this we can calculate dts/pts without waste memory */
73 uint32_t *p_sample_count_dts;
74 uint32_t *p_sample_delta_dts; /* dts delta */
76 /* TODO if needed add pts
77 but quickly *add* support for edts and seeking */
82 /* Contain all needed information for read all track with vlc */
85 int i_track_ID; /* this should be unique */
87 int b_ok; /* The track is usable */
88 int b_enable; /* is the trak enable by default */
89 vlc_bool_t b_selected; /* is the trak being played */
94 /* display size only ! */
98 /* more internal data */
99 uint64_t i_timescale; /* time scale for this track only */
102 int i_elst; /* current elst */
103 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
104 MP4_Box_t *p_elst; /* elst (could be NULL) */
106 /* give the next sample to read, i_chunk is to find quickly where
107 the sample is located */
108 uint32_t i_sample; /* next sample to read */
109 uint32_t i_chunk; /* chunk where next sample is stored */
110 /* total count of chunk and sample */
111 uint32_t i_chunk_count;
112 uint32_t i_sample_count;
114 mp4_chunk_t *chunk; /* always defined for each chunk */
116 /* sample size, p_sample_size defined only if i_sample_size == 0
117 else i_sample_size is size for all sample */
118 uint32_t i_sample_size;
119 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
120 too much time to do sumations each time*/
122 MP4_Box_t *p_stbl; /* will contain all timing information */
123 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
124 MP4_Box_t *p_sample;/* point on actual sdsd */
134 MP4_Box_t *p_root; /* container for the whole file */
138 uint64_t i_time; /* time position of the presentation
139 * in movie timescale */
140 uint64_t i_timescale; /* movie time scale */
141 uint64_t i_duration; /* movie duration */
142 unsigned int i_tracks; /* number of tracks */
143 mp4_track_t *track; /* array of track */
147 /*****************************************************************************
148 * Declaration of local function
149 *****************************************************************************/
150 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *);
151 static void MP4_TrackDestroy( demux_t *, mp4_track_t * );
153 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
154 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
156 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
158 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
159 static int MP4_TrackSampleSize( mp4_track_t * );
160 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
161 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
163 /* Return time in µs of a track */
164 static inline int64_t MP4_TrackGetPTS( demux_t *p_demux, mp4_track_t *p_track )
166 unsigned int i_sample;
167 unsigned int i_index;
170 i_sample = p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first;
171 i_dts = p_track->chunk[p_track->i_chunk].i_first_dts;
173 while( i_sample > 0 )
175 if( i_sample > p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] )
177 i_dts += p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] *
178 p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
179 i_sample -= p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index];
185 p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
191 /* now handle elst */
192 if( p_track->p_elst )
194 demux_sys_t *p_sys = p_demux->p_sys;
195 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
197 /* convert to offset */
198 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
199 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
200 elst->i_media_time[p_track->i_elst] > 0 )
202 i_dts -= elst->i_media_time[p_track->i_elst];
205 /* add i_elst_time */
206 i_dts += p_track->i_elst_time * p_track->i_timescale / p_sys->i_timescale;
208 if( i_dts < 0 ) i_dts = 0;
211 return (int64_t)1000000 * i_dts / p_track->i_timescale;
213 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
215 return (int64_t)1000000 * p_sys->i_time / p_sys->i_timescale;
218 #define FREE( p ) if( p ) { free( p ); (p) = NULL;}
220 /*****************************************************************************
221 * Open: check file and initializes MP4 structures
222 *****************************************************************************/
223 static int Open( vlc_object_t * p_this )
225 demux_t *p_demux = (demux_t *)p_this;
236 vlc_bool_t b_seekable;
238 /* a little test to see if it could be a mp4 */
239 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
241 msg_Warn( p_demux, "MP4 plugin discarded (cannot peek)" );
244 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
255 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
258 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
263 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
266 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
270 /*Set exported functions */
271 p_demux->pf_demux = Demux;
272 p_demux->pf_control = Control;
274 /* create our structure that will contains all data */
275 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
276 memset( p_sys, 0, sizeof( demux_sys_t ) );
278 /* Now load all boxes ( except raw data ) */
279 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
281 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
285 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
287 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
289 switch( p_ftyp->data.p_ftyp->i_major_brand )
293 "ISO Media file (isom) version %d.",
294 p_ftyp->data.p_ftyp->i_minor_version );
298 "unrecognized major file specification (%4.4s).",
299 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
305 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
308 /* the file need to have one moov box */
309 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
311 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
315 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
318 /* we have a free box as a moov, rename it */
319 p_foov->i_type = FOURCC_moov;
322 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
324 playlist_t *p_playlist;
325 int i_count = MP4_BoxCount( p_rmra, "rmda" );
328 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
331 (playlist_t *)vlc_object_find( p_demux,
336 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
337 for( i = 0; i < i_count; i++ )
339 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
343 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
347 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
349 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
350 psz_ref, (char*)&i_ref_type );
352 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
354 if( strstr( psz_ref, "qt5gateQT" ) )
356 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
359 if( !strncmp( psz_ref, "http://", 7 ) ||
360 !strncmp( psz_ref, "rtsp://", 7 ) )
362 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
363 playlist_Add( p_playlist, psz_ref, psz_ref,
364 PLAYLIST_APPEND, PLAYLIST_END );
368 /* msg dbg relative ? */
369 char *psz_absolute = alloca( strlen( p_demux->psz_access ) + 3 + strlen( p_demux->psz_path ) + strlen( psz_ref ) + 1);
370 char *end = strrchr( p_demux->psz_path, '/' );
374 int i_len = end + 1 - p_demux->psz_path;
376 strcpy( psz_absolute, p_demux->psz_access );
377 strcat( psz_absolute, "://" );
378 strncat( psz_absolute, p_demux->psz_path, i_len);
382 strcpy( psz_absolute, "" );
384 strcat( psz_absolute, psz_ref );
385 msg_Dbg( p_demux, "adding ref = `%s'", psz_absolute );
386 playlist_Add( p_playlist, psz_absolute, psz_absolute,
387 PLAYLIST_APPEND, PLAYLIST_END );
392 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
393 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
396 vlc_object_release( p_playlist );
400 msg_Err( p_demux, "can't find playlist" );
404 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
408 msg_Err( p_demux, "cannot find /moov/mvhd" );
413 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
414 p_demux->pf_demux = DemuxRef;
420 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
421 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
424 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
426 msg_Err( p_demux, "cannot find any /moov/trak" );
429 msg_Dbg( p_demux, "find %d track%c",
431 p_sys->i_tracks ? 's':' ' );
433 /* allocate memory */
434 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
435 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
437 /* now process each track and extract all usefull information */
438 for( i = 0; i < p_sys->i_tracks; i++ )
440 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
441 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
443 if( p_sys->track[i].b_ok )
446 switch( p_sys->track[i].fmt.i_cat )
455 psz_cat = "subtitle";
463 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
464 p_sys->track[i].i_track_ID,
466 p_sys->track[i].b_enable ? "enable":"disable",
467 p_sys->track[i].fmt.psz_language ? p_sys->track[i].fmt.psz_language : "undef" );
471 msg_Dbg( p_demux, "ignoring track[Id 0x%x]", p_sys->track[i].i_track_ID );
480 MP4_BoxFree( p_demux->s, p_sys->p_root );
486 /*****************************************************************************
487 * Demux: read packet and send them to decoders
488 *****************************************************************************
489 * TODO check for newly selected track (ie audio upt to now )
490 *****************************************************************************/
491 static int Demux( demux_t *p_demux )
493 demux_sys_t *p_sys = p_demux->p_sys;
494 unsigned int i_track;
497 unsigned int i_track_selected;
499 /* check for newly selected/unselected track */
500 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
503 mp4_track_t *tk = &p_sys->track[i_track];
505 if( tk->b_selected && tk->i_sample >= tk->i_sample_count )
507 msg_Warn( p_demux, "track[0x%x] will be disabled", tk->i_track_ID );
508 MP4_TrackUnselect( p_demux, tk);
513 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
515 if( tk->b_selected && !b )
517 MP4_TrackUnselect( p_demux, tk );
519 else if( !tk->b_selected && b)
521 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
531 if( i_track_selected <= 0 )
533 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
534 if( p_sys->i_timescale > 0 )
536 int64_t i_length = (mtime_t)1000000 *
537 (mtime_t)p_sys->i_duration /
538 (mtime_t)p_sys->i_timescale;
539 if( MP4_GetMoviePTS( p_sys ) >= i_length )
544 msg_Warn( p_demux, "no track selected, exiting..." );
548 /* first wait for the good time to read a packet */
549 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
551 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
553 /* we will read 100ms for each stream so ...*/
554 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
556 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
558 mp4_track_t *tk = &p_sys->track[i_track];
560 if( !tk->b_ok || !tk->b_selected )
565 while( MP4_TrackGetPTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
568 msg_Dbg( p_demux, "tk=%lld mv=%lld",
569 MP4_TrackGetPTS( p_demux, tk ),
570 MP4_GetMoviePTS( p_sys ) );
573 if( MP4_TrackSampleSize( tk ) > 0 )
578 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
580 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
581 MP4_TrackUnselect( p_demux, tk );
586 if( ( p_block = stream_Block( p_demux->s,
587 MP4_TrackSampleSize( tk ) ) ) == NULL )
589 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
590 MP4_TrackUnselect( p_demux, tk );
594 if( tk->b_drms && tk->p_drms )
596 drms_decrypt( tk->p_drms,
597 (uint32_t*)p_block->p_buffer,
600 else if( tk->fmt.i_cat == SPU_ES )
602 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) && p_block->i_buffer >= 2 )
604 uint16_t i_size = GetWBE( p_block->p_buffer );
606 if( i_size + 2 <= p_block->i_buffer )
609 /* remove the length field, and append a '\0' */
610 memmove( &p_block->p_buffer[0], &p_block->p_buffer[2], i_size );
611 p_block->p_buffer[i_size] = '\0';
612 p_block->i_buffer = i_size + 1;
614 /* convert \r -> \n */
615 while( ( p = strchr( p_block->p_buffer, '\r' ) ) )
623 p_block->i_buffer = 0;
627 p_block->i_dts = MP4_TrackGetPTS( p_demux, tk ) + 1;
629 p_block->i_pts = tk->fmt.i_cat == VIDEO_ES ? 0 : p_block->i_dts + 1;
631 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
633 es_out_Send( p_demux->out, tk->p_es, p_block );
638 if( MP4_TrackNextSample( p_demux, tk ) )
647 /*****************************************************************************
648 * Seek: Got to i_date
649 ******************************************************************************/
650 static int Seek( demux_t *p_demux, mtime_t i_date )
652 demux_sys_t *p_sys = p_demux->p_sys;
653 unsigned int i_track;
655 /* First update update global time */
656 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
657 p_sys->i_pcr = i_date;
659 /* Now for each stream try to go to this time */
660 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
662 mp4_track_t *tk = &p_sys->track[i_track];
663 MP4_TrackSeek( p_demux, tk, i_date );
668 /*****************************************************************************
670 *****************************************************************************/
671 static int Control( demux_t *p_demux, int i_query, va_list args )
673 demux_sys_t *p_sys = p_demux->p_sys;
680 case DEMUX_GET_POSITION:
681 pf = (double*)va_arg( args, double * );
682 if( p_sys->i_duration > 0 )
684 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
692 case DEMUX_SET_POSITION:
693 f = (double)va_arg( args, double );
694 if( p_sys->i_timescale > 0 )
696 i64 = (int64_t)( f * (double)1000000 *
697 (double)p_sys->i_duration /
698 (double)p_sys->i_timescale );
699 return Seek( p_demux, i64 );
701 else return VLC_SUCCESS;
704 pi64 = (int64_t*)va_arg( args, int64_t * );
705 if( p_sys->i_timescale > 0 )
707 *pi64 = (mtime_t)1000000 *
708 (mtime_t)p_sys->i_time /
709 (mtime_t)p_sys->i_timescale;
715 i64 = (int64_t)va_arg( args, int64_t );
716 return Seek( p_demux, i64 );
718 case DEMUX_GET_LENGTH:
719 pi64 = (int64_t*)va_arg( args, int64_t * );
720 if( p_sys->i_timescale > 0 )
722 *pi64 = (mtime_t)1000000 *
723 (mtime_t)p_sys->i_duration /
724 (mtime_t)p_sys->i_timescale;
730 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
735 vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
737 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
738 MP4_Box_t *p_0xa9xxx;
743 *pp_meta = meta = vlc_meta_New();
744 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
745 p_0xa9xxx = p_0xa9xxx->p_next )
747 switch( p_0xa9xxx->i_type )
750 vlc_meta_Add( meta, VLC_META_TITLE,
751 p_0xa9xxx->data.p_0xa9xxx->psz_text );
754 vlc_meta_Add( meta, VLC_META_AUTHOR,
755 p_0xa9xxx->data.p_0xa9xxx->psz_text );
758 vlc_meta_Add( meta, VLC_META_ARTIST,
759 p_0xa9xxx->data.p_0xa9xxx->psz_text );
762 vlc_meta_Add( meta, VLC_META_COPYRIGHT,
763 p_0xa9xxx->data.p_0xa9xxx->psz_text );
766 vlc_meta_Add( meta, VLC_META_DATE,
767 p_0xa9xxx->data.p_0xa9xxx->psz_text );
770 vlc_meta_Add( meta, VLC_META_DESCRIPTION,
771 p_0xa9xxx->data.p_0xa9xxx->psz_text );
783 /* TODO one day, but they aren't really meaningfull */
793 case DEMUX_GET_TITLE_INFO:
794 case DEMUX_SET_NEXT_DEMUX_TIME:
795 case DEMUX_SET_GROUP:
799 msg_Warn( p_demux, "control query unimplemented !!!" );
804 /*****************************************************************************
805 * Close: frees unused data
806 *****************************************************************************/
807 static void Close ( vlc_object_t * p_this )
809 unsigned int i_track;
810 demux_t * p_demux = (demux_t *)p_this;
811 demux_sys_t *p_sys = p_demux->p_sys;
813 msg_Dbg( p_demux, "freeing all memory" );
815 MP4_BoxFree( p_demux->s, p_sys->p_root );
816 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
818 MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
820 FREE( p_sys->track );
827 /****************************************************************************
828 * Local functions, specific to vlc
829 ****************************************************************************/
831 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
832 static int TrackCreateChunksIndex( demux_t *p_demux,
833 mp4_track_t *p_demux_track )
835 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
838 unsigned int i_chunk;
839 unsigned int i_index, i_last;
841 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
842 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
843 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
845 return( VLC_EGENERIC );
848 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
849 if( !p_demux_track->i_chunk_count )
851 msg_Warn( p_demux, "no chunk defined" );
852 return( VLC_EGENERIC );
854 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
855 sizeof( mp4_chunk_t ) );
857 /* first we read chunk offset */
858 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
860 p_demux_track->chunk[i_chunk].i_offset =
861 p_co64->data.p_co64->i_chunk_offset[i_chunk];
864 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
865 to be used for the sample XXX begin to 1
866 We construct it begining at the end */
867 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
868 i_index = p_stsc->data.p_stsc->i_entry_count;
871 msg_Warn( p_demux, "cannot read chunk table or table empty" );
872 return( VLC_EGENERIC );
878 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
879 i_chunk < i_last; i_chunk++ )
881 p_demux_track->chunk[i_chunk].i_sample_description_index =
882 p_stsc->data.p_stsc->i_sample_description_index[i_index];
883 p_demux_track->chunk[i_chunk].i_sample_count =
884 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
886 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
889 p_demux_track->chunk[0].i_sample_first = 0;
890 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
892 p_demux_track->chunk[i_chunk].i_sample_first =
893 p_demux_track->chunk[i_chunk-1].i_sample_first +
894 p_demux_track->chunk[i_chunk-1].i_sample_count;
898 "track[Id 0x%x] read %d chunk",
899 p_demux_track->i_track_ID,
900 p_demux_track->i_chunk_count );
902 return( VLC_SUCCESS );
904 static int TrackCreateSamplesIndex( demux_t *p_demux,
905 mp4_track_t *p_demux_track )
907 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
908 ctts make same mapping but for composition time,
909 not yet used and probably not usefull */
910 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
911 that uses a compressed form FIXME make them in libmp4
913 /* TODO use also stss and stsh table for seeking */
914 /* FIXME use edit table */
919 int64_t i_index_sample_used;
923 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
924 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
926 if( ( !p_stts )||( !p_stsz ) )
928 msg_Warn( p_demux, "cannot read sample table" );
929 return( VLC_EGENERIC );
932 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
935 /* for sample size, there are 2 case */
936 if( p_stsz->data.p_stsz->i_sample_size )
938 /* 1: all sample have the same size, so no need to construct a table */
939 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
940 p_demux_track->p_sample_size = NULL;
944 /* 2: each sample can have a different size */
945 p_demux_track->i_sample_size = 0;
946 p_demux_track->p_sample_size =
947 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
949 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
951 p_demux_track->p_sample_size[i_sample] =
952 p_stsz->data.p_stsz->i_entry_size[i_sample];
955 /* we have extract all information from stsz,
958 /* if we don't want to waste too much memory, we can't expand
959 the box !, so each chunk will contain an "extract" of this table
963 i_index = 0; i_index_sample_used =0;
965 /* create and init last data for each chunk */
966 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
969 int64_t i_entry, i_sample_count, i;
971 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
972 /* count how many entries needed for this chunk
973 for p_sample_delta_dts and p_sample_count_dts */
975 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
978 while( i_sample_count > 0 )
980 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
983 i_sample_count += i_index_sample_used; /* don't count already used sample
990 p_demux_track->chunk[i_chunk].p_sample_count_dts =
991 calloc( i_entry, sizeof( uint32_t ) );
992 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
993 calloc( i_entry, sizeof( uint32_t ) );
996 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
997 for( i = 0; i < i_entry; i++ )
1002 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
1004 i_used = __MIN( i_rest, i_sample_count );
1006 i_index_sample_used += i_used;
1007 i_sample_count -= i_used;
1009 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
1011 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
1012 p_stts->data.p_stts->i_sample_delta[i_index];
1014 i_last_dts += i_used *
1015 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
1017 if( i_index_sample_used >=
1018 p_stts->data.p_stts->i_sample_count[i_index] )
1022 i_index_sample_used = 0;
1028 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:"I64Fd"s",
1029 p_demux_track->i_track_ID,
1030 p_demux_track->i_sample_count,
1031 i_last_dts / p_demux_track->i_timescale );
1033 return( VLC_SUCCESS );
1038 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1040 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1041 unsigned int i_chunk, es_out_id_t **pp_es )
1043 MP4_Box_t *p_sample;
1048 if( !p_track->chunk[i_chunk].i_sample_description_index )
1051 "invalid SampleEntry index (track[Id 0x%x])",
1052 p_track->i_track_ID );
1053 return VLC_EGENERIC;
1056 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1057 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1059 if( !p_sample || ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1062 "cannot find SampleEntry (track[Id 0x%x])",
1063 p_track->i_track_ID );
1064 return( VLC_EGENERIC );
1067 p_track->p_sample = p_sample;
1069 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1071 MP4_Box_data_sample_soun_t *p_soun;
1073 p_soun = p_sample->data.p_sample_soun;
1075 if( p_soun->i_qt_version == 0 )
1077 switch( p_sample->i_type )
1079 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1080 p_soun->i_qt_version = 1;
1081 p_soun->i_sample_per_packet = 64;
1082 p_soun->i_bytes_per_packet = 34;
1083 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1084 p_soun->i_bytes_per_sample = 2;
1086 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1087 p_soun->i_qt_version = 1;
1088 p_soun->i_sample_per_packet = 6;
1089 p_soun->i_bytes_per_packet = 2;
1090 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1091 p_soun->i_bytes_per_sample = 2;
1093 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1094 p_soun->i_qt_version = 1;
1095 p_soun->i_sample_per_packet = 12;
1096 p_soun->i_bytes_per_packet = 2;
1097 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1098 p_soun->i_bytes_per_sample = 2;
1100 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1101 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1102 p_soun->i_samplesize = 8;
1109 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1111 p_soun->i_qt_version = 0;
1116 /* It's a little ugly but .. there are special cases */
1117 switch( p_sample->i_type )
1119 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1120 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1121 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1123 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1124 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1126 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1127 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1130 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1131 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1132 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1133 /* FIXME UTF-8 doesn't work here ? */
1134 /* p_track->fmt.subs.psz_encoding = strdup( "UTF-8" ); */
1138 p_track->fmt.i_codec = p_sample->i_type;
1142 /* now see if esds is present and if so create a data packet
1143 with decoder_specific_info */
1144 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1145 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1146 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1147 ( p_esds->data.p_esds )&&
1150 /* First update information based on i_objectTypeIndication */
1151 switch( p_decconfig->i_objectTypeIndication )
1153 case( 0x20 ): /* MPEG4 VIDEO */
1154 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1157 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1164 case( 0x65): /* MPEG2 video */
1165 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1167 /* Theses are MPEG2-AAC */
1168 case( 0x66): /* main profile */
1169 case( 0x67): /* Low complexity profile */
1170 case( 0x68): /* Scaleable Sampling rate profile */
1171 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1173 /* true MPEG 2 audio */
1175 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1177 case( 0x6a): /* MPEG1 video */
1178 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1180 case( 0x6b): /* MPEG1 audio */
1181 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1183 case( 0x6c ): /* jpeg */
1184 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1188 case( 0xe0 ): /* NeroDigital: dvd subs */
1189 if( p_track->fmt.i_cat == SPU_ES )
1191 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1196 /* Unknown entry, but don't touch i_fourcc */
1198 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1199 p_decconfig->i_objectTypeIndication,
1200 p_track->i_track_ID );
1203 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1204 if( p_track->fmt.i_extra > 0 )
1206 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1207 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1208 p_track->fmt.i_extra );
1213 switch( p_sample->i_type )
1215 /* qt decoder, send the complete chunk */
1216 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1217 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1218 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1219 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1220 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1221 p_track->fmt.i_extra =
1222 p_sample->data.p_sample_vide->i_qt_image_description;
1223 if( p_track->fmt.i_extra > 0 )
1225 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1226 memcpy( p_track->fmt.p_extra,
1227 p_sample->data.p_sample_vide->p_qt_image_description,
1228 p_track->fmt.i_extra);
1231 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1232 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1233 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1234 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1235 p_track->fmt.i_extra =
1236 p_sample->data.p_sample_soun->i_qt_description;
1237 if( p_track->fmt.i_extra > 0 )
1239 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1240 memcpy( p_track->fmt.p_extra,
1241 p_sample->data.p_sample_soun->p_qt_description,
1242 p_track->fmt.i_extra);
1246 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1247 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1249 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1253 /* Hack: use a packetizer to reecampsulate data in anexe B format */
1254 msg_Dbg( p_demux, "avcC: size=%d", p_avcC->data.p_avcC->i_avcC );
1255 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1256 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1257 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC, p_track->fmt.i_extra );
1258 p_track->fmt.b_packetized = VLC_FALSE;
1262 msg_Err( p_demux, "missing avcC" );
1274 /* some last initialisation */
1275 switch( p_track->fmt.i_cat )
1278 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1279 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1281 /* fall on display size */
1282 if( p_track->fmt.video.i_width <= 0 )
1283 p_track->fmt.video.i_width = p_track->i_width;
1284 if( p_track->fmt.video.i_height <= 0 )
1285 p_track->fmt.video.i_height = p_track->i_height;
1287 /* Find out apect ratio from display size */
1288 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1289 /* Work-around buggy muxed files */
1290 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1291 p_track->fmt.video.i_aspect =
1292 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1297 p_track->fmt.audio.i_channels =
1298 p_sample->data.p_sample_soun->i_channelcount;
1299 p_track->fmt.audio.i_rate =
1300 p_sample->data.p_sample_soun->i_sampleratehi;
1301 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1302 p_sample->data.p_sample_soun->i_sampleratehi *
1303 p_sample->data.p_sample_soun->i_samplesize;
1304 p_track->fmt.audio.i_bitspersample =
1305 p_sample->data.p_sample_soun->i_samplesize;
1312 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1317 /* given a time it return sample/chunk
1318 * it also update elst field of the track
1320 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1321 int64_t i_start, uint32_t *pi_chunk,
1322 uint32_t *pi_sample )
1324 demux_sys_t *p_sys = p_demux->p_sys;
1327 unsigned int i_sample;
1328 unsigned int i_chunk;
1331 /* FIXME see if it's needed to check p_track->i_chunk_count */
1332 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1334 return( VLC_EGENERIC );
1337 /* handle elst (find the correct one) */
1338 MP4_TrackSetELST( p_demux, p_track, i_start );
1339 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1341 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1342 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1344 /* now calculate i_start for this elst */
1346 i_start -= p_track->i_elst_time * I64C(1000000) / p_sys->i_timescale;
1354 /* to track time scale */
1355 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1356 /* add elst offset */
1357 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1358 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1359 elst->i_media_time[p_track->i_elst] > 0 )
1361 i_start += elst->i_media_time[p_track->i_elst];
1364 msg_Dbg( p_demux, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd
1365 "ms (track)", p_track->i_elst,
1366 i_mvt * 1000 / p_sys->i_timescale,
1367 i_start * 1000 / p_track->i_timescale );
1371 /* convert absolute time to in timescale unit */
1372 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1375 /* we start from sample 0/chunk 0, hope it won't take too much time */
1376 /* *** find good chunk *** */
1377 for( i_chunk = 0; ; i_chunk++ )
1379 if( i_chunk + 1 >= p_track->i_chunk_count )
1381 /* at the end and can't check if i_start in this chunk,
1382 it will be check while searching i_sample */
1383 i_chunk = p_track->i_chunk_count - 1;
1387 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1388 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1394 /* *** find sample in the chunk *** */
1395 i_sample = p_track->chunk[i_chunk].i_sample_first;
1396 i_dts = p_track->chunk[i_chunk].i_first_dts;
1397 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1400 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1401 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1404 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1405 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1407 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1412 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1416 i_sample += ( i_start - i_dts ) /
1417 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1422 if( i_sample >= p_track->i_sample_count )
1424 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1425 "(seeking too far) chunk=%d sample=%d",
1426 p_track->i_track_ID, i_chunk, i_sample );
1427 return( VLC_EGENERIC );
1431 /* *** Try to find nearest sync points *** */
1432 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1434 unsigned int i_index;
1436 "track[Id 0x%x] using Sync Sample Box (stss)",
1437 p_track->i_track_ID );
1438 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1440 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1444 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1446 p_stss->data.p_stss->i_sample_number[i_index-1] );
1447 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1448 /* new i_sample is less than old so i_chunk can only decreased */
1449 while( i_chunk > 0 &&
1450 i_sample < p_track->chunk[i_chunk].i_sample_first )
1457 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1459 p_stss->data.p_stss->i_sample_number[i_index] );
1460 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1461 /* new i_sample is more than old so i_chunk can only increased */
1462 while( i_chunk < p_track->i_chunk_count - 1 &&
1463 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1464 p_track->chunk[i_chunk].i_sample_count )
1475 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1476 "Sample Box (stss)", p_track->i_track_ID );
1479 *pi_chunk = i_chunk;
1480 *pi_sample = i_sample;
1485 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1486 unsigned int i_chunk, unsigned int i_sample )
1488 vlc_bool_t b_reselect = VLC_FALSE;
1490 /* now see if actual es is ok */
1491 if( p_track->i_chunk < 0 ||
1492 p_track->i_chunk >= p_track->i_chunk_count - 1 ||
1493 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1494 p_track->chunk[i_chunk].i_sample_description_index )
1496 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1497 p_track->i_track_ID );
1499 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1500 p_track->p_es, &b_reselect );
1502 es_out_Del( p_demux->out, p_track->p_es );
1504 p_track->p_es = NULL;
1506 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1508 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1509 p_track->i_track_ID );
1511 p_track->b_ok = VLC_FALSE;
1512 p_track->b_selected = VLC_FALSE;
1513 return VLC_EGENERIC;
1517 /* select again the new decoder */
1520 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1523 p_track->i_chunk = i_chunk;
1524 p_track->i_sample = i_sample;
1526 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1529 /****************************************************************************
1531 ****************************************************************************
1532 * Parse track information and create all needed data to run a track
1533 * If it succeed b_ok is set to 1 else to 0
1534 ****************************************************************************/
1535 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1536 MP4_Box_t *p_box_trak )
1538 demux_sys_t *p_sys = p_demux->p_sys;
1540 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1541 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1555 /* hint track unsuported */
1557 /* set default value (-> track unusable) */
1558 p_track->b_ok = VLC_FALSE;
1559 p_track->b_enable = VLC_FALSE;
1560 p_track->b_selected = VLC_FALSE;
1562 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1569 /* do we launch this track by default ? */
1571 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1573 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1574 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1575 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1579 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
1582 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1583 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1585 if( ( !p_mdhd )||( !p_hdlr ) )
1590 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1592 for( i = 0; i < 3; i++ )
1594 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1598 switch( p_hdlr->data.p_hdlr->i_handler_type )
1600 case( FOURCC_soun ):
1601 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1605 p_track->fmt.i_cat = AUDIO_ES;
1608 case( FOURCC_vide ):
1609 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1613 p_track->fmt.i_cat = VIDEO_ES;
1616 case( FOURCC_text ):
1617 case( FOURCC_subp ):
1618 p_track->fmt.i_cat = SPU_ES;
1625 p_track->i_elst = 0;
1626 p_track->i_elst_time = 0;
1627 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1629 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1632 msg_Warn( p_demux, "elst box found" );
1633 for( i = 0; i < elst->i_entry_count; i++ )
1635 msg_Dbg( p_demux, " - [%d] duration="I64Fd"ms media time="I64Fd"ms) rate=%d.%d",
1637 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1638 elst->i_media_time[i] >= 0 ?
1639 elst->i_media_time[i] * 1000 / p_track->i_timescale : -1,
1640 elst->i_media_rate_integer[i],
1641 elst->i_media_rate_fraction[i] );
1648 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1650 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1651 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1656 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1657 p_track->b_drms = p_drms != NULL;
1658 p_track->p_drms = p_track->b_drms ?
1659 p_drms->data.p_sample_soun->p_drms : NULL;
1662 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1664 p_track->fmt.psz_language = strdup( language );
1667 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1668 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1670 MP4_Box_t *p_sample;
1672 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1673 if( p_sample && p_sample->data.p_sample_soun)
1675 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1676 if( p_soun->i_qt_version == 0 &&
1677 p_track->i_timescale != p_soun->i_sampleratehi )
1680 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1682 "Making both equal. (report any problem)",
1683 p_track->i_timescale, p_soun->i_sampleratehi );
1685 if( p_soun->i_sampleratehi )
1686 p_track->i_timescale = p_soun->i_sampleratehi;
1688 p_soun->i_sampleratehi = p_track->i_timescale;
1693 /* Create chunk index table and sample index table */
1694 if( TrackCreateChunksIndex( p_demux,p_track ) ||
1695 TrackCreateSamplesIndex( p_demux, p_track ) )
1697 return; /* cannot create chunks index */
1700 p_track->i_chunk = 0;
1701 p_track->i_sample = 0;
1704 if( TrackCreateES( p_demux,
1705 p_track, p_track->i_chunk,
1708 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1709 p_track->i_track_ID );
1716 for( i = 0; i < p_track->i_chunk_count; i++ )
1718 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
1719 i, p_track->chunk[i].i_sample_count,
1720 p_track->chunk[i].i_first_dts );
1725 p_track->b_ok = VLC_TRUE;
1728 /****************************************************************************
1730 ****************************************************************************
1731 * Destroy a track created by MP4_TrackCreate.
1732 ****************************************************************************/
1733 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
1735 unsigned int i_chunk;
1737 p_track->b_ok = VLC_FALSE;
1738 p_track->b_enable = VLC_FALSE;
1739 p_track->b_selected = VLC_FALSE;
1741 es_format_Clean( &p_track->fmt );
1743 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1745 if( p_track->chunk )
1747 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1748 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1751 FREE( p_track->chunk );
1753 if( !p_track->i_sample_size )
1755 FREE( p_track->p_sample_size );
1759 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
1762 if( !p_track->b_ok )
1764 return VLC_EGENERIC;
1767 if( p_track->b_selected )
1769 msg_Warn( p_demux, "track[Id 0x%x] already selected",
1770 p_track->i_track_ID );
1774 return MP4_TrackSeek( p_demux, p_track, i_start );
1777 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
1779 if( !p_track->b_ok )
1784 if( !p_track->b_selected )
1786 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
1787 p_track->i_track_ID );
1792 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
1793 p_track->p_es, VLC_FALSE );
1796 p_track->b_selected = VLC_FALSE;
1799 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
1805 if( !p_track->b_ok )
1807 return( VLC_EGENERIC );
1810 p_track->b_selected = VLC_FALSE;
1812 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
1813 &i_chunk, &i_sample ) )
1815 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
1816 p_track->i_track_ID );
1817 return( VLC_EGENERIC );
1820 p_track->b_selected = VLC_TRUE;
1822 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
1825 p_track->b_selected = VLC_TRUE;
1827 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1832 * 3 types: for audio
1835 #define QT_V0_MAX_SAMPLES 1500
1836 static int MP4_TrackSampleSize( mp4_track_t *p_track )
1839 MP4_Box_data_sample_soun_t *p_soun;
1841 if( p_track->i_sample_size == 0 )
1843 /* most simple case */
1844 return( p_track->p_sample_size[p_track->i_sample] );
1846 if( p_track->fmt.i_cat != AUDIO_ES )
1848 return( p_track->i_sample_size );
1851 if( p_track->i_sample_size != 1 )
1853 //msg_Warn( p_demux, "SampleSize != 1" );
1854 return( p_track->i_sample_size );
1857 p_soun = p_track->p_sample->data.p_sample_soun;
1859 if( p_soun->i_qt_version == 1 )
1861 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1866 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1867 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1868 if( i_samples > QT_V0_MAX_SAMPLES )
1870 i_samples = QT_V0_MAX_SAMPLES;
1872 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1875 //fprintf( stderr, "size=%d\n", i_size );
1880 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
1882 unsigned int i_sample;
1886 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1888 if( p_track->i_sample_size )
1890 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1892 if( p_soun->i_qt_version == 0 )
1894 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1895 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1899 /* we read chunk by chunk */
1905 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1906 i_sample < p_track->i_sample; i_sample++ )
1908 i_pos += p_track->p_sample_size[i_sample];
1915 static int MP4_TrackNextSample( demux_t *p_demux,
1916 mp4_track_t *p_track )
1919 if( p_track->fmt.i_cat == AUDIO_ES &&
1920 p_track->i_sample_size != 0 )
1922 MP4_Box_data_sample_soun_t *p_soun;
1924 p_soun = p_track->p_sample->data.p_sample_soun;
1926 if( p_soun->i_qt_version == 1 )
1928 /* chunk by chunk */
1930 p_track->chunk[p_track->i_chunk].i_sample_first +
1931 p_track->chunk[p_track->i_chunk].i_sample_count;
1936 p_track->i_sample += QT_V0_MAX_SAMPLES;
1937 if( p_track->i_sample > p_track->chunk[p_track->i_chunk].i_sample_first + p_track->chunk[p_track->i_chunk].i_sample_count )
1940 p_track->chunk[p_track->i_chunk].i_sample_first +
1941 p_track->chunk[p_track->i_chunk].i_sample_count;
1947 p_track->i_sample++;
1950 if( p_track->i_sample >= p_track->i_sample_count )
1952 /* we have reach end of the track so free decoder stuff */
1953 msg_Warn( p_demux, "track[0x%x] will be disabled", p_track->i_track_ID );
1954 MP4_TrackUnselect( p_demux, p_track );
1955 return VLC_EGENERIC;
1958 /* Have we changed chunk ? */
1959 if( p_track->i_sample >=
1960 p_track->chunk[p_track->i_chunk].i_sample_first +
1961 p_track->chunk[p_track->i_chunk].i_sample_count )
1963 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
1964 p_track->i_sample ) )
1966 msg_Warn( p_demux, "track[0x%x] will be disabled "
1967 "(cannot restart decoder)", p_track->i_track_ID );
1968 MP4_TrackUnselect( p_demux, p_track );
1969 return VLC_EGENERIC;
1973 /* Have we changed elst */
1974 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1976 demux_sys_t *p_sys = p_demux->p_sys;
1977 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1978 int64_t i_mvt = MP4_TrackGetPTS( p_demux, p_track ) *
1979 p_sys->i_timescale / (int64_t)1000000;
1981 if( p_track->i_elst < elst->i_entry_count &&
1982 i_mvt >= p_track->i_elst_time +
1983 elst->i_segment_duration[p_track->i_elst] )
1985 MP4_TrackSetELST( p_demux, p_track,
1986 MP4_TrackGetPTS( p_demux, p_track ) );
1993 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
1996 demux_sys_t *p_sys = p_demux->p_sys;
1997 int i_elst_last = tk->i_elst;
1999 /* handle elst (find the correct one) */
2001 tk->i_elst_time = 0;
2002 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2004 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2005 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2007 for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2009 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2011 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2015 tk->i_elst_time += i_dur;
2018 if( tk->i_elst >= elst->i_entry_count )
2020 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2021 tk->i_elst = elst->i_entry_count - 1;
2022 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2025 if( elst->i_media_time[tk->i_elst] < 0 )
2028 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2031 if( i_elst_last != tk->i_elst )
2033 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );