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>
38 #define uint64_t int64_t
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
48 set_description( _("MP4 stream demuxer") );
49 set_capability( "demux2", 242 );
50 set_callbacks( Open, Close );
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Demux ( demux_t * );
57 static int DemuxRef( demux_t *p_demux )
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 /* TODO if needed add pts
81 but quickly *add* support for edts and seeking */
86 /* Contain all needed information for read all track with vlc */
89 int i_track_ID; /* this should be unique */
91 int b_ok; /* The track is usable */
92 int b_enable; /* is the trak enable by default */
93 vlc_bool_t b_selected; /* is the trak being played */
98 /* display size only ! */
102 /* more internal data */
103 uint64_t i_timescale; /* time scale for this track only */
106 int i_elst; /* current elst */
107 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
108 MP4_Box_t *p_elst; /* elst (could be NULL) */
110 /* give the next sample to read, i_chunk is to find quickly where
111 the sample is located */
112 uint32_t i_sample; /* next sample to read */
113 uint32_t i_chunk; /* chunk where next sample is stored */
114 /* total count of chunk and sample */
115 uint32_t i_chunk_count;
116 uint32_t i_sample_count;
118 mp4_chunk_t *chunk; /* always defined for each chunk */
120 /* sample size, p_sample_size defined only if i_sample_size == 0
121 else i_sample_size is size for all sample */
122 uint32_t i_sample_size;
123 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
124 too much time to do sumations each time*/
126 MP4_Box_t *p_stbl; /* will contain all timing information */
127 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
128 MP4_Box_t *p_sample;/* point on actual sdsd */
138 MP4_Box_t *p_root; /* container for the whole file */
142 uint64_t i_time; /* time position of the presentation
143 * in movie timescale */
144 uint64_t i_timescale; /* movie time scale */
145 uint64_t i_duration; /* movie duration */
146 unsigned int i_tracks; /* number of tracks */
147 mp4_track_t *track; /* array of track */
151 /*****************************************************************************
152 * Declaration of local function
153 *****************************************************************************/
154 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *);
155 static void MP4_TrackDestroy( demux_t *, mp4_track_t * );
157 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
158 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
160 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
162 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
163 static int MP4_TrackSampleSize( mp4_track_t * );
164 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
165 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
167 /* Return time in µs of a track */
168 static inline int64_t MP4_TrackGetPTS( demux_t *p_demux, mp4_track_t *p_track )
170 unsigned int i_sample;
171 unsigned int i_index;
174 i_sample = p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first;
175 i_dts = p_track->chunk[p_track->i_chunk].i_first_dts;
177 while( i_sample > 0 )
179 if( i_sample > p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] )
181 i_dts += p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] *
182 p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
183 i_sample -= p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index];
189 p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
195 /* now handle elst */
196 if( p_track->p_elst )
198 demux_sys_t *p_sys = p_demux->p_sys;
199 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
201 /* convert to offset */
202 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
203 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
204 elst->i_media_time[p_track->i_elst] > 0 )
206 i_dts -= elst->i_media_time[p_track->i_elst];
209 /* add i_elst_time */
210 i_dts += p_track->i_elst_time * p_track->i_timescale / p_sys->i_timescale;
212 if( i_dts < 0 ) i_dts = 0;
215 return (int64_t)1000000 * i_dts / p_track->i_timescale;
217 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
219 return (int64_t)1000000 * p_sys->i_time / p_sys->i_timescale;
222 #define FREE( p ) if( p ) { free( p ); (p) = NULL;}
224 /*****************************************************************************
225 * Open: check file and initializes MP4 structures
226 *****************************************************************************/
227 static int Open( vlc_object_t * p_this )
229 demux_t *p_demux = (demux_t *)p_this;
240 vlc_bool_t b_seekable;
242 /* a little test to see if it could be a mp4 */
243 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
245 msg_Warn( p_demux, "MP4 plugin discarded (cannot peek)" );
248 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
259 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
262 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
267 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
270 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
274 /*Set exported functions */
275 p_demux->pf_demux = Demux;
276 p_demux->pf_control = Control;
278 /* create our structure that will contains all data */
279 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
280 memset( p_sys, 0, sizeof( demux_sys_t ) );
282 /* Now load all boxes ( except raw data ) */
283 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
285 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
289 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
291 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
293 switch( p_ftyp->data.p_ftyp->i_major_brand )
297 "ISO Media file (isom) version %d.",
298 p_ftyp->data.p_ftyp->i_minor_version );
302 "unrecognized major file specification (%4.4s).",
303 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
309 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
312 /* the file need to have one moov box */
313 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
315 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
319 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
322 /* we have a free box as a moov, rename it */
323 p_foov->i_type = FOURCC_moov;
326 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
328 playlist_t *p_playlist;
329 int i_count = MP4_BoxCount( p_rmra, "rmda" );
332 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
335 (playlist_t *)vlc_object_find( p_demux,
340 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
341 for( i = 0; i < i_count; i++ )
343 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
346 int i_position = p_playlist->i_index;
348 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
352 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
354 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
355 psz_ref, (char*)&i_ref_type );
357 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
359 if( strstr( psz_ref, "qt5gateQT" ) )
361 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
364 if( !strncmp( psz_ref, "http://", 7 ) ||
365 !strncmp( psz_ref, "rtsp://", 7 ) )
367 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
368 playlist_Add( p_playlist, psz_ref, psz_ref,
369 PLAYLIST_APPEND, i_position );
373 /* msg dbg relative ? */
374 char *psz_absolute = alloca( strlen( p_demux->psz_access ) + 3 + strlen( p_demux->psz_path ) + strlen( psz_ref ) + 1);
375 char *end = strrchr( p_demux->psz_path, '/' );
379 int i_len = end + 1 - p_demux->psz_path;
381 strcpy( psz_absolute, p_demux->psz_access );
382 strcat( psz_absolute, "://" );
383 strncat( psz_absolute, p_demux->psz_path, i_len);
387 strcpy( psz_absolute, "" );
389 strcat( psz_absolute, psz_ref );
390 msg_Dbg( p_demux, "adding ref = `%s'", psz_absolute );
391 playlist_Add( p_playlist, psz_absolute, psz_absolute,
392 PLAYLIST_APPEND, i_position );
397 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
398 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
401 vlc_object_release( p_playlist );
405 msg_Err( p_demux, "can't find playlist" );
409 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
413 msg_Err( p_demux, "cannot find /moov/mvhd" );
418 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
419 p_demux->pf_demux = DemuxRef;
425 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
426 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
429 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
431 msg_Err( p_demux, "cannot find any /moov/trak" );
434 msg_Dbg( p_demux, "find %d track%c",
436 p_sys->i_tracks ? 's':' ' );
438 /* allocate memory */
439 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
440 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
442 /* now process each track and extract all usefull information */
443 for( i = 0; i < p_sys->i_tracks; i++ )
445 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
446 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
448 if( p_sys->track[i].b_ok )
451 switch( p_sys->track[i].fmt.i_cat )
460 psz_cat = "subtitle";
468 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
469 p_sys->track[i].i_track_ID,
471 p_sys->track[i].b_enable ? "enable":"disable",
472 p_sys->track[i].fmt.psz_language ? p_sys->track[i].fmt.psz_language : "undef" );
476 msg_Dbg( p_demux, "ignoring track[Id 0x%x]", p_sys->track[i].i_track_ID );
485 MP4_BoxFree( p_demux->s, p_sys->p_root );
491 /*****************************************************************************
492 * Demux: read packet and send them to decoders
493 *****************************************************************************
494 * TODO check for newly selected track (ie audio upt to now )
495 *****************************************************************************/
496 static int Demux( demux_t *p_demux )
498 demux_sys_t *p_sys = p_demux->p_sys;
499 unsigned int i_track;
502 unsigned int i_track_selected;
504 /* check for newly selected/unselected track */
505 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
508 mp4_track_t *tk = &p_sys->track[i_track];
510 if( tk->b_selected && tk->i_sample >= tk->i_sample_count )
512 msg_Warn( p_demux, "track[0x%x] will be disabled", tk->i_track_ID );
513 MP4_TrackUnselect( p_demux, tk);
518 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
520 if( tk->b_selected && !b )
522 MP4_TrackUnselect( p_demux, tk );
524 else if( !tk->b_selected && b)
526 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
536 if( i_track_selected <= 0 )
538 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
539 if( p_sys->i_timescale > 0 )
541 int64_t i_length = (mtime_t)1000000 *
542 (mtime_t)p_sys->i_duration /
543 (mtime_t)p_sys->i_timescale;
544 if( MP4_GetMoviePTS( p_sys ) >= i_length )
549 msg_Warn( p_demux, "no track selected, exiting..." );
553 /* first wait for the good time to read a packet */
554 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
556 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
558 /* we will read 100ms for each stream so ...*/
559 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
561 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
563 mp4_track_t *tk = &p_sys->track[i_track];
565 if( !tk->b_ok || !tk->b_selected )
570 while( MP4_TrackGetPTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
573 msg_Dbg( p_demux, "tk=%lld mv=%lld",
574 MP4_TrackGetPTS( p_demux, tk ),
575 MP4_GetMoviePTS( p_sys ) );
578 if( MP4_TrackSampleSize( tk ) > 0 )
583 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
585 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
586 MP4_TrackUnselect( p_demux, tk );
591 if( ( p_block = stream_Block( p_demux->s,
592 MP4_TrackSampleSize( tk ) ) ) == NULL )
594 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
595 MP4_TrackUnselect( p_demux, tk );
599 if( tk->b_drms && tk->p_drms )
601 drms_decrypt( tk->p_drms,
602 (uint32_t*)p_block->p_buffer,
605 else if( tk->fmt.i_cat == SPU_ES )
607 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) && p_block->i_buffer >= 2 )
609 uint16_t i_size = GetWBE( p_block->p_buffer );
611 if( i_size + 2 <= p_block->i_buffer )
614 /* remove the length field, and append a '\0' */
615 memmove( &p_block->p_buffer[0], &p_block->p_buffer[2], i_size );
616 p_block->p_buffer[i_size] = '\0';
617 p_block->i_buffer = i_size + 1;
619 /* convert \r -> \n */
620 while( ( p = strchr( p_block->p_buffer, '\r' ) ) )
628 p_block->i_buffer = 0;
632 p_block->i_dts = MP4_TrackGetPTS( p_demux, tk ) + 1;
634 p_block->i_pts = tk->fmt.i_cat == VIDEO_ES ? 0 : p_block->i_dts + 1;
636 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
638 es_out_Send( p_demux->out, tk->p_es, p_block );
643 if( MP4_TrackNextSample( p_demux, tk ) )
652 /*****************************************************************************
653 * Seek: Got to i_date
654 ******************************************************************************/
655 static int Seek( demux_t *p_demux, mtime_t i_date )
657 demux_sys_t *p_sys = p_demux->p_sys;
658 unsigned int i_track;
660 /* First update update global time */
661 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
662 p_sys->i_pcr = i_date;
664 /* Now for each stream try to go to this time */
665 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
667 mp4_track_t *tk = &p_sys->track[i_track];
668 MP4_TrackSeek( p_demux, tk, i_date );
673 /*****************************************************************************
675 *****************************************************************************/
676 static int Control( demux_t *p_demux, int i_query, va_list args )
678 demux_sys_t *p_sys = p_demux->p_sys;
685 case DEMUX_GET_POSITION:
686 pf = (double*)va_arg( args, double * );
687 if( p_sys->i_duration > 0 )
689 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
697 case DEMUX_SET_POSITION:
698 f = (double)va_arg( args, double );
699 if( p_sys->i_timescale > 0 )
701 i64 = (int64_t)( f * (double)1000000 *
702 (double)p_sys->i_duration /
703 (double)p_sys->i_timescale );
704 return Seek( p_demux, i64 );
706 else return VLC_SUCCESS;
709 pi64 = (int64_t*)va_arg( args, int64_t * );
710 if( p_sys->i_timescale > 0 )
712 *pi64 = (mtime_t)1000000 *
713 (mtime_t)p_sys->i_time /
714 (mtime_t)p_sys->i_timescale;
720 i64 = (int64_t)va_arg( args, int64_t );
721 return Seek( p_demux, i64 );
723 case DEMUX_GET_LENGTH:
724 pi64 = (int64_t*)va_arg( args, int64_t * );
725 if( p_sys->i_timescale > 0 )
727 *pi64 = (mtime_t)1000000 *
728 (mtime_t)p_sys->i_duration /
729 (mtime_t)p_sys->i_timescale;
735 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
740 vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
742 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
743 MP4_Box_t *p_0xa9xxx;
748 *pp_meta = meta = vlc_meta_New();
749 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
750 p_0xa9xxx = p_0xa9xxx->p_next )
752 switch( p_0xa9xxx->i_type )
755 vlc_meta_Add( meta, VLC_META_TITLE,
756 p_0xa9xxx->data.p_0xa9xxx->psz_text );
759 vlc_meta_Add( meta, VLC_META_AUTHOR,
760 p_0xa9xxx->data.p_0xa9xxx->psz_text );
763 vlc_meta_Add( meta, VLC_META_ARTIST,
764 p_0xa9xxx->data.p_0xa9xxx->psz_text );
767 vlc_meta_Add( meta, VLC_META_COPYRIGHT,
768 p_0xa9xxx->data.p_0xa9xxx->psz_text );
771 vlc_meta_Add( meta, VLC_META_DATE,
772 p_0xa9xxx->data.p_0xa9xxx->psz_text );
775 vlc_meta_Add( meta, VLC_META_DESCRIPTION,
776 p_0xa9xxx->data.p_0xa9xxx->psz_text );
788 /* TODO one day, but they aren't really meaningfull */
798 case DEMUX_GET_TITLE_INFO:
799 case DEMUX_SET_NEXT_DEMUX_TIME:
800 case DEMUX_SET_GROUP:
804 msg_Warn( p_demux, "control query unimplemented !!!" );
809 /*****************************************************************************
810 * Close: frees unused data
811 *****************************************************************************/
812 static void Close ( vlc_object_t * p_this )
814 unsigned int i_track;
815 demux_t * p_demux = (demux_t *)p_this;
816 demux_sys_t *p_sys = p_demux->p_sys;
818 msg_Dbg( p_demux, "freeing all memory" );
820 MP4_BoxFree( p_demux->s, p_sys->p_root );
821 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
823 MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
825 FREE( p_sys->track );
832 /****************************************************************************
833 * Local functions, specific to vlc
834 ****************************************************************************/
836 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
837 static int TrackCreateChunksIndex( demux_t *p_demux,
838 mp4_track_t *p_demux_track )
840 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
843 unsigned int i_chunk;
844 unsigned int i_index, i_last;
846 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
847 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
848 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
850 return( VLC_EGENERIC );
853 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
854 if( !p_demux_track->i_chunk_count )
856 msg_Warn( p_demux, "no chunk defined" );
857 return( VLC_EGENERIC );
859 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
860 sizeof( mp4_chunk_t ) );
862 /* first we read chunk offset */
863 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
865 p_demux_track->chunk[i_chunk].i_offset =
866 p_co64->data.p_co64->i_chunk_offset[i_chunk];
869 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
870 to be used for the sample XXX begin to 1
871 We construct it begining at the end */
872 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
873 i_index = p_stsc->data.p_stsc->i_entry_count;
876 msg_Warn( p_demux, "cannot read chunk table or table empty" );
877 return( VLC_EGENERIC );
883 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
884 i_chunk < i_last; i_chunk++ )
886 p_demux_track->chunk[i_chunk].i_sample_description_index =
887 p_stsc->data.p_stsc->i_sample_description_index[i_index];
888 p_demux_track->chunk[i_chunk].i_sample_count =
889 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
891 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
894 p_demux_track->chunk[0].i_sample_first = 0;
895 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
897 p_demux_track->chunk[i_chunk].i_sample_first =
898 p_demux_track->chunk[i_chunk-1].i_sample_first +
899 p_demux_track->chunk[i_chunk-1].i_sample_count;
903 "track[Id 0x%x] read %d chunk",
904 p_demux_track->i_track_ID,
905 p_demux_track->i_chunk_count );
907 return( VLC_SUCCESS );
909 static int TrackCreateSamplesIndex( demux_t *p_demux,
910 mp4_track_t *p_demux_track )
912 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
913 ctts make same mapping but for composition time,
914 not yet used and probably not usefull */
915 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
916 that uses a compressed form FIXME make them in libmp4
918 /* TODO use also stss and stsh table for seeking */
919 /* FIXME use edit table */
924 int64_t i_index_sample_used;
928 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
929 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
931 if( ( !p_stts )||( !p_stsz ) )
933 msg_Warn( p_demux, "cannot read sample table" );
934 return( VLC_EGENERIC );
937 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
940 /* for sample size, there are 2 case */
941 if( p_stsz->data.p_stsz->i_sample_size )
943 /* 1: all sample have the same size, so no need to construct a table */
944 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
945 p_demux_track->p_sample_size = NULL;
949 /* 2: each sample can have a different size */
950 p_demux_track->i_sample_size = 0;
951 p_demux_track->p_sample_size =
952 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
954 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
956 p_demux_track->p_sample_size[i_sample] =
957 p_stsz->data.p_stsz->i_entry_size[i_sample];
960 /* we have extract all information from stsz,
963 /* if we don't want to waste too much memory, we can't expand
964 the box !, so each chunk will contain an "extract" of this table
968 i_index = 0; i_index_sample_used =0;
970 /* create and init last data for each chunk */
971 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
974 int64_t i_entry, i_sample_count, i;
976 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
977 /* count how many entries needed for this chunk
978 for p_sample_delta_dts and p_sample_count_dts */
980 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
983 while( i_sample_count > 0 )
985 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
988 i_sample_count += i_index_sample_used; /* don't count already used sample
995 p_demux_track->chunk[i_chunk].p_sample_count_dts =
996 calloc( i_entry, sizeof( uint32_t ) );
997 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
998 calloc( i_entry, sizeof( uint32_t ) );
1001 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
1002 for( i = 0; i < i_entry; i++ )
1007 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
1009 i_used = __MIN( i_rest, i_sample_count );
1011 i_index_sample_used += i_used;
1012 i_sample_count -= i_used;
1014 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
1016 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
1017 p_stts->data.p_stts->i_sample_delta[i_index];
1019 i_last_dts += i_used *
1020 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
1022 if( i_index_sample_used >=
1023 p_stts->data.p_stts->i_sample_count[i_index] )
1027 i_index_sample_used = 0;
1033 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:"I64Fd"s",
1034 p_demux_track->i_track_ID,
1035 p_demux_track->i_sample_count,
1036 i_last_dts / p_demux_track->i_timescale );
1038 return( VLC_SUCCESS );
1043 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1045 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1046 unsigned int i_chunk, es_out_id_t **pp_es )
1048 MP4_Box_t *p_sample;
1053 if( !p_track->chunk[i_chunk].i_sample_description_index )
1056 "invalid SampleEntry index (track[Id 0x%x])",
1057 p_track->i_track_ID );
1058 return VLC_EGENERIC;
1061 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1062 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1064 if( !p_sample || ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1067 "cannot find SampleEntry (track[Id 0x%x])",
1068 p_track->i_track_ID );
1069 return( VLC_EGENERIC );
1072 p_track->p_sample = p_sample;
1074 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1076 MP4_Box_data_sample_soun_t *p_soun;
1078 p_soun = p_sample->data.p_sample_soun;
1080 if( p_soun->i_qt_version == 0 )
1082 switch( p_sample->i_type )
1084 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1085 p_soun->i_qt_version = 1;
1086 p_soun->i_sample_per_packet = 64;
1087 p_soun->i_bytes_per_packet = 34;
1088 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1089 p_soun->i_bytes_per_sample = 2;
1091 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1092 p_soun->i_qt_version = 1;
1093 p_soun->i_sample_per_packet = 6;
1094 p_soun->i_bytes_per_packet = 2;
1095 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1096 p_soun->i_bytes_per_sample = 2;
1098 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1099 p_soun->i_qt_version = 1;
1100 p_soun->i_sample_per_packet = 12;
1101 p_soun->i_bytes_per_packet = 2;
1102 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1103 p_soun->i_bytes_per_sample = 2;
1105 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1106 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1107 p_soun->i_samplesize = 8;
1114 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1116 p_soun->i_qt_version = 0;
1121 /* It's a little ugly but .. there are special cases */
1122 switch( p_sample->i_type )
1124 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1125 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1126 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1128 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1129 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1131 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1132 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1135 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1136 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1137 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1138 /* FIXME UTF-8 doesn't work here ? */
1139 /* p_track->fmt.subs.psz_encoding = strdup( "UTF-8" ); */
1143 p_track->fmt.i_codec = p_sample->i_type;
1147 /* now see if esds is present and if so create a data packet
1148 with decoder_specific_info */
1149 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1150 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1151 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1152 ( p_esds->data.p_esds )&&
1155 /* First update information based on i_objectTypeIndication */
1156 switch( p_decconfig->i_objectTypeIndication )
1158 case( 0x20 ): /* MPEG4 VIDEO */
1159 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1162 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1169 case( 0x65): /* MPEG2 video */
1170 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1172 /* Theses are MPEG2-AAC */
1173 case( 0x66): /* main profile */
1174 case( 0x67): /* Low complexity profile */
1175 case( 0x68): /* Scaleable Sampling rate profile */
1176 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1178 /* true MPEG 2 audio */
1180 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1182 case( 0x6a): /* MPEG1 video */
1183 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1185 case( 0x6b): /* MPEG1 audio */
1186 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1188 case( 0x6c ): /* jpeg */
1189 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1193 case( 0xe0 ): /* NeroDigital: dvd subs */
1194 if( p_track->fmt.i_cat == SPU_ES )
1196 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1201 /* Unknown entry, but don't touch i_fourcc */
1203 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1204 p_decconfig->i_objectTypeIndication,
1205 p_track->i_track_ID );
1208 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1209 if( p_track->fmt.i_extra > 0 )
1211 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1212 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1213 p_track->fmt.i_extra );
1218 switch( p_sample->i_type )
1220 /* qt decoder, send the complete chunk */
1221 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1222 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1223 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1224 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1225 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1226 p_track->fmt.i_extra =
1227 p_sample->data.p_sample_vide->i_qt_image_description;
1228 if( p_track->fmt.i_extra > 0 )
1230 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1231 memcpy( p_track->fmt.p_extra,
1232 p_sample->data.p_sample_vide->p_qt_image_description,
1233 p_track->fmt.i_extra);
1236 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1237 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1238 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1239 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1240 p_track->fmt.i_extra =
1241 p_sample->data.p_sample_soun->i_qt_description;
1242 if( p_track->fmt.i_extra > 0 )
1244 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1245 memcpy( p_track->fmt.p_extra,
1246 p_sample->data.p_sample_soun->p_qt_description,
1247 p_track->fmt.i_extra);
1251 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1252 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1254 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1258 /* Hack: use a packetizer to reecampsulate data in anexe B format */
1259 msg_Dbg( p_demux, "avcC: size=%d", p_avcC->data.p_avcC->i_avcC );
1260 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1261 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1262 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC, p_track->fmt.i_extra );
1263 p_track->fmt.b_packetized = VLC_FALSE;
1267 msg_Err( p_demux, "missing avcC" );
1279 /* some last initialisation */
1280 switch( p_track->fmt.i_cat )
1283 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1284 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1286 /* fall on display size */
1287 if( p_track->fmt.video.i_width <= 0 )
1288 p_track->fmt.video.i_width = p_track->i_width;
1289 if( p_track->fmt.video.i_height <= 0 )
1290 p_track->fmt.video.i_height = p_track->i_height;
1292 /* Find out apect ratio from display size */
1293 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1294 /* Work-around buggy muxed files */
1295 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1296 p_track->fmt.video.i_aspect =
1297 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1302 p_track->fmt.audio.i_channels =
1303 p_sample->data.p_sample_soun->i_channelcount;
1304 p_track->fmt.audio.i_rate =
1305 p_sample->data.p_sample_soun->i_sampleratehi;
1306 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1307 p_sample->data.p_sample_soun->i_sampleratehi *
1308 p_sample->data.p_sample_soun->i_samplesize;
1309 p_track->fmt.audio.i_bitspersample =
1310 p_sample->data.p_sample_soun->i_samplesize;
1317 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1322 /* given a time it return sample/chunk
1323 * it also update elst field of the track
1325 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1326 int64_t i_start, uint32_t *pi_chunk,
1327 uint32_t *pi_sample )
1329 demux_sys_t *p_sys = p_demux->p_sys;
1332 unsigned int i_sample;
1333 unsigned int i_chunk;
1336 /* FIXME see if it's needed to check p_track->i_chunk_count */
1337 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1339 return( VLC_EGENERIC );
1342 /* handle elst (find the correct one) */
1343 MP4_TrackSetELST( p_demux, p_track, i_start );
1344 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1346 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1347 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1349 /* now calculate i_start for this elst */
1351 i_start -= p_track->i_elst_time * I64C(1000000) / p_sys->i_timescale;
1359 /* to track time scale */
1360 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1361 /* add elst offset */
1362 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1363 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1364 elst->i_media_time[p_track->i_elst] > 0 )
1366 i_start += elst->i_media_time[p_track->i_elst];
1369 msg_Dbg( p_demux, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd
1370 "ms (track)", p_track->i_elst,
1371 i_mvt * 1000 / p_sys->i_timescale,
1372 i_start * 1000 / p_track->i_timescale );
1376 /* convert absolute time to in timescale unit */
1377 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1380 /* we start from sample 0/chunk 0, hope it won't take too much time */
1381 /* *** find good chunk *** */
1382 for( i_chunk = 0; ; i_chunk++ )
1384 if( i_chunk + 1 >= p_track->i_chunk_count )
1386 /* at the end and can't check if i_start in this chunk,
1387 it will be check while searching i_sample */
1388 i_chunk = p_track->i_chunk_count - 1;
1392 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1393 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1399 /* *** find sample in the chunk *** */
1400 i_sample = p_track->chunk[i_chunk].i_sample_first;
1401 i_dts = p_track->chunk[i_chunk].i_first_dts;
1402 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1405 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1406 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1409 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1410 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1412 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1417 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1421 i_sample += ( i_start - i_dts ) /
1422 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1427 if( i_sample >= p_track->i_sample_count )
1429 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1430 "(seeking too far) chunk=%d sample=%d",
1431 p_track->i_track_ID, i_chunk, i_sample );
1432 return( VLC_EGENERIC );
1436 /* *** Try to find nearest sync points *** */
1437 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1439 unsigned int i_index;
1441 "track[Id 0x%x] using Sync Sample Box (stss)",
1442 p_track->i_track_ID );
1443 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1445 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1449 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1451 p_stss->data.p_stss->i_sample_number[i_index-1] );
1452 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1453 /* new i_sample is less than old so i_chunk can only decreased */
1454 while( i_chunk > 0 &&
1455 i_sample < p_track->chunk[i_chunk].i_sample_first )
1462 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1464 p_stss->data.p_stss->i_sample_number[i_index] );
1465 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1466 /* new i_sample is more than old so i_chunk can only increased */
1467 while( i_chunk < p_track->i_chunk_count - 1 &&
1468 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1469 p_track->chunk[i_chunk].i_sample_count )
1480 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1481 "Sample Box (stss)", p_track->i_track_ID );
1484 *pi_chunk = i_chunk;
1485 *pi_sample = i_sample;
1490 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1491 unsigned int i_chunk, unsigned int i_sample )
1493 vlc_bool_t b_reselect = VLC_FALSE;
1495 /* now see if actual es is ok */
1496 if( p_track->i_chunk < 0 ||
1497 p_track->i_chunk >= p_track->i_chunk_count - 1 ||
1498 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1499 p_track->chunk[i_chunk].i_sample_description_index )
1501 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1502 p_track->i_track_ID );
1504 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1505 p_track->p_es, &b_reselect );
1507 es_out_Del( p_demux->out, p_track->p_es );
1509 p_track->p_es = NULL;
1511 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1513 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1514 p_track->i_track_ID );
1516 p_track->b_ok = VLC_FALSE;
1517 p_track->b_selected = VLC_FALSE;
1518 return VLC_EGENERIC;
1522 /* select again the new decoder */
1525 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1528 p_track->i_chunk = i_chunk;
1529 p_track->i_sample = i_sample;
1531 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1534 /****************************************************************************
1536 ****************************************************************************
1537 * Parse track information and create all needed data to run a track
1538 * If it succeed b_ok is set to 1 else to 0
1539 ****************************************************************************/
1540 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1541 MP4_Box_t *p_box_trak )
1543 demux_sys_t *p_sys = p_demux->p_sys;
1545 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1546 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1560 /* hint track unsuported */
1562 /* set default value (-> track unusable) */
1563 p_track->b_ok = VLC_FALSE;
1564 p_track->b_enable = VLC_FALSE;
1565 p_track->b_selected = VLC_FALSE;
1567 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1574 /* do we launch this track by default ? */
1576 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1578 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1579 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1580 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1584 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
1587 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1588 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1590 if( ( !p_mdhd )||( !p_hdlr ) )
1595 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1597 for( i = 0; i < 3; i++ )
1599 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1603 switch( p_hdlr->data.p_hdlr->i_handler_type )
1605 case( FOURCC_soun ):
1606 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1610 p_track->fmt.i_cat = AUDIO_ES;
1613 case( FOURCC_vide ):
1614 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1618 p_track->fmt.i_cat = VIDEO_ES;
1621 case( FOURCC_text ):
1622 case( FOURCC_subp ):
1623 p_track->fmt.i_cat = SPU_ES;
1630 p_track->i_elst = 0;
1631 p_track->i_elst_time = 0;
1632 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1634 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1637 msg_Warn( p_demux, "elst box found" );
1638 for( i = 0; i < elst->i_entry_count; i++ )
1640 msg_Dbg( p_demux, " - [%d] duration="I64Fd"ms media time="I64Fd"ms) rate=%d.%d",
1642 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1643 elst->i_media_time[i] >= 0 ?
1644 elst->i_media_time[i] * 1000 / p_track->i_timescale : -1,
1645 elst->i_media_rate_integer[i],
1646 elst->i_media_rate_fraction[i] );
1653 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1655 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1656 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1661 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1662 p_track->b_drms = p_drms != NULL;
1663 p_track->p_drms = p_track->b_drms ?
1664 p_drms->data.p_sample_soun->p_drms : NULL;
1667 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1669 p_track->fmt.psz_language = strdup( language );
1672 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1673 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1675 MP4_Box_t *p_sample;
1677 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1678 if( p_sample && p_sample->data.p_sample_soun)
1680 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1681 if( p_soun->i_qt_version == 0 &&
1682 p_track->i_timescale != p_soun->i_sampleratehi )
1685 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1687 "Making both equal. (report any problem)",
1688 p_track->i_timescale, p_soun->i_sampleratehi );
1690 if( p_soun->i_sampleratehi )
1691 p_track->i_timescale = p_soun->i_sampleratehi;
1693 p_soun->i_sampleratehi = p_track->i_timescale;
1698 /* Create chunk index table and sample index table */
1699 if( TrackCreateChunksIndex( p_demux,p_track ) ||
1700 TrackCreateSamplesIndex( p_demux, p_track ) )
1702 return; /* cannot create chunks index */
1705 p_track->i_chunk = 0;
1706 p_track->i_sample = 0;
1709 if( TrackCreateES( p_demux,
1710 p_track, p_track->i_chunk,
1713 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1714 p_track->i_track_ID );
1721 for( i = 0; i < p_track->i_chunk_count; i++ )
1723 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
1724 i, p_track->chunk[i].i_sample_count,
1725 p_track->chunk[i].i_first_dts );
1730 p_track->b_ok = VLC_TRUE;
1733 /****************************************************************************
1735 ****************************************************************************
1736 * Destroy a track created by MP4_TrackCreate.
1737 ****************************************************************************/
1738 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
1740 unsigned int i_chunk;
1742 p_track->b_ok = VLC_FALSE;
1743 p_track->b_enable = VLC_FALSE;
1744 p_track->b_selected = VLC_FALSE;
1746 es_format_Clean( &p_track->fmt );
1748 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1750 if( p_track->chunk )
1752 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1753 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1756 FREE( p_track->chunk );
1758 if( !p_track->i_sample_size )
1760 FREE( p_track->p_sample_size );
1764 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
1767 if( !p_track->b_ok )
1769 return VLC_EGENERIC;
1772 if( p_track->b_selected )
1774 msg_Warn( p_demux, "track[Id 0x%x] already selected",
1775 p_track->i_track_ID );
1779 return MP4_TrackSeek( p_demux, p_track, i_start );
1782 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
1784 if( !p_track->b_ok )
1789 if( !p_track->b_selected )
1791 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
1792 p_track->i_track_ID );
1797 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
1798 p_track->p_es, VLC_FALSE );
1801 p_track->b_selected = VLC_FALSE;
1804 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
1810 if( !p_track->b_ok )
1812 return( VLC_EGENERIC );
1815 p_track->b_selected = VLC_FALSE;
1817 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
1818 &i_chunk, &i_sample ) )
1820 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
1821 p_track->i_track_ID );
1822 return( VLC_EGENERIC );
1825 p_track->b_selected = VLC_TRUE;
1827 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
1830 p_track->b_selected = VLC_TRUE;
1832 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1837 * 3 types: for audio
1840 #define QT_V0_MAX_SAMPLES 1500
1841 static int MP4_TrackSampleSize( mp4_track_t *p_track )
1844 MP4_Box_data_sample_soun_t *p_soun;
1846 if( p_track->i_sample_size == 0 )
1848 /* most simple case */
1849 return( p_track->p_sample_size[p_track->i_sample] );
1851 if( p_track->fmt.i_cat != AUDIO_ES )
1853 return( p_track->i_sample_size );
1856 if( p_track->i_sample_size != 1 )
1858 //msg_Warn( p_demux, "SampleSize != 1" );
1859 return( p_track->i_sample_size );
1862 p_soun = p_track->p_sample->data.p_sample_soun;
1864 if( p_soun->i_qt_version == 1 )
1866 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1871 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1872 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1873 if( i_samples > QT_V0_MAX_SAMPLES )
1875 i_samples = QT_V0_MAX_SAMPLES;
1877 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1880 //fprintf( stderr, "size=%d\n", i_size );
1885 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
1887 unsigned int i_sample;
1891 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1893 if( p_track->i_sample_size )
1895 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1897 if( p_soun->i_qt_version == 0 )
1899 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1900 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1904 /* we read chunk by chunk */
1910 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1911 i_sample < p_track->i_sample; i_sample++ )
1913 i_pos += p_track->p_sample_size[i_sample];
1920 static int MP4_TrackNextSample( demux_t *p_demux,
1921 mp4_track_t *p_track )
1924 if( p_track->fmt.i_cat == AUDIO_ES &&
1925 p_track->i_sample_size != 0 )
1927 MP4_Box_data_sample_soun_t *p_soun;
1929 p_soun = p_track->p_sample->data.p_sample_soun;
1931 if( p_soun->i_qt_version == 1 )
1933 /* chunk by chunk */
1935 p_track->chunk[p_track->i_chunk].i_sample_first +
1936 p_track->chunk[p_track->i_chunk].i_sample_count;
1941 p_track->i_sample += QT_V0_MAX_SAMPLES;
1942 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 )
1945 p_track->chunk[p_track->i_chunk].i_sample_first +
1946 p_track->chunk[p_track->i_chunk].i_sample_count;
1952 p_track->i_sample++;
1955 if( p_track->i_sample >= p_track->i_sample_count )
1957 /* we have reach end of the track so free decoder stuff */
1958 msg_Warn( p_demux, "track[0x%x] will be disabled", p_track->i_track_ID );
1959 MP4_TrackUnselect( p_demux, p_track );
1960 return VLC_EGENERIC;
1963 /* Have we changed chunk ? */
1964 if( p_track->i_sample >=
1965 p_track->chunk[p_track->i_chunk].i_sample_first +
1966 p_track->chunk[p_track->i_chunk].i_sample_count )
1968 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
1969 p_track->i_sample ) )
1971 msg_Warn( p_demux, "track[0x%x] will be disabled "
1972 "(cannot restart decoder)", p_track->i_track_ID );
1973 MP4_TrackUnselect( p_demux, p_track );
1974 return VLC_EGENERIC;
1978 /* Have we changed elst */
1979 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1981 demux_sys_t *p_sys = p_demux->p_sys;
1982 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1983 int64_t i_mvt = MP4_TrackGetPTS( p_demux, p_track ) *
1984 p_sys->i_timescale / (int64_t)1000000;
1986 if( p_track->i_elst < elst->i_entry_count &&
1987 i_mvt >= p_track->i_elst_time +
1988 elst->i_segment_duration[p_track->i_elst] )
1990 MP4_TrackSetELST( p_demux, p_track,
1991 MP4_TrackGetPTS( p_demux, p_track ) );
1998 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2001 demux_sys_t *p_sys = p_demux->p_sys;
2002 int i_elst_last = tk->i_elst;
2004 /* handle elst (find the correct one) */
2006 tk->i_elst_time = 0;
2007 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2009 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2010 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2012 for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2014 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2016 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2020 tk->i_elst_time += i_dur;
2023 if( tk->i_elst >= elst->i_entry_count )
2025 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2026 tk->i_elst = elst->i_entry_count - 1;
2027 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2030 if( elst->i_media_time[tk->i_elst] < 0 )
2033 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2036 if( i_elst_last != tk->i_elst )
2038 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );