1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 the VideoLAN team
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
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 )
221 p_sys->i_timescale = p_stra->data.p_stra->i_timescale;
222 p_sys->i_duration = p_stra->data.p_stra->i_duration;
229 /* Load all boxes ( except raw data ) */
230 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
232 goto LoadInitFragError;
238 msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" );
242 static int InitTracks( demux_t *p_demux )
244 demux_sys_t *p_sys = p_demux->p_sys;
246 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
247 if( p_sys->track == NULL )
250 if( p_sys->b_fragmented )
252 mp4_track_t *p_track;
253 for( uint16_t i = 0; i < p_sys->i_tracks; i++ )
255 p_track = &p_sys->track[i];
256 p_track->cchunk = calloc( 1, sizeof( mp4_chunk_t ) );
257 if( unlikely( !p_track->cchunk ) )
259 free( p_sys->track );
267 static void CreateTracksFromSmooBox( demux_t *p_demux )
269 demux_sys_t *p_sys = p_demux->p_sys;
271 MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
272 mp4_track_t *p_track;
274 for( int i = 0; i < 3; i++ )
276 MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
277 if( !p_stra || p_stra->data.p_stra->i_track_ID == 0 )
281 p_track = &p_sys->track[j]; j++;
282 MP4_frg_TrackCreate( p_demux, p_track, p_stra );
283 p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
288 /*****************************************************************************
289 * Open: check file and initializes MP4 structures
290 *****************************************************************************/
291 static int Open( vlc_object_t * p_this )
293 demux_t *p_demux = (demux_t *)p_this;
296 const uint8_t *p_peek;
307 /* A little test to see if it could be a mp4 */
308 if( stream_Peek( p_demux->s, &p_peek, 11 ) < 11 ) return VLC_EGENERIC;
310 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
321 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
324 /* We don't yet support f4v, but avformat does. */
325 if( p_peek[8] == 'f' && p_peek[9] == '4' && p_peek[10] == 'v' )
333 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
336 msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" );
340 /*Set exported functions */
341 p_demux->pf_demux = Demux;
342 p_demux->pf_control = Control;
344 /* create our structure that will contains all data */
345 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
347 /* Is it Smooth Streaming? */
348 bool b_smooth = false;
349 if( stream_Peek( p_demux->s, &p_peek, 24 ) < 24 ) return VLC_EGENERIC;
350 if( !CmpUUID( (UUID_t *)(p_peek + 8), &SmooBoxUUID ) )
353 p_sys->b_fragmented = true;
356 if( LoadInitFrag( p_demux, b_smooth ) != VLC_SUCCESS )
359 if( MP4_BoxCount( p_sys->p_root, "/moov/mvex" ) > 0 )
361 p_sys->b_fragmented = true;
363 if( p_sys->b_fragmented )
365 p_demux->pf_demux = DemuxFrg;
368 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
371 if( InitTracks( p_demux ) != VLC_SUCCESS )
373 CreateTracksFromSmooBox( p_demux );
376 else if( p_sys->b_fragmented && b_seekable )
378 /* We are not yet able to demux a fragmented MP4 file, using the 'mfra'
379 * box. So if STREAM_CAN_FASTSEEK is true, we're assuming we've got such
380 * a file, and we let avformat do the job. */
381 msg_Warn( p_demux, "MP4 plugin discarded "\
382 "(fast-seekable and fragmented, let avformat demux it)" );
383 stream_Seek( p_demux->s, 0 ); /* rewind, for other demux */
386 else if( !p_sys->b_fragmented && !b_seekable )
388 msg_Warn( p_demux, "MP4 plugin discarded (not fast-seekable)" );
389 stream_Seek( p_demux->s, 0 ); /* rewind, for other demux */
393 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
395 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
397 switch( p_ftyp->data.p_ftyp->i_major_brand )
401 "ISO Media file (isom) version %d.",
402 p_ftyp->data.p_ftyp->i_minor_version );
405 case( VLC_FOURCC( '3', 'g', 'p', '5' ) ):
406 case( VLC_FOURCC( '3', 'g', 'p', '6' ) ):
407 case( VLC_FOURCC( '3', 'g', 'p', '7' ) ):
408 msg_Dbg( p_demux, "3GPP Media file Release: %c",
409 #ifdef WORDS_BIGENDIAN
410 p_ftyp->data.p_ftyp->i_major_brand
412 p_ftyp->data.p_ftyp->i_major_brand >> 24
416 case( VLC_FOURCC( 'q', 't', ' ', ' ') ):
417 msg_Dbg( p_demux, "Apple QuickTime file" );
419 case( VLC_FOURCC( 'i', 's', 'm', 'l') ):
420 msg_Dbg( p_demux, "PIFF (= isml = fMP4) file" );
424 "unrecognized major file specification (%4.4s).",
425 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
431 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
434 /* the file need to have one moov box */
435 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
437 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
441 msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
444 /* we have a free box as a moov, rename it */
445 p_foov->i_type = ATOM_moov;
448 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
450 int i_count = MP4_BoxCount( p_rmra, "rmda" );
453 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
455 input_thread_t *p_input = demux_GetParentInput( p_demux );
456 input_item_t *p_current = input_GetItem( p_input );
458 input_item_node_t *p_subitems = input_item_node_Create( p_current );
460 for( i = 0; i < i_count; i++ )
462 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
466 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
470 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
472 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
473 psz_ref, (char*)&i_ref_type );
475 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
477 if( strstr( psz_ref, "qt5gateQT" ) )
479 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
482 if( !strncmp( psz_ref, "http://", 7 ) ||
483 !strncmp( psz_ref, "rtsp://", 7 ) )
490 char *psz_path = strdup( p_demux->psz_location );
491 char *end = strrchr( psz_path, '/' );
492 if( end ) end[1] = '\0';
493 else *psz_path = '\0';
495 if( asprintf( &psz_absolute, "%s://%s%s",
496 p_demux->psz_access, psz_path, psz_ref ) < 0 )
500 vlc_object_release( p_input) ;
505 psz_ref = psz_absolute;
508 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
509 input_item_t *p_item = input_item_New( psz_ref, NULL );
510 input_item_CopyOptions( p_current, p_item );
511 input_item_node_AppendItem( p_subitems, p_item );
512 vlc_gc_decref( p_item );
516 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
517 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
521 input_item_node_PostAndDelete( p_subitems );
522 vlc_object_release( p_input );
525 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
529 msg_Err( p_demux, "cannot find /moov/mvhd" );
534 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
535 p_demux->pf_demux = DemuxRef;
541 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
542 if( p_sys->i_timescale == 0 )
544 msg_Err( p_this, "bad timescale" );
547 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
550 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
552 msg_Err( p_demux, "cannot find any /moov/trak" );
555 msg_Dbg( p_demux, "found %d track%c",
557 p_sys->i_tracks ? 's':' ' );
559 if( InitTracks( p_demux ) != VLC_SUCCESS )
562 /* Search the first chap reference (like quicktime) and
563 * check that at least 1 stream is enabled */
564 p_sys->p_tref_chap = NULL;
565 b_enabled_es = false;
566 for( i = 0; i < p_sys->i_tracks; i++ )
568 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
571 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
572 if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
575 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
576 if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
577 p_sys->p_tref_chap = p_chap;
580 /* now process each track and extract all useful information */
581 for( i = 0; i < p_sys->i_tracks; i++ )
583 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
584 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
586 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
589 switch( p_sys->track[i].fmt.i_cat )
598 psz_cat = "subtitle";
606 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
607 p_sys->track[i].i_track_ID, psz_cat,
608 p_sys->track[i].b_enable ? "enable":"disable",
609 p_sys->track[i].fmt.psz_language ?
610 p_sys->track[i].fmt.psz_language : "undef" );
612 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
614 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
615 p_sys->track[i].i_track_ID,
616 p_sys->track[i].fmt.psz_language ?
617 p_sys->track[i].fmt.psz_language : "undef" );
621 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
622 p_sys->track[i].i_track_ID );
627 LoadChapter( p_demux );
634 MP4_BoxFree( p_demux->s, p_sys->p_root );
640 /*****************************************************************************
641 * Demux: read packet and send them to decoders
642 *****************************************************************************
643 * TODO check for newly selected track (ie audio upt to now )
644 *****************************************************************************/
645 static int Demux( demux_t *p_demux )
647 demux_sys_t *p_sys = p_demux->p_sys;
648 unsigned int i_track;
651 unsigned int i_track_selected;
653 /* check for newly selected/unselected track */
654 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
657 mp4_track_t *tk = &p_sys->track[i_track];
660 if( !tk->b_ok || tk->b_chapter ||
661 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
666 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
668 if( tk->b_selected && !b )
670 MP4_TrackUnselect( p_demux, tk );
672 else if( !tk->b_selected && b)
674 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
683 if( i_track_selected <= 0 )
685 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
686 if( p_sys->i_timescale > 0 )
688 int64_t i_length = (mtime_t)1000000 *
689 (mtime_t)p_sys->i_duration /
690 (mtime_t)p_sys->i_timescale;
691 if( MP4_GetMoviePTS( p_sys ) >= i_length )
696 msg_Warn( p_demux, "no track selected, exiting..." );
701 MP4_UpdateSeekpoint( p_demux );
703 /* first wait for the good time to read a packet */
704 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
706 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
708 /* we will read 100ms for each stream so ...*/
709 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
711 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
713 mp4_track_t *tk = &p_sys->track[i_track];
715 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
718 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
721 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
722 MP4_TrackGetDTS( p_demux, tk ),
723 MP4_GetMoviePTS( p_sys ) );
726 if( MP4_TrackSampleSize( tk ) > 0 )
732 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
734 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
736 MP4_TrackUnselect( p_demux, tk );
742 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
744 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
746 MP4_TrackUnselect( p_demux, tk );
750 else if( tk->fmt.i_cat == SPU_ES )
752 if( tk->fmt.i_codec == VLC_CODEC_SUBT &&
753 p_block->i_buffer >= 2 )
755 size_t i_size = GetWBE( p_block->p_buffer );
757 if( i_size + 2 <= p_block->i_buffer )
760 /* remove the length field, and append a '\0' */
761 memmove( &p_block->p_buffer[0],
762 &p_block->p_buffer[2], i_size );
763 p_block->p_buffer[i_size] = '\0';
764 p_block->i_buffer = i_size + 1;
766 /* convert \r -> \n */
767 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
775 p_block->i_buffer = 0;
780 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
782 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
784 p_block->i_pts = p_block->i_dts + i_delta;
785 else if( tk->fmt.i_cat != VIDEO_ES )
786 p_block->i_pts = p_block->i_dts;
788 p_block->i_pts = VLC_TS_INVALID;
790 es_out_Send( p_demux->out, tk->p_es, p_block );
794 if( MP4_TrackNextSample( p_demux, tk ) )
802 static void MP4_UpdateSeekpoint( demux_t *p_demux )
804 demux_sys_t *p_sys = p_demux->p_sys;
807 if( !p_sys->p_title )
809 i_time = MP4_GetMoviePTS( p_sys );
810 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
812 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
817 if( i != p_demux->info.i_seekpoint && i >= 0 )
819 p_demux->info.i_seekpoint = i;
820 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
823 /*****************************************************************************
825 ******************************************************************************/
826 static int Seek( demux_t *p_demux, mtime_t i_date )
828 demux_sys_t *p_sys = p_demux->p_sys;
829 unsigned int i_track;
831 /* First update global time */
832 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
833 p_sys->i_pcr = i_date;
835 /* Now for each stream try to go to this time */
836 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
838 mp4_track_t *tk = &p_sys->track[i_track];
839 MP4_TrackSeek( p_demux, tk, i_date );
841 MP4_UpdateSeekpoint( p_demux );
843 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
848 static int MP4_frg_Seek( demux_t *p_demux, double f )
850 demux_sys_t *p_sys = p_demux->p_sys;
852 int64_t i64 = stream_Size( p_demux->s );
853 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
859 /* update global time */
860 p_sys->i_time = (uint64_t)(f * (double)p_sys->i_duration);
861 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
863 for( unsigned i_track = 0; i_track < p_sys->i_tracks; i_track++ )
865 mp4_track_t *tk = &p_sys->track[i_track];
867 /* We don't want the current chunk to be flushed */
868 tk->cchunk->i_sample = tk->cchunk->i_sample_count;
870 /* reset/update some values */
871 tk->i_sample = tk->i_sample_first = 0;
872 tk->i_first_dts = p_sys->i_time;
874 /* We want to discard the current chunk and get the next one at once */
875 tk->b_has_non_empty_cchunk = false;
877 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->i_pcr );
882 /*****************************************************************************
884 *****************************************************************************/
885 static int Control( demux_t *p_demux, int i_query, va_list args )
887 demux_sys_t *p_sys = p_demux->p_sys;
894 case DEMUX_GET_POSITION:
895 pf = (double*)va_arg( args, double * );
896 if( p_sys->i_duration > 0 )
898 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
906 case DEMUX_SET_POSITION:
907 f = (double)va_arg( args, double );
908 if( p_sys->b_fragmented )
910 return MP4_frg_Seek( p_demux, f );
912 else if( p_sys->i_timescale > 0 )
914 i64 = (int64_t)( f * (double)1000000 *
915 (double)p_sys->i_duration /
916 (double)p_sys->i_timescale );
917 return Seek( p_demux, i64 );
919 else return VLC_SUCCESS;
922 pi64 = (int64_t*)va_arg( args, int64_t * );
923 if( p_sys->i_timescale > 0 )
925 *pi64 = (mtime_t)1000000 *
926 (mtime_t)p_sys->i_time /
927 (mtime_t)p_sys->i_timescale;
933 i64 = (int64_t)va_arg( args, int64_t );
934 return Seek( p_demux, i64 );
936 case DEMUX_GET_LENGTH:
937 pi64 = (int64_t*)va_arg( args, int64_t * );
938 if( p_sys->i_timescale > 0 )
940 *pi64 = (mtime_t)1000000 *
941 (mtime_t)p_sys->i_duration /
942 (mtime_t)p_sys->i_timescale;
948 pf = (double*)va_arg( args, double* );
954 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
955 MP4_Box_t *p_0xa9xxx;
957 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
960 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
967 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
968 p_0xa9xxx = p_0xa9xxx->p_next )
971 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
974 /* FIXME FIXME: should convert from whatever the character
975 * encoding of MP4 meta data is to UTF-8. */
976 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
977 if( psz_utf ) { EnsureUTF8( psz_utf ); \
978 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
980 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
981 switch( p_0xa9xxx->i_type )
983 case ATOM_0xa9nam: /* Full name */
984 SET( vlc_meta_SetTitle );
987 SET( vlc_meta_SetArtist );
990 SET( vlc_meta_SetArtist );
993 SET( vlc_meta_SetCopyright );
995 case ATOM_0xa9day: /* Creation Date */
996 SET( vlc_meta_SetDate );
998 case ATOM_0xa9des: /* Description */
999 SET( vlc_meta_SetDescription );
1001 case ATOM_0xa9gen: /* Genre */
1002 SET( vlc_meta_SetGenre );
1006 if( p_0xa9xxx->data.p_gnre->i_genre <= NUM_GENRES )
1007 vlc_meta_SetGenre( p_meta, ppsz_genres[p_0xa9xxx->data.p_gnre->i_genre - 1] );
1010 case ATOM_0xa9alb: /* Album */
1011 SET( vlc_meta_SetAlbum );
1014 case ATOM_0xa9trk: /* Track */
1015 SET( vlc_meta_SetTrackNum );
1020 snprintf( psz_trck, sizeof( psz_trck ), "%i",
1021 p_0xa9xxx->data.p_trkn->i_track_number );
1022 vlc_meta_SetTrackNum( p_meta, psz_trck );
1023 snprintf( psz_trck, sizeof( psz_trck ), "%i",
1024 p_0xa9xxx->data.p_trkn->i_track_total );
1025 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, psz_trck );
1028 case ATOM_0xa9cmt: /* Commment */
1029 SET( vlc_meta_SetDescription );
1032 case ATOM_0xa9url: /* URL */
1033 SET( vlc_meta_SetURL );
1036 case ATOM_0xa9too: /* Encoder Tool */
1037 case ATOM_0xa9enc: /* Encoded By */
1038 SET( vlc_meta_SetEncodedBy );
1042 SET( vlc_meta_SetPublisher );
1049 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
1051 { ATOM_0xa9wrt, N_("Writer") },
1052 { ATOM_0xa9com, N_("Composer") },
1053 { ATOM_0xa9prd, N_("Producer") },
1054 { ATOM_0xa9inf, N_("Information") },
1055 { ATOM_0xa9dir, N_("Director") },
1056 { ATOM_0xa9dis, N_("Disclaimer") },
1057 { ATOM_0xa9req, N_("Requirements") },
1058 { ATOM_0xa9fmt, N_("Original Format") },
1059 { ATOM_0xa9dsa, N_("Display Source As") },
1060 { ATOM_0xa9hst, N_("Host Computer") },
1061 { ATOM_0xa9prf, N_("Performers") },
1062 { ATOM_0xa9ope, N_("Original Performer") },
1063 { ATOM_0xa9src, N_("Providers Source Content") },
1064 { ATOM_0xa9wrn, N_("Warning") },
1065 { ATOM_0xa9swr, N_("Software") },
1066 { ATOM_0xa9lyr, N_("Lyrics") },
1067 { ATOM_0xa9mak, N_("Record Company") },
1068 { ATOM_0xa9mod, N_("Model") },
1069 { ATOM_0xa9PRD, N_("Product") },
1070 { ATOM_0xa9grp, N_("Grouping") },
1071 { ATOM_0xa9gen, N_("Genre") },
1072 { ATOM_0xa9st3, N_("Sub-Title") },
1073 { ATOM_0xa9arg, N_("Arranger") },
1074 { ATOM_0xa9ard, N_("Art Director") },
1075 { ATOM_0xa9cak, N_("Copyright Acknowledgement") },
1076 { ATOM_0xa9con, N_("Conductor") },
1077 { ATOM_0xa9des, N_("Song Description") },
1078 { ATOM_0xa9lnt, N_("Liner Notes") },
1079 { ATOM_0xa9phg, N_("Phonogram Rights") },
1080 { ATOM_0xa9pub, N_("Publisher") },
1081 { ATOM_0xa9sne, N_("Sound Engineer") },
1082 { ATOM_0xa9sol, N_("Soloist") },
1083 { ATOM_0xa9thx, N_("Thanks") },
1084 { ATOM_0xa9xpd, N_("Executive Producer") },
1087 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
1089 if( p_0xa9xxx->i_type == xa9typetoextrameta[i].xa9_type )
1091 char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" );
1094 EnsureUTF8( psz_utf );
1095 vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
1105 case DEMUX_GET_TITLE_INFO:
1107 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1108 int *pi_int = (int*)va_arg( args, int* );
1109 int *pi_title_offset = (int*)va_arg( args, int* );
1110 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
1112 if( !p_sys->p_title )
1113 return VLC_EGENERIC;
1116 *ppp_title = malloc( sizeof( input_title_t**) );
1117 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
1118 *pi_title_offset = 0;
1119 *pi_seekpoint_offset = 0;
1122 case DEMUX_SET_TITLE:
1124 const int i_title = (int)va_arg( args, int );
1125 if( !p_sys->p_title || i_title != 0 )
1126 return VLC_EGENERIC;
1129 case DEMUX_SET_SEEKPOINT:
1131 const int i_seekpoint = (int)va_arg( args, int );
1132 if( !p_sys->p_title )
1133 return VLC_EGENERIC;
1134 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
1137 case DEMUX_SET_NEXT_DEMUX_TIME:
1138 case DEMUX_SET_GROUP:
1139 case DEMUX_HAS_UNSUPPORTED_META:
1140 case DEMUX_GET_ATTACHMENTS:
1141 case DEMUX_GET_PTS_DELAY:
1142 case DEMUX_CAN_RECORD:
1143 return VLC_EGENERIC;
1146 msg_Warn( p_demux, "control query %u unimplemented", i_query );
1147 return VLC_EGENERIC;
1151 /*****************************************************************************
1152 * Close: frees unused data
1153 *****************************************************************************/
1154 static void Close ( vlc_object_t * p_this )
1156 demux_t * p_demux = (demux_t *)p_this;
1157 demux_sys_t *p_sys = p_demux->p_sys;
1158 unsigned int i_track;
1160 msg_Dbg( p_demux, "freeing all memory" );
1162 MP4_BoxFree( p_demux->s, p_sys->p_root );
1163 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1165 MP4_TrackDestroy( &p_sys->track[i_track] );
1167 FREENULL( p_sys->track );
1169 if( p_sys->p_title )
1170 vlc_input_title_Delete( p_sys->p_title );
1177 /****************************************************************************
1178 * Local functions, specific to vlc
1179 ****************************************************************************/
1181 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1183 demux_sys_t *p_sys = p_demux->p_sys;
1186 p_sys->p_title = vlc_input_title_New();
1187 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1189 seekpoint_t *s = vlc_seekpoint_New();
1191 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1192 EnsureUTF8( s->psz_name );
1193 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1194 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1197 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1199 demux_sys_t *p_sys = p_demux->p_sys;
1201 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1203 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1204 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( p_demux, tk );
1205 const unsigned int i_size = MP4_TrackSampleSize( tk );
1207 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1210 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1211 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1215 seekpoint_t *s = vlc_seekpoint_New();
1217 s->psz_name = strndup( &p_buffer[2], i_len );
1218 EnsureUTF8( s->psz_name );
1220 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1222 if( !p_sys->p_title )
1223 p_sys->p_title = vlc_input_title_New();
1224 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1227 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1228 tk->chunk[tk->i_chunk].i_sample_count )
1232 static void LoadChapter( demux_t *p_demux )
1234 demux_sys_t *p_sys = p_demux->p_sys;
1237 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1239 LoadChapterGpac( p_demux, p_chpl );
1241 else if( p_sys->p_tref_chap )
1243 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1246 /* Load the first subtitle track like quicktime */
1247 for( i = 0; i < p_chap->i_entry_count; i++ )
1249 for( j = 0; j < p_sys->i_tracks; j++ )
1251 mp4_track_t *tk = &p_sys->track[j];
1252 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1253 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_SUBT )
1256 if( j < p_sys->i_tracks )
1258 LoadChapterApple( p_demux, &p_sys->track[j] );
1264 /* Add duration if titles are enabled */
1265 if( p_sys->p_title )
1267 p_sys->p_title->i_length = (uint64_t)1000000 *
1268 (uint64_t)p_sys->i_duration / (uint64_t)p_sys->i_timescale;
1272 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1273 static int TrackCreateChunksIndex( demux_t *p_demux,
1274 mp4_track_t *p_demux_track )
1276 demux_sys_t *p_sys = p_demux->p_sys;
1277 if( p_sys->b_fragmented )
1280 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1283 unsigned int i_chunk;
1284 unsigned int i_index, i_last;
1286 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1287 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1288 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1290 return( VLC_EGENERIC );
1293 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1294 if( !p_demux_track->i_chunk_count )
1296 msg_Warn( p_demux, "no chunk defined" );
1297 return( VLC_EGENERIC );
1299 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1300 sizeof( mp4_chunk_t ) );
1301 if( p_demux_track->chunk == NULL )
1306 /* first we read chunk offset */
1307 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1309 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1311 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1313 ck->i_first_dts = 0;
1314 ck->p_sample_count_dts = NULL;
1315 ck->p_sample_delta_dts = NULL;
1316 ck->p_sample_count_pts = NULL;
1317 ck->p_sample_offset_pts = NULL;
1320 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1321 to be used for the sample XXX begin to 1
1322 We construct it begining at the end */
1323 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1324 i_index = p_stsc->data.p_stsc->i_entry_count;
1327 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1328 return( VLC_EGENERIC );
1333 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1334 i_chunk < i_last; i_chunk++ )
1336 if( i_chunk >= p_demux_track->i_chunk_count )
1338 msg_Warn( p_demux, "corrupted chunk table" );
1339 return VLC_EGENERIC;
1342 p_demux_track->chunk[i_chunk].i_sample_description_index =
1343 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1344 p_demux_track->chunk[i_chunk].i_sample_count =
1345 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1347 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1350 p_demux_track->chunk[0].i_sample_first = 0;
1351 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1353 p_demux_track->chunk[i_chunk].i_sample_first =
1354 p_demux_track->chunk[i_chunk-1].i_sample_first +
1355 p_demux_track->chunk[i_chunk-1].i_sample_count;
1358 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1359 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1364 static int TrackCreateSamplesIndex( demux_t *p_demux,
1365 mp4_track_t *p_demux_track )
1367 demux_sys_t *p_sys = p_demux->p_sys;
1368 if( p_sys->b_fragmented )
1372 MP4_Box_data_stsz_t *stsz;
1373 MP4_Box_data_stts_t *stts;
1374 /* TODO use also stss and stsh table for seeking */
1375 /* FIXME use edit table */
1380 int64_t i_index_sample_used;
1385 * Gives the sample size for each samples. There is also a stz2 table
1386 * (compressed form) that we need to implement TODO */
1387 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1390 /* FIXME and stz2 */
1391 msg_Warn( p_demux, "cannot find STSZ box" );
1392 return VLC_EGENERIC;
1394 stsz = p_box->data.p_stsz;
1397 * Gives mapping between sample and decoding time
1399 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1402 msg_Warn( p_demux, "cannot find STTS box" );
1403 return VLC_EGENERIC;
1405 stts = p_box->data.p_stts;
1407 /* Use stsz table to create a sample number -> sample size table */
1408 p_demux_track->i_sample_count = stsz->i_sample_count;
1409 if( stsz->i_sample_size )
1411 /* 1: all sample have the same size, so no need to construct a table */
1412 p_demux_track->i_sample_size = stsz->i_sample_size;
1413 p_demux_track->p_sample_size = NULL;
1417 /* 2: each sample can have a different size */
1418 p_demux_track->i_sample_size = 0;
1419 p_demux_track->p_sample_size =
1420 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1421 if( p_demux_track->p_sample_size == NULL )
1424 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1426 p_demux_track->p_sample_size[i_sample] =
1427 stsz->i_entry_size[i_sample];
1431 /* Use stts table to create a sample number -> dts table.
1432 * XXX: if we don't want to waste too much memory, we can't expand
1433 * the box! so each chunk will contain an "extract" of this table
1434 * for fast research (problem with raw stream where a sample is sometime
1435 * just channels*bits_per_sample/8 */
1438 i_index = 0; i_index_sample_used = 0;
1439 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1441 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1442 int64_t i_entry, i_sample_count, i;
1444 /* save first dts */
1445 ck->i_first_dts = i_next_dts;
1446 ck->i_last_dts = i_next_dts;
1448 /* count how many entries are needed for this chunk
1449 * for p_sample_delta_dts and p_sample_count_dts */
1450 i_sample_count = ck->i_sample_count;
1453 while( i_sample_count > 0 )
1455 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1456 /* don't count already used sample in this entry */
1458 i_sample_count += i_index_sample_used;
1464 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1465 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1467 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1471 i_sample_count = ck->i_sample_count;
1472 for( i = 0; i < i_entry; i++ )
1477 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1479 i_used = __MIN( i_rest, i_sample_count );
1481 i_index_sample_used += i_used;
1482 i_sample_count -= i_used;
1483 i_next_dts += i_used * stts->i_sample_delta[i_index];
1485 ck->p_sample_count_dts[i] = i_used;
1486 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1488 ck->i_last_dts = i_next_dts - ck->p_sample_delta_dts[i];
1490 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1493 i_index_sample_used = 0;
1499 * Gives the delta between decoding time (dts) and composition table (pts)
1501 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1504 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1506 msg_Warn( p_demux, "CTTS table" );
1508 /* Create pts-dts table per chunk */
1509 i_index = 0; i_index_sample_used = 0;
1510 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1512 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1513 int64_t i_entry, i_sample_count, i;
1515 /* count how many entries are needed for this chunk
1516 * for p_sample_delta_dts and p_sample_count_dts */
1517 i_sample_count = ck->i_sample_count;
1520 while( i_sample_count > 0 )
1522 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1524 /* don't count already used sample in this entry */
1526 i_sample_count += i_index_sample_used;
1532 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1533 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1534 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1538 i_sample_count = ck->i_sample_count;
1539 for( i = 0; i < i_entry; i++ )
1544 i_rest = ctts->i_sample_count[i_index] -
1545 i_index_sample_used;
1547 i_used = __MIN( i_rest, i_sample_count );
1549 i_index_sample_used += i_used;
1550 i_sample_count -= i_used;
1552 ck->p_sample_count_pts[i] = i_used;
1553 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1555 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1558 i_index_sample_used = 0;
1564 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1565 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1566 i_next_dts / p_demux_track->i_timescale );
1572 * It computes the sample rate for a video track using the given sample
1575 static void TrackGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
1576 const mp4_track_t *p_track,
1577 unsigned i_sd_index,
1583 if( p_track->i_chunk_count <= 0 )
1587 const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
1588 while( p_chunk > &p_track->chunk[0] &&
1589 p_chunk[-1].i_sample_description_index == i_sd_index )
1594 uint64_t i_sample = 0;
1595 uint64_t i_first_dts = p_chunk->i_first_dts;
1596 uint64_t i_last_dts;
1599 i_sample += p_chunk->i_sample_count;
1600 i_last_dts = p_chunk->i_last_dts;
1603 while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
1604 p_chunk->i_sample_description_index == i_sd_index );
1606 if( i_sample > 1 && i_first_dts < i_last_dts )
1607 vlc_ureduce( pi_num, pi_den,
1608 ( i_sample - 1) * p_track->i_timescale,
1609 i_last_dts - i_first_dts,
1615 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1617 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1618 unsigned int i_chunk, es_out_id_t **pp_es )
1620 demux_sys_t *p_sys = p_demux->p_sys;
1621 unsigned int i_sample_description_index;
1623 if( p_sys->b_fragmented )
1624 i_sample_description_index = 1; /* XXX */
1626 i_sample_description_index =
1627 p_track->chunk[i_chunk].i_sample_description_index;
1629 MP4_Box_t *p_sample;
1638 if( !i_sample_description_index )
1640 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1641 p_track->i_track_ID );
1642 return VLC_EGENERIC;
1645 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1646 i_sample_description_index - 1 );
1649 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1651 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1652 p_track->i_track_ID );
1653 return VLC_EGENERIC;
1656 p_track->p_sample = p_sample;
1658 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
1660 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
1662 p_sample->i_type = p_frma->data.p_frma->i_type;
1665 p_enda = MP4_BoxGet( p_sample, "wave/enda" );
1667 p_enda = MP4_BoxGet( p_sample, "enda" );
1669 p_pasp = MP4_BoxGet( p_sample, "pasp" );
1672 switch( p_track->fmt.i_cat )
1675 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1676 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1677 p_track->fmt.video.i_bits_per_pixel =
1678 p_sample->data.p_sample_vide->i_depth;
1680 /* fall on display size */
1681 if( p_track->fmt.video.i_width <= 0 )
1682 p_track->fmt.video.i_width = p_track->i_width;
1683 if( p_track->fmt.video.i_height <= 0 )
1684 p_track->fmt.video.i_height = p_track->i_height;
1686 /* Find out apect ratio from display size */
1687 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1688 /* Work-around buggy muxed files */
1689 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1691 p_track->fmt.video.i_sar_num = p_track->i_width * p_track->fmt.video.i_height;
1692 p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
1694 if( p_pasp && p_pasp->data.p_pasp->i_horizontal_spacing > 0 &&
1695 p_pasp->data.p_pasp->i_vertical_spacing > 0 )
1697 p_track->fmt.video.i_sar_num = p_pasp->data.p_pasp->i_horizontal_spacing;
1698 p_track->fmt.video.i_sar_den = p_pasp->data.p_pasp->i_vertical_spacing;
1701 /* Support for cropping (eg. in H263 files) */
1702 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1703 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1706 TrackGetESSampleRate( &p_track->fmt.video.i_frame_rate,
1707 &p_track->fmt.video.i_frame_rate_base,
1708 p_track, i_sample_description_index, i_chunk );
1709 p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
1710 (float)p_track->fmt.video.i_frame_rate_base;
1713 switch( (int)p_track->f_rotation ) {
1715 p_track->fmt.video.orientation = ORIENT_ROTATED_90;
1718 p_track->fmt.video.orientation = ORIENT_ROTATED_180;
1721 p_track->fmt.video.orientation = ORIENT_ROTATED_270;
1728 p_track->fmt.audio.i_channels =
1729 p_sample->data.p_sample_soun->i_channelcount;
1730 p_track->fmt.audio.i_rate =
1731 p_sample->data.p_sample_soun->i_sampleratehi;
1732 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1733 p_sample->data.p_sample_soun->i_sampleratehi *
1734 p_sample->data.p_sample_soun->i_samplesize;
1735 p_track->fmt.audio.i_bitspersample =
1736 p_sample->data.p_sample_soun->i_samplesize;
1738 if( ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1740 MP4_Box_data_sample_soun_t *p_soun;
1741 p_soun = p_sample->data.p_sample_soun;
1743 if( p_soun->i_qt_version == 0 )
1745 switch( p_sample->i_type )
1747 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1748 p_soun->i_qt_version = 1;
1749 p_soun->i_sample_per_packet = 64;
1750 p_soun->i_bytes_per_packet = 34;
1751 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1752 p_soun->i_bytes_per_sample = 2;
1754 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1755 p_soun->i_qt_version = 1;
1756 p_soun->i_sample_per_packet = 6;
1757 p_soun->i_bytes_per_packet = 2;
1758 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1759 p_soun->i_bytes_per_sample = 2;
1761 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1762 p_soun->i_qt_version = 1;
1763 p_soun->i_sample_per_packet = 12;
1764 p_soun->i_bytes_per_packet = 2;
1765 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1766 p_soun->i_bytes_per_sample = 2;
1768 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1769 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1770 p_soun->i_samplesize = 8;
1771 p_track->i_sample_size = p_soun->i_channelcount;
1773 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1774 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1775 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1776 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1777 /* What would be the fun if you could trust the .mov */
1778 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1785 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1787 p_soun->i_qt_version = 0;
1790 else if( p_sample->data.p_sample_soun->i_qt_version == 1 )
1792 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1794 switch( p_sample->i_type )
1796 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1797 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1799 if( p_track->i_sample_size > 1 )
1800 p_soun->i_qt_version = 0;
1803 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1804 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1805 case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
1806 p_soun->i_qt_version = 0;
1813 if( p_track->i_sample_size != 0 &&
1814 p_sample->data.p_sample_soun->i_qt_version == 1 &&
1815 p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1817 msg_Err( p_demux, "Invalid sample per packet value for qt_version 1. Broken muxer!" );
1818 p_sample->data.p_sample_soun->i_qt_version = 0;
1827 /* It's a little ugly but .. there are special cases */
1828 switch( p_sample->i_type )
1830 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1831 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1833 p_track->fmt.i_codec = VLC_CODEC_MPGA;
1836 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1838 MP4_Box_t *p_dac3_box = MP4_BoxGet( p_sample, "dac3", 0 );
1840 p_track->fmt.i_codec = VLC_CODEC_A52;
1843 static const int pi_bitrate[] = {
1850 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
1851 p_track->fmt.audio.i_channels = 0;
1852 p_track->fmt.i_bitrate = 0;
1853 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
1854 p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
1855 p_track->fmt.audio.i_bitspersample = 0;
1859 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1861 p_track->fmt.i_codec = VLC_CODEC_EAC3;
1865 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1866 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1868 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1870 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1871 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1873 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1875 /* Buggy files workaround */
1876 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1877 p_sample->data.p_sample_soun->i_sampleratehi) )
1879 MP4_Box_data_sample_soun_t *p_soun =
1880 p_sample->data.p_sample_soun;
1882 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1883 "(%u), making both equal (report any problem).",
1884 p_track->i_timescale, p_soun->i_sampleratehi );
1886 if( p_soun->i_sampleratehi )
1887 p_track->i_timescale = p_soun->i_sampleratehi;
1889 p_soun->i_sampleratehi = p_track->i_timescale;
1894 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1895 p_track->fmt.i_codec = VLC_CODEC_H263;
1898 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1899 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1900 p_track->fmt.i_codec = VLC_CODEC_SUBT;
1901 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1902 /* FIXME UTF-8 doesn't work here ? */
1903 if( p_track->b_mac_encoding )
1904 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1906 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1909 case VLC_FOURCC('y','v','1','2'):
1910 p_track->fmt.i_codec = VLC_CODEC_YV12;
1912 case VLC_FOURCC('y','u','v','2'):
1913 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1916 case VLC_FOURCC('i','n','2','4'):
1917 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1918 VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
1920 case VLC_FOURCC('f','l','3','2'):
1921 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1922 VLC_CODEC_F32L : VLC_CODEC_F32B;
1924 case VLC_FOURCC('f','l','6','4'):
1925 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1926 VLC_CODEC_F64L : VLC_CODEC_F64B;
1928 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
1930 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1931 if( p_soun->i_qt_version == 2 &&
1932 p_soun->i_qt_description > 20 + 28 )
1939 static const struct {
1943 vlc_fourcc_t i_codec;
1945 { 0x01, 0x03, 32, VLC_CODEC_F32L },
1946 { 0x01, 0x03, 64, VLC_CODEC_F64L },
1947 { 0x01|0x02, 0x03, 32, VLC_CODEC_F32B },
1948 { 0x01|0x02, 0x03, 64, VLC_CODEC_F64B },
1950 { 0x00, 0x05, 8, VLC_CODEC_U8 },
1951 { 0x00| 0x04, 0x05, 8, VLC_CODEC_S8 },
1953 { 0x00, 0x07, 16, VLC_CODEC_U16L },
1954 { 0x00|0x02, 0x07, 16, VLC_CODEC_U16B },
1955 { 0x00 |0x04, 0x07, 16, VLC_CODEC_S16L },
1956 { 0x00|0x02|0x04, 0x07, 16, VLC_CODEC_S16B },
1958 { 0x00, 0x07, 24, VLC_CODEC_U24L },
1959 { 0x00|0x02, 0x07, 24, VLC_CODEC_U24B },
1960 { 0x00 |0x04, 0x07, 24, VLC_CODEC_S24L },
1961 { 0x00|0x02|0x04, 0x07, 24, VLC_CODEC_S24B },
1963 { 0x00, 0x07, 32, VLC_CODEC_U32L },
1964 { 0x00|0x02, 0x07, 32, VLC_CODEC_U32B },
1965 { 0x00 |0x04, 0x07, 32, VLC_CODEC_S32L },
1966 { 0x00|0x02|0x04, 0x07, 32, VLC_CODEC_S32B },
1970 uint32_t i_bits = GetDWBE(&p_soun->p_qt_description[20 + 20]);
1971 uint32_t i_flags = GetDWBE(&p_soun->p_qt_description[20 + 24]);
1973 for( int i = 0; p_formats[i].i_codec; i++ )
1975 if( p_formats[i].i_bits == i_bits &&
1976 (i_flags & p_formats[i].i_mask) == p_formats[i].i_flags )
1978 p_track->fmt.i_codec = p_formats[i].i_codec;
1979 p_track->fmt.audio.i_bitspersample = i_bits;
1980 p_track->fmt.audio.i_blockalign = p_soun->i_channelcount * i_bits / 8;
1981 p_track->i_sample_size = p_track->fmt.audio.i_blockalign;
1983 p_soun->i_qt_version = 0;
1991 p_track->fmt.i_codec = p_sample->i_type;
1995 /* now see if esds is present and if so create a data packet
1996 with decoder_specific_info */
1997 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1998 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1999 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
2000 ( p_esds->data.p_esds )&&
2003 /* First update information based on i_objectTypeIndication */
2004 switch( p_decconfig->i_objectTypeIndication )
2006 case( 0x20 ): /* MPEG4 VIDEO */
2007 p_track->fmt.i_codec = VLC_CODEC_MP4V;
2009 case( 0x21 ): /* H.264 */
2010 p_track->fmt.i_codec = VLC_CODEC_H264;
2013 p_track->fmt.i_codec = VLC_CODEC_MP4A;
2014 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
2015 p_decconfig->p_decoder_specific_info[0] == 0xF8 &&
2016 (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
2018 p_track->fmt.i_codec = VLC_CODEC_ALS;
2026 case( 0x65): /* MPEG2 video */
2027 p_track->fmt.i_codec = VLC_CODEC_MPGV;
2029 /* Theses are MPEG2-AAC */
2030 case( 0x66): /* main profile */
2031 case( 0x67): /* Low complexity profile */
2032 case( 0x68): /* Scaleable Sampling rate profile */
2033 p_track->fmt.i_codec = VLC_CODEC_MP4A;
2035 /* True MPEG 2 audio */
2037 p_track->fmt.i_codec = VLC_CODEC_MPGA;
2039 case( 0x6a): /* MPEG1 video */
2040 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2042 case( 0x6b): /* MPEG1 audio */
2043 p_track->fmt.i_codec = VLC_CODEC_MPGA;
2045 case( 0x6c ): /* jpeg */
2046 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
2048 case( 0x6d ): /* png */
2049 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
2051 case( 0x6e ): /* jpeg2000 */
2052 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
2054 case( 0xa3 ): /* vc1 */
2055 p_track->fmt.i_codec = VLC_CODEC_VC1;
2058 p_track->fmt.i_codec = VLC_CODEC_DIRAC;
2061 p_track->fmt.i_codec = VLC_CODEC_A52;
2064 p_track->fmt.i_codec = VLC_CODEC_EAC3;
2066 case( 0xa9 ): /* dts */
2067 case( 0xaa ): /* DTS-HD HRA */
2068 case( 0xab ): /* DTS-HD Master Audio */
2069 p_track->fmt.i_codec = VLC_CODEC_DTS;
2072 p_track->fmt.i_codec = VLC_CODEC_VORBIS;
2076 case( 0xe0 ): /* NeroDigital: dvd subs */
2077 if( p_track->fmt.i_cat == SPU_ES )
2079 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
2080 if( p_track->i_width > 0 )
2081 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
2082 if( p_track->i_height > 0 )
2083 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
2086 case( 0xe1 ): /* QCelp for 3gp */
2087 if( p_track->fmt.i_cat == AUDIO_ES )
2089 p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
2095 /* Unknown entry, but don't touch i_fourcc */
2097 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
2098 p_decconfig->i_objectTypeIndication,
2099 p_track->i_track_ID );
2102 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
2103 if( p_track->fmt.i_extra > 0 )
2105 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2106 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
2107 p_track->fmt.i_extra );
2112 switch( p_sample->i_type )
2114 /* qt decoder, send the complete chunk */
2115 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
2116 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
2117 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
2118 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
2119 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
2120 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
2121 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
2122 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
2123 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
2124 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
2125 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
2126 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
2127 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2129 /* qt decoder, send the complete chunk */
2130 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
2131 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
2132 case VLC_FOURCC( 'V', 'P', '3', '1' ):
2133 case VLC_FOURCC( '3', 'I', 'V', '1' ):
2134 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
2135 p_track->fmt.i_extra =
2136 p_sample->data.p_sample_vide->i_qt_image_description;
2137 if( p_track->fmt.i_extra > 0 )
2139 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2140 memcpy( p_track->fmt.p_extra,
2141 p_sample->data.p_sample_vide->p_qt_image_description,
2142 p_track->fmt.i_extra);
2146 case VLC_CODEC_AMR_NB:
2147 p_track->fmt.audio.i_rate = 8000;
2149 case VLC_CODEC_AMR_WB:
2150 p_track->fmt.audio.i_rate = 16000;
2152 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
2153 case VLC_CODEC_QDM2:
2154 case VLC_CODEC_ALAC:
2155 p_track->fmt.i_extra =
2156 p_sample->data.p_sample_soun->i_qt_description;
2157 if( p_track->fmt.i_extra > 0 )
2159 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2160 memcpy( p_track->fmt.p_extra,
2161 p_sample->data.p_sample_soun->p_qt_description,
2162 p_track->fmt.i_extra);
2164 if( p_track->fmt.i_extra >= 56 && p_sample->i_type == VLC_CODEC_ALAC )
2166 p_track->fmt.audio.i_channels = *((uint8_t*)p_track->fmt.p_extra + 41);
2167 p_track->fmt.audio.i_rate = GetDWBE((uint8_t*)p_track->fmt.p_extra + 52);
2171 case VLC_FOURCC( 'v', 'c', '-', '1' ):
2173 MP4_Box_t *p_dvc1 = MP4_BoxGet( p_sample, "dvc1" );
2176 p_track->fmt.i_extra = p_dvc1->data.p_dvc1->i_vc1;
2177 if( p_track->fmt.i_extra > 0 )
2179 p_track->fmt.p_extra = malloc( p_dvc1->data.p_dvc1->i_vc1 );
2180 memcpy( p_track->fmt.p_extra, p_dvc1->data.p_dvc1->p_vc1,
2181 p_track->fmt.i_extra );
2186 msg_Err( p_demux, "missing dvc1" );
2191 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
2192 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
2194 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
2198 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
2199 if( p_track->fmt.i_extra > 0 )
2201 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
2202 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
2203 p_track->fmt.i_extra );
2208 msg_Err( p_demux, "missing avcC" );
2213 case VLC_CODEC_ADPCM_MS:
2214 case VLC_CODEC_ADPCM_IMA_WAV:
2215 case VLC_CODEC_QCELP:
2216 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
2220 msg_Dbg( p_demux, "Unrecognized FourCC %4.4s", (char *)&p_sample->i_type );
2228 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2233 /* given a time it return sample/chunk
2234 * it also update elst field of the track
2236 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2237 int64_t i_start, uint32_t *pi_chunk,
2238 uint32_t *pi_sample )
2240 demux_sys_t *p_sys = p_demux->p_sys;
2241 MP4_Box_t *p_box_stss;
2243 unsigned int i_sample;
2244 unsigned int i_chunk;
2247 /* FIXME see if it's needed to check p_track->i_chunk_count */
2248 if( p_track->i_chunk_count == 0 )
2249 return( VLC_EGENERIC );
2251 /* handle elst (find the correct one) */
2252 MP4_TrackSetELST( p_demux, p_track, i_start );
2253 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2255 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2256 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
2258 /* now calculate i_start for this elst */
2260 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
2268 /* to track time scale */
2269 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2270 /* add elst offset */
2271 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2272 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2273 elst->i_media_time[p_track->i_elst] > 0 )
2275 i_start += elst->i_media_time[p_track->i_elst];
2278 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2279 "ms (track)", p_track->i_elst,
2280 i_mvt * 1000 / p_sys->i_timescale,
2281 i_start * 1000 / p_track->i_timescale );
2285 /* convert absolute time to in timescale unit */
2286 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2289 /* we start from sample 0/chunk 0, hope it won't take too much time */
2290 /* *** find good chunk *** */
2291 for( i_chunk = 0; ; i_chunk++ )
2293 if( i_chunk + 1 >= p_track->i_chunk_count )
2295 /* at the end and can't check if i_start in this chunk,
2296 it will be check while searching i_sample */
2297 i_chunk = p_track->i_chunk_count - 1;
2301 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2302 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
2308 /* *** find sample in the chunk *** */
2309 i_sample = p_track->chunk[i_chunk].i_sample_first;
2310 i_dts = p_track->chunk[i_chunk].i_first_dts;
2311 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2314 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2315 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2318 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2319 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2321 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2326 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2330 i_sample += ( i_start - i_dts ) /
2331 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2336 if( i_sample >= p_track->i_sample_count )
2338 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2339 "(seeking too far) chunk=%d sample=%d",
2340 p_track->i_track_ID, i_chunk, i_sample );
2341 return( VLC_EGENERIC );
2345 /* *** Try to find nearest sync points *** */
2346 if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2348 MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2349 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2350 p_track->i_track_ID );
2351 for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2353 if( i_index >= p_stss->i_entry_count - 1 ||
2354 i_sample < p_stss->i_sample_number[i_index+1] )
2356 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2357 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
2358 i_sample, i_sync_sample );
2360 if( i_sync_sample <= i_sample )
2362 while( i_chunk > 0 &&
2363 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2368 while( i_chunk < p_track->i_chunk_count - 1 &&
2369 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2370 p_track->chunk[i_chunk].i_sample_count )
2373 i_sample = i_sync_sample;
2380 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2381 "Sample Box (stss)", p_track->i_track_ID );
2384 *pi_chunk = i_chunk;
2385 *pi_sample = i_sample;
2390 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2391 unsigned int i_chunk, unsigned int i_sample )
2393 bool b_reselect = false;
2395 /* now see if actual es is ok */
2396 if( p_track->i_chunk >= p_track->i_chunk_count ||
2397 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2398 p_track->chunk[i_chunk].i_sample_description_index )
2400 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2401 p_track->i_track_ID );
2403 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2404 p_track->p_es, &b_reselect );
2406 es_out_Del( p_demux->out, p_track->p_es );
2408 p_track->p_es = NULL;
2410 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2412 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2413 p_track->i_track_ID );
2415 p_track->b_ok = false;
2416 p_track->b_selected = false;
2417 return VLC_EGENERIC;
2421 /* select again the new decoder */
2424 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2427 p_track->i_chunk = i_chunk;
2428 p_track->i_sample = i_sample;
2430 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2433 /****************************************************************************
2435 ****************************************************************************
2436 * Parse track information and create all needed data to run a track
2437 * If it succeed b_ok is set to 1 else to 0
2438 ****************************************************************************/
2439 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2440 MP4_Box_t *p_box_trak,
2441 bool b_force_enable )
2443 demux_sys_t *p_sys = p_demux->p_sys;
2445 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2446 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2458 /* hint track unsupported */
2460 /* set default value (-> track unusable) */
2461 p_track->b_ok = false;
2462 p_track->b_enable = false;
2463 p_track->b_selected = false;
2464 p_track->b_chapter = false;
2465 p_track->b_mac_encoding = false;
2467 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2474 /* do we launch this track by default ? */
2476 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2477 if( !p_track->b_enable )
2478 p_track->fmt.i_priority = -1;
2480 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2482 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2483 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2484 p_track->f_rotation = p_tkhd->data.p_tkhd->f_rotation;
2488 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2491 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2492 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2494 if( ( !p_mdhd )||( !p_hdlr ) )
2499 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2500 if( !p_track->i_timescale )
2503 if( p_mdhd->data.p_mdhd->i_language_code < 0x800 )
2505 /* We can convert i_language_code into iso 639 code,
2507 strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2508 p_track->b_mac_encoding = true;
2512 for( unsigned i = 0; i < 3; i++ )
2513 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2517 switch( p_hdlr->data.p_hdlr->i_handler_type )
2520 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2524 p_track->fmt.i_cat = AUDIO_ES;
2528 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2532 p_track->fmt.i_cat = VIDEO_ES;
2539 p_track->fmt.i_cat = SPU_ES;
2546 p_track->i_elst = 0;
2547 p_track->i_elst_time = 0;
2548 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2550 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2553 msg_Warn( p_demux, "elst box found" );
2554 for( i = 0; i < elst->i_entry_count; i++ )
2556 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2557 "ms) rate=%d.%d", i,
2558 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2559 elst->i_media_time[i] >= 0 ?
2560 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2562 elst->i_media_rate_integer[i],
2563 elst->i_media_rate_fraction[i] );
2570 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2572 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2573 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2579 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2581 p_track->fmt.psz_language = strdup( language );
2584 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2587 MP4_Box_t *p_box_iter;
2588 for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2589 p_box_iter = p_box_iter->p_next )
2591 switch( p_box_iter->i_type )
2594 p_track->fmt.psz_description =
2595 strdup( p_box_iter->data.p_0xa9xxx->psz_text );
2598 p_track->fmt.psz_description =
2599 strdup( p_box_iter->data.p_name->psz_text );
2605 /* Create chunk index table and sample index table */
2606 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2607 TrackCreateSamplesIndex( p_demux, p_track ) )
2609 return; /* cannot create chunks index */
2612 p_track->i_chunk = 0;
2613 p_track->i_sample = 0;
2615 /* Mark chapter only track */
2616 if( p_sys->p_tref_chap )
2618 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2621 for( i = 0; i < p_chap->i_entry_count; i++ )
2623 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2625 p_track->b_chapter = true;
2626 p_track->b_enable = false;
2633 if( b_force_enable &&
2634 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2636 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2637 p_track->i_track_ID );
2638 p_track->b_enable = true;
2639 p_track->fmt.i_priority = 0;
2642 p_track->p_es = NULL;
2643 if( TrackCreateES( p_demux,
2644 p_track, p_track->i_chunk,
2645 p_track->b_chapter ? NULL : &p_track->p_es ) )
2647 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2648 p_track->i_track_ID );
2651 p_track->b_ok = true;
2655 for( i = 0; i < p_track->i_chunk_count; i++ )
2657 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2658 i, p_track->chunk[i].i_sample_count,
2659 p_track->chunk[i].i_first_dts );
2666 /****************************************************************************
2668 ****************************************************************************
2669 * Destroy a track created by MP4_TrackCreate.
2670 ****************************************************************************/
2671 static void MP4_TrackDestroy( mp4_track_t *p_track )
2673 unsigned int i_chunk;
2675 p_track->b_ok = false;
2676 p_track->b_enable = false;
2677 p_track->b_selected = false;
2679 es_format_Clean( &p_track->fmt );
2681 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2683 if( p_track->chunk )
2685 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2686 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2688 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2689 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2692 FREENULL( p_track->chunk );
2694 if( !p_track->i_sample_size )
2696 FREENULL( p_track->p_sample_size );
2700 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2703 if( !p_track->b_ok || p_track->b_chapter )
2705 return VLC_EGENERIC;
2708 if( p_track->b_selected )
2710 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2711 p_track->i_track_ID );
2715 return MP4_TrackSeek( p_demux, p_track, i_start );
2718 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2720 if( !p_track->b_ok || p_track->b_chapter )
2725 if( !p_track->b_selected )
2727 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2728 p_track->i_track_ID );
2733 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2734 p_track->p_es, false );
2737 p_track->b_selected = false;
2740 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2746 if( !p_track->b_ok || p_track->b_chapter )
2747 return VLC_EGENERIC;
2749 p_track->b_selected = false;
2751 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2752 &i_chunk, &i_sample ) )
2754 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2755 p_track->i_track_ID );
2756 return VLC_EGENERIC;
2759 p_track->b_selected = true;
2761 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2762 p_track->b_selected = true;
2764 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2769 * 3 types: for audio
2772 #define QT_V0_MAX_SAMPLES 1024
2773 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2776 MP4_Box_data_sample_soun_t *p_soun;
2778 if( p_track->i_sample_size == 0 )
2780 /* most simple case */
2781 return p_track->p_sample_size[p_track->i_sample];
2783 if( p_track->fmt.i_cat != AUDIO_ES )
2785 return p_track->i_sample_size;
2788 p_soun = p_track->p_sample->data.p_sample_soun;
2790 if( p_soun->i_qt_version == 1 )
2792 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
2793 if( p_track->fmt.audio.i_blockalign > 1 )
2794 i_samples = p_soun->i_sample_per_packet;
2796 i_size = i_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2798 else if( p_track->i_sample_size > 256 )
2800 /* We do that so we don't read too much data
2801 * (in this case we are likely dealing with compressed data) */
2802 i_size = p_track->i_sample_size;
2806 /* Read a bunch of samples at once */
2807 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2808 ( p_track->i_sample -
2809 p_track->chunk[p_track->i_chunk].i_sample_first );
2811 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2812 i_size = i_samples * p_track->i_sample_size;
2815 //fprintf( stderr, "size=%d\n", i_size );
2819 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2821 unsigned int i_sample;
2824 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2826 if( p_track->i_sample_size )
2828 MP4_Box_data_sample_soun_t *p_soun =
2829 p_track->p_sample->data.p_sample_soun;
2831 if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
2833 i_pos += ( p_track->i_sample -
2834 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2835 p_track->i_sample_size;
2839 /* we read chunk by chunk unless a blockalign is requested */
2840 if( p_track->fmt.audio.i_blockalign > 1 )
2841 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
2842 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2847 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2848 i_sample < p_track->i_sample; i_sample++ )
2850 i_pos += p_track->p_sample_size[i_sample];
2857 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2859 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2861 MP4_Box_data_sample_soun_t *p_soun;
2863 p_soun = p_track->p_sample->data.p_sample_soun;
2865 if( p_soun->i_qt_version == 1 )
2867 /* we read chunk by chunk unless a blockalign is requested */
2868 if( p_track->fmt.audio.i_blockalign > 1 )
2869 p_track->i_sample += p_soun->i_sample_per_packet;
2871 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
2873 else if( p_track->i_sample_size > 256 )
2875 /* We do that so we don't read too much data
2876 * (in this case we are likely dealing with compressed data) */
2877 p_track->i_sample += 1;
2882 p_track->i_sample += QT_V0_MAX_SAMPLES;
2883 if( p_track->i_sample >
2884 p_track->chunk[p_track->i_chunk].i_sample_first +
2885 p_track->chunk[p_track->i_chunk].i_sample_count )
2888 p_track->chunk[p_track->i_chunk].i_sample_first +
2889 p_track->chunk[p_track->i_chunk].i_sample_count;
2895 p_track->i_sample++;
2898 if( p_track->i_sample >= p_track->i_sample_count )
2899 return VLC_EGENERIC;
2901 /* Have we changed chunk ? */
2902 if( p_track->i_sample >=
2903 p_track->chunk[p_track->i_chunk].i_sample_first +
2904 p_track->chunk[p_track->i_chunk].i_sample_count )
2906 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2907 p_track->i_sample ) )
2909 msg_Warn( p_demux, "track[0x%x] will be disabled "
2910 "(cannot restart decoder)", p_track->i_track_ID );
2911 MP4_TrackUnselect( p_demux, p_track );
2912 return VLC_EGENERIC;
2916 /* Have we changed elst */
2917 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2919 demux_sys_t *p_sys = p_demux->p_sys;
2920 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2921 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2922 p_sys->i_timescale / (int64_t)1000000;
2924 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2925 i_mvt >= p_track->i_elst_time +
2926 elst->i_segment_duration[p_track->i_elst] )
2928 MP4_TrackSetELST( p_demux, p_track,
2929 MP4_TrackGetDTS( p_demux, p_track ) );
2936 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2939 demux_sys_t *p_sys = p_demux->p_sys;
2940 int i_elst_last = tk->i_elst;
2942 /* handle elst (find the correct one) */
2944 tk->i_elst_time = 0;
2945 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2947 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2948 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2950 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2952 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2954 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2958 tk->i_elst_time += i_dur;
2961 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2963 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2964 tk->i_elst = elst->i_entry_count - 1;
2965 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2968 if( elst->i_media_time[tk->i_elst] < 0 )
2971 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2974 if( i_elst_last != tk->i_elst )
2976 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2981 static const char *MP4_ConvertMacCode( uint16_t i_code )
2983 static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
2984 { "en", 0 }, { "fr", 1 }, { "de", 2 }, { "it", 3 }, { "nl", 4 },
2985 { "sv", 5 }, { "es", 6 }, { "da", 7 }, { "pt", 8 }, { "no", 9 },
2986 { "he", 10 }, { "ja", 11 }, { "ar", 12 }, { "fi", 13 }, { "el", 14 },
2987 { "is", 15 }, { "mt", 16 }, { "tr", 17 }, { "hr", 18 }, { "zh", 19 },
2988 { "ur", 20 }, { "hi", 21 }, { "th", 22 }, { "ko", 23 }, { "lt", 24 },
2989 { "pl", 25 }, { "hu", 26 }, { "et", 27 }, { "lv", 28 }, //{ "??", 29 },
2990 { "fo", 30 }, { "fa", 31 }, { "ru", 32 }, { "zh", 33 }, { "nl", 34 },
2991 { "ga", 35 }, { "sq", 36 }, { "ro", 37 }, { "cs", 38 }, { "sk", 39 },
2992 { "sl", 40 }, { "yi", 41 }, { "sr", 42 }, { "mk", 43 }, { "bg", 44 },
2993 { "uk", 45 }, { "be", 46 }, { "uz", 47 }, { "az", 48 }, { "kk", 48 },
2994 { "az", 50 }, { "hy", 51 }, { "ka", 52 }, { "mo", 53 }, { "ky", 54 },
2995 { "tg", 55 }, { "tk", 56 }, { "mn", 57 }, { "mn", 58 }, { "ps", 59 },
2996 { "ku", 60 }, { "ks", 61 }, { "sd", 62 }, { "bo", 63 }, { "ne", 64 },
2997 { "sa", 65 }, { "mr", 66 }, { "bn", 67 }, { "as", 68 }, { "gu", 69 },
2998 { "pa", 70 }, { "or", 71 }, { "ml", 72 }, { "kn", 73 }, { "ta", 74 },
2999 { "te", 75 }, { "si", 76 }, { "my", 77 }, { "km", 78 }, { "lo", 79 },
3000 { "vi", 80 }, { "id", 81 }, { "tl", 82 }, { "ms", 83 }, { "ms", 84 },
3001 { "am", 85 }, { "ti", 86 }, { "om", 87 }, { "so", 88 }, { "sw", 89 },
3002 { "rw", 90 }, { "rn", 91 }, { "ny", 92 }, { "mg", 93 }, { "eo", 94 },
3004 { "cy", 128 }, { "eu", 129 },
3005 { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
3006 { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
3007 { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
3008 { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
3013 for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
3015 if( p_cvt[i].i_code == i_code )
3016 return p_cvt[i].psz_iso639_1;
3021 /******************************************************************************
3022 * Here are the functions used for fragmented MP4
3023 *****************************************************************************/
3026 * It computes the sample rate for a video track using current video chunk
3028 static void ChunkGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
3029 const mp4_track_t *p_track )
3031 if( p_track->cchunk->i_last_dts == 0 )
3038 const mp4_chunk_t *p_chunk = p_track->cchunk;
3040 uint32_t i_sample = p_chunk->i_sample_count;
3041 uint64_t i_first_dts = p_chunk->i_first_dts;
3042 uint64_t i_last_dts = p_chunk->i_last_dts;
3044 if( i_sample > 1 && i_first_dts < i_last_dts )
3045 vlc_ureduce( pi_num, pi_den,
3046 ( i_sample - 1) * p_track->i_timescale,
3047 i_last_dts - i_first_dts,
3052 * Build raw avcC box (without the 8 bytes header)
3053 * \return The size of the box.
3055 static int build_raw_avcC( uint8_t **p_extra, const uint8_t *CodecPrivateData,
3056 const unsigned cpd_len )
3059 unsigned sps_len = 0, pps_len = 0;
3060 const uint32_t mark = 0x00000001;
3062 assert( CodecPrivateData[0] == 0 );
3063 assert( CodecPrivateData[1] == 0 );
3064 assert( CodecPrivateData[2] == 0 );
3065 assert( CodecPrivateData[3] == 1 );
3067 uint32_t length = cpd_len + 3;
3068 avcC = calloc( length, 1 );
3069 if( unlikely( avcC == NULL ) )
3072 uint8_t *sps = avcC + 8;
3074 uint32_t candidate = ~mark;
3075 CodecPrivateData += 4;
3076 for( unsigned i = 0; i < cpd_len - 4; i++ )
3078 sps[i] = CodecPrivateData[i];
3079 candidate = (candidate << 8) | CodecPrivateData[i];
3080 if( candidate == mark )
3088 uint8_t *pps = sps + sps_len + 3;
3089 pps_len = cpd_len - sps_len - 4 * 2;
3090 memcpy( pps, CodecPrivateData + sps_len + 4, pps_len );
3093 uint8_t AVCProfileIndication = 0x64;
3094 uint8_t profile_compatibility = 0x40;
3095 uint8_t AVCLevelIndication = 0x1f;
3096 uint8_t lengthSizeMinusOne = 0x03;
3099 avcC[1] = AVCProfileIndication;
3100 avcC[2] = profile_compatibility;
3101 avcC[3] = AVCLevelIndication;
3102 avcC[4] = 0xfc + lengthSizeMinusOne;
3104 avcC[6] = (sps_len & 0xff00)>>8;
3105 avcC[7] = sps_len & 0xff;
3107 avcC[8+sps_len] = 1;
3108 avcC[9+sps_len] = (pps_len & 0xff00) >> 8;
3109 avcC[10+sps_len] = pps_len & 0xff;
3116 * Build a mp4_track_t from a StraBox
3119 static inline int MP4_SetCodecExtraData( es_format_t *fmt, MP4_Box_data_stra_t *p_data )
3121 fmt->i_extra = p_data->cpd_len;
3122 fmt->p_extra = malloc( p_data->cpd_len );
3123 if( unlikely( !fmt->p_extra ) )
3125 memcpy( fmt->p_extra, p_data->CodecPrivateData, p_data->cpd_len );
3129 static int MP4_frg_TrackCreate( demux_t *p_demux, mp4_track_t *p_track, MP4_Box_t *p_stra )
3131 demux_sys_t *p_sys = p_demux->p_sys;
3133 MP4_Box_data_stra_t *p_data = p_stra->data.p_stra;
3135 return VLC_EGENERIC;
3137 p_track->b_ok = true;
3138 p_track->b_selected = false;
3139 p_track->i_sample_count = UINT32_MAX;
3141 p_track->i_timescale = p_sys->i_timescale;
3142 p_track->i_width = p_data->MaxWidth;
3143 p_track->i_height = p_data->MaxHeight;
3144 p_track->i_track_ID = p_data->i_track_ID;
3146 es_format_t *fmt = &p_track->fmt;
3148 return VLC_EGENERIC;
3150 es_format_Init( fmt, p_data->i_es_cat, 0 );
3152 /* Set language FIXME */
3153 fmt->psz_language = strdup( "en" );
3155 fmt->i_original_fourcc = p_data->FourCC;
3156 fmt->i_codec = vlc_fourcc_GetCodec( fmt->i_cat, p_data->FourCC );
3158 uint8_t **p_extra = (uint8_t **)&fmt->p_extra;
3159 /* See http://msdn.microsoft.com/en-us/library/ff728116%28v=vs.90%29.aspx
3160 * for MS weird use of FourCC*/
3161 switch( fmt->i_cat )
3164 if( p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', '1' ) ||
3165 p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', 'B' ) ||
3166 p_data->FourCC == VLC_FOURCC( 'H', '2', '6', '4' ) )
3168 fmt->i_extra = build_raw_avcC( p_extra,
3169 p_data->CodecPrivateData, p_data->cpd_len );
3173 ret = MP4_SetCodecExtraData( fmt, p_data );
3174 if( ret != VLC_SUCCESS )
3178 fmt->video.i_width = p_data->MaxWidth;
3179 fmt->video.i_height = p_data->MaxHeight;
3180 fmt->video.i_bits_per_pixel = 0x18;
3181 fmt->video.i_visible_width = p_data->MaxWidth;
3182 fmt->video.i_visible_height = p_data->MaxHeight;
3185 ChunkGetESSampleRate( &fmt->video.i_frame_rate,
3186 &fmt->video.i_frame_rate_base, p_track );
3188 if( fmt->video.i_frame_rate_base != 0 )
3190 p_demux->p_sys->f_fps = (float)fmt->video.i_frame_rate /
3191 (float)fmt->video.i_frame_rate_base;
3194 p_demux->p_sys->f_fps = 24;
3199 fmt->audio.i_channels = p_data->Channels;
3200 fmt->audio.i_rate = p_data->SamplingRate;
3201 fmt->audio.i_bitspersample = p_data->BitsPerSample;
3202 fmt->audio.i_blockalign = p_data->nBlockAlign;
3204 fmt->i_bitrate = p_data->Bitrate;
3206 ret = MP4_SetCodecExtraData( fmt, p_data );
3207 if( ret != VLC_SUCCESS )
3220 * Return the track identified by tid
3222 static mp4_track_t *MP4_frg_GetTrack( demux_t *p_demux, const uint16_t tid )
3224 demux_sys_t *p_sys = p_demux->p_sys;
3226 mp4_track_t *ret = NULL;
3227 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
3229 ret = &p_sys->track[i];
3232 if( ret->i_track_ID == tid )
3235 msg_Err( p_demux, "MP4_frg_GetTrack: track %"PRIu16" not found!", tid );
3239 static int FreeAndResetChunk( mp4_chunk_t *ck )
3241 free( ck->p_sample_count_dts );
3242 free( ck->p_sample_delta_dts );
3243 free( ck->p_sample_count_pts );
3244 free( ck->p_sample_offset_pts );
3245 free( ck->p_sample_size );
3246 for( uint32_t i = 0; i < ck->i_sample_count; i++ )
3247 free( ck->p_sample_data[i] );
3248 free( ck->p_sample_data );
3249 memset( ck, 0, sizeof( mp4_chunk_t ) );
3253 static void FlushChunk( demux_t *p_demux, mp4_track_t *tk )
3255 msg_Dbg( p_demux, "Flushing chunk for track id %u", tk->i_track_ID );
3256 mp4_chunk_t *ck = tk->cchunk;
3257 while( ck->i_sample < ck->i_sample_count )
3262 if( ck->p_sample_size == NULL || ck->p_sample_data == NULL )
3265 uint32_t sample_size = ck->p_sample_size[ck->i_sample];
3266 assert( sample_size > 0 );
3267 p_block = block_Alloc( sample_size );
3268 if( unlikely( !p_block ) )
3271 uint8_t *src = ck->p_sample_data[ck->i_sample];
3272 memcpy( p_block->p_buffer, src, sample_size );
3276 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
3278 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
3280 p_block->i_pts = p_block->i_dts + i_delta;
3281 else if( tk->fmt.i_cat != VIDEO_ES )
3282 p_block->i_pts = p_block->i_dts;
3284 p_block->i_pts = VLC_TS_INVALID;
3286 es_out_Send( p_demux->out, tk->p_es, p_block );
3294 * \Note If we call that function too soon,
3295 * before the track has been selected by MP4_TrackSelect
3296 * (during the first execution of Demux), then the track gets disabled
3298 static int ReInitDecoder( demux_t *p_demux, mp4_track_t *p_track )
3300 demux_sys_t *p_sys = p_demux->p_sys;
3302 uint32_t i_sample = 0;
3303 bool b_smooth = false;
3304 MP4_Box_t *p_stra = NULL, *p_trak = NULL;
3306 if( !CmpUUID( &p_sys->p_root->p_first->i_uuid, &SmooBoxUUID ) )
3311 p_stra = MP4_BoxGet( p_sys->p_root, "uuid/uuid[0]" );
3312 if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
3313 return VLC_EGENERIC;
3317 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[0]" );
3319 return VLC_EGENERIC;
3322 i_sample = p_track->i_sample;
3323 es_out_Del( p_demux->out, p_track->p_es );
3324 es_format_Clean( &p_track->fmt );
3327 MP4_frg_TrackCreate( p_demux, p_track, p_stra );
3329 MP4_TrackCreate( p_demux, p_track, p_trak, true );
3331 p_track->i_sample = i_sample;
3333 /* Temporary hack until we support track selection */
3334 p_track->b_selected = true;
3335 p_track->b_ok = true;
3336 p_track->b_enable = true;
3338 p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
3339 p_track->b_codec_need_restart = false;
3345 * This function fills a mp4_chunk_t structure from a MP4_Box_t (p_chunk).
3346 * The 'i_tk_id' argument returns the ID of the track the chunk belongs to.
3347 * \note p_chunk usually contains a 'moof' and a 'mdat', and might contain a 'sidx'.
3348 * \return VLC_SUCCESS, VLC_EGENERIC or VLC_ENOMEM.
3350 static int MP4_frg_GetChunk( demux_t *p_demux, MP4_Box_t *p_chunk, unsigned *i_tk_id )
3352 MP4_Box_t *p_sidx = MP4_BoxGet( p_chunk, "sidx" );
3353 MP4_Box_t *p_moof = MP4_BoxGet( p_chunk, "moof" );
3356 msg_Warn( p_demux, "no moof box found!" );
3357 return VLC_EGENERIC;
3360 MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
3363 msg_Warn( p_demux, "no traf box found!" );
3364 return VLC_EGENERIC;
3367 MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
3370 msg_Warn( p_demux, "no tfhd box found!" );
3371 return VLC_EGENERIC;
3374 uint32_t i_track_ID = p_tfhd->data.p_tfhd->i_track_ID;
3375 *i_tk_id = i_track_ID;
3376 assert( i_track_ID > 0 );
3377 msg_Dbg( p_demux, "GetChunk: track ID is %"PRIu32"", i_track_ID );
3379 mp4_track_t *p_track = MP4_frg_GetTrack( p_demux, i_track_ID );
3381 return VLC_EGENERIC;
3383 mp4_chunk_t *ret = p_track->cchunk;
3385 if( p_tfhd->data.p_tfhd->b_empty )
3386 msg_Warn( p_demux, "No samples in this chunk!" );
3388 /* Usually we read 100 ms of each track. However, suppose we have two tracks,
3389 * Ta and Tv (audio and video). Suppose also that Ta is the first track to be
3390 * read, i.e. we read 100 ms of Ta, then 100 ms of Tv, then 100 ms of Ta,
3391 * and so on. Finally, suppose that we get the chunks the other way around,
3392 * i.e. first a chunk of Tv, then a chunk of Ta, then a chunk of Tv, and so on.
3393 * In that case, it is very likely that at some point, Ta->cchunk or Tv->cchunk
3394 * is not emptied when MP4_frg_GetChunks is called. It is therefore necessary to
3395 * flush it, i.e. send to the decoder the samples not yet sent.
3396 * Note that all the samples to be flushed should worth less than 100 ms,
3397 * (though I did not do the formal proof) and thus this flushing mechanism
3398 * should not cause A/V sync issues, or delays or whatever.
3400 if( ret->i_sample < ret->i_sample_count )
3401 FlushChunk( p_demux, p_track );
3403 if( ret->i_sample_count )
3404 FreeAndResetChunk( ret );
3406 uint32_t default_duration = 0;
3407 if( p_tfhd->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
3408 default_duration = p_tfhd->data.p_tfhd->i_default_sample_duration;
3410 uint32_t default_size = 0;
3411 if( p_tfhd->data.p_tfhd->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
3412 default_size = p_tfhd->data.p_tfhd->i_default_sample_size;
3414 MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun");
3417 msg_Warn( p_demux, "no trun box found!" );
3418 return VLC_EGENERIC;
3420 MP4_Box_data_trun_t *p_trun_data = p_trun->data.p_trun;
3422 ret->i_sample_count = p_trun_data->i_sample_count;
3423 assert( ret->i_sample_count > 0 );
3424 ret->i_sample_description_index = 1; /* seems to be always 1, is it? */
3425 ret->i_sample_first = p_track->i_sample_first;
3426 p_track->i_sample_first += ret->i_sample_count;
3428 ret->i_first_dts = p_track->i_first_dts;
3430 /* XXX I already saw DASH content with no default_duration and no
3431 * MP4_TRUN_SAMPLE_DURATION flag, but a sidx box was present.
3432 * This chunk of code might be buggy with segments having
3433 * more than one subsegment. */
3434 if( !default_duration )
3438 MP4_Box_data_sidx_t *p_sidx_data = p_sidx->data.p_sidx;
3439 assert( p_sidx_data->i_reference_count == 1 );
3440 unsigned i_chunk_duration = p_sidx_data->p_items[0].i_subsegment_duration /
3441 p_sidx_data->i_timescale;
3442 default_duration = i_chunk_duration *
3443 p_track->i_timescale / ret->i_sample_count;
3447 msg_Dbg( p_demux, "Default sample duration is %"PRIu32, default_duration );
3449 ret->p_sample_count_dts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3450 ret->p_sample_delta_dts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3452 if( !ret->p_sample_count_dts || !ret->p_sample_delta_dts )
3455 ret->p_sample_count_pts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3456 if( !ret->p_sample_count_pts )
3459 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
3461 ret->p_sample_offset_pts = calloc( ret->i_sample_count, sizeof( int32_t ) );
3462 if( !ret->p_sample_offset_pts )
3466 ret->p_sample_size = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3467 if( !ret->p_sample_size )
3470 ret->p_sample_data = calloc( ret->i_sample_count, sizeof( uint8_t * ) );
3471 if( !ret->p_sample_data )
3474 uint32_t dur = 0, len;
3475 uint32_t chunk_duration = 0, chunk_size = 0;
3477 /* Skip header of mdat */
3478 stream_Read( p_demux->s, NULL, 8 );
3480 for( uint32_t i = 0; i < ret->i_sample_count; i++)
3482 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_DURATION )
3483 dur = p_trun_data->p_samples[i].i_duration;
3485 dur = default_duration;
3486 ret->p_sample_delta_dts[i] = dur;
3487 chunk_duration += dur;
3489 ret->p_sample_count_dts[i] = ret->p_sample_count_pts[i] = 1;
3491 if( ret->p_sample_offset_pts )
3493 ret->p_sample_offset_pts[i] =
3494 p_trun_data->p_samples[i].i_composition_time_offset;
3497 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_SIZE )
3498 len = ret->p_sample_size[i] = p_trun_data->p_samples[i].i_size;
3500 len = ret->p_sample_size[i] = default_size;
3502 ret->p_sample_data[i] = malloc( len );
3503 if( ret->p_sample_data[i] == NULL )
3505 int read = stream_Read( p_demux->s, ret->p_sample_data[i], len );
3506 if( read < (int)len )
3507 return VLC_EGENERIC;
3510 ret->i_last_dts = ret->i_first_dts + chunk_duration - dur;
3511 p_track->i_first_dts = chunk_duration + ret->i_first_dts;
3513 if( p_track->b_codec_need_restart &&
3514 p_track->fmt.i_cat == VIDEO_ES )
3515 ReInitDecoder( p_demux, p_track );
3517 p_track->b_has_non_empty_cchunk = true;
3522 * Get the next chunk of the track identified by i_tk_id.
3523 * \Note We don't want to seek all the time, so if the first chunk given by the
3524 * input method doesn't belong to the right track, we don't throw it away,
3525 * and so, in general, this function fetch more than one chunk.
3526 * Not to mention that a new init segment might be put everywhere
3527 * between two chunks by the input method.
3529 static int MP4_frg_GetChunks( demux_t *p_demux, const unsigned i_tk_id )
3531 demux_sys_t *p_sys = p_demux->p_sys;
3532 mp4_track_t *p_track;
3534 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
3536 MP4_Box_t *p_chunk = MP4_BoxGetNextChunk( p_demux->s );
3538 return VLC_EGENERIC;
3540 uint32_t i_type = p_chunk->p_first->i_type;
3542 if( i_type == ATOM_uuid || i_type == ATOM_ftyp )
3544 free( p_sys->p_root );
3545 p_sys->p_root = p_chunk;
3547 if( i_type == ATOM_ftyp ) /* DASH */
3549 MP4_Box_t *p_tkhd = MP4_BoxGet( p_chunk, "/moov/trak[0]/tkhd" );
3552 msg_Warn( p_demux, "No tkhd found!" );
3554 return VLC_EGENERIC;
3556 tid = p_tkhd->data.p_tkhd->i_track_ID;
3558 else /* Smooth Streaming */
3560 assert( !CmpUUID( &p_chunk->p_first->i_uuid, &SmooBoxUUID ) );
3561 MP4_Box_t *p_stra = MP4_BoxGet( p_chunk, "/uuid/uuid[0]" );
3562 if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
3564 msg_Warn( p_demux, "No StraBox found!" );
3566 return VLC_EGENERIC;
3568 tid = p_stra->data.p_stra->i_track_ID;
3571 p_track = MP4_frg_GetTrack( p_demux, tid );
3573 return VLC_EGENERIC;
3574 p_track->b_codec_need_restart = true;
3576 return MP4_frg_GetChunks( p_demux, i_tk_id );
3579 if( MP4_frg_GetChunk( p_demux, p_chunk, (unsigned *)&tid ) != VLC_SUCCESS )
3580 return VLC_EGENERIC;
3582 if( tid == i_tk_id )
3589 static int MP4_frg_TrackSelect( demux_t *p_demux, mp4_track_t *p_track )
3591 if( !p_track->b_ok || p_track->b_chapter )
3593 return VLC_EGENERIC;
3596 if( p_track->b_selected )
3598 msg_Warn( p_demux, "track[Id 0x%x] already selected", p_track->i_track_ID );
3602 msg_Dbg( p_demux, "Select track id %u", p_track->i_track_ID );
3603 p_track->b_selected = true;
3608 * DemuxFrg: read packet and send them to decoders
3609 * \return 1 on success, 0 on error.
3610 * TODO check for newly selected track
3612 int DemuxFrg( demux_t *p_demux )
3614 demux_sys_t *p_sys = p_demux->p_sys;
3616 unsigned i_track_selected;
3618 /* check for newly selected/unselected track */
3619 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
3621 mp4_track_t *tk = &p_sys->track[i_track];
3624 if( !tk->b_ok || tk->b_chapter )
3627 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
3628 msg_Dbg( p_demux, "track %u %s!", tk->i_track_ID, b ? "enabled" : "disabled" );
3630 if( tk->b_selected && !b )
3632 MP4_TrackUnselect( p_demux, tk );
3634 else if( !tk->b_selected && b)
3636 MP4_frg_TrackSelect( p_demux, tk );
3639 if( tk->b_selected )
3643 if( i_track_selected <= 0 )
3645 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
3646 if( p_sys->i_timescale > 0 )
3648 int64_t i_length = (mtime_t)1000000 *
3649 (mtime_t)p_sys->i_duration /
3650 (mtime_t)p_sys->i_timescale;
3651 if( MP4_GetMoviePTS( p_sys ) >= i_length )
3656 msg_Warn( p_demux, "no track selected, exiting..." );
3660 /* first wait for the good time to read a packet */
3661 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
3663 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
3665 /* we will read 100ms for each stream so ...*/
3666 p_sys->i_time += __MAX( p_sys->i_timescale / 10, 1 );
3668 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
3670 mp4_track_t *tk = &p_sys->track[i_track];
3672 if( !tk->b_ok || tk->b_chapter || !tk->b_selected )
3674 msg_Warn( p_demux, "Skipping track id %u", tk->i_track_ID );
3678 if( !tk->b_has_non_empty_cchunk )
3680 if( MP4_frg_GetChunks( p_demux, tk->i_track_ID ) != VLC_SUCCESS )
3682 msg_Info( p_demux, "MP4_frg_GetChunks returned error. End of stream?" );
3687 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
3692 mp4_chunk_t *ck = tk->cchunk;
3693 if( ck->i_sample >= ck->i_sample_count )
3695 msg_Err( p_demux, "sample %"PRIu32" of %"PRIu32"",
3696 ck->i_sample, ck->i_sample_count );
3700 uint32_t sample_size = ck->p_sample_size[ck->i_sample];
3701 p_block = block_Alloc( sample_size );
3702 uint8_t *src = ck->p_sample_data[ck->i_sample];
3703 memcpy( p_block->p_buffer, src, sample_size );
3706 if( ck->i_sample == ck->i_sample_count )
3707 tk->b_has_non_empty_cchunk = false;
3710 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
3712 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
3714 p_block->i_pts = p_block->i_dts + i_delta;
3715 else if( tk->fmt.i_cat != VIDEO_ES )
3716 p_block->i_pts = p_block->i_dts;
3718 p_block->i_pts = VLC_TS_INVALID;
3720 es_out_Send( p_demux->out, tk->p_es, p_block );
3724 if( !tk->b_has_non_empty_cchunk )