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 *****************************************************************************/
32 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_playlist.h>
38 #include <vlc_charset.h>
46 #define uint64_t int64_t
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
56 set_category( CAT_INPUT );
57 set_subcategory( SUBCAT_INPUT_DEMUX );
58 set_description( N_("MP4 stream demuxer") );
59 set_capability( "demux", 242 );
60 set_callbacks( Open, Close );
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Demux ( demux_t * );
67 static int DemuxRef( demux_t *p_demux ){ return 0;}
68 static int Seek ( demux_t *, mtime_t );
69 static int Control ( demux_t *, int, va_list );
71 /* Contain all information about a chunk */
74 uint64_t i_offset; /* absolute position of this chunk in the file */
75 uint32_t i_sample_description_index; /* index for SampleEntry to use */
76 uint32_t i_sample_count; /* how many samples in this chunk */
77 uint32_t i_sample_first; /* index of the first sample in this chunk */
79 /* now provide way to calculate pts, dts, and offset without to
80 much memory and with fast acces */
82 /* with this we can calculate dts/pts without waste memory */
84 uint32_t *p_sample_count_dts;
85 uint32_t *p_sample_delta_dts; /* dts delta */
87 uint32_t *p_sample_count_pts;
88 int32_t *p_sample_offset_pts; /* pts-dts */
90 /* TODO if needed add pts
91 but quickly *add* support for edts and seeking */
95 /* Contain all needed information for read all track with vlc */
98 unsigned int i_track_ID;/* this should be unique */
100 int b_ok; /* The track is usable */
101 int b_enable; /* is the trak enable by default */
102 bool b_selected; /* is the trak being played */
103 bool b_chapter; /* True when used for chapter only */
108 /* display size only ! */
112 /* more internal data */
113 uint64_t i_timescale; /* time scale for this track only */
116 int i_elst; /* current elst */
117 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
118 MP4_Box_t *p_elst; /* elst (could be NULL) */
120 /* give the next sample to read, i_chunk is to find quickly where
121 the sample is located */
122 uint32_t i_sample; /* next sample to read */
123 uint32_t i_chunk; /* chunk where next sample is stored */
124 /* total count of chunk and sample */
125 uint32_t i_chunk_count;
126 uint32_t i_sample_count;
128 mp4_chunk_t *chunk; /* always defined for each chunk */
130 /* sample size, p_sample_size defined only if i_sample_size == 0
131 else i_sample_size is size for all sample */
132 uint32_t i_sample_size;
133 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
134 too much time to do sumations each time*/
136 MP4_Box_t *p_stbl; /* will contain all timing information */
137 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
138 MP4_Box_t *p_sample;/* point on actual sdsd */
148 MP4_Box_t *p_root; /* container for the whole file */
152 uint64_t i_time; /* time position of the presentation
153 * in movie timescale */
154 uint64_t i_timescale; /* movie time scale */
155 uint64_t i_duration; /* movie duration */
156 unsigned int i_tracks; /* number of tracks */
157 mp4_track_t *track; /* array of track */
160 MP4_Box_t *p_tref_chap;
163 input_title_t *p_title;
166 /*****************************************************************************
167 * Declaration of local function
168 *****************************************************************************/
169 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *);
170 static void MP4_TrackDestroy( demux_t *, mp4_track_t * );
172 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
173 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
175 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
177 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
178 static int MP4_TrackSampleSize( mp4_track_t * );
179 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
180 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
182 static void MP4_UpdateSeekpoint( demux_t * );
184 /* Return time in s of a track */
185 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
187 #define chunk p_track->chunk[p_track->i_chunk]
189 unsigned int i_index = 0;
190 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
191 int64_t i_dts = chunk.i_first_dts;
193 while( i_sample > 0 )
195 if( i_sample > chunk.p_sample_count_dts[i_index] )
197 i_dts += chunk.p_sample_count_dts[i_index] *
198 chunk.p_sample_delta_dts[i_index];
199 i_sample -= chunk.p_sample_count_dts[i_index];
204 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
212 /* now handle elst */
213 if( p_track->p_elst )
215 demux_sys_t *p_sys = p_demux->p_sys;
216 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
218 /* convert to offset */
219 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
220 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
221 elst->i_media_time[p_track->i_elst] > 0 )
223 i_dts -= elst->i_media_time[p_track->i_elst];
226 /* add i_elst_time */
227 i_dts += p_track->i_elst_time * p_track->i_timescale /
230 if( i_dts < 0 ) i_dts = 0;
233 return INT64_C(1000000) * i_dts / p_track->i_timescale;
236 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
238 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
239 unsigned int i_index = 0;
240 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
242 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
245 for( i_index = 0;; i_index++ )
247 if( i_sample < ck->p_sample_count_pts[i_index] )
248 return ck->p_sample_offset_pts[i_index] * INT64_C(1000000) /
249 (int64_t)p_track->i_timescale;
251 i_sample -= ck->p_sample_count_pts[i_index];
255 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
257 return INT64_C(1000000) * p_sys->i_time / p_sys->i_timescale;
260 /* Function to lookup the currently playing item */
261 static bool FindItem( demux_t *p_demux, playlist_t *p_playlist,
262 playlist_item_t **pp_item );
264 static void LoadChapter( demux_t *p_demux );
266 /*****************************************************************************
267 * Open: check file and initializes MP4 structures
268 *****************************************************************************/
269 static int Open( vlc_object_t * p_this )
271 demux_t *p_demux = (demux_t *)p_this;
274 const uint8_t *p_peek;
284 /* A little test to see if it could be a mp4 */
285 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
287 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
298 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
305 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
308 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
312 /*Set exported functions */
313 p_demux->pf_demux = Demux;
314 p_demux->pf_control = Control;
316 /* create our structure that will contains all data */
317 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
318 memset( p_sys, 0, sizeof( demux_sys_t ) );
320 /* Now load all boxes ( except raw data ) */
321 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
323 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
327 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
329 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
331 switch( p_ftyp->data.p_ftyp->i_major_brand )
335 "ISO Media file (isom) version %d.",
336 p_ftyp->data.p_ftyp->i_minor_version );
340 "unrecognized major file specification (%4.4s).",
341 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
347 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
350 /* the file need to have one moov box */
351 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
353 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
357 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
360 /* we have a free box as a moov, rename it */
361 p_foov->i_type = FOURCC_moov;
364 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
366 playlist_t *p_playlist;
367 playlist_item_t *p_current, *p_item_in_category;
368 int i_count = MP4_BoxCount( p_rmra, "rmda" );
372 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
374 p_playlist = pl_Yield( p_demux );
377 b_play = FindItem( p_demux, p_playlist, &p_current );
378 p_item_in_category = playlist_ItemToNode( p_playlist, p_current, true );
379 p_current->p_input->i_type = ITEM_TYPE_PLAYLIST;
381 for( i = 0; i < i_count; i++ )
383 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
387 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
391 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
393 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
394 psz_ref, (char*)&i_ref_type );
396 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
398 if( strstr( psz_ref, "qt5gateQT" ) )
400 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
403 if( !strncmp( psz_ref, "http://", 7 ) ||
404 !strncmp( psz_ref, "rtsp://", 7 ) )
411 char *psz_path = strdup( p_demux->psz_path );
412 char *end = strrchr( psz_path, '/' );
413 if( end ) end[1] = '\0';
414 else *psz_path = '\0';
416 asprintf( &psz_absolute, "%s://%s%s",
417 p_demux->psz_access, psz_path, psz_ref );
420 psz_ref = psz_absolute;
425 input_item_t *p_input;
426 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
427 p_input = input_ItemNewExt( p_playlist, psz_ref, NULL,
429 input_ItemCopyOptions( p_current->p_input, p_input );
430 /* FIXME: playlist_BothAddInput() can fail */
431 playlist_BothAddInput( p_playlist, p_input,
433 PLAYLIST_APPEND, PLAYLIST_END,
435 vlc_gc_decref( p_input );
440 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
441 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
445 if( b_play && p_item_in_category &&
446 p_item_in_category->i_children > 0 )
448 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true,
449 p_item_in_category, NULL );
451 vlc_object_release( p_playlist );
455 msg_Err( p_demux, "can't find playlist" );
459 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
463 msg_Err( p_demux, "cannot find /moov/mvhd" );
468 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
469 p_demux->pf_demux = DemuxRef;
475 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
476 if( p_sys->i_timescale == 0 )
478 msg_Err( p_this, "bad timescale" );
481 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
484 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
486 msg_Err( p_demux, "cannot find any /moov/trak" );
489 msg_Dbg( p_demux, "found %d track%c",
491 p_sys->i_tracks ? 's':' ' );
493 /* allocate memory */
494 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
495 if( p_sys->track == NULL )
497 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
499 /* Search the first chap reference (like quicktime) */
500 p_sys->p_tref_chap = NULL;
501 for( i = 0; i < p_sys->i_tracks; i++ )
503 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
504 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
505 if( !p_chap || p_chap->data.p_tref_generic->i_entry_count <= 0 )
508 p_sys->p_tref_chap = p_chap;
512 /* now process each track and extract all usefull information */
513 for( i = 0; i < p_sys->i_tracks; i++ )
515 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
516 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
518 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
521 switch( p_sys->track[i].fmt.i_cat )
530 psz_cat = "subtitle";
538 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
539 p_sys->track[i].i_track_ID, psz_cat,
540 p_sys->track[i].b_enable ? "enable":"disable",
541 p_sys->track[i].fmt.psz_language ?
542 p_sys->track[i].fmt.psz_language : "undef" );
544 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
546 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
547 p_sys->track[i].i_track_ID,
548 p_sys->track[i].fmt.psz_language ?
549 p_sys->track[i].fmt.psz_language : "undef" );
553 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
554 p_sys->track[i].i_track_ID );
559 LoadChapter( p_demux );
566 MP4_BoxFree( p_demux->s, p_sys->p_root );
572 /*****************************************************************************
573 * Demux: read packet and send them to decoders
574 *****************************************************************************
575 * TODO check for newly selected track (ie audio upt to now )
576 *****************************************************************************/
577 static int Demux( demux_t *p_demux )
579 demux_sys_t *p_sys = p_demux->p_sys;
580 unsigned int i_track;
583 unsigned int i_track_selected;
585 /* check for newly selected/unselected track */
586 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
589 mp4_track_t *tk = &p_sys->track[i_track];
592 if( !tk->b_ok || tk->b_chapter ||
593 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
598 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
600 if( tk->b_selected && !b )
602 MP4_TrackUnselect( p_demux, tk );
604 else if( !tk->b_selected && b)
606 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
615 if( i_track_selected <= 0 )
617 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
618 if( p_sys->i_timescale > 0 )
620 int64_t i_length = (mtime_t)1000000 *
621 (mtime_t)p_sys->i_duration /
622 (mtime_t)p_sys->i_timescale;
623 if( MP4_GetMoviePTS( p_sys ) >= i_length )
628 msg_Warn( p_demux, "no track selected, exiting..." );
633 MP4_UpdateSeekpoint( p_demux );
635 /* first wait for the good time to read a packet */
636 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
638 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
640 /* we will read 100ms for each stream so ...*/
641 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
643 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
645 mp4_track_t *tk = &p_sys->track[i_track];
647 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
650 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
653 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
654 MP4_TrackGetDTS( p_demux, tk ),
655 MP4_GetMoviePTS( p_sys ) );
658 if( MP4_TrackSampleSize( tk ) > 0 )
664 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
666 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
668 MP4_TrackUnselect( p_demux, tk );
674 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
676 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
678 MP4_TrackUnselect( p_demux, tk );
682 if( tk->b_drms && tk->p_drms )
684 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
687 else if( tk->fmt.i_cat == SPU_ES )
689 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
690 p_block->i_buffer >= 2 )
692 uint16_t i_size = GetWBE( p_block->p_buffer );
694 if( i_size + 2 <= p_block->i_buffer )
697 /* remove the length field, and append a '\0' */
698 memmove( &p_block->p_buffer[0],
699 &p_block->p_buffer[2], i_size );
700 p_block->p_buffer[i_size] = '\0';
701 p_block->i_buffer = i_size + 1;
703 /* convert \r -> \n */
704 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
712 p_block->i_buffer = 0;
717 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
719 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
721 p_block->i_pts = p_block->i_dts + i_delta;
722 else if( tk->fmt.i_cat != VIDEO_ES )
723 p_block->i_pts = p_block->i_dts;
727 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
728 es_out_Send( p_demux->out, tk->p_es, p_block );
732 if( MP4_TrackNextSample( p_demux, tk ) )
740 static void MP4_UpdateSeekpoint( demux_t *p_demux )
742 demux_sys_t *p_sys = p_demux->p_sys;
745 if( !p_sys->p_title )
747 i_time = MP4_GetMoviePTS( p_sys );
748 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
750 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
755 if( i != p_demux->info.i_seekpoint && i >= 0 )
757 p_demux->info.i_seekpoint = i;
758 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
761 /*****************************************************************************
762 * Seek: Got to i_date
763 ******************************************************************************/
764 static int Seek( demux_t *p_demux, mtime_t i_date )
766 demux_sys_t *p_sys = p_demux->p_sys;
767 unsigned int i_track;
769 /* First update update global time */
770 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
771 p_sys->i_pcr = i_date;
773 /* Now for each stream try to go to this time */
774 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
776 mp4_track_t *tk = &p_sys->track[i_track];
777 MP4_TrackSeek( p_demux, tk, i_date );
779 MP4_UpdateSeekpoint( p_demux );
783 /*****************************************************************************
785 *****************************************************************************/
786 static int Control( demux_t *p_demux, int i_query, va_list args )
788 demux_sys_t *p_sys = p_demux->p_sys;
795 case DEMUX_GET_POSITION:
796 pf = (double*)va_arg( args, double * );
797 if( p_sys->i_duration > 0 )
799 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
807 case DEMUX_SET_POSITION:
808 f = (double)va_arg( args, double );
809 if( p_sys->i_timescale > 0 )
811 i64 = (int64_t)( f * (double)1000000 *
812 (double)p_sys->i_duration /
813 (double)p_sys->i_timescale );
814 return Seek( p_demux, i64 );
816 else return VLC_SUCCESS;
819 pi64 = (int64_t*)va_arg( args, int64_t * );
820 if( p_sys->i_timescale > 0 )
822 *pi64 = (mtime_t)1000000 *
823 (mtime_t)p_sys->i_time /
824 (mtime_t)p_sys->i_timescale;
830 i64 = (int64_t)va_arg( args, int64_t );
831 return Seek( p_demux, i64 );
833 case DEMUX_GET_LENGTH:
834 pi64 = (int64_t*)va_arg( args, int64_t * );
835 if( p_sys->i_timescale > 0 )
837 *pi64 = (mtime_t)1000000 *
838 (mtime_t)p_sys->i_duration /
839 (mtime_t)p_sys->i_timescale;
845 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
850 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
851 MP4_Box_t *p_0xa9xxx;
853 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
856 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
863 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
864 p_0xa9xxx = p_0xa9xxx->p_next )
867 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
870 /* FIXME FIXME: should convert from whatever the character
871 * encoding of MP4 meta data is to UTF-8. */
872 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
873 if( psz_utf ) { EnsureUTF8( psz_utf ); \
874 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
876 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
877 switch( p_0xa9xxx->i_type )
879 case FOURCC_0xa9nam: /* Full name */
880 SET( vlc_meta_SetTitle );
883 SET( vlc_meta_SetArtist );
886 SET( vlc_meta_SetArtist );
889 SET( vlc_meta_SetCopyright );
891 case FOURCC_0xa9day: /* Creation Date */
892 SET( vlc_meta_SetDate );
894 case FOURCC_0xa9des: /* Description */
895 SET( vlc_meta_SetDescription );
897 case FOURCC_0xa9gen: /* Genre */
898 SET( vlc_meta_SetGenre );
901 case FOURCC_0xa9alb: /* Album */
902 SET( vlc_meta_SetAlbum );
905 case FOURCC_0xa9trk: /* Track */
906 SET( vlc_meta_SetTracknum );
909 case FOURCC_0xa9cmt: /* Commment */
910 SET( vlc_meta_SetDescription );
913 case FOURCC_0xa9url: /* URL */
914 SET( vlc_meta_SetURL );
917 case FOURCC_0xa9enc: /* Encoded By */
918 SET( vlc_meta_SetEncodedBy );
922 case FOURCC_0xa9inf: /* Information */
923 case FOURCC_0xa9dir: /* Director */
924 case FOURCC_0xa9dis: /* Disclaimer */
925 case FOURCC_0xa9req: /* Requirements */
926 case FOURCC_0xa9fmt: /* Original Format */
927 case FOURCC_0xa9dsa: /* Display Source As */
928 case FOURCC_0xa9hst: /* Host Computer */
929 case FOURCC_0xa9prd: /* Producer */
930 case FOURCC_0xa9prf: /* Performers */
931 case FOURCC_0xa9ope: /* Original Performer */
932 case FOURCC_0xa9src: /* Providers Source Content */
933 case FOURCC_0xa9wrt: /* Writer */
934 case FOURCC_0xa9com: /* Composer */
935 case FOURCC_WLOC: /* Window Location */
936 /* TODO one day, but they aren't really meaningfull */
947 case DEMUX_GET_TITLE_INFO:
949 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
950 int *pi_int = (int*)va_arg( args, int* );
951 int *pi_title_offset = (int*)va_arg( args, int* );
952 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
954 if( !p_sys->p_title )
958 *ppp_title = malloc( sizeof( input_title_t**) );
959 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
960 *pi_title_offset = 0;
961 *pi_seekpoint_offset = 0;
964 case DEMUX_SET_TITLE:
966 const int i_title = (int)va_arg( args, int );
967 if( !p_sys->p_title || i_title != 0 )
971 case DEMUX_SET_SEEKPOINT:
973 const int i_seekpoint = (int)va_arg( args, int );
974 if( !p_sys->p_title )
976 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
979 case DEMUX_SET_NEXT_DEMUX_TIME:
980 case DEMUX_SET_GROUP:
981 case DEMUX_HAS_UNSUPPORTED_META:
982 case DEMUX_GET_ATTACHMENTS:
986 msg_Warn( p_demux, "control query %u unimplemented", i_query );
991 /*****************************************************************************
992 * Close: frees unused data
993 *****************************************************************************/
994 static void Close ( vlc_object_t * p_this )
996 demux_t * p_demux = (demux_t *)p_this;
997 demux_sys_t *p_sys = p_demux->p_sys;
998 unsigned int i_track;
1000 msg_Dbg( p_demux, "freeing all memory" );
1002 MP4_BoxFree( p_demux->s, p_sys->p_root );
1003 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1005 MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
1007 FREENULL( p_sys->track );
1009 if( p_sys->p_title )
1010 vlc_input_title_Delete( p_sys->p_title );
1017 /****************************************************************************
1018 * Local functions, specific to vlc
1019 ****************************************************************************/
1021 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1023 demux_sys_t *p_sys = p_demux->p_sys;
1026 p_sys->p_title = vlc_input_title_New();
1027 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1029 seekpoint_t *s = vlc_seekpoint_New();
1031 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1032 EnsureUTF8( s->psz_name );
1033 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1034 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1037 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1039 demux_sys_t *p_sys = p_demux->p_sys;
1041 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1043 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1044 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( p_demux, tk );
1045 const unsigned int i_size = MP4_TrackSampleSize( tk );
1047 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1050 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1051 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1055 seekpoint_t *s = vlc_seekpoint_New();
1057 s->psz_name = strndup( &p_buffer[2], i_len );
1058 EnsureUTF8( s->psz_name );
1060 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1062 if( !p_sys->p_title )
1063 p_sys->p_title = vlc_input_title_New();
1064 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1067 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1068 tk->chunk[tk->i_chunk].i_sample_count )
1072 static void LoadChapter( demux_t *p_demux )
1074 demux_sys_t *p_sys = p_demux->p_sys;
1077 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1079 LoadChapterGpac( p_demux, p_chpl );
1081 else if( p_sys->p_tref_chap )
1083 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1086 /* Load the first subtitle track like quicktime */
1087 for( i = 0; i < p_chap->i_entry_count; i++ )
1089 for( j = 0; j < p_sys->i_tracks; j++ )
1091 mp4_track_t *tk = &p_sys->track[j];
1092 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1093 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1096 if( j < p_sys->i_tracks )
1098 LoadChapterApple( p_demux, &p_sys->track[i] );
1105 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1106 static int TrackCreateChunksIndex( demux_t *p_demux,
1107 mp4_track_t *p_demux_track )
1109 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1112 unsigned int i_chunk;
1113 unsigned int i_index, i_last;
1115 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1116 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1117 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1119 return( VLC_EGENERIC );
1122 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1123 if( !p_demux_track->i_chunk_count )
1125 msg_Warn( p_demux, "no chunk defined" );
1126 return( VLC_EGENERIC );
1128 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1129 sizeof( mp4_chunk_t ) );
1130 if( p_demux_track->chunk == NULL )
1135 /* first we read chunk offset */
1136 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1138 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1140 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1142 ck->i_first_dts = 0;
1143 ck->p_sample_count_dts = NULL;
1144 ck->p_sample_delta_dts = NULL;
1145 ck->p_sample_count_pts = NULL;
1146 ck->p_sample_offset_pts = NULL;
1149 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1150 to be used for the sample XXX begin to 1
1151 We construct it begining at the end */
1152 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1153 i_index = p_stsc->data.p_stsc->i_entry_count;
1156 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1157 return( VLC_EGENERIC );
1162 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1163 i_chunk < i_last; i_chunk++ )
1165 if( i_chunk >= p_demux_track->i_chunk_count )
1167 msg_Warn( p_demux, "corrupted chunk table" );
1168 return VLC_EGENERIC;
1171 p_demux_track->chunk[i_chunk].i_sample_description_index =
1172 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1173 p_demux_track->chunk[i_chunk].i_sample_count =
1174 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1176 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1179 p_demux_track->chunk[0].i_sample_first = 0;
1180 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1182 p_demux_track->chunk[i_chunk].i_sample_first =
1183 p_demux_track->chunk[i_chunk-1].i_sample_first +
1184 p_demux_track->chunk[i_chunk-1].i_sample_count;
1187 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1188 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1193 static int TrackCreateSamplesIndex( demux_t *p_demux,
1194 mp4_track_t *p_demux_track )
1197 MP4_Box_data_stsz_t *stsz;
1198 MP4_Box_data_stts_t *stts;
1199 /* TODO use also stss and stsh table for seeking */
1200 /* FIXME use edit table */
1205 int64_t i_index_sample_used;
1210 * Gives the sample size for each samples. There is also a stz2 table
1211 * (compressed form) that we need to implement TODO */
1212 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1215 /* FIXME and stz2 */
1216 msg_Warn( p_demux, "cannot find STSZ box" );
1217 return VLC_EGENERIC;
1219 stsz = p_box->data.p_stsz;
1222 * Gives mapping between sample and decoding time
1224 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1227 msg_Warn( p_demux, "cannot find STTS box" );
1228 return VLC_EGENERIC;
1230 stts = p_box->data.p_stts;
1232 /* Use stsz table to create a sample number -> sample size table */
1233 p_demux_track->i_sample_count = stsz->i_sample_count;
1234 if( stsz->i_sample_size )
1236 /* 1: all sample have the same size, so no need to construct a table */
1237 p_demux_track->i_sample_size = stsz->i_sample_size;
1238 p_demux_track->p_sample_size = NULL;
1242 /* 2: each sample can have a different size */
1243 p_demux_track->i_sample_size = 0;
1244 p_demux_track->p_sample_size =
1245 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1246 if( p_demux_track->p_sample_size == NULL )
1249 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1251 p_demux_track->p_sample_size[i_sample] =
1252 stsz->i_entry_size[i_sample];
1256 /* Use stts table to create a sample number -> dts table.
1257 * XXX: if we don't want to waste too much memory, we can't expand
1258 * the box! so each chunk will contain an "extract" of this table
1259 * for fast research (problem with raw stream where a sample is sometime
1260 * just channels*bits_per_sample/8 */
1263 i_index = 0; i_index_sample_used = 0;
1264 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1266 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1267 int64_t i_entry, i_sample_count, i;
1270 ck->i_first_dts = i_last_dts;
1272 /* count how many entries are needed for this chunk
1273 * for p_sample_delta_dts and p_sample_count_dts */
1274 i_sample_count = ck->i_sample_count;
1277 while( i_sample_count > 0 )
1279 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1280 /* don't count already used sample in this entry */
1282 i_sample_count += i_index_sample_used;
1288 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1289 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1292 i_sample_count = ck->i_sample_count;
1293 for( i = 0; i < i_entry; i++ )
1298 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1300 i_used = __MIN( i_rest, i_sample_count );
1302 i_index_sample_used += i_used;
1303 i_sample_count -= i_used;
1305 ck->p_sample_count_dts[i] = i_used;
1306 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1308 i_last_dts += i_used * ck->p_sample_delta_dts[i];
1310 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1313 i_index_sample_used = 0;
1319 * Gives the delta between decoding time (dts) and composition table (pts)
1321 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1324 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1326 msg_Warn( p_demux, "CTTS table" );
1328 /* Create pts-dts table per chunk */
1329 i_index = 0; i_index_sample_used = 0;
1330 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1332 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1333 int64_t i_entry, i_sample_count, i;
1335 /* count how many entries are needed for this chunk
1336 * for p_sample_delta_dts and p_sample_count_dts */
1337 i_sample_count = ck->i_sample_count;
1340 while( i_sample_count > 0 )
1342 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1344 /* don't count already used sample in this entry */
1346 i_sample_count += i_index_sample_used;
1352 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1353 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1356 i_sample_count = ck->i_sample_count;
1357 for( i = 0; i < i_entry; i++ )
1362 i_rest = ctts->i_sample_count[i_index] -
1363 i_index_sample_used;
1365 i_used = __MIN( i_rest, i_sample_count );
1367 i_index_sample_used += i_used;
1368 i_sample_count -= i_used;
1370 ck->p_sample_count_pts[i] = i_used;
1371 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1373 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1376 i_index_sample_used = 0;
1382 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1383 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1384 i_last_dts / p_demux_track->i_timescale );
1391 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1393 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1394 unsigned int i_chunk, es_out_id_t **pp_es )
1396 MP4_Box_t *p_sample;
1403 if( !p_track->chunk[i_chunk].i_sample_description_index )
1405 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1406 p_track->i_track_ID );
1407 return VLC_EGENERIC;
1410 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1411 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1414 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1416 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1417 p_track->i_track_ID );
1418 return VLC_EGENERIC;
1421 p_track->p_sample = p_sample;
1423 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1425 MP4_Box_data_sample_soun_t *p_soun;
1427 p_soun = p_sample->data.p_sample_soun;
1429 if( p_soun->i_qt_version == 0 )
1431 switch( p_sample->i_type )
1433 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1434 p_soun->i_qt_version = 1;
1435 p_soun->i_sample_per_packet = 64;
1436 p_soun->i_bytes_per_packet = 34;
1437 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1438 p_soun->i_bytes_per_sample = 2;
1440 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1441 p_soun->i_qt_version = 1;
1442 p_soun->i_sample_per_packet = 6;
1443 p_soun->i_bytes_per_packet = 2;
1444 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1445 p_soun->i_bytes_per_sample = 2;
1447 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1448 p_soun->i_qt_version = 1;
1449 p_soun->i_sample_per_packet = 12;
1450 p_soun->i_bytes_per_packet = 2;
1451 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1452 p_soun->i_bytes_per_sample = 2;
1454 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1455 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1456 p_soun->i_samplesize = 8;
1463 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1465 p_soun->i_qt_version = 0;
1470 /* It's a little ugly but .. there are special cases */
1471 switch( p_sample->i_type )
1473 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1474 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1475 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1478 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1479 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1481 /* Buggy files workaround */
1482 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1483 p_sample->data.p_sample_soun->i_sampleratehi) )
1485 MP4_Box_data_sample_soun_t *p_soun =
1486 p_sample->data.p_sample_soun;
1488 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1489 "(%u), making both equal (report any problem).",
1490 p_track->i_timescale, p_soun->i_sampleratehi );
1492 if( p_soun->i_sampleratehi )
1493 p_track->i_timescale = p_soun->i_sampleratehi;
1495 p_soun->i_sampleratehi = p_track->i_timescale;
1499 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1500 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1503 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1504 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1505 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1506 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1507 /* FIXME UTF-8 doesn't work here ? */
1508 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1511 case VLC_FOURCC('y','v','1','2'):
1512 p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1514 case VLC_FOURCC('y','u','v','2'):
1515 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1518 case VLC_FOURCC('i','n','2','4'):
1519 /* in in24/in32 there's enda-atom to tell it's little-endian (if present) */
1520 if( ( MP4_BoxGet( p_sample, "wave/enda" ) ) ||
1521 ( MP4_BoxGet( p_sample, "enda" ) ) )
1523 p_track->fmt.i_codec = VLC_FOURCC('4','2','n','i');
1525 p_track->fmt.i_codec = p_sample->i_type;
1530 p_track->fmt.i_codec = p_sample->i_type;
1534 /* now see if esds is present and if so create a data packet
1535 with decoder_specific_info */
1536 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1537 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1538 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1539 ( p_esds->data.p_esds )&&
1542 /* First update information based on i_objectTypeIndication */
1543 switch( p_decconfig->i_objectTypeIndication )
1545 case( 0x20 ): /* MPEG4 VIDEO */
1546 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1549 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1556 case( 0x65): /* MPEG2 video */
1557 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1559 /* Theses are MPEG2-AAC */
1560 case( 0x66): /* main profile */
1561 case( 0x67): /* Low complexity profile */
1562 case( 0x68): /* Scaleable Sampling rate profile */
1563 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1565 /* true MPEG 2 audio */
1567 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1569 case( 0x6a): /* MPEG1 video */
1570 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1572 case( 0x6b): /* MPEG1 audio */
1573 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1575 case( 0x6c ): /* jpeg */
1576 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1580 case( 0xe0 ): /* NeroDigital: dvd subs */
1581 if( p_track->fmt.i_cat == SPU_ES )
1583 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1584 if( p_track->i_width > 0 )
1585 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1586 if( p_track->i_height > 0 )
1587 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1592 /* Unknown entry, but don't touch i_fourcc */
1594 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1595 p_decconfig->i_objectTypeIndication,
1596 p_track->i_track_ID );
1599 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1600 if( p_track->fmt.i_extra > 0 )
1602 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1603 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1604 p_track->fmt.i_extra );
1609 switch( p_sample->i_type )
1611 /* qt decoder, send the complete chunk */
1612 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1613 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1614 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1615 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1616 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1617 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1618 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1619 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1620 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1621 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1622 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1623 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1624 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1626 /* qt decoder, send the complete chunk */
1627 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1628 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1629 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1630 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1631 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1632 p_track->fmt.i_extra =
1633 p_sample->data.p_sample_vide->i_qt_image_description;
1634 if( p_track->fmt.i_extra > 0 )
1636 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1637 memcpy( p_track->fmt.p_extra,
1638 p_sample->data.p_sample_vide->p_qt_image_description,
1639 p_track->fmt.i_extra);
1642 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1643 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1644 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1645 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1646 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1647 p_track->fmt.i_extra =
1648 p_sample->data.p_sample_soun->i_qt_description;
1649 if( p_track->fmt.i_extra > 0 )
1651 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1652 memcpy( p_track->fmt.p_extra,
1653 p_sample->data.p_sample_soun->p_qt_description,
1654 p_track->fmt.i_extra);
1658 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1659 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1661 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1665 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1666 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1667 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1668 p_track->fmt.i_extra );
1672 msg_Err( p_demux, "missing avcC" );
1684 /* some last initialisation */
1685 switch( p_track->fmt.i_cat )
1688 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1689 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1690 p_track->fmt.video.i_bits_per_pixel =
1691 p_sample->data.p_sample_vide->i_depth;
1693 /* fall on display size */
1694 if( p_track->fmt.video.i_width <= 0 )
1695 p_track->fmt.video.i_width = p_track->i_width;
1696 if( p_track->fmt.video.i_height <= 0 )
1697 p_track->fmt.video.i_height = p_track->i_height;
1699 /* Find out apect ratio from display size */
1700 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1701 /* Work-around buggy muxed files */
1702 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1703 p_track->fmt.video.i_aspect =
1704 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1706 /* Support for cropping (eg. in H263 files) */
1707 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1708 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1711 p_track->fmt.video.i_frame_rate = p_track->i_timescale;
1712 p_track->fmt.video.i_frame_rate_base = 1;
1714 if( p_track->fmt.video.i_frame_rate &&
1715 (p_box = MP4_BoxGet( p_track->p_stbl, "stts" )) &&
1716 p_box->data.p_stts->i_entry_count >= 1 )
1718 p_track->fmt.video.i_frame_rate_base =
1719 p_box->data.p_stts->i_sample_delta[0];
1725 p_track->fmt.audio.i_channels =
1726 p_sample->data.p_sample_soun->i_channelcount;
1727 p_track->fmt.audio.i_rate =
1728 p_sample->data.p_sample_soun->i_sampleratehi;
1729 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1730 p_sample->data.p_sample_soun->i_sampleratehi *
1731 p_sample->data.p_sample_soun->i_samplesize;
1732 p_track->fmt.audio.i_bitspersample =
1733 p_sample->data.p_sample_soun->i_samplesize;
1741 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1746 /* given a time it return sample/chunk
1747 * it also update elst field of the track
1749 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1750 int64_t i_start, uint32_t *pi_chunk,
1751 uint32_t *pi_sample )
1753 demux_sys_t *p_sys = p_demux->p_sys;
1756 unsigned int i_sample;
1757 unsigned int i_chunk;
1760 /* FIXME see if it's needed to check p_track->i_chunk_count */
1761 if( p_track->i_chunk_count == 0 )
1762 return( VLC_EGENERIC );
1764 /* handle elst (find the correct one) */
1765 MP4_TrackSetELST( p_demux, p_track, i_start );
1766 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1768 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1769 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1771 /* now calculate i_start for this elst */
1773 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
1781 /* to track time scale */
1782 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1783 /* add elst offset */
1784 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1785 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1786 elst->i_media_time[p_track->i_elst] > 0 )
1788 i_start += elst->i_media_time[p_track->i_elst];
1791 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
1792 "ms (track)", p_track->i_elst,
1793 i_mvt * 1000 / p_sys->i_timescale,
1794 i_start * 1000 / p_track->i_timescale );
1798 /* convert absolute time to in timescale unit */
1799 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1802 /* we start from sample 0/chunk 0, hope it won't take too much time */
1803 /* *** find good chunk *** */
1804 for( i_chunk = 0; ; i_chunk++ )
1806 if( i_chunk + 1 >= p_track->i_chunk_count )
1808 /* at the end and can't check if i_start in this chunk,
1809 it will be check while searching i_sample */
1810 i_chunk = p_track->i_chunk_count - 1;
1814 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
1815 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1821 /* *** find sample in the chunk *** */
1822 i_sample = p_track->chunk[i_chunk].i_sample_first;
1823 i_dts = p_track->chunk[i_chunk].i_first_dts;
1824 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1827 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1828 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
1831 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1832 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1834 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1839 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1843 i_sample += ( i_start - i_dts ) /
1844 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1849 if( i_sample >= p_track->i_sample_count )
1851 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1852 "(seeking too far) chunk=%d sample=%d",
1853 p_track->i_track_ID, i_chunk, i_sample );
1854 return( VLC_EGENERIC );
1858 /* *** Try to find nearest sync points *** */
1859 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1861 unsigned int i_index;
1863 "track[Id 0x%x] using Sync Sample Box (stss)",
1864 p_track->i_track_ID );
1865 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1867 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1871 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1873 p_stss->data.p_stss->i_sample_number[i_index-1] );
1874 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1875 /* new i_sample is less than old so i_chunk can only decreased */
1876 while( i_chunk > 0 &&
1877 i_sample < p_track->chunk[i_chunk].i_sample_first )
1884 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1886 p_stss->data.p_stss->i_sample_number[i_index] );
1887 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1888 /* new i_sample is more than old so i_chunk can only increased */
1889 while( i_chunk < p_track->i_chunk_count - 1 &&
1890 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1891 p_track->chunk[i_chunk].i_sample_count )
1902 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1903 "Sample Box (stss)", p_track->i_track_ID );
1906 *pi_chunk = i_chunk;
1907 *pi_sample = i_sample;
1912 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1913 unsigned int i_chunk, unsigned int i_sample )
1915 bool b_reselect = false;
1917 /* now see if actual es is ok */
1918 if( (p_track->i_chunk >= p_track->i_chunk_count - 1) ||
1919 (p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1920 p_track->chunk[i_chunk].i_sample_description_index) )
1922 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1923 p_track->i_track_ID );
1925 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1926 p_track->p_es, &b_reselect );
1928 es_out_Del( p_demux->out, p_track->p_es );
1930 p_track->p_es = NULL;
1932 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1934 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1935 p_track->i_track_ID );
1937 p_track->b_ok = false;
1938 p_track->b_selected = false;
1939 return VLC_EGENERIC;
1943 /* select again the new decoder */
1946 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1949 p_track->i_chunk = i_chunk;
1950 p_track->i_sample = i_sample;
1952 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1955 /****************************************************************************
1957 ****************************************************************************
1958 * Parse track information and create all needed data to run a track
1959 * If it succeed b_ok is set to 1 else to 0
1960 ****************************************************************************/
1961 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1962 MP4_Box_t *p_box_trak )
1964 demux_sys_t *p_sys = p_demux->p_sys;
1966 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1967 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1982 /* hint track unsupported */
1984 /* set default value (-> track unusable) */
1985 p_track->b_ok = false;
1986 p_track->b_enable = false;
1987 p_track->b_selected = false;
1988 p_track->b_chapter = false;
1990 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1997 /* do we launch this track by default ? */
1999 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2001 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2002 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2003 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2007 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2010 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2011 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2013 if( ( !p_mdhd )||( !p_hdlr ) )
2018 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2019 if( !p_track->i_timescale )
2022 for( i = 0; i < 3; i++ )
2024 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2028 switch( p_hdlr->data.p_hdlr->i_handler_type )
2030 case( FOURCC_soun ):
2031 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2035 p_track->fmt.i_cat = AUDIO_ES;
2038 case( FOURCC_vide ):
2039 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2043 p_track->fmt.i_cat = VIDEO_ES;
2046 case( FOURCC_text ):
2047 case( FOURCC_subp ):
2048 case( FOURCC_tx3g ):
2049 p_track->fmt.i_cat = SPU_ES;
2056 p_track->i_elst = 0;
2057 p_track->i_elst_time = 0;
2058 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2060 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2063 msg_Warn( p_demux, "elst box found" );
2064 for( i = 0; i < elst->i_entry_count; i++ )
2066 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2067 "ms) rate=%d.%d", i,
2068 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2069 elst->i_media_time[i] >= 0 ?
2070 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2072 elst->i_media_rate_integer[i],
2073 elst->i_media_rate_fraction[i] );
2080 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2082 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2083 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2088 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2089 p_track->b_drms = p_drms != NULL;
2090 p_track->p_drms = p_track->b_drms ?
2091 p_drms->data.p_sample_soun->p_drms : NULL;
2094 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
2096 p_track->fmt.psz_language = strdup( language );
2099 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2102 MP4_Box_t *p_0xa9xxx;
2103 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
2104 p_0xa9xxx = p_0xa9xxx->p_next )
2106 switch( p_0xa9xxx->i_type )
2108 case FOURCC_0xa9nam:
2109 p_track->fmt.psz_description =
2110 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
2116 /* Create chunk index table and sample index table */
2117 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2118 TrackCreateSamplesIndex( p_demux, p_track ) )
2120 return; /* cannot create chunks index */
2123 p_track->i_chunk = 0;
2124 p_track->i_sample = 0;
2126 /* Mark chapter only track */
2127 if( !p_track->b_enable && p_sys->p_tref_chap )
2129 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2132 for( i = 0; i < p_chap->i_entry_count; i++ )
2134 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2136 p_track->b_chapter = true;
2143 p_track->p_es = NULL;
2144 if( TrackCreateES( p_demux,
2145 p_track, p_track->i_chunk,
2146 p_track->b_chapter ? NULL : &p_track->p_es ) )
2148 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2149 p_track->i_track_ID );
2152 p_track->b_ok = true;
2156 for( i = 0; i < p_track->i_chunk_count; i++ )
2158 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2159 i, p_track->chunk[i].i_sample_count,
2160 p_track->chunk[i].i_first_dts );
2167 /****************************************************************************
2169 ****************************************************************************
2170 * Destroy a track created by MP4_TrackCreate.
2171 ****************************************************************************/
2172 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
2174 unsigned int i_chunk;
2176 p_track->b_ok = false;
2177 p_track->b_enable = false;
2178 p_track->b_selected = false;
2180 es_format_Clean( &p_track->fmt );
2182 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2184 if( p_track->chunk )
2186 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2187 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2189 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2190 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2193 FREENULL( p_track->chunk );
2195 if( !p_track->i_sample_size )
2197 FREENULL( p_track->p_sample_size );
2201 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2204 if( !p_track->b_ok || p_track->b_chapter )
2206 return VLC_EGENERIC;
2209 if( p_track->b_selected )
2211 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2212 p_track->i_track_ID );
2216 return MP4_TrackSeek( p_demux, p_track, i_start );
2219 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2221 if( !p_track->b_ok || p_track->b_chapter )
2226 if( !p_track->b_selected )
2228 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2229 p_track->i_track_ID );
2234 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2235 p_track->p_es, false );
2238 p_track->b_selected = false;
2241 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2247 if( !p_track->b_ok || p_track->b_chapter )
2248 return VLC_EGENERIC;
2250 p_track->b_selected = false;
2252 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2253 &i_chunk, &i_sample ) )
2255 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2256 p_track->i_track_ID );
2257 return VLC_EGENERIC;
2260 p_track->b_selected = true;
2262 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
2265 p_track->b_selected = true;
2267 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
2268 p_track->p_es, i_start );
2271 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2276 * 3 types: for audio
2279 #define QT_V0_MAX_SAMPLES 1024
2280 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2283 MP4_Box_data_sample_soun_t *p_soun;
2285 if( p_track->i_sample_size == 0 )
2287 /* most simple case */
2288 return p_track->p_sample_size[p_track->i_sample];
2290 if( p_track->fmt.i_cat != AUDIO_ES )
2292 return p_track->i_sample_size;
2295 p_soun = p_track->p_sample->data.p_sample_soun;
2297 if( p_soun->i_qt_version == 1 )
2299 i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
2300 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2302 else if( p_track->i_sample_size > 256 )
2304 /* We do that so we don't read too much data
2305 * (in this case we are likely dealing with compressed data) */
2306 i_size = p_track->i_sample_size;
2310 /* Read a bunch of samples at once */
2311 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2312 ( p_track->i_sample -
2313 p_track->chunk[p_track->i_chunk].i_sample_first );
2315 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2316 i_size = i_samples * p_track->i_sample_size;
2319 //fprintf( stderr, "size=%d\n", i_size );
2323 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2325 unsigned int i_sample;
2328 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2330 if( p_track->i_sample_size )
2332 MP4_Box_data_sample_soun_t *p_soun =
2333 p_track->p_sample->data.p_sample_soun;
2335 if( p_soun->i_qt_version == 0 )
2337 i_pos += ( p_track->i_sample -
2338 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2339 p_track->i_sample_size;
2343 /* we read chunk by chunk */
2349 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2350 i_sample < p_track->i_sample; i_sample++ )
2352 i_pos += p_track->p_sample_size[i_sample];
2359 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2361 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2363 MP4_Box_data_sample_soun_t *p_soun;
2365 p_soun = p_track->p_sample->data.p_sample_soun;
2367 if( p_soun->i_qt_version == 1 )
2369 /* chunk by chunk */
2371 p_track->chunk[p_track->i_chunk].i_sample_first +
2372 p_track->chunk[p_track->i_chunk].i_sample_count;
2374 else if( p_track->i_sample_size > 256 )
2376 /* We do that so we don't read too much data
2377 * (in this case we are likely dealing with compressed data) */
2378 p_track->i_sample += 1;
2383 p_track->i_sample += QT_V0_MAX_SAMPLES;
2384 if( p_track->i_sample >
2385 p_track->chunk[p_track->i_chunk].i_sample_first +
2386 p_track->chunk[p_track->i_chunk].i_sample_count )
2389 p_track->chunk[p_track->i_chunk].i_sample_first +
2390 p_track->chunk[p_track->i_chunk].i_sample_count;
2396 p_track->i_sample++;
2399 if( p_track->i_sample >= p_track->i_sample_count )
2400 return VLC_EGENERIC;
2402 /* Have we changed chunk ? */
2403 if( p_track->i_sample >=
2404 p_track->chunk[p_track->i_chunk].i_sample_first +
2405 p_track->chunk[p_track->i_chunk].i_sample_count )
2407 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2408 p_track->i_sample ) )
2410 msg_Warn( p_demux, "track[0x%x] will be disabled "
2411 "(cannot restart decoder)", p_track->i_track_ID );
2412 MP4_TrackUnselect( p_demux, p_track );
2413 return VLC_EGENERIC;
2417 /* Have we changed elst */
2418 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2420 demux_sys_t *p_sys = p_demux->p_sys;
2421 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2422 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2423 p_sys->i_timescale / (int64_t)1000000;
2425 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2426 i_mvt >= p_track->i_elst_time +
2427 elst->i_segment_duration[p_track->i_elst] )
2429 MP4_TrackSetELST( p_demux, p_track,
2430 MP4_TrackGetDTS( p_demux, p_track ) );
2437 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2440 demux_sys_t *p_sys = p_demux->p_sys;
2441 int i_elst_last = tk->i_elst;
2443 /* handle elst (find the correct one) */
2445 tk->i_elst_time = 0;
2446 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2448 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2449 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2451 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2453 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2455 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2459 tk->i_elst_time += i_dur;
2462 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2464 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2465 tk->i_elst = elst->i_entry_count - 1;
2466 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2469 if( elst->i_media_time[tk->i_elst] < 0 )
2472 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2475 if( i_elst_last != tk->i_elst )
2477 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2481 static bool FindItem( demux_t *p_demux, playlist_t *p_playlist,
2482 playlist_item_t **pp_item )
2484 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
2485 bool b_play = var_CreateGetBool( p_demux, "playlist-autostart" );
2490 if( b_play && p_playlist->status.p_item &&
2491 p_playlist->status.p_item->p_input == input_GetItem(p_input) )
2493 msg_Dbg( p_playlist, "starting playlist playback" );
2494 *pp_item = p_playlist->status.p_item;
2499 input_item_t *p_current = input_GetItem( p_input );
2501 *pp_item = playlist_ItemGetByInput( p_playlist, p_current, false );
2503 msg_Dbg( p_playlist, "unable to find item in playlist");
2505 msg_Dbg( p_playlist, "not starting playlist playback");
2508 vlc_object_release( p_input );