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 unsigned 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 */
100 vlc_bool_t b_chapter; /* True when used for chapter only */
105 /* display size only ! */
109 /* more internal data */
110 uint64_t i_timescale; /* time scale for this track only */
113 int i_elst; /* current elst */
114 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
115 MP4_Box_t *p_elst; /* elst (could be NULL) */
117 /* give the next sample to read, i_chunk is to find quickly where
118 the sample is located */
119 uint32_t i_sample; /* next sample to read */
120 uint32_t i_chunk; /* chunk where next sample is stored */
121 /* total count of chunk and sample */
122 uint32_t i_chunk_count;
123 uint32_t i_sample_count;
125 mp4_chunk_t *chunk; /* always defined for each chunk */
127 /* sample size, p_sample_size defined only if i_sample_size == 0
128 else i_sample_size is size for all sample */
129 uint32_t i_sample_size;
130 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
131 too much time to do sumations each time*/
133 MP4_Box_t *p_stbl; /* will contain all timing information */
134 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
135 MP4_Box_t *p_sample;/* point on actual sdsd */
145 MP4_Box_t *p_root; /* container for the whole file */
149 uint64_t i_time; /* time position of the presentation
150 * in movie timescale */
151 uint64_t i_timescale; /* movie time scale */
152 uint64_t i_duration; /* movie duration */
153 unsigned int i_tracks; /* number of tracks */
154 mp4_track_t *track; /* array of track */
157 MP4_Box_t *p_tref_chap;
160 input_title_t *p_title;
163 /*****************************************************************************
164 * Declaration of local function
165 *****************************************************************************/
166 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *);
167 static void MP4_TrackDestroy( demux_t *, mp4_track_t * );
169 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
170 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
172 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
174 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
175 static int MP4_TrackSampleSize( mp4_track_t * );
176 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
177 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
179 static void MP4_UpdateSeekpoint( demux_t * );
181 /* Return time in s of a track */
182 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
184 #define chunk p_track->chunk[p_track->i_chunk]
186 unsigned int i_index = 0;
187 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
188 int64_t i_dts = chunk.i_first_dts;
190 while( i_sample > 0 )
192 if( i_sample > chunk.p_sample_count_dts[i_index] )
194 i_dts += chunk.p_sample_count_dts[i_index] *
195 chunk.p_sample_delta_dts[i_index];
196 i_sample -= chunk.p_sample_count_dts[i_index];
201 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
209 /* now handle elst */
210 if( p_track->p_elst )
212 demux_sys_t *p_sys = p_demux->p_sys;
213 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
215 /* convert to offset */
216 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
217 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
218 elst->i_media_time[p_track->i_elst] > 0 )
220 i_dts -= elst->i_media_time[p_track->i_elst];
223 /* add i_elst_time */
224 i_dts += p_track->i_elst_time * p_track->i_timescale /
227 if( i_dts < 0 ) i_dts = 0;
230 return I64C(1000000) * i_dts / p_track->i_timescale;
233 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
235 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
236 unsigned int i_index = 0;
237 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
239 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
242 for( i_index = 0;; i_index++ )
244 if( i_sample < ck->p_sample_count_pts[i_index] )
245 return ck->p_sample_offset_pts[i_index] * I64C(1000000) /
246 (int64_t)p_track->i_timescale;
248 i_sample -= ck->p_sample_count_pts[i_index];
252 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
254 return I64C(1000000) * p_sys->i_time / p_sys->i_timescale;
257 /* Function to lookup the currently playing item */
258 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
259 playlist_item_t **pp_item );
261 static void LoadChapter( demux_t *p_demux );
263 /*****************************************************************************
264 * Open: check file and initializes MP4 structures
265 *****************************************************************************/
266 static int Open( vlc_object_t * p_this )
268 demux_t *p_demux = (demux_t *)p_this;
279 vlc_bool_t b_seekable;
281 /* A little test to see if it could be a mp4 */
282 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
284 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
295 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
302 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
305 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
309 /*Set exported functions */
310 p_demux->pf_demux = Demux;
311 p_demux->pf_control = Control;
313 /* create our structure that will contains all data */
314 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
315 memset( p_sys, 0, sizeof( demux_sys_t ) );
317 /* Now load all boxes ( except raw data ) */
318 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
320 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
324 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
326 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
328 switch( p_ftyp->data.p_ftyp->i_major_brand )
332 "ISO Media file (isom) version %d.",
333 p_ftyp->data.p_ftyp->i_minor_version );
337 "unrecognized major file specification (%4.4s).",
338 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
344 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
347 /* the file need to have one moov box */
348 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
350 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
354 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
357 /* we have a free box as a moov, rename it */
358 p_foov->i_type = FOURCC_moov;
361 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
363 playlist_t *p_playlist;
364 playlist_item_t *p_current, *p_item_in_category;
365 int i_count = MP4_BoxCount( p_rmra, "rmda" );
367 vlc_bool_t b_play = VLC_FALSE;
369 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
371 p_playlist = pl_Yield( p_demux );
374 b_play = FindItem( p_demux, p_playlist, &p_current );
375 p_item_in_category = playlist_ItemToNode( p_playlist, p_current, VLC_TRUE );
376 p_current->p_input->i_type = ITEM_TYPE_PLAYLIST;
378 for( i = 0; i < i_count; i++ )
380 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
384 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
388 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
390 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
391 psz_ref, (char*)&i_ref_type );
393 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
395 if( strstr( psz_ref, "qt5gateQT" ) )
397 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
400 if( !strncmp( psz_ref, "http://", 7 ) ||
401 !strncmp( psz_ref, "rtsp://", 7 ) )
408 char *psz_path = strdup( p_demux->psz_path );
409 char *end = strrchr( psz_path, '/' );
410 if( end ) end[1] = '\0';
411 else *psz_path = '\0';
413 asprintf( &psz_absolute, "%s://%s%s",
414 p_demux->psz_access, psz_path, psz_ref );
416 if( psz_ref ) free( psz_ref );
417 psz_ref = psz_absolute;
422 input_item_t *p_input;
423 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
424 p_input = input_ItemNewExt( p_playlist, psz_ref, NULL,
426 input_ItemCopyOptions( p_current->p_input, p_input );
427 playlist_BothAddInput( p_playlist, p_input,
429 PLAYLIST_APPEND, PLAYLIST_END,
430 NULL, NULL, VLC_FALSE );
435 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
436 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
438 if( psz_ref ) free( psz_ref );
440 if( b_play && p_item_in_category &&
441 p_item_in_category->i_children > 0 )
443 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
444 p_item_in_category, NULL );
446 vlc_object_release( p_playlist );
450 msg_Err( p_demux, "can't find playlist" );
454 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
458 msg_Err( p_demux, "cannot find /moov/mvhd" );
463 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
464 p_demux->pf_demux = DemuxRef;
470 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
471 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
474 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
476 msg_Err( p_demux, "cannot find any /moov/trak" );
479 msg_Dbg( p_demux, "find %d track%c",
481 p_sys->i_tracks ? 's':' ' );
483 /* allocate memory */
484 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
485 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
487 /* Search the first chap reference (like quicktime) */
488 p_sys->p_tref_chap = NULL;
489 for( i = 0; i < p_sys->i_tracks; i++ )
491 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
492 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
493 if( !p_chap || p_chap->data.p_tref_generic->i_entry_count <= 0 )
496 p_sys->p_tref_chap = p_chap;
500 /* now process each track and extract all usefull information */
501 for( i = 0; i < p_sys->i_tracks; i++ )
503 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
504 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
506 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
509 switch( p_sys->track[i].fmt.i_cat )
518 psz_cat = "subtitle";
526 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
527 p_sys->track[i].i_track_ID, psz_cat,
528 p_sys->track[i].b_enable ? "enable":"disable",
529 p_sys->track[i].fmt.psz_language ?
530 p_sys->track[i].fmt.psz_language : "undef" );
532 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
534 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
535 p_sys->track[i].i_track_ID,
536 p_sys->track[i].fmt.psz_language ?
537 p_sys->track[i].fmt.psz_language : "undef" );
541 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
542 p_sys->track[i].i_track_ID );
547 LoadChapter( p_demux );
554 MP4_BoxFree( p_demux->s, p_sys->p_root );
560 /*****************************************************************************
561 * Demux: read packet and send them to decoders
562 *****************************************************************************
563 * TODO check for newly selected track (ie audio upt to now )
564 *****************************************************************************/
565 static int Demux( demux_t *p_demux )
567 demux_sys_t *p_sys = p_demux->p_sys;
568 unsigned int i_track;
571 unsigned int i_track_selected;
573 /* check for newly selected/unselected track */
574 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
577 mp4_track_t *tk = &p_sys->track[i_track];
580 if( !tk->b_ok || tk->b_chapter ||
581 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
586 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
588 if( tk->b_selected && !b )
590 MP4_TrackUnselect( p_demux, tk );
592 else if( !tk->b_selected && b)
594 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
603 if( i_track_selected <= 0 )
605 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
606 if( p_sys->i_timescale > 0 )
608 int64_t i_length = (mtime_t)1000000 *
609 (mtime_t)p_sys->i_duration /
610 (mtime_t)p_sys->i_timescale;
611 if( MP4_GetMoviePTS( p_sys ) >= i_length )
616 msg_Warn( p_demux, "no track selected, exiting..." );
621 MP4_UpdateSeekpoint( p_demux );
623 /* first wait for the good time to read a packet */
624 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
626 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
628 /* we will read 100ms for each stream so ...*/
629 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
631 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
633 mp4_track_t *tk = &p_sys->track[i_track];
635 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
638 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
641 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
642 MP4_TrackGetDTS( p_demux, tk ),
643 MP4_GetMoviePTS( p_sys ) );
646 if( MP4_TrackSampleSize( tk ) > 0 )
652 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
654 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
656 MP4_TrackUnselect( p_demux, tk );
662 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
664 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
666 MP4_TrackUnselect( p_demux, tk );
670 if( tk->b_drms && tk->p_drms )
672 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
675 else if( tk->fmt.i_cat == SPU_ES )
677 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
678 p_block->i_buffer >= 2 )
680 uint16_t i_size = GetWBE( p_block->p_buffer );
682 if( i_size + 2 <= p_block->i_buffer )
685 /* remove the length field, and append a '\0' */
686 memmove( &p_block->p_buffer[0],
687 &p_block->p_buffer[2], i_size );
688 p_block->p_buffer[i_size] = '\0';
689 p_block->i_buffer = i_size + 1;
691 /* convert \r -> \n */
692 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
700 p_block->i_buffer = 0;
705 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
707 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
709 p_block->i_pts = p_block->i_dts + i_delta;
710 else if( tk->fmt.i_cat != VIDEO_ES )
711 p_block->i_pts = p_block->i_dts;
715 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
716 es_out_Send( p_demux->out, tk->p_es, p_block );
720 if( MP4_TrackNextSample( p_demux, tk ) )
728 static void MP4_UpdateSeekpoint( demux_t *p_demux )
730 demux_sys_t *p_sys = p_demux->p_sys;
733 if( !p_sys->p_title )
735 i_time = MP4_GetMoviePTS( p_sys );
736 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
738 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
743 if( i != p_demux->info.i_seekpoint && i >= 0 )
745 p_demux->info.i_seekpoint = i;
746 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
749 /*****************************************************************************
750 * Seek: Got to i_date
751 ******************************************************************************/
752 static int Seek( demux_t *p_demux, mtime_t i_date )
754 demux_sys_t *p_sys = p_demux->p_sys;
755 unsigned int i_track;
757 /* First update update global time */
758 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
759 p_sys->i_pcr = i_date;
761 /* Now for each stream try to go to this time */
762 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
764 mp4_track_t *tk = &p_sys->track[i_track];
765 MP4_TrackSeek( p_demux, tk, i_date );
767 MP4_UpdateSeekpoint( p_demux );
771 /*****************************************************************************
773 *****************************************************************************/
774 static int Control( demux_t *p_demux, int i_query, va_list args )
776 demux_sys_t *p_sys = p_demux->p_sys;
783 case DEMUX_GET_POSITION:
784 pf = (double*)va_arg( args, double * );
785 if( p_sys->i_duration > 0 )
787 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
795 case DEMUX_SET_POSITION:
796 f = (double)va_arg( args, double );
797 if( p_sys->i_timescale > 0 )
799 i64 = (int64_t)( f * (double)1000000 *
800 (double)p_sys->i_duration /
801 (double)p_sys->i_timescale );
802 return Seek( p_demux, i64 );
804 else return VLC_SUCCESS;
807 pi64 = (int64_t*)va_arg( args, int64_t * );
808 if( p_sys->i_timescale > 0 )
810 *pi64 = (mtime_t)1000000 *
811 (mtime_t)p_sys->i_time /
812 (mtime_t)p_sys->i_timescale;
818 i64 = (int64_t)va_arg( args, int64_t );
819 return Seek( p_demux, i64 );
821 case DEMUX_GET_LENGTH:
822 pi64 = (int64_t*)va_arg( args, int64_t * );
823 if( p_sys->i_timescale > 0 )
825 *pi64 = (mtime_t)1000000 *
826 (mtime_t)p_sys->i_duration /
827 (mtime_t)p_sys->i_timescale;
833 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
838 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
839 MP4_Box_t *p_0xa9xxx;
841 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
844 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
851 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
852 p_0xa9xxx = p_0xa9xxx->p_next )
855 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
858 /* FIXME FIXME: should convert from whatever the character
859 * encoding of MP4 meta data is to UTF-8. */
860 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
861 if( psz_utf ) { EnsureUTF8( psz_utf ); \
862 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
864 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
865 switch( p_0xa9xxx->i_type )
867 case FOURCC_0xa9nam: /* Full name */
868 SET( vlc_meta_SetTitle );
871 SET( vlc_meta_SetArtist );
874 SET( vlc_meta_SetArtist );
877 SET( vlc_meta_SetCopyright );
879 case FOURCC_0xa9day: /* Creation Date */
880 SET( vlc_meta_SetDate );
882 case FOURCC_0xa9des: /* Description */
883 SET( vlc_meta_SetDescription );
885 case FOURCC_0xa9gen: /* Genre */
886 SET( vlc_meta_SetGenre );
889 case FOURCC_0xa9alb: /* Album */
890 SET( vlc_meta_SetAlbum );
894 case FOURCC_0xa9inf: /* Information */
895 case FOURCC_0xa9dir: /* Director */
896 case FOURCC_0xa9dis: /* Disclaimer */
897 case FOURCC_0xa9enc: /* Encoded By */
898 case FOURCC_0xa9trk: /* Track */
899 case FOURCC_0xa9cmt: /* Commment */
900 case FOURCC_0xa9url: /* URL */
901 case FOURCC_0xa9req: /* Requirements */
902 case FOURCC_0xa9fmt: /* Original Format */
903 case FOURCC_0xa9dsa: /* Display Source As */
904 case FOURCC_0xa9hst: /* Host Computer */
905 case FOURCC_0xa9prd: /* Producer */
906 case FOURCC_0xa9prf: /* Performers */
907 case FOURCC_0xa9ope: /* Original Performer */
908 case FOURCC_0xa9src: /* Providers Source Content */
909 case FOURCC_0xa9wrt: /* Writer */
910 case FOURCC_0xa9com: /* Composer */
911 case FOURCC_WLOC: /* Window Location */
912 /* TODO one day, but they aren't really meaningfull */
923 case DEMUX_GET_TITLE_INFO:
925 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
926 int *pi_int = (int*)va_arg( args, int* );
927 int *pi_title_offset = (int*)va_arg( args, int* );
928 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
930 if( !p_sys->p_title )
934 *ppp_title = malloc( sizeof( input_title_t**) );
935 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
936 *pi_title_offset = 0;
937 *pi_seekpoint_offset = 0;
940 case DEMUX_SET_TITLE:
942 const int i_title = (int)va_arg( args, int );
943 if( !p_sys->p_title || i_title != 0 )
947 case DEMUX_SET_SEEKPOINT:
949 const int i_seekpoint = (int)va_arg( args, int );
950 if( !p_sys->p_title )
952 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
955 case DEMUX_SET_NEXT_DEMUX_TIME:
956 case DEMUX_SET_GROUP:
960 msg_Warn( p_demux, "control query %u unimplemented", i_query );
965 /*****************************************************************************
966 * Close: frees unused data
967 *****************************************************************************/
968 static void Close ( vlc_object_t * p_this )
970 demux_t * p_demux = (demux_t *)p_this;
971 demux_sys_t *p_sys = p_demux->p_sys;
972 unsigned int i_track;
974 msg_Dbg( p_demux, "freeing all memory" );
976 MP4_BoxFree( p_demux->s, p_sys->p_root );
977 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
979 MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
981 FREENULL( p_sys->track );
984 vlc_input_title_Delete( p_sys->p_title );
991 /****************************************************************************
992 * Local functions, specific to vlc
993 ****************************************************************************/
995 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
997 demux_sys_t *p_sys = p_demux->p_sys;
1000 p_sys->p_title = vlc_input_title_New();
1001 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1003 seekpoint_t *s = vlc_seekpoint_New();
1005 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1006 EnsureUTF8( s->psz_name );
1007 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1008 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1011 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1013 demux_sys_t *p_sys = p_demux->p_sys;
1015 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1017 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1018 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( p_demux, tk );
1019 const unsigned int i_size = MP4_TrackSampleSize( tk );
1021 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1024 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1025 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1029 seekpoint_t *s = vlc_seekpoint_New();
1031 s->psz_name = strndup( &p_buffer[2], i_len );
1032 EnsureUTF8( s->psz_name );
1034 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1036 if( !p_sys->p_title )
1037 p_sys->p_title = vlc_input_title_New();
1038 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1041 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1042 tk->chunk[tk->i_chunk].i_sample_count )
1046 static void LoadChapter( demux_t *p_demux )
1048 demux_sys_t *p_sys = p_demux->p_sys;
1051 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1053 LoadChapterGpac( p_demux, p_chpl );
1055 else if( p_sys->p_tref_chap )
1057 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1060 /* Load the first subtitle track like quicktime */
1061 for( i = 0; i < p_chap->i_entry_count; i++ )
1063 for( j = 0; j < p_sys->i_tracks; j++ )
1065 mp4_track_t *tk = &p_sys->track[j];
1066 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1067 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1070 if( j < p_sys->i_tracks )
1072 LoadChapterApple( p_demux, &p_sys->track[i] );
1079 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1080 static int TrackCreateChunksIndex( demux_t *p_demux,
1081 mp4_track_t *p_demux_track )
1083 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1086 unsigned int i_chunk;
1087 unsigned int i_index, i_last;
1089 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1090 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1091 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1093 return( VLC_EGENERIC );
1096 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1097 if( !p_demux_track->i_chunk_count )
1099 msg_Warn( p_demux, "no chunk defined" );
1100 return( VLC_EGENERIC );
1102 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1103 sizeof( mp4_chunk_t ) );
1105 /* first we read chunk offset */
1106 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1108 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1110 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1112 ck->i_first_dts = 0;
1113 ck->p_sample_count_dts = NULL;
1114 ck->p_sample_delta_dts = NULL;
1115 ck->p_sample_count_pts = NULL;
1116 ck->p_sample_offset_pts = NULL;
1119 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1120 to be used for the sample XXX begin to 1
1121 We construct it begining at the end */
1122 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1123 i_index = p_stsc->data.p_stsc->i_entry_count;
1126 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1127 return( VLC_EGENERIC );
1132 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1133 i_chunk < i_last; i_chunk++ )
1135 p_demux_track->chunk[i_chunk].i_sample_description_index =
1136 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1137 p_demux_track->chunk[i_chunk].i_sample_count =
1138 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1140 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1143 p_demux_track->chunk[0].i_sample_first = 0;
1144 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1146 p_demux_track->chunk[i_chunk].i_sample_first =
1147 p_demux_track->chunk[i_chunk-1].i_sample_first +
1148 p_demux_track->chunk[i_chunk-1].i_sample_count;
1151 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1152 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1157 static int TrackCreateSamplesIndex( demux_t *p_demux,
1158 mp4_track_t *p_demux_track )
1161 MP4_Box_data_stsz_t *stsz;
1162 MP4_Box_data_stts_t *stts;
1163 /* TODO use also stss and stsh table for seeking */
1164 /* FIXME use edit table */
1169 int64_t i_index_sample_used;
1174 * Gives the sample size for each samples. There is also a stz2 table
1175 * (compressed form) that we need to implement TODO */
1176 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1179 /* FIXME and stz2 */
1180 msg_Warn( p_demux, "cannot find STSZ box" );
1181 return VLC_EGENERIC;
1183 stsz = p_box->data.p_stsz;
1186 * Gives mapping between sample and decoding time
1188 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1191 msg_Warn( p_demux, "cannot find STTS box" );
1192 return VLC_EGENERIC;
1194 stts = p_box->data.p_stts;
1196 /* Use stsz table to create a sample number -> sample size table */
1197 p_demux_track->i_sample_count = stsz->i_sample_count;
1198 if( stsz->i_sample_size )
1200 /* 1: all sample have the same size, so no need to construct a table */
1201 p_demux_track->i_sample_size = stsz->i_sample_size;
1202 p_demux_track->p_sample_size = NULL;
1206 /* 2: each sample can have a different size */
1207 p_demux_track->i_sample_size = 0;
1208 p_demux_track->p_sample_size =
1209 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1211 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1213 p_demux_track->p_sample_size[i_sample] =
1214 stsz->i_entry_size[i_sample];
1218 /* Use stts table to create a sample number -> dts table.
1219 * XXX: if we don't want to waste too much memory, we can't expand
1220 * the box! so each chunk will contain an "extract" of this table
1221 * for fast research (problem with raw stream where a sample is sometime
1222 * just channels*bits_per_sample/8 */
1225 i_index = 0; i_index_sample_used = 0;
1226 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1228 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1229 int64_t i_entry, i_sample_count, i;
1232 ck->i_first_dts = i_last_dts;
1234 /* count how many entries are needed for this chunk
1235 * for p_sample_delta_dts and p_sample_count_dts */
1236 i_sample_count = ck->i_sample_count;
1239 while( i_sample_count > 0 )
1241 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1242 /* don't count already used sample in this entry */
1244 i_sample_count += i_index_sample_used;
1250 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1251 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1254 i_sample_count = ck->i_sample_count;
1255 for( i = 0; i < i_entry; i++ )
1260 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1262 i_used = __MIN( i_rest, i_sample_count );
1264 i_index_sample_used += i_used;
1265 i_sample_count -= i_used;
1267 ck->p_sample_count_dts[i] = i_used;
1268 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1270 i_last_dts += i_used * ck->p_sample_delta_dts[i];
1272 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1275 i_index_sample_used = 0;
1281 * Gives the delta between decoding time (dts) and composition table (pts)
1283 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1286 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1288 msg_Warn( p_demux, "CTTS table" );
1290 /* Create pts-dts table per chunk */
1291 i_index = 0; i_index_sample_used = 0;
1292 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1294 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1295 int64_t i_entry, i_sample_count, i;
1297 /* count how many entries are needed for this chunk
1298 * for p_sample_delta_dts and p_sample_count_dts */
1299 i_sample_count = ck->i_sample_count;
1302 while( i_sample_count > 0 )
1304 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1306 /* don't count already used sample in this entry */
1308 i_sample_count += i_index_sample_used;
1314 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1315 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1318 i_sample_count = ck->i_sample_count;
1319 for( i = 0; i < i_entry; i++ )
1324 i_rest = ctts->i_sample_count[i_index] -
1325 i_index_sample_used;
1327 i_used = __MIN( i_rest, i_sample_count );
1329 i_index_sample_used += i_used;
1330 i_sample_count -= i_used;
1332 ck->p_sample_count_pts[i] = i_used;
1333 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1335 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1338 i_index_sample_used = 0;
1344 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:"I64Fd"s",
1345 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1346 i_last_dts / p_demux_track->i_timescale );
1353 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1355 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1356 unsigned int i_chunk, es_out_id_t **pp_es )
1358 MP4_Box_t *p_sample;
1365 if( !p_track->chunk[i_chunk].i_sample_description_index )
1367 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1368 p_track->i_track_ID );
1369 return VLC_EGENERIC;
1372 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1373 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1376 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1378 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1379 p_track->i_track_ID );
1380 return VLC_EGENERIC;
1383 p_track->p_sample = p_sample;
1385 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1387 MP4_Box_data_sample_soun_t *p_soun;
1389 p_soun = p_sample->data.p_sample_soun;
1391 if( p_soun->i_qt_version == 0 )
1393 switch( p_sample->i_type )
1395 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1396 p_soun->i_qt_version = 1;
1397 p_soun->i_sample_per_packet = 64;
1398 p_soun->i_bytes_per_packet = 34;
1399 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1400 p_soun->i_bytes_per_sample = 2;
1402 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1403 p_soun->i_qt_version = 1;
1404 p_soun->i_sample_per_packet = 6;
1405 p_soun->i_bytes_per_packet = 2;
1406 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1407 p_soun->i_bytes_per_sample = 2;
1409 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1410 p_soun->i_qt_version = 1;
1411 p_soun->i_sample_per_packet = 12;
1412 p_soun->i_bytes_per_packet = 2;
1413 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1414 p_soun->i_bytes_per_sample = 2;
1416 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1417 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1418 p_soun->i_samplesize = 8;
1425 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1427 p_soun->i_qt_version = 0;
1432 /* It's a little ugly but .. there are special cases */
1433 switch( p_sample->i_type )
1435 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1436 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1437 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1440 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1441 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1443 /* Buggy files workaround */
1444 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1445 p_sample->data.p_sample_soun->i_sampleratehi) )
1447 MP4_Box_data_sample_soun_t *p_soun =
1448 p_sample->data.p_sample_soun;
1450 msg_Warn( p_demux, "i_timescale ("I64Fu") != i_sampleratehi "
1451 "(%u), making both equal (report any problem).",
1452 p_track->i_timescale, p_soun->i_sampleratehi );
1454 if( p_soun->i_sampleratehi )
1455 p_track->i_timescale = p_soun->i_sampleratehi;
1457 p_soun->i_sampleratehi = p_track->i_timescale;
1461 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1462 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1465 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1466 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1467 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1468 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1469 /* FIXME UTF-8 doesn't work here ? */
1470 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1473 case VLC_FOURCC('y','v','1','2'):
1474 p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1476 case VLC_FOURCC('y','u','v','2'):
1477 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1480 case VLC_FOURCC('i','n','2','4'):
1481 /* in in24/in32 there's enda-atom to tell it's little-endian (if present) */
1482 if( ( MP4_BoxGet( p_sample, "wave/enda" ) ) ||
1483 ( MP4_BoxGet( p_sample, "enda" ) ) )
1485 p_track->fmt.i_codec = VLC_FOURCC('4','2','n','i');
1487 p_track->fmt.i_codec = p_sample->i_type;
1492 p_track->fmt.i_codec = p_sample->i_type;
1496 /* now see if esds is present and if so create a data packet
1497 with decoder_specific_info */
1498 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1499 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1500 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1501 ( p_esds->data.p_esds )&&
1504 /* First update information based on i_objectTypeIndication */
1505 switch( p_decconfig->i_objectTypeIndication )
1507 case( 0x20 ): /* MPEG4 VIDEO */
1508 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1511 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1518 case( 0x65): /* MPEG2 video */
1519 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1521 /* Theses are MPEG2-AAC */
1522 case( 0x66): /* main profile */
1523 case( 0x67): /* Low complexity profile */
1524 case( 0x68): /* Scaleable Sampling rate profile */
1525 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1527 /* true MPEG 2 audio */
1529 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1531 case( 0x6a): /* MPEG1 video */
1532 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1534 case( 0x6b): /* MPEG1 audio */
1535 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1537 case( 0x6c ): /* jpeg */
1538 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1542 case( 0xe0 ): /* NeroDigital: dvd subs */
1543 if( p_track->fmt.i_cat == SPU_ES )
1545 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1546 if( p_track->i_width > 0 )
1547 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1548 if( p_track->i_height > 0 )
1549 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1554 /* Unknown entry, but don't touch i_fourcc */
1556 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1557 p_decconfig->i_objectTypeIndication,
1558 p_track->i_track_ID );
1561 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1562 if( p_track->fmt.i_extra > 0 )
1564 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1565 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1566 p_track->fmt.i_extra );
1571 switch( p_sample->i_type )
1573 /* qt decoder, send the complete chunk */
1574 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1575 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1576 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1577 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1578 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1579 p_track->fmt.i_extra =
1580 p_sample->data.p_sample_vide->i_qt_image_description;
1581 if( p_track->fmt.i_extra > 0 )
1583 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1584 memcpy( p_track->fmt.p_extra,
1585 p_sample->data.p_sample_vide->p_qt_image_description,
1586 p_track->fmt.i_extra);
1589 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1590 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1591 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1592 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1593 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1594 p_track->fmt.i_extra =
1595 p_sample->data.p_sample_soun->i_qt_description;
1596 if( p_track->fmt.i_extra > 0 )
1598 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1599 memcpy( p_track->fmt.p_extra,
1600 p_sample->data.p_sample_soun->p_qt_description,
1601 p_track->fmt.i_extra);
1605 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1606 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1608 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1612 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1613 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1614 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1615 p_track->fmt.i_extra );
1619 msg_Err( p_demux, "missing avcC" );
1631 /* some last initialisation */
1632 switch( p_track->fmt.i_cat )
1635 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1636 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1637 p_track->fmt.video.i_bits_per_pixel =
1638 p_sample->data.p_sample_vide->i_depth;
1640 /* fall on display size */
1641 if( p_track->fmt.video.i_width <= 0 )
1642 p_track->fmt.video.i_width = p_track->i_width;
1643 if( p_track->fmt.video.i_height <= 0 )
1644 p_track->fmt.video.i_height = p_track->i_height;
1646 /* Find out apect ratio from display size */
1647 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1648 /* Work-around buggy muxed files */
1649 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1650 p_track->fmt.video.i_aspect =
1651 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1653 /* Support for cropping (eg. in H263 files) */
1654 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1655 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1658 p_track->fmt.video.i_frame_rate = p_track->i_timescale;
1659 p_track->fmt.video.i_frame_rate_base = 1;
1661 if( p_track->fmt.video.i_frame_rate &&
1662 (p_box = MP4_BoxGet( p_track->p_stbl, "stts" )) &&
1663 p_box->data.p_stts->i_entry_count >= 1 )
1665 p_track->fmt.video.i_frame_rate_base =
1666 p_box->data.p_stts->i_sample_delta[0];
1672 p_track->fmt.audio.i_channels =
1673 p_sample->data.p_sample_soun->i_channelcount;
1674 p_track->fmt.audio.i_rate =
1675 p_sample->data.p_sample_soun->i_sampleratehi;
1676 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1677 p_sample->data.p_sample_soun->i_sampleratehi *
1678 p_sample->data.p_sample_soun->i_samplesize;
1679 p_track->fmt.audio.i_bitspersample =
1680 p_sample->data.p_sample_soun->i_samplesize;
1688 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1693 /* given a time it return sample/chunk
1694 * it also update elst field of the track
1696 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1697 int64_t i_start, uint32_t *pi_chunk,
1698 uint32_t *pi_sample )
1700 demux_sys_t *p_sys = p_demux->p_sys;
1703 unsigned int i_sample;
1704 unsigned int i_chunk;
1707 /* FIXME see if it's needed to check p_track->i_chunk_count */
1708 if( p_track->i_chunk_count == 0 )
1709 return( VLC_EGENERIC );
1711 /* handle elst (find the correct one) */
1712 MP4_TrackSetELST( p_demux, p_track, i_start );
1713 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1715 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1716 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1718 /* now calculate i_start for this elst */
1720 i_start -= p_track->i_elst_time * I64C(1000000) / p_sys->i_timescale;
1728 /* to track time scale */
1729 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1730 /* add elst offset */
1731 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1732 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1733 elst->i_media_time[p_track->i_elst] > 0 )
1735 i_start += elst->i_media_time[p_track->i_elst];
1738 msg_Dbg( p_demux, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd
1739 "ms (track)", p_track->i_elst,
1740 i_mvt * 1000 / p_sys->i_timescale,
1741 i_start * 1000 / p_track->i_timescale );
1745 /* convert absolute time to in timescale unit */
1746 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1749 /* we start from sample 0/chunk 0, hope it won't take too much time */
1750 /* *** find good chunk *** */
1751 for( i_chunk = 0; ; i_chunk++ )
1753 if( i_chunk + 1 >= p_track->i_chunk_count )
1755 /* at the end and can't check if i_start in this chunk,
1756 it will be check while searching i_sample */
1757 i_chunk = p_track->i_chunk_count - 1;
1761 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
1762 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1768 /* *** find sample in the chunk *** */
1769 i_sample = p_track->chunk[i_chunk].i_sample_first;
1770 i_dts = p_track->chunk[i_chunk].i_first_dts;
1771 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1774 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1775 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
1778 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1779 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1781 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1786 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1790 i_sample += ( i_start - i_dts ) /
1791 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1796 if( i_sample >= p_track->i_sample_count )
1798 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1799 "(seeking too far) chunk=%d sample=%d",
1800 p_track->i_track_ID, i_chunk, i_sample );
1801 return( VLC_EGENERIC );
1805 /* *** Try to find nearest sync points *** */
1806 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1808 unsigned int i_index;
1810 "track[Id 0x%x] using Sync Sample Box (stss)",
1811 p_track->i_track_ID );
1812 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1814 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1818 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1820 p_stss->data.p_stss->i_sample_number[i_index-1] );
1821 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1822 /* new i_sample is less than old so i_chunk can only decreased */
1823 while( i_chunk > 0 &&
1824 i_sample < p_track->chunk[i_chunk].i_sample_first )
1831 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1833 p_stss->data.p_stss->i_sample_number[i_index] );
1834 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1835 /* new i_sample is more than old so i_chunk can only increased */
1836 while( i_chunk < p_track->i_chunk_count - 1 &&
1837 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1838 p_track->chunk[i_chunk].i_sample_count )
1849 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1850 "Sample Box (stss)", p_track->i_track_ID );
1853 *pi_chunk = i_chunk;
1854 *pi_sample = i_sample;
1859 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1860 unsigned int i_chunk, unsigned int i_sample )
1862 vlc_bool_t b_reselect = VLC_FALSE;
1864 /* now see if actual es is ok */
1865 if( (p_track->i_chunk >= p_track->i_chunk_count - 1) ||
1866 (p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1867 p_track->chunk[i_chunk].i_sample_description_index) )
1869 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1870 p_track->i_track_ID );
1872 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1873 p_track->p_es, &b_reselect );
1875 es_out_Del( p_demux->out, p_track->p_es );
1877 p_track->p_es = NULL;
1879 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1881 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1882 p_track->i_track_ID );
1884 p_track->b_ok = VLC_FALSE;
1885 p_track->b_selected = VLC_FALSE;
1886 return VLC_EGENERIC;
1890 /* select again the new decoder */
1893 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1896 p_track->i_chunk = i_chunk;
1897 p_track->i_sample = i_sample;
1899 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1902 /****************************************************************************
1904 ****************************************************************************
1905 * Parse track information and create all needed data to run a track
1906 * If it succeed b_ok is set to 1 else to 0
1907 ****************************************************************************/
1908 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1909 MP4_Box_t *p_box_trak )
1911 demux_sys_t *p_sys = p_demux->p_sys;
1913 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1914 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1929 /* hint track unsupported */
1931 /* set default value (-> track unusable) */
1932 p_track->b_ok = VLC_FALSE;
1933 p_track->b_enable = VLC_FALSE;
1934 p_track->b_selected = VLC_FALSE;
1935 p_track->b_chapter = VLC_FALSE;
1937 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1944 /* do we launch this track by default ? */
1946 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1948 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1949 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1950 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1954 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
1957 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1958 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1960 if( ( !p_mdhd )||( !p_hdlr ) )
1965 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1967 for( i = 0; i < 3; i++ )
1969 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1973 switch( p_hdlr->data.p_hdlr->i_handler_type )
1975 case( FOURCC_soun ):
1976 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1980 p_track->fmt.i_cat = AUDIO_ES;
1983 case( FOURCC_vide ):
1984 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1988 p_track->fmt.i_cat = VIDEO_ES;
1991 case( FOURCC_text ):
1992 case( FOURCC_subp ):
1993 case( FOURCC_tx3g ):
1994 p_track->fmt.i_cat = SPU_ES;
2001 p_track->i_elst = 0;
2002 p_track->i_elst_time = 0;
2003 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2005 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2008 msg_Warn( p_demux, "elst box found" );
2009 for( i = 0; i < elst->i_entry_count; i++ )
2011 msg_Dbg( p_demux, " - [%d] duration="I64Fd"ms media time="I64Fd
2012 "ms) rate=%d.%d", i,
2013 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2014 elst->i_media_time[i] >= 0 ?
2015 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2017 elst->i_media_rate_integer[i],
2018 elst->i_media_rate_fraction[i] );
2025 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2027 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2028 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2033 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2034 p_track->b_drms = p_drms != NULL;
2035 p_track->p_drms = p_track->b_drms ?
2036 p_drms->data.p_sample_soun->p_drms : NULL;
2039 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
2041 p_track->fmt.psz_language = strdup( language );
2044 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2047 MP4_Box_t *p_0xa9xxx;
2048 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
2049 p_0xa9xxx = p_0xa9xxx->p_next )
2051 switch( p_0xa9xxx->i_type )
2053 case FOURCC_0xa9nam:
2054 p_track->fmt.psz_description =
2055 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
2061 /* Create chunk index table and sample index table */
2062 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2063 TrackCreateSamplesIndex( p_demux, p_track ) )
2065 return; /* cannot create chunks index */
2068 p_track->i_chunk = 0;
2069 p_track->i_sample = 0;
2071 /* Mark chapter only track */
2072 if( !p_track->b_enable && p_sys->p_tref_chap )
2074 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2077 for( i = 0; i < p_chap->i_entry_count; i++ )
2079 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2081 p_track->b_chapter = VLC_TRUE;
2088 p_track->p_es = NULL;
2089 if( TrackCreateES( p_demux,
2090 p_track, p_track->i_chunk,
2091 p_track->b_chapter ? NULL : &p_track->p_es ) )
2093 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2094 p_track->i_track_ID );
2097 p_track->b_ok = VLC_TRUE;
2101 for( i = 0; i < p_track->i_chunk_count; i++ )
2103 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2104 i, p_track->chunk[i].i_sample_count,
2105 p_track->chunk[i].i_first_dts );
2112 /****************************************************************************
2114 ****************************************************************************
2115 * Destroy a track created by MP4_TrackCreate.
2116 ****************************************************************************/
2117 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
2119 unsigned int i_chunk;
2121 p_track->b_ok = VLC_FALSE;
2122 p_track->b_enable = VLC_FALSE;
2123 p_track->b_selected = VLC_FALSE;
2125 es_format_Clean( &p_track->fmt );
2127 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2129 if( p_track->chunk )
2131 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2132 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2134 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2135 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2138 FREENULL( p_track->chunk );
2140 if( !p_track->i_sample_size )
2142 FREENULL( p_track->p_sample_size );
2146 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2149 if( !p_track->b_ok || p_track->b_chapter )
2151 return VLC_EGENERIC;
2154 if( p_track->b_selected )
2156 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2157 p_track->i_track_ID );
2161 return MP4_TrackSeek( p_demux, p_track, i_start );
2164 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2166 if( !p_track->b_ok || p_track->b_chapter )
2171 if( !p_track->b_selected )
2173 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2174 p_track->i_track_ID );
2179 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2180 p_track->p_es, VLC_FALSE );
2183 p_track->b_selected = VLC_FALSE;
2186 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2192 if( !p_track->b_ok || p_track->b_chapter )
2193 return VLC_EGENERIC;
2195 p_track->b_selected = VLC_FALSE;
2197 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2198 &i_chunk, &i_sample ) )
2200 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2201 p_track->i_track_ID );
2202 return VLC_EGENERIC;
2205 p_track->b_selected = VLC_TRUE;
2207 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
2210 p_track->b_selected = VLC_TRUE;
2212 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
2213 p_track->p_es, i_start );
2216 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2221 * 3 types: for audio
2224 #define QT_V0_MAX_SAMPLES 1024
2225 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2228 MP4_Box_data_sample_soun_t *p_soun;
2230 if( p_track->i_sample_size == 0 )
2232 /* most simple case */
2233 return p_track->p_sample_size[p_track->i_sample];
2235 if( p_track->fmt.i_cat != AUDIO_ES )
2237 return p_track->i_sample_size;
2240 p_soun = p_track->p_sample->data.p_sample_soun;
2242 if( p_soun->i_qt_version == 1 )
2244 i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
2245 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2247 else if( p_track->i_sample_size > 256 )
2249 /* We do that so we don't read too much data
2250 * (in this case we are likely dealing with compressed data) */
2251 i_size = p_track->i_sample_size;
2255 /* Read a bunch of samples at once */
2256 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2257 ( p_track->i_sample -
2258 p_track->chunk[p_track->i_chunk].i_sample_first );
2260 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2261 i_size = i_samples * p_track->i_sample_size;
2264 //fprintf( stderr, "size=%d\n", i_size );
2268 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2270 unsigned int i_sample;
2273 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2275 if( p_track->i_sample_size )
2277 MP4_Box_data_sample_soun_t *p_soun =
2278 p_track->p_sample->data.p_sample_soun;
2280 if( p_soun->i_qt_version == 0 )
2282 i_pos += ( p_track->i_sample -
2283 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2284 p_track->i_sample_size;
2288 /* we read chunk by chunk */
2294 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2295 i_sample < p_track->i_sample; i_sample++ )
2297 i_pos += p_track->p_sample_size[i_sample];
2304 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2306 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2308 MP4_Box_data_sample_soun_t *p_soun;
2310 p_soun = p_track->p_sample->data.p_sample_soun;
2312 if( p_soun->i_qt_version == 1 )
2314 /* chunk by chunk */
2316 p_track->chunk[p_track->i_chunk].i_sample_first +
2317 p_track->chunk[p_track->i_chunk].i_sample_count;
2319 else if( p_track->i_sample_size > 256 )
2321 /* We do that so we don't read too much data
2322 * (in this case we are likely dealing with compressed data) */
2323 p_track->i_sample += 1;
2328 p_track->i_sample += QT_V0_MAX_SAMPLES;
2329 if( p_track->i_sample >
2330 p_track->chunk[p_track->i_chunk].i_sample_first +
2331 p_track->chunk[p_track->i_chunk].i_sample_count )
2334 p_track->chunk[p_track->i_chunk].i_sample_first +
2335 p_track->chunk[p_track->i_chunk].i_sample_count;
2341 p_track->i_sample++;
2344 if( p_track->i_sample >= p_track->i_sample_count )
2345 return VLC_EGENERIC;
2347 /* Have we changed chunk ? */
2348 if( p_track->i_sample >=
2349 p_track->chunk[p_track->i_chunk].i_sample_first +
2350 p_track->chunk[p_track->i_chunk].i_sample_count )
2352 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2353 p_track->i_sample ) )
2355 msg_Warn( p_demux, "track[0x%x] will be disabled "
2356 "(cannot restart decoder)", p_track->i_track_ID );
2357 MP4_TrackUnselect( p_demux, p_track );
2358 return VLC_EGENERIC;
2362 /* Have we changed elst */
2363 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2365 demux_sys_t *p_sys = p_demux->p_sys;
2366 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2367 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2368 p_sys->i_timescale / (int64_t)1000000;
2370 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2371 i_mvt >= p_track->i_elst_time +
2372 elst->i_segment_duration[p_track->i_elst] )
2374 MP4_TrackSetELST( p_demux, p_track,
2375 MP4_TrackGetDTS( p_demux, p_track ) );
2382 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2385 demux_sys_t *p_sys = p_demux->p_sys;
2386 int i_elst_last = tk->i_elst;
2388 /* handle elst (find the correct one) */
2390 tk->i_elst_time = 0;
2391 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2393 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2394 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2396 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2398 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2400 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2404 tk->i_elst_time += i_dur;
2407 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2409 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2410 tk->i_elst = elst->i_entry_count - 1;
2411 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2414 if( elst->i_media_time[tk->i_elst] < 0 )
2417 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2420 if( i_elst_last != tk->i_elst )
2422 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2426 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
2427 playlist_item_t **pp_item )
2429 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
2430 vlc_bool_t b_play = var_CreateGetBool( p_demux, "playlist-autostart" );
2435 if( b_play && p_playlist->status.p_item &&
2436 p_playlist->status.p_item->p_input == input_GetItem(p_input) )
2438 msg_Dbg( p_playlist, "starting playlist playback" );
2439 *pp_item = p_playlist->status.p_item;
2444 input_item_t *p_current = input_GetItem( p_input );
2446 *pp_item = playlist_ItemGetByInput( p_playlist, p_current, VLC_FALSE );
2448 msg_Dbg( p_playlist, "unable to find item in playlist");
2450 msg_Dbg( p_playlist, "not starting playlist playback");
2453 vlc_object_release( p_input );