1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_charset.h> /* EnsureUTF8 */
36 #include <vlc_meta.h> /* vlc_meta_t, vlc_meta_ */
37 #include <vlc_input.h>
41 #include "id3genres.h" /* for ATOM_gnre */
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_category( CAT_INPUT )
51 set_subcategory( SUBCAT_INPUT_DEMUX )
52 set_description( N_("MP4 stream demuxer") )
53 set_shortname( N_("MP4") )
54 set_capability( "demux", 240 )
55 set_callbacks( Open, Close )
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Demux ( demux_t * );
62 static int DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
63 static int DemuxFrg( demux_t * );
64 static int Seek ( demux_t *, mtime_t );
65 static int Control ( demux_t *, int, va_list );
69 MP4_Box_t *p_root; /* container for the whole file */
73 uint64_t i_time; /* time position of the presentation
74 * in movie timescale */
75 uint64_t i_timescale; /* movie time scale */
76 uint64_t i_duration; /* movie duration */
77 unsigned int i_tracks; /* number of tracks */
78 mp4_track_t *track; /* array of track */
79 float f_fps; /* number of frame per seconds */
81 bool b_fragmented; /* fMP4 */
84 MP4_Box_t *p_tref_chap;
87 input_title_t *p_title;
90 /*****************************************************************************
91 * Declaration of local function
92 *****************************************************************************/
93 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *, bool b_force_enable );
94 static int MP4_frg_TrackCreate( demux_t *, mp4_track_t *, MP4_Box_t *);
95 static void MP4_TrackDestroy( mp4_track_t * );
97 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
98 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
100 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
102 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
103 static int MP4_TrackSampleSize( mp4_track_t * );
104 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
105 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
107 static void MP4_UpdateSeekpoint( demux_t * );
108 static const char *MP4_ConvertMacCode( uint16_t );
110 /* Return time in microsecond of a track */
111 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
113 demux_sys_t *p_sys = p_demux->p_sys;
115 if( p_sys->b_fragmented )
116 chunk = *p_track->cchunk;
118 chunk = p_track->chunk[p_track->i_chunk];
120 unsigned int i_index = 0;
121 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
122 int64_t i_dts = chunk.i_first_dts;
124 while( i_sample > 0 )
126 if( i_sample > chunk.p_sample_count_dts[i_index] )
128 i_dts += chunk.p_sample_count_dts[i_index] *
129 chunk.p_sample_delta_dts[i_index];
130 i_sample -= chunk.p_sample_count_dts[i_index];
135 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
140 /* now handle elst */
141 if( p_track->p_elst )
143 demux_sys_t *p_sys = p_demux->p_sys;
144 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
146 /* convert to offset */
147 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
148 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
149 elst->i_media_time[p_track->i_elst] > 0 )
151 i_dts -= elst->i_media_time[p_track->i_elst];
154 /* add i_elst_time */
155 i_dts += p_track->i_elst_time * p_track->i_timescale /
158 if( i_dts < 0 ) i_dts = 0;
161 return INT64_C(1000000) * i_dts / p_track->i_timescale;
164 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
166 demux_sys_t *p_sys = p_demux->p_sys;
168 if( p_sys->b_fragmented )
169 ck = p_track->cchunk;
171 ck = &p_track->chunk[p_track->i_chunk];
173 unsigned int i_index = 0;
174 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
176 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
179 for( i_index = 0;; i_index++ )
181 if( i_sample < ck->p_sample_count_pts[i_index] )
182 return ck->p_sample_offset_pts[i_index] * INT64_C(1000000) /
183 (int64_t)p_track->i_timescale;
185 i_sample -= ck->p_sample_count_pts[i_index];
189 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
191 return INT64_C(1000000) * p_sys->i_time / p_sys->i_timescale;
194 static void LoadChapter( demux_t *p_demux );
196 static int LoadInitFrag( demux_t *p_demux, const bool b_smooth )
198 demux_sys_t *p_sys = p_demux->p_sys;
200 if( b_smooth ) /* Smooth Streaming */
202 if( ( p_sys->p_root = MP4_BoxGetSmooBox( p_demux->s ) ) == NULL )
204 goto LoadInitFragError;
208 MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
209 if( !p_smoo || CmpUUID( &p_smoo->i_uuid, &SmooBoxUUID ) )
210 goto LoadInitFragError;
211 /* Get number of tracks */
213 for( int i = 0; i < 3; i++ )
215 MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
216 if( p_stra && p_stra->data.p_stra->i_track_ID )
218 /* Get timescale and duration of the video track; */
219 if( p_sys->i_timescale == 0 )
221 p_sys->i_timescale = p_stra->data.p_stra->i_timescale;
222 p_sys->i_duration = p_stra->data.p_stra->i_duration;
223 if( p_sys->i_timescale == 0 )
225 msg_Err( p_demux, "bad timescale" );
226 goto LoadInitFragError;
234 /* Load all boxes ( except raw data ) */
235 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
237 goto LoadInitFragError;
243 msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" );
247 static int InitTracks( demux_t *p_demux )
249 demux_sys_t *p_sys = p_demux->p_sys;
251 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
252 if( p_sys->track == NULL )
255 if( p_sys->b_fragmented )
257 mp4_track_t *p_track;
258 for( uint16_t i = 0; i < p_sys->i_tracks; i++ )
260 p_track = &p_sys->track[i];
261 p_track->cchunk = calloc( 1, sizeof( mp4_chunk_t ) );
262 if( unlikely( !p_track->cchunk ) )
264 free( p_sys->track );
272 static void CreateTracksFromSmooBox( demux_t *p_demux )
274 demux_sys_t *p_sys = p_demux->p_sys;
276 MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
277 mp4_track_t *p_track;
279 for( int i = 0; i < 3; i++ )
281 MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
282 if( !p_stra || p_stra->data.p_stra->i_track_ID == 0 )
286 p_track = &p_sys->track[j]; j++;
287 MP4_frg_TrackCreate( p_demux, p_track, p_stra );
288 p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
293 /*****************************************************************************
294 * Open: check file and initializes MP4 structures
295 *****************************************************************************/
296 static int Open( vlc_object_t * p_this )
298 demux_t *p_demux = (demux_t *)p_this;
301 const uint8_t *p_peek;
312 /* A little test to see if it could be a mp4 */
313 if( stream_Peek( p_demux->s, &p_peek, 11 ) < 11 ) return VLC_EGENERIC;
315 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
326 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
329 /* We don't yet support f4v, but avformat does. */
330 if( p_peek[8] == 'f' && p_peek[9] == '4' && p_peek[10] == 'v' )
338 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
341 msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" );
345 /*Set exported functions */
346 p_demux->pf_demux = Demux;
347 p_demux->pf_control = Control;
349 /* create our structure that will contains all data */
350 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
352 /* Is it Smooth Streaming? */
353 bool b_smooth = false;
354 if( stream_Peek( p_demux->s, &p_peek, 24 ) < 24 ) return VLC_EGENERIC;
355 if( !CmpUUID( (UUID_t *)(p_peek + 8), &SmooBoxUUID ) )
358 p_sys->b_fragmented = true;
361 if( LoadInitFrag( p_demux, b_smooth ) != VLC_SUCCESS )
364 if( MP4_BoxCount( p_sys->p_root, "/moov/mvex" ) > 0 )
366 p_sys->b_fragmented = true;
368 if( p_sys->b_fragmented )
370 p_demux->pf_demux = DemuxFrg;
373 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
376 if( InitTracks( p_demux ) != VLC_SUCCESS )
378 CreateTracksFromSmooBox( p_demux );
381 else if( p_sys->b_fragmented && b_seekable )
383 /* We are not yet able to demux a fragmented MP4 file, using the 'mfra'
384 * box. So if STREAM_CAN_FASTSEEK is true, we're assuming we've got such
385 * a file, and we let avformat do the job. */
386 msg_Warn( p_demux, "MP4 plugin discarded "\
387 "(fast-seekable and fragmented, let avformat demux it)" );
388 stream_Seek( p_demux->s, 0 ); /* rewind, for other demux */
391 else if( !p_sys->b_fragmented && !b_seekable )
393 msg_Warn( p_demux, "MP4 plugin discarded (not fast-seekable)" );
394 stream_Seek( p_demux->s, 0 ); /* rewind, for other demux */
398 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
400 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
402 switch( p_ftyp->data.p_ftyp->i_major_brand )
406 "ISO Media file (isom) version %d.",
407 p_ftyp->data.p_ftyp->i_minor_version );
410 case( VLC_FOURCC( '3', 'g', 'p', '5' ) ):
411 case( VLC_FOURCC( '3', 'g', 'p', '6' ) ):
412 case( VLC_FOURCC( '3', 'g', 'p', '7' ) ):
413 msg_Dbg( p_demux, "3GPP Media file Release: %c",
414 #ifdef WORDS_BIGENDIAN
415 p_ftyp->data.p_ftyp->i_major_brand
417 p_ftyp->data.p_ftyp->i_major_brand >> 24
421 case( VLC_FOURCC( 'q', 't', ' ', ' ') ):
422 msg_Dbg( p_demux, "Apple QuickTime file" );
424 case( VLC_FOURCC( 'i', 's', 'm', 'l') ):
425 msg_Dbg( p_demux, "PIFF (= isml = fMP4) file" );
429 "unrecognized major file specification (%4.4s).",
430 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
436 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
439 /* the file need to have one moov box */
440 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
442 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
446 msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
449 /* we have a free box as a moov, rename it */
450 p_foov->i_type = ATOM_moov;
453 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
455 int i_count = MP4_BoxCount( p_rmra, "rmda" );
458 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
460 input_thread_t *p_input = demux_GetParentInput( p_demux );
461 input_item_t *p_current = input_GetItem( p_input );
463 input_item_node_t *p_subitems = input_item_node_Create( p_current );
465 for( i = 0; i < i_count; i++ )
467 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
471 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
475 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
477 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
478 psz_ref, (char*)&i_ref_type );
480 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
482 if( strstr( psz_ref, "qt5gateQT" ) )
484 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
487 if( !strncmp( psz_ref, "http://", 7 ) ||
488 !strncmp( psz_ref, "rtsp://", 7 ) )
495 char *psz_path = strdup( p_demux->psz_location );
496 char *end = strrchr( psz_path, '/' );
497 if( end ) end[1] = '\0';
498 else *psz_path = '\0';
500 if( asprintf( &psz_absolute, "%s://%s%s",
501 p_demux->psz_access, psz_path, psz_ref ) < 0 )
505 vlc_object_release( p_input) ;
510 psz_ref = psz_absolute;
513 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
514 input_item_t *p_item = input_item_New( psz_ref, NULL );
515 input_item_CopyOptions( p_current, p_item );
516 input_item_node_AppendItem( p_subitems, p_item );
517 vlc_gc_decref( p_item );
521 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
522 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
526 input_item_node_PostAndDelete( p_subitems );
527 vlc_object_release( p_input );
530 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
534 msg_Err( p_demux, "cannot find /moov/mvhd" );
539 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
540 p_demux->pf_demux = DemuxRef;
546 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
547 if( p_sys->i_timescale == 0 )
549 msg_Err( p_this, "bad timescale" );
552 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
555 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
557 msg_Err( p_demux, "cannot find any /moov/trak" );
560 msg_Dbg( p_demux, "found %d track%c",
562 p_sys->i_tracks ? 's':' ' );
564 if( InitTracks( p_demux ) != VLC_SUCCESS )
567 /* Search the first chap reference (like quicktime) and
568 * check that at least 1 stream is enabled */
569 p_sys->p_tref_chap = NULL;
570 b_enabled_es = false;
571 for( i = 0; i < p_sys->i_tracks; i++ )
573 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
576 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
577 if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
580 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
581 if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
582 p_sys->p_tref_chap = p_chap;
585 /* now process each track and extract all useful information */
586 for( i = 0; i < p_sys->i_tracks; i++ )
588 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
589 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
591 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
594 switch( p_sys->track[i].fmt.i_cat )
603 psz_cat = "subtitle";
611 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
612 p_sys->track[i].i_track_ID, psz_cat,
613 p_sys->track[i].b_enable ? "enable":"disable",
614 p_sys->track[i].fmt.psz_language ?
615 p_sys->track[i].fmt.psz_language : "undef" );
617 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
619 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
620 p_sys->track[i].i_track_ID,
621 p_sys->track[i].fmt.psz_language ?
622 p_sys->track[i].fmt.psz_language : "undef" );
626 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
627 p_sys->track[i].i_track_ID );
632 LoadChapter( p_demux );
639 MP4_BoxFree( p_demux->s, p_sys->p_root );
645 /*****************************************************************************
646 * Demux: read packet and send them to decoders
647 *****************************************************************************
648 * TODO check for newly selected track (ie audio upt to now )
649 *****************************************************************************/
650 static int Demux( demux_t *p_demux )
652 demux_sys_t *p_sys = p_demux->p_sys;
653 unsigned int i_track;
656 unsigned int i_track_selected;
658 /* check for newly selected/unselected track */
659 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
662 mp4_track_t *tk = &p_sys->track[i_track];
665 if( !tk->b_ok || tk->b_chapter ||
666 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
671 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
673 if( tk->b_selected && !b )
675 MP4_TrackUnselect( p_demux, tk );
677 else if( !tk->b_selected && b)
679 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
688 if( i_track_selected <= 0 )
690 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
691 if( p_sys->i_timescale > 0 )
693 int64_t i_length = (mtime_t)1000000 *
694 (mtime_t)p_sys->i_duration /
695 (mtime_t)p_sys->i_timescale;
696 if( MP4_GetMoviePTS( p_sys ) >= i_length )
701 msg_Warn( p_demux, "no track selected, exiting..." );
706 MP4_UpdateSeekpoint( p_demux );
708 /* first wait for the good time to read a packet */
709 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
711 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
713 /* we will read 100ms for each stream so ...*/
714 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
716 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
718 mp4_track_t *tk = &p_sys->track[i_track];
720 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
723 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
726 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
727 MP4_TrackGetDTS( p_demux, tk ),
728 MP4_GetMoviePTS( p_sys ) );
731 if( MP4_TrackSampleSize( tk ) > 0 )
737 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
739 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
741 MP4_TrackUnselect( p_demux, tk );
747 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
749 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
751 MP4_TrackUnselect( p_demux, tk );
755 else if( tk->fmt.i_cat == SPU_ES )
757 if( tk->fmt.i_codec == VLC_CODEC_SUBT &&
758 p_block->i_buffer >= 2 )
760 size_t i_size = GetWBE( p_block->p_buffer );
762 if( i_size + 2 <= p_block->i_buffer )
765 /* remove the length field, and append a '\0' */
766 memmove( &p_block->p_buffer[0],
767 &p_block->p_buffer[2], i_size );
768 p_block->p_buffer[i_size] = '\0';
769 p_block->i_buffer = i_size + 1;
771 /* convert \r -> \n */
772 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
780 p_block->i_buffer = 0;
785 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
787 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
789 p_block->i_pts = p_block->i_dts + i_delta;
790 else if( tk->fmt.i_cat != VIDEO_ES )
791 p_block->i_pts = p_block->i_dts;
793 p_block->i_pts = VLC_TS_INVALID;
795 es_out_Send( p_demux->out, tk->p_es, p_block );
799 if( MP4_TrackNextSample( p_demux, tk ) )
807 static void MP4_UpdateSeekpoint( demux_t *p_demux )
809 demux_sys_t *p_sys = p_demux->p_sys;
812 if( !p_sys->p_title )
814 i_time = MP4_GetMoviePTS( p_sys );
815 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
817 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
822 if( i != p_demux->info.i_seekpoint && i >= 0 )
824 p_demux->info.i_seekpoint = i;
825 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
828 /*****************************************************************************
830 ******************************************************************************/
831 static int Seek( demux_t *p_demux, mtime_t i_date )
833 demux_sys_t *p_sys = p_demux->p_sys;
834 unsigned int i_track;
836 /* First update global time */
837 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
838 p_sys->i_pcr = i_date;
840 /* Now for each stream try to go to this time */
841 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
843 mp4_track_t *tk = &p_sys->track[i_track];
844 MP4_TrackSeek( p_demux, tk, i_date );
846 MP4_UpdateSeekpoint( p_demux );
848 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
853 static int MP4_frg_Seek( demux_t *p_demux, double f )
855 demux_sys_t *p_sys = p_demux->p_sys;
857 int64_t i64 = stream_Size( p_demux->s );
858 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
864 /* update global time */
865 p_sys->i_time = (uint64_t)(f * (double)p_sys->i_duration);
866 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
868 for( unsigned i_track = 0; i_track < p_sys->i_tracks; i_track++ )
870 mp4_track_t *tk = &p_sys->track[i_track];
872 /* We don't want the current chunk to be flushed */
873 tk->cchunk->i_sample = tk->cchunk->i_sample_count;
875 /* reset/update some values */
876 tk->i_sample = tk->i_sample_first = 0;
877 tk->i_first_dts = p_sys->i_time;
879 /* We want to discard the current chunk and get the next one at once */
880 tk->b_has_non_empty_cchunk = false;
882 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->i_pcr );
887 /*****************************************************************************
889 *****************************************************************************/
890 static int Control( demux_t *p_demux, int i_query, va_list args )
892 demux_sys_t *p_sys = p_demux->p_sys;
899 case DEMUX_GET_POSITION:
900 pf = (double*)va_arg( args, double * );
901 if( p_sys->i_duration > 0 )
903 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
911 case DEMUX_SET_POSITION:
912 f = (double)va_arg( args, double );
913 if( p_sys->b_fragmented )
915 return MP4_frg_Seek( p_demux, f );
917 else if( p_sys->i_timescale > 0 )
919 i64 = (int64_t)( f * (double)1000000 *
920 (double)p_sys->i_duration /
921 (double)p_sys->i_timescale );
922 return Seek( p_demux, i64 );
924 else return VLC_SUCCESS;
927 pi64 = (int64_t*)va_arg( args, int64_t * );
928 if( p_sys->i_timescale > 0 )
930 *pi64 = (mtime_t)1000000 *
931 (mtime_t)p_sys->i_time /
932 (mtime_t)p_sys->i_timescale;
938 i64 = (int64_t)va_arg( args, int64_t );
939 return Seek( p_demux, i64 );
941 case DEMUX_GET_LENGTH:
942 pi64 = (int64_t*)va_arg( args, int64_t * );
943 if( p_sys->i_timescale > 0 )
945 *pi64 = (mtime_t)1000000 *
946 (mtime_t)p_sys->i_duration /
947 (mtime_t)p_sys->i_timescale;
953 pf = (double*)va_arg( args, double* );
959 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
960 MP4_Box_t *p_0xa9xxx;
962 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
965 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
972 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
973 p_0xa9xxx = p_0xa9xxx->p_next )
976 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
979 /* FIXME FIXME: should convert from whatever the character
980 * encoding of MP4 meta data is to UTF-8. */
981 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
982 if( psz_utf ) { EnsureUTF8( psz_utf ); \
983 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
985 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
986 switch( p_0xa9xxx->i_type )
988 case ATOM_0xa9nam: /* Full name */
989 SET( vlc_meta_SetTitle );
992 SET( vlc_meta_SetArtist );
995 SET( vlc_meta_SetArtist );
998 SET( vlc_meta_SetCopyright );
1000 case ATOM_0xa9day: /* Creation Date */
1001 SET( vlc_meta_SetDate );
1003 case ATOM_0xa9des: /* Description */
1004 SET( vlc_meta_SetDescription );
1006 case ATOM_0xa9gen: /* Genre */
1007 SET( vlc_meta_SetGenre );
1011 if( p_0xa9xxx->data.p_gnre->i_genre <= NUM_GENRES )
1012 vlc_meta_SetGenre( p_meta, ppsz_genres[p_0xa9xxx->data.p_gnre->i_genre - 1] );
1015 case ATOM_0xa9alb: /* Album */
1016 SET( vlc_meta_SetAlbum );
1019 case ATOM_0xa9trk: /* Track */
1020 SET( vlc_meta_SetTrackNum );
1025 snprintf( psz_trck, sizeof( psz_trck ), "%i",
1026 p_0xa9xxx->data.p_trkn->i_track_number );
1027 vlc_meta_SetTrackNum( p_meta, psz_trck );
1028 if( p_0xa9xxx->data.p_trkn->i_track_total > 0 )
1030 snprintf( psz_trck, sizeof( psz_trck ), "%i",
1031 p_0xa9xxx->data.p_trkn->i_track_total );
1032 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, psz_trck );
1036 case ATOM_0xa9cmt: /* Commment */
1037 SET( vlc_meta_SetDescription );
1040 case ATOM_0xa9url: /* URL */
1041 SET( vlc_meta_SetURL );
1044 case ATOM_0xa9too: /* Encoder Tool */
1045 case ATOM_0xa9enc: /* Encoded By */
1046 SET( vlc_meta_SetEncodedBy );
1050 SET( vlc_meta_SetPublisher );
1057 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
1059 { ATOM_0xa9wrt, N_("Writer") },
1060 { ATOM_0xa9com, N_("Composer") },
1061 { ATOM_0xa9prd, N_("Producer") },
1062 { ATOM_0xa9inf, N_("Information") },
1063 { ATOM_0xa9dir, N_("Director") },
1064 { ATOM_0xa9dis, N_("Disclaimer") },
1065 { ATOM_0xa9req, N_("Requirements") },
1066 { ATOM_0xa9fmt, N_("Original Format") },
1067 { ATOM_0xa9dsa, N_("Display Source As") },
1068 { ATOM_0xa9hst, N_("Host Computer") },
1069 { ATOM_0xa9prf, N_("Performers") },
1070 { ATOM_0xa9ope, N_("Original Performer") },
1071 { ATOM_0xa9src, N_("Providers Source Content") },
1072 { ATOM_0xa9wrn, N_("Warning") },
1073 { ATOM_0xa9swr, N_("Software") },
1074 { ATOM_0xa9lyr, N_("Lyrics") },
1075 { ATOM_0xa9mak, N_("Record Company") },
1076 { ATOM_0xa9mod, N_("Model") },
1077 { ATOM_0xa9PRD, N_("Product") },
1078 { ATOM_0xa9grp, N_("Grouping") },
1079 { ATOM_0xa9gen, N_("Genre") },
1080 { ATOM_0xa9st3, N_("Sub-Title") },
1081 { ATOM_0xa9arg, N_("Arranger") },
1082 { ATOM_0xa9ard, N_("Art Director") },
1083 { ATOM_0xa9cak, N_("Copyright Acknowledgement") },
1084 { ATOM_0xa9con, N_("Conductor") },
1085 { ATOM_0xa9des, N_("Song Description") },
1086 { ATOM_0xa9lnt, N_("Liner Notes") },
1087 { ATOM_0xa9phg, N_("Phonogram Rights") },
1088 { ATOM_0xa9pub, N_("Publisher") },
1089 { ATOM_0xa9sne, N_("Sound Engineer") },
1090 { ATOM_0xa9sol, N_("Soloist") },
1091 { ATOM_0xa9thx, N_("Thanks") },
1092 { ATOM_0xa9xpd, N_("Executive Producer") },
1095 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
1097 if( p_0xa9xxx->i_type == xa9typetoextrameta[i].xa9_type )
1099 char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" );
1102 EnsureUTF8( psz_utf );
1103 vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
1113 case DEMUX_GET_TITLE_INFO:
1115 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1116 int *pi_int = (int*)va_arg( args, int* );
1117 int *pi_title_offset = (int*)va_arg( args, int* );
1118 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
1120 if( !p_sys->p_title )
1121 return VLC_EGENERIC;
1124 *ppp_title = malloc( sizeof( input_title_t*) );
1125 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
1126 *pi_title_offset = 0;
1127 *pi_seekpoint_offset = 0;
1130 case DEMUX_SET_TITLE:
1132 const int i_title = (int)va_arg( args, int );
1133 if( !p_sys->p_title || i_title != 0 )
1134 return VLC_EGENERIC;
1137 case DEMUX_SET_SEEKPOINT:
1139 const int i_seekpoint = (int)va_arg( args, int );
1140 if( !p_sys->p_title )
1141 return VLC_EGENERIC;
1142 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
1145 case DEMUX_SET_NEXT_DEMUX_TIME:
1146 case DEMUX_SET_GROUP:
1147 case DEMUX_HAS_UNSUPPORTED_META:
1148 case DEMUX_GET_ATTACHMENTS:
1149 case DEMUX_GET_PTS_DELAY:
1150 case DEMUX_CAN_RECORD:
1151 return VLC_EGENERIC;
1154 msg_Warn( p_demux, "control query %u unimplemented", i_query );
1155 return VLC_EGENERIC;
1159 /*****************************************************************************
1160 * Close: frees unused data
1161 *****************************************************************************/
1162 static void Close ( vlc_object_t * p_this )
1164 demux_t * p_demux = (demux_t *)p_this;
1165 demux_sys_t *p_sys = p_demux->p_sys;
1166 unsigned int i_track;
1168 msg_Dbg( p_demux, "freeing all memory" );
1170 MP4_BoxFree( p_demux->s, p_sys->p_root );
1171 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1173 MP4_TrackDestroy( &p_sys->track[i_track] );
1175 FREENULL( p_sys->track );
1177 if( p_sys->p_title )
1178 vlc_input_title_Delete( p_sys->p_title );
1185 /****************************************************************************
1186 * Local functions, specific to vlc
1187 ****************************************************************************/
1189 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1191 demux_sys_t *p_sys = p_demux->p_sys;
1194 p_sys->p_title = vlc_input_title_New();
1195 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1197 seekpoint_t *s = vlc_seekpoint_New();
1199 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1200 EnsureUTF8( s->psz_name );
1201 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1202 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1205 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1207 demux_sys_t *p_sys = p_demux->p_sys;
1209 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1211 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1212 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( p_demux, tk );
1213 const unsigned int i_size = MP4_TrackSampleSize( tk );
1215 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1218 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1219 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1223 seekpoint_t *s = vlc_seekpoint_New();
1225 s->psz_name = strndup( &p_buffer[2], i_len );
1226 EnsureUTF8( s->psz_name );
1228 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1230 if( !p_sys->p_title )
1231 p_sys->p_title = vlc_input_title_New();
1232 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1235 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1236 tk->chunk[tk->i_chunk].i_sample_count )
1240 static void LoadChapter( demux_t *p_demux )
1242 demux_sys_t *p_sys = p_demux->p_sys;
1245 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1247 LoadChapterGpac( p_demux, p_chpl );
1249 else if( p_sys->p_tref_chap )
1251 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1254 /* Load the first subtitle track like quicktime */
1255 for( i = 0; i < p_chap->i_entry_count; i++ )
1257 for( j = 0; j < p_sys->i_tracks; j++ )
1259 mp4_track_t *tk = &p_sys->track[j];
1260 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1261 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_SUBT )
1264 if( j < p_sys->i_tracks )
1266 LoadChapterApple( p_demux, &p_sys->track[j] );
1272 /* Add duration if titles are enabled */
1273 if( p_sys->p_title )
1275 p_sys->p_title->i_length = (uint64_t)1000000 *
1276 (uint64_t)p_sys->i_duration / (uint64_t)p_sys->i_timescale;
1280 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1281 static int TrackCreateChunksIndex( demux_t *p_demux,
1282 mp4_track_t *p_demux_track )
1284 demux_sys_t *p_sys = p_demux->p_sys;
1285 if( p_sys->b_fragmented )
1288 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1291 unsigned int i_chunk;
1292 unsigned int i_index, i_last;
1294 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1295 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1296 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1298 return( VLC_EGENERIC );
1301 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1302 if( !p_demux_track->i_chunk_count )
1304 msg_Warn( p_demux, "no chunk defined" );
1305 return( VLC_EGENERIC );
1307 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1308 sizeof( mp4_chunk_t ) );
1309 if( p_demux_track->chunk == NULL )
1314 /* first we read chunk offset */
1315 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1317 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1319 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1321 ck->i_first_dts = 0;
1322 ck->p_sample_count_dts = NULL;
1323 ck->p_sample_delta_dts = NULL;
1324 ck->p_sample_count_pts = NULL;
1325 ck->p_sample_offset_pts = NULL;
1328 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1329 to be used for the sample XXX begin to 1
1330 We construct it begining at the end */
1331 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1332 i_index = p_stsc->data.p_stsc->i_entry_count;
1335 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1336 return( VLC_EGENERIC );
1341 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1342 i_chunk < i_last; i_chunk++ )
1344 if( i_chunk >= p_demux_track->i_chunk_count )
1346 msg_Warn( p_demux, "corrupted chunk table" );
1347 return VLC_EGENERIC;
1350 p_demux_track->chunk[i_chunk].i_sample_description_index =
1351 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1352 p_demux_track->chunk[i_chunk].i_sample_count =
1353 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1355 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1358 p_demux_track->chunk[0].i_sample_first = 0;
1359 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1361 p_demux_track->chunk[i_chunk].i_sample_first =
1362 p_demux_track->chunk[i_chunk-1].i_sample_first +
1363 p_demux_track->chunk[i_chunk-1].i_sample_count;
1366 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1367 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1372 static int TrackCreateSamplesIndex( demux_t *p_demux,
1373 mp4_track_t *p_demux_track )
1375 demux_sys_t *p_sys = p_demux->p_sys;
1376 if( p_sys->b_fragmented )
1380 MP4_Box_data_stsz_t *stsz;
1381 MP4_Box_data_stts_t *stts;
1382 /* TODO use also stss and stsh table for seeking */
1383 /* FIXME use edit table */
1388 int64_t i_index_sample_used;
1393 * Gives the sample size for each samples. There is also a stz2 table
1394 * (compressed form) that we need to implement TODO */
1395 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1398 /* FIXME and stz2 */
1399 msg_Warn( p_demux, "cannot find STSZ box" );
1400 return VLC_EGENERIC;
1402 stsz = p_box->data.p_stsz;
1405 * Gives mapping between sample and decoding time
1407 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1410 msg_Warn( p_demux, "cannot find STTS box" );
1411 return VLC_EGENERIC;
1413 stts = p_box->data.p_stts;
1415 /* Use stsz table to create a sample number -> sample size table */
1416 p_demux_track->i_sample_count = stsz->i_sample_count;
1417 if( stsz->i_sample_size )
1419 /* 1: all sample have the same size, so no need to construct a table */
1420 p_demux_track->i_sample_size = stsz->i_sample_size;
1421 p_demux_track->p_sample_size = NULL;
1425 /* 2: each sample can have a different size */
1426 p_demux_track->i_sample_size = 0;
1427 p_demux_track->p_sample_size =
1428 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1429 if( p_demux_track->p_sample_size == NULL )
1432 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1434 p_demux_track->p_sample_size[i_sample] =
1435 stsz->i_entry_size[i_sample];
1439 /* Use stts table to create a sample number -> dts table.
1440 * XXX: if we don't want to waste too much memory, we can't expand
1441 * the box! so each chunk will contain an "extract" of this table
1442 * for fast research (problem with raw stream where a sample is sometime
1443 * just channels*bits_per_sample/8 */
1446 i_index = 0; i_index_sample_used = 0;
1447 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1449 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1450 int64_t i_entry, i_sample_count, i;
1452 /* save first dts */
1453 ck->i_first_dts = i_next_dts;
1454 ck->i_last_dts = i_next_dts;
1456 /* count how many entries are needed for this chunk
1457 * for p_sample_delta_dts and p_sample_count_dts */
1458 i_sample_count = ck->i_sample_count;
1461 while( i_sample_count > 0 )
1463 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1464 /* don't count already used sample in this entry */
1466 i_sample_count += i_index_sample_used;
1472 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1473 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1475 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1479 i_sample_count = ck->i_sample_count;
1480 for( i = 0; i < i_entry; i++ )
1485 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1487 i_used = __MIN( i_rest, i_sample_count );
1489 i_index_sample_used += i_used;
1490 i_sample_count -= i_used;
1491 i_next_dts += i_used * stts->i_sample_delta[i_index];
1493 ck->p_sample_count_dts[i] = i_used;
1494 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1496 ck->i_last_dts = i_next_dts - ck->p_sample_delta_dts[i];
1498 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1501 i_index_sample_used = 0;
1507 * Gives the delta between decoding time (dts) and composition table (pts)
1509 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1512 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1514 msg_Warn( p_demux, "CTTS table" );
1516 /* Create pts-dts table per chunk */
1517 i_index = 0; i_index_sample_used = 0;
1518 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1520 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1521 int64_t i_entry, i_sample_count, i;
1523 /* count how many entries are needed for this chunk
1524 * for p_sample_delta_dts and p_sample_count_dts */
1525 i_sample_count = ck->i_sample_count;
1528 while( i_sample_count > 0 )
1530 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1532 /* don't count already used sample in this entry */
1534 i_sample_count += i_index_sample_used;
1540 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1541 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1542 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1546 i_sample_count = ck->i_sample_count;
1547 for( i = 0; i < i_entry; i++ )
1552 i_rest = ctts->i_sample_count[i_index] -
1553 i_index_sample_used;
1555 i_used = __MIN( i_rest, i_sample_count );
1557 i_index_sample_used += i_used;
1558 i_sample_count -= i_used;
1560 ck->p_sample_count_pts[i] = i_used;
1561 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1563 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1566 i_index_sample_used = 0;
1572 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1573 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1574 i_next_dts / p_demux_track->i_timescale );
1580 * It computes the sample rate for a video track using the given sample
1583 static void TrackGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
1584 const mp4_track_t *p_track,
1585 unsigned i_sd_index,
1591 if( p_track->i_chunk_count <= 0 )
1595 const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
1596 while( p_chunk > &p_track->chunk[0] &&
1597 p_chunk[-1].i_sample_description_index == i_sd_index )
1602 uint64_t i_sample = 0;
1603 uint64_t i_first_dts = p_chunk->i_first_dts;
1604 uint64_t i_last_dts;
1607 i_sample += p_chunk->i_sample_count;
1608 i_last_dts = p_chunk->i_last_dts;
1611 while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
1612 p_chunk->i_sample_description_index == i_sd_index );
1614 if( i_sample > 1 && i_first_dts < i_last_dts )
1615 vlc_ureduce( pi_num, pi_den,
1616 ( i_sample - 1) * p_track->i_timescale,
1617 i_last_dts - i_first_dts,
1623 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1625 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1626 unsigned int i_chunk, es_out_id_t **pp_es )
1628 demux_sys_t *p_sys = p_demux->p_sys;
1629 unsigned int i_sample_description_index;
1631 if( p_sys->b_fragmented )
1632 i_sample_description_index = 1; /* XXX */
1634 i_sample_description_index =
1635 p_track->chunk[i_chunk].i_sample_description_index;
1637 MP4_Box_t *p_sample;
1646 if( !i_sample_description_index )
1648 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1649 p_track->i_track_ID );
1650 return VLC_EGENERIC;
1653 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1654 i_sample_description_index - 1 );
1657 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1659 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1660 p_track->i_track_ID );
1661 return VLC_EGENERIC;
1664 p_track->p_sample = p_sample;
1666 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
1668 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
1670 p_sample->i_type = p_frma->data.p_frma->i_type;
1673 p_enda = MP4_BoxGet( p_sample, "wave/enda" );
1675 p_enda = MP4_BoxGet( p_sample, "enda" );
1677 p_pasp = MP4_BoxGet( p_sample, "pasp" );
1680 switch( p_track->fmt.i_cat )
1683 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1684 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1685 p_track->fmt.video.i_bits_per_pixel =
1686 p_sample->data.p_sample_vide->i_depth;
1688 /* fall on display size */
1689 if( p_track->fmt.video.i_width <= 0 )
1690 p_track->fmt.video.i_width = p_track->i_width;
1691 if( p_track->fmt.video.i_height <= 0 )
1692 p_track->fmt.video.i_height = p_track->i_height;
1694 /* Find out apect ratio from display size */
1695 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1696 /* Work-around buggy muxed files */
1697 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1699 p_track->fmt.video.i_sar_num = p_track->i_width * p_track->fmt.video.i_height;
1700 p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
1702 if( p_pasp && p_pasp->data.p_pasp->i_horizontal_spacing > 0 &&
1703 p_pasp->data.p_pasp->i_vertical_spacing > 0 )
1705 p_track->fmt.video.i_sar_num = p_pasp->data.p_pasp->i_horizontal_spacing;
1706 p_track->fmt.video.i_sar_den = p_pasp->data.p_pasp->i_vertical_spacing;
1709 /* Support for cropping (eg. in H263 files) */
1710 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1711 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1714 TrackGetESSampleRate( &p_track->fmt.video.i_frame_rate,
1715 &p_track->fmt.video.i_frame_rate_base,
1716 p_track, i_sample_description_index, i_chunk );
1717 p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
1718 (float)p_track->fmt.video.i_frame_rate_base;
1721 switch( (int)p_track->f_rotation ) {
1723 p_track->fmt.video.orientation = ORIENT_ROTATED_90;
1726 p_track->fmt.video.orientation = ORIENT_ROTATED_180;
1729 p_track->fmt.video.orientation = ORIENT_ROTATED_270;
1736 p_track->fmt.audio.i_channels =
1737 p_sample->data.p_sample_soun->i_channelcount;
1738 p_track->fmt.audio.i_rate =
1739 p_sample->data.p_sample_soun->i_sampleratehi;
1740 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1741 p_sample->data.p_sample_soun->i_sampleratehi *
1742 p_sample->data.p_sample_soun->i_samplesize;
1743 p_track->fmt.audio.i_bitspersample =
1744 p_sample->data.p_sample_soun->i_samplesize;
1746 if( ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1748 MP4_Box_data_sample_soun_t *p_soun;
1749 p_soun = p_sample->data.p_sample_soun;
1751 if( p_soun->i_qt_version == 0 )
1753 switch( p_sample->i_type )
1755 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1756 p_soun->i_qt_version = 1;
1757 p_soun->i_sample_per_packet = 64;
1758 p_soun->i_bytes_per_packet = 34;
1759 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1760 p_soun->i_bytes_per_sample = 2;
1762 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1763 p_soun->i_qt_version = 1;
1764 p_soun->i_sample_per_packet = 6;
1765 p_soun->i_bytes_per_packet = 2;
1766 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1767 p_soun->i_bytes_per_sample = 2;
1769 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1770 p_soun->i_qt_version = 1;
1771 p_soun->i_sample_per_packet = 12;
1772 p_soun->i_bytes_per_packet = 2;
1773 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1774 p_soun->i_bytes_per_sample = 2;
1776 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1777 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1778 p_soun->i_samplesize = 8;
1779 p_track->i_sample_size = p_soun->i_channelcount;
1781 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1782 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1783 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1784 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1785 /* What would be the fun if you could trust the .mov */
1786 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1793 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1795 p_soun->i_qt_version = 0;
1798 else if( p_sample->data.p_sample_soun->i_qt_version == 1 )
1800 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1802 switch( p_sample->i_type )
1804 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1805 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1807 if( p_track->i_sample_size > 1 )
1808 p_soun->i_qt_version = 0;
1811 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1812 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1813 case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
1814 p_soun->i_qt_version = 0;
1821 if( p_track->i_sample_size != 0 &&
1822 p_sample->data.p_sample_soun->i_qt_version == 1 &&
1823 p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1825 msg_Err( p_demux, "Invalid sample per packet value for qt_version 1. Broken muxer!" );
1826 p_sample->data.p_sample_soun->i_qt_version = 0;
1835 /* It's a little ugly but .. there are special cases */
1836 switch( p_sample->i_type )
1838 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1839 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1841 p_track->fmt.i_codec = VLC_CODEC_MPGA;
1844 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1846 MP4_Box_t *p_dac3_box = MP4_BoxGet( p_sample, "dac3", 0 );
1848 p_track->fmt.i_codec = VLC_CODEC_A52;
1851 static const int pi_bitrate[] = {
1858 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
1859 p_track->fmt.audio.i_channels = 0;
1860 p_track->fmt.i_bitrate = 0;
1861 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
1862 p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
1863 p_track->fmt.audio.i_bitspersample = 0;
1867 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1869 p_track->fmt.i_codec = VLC_CODEC_EAC3;
1873 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1874 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1876 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1878 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1879 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1881 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1883 /* Buggy files workaround */
1884 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1885 p_sample->data.p_sample_soun->i_sampleratehi) )
1887 MP4_Box_data_sample_soun_t *p_soun =
1888 p_sample->data.p_sample_soun;
1890 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1891 "(%u), making both equal (report any problem).",
1892 p_track->i_timescale, p_soun->i_sampleratehi );
1894 if( p_soun->i_sampleratehi != 0 )
1895 p_track->i_timescale = p_soun->i_sampleratehi;
1897 p_soun->i_sampleratehi = p_track->i_timescale;
1902 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1903 p_track->fmt.i_codec = VLC_CODEC_H263;
1906 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1907 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1908 p_track->fmt.i_codec = VLC_CODEC_SUBT;
1909 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1910 /* FIXME UTF-8 doesn't work here ? */
1911 if( p_track->b_mac_encoding )
1912 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1914 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1917 case VLC_FOURCC('y','v','1','2'):
1918 p_track->fmt.i_codec = VLC_CODEC_YV12;
1920 case VLC_FOURCC('y','u','v','2'):
1921 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1924 case VLC_FOURCC('i','n','2','4'):
1925 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1926 VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
1928 case VLC_FOURCC('f','l','3','2'):
1929 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1930 VLC_CODEC_F32L : VLC_CODEC_F32B;
1932 case VLC_FOURCC('f','l','6','4'):
1933 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1934 VLC_CODEC_F64L : VLC_CODEC_F64B;
1936 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
1938 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1939 if( p_soun->i_qt_version == 2 &&
1940 p_soun->i_qt_description > 20 + 28 )
1947 static const struct {
1951 vlc_fourcc_t i_codec;
1953 { 0x01, 0x03, 32, VLC_CODEC_F32L },
1954 { 0x01, 0x03, 64, VLC_CODEC_F64L },
1955 { 0x01|0x02, 0x03, 32, VLC_CODEC_F32B },
1956 { 0x01|0x02, 0x03, 64, VLC_CODEC_F64B },
1958 { 0x00, 0x05, 8, VLC_CODEC_U8 },
1959 { 0x00| 0x04, 0x05, 8, VLC_CODEC_S8 },
1961 { 0x00, 0x07, 16, VLC_CODEC_U16L },
1962 { 0x00|0x02, 0x07, 16, VLC_CODEC_U16B },
1963 { 0x00 |0x04, 0x07, 16, VLC_CODEC_S16L },
1964 { 0x00|0x02|0x04, 0x07, 16, VLC_CODEC_S16B },
1966 { 0x00, 0x07, 24, VLC_CODEC_U24L },
1967 { 0x00|0x02, 0x07, 24, VLC_CODEC_U24B },
1968 { 0x00 |0x04, 0x07, 24, VLC_CODEC_S24L },
1969 { 0x00|0x02|0x04, 0x07, 24, VLC_CODEC_S24B },
1971 { 0x00, 0x07, 32, VLC_CODEC_U32L },
1972 { 0x00|0x02, 0x07, 32, VLC_CODEC_U32B },
1973 { 0x00 |0x04, 0x07, 32, VLC_CODEC_S32L },
1974 { 0x00|0x02|0x04, 0x07, 32, VLC_CODEC_S32B },
1978 uint32_t i_bits = GetDWBE(&p_soun->p_qt_description[20 + 20]);
1979 uint32_t i_flags = GetDWBE(&p_soun->p_qt_description[20 + 24]);
1981 for( int i = 0; p_formats[i].i_codec; i++ )
1983 if( p_formats[i].i_bits == i_bits &&
1984 (i_flags & p_formats[i].i_mask) == p_formats[i].i_flags )
1986 p_track->fmt.i_codec = p_formats[i].i_codec;
1987 p_track->fmt.audio.i_bitspersample = i_bits;
1988 p_track->fmt.audio.i_blockalign = p_soun->i_channelcount * i_bits / 8;
1989 p_track->i_sample_size = p_track->fmt.audio.i_blockalign;
1991 p_soun->i_qt_version = 0;
1999 p_track->fmt.i_codec = p_sample->i_type;
2003 /* now see if esds is present and if so create a data packet
2004 with decoder_specific_info */
2005 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
2006 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
2007 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
2008 ( p_esds->data.p_esds )&&
2011 /* First update information based on i_objectTypeIndication */
2012 switch( p_decconfig->i_objectTypeIndication )
2014 case( 0x20 ): /* MPEG4 VIDEO */
2015 p_track->fmt.i_codec = VLC_CODEC_MP4V;
2017 case( 0x21 ): /* H.264 */
2018 p_track->fmt.i_codec = VLC_CODEC_H264;
2021 p_track->fmt.i_codec = VLC_CODEC_MP4A;
2022 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
2023 p_decconfig->p_decoder_specific_info[0] == 0xF8 &&
2024 (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
2026 p_track->fmt.i_codec = VLC_CODEC_ALS;
2034 case( 0x65): /* MPEG2 video */
2035 p_track->fmt.i_codec = VLC_CODEC_MPGV;
2037 /* Theses are MPEG2-AAC */
2038 case( 0x66): /* main profile */
2039 case( 0x67): /* Low complexity profile */
2040 case( 0x68): /* Scaleable Sampling rate profile */
2041 p_track->fmt.i_codec = VLC_CODEC_MP4A;
2043 /* True MPEG 2 audio */
2045 p_track->fmt.i_codec = VLC_CODEC_MPGA;
2047 case( 0x6a): /* MPEG1 video */
2048 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2050 case( 0x6b): /* MPEG1 audio */
2051 p_track->fmt.i_codec = VLC_CODEC_MPGA;
2053 case( 0x6c ): /* jpeg */
2054 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
2056 case( 0x6d ): /* png */
2057 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
2059 case( 0x6e ): /* jpeg2000 */
2060 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
2062 case( 0xa3 ): /* vc1 */
2063 p_track->fmt.i_codec = VLC_CODEC_VC1;
2066 p_track->fmt.i_codec = VLC_CODEC_DIRAC;
2069 p_track->fmt.i_codec = VLC_CODEC_A52;
2072 p_track->fmt.i_codec = VLC_CODEC_EAC3;
2074 case( 0xa9 ): /* dts */
2075 case( 0xaa ): /* DTS-HD HRA */
2076 case( 0xab ): /* DTS-HD Master Audio */
2077 p_track->fmt.i_codec = VLC_CODEC_DTS;
2080 p_track->fmt.i_codec = VLC_CODEC_VORBIS;
2084 case( 0xe0 ): /* NeroDigital: dvd subs */
2085 if( p_track->fmt.i_cat == SPU_ES )
2087 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
2088 if( p_track->i_width > 0 )
2089 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
2090 if( p_track->i_height > 0 )
2091 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
2094 case( 0xe1 ): /* QCelp for 3gp */
2095 if( p_track->fmt.i_cat == AUDIO_ES )
2097 p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
2103 /* Unknown entry, but don't touch i_fourcc */
2105 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
2106 p_decconfig->i_objectTypeIndication,
2107 p_track->i_track_ID );
2110 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
2111 if( p_track->fmt.i_extra > 0 )
2113 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2114 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
2115 p_track->fmt.i_extra );
2120 switch( p_sample->i_type )
2122 /* qt decoder, send the complete chunk */
2123 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
2124 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
2125 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
2126 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
2127 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
2128 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
2129 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
2130 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
2131 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
2132 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
2133 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
2134 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
2135 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2137 /* qt decoder, send the complete chunk */
2138 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
2139 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
2140 case VLC_FOURCC( 'V', 'P', '3', '1' ):
2141 case VLC_FOURCC( '3', 'I', 'V', '1' ):
2142 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
2143 p_track->fmt.i_extra =
2144 p_sample->data.p_sample_vide->i_qt_image_description;
2145 if( p_track->fmt.i_extra > 0 )
2147 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2148 memcpy( p_track->fmt.p_extra,
2149 p_sample->data.p_sample_vide->p_qt_image_description,
2150 p_track->fmt.i_extra);
2154 case VLC_CODEC_AMR_NB:
2155 p_track->fmt.audio.i_rate = 8000;
2157 case VLC_CODEC_AMR_WB:
2158 p_track->fmt.audio.i_rate = 16000;
2160 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
2161 case VLC_CODEC_QDM2:
2162 case VLC_CODEC_ALAC:
2163 p_track->fmt.i_extra =
2164 p_sample->data.p_sample_soun->i_qt_description;
2165 if( p_track->fmt.i_extra > 0 )
2167 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2168 memcpy( p_track->fmt.p_extra,
2169 p_sample->data.p_sample_soun->p_qt_description,
2170 p_track->fmt.i_extra);
2172 if( p_track->fmt.i_extra == 56 && p_sample->i_type == VLC_CODEC_ALAC )
2174 p_track->fmt.audio.i_channels = *((uint8_t*)p_track->fmt.p_extra + 41);
2175 p_track->fmt.audio.i_rate = GetDWBE((uint8_t*)p_track->fmt.p_extra + 52);
2179 case VLC_FOURCC( 'v', 'c', '-', '1' ):
2181 MP4_Box_t *p_dvc1 = MP4_BoxGet( p_sample, "dvc1" );
2184 p_track->fmt.i_extra = p_dvc1->data.p_dvc1->i_vc1;
2185 if( p_track->fmt.i_extra > 0 )
2187 p_track->fmt.p_extra = malloc( p_dvc1->data.p_dvc1->i_vc1 );
2188 memcpy( p_track->fmt.p_extra, p_dvc1->data.p_dvc1->p_vc1,
2189 p_track->fmt.i_extra );
2194 msg_Err( p_demux, "missing dvc1" );
2199 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
2200 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
2202 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
2206 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
2207 if( p_track->fmt.i_extra > 0 )
2209 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
2210 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
2211 p_track->fmt.i_extra );
2216 msg_Err( p_demux, "missing avcC" );
2220 case VLC_FOURCC( 'h', 'v', 'c', '1' ):
2222 MP4_Box_t *p_hvcC = MP4_BoxGet( p_sample, "hvcC" );
2226 p_track->fmt.i_extra = p_hvcC->data.p_hvcC->i_hvcC;
2227 if( p_track->fmt.i_extra > 0 )
2229 p_track->fmt.p_extra = malloc( p_hvcC->data.p_hvcC->i_hvcC );
2230 memcpy( p_track->fmt.p_extra, p_hvcC->data.p_hvcC->p_hvcC,
2231 p_track->fmt.i_extra );
2233 p_track->fmt.i_codec = VLC_CODEC_HEVC;
2237 msg_Err( p_demux, "missing hvcC" );
2243 case VLC_CODEC_ADPCM_MS:
2244 case VLC_CODEC_ADPCM_IMA_WAV:
2245 case VLC_CODEC_QCELP:
2246 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
2250 msg_Dbg( p_demux, "Unrecognized FourCC %4.4s", (char *)&p_sample->i_type );
2258 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2263 /* given a time it return sample/chunk
2264 * it also update elst field of the track
2266 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2267 int64_t i_start, uint32_t *pi_chunk,
2268 uint32_t *pi_sample )
2270 demux_sys_t *p_sys = p_demux->p_sys;
2271 MP4_Box_t *p_box_stss;
2273 unsigned int i_sample;
2274 unsigned int i_chunk;
2277 /* FIXME see if it's needed to check p_track->i_chunk_count */
2278 if( p_track->i_chunk_count == 0 )
2279 return( VLC_EGENERIC );
2281 /* handle elst (find the correct one) */
2282 MP4_TrackSetELST( p_demux, p_track, i_start );
2283 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2285 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2286 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
2288 /* now calculate i_start for this elst */
2290 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
2298 /* to track time scale */
2299 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2300 /* add elst offset */
2301 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2302 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2303 elst->i_media_time[p_track->i_elst] > 0 )
2305 i_start += elst->i_media_time[p_track->i_elst];
2308 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2309 "ms (track)", p_track->i_elst,
2310 i_mvt * 1000 / p_sys->i_timescale,
2311 i_start * 1000 / p_track->i_timescale );
2315 /* convert absolute time to in timescale unit */
2316 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2319 /* we start from sample 0/chunk 0, hope it won't take too much time */
2320 /* *** find good chunk *** */
2321 for( i_chunk = 0; ; i_chunk++ )
2323 if( i_chunk + 1 >= p_track->i_chunk_count )
2325 /* at the end and can't check if i_start in this chunk,
2326 it will be check while searching i_sample */
2327 i_chunk = p_track->i_chunk_count - 1;
2331 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2332 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
2338 /* *** find sample in the chunk *** */
2339 i_sample = p_track->chunk[i_chunk].i_sample_first;
2340 i_dts = p_track->chunk[i_chunk].i_first_dts;
2341 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2344 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2345 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2348 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2349 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2351 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2356 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2360 i_sample += ( i_start - i_dts ) /
2361 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2366 if( i_sample >= p_track->i_sample_count )
2368 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2369 "(seeking too far) chunk=%d sample=%d",
2370 p_track->i_track_ID, i_chunk, i_sample );
2371 return( VLC_EGENERIC );
2375 /* *** Try to find nearest sync points *** */
2376 if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2378 MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2379 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2380 p_track->i_track_ID );
2381 for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2383 if( i_index >= p_stss->i_entry_count - 1 ||
2384 i_sample < p_stss->i_sample_number[i_index+1] )
2386 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2387 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
2388 i_sample, i_sync_sample );
2390 if( i_sync_sample <= i_sample )
2392 while( i_chunk > 0 &&
2393 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2398 while( i_chunk < p_track->i_chunk_count - 1 &&
2399 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2400 p_track->chunk[i_chunk].i_sample_count )
2403 i_sample = i_sync_sample;
2410 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2411 "Sample Box (stss)", p_track->i_track_ID );
2414 *pi_chunk = i_chunk;
2415 *pi_sample = i_sample;
2420 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2421 unsigned int i_chunk, unsigned int i_sample )
2423 bool b_reselect = false;
2425 /* now see if actual es is ok */
2426 if( p_track->i_chunk >= p_track->i_chunk_count ||
2427 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2428 p_track->chunk[i_chunk].i_sample_description_index )
2430 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2431 p_track->i_track_ID );
2433 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2434 p_track->p_es, &b_reselect );
2436 es_out_Del( p_demux->out, p_track->p_es );
2438 p_track->p_es = NULL;
2440 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2442 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2443 p_track->i_track_ID );
2445 p_track->b_ok = false;
2446 p_track->b_selected = false;
2447 return VLC_EGENERIC;
2451 /* select again the new decoder */
2454 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2457 p_track->i_chunk = i_chunk;
2458 p_track->i_sample = i_sample;
2460 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2463 /****************************************************************************
2465 ****************************************************************************
2466 * Parse track information and create all needed data to run a track
2467 * If it succeed b_ok is set to 1 else to 0
2468 ****************************************************************************/
2469 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2470 MP4_Box_t *p_box_trak,
2471 bool b_force_enable )
2473 demux_sys_t *p_sys = p_demux->p_sys;
2475 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2476 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2486 char language[4] = { '\0' };
2488 /* hint track unsupported */
2490 /* set default value (-> track unusable) */
2491 p_track->b_ok = false;
2492 p_track->b_enable = false;
2493 p_track->b_selected = false;
2494 p_track->b_chapter = false;
2495 p_track->b_mac_encoding = false;
2497 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2504 /* do we launch this track by default ? */
2506 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2507 if( !p_track->b_enable )
2508 p_track->fmt.i_priority = -1;
2510 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2512 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2513 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2514 p_track->f_rotation = p_tkhd->data.p_tkhd->f_rotation;
2518 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2521 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2522 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2524 if( ( !p_mdhd )||( !p_hdlr ) )
2529 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2530 if( p_track->i_timescale == 0 )
2533 if( p_mdhd->data.p_mdhd->i_language_code < 0x400 )
2535 strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2536 p_track->b_mac_encoding = true;
2538 else if( p_mdhd->data.p_mdhd->i_language_code == 0x7fff )
2539 p_track->b_mac_encoding = true;
2542 for( unsigned i = 0; i < 3; i++ )
2543 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2547 switch( p_hdlr->data.p_hdlr->i_handler_type )
2550 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2554 p_track->fmt.i_cat = AUDIO_ES;
2558 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2562 p_track->fmt.i_cat = VIDEO_ES;
2569 p_track->fmt.i_cat = SPU_ES;
2576 p_track->i_elst = 0;
2577 p_track->i_elst_time = 0;
2578 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2580 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2583 msg_Warn( p_demux, "elst box found" );
2584 for( i = 0; i < elst->i_entry_count; i++ )
2586 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2587 "ms) rate=%d.%d", i,
2588 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2589 elst->i_media_time[i] >= 0 ?
2590 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2592 elst->i_media_rate_integer[i],
2593 elst->i_media_rate_fraction[i] );
2600 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2602 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2603 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2609 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2611 p_track->fmt.psz_language = strdup( language );
2614 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2617 MP4_Box_t *p_box_iter;
2618 for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2619 p_box_iter = p_box_iter->p_next )
2621 switch( p_box_iter->i_type )
2624 p_track->fmt.psz_description =
2625 strdup( p_box_iter->data.p_0xa9xxx->psz_text );
2628 p_track->fmt.psz_description =
2629 strdup( p_box_iter->data.p_name->psz_text );
2635 /* Create chunk index table and sample index table */
2636 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2637 TrackCreateSamplesIndex( p_demux, p_track ) )
2639 return; /* cannot create chunks index */
2642 p_track->i_chunk = 0;
2643 p_track->i_sample = 0;
2645 /* Mark chapter only track */
2646 if( p_sys->p_tref_chap )
2648 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2651 for( i = 0; i < p_chap->i_entry_count; i++ )
2653 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2655 p_track->b_chapter = true;
2656 p_track->b_enable = false;
2663 if( b_force_enable &&
2664 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2666 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2667 p_track->i_track_ID );
2668 p_track->b_enable = true;
2669 p_track->fmt.i_priority = 0;
2672 p_track->p_es = NULL;
2673 if( TrackCreateES( p_demux,
2674 p_track, p_track->i_chunk,
2675 p_track->b_chapter ? NULL : &p_track->p_es ) )
2677 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2678 p_track->i_track_ID );
2681 p_track->b_ok = true;
2685 for( i = 0; i < p_track->i_chunk_count; i++ )
2687 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2688 i, p_track->chunk[i].i_sample_count,
2689 p_track->chunk[i].i_first_dts );
2696 static void FreeAndResetChunk( mp4_chunk_t *ck )
2698 free( ck->p_sample_count_dts );
2699 free( ck->p_sample_delta_dts );
2700 free( ck->p_sample_count_pts );
2701 free( ck->p_sample_offset_pts );
2702 free( ck->p_sample_size );
2703 for( uint32_t i = 0; i < ck->i_sample_count; i++ )
2704 free( ck->p_sample_data[i] );
2705 free( ck->p_sample_data );
2706 memset( ck, 0, sizeof( mp4_chunk_t ) );
2709 /****************************************************************************
2711 ****************************************************************************
2712 * Destroy a track created by MP4_TrackCreate.
2713 ****************************************************************************/
2714 static void MP4_TrackDestroy( mp4_track_t *p_track )
2716 unsigned int i_chunk;
2718 p_track->b_ok = false;
2719 p_track->b_enable = false;
2720 p_track->b_selected = false;
2722 es_format_Clean( &p_track->fmt );
2724 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2726 if( p_track->chunk )
2728 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2729 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2731 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2732 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2735 FREENULL( p_track->chunk );
2736 if( p_track->cchunk ) {
2737 FreeAndResetChunk( p_track->cchunk );
2738 FREENULL( p_track->cchunk );
2741 if( !p_track->i_sample_size )
2743 FREENULL( p_track->p_sample_size );
2747 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2750 if( !p_track->b_ok || p_track->b_chapter )
2752 return VLC_EGENERIC;
2755 if( p_track->b_selected )
2757 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2758 p_track->i_track_ID );
2762 return MP4_TrackSeek( p_demux, p_track, i_start );
2765 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2767 if( !p_track->b_ok || p_track->b_chapter )
2772 if( !p_track->b_selected )
2774 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2775 p_track->i_track_ID );
2780 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2781 p_track->p_es, false );
2784 p_track->b_selected = false;
2787 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2793 if( !p_track->b_ok || p_track->b_chapter )
2794 return VLC_EGENERIC;
2796 p_track->b_selected = false;
2798 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2799 &i_chunk, &i_sample ) )
2801 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2802 p_track->i_track_ID );
2803 return VLC_EGENERIC;
2806 p_track->b_selected = true;
2808 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2809 p_track->b_selected = true;
2811 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2816 * 3 types: for audio
2819 #define QT_V0_MAX_SAMPLES 1024
2820 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2823 MP4_Box_data_sample_soun_t *p_soun;
2825 if( p_track->i_sample_size == 0 )
2827 /* most simple case */
2828 return p_track->p_sample_size[p_track->i_sample];
2830 if( p_track->fmt.i_cat != AUDIO_ES )
2832 return p_track->i_sample_size;
2835 p_soun = p_track->p_sample->data.p_sample_soun;
2837 if( p_soun->i_qt_version == 1 )
2839 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
2840 if( p_track->fmt.audio.i_blockalign > 1 )
2841 i_samples = p_soun->i_sample_per_packet;
2843 i_size = i_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2845 else if( p_track->i_sample_size > 256 )
2847 /* We do that so we don't read too much data
2848 * (in this case we are likely dealing with compressed data) */
2849 i_size = p_track->i_sample_size;
2853 /* Read a bunch of samples at once */
2854 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2855 ( p_track->i_sample -
2856 p_track->chunk[p_track->i_chunk].i_sample_first );
2858 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2859 i_size = i_samples * p_track->i_sample_size;
2862 //fprintf( stderr, "size=%d\n", i_size );
2866 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2868 unsigned int i_sample;
2871 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2873 if( p_track->i_sample_size )
2875 MP4_Box_data_sample_soun_t *p_soun =
2876 p_track->p_sample->data.p_sample_soun;
2878 if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
2880 i_pos += ( p_track->i_sample -
2881 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2882 p_track->i_sample_size;
2886 /* we read chunk by chunk unless a blockalign is requested */
2887 if( p_track->fmt.audio.i_blockalign > 1 )
2888 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
2889 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2894 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2895 i_sample < p_track->i_sample; i_sample++ )
2897 i_pos += p_track->p_sample_size[i_sample];
2904 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2906 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2908 MP4_Box_data_sample_soun_t *p_soun;
2910 p_soun = p_track->p_sample->data.p_sample_soun;
2912 if( p_soun->i_qt_version == 1 )
2914 /* we read chunk by chunk unless a blockalign is requested */
2915 if( p_track->fmt.audio.i_blockalign > 1 )
2916 p_track->i_sample += p_soun->i_sample_per_packet;
2918 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
2920 else if( p_track->i_sample_size > 256 )
2922 /* We do that so we don't read too much data
2923 * (in this case we are likely dealing with compressed data) */
2924 p_track->i_sample += 1;
2929 p_track->i_sample += QT_V0_MAX_SAMPLES;
2930 if( p_track->i_sample >
2931 p_track->chunk[p_track->i_chunk].i_sample_first +
2932 p_track->chunk[p_track->i_chunk].i_sample_count )
2935 p_track->chunk[p_track->i_chunk].i_sample_first +
2936 p_track->chunk[p_track->i_chunk].i_sample_count;
2942 p_track->i_sample++;
2945 if( p_track->i_sample >= p_track->i_sample_count )
2946 return VLC_EGENERIC;
2948 /* Have we changed chunk ? */
2949 if( p_track->i_sample >=
2950 p_track->chunk[p_track->i_chunk].i_sample_first +
2951 p_track->chunk[p_track->i_chunk].i_sample_count )
2953 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2954 p_track->i_sample ) )
2956 msg_Warn( p_demux, "track[0x%x] will be disabled "
2957 "(cannot restart decoder)", p_track->i_track_ID );
2958 MP4_TrackUnselect( p_demux, p_track );
2959 return VLC_EGENERIC;
2963 /* Have we changed elst */
2964 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2966 demux_sys_t *p_sys = p_demux->p_sys;
2967 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2968 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2969 p_sys->i_timescale / (int64_t)1000000;
2971 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2972 i_mvt >= p_track->i_elst_time +
2973 elst->i_segment_duration[p_track->i_elst] )
2975 MP4_TrackSetELST( p_demux, p_track,
2976 MP4_TrackGetDTS( p_demux, p_track ) );
2983 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2986 demux_sys_t *p_sys = p_demux->p_sys;
2987 int i_elst_last = tk->i_elst;
2989 /* handle elst (find the correct one) */
2991 tk->i_elst_time = 0;
2992 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2994 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2995 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2997 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2999 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
3001 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
3005 tk->i_elst_time += i_dur;
3008 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
3010 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
3011 tk->i_elst = elst->i_entry_count - 1;
3012 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
3015 if( elst->i_media_time[tk->i_elst] < 0 )
3018 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
3021 if( i_elst_last != tk->i_elst )
3023 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
3028 static const char *MP4_ConvertMacCode( uint16_t i_code )
3030 static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
3031 { "en", 0 }, { "fr", 1 }, { "de", 2 }, { "it", 3 }, { "nl", 4 },
3032 { "sv", 5 }, { "es", 6 }, { "da", 7 }, { "pt", 8 }, { "no", 9 },
3033 { "he", 10 }, { "ja", 11 }, { "ar", 12 }, { "fi", 13 }, { "el", 14 },
3034 { "is", 15 }, { "mt", 16 }, { "tr", 17 }, { "hr", 18 }, { "zh", 19 },
3035 { "ur", 20 }, { "hi", 21 }, { "th", 22 }, { "ko", 23 }, { "lt", 24 },
3036 { "pl", 25 }, { "hu", 26 }, { "et", 27 }, { "lv", 28 }, //{ "??", 29 },
3037 { "fo", 30 }, { "fa", 31 }, { "ru", 32 }, { "zh", 33 }, { "nl", 34 },
3038 { "ga", 35 }, { "sq", 36 }, { "ro", 37 }, { "cs", 38 }, { "sk", 39 },
3039 { "sl", 40 }, { "yi", 41 }, { "sr", 42 }, { "mk", 43 }, { "bg", 44 },
3040 { "uk", 45 }, { "be", 46 }, { "uz", 47 }, { "az", 48 }, { "kk", 48 },
3041 { "az", 50 }, { "hy", 51 }, { "ka", 52 }, { "mo", 53 }, { "ky", 54 },
3042 { "tg", 55 }, { "tk", 56 }, { "mn", 57 }, { "mn", 58 }, { "ps", 59 },
3043 { "ku", 60 }, { "ks", 61 }, { "sd", 62 }, { "bo", 63 }, { "ne", 64 },
3044 { "sa", 65 }, { "mr", 66 }, { "bn", 67 }, { "as", 68 }, { "gu", 69 },
3045 { "pa", 70 }, { "or", 71 }, { "ml", 72 }, { "kn", 73 }, { "ta", 74 },
3046 { "te", 75 }, { "si", 76 }, { "my", 77 }, { "km", 78 }, { "lo", 79 },
3047 { "vi", 80 }, { "id", 81 }, { "tl", 82 }, { "ms", 83 }, { "ms", 84 },
3048 { "am", 85 }, { "ti", 86 }, { "om", 87 }, { "so", 88 }, { "sw", 89 },
3049 { "rw", 90 }, { "rn", 91 }, { "ny", 92 }, { "mg", 93 }, { "eo", 94 },
3051 { "cy", 128 }, { "eu", 129 },
3052 { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
3053 { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
3054 { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
3055 { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
3060 for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
3062 if( p_cvt[i].i_code == i_code )
3063 return p_cvt[i].psz_iso639_1;
3068 /******************************************************************************
3069 * Here are the functions used for fragmented MP4
3070 *****************************************************************************/
3073 * It computes the sample rate for a video track using current video chunk
3075 static void ChunkGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
3076 const mp4_track_t *p_track )
3078 if( p_track->cchunk->i_last_dts == 0 )
3085 const mp4_chunk_t *p_chunk = p_track->cchunk;
3087 uint32_t i_sample = p_chunk->i_sample_count;
3088 uint64_t i_first_dts = p_chunk->i_first_dts;
3089 uint64_t i_last_dts = p_chunk->i_last_dts;
3091 if( i_sample > 1 && i_first_dts < i_last_dts )
3092 vlc_ureduce( pi_num, pi_den,
3093 ( i_sample - 1) * p_track->i_timescale,
3094 i_last_dts - i_first_dts,
3099 * Build raw avcC box (without the 8 bytes header)
3100 * \return The size of the box.
3102 static int build_raw_avcC( uint8_t **p_extra, const uint8_t *CodecPrivateData,
3103 const unsigned cpd_len )
3106 unsigned sps_len = 0, pps_len = 0;
3107 const uint32_t mark = 0x00000001;
3109 assert( CodecPrivateData[0] == 0 );
3110 assert( CodecPrivateData[1] == 0 );
3111 assert( CodecPrivateData[2] == 0 );
3112 assert( CodecPrivateData[3] == 1 );
3114 uint32_t length = cpd_len + 3;
3115 avcC = calloc( length, 1 );
3116 if( unlikely( avcC == NULL ) )
3119 uint8_t *sps = avcC + 8;
3121 uint32_t candidate = ~mark;
3122 CodecPrivateData += 4;
3123 for( unsigned i = 0; i < cpd_len - 4; i++ )
3125 sps[i] = CodecPrivateData[i];
3126 candidate = (candidate << 8) | CodecPrivateData[i];
3127 if( candidate == mark )
3135 uint8_t *pps = sps + sps_len + 3;
3136 pps_len = cpd_len - sps_len - 4 * 2;
3137 memcpy( pps, CodecPrivateData + sps_len + 4, pps_len );
3140 uint8_t AVCProfileIndication = 0x64;
3141 uint8_t profile_compatibility = 0x40;
3142 uint8_t AVCLevelIndication = 0x1f;
3143 uint8_t lengthSizeMinusOne = 0x03;
3146 avcC[1] = AVCProfileIndication;
3147 avcC[2] = profile_compatibility;
3148 avcC[3] = AVCLevelIndication;
3149 avcC[4] = 0xfc + lengthSizeMinusOne;
3151 avcC[6] = (sps_len & 0xff00)>>8;
3152 avcC[7] = sps_len & 0xff;
3154 avcC[8+sps_len] = 1;
3155 avcC[9+sps_len] = (pps_len & 0xff00) >> 8;
3156 avcC[10+sps_len] = pps_len & 0xff;
3163 * Build a mp4_track_t from a StraBox
3166 static inline int MP4_SetCodecExtraData( es_format_t *fmt, MP4_Box_data_stra_t *p_data )
3168 fmt->i_extra = p_data->cpd_len;
3169 fmt->p_extra = malloc( p_data->cpd_len );
3170 if( unlikely( !fmt->p_extra ) )
3172 memcpy( fmt->p_extra, p_data->CodecPrivateData, p_data->cpd_len );
3176 static int MP4_frg_TrackCreate( demux_t *p_demux, mp4_track_t *p_track, MP4_Box_t *p_stra )
3178 demux_sys_t *p_sys = p_demux->p_sys;
3180 MP4_Box_data_stra_t *p_data = p_stra->data.p_stra;
3182 return VLC_EGENERIC;
3184 p_track->b_ok = true;
3185 p_track->b_selected = false;
3186 p_track->i_sample_count = UINT32_MAX;
3188 p_track->i_timescale = p_sys->i_timescale;
3189 p_track->i_width = p_data->MaxWidth;
3190 p_track->i_height = p_data->MaxHeight;
3191 p_track->i_track_ID = p_data->i_track_ID;
3193 es_format_t *fmt = &p_track->fmt;
3195 return VLC_EGENERIC;
3197 es_format_Init( fmt, p_data->i_es_cat, 0 );
3199 /* Set language FIXME */
3200 fmt->psz_language = strdup( "en" );
3202 fmt->i_original_fourcc = p_data->FourCC;
3203 fmt->i_codec = vlc_fourcc_GetCodec( fmt->i_cat, p_data->FourCC );
3205 uint8_t **p_extra = (uint8_t **)&fmt->p_extra;
3206 /* See http://msdn.microsoft.com/en-us/library/ff728116%28v=vs.90%29.aspx
3207 * for MS weird use of FourCC*/
3208 switch( fmt->i_cat )
3211 if( p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', '1' ) ||
3212 p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', 'B' ) ||
3213 p_data->FourCC == VLC_FOURCC( 'H', '2', '6', '4' ) )
3215 fmt->i_extra = build_raw_avcC( p_extra,
3216 p_data->CodecPrivateData, p_data->cpd_len );
3220 ret = MP4_SetCodecExtraData( fmt, p_data );
3221 if( ret != VLC_SUCCESS )
3225 fmt->video.i_width = p_data->MaxWidth;
3226 fmt->video.i_height = p_data->MaxHeight;
3227 fmt->video.i_bits_per_pixel = 0x18;
3228 fmt->video.i_visible_width = p_data->MaxWidth;
3229 fmt->video.i_visible_height = p_data->MaxHeight;
3232 ChunkGetESSampleRate( &fmt->video.i_frame_rate,
3233 &fmt->video.i_frame_rate_base, p_track );
3235 if( fmt->video.i_frame_rate_base != 0 )
3237 p_demux->p_sys->f_fps = (float)fmt->video.i_frame_rate /
3238 (float)fmt->video.i_frame_rate_base;
3241 p_demux->p_sys->f_fps = 24;
3246 fmt->audio.i_channels = p_data->Channels;
3247 fmt->audio.i_rate = p_data->SamplingRate;
3248 fmt->audio.i_bitspersample = p_data->BitsPerSample;
3249 fmt->audio.i_blockalign = p_data->nBlockAlign;
3251 fmt->i_bitrate = p_data->Bitrate;
3253 ret = MP4_SetCodecExtraData( fmt, p_data );
3254 if( ret != VLC_SUCCESS )
3267 * Return the track identified by tid
3269 static mp4_track_t *MP4_frg_GetTrack( demux_t *p_demux, const uint32_t tid )
3271 demux_sys_t *p_sys = p_demux->p_sys;
3273 mp4_track_t *ret = NULL;
3274 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
3276 ret = &p_sys->track[i];
3279 if( ret->i_track_ID == tid )
3282 msg_Err( p_demux, "MP4_frg_GetTrack: track %"PRIu32" not found!", tid );
3286 static void FlushChunk( demux_t *p_demux, mp4_track_t *tk )
3288 msg_Dbg( p_demux, "Flushing chunk for track id %u", tk->i_track_ID );
3289 mp4_chunk_t *ck = tk->cchunk;
3290 while( ck->i_sample < ck->i_sample_count )
3295 if( ck->p_sample_size == NULL || ck->p_sample_data == NULL )
3298 uint32_t sample_size = ck->p_sample_size[ck->i_sample];
3299 assert( sample_size > 0 );
3300 p_block = block_Alloc( sample_size );
3301 if( unlikely( !p_block ) )
3304 uint8_t *src = ck->p_sample_data[ck->i_sample];
3305 memcpy( p_block->p_buffer, src, sample_size );
3309 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
3311 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
3313 p_block->i_pts = p_block->i_dts + i_delta;
3314 else if( tk->fmt.i_cat != VIDEO_ES )
3315 p_block->i_pts = p_block->i_dts;
3317 p_block->i_pts = VLC_TS_INVALID;
3319 es_out_Send( p_demux->out, tk->p_es, p_block );
3327 * \Note If we call that function too soon,
3328 * before the track has been selected by MP4_TrackSelect
3329 * (during the first execution of Demux), then the track gets disabled
3331 static int ReInitDecoder( demux_t *p_demux, mp4_track_t *p_track )
3333 demux_sys_t *p_sys = p_demux->p_sys;
3335 uint32_t i_sample = 0;
3336 bool b_smooth = false;
3337 MP4_Box_t *p_stra = NULL, *p_trak = NULL;
3339 if( !CmpUUID( &p_sys->p_root->p_first->i_uuid, &SmooBoxUUID ) )
3344 p_stra = MP4_BoxGet( p_sys->p_root, "uuid/uuid[0]" );
3345 if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
3346 return VLC_EGENERIC;
3350 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[0]" );
3352 return VLC_EGENERIC;
3355 i_sample = p_track->i_sample;
3356 es_out_Del( p_demux->out, p_track->p_es );
3357 es_format_Clean( &p_track->fmt );
3360 MP4_frg_TrackCreate( p_demux, p_track, p_stra );
3362 MP4_TrackCreate( p_demux, p_track, p_trak, true );
3364 p_track->i_sample = i_sample;
3366 /* Temporary hack until we support track selection */
3367 p_track->b_selected = true;
3368 p_track->b_ok = true;
3369 p_track->b_enable = true;
3371 p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
3372 p_track->b_codec_need_restart = false;
3378 * This function fills a mp4_chunk_t structure from a MP4_Box_t (p_chunk).
3379 * The 'i_tk_id' argument returns the ID of the track the chunk belongs to.
3380 * \note p_chunk usually contains a 'moof' and a 'mdat', and might contain a 'sidx'.
3381 * \return VLC_SUCCESS, VLC_EGENERIC or VLC_ENOMEM.
3383 static int MP4_frg_GetChunk( demux_t *p_demux, MP4_Box_t *p_chunk, unsigned *i_tk_id )
3385 MP4_Box_t *p_sidx = MP4_BoxGet( p_chunk, "sidx" );
3386 MP4_Box_t *p_moof = MP4_BoxGet( p_chunk, "moof" );
3389 msg_Warn( p_demux, "no moof box found!" );
3390 return VLC_EGENERIC;
3393 MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
3396 msg_Warn( p_demux, "no traf box found!" );
3397 return VLC_EGENERIC;
3400 MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
3403 msg_Warn( p_demux, "no tfhd box found!" );
3404 return VLC_EGENERIC;
3407 uint32_t i_track_ID = p_tfhd->data.p_tfhd->i_track_ID;
3408 *i_tk_id = i_track_ID;
3409 assert( i_track_ID > 0 );
3410 msg_Dbg( p_demux, "GetChunk: track ID is %"PRIu32"", i_track_ID );
3412 mp4_track_t *p_track = MP4_frg_GetTrack( p_demux, i_track_ID );
3414 return VLC_EGENERIC;
3416 mp4_chunk_t *ret = p_track->cchunk;
3418 if( p_tfhd->data.p_tfhd->b_empty )
3419 msg_Warn( p_demux, "No samples in this chunk!" );
3421 /* Usually we read 100 ms of each track. However, suppose we have two tracks,
3422 * Ta and Tv (audio and video). Suppose also that Ta is the first track to be
3423 * read, i.e. we read 100 ms of Ta, then 100 ms of Tv, then 100 ms of Ta,
3424 * and so on. Finally, suppose that we get the chunks the other way around,
3425 * i.e. first a chunk of Tv, then a chunk of Ta, then a chunk of Tv, and so on.
3426 * In that case, it is very likely that at some point, Ta->cchunk or Tv->cchunk
3427 * is not emptied when MP4_frg_GetChunks is called. It is therefore necessary to
3428 * flush it, i.e. send to the decoder the samples not yet sent.
3429 * Note that all the samples to be flushed should worth less than 100 ms,
3430 * (though I did not do the formal proof) and thus this flushing mechanism
3431 * should not cause A/V sync issues, or delays or whatever.
3433 if( ret->i_sample < ret->i_sample_count )
3434 FlushChunk( p_demux, p_track );
3436 if( ret->i_sample_count )
3437 FreeAndResetChunk( ret );
3439 uint32_t default_duration = 0;
3440 if( p_tfhd->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
3441 default_duration = p_tfhd->data.p_tfhd->i_default_sample_duration;
3443 uint32_t default_size = 0;
3444 if( p_tfhd->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
3445 default_size = p_tfhd->data.p_tfhd->i_default_sample_size;
3447 MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun");
3450 msg_Warn( p_demux, "no trun box found!" );
3451 return VLC_EGENERIC;
3453 MP4_Box_data_trun_t *p_trun_data = p_trun->data.p_trun;
3455 ret->i_sample_count = p_trun_data->i_sample_count;
3456 assert( ret->i_sample_count > 0 );
3457 ret->i_sample_description_index = 1; /* seems to be always 1, is it? */
3458 ret->i_sample_first = p_track->i_sample_first;
3459 p_track->i_sample_first += ret->i_sample_count;
3461 ret->i_first_dts = p_track->i_first_dts;
3463 /* XXX I already saw DASH content with no default_duration and no
3464 * MP4_TRUN_SAMPLE_DURATION flag, but a sidx box was present.
3465 * This chunk of code might be buggy with segments having
3466 * more than one subsegment. */
3467 if( !default_duration )
3471 MP4_Box_data_sidx_t *p_sidx_data = p_sidx->data.p_sidx;
3472 assert( p_sidx_data->i_reference_count == 1 );
3474 if( p_sidx_data->i_timescale == 0 )
3475 return VLC_EGENERIC;
3477 unsigned i_chunk_duration = p_sidx_data->p_items[0].i_subsegment_duration /
3478 p_sidx_data->i_timescale;
3479 default_duration = i_chunk_duration * p_track->i_timescale / ret->i_sample_count;
3484 msg_Dbg( p_demux, "Default sample duration is %"PRIu32, default_duration );
3486 ret->p_sample_count_dts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3487 ret->p_sample_delta_dts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3489 if( !ret->p_sample_count_dts || !ret->p_sample_delta_dts )
3492 ret->p_sample_count_pts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3493 if( !ret->p_sample_count_pts )
3496 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
3498 ret->p_sample_offset_pts = calloc( ret->i_sample_count, sizeof( int32_t ) );
3499 if( !ret->p_sample_offset_pts )
3503 ret->p_sample_size = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3504 if( !ret->p_sample_size )
3507 ret->p_sample_data = calloc( ret->i_sample_count, sizeof( uint8_t * ) );
3508 if( !ret->p_sample_data )
3511 uint32_t dur = 0, len;
3512 uint32_t chunk_duration = 0, chunk_size = 0;
3514 /* Skip header of mdat */
3515 stream_Read( p_demux->s, NULL, 8 );
3517 for( uint32_t i = 0; i < ret->i_sample_count; i++)
3519 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_DURATION )
3520 dur = p_trun_data->p_samples[i].i_duration;
3522 dur = default_duration;
3523 ret->p_sample_delta_dts[i] = dur;
3524 chunk_duration += dur;
3526 ret->p_sample_count_dts[i] = ret->p_sample_count_pts[i] = 1;
3528 if( ret->p_sample_offset_pts )
3530 ret->p_sample_offset_pts[i] =
3531 p_trun_data->p_samples[i].i_composition_time_offset;
3534 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_SIZE )
3535 len = ret->p_sample_size[i] = p_trun_data->p_samples[i].i_size;
3537 len = ret->p_sample_size[i] = default_size;
3539 ret->p_sample_data[i] = malloc( len );
3540 if( ret->p_sample_data[i] == NULL )
3542 int read = stream_Read( p_demux->s, ret->p_sample_data[i], len );
3543 if( read < (int)len )
3544 return VLC_EGENERIC;
3547 ret->i_last_dts = ret->i_first_dts + chunk_duration - dur;
3548 p_track->i_first_dts = chunk_duration + ret->i_first_dts;
3550 if( p_track->b_codec_need_restart &&
3551 p_track->fmt.i_cat == VIDEO_ES )
3552 ReInitDecoder( p_demux, p_track );
3554 p_track->b_has_non_empty_cchunk = true;
3559 * Get the next chunk of the track identified by i_tk_id.
3560 * \Note We don't want to seek all the time, so if the first chunk given by the
3561 * input method doesn't belong to the right track, we don't throw it away,
3562 * and so, in general, this function fetch more than one chunk.
3563 * Not to mention that a new init segment might be put everywhere
3564 * between two chunks by the input method.
3566 static int MP4_frg_GetChunks( demux_t *p_demux, const unsigned i_tk_id )
3568 demux_sys_t *p_sys = p_demux->p_sys;
3569 mp4_track_t *p_track;
3571 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
3573 MP4_Box_t *p_chunk = MP4_BoxGetNextChunk( p_demux->s );
3575 return VLC_EGENERIC;
3577 if( !p_chunk->p_first )
3578 goto MP4_frg_GetChunks_Error;
3579 uint32_t i_type = p_chunk->p_first->i_type;
3581 if( i_type == ATOM_uuid || i_type == ATOM_ftyp )
3583 MP4_BoxFree( p_demux->s, p_sys->p_root );
3584 p_sys->p_root = p_chunk;
3586 if( i_type == ATOM_ftyp ) /* DASH */
3588 MP4_Box_t *p_tkhd = MP4_BoxGet( p_chunk, "/moov/trak[0]/tkhd" );
3591 msg_Warn( p_demux, "No tkhd found!" );
3592 goto MP4_frg_GetChunks_Error;
3594 tid = p_tkhd->data.p_tkhd->i_track_ID;
3596 else /* Smooth Streaming */
3598 assert( !CmpUUID( &p_chunk->p_first->i_uuid, &SmooBoxUUID ) );
3599 MP4_Box_t *p_stra = MP4_BoxGet( p_chunk, "/uuid/uuid[0]" );
3600 if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
3602 msg_Warn( p_demux, "No StraBox found!" );
3603 goto MP4_frg_GetChunks_Error;
3605 tid = p_stra->data.p_stra->i_track_ID;
3608 p_track = MP4_frg_GetTrack( p_demux, tid );
3610 goto MP4_frg_GetChunks_Error;
3611 p_track->b_codec_need_restart = true;
3613 return MP4_frg_GetChunks( p_demux, i_tk_id );
3616 if( MP4_frg_GetChunk( p_demux, p_chunk, &tid ) != VLC_SUCCESS )
3617 goto MP4_frg_GetChunks_Error;
3619 MP4_BoxFree( p_demux->s, p_chunk );
3621 if( tid == i_tk_id )
3626 MP4_frg_GetChunks_Error:
3627 MP4_BoxFree( p_demux->s, p_chunk );
3628 return VLC_EGENERIC;
3634 static int MP4_frg_TrackSelect( demux_t *p_demux, mp4_track_t *p_track )
3636 if( !p_track->b_ok || p_track->b_chapter )
3638 return VLC_EGENERIC;
3641 if( p_track->b_selected )
3643 msg_Warn( p_demux, "track[Id 0x%x] already selected", p_track->i_track_ID );
3647 msg_Dbg( p_demux, "Select track id %u", p_track->i_track_ID );
3648 p_track->b_selected = true;
3653 * DemuxFrg: read packet and send them to decoders
3654 * \return 1 on success, 0 on error.
3655 * TODO check for newly selected track
3657 int DemuxFrg( demux_t *p_demux )
3659 demux_sys_t *p_sys = p_demux->p_sys;
3661 unsigned i_track_selected;
3663 /* check for newly selected/unselected track */
3664 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
3666 mp4_track_t *tk = &p_sys->track[i_track];
3669 if( !tk->b_ok || tk->b_chapter )
3672 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
3673 msg_Dbg( p_demux, "track %u %s!", tk->i_track_ID, b ? "enabled" : "disabled" );
3675 if( tk->b_selected && !b )
3677 MP4_TrackUnselect( p_demux, tk );
3679 else if( !tk->b_selected && b)
3681 MP4_frg_TrackSelect( p_demux, tk );
3684 if( tk->b_selected )
3688 if( i_track_selected <= 0 )
3690 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
3691 if( p_sys->i_timescale > 0 )
3693 int64_t i_length = (mtime_t)1000000 *
3694 (mtime_t)p_sys->i_duration /
3695 (mtime_t)p_sys->i_timescale;
3696 if( MP4_GetMoviePTS( p_sys ) >= i_length )
3701 msg_Warn( p_demux, "no track selected, exiting..." );
3705 /* first wait for the good time to read a packet */
3706 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
3708 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
3710 /* we will read 100ms for each stream so ...*/
3711 p_sys->i_time += __MAX( p_sys->i_timescale / 10, 1 );
3713 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
3715 mp4_track_t *tk = &p_sys->track[i_track];
3717 if( !tk->b_ok || tk->b_chapter || !tk->b_selected )
3719 msg_Warn( p_demux, "Skipping track id %u", tk->i_track_ID );
3723 if( !tk->b_has_non_empty_cchunk )
3725 if( MP4_frg_GetChunks( p_demux, tk->i_track_ID ) != VLC_SUCCESS )
3727 msg_Info( p_demux, "MP4_frg_GetChunks returned error. End of stream?" );
3732 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
3737 mp4_chunk_t *ck = tk->cchunk;
3738 if( ck->i_sample >= ck->i_sample_count )
3740 msg_Err( p_demux, "sample %"PRIu32" of %"PRIu32"",
3741 ck->i_sample, ck->i_sample_count );
3745 uint32_t sample_size = ck->p_sample_size[ck->i_sample];
3746 p_block = block_Alloc( sample_size );
3747 uint8_t *src = ck->p_sample_data[ck->i_sample];
3748 memcpy( p_block->p_buffer, src, sample_size );
3751 if( ck->i_sample == ck->i_sample_count )
3752 tk->b_has_non_empty_cchunk = false;
3755 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
3757 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
3759 p_block->i_pts = p_block->i_dts + i_delta;
3760 else if( tk->fmt.i_cat != VIDEO_ES )
3761 p_block->i_pts = p_block->i_dts;
3763 p_block->i_pts = VLC_TS_INVALID;
3765 es_out_Send( p_demux->out, tk->p_es, p_block );
3769 if( !tk->b_has_non_empty_cchunk )