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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
37 #include <vlc_charset.h>
38 #include <vlc_iso_lang.h>
40 #include <vlc_input.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 ){ (void)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 *, bool b_force_enable );
170 static void MP4_TrackDestroy( 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( 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 static void LoadChapter( demux_t *p_demux );
262 /*****************************************************************************
263 * Open: check file and initializes MP4 structures
264 *****************************************************************************/
265 static int Open( vlc_object_t * p_this )
267 demux_t *p_demux = (demux_t *)p_this;
270 const uint8_t *p_peek;
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 int i_count = MP4_BoxCount( p_rmra, "rmda" );
366 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
368 input_thread_t * p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
369 input_item_t * p_current = input_GetItem( p_input );
370 p_current->i_type = ITEM_TYPE_PLAYLIST;
372 for( i = 0; i < i_count; i++ )
374 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
378 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
382 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
384 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
385 psz_ref, (char*)&i_ref_type );
387 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
389 if( strstr( psz_ref, "qt5gateQT" ) )
391 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
394 if( !strncmp( psz_ref, "http://", 7 ) ||
395 !strncmp( psz_ref, "rtsp://", 7 ) )
402 char *psz_path = strdup( p_demux->psz_path );
403 char *end = strrchr( psz_path, '/' );
404 if( end ) end[1] = '\0';
405 else *psz_path = '\0';
407 if( asprintf( &psz_absolute, "%s://%s%s",
408 p_demux->psz_access, psz_path, psz_ref ) < 0 )
412 psz_ref = psz_absolute;
415 input_item_t *p_input;
416 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
417 p_input = input_ItemNewExt( p_demux, psz_ref, NULL,
419 input_ItemCopyOptions( p_current, p_input );
420 input_ItemAddSubItem( p_current, p_input );
421 vlc_gc_decref( p_input );
425 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
426 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
430 vlc_object_release( p_input );
433 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
437 msg_Err( p_demux, "cannot find /moov/mvhd" );
442 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
443 p_demux->pf_demux = DemuxRef;
449 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
450 if( p_sys->i_timescale == 0 )
452 msg_Err( p_this, "bad timescale" );
455 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
458 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
460 msg_Err( p_demux, "cannot find any /moov/trak" );
463 msg_Dbg( p_demux, "found %d track%c",
465 p_sys->i_tracks ? 's':' ' );
467 /* allocate memory */
468 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
469 if( p_sys->track == NULL )
471 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
473 /* Search the first chap reference (like quicktime) and
474 * check that at least 1 stream is enabled */
475 p_sys->p_tref_chap = NULL;
476 b_enabled_es = false;
477 for( i = 0; i < p_sys->i_tracks; i++ )
479 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
482 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
483 if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
486 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
487 if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
488 p_sys->p_tref_chap = p_chap;
491 /* now process each track and extract all usefull information */
492 for( i = 0; i < p_sys->i_tracks; i++ )
494 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
495 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
497 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
500 switch( p_sys->track[i].fmt.i_cat )
509 psz_cat = "subtitle";
517 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
518 p_sys->track[i].i_track_ID, psz_cat,
519 p_sys->track[i].b_enable ? "enable":"disable",
520 p_sys->track[i].fmt.psz_language ?
521 p_sys->track[i].fmt.psz_language : "undef" );
523 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
525 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
526 p_sys->track[i].i_track_ID,
527 p_sys->track[i].fmt.psz_language ?
528 p_sys->track[i].fmt.psz_language : "undef" );
532 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
533 p_sys->track[i].i_track_ID );
538 LoadChapter( p_demux );
545 MP4_BoxFree( p_demux->s, p_sys->p_root );
551 /*****************************************************************************
552 * Demux: read packet and send them to decoders
553 *****************************************************************************
554 * TODO check for newly selected track (ie audio upt to now )
555 *****************************************************************************/
556 static int Demux( demux_t *p_demux )
558 demux_sys_t *p_sys = p_demux->p_sys;
559 unsigned int i_track;
562 unsigned int i_track_selected;
564 /* check for newly selected/unselected track */
565 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
568 mp4_track_t *tk = &p_sys->track[i_track];
571 if( !tk->b_ok || tk->b_chapter ||
572 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
577 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
579 if( tk->b_selected && !b )
581 MP4_TrackUnselect( p_demux, tk );
583 else if( !tk->b_selected && b)
585 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
594 if( i_track_selected <= 0 )
596 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
597 if( p_sys->i_timescale > 0 )
599 int64_t i_length = (mtime_t)1000000 *
600 (mtime_t)p_sys->i_duration /
601 (mtime_t)p_sys->i_timescale;
602 if( MP4_GetMoviePTS( p_sys ) >= i_length )
607 msg_Warn( p_demux, "no track selected, exiting..." );
612 MP4_UpdateSeekpoint( p_demux );
614 /* first wait for the good time to read a packet */
615 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
617 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
619 /* we will read 100ms for each stream so ...*/
620 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
622 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
624 mp4_track_t *tk = &p_sys->track[i_track];
626 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
629 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
632 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
633 MP4_TrackGetDTS( p_demux, tk ),
634 MP4_GetMoviePTS( p_sys ) );
637 if( MP4_TrackSampleSize( tk ) > 0 )
643 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
645 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
647 MP4_TrackUnselect( p_demux, tk );
653 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
655 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
657 MP4_TrackUnselect( p_demux, tk );
661 if( tk->b_drms && tk->p_drms )
663 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
666 else if( tk->fmt.i_cat == SPU_ES )
668 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
669 p_block->i_buffer >= 2 )
671 uint16_t i_size = GetWBE( p_block->p_buffer );
673 if( i_size + 2 <= p_block->i_buffer )
676 /* remove the length field, and append a '\0' */
677 memmove( &p_block->p_buffer[0],
678 &p_block->p_buffer[2], i_size );
679 p_block->p_buffer[i_size] = '\0';
680 p_block->i_buffer = i_size + 1;
682 /* convert \r -> \n */
683 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
691 p_block->i_buffer = 0;
696 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
698 i_delta = MP4_TrackGetPTSDelta( tk );
700 p_block->i_pts = p_block->i_dts + i_delta;
701 else if( tk->fmt.i_cat != VIDEO_ES )
702 p_block->i_pts = p_block->i_dts;
706 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
707 es_out_Send( p_demux->out, tk->p_es, p_block );
711 if( MP4_TrackNextSample( p_demux, tk ) )
719 static void MP4_UpdateSeekpoint( demux_t *p_demux )
721 demux_sys_t *p_sys = p_demux->p_sys;
724 if( !p_sys->p_title )
726 i_time = MP4_GetMoviePTS( p_sys );
727 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
729 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
734 if( i != p_demux->info.i_seekpoint && i >= 0 )
736 p_demux->info.i_seekpoint = i;
737 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
740 /*****************************************************************************
741 * Seek: Got to i_date
742 ******************************************************************************/
743 static int Seek( demux_t *p_demux, mtime_t i_date )
745 demux_sys_t *p_sys = p_demux->p_sys;
746 unsigned int i_track;
748 /* First update update global time */
749 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
750 p_sys->i_pcr = i_date;
752 /* Now for each stream try to go to this time */
753 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
755 mp4_track_t *tk = &p_sys->track[i_track];
756 MP4_TrackSeek( p_demux, tk, i_date );
758 MP4_UpdateSeekpoint( p_demux );
762 /*****************************************************************************
764 *****************************************************************************/
765 static int Control( demux_t *p_demux, int i_query, va_list args )
767 demux_sys_t *p_sys = p_demux->p_sys;
774 case DEMUX_GET_POSITION:
775 pf = (double*)va_arg( args, double * );
776 if( p_sys->i_duration > 0 )
778 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
786 case DEMUX_SET_POSITION:
787 f = (double)va_arg( args, double );
788 if( p_sys->i_timescale > 0 )
790 i64 = (int64_t)( f * (double)1000000 *
791 (double)p_sys->i_duration /
792 (double)p_sys->i_timescale );
793 return Seek( p_demux, i64 );
795 else return VLC_SUCCESS;
798 pi64 = (int64_t*)va_arg( args, int64_t * );
799 if( p_sys->i_timescale > 0 )
801 *pi64 = (mtime_t)1000000 *
802 (mtime_t)p_sys->i_time /
803 (mtime_t)p_sys->i_timescale;
809 i64 = (int64_t)va_arg( args, int64_t );
810 return Seek( p_demux, i64 );
812 case DEMUX_GET_LENGTH:
813 pi64 = (int64_t*)va_arg( args, int64_t * );
814 if( p_sys->i_timescale > 0 )
816 *pi64 = (mtime_t)1000000 *
817 (mtime_t)p_sys->i_duration /
818 (mtime_t)p_sys->i_timescale;
824 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
829 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
830 MP4_Box_t *p_0xa9xxx;
832 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
835 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
842 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
843 p_0xa9xxx = p_0xa9xxx->p_next )
846 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
849 /* FIXME FIXME: should convert from whatever the character
850 * encoding of MP4 meta data is to UTF-8. */
851 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
852 if( psz_utf ) { EnsureUTF8( psz_utf ); \
853 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
855 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
856 switch( p_0xa9xxx->i_type )
858 case FOURCC_0xa9nam: /* Full name */
859 SET( vlc_meta_SetTitle );
862 SET( vlc_meta_SetArtist );
865 SET( vlc_meta_SetArtist );
868 SET( vlc_meta_SetCopyright );
870 case FOURCC_0xa9day: /* Creation Date */
871 SET( vlc_meta_SetDate );
873 case FOURCC_0xa9des: /* Description */
874 SET( vlc_meta_SetDescription );
876 case FOURCC_0xa9gen: /* Genre */
877 SET( vlc_meta_SetGenre );
880 case FOURCC_0xa9alb: /* Album */
881 SET( vlc_meta_SetAlbum );
884 case FOURCC_0xa9trk: /* Track */
885 SET( vlc_meta_SetTracknum );
888 case FOURCC_0xa9cmt: /* Commment */
889 SET( vlc_meta_SetDescription );
892 case FOURCC_0xa9url: /* URL */
893 SET( vlc_meta_SetURL );
896 case FOURCC_0xa9enc: /* Encoded By */
897 SET( vlc_meta_SetEncodedBy );
901 case FOURCC_0xa9inf: /* Information */
902 case FOURCC_0xa9dir: /* Director */
903 case FOURCC_0xa9dis: /* Disclaimer */
904 case FOURCC_0xa9req: /* Requirements */
905 case FOURCC_0xa9fmt: /* Original Format */
906 case FOURCC_0xa9dsa: /* Display Source As */
907 case FOURCC_0xa9hst: /* Host Computer */
908 case FOURCC_0xa9prd: /* Producer */
909 case FOURCC_0xa9prf: /* Performers */
910 case FOURCC_0xa9ope: /* Original Performer */
911 case FOURCC_0xa9src: /* Providers Source Content */
912 case FOURCC_0xa9wrt: /* Writer */
913 case FOURCC_0xa9com: /* Composer */
914 case FOURCC_WLOC: /* Window Location */
915 /* TODO one day, but they aren't really meaningfull */
926 case DEMUX_GET_TITLE_INFO:
928 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
929 int *pi_int = (int*)va_arg( args, int* );
930 int *pi_title_offset = (int*)va_arg( args, int* );
931 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
933 if( !p_sys->p_title )
937 *ppp_title = malloc( sizeof( input_title_t**) );
938 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
939 *pi_title_offset = 0;
940 *pi_seekpoint_offset = 0;
943 case DEMUX_SET_TITLE:
945 const int i_title = (int)va_arg( args, int );
946 if( !p_sys->p_title || i_title != 0 )
950 case DEMUX_SET_SEEKPOINT:
952 const int i_seekpoint = (int)va_arg( args, int );
953 if( !p_sys->p_title )
955 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
958 case DEMUX_SET_NEXT_DEMUX_TIME:
959 case DEMUX_SET_GROUP:
960 case DEMUX_HAS_UNSUPPORTED_META:
961 case DEMUX_GET_ATTACHMENTS:
965 msg_Warn( p_demux, "control query %u unimplemented", i_query );
970 /*****************************************************************************
971 * Close: frees unused data
972 *****************************************************************************/
973 static void Close ( vlc_object_t * p_this )
975 demux_t * p_demux = (demux_t *)p_this;
976 demux_sys_t *p_sys = p_demux->p_sys;
977 unsigned int i_track;
979 msg_Dbg( p_demux, "freeing all memory" );
981 MP4_BoxFree( p_demux->s, p_sys->p_root );
982 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
984 MP4_TrackDestroy( &p_sys->track[i_track] );
986 FREENULL( p_sys->track );
989 vlc_input_title_Delete( p_sys->p_title );
996 /****************************************************************************
997 * Local functions, specific to vlc
998 ****************************************************************************/
1000 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1002 demux_sys_t *p_sys = p_demux->p_sys;
1005 p_sys->p_title = vlc_input_title_New();
1006 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1008 seekpoint_t *s = vlc_seekpoint_New();
1010 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1011 EnsureUTF8( s->psz_name );
1012 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1013 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1016 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1018 demux_sys_t *p_sys = p_demux->p_sys;
1020 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1022 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1023 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( tk );
1024 const unsigned int i_size = MP4_TrackSampleSize( tk );
1026 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1029 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1030 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1034 seekpoint_t *s = vlc_seekpoint_New();
1036 s->psz_name = strndup( &p_buffer[2], i_len );
1037 EnsureUTF8( s->psz_name );
1039 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1041 if( !p_sys->p_title )
1042 p_sys->p_title = vlc_input_title_New();
1043 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1046 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1047 tk->chunk[tk->i_chunk].i_sample_count )
1051 static void LoadChapter( demux_t *p_demux )
1053 demux_sys_t *p_sys = p_demux->p_sys;
1056 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1058 LoadChapterGpac( p_demux, p_chpl );
1060 else if( p_sys->p_tref_chap )
1062 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1065 /* Load the first subtitle track like quicktime */
1066 for( i = 0; i < p_chap->i_entry_count; i++ )
1068 for( j = 0; j < p_sys->i_tracks; j++ )
1070 mp4_track_t *tk = &p_sys->track[j];
1071 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1072 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1075 if( j < p_sys->i_tracks )
1077 LoadChapterApple( p_demux, &p_sys->track[i] );
1084 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1085 static int TrackCreateChunksIndex( demux_t *p_demux,
1086 mp4_track_t *p_demux_track )
1088 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1091 unsigned int i_chunk;
1092 unsigned int i_index, i_last;
1094 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1095 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1096 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1098 return( VLC_EGENERIC );
1101 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1102 if( !p_demux_track->i_chunk_count )
1104 msg_Warn( p_demux, "no chunk defined" );
1105 return( VLC_EGENERIC );
1107 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1108 sizeof( mp4_chunk_t ) );
1109 if( p_demux_track->chunk == NULL )
1114 /* first we read chunk offset */
1115 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1117 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1119 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1121 ck->i_first_dts = 0;
1122 ck->p_sample_count_dts = NULL;
1123 ck->p_sample_delta_dts = NULL;
1124 ck->p_sample_count_pts = NULL;
1125 ck->p_sample_offset_pts = NULL;
1128 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1129 to be used for the sample XXX begin to 1
1130 We construct it begining at the end */
1131 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1132 i_index = p_stsc->data.p_stsc->i_entry_count;
1135 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1136 return( VLC_EGENERIC );
1141 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1142 i_chunk < i_last; i_chunk++ )
1144 if( i_chunk >= p_demux_track->i_chunk_count )
1146 msg_Warn( p_demux, "corrupted chunk table" );
1147 return VLC_EGENERIC;
1150 p_demux_track->chunk[i_chunk].i_sample_description_index =
1151 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1152 p_demux_track->chunk[i_chunk].i_sample_count =
1153 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1155 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1158 p_demux_track->chunk[0].i_sample_first = 0;
1159 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1161 p_demux_track->chunk[i_chunk].i_sample_first =
1162 p_demux_track->chunk[i_chunk-1].i_sample_first +
1163 p_demux_track->chunk[i_chunk-1].i_sample_count;
1166 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1167 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1172 static int TrackCreateSamplesIndex( demux_t *p_demux,
1173 mp4_track_t *p_demux_track )
1176 MP4_Box_data_stsz_t *stsz;
1177 MP4_Box_data_stts_t *stts;
1178 /* TODO use also stss and stsh table for seeking */
1179 /* FIXME use edit table */
1184 int64_t i_index_sample_used;
1189 * Gives the sample size for each samples. There is also a stz2 table
1190 * (compressed form) that we need to implement TODO */
1191 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1194 /* FIXME and stz2 */
1195 msg_Warn( p_demux, "cannot find STSZ box" );
1196 return VLC_EGENERIC;
1198 stsz = p_box->data.p_stsz;
1201 * Gives mapping between sample and decoding time
1203 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1206 msg_Warn( p_demux, "cannot find STTS box" );
1207 return VLC_EGENERIC;
1209 stts = p_box->data.p_stts;
1211 /* Use stsz table to create a sample number -> sample size table */
1212 p_demux_track->i_sample_count = stsz->i_sample_count;
1213 if( stsz->i_sample_size )
1215 /* 1: all sample have the same size, so no need to construct a table */
1216 p_demux_track->i_sample_size = stsz->i_sample_size;
1217 p_demux_track->p_sample_size = NULL;
1221 /* 2: each sample can have a different size */
1222 p_demux_track->i_sample_size = 0;
1223 p_demux_track->p_sample_size =
1224 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1225 if( p_demux_track->p_sample_size == NULL )
1228 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1230 p_demux_track->p_sample_size[i_sample] =
1231 stsz->i_entry_size[i_sample];
1235 /* Use stts table to create a sample number -> dts table.
1236 * XXX: if we don't want to waste too much memory, we can't expand
1237 * the box! so each chunk will contain an "extract" of this table
1238 * for fast research (problem with raw stream where a sample is sometime
1239 * just channels*bits_per_sample/8 */
1242 i_index = 0; i_index_sample_used = 0;
1243 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1245 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1246 int64_t i_entry, i_sample_count, i;
1249 ck->i_first_dts = i_last_dts;
1251 /* count how many entries are needed for this chunk
1252 * for p_sample_delta_dts and p_sample_count_dts */
1253 i_sample_count = ck->i_sample_count;
1256 while( i_sample_count > 0 )
1258 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1259 /* don't count already used sample in this entry */
1261 i_sample_count += i_index_sample_used;
1267 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1268 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1271 i_sample_count = ck->i_sample_count;
1272 for( i = 0; i < i_entry; i++ )
1277 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1279 i_used = __MIN( i_rest, i_sample_count );
1281 i_index_sample_used += i_used;
1282 i_sample_count -= i_used;
1284 ck->p_sample_count_dts[i] = i_used;
1285 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1287 i_last_dts += i_used * ck->p_sample_delta_dts[i];
1289 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1292 i_index_sample_used = 0;
1298 * Gives the delta between decoding time (dts) and composition table (pts)
1300 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1303 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1305 msg_Warn( p_demux, "CTTS table" );
1307 /* Create pts-dts table per chunk */
1308 i_index = 0; i_index_sample_used = 0;
1309 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1311 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1312 int64_t i_entry, i_sample_count, i;
1314 /* count how many entries are needed for this chunk
1315 * for p_sample_delta_dts and p_sample_count_dts */
1316 i_sample_count = ck->i_sample_count;
1319 while( i_sample_count > 0 )
1321 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1323 /* don't count already used sample in this entry */
1325 i_sample_count += i_index_sample_used;
1331 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1332 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1335 i_sample_count = ck->i_sample_count;
1336 for( i = 0; i < i_entry; i++ )
1341 i_rest = ctts->i_sample_count[i_index] -
1342 i_index_sample_used;
1344 i_used = __MIN( i_rest, i_sample_count );
1346 i_index_sample_used += i_used;
1347 i_sample_count -= i_used;
1349 ck->p_sample_count_pts[i] = i_used;
1350 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1352 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1355 i_index_sample_used = 0;
1361 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1362 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1363 i_last_dts / p_demux_track->i_timescale );
1370 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1372 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1373 unsigned int i_chunk, es_out_id_t **pp_es )
1375 MP4_Box_t *p_sample;
1382 if( !p_track->chunk[i_chunk].i_sample_description_index )
1384 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1385 p_track->i_track_ID );
1386 return VLC_EGENERIC;
1389 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1390 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1393 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1395 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1396 p_track->i_track_ID );
1397 return VLC_EGENERIC;
1400 p_track->p_sample = p_sample;
1402 if( p_track->fmt.i_cat == AUDIO_ES && ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1404 MP4_Box_data_sample_soun_t *p_soun;
1406 p_soun = p_sample->data.p_sample_soun;
1408 if( p_soun->i_qt_version == 0 )
1410 switch( p_sample->i_type )
1412 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1413 p_soun->i_qt_version = 1;
1414 p_soun->i_sample_per_packet = 64;
1415 p_soun->i_bytes_per_packet = 34;
1416 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1417 p_soun->i_bytes_per_sample = 2;
1419 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1420 p_soun->i_qt_version = 1;
1421 p_soun->i_sample_per_packet = 6;
1422 p_soun->i_bytes_per_packet = 2;
1423 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1424 p_soun->i_bytes_per_sample = 2;
1426 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1427 p_soun->i_qt_version = 1;
1428 p_soun->i_sample_per_packet = 12;
1429 p_soun->i_bytes_per_packet = 2;
1430 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1431 p_soun->i_bytes_per_sample = 2;
1433 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1434 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1435 p_soun->i_samplesize = 8;
1437 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1438 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1439 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1440 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1441 /* What would be the fun if you could trust the .mov */
1442 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1449 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1451 p_soun->i_qt_version = 0;
1455 /* It's a little ugly but .. there are special cases */
1456 switch( p_sample->i_type )
1458 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1459 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1460 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1463 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1464 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1466 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1468 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1469 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1471 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1473 /* Buggy files workaround */
1474 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1475 p_sample->data.p_sample_soun->i_sampleratehi) )
1477 MP4_Box_data_sample_soun_t *p_soun =
1478 p_sample->data.p_sample_soun;
1480 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1481 "(%u), making both equal (report any problem).",
1482 p_track->i_timescale, p_soun->i_sampleratehi );
1484 if( p_soun->i_sampleratehi )
1485 p_track->i_timescale = p_soun->i_sampleratehi;
1487 p_soun->i_sampleratehi = p_track->i_timescale;
1492 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1493 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1496 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1497 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1498 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1499 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1500 /* FIXME UTF-8 doesn't work here ? */
1501 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1504 case VLC_FOURCC('y','v','1','2'):
1505 p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1507 case VLC_FOURCC('y','u','v','2'):
1508 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1511 case VLC_FOURCC('i','n','2','4'):
1512 /* in in24/in32 there's enda-atom to tell it's little-endian (if present) */
1513 if( ( MP4_BoxGet( p_sample, "wave/enda" ) ) ||
1514 ( MP4_BoxGet( p_sample, "enda" ) ) )
1516 p_track->fmt.i_codec = VLC_FOURCC('4','2','n','i');
1518 p_track->fmt.i_codec = p_sample->i_type;
1523 p_track->fmt.i_codec = p_sample->i_type;
1527 /* now see if esds is present and if so create a data packet
1528 with decoder_specific_info */
1529 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1530 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1531 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1532 ( p_esds->data.p_esds )&&
1535 /* First update information based on i_objectTypeIndication */
1536 switch( p_decconfig->i_objectTypeIndication )
1538 case( 0x20 ): /* MPEG4 VIDEO */
1539 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1542 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1549 case( 0x65): /* MPEG2 video */
1550 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1552 /* Theses are MPEG2-AAC */
1553 case( 0x66): /* main profile */
1554 case( 0x67): /* Low complexity profile */
1555 case( 0x68): /* Scaleable Sampling rate profile */
1556 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1558 /* true MPEG 2 audio */
1560 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1562 case( 0x6a): /* MPEG1 video */
1563 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1565 case( 0x6b): /* MPEG1 audio */
1566 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1568 case( 0x6c ): /* jpeg */
1569 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1571 case( 0x6d ): /* png */
1572 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
1574 case( 0x6e ): /* jpeg200 */
1575 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
1577 case( 0xa3 ): /* vc1 */
1578 p_track->fmt.i_codec = VLC_FOURCC( 'W','V','C','1' );
1582 case( 0xe0 ): /* NeroDigital: dvd subs */
1583 if( p_track->fmt.i_cat == SPU_ES )
1585 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1586 if( p_track->i_width > 0 )
1587 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1588 if( p_track->i_height > 0 )
1589 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1592 case( 0xe1 ): /* QCelp for 3gp */
1593 if( p_track->fmt.i_cat == AUDIO_ES )
1595 p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
1601 /* Unknown entry, but don't touch i_fourcc */
1603 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1604 p_decconfig->i_objectTypeIndication,
1605 p_track->i_track_ID );
1608 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1609 if( p_track->fmt.i_extra > 0 )
1611 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1612 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1613 p_track->fmt.i_extra );
1618 switch( p_sample->i_type )
1620 /* qt decoder, send the complete chunk */
1621 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1622 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1623 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1624 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1625 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1626 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1627 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1628 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1629 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1630 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1631 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1632 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1633 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1635 /* qt decoder, send the complete chunk */
1636 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1637 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1638 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1639 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1640 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1641 p_track->fmt.i_extra =
1642 p_sample->data.p_sample_vide->i_qt_image_description;
1643 if( p_track->fmt.i_extra > 0 )
1645 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1646 memcpy( p_track->fmt.p_extra,
1647 p_sample->data.p_sample_vide->p_qt_image_description,
1648 p_track->fmt.i_extra);
1651 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1652 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1653 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1654 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1655 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1656 p_track->fmt.i_extra =
1657 p_sample->data.p_sample_soun->i_qt_description;
1658 if( p_track->fmt.i_extra > 0 )
1660 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1661 memcpy( p_track->fmt.p_extra,
1662 p_sample->data.p_sample_soun->p_qt_description,
1663 p_track->fmt.i_extra);
1667 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1668 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1670 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1674 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1675 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1676 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1677 p_track->fmt.i_extra );
1681 msg_Err( p_demux, "missing avcC" );
1686 case VLC_FOURCC('m','s',0x00,0x02):
1687 case VLC_FOURCC('m','s',0x00,0x11):
1688 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
1698 /* some last initialisation */
1699 switch( p_track->fmt.i_cat )
1702 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1703 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1704 p_track->fmt.video.i_bits_per_pixel =
1705 p_sample->data.p_sample_vide->i_depth;
1707 /* fall on display size */
1708 if( p_track->fmt.video.i_width <= 0 )
1709 p_track->fmt.video.i_width = p_track->i_width;
1710 if( p_track->fmt.video.i_height <= 0 )
1711 p_track->fmt.video.i_height = p_track->i_height;
1713 /* Find out apect ratio from display size */
1714 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1715 /* Work-around buggy muxed files */
1716 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1717 p_track->fmt.video.i_aspect =
1718 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1720 /* Support for cropping (eg. in H263 files) */
1721 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1722 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1725 p_track->fmt.video.i_frame_rate = p_track->i_timescale;
1726 p_track->fmt.video.i_frame_rate_base = 1;
1728 if( p_track->fmt.video.i_frame_rate &&
1729 (p_box = MP4_BoxGet( p_track->p_stbl, "stts" )) &&
1730 p_box->data.p_stts->i_entry_count >= 1 )
1732 p_track->fmt.video.i_frame_rate_base =
1733 p_box->data.p_stts->i_sample_delta[0];
1739 p_track->fmt.audio.i_channels =
1740 p_sample->data.p_sample_soun->i_channelcount;
1741 p_track->fmt.audio.i_rate =
1742 p_sample->data.p_sample_soun->i_sampleratehi;
1743 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1744 p_sample->data.p_sample_soun->i_sampleratehi *
1745 p_sample->data.p_sample_soun->i_samplesize;
1746 p_track->fmt.audio.i_bitspersample =
1747 p_sample->data.p_sample_soun->i_samplesize;
1755 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1760 /* given a time it return sample/chunk
1761 * it also update elst field of the track
1763 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1764 int64_t i_start, uint32_t *pi_chunk,
1765 uint32_t *pi_sample )
1767 demux_sys_t *p_sys = p_demux->p_sys;
1770 unsigned int i_sample;
1771 unsigned int i_chunk;
1774 /* FIXME see if it's needed to check p_track->i_chunk_count */
1775 if( p_track->i_chunk_count == 0 )
1776 return( VLC_EGENERIC );
1778 /* handle elst (find the correct one) */
1779 MP4_TrackSetELST( p_demux, p_track, i_start );
1780 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1782 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1783 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1785 /* now calculate i_start for this elst */
1787 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
1795 /* to track time scale */
1796 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1797 /* add elst offset */
1798 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1799 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1800 elst->i_media_time[p_track->i_elst] > 0 )
1802 i_start += elst->i_media_time[p_track->i_elst];
1805 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
1806 "ms (track)", p_track->i_elst,
1807 i_mvt * 1000 / p_sys->i_timescale,
1808 i_start * 1000 / p_track->i_timescale );
1812 /* convert absolute time to in timescale unit */
1813 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1816 /* we start from sample 0/chunk 0, hope it won't take too much time */
1817 /* *** find good chunk *** */
1818 for( i_chunk = 0; ; i_chunk++ )
1820 if( i_chunk + 1 >= p_track->i_chunk_count )
1822 /* at the end and can't check if i_start in this chunk,
1823 it will be check while searching i_sample */
1824 i_chunk = p_track->i_chunk_count - 1;
1828 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
1829 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1835 /* *** find sample in the chunk *** */
1836 i_sample = p_track->chunk[i_chunk].i_sample_first;
1837 i_dts = p_track->chunk[i_chunk].i_first_dts;
1838 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1841 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1842 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
1845 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1846 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1848 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1853 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1857 i_sample += ( i_start - i_dts ) /
1858 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1863 if( i_sample >= p_track->i_sample_count )
1865 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1866 "(seeking too far) chunk=%d sample=%d",
1867 p_track->i_track_ID, i_chunk, i_sample );
1868 return( VLC_EGENERIC );
1872 /* *** Try to find nearest sync points *** */
1873 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1875 unsigned int i_index;
1877 "track[Id 0x%x] using Sync Sample Box (stss)",
1878 p_track->i_track_ID );
1879 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1881 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1885 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1887 p_stss->data.p_stss->i_sample_number[i_index-1] );
1888 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1889 /* new i_sample is less than old so i_chunk can only decreased */
1890 while( i_chunk > 0 &&
1891 i_sample < p_track->chunk[i_chunk].i_sample_first )
1898 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1900 p_stss->data.p_stss->i_sample_number[i_index] );
1901 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1902 /* new i_sample is more than old so i_chunk can only increased */
1903 while( i_chunk < p_track->i_chunk_count - 1 &&
1904 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1905 p_track->chunk[i_chunk].i_sample_count )
1916 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1917 "Sample Box (stss)", p_track->i_track_ID );
1920 *pi_chunk = i_chunk;
1921 *pi_sample = i_sample;
1926 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1927 unsigned int i_chunk, unsigned int i_sample )
1929 bool b_reselect = false;
1931 /* now see if actual es is ok */
1932 if( (p_track->i_chunk >= p_track->i_chunk_count - 1) ||
1933 (p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1934 p_track->chunk[i_chunk].i_sample_description_index) )
1936 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1937 p_track->i_track_ID );
1939 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1940 p_track->p_es, &b_reselect );
1942 es_out_Del( p_demux->out, p_track->p_es );
1944 p_track->p_es = NULL;
1946 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1948 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1949 p_track->i_track_ID );
1951 p_track->b_ok = false;
1952 p_track->b_selected = false;
1953 return VLC_EGENERIC;
1957 /* select again the new decoder */
1960 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1963 p_track->i_chunk = i_chunk;
1964 p_track->i_sample = i_sample;
1966 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1969 /****************************************************************************
1971 ****************************************************************************
1972 * Parse track information and create all needed data to run a track
1973 * If it succeed b_ok is set to 1 else to 0
1974 ****************************************************************************/
1975 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1976 MP4_Box_t *p_box_trak,
1977 bool b_force_enable )
1979 demux_sys_t *p_sys = p_demux->p_sys;
1981 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1982 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1997 /* hint track unsupported */
1999 /* set default value (-> track unusable) */
2000 p_track->b_ok = false;
2001 p_track->b_enable = false;
2002 p_track->b_selected = false;
2003 p_track->b_chapter = false;
2005 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2012 /* do we launch this track by default ? */
2014 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2015 if( !p_track->b_enable )
2016 p_track->fmt.i_priority = -1;
2018 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2019 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2020 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2024 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2027 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2028 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2030 if( ( !p_mdhd )||( !p_hdlr ) )
2035 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2036 if( !p_track->i_timescale )
2039 for( i = 0; i < 3; i++ )
2041 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2045 switch( p_hdlr->data.p_hdlr->i_handler_type )
2047 case( FOURCC_soun ):
2048 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2052 p_track->fmt.i_cat = AUDIO_ES;
2055 case( FOURCC_vide ):
2056 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2060 p_track->fmt.i_cat = VIDEO_ES;
2063 case( FOURCC_text ):
2064 case( FOURCC_subp ):
2065 case( FOURCC_tx3g ):
2066 p_track->fmt.i_cat = SPU_ES;
2073 p_track->i_elst = 0;
2074 p_track->i_elst_time = 0;
2075 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2077 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2080 msg_Warn( p_demux, "elst box found" );
2081 for( i = 0; i < elst->i_entry_count; i++ )
2083 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2084 "ms) rate=%d.%d", i,
2085 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2086 elst->i_media_time[i] >= 0 ?
2087 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2089 elst->i_media_rate_integer[i],
2090 elst->i_media_rate_fraction[i] );
2097 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2099 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2100 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2105 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2106 p_track->b_drms = p_drms != NULL;
2107 p_track->p_drms = p_track->b_drms ?
2108 p_drms->data.p_sample_soun->p_drms : NULL;
2111 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
2113 p_track->fmt.psz_language = strdup( language );
2116 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2119 MP4_Box_t *p_0xa9xxx;
2120 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
2121 p_0xa9xxx = p_0xa9xxx->p_next )
2123 switch( p_0xa9xxx->i_type )
2125 case FOURCC_0xa9nam:
2126 p_track->fmt.psz_description =
2127 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
2133 /* Create chunk index table and sample index table */
2134 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2135 TrackCreateSamplesIndex( p_demux, p_track ) )
2137 return; /* cannot create chunks index */
2140 p_track->i_chunk = 0;
2141 p_track->i_sample = 0;
2143 /* Mark chapter only track */
2144 if( !p_track->b_enable && p_sys->p_tref_chap )
2146 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2149 for( i = 0; i < p_chap->i_entry_count; i++ )
2151 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2153 p_track->b_chapter = true;
2160 if( b_force_enable &&
2161 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2163 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2164 p_track->i_track_ID );
2165 p_track->b_enable = true;
2166 p_track->fmt.i_priority = 0;
2169 p_track->p_es = NULL;
2170 if( TrackCreateES( p_demux,
2171 p_track, p_track->i_chunk,
2172 p_track->b_chapter ? NULL : &p_track->p_es ) )
2174 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2175 p_track->i_track_ID );
2178 p_track->b_ok = true;
2182 for( i = 0; i < p_track->i_chunk_count; i++ )
2184 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2185 i, p_track->chunk[i].i_sample_count,
2186 p_track->chunk[i].i_first_dts );
2193 /****************************************************************************
2195 ****************************************************************************
2196 * Destroy a track created by MP4_TrackCreate.
2197 ****************************************************************************/
2198 static void MP4_TrackDestroy( mp4_track_t *p_track )
2200 unsigned int i_chunk;
2202 p_track->b_ok = false;
2203 p_track->b_enable = false;
2204 p_track->b_selected = false;
2206 es_format_Clean( &p_track->fmt );
2208 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2210 if( p_track->chunk )
2212 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2213 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2215 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2216 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2219 FREENULL( p_track->chunk );
2221 if( !p_track->i_sample_size )
2223 FREENULL( p_track->p_sample_size );
2227 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2230 if( !p_track->b_ok || p_track->b_chapter )
2232 return VLC_EGENERIC;
2235 if( p_track->b_selected )
2237 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2238 p_track->i_track_ID );
2242 return MP4_TrackSeek( p_demux, p_track, i_start );
2245 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2247 if( !p_track->b_ok || p_track->b_chapter )
2252 if( !p_track->b_selected )
2254 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2255 p_track->i_track_ID );
2260 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2261 p_track->p_es, false );
2264 p_track->b_selected = false;
2267 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2273 if( !p_track->b_ok || p_track->b_chapter )
2274 return VLC_EGENERIC;
2276 p_track->b_selected = false;
2278 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2279 &i_chunk, &i_sample ) )
2281 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2282 p_track->i_track_ID );
2283 return VLC_EGENERIC;
2286 p_track->b_selected = true;
2288 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
2291 p_track->b_selected = true;
2293 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
2294 p_track->p_es, i_start );
2297 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2302 * 3 types: for audio
2305 #define QT_V0_MAX_SAMPLES 1024
2306 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2309 MP4_Box_data_sample_soun_t *p_soun;
2311 if( p_track->i_sample_size == 0 )
2313 /* most simple case */
2314 return p_track->p_sample_size[p_track->i_sample];
2316 if( p_track->fmt.i_cat != AUDIO_ES )
2318 return p_track->i_sample_size;
2321 p_soun = p_track->p_sample->data.p_sample_soun;
2323 if( p_soun->i_qt_version == 1 )
2325 i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
2326 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2328 else if( p_track->i_sample_size > 256 )
2330 /* We do that so we don't read too much data
2331 * (in this case we are likely dealing with compressed data) */
2332 i_size = p_track->i_sample_size;
2336 /* Read a bunch of samples at once */
2337 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2338 ( p_track->i_sample -
2339 p_track->chunk[p_track->i_chunk].i_sample_first );
2341 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2342 i_size = i_samples * p_track->i_sample_size;
2345 //fprintf( stderr, "size=%d\n", i_size );
2349 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2351 unsigned int i_sample;
2354 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2356 if( p_track->i_sample_size )
2358 MP4_Box_data_sample_soun_t *p_soun =
2359 p_track->p_sample->data.p_sample_soun;
2361 if( p_soun->i_qt_version == 0 )
2363 i_pos += ( p_track->i_sample -
2364 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2365 p_track->i_sample_size;
2369 /* we read chunk by chunk */
2375 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2376 i_sample < p_track->i_sample; i_sample++ )
2378 i_pos += p_track->p_sample_size[i_sample];
2385 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2387 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2389 MP4_Box_data_sample_soun_t *p_soun;
2391 p_soun = p_track->p_sample->data.p_sample_soun;
2393 if( p_soun->i_qt_version == 1 )
2395 /* chunk by chunk */
2397 p_track->chunk[p_track->i_chunk].i_sample_first +
2398 p_track->chunk[p_track->i_chunk].i_sample_count;
2400 else if( p_track->i_sample_size > 256 )
2402 /* We do that so we don't read too much data
2403 * (in this case we are likely dealing with compressed data) */
2404 p_track->i_sample += 1;
2409 p_track->i_sample += QT_V0_MAX_SAMPLES;
2410 if( p_track->i_sample >
2411 p_track->chunk[p_track->i_chunk].i_sample_first +
2412 p_track->chunk[p_track->i_chunk].i_sample_count )
2415 p_track->chunk[p_track->i_chunk].i_sample_first +
2416 p_track->chunk[p_track->i_chunk].i_sample_count;
2422 p_track->i_sample++;
2425 if( p_track->i_sample >= p_track->i_sample_count )
2426 return VLC_EGENERIC;
2428 /* Have we changed chunk ? */
2429 if( p_track->i_sample >=
2430 p_track->chunk[p_track->i_chunk].i_sample_first +
2431 p_track->chunk[p_track->i_chunk].i_sample_count )
2433 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2434 p_track->i_sample ) )
2436 msg_Warn( p_demux, "track[0x%x] will be disabled "
2437 "(cannot restart decoder)", p_track->i_track_ID );
2438 MP4_TrackUnselect( p_demux, p_track );
2439 return VLC_EGENERIC;
2443 /* Have we changed elst */
2444 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2446 demux_sys_t *p_sys = p_demux->p_sys;
2447 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2448 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2449 p_sys->i_timescale / (int64_t)1000000;
2451 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2452 i_mvt >= p_track->i_elst_time +
2453 elst->i_segment_duration[p_track->i_elst] )
2455 MP4_TrackSetELST( p_demux, p_track,
2456 MP4_TrackGetDTS( p_demux, p_track ) );
2463 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2466 demux_sys_t *p_sys = p_demux->p_sys;
2467 int i_elst_last = tk->i_elst;
2469 /* handle elst (find the correct one) */
2471 tk->i_elst_time = 0;
2472 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2474 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2475 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2477 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2479 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2481 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2485 tk->i_elst_time += i_dur;
2488 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2490 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2491 tk->i_elst = elst->i_entry_count - 1;
2492 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2495 if( elst->i_media_time[tk->i_elst] < 0 )
2498 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2501 if( i_elst_last != tk->i_elst )
2503 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );