1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_charset.h> /* EnsureUTF8 */
36 #include <vlc_meta.h> /* vlc_meta_t, vlc_meta_ */
37 #include <vlc_input.h>
41 #include "id3genres.h" /* for ATOM_gnre */
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_category( CAT_INPUT )
51 set_subcategory( SUBCAT_INPUT_DEMUX )
52 set_description( N_("MP4 stream demuxer") )
53 set_shortname( N_("MP4") )
54 set_capability( "demux", 240 )
55 set_callbacks( Open, Close )
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Demux ( demux_t * );
62 static int DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
63 static int DemuxFrg( demux_t * );
64 static int Seek ( demux_t *, mtime_t );
65 static int Control ( demux_t *, int, va_list );
67 typedef struct mp4_fragment_t mp4_fragment_t;
70 uint64_t i_chunk_range_min_offset;
71 uint64_t i_chunk_range_max_offset;
74 mp4_fragment_t *p_next;
79 MP4_Box_t *p_root; /* container for the whole file */
83 uint64_t i_overall_duration; /* Full duration, including all fragments */
84 uint64_t i_time; /* time position of the presentation
85 * in movie timescale */
86 uint32_t i_timescale; /* movie time scale */
87 uint64_t i_duration; /* movie duration */
88 unsigned int i_tracks; /* number of tracks */
89 mp4_track_t *track; /* array of track */
90 float f_fps; /* number of frame per seconds */
92 bool b_fragmented; /* fMP4 */
95 bool b_smooth; /* Is it Smooth Streaming? */
97 bool b_fragments_probed;
98 mp4_fragment_t moovfragment; /* moov */
99 mp4_fragment_t *p_fragments; /* known fragments (moof following moov) */
102 MP4_Box_t *p_tref_chap;
105 input_title_t *p_title;
108 #define BOXDATA(type) type->data.type
110 /*****************************************************************************
111 * Declaration of local function
112 *****************************************************************************/
113 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *, bool b_force_enable );
114 static int MP4_frg_TrackCreate( demux_t *, mp4_track_t *, MP4_Box_t *);
115 static void MP4_TrackDestroy( mp4_track_t * );
117 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
118 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
120 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
122 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
123 static uint32_t MP4_TrackSampleSize( mp4_track_t *, uint32_t * );
124 static int MP4_TrackNextSample( demux_t *, mp4_track_t *, uint32_t );
125 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
127 static void MP4_UpdateSeekpoint( demux_t * );
128 static const char *MP4_ConvertMacCode( uint16_t );
130 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id );
132 static bool AddFragment( demux_t *p_demux, MP4_Box_t *p_moox );
133 static int ProbeFragments( demux_t *p_demux );
137 static uint32_t stream_ReadU32( stream_t *s, void *p_read, uint32_t i_toread )
139 uint32_t i_return = 0;
140 if ( i_toread > INT32_MAX )
142 i_return = stream_Read( s, p_read, INT32_MAX );
143 if ( i_return < INT32_MAX )
146 i_toread -= INT32_MAX;
148 i_return += stream_Read( s, (uint8_t *)p_read + i_return, (int32_t) i_toread );
152 static bool MP4_stream_Tell( stream_t *s, uint64_t *pi_pos )
154 int64_t i_pos = stream_Tell( s );
159 *pi_pos = (uint64_t) i_pos;
164 static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
166 MP4_Box_t *p_trex = MP4_BoxGet( p_moov, "mvex/trex" );
169 if ( p_trex->i_type == ATOM_trex && BOXDATA(p_trex)->i_track_ID == i_id )
172 p_trex = p_trex->p_next;
177 static MP4_Box_t * MP4_GetTrakByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
179 MP4_Box_t *p_trak = MP4_BoxGet( p_moov, "trak" );
183 if( p_trak->i_type == ATOM_trak &&
184 (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) &&
185 BOXDATA(p_tkhd)->i_track_ID == i_id )
188 p_trak = p_trak->p_next;
193 /* Return time in microsecond of a track */
194 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
196 demux_sys_t *p_sys = p_demux->p_sys;
198 if( p_sys->b_fragmented )
199 chunk = *p_track->cchunk;
201 chunk = p_track->chunk[p_track->i_chunk];
203 unsigned int i_index = 0;
204 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
205 int64_t i_dts = chunk.i_first_dts;
207 while( i_sample > 0 )
209 if( i_sample > chunk.p_sample_count_dts[i_index] )
211 i_dts += chunk.p_sample_count_dts[i_index] *
212 chunk.p_sample_delta_dts[i_index];
213 i_sample -= chunk.p_sample_count_dts[i_index];
218 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
223 /* now handle elst */
224 if( p_track->p_elst )
226 demux_sys_t *p_sys = p_demux->p_sys;
227 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
229 /* convert to offset */
230 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
231 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
232 elst->i_media_time[p_track->i_elst] > 0 )
234 i_dts -= elst->i_media_time[p_track->i_elst];
237 /* add i_elst_time */
238 i_dts += p_track->i_elst_time * p_track->i_timescale /
241 if( i_dts < 0 ) i_dts = 0;
244 return CLOCK_FREQ * i_dts / p_track->i_timescale;
247 static inline int64_t MP4_TrackGetPTSDelta( demux_t *p_demux, mp4_track_t *p_track )
249 demux_sys_t *p_sys = p_demux->p_sys;
251 if( p_sys->b_fragmented )
252 ck = p_track->cchunk;
254 ck = &p_track->chunk[p_track->i_chunk];
256 unsigned int i_index = 0;
257 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
259 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
262 for( i_index = 0;; i_index++ )
264 if( i_sample < ck->p_sample_count_pts[i_index] )
265 return ck->p_sample_offset_pts[i_index] * CLOCK_FREQ /
266 (int64_t)p_track->i_timescale;
268 i_sample -= ck->p_sample_count_pts[i_index];
272 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
274 return CLOCK_FREQ * p_sys->i_time / p_sys->i_timescale;
277 static void LoadChapter( demux_t *p_demux );
279 static int LoadInitFrag( demux_t *p_demux )
281 demux_sys_t *p_sys = p_demux->p_sys;
283 if( p_sys->b_smooth ) /* Smooth Streaming */
285 if( ( p_sys->p_root = MP4_BoxGetSmooBox( p_demux->s ) ) == NULL )
287 goto LoadInitFragError;
291 MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
292 if( !p_smoo || CmpUUID( &p_smoo->i_uuid, &SmooBoxUUID ) )
293 goto LoadInitFragError;
294 /* Get number of tracks */
296 for( int i = 0; i < 3; i++ )
298 MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
299 if( p_stra && BOXDATA(p_stra)->i_track_ID )
301 /* Get timescale and duration of the video track; */
302 if( p_sys->i_timescale == 0 )
304 p_sys->i_timescale = BOXDATA(p_stra)->i_timescale;
305 p_sys->i_duration = BOXDATA(p_stra)->i_duration;
306 if( p_sys->i_timescale == 0 )
308 msg_Err( p_demux, "bad timescale" );
309 goto LoadInitFragError;
317 /* Load all boxes ( except raw data ) */
318 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
320 goto LoadInitFragError;
326 msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" );
330 static int InitTracks( demux_t *p_demux )
332 demux_sys_t *p_sys = p_demux->p_sys;
334 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
335 if( p_sys->track == NULL )
338 if( p_sys->b_fragmented )
340 mp4_track_t *p_track;
341 for( uint16_t i = 0; i < p_sys->i_tracks; i++ )
343 p_track = &p_sys->track[i];
344 p_track->cchunk = calloc( 1, sizeof( mp4_chunk_t ) );
345 if( unlikely( !p_track->cchunk ) )
347 free( p_sys->track );
355 static void CreateTracksFromSmooBox( demux_t *p_demux )
357 demux_sys_t *p_sys = p_demux->p_sys;
359 MP4_Box_t *p_smoo = MP4_BoxGet( p_sys->p_root, "uuid" );
360 mp4_track_t *p_track;
362 for( int i = 0; i < 3; i++ )
364 MP4_Box_t *p_stra = MP4_BoxGet( p_smoo, "uuid[%d]", i );
365 if( !p_stra || BOXDATA(p_stra)->i_track_ID == 0 )
369 p_track = &p_sys->track[j]; j++;
370 MP4_frg_TrackCreate( p_demux, p_track, p_stra );
371 p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
376 /*****************************************************************************
377 * Open: check file and initializes MP4 structures
378 *****************************************************************************/
379 static int Open( vlc_object_t * p_this )
381 demux_t *p_demux = (demux_t *)p_this;
384 const uint8_t *p_peek;
394 /* A little test to see if it could be a mp4 */
395 if( stream_Peek( p_demux->s, &p_peek, 11 ) < 11 ) return VLC_EGENERIC;
397 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
408 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
411 /* We don't yet support f4v, but avformat does. */
412 if( p_peek[8] == 'f' && p_peek[9] == '4' && p_peek[10] == 'v' )
419 /* create our structure that will contains all data */
420 p_sys = calloc( 1, sizeof( demux_sys_t ) );
425 stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
426 if( !p_sys->b_seekable )
428 msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" );
432 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable );
434 /*Set exported functions */
435 p_demux->pf_demux = Demux;
436 p_demux->pf_control = Control;
438 p_demux->p_sys = p_sys;
440 if( stream_Peek( p_demux->s, &p_peek, 24 ) < 24 ) return VLC_EGENERIC;
441 if( !CmpUUID( (UUID_t *)(p_peek + 8), &SmooBoxUUID ) )
443 p_sys->b_smooth = true;
444 p_sys->b_fragmented = true;
447 if( LoadInitFrag( p_demux ) != VLC_SUCCESS )
450 if( MP4_BoxCount( p_sys->p_root, "/moov/mvex" ) > 0 )
452 if ( p_sys->b_seekable )
454 /* Probe remaining to check if there's really fragments
455 or if that file is just ready to append fragments */
456 ProbeFragments( p_demux );
457 p_sys->b_fragmented = !!MP4_BoxCount( p_sys->p_root, "/moof" );
460 p_sys->b_fragmented = true;
463 if ( !p_sys->moovfragment.p_moox )
464 AddFragment( p_demux, MP4_BoxGet( p_sys->p_root, "/moov" ) );
466 /* we always need a moov entry, but smooth has a workaround */
467 if ( !p_sys->moovfragment.p_moox && !p_sys->b_smooth )
472 if( p_sys->b_fragmented || p_sys->b_smooth )
474 p_demux->pf_demux = DemuxFrg;
477 if( p_sys->b_smooth )
479 if( InitTracks( p_demux ) != VLC_SUCCESS )
481 CreateTracksFromSmooBox( p_demux );
484 else if( p_sys->b_fragmented )
486 /* We are not yet able to demux a fragmented MP4 file, using the 'mfra'
487 * a file, and we let avformat do the job. */
488 msg_Warn( p_demux, "MP4 plugin discarded "\
489 "(fragmented, let avformat demux it)" );
490 stream_Seek( p_demux->s, 0 ); /* rewind, for other demux */
494 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
496 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
498 switch( BOXDATA(p_ftyp)->i_major_brand )
502 "ISO Media file (isom) version %d.",
503 BOXDATA(p_ftyp)->i_minor_version );
506 case( VLC_FOURCC( '3', 'g', 'p', '5' ) ):
507 case( VLC_FOURCC( '3', 'g', 'p', '6' ) ):
508 case( VLC_FOURCC( '3', 'g', 'p', '7' ) ):
509 msg_Dbg( p_demux, "3GPP Media file Release: %c",
510 #ifdef WORDS_BIGENDIAN
511 BOXDATA(p_ftyp)->i_major_brand
513 BOXDATA(p_ftyp)->i_major_brand >> 24
517 case( VLC_FOURCC( 'q', 't', ' ', ' ') ):
518 msg_Dbg( p_demux, "Apple QuickTime file" );
520 case( VLC_FOURCC( 'i', 's', 'm', 'l') ):
521 msg_Dbg( p_demux, "PIFF (= isml = fMP4) file" );
525 "unrecognized major file specification (%4.4s).",
526 (char*)&BOXDATA(p_ftyp)->i_major_brand );
532 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
535 /* the file need to have one moov box */
536 p_sys->moovfragment.p_moox = MP4_BoxGet( p_sys->p_root, "/moov", 0 );
537 if( !p_sys->moovfragment.p_moox )
539 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
543 msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
546 /* we have a free box as a moov, rename it */
547 p_foov->i_type = ATOM_moov;
548 p_sys->moovfragment.p_moox = p_foov;
551 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
553 int i_count = MP4_BoxCount( p_rmra, "rmda" );
556 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
558 input_thread_t *p_input = demux_GetParentInput( p_demux );
559 input_item_t *p_current = input_GetItem( p_input );
561 input_item_node_t *p_subitems = input_item_node_Create( p_current );
563 for( i = 0; i < i_count; i++ )
565 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
569 if( !p_rdrf || !( psz_ref = strdup( BOXDATA(p_rdrf)->psz_ref ) ) )
573 i_ref_type = BOXDATA(p_rdrf)->i_ref_type;
575 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
576 psz_ref, (char*)&i_ref_type );
578 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
580 if( strstr( psz_ref, "qt5gateQT" ) )
582 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
585 if( !strncmp( psz_ref, "http://", 7 ) ||
586 !strncmp( psz_ref, "rtsp://", 7 ) )
593 char *psz_path = strdup( p_demux->psz_location );
594 char *end = strrchr( psz_path, '/' );
595 if( end ) end[1] = '\0';
596 else *psz_path = '\0';
598 if( asprintf( &psz_absolute, "%s://%s%s",
599 p_demux->psz_access, psz_path, psz_ref ) < 0 )
603 input_item_node_Delete( p_subitems );
604 vlc_object_release( p_input) ;
609 psz_ref = psz_absolute;
612 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
613 input_item_t *p_item = input_item_New( psz_ref, NULL );
614 input_item_CopyOptions( p_current, p_item );
615 input_item_node_AppendItem( p_subitems, p_item );
616 vlc_gc_decref( p_item );
620 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
621 (char*)&BOXDATA(p_rdrf)->i_ref_type );
625 input_item_node_PostAndDelete( p_subitems );
626 vlc_object_release( p_input );
629 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
633 msg_Err( p_demux, "cannot find /moov/mvhd" );
638 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
639 p_demux->pf_demux = DemuxRef;
645 p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
646 if( p_sys->i_timescale == 0 )
648 msg_Err( p_this, "bad timescale" );
653 if ( p_sys->i_overall_duration == 0 )
655 /* Try in mehd if fragmented */
656 MP4_Box_t *p_mehd = MP4_BoxGet( p_demux->p_sys->p_root, "moov/mvex/mehd");
658 p_sys->i_overall_duration = p_mehd->data.p_mehd->i_fragment_duration;
660 p_sys->i_overall_duration = p_sys->moovfragment.i_duration;
663 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
665 msg_Err( p_demux, "cannot find any /moov/trak" );
668 msg_Dbg( p_demux, "found %d track%c",
670 p_sys->i_tracks ? 's':' ' );
672 if( InitTracks( p_demux ) != VLC_SUCCESS )
675 /* Search the first chap reference (like quicktime) and
676 * check that at least 1 stream is enabled */
677 p_sys->p_tref_chap = NULL;
678 b_enabled_es = false;
679 for( i = 0; i < p_sys->i_tracks; i++ )
681 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
684 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
685 if( p_tkhd && (BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED) )
688 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
689 if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
690 p_sys->p_tref_chap = p_chap;
693 /* now process each track and extract all useful information */
694 for( i = 0; i < p_sys->i_tracks; i++ )
696 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
697 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
699 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
702 switch( p_sys->track[i].fmt.i_cat )
711 psz_cat = "subtitle";
719 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
720 p_sys->track[i].i_track_ID, psz_cat,
721 p_sys->track[i].b_enable ? "enable":"disable",
722 p_sys->track[i].fmt.psz_language ?
723 p_sys->track[i].fmt.psz_language : "undef" );
725 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
727 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
728 p_sys->track[i].i_track_ID,
729 p_sys->track[i].fmt.psz_language ?
730 p_sys->track[i].fmt.psz_language : "undef" );
734 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
735 p_sys->track[i].i_track_ID );
739 mp4_fragment_t *p_fragment = &p_sys->moovfragment;
742 msg_Dbg( p_demux, "fragment offset %"PRId64", data %"PRId64"<->%"PRId64", time %"PRId64,
743 p_fragment->p_moox->i_pos, p_fragment->i_chunk_range_min_offset,
744 p_fragment->i_chunk_range_max_offset, p_fragment->i_duration );
745 p_fragment = p_fragment->p_next;
749 LoadChapter( p_demux );
756 MP4_BoxFree( p_demux->s, p_sys->p_root );
762 /*****************************************************************************
763 * Demux: read packet and send them to decoders
764 *****************************************************************************
765 * TODO check for newly selected track (ie audio upt to now )
766 *****************************************************************************/
767 static int Demux( demux_t *p_demux )
769 demux_sys_t *p_sys = p_demux->p_sys;
770 unsigned int i_track;
773 unsigned int i_track_selected;
775 /* check for newly selected/unselected track */
776 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
779 mp4_track_t *tk = &p_sys->track[i_track];
782 if( !tk->b_ok || tk->b_chapter ||
783 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
788 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
790 if( tk->b_selected && !b )
792 MP4_TrackUnselect( p_demux, tk );
794 else if( !tk->b_selected && b)
796 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
805 if( i_track_selected <= 0 )
807 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
808 if( p_sys->i_timescale > 0 )
810 int64_t i_length = CLOCK_FREQ *
811 (mtime_t)p_sys->moovfragment.i_duration /
812 (mtime_t)p_sys->i_timescale;
813 if( MP4_GetMoviePTS( p_sys ) >= i_length )
818 msg_Warn( p_demux, "no track selected, exiting..." );
823 MP4_UpdateSeekpoint( p_demux );
825 /* first wait for the good time to read a packet */
826 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
828 bool b_data_sent = false;
830 /* Find next track matching contiguous data */
831 mp4_track_t *tk = NULL;
832 uint64_t i_candidate_pos = UINT64_MAX;
833 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
835 mp4_track_t *tk_tmp = &p_sys->track[i_track];
836 if( !tk_tmp->b_ok || tk_tmp->b_chapter || !tk_tmp->b_selected || tk_tmp->i_sample >= tk_tmp->i_sample_count )
839 uint64_t i_pos = MP4_TrackGetPos( tk_tmp );
840 if ( i_pos <= i_candidate_pos )
842 i_candidate_pos = i_pos;
849 msg_Dbg( p_demux, "Could not select track by data position" );
854 msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, i_track,
855 MP4_TrackGetDTS( p_demux, tk ),
856 MP4_GetMoviePTS( p_sys ), i_candidate_pos );
859 uint32_t i_nb_samples = 0;
860 uint32_t i_samplessize = MP4_TrackSampleSize( tk, &i_nb_samples );
861 if( i_samplessize > 0 )
865 uint64_t i_current_pos;
868 if ( !MP4_stream_Tell( p_demux->s, &i_current_pos ) )
871 if( i_current_pos != i_candidate_pos )
873 if( stream_Seek( p_demux->s, i_candidate_pos ) )
875 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
877 MP4_TrackUnselect( p_demux, tk );
883 if( !(p_block = stream_Block( p_demux->s, i_samplessize )) )
885 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
887 MP4_TrackUnselect( p_demux, tk );
890 else if( tk->fmt.i_cat == SPU_ES )
892 if ( tk->fmt.i_codec != VLC_CODEC_TX3G &&
893 tk->fmt.i_codec != VLC_CODEC_SPU )
894 p_block->i_buffer = 0;
898 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
900 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
902 p_block->i_pts = p_block->i_dts + i_delta;
903 else if( tk->fmt.i_cat != VIDEO_ES )
904 p_block->i_pts = p_block->i_dts;
906 p_block->i_pts = VLC_TS_INVALID;
910 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
913 es_out_Send( p_demux->out, tk->p_es, p_block );
916 MP4_TrackNextSample( p_demux, tk, i_nb_samples );
922 p_sys->i_pcr = INT64_MAX;
923 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
925 mp4_track_t *tk = &p_sys->track[i_track];
926 if ( !tk->b_ok || !tk->b_selected ) continue;
927 p_sys->i_pcr = __MIN( MP4_TrackGetDTS( p_demux, tk ), p_sys->i_pcr );
928 p_sys->i_time = p_sys->i_pcr * p_sys->i_timescale / CLOCK_FREQ;
935 static void MP4_UpdateSeekpoint( demux_t *p_demux )
937 demux_sys_t *p_sys = p_demux->p_sys;
940 if( !p_sys->p_title )
942 i_time = MP4_GetMoviePTS( p_sys );
943 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
945 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
950 if( i != p_demux->info.i_seekpoint && i >= 0 )
952 p_demux->info.i_seekpoint = i;
953 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
956 /*****************************************************************************
958 ******************************************************************************/
959 static int Seek( demux_t *p_demux, mtime_t i_date )
961 demux_sys_t *p_sys = p_demux->p_sys;
962 unsigned int i_track;
964 /* First update global time */
965 p_sys->i_time = i_date * p_sys->i_timescale / CLOCK_FREQ;
966 p_sys->i_pcr = VLC_TS_INVALID;
968 /* Now for each stream try to go to this time */
969 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
971 mp4_track_t *tk = &p_sys->track[i_track];
972 MP4_TrackSeek( p_demux, tk, i_date );
974 MP4_UpdateSeekpoint( p_demux );
976 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
981 static int MP4_frg_Seek( demux_t *p_demux, double f )
983 demux_sys_t *p_sys = p_demux->p_sys;
985 int64_t i64 = stream_Size( p_demux->s );
986 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
992 /* update global time */
993 p_sys->i_time = (uint64_t)(f * (double)p_sys->moovfragment.i_duration);
994 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
996 for( unsigned i_track = 0; i_track < p_sys->i_tracks; i_track++ )
998 mp4_track_t *tk = &p_sys->track[i_track];
1000 /* We don't want the current chunk to be flushed */
1001 tk->cchunk->i_sample = tk->cchunk->i_sample_count;
1003 /* reset/update some values */
1004 tk->i_sample = tk->i_sample_first = 0;
1005 tk->i_first_dts = p_sys->i_time;
1007 /* We want to discard the current chunk and get the next one at once */
1008 tk->b_has_non_empty_cchunk = false;
1010 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_sys->i_pcr );
1015 /*****************************************************************************
1017 *****************************************************************************/
1018 static int Control( demux_t *p_demux, int i_query, va_list args )
1020 demux_sys_t *p_sys = p_demux->p_sys;
1027 case DEMUX_GET_POSITION:
1028 pf = (double*)va_arg( args, double * );
1029 if( p_sys->i_overall_duration > 0 )
1031 *pf = (double)p_sys->i_time / (double)p_sys->i_overall_duration;
1039 case DEMUX_SET_POSITION:
1040 f = (double)va_arg( args, double );
1041 if( p_sys->b_fragmented )
1043 return MP4_frg_Seek( p_demux, f );
1045 else if( p_sys->i_timescale > 0 )
1047 i64 = (int64_t)( f * CLOCK_FREQ *
1048 (double)p_sys->i_overall_duration /
1049 (double)p_sys->i_timescale );
1050 return Seek( p_demux, i64 );
1052 else return VLC_SUCCESS;
1054 case DEMUX_GET_TIME:
1055 pi64 = (int64_t*)va_arg( args, int64_t * );
1056 if( p_sys->i_timescale > 0 )
1058 *pi64 = CLOCK_FREQ *
1059 (mtime_t)p_sys->i_time /
1060 (mtime_t)p_sys->i_timescale;
1065 case DEMUX_SET_TIME:
1066 i64 = (int64_t)va_arg( args, int64_t );
1067 return Seek( p_demux, i64 );
1069 case DEMUX_GET_LENGTH:
1070 pi64 = (int64_t*)va_arg( args, int64_t * );
1071 if( p_sys->i_timescale > 0 )
1073 *pi64 = CLOCK_FREQ *
1074 (mtime_t)p_sys->i_overall_duration /
1075 (mtime_t)p_sys->i_timescale;
1081 pf = (double*)va_arg( args, double* );
1085 case DEMUX_GET_ATTACHMENTS:
1087 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1088 int *pi_int = va_arg( args, int * );
1090 MP4_Box_t *p_covr = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst/covr" );
1091 if ( !p_covr ) return VLC_EGENERIC;
1095 for( p_box = p_covr->p_first; p_box != NULL; p_box = p_box->p_next )
1097 if ( p_box->i_type == ATOM_data && p_box->data.p_data->i_blob >= 16 )
1102 return VLC_EGENERIC;
1104 *ppp_attach = (input_attachment_t**)
1105 malloc( sizeof(input_attachment_t*) * i_count );
1106 if( !(*ppp_attach) ) return VLC_ENOMEM;
1110 for( p_box = p_covr->p_first; p_box != NULL; p_box = p_box->p_next )
1112 if ( p_box->i_type != ATOM_data || p_box->data.p_data->i_blob < 16 )
1115 if ( !memcmp( p_box->data.p_data->p_blob,
1116 "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8 ) )
1118 psz_mime = strdup( "image/png" );
1120 else if ( !memcmp( p_box->data.p_data->p_blob, "\xFF\xD8", 2 ) )
1122 psz_mime = strdup( "image/jpeg" );
1129 if ( asprintf( &psz_filename, "picture%u", i_count ) >= 0 )
1131 (*ppp_attach)[i_count++] =
1132 vlc_input_attachment_New( psz_filename, psz_mime, NULL,
1133 p_box->data.p_data->p_blob, p_box->data.p_data->i_blob );
1134 free( psz_filename );
1142 free( *ppp_attach );
1143 return VLC_EGENERIC;
1151 case DEMUX_GET_META:
1153 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
1154 MP4_Box_t *p_0xa9xxx;
1156 MP4_Box_t *p_covr = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst/covr/data[0]" );
1158 vlc_meta_SetArtURL( p_meta, "attachment://picture0" );
1160 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
1161 if( p_udta == NULL )
1163 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
1164 if( p_udta == NULL && p_covr == NULL )
1165 return VLC_EGENERIC;
1170 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
1171 p_0xa9xxx = p_0xa9xxx->p_next )
1174 if( !p_0xa9xxx || !BOXDATA(p_0xa9xxx) )
1177 /* FIXME FIXME: should convert from whatever the character
1178 * encoding of MP4 meta data is to UTF-8. */
1179 #define SET(fct) do { char *psz_utf = strdup( BOXDATA(p_0xa9xxx)->psz_text ? BOXDATA(p_0xa9xxx)->psz_text : "" ); \
1180 if( psz_utf ) { EnsureUTF8( psz_utf ); \
1181 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
1183 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
1184 switch( p_0xa9xxx->i_type )
1186 case ATOM_0xa9nam: /* Full name */
1187 SET( vlc_meta_SetTitle );
1190 SET( vlc_meta_SetArtist );
1193 SET( vlc_meta_SetArtist );
1196 SET( vlc_meta_SetCopyright );
1198 case ATOM_0xa9day: /* Creation Date */
1199 SET( vlc_meta_SetDate );
1201 case ATOM_0xa9des: /* Description */
1202 SET( vlc_meta_SetDescription );
1204 case ATOM_0xa9gen: /* Genre */
1205 SET( vlc_meta_SetGenre );
1209 if( p_0xa9xxx->data.p_gnre->i_genre <= NUM_GENRES )
1210 vlc_meta_SetGenre( p_meta, ppsz_genres[p_0xa9xxx->data.p_gnre->i_genre - 1] );
1213 case ATOM_0xa9alb: /* Album */
1214 SET( vlc_meta_SetAlbum );
1217 case ATOM_0xa9trk: /* Track */
1218 SET( vlc_meta_SetTrackNum );
1223 snprintf( psz_trck, sizeof( psz_trck ), "%i",
1224 p_0xa9xxx->data.p_trkn->i_track_number );
1225 vlc_meta_SetTrackNum( p_meta, psz_trck );
1226 if( p_0xa9xxx->data.p_trkn->i_track_total > 0 )
1228 snprintf( psz_trck, sizeof( psz_trck ), "%i",
1229 p_0xa9xxx->data.p_trkn->i_track_total );
1230 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, psz_trck );
1234 case ATOM_0xa9cmt: /* Commment */
1235 SET( vlc_meta_SetDescription );
1238 case ATOM_0xa9url: /* URL */
1239 SET( vlc_meta_SetURL );
1242 case ATOM_0xa9too: /* Encoder Tool */
1243 case ATOM_0xa9enc: /* Encoded By */
1244 SET( vlc_meta_SetEncodedBy );
1248 SET( vlc_meta_SetPublisher );
1252 SET( vlc_meta_SetDirector );
1259 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
1261 { ATOM_0xa9wrt, N_("Writer") },
1262 { ATOM_0xa9com, N_("Composer") },
1263 { ATOM_0xa9prd, N_("Producer") },
1264 { ATOM_0xa9inf, N_("Information") },
1265 { ATOM_0xa9dis, N_("Disclaimer") },
1266 { ATOM_0xa9req, N_("Requirements") },
1267 { ATOM_0xa9fmt, N_("Original Format") },
1268 { ATOM_0xa9dsa, N_("Display Source As") },
1269 { ATOM_0xa9hst, N_("Host Computer") },
1270 { ATOM_0xa9prf, N_("Performers") },
1271 { ATOM_0xa9ope, N_("Original Performer") },
1272 { ATOM_0xa9src, N_("Providers Source Content") },
1273 { ATOM_0xa9wrn, N_("Warning") },
1274 { ATOM_0xa9swr, N_("Software") },
1275 { ATOM_0xa9lyr, N_("Lyrics") },
1276 { ATOM_0xa9mak, N_("Record Company") },
1277 { ATOM_0xa9mod, N_("Model") },
1278 { ATOM_0xa9PRD, N_("Product") },
1279 { ATOM_0xa9grp, N_("Grouping") },
1280 { ATOM_0xa9gen, N_("Genre") },
1281 { ATOM_0xa9st3, N_("Sub-Title") },
1282 { ATOM_0xa9arg, N_("Arranger") },
1283 { ATOM_0xa9ard, N_("Art Director") },
1284 { ATOM_0xa9cak, N_("Copyright Acknowledgement") },
1285 { ATOM_0xa9con, N_("Conductor") },
1286 { ATOM_0xa9des, N_("Song Description") },
1287 { ATOM_0xa9lnt, N_("Liner Notes") },
1288 { ATOM_0xa9phg, N_("Phonogram Rights") },
1289 { ATOM_0xa9pub, N_("Publisher") },
1290 { ATOM_0xa9sne, N_("Sound Engineer") },
1291 { ATOM_0xa9sol, N_("Soloist") },
1292 { ATOM_0xa9thx, N_("Thanks") },
1293 { ATOM_0xa9xpd, N_("Executive Producer") },
1296 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
1298 if( p_0xa9xxx->i_type == xa9typetoextrameta[i].xa9_type )
1300 char *psz_utf = strdup( BOXDATA(p_0xa9xxx)->psz_text ? BOXDATA(p_0xa9xxx)->psz_text : "" );
1303 EnsureUTF8( psz_utf );
1304 vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
1314 case DEMUX_GET_TITLE_INFO:
1316 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1317 int *pi_int = (int*)va_arg( args, int* );
1318 int *pi_title_offset = (int*)va_arg( args, int* );
1319 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
1321 if( !p_sys->p_title )
1322 return VLC_EGENERIC;
1325 *ppp_title = malloc( sizeof( input_title_t*) );
1326 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
1327 *pi_title_offset = 0;
1328 *pi_seekpoint_offset = 0;
1331 case DEMUX_SET_TITLE:
1333 const int i_title = (int)va_arg( args, int );
1334 if( !p_sys->p_title || i_title != 0 )
1335 return VLC_EGENERIC;
1338 case DEMUX_SET_SEEKPOINT:
1340 const int i_seekpoint = (int)va_arg( args, int );
1341 if( !p_sys->p_title )
1342 return VLC_EGENERIC;
1343 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
1346 case DEMUX_SET_NEXT_DEMUX_TIME:
1347 case DEMUX_SET_GROUP:
1348 case DEMUX_HAS_UNSUPPORTED_META:
1349 case DEMUX_GET_PTS_DELAY:
1350 case DEMUX_CAN_RECORD:
1351 return VLC_EGENERIC;
1354 msg_Warn( p_demux, "control query %u unimplemented", i_query );
1355 return VLC_EGENERIC;
1359 /*****************************************************************************
1360 * Close: frees unused data
1361 *****************************************************************************/
1362 static void Close ( vlc_object_t * p_this )
1364 demux_t * p_demux = (demux_t *)p_this;
1365 demux_sys_t *p_sys = p_demux->p_sys;
1366 unsigned int i_track;
1368 msg_Dbg( p_demux, "freeing all memory" );
1370 MP4_BoxFree( p_demux->s, p_sys->p_root );
1371 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1373 MP4_TrackDestroy( &p_sys->track[i_track] );
1375 FREENULL( p_sys->track );
1377 if( p_sys->p_title )
1378 vlc_input_title_Delete( p_sys->p_title );
1380 while( p_sys->moovfragment.p_next )
1382 mp4_fragment_t *p_fragment = p_sys->moovfragment.p_next->p_next;
1383 free( p_sys->moovfragment.p_next );
1384 p_sys->moovfragment.p_next = p_fragment;
1392 /****************************************************************************
1393 * Local functions, specific to vlc
1394 ****************************************************************************/
1396 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1398 demux_sys_t *p_sys = p_demux->p_sys;
1401 p_sys->p_title = vlc_input_title_New();
1402 for( i = 0; i < BOXDATA(p_chpl)->i_chapter; i++ )
1404 seekpoint_t *s = vlc_seekpoint_New();
1406 s->psz_name = strdup( BOXDATA(p_chpl)->chapter[i].psz_name );
1407 EnsureUTF8( s->psz_name );
1408 s->i_time_offset = BOXDATA(p_chpl)->chapter[i].i_start / 10;
1409 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1412 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1414 demux_sys_t *p_sys = p_demux->p_sys;
1416 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1418 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1419 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( p_demux, tk );
1420 uint32_t i_nb_samples = 0;
1421 const uint32_t i_size = MP4_TrackSampleSize( tk, &i_nb_samples );
1423 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1426 const uint32_t i_read = stream_ReadU32( p_demux->s, p_buffer,
1427 __MIN( sizeof(p_buffer), i_size ) );
1428 const uint32_t i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1432 seekpoint_t *s = vlc_seekpoint_New();
1434 s->psz_name = strndup( &p_buffer[2], i_len );
1435 EnsureUTF8( s->psz_name );
1437 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1439 if( !p_sys->p_title )
1440 p_sys->p_title = vlc_input_title_New();
1441 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1444 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1445 tk->chunk[tk->i_chunk].i_sample_count )
1449 static void LoadChapter( demux_t *p_demux )
1451 demux_sys_t *p_sys = p_demux->p_sys;
1454 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && BOXDATA(p_chpl)->i_chapter > 0 )
1456 LoadChapterGpac( p_demux, p_chpl );
1458 else if( p_sys->p_tref_chap )
1460 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1463 /* Load the first subtitle track like quicktime */
1464 for( i = 0; i < p_chap->i_entry_count; i++ )
1466 for( j = 0; j < p_sys->i_tracks; j++ )
1468 mp4_track_t *tk = &p_sys->track[j];
1469 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1470 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_SUBT )
1473 if( j < p_sys->i_tracks )
1475 LoadChapterApple( p_demux, &p_sys->track[j] );
1481 /* Add duration if titles are enabled */
1482 if( p_sys->p_title )
1484 p_sys->p_title->i_length = CLOCK_FREQ *
1485 (uint64_t)p_sys->i_overall_duration / (uint64_t)p_sys->i_timescale;
1489 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1490 static int TrackCreateChunksIndex( demux_t *p_demux,
1491 mp4_track_t *p_demux_track )
1493 demux_sys_t *p_sys = p_demux->p_sys;
1495 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1498 unsigned int i_chunk;
1499 unsigned int i_index, i_last;
1501 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1502 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1503 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1505 return( VLC_EGENERIC );
1508 p_demux_track->i_chunk_count = BOXDATA(p_co64)->i_entry_count;
1509 if( !p_demux_track->i_chunk_count )
1511 msg_Warn( p_demux, "no chunk defined" );
1513 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1514 sizeof( mp4_chunk_t ) );
1515 if( p_demux_track->chunk == NULL )
1520 /* first we read chunk offset */
1521 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1523 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1525 ck->i_offset = BOXDATA(p_co64)->i_chunk_offset[i_chunk];
1527 ck->i_first_dts = 0;
1528 ck->p_sample_count_dts = NULL;
1529 ck->p_sample_delta_dts = NULL;
1530 ck->p_sample_count_pts = NULL;
1531 ck->p_sample_offset_pts = NULL;
1534 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1535 to be used for the sample XXX begin to 1
1536 We construct it begining at the end */
1537 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1538 i_index = BOXDATA(p_stsc)->i_entry_count;
1540 while( i_index-- > 0 )
1542 for( i_chunk = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
1543 i_chunk < i_last; i_chunk++ )
1545 if( i_chunk >= p_demux_track->i_chunk_count )
1547 msg_Warn( p_demux, "corrupted chunk table" );
1548 return VLC_EGENERIC;
1551 p_demux_track->chunk[i_chunk].i_sample_description_index =
1552 BOXDATA(p_stsc)->i_sample_description_index[i_index];
1553 p_demux_track->chunk[i_chunk].i_sample_count =
1554 BOXDATA(p_stsc)->i_samples_per_chunk[i_index];
1556 i_last = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
1559 p_demux_track->chunk[0].i_sample_first = 0;
1560 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1562 p_demux_track->chunk[i_chunk].i_sample_first =
1563 p_demux_track->chunk[i_chunk-1].i_sample_first +
1564 p_demux_track->chunk[i_chunk-1].i_sample_count;
1567 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1568 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1570 if ( p_demux_track->i_chunk_count && (
1571 p_sys->moovfragment.i_chunk_range_min_offset == 0 ||
1572 p_sys->moovfragment.i_chunk_range_min_offset > p_demux_track->chunk[0].i_offset
1574 p_sys->moovfragment.i_chunk_range_min_offset = p_demux_track->chunk[0].i_offset;
1579 static int xTTS_CountEntries( demux_t *p_demux, uint32_t *pi_entry /* out */,
1580 const uint32_t i_index,
1581 uint32_t i_index_samples_left,
1582 uint32_t i_sample_count,
1583 const uint32_t *pi_index_sample_count,
1584 const uint32_t i_table_count )
1586 uint32_t i_array_offset;
1587 while( i_sample_count > 0 )
1589 if ( likely((UINT32_MAX - i_index) >= *pi_entry) )
1590 i_array_offset = i_index + *pi_entry;
1592 return VLC_EGENERIC;
1594 if ( i_array_offset >= i_table_count )
1596 msg_Err( p_demux, "invalid index counting total samples %u %u", i_array_offset, i_table_count );
1597 return VLC_EGENERIC;
1600 if ( i_index_samples_left )
1602 if ( i_index_samples_left > i_sample_count )
1604 i_index_samples_left -= i_sample_count;
1606 *pi_entry +=1; /* No samples left, go copy */
1611 i_sample_count -= i_index_samples_left;
1612 i_index_samples_left = 0;
1619 i_sample_count -= __MIN( i_sample_count, pi_index_sample_count[i_array_offset] );
1627 static int TrackCreateSamplesIndex( demux_t *p_demux,
1628 mp4_track_t *p_demux_track )
1630 demux_sys_t *p_sys = p_demux->p_sys;
1633 MP4_Box_data_stsz_t *stsz;
1634 /* TODO use also stss and stsh table for seeking */
1635 /* FIXME use edit table */
1638 * Gives the sample size for each samples. There is also a stz2 table
1639 * (compressed form) that we need to implement TODO */
1640 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1643 /* FIXME and stz2 */
1644 msg_Warn( p_demux, "cannot find STSZ box" );
1645 return VLC_EGENERIC;
1647 stsz = p_box->data.p_stsz;
1649 /* Use stsz table to create a sample number -> sample size table */
1650 p_demux_track->i_sample_count = stsz->i_sample_count;
1651 if( stsz->i_sample_size )
1653 /* 1: all sample have the same size, so no need to construct a table */
1654 p_demux_track->i_sample_size = stsz->i_sample_size;
1655 p_demux_track->p_sample_size = NULL;
1659 /* 2: each sample can have a different size */
1660 p_demux_track->i_sample_size = 0;
1661 p_demux_track->p_sample_size =
1662 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1663 if( p_demux_track->p_sample_size == NULL )
1666 for( uint32_t i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1668 p_demux_track->p_sample_size[i_sample] =
1669 stsz->i_entry_size[i_sample];
1673 if ( p_demux_track->i_chunk_count )
1675 mp4_chunk_t *lastchunk = &p_demux_track->chunk[p_demux_track->i_chunk_count - 1];
1676 uint64_t i_total_size = lastchunk->i_offset;
1677 for( uint32_t i=0; i<lastchunk->i_sample_count; i++)
1679 if( p_demux_track->i_sample_size == 0 )
1680 i_total_size += stsz->i_entry_size[i];
1682 i_total_size += p_demux_track->i_sample_size;
1685 if ( i_total_size > p_sys->moovfragment.i_chunk_range_max_offset )
1686 p_sys->moovfragment.i_chunk_range_max_offset = i_total_size;
1689 /* Use stts table to create a sample number -> dts table.
1690 * XXX: if we don't want to waste too much memory, we can't expand
1691 * the box! so each chunk will contain an "extract" of this table
1692 * for fast research (problem with raw stream where a sample is sometime
1693 * just channels*bits_per_sample/8 */
1695 /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
1696 * differing in 2/2 fields and 1 signedness */
1698 mtime_t i_next_dts = 0;
1700 * Gives mapping between sample and decoding time
1702 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1705 msg_Warn( p_demux, "cannot find STTS box" );
1706 return VLC_EGENERIC;
1710 MP4_Box_data_stts_t *stts = p_box->data.p_stts;
1712 msg_Warn( p_demux, "STTS table of %"PRIu32" entries", stts->i_entry_count );
1714 /* Create sample -> dts table per chunk */
1715 uint32_t i_index = 0;
1716 uint32_t i_current_index_samples_left = 0;
1718 for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1720 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1721 uint32_t i_entry, i_sample_count;
1723 /* save first dts */
1724 ck->i_first_dts = i_next_dts;
1725 ck->i_last_dts = i_next_dts;
1727 /* count how many entries are needed for this chunk
1728 * for p_sample_delta_dts and p_sample_count_dts */
1731 int i_ret = xTTS_CountEntries( p_demux, &i_entry, i_index,
1732 i_current_index_samples_left,
1734 stts->pi_sample_count,
1735 stts->i_entry_count );
1736 if ( i_ret != VLC_SUCCESS )
1740 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1741 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1742 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1744 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, i_entry );
1749 i_sample_count = ck->i_sample_count;
1751 for( uint32_t i = 0; i < i_entry; i++ )
1753 if ( i_current_index_samples_left )
1755 if ( i_current_index_samples_left > i_sample_count )
1757 if ( i_sample_count ) ck->i_last_dts = i_next_dts;
1758 ck->p_sample_count_dts[i] = i_sample_count;
1759 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
1760 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
1761 i_current_index_samples_left -= i_sample_count;
1763 assert( i == i_entry - 1 );
1768 if ( i_current_index_samples_left ) ck->i_last_dts = i_next_dts;
1769 ck->p_sample_count_dts[i] = i_current_index_samples_left;
1770 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
1771 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
1772 i_sample_count -= i_current_index_samples_left;
1773 i_current_index_samples_left = 0;
1779 if ( stts->pi_sample_count[i_index] > i_sample_count )
1781 if ( i_sample_count ) ck->i_last_dts = i_next_dts;
1782 ck->p_sample_count_dts[i] = i_sample_count;
1783 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
1784 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
1785 i_current_index_samples_left = stts->pi_sample_count[i_index] - i_sample_count;
1787 assert( i == i_entry - 1 );
1788 // keep building from same index
1792 if ( stts->pi_sample_count[i_index] ) ck->i_last_dts = i_next_dts;
1793 ck->p_sample_count_dts[i] = stts->pi_sample_count[i_index];
1794 ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
1795 i_next_dts += ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
1796 i_sample_count -= stts->pi_sample_count[i_index];
1807 * Gives the delta between decoding time (dts) and composition table (pts)
1809 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1812 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1814 msg_Warn( p_demux, "CTTS table of %"PRIu32" entries", ctts->i_entry_count );
1816 /* Create pts-dts table per chunk */
1817 uint32_t i_index = 0;
1818 uint32_t i_current_index_samples_left = 0;
1820 for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1822 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1823 uint32_t i_entry, i_sample_count;
1825 /* count how many entries are needed for this chunk
1826 * for p_sample_offset_pts and p_sample_count_pts */
1828 int i_ret = xTTS_CountEntries( p_demux, &i_entry, i_index,
1829 i_current_index_samples_left,
1831 ctts->pi_sample_count,
1832 ctts->i_entry_count );
1833 if ( i_ret != VLC_SUCCESS )
1837 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1838 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1839 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1841 msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, i_entry );
1846 i_sample_count = ck->i_sample_count;
1848 for( uint32_t i = 0; i < i_entry; i++ )
1850 if ( i_current_index_samples_left )
1852 if ( i_current_index_samples_left > i_sample_count )
1854 ck->p_sample_count_pts[i] = i_sample_count;
1855 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
1856 i_current_index_samples_left -= i_sample_count;
1858 assert( i == i_entry - 1 );
1863 ck->p_sample_count_pts[i] = i_current_index_samples_left;
1864 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
1865 i_sample_count -= i_current_index_samples_left;
1866 i_current_index_samples_left = 0;
1872 if ( ctts->pi_sample_count[i_index] > i_sample_count )
1874 ck->p_sample_count_pts[i] = i_sample_count;
1875 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
1876 i_current_index_samples_left = ctts->pi_sample_count[i_index] - i_sample_count;
1878 assert( i == i_entry - 1 );
1879 // keep building from same index
1883 ck->p_sample_count_pts[i] = ctts->pi_sample_count[i_index];
1884 ck->p_sample_offset_pts[i] = ctts->pi_sample_offset[i_index];
1885 i_sample_count -= ctts->pi_sample_count[i_index];
1895 msg_Dbg( p_demux, "track[Id 0x%x] read %"PRIu32" samples length:%"PRId64"s",
1896 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1897 i_next_dts / p_demux_track->i_timescale );
1904 * It computes the sample rate for a video track using the given sample
1907 static void TrackGetESSampleRate( demux_t *p_demux,
1908 unsigned *pi_num, unsigned *pi_den,
1909 const mp4_track_t *p_track,
1910 unsigned i_sd_index,
1916 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( MP4_BoxGet( p_demux->p_sys->p_root, "/moov" ),
1917 p_track->i_track_ID );
1918 MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
1921 vlc_ureduce( pi_num, pi_den,
1922 BOXDATA(p_mdhd)->i_timescale * p_track->i_sample_count,
1923 BOXDATA(p_mdhd)->i_duration,
1928 if( p_track->i_chunk_count <= 0 )
1932 const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
1933 while( p_chunk > &p_track->chunk[0] &&
1934 p_chunk[-1].i_sample_description_index == i_sd_index )
1939 uint64_t i_sample = 0;
1940 uint64_t i_first_dts = p_chunk->i_first_dts;
1941 uint64_t i_last_dts;
1944 i_sample += p_chunk->i_sample_count;
1945 i_last_dts = p_chunk->i_last_dts;
1948 while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
1949 p_chunk->i_sample_description_index == i_sd_index );
1951 if( i_sample > 1 && i_first_dts < i_last_dts )
1952 vlc_ureduce( pi_num, pi_den,
1953 ( i_sample - 1) * p_track->i_timescale,
1954 i_last_dts - i_first_dts,
1960 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1962 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1963 unsigned int i_chunk, es_out_id_t **pp_es )
1965 demux_sys_t *p_sys = p_demux->p_sys;
1966 unsigned int i_sample_description_index;
1968 if( p_sys->b_fragmented )
1969 i_sample_description_index = 1; /* XXX */
1971 i_sample_description_index =
1972 p_track->chunk[i_chunk].i_sample_description_index;
1974 MP4_Box_t *p_sample;
1983 if( !i_sample_description_index )
1985 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1986 p_track->i_track_ID );
1987 return VLC_EGENERIC;
1990 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1991 i_sample_description_index - 1 );
1994 ( !p_sample->data.p_payload && p_track->fmt.i_cat != SPU_ES ) )
1996 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1997 p_track->i_track_ID );
1998 return VLC_EGENERIC;
2001 p_track->p_sample = p_sample;
2003 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
2005 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
2007 p_sample->i_type = p_frma->data.p_frma->i_type;
2010 p_enda = MP4_BoxGet( p_sample, "wave/enda" );
2012 p_enda = MP4_BoxGet( p_sample, "enda" );
2014 p_pasp = MP4_BoxGet( p_sample, "pasp" );
2017 switch( p_track->fmt.i_cat )
2020 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
2021 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
2022 p_track->fmt.video.i_bits_per_pixel =
2023 p_sample->data.p_sample_vide->i_depth;
2025 /* fall on display size */
2026 if( p_track->fmt.video.i_width <= 0 )
2027 p_track->fmt.video.i_width = p_track->i_width;
2028 if( p_track->fmt.video.i_height <= 0 )
2029 p_track->fmt.video.i_height = p_track->i_height;
2031 /* Find out apect ratio from display size */
2032 if( p_track->i_width > 0 && p_track->i_height > 0 &&
2033 /* Work-around buggy muxed files */
2034 p_sample->data.p_sample_vide->i_width != p_track->i_width )
2036 p_track->fmt.video.i_sar_num = p_track->i_width * p_track->fmt.video.i_height;
2037 p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
2039 if( p_pasp && p_pasp->data.p_pasp->i_horizontal_spacing > 0 &&
2040 p_pasp->data.p_pasp->i_vertical_spacing > 0 )
2042 p_track->fmt.video.i_sar_num = p_pasp->data.p_pasp->i_horizontal_spacing;
2043 p_track->fmt.video.i_sar_den = p_pasp->data.p_pasp->i_vertical_spacing;
2046 /* Support for cropping (eg. in H263 files) */
2047 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
2048 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
2051 TrackGetESSampleRate( p_demux,
2052 &p_track->fmt.video.i_frame_rate,
2053 &p_track->fmt.video.i_frame_rate_base,
2054 p_track, i_sample_description_index, i_chunk );
2056 p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
2057 (float)p_track->fmt.video.i_frame_rate_base;
2060 switch( (int)p_track->f_rotation ) {
2062 p_track->fmt.video.orientation = ORIENT_ROTATED_90;
2065 p_track->fmt.video.orientation = ORIENT_ROTATED_180;
2068 p_track->fmt.video.orientation = ORIENT_ROTATED_270;
2075 p_track->fmt.audio.i_channels =
2076 p_sample->data.p_sample_soun->i_channelcount;
2077 p_track->fmt.audio.i_rate =
2078 p_sample->data.p_sample_soun->i_sampleratehi;
2079 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
2080 p_sample->data.p_sample_soun->i_sampleratehi *
2081 p_sample->data.p_sample_soun->i_samplesize;
2082 p_track->fmt.audio.i_bitspersample =
2083 p_sample->data.p_sample_soun->i_samplesize;
2085 if( ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
2087 MP4_Box_data_sample_soun_t *p_soun;
2088 p_soun = p_sample->data.p_sample_soun;
2090 if( p_soun->i_qt_version == 0 )
2092 switch( p_sample->i_type )
2094 case VLC_CODEC_ADPCM_IMA_QT:
2095 p_soun->i_qt_version = 1;
2096 p_soun->i_sample_per_packet = 64;
2097 p_soun->i_bytes_per_packet = 34;
2098 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
2099 p_soun->i_bytes_per_sample = 2;
2101 case VLC_CODEC_MACE3:
2102 p_soun->i_qt_version = 1;
2103 p_soun->i_sample_per_packet = 6;
2104 p_soun->i_bytes_per_packet = 2;
2105 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
2106 p_soun->i_bytes_per_sample = 2;
2108 case VLC_CODEC_MACE6:
2109 p_soun->i_qt_version = 1;
2110 p_soun->i_sample_per_packet = 12;
2111 p_soun->i_bytes_per_packet = 2;
2112 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
2113 p_soun->i_bytes_per_sample = 2;
2115 case VLC_CODEC_ALAW:
2116 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
2117 p_soun->i_samplesize = 8;
2118 p_track->i_sample_size = p_soun->i_channelcount;
2120 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
2121 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
2122 case VLC_FOURCC( 't', 'w', 'o', 's' ):
2123 case VLC_FOURCC( 's', 'o', 'w', 't' ):
2124 /* What would be the fun if you could trust the .mov */
2125 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
2132 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
2134 p_soun->i_qt_version = 0;
2137 else if( p_sample->data.p_sample_soun->i_qt_version == 1 )
2139 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
2141 switch( p_sample->i_type )
2143 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
2144 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
2146 if( p_track->i_sample_size > 1 )
2147 p_soun->i_qt_version = 0;
2150 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
2151 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
2152 case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
2153 p_soun->i_qt_version = 0;
2160 if( p_track->i_sample_size != 0 &&
2161 p_sample->data.p_sample_soun->i_qt_version == 1 &&
2162 p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
2164 msg_Err( p_demux, "Invalid sample per packet value for qt_version 1. Broken muxer!" );
2165 p_sample->data.p_sample_soun->i_qt_version = 0;
2174 /* It's a little ugly but .. there are special cases */
2175 switch( p_sample->i_type )
2177 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
2178 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
2180 p_track->fmt.i_codec = VLC_CODEC_MPGA;
2183 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
2185 MP4_Box_t *p_dac3_box = MP4_BoxGet( p_sample, "dac3", 0 );
2187 p_track->fmt.i_codec = VLC_CODEC_A52;
2190 static const int pi_bitrate[] = {
2197 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
2198 p_track->fmt.audio.i_channels = 0;
2199 p_track->fmt.i_bitrate = 0;
2200 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
2201 p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
2202 p_track->fmt.audio.i_bitspersample = 0;
2206 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
2208 p_track->fmt.i_codec = VLC_CODEC_EAC3;
2212 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
2213 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
2215 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
2217 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
2218 p_track->fmt.i_codec = VLC_CODEC_U8;
2220 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
2222 /* Buggy files workaround */
2223 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
2224 p_sample->data.p_sample_soun->i_sampleratehi) )
2226 MP4_Box_data_sample_soun_t *p_soun =
2227 p_sample->data.p_sample_soun;
2229 msg_Warn( p_demux, "i_timescale (%"PRId32") != i_sampleratehi "
2230 "(%u), making both equal (report any problem).",
2231 p_track->i_timescale, p_soun->i_sampleratehi );
2233 if( p_soun->i_sampleratehi != 0 )
2234 p_track->i_timescale = p_soun->i_sampleratehi;
2236 p_soun->i_sampleratehi = p_track->i_timescale;
2241 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
2242 p_track->fmt.i_codec = VLC_CODEC_H263;
2245 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
2246 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
2248 p_track->fmt.i_codec = VLC_CODEC_TX3G;
2249 MP4_Box_data_sample_text_t *p_text = p_sample->data.p_sample_text;
2252 text_style_t *p_style = text_style_New();
2255 if ( p_text->i_font_size ) /* !WARN: % in absolute storage */
2256 p_style->i_font_size = p_text->i_font_size;
2257 if ( p_text->i_font_color )
2259 p_style->i_font_color = p_text->i_font_color >> 8;
2260 p_style->i_font_alpha = p_text->i_font_color & 0xFF;
2262 if ( p_text->i_background_color[3] >> 8 )
2264 p_style->i_background_color = p_text->i_background_color[0] >> 8;
2265 p_style->i_background_color |= p_text->i_background_color[1] >> 8;
2266 p_style->i_background_color |= p_text->i_background_color[2] >> 8;
2267 p_style->i_background_alpha = p_text->i_background_color[3] >> 8;
2270 p_track->fmt.subs.p_style = p_style;
2272 /* FIXME UTF-8 doesn't work here ? */
2273 if( p_track->b_mac_encoding )
2274 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
2276 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
2279 case VLC_FOURCC('y','v','1','2'):
2280 p_track->fmt.i_codec = VLC_CODEC_YV12;
2282 case VLC_FOURCC('y','u','v','2'):
2283 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
2286 case VLC_FOURCC('i','n','2','4'):
2287 p_track->fmt.i_codec = p_enda && BOXDATA(p_enda)->i_little_endian == 1 ?
2288 VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
2290 case VLC_FOURCC('i','n','3','2'):
2291 p_track->fmt.i_codec = p_enda && BOXDATA(p_enda)->i_little_endian == 1 ?
2292 VLC_CODEC_S32L : VLC_CODEC_S32B;
2294 case VLC_FOURCC('f','l','3','2'):
2295 p_track->fmt.i_codec = p_enda && BOXDATA(p_enda)->i_little_endian == 1 ?
2296 VLC_CODEC_F32L : VLC_CODEC_F32B;
2298 case VLC_FOURCC('f','l','6','4'):
2299 p_track->fmt.i_codec = p_enda && BOXDATA(p_enda)->i_little_endian == 1 ?
2300 VLC_CODEC_F64L : VLC_CODEC_F64B;
2302 case VLC_CODEC_DVD_LPCM:
2304 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
2305 if( p_soun->i_qt_version == 2 &&
2306 p_soun->i_qt_description > 20 + 28 )
2313 static const struct {
2317 vlc_fourcc_t i_codec;
2319 { 0x01, 0x03, 32, VLC_CODEC_F32L },
2320 { 0x01, 0x03, 64, VLC_CODEC_F64L },
2321 { 0x01|0x02, 0x03, 32, VLC_CODEC_F32B },
2322 { 0x01|0x02, 0x03, 64, VLC_CODEC_F64B },
2324 { 0x00, 0x05, 8, VLC_CODEC_U8 },
2325 { 0x00| 0x04, 0x05, 8, VLC_CODEC_S8 },
2327 { 0x00, 0x07, 16, VLC_CODEC_U16L },
2328 { 0x00|0x02, 0x07, 16, VLC_CODEC_U16B },
2329 { 0x00 |0x04, 0x07, 16, VLC_CODEC_S16L },
2330 { 0x00|0x02|0x04, 0x07, 16, VLC_CODEC_S16B },
2332 { 0x00, 0x07, 24, VLC_CODEC_U24L },
2333 { 0x00|0x02, 0x07, 24, VLC_CODEC_U24B },
2334 { 0x00 |0x04, 0x07, 24, VLC_CODEC_S24L },
2335 { 0x00|0x02|0x04, 0x07, 24, VLC_CODEC_S24B },
2337 { 0x00, 0x07, 32, VLC_CODEC_U32L },
2338 { 0x00|0x02, 0x07, 32, VLC_CODEC_U32B },
2339 { 0x00 |0x04, 0x07, 32, VLC_CODEC_S32L },
2340 { 0x00|0x02|0x04, 0x07, 32, VLC_CODEC_S32B },
2344 uint32_t i_bits = GetDWBE(&p_soun->p_qt_description[20 + 20]);
2345 uint32_t i_flags = GetDWBE(&p_soun->p_qt_description[20 + 24]);
2347 for( int i = 0; p_formats[i].i_codec; i++ )
2349 if( p_formats[i].i_bits == i_bits &&
2350 (i_flags & p_formats[i].i_mask) == p_formats[i].i_flags )
2352 p_track->fmt.i_codec = p_formats[i].i_codec;
2353 p_track->fmt.audio.i_bitspersample = i_bits;
2354 p_track->fmt.audio.i_blockalign = p_soun->i_channelcount * i_bits / 8;
2355 p_track->i_sample_size = p_track->fmt.audio.i_blockalign;
2357 p_soun->i_qt_version = 0;
2365 p_track->fmt.i_codec = p_sample->i_type;
2369 /* now see if esds is present and if so create a data packet
2370 with decoder_specific_info */
2371 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
2372 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
2373 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
2374 ( p_esds->data.p_esds )&&
2377 /* First update information based on i_objectTypeIndication */
2378 switch( p_decconfig->i_objectTypeIndication )
2380 case( 0x20 ): /* MPEG4 VIDEO */
2381 p_track->fmt.i_codec = VLC_CODEC_MP4V;
2383 case( 0x21 ): /* H.264 */
2384 p_track->fmt.i_codec = VLC_CODEC_H264;
2387 p_track->fmt.i_codec = VLC_CODEC_MP4A;
2388 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
2389 p_decconfig->p_decoder_specific_info[0] == 0xF8 &&
2390 (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
2392 p_track->fmt.i_codec = VLC_CODEC_ALS;
2400 case( 0x65): /* MPEG2 video */
2401 p_track->fmt.i_codec = VLC_CODEC_MPGV;
2403 /* Theses are MPEG2-AAC */
2404 case( 0x66): /* main profile */
2405 case( 0x67): /* Low complexity profile */
2406 case( 0x68): /* Scaleable Sampling rate profile */
2407 p_track->fmt.i_codec = VLC_CODEC_MP4A;
2409 /* True MPEG 2 audio */
2411 p_track->fmt.i_codec = VLC_CODEC_MPGA;
2413 case( 0x6a): /* MPEG1 video */
2414 p_track->fmt.i_codec = VLC_CODEC_MPGV;
2416 case( 0x6b): /* MPEG1 audio */
2417 p_track->fmt.i_codec = VLC_CODEC_MPGA;
2419 case( 0x6c ): /* jpeg */
2420 p_track->fmt.i_codec = VLC_CODEC_JPEG;
2422 case( 0x6d ): /* png */
2423 p_track->fmt.i_codec = VLC_CODEC_PNG;
2425 case( 0x6e ): /* jpeg2000 */
2426 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
2428 case( 0xa3 ): /* vc1 */
2429 p_track->fmt.i_codec = VLC_CODEC_VC1;
2432 p_track->fmt.i_codec = VLC_CODEC_DIRAC;
2435 p_track->fmt.i_codec = VLC_CODEC_A52;
2438 p_track->fmt.i_codec = VLC_CODEC_EAC3;
2440 case( 0xa9 ): /* dts */
2441 case( 0xaa ): /* DTS-HD HRA */
2442 case( 0xab ): /* DTS-HD Master Audio */
2443 p_track->fmt.i_codec = VLC_CODEC_DTS;
2446 p_track->fmt.i_codec = VLC_CODEC_VORBIS;
2450 case( 0xe0 ): /* NeroDigital: dvd subs */
2451 if( p_track->fmt.i_cat == SPU_ES )
2453 p_track->fmt.i_codec = VLC_CODEC_SPU;
2454 if( p_track->i_width > 0 )
2455 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
2456 if( p_track->i_height > 0 )
2457 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
2460 case( 0xe1 ): /* QCelp for 3gp */
2461 if( p_track->fmt.i_cat == AUDIO_ES )
2463 p_track->fmt.i_codec = VLC_CODEC_QCELP;
2469 /* Unknown entry, but don't touch i_fourcc */
2471 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
2472 p_decconfig->i_objectTypeIndication,
2473 p_track->i_track_ID );
2476 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
2477 if( p_track->fmt.i_extra > 0 )
2479 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2480 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
2481 p_track->fmt.i_extra );
2483 if( p_track->fmt.i_codec == VLC_CODEC_SPU &&
2484 p_track->fmt.i_extra >= 16 * 4 )
2486 for( int i = 0; i < 16; i++ )
2488 p_track->fmt.subs.spu.palette[1 + i] =
2489 GetDWBE((char*)p_track->fmt.p_extra + i * 4);
2491 p_track->fmt.subs.spu.palette[0] = 0xBeef;
2496 switch( p_sample->i_type )
2498 /* qt decoder, send the complete chunk */
2499 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
2500 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
2501 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
2502 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
2503 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
2504 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
2505 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
2506 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
2507 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
2508 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
2509 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
2510 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
2511 p_track->fmt.i_codec = VLC_CODEC_MPGV;
2513 /* qt decoder, send the complete chunk */
2514 case VLC_CODEC_SVQ1:
2515 case VLC_CODEC_SVQ3:
2516 case VLC_FOURCC( 'V', 'P', '3', '1' ):
2517 case VLC_FOURCC( '3', 'I', 'V', '1' ):
2518 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
2519 p_track->fmt.i_extra =
2520 p_sample->data.p_sample_vide->i_qt_image_description;
2521 if( p_track->fmt.i_extra > 0 )
2523 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2524 memcpy( p_track->fmt.p_extra,
2525 p_sample->data.p_sample_vide->p_qt_image_description,
2526 p_track->fmt.i_extra);
2530 case VLC_CODEC_AMR_NB:
2531 p_track->fmt.audio.i_rate = 8000;
2533 case VLC_CODEC_AMR_WB:
2534 p_track->fmt.audio.i_rate = 16000;
2536 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
2537 case VLC_CODEC_QDM2:
2538 case VLC_CODEC_ALAC:
2539 p_track->fmt.i_extra =
2540 p_sample->data.p_sample_soun->i_qt_description;
2541 if( p_track->fmt.i_extra > 0 )
2543 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2544 memcpy( p_track->fmt.p_extra,
2545 p_sample->data.p_sample_soun->p_qt_description,
2546 p_track->fmt.i_extra);
2548 if( p_track->fmt.i_extra == 56 && p_sample->i_type == VLC_CODEC_ALAC )
2550 p_track->fmt.audio.i_channels = *((uint8_t*)p_track->fmt.p_extra + 41);
2551 p_track->fmt.audio.i_rate = GetDWBE((uint8_t*)p_track->fmt.p_extra + 52);
2555 case VLC_FOURCC( 'v', 'c', '-', '1' ):
2557 MP4_Box_t *p_dvc1 = MP4_BoxGet( p_sample, "dvc1" );
2560 p_track->fmt.i_extra = BOXDATA(p_dvc1)->i_vc1;
2561 if( p_track->fmt.i_extra > 0 )
2563 p_track->fmt.p_extra = malloc( BOXDATA(p_dvc1)->i_vc1 );
2564 memcpy( p_track->fmt.p_extra, BOXDATA(p_dvc1)->p_vc1,
2565 p_track->fmt.i_extra );
2570 msg_Err( p_demux, "missing dvc1" );
2575 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
2576 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
2578 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
2582 p_track->fmt.i_extra = BOXDATA(p_avcC)->i_avcC;
2583 if( p_track->fmt.i_extra > 0 )
2585 p_track->fmt.p_extra = malloc( BOXDATA(p_avcC)->i_avcC );
2586 memcpy( p_track->fmt.p_extra, BOXDATA(p_avcC)->p_avcC,
2587 p_track->fmt.i_extra );
2592 msg_Err( p_demux, "missing avcC" );
2596 case VLC_FOURCC( 'h', 'v', 'c', '1' ):
2597 case VLC_FOURCC( 'h', 'e', 'v', '1' ):
2599 MP4_Box_t *p_hvcC = MP4_BoxGet( p_sample, "hvcC" );
2603 p_track->fmt.i_extra = p_hvcC->data.p_hvcC->i_hvcC;
2604 if( p_track->fmt.i_extra > 0 )
2606 p_track->fmt.p_extra = malloc( p_hvcC->data.p_hvcC->i_hvcC );
2607 memcpy( p_track->fmt.p_extra, p_hvcC->data.p_hvcC->p_hvcC,
2608 p_track->fmt.i_extra );
2610 p_track->fmt.i_codec = VLC_CODEC_HEVC;
2614 msg_Err( p_demux, "missing hvcC" );
2620 case VLC_CODEC_ADPCM_MS:
2621 case VLC_CODEC_ADPCM_IMA_WAV:
2622 case VLC_CODEC_QCELP:
2623 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
2627 msg_Dbg( p_demux, "Unrecognized FourCC %4.4s", (char *)&p_sample->i_type );
2635 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2640 /* given a time it return sample/chunk
2641 * it also update elst field of the track
2643 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2644 int64_t i_start, uint32_t *pi_chunk,
2645 uint32_t *pi_sample )
2647 demux_sys_t *p_sys = p_demux->p_sys;
2648 MP4_Box_t *p_box_stss;
2650 unsigned int i_sample;
2651 unsigned int i_chunk;
2654 /* FIXME see if it's needed to check p_track->i_chunk_count */
2655 if( p_track->i_chunk_count == 0 )
2656 return( VLC_EGENERIC );
2658 /* handle elst (find the correct one) */
2659 MP4_TrackSetELST( p_demux, p_track, i_start );
2660 if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
2662 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
2663 int64_t i_mvt= i_start * p_sys->i_timescale / CLOCK_FREQ;
2665 /* now calculate i_start for this elst */
2667 i_start -= p_track->i_elst_time * CLOCK_FREQ / p_sys->i_timescale;
2675 /* to track time scale */
2676 i_start = i_start * p_track->i_timescale / CLOCK_FREQ;
2677 /* add elst offset */
2678 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2679 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2680 elst->i_media_time[p_track->i_elst] > 0 )
2682 i_start += elst->i_media_time[p_track->i_elst];
2685 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2686 "ms (track)", p_track->i_elst,
2687 i_mvt * 1000 / p_sys->i_timescale,
2688 i_start * 1000 / p_track->i_timescale );
2692 /* convert absolute time to in timescale unit */
2693 i_start = i_start * p_track->i_timescale / CLOCK_FREQ;
2696 /* we start from sample 0/chunk 0, hope it won't take too much time */
2697 /* *** find good chunk *** */
2698 for( i_chunk = 0; ; i_chunk++ )
2700 if( i_chunk + 1 >= p_track->i_chunk_count )
2702 /* at the end and can't check if i_start in this chunk,
2703 it will be check while searching i_sample */
2704 i_chunk = p_track->i_chunk_count - 1;
2708 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2709 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
2715 /* *** find sample in the chunk *** */
2716 i_sample = p_track->chunk[i_chunk].i_sample_first;
2717 i_dts = p_track->chunk[i_chunk].i_first_dts;
2718 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2721 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2722 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2725 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2726 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2728 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2733 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2737 i_sample += ( i_start - i_dts ) /
2738 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2743 if( i_sample >= p_track->i_sample_count )
2745 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2746 "(seeking too far) chunk=%d sample=%d",
2747 p_track->i_track_ID, i_chunk, i_sample );
2748 return( VLC_EGENERIC );
2752 /* *** Try to find nearest sync points *** */
2753 if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2755 MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2756 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2757 p_track->i_track_ID );
2758 for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2760 if( i_index >= p_stss->i_entry_count - 1 ||
2761 i_sample < p_stss->i_sample_number[i_index+1] )
2763 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2764 msg_Dbg( p_demux, "stss gives %d --> %d (sample number)",
2765 i_sample, i_sync_sample );
2767 if( i_sync_sample <= i_sample )
2769 while( i_chunk > 0 &&
2770 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2775 while( i_chunk < p_track->i_chunk_count - 1 &&
2776 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2777 p_track->chunk[i_chunk].i_sample_count )
2780 i_sample = i_sync_sample;
2787 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2788 "Sample Box (stss)", p_track->i_track_ID );
2791 *pi_chunk = i_chunk;
2792 *pi_sample = i_sample;
2797 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2798 unsigned int i_chunk, unsigned int i_sample )
2800 bool b_reselect = false;
2802 /* now see if actual es is ok */
2803 if( p_track->i_chunk >= p_track->i_chunk_count ||
2804 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2805 p_track->chunk[i_chunk].i_sample_description_index )
2807 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2808 p_track->i_track_ID );
2810 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2811 p_track->p_es, &b_reselect );
2813 es_out_Del( p_demux->out, p_track->p_es );
2815 p_track->p_es = NULL;
2817 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2819 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2820 p_track->i_track_ID );
2822 p_track->b_ok = false;
2823 p_track->b_selected = false;
2824 return VLC_EGENERIC;
2828 /* select again the new decoder */
2831 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2834 p_track->i_chunk = i_chunk;
2835 p_track->i_sample = i_sample;
2837 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2840 /****************************************************************************
2842 ****************************************************************************
2843 * Parse track information and create all needed data to run a track
2844 * If it succeed b_ok is set to 1 else to 0
2845 ****************************************************************************/
2846 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2847 MP4_Box_t *p_box_trak,
2848 bool b_force_enable )
2850 demux_sys_t *p_sys = p_demux->p_sys;
2852 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2853 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2863 char language[4] = { '\0' };
2865 /* hint track unsupported */
2867 /* set default value (-> track unusable) */
2868 p_track->b_ok = false;
2869 p_track->b_enable = false;
2870 p_track->b_selected = false;
2871 p_track->b_chapter = false;
2872 p_track->b_mac_encoding = false;
2874 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2881 /* do we launch this track by default ? */
2883 ( ( BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED ) != 0 );
2884 if( !p_track->b_enable )
2885 p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
2887 p_track->i_track_ID = BOXDATA(p_tkhd)->i_track_ID;
2889 p_track->i_width = BOXDATA(p_tkhd)->i_width / BLOCK16x16;
2890 p_track->i_height = BOXDATA(p_tkhd)->i_height / BLOCK16x16;
2891 p_track->f_rotation = BOXDATA(p_tkhd)->f_rotation;
2895 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2898 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2899 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2901 if( ( !p_mdhd )||( !p_hdlr ) )
2906 p_track->i_timescale = BOXDATA(p_mdhd)->i_timescale;
2907 if( p_track->i_timescale == 0 )
2910 if( BOXDATA(p_mdhd)->i_language_code < 0x400 )
2912 strcpy( language, MP4_ConvertMacCode( BOXDATA(p_mdhd)->i_language_code ) );
2913 p_track->b_mac_encoding = true;
2915 else if( BOXDATA(p_mdhd)->i_language_code == 0x7fff )
2916 p_track->b_mac_encoding = true;
2919 for( unsigned i = 0; i < 3; i++ )
2920 language[i] = BOXDATA(p_mdhd)->i_language[i];
2924 switch( p_hdlr->data.p_hdlr->i_handler_type )
2927 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2931 p_track->fmt.i_cat = AUDIO_ES;
2935 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2939 p_track->fmt.i_cat = VIDEO_ES;
2946 p_track->fmt.i_codec = VLC_CODEC_TX3G;
2947 p_track->fmt.i_cat = SPU_ES;
2954 p_track->i_elst = 0;
2955 p_track->i_elst_time = 0;
2956 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2958 MP4_Box_data_elst_t *elst = BOXDATA(p_elst);
2961 msg_Warn( p_demux, "elst box found" );
2962 for( i = 0; i < elst->i_entry_count; i++ )
2964 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2965 "ms) rate=%d.%d", i,
2966 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2967 elst->i_media_time[i] >= 0 ?
2968 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2970 elst->i_media_rate_integer[i],
2971 elst->i_media_rate_fraction[i] );
2978 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2980 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2981 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2987 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2989 p_track->fmt.psz_language = strdup( language );
2992 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2995 MP4_Box_t *p_box_iter;
2996 for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2997 p_box_iter = p_box_iter->p_next )
2999 switch( p_box_iter->i_type )
3002 p_track->fmt.psz_description =
3003 strdup( p_box_iter->data.p_0xa9xxx->psz_text );
3006 p_track->fmt.psz_description =
3007 strdup( p_box_iter->data.p_name->psz_text );
3013 /* Create chunk index table and sample index table */
3014 if( TrackCreateChunksIndex( p_demux,p_track ) ||
3015 TrackCreateSamplesIndex( p_demux, p_track ) )
3017 msg_Err( p_demux, "cannot create chunks index" );
3018 return; /* cannot create chunks index */
3021 p_track->i_chunk = 0;
3022 p_track->i_sample = 0;
3024 /* Mark chapter only track */
3025 if( p_sys->p_tref_chap )
3027 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
3030 for( i = 0; i < p_chap->i_entry_count; i++ )
3032 if( p_track->i_track_ID == p_chap->i_track_ID[i] &&
3033 p_track->fmt.i_cat == UNKNOWN_ES )
3035 p_track->b_chapter = true;
3036 p_track->b_enable = false;
3043 if( b_force_enable &&
3044 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
3046 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
3047 p_track->i_track_ID );
3048 p_track->b_enable = true;
3049 p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN;
3052 p_track->p_es = NULL;
3053 if( TrackCreateES( p_demux,
3054 p_track, p_track->i_chunk,
3055 p_track->b_chapter ? NULL : &p_track->p_es ) )
3057 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
3058 p_track->i_track_ID );
3061 p_track->b_ok = true;
3065 for( i = 0; i < p_track->i_chunk_count; i++ )
3067 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
3068 i, p_track->chunk[i].i_sample_count,
3069 p_track->chunk[i].i_first_dts );
3076 static void FreeAndResetChunk( mp4_chunk_t *ck )
3078 free( ck->p_sample_count_dts );
3079 free( ck->p_sample_delta_dts );
3080 free( ck->p_sample_count_pts );
3081 free( ck->p_sample_offset_pts );
3082 free( ck->p_sample_size );
3083 for( uint32_t i = 0; i < ck->i_sample_count; i++ )
3084 free( ck->p_sample_data[i] );
3085 free( ck->p_sample_data );
3086 memset( ck, 0, sizeof( mp4_chunk_t ) );
3089 /****************************************************************************
3091 ****************************************************************************
3092 * Destroy a track created by MP4_TrackCreate.
3093 ****************************************************************************/
3094 static void MP4_TrackDestroy( mp4_track_t *p_track )
3096 unsigned int i_chunk;
3098 p_track->b_ok = false;
3099 p_track->b_enable = false;
3100 p_track->b_selected = false;
3102 es_format_Clean( &p_track->fmt );
3104 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
3106 if( p_track->chunk )
3108 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
3109 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
3111 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
3112 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
3115 FREENULL( p_track->chunk );
3116 if( p_track->cchunk ) {
3117 FreeAndResetChunk( p_track->cchunk );
3118 FREENULL( p_track->cchunk );
3121 if( !p_track->i_sample_size )
3123 FREENULL( p_track->p_sample_size );
3127 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
3130 if( !p_track->b_ok || p_track->b_chapter )
3132 return VLC_EGENERIC;
3135 if( p_track->b_selected )
3137 msg_Warn( p_demux, "track[Id 0x%x] already selected",
3138 p_track->i_track_ID );
3142 return MP4_TrackSeek( p_demux, p_track, i_start );
3145 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
3147 if( !p_track->b_ok || p_track->b_chapter )
3152 if( !p_track->b_selected )
3154 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
3155 p_track->i_track_ID );
3160 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
3161 p_track->p_es, false );
3164 p_track->b_selected = false;
3167 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
3173 if( !p_track->b_ok || p_track->b_chapter )
3174 return VLC_EGENERIC;
3176 p_track->b_selected = false;
3178 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
3179 &i_chunk, &i_sample ) )
3181 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
3182 p_track->i_track_ID );
3183 return VLC_EGENERIC;
3186 p_track->b_selected = true;
3188 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
3189 p_track->b_selected = true;
3191 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
3196 * 3 types: for audio
3199 #define QT_V0_MAX_SAMPLES 1024
3200 static uint32_t MP4_TrackSampleSize( mp4_track_t *p_track, uint32_t *pi_nb_samples )
3203 MP4_Box_data_sample_soun_t *p_soun;
3205 if( p_track->i_sample_size == 0 )
3207 /* most simple case */
3209 return p_track->p_sample_size[p_track->i_sample];
3212 if( p_track->fmt.i_cat != AUDIO_ES )
3215 return p_track->i_sample_size;
3218 p_soun = p_track->p_sample->data.p_sample_soun;
3220 if( p_soun->i_qt_version == 1 )
3222 uint32_t i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
3223 if( p_track->fmt.audio.i_blockalign > 1 )
3224 i_samples = p_soun->i_sample_per_packet;
3225 i_size = i_samples / p_soun->i_sample_per_packet;
3226 if ( UINT32_MAX / i_size >= p_soun->i_bytes_per_frame )
3227 i_size *= p_soun->i_bytes_per_frame;
3229 i_size = UINT32_MAX;
3230 *pi_nb_samples = i_samples;
3232 else if( p_track->i_sample_size > 256 )
3234 /* We do that so we don't read too much data
3235 * (in this case we are likely dealing with compressed data) */
3236 i_size = p_track->i_sample_size;
3241 mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
3242 int64_t i_length = CLOCK_FREQ / 10;
3243 uint32_t i_samples = 0;
3244 uint32_t i_maxsamples = 0;
3245 for( uint32_t i=p_track->i_sample; i<p_chunk->i_sample_count; i++ )
3247 i_length -= CLOCK_FREQ * p_chunk->p_sample_delta_dts[i] / p_track->i_timescale;
3248 if ( i_length < 0 ) break;
3252 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
3253 i_samples = __MIN( i_maxsamples, i_samples );
3254 if ( i_samples == 0 ) i_samples = 1;
3255 i_size = i_samples * p_track->i_sample_size;
3256 *pi_nb_samples = i_samples;
3259 //fprintf( stderr, "size=%d\n", i_size );
3263 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
3265 unsigned int i_sample;
3268 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
3270 if( p_track->i_sample_size )
3272 MP4_Box_data_sample_soun_t *p_soun =
3273 p_track->p_sample->data.p_sample_soun;
3275 if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
3277 i_pos += ( p_track->i_sample -
3278 p_track->chunk[p_track->i_chunk].i_sample_first ) *
3279 p_track->i_sample_size;
3283 /* we read chunk by chunk unless a blockalign is requested */
3284 if( p_track->fmt.audio.i_blockalign > 1 )
3285 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
3286 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
3291 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
3292 i_sample < p_track->i_sample; i_sample++ )
3294 i_pos += p_track->p_sample_size[i_sample];
3301 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track, uint32_t i_samples )
3303 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
3305 MP4_Box_data_sample_soun_t *p_soun;
3307 p_soun = p_track->p_sample->data.p_sample_soun;
3309 if( p_soun->i_qt_version == 1 )
3311 /* we read chunk by chunk unless a blockalign is requested */
3312 if( p_track->fmt.audio.i_blockalign > 1 )
3313 p_track->i_sample += p_soun->i_sample_per_packet;
3315 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
3317 else if( p_track->i_sample_size > 256 )
3319 /* We do that so we don't read too much data
3320 * (in this case we are likely dealing with compressed data) */
3321 p_track->i_sample += 1;
3326 p_track->i_sample += i_samples;
3327 if( p_track->i_sample >
3328 p_track->chunk[p_track->i_chunk].i_sample_first +
3329 p_track->chunk[p_track->i_chunk].i_sample_count )
3332 p_track->chunk[p_track->i_chunk].i_sample_first +
3333 p_track->chunk[p_track->i_chunk].i_sample_count;
3339 p_track->i_sample++;
3342 if( p_track->i_sample >= p_track->i_sample_count )
3343 return VLC_EGENERIC;
3345 /* Have we changed chunk ? */
3346 if( p_track->i_sample >=
3347 p_track->chunk[p_track->i_chunk].i_sample_first +
3348 p_track->chunk[p_track->i_chunk].i_sample_count )
3350 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
3351 p_track->i_sample ) )
3353 msg_Warn( p_demux, "track[0x%x] will be disabled "
3354 "(cannot restart decoder)", p_track->i_track_ID );
3355 MP4_TrackUnselect( p_demux, p_track );
3356 return VLC_EGENERIC;
3360 /* Have we changed elst */
3361 if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
3363 demux_sys_t *p_sys = p_demux->p_sys;
3364 MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
3365 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
3366 p_sys->i_timescale / CLOCK_FREQ;
3368 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
3369 i_mvt >= p_track->i_elst_time +
3370 elst->i_segment_duration[p_track->i_elst] )
3372 MP4_TrackSetELST( p_demux, p_track,
3373 MP4_TrackGetDTS( p_demux, p_track ) );
3380 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
3383 demux_sys_t *p_sys = p_demux->p_sys;
3384 int i_elst_last = tk->i_elst;
3386 /* handle elst (find the correct one) */
3388 tk->i_elst_time = 0;
3389 if( tk->p_elst && tk->BOXDATA(p_elst)->i_entry_count > 0 )
3391 MP4_Box_data_elst_t *elst = tk->BOXDATA(p_elst);
3392 int64_t i_mvt= i_time * p_sys->i_timescale / CLOCK_FREQ;
3394 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
3396 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
3398 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
3402 tk->i_elst_time += i_dur;
3405 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
3407 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
3408 tk->i_elst = elst->i_entry_count - 1;
3409 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
3412 if( elst->i_media_time[tk->i_elst] < 0 )
3415 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
3418 if( i_elst_last != tk->i_elst )
3420 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
3425 static const char *MP4_ConvertMacCode( uint16_t i_code )
3427 static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
3428 { "en", 0 }, { "fr", 1 }, { "de", 2 }, { "it", 3 }, { "nl", 4 },
3429 { "sv", 5 }, { "es", 6 }, { "da", 7 }, { "pt", 8 }, { "no", 9 },
3430 { "he", 10 }, { "ja", 11 }, { "ar", 12 }, { "fi", 13 }, { "el", 14 },
3431 { "is", 15 }, { "mt", 16 }, { "tr", 17 }, { "hr", 18 }, { "zh", 19 },
3432 { "ur", 20 }, { "hi", 21 }, { "th", 22 }, { "ko", 23 }, { "lt", 24 },
3433 { "pl", 25 }, { "hu", 26 }, { "et", 27 }, { "lv", 28 }, //{ "??", 29 },
3434 { "fo", 30 }, { "fa", 31 }, { "ru", 32 }, { "zh", 33 }, { "nl", 34 },
3435 { "ga", 35 }, { "sq", 36 }, { "ro", 37 }, { "cs", 38 }, { "sk", 39 },
3436 { "sl", 40 }, { "yi", 41 }, { "sr", 42 }, { "mk", 43 }, { "bg", 44 },
3437 { "uk", 45 }, { "be", 46 }, { "uz", 47 }, { "az", 48 }, { "kk", 48 },
3438 { "az", 50 }, { "hy", 51 }, { "ka", 52 }, { "mo", 53 }, { "ky", 54 },
3439 { "tg", 55 }, { "tk", 56 }, { "mn", 57 }, { "mn", 58 }, { "ps", 59 },
3440 { "ku", 60 }, { "ks", 61 }, { "sd", 62 }, { "bo", 63 }, { "ne", 64 },
3441 { "sa", 65 }, { "mr", 66 }, { "bn", 67 }, { "as", 68 }, { "gu", 69 },
3442 { "pa", 70 }, { "or", 71 }, { "ml", 72 }, { "kn", 73 }, { "ta", 74 },
3443 { "te", 75 }, { "si", 76 }, { "my", 77 }, { "km", 78 }, { "lo", 79 },
3444 { "vi", 80 }, { "id", 81 }, { "tl", 82 }, { "ms", 83 }, { "ms", 84 },
3445 { "am", 85 }, { "ti", 86 }, { "om", 87 }, { "so", 88 }, { "sw", 89 },
3446 { "rw", 90 }, { "rn", 91 }, { "ny", 92 }, { "mg", 93 }, { "eo", 94 },
3448 { "cy", 128 }, { "eu", 129 },
3449 { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
3450 { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
3451 { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
3452 { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
3457 for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
3459 if( p_cvt[i].i_code == i_code )
3460 return p_cvt[i].psz_iso639_1;
3465 /******************************************************************************
3466 * Here are the functions used for fragmented MP4
3467 *****************************************************************************/
3470 * It computes the sample rate for a video track using current video chunk
3472 static void ChunkGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
3473 const mp4_track_t *p_track )
3475 if( p_track->cchunk->i_last_dts == 0 )
3482 const mp4_chunk_t *p_chunk = p_track->cchunk;
3484 uint32_t i_sample = p_chunk->i_sample_count;
3485 uint64_t i_first_dts = p_chunk->i_first_dts;
3486 uint64_t i_last_dts = p_chunk->i_last_dts;
3488 if( i_sample > 1 && i_first_dts < i_last_dts )
3489 vlc_ureduce( pi_num, pi_den,
3490 ( i_sample - 1) * p_track->i_timescale,
3491 i_last_dts - i_first_dts,
3496 * Build raw avcC box (without the 8 bytes header)
3497 * \return The size of the box.
3499 static int build_raw_avcC( uint8_t **p_extra, const uint8_t *CodecPrivateData,
3500 const unsigned cpd_len )
3503 unsigned sps_len = 0, pps_len = 0;
3504 const uint32_t mark = 0x00000001;
3506 assert( CodecPrivateData[0] == 0 );
3507 assert( CodecPrivateData[1] == 0 );
3508 assert( CodecPrivateData[2] == 0 );
3509 assert( CodecPrivateData[3] == 1 );
3511 uint32_t length = cpd_len + 3;
3512 avcC = calloc( length, 1 );
3513 if( unlikely( avcC == NULL ) )
3516 uint8_t *sps = avcC + 8;
3518 uint32_t candidate = ~mark;
3519 CodecPrivateData += 4;
3520 for( unsigned i = 0; i < cpd_len - 4; i++ )
3522 sps[i] = CodecPrivateData[i];
3523 candidate = (candidate << 8) | CodecPrivateData[i];
3524 if( candidate == mark )
3535 uint8_t *pps = sps + sps_len + 3;
3536 pps_len = cpd_len - sps_len - 4 * 2;
3537 memcpy( pps, CodecPrivateData + sps_len + 4, pps_len );
3540 uint8_t AVCProfileIndication = 0x64;
3541 uint8_t profile_compatibility = 0x40;
3542 uint8_t AVCLevelIndication = 0x1f;
3543 uint8_t lengthSizeMinusOne = 0x03;
3546 avcC[1] = AVCProfileIndication;
3547 avcC[2] = profile_compatibility;
3548 avcC[3] = AVCLevelIndication;
3549 avcC[4] = 0xfc + lengthSizeMinusOne;
3551 avcC[6] = (sps_len & 0xff00)>>8;
3552 avcC[7] = sps_len & 0xff;
3554 avcC[8+sps_len] = 1;
3555 avcC[9+sps_len] = (pps_len & 0xff00) >> 8;
3556 avcC[10+sps_len] = pps_len & 0xff;
3563 * Build a mp4_track_t from a StraBox
3566 static inline int MP4_SetCodecExtraData( es_format_t *fmt, MP4_Box_data_stra_t *p_data )
3568 fmt->i_extra = p_data->cpd_len;
3569 fmt->p_extra = malloc( p_data->cpd_len );
3570 if( unlikely( !fmt->p_extra ) )
3572 memcpy( fmt->p_extra, p_data->CodecPrivateData, p_data->cpd_len );
3576 static int MP4_frg_TrackCreate( demux_t *p_demux, mp4_track_t *p_track, MP4_Box_t *p_stra )
3578 demux_sys_t *p_sys = p_demux->p_sys;
3580 MP4_Box_data_stra_t *p_data = BOXDATA(p_stra);
3582 return VLC_EGENERIC;
3584 p_track->b_ok = true;
3585 p_track->b_selected = false;
3586 p_track->i_sample_count = UINT32_MAX;
3588 p_track->i_timescale = p_sys->i_timescale;
3589 p_track->i_width = p_data->MaxWidth;
3590 p_track->i_height = p_data->MaxHeight;
3591 p_track->i_track_ID = p_data->i_track_ID;
3593 es_format_t *fmt = &p_track->fmt;
3595 return VLC_EGENERIC;
3597 es_format_Init( fmt, p_data->i_es_cat, 0 );
3599 /* Set language FIXME */
3600 fmt->psz_language = strdup( "en" );
3602 fmt->i_original_fourcc = p_data->FourCC;
3603 fmt->i_codec = vlc_fourcc_GetCodec( fmt->i_cat, p_data->FourCC );
3605 uint8_t **p_extra = (uint8_t **)&fmt->p_extra;
3606 /* See http://msdn.microsoft.com/en-us/library/ff728116%28v=vs.90%29.aspx
3607 * for MS weird use of FourCC*/
3608 switch( fmt->i_cat )
3611 if( p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', '1' ) ||
3612 p_data->FourCC == VLC_FOURCC( 'A', 'V', 'C', 'B' ) ||
3613 p_data->FourCC == VLC_FOURCC( 'H', '2', '6', '4' ) )
3615 fmt->i_extra = build_raw_avcC( p_extra,
3616 p_data->CodecPrivateData, p_data->cpd_len );
3620 ret = MP4_SetCodecExtraData( fmt, p_data );
3621 if( ret != VLC_SUCCESS )
3625 fmt->video.i_width = p_data->MaxWidth;
3626 fmt->video.i_height = p_data->MaxHeight;
3627 fmt->video.i_bits_per_pixel = 0x18;
3628 fmt->video.i_visible_width = p_data->MaxWidth;
3629 fmt->video.i_visible_height = p_data->MaxHeight;
3632 ChunkGetESSampleRate( &fmt->video.i_frame_rate,
3633 &fmt->video.i_frame_rate_base, p_track );
3635 if( fmt->video.i_frame_rate_base != 0 )
3637 p_demux->p_sys->f_fps = (float)fmt->video.i_frame_rate /
3638 (float)fmt->video.i_frame_rate_base;
3641 p_demux->p_sys->f_fps = 24;
3646 fmt->audio.i_channels = p_data->Channels;
3647 fmt->audio.i_rate = p_data->SamplingRate;
3648 fmt->audio.i_bitspersample = p_data->BitsPerSample;
3649 fmt->audio.i_blockalign = p_data->nBlockAlign;
3651 fmt->i_bitrate = p_data->Bitrate;
3653 ret = MP4_SetCodecExtraData( fmt, p_data );
3654 if( ret != VLC_SUCCESS )
3667 * Return the track identified by tid
3669 static mp4_track_t *MP4_frg_GetTrackByID( demux_t *p_demux, const uint32_t tid )
3671 demux_sys_t *p_sys = p_demux->p_sys;
3673 mp4_track_t *ret = NULL;
3674 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
3676 ret = &p_sys->track[i];
3677 if( ret->i_track_ID == tid )
3680 msg_Err( p_demux, "MP4_frg_GetTrack: track %"PRIu32" not found!", tid );
3684 static void FlushChunk( demux_t *p_demux, mp4_track_t *tk )
3686 msg_Dbg( p_demux, "Flushing chunk for track id %u", tk->i_track_ID );
3687 mp4_chunk_t *ck = tk->cchunk;
3688 while( ck->i_sample < ck->i_sample_count )
3693 if( ck->p_sample_size == NULL || ck->p_sample_data == NULL )
3696 uint32_t sample_size = ck->p_sample_size[ck->i_sample];
3697 assert( sample_size > 0 );
3698 p_block = block_Alloc( sample_size );
3699 if( unlikely( !p_block ) )
3702 uint8_t *src = ck->p_sample_data[ck->i_sample];
3703 memcpy( p_block->p_buffer, src, sample_size );
3707 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
3709 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
3711 p_block->i_pts = p_block->i_dts + i_delta;
3712 else if( tk->fmt.i_cat != VIDEO_ES )
3713 p_block->i_pts = p_block->i_dts;
3715 p_block->i_pts = VLC_TS_INVALID;
3717 es_out_Send( p_demux->out, tk->p_es, p_block );
3725 * \Note If we call that function too soon,
3726 * before the track has been selected by MP4_TrackSelect
3727 * (during the first execution of Demux), then the track gets disabled
3729 static int ReInitDecoder( demux_t *p_demux, mp4_track_t *p_track )
3731 demux_sys_t *p_sys = p_demux->p_sys;
3733 uint32_t i_sample = 0;
3734 bool b_smooth = false;
3735 MP4_Box_t *p_stra = NULL, *p_trak = NULL;
3737 if( !CmpUUID( &p_sys->p_root->p_first->i_uuid, &SmooBoxUUID ) )
3742 p_stra = MP4_BoxGet( p_sys->p_root, "uuid/uuid[0]" );
3743 if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
3744 return VLC_EGENERIC;
3748 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[0]" );
3750 return VLC_EGENERIC;
3753 i_sample = p_track->i_sample;
3754 es_out_Del( p_demux->out, p_track->p_es );
3755 es_format_Clean( &p_track->fmt );
3758 MP4_frg_TrackCreate( p_demux, p_track, p_stra );
3760 MP4_TrackCreate( p_demux, p_track, p_trak, true );
3762 p_track->i_sample = i_sample;
3764 /* Temporary hack until we support track selection */
3765 p_track->b_selected = true;
3766 p_track->b_ok = true;
3767 p_track->b_enable = true;
3769 p_track->p_es = es_out_Add( p_demux->out, &p_track->fmt );
3770 p_track->b_codec_need_restart = false;
3776 * This function fills a mp4_chunk_t structure from a MP4_Box_t (p_chunk).
3777 * The 'i_tk_id' argument returns the ID of the track the chunk belongs to.
3778 * \note p_chunk usually contains a 'moof' and a 'mdat', and might contain a 'sidx'.
3779 * \return VLC_SUCCESS, VLC_EGENERIC or VLC_ENOMEM.
3781 static int MP4_frg_GetChunk( demux_t *p_demux, MP4_Box_t *p_chunk, unsigned *i_tk_id )
3783 MP4_Box_t *p_sidx = MP4_BoxGet( p_chunk, "sidx" );
3784 MP4_Box_t *p_moof = MP4_BoxGet( p_chunk, "moof" );
3787 msg_Warn( p_demux, "no moof box found!" );
3788 return VLC_EGENERIC;
3791 MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
3794 msg_Warn( p_demux, "no traf box found!" );
3795 return VLC_EGENERIC;
3798 MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
3801 msg_Warn( p_demux, "no tfhd box found!" );
3802 return VLC_EGENERIC;
3805 uint32_t i_track_ID = BOXDATA(p_tfhd)->i_track_ID;
3806 *i_tk_id = i_track_ID;
3807 assert( i_track_ID > 0 );
3808 msg_Dbg( p_demux, "GetChunk: track ID is %"PRIu32"", i_track_ID );
3810 mp4_track_t *p_track = MP4_frg_GetTrackByID( p_demux, i_track_ID );
3812 return VLC_EGENERIC;
3814 mp4_chunk_t *ret = p_track->cchunk;
3816 if( BOXDATA(p_tfhd)->b_empty )
3817 msg_Warn( p_demux, "No samples in this chunk!" );
3819 /* Usually we read 100 ms of each track. However, suppose we have two tracks,
3820 * Ta and Tv (audio and video). Suppose also that Ta is the first track to be
3821 * read, i.e. we read 100 ms of Ta, then 100 ms of Tv, then 100 ms of Ta,
3822 * and so on. Finally, suppose that we get the chunks the other way around,
3823 * i.e. first a chunk of Tv, then a chunk of Ta, then a chunk of Tv, and so on.
3824 * In that case, it is very likely that at some point, Ta->cchunk or Tv->cchunk
3825 * is not emptied when MP4_frg_GetChunks is called. It is therefore necessary to
3826 * flush it, i.e. send to the decoder the samples not yet sent.
3827 * Note that all the samples to be flushed should worth less than 100 ms,
3828 * (though I did not do the formal proof) and thus this flushing mechanism
3829 * should not cause A/V sync issues, or delays or whatever.
3831 if( ret->i_sample < ret->i_sample_count )
3832 FlushChunk( p_demux, p_track );
3834 if( ret->i_sample_count )
3835 FreeAndResetChunk( ret );
3837 uint32_t default_duration = 0;
3838 if( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
3839 default_duration = BOXDATA(p_tfhd)->i_default_sample_duration;
3841 uint32_t default_size = 0;
3842 if( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
3843 default_size = BOXDATA(p_tfhd)->i_default_sample_size;
3845 MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun");
3848 msg_Warn( p_demux, "no trun box found!" );
3849 return VLC_EGENERIC;
3851 MP4_Box_data_trun_t *p_trun_data = p_trun->data.p_trun;
3853 ret->i_sample_count = p_trun_data->i_sample_count;
3854 assert( ret->i_sample_count > 0 );
3855 ret->i_sample_description_index = 1; /* seems to be always 1, is it? */
3856 ret->i_sample_first = p_track->i_sample_first;
3857 p_track->i_sample_first += ret->i_sample_count;
3859 ret->i_first_dts = p_track->i_first_dts;
3861 /* XXX I already saw DASH content with no default_duration and no
3862 * MP4_TRUN_SAMPLE_DURATION flag, but a sidx box was present.
3863 * This chunk of code might be buggy with segments having
3864 * more than one subsegment. */
3865 if( !default_duration )
3867 MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_moof, i_track_ID );
3869 default_duration = BOXDATA(p_trex)->i_default_sample_duration;
3872 MP4_Box_data_sidx_t *p_sidx_data = BOXDATA(p_sidx);
3873 assert( p_sidx_data->i_reference_count == 1 );
3875 if( p_sidx_data->i_timescale == 0 )
3876 return VLC_EGENERIC;
3878 unsigned i_chunk_duration = p_sidx_data->p_items[0].i_subsegment_duration /
3879 p_sidx_data->i_timescale;
3880 default_duration = i_chunk_duration * p_track->i_timescale / ret->i_sample_count;
3885 msg_Dbg( p_demux, "Default sample duration is %"PRIu32, default_duration );
3887 ret->p_sample_count_dts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3888 ret->p_sample_delta_dts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3890 if( !ret->p_sample_count_dts || !ret->p_sample_delta_dts )
3893 ret->p_sample_count_pts = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3894 if( !ret->p_sample_count_pts )
3897 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
3899 ret->p_sample_offset_pts = calloc( ret->i_sample_count, sizeof( int32_t ) );
3900 if( !ret->p_sample_offset_pts )
3904 ret->p_sample_size = calloc( ret->i_sample_count, sizeof( uint32_t ) );
3905 if( !ret->p_sample_size )
3908 ret->p_sample_data = calloc( ret->i_sample_count, sizeof( uint8_t * ) );
3909 if( !ret->p_sample_data )
3912 uint32_t dur = 0, i_mdatlen = 0, len;
3913 uint32_t chunk_duration = 0, chunk_size = 0;
3915 /* Skip header of mdat */
3917 int i_read = stream_Read( p_demux->s, &mdat, 8 );
3918 i_mdatlen = GetDWBE( mdat );
3919 if ( i_read < 8 || i_mdatlen < 8 ||
3920 VLC_FOURCC( mdat[4], mdat[5], mdat[6], mdat[7] ) != ATOM_mdat )
3921 return VLC_EGENERIC;
3923 for( uint32_t i = 0; i < ret->i_sample_count; i++)
3925 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_DURATION )
3926 dur = p_trun_data->p_samples[i].i_duration;
3928 dur = default_duration;
3929 ret->p_sample_delta_dts[i] = dur;
3930 chunk_duration += dur;
3932 ret->p_sample_count_dts[i] = ret->p_sample_count_pts[i] = 1;
3934 if( ret->p_sample_offset_pts )
3936 ret->p_sample_offset_pts[i] =
3937 p_trun_data->p_samples[i].i_composition_time_offset;
3940 if( p_trun_data->i_flags & MP4_TRUN_SAMPLE_SIZE )
3941 len = ret->p_sample_size[i] = p_trun_data->p_samples[i].i_size;
3943 len = ret->p_sample_size[i] = default_size;
3945 if ( chunk_size + len > ( i_mdatlen - 8 ) )
3946 return VLC_EGENERIC;
3948 ret->p_sample_data[i] = malloc( len );
3949 if( ret->p_sample_data[i] == NULL )
3951 uint32_t i_read = stream_ReadU32( p_demux->s, ret->p_sample_data[i], len );
3953 return VLC_EGENERIC;
3956 ret->i_last_dts = ret->i_first_dts + chunk_duration - dur;
3957 p_track->i_first_dts = chunk_duration + ret->i_first_dts;
3959 if( p_track->b_codec_need_restart &&
3960 p_track->fmt.i_cat == VIDEO_ES )
3961 ReInitDecoder( p_demux, p_track );
3963 /* Skip if we didn't reach the end of mdat box */
3964 if ( chunk_size < (i_mdatlen - 8) )
3965 stream_ReadU32( p_demux->s, NULL, i_mdatlen - chunk_size - 8 );
3967 p_track->b_has_non_empty_cchunk = true;
3972 * Get the next chunk of the track identified by i_tk_id.
3973 * \Note We don't want to seek all the time, so if the first chunk given by the
3974 * input method doesn't belong to the right track, we don't throw it away,
3975 * and so, in general, this function fetch more than one chunk.
3976 * Not to mention that a new init segment might be put everywhere
3977 * between two chunks by the input method.
3979 static int MP4_frg_GetChunks( demux_t *p_demux, const unsigned i_tk_id )
3981 demux_sys_t *p_sys = p_demux->p_sys;
3982 mp4_track_t *p_track;
3984 for( unsigned i = 0; i < p_sys->i_tracks; i++ )
3986 MP4_Box_t *p_chunk = MP4_BoxGetNextChunk( p_demux->s );
3988 return VLC_EGENERIC;
3990 if( !p_chunk->p_first )
3991 goto MP4_frg_GetChunks_Error;
3992 uint32_t i_type = p_chunk->p_first->i_type;
3994 if( i_type == ATOM_uuid || i_type == ATOM_ftyp )
3996 MP4_BoxFree( p_demux->s, p_sys->p_root );
3997 p_sys->p_root = p_chunk;
3999 if( i_type == ATOM_ftyp ) /* DASH */
4001 MP4_Box_t *p_tkhd = MP4_BoxGet( p_chunk, "/moov/trak[0]/tkhd" );
4004 msg_Warn( p_demux, "No tkhd found!" );
4005 goto MP4_frg_GetChunks_Error;
4007 tid = BOXDATA(p_tkhd)->i_track_ID;
4009 else /* Smooth Streaming */
4011 assert( !CmpUUID( &p_chunk->p_first->i_uuid, &SmooBoxUUID ) );
4012 MP4_Box_t *p_stra = MP4_BoxGet( p_chunk, "/uuid/uuid[0]" );
4013 if( !p_stra || CmpUUID( &p_stra->i_uuid, &StraBoxUUID ) )
4015 msg_Warn( p_demux, "No StraBox found!" );
4016 goto MP4_frg_GetChunks_Error;
4018 tid = BOXDATA(p_stra)->i_track_ID;
4021 p_track = MP4_frg_GetTrackByID( p_demux, tid );
4023 goto MP4_frg_GetChunks_Error;
4024 p_track->b_codec_need_restart = true;
4026 return MP4_frg_GetChunks( p_demux, i_tk_id );
4029 if( MP4_frg_GetChunk( p_demux, p_chunk, &tid ) != VLC_SUCCESS )
4030 goto MP4_frg_GetChunks_Error;
4032 MP4_BoxFree( p_demux->s, p_chunk );
4034 if( tid == i_tk_id )
4039 MP4_frg_GetChunks_Error:
4040 MP4_BoxFree( p_demux->s, p_chunk );
4041 return VLC_EGENERIC;
4047 static int MP4_frg_TrackSelect( demux_t *p_demux, mp4_track_t *p_track )
4049 if( !p_track->b_ok || p_track->b_chapter )
4051 return VLC_EGENERIC;
4054 if( p_track->b_selected )
4056 msg_Warn( p_demux, "track[Id 0x%x] already selected", p_track->i_track_ID );
4060 msg_Dbg( p_demux, "Select track id %u", p_track->i_track_ID );
4061 p_track->b_selected = true;
4066 /* Keeps an ordered chain of all fragments */
4067 static bool AddFragment( demux_t *p_demux, MP4_Box_t *p_moox )
4069 demux_sys_t *p_sys = p_demux->p_sys;
4070 mp4_fragment_t *p_base_fragment = & p_sys->moovfragment;
4074 if( p_moox->i_type == ATOM_moov )
4076 if ( !p_sys->moovfragment.p_moox )
4078 p_sys->moovfragment.p_moox = p_moox;
4081 if( (p_mvhd = MP4_BoxGet( p_moox, "mvhd" )) )
4083 p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
4084 p_sys->moovfragment.i_duration = BOXDATA(p_mvhd)->i_duration;
4086 msg_Dbg( p_demux, "added fragment %4.4s", (char*) &p_moox->i_type );
4091 else // p_moox->i_type == ATOM_moof
4093 assert(p_moox->i_type == ATOM_moof);
4094 mp4_fragment_t *p_fragment = p_sys->moovfragment.p_next;
4095 while ( p_fragment )
4097 if ( !p_base_fragment->p_moox || p_moox->i_pos > p_base_fragment->p_moox->i_pos )
4099 p_base_fragment = p_fragment;
4100 p_fragment = p_fragment->p_next;
4102 else if ( p_moox->i_pos == p_base_fragment->p_moox->i_pos )
4104 /* already exists */
4110 /* Add the moof fragment */
4111 mp4_fragment_t *p_new = malloc(sizeof(mp4_fragment_t));
4112 if ( !p_new ) return false;
4113 p_new->p_moox = p_moox;
4114 p_new->i_duration = 0;
4115 p_new->p_next = p_base_fragment->p_next;
4116 p_base_fragment->p_next = p_new;
4117 msg_Dbg( p_demux, "added fragment %4.4s", (char*) &p_moox->i_type );
4119 /* we have to probe all fragments :/ */
4120 uint64_t i_traf_base_data_offset = 0;
4121 uint64_t i_traf_min_offset = 0;
4122 uint32_t i_traf = 0;
4123 uint32_t i_traf_total_size = 0;
4124 uint32_t i_trafs_total_size = 0;
4126 MP4_Box_t *p_traf = MP4_BoxGet( p_new->p_moox, "traf" );
4129 if ( p_traf->i_type != ATOM_traf )
4131 p_traf = p_traf->p_next;
4134 const MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
4135 const MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun" );
4136 if ( !p_tfhd || !p_trun )
4138 p_traf = p_traf->p_next;
4142 uint32_t i_track_timescale = 0;
4143 uint32_t i_track_defaultsamplesize = 0;
4144 uint32_t i_track_defaultsampleduration = 0;
4145 if ( p_sys->b_smooth )
4147 /* stra sets identical timescales */
4148 i_track_timescale = p_sys->i_timescale;
4149 i_track_defaultsamplesize = 1;
4150 i_track_defaultsampleduration = 1;
4154 /* set trex for defaults */
4155 MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_sys->moovfragment.p_moox, BOXDATA(p_tfhd)->i_track_ID );
4158 i_track_defaultsamplesize = BOXDATA(p_trex)->i_default_sample_size;
4159 i_track_defaultsampleduration = BOXDATA(p_trex)->i_default_sample_duration;
4161 MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->moovfragment.p_moox, BOXDATA(p_tfhd)->i_track_ID );
4164 MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
4165 if ( p_mdhd ) i_track_timescale = BOXDATA(p_mdhd)->i_timescale;
4169 if ( !i_track_timescale )
4171 p_traf = p_traf->p_next;
4175 if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
4177 i_traf_base_data_offset = BOXDATA(p_tfhd)->i_base_data_offset;
4179 else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DEFAULT_BASE_IS_MOOF )
4181 i_traf_base_data_offset = p_new->p_moox->i_pos + 8;
4186 i_traf_base_data_offset = p_new->p_moox->i_pos /*+ 8*/;
4188 i_traf_base_data_offset += i_traf_total_size;
4191 i_traf_total_size = 0;
4193 uint64_t i_trun_data_offset = i_traf_base_data_offset;
4194 uint64_t i_traf_duration = 0;
4195 uint32_t i_trun_size = 0;
4196 while ( p_trun && p_tfhd )
4198 if ( p_trun->i_type != ATOM_trun )
4200 p_trun = p_trun->p_next;
4203 const MP4_Box_data_trun_t *p_trundata = p_trun->data.p_trun;
4205 /* Get data offset */
4206 if ( p_trundata->i_flags & MP4_TRUN_DATA_OFFSET )
4207 i_trun_data_offset += __MAX( p_trundata->i_data_offset, 0 );
4209 i_trun_data_offset += i_trun_size;
4213 /* Sum total time */
4214 if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_DURATION )
4216 for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
4217 i_traf_duration += p_trundata->p_samples[i].i_duration;
4219 else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
4221 i_traf_duration += p_trundata->i_sample_count *
4222 BOXDATA(p_tfhd)->i_default_sample_duration;
4226 i_traf_duration += p_trundata->i_sample_count *
4227 i_track_defaultsampleduration;
4230 /* Get total traf size */
4231 if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_SIZE )
4233 for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
4234 i_trun_size += p_trundata->p_samples[i].i_size;
4236 else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
4238 i_trun_size += p_trundata->i_sample_count *
4239 BOXDATA(p_tfhd)->i_default_sample_size;
4243 i_trun_size += p_trundata->i_sample_count *
4244 i_track_defaultsamplesize;
4247 i_traf_total_size += i_trun_size;
4249 if ( i_traf_min_offset )
4250 i_traf_min_offset = __MIN( i_trun_data_offset, i_traf_min_offset );
4252 i_traf_min_offset = i_trun_data_offset;
4254 p_trun = p_trun->p_next;
4257 i_trafs_total_size += i_traf_total_size;
4258 p_new->i_duration = __MAX( p_new->i_duration, i_traf_duration * p_sys->i_timescale / i_track_timescale );
4260 p_traf = p_traf->p_next;
4264 p_new->i_chunk_range_min_offset = i_traf_min_offset;
4265 p_new->i_chunk_range_max_offset = i_traf_min_offset + i_trafs_total_size;
4267 msg_Dbg( p_demux, "new fragment is %"PRId64" %"PRId64, p_new->i_chunk_range_min_offset, p_new->i_chunk_range_max_offset );
4269 /* compute total duration with that new fragment if no overall provided */
4270 MP4_Box_t *p_mehd = MP4_BoxGet( p_sys->moovfragment.p_moox, "mvex/mehd");
4273 p_sys->i_overall_duration = 0;
4274 p_new = &p_sys->moovfragment;
4277 p_sys->i_overall_duration += p_new->i_duration;
4278 p_new = p_new->p_next;
4282 p_sys->i_overall_duration = BOXDATA(p_mehd)->i_fragment_duration;
4284 msg_Dbg( p_demux, "total fragments duration %"PRId64, CLOCK_FREQ * p_sys->i_overall_duration / p_sys->i_timescale);
4288 static int ProbeFragments( demux_t *p_demux )
4290 demux_sys_t *p_sys = p_demux->p_sys;
4291 uint64_t i_current_pos;
4293 if ( MP4_stream_Tell( p_demux->s, &i_current_pos ) )
4294 msg_Dbg( p_demux, "probing fragments from %"PRId64, i_current_pos );
4296 assert( p_sys->p_root );
4298 MP4_ReadBoxContainerRaw( p_demux->s, p_sys->p_root ); /* Get the rest of the file */
4300 MP4_Box_t *p_moov = MP4_BoxGet( p_sys->p_root, "/moov" );
4303 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
4304 return VLC_EGENERIC;
4306 AddFragment( p_demux, p_moov );
4308 MP4_Box_t *p_moof = MP4_BoxGet( p_sys->p_root, "moof" );
4311 if ( p_moof->i_type == ATOM_moof )
4312 AddFragment( p_demux, p_moof );
4313 p_moof = p_moof->p_next;
4316 p_sys->b_fragments_probed = true;
4318 MP4_Box_t *p_mdat = MP4_BoxGet( p_sys->p_root, "mdat" );
4320 stream_Seek( p_demux->s, p_mdat->i_pos );
4321 msg_Dbg( p_demux, "rewinding to mdat %"PRId64, p_mdat->i_pos );
4327 * DemuxFrg: read packet and send them to decoders
4328 * \return 1 on success, 0 on error.
4329 * TODO check for newly selected track
4331 int DemuxFrg( demux_t *p_demux )
4333 demux_sys_t *p_sys = p_demux->p_sys;
4335 unsigned i_track_selected;
4337 /* check for newly selected/unselected track */
4338 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
4340 mp4_track_t *tk = &p_sys->track[i_track];
4343 if( !tk->b_ok || tk->b_chapter )
4346 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
4347 msg_Dbg( p_demux, "track %u %s!", tk->i_track_ID, b ? "enabled" : "disabled" );
4349 if( tk->b_selected && !b )
4351 MP4_TrackUnselect( p_demux, tk );
4353 else if( !tk->b_selected && b)
4355 MP4_frg_TrackSelect( p_demux, tk );
4358 if( tk->b_selected )
4362 if( i_track_selected <= 0 )
4364 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
4365 if( p_sys->i_timescale > 0 )
4367 int64_t i_length = CLOCK_FREQ *
4368 (mtime_t)p_sys->moovfragment.i_duration /
4369 (mtime_t)p_sys->i_timescale;
4370 if( MP4_GetMoviePTS( p_sys ) >= i_length )
4375 msg_Warn( p_demux, "no track selected, exiting..." );
4379 /* first wait for the good time to read a packet */
4380 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
4382 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
4384 /* we will read 100ms for each stream so ...*/
4385 p_sys->i_time += __MAX( p_sys->i_timescale / 10, 1 );
4387 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
4389 mp4_track_t *tk = &p_sys->track[i_track];
4391 if( !tk->b_ok || tk->b_chapter || !tk->b_selected )
4393 msg_Warn( p_demux, "Skipping track id %u", tk->i_track_ID );
4397 if( !tk->b_has_non_empty_cchunk )
4399 if( MP4_frg_GetChunks( p_demux, tk->i_track_ID ) != VLC_SUCCESS )
4401 msg_Info( p_demux, "MP4_frg_GetChunks returned error. End of stream?" );
4406 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
4411 mp4_chunk_t *ck = tk->cchunk;
4412 if( ck->i_sample >= ck->i_sample_count )
4414 msg_Err( p_demux, "sample %"PRIu32" of %"PRIu32"",
4415 ck->i_sample, ck->i_sample_count );
4419 uint32_t sample_size = ck->p_sample_size[ck->i_sample];
4420 p_block = block_Alloc( sample_size );
4421 uint8_t *src = ck->p_sample_data[ck->i_sample];
4422 memcpy( p_block->p_buffer, src, sample_size );
4425 if( ck->i_sample == ck->i_sample_count )
4426 tk->b_has_non_empty_cchunk = false;
4429 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
4431 i_delta = MP4_TrackGetPTSDelta( p_demux, tk );
4433 p_block->i_pts = p_block->i_dts + i_delta;
4434 else if( tk->fmt.i_cat != VIDEO_ES )
4435 p_block->i_pts = p_block->i_dts;
4437 p_block->i_pts = VLC_TS_INVALID;
4439 es_out_Send( p_demux->out, tk->p_es, p_block );
4443 if( !tk->b_has_non_empty_cchunk )