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 /* TODO if needed add pts
80 but quickly *add* support for edts and seeking */
84 /* Contain all needed information for read all track with vlc */
87 int i_track_ID; /* this should be unique */
89 int b_ok; /* The track is usable */
90 int b_enable; /* is the trak enable by default */
91 vlc_bool_t b_selected; /* is the trak being played */
96 /* display size only ! */
100 /* more internal data */
101 uint64_t i_timescale; /* time scale for this track only */
104 int i_elst; /* current elst */
105 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
106 MP4_Box_t *p_elst; /* elst (could be NULL) */
108 /* give the next sample to read, i_chunk is to find quickly where
109 the sample is located */
110 uint32_t i_sample; /* next sample to read */
111 uint32_t i_chunk; /* chunk where next sample is stored */
112 /* total count of chunk and sample */
113 uint32_t i_chunk_count;
114 uint32_t i_sample_count;
116 mp4_chunk_t *chunk; /* always defined for each chunk */
118 /* sample size, p_sample_size defined only if i_sample_size == 0
119 else i_sample_size is size for all sample */
120 uint32_t i_sample_size;
121 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
122 too much time to do sumations each time*/
124 MP4_Box_t *p_stbl; /* will contain all timing information */
125 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
126 MP4_Box_t *p_sample;/* point on actual sdsd */
136 MP4_Box_t *p_root; /* container for the whole file */
140 uint64_t i_time; /* time position of the presentation
141 * in movie timescale */
142 uint64_t i_timescale; /* movie time scale */
143 uint64_t i_duration; /* movie duration */
144 unsigned int i_tracks; /* number of tracks */
145 mp4_track_t *track; /* array of track */
148 /*****************************************************************************
149 * Declaration of local function
150 *****************************************************************************/
151 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *);
152 static void MP4_TrackDestroy( demux_t *, mp4_track_t * );
154 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
155 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
157 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
159 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
160 static int MP4_TrackSampleSize( mp4_track_t * );
161 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
162 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
164 /* Return time in µs of a track */
165 static inline int64_t MP4_TrackGetPTS( demux_t *p_demux, mp4_track_t *p_track )
167 #define chunk p_track->chunk[p_track->i_chunk]
169 unsigned int i_index = 0;
170 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
171 int64_t i_dts = chunk.i_first_dts;
173 while( i_sample > 0 )
175 if( i_sample > chunk.p_sample_count_dts[i_index] )
177 i_dts += chunk.p_sample_count_dts[i_index] *
178 chunk.p_sample_delta_dts[i_index];
179 i_sample -= chunk.p_sample_count_dts[i_index];
184 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
192 /* now handle elst */
193 if( p_track->p_elst )
195 demux_sys_t *p_sys = p_demux->p_sys;
196 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
198 /* convert to offset */
199 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
200 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
201 elst->i_media_time[p_track->i_elst] > 0 )
203 i_dts -= elst->i_media_time[p_track->i_elst];
206 /* add i_elst_time */
207 i_dts += p_track->i_elst_time * p_track->i_timescale /
210 if( i_dts < 0 ) i_dts = 0;
213 return I64C(1000000) * i_dts / p_track->i_timescale;
216 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
218 return I64C(1000000) * p_sys->i_time / p_sys->i_timescale;
221 #define FREE( p ) if( p ) { free( p ); (p) = NULL;}
223 /*****************************************************************************
224 * Open: check file and initializes MP4 structures
225 *****************************************************************************/
226 static int Open( vlc_object_t * p_this )
228 demux_t *p_demux = (demux_t *)p_this;
239 vlc_bool_t b_seekable;
241 /* a little test to see if it could be a mp4 */
242 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
244 msg_Warn( p_demux, "MP4 plugin discarded (cannot peek)" );
247 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
258 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
261 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
266 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
269 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
273 /*Set exported functions */
274 p_demux->pf_demux = Demux;
275 p_demux->pf_control = Control;
277 /* create our structure that will contains all data */
278 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
279 memset( p_sys, 0, sizeof( demux_sys_t ) );
281 /* Now load all boxes ( except raw data ) */
282 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
284 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
288 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
290 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
292 switch( p_ftyp->data.p_ftyp->i_major_brand )
296 "ISO Media file (isom) version %d.",
297 p_ftyp->data.p_ftyp->i_minor_version );
301 "unrecognized major file specification (%4.4s).",
302 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
308 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
311 /* the file need to have one moov box */
312 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
314 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
318 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
321 /* we have a free box as a moov, rename it */
322 p_foov->i_type = FOURCC_moov;
325 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
327 playlist_t *p_playlist;
328 playlist_item_t *p_item;
329 int i_count = MP4_BoxCount( p_rmra, "rmda" );
331 vlc_bool_t b_play = VLC_FALSE;
333 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
336 (playlist_t *)vlc_object_find( p_demux,
341 p_item = playlist_LockItemGetByInput( p_playlist,
342 ((input_thread_t *)p_demux->p_parent)->input.p_item );
343 playlist_ItemToNode( p_playlist, p_item );
345 for( i = 0; i < i_count; i++ )
347 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
351 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
355 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
357 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
358 psz_ref, (char*)&i_ref_type );
360 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
362 if( strstr( psz_ref, "qt5gateQT" ) )
364 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
367 if( !strncmp( psz_ref, "http://", 7 ) ||
368 !strncmp( psz_ref, "rtsp://", 7 ) )
370 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
373 playlist_item_t *p_child =
374 playlist_ItemNew( p_playlist,
378 playlist_NodeAddItem( p_playlist, p_child,
379 p_item->pp_parents[0]->i_view,
380 p_item, PLAYLIST_APPEND,
382 playlist_CopyParents( p_item, p_child );
389 /* msg dbg relative ? */
390 char *psz_absolute = alloca( strlen( p_demux->psz_access ) + 3 + strlen( p_demux->psz_path ) + strlen( psz_ref ) + 1);
391 char *end = strrchr( p_demux->psz_path, '/' );
395 int i_len = end + 1 - p_demux->psz_path;
397 strcpy( psz_absolute, p_demux->psz_access );
398 strcat( psz_absolute, "://" );
399 strncat( psz_absolute, p_demux->psz_path, i_len);
403 strcpy( psz_absolute, "" );
405 strcat( psz_absolute, psz_ref );
406 msg_Dbg( p_demux, "adding ref = `%s'", psz_absolute );
409 playlist_item_t *p_child =
410 playlist_ItemNew( p_playlist,
415 playlist_NodeAddItem( p_playlist, p_child,
416 p_item->pp_parents[0]->i_view,
417 p_item, PLAYLIST_APPEND,
419 playlist_CopyParents( p_item, p_child );
427 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
428 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
431 if( b_play == VLC_TRUE )
433 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
434 p_playlist->status.i_view,
435 p_playlist->status.p_item, NULL );
437 vlc_object_release( p_playlist );
441 msg_Err( p_demux, "can't find playlist" );
445 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
449 msg_Err( p_demux, "cannot find /moov/mvhd" );
454 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
455 p_demux->pf_demux = DemuxRef;
461 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
462 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
465 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
467 msg_Err( p_demux, "cannot find any /moov/trak" );
470 msg_Dbg( p_demux, "find %d track%c",
472 p_sys->i_tracks ? 's':' ' );
474 /* allocate memory */
475 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
476 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
478 /* now process each track and extract all usefull information */
479 for( i = 0; i < p_sys->i_tracks; i++ )
481 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
482 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
484 if( p_sys->track[i].b_ok )
487 switch( p_sys->track[i].fmt.i_cat )
496 psz_cat = "subtitle";
504 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
505 p_sys->track[i].i_track_ID, psz_cat,
506 p_sys->track[i].b_enable ? "enable":"disable",
507 p_sys->track[i].fmt.psz_language ?
508 p_sys->track[i].fmt.psz_language : "undef" );
512 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
513 p_sys->track[i].i_track_ID );
522 MP4_BoxFree( p_demux->s, p_sys->p_root );
528 /*****************************************************************************
529 * Demux: read packet and send them to decoders
530 *****************************************************************************
531 * TODO check for newly selected track (ie audio upt to now )
532 *****************************************************************************/
533 static int Demux( demux_t *p_demux )
535 demux_sys_t *p_sys = p_demux->p_sys;
536 unsigned int i_track;
539 unsigned int i_track_selected;
541 /* check for newly selected/unselected track */
542 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
545 mp4_track_t *tk = &p_sys->track[i_track];
547 if( tk->b_selected && tk->i_sample >= tk->i_sample_count )
549 msg_Warn( p_demux, "track[0x%x] will be disabled", tk->i_track_ID );
550 MP4_TrackUnselect( p_demux, tk);
555 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
557 if( tk->b_selected && !b )
559 MP4_TrackUnselect( p_demux, tk );
561 else if( !tk->b_selected && b)
563 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
573 if( i_track_selected <= 0 )
575 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
576 if( p_sys->i_timescale > 0 )
578 int64_t i_length = (mtime_t)1000000 *
579 (mtime_t)p_sys->i_duration /
580 (mtime_t)p_sys->i_timescale;
581 if( MP4_GetMoviePTS( p_sys ) >= i_length )
586 msg_Warn( p_demux, "no track selected, exiting..." );
590 /* first wait for the good time to read a packet */
591 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
593 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
595 /* we will read 100ms for each stream so ...*/
596 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
598 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
600 mp4_track_t *tk = &p_sys->track[i_track];
602 if( !tk->b_ok || !tk->b_selected )
607 while( MP4_TrackGetPTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
610 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
611 MP4_TrackGetPTS( p_demux, tk ),
612 MP4_GetMoviePTS( p_sys ) );
615 if( MP4_TrackSampleSize( tk ) > 0 )
620 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
622 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
624 MP4_TrackUnselect( p_demux, tk );
630 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
632 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
634 MP4_TrackUnselect( p_demux, tk );
638 if( tk->b_drms && tk->p_drms )
640 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
643 else if( tk->fmt.i_cat == SPU_ES )
645 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
646 p_block->i_buffer >= 2 )
648 uint16_t i_size = GetWBE( p_block->p_buffer );
650 if( i_size + 2 <= p_block->i_buffer )
653 /* remove the length field, and append a '\0' */
654 memmove( &p_block->p_buffer[0],
655 &p_block->p_buffer[2], i_size );
656 p_block->p_buffer[i_size] = '\0';
657 p_block->i_buffer = i_size + 1;
659 /* convert \r -> \n */
660 while( ( p = strchr( p_block->p_buffer, '\r' ) ) )
668 p_block->i_buffer = 0;
672 p_block->i_dts = MP4_TrackGetPTS( p_demux, tk ) + 1;
674 p_block->i_pts = tk->fmt.i_cat == VIDEO_ES ?
675 0 : p_block->i_dts + 1;
677 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
679 es_out_Send( p_demux->out, tk->p_es, p_block );
684 if( MP4_TrackNextSample( p_demux, tk ) )
693 /*****************************************************************************
694 * Seek: Got to i_date
695 ******************************************************************************/
696 static int Seek( demux_t *p_demux, mtime_t i_date )
698 demux_sys_t *p_sys = p_demux->p_sys;
699 unsigned int i_track;
701 /* First update update global time */
702 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
703 p_sys->i_pcr = i_date;
705 /* Now for each stream try to go to this time */
706 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
708 mp4_track_t *tk = &p_sys->track[i_track];
709 MP4_TrackSeek( p_demux, tk, i_date );
714 /*****************************************************************************
716 *****************************************************************************/
717 static int Control( demux_t *p_demux, int i_query, va_list args )
719 demux_sys_t *p_sys = p_demux->p_sys;
726 case DEMUX_GET_POSITION:
727 pf = (double*)va_arg( args, double * );
728 if( p_sys->i_duration > 0 )
730 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
738 case DEMUX_SET_POSITION:
739 f = (double)va_arg( args, double );
740 if( p_sys->i_timescale > 0 )
742 i64 = (int64_t)( f * (double)1000000 *
743 (double)p_sys->i_duration /
744 (double)p_sys->i_timescale );
745 return Seek( p_demux, i64 );
747 else return VLC_SUCCESS;
750 pi64 = (int64_t*)va_arg( args, int64_t * );
751 if( p_sys->i_timescale > 0 )
753 *pi64 = (mtime_t)1000000 *
754 (mtime_t)p_sys->i_time /
755 (mtime_t)p_sys->i_timescale;
761 i64 = (int64_t)va_arg( args, int64_t );
762 return Seek( p_demux, i64 );
764 case DEMUX_GET_LENGTH:
765 pi64 = (int64_t*)va_arg( args, int64_t * );
766 if( p_sys->i_timescale > 0 )
768 *pi64 = (mtime_t)1000000 *
769 (mtime_t)p_sys->i_duration /
770 (mtime_t)p_sys->i_timescale;
776 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
781 vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
783 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
784 MP4_Box_t *p_0xa9xxx;
789 *pp_meta = meta = vlc_meta_New();
790 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
791 p_0xa9xxx = p_0xa9xxx->p_next )
793 switch( p_0xa9xxx->i_type )
795 case FOURCC_0xa9nam: /* Full name */
796 vlc_meta_Add( meta, VLC_META_TITLE,
797 p_0xa9xxx->data.p_0xa9xxx->psz_text );
800 vlc_meta_Add( meta, VLC_META_AUTHOR,
801 p_0xa9xxx->data.p_0xa9xxx->psz_text );
804 vlc_meta_Add( meta, VLC_META_ARTIST,
805 p_0xa9xxx->data.p_0xa9xxx->psz_text );
808 vlc_meta_Add( meta, VLC_META_COPYRIGHT,
809 p_0xa9xxx->data.p_0xa9xxx->psz_text );
811 case FOURCC_0xa9day: /* Creation Date */
812 vlc_meta_Add( meta, VLC_META_DATE,
813 p_0xa9xxx->data.p_0xa9xxx->psz_text );
815 case FOURCC_0xa9des: /* Description */
816 vlc_meta_Add( meta, VLC_META_DESCRIPTION,
817 p_0xa9xxx->data.p_0xa9xxx->psz_text );
819 case FOURCC_0xa9gen: /* Genre */
820 vlc_meta_Add( meta, VLC_META_GENRE,
821 p_0xa9xxx->data.p_0xa9xxx->psz_text );
825 case FOURCC_0xa9inf: /* Information */
826 case FOURCC_0xa9alb: /* Album */
827 case FOURCC_0xa9dir: /* Director */
828 case FOURCC_0xa9dis: /* Disclaimer */
829 case FOURCC_0xa9enc: /* Encoded By */
830 case FOURCC_0xa9trk: /* Track */
831 case FOURCC_0xa9cmt: /* Commment */
832 case FOURCC_0xa9url: /* URL */
833 case FOURCC_0xa9req: /* Requirements */
834 case FOURCC_0xa9fmt: /* Original Format */
835 case FOURCC_0xa9dsa: /* Display Source As */
836 case FOURCC_0xa9hst: /* Host Computer */
837 case FOURCC_0xa9prd: /* Producer */
838 case FOURCC_0xa9prf: /* Performers */
839 case FOURCC_0xa9ope: /* Original Performer */
840 case FOURCC_0xa9src: /* Providers Source Content */
841 case FOURCC_0xa9wrt: /* Writer */
842 case FOURCC_0xa9com: /* Composer */
843 case FOURCC_WLOC: /* Window Location */
844 /* TODO one day, but they aren't really meaningfull */
854 case DEMUX_GET_TITLE_INFO:
855 case DEMUX_SET_NEXT_DEMUX_TIME:
856 case DEMUX_SET_GROUP:
860 msg_Warn( p_demux, "control query unimplemented !!!" );
865 /*****************************************************************************
866 * Close: frees unused data
867 *****************************************************************************/
868 static void Close ( vlc_object_t * p_this )
870 unsigned int i_track;
871 demux_t * p_demux = (demux_t *)p_this;
872 demux_sys_t *p_sys = p_demux->p_sys;
874 msg_Dbg( p_demux, "freeing all memory" );
876 MP4_BoxFree( p_demux->s, p_sys->p_root );
877 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
879 MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
881 FREE( p_sys->track );
888 /****************************************************************************
889 * Local functions, specific to vlc
890 ****************************************************************************/
892 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
893 static int TrackCreateChunksIndex( demux_t *p_demux,
894 mp4_track_t *p_demux_track )
896 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
899 unsigned int i_chunk;
900 unsigned int i_index, i_last;
902 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
903 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
904 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
906 return( VLC_EGENERIC );
909 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
910 if( !p_demux_track->i_chunk_count )
912 msg_Warn( p_demux, "no chunk defined" );
913 return( VLC_EGENERIC );
915 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
916 sizeof( mp4_chunk_t ) );
918 /* first we read chunk offset */
919 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
921 p_demux_track->chunk[i_chunk].i_offset =
922 p_co64->data.p_co64->i_chunk_offset[i_chunk];
925 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
926 to be used for the sample XXX begin to 1
927 We construct it begining at the end */
928 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
929 i_index = p_stsc->data.p_stsc->i_entry_count;
932 msg_Warn( p_demux, "cannot read chunk table or table empty" );
933 return( VLC_EGENERIC );
938 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
939 i_chunk < i_last; i_chunk++ )
941 p_demux_track->chunk[i_chunk].i_sample_description_index =
942 p_stsc->data.p_stsc->i_sample_description_index[i_index];
943 p_demux_track->chunk[i_chunk].i_sample_count =
944 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
946 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
949 p_demux_track->chunk[0].i_sample_first = 0;
950 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
952 p_demux_track->chunk[i_chunk].i_sample_first =
953 p_demux_track->chunk[i_chunk-1].i_sample_first +
954 p_demux_track->chunk[i_chunk-1].i_sample_count;
957 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
958 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
963 static int TrackCreateSamplesIndex( demux_t *p_demux,
964 mp4_track_t *p_demux_track )
966 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
967 ctts make same mapping but for composition time,
968 not yet used and probably not usefull */
969 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
970 that uses a compressed form FIXME make them in libmp4
972 /* TODO use also stss and stsh table for seeking */
973 /* FIXME use edit table */
978 int64_t i_index_sample_used;
982 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
983 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
985 if( ( !p_stts )||( !p_stsz ) )
987 msg_Warn( p_demux, "cannot read sample table" );
988 return( VLC_EGENERIC );
991 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
994 /* for sample size, there are 2 case */
995 if( p_stsz->data.p_stsz->i_sample_size )
997 /* 1: all sample have the same size, so no need to construct a table */
998 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
999 p_demux_track->p_sample_size = NULL;
1003 /* 2: each sample can have a different size */
1004 p_demux_track->i_sample_size = 0;
1005 p_demux_track->p_sample_size =
1006 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1008 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1010 p_demux_track->p_sample_size[i_sample] =
1011 p_stsz->data.p_stsz->i_entry_size[i_sample];
1014 /* we have extracted all the information from stsz, now use stts */
1016 /* if we don't want to waste too much memory, we can't expand
1017 the box! so each chunk will contain an "extract" of this table
1018 for fast research */
1021 i_index = 0; i_index_sample_used = 0;
1023 /* create and init last data for each chunk */
1024 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1027 int64_t i_entry, i_sample_count, i;
1030 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
1032 /* count how many entries are needed for this chunk
1033 * for p_sample_delta_dts and p_sample_count_dts */
1034 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
1037 while( i_sample_count > 0 )
1040 p_stts->data.p_stts->i_sample_count[i_index+i_entry];
1043 /* don't count already used sample in this entry */
1044 i_sample_count += i_index_sample_used;
1050 p_demux_track->chunk[i_chunk].p_sample_count_dts =
1051 calloc( i_entry, sizeof( uint32_t ) );
1052 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
1053 calloc( i_entry, sizeof( uint32_t ) );
1056 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
1057 for( i = 0; i < i_entry; i++ )
1062 i_rest = p_stts->data.p_stts->i_sample_count[i_index] -
1063 i_index_sample_used;
1065 i_used = __MIN( i_rest, i_sample_count );
1067 i_index_sample_used += i_used;
1068 i_sample_count -= i_used;
1070 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
1072 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
1073 p_stts->data.p_stts->i_sample_delta[i_index];
1075 i_last_dts += i_used *
1076 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
1078 if( i_index_sample_used >=
1079 p_stts->data.p_stts->i_sample_count[i_index] )
1082 i_index_sample_used = 0;
1087 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:"I64Fd"s",
1088 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1089 i_last_dts / p_demux_track->i_timescale );
1096 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1098 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1099 unsigned int i_chunk, es_out_id_t **pp_es )
1101 MP4_Box_t *p_sample;
1106 if( !p_track->chunk[i_chunk].i_sample_description_index )
1108 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1109 p_track->i_track_ID );
1110 return VLC_EGENERIC;
1113 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1114 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1117 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1119 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1120 p_track->i_track_ID );
1121 return VLC_EGENERIC;
1124 p_track->p_sample = p_sample;
1126 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1128 MP4_Box_data_sample_soun_t *p_soun;
1130 p_soun = p_sample->data.p_sample_soun;
1132 if( p_soun->i_qt_version == 0 )
1134 switch( p_sample->i_type )
1136 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1137 p_soun->i_qt_version = 1;
1138 p_soun->i_sample_per_packet = 64;
1139 p_soun->i_bytes_per_packet = 34;
1140 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1141 p_soun->i_bytes_per_sample = 2;
1143 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1144 p_soun->i_qt_version = 1;
1145 p_soun->i_sample_per_packet = 6;
1146 p_soun->i_bytes_per_packet = 2;
1147 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1148 p_soun->i_bytes_per_sample = 2;
1150 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1151 p_soun->i_qt_version = 1;
1152 p_soun->i_sample_per_packet = 12;
1153 p_soun->i_bytes_per_packet = 2;
1154 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1155 p_soun->i_bytes_per_sample = 2;
1157 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1158 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1159 p_soun->i_samplesize = 8;
1166 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1168 p_soun->i_qt_version = 0;
1173 /* It's a little ugly but .. there are special cases */
1174 switch( p_sample->i_type )
1176 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1177 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1178 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1180 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1181 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1183 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1184 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1187 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1188 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1189 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1190 /* FIXME UTF-8 doesn't work here ? */
1191 /* p_track->fmt.subs.psz_encoding = strdup( "UTF-8" ); */
1195 p_track->fmt.i_codec = p_sample->i_type;
1199 /* now see if esds is present and if so create a data packet
1200 with decoder_specific_info */
1201 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1202 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1203 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1204 ( p_esds->data.p_esds )&&
1207 /* First update information based on i_objectTypeIndication */
1208 switch( p_decconfig->i_objectTypeIndication )
1210 case( 0x20 ): /* MPEG4 VIDEO */
1211 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1214 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1221 case( 0x65): /* MPEG2 video */
1222 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1224 /* Theses are MPEG2-AAC */
1225 case( 0x66): /* main profile */
1226 case( 0x67): /* Low complexity profile */
1227 case( 0x68): /* Scaleable Sampling rate profile */
1228 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1230 /* true MPEG 2 audio */
1232 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1234 case( 0x6a): /* MPEG1 video */
1235 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1237 case( 0x6b): /* MPEG1 audio */
1238 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1240 case( 0x6c ): /* jpeg */
1241 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1245 case( 0xe0 ): /* NeroDigital: dvd subs */
1246 if( p_track->fmt.i_cat == SPU_ES )
1248 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1253 /* Unknown entry, but don't touch i_fourcc */
1255 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1256 p_decconfig->i_objectTypeIndication,
1257 p_track->i_track_ID );
1260 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1261 if( p_track->fmt.i_extra > 0 )
1263 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1264 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1265 p_track->fmt.i_extra );
1270 switch( p_sample->i_type )
1272 /* qt decoder, send the complete chunk */
1273 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1274 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1275 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1276 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1277 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1278 p_track->fmt.i_extra =
1279 p_sample->data.p_sample_vide->i_qt_image_description;
1280 if( p_track->fmt.i_extra > 0 )
1282 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1283 memcpy( p_track->fmt.p_extra,
1284 p_sample->data.p_sample_vide->p_qt_image_description,
1285 p_track->fmt.i_extra);
1288 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1289 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1290 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1291 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1292 p_track->fmt.i_extra =
1293 p_sample->data.p_sample_soun->i_qt_description;
1294 if( p_track->fmt.i_extra > 0 )
1296 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1297 memcpy( p_track->fmt.p_extra,
1298 p_sample->data.p_sample_soun->p_qt_description,
1299 p_track->fmt.i_extra);
1303 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1304 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1306 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1310 /* Hack: use a packetizer to reecampsulate data in anexe B format */
1311 msg_Dbg( p_demux, "avcC: size=%d", p_avcC->data.p_avcC->i_avcC );
1312 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1313 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1314 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC, p_track->fmt.i_extra );
1315 p_track->fmt.b_packetized = VLC_FALSE;
1319 msg_Err( p_demux, "missing avcC" );
1331 /* some last initialisation */
1332 switch( p_track->fmt.i_cat )
1335 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1336 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1338 /* fall on display size */
1339 if( p_track->fmt.video.i_width <= 0 )
1340 p_track->fmt.video.i_width = p_track->i_width;
1341 if( p_track->fmt.video.i_height <= 0 )
1342 p_track->fmt.video.i_height = p_track->i_height;
1344 /* Find out apect ratio from display size */
1345 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1346 /* Work-around buggy muxed files */
1347 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1348 p_track->fmt.video.i_aspect =
1349 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1354 p_track->fmt.audio.i_channels =
1355 p_sample->data.p_sample_soun->i_channelcount;
1356 p_track->fmt.audio.i_rate =
1357 p_sample->data.p_sample_soun->i_sampleratehi;
1358 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1359 p_sample->data.p_sample_soun->i_sampleratehi *
1360 p_sample->data.p_sample_soun->i_samplesize;
1361 p_track->fmt.audio.i_bitspersample =
1362 p_sample->data.p_sample_soun->i_samplesize;
1369 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1374 /* given a time it return sample/chunk
1375 * it also update elst field of the track
1377 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1378 int64_t i_start, uint32_t *pi_chunk,
1379 uint32_t *pi_sample )
1381 demux_sys_t *p_sys = p_demux->p_sys;
1384 unsigned int i_sample;
1385 unsigned int i_chunk;
1388 /* FIXME see if it's needed to check p_track->i_chunk_count */
1389 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1391 return( VLC_EGENERIC );
1394 /* handle elst (find the correct one) */
1395 MP4_TrackSetELST( p_demux, p_track, i_start );
1396 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1398 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1399 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1401 /* now calculate i_start for this elst */
1403 i_start -= p_track->i_elst_time * I64C(1000000) / p_sys->i_timescale;
1411 /* to track time scale */
1412 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1413 /* add elst offset */
1414 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1415 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1416 elst->i_media_time[p_track->i_elst] > 0 )
1418 i_start += elst->i_media_time[p_track->i_elst];
1421 msg_Dbg( p_demux, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd
1422 "ms (track)", p_track->i_elst,
1423 i_mvt * 1000 / p_sys->i_timescale,
1424 i_start * 1000 / p_track->i_timescale );
1428 /* convert absolute time to in timescale unit */
1429 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1432 /* we start from sample 0/chunk 0, hope it won't take too much time */
1433 /* *** find good chunk *** */
1434 for( i_chunk = 0; ; i_chunk++ )
1436 if( i_chunk + 1 >= p_track->i_chunk_count )
1438 /* at the end and can't check if i_start in this chunk,
1439 it will be check while searching i_sample */
1440 i_chunk = p_track->i_chunk_count - 1;
1444 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1445 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1451 /* *** find sample in the chunk *** */
1452 i_sample = p_track->chunk[i_chunk].i_sample_first;
1453 i_dts = p_track->chunk[i_chunk].i_first_dts;
1454 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1457 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1458 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1461 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1462 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1464 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1469 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1473 i_sample += ( i_start - i_dts ) /
1474 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1479 if( i_sample >= p_track->i_sample_count )
1481 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1482 "(seeking too far) chunk=%d sample=%d",
1483 p_track->i_track_ID, i_chunk, i_sample );
1484 return( VLC_EGENERIC );
1488 /* *** Try to find nearest sync points *** */
1489 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1491 unsigned int i_index;
1493 "track[Id 0x%x] using Sync Sample Box (stss)",
1494 p_track->i_track_ID );
1495 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1497 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1501 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1503 p_stss->data.p_stss->i_sample_number[i_index-1] );
1504 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1505 /* new i_sample is less than old so i_chunk can only decreased */
1506 while( i_chunk > 0 &&
1507 i_sample < p_track->chunk[i_chunk].i_sample_first )
1514 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1516 p_stss->data.p_stss->i_sample_number[i_index] );
1517 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1518 /* new i_sample is more than old so i_chunk can only increased */
1519 while( i_chunk < p_track->i_chunk_count - 1 &&
1520 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1521 p_track->chunk[i_chunk].i_sample_count )
1532 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1533 "Sample Box (stss)", p_track->i_track_ID );
1536 *pi_chunk = i_chunk;
1537 *pi_sample = i_sample;
1542 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1543 unsigned int i_chunk, unsigned int i_sample )
1545 vlc_bool_t b_reselect = VLC_FALSE;
1547 /* now see if actual es is ok */
1548 if( p_track->i_chunk < 0 ||
1549 p_track->i_chunk >= p_track->i_chunk_count - 1 ||
1550 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1551 p_track->chunk[i_chunk].i_sample_description_index )
1553 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1554 p_track->i_track_ID );
1556 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1557 p_track->p_es, &b_reselect );
1559 es_out_Del( p_demux->out, p_track->p_es );
1561 p_track->p_es = NULL;
1563 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1565 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1566 p_track->i_track_ID );
1568 p_track->b_ok = VLC_FALSE;
1569 p_track->b_selected = VLC_FALSE;
1570 return VLC_EGENERIC;
1574 /* select again the new decoder */
1577 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1580 p_track->i_chunk = i_chunk;
1581 p_track->i_sample = i_sample;
1583 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1586 /****************************************************************************
1588 ****************************************************************************
1589 * Parse track information and create all needed data to run a track
1590 * If it succeed b_ok is set to 1 else to 0
1591 ****************************************************************************/
1592 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1593 MP4_Box_t *p_box_trak )
1595 demux_sys_t *p_sys = p_demux->p_sys;
1597 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1598 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1613 /* hint track unsuported */
1615 /* set default value (-> track unusable) */
1616 p_track->b_ok = VLC_FALSE;
1617 p_track->b_enable = VLC_FALSE;
1618 p_track->b_selected = VLC_FALSE;
1620 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1627 /* do we launch this track by default ? */
1629 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1631 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1632 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1633 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1637 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
1640 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1641 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1643 if( ( !p_mdhd )||( !p_hdlr ) )
1648 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1650 for( i = 0; i < 3; i++ )
1652 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1656 switch( p_hdlr->data.p_hdlr->i_handler_type )
1658 case( FOURCC_soun ):
1659 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1663 p_track->fmt.i_cat = AUDIO_ES;
1666 case( FOURCC_vide ):
1667 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1671 p_track->fmt.i_cat = VIDEO_ES;
1674 case( FOURCC_text ):
1675 case( FOURCC_subp ):
1676 p_track->fmt.i_cat = SPU_ES;
1683 p_track->i_elst = 0;
1684 p_track->i_elst_time = 0;
1685 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1687 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1690 msg_Warn( p_demux, "elst box found" );
1691 for( i = 0; i < elst->i_entry_count; i++ )
1693 msg_Dbg( p_demux, " - [%d] duration="I64Fd"ms media time="I64Fd
1694 "ms) rate=%d.%d", i,
1695 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1696 elst->i_media_time[i] >= 0 ?
1697 elst->i_media_time[i] * 1000 / p_track->i_timescale : -1,
1698 elst->i_media_rate_integer[i],
1699 elst->i_media_rate_fraction[i] );
1706 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1708 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1709 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1714 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1715 p_track->b_drms = p_drms != NULL;
1716 p_track->p_drms = p_track->b_drms ?
1717 p_drms->data.p_sample_soun->p_drms : NULL;
1720 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1722 p_track->fmt.psz_language = strdup( language );
1725 p_udta = MP4_BoxGet( p_box_trak, "udta" );
1728 MP4_Box_t *p_0xa9xxx;
1729 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
1730 p_0xa9xxx = p_0xa9xxx->p_next )
1732 switch( p_0xa9xxx->i_type )
1734 case FOURCC_0xa9nam:
1735 p_track->fmt.psz_description =
1736 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
1742 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1743 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1745 MP4_Box_t *p_sample;
1747 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1748 if( p_sample && p_sample->data.p_sample_soun)
1750 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1751 if( p_soun->i_qt_version == 0 &&
1752 p_track->i_timescale != p_soun->i_sampleratehi )
1755 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1757 "Making both equal. (report any problem)",
1758 p_track->i_timescale, p_soun->i_sampleratehi );
1760 if( p_soun->i_sampleratehi )
1761 p_track->i_timescale = p_soun->i_sampleratehi;
1763 p_soun->i_sampleratehi = p_track->i_timescale;
1768 /* Create chunk index table and sample index table */
1769 if( TrackCreateChunksIndex( p_demux,p_track ) ||
1770 TrackCreateSamplesIndex( p_demux, p_track ) )
1772 return; /* cannot create chunks index */
1775 p_track->i_chunk = 0;
1776 p_track->i_sample = 0;
1779 if( TrackCreateES( p_demux,
1780 p_track, p_track->i_chunk,
1783 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1784 p_track->i_track_ID );
1791 for( i = 0; i < p_track->i_chunk_count; i++ )
1793 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
1794 i, p_track->chunk[i].i_sample_count,
1795 p_track->chunk[i].i_first_dts );
1800 p_track->b_ok = VLC_TRUE;
1803 /****************************************************************************
1805 ****************************************************************************
1806 * Destroy a track created by MP4_TrackCreate.
1807 ****************************************************************************/
1808 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
1810 unsigned int i_chunk;
1812 p_track->b_ok = VLC_FALSE;
1813 p_track->b_enable = VLC_FALSE;
1814 p_track->b_selected = VLC_FALSE;
1816 es_format_Clean( &p_track->fmt );
1818 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1820 if( p_track->chunk )
1822 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1823 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1826 FREE( p_track->chunk );
1828 if( !p_track->i_sample_size )
1830 FREE( p_track->p_sample_size );
1834 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
1837 if( !p_track->b_ok )
1839 return VLC_EGENERIC;
1842 if( p_track->b_selected )
1844 msg_Warn( p_demux, "track[Id 0x%x] already selected",
1845 p_track->i_track_ID );
1849 return MP4_TrackSeek( p_demux, p_track, i_start );
1852 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
1854 if( !p_track->b_ok )
1859 if( !p_track->b_selected )
1861 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
1862 p_track->i_track_ID );
1867 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
1868 p_track->p_es, VLC_FALSE );
1871 p_track->b_selected = VLC_FALSE;
1874 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
1880 if( !p_track->b_ok )
1882 return( VLC_EGENERIC );
1885 p_track->b_selected = VLC_FALSE;
1887 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
1888 &i_chunk, &i_sample ) )
1890 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
1891 p_track->i_track_ID );
1892 return( VLC_EGENERIC );
1895 p_track->b_selected = VLC_TRUE;
1897 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
1900 p_track->b_selected = VLC_TRUE;
1902 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1907 * 3 types: for audio
1910 #define QT_V0_MAX_SAMPLES 1024
1911 static int MP4_TrackSampleSize( mp4_track_t *p_track )
1914 MP4_Box_data_sample_soun_t *p_soun;
1916 if( p_track->i_sample_size == 0 )
1918 /* most simple case */
1919 return p_track->p_sample_size[p_track->i_sample];
1921 if( p_track->fmt.i_cat != AUDIO_ES )
1923 return p_track->i_sample_size;
1926 p_soun = p_track->p_sample->data.p_sample_soun;
1928 if( p_soun->i_qt_version == 1 )
1930 i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
1931 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1933 else if( p_track->i_sample_size > 256 )
1935 /* We do that so we don't read too much data
1936 * (in this case we are likely dealing with compressed data) */
1937 i_size = p_track->i_sample_size;
1941 /* Read a bunch of samples at once */
1942 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1943 ( p_track->i_sample -
1944 p_track->chunk[p_track->i_chunk].i_sample_first );
1946 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
1947 i_size = i_samples * p_track->i_sample_size;
1950 //fprintf( stderr, "size=%d\n", i_size );
1954 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
1956 unsigned int i_sample;
1959 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1961 if( p_track->i_sample_size )
1963 MP4_Box_data_sample_soun_t *p_soun =
1964 p_track->p_sample->data.p_sample_soun;
1966 if( p_soun->i_qt_version == 0 )
1968 i_pos += ( p_track->i_sample -
1969 p_track->chunk[p_track->i_chunk].i_sample_first ) *
1970 p_track->i_sample_size;
1974 /* we read chunk by chunk */
1980 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1981 i_sample < p_track->i_sample; i_sample++ )
1983 i_pos += p_track->p_sample_size[i_sample];
1990 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
1992 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
1994 MP4_Box_data_sample_soun_t *p_soun;
1996 p_soun = p_track->p_sample->data.p_sample_soun;
1998 if( p_soun->i_qt_version == 1 )
2000 /* chunk by chunk */
2002 p_track->chunk[p_track->i_chunk].i_sample_first +
2003 p_track->chunk[p_track->i_chunk].i_sample_count;
2005 else if( p_track->i_sample_size > 256 )
2007 /* We do that so we don't read too much data
2008 * (in this case we are likely dealing with compressed data) */
2009 p_track->i_sample += 1;
2014 p_track->i_sample += QT_V0_MAX_SAMPLES;
2015 if( p_track->i_sample >
2016 p_track->chunk[p_track->i_chunk].i_sample_first +
2017 p_track->chunk[p_track->i_chunk].i_sample_count )
2020 p_track->chunk[p_track->i_chunk].i_sample_first +
2021 p_track->chunk[p_track->i_chunk].i_sample_count;
2027 p_track->i_sample++;
2030 if( p_track->i_sample >= p_track->i_sample_count )
2032 /* we have reach end of the track so free decoder stuff */
2033 msg_Warn( p_demux, "track[0x%x] will be disabled", p_track->i_track_ID );
2034 MP4_TrackUnselect( p_demux, p_track );
2035 return VLC_EGENERIC;
2038 /* Have we changed chunk ? */
2039 if( p_track->i_sample >=
2040 p_track->chunk[p_track->i_chunk].i_sample_first +
2041 p_track->chunk[p_track->i_chunk].i_sample_count )
2043 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2044 p_track->i_sample ) )
2046 msg_Warn( p_demux, "track[0x%x] will be disabled "
2047 "(cannot restart decoder)", p_track->i_track_ID );
2048 MP4_TrackUnselect( p_demux, p_track );
2049 return VLC_EGENERIC;
2053 /* Have we changed elst */
2054 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2056 demux_sys_t *p_sys = p_demux->p_sys;
2057 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2058 int64_t i_mvt = MP4_TrackGetPTS( p_demux, p_track ) *
2059 p_sys->i_timescale / (int64_t)1000000;
2061 if( p_track->i_elst < elst->i_entry_count &&
2062 i_mvt >= p_track->i_elst_time +
2063 elst->i_segment_duration[p_track->i_elst] )
2065 MP4_TrackSetELST( p_demux, p_track,
2066 MP4_TrackGetPTS( p_demux, p_track ) );
2073 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2076 demux_sys_t *p_sys = p_demux->p_sys;
2077 int i_elst_last = tk->i_elst;
2079 /* handle elst (find the correct one) */
2081 tk->i_elst_time = 0;
2082 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2084 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2085 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2087 for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2089 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2091 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2095 tk->i_elst_time += i_dur;
2098 if( tk->i_elst >= elst->i_entry_count )
2100 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2101 tk->i_elst = elst->i_entry_count - 1;
2102 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2105 if( elst->i_media_time[tk->i_elst] < 0 )
2108 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2111 if( i_elst_last != tk->i_elst )
2113 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );