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 )
754 case FOURCC_0xa9nam: /* Full name */
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 );
770 case FOURCC_0xa9day: /* Creation Date */
771 vlc_meta_Add( meta, VLC_META_DATE,
772 p_0xa9xxx->data.p_0xa9xxx->psz_text );
774 case FOURCC_0xa9des: /* Description */
775 vlc_meta_Add( meta, VLC_META_DESCRIPTION,
776 p_0xa9xxx->data.p_0xa9xxx->psz_text );
778 case FOURCC_0xa9gen: /* Genre */
779 vlc_meta_Add( meta, VLC_META_GENRE,
780 p_0xa9xxx->data.p_0xa9xxx->psz_text );
784 case FOURCC_0xa9inf: /* Information */
785 case FOURCC_0xa9alb: /* Album */
786 case FOURCC_0xa9dir: /* Director */
787 case FOURCC_0xa9dis: /* Disclaimer */
788 case FOURCC_0xa9enc: /* Encoded By */
789 case FOURCC_0xa9trk: /* Track */
790 case FOURCC_0xa9cmt: /* Commment */
791 case FOURCC_0xa9url: /* URL */
792 case FOURCC_0xa9req: /* Requirements */
793 case FOURCC_0xa9fmt: /* Original Format */
794 case FOURCC_0xa9dsa: /* Display Source As */
795 case FOURCC_0xa9hst: /* Host Computer */
796 case FOURCC_0xa9prd: /* Producer */
797 case FOURCC_0xa9prf: /* Performers */
798 case FOURCC_0xa9ope: /* Original Performer */
799 case FOURCC_0xa9src: /* Providers Source Content */
800 case FOURCC_0xa9wrt: /* Writer */
801 case FOURCC_0xa9com: /* Composer */
802 case FOURCC_WLOC: /* Window Location */
803 /* TODO one day, but they aren't really meaningfull */
813 case DEMUX_GET_TITLE_INFO:
814 case DEMUX_SET_NEXT_DEMUX_TIME:
815 case DEMUX_SET_GROUP:
819 msg_Warn( p_demux, "control query unimplemented !!!" );
824 /*****************************************************************************
825 * Close: frees unused data
826 *****************************************************************************/
827 static void Close ( vlc_object_t * p_this )
829 unsigned int i_track;
830 demux_t * p_demux = (demux_t *)p_this;
831 demux_sys_t *p_sys = p_demux->p_sys;
833 msg_Dbg( p_demux, "freeing all memory" );
835 MP4_BoxFree( p_demux->s, p_sys->p_root );
836 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
838 MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
840 FREE( p_sys->track );
847 /****************************************************************************
848 * Local functions, specific to vlc
849 ****************************************************************************/
851 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
852 static int TrackCreateChunksIndex( demux_t *p_demux,
853 mp4_track_t *p_demux_track )
855 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
858 unsigned int i_chunk;
859 unsigned int i_index, i_last;
861 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
862 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
863 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
865 return( VLC_EGENERIC );
868 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
869 if( !p_demux_track->i_chunk_count )
871 msg_Warn( p_demux, "no chunk defined" );
872 return( VLC_EGENERIC );
874 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
875 sizeof( mp4_chunk_t ) );
877 /* first we read chunk offset */
878 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
880 p_demux_track->chunk[i_chunk].i_offset =
881 p_co64->data.p_co64->i_chunk_offset[i_chunk];
884 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
885 to be used for the sample XXX begin to 1
886 We construct it begining at the end */
887 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
888 i_index = p_stsc->data.p_stsc->i_entry_count;
891 msg_Warn( p_demux, "cannot read chunk table or table empty" );
892 return( VLC_EGENERIC );
898 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
899 i_chunk < i_last; i_chunk++ )
901 p_demux_track->chunk[i_chunk].i_sample_description_index =
902 p_stsc->data.p_stsc->i_sample_description_index[i_index];
903 p_demux_track->chunk[i_chunk].i_sample_count =
904 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
906 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
909 p_demux_track->chunk[0].i_sample_first = 0;
910 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
912 p_demux_track->chunk[i_chunk].i_sample_first =
913 p_demux_track->chunk[i_chunk-1].i_sample_first +
914 p_demux_track->chunk[i_chunk-1].i_sample_count;
918 "track[Id 0x%x] read %d chunk",
919 p_demux_track->i_track_ID,
920 p_demux_track->i_chunk_count );
922 return( VLC_SUCCESS );
924 static int TrackCreateSamplesIndex( demux_t *p_demux,
925 mp4_track_t *p_demux_track )
927 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
928 ctts make same mapping but for composition time,
929 not yet used and probably not usefull */
930 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
931 that uses a compressed form FIXME make them in libmp4
933 /* TODO use also stss and stsh table for seeking */
934 /* FIXME use edit table */
939 int64_t i_index_sample_used;
943 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
944 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
946 if( ( !p_stts )||( !p_stsz ) )
948 msg_Warn( p_demux, "cannot read sample table" );
949 return( VLC_EGENERIC );
952 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
955 /* for sample size, there are 2 case */
956 if( p_stsz->data.p_stsz->i_sample_size )
958 /* 1: all sample have the same size, so no need to construct a table */
959 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
960 p_demux_track->p_sample_size = NULL;
964 /* 2: each sample can have a different size */
965 p_demux_track->i_sample_size = 0;
966 p_demux_track->p_sample_size =
967 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
969 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
971 p_demux_track->p_sample_size[i_sample] =
972 p_stsz->data.p_stsz->i_entry_size[i_sample];
975 /* we have extract all information from stsz,
978 /* if we don't want to waste too much memory, we can't expand
979 the box !, so each chunk will contain an "extract" of this table
983 i_index = 0; i_index_sample_used =0;
985 /* create and init last data for each chunk */
986 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
989 int64_t i_entry, i_sample_count, i;
991 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
992 /* count how many entries needed for this chunk
993 for p_sample_delta_dts and p_sample_count_dts */
995 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
998 while( i_sample_count > 0 )
1000 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
1003 i_sample_count += i_index_sample_used; /* don't count already used sample
1010 p_demux_track->chunk[i_chunk].p_sample_count_dts =
1011 calloc( i_entry, sizeof( uint32_t ) );
1012 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
1013 calloc( i_entry, sizeof( uint32_t ) );
1016 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
1017 for( i = 0; i < i_entry; i++ )
1022 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
1024 i_used = __MIN( i_rest, i_sample_count );
1026 i_index_sample_used += i_used;
1027 i_sample_count -= i_used;
1029 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
1031 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
1032 p_stts->data.p_stts->i_sample_delta[i_index];
1034 i_last_dts += i_used *
1035 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
1037 if( i_index_sample_used >=
1038 p_stts->data.p_stts->i_sample_count[i_index] )
1042 i_index_sample_used = 0;
1048 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:"I64Fd"s",
1049 p_demux_track->i_track_ID,
1050 p_demux_track->i_sample_count,
1051 i_last_dts / p_demux_track->i_timescale );
1053 return( VLC_SUCCESS );
1058 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1060 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1061 unsigned int i_chunk, es_out_id_t **pp_es )
1063 MP4_Box_t *p_sample;
1068 if( !p_track->chunk[i_chunk].i_sample_description_index )
1071 "invalid SampleEntry index (track[Id 0x%x])",
1072 p_track->i_track_ID );
1073 return VLC_EGENERIC;
1076 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1077 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1079 if( !p_sample || ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1082 "cannot find SampleEntry (track[Id 0x%x])",
1083 p_track->i_track_ID );
1084 return( VLC_EGENERIC );
1087 p_track->p_sample = p_sample;
1089 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1091 MP4_Box_data_sample_soun_t *p_soun;
1093 p_soun = p_sample->data.p_sample_soun;
1095 if( p_soun->i_qt_version == 0 )
1097 switch( p_sample->i_type )
1099 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1100 p_soun->i_qt_version = 1;
1101 p_soun->i_sample_per_packet = 64;
1102 p_soun->i_bytes_per_packet = 34;
1103 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1104 p_soun->i_bytes_per_sample = 2;
1106 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1107 p_soun->i_qt_version = 1;
1108 p_soun->i_sample_per_packet = 6;
1109 p_soun->i_bytes_per_packet = 2;
1110 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1111 p_soun->i_bytes_per_sample = 2;
1113 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1114 p_soun->i_qt_version = 1;
1115 p_soun->i_sample_per_packet = 12;
1116 p_soun->i_bytes_per_packet = 2;
1117 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1118 p_soun->i_bytes_per_sample = 2;
1120 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1121 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1122 p_soun->i_samplesize = 8;
1129 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1131 p_soun->i_qt_version = 0;
1136 /* It's a little ugly but .. there are special cases */
1137 switch( p_sample->i_type )
1139 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1140 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1141 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1143 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1144 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1146 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1147 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1150 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1151 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1152 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1153 /* FIXME UTF-8 doesn't work here ? */
1154 /* p_track->fmt.subs.psz_encoding = strdup( "UTF-8" ); */
1158 p_track->fmt.i_codec = p_sample->i_type;
1162 /* now see if esds is present and if so create a data packet
1163 with decoder_specific_info */
1164 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1165 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1166 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1167 ( p_esds->data.p_esds )&&
1170 /* First update information based on i_objectTypeIndication */
1171 switch( p_decconfig->i_objectTypeIndication )
1173 case( 0x20 ): /* MPEG4 VIDEO */
1174 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1177 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1184 case( 0x65): /* MPEG2 video */
1185 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1187 /* Theses are MPEG2-AAC */
1188 case( 0x66): /* main profile */
1189 case( 0x67): /* Low complexity profile */
1190 case( 0x68): /* Scaleable Sampling rate profile */
1191 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1193 /* true MPEG 2 audio */
1195 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1197 case( 0x6a): /* MPEG1 video */
1198 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1200 case( 0x6b): /* MPEG1 audio */
1201 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1203 case( 0x6c ): /* jpeg */
1204 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1208 case( 0xe0 ): /* NeroDigital: dvd subs */
1209 if( p_track->fmt.i_cat == SPU_ES )
1211 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1216 /* Unknown entry, but don't touch i_fourcc */
1218 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1219 p_decconfig->i_objectTypeIndication,
1220 p_track->i_track_ID );
1223 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1224 if( p_track->fmt.i_extra > 0 )
1226 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1227 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1228 p_track->fmt.i_extra );
1233 switch( p_sample->i_type )
1235 /* qt decoder, send the complete chunk */
1236 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1237 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1238 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1239 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1240 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1241 p_track->fmt.i_extra =
1242 p_sample->data.p_sample_vide->i_qt_image_description;
1243 if( p_track->fmt.i_extra > 0 )
1245 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1246 memcpy( p_track->fmt.p_extra,
1247 p_sample->data.p_sample_vide->p_qt_image_description,
1248 p_track->fmt.i_extra);
1251 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1252 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1253 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1254 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1255 p_track->fmt.i_extra =
1256 p_sample->data.p_sample_soun->i_qt_description;
1257 if( p_track->fmt.i_extra > 0 )
1259 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1260 memcpy( p_track->fmt.p_extra,
1261 p_sample->data.p_sample_soun->p_qt_description,
1262 p_track->fmt.i_extra);
1266 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1267 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1269 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1273 /* Hack: use a packetizer to reecampsulate data in anexe B format */
1274 msg_Dbg( p_demux, "avcC: size=%d", p_avcC->data.p_avcC->i_avcC );
1275 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1276 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1277 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC, p_track->fmt.i_extra );
1278 p_track->fmt.b_packetized = VLC_FALSE;
1282 msg_Err( p_demux, "missing avcC" );
1294 /* some last initialisation */
1295 switch( p_track->fmt.i_cat )
1298 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1299 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1301 /* fall on display size */
1302 if( p_track->fmt.video.i_width <= 0 )
1303 p_track->fmt.video.i_width = p_track->i_width;
1304 if( p_track->fmt.video.i_height <= 0 )
1305 p_track->fmt.video.i_height = p_track->i_height;
1307 /* Find out apect ratio from display size */
1308 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1309 /* Work-around buggy muxed files */
1310 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1311 p_track->fmt.video.i_aspect =
1312 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1317 p_track->fmt.audio.i_channels =
1318 p_sample->data.p_sample_soun->i_channelcount;
1319 p_track->fmt.audio.i_rate =
1320 p_sample->data.p_sample_soun->i_sampleratehi;
1321 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1322 p_sample->data.p_sample_soun->i_sampleratehi *
1323 p_sample->data.p_sample_soun->i_samplesize;
1324 p_track->fmt.audio.i_bitspersample =
1325 p_sample->data.p_sample_soun->i_samplesize;
1332 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1337 /* given a time it return sample/chunk
1338 * it also update elst field of the track
1340 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1341 int64_t i_start, uint32_t *pi_chunk,
1342 uint32_t *pi_sample )
1344 demux_sys_t *p_sys = p_demux->p_sys;
1347 unsigned int i_sample;
1348 unsigned int i_chunk;
1351 /* FIXME see if it's needed to check p_track->i_chunk_count */
1352 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1354 return( VLC_EGENERIC );
1357 /* handle elst (find the correct one) */
1358 MP4_TrackSetELST( p_demux, p_track, i_start );
1359 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1361 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1362 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1364 /* now calculate i_start for this elst */
1366 i_start -= p_track->i_elst_time * I64C(1000000) / p_sys->i_timescale;
1374 /* to track time scale */
1375 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1376 /* add elst offset */
1377 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1378 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1379 elst->i_media_time[p_track->i_elst] > 0 )
1381 i_start += elst->i_media_time[p_track->i_elst];
1384 msg_Dbg( p_demux, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd
1385 "ms (track)", p_track->i_elst,
1386 i_mvt * 1000 / p_sys->i_timescale,
1387 i_start * 1000 / p_track->i_timescale );
1391 /* convert absolute time to in timescale unit */
1392 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1395 /* we start from sample 0/chunk 0, hope it won't take too much time */
1396 /* *** find good chunk *** */
1397 for( i_chunk = 0; ; i_chunk++ )
1399 if( i_chunk + 1 >= p_track->i_chunk_count )
1401 /* at the end and can't check if i_start in this chunk,
1402 it will be check while searching i_sample */
1403 i_chunk = p_track->i_chunk_count - 1;
1407 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1408 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1414 /* *** find sample in the chunk *** */
1415 i_sample = p_track->chunk[i_chunk].i_sample_first;
1416 i_dts = p_track->chunk[i_chunk].i_first_dts;
1417 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1420 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1421 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1424 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1425 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1427 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1432 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1436 i_sample += ( i_start - i_dts ) /
1437 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1442 if( i_sample >= p_track->i_sample_count )
1444 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1445 "(seeking too far) chunk=%d sample=%d",
1446 p_track->i_track_ID, i_chunk, i_sample );
1447 return( VLC_EGENERIC );
1451 /* *** Try to find nearest sync points *** */
1452 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1454 unsigned int i_index;
1456 "track[Id 0x%x] using Sync Sample Box (stss)",
1457 p_track->i_track_ID );
1458 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1460 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1464 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1466 p_stss->data.p_stss->i_sample_number[i_index-1] );
1467 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1468 /* new i_sample is less than old so i_chunk can only decreased */
1469 while( i_chunk > 0 &&
1470 i_sample < p_track->chunk[i_chunk].i_sample_first )
1477 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1479 p_stss->data.p_stss->i_sample_number[i_index] );
1480 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1481 /* new i_sample is more than old so i_chunk can only increased */
1482 while( i_chunk < p_track->i_chunk_count - 1 &&
1483 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1484 p_track->chunk[i_chunk].i_sample_count )
1495 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1496 "Sample Box (stss)", p_track->i_track_ID );
1499 *pi_chunk = i_chunk;
1500 *pi_sample = i_sample;
1505 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1506 unsigned int i_chunk, unsigned int i_sample )
1508 vlc_bool_t b_reselect = VLC_FALSE;
1510 /* now see if actual es is ok */
1511 if( p_track->i_chunk < 0 ||
1512 p_track->i_chunk >= p_track->i_chunk_count - 1 ||
1513 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1514 p_track->chunk[i_chunk].i_sample_description_index )
1516 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1517 p_track->i_track_ID );
1519 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1520 p_track->p_es, &b_reselect );
1522 es_out_Del( p_demux->out, p_track->p_es );
1524 p_track->p_es = NULL;
1526 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1528 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1529 p_track->i_track_ID );
1531 p_track->b_ok = VLC_FALSE;
1532 p_track->b_selected = VLC_FALSE;
1533 return VLC_EGENERIC;
1537 /* select again the new decoder */
1540 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1543 p_track->i_chunk = i_chunk;
1544 p_track->i_sample = i_sample;
1546 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1549 /****************************************************************************
1551 ****************************************************************************
1552 * Parse track information and create all needed data to run a track
1553 * If it succeed b_ok is set to 1 else to 0
1554 ****************************************************************************/
1555 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1556 MP4_Box_t *p_box_trak )
1558 demux_sys_t *p_sys = p_demux->p_sys;
1560 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1561 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1576 /* hint track unsuported */
1578 /* set default value (-> track unusable) */
1579 p_track->b_ok = VLC_FALSE;
1580 p_track->b_enable = VLC_FALSE;
1581 p_track->b_selected = VLC_FALSE;
1583 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1590 /* do we launch this track by default ? */
1592 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1594 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1595 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1596 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1600 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
1603 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1604 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1606 if( ( !p_mdhd )||( !p_hdlr ) )
1611 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1613 for( i = 0; i < 3; i++ )
1615 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1619 switch( p_hdlr->data.p_hdlr->i_handler_type )
1621 case( FOURCC_soun ):
1622 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1626 p_track->fmt.i_cat = AUDIO_ES;
1629 case( FOURCC_vide ):
1630 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1634 p_track->fmt.i_cat = VIDEO_ES;
1637 case( FOURCC_text ):
1638 case( FOURCC_subp ):
1639 p_track->fmt.i_cat = SPU_ES;
1646 p_track->i_elst = 0;
1647 p_track->i_elst_time = 0;
1648 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1650 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1653 msg_Warn( p_demux, "elst box found" );
1654 for( i = 0; i < elst->i_entry_count; i++ )
1656 msg_Dbg( p_demux, " - [%d] duration="I64Fd"ms media time="I64Fd"ms) rate=%d.%d",
1658 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1659 elst->i_media_time[i] >= 0 ?
1660 elst->i_media_time[i] * 1000 / p_track->i_timescale : -1,
1661 elst->i_media_rate_integer[i],
1662 elst->i_media_rate_fraction[i] );
1669 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1671 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1672 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1677 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1678 p_track->b_drms = p_drms != NULL;
1679 p_track->p_drms = p_track->b_drms ?
1680 p_drms->data.p_sample_soun->p_drms : NULL;
1683 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1685 p_track->fmt.psz_language = strdup( language );
1688 p_udta = MP4_BoxGet( p_box_trak, "udta" );
1691 MP4_Box_t *p_0xa9xxx;
1692 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
1693 p_0xa9xxx = p_0xa9xxx->p_next )
1695 switch( p_0xa9xxx->i_type )
1697 case FOURCC_0xa9nam:
1698 p_track->fmt.psz_description = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
1704 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1705 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1707 MP4_Box_t *p_sample;
1709 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1710 if( p_sample && p_sample->data.p_sample_soun)
1712 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1713 if( p_soun->i_qt_version == 0 &&
1714 p_track->i_timescale != p_soun->i_sampleratehi )
1717 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1719 "Making both equal. (report any problem)",
1720 p_track->i_timescale, p_soun->i_sampleratehi );
1722 if( p_soun->i_sampleratehi )
1723 p_track->i_timescale = p_soun->i_sampleratehi;
1725 p_soun->i_sampleratehi = p_track->i_timescale;
1730 /* Create chunk index table and sample index table */
1731 if( TrackCreateChunksIndex( p_demux,p_track ) ||
1732 TrackCreateSamplesIndex( p_demux, p_track ) )
1734 return; /* cannot create chunks index */
1737 p_track->i_chunk = 0;
1738 p_track->i_sample = 0;
1741 if( TrackCreateES( p_demux,
1742 p_track, p_track->i_chunk,
1745 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1746 p_track->i_track_ID );
1753 for( i = 0; i < p_track->i_chunk_count; i++ )
1755 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
1756 i, p_track->chunk[i].i_sample_count,
1757 p_track->chunk[i].i_first_dts );
1762 p_track->b_ok = VLC_TRUE;
1765 /****************************************************************************
1767 ****************************************************************************
1768 * Destroy a track created by MP4_TrackCreate.
1769 ****************************************************************************/
1770 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
1772 unsigned int i_chunk;
1774 p_track->b_ok = VLC_FALSE;
1775 p_track->b_enable = VLC_FALSE;
1776 p_track->b_selected = VLC_FALSE;
1778 es_format_Clean( &p_track->fmt );
1780 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1782 if( p_track->chunk )
1784 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1785 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1788 FREE( p_track->chunk );
1790 if( !p_track->i_sample_size )
1792 FREE( p_track->p_sample_size );
1796 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
1799 if( !p_track->b_ok )
1801 return VLC_EGENERIC;
1804 if( p_track->b_selected )
1806 msg_Warn( p_demux, "track[Id 0x%x] already selected",
1807 p_track->i_track_ID );
1811 return MP4_TrackSeek( p_demux, p_track, i_start );
1814 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
1816 if( !p_track->b_ok )
1821 if( !p_track->b_selected )
1823 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
1824 p_track->i_track_ID );
1829 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
1830 p_track->p_es, VLC_FALSE );
1833 p_track->b_selected = VLC_FALSE;
1836 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
1842 if( !p_track->b_ok )
1844 return( VLC_EGENERIC );
1847 p_track->b_selected = VLC_FALSE;
1849 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
1850 &i_chunk, &i_sample ) )
1852 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
1853 p_track->i_track_ID );
1854 return( VLC_EGENERIC );
1857 p_track->b_selected = VLC_TRUE;
1859 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
1862 p_track->b_selected = VLC_TRUE;
1864 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1869 * 3 types: for audio
1872 #define QT_V0_MAX_SAMPLES 1500
1873 static int MP4_TrackSampleSize( mp4_track_t *p_track )
1876 MP4_Box_data_sample_soun_t *p_soun;
1878 if( p_track->i_sample_size == 0 )
1880 /* most simple case */
1881 return( p_track->p_sample_size[p_track->i_sample] );
1883 if( p_track->fmt.i_cat != AUDIO_ES )
1885 return( p_track->i_sample_size );
1888 if( p_track->i_sample_size != 1 )
1890 //msg_Warn( p_demux, "SampleSize != 1" );
1891 return( p_track->i_sample_size );
1894 p_soun = p_track->p_sample->data.p_sample_soun;
1896 if( p_soun->i_qt_version == 1 )
1898 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1903 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1904 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1905 if( i_samples > QT_V0_MAX_SAMPLES )
1907 i_samples = QT_V0_MAX_SAMPLES;
1909 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1912 //fprintf( stderr, "size=%d\n", i_size );
1917 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
1919 unsigned int i_sample;
1923 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1925 if( p_track->i_sample_size )
1927 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1929 if( p_soun->i_qt_version == 0 )
1931 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1932 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1936 /* we read chunk by chunk */
1942 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1943 i_sample < p_track->i_sample; i_sample++ )
1945 i_pos += p_track->p_sample_size[i_sample];
1952 static int MP4_TrackNextSample( demux_t *p_demux,
1953 mp4_track_t *p_track )
1956 if( p_track->fmt.i_cat == AUDIO_ES &&
1957 p_track->i_sample_size != 0 )
1959 MP4_Box_data_sample_soun_t *p_soun;
1961 p_soun = p_track->p_sample->data.p_sample_soun;
1963 if( p_soun->i_qt_version == 1 )
1965 /* chunk by chunk */
1967 p_track->chunk[p_track->i_chunk].i_sample_first +
1968 p_track->chunk[p_track->i_chunk].i_sample_count;
1973 p_track->i_sample += QT_V0_MAX_SAMPLES;
1974 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 )
1977 p_track->chunk[p_track->i_chunk].i_sample_first +
1978 p_track->chunk[p_track->i_chunk].i_sample_count;
1984 p_track->i_sample++;
1987 if( p_track->i_sample >= p_track->i_sample_count )
1989 /* we have reach end of the track so free decoder stuff */
1990 msg_Warn( p_demux, "track[0x%x] will be disabled", p_track->i_track_ID );
1991 MP4_TrackUnselect( p_demux, p_track );
1992 return VLC_EGENERIC;
1995 /* Have we changed chunk ? */
1996 if( p_track->i_sample >=
1997 p_track->chunk[p_track->i_chunk].i_sample_first +
1998 p_track->chunk[p_track->i_chunk].i_sample_count )
2000 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2001 p_track->i_sample ) )
2003 msg_Warn( p_demux, "track[0x%x] will be disabled "
2004 "(cannot restart decoder)", p_track->i_track_ID );
2005 MP4_TrackUnselect( p_demux, p_track );
2006 return VLC_EGENERIC;
2010 /* Have we changed elst */
2011 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2013 demux_sys_t *p_sys = p_demux->p_sys;
2014 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2015 int64_t i_mvt = MP4_TrackGetPTS( p_demux, p_track ) *
2016 p_sys->i_timescale / (int64_t)1000000;
2018 if( p_track->i_elst < elst->i_entry_count &&
2019 i_mvt >= p_track->i_elst_time +
2020 elst->i_segment_duration[p_track->i_elst] )
2022 MP4_TrackSetELST( p_demux, p_track,
2023 MP4_TrackGetPTS( p_demux, p_track ) );
2030 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2033 demux_sys_t *p_sys = p_demux->p_sys;
2034 int i_elst_last = tk->i_elst;
2036 /* handle elst (find the correct one) */
2038 tk->i_elst_time = 0;
2039 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2041 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2042 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2044 for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2046 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2048 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2052 tk->i_elst_time += i_dur;
2055 if( tk->i_elst >= elst->i_entry_count )
2057 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2058 tk->i_elst = elst->i_entry_count - 1;
2059 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2062 if( elst->i_media_time[tk->i_elst] < 0 )
2065 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2068 if( i_elst_last != tk->i_elst )
2070 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );