1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc_demux.h>
33 #include <vlc_playlist.h>
35 #include <vlc_charset.h>
43 #define uint64_t int64_t
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
53 set_category( CAT_INPUT );
54 set_subcategory( SUBCAT_INPUT_DEMUX );
55 set_description( _("MP4 stream demuxer") );
56 set_capability( "demux2", 242 );
57 set_callbacks( Open, Close );
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Demux ( demux_t * );
64 static int DemuxRef( demux_t *p_demux ){ return 0;}
65 static int Seek ( demux_t *, mtime_t );
66 static int Control ( demux_t *, int, va_list );
68 /* Contain all information about a chunk */
71 uint64_t i_offset; /* absolute position of this chunk in the file */
72 uint32_t i_sample_description_index; /* index for SampleEntry to use */
73 uint32_t i_sample_count; /* how many samples in this chunk */
74 uint32_t i_sample_first; /* index of the first sample in this chunk */
76 /* now provide way to calculate pts, dts, and offset without to
77 much memory and with fast acces */
79 /* with this we can calculate dts/pts without waste memory */
81 uint32_t *p_sample_count_dts;
82 uint32_t *p_sample_delta_dts; /* dts delta */
84 uint32_t *p_sample_count_pts;
85 int32_t *p_sample_offset_pts; /* pts-dts */
87 /* TODO if needed add pts
88 but quickly *add* support for edts and seeking */
92 /* Contain all needed information for read all track with vlc */
95 int i_track_ID; /* this should be unique */
97 int b_ok; /* The track is usable */
98 int b_enable; /* is the trak enable by default */
99 vlc_bool_t b_selected; /* is the trak being played */
104 /* display size only ! */
108 /* more internal data */
109 uint64_t i_timescale; /* time scale for this track only */
112 int i_elst; /* current elst */
113 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
114 MP4_Box_t *p_elst; /* elst (could be NULL) */
116 /* give the next sample to read, i_chunk is to find quickly where
117 the sample is located */
118 uint32_t i_sample; /* next sample to read */
119 uint32_t i_chunk; /* chunk where next sample is stored */
120 /* total count of chunk and sample */
121 uint32_t i_chunk_count;
122 uint32_t i_sample_count;
124 mp4_chunk_t *chunk; /* always defined for each chunk */
126 /* sample size, p_sample_size defined only if i_sample_size == 0
127 else i_sample_size is size for all sample */
128 uint32_t i_sample_size;
129 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
130 too much time to do sumations each time*/
132 MP4_Box_t *p_stbl; /* will contain all timing information */
133 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
134 MP4_Box_t *p_sample;/* point on actual sdsd */
144 MP4_Box_t *p_root; /* container for the whole file */
148 uint64_t i_time; /* time position of the presentation
149 * in movie timescale */
150 uint64_t i_timescale; /* movie time scale */
151 uint64_t i_duration; /* movie duration */
152 unsigned int i_tracks; /* number of tracks */
153 mp4_track_t *track; /* array of track */
156 input_title_t *p_title;
159 /*****************************************************************************
160 * Declaration of local function
161 *****************************************************************************/
162 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *);
163 static void MP4_TrackDestroy( demux_t *, mp4_track_t * );
165 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
166 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
168 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
170 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
171 static int MP4_TrackSampleSize( mp4_track_t * );
172 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
173 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
175 static void MP4_UpdateSeekpoint( demux_t * );
177 /* Return time in s of a track */
178 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
180 #define chunk p_track->chunk[p_track->i_chunk]
182 unsigned int i_index = 0;
183 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
184 int64_t i_dts = chunk.i_first_dts;
186 while( i_sample > 0 )
188 if( i_sample > chunk.p_sample_count_dts[i_index] )
190 i_dts += chunk.p_sample_count_dts[i_index] *
191 chunk.p_sample_delta_dts[i_index];
192 i_sample -= chunk.p_sample_count_dts[i_index];
197 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
205 /* now handle elst */
206 if( p_track->p_elst )
208 demux_sys_t *p_sys = p_demux->p_sys;
209 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
211 /* convert to offset */
212 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
213 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
214 elst->i_media_time[p_track->i_elst] > 0 )
216 i_dts -= elst->i_media_time[p_track->i_elst];
219 /* add i_elst_time */
220 i_dts += p_track->i_elst_time * p_track->i_timescale /
223 if( i_dts < 0 ) i_dts = 0;
226 return I64C(1000000) * i_dts / p_track->i_timescale;
229 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
231 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
232 unsigned int i_index = 0;
233 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
235 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
238 for( i_index = 0;; i_index++ )
240 if( i_sample < ck->p_sample_count_pts[i_index] )
241 return ck->p_sample_offset_pts[i_index] * I64C(1000000) /
242 (int64_t)p_track->i_timescale;
244 i_sample -= ck->p_sample_count_pts[i_index];
248 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
250 return I64C(1000000) * p_sys->i_time / p_sys->i_timescale;
253 /* Function to lookup the currently playing item */
254 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
255 playlist_item_t **pp_item );
257 /*****************************************************************************
258 * Open: check file and initializes MP4 structures
259 *****************************************************************************/
260 static int Open( vlc_object_t * p_this )
262 demux_t *p_demux = (demux_t *)p_this;
274 vlc_bool_t b_seekable;
276 /* A little test to see if it could be a mp4 */
277 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
279 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
290 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
297 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
300 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
304 /*Set exported functions */
305 p_demux->pf_demux = Demux;
306 p_demux->pf_control = Control;
308 /* create our structure that will contains all data */
309 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
310 memset( p_sys, 0, sizeof( demux_sys_t ) );
312 /* Now load all boxes ( except raw data ) */
313 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
315 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
319 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
321 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
323 switch( p_ftyp->data.p_ftyp->i_major_brand )
327 "ISO Media file (isom) version %d.",
328 p_ftyp->data.p_ftyp->i_minor_version );
332 "unrecognized major file specification (%4.4s).",
333 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
339 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
342 /* the file need to have one moov box */
343 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
345 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
349 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
352 /* we have a free box as a moov, rename it */
353 p_foov->i_type = FOURCC_moov;
356 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
358 playlist_t *p_playlist;
359 playlist_item_t *p_current, *p_item_in_category;
360 int i_count = MP4_BoxCount( p_rmra, "rmda" );
362 vlc_bool_t b_play = VLC_FALSE;
364 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
366 p_playlist = pl_Yield( p_demux );
369 b_play = FindItem( p_demux, p_playlist, &p_current );
370 p_item_in_category = playlist_ItemToNode( p_playlist, p_current, VLC_TRUE );
371 p_current->p_input->i_type = ITEM_TYPE_PLAYLIST;
373 for( i = 0; i < i_count; i++ )
375 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
379 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
383 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
385 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
386 psz_ref, (char*)&i_ref_type );
388 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
390 if( strstr( psz_ref, "qt5gateQT" ) )
392 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
395 if( !strncmp( psz_ref, "http://", 7 ) ||
396 !strncmp( psz_ref, "rtsp://", 7 ) )
403 char *psz_path = strdup( p_demux->psz_path );
404 char *end = strrchr( psz_path, '/' );
405 if( end ) end[1] = '\0';
406 else *psz_path = '\0';
408 asprintf( &psz_absolute, "%s://%s%s",
409 p_demux->psz_access, psz_path, psz_ref );
411 if( psz_ref ) free( psz_ref );
412 psz_ref = psz_absolute;
417 input_item_t *p_input;
418 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
419 p_input = input_ItemNewExt( p_playlist, psz_ref, NULL,
421 input_ItemCopyOptions( p_current->p_input, p_input );
422 playlist_BothAddInput( p_playlist, p_input,
424 PLAYLIST_APPEND, PLAYLIST_END,
425 NULL, NULL, VLC_FALSE );
430 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
431 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
433 if( psz_ref ) free( psz_ref );
435 if( b_play && p_item_in_category &&
436 p_item_in_category->i_children > 0 )
438 playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
439 p_item_in_category, NULL );
441 vlc_object_release( p_playlist );
445 msg_Err( p_demux, "can't find playlist" );
449 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
453 msg_Err( p_demux, "cannot find /moov/mvhd" );
458 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
459 p_demux->pf_demux = DemuxRef;
465 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
466 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
469 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
471 msg_Err( p_demux, "cannot find any /moov/trak" );
474 msg_Dbg( p_demux, "find %d track%c",
476 p_sys->i_tracks ? 's':' ' );
478 /* allocate memory */
479 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
480 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
482 /* now process each track and extract all usefull information */
483 for( i = 0; i < p_sys->i_tracks; i++ )
485 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
486 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak );
488 if( p_sys->track[i].b_ok )
491 switch( p_sys->track[i].fmt.i_cat )
500 psz_cat = "subtitle";
508 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
509 p_sys->track[i].i_track_ID, psz_cat,
510 p_sys->track[i].b_enable ? "enable":"disable",
511 p_sys->track[i].fmt.psz_language ?
512 p_sys->track[i].fmt.psz_language : "undef" );
516 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
517 p_sys->track[i].i_track_ID );
522 /* Process chapter if any */
523 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
526 p_sys->p_title = vlc_input_title_New();
527 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
529 seekpoint_t *s = vlc_seekpoint_New();
531 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
532 EnsureUTF8( s->psz_name );
533 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
534 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
542 MP4_BoxFree( p_demux->s, p_sys->p_root );
548 /*****************************************************************************
549 * Demux: read packet and send them to decoders
550 *****************************************************************************
551 * TODO check for newly selected track (ie audio upt to now )
552 *****************************************************************************/
553 static int Demux( demux_t *p_demux )
555 demux_sys_t *p_sys = p_demux->p_sys;
556 unsigned int i_track;
559 unsigned int i_track_selected;
561 /* check for newly selected/unselected track */
562 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
565 mp4_track_t *tk = &p_sys->track[i_track];
569 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
574 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
576 if( tk->b_selected && !b )
578 MP4_TrackUnselect( p_demux, tk );
580 else if( !tk->b_selected && b)
582 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
591 if( i_track_selected <= 0 )
593 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
594 if( p_sys->i_timescale > 0 )
596 int64_t i_length = (mtime_t)1000000 *
597 (mtime_t)p_sys->i_duration /
598 (mtime_t)p_sys->i_timescale;
599 if( MP4_GetMoviePTS( p_sys ) >= i_length )
604 msg_Warn( p_demux, "no track selected, exiting..." );
609 MP4_UpdateSeekpoint( p_demux );
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 || tk->i_sample >= tk->i_sample_count )
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((char *) 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 ) )
720 static void MP4_UpdateSeekpoint( demux_t *p_demux )
722 demux_sys_t *p_sys = p_demux->p_sys;
725 if( !p_sys->p_title )
727 i_time = MP4_GetMoviePTS( p_sys );
728 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
730 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
735 if( i != p_demux->info.i_seekpoint && i >= 0 )
737 p_demux->info.i_seekpoint = i;
738 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
741 /*****************************************************************************
742 * Seek: Got to i_date
743 ******************************************************************************/
744 static int Seek( demux_t *p_demux, mtime_t i_date )
746 demux_sys_t *p_sys = p_demux->p_sys;
747 unsigned int i_track;
749 /* First update update global time */
750 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
751 p_sys->i_pcr = i_date;
753 /* Now for each stream try to go to this time */
754 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
756 mp4_track_t *tk = &p_sys->track[i_track];
757 MP4_TrackSeek( p_demux, tk, i_date );
759 MP4_UpdateSeekpoint( p_demux );
763 /*****************************************************************************
765 *****************************************************************************/
766 static int Control( demux_t *p_demux, int i_query, va_list args )
768 demux_sys_t *p_sys = p_demux->p_sys;
775 case DEMUX_GET_POSITION:
776 pf = (double*)va_arg( args, double * );
777 if( p_sys->i_duration > 0 )
779 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
787 case DEMUX_SET_POSITION:
788 f = (double)va_arg( args, double );
789 if( p_sys->i_timescale > 0 )
791 i64 = (int64_t)( f * (double)1000000 *
792 (double)p_sys->i_duration /
793 (double)p_sys->i_timescale );
794 return Seek( p_demux, i64 );
796 else return VLC_SUCCESS;
799 pi64 = (int64_t*)va_arg( args, int64_t * );
800 if( p_sys->i_timescale > 0 )
802 *pi64 = (mtime_t)1000000 *
803 (mtime_t)p_sys->i_time /
804 (mtime_t)p_sys->i_timescale;
810 i64 = (int64_t)va_arg( args, int64_t );
811 return Seek( p_demux, i64 );
813 case DEMUX_GET_LENGTH:
814 pi64 = (int64_t*)va_arg( args, int64_t * );
815 if( p_sys->i_timescale > 0 )
817 *pi64 = (mtime_t)1000000 *
818 (mtime_t)p_sys->i_duration /
819 (mtime_t)p_sys->i_timescale;
825 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
830 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
831 MP4_Box_t *p_0xa9xxx;
833 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
836 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
843 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
844 p_0xa9xxx = p_0xa9xxx->p_next )
847 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
850 /* FIXME FIXME: should convert from whatever the character
851 * encoding of MP4 meta data is to UTF-8. */
852 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
853 if( psz_utf ) { EnsureUTF8( psz_utf ); \
854 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
856 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
857 switch( p_0xa9xxx->i_type )
859 case FOURCC_0xa9nam: /* Full name */
860 SET( vlc_meta_SetTitle );
863 SET( vlc_meta_SetArtist );
866 SET( vlc_meta_SetArtist );
869 SET( vlc_meta_SetCopyright );
871 case FOURCC_0xa9day: /* Creation Date */
872 SET( vlc_meta_SetDate );
874 case FOURCC_0xa9des: /* Description */
875 SET( vlc_meta_SetDescription );
877 case FOURCC_0xa9gen: /* Genre */
878 SET( vlc_meta_SetGenre );
881 case FOURCC_0xa9alb: /* Album */
882 SET( vlc_meta_SetAlbum );
886 case FOURCC_0xa9inf: /* Information */
887 case FOURCC_0xa9dir: /* Director */
888 case FOURCC_0xa9dis: /* Disclaimer */
889 case FOURCC_0xa9enc: /* Encoded By */
890 case FOURCC_0xa9trk: /* Track */
891 case FOURCC_0xa9cmt: /* Commment */
892 case FOURCC_0xa9url: /* URL */
893 case FOURCC_0xa9req: /* Requirements */
894 case FOURCC_0xa9fmt: /* Original Format */
895 case FOURCC_0xa9dsa: /* Display Source As */
896 case FOURCC_0xa9hst: /* Host Computer */
897 case FOURCC_0xa9prd: /* Producer */
898 case FOURCC_0xa9prf: /* Performers */
899 case FOURCC_0xa9ope: /* Original Performer */
900 case FOURCC_0xa9src: /* Providers Source Content */
901 case FOURCC_0xa9wrt: /* Writer */
902 case FOURCC_0xa9com: /* Composer */
903 case FOURCC_WLOC: /* Window Location */
904 /* TODO one day, but they aren't really meaningfull */
915 case DEMUX_GET_TITLE_INFO:
917 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
918 int *pi_int = (int*)va_arg( args, int* );
919 int *pi_title_offset = (int*)va_arg( args, int* );
920 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
922 if( !p_sys->p_title )
926 *ppp_title = malloc( sizeof( input_title_t**) );
927 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
928 *pi_title_offset = 0;
929 *pi_seekpoint_offset = 0;
932 case DEMUX_SET_TITLE:
934 const int i_title = (int)va_arg( args, int );
935 if( !p_sys->p_title || i_title != 0 )
939 case DEMUX_SET_SEEKPOINT:
941 const int i_seekpoint = (int)va_arg( args, int );
942 if( !p_sys->p_title )
944 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
947 case DEMUX_SET_NEXT_DEMUX_TIME:
948 case DEMUX_SET_GROUP:
952 msg_Warn( p_demux, "control query %u unimplemented", i_query );
957 /*****************************************************************************
958 * Close: frees unused data
959 *****************************************************************************/
960 static void Close ( vlc_object_t * p_this )
962 unsigned int i_track;
963 demux_t * p_demux = (demux_t *)p_this;
964 demux_sys_t *p_sys = p_demux->p_sys;
966 msg_Dbg( p_demux, "freeing all memory" );
968 MP4_BoxFree( p_demux->s, p_sys->p_root );
969 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
971 MP4_TrackDestroy( p_demux, &p_sys->track[i_track] );
973 FREENULL( p_sys->track );
976 vlc_input_title_Delete( p_sys->p_title );
983 /****************************************************************************
984 * Local functions, specific to vlc
985 ****************************************************************************/
987 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
988 static int TrackCreateChunksIndex( demux_t *p_demux,
989 mp4_track_t *p_demux_track )
991 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
994 unsigned int i_chunk;
995 unsigned int i_index, i_last;
997 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
998 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
999 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1001 return( VLC_EGENERIC );
1004 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1005 if( !p_demux_track->i_chunk_count )
1007 msg_Warn( p_demux, "no chunk defined" );
1008 return( VLC_EGENERIC );
1010 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1011 sizeof( mp4_chunk_t ) );
1013 /* first we read chunk offset */
1014 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1016 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1018 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1020 ck->i_first_dts = 0;
1021 ck->p_sample_count_dts = NULL;
1022 ck->p_sample_delta_dts = NULL;
1023 ck->p_sample_count_pts = NULL;
1024 ck->p_sample_offset_pts = NULL;
1027 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1028 to be used for the sample XXX begin to 1
1029 We construct it begining at the end */
1030 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1031 i_index = p_stsc->data.p_stsc->i_entry_count;
1034 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1035 return( VLC_EGENERIC );
1040 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1041 i_chunk < i_last; i_chunk++ )
1043 p_demux_track->chunk[i_chunk].i_sample_description_index =
1044 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1045 p_demux_track->chunk[i_chunk].i_sample_count =
1046 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1048 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1051 p_demux_track->chunk[0].i_sample_first = 0;
1052 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1054 p_demux_track->chunk[i_chunk].i_sample_first =
1055 p_demux_track->chunk[i_chunk-1].i_sample_first +
1056 p_demux_track->chunk[i_chunk-1].i_sample_count;
1059 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1060 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1065 static int TrackCreateSamplesIndex( demux_t *p_demux,
1066 mp4_track_t *p_demux_track )
1069 MP4_Box_data_stsz_t *stsz;
1070 MP4_Box_data_stts_t *stts;
1071 /* TODO use also stss and stsh table for seeking */
1072 /* FIXME use edit table */
1077 int64_t i_index_sample_used;
1082 * Gives the sample size for each samples. There is also a stz2 table
1083 * (compressed form) that we need to implement TODO */
1084 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1087 /* FIXME and stz2 */
1088 msg_Warn( p_demux, "cannot find STSZ box" );
1089 return VLC_EGENERIC;
1091 stsz = p_box->data.p_stsz;
1094 * Gives mapping between sample and decoding time
1096 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1099 msg_Warn( p_demux, "cannot find STTS box" );
1100 return VLC_EGENERIC;
1102 stts = p_box->data.p_stts;
1104 /* Use stsz table to create a sample number -> sample size table */
1105 p_demux_track->i_sample_count = stsz->i_sample_count;
1106 if( stsz->i_sample_size )
1108 /* 1: all sample have the same size, so no need to construct a table */
1109 p_demux_track->i_sample_size = stsz->i_sample_size;
1110 p_demux_track->p_sample_size = NULL;
1114 /* 2: each sample can have a different size */
1115 p_demux_track->i_sample_size = 0;
1116 p_demux_track->p_sample_size =
1117 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1119 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1121 p_demux_track->p_sample_size[i_sample] =
1122 stsz->i_entry_size[i_sample];
1126 /* Use stts table to create a sample number -> dts table.
1127 * XXX: if we don't want to waste too much memory, we can't expand
1128 * the box! so each chunk will contain an "extract" of this table
1129 * for fast research (problem with raw stream where a sample is sometime
1130 * just channels*bits_per_sample/8 */
1133 i_index = 0; i_index_sample_used = 0;
1134 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1136 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1137 int64_t i_entry, i_sample_count, i;
1140 ck->i_first_dts = i_last_dts;
1142 /* count how many entries are needed for this chunk
1143 * for p_sample_delta_dts and p_sample_count_dts */
1144 i_sample_count = ck->i_sample_count;
1147 while( i_sample_count > 0 )
1149 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1150 /* don't count already used sample in this entry */
1152 i_sample_count += i_index_sample_used;
1158 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1159 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1162 i_sample_count = ck->i_sample_count;
1163 for( i = 0; i < i_entry; i++ )
1168 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1170 i_used = __MIN( i_rest, i_sample_count );
1172 i_index_sample_used += i_used;
1173 i_sample_count -= i_used;
1175 ck->p_sample_count_dts[i] = i_used;
1176 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1178 i_last_dts += i_used * ck->p_sample_delta_dts[i];
1180 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1183 i_index_sample_used = 0;
1189 * Gives the delta between decoding time (dts) and composition table (pts)
1191 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1194 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1196 msg_Warn( p_demux, "CTTS table" );
1198 /* Create pts-dts table per chunk */
1199 i_index = 0; i_index_sample_used = 0;
1200 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1202 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1203 int64_t i_entry, i_sample_count, i;
1205 /* count how many entries are needed for this chunk
1206 * for p_sample_delta_dts and p_sample_count_dts */
1207 i_sample_count = ck->i_sample_count;
1210 while( i_sample_count > 0 )
1212 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1214 /* don't count already used sample in this entry */
1216 i_sample_count += i_index_sample_used;
1222 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1223 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1226 i_sample_count = ck->i_sample_count;
1227 for( i = 0; i < i_entry; i++ )
1232 i_rest = ctts->i_sample_count[i_index] -
1233 i_index_sample_used;
1235 i_used = __MIN( i_rest, i_sample_count );
1237 i_index_sample_used += i_used;
1238 i_sample_count -= i_used;
1240 ck->p_sample_count_pts[i] = i_used;
1241 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1243 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1246 i_index_sample_used = 0;
1252 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:"I64Fd"s",
1253 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1254 i_last_dts / p_demux_track->i_timescale );
1261 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1263 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1264 unsigned int i_chunk, es_out_id_t **pp_es )
1266 MP4_Box_t *p_sample;
1272 if( !p_track->chunk[i_chunk].i_sample_description_index )
1274 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1275 p_track->i_track_ID );
1276 return VLC_EGENERIC;
1279 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1280 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1283 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1285 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1286 p_track->i_track_ID );
1287 return VLC_EGENERIC;
1290 p_track->p_sample = p_sample;
1292 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size == 1 )
1294 MP4_Box_data_sample_soun_t *p_soun;
1296 p_soun = p_sample->data.p_sample_soun;
1298 if( p_soun->i_qt_version == 0 )
1300 switch( p_sample->i_type )
1302 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1303 p_soun->i_qt_version = 1;
1304 p_soun->i_sample_per_packet = 64;
1305 p_soun->i_bytes_per_packet = 34;
1306 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1307 p_soun->i_bytes_per_sample = 2;
1309 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1310 p_soun->i_qt_version = 1;
1311 p_soun->i_sample_per_packet = 6;
1312 p_soun->i_bytes_per_packet = 2;
1313 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1314 p_soun->i_bytes_per_sample = 2;
1316 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1317 p_soun->i_qt_version = 1;
1318 p_soun->i_sample_per_packet = 12;
1319 p_soun->i_bytes_per_packet = 2;
1320 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1321 p_soun->i_bytes_per_sample = 2;
1323 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1324 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1325 p_soun->i_samplesize = 8;
1332 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1334 p_soun->i_qt_version = 0;
1339 /* It's a little ugly but .. there are special cases */
1340 switch( p_sample->i_type )
1342 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1343 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1344 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1347 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1348 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1350 /* Buggy files workaround */
1351 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1352 p_sample->data.p_sample_soun->i_sampleratehi) )
1354 MP4_Box_data_sample_soun_t *p_soun =
1355 p_sample->data.p_sample_soun;
1357 msg_Warn( p_demux, "i_timescale ("I64Fu") != i_sampleratehi "
1358 "(%u), making both equal (report any problem).",
1359 p_track->i_timescale, p_soun->i_sampleratehi );
1361 if( p_soun->i_sampleratehi )
1362 p_track->i_timescale = p_soun->i_sampleratehi;
1364 p_soun->i_sampleratehi = p_track->i_timescale;
1368 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1369 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1372 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1373 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1374 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1375 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1376 /* FIXME UTF-8 doesn't work here ? */
1377 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1380 case VLC_FOURCC('y','v','1','2'):
1381 p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1383 case VLC_FOURCC('y','u','v','2'):
1384 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1388 p_track->fmt.i_codec = p_sample->i_type;
1392 /* now see if esds is present and if so create a data packet
1393 with decoder_specific_info */
1394 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1395 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1396 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1397 ( p_esds->data.p_esds )&&
1400 /* First update information based on i_objectTypeIndication */
1401 switch( p_decconfig->i_objectTypeIndication )
1403 case( 0x20 ): /* MPEG4 VIDEO */
1404 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1407 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1414 case( 0x65): /* MPEG2 video */
1415 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1417 /* Theses are MPEG2-AAC */
1418 case( 0x66): /* main profile */
1419 case( 0x67): /* Low complexity profile */
1420 case( 0x68): /* Scaleable Sampling rate profile */
1421 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1423 /* true MPEG 2 audio */
1425 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1427 case( 0x6a): /* MPEG1 video */
1428 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1430 case( 0x6b): /* MPEG1 audio */
1431 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1433 case( 0x6c ): /* jpeg */
1434 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1438 case( 0xe0 ): /* NeroDigital: dvd subs */
1439 if( p_track->fmt.i_cat == SPU_ES )
1441 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1442 if( p_track->i_width > 0 )
1443 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1444 if( p_track->i_height > 0 )
1445 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1450 /* Unknown entry, but don't touch i_fourcc */
1452 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1453 p_decconfig->i_objectTypeIndication,
1454 p_track->i_track_ID );
1457 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1458 if( p_track->fmt.i_extra > 0 )
1460 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1461 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1462 p_track->fmt.i_extra );
1467 switch( p_sample->i_type )
1469 /* qt decoder, send the complete chunk */
1470 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1471 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1472 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1473 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1474 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1475 p_track->fmt.i_extra =
1476 p_sample->data.p_sample_vide->i_qt_image_description;
1477 if( p_track->fmt.i_extra > 0 )
1479 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1480 memcpy( p_track->fmt.p_extra,
1481 p_sample->data.p_sample_vide->p_qt_image_description,
1482 p_track->fmt.i_extra);
1485 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1486 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1487 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1488 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1489 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1490 p_track->fmt.i_extra =
1491 p_sample->data.p_sample_soun->i_qt_description;
1492 if( p_track->fmt.i_extra > 0 )
1494 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1495 memcpy( p_track->fmt.p_extra,
1496 p_sample->data.p_sample_soun->p_qt_description,
1497 p_track->fmt.i_extra);
1501 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1502 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1504 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1508 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1509 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1510 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1511 p_track->fmt.i_extra );
1515 msg_Err( p_demux, "missing avcC" );
1527 /* some last initialisation */
1528 switch( p_track->fmt.i_cat )
1531 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1532 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1533 p_track->fmt.video.i_bits_per_pixel =
1534 p_sample->data.p_sample_vide->i_depth;
1536 /* fall on display size */
1537 if( p_track->fmt.video.i_width <= 0 )
1538 p_track->fmt.video.i_width = p_track->i_width;
1539 if( p_track->fmt.video.i_height <= 0 )
1540 p_track->fmt.video.i_height = p_track->i_height;
1542 /* Find out apect ratio from display size */
1543 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1544 /* Work-around buggy muxed files */
1545 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1546 p_track->fmt.video.i_aspect =
1547 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1549 /* Support for cropping (eg. in H263 files) */
1550 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1551 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1554 p_track->fmt.video.i_frame_rate = p_track->i_timescale;
1555 p_track->fmt.video.i_frame_rate_base = 1;
1557 if( p_track->fmt.video.i_frame_rate &&
1558 (p_box = MP4_BoxGet( p_track->p_stbl, "stts" )) &&
1559 p_box->data.p_stts->i_entry_count >= 1 )
1561 p_track->fmt.video.i_frame_rate_base =
1562 p_box->data.p_stts->i_sample_delta[0];
1568 p_track->fmt.audio.i_channels =
1569 p_sample->data.p_sample_soun->i_channelcount;
1570 p_track->fmt.audio.i_rate =
1571 p_sample->data.p_sample_soun->i_sampleratehi;
1572 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1573 p_sample->data.p_sample_soun->i_sampleratehi *
1574 p_sample->data.p_sample_soun->i_samplesize;
1575 p_track->fmt.audio.i_bitspersample =
1576 p_sample->data.p_sample_soun->i_samplesize;
1583 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1588 /* given a time it return sample/chunk
1589 * it also update elst field of the track
1591 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1592 int64_t i_start, uint32_t *pi_chunk,
1593 uint32_t *pi_sample )
1595 demux_sys_t *p_sys = p_demux->p_sys;
1598 unsigned int i_sample;
1599 unsigned int i_chunk;
1602 /* FIXME see if it's needed to check p_track->i_chunk_count */
1603 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1605 return( VLC_EGENERIC );
1608 /* handle elst (find the correct one) */
1609 MP4_TrackSetELST( p_demux, p_track, i_start );
1610 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1612 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1613 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1615 /* now calculate i_start for this elst */
1617 i_start -= p_track->i_elst_time * I64C(1000000) / p_sys->i_timescale;
1625 /* to track time scale */
1626 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1627 /* add elst offset */
1628 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1629 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1630 elst->i_media_time[p_track->i_elst] > 0 )
1632 i_start += elst->i_media_time[p_track->i_elst];
1635 msg_Dbg( p_demux, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd
1636 "ms (track)", p_track->i_elst,
1637 i_mvt * 1000 / p_sys->i_timescale,
1638 i_start * 1000 / p_track->i_timescale );
1642 /* convert absolute time to in timescale unit */
1643 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1646 /* we start from sample 0/chunk 0, hope it won't take too much time */
1647 /* *** find good chunk *** */
1648 for( i_chunk = 0; ; i_chunk++ )
1650 if( i_chunk + 1 >= p_track->i_chunk_count )
1652 /* at the end and can't check if i_start in this chunk,
1653 it will be check while searching i_sample */
1654 i_chunk = p_track->i_chunk_count - 1;
1658 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
1659 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1665 /* *** find sample in the chunk *** */
1666 i_sample = p_track->chunk[i_chunk].i_sample_first;
1667 i_dts = p_track->chunk[i_chunk].i_first_dts;
1668 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1671 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1672 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
1675 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1676 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1678 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1683 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1687 i_sample += ( i_start - i_dts ) /
1688 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1693 if( i_sample >= p_track->i_sample_count )
1695 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1696 "(seeking too far) chunk=%d sample=%d",
1697 p_track->i_track_ID, i_chunk, i_sample );
1698 return( VLC_EGENERIC );
1702 /* *** Try to find nearest sync points *** */
1703 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1705 unsigned int i_index;
1707 "track[Id 0x%x] using Sync Sample Box (stss)",
1708 p_track->i_track_ID );
1709 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1711 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1715 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1717 p_stss->data.p_stss->i_sample_number[i_index-1] );
1718 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1719 /* new i_sample is less than old so i_chunk can only decreased */
1720 while( i_chunk > 0 &&
1721 i_sample < p_track->chunk[i_chunk].i_sample_first )
1728 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1730 p_stss->data.p_stss->i_sample_number[i_index] );
1731 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1732 /* new i_sample is more than old so i_chunk can only increased */
1733 while( i_chunk < p_track->i_chunk_count - 1 &&
1734 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1735 p_track->chunk[i_chunk].i_sample_count )
1746 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1747 "Sample Box (stss)", p_track->i_track_ID );
1750 *pi_chunk = i_chunk;
1751 *pi_sample = i_sample;
1756 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1757 unsigned int i_chunk, unsigned int i_sample )
1759 vlc_bool_t b_reselect = VLC_FALSE;
1761 /* now see if actual es is ok */
1762 if( (p_track->i_chunk >= p_track->i_chunk_count - 1) ||
1763 (p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1764 p_track->chunk[i_chunk].i_sample_description_index) )
1766 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1767 p_track->i_track_ID );
1769 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1770 p_track->p_es, &b_reselect );
1772 es_out_Del( p_demux->out, p_track->p_es );
1774 p_track->p_es = NULL;
1776 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1778 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1779 p_track->i_track_ID );
1781 p_track->b_ok = VLC_FALSE;
1782 p_track->b_selected = VLC_FALSE;
1783 return VLC_EGENERIC;
1787 /* select again the new decoder */
1790 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1793 p_track->i_chunk = i_chunk;
1794 p_track->i_sample = i_sample;
1796 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1799 /****************************************************************************
1801 ****************************************************************************
1802 * Parse track information and create all needed data to run a track
1803 * If it succeed b_ok is set to 1 else to 0
1804 ****************************************************************************/
1805 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
1806 MP4_Box_t *p_box_trak )
1808 demux_sys_t *p_sys = p_demux->p_sys;
1810 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1811 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1826 /* hint track unsupported */
1828 /* set default value (-> track unusable) */
1829 p_track->b_ok = VLC_FALSE;
1830 p_track->b_enable = VLC_FALSE;
1831 p_track->b_selected = VLC_FALSE;
1833 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1840 /* do we launch this track by default ? */
1842 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1844 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1845 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1846 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1850 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
1853 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1854 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1856 if( ( !p_mdhd )||( !p_hdlr ) )
1861 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1863 for( i = 0; i < 3; i++ )
1865 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1869 switch( p_hdlr->data.p_hdlr->i_handler_type )
1871 case( FOURCC_soun ):
1872 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1876 p_track->fmt.i_cat = AUDIO_ES;
1879 case( FOURCC_vide ):
1880 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1884 p_track->fmt.i_cat = VIDEO_ES;
1887 case( FOURCC_text ):
1888 case( FOURCC_subp ):
1889 case( FOURCC_tx3g ):
1890 p_track->fmt.i_cat = SPU_ES;
1897 p_track->i_elst = 0;
1898 p_track->i_elst_time = 0;
1899 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1901 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1904 msg_Warn( p_demux, "elst box found" );
1905 for( i = 0; i < elst->i_entry_count; i++ )
1907 msg_Dbg( p_demux, " - [%d] duration="I64Fd"ms media time="I64Fd
1908 "ms) rate=%d.%d", i,
1909 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1910 elst->i_media_time[i] >= 0 ?
1911 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
1913 elst->i_media_rate_integer[i],
1914 elst->i_media_rate_fraction[i] );
1921 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1923 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1924 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1929 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1930 p_track->b_drms = p_drms != NULL;
1931 p_track->p_drms = p_track->b_drms ?
1932 p_drms->data.p_sample_soun->p_drms : NULL;
1935 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1937 p_track->fmt.psz_language = strdup( language );
1940 p_udta = MP4_BoxGet( p_box_trak, "udta" );
1943 MP4_Box_t *p_0xa9xxx;
1944 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
1945 p_0xa9xxx = p_0xa9xxx->p_next )
1947 switch( p_0xa9xxx->i_type )
1949 case FOURCC_0xa9nam:
1950 p_track->fmt.psz_description =
1951 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
1957 /* Create chunk index table and sample index table */
1958 if( TrackCreateChunksIndex( p_demux,p_track ) ||
1959 TrackCreateSamplesIndex( p_demux, p_track ) )
1961 return; /* cannot create chunks index */
1964 p_track->i_chunk = 0;
1965 p_track->i_sample = 0;
1968 if( TrackCreateES( p_demux,
1969 p_track, p_track->i_chunk,
1972 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1973 p_track->i_track_ID );
1980 for( i = 0; i < p_track->i_chunk_count; i++ )
1982 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
1983 i, p_track->chunk[i].i_sample_count,
1984 p_track->chunk[i].i_first_dts );
1989 p_track->b_ok = VLC_TRUE;
1992 /****************************************************************************
1994 ****************************************************************************
1995 * Destroy a track created by MP4_TrackCreate.
1996 ****************************************************************************/
1997 static void MP4_TrackDestroy( demux_t *p_demux, mp4_track_t *p_track )
1999 unsigned int i_chunk;
2001 p_track->b_ok = VLC_FALSE;
2002 p_track->b_enable = VLC_FALSE;
2003 p_track->b_selected = VLC_FALSE;
2005 es_format_Clean( &p_track->fmt );
2007 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2009 if( p_track->chunk )
2011 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2012 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2014 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2015 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2018 FREENULL( p_track->chunk );
2020 if( !p_track->i_sample_size )
2022 FREENULL( p_track->p_sample_size );
2026 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2029 if( !p_track->b_ok )
2031 return VLC_EGENERIC;
2034 if( p_track->b_selected )
2036 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2037 p_track->i_track_ID );
2041 return MP4_TrackSeek( p_demux, p_track, i_start );
2044 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2046 if( !p_track->b_ok )
2051 if( !p_track->b_selected )
2053 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2054 p_track->i_track_ID );
2059 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2060 p_track->p_es, VLC_FALSE );
2063 p_track->b_selected = VLC_FALSE;
2066 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2072 if( !p_track->b_ok )
2074 return( VLC_EGENERIC );
2077 p_track->b_selected = VLC_FALSE;
2079 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2080 &i_chunk, &i_sample ) )
2082 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2083 p_track->i_track_ID );
2084 return( VLC_EGENERIC );
2087 p_track->b_selected = VLC_TRUE;
2089 if( TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) ==
2092 p_track->b_selected = VLC_TRUE;
2094 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
2095 p_track->p_es, i_start );
2098 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
2103 * 3 types: for audio
2106 #define QT_V0_MAX_SAMPLES 1024
2107 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2110 MP4_Box_data_sample_soun_t *p_soun;
2112 if( p_track->i_sample_size == 0 )
2114 /* most simple case */
2115 return p_track->p_sample_size[p_track->i_sample];
2117 if( p_track->fmt.i_cat != AUDIO_ES )
2119 return p_track->i_sample_size;
2122 p_soun = p_track->p_sample->data.p_sample_soun;
2124 if( p_soun->i_qt_version == 1 )
2126 i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
2127 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2129 else if( p_track->i_sample_size > 256 )
2131 /* We do that so we don't read too much data
2132 * (in this case we are likely dealing with compressed data) */
2133 i_size = p_track->i_sample_size;
2137 /* Read a bunch of samples at once */
2138 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2139 ( p_track->i_sample -
2140 p_track->chunk[p_track->i_chunk].i_sample_first );
2142 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2143 i_size = i_samples * p_track->i_sample_size;
2146 //fprintf( stderr, "size=%d\n", i_size );
2150 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2152 unsigned int i_sample;
2155 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2157 if( p_track->i_sample_size )
2159 MP4_Box_data_sample_soun_t *p_soun =
2160 p_track->p_sample->data.p_sample_soun;
2162 if( p_soun->i_qt_version == 0 )
2164 i_pos += ( p_track->i_sample -
2165 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2166 p_track->i_sample_size;
2170 /* we read chunk by chunk */
2176 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2177 i_sample < p_track->i_sample; i_sample++ )
2179 i_pos += p_track->p_sample_size[i_sample];
2186 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2188 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2190 MP4_Box_data_sample_soun_t *p_soun;
2192 p_soun = p_track->p_sample->data.p_sample_soun;
2194 if( p_soun->i_qt_version == 1 )
2196 /* chunk by chunk */
2198 p_track->chunk[p_track->i_chunk].i_sample_first +
2199 p_track->chunk[p_track->i_chunk].i_sample_count;
2201 else if( p_track->i_sample_size > 256 )
2203 /* We do that so we don't read too much data
2204 * (in this case we are likely dealing with compressed data) */
2205 p_track->i_sample += 1;
2210 p_track->i_sample += QT_V0_MAX_SAMPLES;
2211 if( p_track->i_sample >
2212 p_track->chunk[p_track->i_chunk].i_sample_first +
2213 p_track->chunk[p_track->i_chunk].i_sample_count )
2216 p_track->chunk[p_track->i_chunk].i_sample_first +
2217 p_track->chunk[p_track->i_chunk].i_sample_count;
2223 p_track->i_sample++;
2226 if( p_track->i_sample >= p_track->i_sample_count )
2227 return VLC_EGENERIC;
2229 /* Have we changed chunk ? */
2230 if( p_track->i_sample >=
2231 p_track->chunk[p_track->i_chunk].i_sample_first +
2232 p_track->chunk[p_track->i_chunk].i_sample_count )
2234 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2235 p_track->i_sample ) )
2237 msg_Warn( p_demux, "track[0x%x] will be disabled "
2238 "(cannot restart decoder)", p_track->i_track_ID );
2239 MP4_TrackUnselect( p_demux, p_track );
2240 return VLC_EGENERIC;
2244 /* Have we changed elst */
2245 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2247 demux_sys_t *p_sys = p_demux->p_sys;
2248 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2249 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2250 p_sys->i_timescale / (int64_t)1000000;
2252 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2253 i_mvt >= p_track->i_elst_time +
2254 elst->i_segment_duration[p_track->i_elst] )
2256 MP4_TrackSetELST( p_demux, p_track,
2257 MP4_TrackGetDTS( p_demux, p_track ) );
2264 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2267 demux_sys_t *p_sys = p_demux->p_sys;
2268 int i_elst_last = tk->i_elst;
2270 /* handle elst (find the correct one) */
2272 tk->i_elst_time = 0;
2273 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2275 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2276 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2278 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2280 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2282 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2286 tk->i_elst_time += i_dur;
2289 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2291 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2292 tk->i_elst = elst->i_entry_count - 1;
2293 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2296 if( elst->i_media_time[tk->i_elst] < 0 )
2299 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2302 if( i_elst_last != tk->i_elst )
2304 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2308 static vlc_bool_t FindItem( demux_t *p_demux, playlist_t *p_playlist,
2309 playlist_item_t **pp_item )
2311 input_thread_t *p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
2312 vlc_bool_t b_play = var_CreateGetBool( p_demux, "playlist-autostart" );
2317 if( b_play && p_playlist->status.p_item &&
2318 p_playlist->status.p_item->p_input == input_GetItem(p_input) )
2320 msg_Dbg( p_playlist, "starting playlist playback" );
2321 *pp_item = p_playlist->status.p_item;
2326 input_item_t *p_current = input_GetItem( p_input );
2328 *pp_item = playlist_ItemGetByInput( p_playlist, p_current, VLC_FALSE );
2330 msg_Dbg( p_playlist, "unable to find item in playlist");
2332 msg_Dbg( p_playlist, "not starting playlist playback");
2335 vlc_object_release( p_input );