1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc_demux.h>
33 #include <vlc_playlist.h>
35 #include <vlc_charset.h>
43 #define uint64_t int64_t
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
53 set_category( CAT_INPUT );
54 set_subcategory( SUBCAT_INPUT_DEMUX );
55 set_description( _("MP4 stream demuxer") );
56 set_capability( "demux2", 242 );
57 set_callbacks( Open, Close );
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Demux ( demux_t * );
64 static int DemuxRef( demux_t *p_demux ){ return 0;}
65 static int Seek ( demux_t *, mtime_t );
66 static int Control ( demux_t *, int, va_list );
68 /* Contain all information about a chunk */
71 uint64_t i_offset; /* absolute position of this chunk in the file */
72 uint32_t i_sample_description_index; /* index for SampleEntry to use */
73 uint32_t i_sample_count; /* how many samples in this chunk */
74 uint32_t i_sample_first; /* index of the first sample in this chunk */
76 /* now provide way to calculate pts, dts, and offset without to
77 much memory and with fast acces */
79 /* with this we can calculate dts/pts without waste memory */
81 uint32_t *p_sample_count_dts;
82 uint32_t *p_sample_delta_dts; /* dts delta */
84 uint32_t *p_sample_count_pts;
85 int32_t *p_sample_offset_pts; /* pts-dts */
87 /* TODO if needed add pts
88 but quickly *add* support for edts and seeking */
92 /* Contain all needed information for read all track with vlc */
95 int i_track_ID; /* this should be unique */
97 int b_ok; /* The track is usable */
98 int b_enable; /* is the trak enable by default */
99 vlc_bool_t b_selected; /* is the trak being played */
104 /* display size only ! */
108 /* more internal data */
109 uint64_t i_timescale; /* time scale for this track only */
112 int i_elst; /* current elst */
113 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
114 MP4_Box_t *p_elst; /* elst (could be NULL) */
116 /* give the next sample to read, i_chunk is to find quickly where
117 the sample is located */
118 uint32_t i_sample; /* next sample to read */
119 uint32_t i_chunk; /* chunk where next sample is stored */
120 /* total count of chunk and sample */
121 uint32_t i_chunk_count;
122 uint32_t i_sample_count;
124 mp4_chunk_t *chunk; /* always defined for each chunk */
126 /* sample size, p_sample_size defined only if i_sample_size == 0
127 else i_sample_size is size for all sample */
128 uint32_t i_sample_size;
129 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
130 too much time to do sumations each time*/
132 MP4_Box_t *p_stbl; /* will contain all timing information */
133 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
134 MP4_Box_t *p_sample;/* point on actual sdsd */
144 MP4_Box_t *p_root; /* container for the whole file */
148 uint64_t i_time; /* time position of the presentation
149 * in movie timescale */
150 uint64_t i_timescale; /* movie time scale */
151 uint64_t i_duration; /* movie duration */
152 unsigned int i_tracks; /* number of tracks */
153 mp4_track_t *track; /* array of track */
156 /*****************************************************************************
157 * Declaration of local function
158 *****************************************************************************/
159 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *);
160 static void MP4_TrackDestroy( demux_t *, mp4_track_t * );
162 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
163 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
165 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
167 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
168 static int MP4_TrackSampleSize( mp4_track_t * );
169 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
170 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
172 /* Return time in s of a track */
173 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
175 #define chunk p_track->chunk[p_track->i_chunk]
177 unsigned int i_index = 0;
178 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
179 int64_t i_dts = chunk.i_first_dts;
181 while( i_sample > 0 )
183 if( i_sample > chunk.p_sample_count_dts[i_index] )
185 i_dts += chunk.p_sample_count_dts[i_index] *
186 chunk.p_sample_delta_dts[i_index];
187 i_sample -= chunk.p_sample_count_dts[i_index];
192 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
200 /* now handle elst */
201 if( p_track->p_elst )
203 demux_sys_t *p_sys = p_demux->p_sys;
204 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
206 /* convert to offset */
207 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
208 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
209 elst->i_media_time[p_track->i_elst] > 0 )
211 i_dts -= elst->i_media_time[p_track->i_elst];
214 /* add i_elst_time */
215 i_dts += p_track->i_elst_time * p_track->i_timescale /
218 if( i_dts < 0 ) i_dts = 0;
221 return I64C(1000000) * i_dts / p_track->i_timescale;
224 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
226 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
227 unsigned int i_index = 0;
228 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
230 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
233 for( i_index = 0;; i_index++ )
235 if( i_sample < ck->p_sample_count_pts[i_index] )
236 return ck->p_sample_offset_pts[i_index] * I64C(1000000) /
237 (int64_t)p_track->i_timescale;
239 i_sample -= ck->p_sample_count_pts[i_index];
243 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
245 return I64C(1000000) * p_sys->i_time / p_sys->i_timescale;
248 /* Function to lookup the currently playing item */
249 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
250 playlist_item_t **pp_item );
252 /*****************************************************************************
253 * Open: check file and initializes MP4 structures
254 *****************************************************************************/
255 static int Open( vlc_object_t * p_this )
257 demux_t *p_demux = (demux_t *)p_this;
268 vlc_bool_t b_seekable;
270 /* A little test to see if it could be a mp4 */
271 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
273 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
284 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
291 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
294 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
298 /*Set exported functions */
299 p_demux->pf_demux = Demux;
300 p_demux->pf_control = Control;
302 /* create our structure that will contains all data */
303 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
304 memset( p_sys, 0, sizeof( demux_sys_t ) );
306 /* Now load all boxes ( except raw data ) */
307 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
309 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
313 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
315 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
317 switch( p_ftyp->data.p_ftyp->i_major_brand )
321 "ISO Media file (isom) version %d.",
322 p_ftyp->data.p_ftyp->i_minor_version );
326 "unrecognized major file specification (%4.4s).",
327 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
333 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
336 /* the file need to have one moov box */
337 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
339 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
343 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
346 /* we have a free box as a moov, rename it */
347 p_foov->i_type = FOURCC_moov;
350 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
352 playlist_t *p_playlist;
353 playlist_item_t *p_current, *p_item_in_category;
354 int i_count = MP4_BoxCount( p_rmra, "rmda" );
356 vlc_bool_t b_play = VLC_FALSE;
358 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
361 (playlist_t *)vlc_object_find( p_demux,
366 b_play = FindItem( p_demux, p_playlist, &p_current );
367 p_item_in_category = playlist_ItemToNode( p_playlist, p_current, VLC_TRUE );
368 p_current->p_input->i_type = ITEM_TYPE_PLAYLIST;
370 for( i = 0; i < i_count; i++ )
372 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
376 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
380 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
382 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
383 psz_ref, (char*)&i_ref_type );
385 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
387 if( strstr( psz_ref, "qt5gateQT" ) )
389 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
392 if( !strncmp( psz_ref, "http://", 7 ) ||
393 !strncmp( psz_ref, "rtsp://", 7 ) )
400 char *psz_path = strdup( p_demux->psz_path );
401 char *end = strrchr( psz_path, '/' );
402 if( end ) end[1] = '\0';
403 else *psz_path = '\0';
405 asprintf( &psz_absolute, "%s://%s%s",
406 p_demux->psz_access, psz_path, psz_ref );
408 if( psz_ref ) free( psz_ref );
409 psz_ref = psz_absolute;
414 input_item_t *p_input;
415 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
416 p_input = input_ItemNewExt( p_playlist, psz_ref, NULL,
418 input_ItemCopyOptions( p_current->p_input, p_input );
419 playlist_BothAddInput( p_playlist, p_input,
421 PLAYLIST_APPEND, PLAYLIST_END,
427 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
428 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
430 if( psz_ref ) free( psz_ref );
432 if( b_play && p_item_in_category &&
433 p_item_in_category->i_children > 0 )
435 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
436 p_item_in_category, NULL );
438 vlc_object_release( p_playlist );
442 msg_Err( p_demux, "can't find playlist" );
446 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
450 msg_Err( p_demux, "cannot find /moov/mvhd" );
455 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
456 p_demux->pf_demux = DemuxRef;
462 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
463 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
466 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
468 msg_Err( p_demux, "cannot find any /moov/trak" );
471 msg_Dbg( p_demux, "find %d track%c",
473 p_sys->i_tracks ? 's':' ' );
475 /* allocate memory */
476 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
477 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
479 /* now process each track and extract all usefull information */
480 for( i = 0; i < p_sys->i_tracks; i++ )
482 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
483 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
485 if( p_sys->track[i].b_ok )
488 switch( p_sys->track[i].fmt.i_cat )
497 psz_cat = "subtitle";
505 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
506 p_sys->track[i].i_track_ID, psz_cat,
507 p_sys->track[i].b_enable ? "enable":"disable",
508 p_sys->track[i].fmt.psz_language ?
509 p_sys->track[i].fmt.psz_language : "undef" );
513 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
514 p_sys->track[i].i_track_ID );
523 MP4_BoxFree( p_demux->s, p_sys->p_root );
529 /*****************************************************************************
530 * Demux: read packet and send them to decoders
531 *****************************************************************************
532 * TODO check for newly selected track (ie audio upt to now )
533 *****************************************************************************/
534 static int Demux( demux_t *p_demux )
536 demux_sys_t *p_sys = p_demux->p_sys;
537 unsigned int i_track;
540 unsigned int i_track_selected;
542 /* check for newly selected/unselected track */
543 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
546 mp4_track_t *tk = &p_sys->track[i_track];
550 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
555 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
557 if( tk->b_selected && !b )
559 MP4_TrackUnselect( p_demux, tk );
561 else if( !tk->b_selected && b)
563 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
572 if( i_track_selected <= 0 )
574 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
575 if( p_sys->i_timescale > 0 )
577 int64_t i_length = (mtime_t)1000000 *
578 (mtime_t)p_sys->i_duration /
579 (mtime_t)p_sys->i_timescale;
580 if( MP4_GetMoviePTS( p_sys ) >= i_length )
585 msg_Warn( p_demux, "no track selected, exiting..." );
589 /* first wait for the good time to read a packet */
590 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
592 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
594 /* we will read 100ms for each stream so ...*/
595 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
597 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
599 mp4_track_t *tk = &p_sys->track[i_track];
601 if( !tk->b_ok || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
606 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
609 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
610 MP4_TrackGetDTS( p_demux, tk ),
611 MP4_GetMoviePTS( p_sys ) );
614 if( MP4_TrackSampleSize( tk ) > 0 )
620 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
622 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
624 MP4_TrackUnselect( p_demux, tk );
630 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
632 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
634 MP4_TrackUnselect( p_demux, tk );
638 if( tk->b_drms && tk->p_drms )
640 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
643 else if( tk->fmt.i_cat == SPU_ES )
645 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
646 p_block->i_buffer >= 2 )
648 uint16_t i_size = GetWBE( p_block->p_buffer );
650 if( i_size + 2 <= p_block->i_buffer )
653 /* remove the length field, and append a '\0' */
654 memmove( &p_block->p_buffer[0],
655 &p_block->p_buffer[2], i_size );
656 p_block->p_buffer[i_size] = '\0';
657 p_block->i_buffer = i_size + 1;
659 /* convert \r -> \n */
660 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
668 p_block->i_buffer = 0;
673 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
675 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
677 p_block->i_pts = p_block->i_dts + i_delta;
678 else if( tk->fmt.i_cat != VIDEO_ES )
679 p_block->i_pts = p_block->i_dts;
683 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
684 es_out_Send( p_demux->out, tk->p_es, p_block );
688 if( MP4_TrackNextSample( p_demux, tk ) )
697 /*****************************************************************************
698 * Seek: Got to i_date
699 ******************************************************************************/
700 static int Seek( demux_t *p_demux, mtime_t i_date )
702 demux_sys_t *p_sys = p_demux->p_sys;
703 unsigned int i_track;
705 /* First update update global time */
706 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
707 p_sys->i_pcr = i_date;
709 /* Now for each stream try to go to this time */
710 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
712 mp4_track_t *tk = &p_sys->track[i_track];
713 MP4_TrackSeek( p_demux, tk, i_date );
718 /*****************************************************************************
720 *****************************************************************************/
721 static int Control( demux_t *p_demux, int i_query, va_list args )
723 demux_sys_t *p_sys = p_demux->p_sys;
730 case DEMUX_GET_POSITION:
731 pf = (double*)va_arg( args, double * );
732 if( p_sys->i_duration > 0 )
734 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
742 case DEMUX_SET_POSITION:
743 f = (double)va_arg( args, double );
744 if( p_sys->i_timescale > 0 )
746 i64 = (int64_t)( f * (double)1000000 *
747 (double)p_sys->i_duration /
748 (double)p_sys->i_timescale );
749 return Seek( p_demux, i64 );
751 else return VLC_SUCCESS;
754 pi64 = (int64_t*)va_arg( args, int64_t * );
755 if( p_sys->i_timescale > 0 )
757 *pi64 = (mtime_t)1000000 *
758 (mtime_t)p_sys->i_time /
759 (mtime_t)p_sys->i_timescale;
765 i64 = (int64_t)va_arg( args, int64_t );
766 return Seek( p_demux, i64 );
768 case DEMUX_GET_LENGTH:
769 pi64 = (int64_t*)va_arg( args, int64_t * );
770 if( p_sys->i_timescale > 0 )
772 *pi64 = (mtime_t)1000000 *
773 (mtime_t)p_sys->i_duration /
774 (mtime_t)p_sys->i_timescale;
780 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
785 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
786 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
787 MP4_Box_t *p_0xa9xxx;
792 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
793 p_0xa9xxx = p_0xa9xxx->p_next )
796 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
798 psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
799 if( psz_utf == NULL )
801 /* FIXME FIXME: should convert from whatever the character
802 * encoding of MP4 meta data is to UTF-8. */
803 EnsureUTF8( psz_utf );
805 switch( p_0xa9xxx->i_type )
807 case FOURCC_0xa9nam: /* Full name */
808 vlc_meta_SetArtist( p_meta, psz_utf );
811 vlc_meta_SetArtist( p_meta, psz_utf );
814 vlc_meta_SetArtist( p_meta, psz_utf );
817 vlc_meta_SetCopyright( p_meta, psz_utf );
819 case FOURCC_0xa9day: /* Creation Date */
820 vlc_meta_SetDate( p_meta, psz_utf );
822 case FOURCC_0xa9des: /* Description */
823 vlc_meta_SetDescription( p_meta, psz_utf );
825 case FOURCC_0xa9gen: /* Genre */
826 vlc_meta_SetGenre( p_meta, psz_utf );
830 case FOURCC_0xa9inf: /* Information */
831 case FOURCC_0xa9alb: /* Album */
832 case FOURCC_0xa9dir: /* Director */
833 case FOURCC_0xa9dis: /* Disclaimer */
834 case FOURCC_0xa9enc: /* Encoded By */
835 case FOURCC_0xa9trk: /* Track */
836 case FOURCC_0xa9cmt: /* Commment */
837 case FOURCC_0xa9url: /* URL */
838 case FOURCC_0xa9req: /* Requirements */
839 case FOURCC_0xa9fmt: /* Original Format */
840 case FOURCC_0xa9dsa: /* Display Source As */
841 case FOURCC_0xa9hst: /* Host Computer */
842 case FOURCC_0xa9prd: /* Producer */
843 case FOURCC_0xa9prf: /* Performers */
844 case FOURCC_0xa9ope: /* Original Performer */
845 case FOURCC_0xa9src: /* Providers Source Content */
846 case FOURCC_0xa9wrt: /* Writer */
847 case FOURCC_0xa9com: /* Composer */
848 case FOURCC_WLOC: /* Window Location */
849 /* TODO one day, but they aren't really meaningfull */
860 case DEMUX_GET_TITLE_INFO:
861 case DEMUX_SET_NEXT_DEMUX_TIME:
862 case DEMUX_SET_GROUP:
866 msg_Warn( p_demux, "control query %u unimplemented", i_query );
871 /*****************************************************************************
872 * Close: frees unused data
873 *****************************************************************************/
874 static void Close ( vlc_object_t * p_this )
876 unsigned int i_track;
877 demux_t * p_demux = (demux_t *)p_this;
878 demux_sys_t *p_sys = p_demux->p_sys;
880 msg_Dbg( p_demux, "freeing all memory" );
882 MP4_BoxFree( p_demux->s, p_sys->p_root );
883 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
885 MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
887 FREENULL( p_sys->track );
894 /****************************************************************************
895 * Local functions, specific to vlc
896 ****************************************************************************/
898 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
899 static int TrackCreateChunksIndex( demux_t *p_demux,
900 mp4_track_t *p_demux_track )
902 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
905 unsigned int i_chunk;
906 unsigned int i_index, i_last;
908 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
909 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
910 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
912 return( VLC_EGENERIC );
915 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
916 if( !p_demux_track->i_chunk_count )
918 msg_Warn( p_demux, "no chunk defined" );
919 return( VLC_EGENERIC );
921 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
922 sizeof( mp4_chunk_t ) );
924 /* first we read chunk offset */
925 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
927 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
929 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
932 ck->p_sample_count_dts = NULL;
933 ck->p_sample_delta_dts = NULL;
934 ck->p_sample_count_pts = NULL;
935 ck->p_sample_offset_pts = NULL;
938 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
939 to be used for the sample XXX begin to 1
940 We construct it begining at the end */
941 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
942 i_index = p_stsc->data.p_stsc->i_entry_count;
945 msg_Warn( p_demux, "cannot read chunk table or table empty" );
946 return( VLC_EGENERIC );
951 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
952 i_chunk < i_last; i_chunk++ )
954 p_demux_track->chunk[i_chunk].i_sample_description_index =
955 p_stsc->data.p_stsc->i_sample_description_index[i_index];
956 p_demux_track->chunk[i_chunk].i_sample_count =
957 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
959 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
962 p_demux_track->chunk[0].i_sample_first = 0;
963 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
965 p_demux_track->chunk[i_chunk].i_sample_first =
966 p_demux_track->chunk[i_chunk-1].i_sample_first +
967 p_demux_track->chunk[i_chunk-1].i_sample_count;
970 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
971 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
976 static int TrackCreateSamplesIndex( demux_t *p_demux,
977 mp4_track_t *p_demux_track )
980 MP4_Box_data_stsz_t *stsz;
981 MP4_Box_data_stts_t *stts;
982 /* TODO use also stss and stsh table for seeking */
983 /* FIXME use edit table */
988 int64_t i_index_sample_used;
993 * Gives the sample size for each samples. There is also a stz2 table
994 * (compressed form) that we need to implement TODO */
995 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
999 msg_Warn( p_demux, "cannot find STSZ box" );
1000 return VLC_EGENERIC;
1002 stsz = p_box->data.p_stsz;
1005 * Gives mapping between sample and decoding time
1007 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1010 msg_Warn( p_demux, "cannot find STTS box" );
1011 return VLC_EGENERIC;
1013 stts = p_box->data.p_stts;
1015 /* Use stsz table to create a sample number -> sample size table */
1016 p_demux_track->i_sample_count = stsz->i_sample_count;
1017 if( stsz->i_sample_size )
1019 /* 1: all sample have the same size, so no need to construct a table */
1020 p_demux_track->i_sample_size = stsz->i_sample_size;
1021 p_demux_track->p_sample_size = NULL;
1025 /* 2: each sample can have a different size */
1026 p_demux_track->i_sample_size = 0;
1027 p_demux_track->p_sample_size =
1028 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1030 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1032 p_demux_track->p_sample_size[i_sample] =
1033 stsz->i_entry_size[i_sample];
1037 /* Use stts table to create a sample number -> dts table.
1038 * XXX: if we don't want to waste too much memory, we can't expand
1039 * the box! so each chunk will contain an "extract" of this table
1040 * for fast research (problem with raw stream where a sample is sometime
1041 * just channels*bits_per_sample/8 */
1044 i_index = 0; i_index_sample_used = 0;
1045 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1047 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1048 int64_t i_entry, i_sample_count, i;
1051 ck->i_first_dts = i_last_dts;
1053 /* count how many entries are needed for this chunk
1054 * for p_sample_delta_dts and p_sample_count_dts */
1055 i_sample_count = ck->i_sample_count;
1058 while( i_sample_count > 0 )
1060 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1061 /* don't count already used sample in this entry */
1063 i_sample_count += i_index_sample_used;
1069 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1070 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1073 i_sample_count = ck->i_sample_count;
1074 for( i = 0; i < i_entry; i++ )
1079 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1081 i_used = __MIN( i_rest, i_sample_count );
1083 i_index_sample_used += i_used;
1084 i_sample_count -= i_used;
1086 ck->p_sample_count_dts[i] = i_used;
1087 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1089 i_last_dts += i_used * ck->p_sample_delta_dts[i];
1091 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1094 i_index_sample_used = 0;
1100 * Gives the delta between decoding time (dts) and composition table (pts)
1102 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1105 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1107 msg_Warn( p_demux, "CTTS table" );
1109 /* Create pts-dts table per chunk */
1110 i_index = 0; i_index_sample_used = 0;
1111 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1113 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1114 int64_t i_entry, i_sample_count, i;
1116 /* count how many entries are needed for this chunk
1117 * for p_sample_delta_dts and p_sample_count_dts */
1118 i_sample_count = ck->i_sample_count;
1121 while( i_sample_count > 0 )
1123 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1125 /* don't count already used sample in this entry */
1127 i_sample_count += i_index_sample_used;
1133 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1134 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1137 i_sample_count = ck->i_sample_count;
1138 for( i = 0; i < i_entry; i++ )
1143 i_rest = ctts->i_sample_count[i_index] -
1144 i_index_sample_used;
1146 i_used = __MIN( i_rest, i_sample_count );
1148 i_index_sample_used += i_used;
1149 i_sample_count -= i_used;
1151 ck->p_sample_count_pts[i] = i_used;
1152 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1154 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1157 i_index_sample_used = 0;
1163 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:"I64Fd"s",
1164 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1165 i_last_dts / p_demux_track->i_timescale );
1172 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1174 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1175 unsigned int i_chunk, es_out_id_t **pp_es )
1177 MP4_Box_t *p_sample;
1183 if( !p_track->chunk[i_chunk].i_sample_description_index )
1185 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1186 p_track->i_track_ID );
1187 return VLC_EGENERIC;
1190 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1191 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1194 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1196 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1197 p_track->i_track_ID );
1198 return VLC_EGENERIC;
1201 p_track->p_sample = p_sample;
1203 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1205 MP4_Box_data_sample_soun_t *p_soun;
1207 p_soun = p_sample->data.p_sample_soun;
1209 if( p_soun->i_qt_version == 0 )
1211 switch( p_sample->i_type )
1213 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1214 p_soun->i_qt_version = 1;
1215 p_soun->i_sample_per_packet = 64;
1216 p_soun->i_bytes_per_packet = 34;
1217 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1218 p_soun->i_bytes_per_sample = 2;
1220 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1221 p_soun->i_qt_version = 1;
1222 p_soun->i_sample_per_packet = 6;
1223 p_soun->i_bytes_per_packet = 2;
1224 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1225 p_soun->i_bytes_per_sample = 2;
1227 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1228 p_soun->i_qt_version = 1;
1229 p_soun->i_sample_per_packet = 12;
1230 p_soun->i_bytes_per_packet = 2;
1231 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1232 p_soun->i_bytes_per_sample = 2;
1234 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1235 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1236 p_soun->i_samplesize = 8;
1243 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1245 p_soun->i_qt_version = 0;
1250 /* It's a little ugly but .. there are special cases */
1251 switch( p_sample->i_type )
1253 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1254 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1255 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1258 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1259 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1261 /* Buggy files workaround */
1262 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1263 p_sample->data.p_sample_soun->i_sampleratehi) )
1265 MP4_Box_data_sample_soun_t *p_soun =
1266 p_sample->data.p_sample_soun;
1268 msg_Warn( p_demux, "i_timescale ("I64Fu") != i_sampleratehi "
1269 "(%u), making both equal (report any problem).",
1270 p_track->i_timescale, p_soun->i_sampleratehi );
1272 if( p_soun->i_sampleratehi )
1273 p_track->i_timescale = p_soun->i_sampleratehi;
1275 p_soun->i_sampleratehi = p_track->i_timescale;
1279 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1280 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1283 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1284 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1285 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1286 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1287 /* FIXME UTF-8 doesn't work here ? */
1288 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1292 p_track->fmt.i_codec = p_sample->i_type;
1296 /* now see if esds is present and if so create a data packet
1297 with decoder_specific_info */
1298 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1299 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1300 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1301 ( p_esds->data.p_esds )&&
1304 /* First update information based on i_objectTypeIndication */
1305 switch( p_decconfig->i_objectTypeIndication )
1307 case( 0x20 ): /* MPEG4 VIDEO */
1308 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1311 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1318 case( 0x65): /* MPEG2 video */
1319 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1321 /* Theses are MPEG2-AAC */
1322 case( 0x66): /* main profile */
1323 case( 0x67): /* Low complexity profile */
1324 case( 0x68): /* Scaleable Sampling rate profile */
1325 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1327 /* true MPEG 2 audio */
1329 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1331 case( 0x6a): /* MPEG1 video */
1332 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1334 case( 0x6b): /* MPEG1 audio */
1335 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1337 case( 0x6c ): /* jpeg */
1338 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1342 case( 0xe0 ): /* NeroDigital: dvd subs */
1343 if( p_track->fmt.i_cat == SPU_ES )
1345 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1346 if( p_track->i_width > 0 )
1347 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1348 if( p_track->i_height > 0 )
1349 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1354 /* Unknown entry, but don't touch i_fourcc */
1356 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1357 p_decconfig->i_objectTypeIndication,
1358 p_track->i_track_ID );
1361 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1362 if( p_track->fmt.i_extra > 0 )
1364 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1365 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1366 p_track->fmt.i_extra );
1371 switch( p_sample->i_type )
1373 /* qt decoder, send the complete chunk */
1374 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1375 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1376 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1377 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1378 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1379 p_track->fmt.i_extra =
1380 p_sample->data.p_sample_vide->i_qt_image_description;
1381 if( p_track->fmt.i_extra > 0 )
1383 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1384 memcpy( p_track->fmt.p_extra,
1385 p_sample->data.p_sample_vide->p_qt_image_description,
1386 p_track->fmt.i_extra);
1389 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1390 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1391 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1392 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1393 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1394 p_track->fmt.i_extra =
1395 p_sample->data.p_sample_soun->i_qt_description;
1396 if( p_track->fmt.i_extra > 0 )
1398 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1399 memcpy( p_track->fmt.p_extra,
1400 p_sample->data.p_sample_soun->p_qt_description,
1401 p_track->fmt.i_extra);
1405 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1406 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1408 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1412 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1413 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1414 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1415 p_track->fmt.i_extra );
1419 msg_Err( p_demux, "missing avcC" );
1431 /* some last initialisation */
1432 switch( p_track->fmt.i_cat )
1435 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1436 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1437 p_track->fmt.video.i_bits_per_pixel =
1438 p_sample->data.p_sample_vide->i_depth;
1440 /* fall on display size */
1441 if( p_track->fmt.video.i_width <= 0 )
1442 p_track->fmt.video.i_width = p_track->i_width;
1443 if( p_track->fmt.video.i_height <= 0 )
1444 p_track->fmt.video.i_height = p_track->i_height;
1446 /* Find out apect ratio from display size */
1447 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1448 /* Work-around buggy muxed files */
1449 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1450 p_track->fmt.video.i_aspect =
1451 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1453 /* Support for cropping (eg. in H263 files) */
1454 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1455 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1458 p_track->fmt.video.i_frame_rate = p_track->i_timescale;
1459 p_track->fmt.video.i_frame_rate_base = 1;
1461 if( p_track->fmt.video.i_frame_rate &&
1462 (p_box = MP4_BoxGet( p_track->p_stbl, "stts" )) &&
1463 p_box->data.p_stts->i_entry_count >= 1 )
1465 p_track->fmt.video.i_frame_rate_base =
1466 p_box->data.p_stts->i_sample_delta[0];
1472 p_track->fmt.audio.i_channels =
1473 p_sample->data.p_sample_soun->i_channelcount;
1474 p_track->fmt.audio.i_rate =
1475 p_sample->data.p_sample_soun->i_sampleratehi;
1476 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1477 p_sample->data.p_sample_soun->i_sampleratehi *
1478 p_sample->data.p_sample_soun->i_samplesize;
1479 p_track->fmt.audio.i_bitspersample =
1480 p_sample->data.p_sample_soun->i_samplesize;
1487 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1492 /* given a time it return sample/chunk
1493 * it also update elst field of the track
1495 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1496 int64_t i_start, uint32_t *pi_chunk,
1497 uint32_t *pi_sample )
1499 demux_sys_t *p_sys = p_demux->p_sys;
1502 unsigned int i_sample;
1503 unsigned int i_chunk;
1506 /* FIXME see if it's needed to check p_track->i_chunk_count */
1507 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1509 return( VLC_EGENERIC );
1512 /* handle elst (find the correct one) */
1513 MP4_TrackSetELST( p_demux, p_track, i_start );
1514 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1516 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1517 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1519 /* now calculate i_start for this elst */
1521 i_start -= p_track->i_elst_time * I64C(1000000) / p_sys->i_timescale;
1529 /* to track time scale */
1530 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1531 /* add elst offset */
1532 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1533 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1534 elst->i_media_time[p_track->i_elst] > 0 )
1536 i_start += elst->i_media_time[p_track->i_elst];
1539 msg_Dbg( p_demux, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd
1540 "ms (track)", p_track->i_elst,
1541 i_mvt * 1000 / p_sys->i_timescale,
1542 i_start * 1000 / p_track->i_timescale );
1546 /* convert absolute time to in timescale unit */
1547 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1550 /* we start from sample 0/chunk 0, hope it won't take too much time */
1551 /* *** find good chunk *** */
1552 for( i_chunk = 0; ; i_chunk++ )
1554 if( i_chunk + 1 >= p_track->i_chunk_count )
1556 /* at the end and can't check if i_start in this chunk,
1557 it will be check while searching i_sample */
1558 i_chunk = p_track->i_chunk_count - 1;
1562 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
1563 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1569 /* *** find sample in the chunk *** */
1570 i_sample = p_track->chunk[i_chunk].i_sample_first;
1571 i_dts = p_track->chunk[i_chunk].i_first_dts;
1572 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1575 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1576 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
1579 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1580 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1582 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1587 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1591 i_sample += ( i_start - i_dts ) /
1592 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1597 if( i_sample >= p_track->i_sample_count )
1599 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1600 "(seeking too far) chunk=%d sample=%d",
1601 p_track->i_track_ID, i_chunk, i_sample );
1602 return( VLC_EGENERIC );
1606 /* *** Try to find nearest sync points *** */
1607 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1609 unsigned int i_index;
1611 "track[Id 0x%x] using Sync Sample Box (stss)",
1612 p_track->i_track_ID );
1613 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1615 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1619 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1621 p_stss->data.p_stss->i_sample_number[i_index-1] );
1622 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1623 /* new i_sample is less than old so i_chunk can only decreased */
1624 while( i_chunk > 0 &&
1625 i_sample < p_track->chunk[i_chunk].i_sample_first )
1632 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1634 p_stss->data.p_stss->i_sample_number[i_index] );
1635 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1636 /* new i_sample is more than old so i_chunk can only increased */
1637 while( i_chunk < p_track->i_chunk_count - 1 &&
1638 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1639 p_track->chunk[i_chunk].i_sample_count )
1650 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1651 "Sample Box (stss)", p_track->i_track_ID );
1654 *pi_chunk = i_chunk;
1655 *pi_sample = i_sample;
1660 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1661 unsigned int i_chunk, unsigned int i_sample )
1663 vlc_bool_t b_reselect = VLC_FALSE;
1665 /* now see if actual es is ok */
1666 if( (p_track->i_chunk >= p_track->i_chunk_count - 1) ||
1667 (p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1668 p_track->chunk[i_chunk].i_sample_description_index) )
1670 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1671 p_track->i_track_ID );
1673 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1674 p_track->p_es, &b_reselect );
1676 es_out_Del( p_demux->out, p_track->p_es );
1678 p_track->p_es = NULL;
1680 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1682 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1683 p_track->i_track_ID );
1685 p_track->b_ok = VLC_FALSE;
1686 p_track->b_selected = VLC_FALSE;
1687 return VLC_EGENERIC;
1691 /* select again the new decoder */
1694 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1697 p_track->i_chunk = i_chunk;
1698 p_track->i_sample = i_sample;
1700 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1703 /****************************************************************************
1705 ****************************************************************************
1706 * Parse track information and create all needed data to run a track
1707 * If it succeed b_ok is set to 1 else to 0
1708 ****************************************************************************/
1709 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1710 MP4_Box_t *p_box_trak )
1712 demux_sys_t *p_sys = p_demux->p_sys;
1714 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1715 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1730 /* hint track unsupported */
1732 /* set default value (-> track unusable) */
1733 p_track->b_ok = VLC_FALSE;
1734 p_track->b_enable = VLC_FALSE;
1735 p_track->b_selected = VLC_FALSE;
1737 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1744 /* do we launch this track by default ? */
1746 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1748 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1749 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1750 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1754 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
1757 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1758 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1760 if( ( !p_mdhd )||( !p_hdlr ) )
1765 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1767 for( i = 0; i < 3; i++ )
1769 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1773 switch( p_hdlr->data.p_hdlr->i_handler_type )
1775 case( FOURCC_soun ):
1776 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1780 p_track->fmt.i_cat = AUDIO_ES;
1783 case( FOURCC_vide ):
1784 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1788 p_track->fmt.i_cat = VIDEO_ES;
1791 case( FOURCC_text ):
1792 case( FOURCC_subp ):
1793 case( FOURCC_tx3g ):
1794 p_track->fmt.i_cat = SPU_ES;
1801 p_track->i_elst = 0;
1802 p_track->i_elst_time = 0;
1803 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1805 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1808 msg_Warn( p_demux, "elst box found" );
1809 for( i = 0; i < elst->i_entry_count; i++ )
1811 msg_Dbg( p_demux, " - [%d] duration="I64Fd"ms media time="I64Fd
1812 "ms) rate=%d.%d", i,
1813 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1814 elst->i_media_time[i] >= 0 ?
1815 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
1817 elst->i_media_rate_integer[i],
1818 elst->i_media_rate_fraction[i] );
1825 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1827 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1828 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1833 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1834 p_track->b_drms = p_drms != NULL;
1835 p_track->p_drms = p_track->b_drms ?
1836 p_drms->data.p_sample_soun->p_drms : NULL;
1839 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1841 p_track->fmt.psz_language = strdup( language );
1844 p_udta = MP4_BoxGet( p_box_trak, "udta" );
1847 MP4_Box_t *p_0xa9xxx;
1848 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
1849 p_0xa9xxx = p_0xa9xxx->p_next )
1851 switch( p_0xa9xxx->i_type )
1853 case FOURCC_0xa9nam:
1854 p_track->fmt.psz_description =
1855 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
1861 /* Create chunk index table and sample index table */
1862 if( TrackCreateChunksIndex( p_demux,p_track ) ||
1863 TrackCreateSamplesIndex( p_demux, p_track ) )
1865 return; /* cannot create chunks index */
1868 p_track->i_chunk = 0;
1869 p_track->i_sample = 0;
1872 if( TrackCreateES( p_demux,
1873 p_track, p_track->i_chunk,
1876 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1877 p_track->i_track_ID );
1884 for( i = 0; i < p_track->i_chunk_count; i++ )
1886 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
1887 i, p_track->chunk[i].i_sample_count,
1888 p_track->chunk[i].i_first_dts );
1893 p_track->b_ok = VLC_TRUE;
1896 /****************************************************************************
1898 ****************************************************************************
1899 * Destroy a track created by MP4_TrackCreate.
1900 ****************************************************************************/
1901 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
1903 unsigned int i_chunk;
1905 p_track->b_ok = VLC_FALSE;
1906 p_track->b_enable = VLC_FALSE;
1907 p_track->b_selected = VLC_FALSE;
1909 es_format_Clean( &p_track->fmt );
1911 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1913 if( p_track->chunk )
1915 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
1916 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
1918 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
1919 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
1922 FREENULL( p_track->chunk );
1924 if( !p_track->i_sample_size )
1926 FREENULL( p_track->p_sample_size );
1930 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
1933 if( !p_track->b_ok )
1935 return VLC_EGENERIC;
1938 if( p_track->b_selected )
1940 msg_Warn( p_demux, "track[Id 0x%x] already selected",
1941 p_track->i_track_ID );
1945 return MP4_TrackSeek( p_demux, p_track, i_start );
1948 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
1950 if( !p_track->b_ok )
1955 if( !p_track->b_selected )
1957 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
1958 p_track->i_track_ID );
1963 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
1964 p_track->p_es, VLC_FALSE );
1967 p_track->b_selected = VLC_FALSE;
1970 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
1976 if( !p_track->b_ok )
1978 return( VLC_EGENERIC );
1981 p_track->b_selected = VLC_FALSE;
1983 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
1984 &i_chunk, &i_sample ) )
1986 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
1987 p_track->i_track_ID );
1988 return( VLC_EGENERIC );
1991 p_track->b_selected = VLC_TRUE;
1993 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
1996 p_track->b_selected = VLC_TRUE;
1998 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1999 p_track->p_es, i_start );
2002 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
2007 * 3 types: for audio
2010 #define QT_V0_MAX_SAMPLES 1024
2011 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2014 MP4_Box_data_sample_soun_t *p_soun;
2016 if( p_track->i_sample_size == 0 )
2018 /* most simple case */
2019 return p_track->p_sample_size[p_track->i_sample];
2021 if( p_track->fmt.i_cat != AUDIO_ES )
2023 return p_track->i_sample_size;
2026 p_soun = p_track->p_sample->data.p_sample_soun;
2028 if( p_soun->i_qt_version == 1 )
2030 i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
2031 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2033 else if( p_track->i_sample_size > 256 )
2035 /* We do that so we don't read too much data
2036 * (in this case we are likely dealing with compressed data) */
2037 i_size = p_track->i_sample_size;
2041 /* Read a bunch of samples at once */
2042 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2043 ( p_track->i_sample -
2044 p_track->chunk[p_track->i_chunk].i_sample_first );
2046 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2047 i_size = i_samples * p_track->i_sample_size;
2050 //fprintf( stderr, "size=%d\n", i_size );
2054 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2056 unsigned int i_sample;
2059 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2061 if( p_track->i_sample_size )
2063 MP4_Box_data_sample_soun_t *p_soun =
2064 p_track->p_sample->data.p_sample_soun;
2066 if( p_soun->i_qt_version == 0 )
2068 i_pos += ( p_track->i_sample -
2069 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2070 p_track->i_sample_size;
2074 /* we read chunk by chunk */
2080 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2081 i_sample < p_track->i_sample; i_sample++ )
2083 i_pos += p_track->p_sample_size[i_sample];
2090 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2092 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2094 MP4_Box_data_sample_soun_t *p_soun;
2096 p_soun = p_track->p_sample->data.p_sample_soun;
2098 if( p_soun->i_qt_version == 1 )
2100 /* chunk by chunk */
2102 p_track->chunk[p_track->i_chunk].i_sample_first +
2103 p_track->chunk[p_track->i_chunk].i_sample_count;
2105 else if( p_track->i_sample_size > 256 )
2107 /* We do that so we don't read too much data
2108 * (in this case we are likely dealing with compressed data) */
2109 p_track->i_sample += 1;
2114 p_track->i_sample += QT_V0_MAX_SAMPLES;
2115 if( p_track->i_sample >
2116 p_track->chunk[p_track->i_chunk].i_sample_first +
2117 p_track->chunk[p_track->i_chunk].i_sample_count )
2120 p_track->chunk[p_track->i_chunk].i_sample_first +
2121 p_track->chunk[p_track->i_chunk].i_sample_count;
2127 p_track->i_sample++;
2130 if( p_track->i_sample >= p_track->i_sample_count )
2131 return VLC_EGENERIC;
2133 /* Have we changed chunk ? */
2134 if( p_track->i_sample >=
2135 p_track->chunk[p_track->i_chunk].i_sample_first +
2136 p_track->chunk[p_track->i_chunk].i_sample_count )
2138 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2139 p_track->i_sample ) )
2141 msg_Warn( p_demux, "track[0x%x] will be disabled "
2142 "(cannot restart decoder)", p_track->i_track_ID );
2143 MP4_TrackUnselect( p_demux, p_track );
2144 return VLC_EGENERIC;
2148 /* Have we changed elst */
2149 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2151 demux_sys_t *p_sys = p_demux->p_sys;
2152 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2153 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2154 p_sys->i_timescale / (int64_t)1000000;
2156 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2157 i_mvt >= p_track->i_elst_time +
2158 elst->i_segment_duration[p_track->i_elst] )
2160 MP4_TrackSetELST( p_demux, p_track,
2161 MP4_TrackGetDTS( p_demux, p_track ) );
2168 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2171 demux_sys_t *p_sys = p_demux->p_sys;
2172 int i_elst_last = tk->i_elst;
2174 /* handle elst (find the correct one) */
2176 tk->i_elst_time = 0;
2177 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2179 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2180 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2182 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2184 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2186 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2190 tk->i_elst_time += i_dur;
2193 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2195 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2196 tk->i_elst = elst->i_entry_count - 1;
2197 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2200 if( elst->i_media_time[tk->i_elst] < 0 )
2203 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2206 if( i_elst_last != tk->i_elst )
2208 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2212 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
2213 playlist_item_t **pp_item )
2215 vlc_bool_t b_play = var_CreateGetBool( p_demux, "playlist-autostart" );
2217 if( b_play && p_playlist->status.p_item &&
2218 p_playlist->status.p_item->p_input ==
2219 input_GetItem((input_thread_t *)p_demux->p_parent))
2221 msg_Dbg( p_playlist, "starting playlist playback" );
2222 *pp_item = p_playlist->status.p_item;
2227 input_item_t *p_current = input_GetItem(
2228 (input_thread_t*)p_demux->p_parent);
2229 *pp_item = playlist_ItemGetByInput( p_playlist, p_current, VLC_FALSE );
2232 msg_Dbg( p_playlist, "unable to find item in playlist");
2234 msg_Dbg( p_playlist, "not starting playlist playback");