1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
38 #define uint64_t int64_t
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
48 set_category( CAT_INPUT );
49 set_subcategory( SUBCAT_INPUT_DEMUX );
50 set_description( _("MP4 stream demuxer") );
51 set_capability( "demux2", 242 );
52 set_callbacks( Open, Close );
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Demux ( demux_t * );
59 static int DemuxRef( demux_t *p_demux ){ return 0;}
60 static int Seek ( demux_t *, mtime_t );
61 static int Control ( demux_t *, int, va_list );
63 /* Contain all information about a chunk */
66 uint64_t i_offset; /* absolute position of this chunk in the file */
67 uint32_t i_sample_description_index; /* index for SampleEntry to use */
68 uint32_t i_sample_count; /* how many samples in this chunk */
69 uint32_t i_sample_first; /* index of the first sample in this chunk */
71 /* now provide way to calculate pts, dts, and offset without to
72 much memory and with fast acces */
74 /* with this we can calculate dts/pts without waste memory */
76 uint32_t *p_sample_count_dts;
77 uint32_t *p_sample_delta_dts; /* dts delta */
79 uint32_t *p_sample_count_pts;
80 uint32_t *p_sample_offset_pts; /* pts-dts */
82 /* TODO if needed add pts
83 but quickly *add* support for edts and seeking */
87 /* Contain all needed information for read all track with vlc */
90 int i_track_ID; /* this should be unique */
92 int b_ok; /* The track is usable */
93 int b_enable; /* is the trak enable by default */
94 vlc_bool_t b_selected; /* is the trak being played */
99 /* display size only ! */
103 /* more internal data */
104 uint64_t i_timescale; /* time scale for this track only */
107 int i_elst; /* current elst */
108 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
109 MP4_Box_t *p_elst; /* elst (could be NULL) */
111 /* give the next sample to read, i_chunk is to find quickly where
112 the sample is located */
113 uint32_t i_sample; /* next sample to read */
114 uint32_t i_chunk; /* chunk where next sample is stored */
115 /* total count of chunk and sample */
116 uint32_t i_chunk_count;
117 uint32_t i_sample_count;
119 mp4_chunk_t *chunk; /* always defined for each chunk */
121 /* sample size, p_sample_size defined only if i_sample_size == 0
122 else i_sample_size is size for all sample */
123 uint32_t i_sample_size;
124 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
125 too much time to do sumations each time*/
127 MP4_Box_t *p_stbl; /* will contain all timing information */
128 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
129 MP4_Box_t *p_sample;/* point on actual sdsd */
139 MP4_Box_t *p_root; /* container for the whole file */
143 uint64_t i_time; /* time position of the presentation
144 * in movie timescale */
145 uint64_t i_timescale; /* movie time scale */
146 uint64_t i_duration; /* movie duration */
147 unsigned int i_tracks; /* number of tracks */
148 mp4_track_t *track; /* array of track */
151 /*****************************************************************************
152 * Declaration of local function
153 *****************************************************************************/
154 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *);
155 static void MP4_TrackDestroy( demux_t *, mp4_track_t * );
157 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
158 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
160 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
162 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
163 static int MP4_TrackSampleSize( mp4_track_t * );
164 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
165 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
167 /* Return time in µs of a track */
168 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
170 #define chunk p_track->chunk[p_track->i_chunk]
172 unsigned int i_index = 0;
173 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
174 int64_t i_dts = chunk.i_first_dts;
176 while( i_sample > 0 )
178 if( i_sample > chunk.p_sample_count_dts[i_index] )
180 i_dts += chunk.p_sample_count_dts[i_index] *
181 chunk.p_sample_delta_dts[i_index];
182 i_sample -= chunk.p_sample_count_dts[i_index];
187 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
195 /* now handle elst */
196 if( p_track->p_elst )
198 demux_sys_t *p_sys = p_demux->p_sys;
199 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
201 /* convert to offset */
202 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
203 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
204 elst->i_media_time[p_track->i_elst] > 0 )
206 i_dts -= elst->i_media_time[p_track->i_elst];
209 /* add i_elst_time */
210 i_dts += p_track->i_elst_time * p_track->i_timescale /
213 if( i_dts < 0 ) i_dts = 0;
216 return I64C(1000000) * i_dts / p_track->i_timescale;
219 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
221 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
222 unsigned int i_index = 0;
223 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
225 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
228 for( i_index = 0;; i_index++ )
230 if( i_sample < ck->p_sample_count_pts[i_index] )
231 return ck->p_sample_offset_pts[i_index] * I64C(1000000) / p_track->i_timescale;
233 i_sample -= ck->p_sample_count_pts[i_index];
237 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
239 return I64C(1000000) * p_sys->i_time / p_sys->i_timescale;
242 #define FREE( p ) if( p ) { free( p ); (p) = NULL;}
244 /*****************************************************************************
245 * Open: check file and initializes MP4 structures
246 *****************************************************************************/
247 static int Open( vlc_object_t * p_this )
249 demux_t *p_demux = (demux_t *)p_this;
260 vlc_bool_t b_seekable;
262 /* a little test to see if it could be a mp4 */
263 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
265 msg_Warn( p_demux, "MP4 plugin discarded (cannot peek)" );
268 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
279 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
282 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
287 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
290 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
294 /*Set exported functions */
295 p_demux->pf_demux = Demux;
296 p_demux->pf_control = Control;
298 /* create our structure that will contains all data */
299 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
300 memset( p_sys, 0, sizeof( demux_sys_t ) );
302 /* Now load all boxes ( except raw data ) */
303 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
305 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
309 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
311 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
313 switch( p_ftyp->data.p_ftyp->i_major_brand )
317 "ISO Media file (isom) version %d.",
318 p_ftyp->data.p_ftyp->i_minor_version );
322 "unrecognized major file specification (%4.4s).",
323 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
329 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
332 /* the file need to have one moov box */
333 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
335 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
339 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
342 /* we have a free box as a moov, rename it */
343 p_foov->i_type = FOURCC_moov;
346 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
348 playlist_t *p_playlist;
349 playlist_item_t *p_item;
350 int i_count = MP4_BoxCount( p_rmra, "rmda" );
352 vlc_bool_t b_play = VLC_FALSE;
354 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
357 (playlist_t *)vlc_object_find( p_demux,
362 p_item = playlist_LockItemGetByInput( p_playlist,
363 ((input_thread_t *)p_demux->p_parent)->input.p_item );
364 playlist_ItemToNode( p_playlist, p_item );
366 for( i = 0; i < i_count; i++ )
368 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
372 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
376 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
378 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
379 psz_ref, (char*)&i_ref_type );
381 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
383 if( strstr( psz_ref, "qt5gateQT" ) )
385 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
388 if( !strncmp( psz_ref, "http://", 7 ) ||
389 !strncmp( psz_ref, "rtsp://", 7 ) )
391 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
394 playlist_item_t *p_child =
395 playlist_ItemNew( p_playlist,
399 playlist_NodeAddItem( p_playlist, p_child,
400 p_item->pp_parents[0]->i_view,
401 p_item, PLAYLIST_APPEND,
403 playlist_CopyParents( p_item, p_child );
410 /* msg dbg relative ? */
411 char *psz_absolute = alloca( strlen( p_demux->psz_access ) + 3 + strlen( p_demux->psz_path ) + strlen( psz_ref ) + 1);
412 char *end = strrchr( p_demux->psz_path, '/' );
416 int i_len = end + 1 - p_demux->psz_path;
418 strcpy( psz_absolute, p_demux->psz_access );
419 strcat( psz_absolute, "://" );
420 strncat( psz_absolute, p_demux->psz_path, i_len);
424 strcpy( psz_absolute, "" );
426 strcat( psz_absolute, psz_ref );
427 msg_Dbg( p_demux, "adding ref = `%s'", psz_absolute );
430 playlist_item_t *p_child =
431 playlist_ItemNew( p_playlist,
436 playlist_NodeAddItem( p_playlist, p_child,
437 p_item->pp_parents[0]->i_view,
438 p_item, PLAYLIST_APPEND,
440 playlist_CopyParents( p_item, p_child );
448 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
449 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
452 if( b_play == VLC_TRUE )
454 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
455 p_playlist->status.i_view,
456 p_playlist->status.p_item, NULL );
458 vlc_object_release( p_playlist );
462 msg_Err( p_demux, "can't find playlist" );
466 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
470 msg_Err( p_demux, "cannot find /moov/mvhd" );
475 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
476 p_demux->pf_demux = DemuxRef;
482 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
483 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
486 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
488 msg_Err( p_demux, "cannot find any /moov/trak" );
491 msg_Dbg( p_demux, "find %d track%c",
493 p_sys->i_tracks ? 's':' ' );
495 /* allocate memory */
496 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
497 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
499 /* now process each track and extract all usefull information */
500 for( i = 0; i < p_sys->i_tracks; i++ )
502 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
503 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
505 if( p_sys->track[i].b_ok )
508 switch( p_sys->track[i].fmt.i_cat )
517 psz_cat = "subtitle";
525 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
526 p_sys->track[i].i_track_ID, psz_cat,
527 p_sys->track[i].b_enable ? "enable":"disable",
528 p_sys->track[i].fmt.psz_language ?
529 p_sys->track[i].fmt.psz_language : "undef" );
533 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
534 p_sys->track[i].i_track_ID );
543 MP4_BoxFree( p_demux->s, p_sys->p_root );
549 /*****************************************************************************
550 * Demux: read packet and send them to decoders
551 *****************************************************************************
552 * TODO check for newly selected track (ie audio upt to now )
553 *****************************************************************************/
554 static int Demux( demux_t *p_demux )
556 demux_sys_t *p_sys = p_demux->p_sys;
557 unsigned int i_track;
560 unsigned int i_track_selected;
562 /* check for newly selected/unselected track */
563 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
566 mp4_track_t *tk = &p_sys->track[i_track];
568 if( tk->b_selected && tk->i_sample >= tk->i_sample_count )
570 msg_Warn( p_demux, "track[0x%x] will be disabled", tk->i_track_ID );
571 MP4_TrackUnselect( p_demux, tk);
576 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
578 if( tk->b_selected && !b )
580 MP4_TrackUnselect( p_demux, tk );
582 else if( !tk->b_selected && b)
584 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..." );
611 /* first wait for the good time to read a packet */
612 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
614 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
616 /* we will read 100ms for each stream so ...*/
617 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
619 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
621 mp4_track_t *tk = &p_sys->track[i_track];
623 if( !tk->b_ok || !tk->b_selected )
628 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
631 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
632 MP4_TrackGetDTS( p_demux, tk ),
633 MP4_GetMoviePTS( p_sys ) );
636 if( MP4_TrackSampleSize( tk ) > 0 )
642 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
644 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
646 MP4_TrackUnselect( p_demux, tk );
652 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
654 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
656 MP4_TrackUnselect( p_demux, tk );
660 if( tk->b_drms && tk->p_drms )
662 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
665 else if( tk->fmt.i_cat == SPU_ES )
667 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
668 p_block->i_buffer >= 2 )
670 uint16_t i_size = GetWBE( p_block->p_buffer );
672 if( i_size + 2 <= p_block->i_buffer )
675 /* remove the length field, and append a '\0' */
676 memmove( &p_block->p_buffer[0],
677 &p_block->p_buffer[2], i_size );
678 p_block->p_buffer[i_size] = '\0';
679 p_block->i_buffer = i_size + 1;
681 /* convert \r -> \n */
682 while( ( p = strchr( p_block->p_buffer, '\r' ) ) )
690 p_block->i_buffer = 0;
695 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
697 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
699 p_block->i_pts = p_block->i_dts + i_delta;
700 else if( tk->fmt.i_cat != VIDEO_ES )
701 p_block->i_pts = p_block->i_dts;
705 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
706 es_out_Send( p_demux->out, tk->p_es, p_block );
710 if( MP4_TrackNextSample( p_demux, tk ) )
719 /*****************************************************************************
720 * Seek: Got to i_date
721 ******************************************************************************/
722 static int Seek( demux_t *p_demux, mtime_t i_date )
724 demux_sys_t *p_sys = p_demux->p_sys;
725 unsigned int i_track;
727 /* First update update global time */
728 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
729 p_sys->i_pcr = i_date;
731 /* Now for each stream try to go to this time */
732 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
734 mp4_track_t *tk = &p_sys->track[i_track];
735 MP4_TrackSeek( p_demux, tk, i_date );
740 /*****************************************************************************
742 *****************************************************************************/
743 static int Control( demux_t *p_demux, int i_query, va_list args )
745 demux_sys_t *p_sys = p_demux->p_sys;
752 case DEMUX_GET_POSITION:
753 pf = (double*)va_arg( args, double * );
754 if( p_sys->i_duration > 0 )
756 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
764 case DEMUX_SET_POSITION:
765 f = (double)va_arg( args, double );
766 if( p_sys->i_timescale > 0 )
768 i64 = (int64_t)( f * (double)1000000 *
769 (double)p_sys->i_duration /
770 (double)p_sys->i_timescale );
771 return Seek( p_demux, i64 );
773 else return VLC_SUCCESS;
776 pi64 = (int64_t*)va_arg( args, int64_t * );
777 if( p_sys->i_timescale > 0 )
779 *pi64 = (mtime_t)1000000 *
780 (mtime_t)p_sys->i_time /
781 (mtime_t)p_sys->i_timescale;
787 i64 = (int64_t)va_arg( args, int64_t );
788 return Seek( p_demux, i64 );
790 case DEMUX_GET_LENGTH:
791 pi64 = (int64_t*)va_arg( args, int64_t * );
792 if( p_sys->i_timescale > 0 )
794 *pi64 = (mtime_t)1000000 *
795 (mtime_t)p_sys->i_duration /
796 (mtime_t)p_sys->i_timescale;
802 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
807 vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
809 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
810 MP4_Box_t *p_0xa9xxx;
815 *pp_meta = meta = vlc_meta_New();
816 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
817 p_0xa9xxx = p_0xa9xxx->p_next )
819 switch( p_0xa9xxx->i_type )
821 case FOURCC_0xa9nam: /* Full name */
822 vlc_meta_Add( meta, VLC_META_TITLE,
823 p_0xa9xxx->data.p_0xa9xxx->psz_text );
826 vlc_meta_Add( meta, VLC_META_AUTHOR,
827 p_0xa9xxx->data.p_0xa9xxx->psz_text );
830 vlc_meta_Add( meta, VLC_META_ARTIST,
831 p_0xa9xxx->data.p_0xa9xxx->psz_text );
834 vlc_meta_Add( meta, VLC_META_COPYRIGHT,
835 p_0xa9xxx->data.p_0xa9xxx->psz_text );
837 case FOURCC_0xa9day: /* Creation Date */
838 vlc_meta_Add( meta, VLC_META_DATE,
839 p_0xa9xxx->data.p_0xa9xxx->psz_text );
841 case FOURCC_0xa9des: /* Description */
842 vlc_meta_Add( meta, VLC_META_DESCRIPTION,
843 p_0xa9xxx->data.p_0xa9xxx->psz_text );
845 case FOURCC_0xa9gen: /* Genre */
846 vlc_meta_Add( meta, VLC_META_GENRE,
847 p_0xa9xxx->data.p_0xa9xxx->psz_text );
851 case FOURCC_0xa9inf: /* Information */
852 case FOURCC_0xa9alb: /* Album */
853 case FOURCC_0xa9dir: /* Director */
854 case FOURCC_0xa9dis: /* Disclaimer */
855 case FOURCC_0xa9enc: /* Encoded By */
856 case FOURCC_0xa9trk: /* Track */
857 case FOURCC_0xa9cmt: /* Commment */
858 case FOURCC_0xa9url: /* URL */
859 case FOURCC_0xa9req: /* Requirements */
860 case FOURCC_0xa9fmt: /* Original Format */
861 case FOURCC_0xa9dsa: /* Display Source As */
862 case FOURCC_0xa9hst: /* Host Computer */
863 case FOURCC_0xa9prd: /* Producer */
864 case FOURCC_0xa9prf: /* Performers */
865 case FOURCC_0xa9ope: /* Original Performer */
866 case FOURCC_0xa9src: /* Providers Source Content */
867 case FOURCC_0xa9wrt: /* Writer */
868 case FOURCC_0xa9com: /* Composer */
869 case FOURCC_WLOC: /* Window Location */
870 /* TODO one day, but they aren't really meaningfull */
880 case DEMUX_GET_TITLE_INFO:
881 case DEMUX_SET_NEXT_DEMUX_TIME:
882 case DEMUX_SET_GROUP:
886 msg_Warn( p_demux, "control query unimplemented !!!" );
891 /*****************************************************************************
892 * Close: frees unused data
893 *****************************************************************************/
894 static void Close ( vlc_object_t * p_this )
896 unsigned int i_track;
897 demux_t * p_demux = (demux_t *)p_this;
898 demux_sys_t *p_sys = p_demux->p_sys;
900 msg_Dbg( p_demux, "freeing all memory" );
902 MP4_BoxFree( p_demux->s, p_sys->p_root );
903 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
905 MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
907 FREE( p_sys->track );
914 /****************************************************************************
915 * Local functions, specific to vlc
916 ****************************************************************************/
918 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
919 static int TrackCreateChunksIndex( demux_t *p_demux,
920 mp4_track_t *p_demux_track )
922 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
925 unsigned int i_chunk;
926 unsigned int i_index, i_last;
928 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
929 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
930 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
932 return( VLC_EGENERIC );
935 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
936 if( !p_demux_track->i_chunk_count )
938 msg_Warn( p_demux, "no chunk defined" );
939 return( VLC_EGENERIC );
941 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
942 sizeof( mp4_chunk_t ) );
944 /* first we read chunk offset */
945 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
947 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
949 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
952 ck->p_sample_count_dts = NULL;
953 ck->p_sample_delta_dts = NULL;
954 ck->p_sample_count_pts = NULL;
955 ck->p_sample_offset_pts = NULL;
958 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
959 to be used for the sample XXX begin to 1
960 We construct it begining at the end */
961 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
962 i_index = p_stsc->data.p_stsc->i_entry_count;
965 msg_Warn( p_demux, "cannot read chunk table or table empty" );
966 return( VLC_EGENERIC );
971 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
972 i_chunk < i_last; i_chunk++ )
974 p_demux_track->chunk[i_chunk].i_sample_description_index =
975 p_stsc->data.p_stsc->i_sample_description_index[i_index];
976 p_demux_track->chunk[i_chunk].i_sample_count =
977 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
979 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
982 p_demux_track->chunk[0].i_sample_first = 0;
983 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
985 p_demux_track->chunk[i_chunk].i_sample_first =
986 p_demux_track->chunk[i_chunk-1].i_sample_first +
987 p_demux_track->chunk[i_chunk-1].i_sample_count;
990 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
991 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
996 static int TrackCreateSamplesIndex( demux_t *p_demux,
997 mp4_track_t *p_demux_track )
1000 MP4_Box_data_stsz_t *stsz;
1001 MP4_Box_data_stts_t *stts;
1002 /* TODO use also stss and stsh table for seeking */
1003 /* FIXME use edit table */
1008 int64_t i_index_sample_used;
1013 * Gives the sample size for each samples. There is also a stz2 table
1014 * (compressed form) that we need to implement TODO */
1015 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1018 /* FIXME and stz2 */
1019 msg_Warn( p_demux, "cannot find STSZ box" );
1020 return VLC_EGENERIC;
1022 stsz = p_box->data.p_stsz;
1025 * Gives mapping between sample and decoding time
1027 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1030 msg_Warn( p_demux, "cannot find STTS box" );
1031 return VLC_EGENERIC;
1033 stts = p_box->data.p_stts;
1035 /* Use stsz table to create a sample number -> sample size table */
1036 p_demux_track->i_sample_count = stsz->i_sample_count;
1037 if( stsz->i_sample_size )
1039 /* 1: all sample have the same size, so no need to construct a table */
1040 p_demux_track->i_sample_size = stsz->i_sample_size;
1041 p_demux_track->p_sample_size = NULL;
1045 /* 2: each sample can have a different size */
1046 p_demux_track->i_sample_size = 0;
1047 p_demux_track->p_sample_size =
1048 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1050 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1052 p_demux_track->p_sample_size[i_sample] =
1053 stsz->i_entry_size[i_sample];
1057 /* Use stts table to crate a sample number -> dts table.
1058 * XXX: if we don't want to waste too much memory, we can't expand
1059 * the box! so each chunk will contain an "extract" of this table
1060 * for fast research (problem with raw stream where a sample is sometime
1061 * just channels*bits_per_sample/8 */
1064 i_index = 0; i_index_sample_used = 0;
1065 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1067 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1068 int64_t i_entry, i_sample_count, i;
1071 ck->i_first_dts = i_last_dts;
1073 /* count how many entries are needed for this chunk
1074 * for p_sample_delta_dts and p_sample_count_dts */
1075 i_sample_count = ck->i_sample_count;
1078 while( i_sample_count > 0 )
1080 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1081 /* don't count already used sample in this entry */
1083 i_sample_count += i_index_sample_used;
1089 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1090 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1093 i_sample_count = ck->i_sample_count;
1094 for( i = 0; i < i_entry; i++ )
1099 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1101 i_used = __MIN( i_rest, i_sample_count );
1103 i_index_sample_used += i_used;
1104 i_sample_count -= i_used;
1106 ck->p_sample_count_dts[i] = i_used;
1107 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1109 i_last_dts += i_used * ck->p_sample_delta_dts[i];
1111 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1114 i_index_sample_used = 0;
1120 * Gives the delta between decoding time (dts) and composition table (pts)
1122 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1125 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1127 msg_Warn( p_demux, "CTTS table" );
1129 /* Create pts-dts table per chunk */
1130 i_index = 0; i_index_sample_used = 0;
1131 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1133 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1134 int64_t i_entry, i_sample_count, i;
1136 /* count how many entries are needed for this chunk
1137 * for p_sample_delta_dts and p_sample_count_dts */
1138 i_sample_count = ck->i_sample_count;
1141 while( i_sample_count > 0 )
1143 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1145 /* don't count already used sample in this entry */
1147 i_sample_count += i_index_sample_used;
1153 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1154 ck->p_sample_offset_pts = calloc( i_entry, sizeof( uint32_t ) );
1157 i_sample_count = ck->i_sample_count;
1158 for( i = 0; i < i_entry; i++ )
1163 i_rest = ctts->i_sample_count[i_index] -
1164 i_index_sample_used;
1166 i_used = __MIN( i_rest, i_sample_count );
1168 i_index_sample_used += i_used;
1169 i_sample_count -= i_used;
1171 ck->p_sample_count_pts[i] = i_used;
1172 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1174 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1177 i_index_sample_used = 0;
1183 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:"I64Fd"s",
1184 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1185 i_last_dts / p_demux_track->i_timescale );
1192 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1194 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1195 unsigned int i_chunk, es_out_id_t **pp_es )
1197 MP4_Box_t *p_sample;
1202 if( !p_track->chunk[i_chunk].i_sample_description_index )
1204 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1205 p_track->i_track_ID );
1206 return VLC_EGENERIC;
1209 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1210 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1213 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1215 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1216 p_track->i_track_ID );
1217 return VLC_EGENERIC;
1220 p_track->p_sample = p_sample;
1222 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1224 MP4_Box_data_sample_soun_t *p_soun;
1226 p_soun = p_sample->data.p_sample_soun;
1228 if( p_soun->i_qt_version == 0 )
1230 switch( p_sample->i_type )
1232 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1233 p_soun->i_qt_version = 1;
1234 p_soun->i_sample_per_packet = 64;
1235 p_soun->i_bytes_per_packet = 34;
1236 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1237 p_soun->i_bytes_per_sample = 2;
1239 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1240 p_soun->i_qt_version = 1;
1241 p_soun->i_sample_per_packet = 6;
1242 p_soun->i_bytes_per_packet = 2;
1243 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1244 p_soun->i_bytes_per_sample = 2;
1246 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1247 p_soun->i_qt_version = 1;
1248 p_soun->i_sample_per_packet = 12;
1249 p_soun->i_bytes_per_packet = 2;
1250 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1251 p_soun->i_bytes_per_sample = 2;
1253 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1254 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1255 p_soun->i_samplesize = 8;
1262 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1264 p_soun->i_qt_version = 0;
1269 /* It's a little ugly but .. there are special cases */
1270 switch( p_sample->i_type )
1272 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1273 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1274 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1276 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1277 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1279 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1280 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1283 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1284 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1285 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1286 /* FIXME UTF-8 doesn't work here ? */
1287 /* p_track->fmt.subs.psz_encoding = strdup( "UTF-8" ); */
1291 p_track->fmt.i_codec = p_sample->i_type;
1295 /* now see if esds is present and if so create a data packet
1296 with decoder_specific_info */
1297 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1298 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1299 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1300 ( p_esds->data.p_esds )&&
1303 /* First update information based on i_objectTypeIndication */
1304 switch( p_decconfig->i_objectTypeIndication )
1306 case( 0x20 ): /* MPEG4 VIDEO */
1307 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1310 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1317 case( 0x65): /* MPEG2 video */
1318 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1320 /* Theses are MPEG2-AAC */
1321 case( 0x66): /* main profile */
1322 case( 0x67): /* Low complexity profile */
1323 case( 0x68): /* Scaleable Sampling rate profile */
1324 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1326 /* true MPEG 2 audio */
1328 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1330 case( 0x6a): /* MPEG1 video */
1331 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1333 case( 0x6b): /* MPEG1 audio */
1334 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1336 case( 0x6c ): /* jpeg */
1337 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1341 case( 0xe0 ): /* NeroDigital: dvd subs */
1342 if( p_track->fmt.i_cat == SPU_ES )
1344 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1349 /* Unknown entry, but don't touch i_fourcc */
1351 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1352 p_decconfig->i_objectTypeIndication,
1353 p_track->i_track_ID );
1356 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1357 if( p_track->fmt.i_extra > 0 )
1359 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1360 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1361 p_track->fmt.i_extra );
1366 switch( p_sample->i_type )
1368 /* qt decoder, send the complete chunk */
1369 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1370 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1371 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1372 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1373 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1374 p_track->fmt.i_extra =
1375 p_sample->data.p_sample_vide->i_qt_image_description;
1376 if( p_track->fmt.i_extra > 0 )
1378 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1379 memcpy( p_track->fmt.p_extra,
1380 p_sample->data.p_sample_vide->p_qt_image_description,
1381 p_track->fmt.i_extra);
1384 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1385 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1386 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1387 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1388 p_track->fmt.i_extra =
1389 p_sample->data.p_sample_soun->i_qt_description;
1390 if( p_track->fmt.i_extra > 0 )
1392 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1393 memcpy( p_track->fmt.p_extra,
1394 p_sample->data.p_sample_soun->p_qt_description,
1395 p_track->fmt.i_extra);
1399 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1400 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1402 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1406 /* Hack: use a packetizer to reecampsulate data in anexe B format */
1407 msg_Dbg( p_demux, "avcC: size=%d", p_avcC->data.p_avcC->i_avcC );
1408 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1409 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1410 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC, p_track->fmt.i_extra );
1411 p_track->fmt.b_packetized = VLC_FALSE;
1415 msg_Err( p_demux, "missing avcC" );
1427 /* some last initialisation */
1428 switch( p_track->fmt.i_cat )
1431 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1432 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1434 /* fall on display size */
1435 if( p_track->fmt.video.i_width <= 0 )
1436 p_track->fmt.video.i_width = p_track->i_width;
1437 if( p_track->fmt.video.i_height <= 0 )
1438 p_track->fmt.video.i_height = p_track->i_height;
1440 /* Find out apect ratio from display size */
1441 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1442 /* Work-around buggy muxed files */
1443 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1444 p_track->fmt.video.i_aspect =
1445 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1450 p_track->fmt.audio.i_channels =
1451 p_sample->data.p_sample_soun->i_channelcount;
1452 p_track->fmt.audio.i_rate =
1453 p_sample->data.p_sample_soun->i_sampleratehi;
1454 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1455 p_sample->data.p_sample_soun->i_sampleratehi *
1456 p_sample->data.p_sample_soun->i_samplesize;
1457 p_track->fmt.audio.i_bitspersample =
1458 p_sample->data.p_sample_soun->i_samplesize;
1465 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1470 /* given a time it return sample/chunk
1471 * it also update elst field of the track
1473 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1474 int64_t i_start, uint32_t *pi_chunk,
1475 uint32_t *pi_sample )
1477 demux_sys_t *p_sys = p_demux->p_sys;
1480 unsigned int i_sample;
1481 unsigned int i_chunk;
1484 /* FIXME see if it's needed to check p_track->i_chunk_count */
1485 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1487 return( VLC_EGENERIC );
1490 /* handle elst (find the correct one) */
1491 MP4_TrackSetELST( p_demux, p_track, i_start );
1492 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1494 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1495 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1497 /* now calculate i_start for this elst */
1499 i_start -= p_track->i_elst_time * I64C(1000000) / p_sys->i_timescale;
1507 /* to track time scale */
1508 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1509 /* add elst offset */
1510 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1511 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1512 elst->i_media_time[p_track->i_elst] > 0 )
1514 i_start += elst->i_media_time[p_track->i_elst];
1517 msg_Dbg( p_demux, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd
1518 "ms (track)", p_track->i_elst,
1519 i_mvt * 1000 / p_sys->i_timescale,
1520 i_start * 1000 / p_track->i_timescale );
1524 /* convert absolute time to in timescale unit */
1525 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1528 /* we start from sample 0/chunk 0, hope it won't take too much time */
1529 /* *** find good chunk *** */
1530 for( i_chunk = 0; ; i_chunk++ )
1532 if( i_chunk + 1 >= p_track->i_chunk_count )
1534 /* at the end and can't check if i_start in this chunk,
1535 it will be check while searching i_sample */
1536 i_chunk = p_track->i_chunk_count - 1;
1540 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1541 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1547 /* *** find sample in the chunk *** */
1548 i_sample = p_track->chunk[i_chunk].i_sample_first;
1549 i_dts = p_track->chunk[i_chunk].i_first_dts;
1550 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1553 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1554 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1557 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1558 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1560 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1565 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1569 i_sample += ( i_start - i_dts ) /
1570 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1575 if( i_sample >= p_track->i_sample_count )
1577 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1578 "(seeking too far) chunk=%d sample=%d",
1579 p_track->i_track_ID, i_chunk, i_sample );
1580 return( VLC_EGENERIC );
1584 /* *** Try to find nearest sync points *** */
1585 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1587 unsigned int i_index;
1589 "track[Id 0x%x] using Sync Sample Box (stss)",
1590 p_track->i_track_ID );
1591 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1593 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1597 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1599 p_stss->data.p_stss->i_sample_number[i_index-1] );
1600 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1601 /* new i_sample is less than old so i_chunk can only decreased */
1602 while( i_chunk > 0 &&
1603 i_sample < p_track->chunk[i_chunk].i_sample_first )
1610 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1612 p_stss->data.p_stss->i_sample_number[i_index] );
1613 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1614 /* new i_sample is more than old so i_chunk can only increased */
1615 while( i_chunk < p_track->i_chunk_count - 1 &&
1616 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1617 p_track->chunk[i_chunk].i_sample_count )
1628 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1629 "Sample Box (stss)", p_track->i_track_ID );
1632 *pi_chunk = i_chunk;
1633 *pi_sample = i_sample;
1638 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1639 unsigned int i_chunk, unsigned int i_sample )
1641 vlc_bool_t b_reselect = VLC_FALSE;
1643 /* now see if actual es is ok */
1644 if( p_track->i_chunk < 0 ||
1645 p_track->i_chunk >= p_track->i_chunk_count - 1 ||
1646 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1647 p_track->chunk[i_chunk].i_sample_description_index )
1649 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1650 p_track->i_track_ID );
1652 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1653 p_track->p_es, &b_reselect );
1655 es_out_Del( p_demux->out, p_track->p_es );
1657 p_track->p_es = NULL;
1659 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1661 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1662 p_track->i_track_ID );
1664 p_track->b_ok = VLC_FALSE;
1665 p_track->b_selected = VLC_FALSE;
1666 return VLC_EGENERIC;
1670 /* select again the new decoder */
1673 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1676 p_track->i_chunk = i_chunk;
1677 p_track->i_sample = i_sample;
1679 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1682 /****************************************************************************
1684 ****************************************************************************
1685 * Parse track information and create all needed data to run a track
1686 * If it succeed b_ok is set to 1 else to 0
1687 ****************************************************************************/
1688 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1689 MP4_Box_t *p_box_trak )
1691 demux_sys_t *p_sys = p_demux->p_sys;
1693 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1694 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1709 /* hint track unsuported */
1711 /* set default value (-> track unusable) */
1712 p_track->b_ok = VLC_FALSE;
1713 p_track->b_enable = VLC_FALSE;
1714 p_track->b_selected = VLC_FALSE;
1716 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1723 /* do we launch this track by default ? */
1725 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1727 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1728 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1729 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1733 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
1736 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1737 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1739 if( ( !p_mdhd )||( !p_hdlr ) )
1744 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1746 for( i = 0; i < 3; i++ )
1748 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1752 switch( p_hdlr->data.p_hdlr->i_handler_type )
1754 case( FOURCC_soun ):
1755 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1759 p_track->fmt.i_cat = AUDIO_ES;
1762 case( FOURCC_vide ):
1763 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1767 p_track->fmt.i_cat = VIDEO_ES;
1770 case( FOURCC_text ):
1771 case( FOURCC_subp ):
1772 p_track->fmt.i_cat = SPU_ES;
1779 p_track->i_elst = 0;
1780 p_track->i_elst_time = 0;
1781 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1783 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1786 msg_Warn( p_demux, "elst box found" );
1787 for( i = 0; i < elst->i_entry_count; i++ )
1789 msg_Dbg( p_demux, " - [%d] duration="I64Fd"ms media time="I64Fd
1790 "ms) rate=%d.%d", i,
1791 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1792 elst->i_media_time[i] >= 0 ?
1793 elst->i_media_time[i] * 1000 / p_track->i_timescale : -1,
1794 elst->i_media_rate_integer[i],
1795 elst->i_media_rate_fraction[i] );
1802 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1804 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1805 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1810 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1811 p_track->b_drms = p_drms != NULL;
1812 p_track->p_drms = p_track->b_drms ?
1813 p_drms->data.p_sample_soun->p_drms : NULL;
1816 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1818 p_track->fmt.psz_language = strdup( language );
1821 p_udta = MP4_BoxGet( p_box_trak, "udta" );
1824 MP4_Box_t *p_0xa9xxx;
1825 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
1826 p_0xa9xxx = p_0xa9xxx->p_next )
1828 switch( p_0xa9xxx->i_type )
1830 case FOURCC_0xa9nam:
1831 p_track->fmt.psz_description =
1832 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
1838 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1839 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1841 MP4_Box_t *p_sample;
1843 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1844 if( p_sample && p_sample->data.p_sample_soun)
1846 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1847 if( p_soun->i_qt_version == 0 &&
1848 p_track->i_timescale != p_soun->i_sampleratehi )
1851 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1853 "Making both equal. (report any problem)",
1854 p_track->i_timescale, p_soun->i_sampleratehi );
1856 if( p_soun->i_sampleratehi )
1857 p_track->i_timescale = p_soun->i_sampleratehi;
1859 p_soun->i_sampleratehi = p_track->i_timescale;
1864 /* Create chunk index table and sample index table */
1865 if( TrackCreateChunksIndex( p_demux,p_track ) ||
1866 TrackCreateSamplesIndex( p_demux, p_track ) )
1868 return; /* cannot create chunks index */
1871 p_track->i_chunk = 0;
1872 p_track->i_sample = 0;
1875 if( TrackCreateES( p_demux,
1876 p_track, p_track->i_chunk,
1879 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1880 p_track->i_track_ID );
1887 for( i = 0; i < p_track->i_chunk_count; i++ )
1889 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
1890 i, p_track->chunk[i].i_sample_count,
1891 p_track->chunk[i].i_first_dts );
1896 p_track->b_ok = VLC_TRUE;
1899 /****************************************************************************
1901 ****************************************************************************
1902 * Destroy a track created by MP4_TrackCreate.
1903 ****************************************************************************/
1904 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
1906 unsigned int i_chunk;
1908 p_track->b_ok = VLC_FALSE;
1909 p_track->b_enable = VLC_FALSE;
1910 p_track->b_selected = VLC_FALSE;
1912 es_format_Clean( &p_track->fmt );
1914 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1916 if( p_track->chunk )
1918 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1919 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1921 FREE(p_track->chunk[i_chunk].p_sample_count_pts);
1922 FREE(p_track->chunk[i_chunk].p_sample_offset_pts );
1925 FREE( p_track->chunk );
1927 if( !p_track->i_sample_size )
1929 FREE( p_track->p_sample_size );
1933 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
1936 if( !p_track->b_ok )
1938 return VLC_EGENERIC;
1941 if( p_track->b_selected )
1943 msg_Warn( p_demux, "track[Id 0x%x] already selected",
1944 p_track->i_track_ID );
1948 return MP4_TrackSeek( p_demux, p_track, i_start );
1951 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
1953 if( !p_track->b_ok )
1958 if( !p_track->b_selected )
1960 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
1961 p_track->i_track_ID );
1966 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
1967 p_track->p_es, VLC_FALSE );
1970 p_track->b_selected = VLC_FALSE;
1973 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
1979 if( !p_track->b_ok )
1981 return( VLC_EGENERIC );
1984 p_track->b_selected = VLC_FALSE;
1986 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
1987 &i_chunk, &i_sample ) )
1989 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
1990 p_track->i_track_ID );
1991 return( VLC_EGENERIC );
1994 p_track->b_selected = VLC_TRUE;
1996 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
1999 p_track->b_selected = VLC_TRUE;
2001 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
2006 * 3 types: for audio
2009 #define QT_V0_MAX_SAMPLES 1024
2010 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2013 MP4_Box_data_sample_soun_t *p_soun;
2015 if( p_track->i_sample_size == 0 )
2017 /* most simple case */
2018 return p_track->p_sample_size[p_track->i_sample];
2020 if( p_track->fmt.i_cat != AUDIO_ES )
2022 return p_track->i_sample_size;
2025 p_soun = p_track->p_sample->data.p_sample_soun;
2027 if( p_soun->i_qt_version == 1 )
2029 i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
2030 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2032 else if( p_track->i_sample_size > 256 )
2034 /* We do that so we don't read too much data
2035 * (in this case we are likely dealing with compressed data) */
2036 i_size = p_track->i_sample_size;
2040 /* Read a bunch of samples at once */
2041 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2042 ( p_track->i_sample -
2043 p_track->chunk[p_track->i_chunk].i_sample_first );
2045 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2046 i_size = i_samples * p_track->i_sample_size;
2049 //fprintf( stderr, "size=%d\n", i_size );
2053 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2055 unsigned int i_sample;
2058 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2060 if( p_track->i_sample_size )
2062 MP4_Box_data_sample_soun_t *p_soun =
2063 p_track->p_sample->data.p_sample_soun;
2065 if( p_soun->i_qt_version == 0 )
2067 i_pos += ( p_track->i_sample -
2068 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2069 p_track->i_sample_size;
2073 /* we read chunk by chunk */
2079 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2080 i_sample < p_track->i_sample; i_sample++ )
2082 i_pos += p_track->p_sample_size[i_sample];
2089 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2091 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2093 MP4_Box_data_sample_soun_t *p_soun;
2095 p_soun = p_track->p_sample->data.p_sample_soun;
2097 if( p_soun->i_qt_version == 1 )
2099 /* chunk by chunk */
2101 p_track->chunk[p_track->i_chunk].i_sample_first +
2102 p_track->chunk[p_track->i_chunk].i_sample_count;
2104 else if( p_track->i_sample_size > 256 )
2106 /* We do that so we don't read too much data
2107 * (in this case we are likely dealing with compressed data) */
2108 p_track->i_sample += 1;
2113 p_track->i_sample += QT_V0_MAX_SAMPLES;
2114 if( p_track->i_sample >
2115 p_track->chunk[p_track->i_chunk].i_sample_first +
2116 p_track->chunk[p_track->i_chunk].i_sample_count )
2119 p_track->chunk[p_track->i_chunk].i_sample_first +
2120 p_track->chunk[p_track->i_chunk].i_sample_count;
2126 p_track->i_sample++;
2129 if( p_track->i_sample >= p_track->i_sample_count )
2131 /* we have reach end of the track so free decoder stuff */
2132 msg_Warn( p_demux, "track[0x%x] will be disabled", p_track->i_track_ID );
2133 MP4_TrackUnselect( p_demux, p_track );
2134 return VLC_EGENERIC;
2137 /* Have we changed chunk ? */
2138 if( p_track->i_sample >=
2139 p_track->chunk[p_track->i_chunk].i_sample_first +
2140 p_track->chunk[p_track->i_chunk].i_sample_count )
2142 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2143 p_track->i_sample ) )
2145 msg_Warn( p_demux, "track[0x%x] will be disabled "
2146 "(cannot restart decoder)", p_track->i_track_ID );
2147 MP4_TrackUnselect( p_demux, p_track );
2148 return VLC_EGENERIC;
2152 /* Have we changed elst */
2153 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2155 demux_sys_t *p_sys = p_demux->p_sys;
2156 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2157 int64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2158 p_sys->i_timescale / (int64_t)1000000;
2160 if( p_track->i_elst < elst->i_entry_count &&
2161 i_mvt >= p_track->i_elst_time +
2162 elst->i_segment_duration[p_track->i_elst] )
2164 MP4_TrackSetELST( p_demux, p_track,
2165 MP4_TrackGetDTS( p_demux, p_track ) );
2172 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2175 demux_sys_t *p_sys = p_demux->p_sys;
2176 int i_elst_last = tk->i_elst;
2178 /* handle elst (find the correct one) */
2180 tk->i_elst_time = 0;
2181 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2183 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2184 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2186 for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2188 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2190 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2194 tk->i_elst_time += i_dur;
2197 if( tk->i_elst >= elst->i_entry_count )
2199 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2200 tk->i_elst = elst->i_entry_count - 1;
2201 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2204 if( elst->i_media_time[tk->i_elst] < 0 )
2207 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2210 if( i_elst_last != tk->i_elst )
2212 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );