1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
39 #define uint64_t int64_t
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
49 set_category( CAT_INPUT );
50 set_subcategory( SUBCAT_INPUT_DEMUX );
51 set_description( _("MP4 stream demuxer") );
52 set_capability( "demux2", 242 );
53 set_callbacks( Open, Close );
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Demux ( demux_t * );
60 static int DemuxRef( demux_t *p_demux ){ return 0;}
61 static int Seek ( demux_t *, mtime_t );
62 static int Control ( demux_t *, int, va_list );
64 /* Contain all information about a chunk */
67 uint64_t i_offset; /* absolute position of this chunk in the file */
68 uint32_t i_sample_description_index; /* index for SampleEntry to use */
69 uint32_t i_sample_count; /* how many samples in this chunk */
70 uint32_t i_sample_first; /* index of the first sample in this chunk */
72 /* now provide way to calculate pts, dts, and offset without to
73 much memory and with fast acces */
75 /* with this we can calculate dts/pts without waste memory */
77 uint32_t *p_sample_count_dts;
78 uint32_t *p_sample_delta_dts; /* dts delta */
80 uint32_t *p_sample_count_pts;
81 int32_t *p_sample_offset_pts; /* pts-dts */
83 /* TODO if needed add pts
84 but quickly *add* support for edts and seeking */
88 /* Contain all needed information for read all track with vlc */
91 int i_track_ID; /* this should be unique */
93 int b_ok; /* The track is usable */
94 int b_enable; /* is the trak enable by default */
95 vlc_bool_t b_selected; /* is the trak being played */
100 /* display size only ! */
104 /* more internal data */
105 uint64_t i_timescale; /* time scale for this track only */
108 int i_elst; /* current elst */
109 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
110 MP4_Box_t *p_elst; /* elst (could be NULL) */
112 /* give the next sample to read, i_chunk is to find quickly where
113 the sample is located */
114 uint32_t i_sample; /* next sample to read */
115 uint32_t i_chunk; /* chunk where next sample is stored */
116 /* total count of chunk and sample */
117 uint32_t i_chunk_count;
118 uint32_t i_sample_count;
120 mp4_chunk_t *chunk; /* always defined for each chunk */
122 /* sample size, p_sample_size defined only if i_sample_size == 0
123 else i_sample_size is size for all sample */
124 uint32_t i_sample_size;
125 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
126 too much time to do sumations each time*/
128 MP4_Box_t *p_stbl; /* will contain all timing information */
129 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
130 MP4_Box_t *p_sample;/* point on actual sdsd */
140 MP4_Box_t *p_root; /* container for the whole file */
144 uint64_t i_time; /* time position of the presentation
145 * in movie timescale */
146 uint64_t i_timescale; /* movie time scale */
147 uint64_t i_duration; /* movie duration */
148 unsigned int i_tracks; /* number of tracks */
149 mp4_track_t *track; /* array of track */
152 /*****************************************************************************
153 * Declaration of local function
154 *****************************************************************************/
155 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *);
156 static void MP4_TrackDestroy( demux_t *, mp4_track_t * );
158 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
159 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
161 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
163 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
164 static int MP4_TrackSampleSize( mp4_track_t * );
165 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
166 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
168 /* Return time in s of a track */
169 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
171 #define chunk p_track->chunk[p_track->i_chunk]
173 unsigned int i_index = 0;
174 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
175 int64_t i_dts = chunk.i_first_dts;
177 while( i_sample > 0 )
179 if( i_sample > chunk.p_sample_count_dts[i_index] )
181 i_dts += chunk.p_sample_count_dts[i_index] *
182 chunk.p_sample_delta_dts[i_index];
183 i_sample -= chunk.p_sample_count_dts[i_index];
188 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
196 /* now handle elst */
197 if( p_track->p_elst )
199 demux_sys_t *p_sys = p_demux->p_sys;
200 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
202 /* convert to offset */
203 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
204 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
205 elst->i_media_time[p_track->i_elst] > 0 )
207 i_dts -= elst->i_media_time[p_track->i_elst];
210 /* add i_elst_time */
211 i_dts += p_track->i_elst_time * p_track->i_timescale /
214 if( i_dts < 0 ) i_dts = 0;
217 return I64C(1000000) * i_dts / p_track->i_timescale;
220 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
222 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
223 unsigned int i_index = 0;
224 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
226 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
229 for( i_index = 0;; i_index++ )
231 if( i_sample < ck->p_sample_count_pts[i_index] )
232 return ck->p_sample_offset_pts[i_index] * I64C(1000000) /
233 (int64_t)p_track->i_timescale;
235 i_sample -= ck->p_sample_count_pts[i_index];
239 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
241 return I64C(1000000) * p_sys->i_time / p_sys->i_timescale;
244 #define FREE( p ) if( p ) { free( p ); (p) = NULL;}
246 /*****************************************************************************
247 * Open: check file and initializes MP4 structures
248 *****************************************************************************/
249 static int Open( vlc_object_t * p_this )
251 demux_t *p_demux = (demux_t *)p_this;
262 vlc_bool_t b_seekable;
264 /* A little test to see if it could be a mp4 */
265 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
267 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
278 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
285 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
288 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
292 /*Set exported functions */
293 p_demux->pf_demux = Demux;
294 p_demux->pf_control = Control;
296 /* create our structure that will contains all data */
297 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
298 memset( p_sys, 0, sizeof( demux_sys_t ) );
300 /* Now load all boxes ( except raw data ) */
301 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
303 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
307 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
309 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
311 switch( p_ftyp->data.p_ftyp->i_major_brand )
315 "ISO Media file (isom) version %d.",
316 p_ftyp->data.p_ftyp->i_minor_version );
320 "unrecognized major file specification (%4.4s).",
321 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
327 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
330 /* the file need to have one moov box */
331 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
333 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
337 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
340 /* we have a free box as a moov, rename it */
341 p_foov->i_type = FOURCC_moov;
344 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
346 playlist_t *p_playlist;
347 playlist_item_t *p_item;
348 int i_count = MP4_BoxCount( p_rmra, "rmda" );
350 vlc_bool_t b_play = VLC_FALSE;
352 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
355 (playlist_t *)vlc_object_find( p_demux,
360 p_item = playlist_LockItemGetByInput( p_playlist,
361 ((input_thread_t *)p_demux->p_parent)->input.p_item );
362 playlist_ItemToNode( p_playlist, p_item );
364 for( i = 0; i < i_count; i++ )
366 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
370 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
374 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
376 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
377 psz_ref, (char*)&i_ref_type );
379 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
381 if( strstr( psz_ref, "qt5gateQT" ) )
383 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
386 if( !strncmp( psz_ref, "http://", 7 ) ||
387 !strncmp( psz_ref, "rtsp://", 7 ) )
389 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
392 playlist_item_t *p_child =
393 playlist_ItemNew( p_playlist,
397 playlist_NodeAddItem( p_playlist, p_child,
398 p_item->pp_parents[0]->i_view,
399 p_item, PLAYLIST_APPEND,
401 playlist_CopyParents( p_item, p_child );
408 /* msg dbg relative ? */
409 int i_path_size = strlen( p_demux->psz_access ) + 3 +
410 strlen( p_demux->psz_path ) + strlen( psz_ref ) + 1;
412 char *psz_absolute = alloca( i_path_size );
414 char *psz_absolute = (char *)malloc( i_path_size );
416 char *end = strrchr( p_demux->psz_path, '/' );
420 int i_len = end + 1 - p_demux->psz_path;
422 strcpy( psz_absolute, p_demux->psz_access );
423 strcat( psz_absolute, "://" );
424 strncat( psz_absolute, p_demux->psz_path, i_len);
428 strcpy( psz_absolute, "" );
430 strcat( psz_absolute, psz_ref );
431 msg_Dbg( p_demux, "adding ref = `%s'", psz_absolute );
434 playlist_item_t *p_child =
435 playlist_ItemNew( p_playlist,
440 playlist_NodeAddItem( p_playlist, p_child,
441 p_item->pp_parents[0]->i_view,
442 p_item, PLAYLIST_APPEND,
444 playlist_CopyParents( p_item, p_child );
449 free( psz_absolute );
455 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
456 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
459 if( b_play == VLC_TRUE )
461 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
462 p_playlist->status.i_view,
463 p_playlist->status.p_item, NULL );
465 vlc_object_release( p_playlist );
469 msg_Err( p_demux, "can't find playlist" );
473 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
477 msg_Err( p_demux, "cannot find /moov/mvhd" );
482 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
483 p_demux->pf_demux = DemuxRef;
489 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
490 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
493 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
495 msg_Err( p_demux, "cannot find any /moov/trak" );
498 msg_Dbg( p_demux, "find %d track%c",
500 p_sys->i_tracks ? 's':' ' );
502 /* allocate memory */
503 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
504 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
506 /* now process each track and extract all usefull information */
507 for( i = 0; i < p_sys->i_tracks; i++ )
509 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
510 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
512 if( p_sys->track[i].b_ok )
515 switch( p_sys->track[i].fmt.i_cat )
524 psz_cat = "subtitle";
532 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
533 p_sys->track[i].i_track_ID, psz_cat,
534 p_sys->track[i].b_enable ? "enable":"disable",
535 p_sys->track[i].fmt.psz_language ?
536 p_sys->track[i].fmt.psz_language : "undef" );
540 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
541 p_sys->track[i].i_track_ID );
550 MP4_BoxFree( p_demux->s, p_sys->p_root );
556 /*****************************************************************************
557 * Demux: read packet and send them to decoders
558 *****************************************************************************
559 * TODO check for newly selected track (ie audio upt to now )
560 *****************************************************************************/
561 static int Demux( demux_t *p_demux )
563 demux_sys_t *p_sys = p_demux->p_sys;
564 unsigned int i_track;
567 unsigned int i_track_selected;
569 /* check for newly selected/unselected track */
570 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
573 mp4_track_t *tk = &p_sys->track[i_track];
577 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
582 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
584 if( tk->b_selected && !b )
586 MP4_TrackUnselect( p_demux, tk );
588 else if( !tk->b_selected && b)
590 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
599 if( i_track_selected <= 0 )
601 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
602 if( p_sys->i_timescale > 0 )
604 int64_t i_length = (mtime_t)1000000 *
605 (mtime_t)p_sys->i_duration /
606 (mtime_t)p_sys->i_timescale;
607 if( MP4_GetMoviePTS( p_sys ) >= i_length )
612 msg_Warn( p_demux, "no track selected, exiting..." );
616 /* first wait for the good time to read a packet */
617 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
619 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
621 /* we will read 100ms for each stream so ...*/
622 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
624 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
626 mp4_track_t *tk = &p_sys->track[i_track];
628 if( !tk->b_ok || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
633 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
636 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
637 MP4_TrackGetDTS( p_demux, tk ),
638 MP4_GetMoviePTS( p_sys ) );
641 if( MP4_TrackSampleSize( tk ) > 0 )
647 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
649 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
651 MP4_TrackUnselect( p_demux, tk );
657 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
659 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
661 MP4_TrackUnselect( p_demux, tk );
665 if( tk->b_drms && tk->p_drms )
667 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
670 else if( tk->fmt.i_cat == SPU_ES )
672 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
673 p_block->i_buffer >= 2 )
675 uint16_t i_size = GetWBE( p_block->p_buffer );
677 if( i_size + 2 <= p_block->i_buffer )
680 /* remove the length field, and append a '\0' */
681 memmove( &p_block->p_buffer[0],
682 &p_block->p_buffer[2], i_size );
683 p_block->p_buffer[i_size] = '\0';
684 p_block->i_buffer = i_size + 1;
686 /* convert \r -> \n */
687 while( ( p = strchr( p_block->p_buffer, '\r' ) ) )
695 p_block->i_buffer = 0;
700 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
702 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
704 p_block->i_pts = p_block->i_dts + i_delta;
705 else if( tk->fmt.i_cat != VIDEO_ES )
706 p_block->i_pts = p_block->i_dts;
710 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
711 es_out_Send( p_demux->out, tk->p_es, p_block );
715 if( MP4_TrackNextSample( p_demux, tk ) )
724 /*****************************************************************************
725 * Seek: Got to i_date
726 ******************************************************************************/
727 static int Seek( demux_t *p_demux, mtime_t i_date )
729 demux_sys_t *p_sys = p_demux->p_sys;
730 unsigned int i_track;
732 /* First update update global time */
733 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
734 p_sys->i_pcr = i_date;
736 /* Now for each stream try to go to this time */
737 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
739 mp4_track_t *tk = &p_sys->track[i_track];
740 MP4_TrackSeek( p_demux, tk, i_date );
745 /*****************************************************************************
747 *****************************************************************************/
748 static int Control( demux_t *p_demux, int i_query, va_list args )
750 demux_sys_t *p_sys = p_demux->p_sys;
757 case DEMUX_GET_POSITION:
758 pf = (double*)va_arg( args, double * );
759 if( p_sys->i_duration > 0 )
761 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
769 case DEMUX_SET_POSITION:
770 f = (double)va_arg( args, double );
771 if( p_sys->i_timescale > 0 )
773 i64 = (int64_t)( f * (double)1000000 *
774 (double)p_sys->i_duration /
775 (double)p_sys->i_timescale );
776 return Seek( p_demux, i64 );
778 else return VLC_SUCCESS;
781 pi64 = (int64_t*)va_arg( args, int64_t * );
782 if( p_sys->i_timescale > 0 )
784 *pi64 = (mtime_t)1000000 *
785 (mtime_t)p_sys->i_time /
786 (mtime_t)p_sys->i_timescale;
792 i64 = (int64_t)va_arg( args, int64_t );
793 return Seek( p_demux, i64 );
795 case DEMUX_GET_LENGTH:
796 pi64 = (int64_t*)va_arg( args, int64_t * );
797 if( p_sys->i_timescale > 0 )
799 *pi64 = (mtime_t)1000000 *
800 (mtime_t)p_sys->i_duration /
801 (mtime_t)p_sys->i_timescale;
807 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
812 vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
814 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
815 MP4_Box_t *p_0xa9xxx;
820 *pp_meta = meta = vlc_meta_New();
821 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
822 p_0xa9xxx = p_0xa9xxx->p_next )
825 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
827 psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
828 if( psz_utf == NULL )
830 /* FIXME FIXME: should convert from whatever the character
831 * encoding of MP4 meta data is to UTF-8. */
832 EnsureUTF8( psz_utf );
834 switch( p_0xa9xxx->i_type )
836 case FOURCC_0xa9nam: /* Full name */
837 vlc_meta_Add( meta, VLC_META_TITLE, psz_utf );
840 vlc_meta_Add( meta, VLC_META_AUTHOR, psz_utf );
843 vlc_meta_Add( meta, VLC_META_ARTIST, psz_utf );
846 vlc_meta_Add( meta, VLC_META_COPYRIGHT, psz_utf );
848 case FOURCC_0xa9day: /* Creation Date */
849 vlc_meta_Add( meta, VLC_META_DATE, psz_utf );
851 case FOURCC_0xa9des: /* Description */
852 vlc_meta_Add( meta, VLC_META_DESCRIPTION, psz_utf );
854 case FOURCC_0xa9gen: /* Genre */
855 vlc_meta_Add( meta, VLC_META_GENRE, psz_utf );
859 case FOURCC_0xa9inf: /* Information */
860 case FOURCC_0xa9alb: /* Album */
861 case FOURCC_0xa9dir: /* Director */
862 case FOURCC_0xa9dis: /* Disclaimer */
863 case FOURCC_0xa9enc: /* Encoded By */
864 case FOURCC_0xa9trk: /* Track */
865 case FOURCC_0xa9cmt: /* Commment */
866 case FOURCC_0xa9url: /* URL */
867 case FOURCC_0xa9req: /* Requirements */
868 case FOURCC_0xa9fmt: /* Original Format */
869 case FOURCC_0xa9dsa: /* Display Source As */
870 case FOURCC_0xa9hst: /* Host Computer */
871 case FOURCC_0xa9prd: /* Producer */
872 case FOURCC_0xa9prf: /* Performers */
873 case FOURCC_0xa9ope: /* Original Performer */
874 case FOURCC_0xa9src: /* Providers Source Content */
875 case FOURCC_0xa9wrt: /* Writer */
876 case FOURCC_0xa9com: /* Composer */
877 case FOURCC_WLOC: /* Window Location */
878 /* TODO one day, but they aren't really meaningfull */
889 case DEMUX_GET_TITLE_INFO:
890 case DEMUX_SET_NEXT_DEMUX_TIME:
891 case DEMUX_SET_GROUP:
895 msg_Warn( p_demux, "control query %u unimplemented", i_query );
900 /*****************************************************************************
901 * Close: frees unused data
902 *****************************************************************************/
903 static void Close ( vlc_object_t * p_this )
905 unsigned int i_track;
906 demux_t * p_demux = (demux_t *)p_this;
907 demux_sys_t *p_sys = p_demux->p_sys;
909 msg_Dbg( p_demux, "freeing all memory" );
911 MP4_BoxFree( p_demux->s, p_sys->p_root );
912 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
914 MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
916 FREE( p_sys->track );
923 /****************************************************************************
924 * Local functions, specific to vlc
925 ****************************************************************************/
927 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
928 static int TrackCreateChunksIndex( demux_t *p_demux,
929 mp4_track_t *p_demux_track )
931 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
934 unsigned int i_chunk;
935 unsigned int i_index, i_last;
937 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
938 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
939 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
941 return( VLC_EGENERIC );
944 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
945 if( !p_demux_track->i_chunk_count )
947 msg_Warn( p_demux, "no chunk defined" );
948 return( VLC_EGENERIC );
950 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
951 sizeof( mp4_chunk_t ) );
953 /* first we read chunk offset */
954 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
956 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
958 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
961 ck->p_sample_count_dts = NULL;
962 ck->p_sample_delta_dts = NULL;
963 ck->p_sample_count_pts = NULL;
964 ck->p_sample_offset_pts = NULL;
967 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
968 to be used for the sample XXX begin to 1
969 We construct it begining at the end */
970 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
971 i_index = p_stsc->data.p_stsc->i_entry_count;
974 msg_Warn( p_demux, "cannot read chunk table or table empty" );
975 return( VLC_EGENERIC );
980 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
981 i_chunk < i_last; i_chunk++ )
983 p_demux_track->chunk[i_chunk].i_sample_description_index =
984 p_stsc->data.p_stsc->i_sample_description_index[i_index];
985 p_demux_track->chunk[i_chunk].i_sample_count =
986 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
988 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
991 p_demux_track->chunk[0].i_sample_first = 0;
992 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
994 p_demux_track->chunk[i_chunk].i_sample_first =
995 p_demux_track->chunk[i_chunk-1].i_sample_first +
996 p_demux_track->chunk[i_chunk-1].i_sample_count;
999 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1000 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1005 static int TrackCreateSamplesIndex( demux_t *p_demux,
1006 mp4_track_t *p_demux_track )
1009 MP4_Box_data_stsz_t *stsz;
1010 MP4_Box_data_stts_t *stts;
1011 /* TODO use also stss and stsh table for seeking */
1012 /* FIXME use edit table */
1017 int64_t i_index_sample_used;
1022 * Gives the sample size for each samples. There is also a stz2 table
1023 * (compressed form) that we need to implement TODO */
1024 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1027 /* FIXME and stz2 */
1028 msg_Warn( p_demux, "cannot find STSZ box" );
1029 return VLC_EGENERIC;
1031 stsz = p_box->data.p_stsz;
1034 * Gives mapping between sample and decoding time
1036 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1039 msg_Warn( p_demux, "cannot find STTS box" );
1040 return VLC_EGENERIC;
1042 stts = p_box->data.p_stts;
1044 /* Use stsz table to create a sample number -> sample size table */
1045 p_demux_track->i_sample_count = stsz->i_sample_count;
1046 if( stsz->i_sample_size )
1048 /* 1: all sample have the same size, so no need to construct a table */
1049 p_demux_track->i_sample_size = stsz->i_sample_size;
1050 p_demux_track->p_sample_size = NULL;
1054 /* 2: each sample can have a different size */
1055 p_demux_track->i_sample_size = 0;
1056 p_demux_track->p_sample_size =
1057 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1059 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1061 p_demux_track->p_sample_size[i_sample] =
1062 stsz->i_entry_size[i_sample];
1066 /* Use stts table to create a sample number -> dts table.
1067 * XXX: if we don't want to waste too much memory, we can't expand
1068 * the box! so each chunk will contain an "extract" of this table
1069 * for fast research (problem with raw stream where a sample is sometime
1070 * just channels*bits_per_sample/8 */
1073 i_index = 0; i_index_sample_used = 0;
1074 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1076 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1077 int64_t i_entry, i_sample_count, i;
1080 ck->i_first_dts = i_last_dts;
1082 /* count how many entries are needed for this chunk
1083 * for p_sample_delta_dts and p_sample_count_dts */
1084 i_sample_count = ck->i_sample_count;
1087 while( i_sample_count > 0 )
1089 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1090 /* don't count already used sample in this entry */
1092 i_sample_count += i_index_sample_used;
1098 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1099 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1102 i_sample_count = ck->i_sample_count;
1103 for( i = 0; i < i_entry; i++ )
1108 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1110 i_used = __MIN( i_rest, i_sample_count );
1112 i_index_sample_used += i_used;
1113 i_sample_count -= i_used;
1115 ck->p_sample_count_dts[i] = i_used;
1116 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1118 i_last_dts += i_used * ck->p_sample_delta_dts[i];
1120 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1123 i_index_sample_used = 0;
1129 * Gives the delta between decoding time (dts) and composition table (pts)
1131 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1134 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1136 msg_Warn( p_demux, "CTTS table" );
1138 /* Create pts-dts table per chunk */
1139 i_index = 0; i_index_sample_used = 0;
1140 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1142 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1143 int64_t i_entry, i_sample_count, i;
1145 /* count how many entries are needed for this chunk
1146 * for p_sample_delta_dts and p_sample_count_dts */
1147 i_sample_count = ck->i_sample_count;
1150 while( i_sample_count > 0 )
1152 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1154 /* don't count already used sample in this entry */
1156 i_sample_count += i_index_sample_used;
1162 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1163 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1166 i_sample_count = ck->i_sample_count;
1167 for( i = 0; i < i_entry; i++ )
1172 i_rest = ctts->i_sample_count[i_index] -
1173 i_index_sample_used;
1175 i_used = __MIN( i_rest, i_sample_count );
1177 i_index_sample_used += i_used;
1178 i_sample_count -= i_used;
1180 ck->p_sample_count_pts[i] = i_used;
1181 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1183 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1186 i_index_sample_used = 0;
1192 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:"I64Fd"s",
1193 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1194 i_last_dts / p_demux_track->i_timescale );
1201 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1203 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1204 unsigned int i_chunk, es_out_id_t **pp_es )
1206 MP4_Box_t *p_sample;
1212 if( !p_track->chunk[i_chunk].i_sample_description_index )
1214 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1215 p_track->i_track_ID );
1216 return VLC_EGENERIC;
1219 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1220 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1223 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1225 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1226 p_track->i_track_ID );
1227 return VLC_EGENERIC;
1230 p_track->p_sample = p_sample;
1232 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1234 MP4_Box_data_sample_soun_t *p_soun;
1236 p_soun = p_sample->data.p_sample_soun;
1238 if( p_soun->i_qt_version == 0 )
1240 switch( p_sample->i_type )
1242 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1243 p_soun->i_qt_version = 1;
1244 p_soun->i_sample_per_packet = 64;
1245 p_soun->i_bytes_per_packet = 34;
1246 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1247 p_soun->i_bytes_per_sample = 2;
1249 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1250 p_soun->i_qt_version = 1;
1251 p_soun->i_sample_per_packet = 6;
1252 p_soun->i_bytes_per_packet = 2;
1253 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1254 p_soun->i_bytes_per_sample = 2;
1256 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1257 p_soun->i_qt_version = 1;
1258 p_soun->i_sample_per_packet = 12;
1259 p_soun->i_bytes_per_packet = 2;
1260 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1261 p_soun->i_bytes_per_sample = 2;
1263 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1264 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1265 p_soun->i_samplesize = 8;
1272 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1274 p_soun->i_qt_version = 0;
1279 /* It's a little ugly but .. there are special cases */
1280 switch( p_sample->i_type )
1282 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1283 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1284 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1287 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1288 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1290 /* Buggy files workaround */
1291 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1292 p_sample->data.p_sample_soun->i_sampleratehi) )
1294 MP4_Box_data_sample_soun_t *p_soun =
1295 p_sample->data.p_sample_soun;
1297 msg_Warn( p_demux, "i_timescale ("I64Fu") != i_sampleratehi "
1298 "(%u), making both equal (report any problem).",
1299 p_track->i_timescale, p_soun->i_sampleratehi );
1301 if( p_soun->i_sampleratehi )
1302 p_track->i_timescale = p_soun->i_sampleratehi;
1304 p_soun->i_sampleratehi = p_track->i_timescale;
1308 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1309 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1312 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1313 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1314 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1315 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1316 /* FIXME UTF-8 doesn't work here ? */
1317 /* p_track->fmt.subs.psz_encoding = strdup( "UTF-8" ); */
1321 p_track->fmt.i_codec = p_sample->i_type;
1325 /* now see if esds is present and if so create a data packet
1326 with decoder_specific_info */
1327 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1328 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1329 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1330 ( p_esds->data.p_esds )&&
1333 /* First update information based on i_objectTypeIndication */
1334 switch( p_decconfig->i_objectTypeIndication )
1336 case( 0x20 ): /* MPEG4 VIDEO */
1337 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1340 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1347 case( 0x65): /* MPEG2 video */
1348 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1350 /* Theses are MPEG2-AAC */
1351 case( 0x66): /* main profile */
1352 case( 0x67): /* Low complexity profile */
1353 case( 0x68): /* Scaleable Sampling rate profile */
1354 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1356 /* true MPEG 2 audio */
1358 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1360 case( 0x6a): /* MPEG1 video */
1361 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1363 case( 0x6b): /* MPEG1 audio */
1364 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1366 case( 0x6c ): /* jpeg */
1367 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1371 case( 0xe0 ): /* NeroDigital: dvd subs */
1372 if( p_track->fmt.i_cat == SPU_ES )
1374 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1375 if( p_track->i_width > 0 )
1376 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1377 if( p_track->i_height > 0 )
1378 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1383 /* Unknown entry, but don't touch i_fourcc */
1385 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1386 p_decconfig->i_objectTypeIndication,
1387 p_track->i_track_ID );
1390 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1391 if( p_track->fmt.i_extra > 0 )
1393 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1394 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1395 p_track->fmt.i_extra );
1400 switch( p_sample->i_type )
1402 /* qt decoder, send the complete chunk */
1403 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1404 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1405 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1406 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1407 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1408 p_track->fmt.i_extra =
1409 p_sample->data.p_sample_vide->i_qt_image_description;
1410 if( p_track->fmt.i_extra > 0 )
1412 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1413 memcpy( p_track->fmt.p_extra,
1414 p_sample->data.p_sample_vide->p_qt_image_description,
1415 p_track->fmt.i_extra);
1418 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1419 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1420 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1421 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1422 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1423 p_track->fmt.i_extra =
1424 p_sample->data.p_sample_soun->i_qt_description;
1425 if( p_track->fmt.i_extra > 0 )
1427 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1428 memcpy( p_track->fmt.p_extra,
1429 p_sample->data.p_sample_soun->p_qt_description,
1430 p_track->fmt.i_extra);
1434 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1435 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1437 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1441 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1442 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1443 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1444 p_track->fmt.i_extra );
1448 msg_Err( p_demux, "missing avcC" );
1460 /* some last initialisation */
1461 switch( p_track->fmt.i_cat )
1464 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1465 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1466 p_track->fmt.video.i_bits_per_pixel =
1467 p_sample->data.p_sample_vide->i_depth;
1469 /* fall on display size */
1470 if( p_track->fmt.video.i_width <= 0 )
1471 p_track->fmt.video.i_width = p_track->i_width;
1472 if( p_track->fmt.video.i_height <= 0 )
1473 p_track->fmt.video.i_height = p_track->i_height;
1475 /* Find out apect ratio from display size */
1476 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1477 /* Work-around buggy muxed files */
1478 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1479 p_track->fmt.video.i_aspect =
1480 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1483 p_track->fmt.video.i_frame_rate = p_track->i_timescale;
1484 p_track->fmt.video.i_frame_rate_base = 1;
1486 if( p_track->fmt.video.i_frame_rate &&
1487 (p_box = MP4_BoxGet( p_track->p_stbl, "stts" )) &&
1488 p_box->data.p_stts->i_entry_count >= 1 )
1490 p_track->fmt.video.i_frame_rate_base =
1491 p_box->data.p_stts->i_sample_delta[0];
1497 p_track->fmt.audio.i_channels =
1498 p_sample->data.p_sample_soun->i_channelcount;
1499 p_track->fmt.audio.i_rate =
1500 p_sample->data.p_sample_soun->i_sampleratehi;
1501 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1502 p_sample->data.p_sample_soun->i_sampleratehi *
1503 p_sample->data.p_sample_soun->i_samplesize;
1504 p_track->fmt.audio.i_bitspersample =
1505 p_sample->data.p_sample_soun->i_samplesize;
1512 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1517 /* given a time it return sample/chunk
1518 * it also update elst field of the track
1520 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1521 int64_t i_start, uint32_t *pi_chunk,
1522 uint32_t *pi_sample )
1524 demux_sys_t *p_sys = p_demux->p_sys;
1527 unsigned int i_sample;
1528 unsigned int i_chunk;
1531 /* FIXME see if it's needed to check p_track->i_chunk_count */
1532 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1534 return( VLC_EGENERIC );
1537 /* handle elst (find the correct one) */
1538 MP4_TrackSetELST( p_demux, p_track, i_start );
1539 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1541 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1542 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1544 /* now calculate i_start for this elst */
1546 i_start -= p_track->i_elst_time * I64C(1000000) / p_sys->i_timescale;
1554 /* to track time scale */
1555 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1556 /* add elst offset */
1557 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1558 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1559 elst->i_media_time[p_track->i_elst] > 0 )
1561 i_start += elst->i_media_time[p_track->i_elst];
1564 msg_Dbg( p_demux, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd
1565 "ms (track)", p_track->i_elst,
1566 i_mvt * 1000 / p_sys->i_timescale,
1567 i_start * 1000 / p_track->i_timescale );
1571 /* convert absolute time to in timescale unit */
1572 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1575 /* we start from sample 0/chunk 0, hope it won't take too much time */
1576 /* *** find good chunk *** */
1577 for( i_chunk = 0; ; i_chunk++ )
1579 if( i_chunk + 1 >= p_track->i_chunk_count )
1581 /* at the end and can't check if i_start in this chunk,
1582 it will be check while searching i_sample */
1583 i_chunk = p_track->i_chunk_count - 1;
1587 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
1588 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1594 /* *** find sample in the chunk *** */
1595 i_sample = p_track->chunk[i_chunk].i_sample_first;
1596 i_dts = p_track->chunk[i_chunk].i_first_dts;
1597 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1600 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1601 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
1604 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1605 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1607 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1612 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1616 i_sample += ( i_start - i_dts ) /
1617 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1622 if( i_sample >= p_track->i_sample_count )
1624 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1625 "(seeking too far) chunk=%d sample=%d",
1626 p_track->i_track_ID, i_chunk, i_sample );
1627 return( VLC_EGENERIC );
1631 /* *** Try to find nearest sync points *** */
1632 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1634 unsigned int i_index;
1636 "track[Id 0x%x] using Sync Sample Box (stss)",
1637 p_track->i_track_ID );
1638 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1640 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1644 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1646 p_stss->data.p_stss->i_sample_number[i_index-1] );
1647 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1648 /* new i_sample is less than old so i_chunk can only decreased */
1649 while( i_chunk > 0 &&
1650 i_sample < p_track->chunk[i_chunk].i_sample_first )
1657 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1659 p_stss->data.p_stss->i_sample_number[i_index] );
1660 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1661 /* new i_sample is more than old so i_chunk can only increased */
1662 while( i_chunk < p_track->i_chunk_count - 1 &&
1663 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1664 p_track->chunk[i_chunk].i_sample_count )
1675 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1676 "Sample Box (stss)", p_track->i_track_ID );
1679 *pi_chunk = i_chunk;
1680 *pi_sample = i_sample;
1685 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1686 unsigned int i_chunk, unsigned int i_sample )
1688 vlc_bool_t b_reselect = VLC_FALSE;
1690 /* now see if actual es is ok */
1691 if( p_track->i_chunk < 0 ||
1692 p_track->i_chunk >= p_track->i_chunk_count - 1 ||
1693 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1694 p_track->chunk[i_chunk].i_sample_description_index )
1696 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1697 p_track->i_track_ID );
1699 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1700 p_track->p_es, &b_reselect );
1702 es_out_Del( p_demux->out, p_track->p_es );
1704 p_track->p_es = NULL;
1706 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1708 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1709 p_track->i_track_ID );
1711 p_track->b_ok = VLC_FALSE;
1712 p_track->b_selected = VLC_FALSE;
1713 return VLC_EGENERIC;
1717 /* select again the new decoder */
1720 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1723 p_track->i_chunk = i_chunk;
1724 p_track->i_sample = i_sample;
1726 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1729 /****************************************************************************
1731 ****************************************************************************
1732 * Parse track information and create all needed data to run a track
1733 * If it succeed b_ok is set to 1 else to 0
1734 ****************************************************************************/
1735 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1736 MP4_Box_t *p_box_trak )
1738 demux_sys_t *p_sys = p_demux->p_sys;
1740 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1741 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1756 /* hint track unsupported */
1758 /* set default value (-> track unusable) */
1759 p_track->b_ok = VLC_FALSE;
1760 p_track->b_enable = VLC_FALSE;
1761 p_track->b_selected = VLC_FALSE;
1763 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1770 /* do we launch this track by default ? */
1772 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1774 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1775 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1776 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1780 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
1783 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1784 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1786 if( ( !p_mdhd )||( !p_hdlr ) )
1791 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1793 for( i = 0; i < 3; i++ )
1795 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1799 switch( p_hdlr->data.p_hdlr->i_handler_type )
1801 case( FOURCC_soun ):
1802 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1806 p_track->fmt.i_cat = AUDIO_ES;
1809 case( FOURCC_vide ):
1810 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1814 p_track->fmt.i_cat = VIDEO_ES;
1817 case( FOURCC_text ):
1818 case( FOURCC_subp ):
1819 case( FOURCC_tx3g ):
1820 p_track->fmt.i_cat = SPU_ES;
1827 p_track->i_elst = 0;
1828 p_track->i_elst_time = 0;
1829 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1831 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1834 msg_Warn( p_demux, "elst box found" );
1835 for( i = 0; i < elst->i_entry_count; i++ )
1837 msg_Dbg( p_demux, " - [%d] duration="I64Fd"ms media time="I64Fd
1838 "ms) rate=%d.%d", i,
1839 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1840 elst->i_media_time[i] >= 0 ?
1841 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
1843 elst->i_media_rate_integer[i],
1844 elst->i_media_rate_fraction[i] );
1851 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1853 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1854 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1859 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1860 p_track->b_drms = p_drms != NULL;
1861 p_track->p_drms = p_track->b_drms ?
1862 p_drms->data.p_sample_soun->p_drms : NULL;
1865 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1867 p_track->fmt.psz_language = strdup( language );
1870 p_udta = MP4_BoxGet( p_box_trak, "udta" );
1873 MP4_Box_t *p_0xa9xxx;
1874 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
1875 p_0xa9xxx = p_0xa9xxx->p_next )
1877 switch( p_0xa9xxx->i_type )
1879 case FOURCC_0xa9nam:
1880 p_track->fmt.psz_description =
1881 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
1887 /* Create chunk index table and sample index table */
1888 if( TrackCreateChunksIndex( p_demux,p_track ) ||
1889 TrackCreateSamplesIndex( p_demux, p_track ) )
1891 return; /* cannot create chunks index */
1894 p_track->i_chunk = 0;
1895 p_track->i_sample = 0;
1898 if( TrackCreateES( p_demux,
1899 p_track, p_track->i_chunk,
1902 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1903 p_track->i_track_ID );
1910 for( i = 0; i < p_track->i_chunk_count; i++ )
1912 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
1913 i, p_track->chunk[i].i_sample_count,
1914 p_track->chunk[i].i_first_dts );
1919 p_track->b_ok = VLC_TRUE;
1922 /****************************************************************************
1924 ****************************************************************************
1925 * Destroy a track created by MP4_TrackCreate.
1926 ****************************************************************************/
1927 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
1929 unsigned int i_chunk;
1931 p_track->b_ok = VLC_FALSE;
1932 p_track->b_enable = VLC_FALSE;
1933 p_track->b_selected = VLC_FALSE;
1935 es_format_Clean( &p_track->fmt );
1937 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1939 if( p_track->chunk )
1941 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1942 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1944 FREE(p_track->chunk[i_chunk].p_sample_count_pts);
1945 FREE(p_track->chunk[i_chunk].p_sample_offset_pts );
1948 FREE( p_track->chunk );
1950 if( !p_track->i_sample_size )
1952 FREE( p_track->p_sample_size );
1956 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
1959 if( !p_track->b_ok )
1961 return VLC_EGENERIC;
1964 if( p_track->b_selected )
1966 msg_Warn( p_demux, "track[Id 0x%x] already selected",
1967 p_track->i_track_ID );
1971 return MP4_TrackSeek( p_demux, p_track, i_start );
1974 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
1976 if( !p_track->b_ok )
1981 if( !p_track->b_selected )
1983 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
1984 p_track->i_track_ID );
1989 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
1990 p_track->p_es, VLC_FALSE );
1993 p_track->b_selected = VLC_FALSE;
1996 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2002 if( !p_track->b_ok )
2004 return( VLC_EGENERIC );
2007 p_track->b_selected = VLC_FALSE;
2009 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2010 &i_chunk, &i_sample ) )
2012 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2013 p_track->i_track_ID );
2014 return( VLC_EGENERIC );
2017 p_track->b_selected = VLC_TRUE;
2019 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
2022 p_track->b_selected = VLC_TRUE;
2024 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
2025 p_track->p_es, i_start );
2028 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
2033 * 3 types: for audio
2036 #define QT_V0_MAX_SAMPLES 1024
2037 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2040 MP4_Box_data_sample_soun_t *p_soun;
2042 if( p_track->i_sample_size == 0 )
2044 /* most simple case */
2045 return p_track->p_sample_size[p_track->i_sample];
2047 if( p_track->fmt.i_cat != AUDIO_ES )
2049 return p_track->i_sample_size;
2052 p_soun = p_track->p_sample->data.p_sample_soun;
2054 if( p_soun->i_qt_version == 1 )
2056 i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
2057 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2059 else if( p_track->i_sample_size > 256 )
2061 /* We do that so we don't read too much data
2062 * (in this case we are likely dealing with compressed data) */
2063 i_size = p_track->i_sample_size;
2067 /* Read a bunch of samples at once */
2068 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2069 ( p_track->i_sample -
2070 p_track->chunk[p_track->i_chunk].i_sample_first );
2072 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2073 i_size = i_samples * p_track->i_sample_size;
2076 //fprintf( stderr, "size=%d\n", i_size );
2080 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2082 unsigned int i_sample;
2085 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2087 if( p_track->i_sample_size )
2089 MP4_Box_data_sample_soun_t *p_soun =
2090 p_track->p_sample->data.p_sample_soun;
2092 if( p_soun->i_qt_version == 0 )
2094 i_pos += ( p_track->i_sample -
2095 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2096 p_track->i_sample_size;
2100 /* we read chunk by chunk */
2106 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2107 i_sample < p_track->i_sample; i_sample++ )
2109 i_pos += p_track->p_sample_size[i_sample];
2116 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2118 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2120 MP4_Box_data_sample_soun_t *p_soun;
2122 p_soun = p_track->p_sample->data.p_sample_soun;
2124 if( p_soun->i_qt_version == 1 )
2126 /* chunk by chunk */
2128 p_track->chunk[p_track->i_chunk].i_sample_first +
2129 p_track->chunk[p_track->i_chunk].i_sample_count;
2131 else if( p_track->i_sample_size > 256 )
2133 /* We do that so we don't read too much data
2134 * (in this case we are likely dealing with compressed data) */
2135 p_track->i_sample += 1;
2140 p_track->i_sample += QT_V0_MAX_SAMPLES;
2141 if( p_track->i_sample >
2142 p_track->chunk[p_track->i_chunk].i_sample_first +
2143 p_track->chunk[p_track->i_chunk].i_sample_count )
2146 p_track->chunk[p_track->i_chunk].i_sample_first +
2147 p_track->chunk[p_track->i_chunk].i_sample_count;
2153 p_track->i_sample++;
2156 if( p_track->i_sample >= p_track->i_sample_count )
2157 return VLC_EGENERIC;
2159 /* Have we changed chunk ? */
2160 if( p_track->i_sample >=
2161 p_track->chunk[p_track->i_chunk].i_sample_first +
2162 p_track->chunk[p_track->i_chunk].i_sample_count )
2164 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2165 p_track->i_sample ) )
2167 msg_Warn( p_demux, "track[0x%x] will be disabled "
2168 "(cannot restart decoder)", p_track->i_track_ID );
2169 MP4_TrackUnselect( p_demux, p_track );
2170 return VLC_EGENERIC;
2174 /* Have we changed elst */
2175 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2177 demux_sys_t *p_sys = p_demux->p_sys;
2178 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2179 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2180 p_sys->i_timescale / (int64_t)1000000;
2182 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2183 i_mvt >= p_track->i_elst_time +
2184 elst->i_segment_duration[p_track->i_elst] )
2186 MP4_TrackSetELST( p_demux, p_track,
2187 MP4_TrackGetDTS( p_demux, p_track ) );
2194 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2197 demux_sys_t *p_sys = p_demux->p_sys;
2198 int i_elst_last = tk->i_elst;
2200 /* handle elst (find the correct one) */
2202 tk->i_elst_time = 0;
2203 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2205 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2206 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2208 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2210 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2212 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2216 tk->i_elst_time += i_dur;
2219 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2221 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2222 tk->i_elst = elst->i_entry_count - 1;
2223 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2226 if( elst->i_media_time[tk->i_elst] < 0 )
2229 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2232 if( i_elst_last != tk->i_elst )
2234 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );