1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004, 2010 the VideoLAN team
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_charset.h> /* EnsureUTF8 */
36 #include <vlc_meta.h> /* vlc_meta_t, vlc_meta_ */
37 #include <vlc_input.h>
41 #include "id3genres.h" /* for ATOM_gnre */
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_category( CAT_INPUT )
51 set_subcategory( SUBCAT_INPUT_DEMUX )
52 set_description( N_("MP4 stream demuxer") )
53 set_shortname( N_("MP4") )
54 set_capability( "demux", 240 )
55 set_callbacks( Open, Close )
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Demux ( demux_t * );
62 static int DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
63 static int Seek ( demux_t *, mtime_t );
64 static int Control ( demux_t *, int, va_list );
66 /* Contain all information about a chunk */
69 uint64_t i_offset; /* absolute position of this chunk in the file */
70 uint32_t i_sample_description_index; /* index for SampleEntry to use */
71 uint32_t i_sample_count; /* how many samples in this chunk */
72 uint32_t i_sample_first; /* index of the first sample in this chunk */
74 /* now provide way to calculate pts, dts, and offset without too
75 much memory and with fast access */
77 /* with this we can calculate dts/pts without waste memory */
78 uint64_t i_first_dts; /* DTS of the first sample */
79 uint64_t i_last_dts; /* DTS of the last sample */
80 uint32_t *p_sample_count_dts;
81 uint32_t *p_sample_delta_dts; /* dts delta */
83 uint32_t *p_sample_count_pts;
84 int32_t *p_sample_offset_pts; /* pts-dts */
86 /* TODO if needed add pts
87 but quickly *add* support for edts and seeking */
91 /* Contain all needed information for read all track with vlc */
94 unsigned int i_track_ID;/* this should be unique */
96 int b_ok; /* The track is usable */
97 int b_enable; /* is the trak enable by default */
98 bool b_selected; /* is the trak being played */
99 bool b_chapter; /* True when used for chapter only */
106 /* display size only ! */
110 /* more internal data */
111 uint64_t i_timescale; /* time scale for this track only */
114 int i_elst; /* current elst */
115 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
116 MP4_Box_t *p_elst; /* elst (could be NULL) */
118 /* give the next sample to read, i_chunk is to find quickly where
119 the sample is located */
120 uint32_t i_sample; /* next sample to read */
121 uint32_t i_chunk; /* chunk where next sample is stored */
122 /* total count of chunk and sample */
123 uint32_t i_chunk_count;
124 uint32_t i_sample_count;
126 mp4_chunk_t *chunk; /* always defined for each chunk */
128 /* sample size, p_sample_size defined only if i_sample_size == 0
129 else i_sample_size is size for all sample */
130 uint32_t i_sample_size;
131 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
132 too much time to do sumations each time*/
134 MP4_Box_t *p_stbl; /* will contain all timing information */
135 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
136 MP4_Box_t *p_sample;/* point on actual sdsd */
147 MP4_Box_t *p_root; /* container for the whole file */
151 uint64_t i_time; /* time position of the presentation
152 * in movie timescale */
153 uint64_t i_timescale; /* movie time scale */
154 uint64_t i_duration; /* movie duration */
155 unsigned int i_tracks; /* number of tracks */
156 mp4_track_t *track; /* array of track */
157 float f_fps; /* number of frame per seconds */
160 MP4_Box_t *p_tref_chap;
163 input_title_t *p_title;
166 /*****************************************************************************
167 * Declaration of local function
168 *****************************************************************************/
169 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *, bool b_force_enable );
170 static void MP4_TrackDestroy( mp4_track_t * );
172 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
173 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
175 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
177 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
178 static int MP4_TrackSampleSize( mp4_track_t * );
179 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
180 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
182 static void MP4_UpdateSeekpoint( demux_t * );
183 static const char *MP4_ConvertMacCode( uint16_t );
185 /* Return time in s of a track */
186 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
188 #define chunk p_track->chunk[p_track->i_chunk]
190 unsigned int i_index = 0;
191 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
192 int64_t i_dts = chunk.i_first_dts;
194 while( i_sample > 0 )
196 if( i_sample > chunk.p_sample_count_dts[i_index] )
198 i_dts += chunk.p_sample_count_dts[i_index] *
199 chunk.p_sample_delta_dts[i_index];
200 i_sample -= chunk.p_sample_count_dts[i_index];
205 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
212 /* now handle elst */
213 if( p_track->p_elst )
215 demux_sys_t *p_sys = p_demux->p_sys;
216 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
218 /* convert to offset */
219 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
220 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
221 elst->i_media_time[p_track->i_elst] > 0 )
223 i_dts -= elst->i_media_time[p_track->i_elst];
226 /* add i_elst_time */
227 i_dts += p_track->i_elst_time * p_track->i_timescale /
230 if( i_dts < 0 ) i_dts = 0;
233 return INT64_C(1000000) * i_dts / p_track->i_timescale;
236 static inline int64_t MP4_TrackGetPTSDelta( mp4_track_t *p_track )
238 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
239 unsigned int i_index = 0;
240 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
242 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
245 for( i_index = 0;; i_index++ )
247 if( i_sample < ck->p_sample_count_pts[i_index] )
248 return ck->p_sample_offset_pts[i_index] * INT64_C(1000000) /
249 (int64_t)p_track->i_timescale;
251 i_sample -= ck->p_sample_count_pts[i_index];
255 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
257 return INT64_C(1000000) * p_sys->i_time / p_sys->i_timescale;
260 static void LoadChapter( demux_t *p_demux );
262 /*****************************************************************************
263 * Open: check file and initializes MP4 structures
264 *****************************************************************************/
265 static int Open( vlc_object_t * p_this )
267 demux_t *p_demux = (demux_t *)p_this;
270 const uint8_t *p_peek;
281 /* A little test to see if it could be a mp4 */
282 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
284 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
295 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
302 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
305 msg_Warn( p_demux, "MP4 plugin discarded (not fastseekable)" );
309 /*Set exported functions */
310 p_demux->pf_demux = Demux;
311 p_demux->pf_control = Control;
313 /* create our structure that will contains all data */
314 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
316 /* Now load all boxes ( except raw data ) */
317 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
319 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
323 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
325 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
327 switch( p_ftyp->data.p_ftyp->i_major_brand )
331 "ISO Media file (isom) version %d.",
332 p_ftyp->data.p_ftyp->i_minor_version );
335 case( VLC_FOURCC( '3', 'g', 'p', '5' ) ):
336 case( VLC_FOURCC( '3', 'g', 'p', '6' ) ):
337 case( VLC_FOURCC( '3', 'g', 'p', '7' ) ):
338 msg_Dbg( p_demux, "3GPP Media file Release: %c",
339 #ifdef WORDS_BIGENDIAN
340 p_ftyp->data.p_ftyp->i_major_brand
342 p_ftyp->data.p_ftyp->i_major_brand >> 24
346 case( VLC_FOURCC( 'q', 't', ' ', ' ') ):
347 msg_Dbg( p_demux, "Apple QuickTime file" );
351 "unrecognized major file specification (%4.4s).",
352 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
358 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
361 /* the file need to have one moov box */
362 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
364 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
368 /* search also for moof box used by smoothstreaming */
369 p_foov = MP4_BoxGet( p_sys->p_root, "/moof" );
372 msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
376 /* we have a free box as a moov, rename it */
377 p_foov->i_type = ATOM_moov;
380 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
382 int i_count = MP4_BoxCount( p_rmra, "rmda" );
385 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
387 input_thread_t *p_input = demux_GetParentInput( p_demux );
388 input_item_t *p_current = input_GetItem( p_input );
390 input_item_node_t *p_subitems = input_item_node_Create( p_current );
392 for( i = 0; i < i_count; i++ )
394 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
398 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
402 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
404 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
405 psz_ref, (char*)&i_ref_type );
407 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
409 if( strstr( psz_ref, "qt5gateQT" ) )
411 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
414 if( !strncmp( psz_ref, "http://", 7 ) ||
415 !strncmp( psz_ref, "rtsp://", 7 ) )
422 char *psz_path = strdup( p_demux->psz_location );
423 char *end = strrchr( psz_path, '/' );
424 if( end ) end[1] = '\0';
425 else *psz_path = '\0';
427 if( asprintf( &psz_absolute, "%s://%s%s",
428 p_demux->psz_access, psz_path, psz_ref ) < 0 )
432 vlc_object_release( p_input) ;
437 psz_ref = psz_absolute;
440 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
441 input_item_t *p_input = input_item_New( psz_ref, NULL );
442 input_item_CopyOptions( p_current, p_input );
443 input_item_node_AppendItem( p_subitems, p_input );
444 vlc_gc_decref( p_input );
448 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
449 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
453 input_item_node_PostAndDelete( p_subitems );
454 vlc_object_release( p_input );
457 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
461 msg_Err( p_demux, "cannot find /moov/mvhd" );
466 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
467 p_demux->pf_demux = DemuxRef;
473 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
474 if( p_sys->i_timescale == 0 )
476 msg_Err( p_this, "bad timescale" );
479 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
482 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
484 msg_Err( p_demux, "cannot find any /moov/trak" );
487 msg_Dbg( p_demux, "found %d track%c",
489 p_sys->i_tracks ? 's':' ' );
491 /* allocate memory */
492 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
493 if( p_sys->track == NULL )
495 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
497 /* Search the first chap reference (like quicktime) and
498 * check that at least 1 stream is enabled */
499 p_sys->p_tref_chap = NULL;
500 b_enabled_es = false;
501 for( i = 0; i < p_sys->i_tracks; i++ )
503 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
506 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
507 if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
510 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
511 if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
512 p_sys->p_tref_chap = p_chap;
515 /* now process each track and extract all useful information */
516 for( i = 0; i < p_sys->i_tracks; i++ )
518 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
519 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
521 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
524 switch( p_sys->track[i].fmt.i_cat )
533 psz_cat = "subtitle";
541 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
542 p_sys->track[i].i_track_ID, psz_cat,
543 p_sys->track[i].b_enable ? "enable":"disable",
544 p_sys->track[i].fmt.psz_language ?
545 p_sys->track[i].fmt.psz_language : "undef" );
547 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
549 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
550 p_sys->track[i].i_track_ID,
551 p_sys->track[i].fmt.psz_language ?
552 p_sys->track[i].fmt.psz_language : "undef" );
556 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
557 p_sys->track[i].i_track_ID );
562 LoadChapter( p_demux );
569 MP4_BoxFree( p_demux->s, p_sys->p_root );
575 /*****************************************************************************
576 * Demux: read packet and send them to decoders
577 *****************************************************************************
578 * TODO check for newly selected track (ie audio upt to now )
579 *****************************************************************************/
580 static int Demux( demux_t *p_demux )
582 demux_sys_t *p_sys = p_demux->p_sys;
583 unsigned int i_track;
586 unsigned int i_track_selected;
588 /* check for newly selected/unselected track */
589 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
592 mp4_track_t *tk = &p_sys->track[i_track];
595 if( !tk->b_ok || tk->b_chapter ||
596 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
601 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
603 if( tk->b_selected && !b )
605 MP4_TrackUnselect( p_demux, tk );
607 else if( !tk->b_selected && b)
609 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
618 if( i_track_selected <= 0 )
620 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
621 if( p_sys->i_timescale > 0 )
623 int64_t i_length = (mtime_t)1000000 *
624 (mtime_t)p_sys->i_duration /
625 (mtime_t)p_sys->i_timescale;
626 if( MP4_GetMoviePTS( p_sys ) >= i_length )
631 msg_Warn( p_demux, "no track selected, exiting..." );
636 MP4_UpdateSeekpoint( p_demux );
638 /* first wait for the good time to read a packet */
639 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
641 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
643 /* we will read 100ms for each stream so ...*/
644 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
646 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
648 mp4_track_t *tk = &p_sys->track[i_track];
650 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
653 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
656 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
657 MP4_TrackGetDTS( p_demux, tk ),
658 MP4_GetMoviePTS( p_sys ) );
661 if( MP4_TrackSampleSize( tk ) > 0 )
667 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
669 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
671 MP4_TrackUnselect( p_demux, tk );
677 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
679 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
681 MP4_TrackUnselect( p_demux, tk );
685 if( tk->b_drms && tk->p_drms )
690 drms_get_p_key( tk->p_drms, p_key );
692 for( size_t i_pos = tk->p_skcr->data.p_skcr->i_init; i_pos < p_block->i_buffer; )
694 int n = __MIN( tk->p_skcr->data.p_skcr->i_encr, p_block->i_buffer - i_pos );
695 drms_decrypt( tk->p_drms, (uint32_t*)&p_block->p_buffer[i_pos], n, p_key );
697 i_pos += __MIN( tk->p_skcr->data.p_skcr->i_decr, p_block->i_buffer - i_pos );
702 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
703 p_block->i_buffer, NULL );
706 else if( tk->fmt.i_cat == SPU_ES )
708 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
709 p_block->i_buffer >= 2 )
711 uint16_t i_size = GetWBE( p_block->p_buffer );
713 if( i_size + 2 <= p_block->i_buffer )
716 /* remove the length field, and append a '\0' */
717 memmove( &p_block->p_buffer[0],
718 &p_block->p_buffer[2], i_size );
719 p_block->p_buffer[i_size] = '\0';
720 p_block->i_buffer = i_size + 1;
722 /* convert \r -> \n */
723 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
731 p_block->i_buffer = 0;
736 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
738 i_delta = MP4_TrackGetPTSDelta( tk );
740 p_block->i_pts = p_block->i_dts + i_delta;
741 else if( tk->fmt.i_cat != VIDEO_ES )
742 p_block->i_pts = p_block->i_dts;
744 p_block->i_pts = VLC_TS_INVALID;
746 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
747 es_out_Send( p_demux->out, tk->p_es, p_block );
751 if( MP4_TrackNextSample( p_demux, tk ) )
759 static void MP4_UpdateSeekpoint( demux_t *p_demux )
761 demux_sys_t *p_sys = p_demux->p_sys;
764 if( !p_sys->p_title )
766 i_time = MP4_GetMoviePTS( p_sys );
767 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
769 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
774 if( i != p_demux->info.i_seekpoint && i >= 0 )
776 p_demux->info.i_seekpoint = i;
777 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
780 /*****************************************************************************
782 ******************************************************************************/
783 static int Seek( demux_t *p_demux, mtime_t i_date )
785 demux_sys_t *p_sys = p_demux->p_sys;
786 unsigned int i_track;
788 /* First update update global time */
789 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
790 p_sys->i_pcr = i_date;
792 /* Now for each stream try to go to this time */
793 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
795 mp4_track_t *tk = &p_sys->track[i_track];
796 MP4_TrackSeek( p_demux, tk, i_date );
798 MP4_UpdateSeekpoint( p_demux );
800 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
805 /*****************************************************************************
807 *****************************************************************************/
808 static int Control( demux_t *p_demux, int i_query, va_list args )
810 demux_sys_t *p_sys = p_demux->p_sys;
817 case DEMUX_GET_POSITION:
818 pf = (double*)va_arg( args, double * );
819 if( p_sys->i_duration > 0 )
821 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
829 case DEMUX_SET_POSITION:
830 f = (double)va_arg( args, double );
831 if( p_sys->i_timescale > 0 )
833 i64 = (int64_t)( f * (double)1000000 *
834 (double)p_sys->i_duration /
835 (double)p_sys->i_timescale );
836 return Seek( p_demux, i64 );
838 else return VLC_SUCCESS;
841 pi64 = (int64_t*)va_arg( args, int64_t * );
842 if( p_sys->i_timescale > 0 )
844 *pi64 = (mtime_t)1000000 *
845 (mtime_t)p_sys->i_time /
846 (mtime_t)p_sys->i_timescale;
852 i64 = (int64_t)va_arg( args, int64_t );
853 return Seek( p_demux, i64 );
855 case DEMUX_GET_LENGTH:
856 pi64 = (int64_t*)va_arg( args, int64_t * );
857 if( p_sys->i_timescale > 0 )
859 *pi64 = (mtime_t)1000000 *
860 (mtime_t)p_sys->i_duration /
861 (mtime_t)p_sys->i_timescale;
867 pf = (double*)va_arg( args, double* );
873 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
874 MP4_Box_t *p_0xa9xxx;
876 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
879 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
886 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
887 p_0xa9xxx = p_0xa9xxx->p_next )
890 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
893 /* FIXME FIXME: should convert from whatever the character
894 * encoding of MP4 meta data is to UTF-8. */
895 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
896 if( psz_utf ) { EnsureUTF8( psz_utf ); \
897 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
899 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
900 switch( p_0xa9xxx->i_type )
902 case ATOM_0xa9nam: /* Full name */
903 SET( vlc_meta_SetTitle );
906 SET( vlc_meta_SetArtist );
909 SET( vlc_meta_SetArtist );
912 SET( vlc_meta_SetCopyright );
914 case ATOM_0xa9day: /* Creation Date */
915 SET( vlc_meta_SetDate );
917 case ATOM_0xa9des: /* Description */
918 SET( vlc_meta_SetDescription );
920 case ATOM_0xa9gen: /* Genre */
921 SET( vlc_meta_SetGenre );
925 if( p_0xa9xxx->data.p_gnre->i_genre <= NUM_GENRES )
926 vlc_meta_SetGenre( p_meta, ppsz_genres[p_0xa9xxx->data.p_gnre->i_genre - 1] );
929 case ATOM_0xa9alb: /* Album */
930 SET( vlc_meta_SetAlbum );
933 case ATOM_0xa9trk: /* Track */
934 SET( vlc_meta_SetTrackNum );
939 snprintf( psz_trck, sizeof( psz_trck ), "%i",
940 p_0xa9xxx->data.p_trkn->i_track_number );
941 vlc_meta_SetTrackNum( p_meta, psz_trck );
944 case ATOM_0xa9cmt: /* Commment */
945 SET( vlc_meta_SetDescription );
948 case ATOM_0xa9url: /* URL */
949 SET( vlc_meta_SetURL );
952 case ATOM_0xa9too: /* Encoder Tool */
953 case ATOM_0xa9enc: /* Encoded By */
954 SET( vlc_meta_SetEncodedBy );
961 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
963 { ATOM_0xa9wrt, N_("Writer") },
964 { ATOM_0xa9com, N_("Composr") },
965 { ATOM_0xa9prd, N_("Producer") },
966 { ATOM_0xa9inf, N_("Information") },
967 { ATOM_0xa9dir, N_("Director") },
968 { ATOM_0xa9dis, N_("Disclaimer") },
969 { ATOM_0xa9req, N_("Requirements") },
970 { ATOM_0xa9fmt, N_("Original Format") },
971 { ATOM_0xa9dsa, N_("Display Source As") },
972 { ATOM_0xa9hst, N_("Host Computer") },
973 { ATOM_0xa9prf, N_("Performers") },
974 { ATOM_0xa9ope, N_("Original Performer") },
975 { ATOM_0xa9src, N_("Providers Source Content") },
976 { ATOM_0xa9wrn, N_("Warning") },
977 { ATOM_0xa9swr, N_("Software") },
978 { ATOM_0xa9lyr, N_("Lyrics") },
979 { ATOM_0xa9mak, N_("Make") },
980 { ATOM_0xa9mod, N_("Model") },
981 { ATOM_0xa9PRD, N_("Product") },
982 { ATOM_0xa9grp, N_("Grouping") },
985 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
987 if( p_0xa9xxx->i_type == xa9typetoextrameta[i].xa9_type )
989 char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" );
992 EnsureUTF8( psz_utf );
993 vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
1003 case DEMUX_GET_TITLE_INFO:
1005 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1006 int *pi_int = (int*)va_arg( args, int* );
1007 int *pi_title_offset = (int*)va_arg( args, int* );
1008 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
1010 if( !p_sys->p_title )
1011 return VLC_EGENERIC;
1014 *ppp_title = malloc( sizeof( input_title_t**) );
1015 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
1016 *pi_title_offset = 0;
1017 *pi_seekpoint_offset = 0;
1020 case DEMUX_SET_TITLE:
1022 const int i_title = (int)va_arg( args, int );
1023 if( !p_sys->p_title || i_title != 0 )
1024 return VLC_EGENERIC;
1027 case DEMUX_SET_SEEKPOINT:
1029 const int i_seekpoint = (int)va_arg( args, int );
1030 if( !p_sys->p_title )
1031 return VLC_EGENERIC;
1032 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
1035 case DEMUX_SET_NEXT_DEMUX_TIME:
1036 case DEMUX_SET_GROUP:
1037 case DEMUX_HAS_UNSUPPORTED_META:
1038 case DEMUX_GET_ATTACHMENTS:
1039 case DEMUX_GET_PTS_DELAY:
1040 case DEMUX_CAN_RECORD:
1041 return VLC_EGENERIC;
1044 msg_Warn( p_demux, "control query %u unimplemented", i_query );
1045 return VLC_EGENERIC;
1049 /*****************************************************************************
1050 * Close: frees unused data
1051 *****************************************************************************/
1052 static void Close ( vlc_object_t * p_this )
1054 demux_t * p_demux = (demux_t *)p_this;
1055 demux_sys_t *p_sys = p_demux->p_sys;
1056 unsigned int i_track;
1058 msg_Dbg( p_demux, "freeing all memory" );
1060 MP4_BoxFree( p_demux->s, p_sys->p_root );
1061 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1063 MP4_TrackDestroy( &p_sys->track[i_track] );
1065 FREENULL( p_sys->track );
1067 if( p_sys->p_title )
1068 vlc_input_title_Delete( p_sys->p_title );
1075 /****************************************************************************
1076 * Local functions, specific to vlc
1077 ****************************************************************************/
1079 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1081 demux_sys_t *p_sys = p_demux->p_sys;
1084 p_sys->p_title = vlc_input_title_New();
1085 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1087 seekpoint_t *s = vlc_seekpoint_New();
1089 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1090 EnsureUTF8( s->psz_name );
1091 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1092 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1095 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1097 demux_sys_t *p_sys = p_demux->p_sys;
1099 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1101 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1102 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( tk );
1103 const unsigned int i_size = MP4_TrackSampleSize( tk );
1105 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1108 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1109 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1113 seekpoint_t *s = vlc_seekpoint_New();
1115 s->psz_name = strndup( &p_buffer[2], i_len );
1116 EnsureUTF8( s->psz_name );
1118 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1120 if( !p_sys->p_title )
1121 p_sys->p_title = vlc_input_title_New();
1122 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1125 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1126 tk->chunk[tk->i_chunk].i_sample_count )
1130 static void LoadChapter( demux_t *p_demux )
1132 demux_sys_t *p_sys = p_demux->p_sys;
1135 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1137 LoadChapterGpac( p_demux, p_chpl );
1139 else if( p_sys->p_tref_chap )
1141 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1144 /* Load the first subtitle track like quicktime */
1145 for( i = 0; i < p_chap->i_entry_count; i++ )
1147 for( j = 0; j < p_sys->i_tracks; j++ )
1149 mp4_track_t *tk = &p_sys->track[j];
1150 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1151 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1154 if( j < p_sys->i_tracks )
1156 LoadChapterApple( p_demux, &p_sys->track[j] );
1163 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1164 static int TrackCreateChunksIndex( demux_t *p_demux,
1165 mp4_track_t *p_demux_track )
1167 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1170 unsigned int i_chunk;
1171 unsigned int i_index, i_last;
1173 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1174 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1175 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1177 return( VLC_EGENERIC );
1180 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1181 if( !p_demux_track->i_chunk_count )
1183 msg_Warn( p_demux, "no chunk defined" );
1184 return( VLC_EGENERIC );
1186 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1187 sizeof( mp4_chunk_t ) );
1188 if( p_demux_track->chunk == NULL )
1193 /* first we read chunk offset */
1194 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1196 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1198 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1200 ck->i_first_dts = 0;
1201 ck->p_sample_count_dts = NULL;
1202 ck->p_sample_delta_dts = NULL;
1203 ck->p_sample_count_pts = NULL;
1204 ck->p_sample_offset_pts = NULL;
1207 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1208 to be used for the sample XXX begin to 1
1209 We construct it begining at the end */
1210 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1211 i_index = p_stsc->data.p_stsc->i_entry_count;
1214 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1215 return( VLC_EGENERIC );
1220 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1221 i_chunk < i_last; i_chunk++ )
1223 if( i_chunk >= p_demux_track->i_chunk_count )
1225 msg_Warn( p_demux, "corrupted chunk table" );
1226 return VLC_EGENERIC;
1229 p_demux_track->chunk[i_chunk].i_sample_description_index =
1230 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1231 p_demux_track->chunk[i_chunk].i_sample_count =
1232 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1234 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1237 p_demux_track->chunk[0].i_sample_first = 0;
1238 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1240 p_demux_track->chunk[i_chunk].i_sample_first =
1241 p_demux_track->chunk[i_chunk-1].i_sample_first +
1242 p_demux_track->chunk[i_chunk-1].i_sample_count;
1245 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1246 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1251 static int TrackCreateSamplesIndex( demux_t *p_demux,
1252 mp4_track_t *p_demux_track )
1255 MP4_Box_data_stsz_t *stsz;
1256 MP4_Box_data_stts_t *stts;
1257 /* TODO use also stss and stsh table for seeking */
1258 /* FIXME use edit table */
1263 int64_t i_index_sample_used;
1268 * Gives the sample size for each samples. There is also a stz2 table
1269 * (compressed form) that we need to implement TODO */
1270 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1273 /* FIXME and stz2 */
1274 msg_Warn( p_demux, "cannot find STSZ box" );
1275 return VLC_EGENERIC;
1277 stsz = p_box->data.p_stsz;
1280 * Gives mapping between sample and decoding time
1282 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1285 msg_Warn( p_demux, "cannot find STTS box" );
1286 return VLC_EGENERIC;
1288 stts = p_box->data.p_stts;
1290 /* Use stsz table to create a sample number -> sample size table */
1291 p_demux_track->i_sample_count = stsz->i_sample_count;
1292 if( stsz->i_sample_size )
1294 /* 1: all sample have the same size, so no need to construct a table */
1295 p_demux_track->i_sample_size = stsz->i_sample_size;
1296 p_demux_track->p_sample_size = NULL;
1300 /* 2: each sample can have a different size */
1301 p_demux_track->i_sample_size = 0;
1302 p_demux_track->p_sample_size =
1303 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1304 if( p_demux_track->p_sample_size == NULL )
1307 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1309 p_demux_track->p_sample_size[i_sample] =
1310 stsz->i_entry_size[i_sample];
1314 /* Use stts table to create a sample number -> dts table.
1315 * XXX: if we don't want to waste too much memory, we can't expand
1316 * the box! so each chunk will contain an "extract" of this table
1317 * for fast research (problem with raw stream where a sample is sometime
1318 * just channels*bits_per_sample/8 */
1321 i_index = 0; i_index_sample_used = 0;
1322 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1324 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1325 int64_t i_entry, i_sample_count, i;
1327 /* save first dts */
1328 ck->i_first_dts = i_next_dts;
1329 ck->i_last_dts = i_next_dts;
1331 /* count how many entries are needed for this chunk
1332 * for p_sample_delta_dts and p_sample_count_dts */
1333 i_sample_count = ck->i_sample_count;
1336 while( i_sample_count > 0 )
1338 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1339 /* don't count already used sample in this entry */
1341 i_sample_count += i_index_sample_used;
1347 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1348 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1350 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1354 i_sample_count = ck->i_sample_count;
1355 for( i = 0; i < i_entry; i++ )
1360 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1362 i_used = __MIN( i_rest, i_sample_count );
1364 i_index_sample_used += i_used;
1365 i_sample_count -= i_used;
1366 i_next_dts += i_used * stts->i_sample_delta[i_index];
1368 ck->p_sample_count_dts[i] = i_used;
1369 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1371 ck->i_last_dts = i_next_dts - ck->p_sample_delta_dts[i];
1373 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1376 i_index_sample_used = 0;
1382 * Gives the delta between decoding time (dts) and composition table (pts)
1384 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1387 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1389 msg_Warn( p_demux, "CTTS table" );
1391 /* Create pts-dts table per chunk */
1392 i_index = 0; i_index_sample_used = 0;
1393 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1395 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1396 int64_t i_entry, i_sample_count, i;
1398 /* count how many entries are needed for this chunk
1399 * for p_sample_delta_dts and p_sample_count_dts */
1400 i_sample_count = ck->i_sample_count;
1403 while( i_sample_count > 0 )
1405 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1407 /* don't count already used sample in this entry */
1409 i_sample_count += i_index_sample_used;
1415 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1416 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1417 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1421 i_sample_count = ck->i_sample_count;
1422 for( i = 0; i < i_entry; i++ )
1427 i_rest = ctts->i_sample_count[i_index] -
1428 i_index_sample_used;
1430 i_used = __MIN( i_rest, i_sample_count );
1432 i_index_sample_used += i_used;
1433 i_sample_count -= i_used;
1435 ck->p_sample_count_pts[i] = i_used;
1436 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1438 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1441 i_index_sample_used = 0;
1447 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1448 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1449 i_next_dts / p_demux_track->i_timescale );
1455 * It computes the sample rate for a video track using the given sample
1458 static void TrackGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
1459 const mp4_track_t *p_track,
1460 unsigned i_sd_index,
1466 if( p_track->i_chunk_count <= 0 )
1470 const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
1471 while( p_chunk > &p_track->chunk[0] &&
1472 p_chunk[-1].i_sample_description_index == i_sd_index )
1477 uint64_t i_sample = 0;
1478 uint64_t i_first_dts = p_chunk->i_first_dts;
1479 uint64_t i_last_dts;
1482 i_sample += p_chunk->i_sample_count;
1483 i_last_dts = p_chunk->i_last_dts;
1486 while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
1487 p_chunk->i_sample_description_index == i_sd_index );
1489 if( i_sample > 1 && i_first_dts < i_last_dts )
1490 vlc_ureduce( pi_num, pi_den,
1491 ( i_sample - 1) * p_track->i_timescale,
1492 i_last_dts - i_first_dts,
1498 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1500 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1501 unsigned int i_chunk, es_out_id_t **pp_es )
1503 const unsigned i_sample_description_index =
1504 p_track->chunk[i_chunk].i_sample_description_index;
1505 MP4_Box_t *p_sample;
1513 if( !i_sample_description_index )
1515 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1516 p_track->i_track_ID );
1517 return VLC_EGENERIC;
1520 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1521 i_sample_description_index - 1 );
1524 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1526 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1527 p_track->i_track_ID );
1528 return VLC_EGENERIC;
1531 p_track->p_sample = p_sample;
1533 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
1535 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
1537 p_sample->i_type = p_frma->data.p_frma->i_type;
1540 p_enda = MP4_BoxGet( p_sample, "wave/enda" );
1542 p_enda = MP4_BoxGet( p_sample, "enda" );
1544 if( p_track->fmt.i_cat == AUDIO_ES && ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1546 MP4_Box_data_sample_soun_t *p_soun;
1548 p_soun = p_sample->data.p_sample_soun;
1550 if( p_soun->i_qt_version == 0 )
1552 switch( p_sample->i_type )
1554 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1555 p_soun->i_qt_version = 1;
1556 p_soun->i_sample_per_packet = 64;
1557 p_soun->i_bytes_per_packet = 34;
1558 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1559 p_soun->i_bytes_per_sample = 2;
1561 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1562 p_soun->i_qt_version = 1;
1563 p_soun->i_sample_per_packet = 6;
1564 p_soun->i_bytes_per_packet = 2;
1565 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1566 p_soun->i_bytes_per_sample = 2;
1568 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1569 p_soun->i_qt_version = 1;
1570 p_soun->i_sample_per_packet = 12;
1571 p_soun->i_bytes_per_packet = 2;
1572 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1573 p_soun->i_bytes_per_sample = 2;
1575 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1576 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1577 p_soun->i_samplesize = 8;
1578 p_track->i_sample_size = p_soun->i_channelcount;
1580 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1581 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1582 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1583 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1584 /* What would be the fun if you could trust the .mov */
1585 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1592 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1594 p_soun->i_qt_version = 0;
1597 else if( p_track->fmt.i_cat == AUDIO_ES && p_sample->data.p_sample_soun->i_qt_version == 1 )
1599 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1601 switch( p_sample->i_type )
1603 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1604 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1606 if( p_track->i_sample_size > 1 )
1607 p_soun->i_qt_version = 0;
1610 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1611 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1612 case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
1613 p_soun->i_qt_version = 0;
1621 switch( p_track->fmt.i_cat )
1624 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1625 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1626 p_track->fmt.video.i_bits_per_pixel =
1627 p_sample->data.p_sample_vide->i_depth;
1629 /* fall on display size */
1630 if( p_track->fmt.video.i_width <= 0 )
1631 p_track->fmt.video.i_width = p_track->i_width;
1632 if( p_track->fmt.video.i_height <= 0 )
1633 p_track->fmt.video.i_height = p_track->i_height;
1635 /* Find out apect ratio from display size */
1636 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1637 /* Work-around buggy muxed files */
1638 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1640 p_track->fmt.video.i_sar_num = p_track->i_width * p_track->fmt.video.i_height;
1641 p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
1644 /* Support for cropping (eg. in H263 files) */
1645 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1646 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1649 TrackGetESSampleRate( &p_track->fmt.video.i_frame_rate,
1650 &p_track->fmt.video.i_frame_rate_base,
1651 p_track, i_sample_description_index, i_chunk );
1652 p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
1653 (float)p_track->fmt.video.i_frame_rate_base;
1657 p_track->fmt.audio.i_channels =
1658 p_sample->data.p_sample_soun->i_channelcount;
1659 p_track->fmt.audio.i_rate =
1660 p_sample->data.p_sample_soun->i_sampleratehi;
1661 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1662 p_sample->data.p_sample_soun->i_sampleratehi *
1663 p_sample->data.p_sample_soun->i_samplesize;
1664 p_track->fmt.audio.i_bitspersample =
1665 p_sample->data.p_sample_soun->i_samplesize;
1667 if( p_track->i_sample_size != 0 &&
1668 p_sample->data.p_sample_soun->i_qt_version == 1 && p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1670 msg_Err( p_demux, "Invalid sample per packet value for qt_version 1" );
1671 return VLC_EGENERIC;
1680 /* It's a little ugly but .. there are special cases */
1681 switch( p_sample->i_type )
1683 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1684 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1686 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1689 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1691 MP4_Box_t *p_dac3_box = MP4_BoxGet( p_sample, "dac3", 0 );
1693 p_track->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1696 static const int pi_bitrate[] = {
1703 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
1704 p_track->fmt.audio.i_channels = 0;
1705 p_track->fmt.i_bitrate = 0;
1706 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
1707 p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
1708 p_track->fmt.audio.i_bitspersample = 0;
1712 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1714 p_track->fmt.i_codec = VLC_FOURCC( 'e', 'a', 'c', '3' );
1718 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1719 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1721 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1723 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1724 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1726 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1728 /* Buggy files workaround */
1729 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1730 p_sample->data.p_sample_soun->i_sampleratehi) )
1732 MP4_Box_data_sample_soun_t *p_soun =
1733 p_sample->data.p_sample_soun;
1735 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1736 "(%u), making both equal (report any problem).",
1737 p_track->i_timescale, p_soun->i_sampleratehi );
1739 if( p_soun->i_sampleratehi )
1740 p_track->i_timescale = p_soun->i_sampleratehi;
1742 p_soun->i_sampleratehi = p_track->i_timescale;
1747 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1748 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1751 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1752 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1753 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1754 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1755 /* FIXME UTF-8 doesn't work here ? */
1756 if( p_track->b_mac_encoding )
1757 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1759 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1762 case VLC_FOURCC('y','v','1','2'):
1763 p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1765 case VLC_FOURCC('y','u','v','2'):
1766 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1769 case VLC_FOURCC('i','n','2','4'):
1770 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1771 VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
1773 case VLC_FOURCC('f','l','3','2'):
1774 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1775 VLC_CODEC_F32L : VLC_CODEC_F32B;
1777 case VLC_FOURCC('f','l','6','4'):
1778 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1779 VLC_CODEC_F64L : VLC_CODEC_F64B;
1781 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
1783 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1784 if( p_soun->i_qt_version == 2 &&
1785 p_soun->i_qt_description > 20 + 28 )
1792 static const struct {
1796 vlc_fourcc_t i_codec;
1798 { 0x01, 0x03, 32, VLC_CODEC_F32L },
1799 { 0x01, 0x03, 64, VLC_CODEC_F64L },
1800 { 0x01|0x02, 0x03, 32, VLC_CODEC_F32B },
1801 { 0x01|0x02, 0x03, 64, VLC_CODEC_F64B },
1803 { 0x00, 0x05, 8, VLC_CODEC_U8 },
1804 { 0x00| 0x04, 0x05, 8, VLC_CODEC_S8 },
1806 { 0x00, 0x07, 16, VLC_CODEC_U16L },
1807 { 0x00|0x02, 0x07, 16, VLC_CODEC_U16B },
1808 { 0x00 |0x04, 0x07, 16, VLC_CODEC_S16L },
1809 { 0x00|0x02|0x04, 0x07, 16, VLC_CODEC_S16B },
1811 { 0x00, 0x07, 24, VLC_CODEC_U24L },
1812 { 0x00|0x02, 0x07, 24, VLC_CODEC_U24B },
1813 { 0x00 |0x04, 0x07, 24, VLC_CODEC_S24L },
1814 { 0x00|0x02|0x04, 0x07, 24, VLC_CODEC_S24B },
1816 { 0x00, 0x07, 32, VLC_CODEC_U32L },
1817 { 0x00|0x02, 0x07, 32, VLC_CODEC_U32B },
1818 { 0x00 |0x04, 0x07, 32, VLC_CODEC_S32L },
1819 { 0x00|0x02|0x04, 0x07, 32, VLC_CODEC_S32B },
1823 uint32_t i_bits = GetDWBE(&p_soun->p_qt_description[20 + 20]);
1824 uint32_t i_flags = GetDWBE(&p_soun->p_qt_description[20 + 24]);
1826 for( int i = 0; p_formats[i].i_codec; i++ )
1828 if( p_formats[i].i_bits == i_bits &&
1829 (i_flags & p_formats[i].i_mask) == p_formats[i].i_flags )
1831 p_track->fmt.i_codec = p_formats[i].i_codec;
1832 p_track->fmt.audio.i_bitspersample = i_bits;
1833 p_track->fmt.audio.i_blockalign = p_soun->i_channelcount * i_bits / 8;
1834 p_track->i_sample_size = p_track->fmt.audio.i_blockalign;
1836 p_soun->i_qt_version = 0;
1844 p_track->fmt.i_codec = p_sample->i_type;
1848 /* now see if esds is present and if so create a data packet
1849 with decoder_specific_info */
1850 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1851 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1852 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1853 ( p_esds->data.p_esds )&&
1856 /* First update information based on i_objectTypeIndication */
1857 switch( p_decconfig->i_objectTypeIndication )
1859 case( 0x20 ): /* MPEG4 VIDEO */
1860 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1863 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1864 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
1865 p_decconfig->p_decoder_specific_info[0] == 0xF8 &&
1866 (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
1868 p_track->fmt.i_codec = VLC_CODEC_ALS;
1876 case( 0x65): /* MPEG2 video */
1877 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1879 /* Theses are MPEG2-AAC */
1880 case( 0x66): /* main profile */
1881 case( 0x67): /* Low complexity profile */
1882 case( 0x68): /* Scaleable Sampling rate profile */
1883 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1885 /* true MPEG 2 audio */
1887 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1889 case( 0x6a): /* MPEG1 video */
1890 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1892 case( 0x6b): /* MPEG1 audio */
1893 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1895 case( 0x6c ): /* jpeg */
1896 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1898 case( 0x6d ): /* png */
1899 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
1901 case( 0x6e ): /* jpeg200 */
1902 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
1904 case( 0xa3 ): /* vc1 */
1905 p_track->fmt.i_codec = VLC_FOURCC( 'W','V','C','1' );
1909 case( 0xe0 ): /* NeroDigital: dvd subs */
1910 if( p_track->fmt.i_cat == SPU_ES )
1912 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1913 if( p_track->i_width > 0 )
1914 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1915 if( p_track->i_height > 0 )
1916 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1919 case( 0xe1 ): /* QCelp for 3gp */
1920 if( p_track->fmt.i_cat == AUDIO_ES )
1922 p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
1928 /* Unknown entry, but don't touch i_fourcc */
1930 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1931 p_decconfig->i_objectTypeIndication,
1932 p_track->i_track_ID );
1935 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1936 if( p_track->fmt.i_extra > 0 )
1938 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1939 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1940 p_track->fmt.i_extra );
1945 switch( p_sample->i_type )
1947 /* qt decoder, send the complete chunk */
1948 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1949 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1950 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1951 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1952 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1953 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1954 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1955 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1956 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1957 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1958 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1959 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1960 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1962 /* qt decoder, send the complete chunk */
1963 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1964 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1965 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1966 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1967 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1968 p_track->fmt.i_extra =
1969 p_sample->data.p_sample_vide->i_qt_image_description;
1970 if( p_track->fmt.i_extra > 0 )
1972 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1973 memcpy( p_track->fmt.p_extra,
1974 p_sample->data.p_sample_vide->p_qt_image_description,
1975 p_track->fmt.i_extra);
1978 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1979 p_track->fmt.audio.i_rate = 8000;
1980 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1981 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1982 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1983 p_track->fmt.i_extra =
1984 p_sample->data.p_sample_soun->i_qt_description;
1985 if( p_track->fmt.i_extra > 0 )
1987 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1988 memcpy( p_track->fmt.p_extra,
1989 p_sample->data.p_sample_soun->p_qt_description,
1990 p_track->fmt.i_extra);
1994 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1995 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1997 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
2001 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
2002 if( p_track->fmt.i_extra > 0 )
2004 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
2005 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
2006 p_track->fmt.i_extra );
2011 msg_Err( p_demux, "missing avcC" );
2016 case VLC_FOURCC('m','s',0x00,0x02):
2017 case VLC_FOURCC('m','s',0x00,0x11):
2018 case VLC_FOURCC('Q','c','l','p'):
2019 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
2030 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2035 /* given a time it return sample/chunk
2036 * it also update elst field of the track
2038 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2039 int64_t i_start, uint32_t *pi_chunk,
2040 uint32_t *pi_sample )
2042 demux_sys_t *p_sys = p_demux->p_sys;
2043 MP4_Box_t *p_box_stss;
2045 unsigned int i_sample;
2046 unsigned int i_chunk;
2049 /* FIXME see if it's needed to check p_track->i_chunk_count */
2050 if( p_track->i_chunk_count == 0 )
2051 return( VLC_EGENERIC );
2053 /* handle elst (find the correct one) */
2054 MP4_TrackSetELST( p_demux, p_track, i_start );
2055 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2057 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2058 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
2060 /* now calculate i_start for this elst */
2062 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
2070 /* to track time scale */
2071 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2072 /* add elst offset */
2073 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2074 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2075 elst->i_media_time[p_track->i_elst] > 0 )
2077 i_start += elst->i_media_time[p_track->i_elst];
2080 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2081 "ms (track)", p_track->i_elst,
2082 i_mvt * 1000 / p_sys->i_timescale,
2083 i_start * 1000 / p_track->i_timescale );
2087 /* convert absolute time to in timescale unit */
2088 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2091 /* we start from sample 0/chunk 0, hope it won't take too much time */
2092 /* *** find good chunk *** */
2093 for( i_chunk = 0; ; i_chunk++ )
2095 if( i_chunk + 1 >= p_track->i_chunk_count )
2097 /* at the end and can't check if i_start in this chunk,
2098 it will be check while searching i_sample */
2099 i_chunk = p_track->i_chunk_count - 1;
2103 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2104 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
2110 /* *** find sample in the chunk *** */
2111 i_sample = p_track->chunk[i_chunk].i_sample_first;
2112 i_dts = p_track->chunk[i_chunk].i_first_dts;
2113 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2116 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2117 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2120 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2121 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2123 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2128 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2132 i_sample += ( i_start - i_dts ) /
2133 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2138 if( i_sample >= p_track->i_sample_count )
2140 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2141 "(seeking too far) chunk=%d sample=%d",
2142 p_track->i_track_ID, i_chunk, i_sample );
2143 return( VLC_EGENERIC );
2147 /* *** Try to find nearest sync points *** */
2148 if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2150 MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2151 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2152 p_track->i_track_ID );
2153 for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2155 if( i_index >= p_stss->i_entry_count - 1 ||
2156 i_sample < p_stss->i_sample_number[i_index+1] )
2158 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2159 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
2160 i_sample, i_sync_sample );
2162 if( i_sync_sample <= i_sample )
2164 while( i_chunk > 0 &&
2165 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2170 while( i_chunk < p_track->i_chunk_count - 1 &&
2171 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2172 p_track->chunk[i_chunk].i_sample_count )
2175 i_sample = i_sync_sample;
2182 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2183 "Sample Box (stss)", p_track->i_track_ID );
2186 *pi_chunk = i_chunk;
2187 *pi_sample = i_sample;
2192 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2193 unsigned int i_chunk, unsigned int i_sample )
2195 bool b_reselect = false;
2197 /* now see if actual es is ok */
2198 if( p_track->i_chunk >= p_track->i_chunk_count ||
2199 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2200 p_track->chunk[i_chunk].i_sample_description_index )
2202 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2203 p_track->i_track_ID );
2205 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2206 p_track->p_es, &b_reselect );
2208 es_out_Del( p_demux->out, p_track->p_es );
2210 p_track->p_es = NULL;
2212 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2214 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2215 p_track->i_track_ID );
2217 p_track->b_ok = false;
2218 p_track->b_selected = false;
2219 return VLC_EGENERIC;
2223 /* select again the new decoder */
2226 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2229 p_track->i_chunk = i_chunk;
2230 p_track->i_sample = i_sample;
2232 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2235 /****************************************************************************
2237 ****************************************************************************
2238 * Parse track information and create all needed data to run a track
2239 * If it succeed b_ok is set to 1 else to 0
2240 ****************************************************************************/
2241 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2242 MP4_Box_t *p_box_trak,
2243 bool b_force_enable )
2245 demux_sys_t *p_sys = p_demux->p_sys;
2247 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2248 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2263 /* hint track unsupported */
2265 /* set default value (-> track unusable) */
2266 p_track->b_ok = false;
2267 p_track->b_enable = false;
2268 p_track->b_selected = false;
2269 p_track->b_chapter = false;
2270 p_track->b_mac_encoding = false;
2272 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2279 /* do we launch this track by default ? */
2281 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2282 if( !p_track->b_enable )
2283 p_track->fmt.i_priority = -1;
2285 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2286 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2287 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2291 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2294 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2295 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2297 if( ( !p_mdhd )||( !p_hdlr ) )
2302 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2303 if( !p_track->i_timescale )
2306 if( p_mdhd->data.p_mdhd->i_language_code < 0x800 )
2308 /* We can convert i_language_code into iso 639 code,
2310 strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2311 p_track->b_mac_encoding = true;
2315 for( i = 0; i < 3; i++ )
2316 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2320 switch( p_hdlr->data.p_hdlr->i_handler_type )
2323 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2327 p_track->fmt.i_cat = AUDIO_ES;
2331 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2335 p_track->fmt.i_cat = VIDEO_ES;
2342 p_track->fmt.i_cat = SPU_ES;
2349 p_track->i_elst = 0;
2350 p_track->i_elst_time = 0;
2351 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2353 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2356 msg_Warn( p_demux, "elst box found" );
2357 for( i = 0; i < elst->i_entry_count; i++ )
2359 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2360 "ms) rate=%d.%d", i,
2361 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2362 elst->i_media_time[i] >= 0 ?
2363 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2365 elst->i_media_rate_integer[i],
2366 elst->i_media_rate_fraction[i] );
2373 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2375 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2376 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2381 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2382 p_track->b_drms = p_drms != NULL;
2383 p_track->p_drms = p_track->b_drms ?
2384 p_drms->data.p_sample_soun->p_drms : NULL;
2388 p_drms = MP4_BoxGet( p_track->p_stsd, "drmi" );
2389 p_track->b_drms = p_drms != NULL;
2390 p_track->p_drms = p_track->b_drms ?
2391 p_drms->data.p_sample_vide->p_drms : NULL;
2395 p_track->p_skcr = MP4_BoxGet( p_drms, "sinf/skcr" );
2398 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2400 p_track->fmt.psz_language = strdup( language );
2403 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2406 MP4_Box_t *p_box_iter;
2407 for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2408 p_box_iter = p_box_iter->p_next )
2410 switch( p_box_iter->i_type )
2413 p_track->fmt.psz_description =
2414 strdup( p_box_iter->data.p_0xa9xxx->psz_text );
2417 p_track->fmt.psz_description =
2418 strdup( p_box_iter->data.p_name->psz_text );
2424 /* Create chunk index table and sample index table */
2425 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2426 TrackCreateSamplesIndex( p_demux, p_track ) )
2428 return; /* cannot create chunks index */
2431 p_track->i_chunk = 0;
2432 p_track->i_sample = 0;
2434 /* Mark chapter only track */
2435 if( p_sys->p_tref_chap )
2437 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2440 for( i = 0; i < p_chap->i_entry_count; i++ )
2442 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2444 p_track->b_chapter = true;
2445 p_track->b_enable = false;
2452 if( b_force_enable &&
2453 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2455 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2456 p_track->i_track_ID );
2457 p_track->b_enable = true;
2458 p_track->fmt.i_priority = 0;
2461 p_track->p_es = NULL;
2462 if( TrackCreateES( p_demux,
2463 p_track, p_track->i_chunk,
2464 p_track->b_chapter ? NULL : &p_track->p_es ) )
2466 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2467 p_track->i_track_ID );
2470 p_track->b_ok = true;
2474 for( i = 0; i < p_track->i_chunk_count; i++ )
2476 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2477 i, p_track->chunk[i].i_sample_count,
2478 p_track->chunk[i].i_first_dts );
2485 /****************************************************************************
2487 ****************************************************************************
2488 * Destroy a track created by MP4_TrackCreate.
2489 ****************************************************************************/
2490 static void MP4_TrackDestroy( mp4_track_t *p_track )
2492 unsigned int i_chunk;
2494 p_track->b_ok = false;
2495 p_track->b_enable = false;
2496 p_track->b_selected = false;
2498 es_format_Clean( &p_track->fmt );
2500 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2502 if( p_track->chunk )
2504 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2505 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2507 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2508 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2511 FREENULL( p_track->chunk );
2513 if( !p_track->i_sample_size )
2515 FREENULL( p_track->p_sample_size );
2519 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2522 if( !p_track->b_ok || p_track->b_chapter )
2524 return VLC_EGENERIC;
2527 if( p_track->b_selected )
2529 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2530 p_track->i_track_ID );
2534 return MP4_TrackSeek( p_demux, p_track, i_start );
2537 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2539 if( !p_track->b_ok || p_track->b_chapter )
2544 if( !p_track->b_selected )
2546 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2547 p_track->i_track_ID );
2552 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2553 p_track->p_es, false );
2556 p_track->b_selected = false;
2559 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2565 if( !p_track->b_ok || p_track->b_chapter )
2566 return VLC_EGENERIC;
2568 p_track->b_selected = false;
2570 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2571 &i_chunk, &i_sample ) )
2573 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2574 p_track->i_track_ID );
2575 return VLC_EGENERIC;
2578 p_track->b_selected = true;
2580 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2581 p_track->b_selected = true;
2583 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2588 * 3 types: for audio
2591 #define QT_V0_MAX_SAMPLES 1024
2592 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2595 MP4_Box_data_sample_soun_t *p_soun;
2597 if( p_track->i_sample_size == 0 )
2599 /* most simple case */
2600 return p_track->p_sample_size[p_track->i_sample];
2602 if( p_track->fmt.i_cat != AUDIO_ES )
2604 return p_track->i_sample_size;
2607 p_soun = p_track->p_sample->data.p_sample_soun;
2609 if( p_soun->i_qt_version == 1 )
2611 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
2612 if( p_track->fmt.audio.i_blockalign > 1 )
2613 i_samples = p_soun->i_sample_per_packet;
2615 i_size = i_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2617 else if( p_track->i_sample_size > 256 )
2619 /* We do that so we don't read too much data
2620 * (in this case we are likely dealing with compressed data) */
2621 i_size = p_track->i_sample_size;
2625 /* Read a bunch of samples at once */
2626 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2627 ( p_track->i_sample -
2628 p_track->chunk[p_track->i_chunk].i_sample_first );
2630 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2631 i_size = i_samples * p_track->i_sample_size;
2634 //fprintf( stderr, "size=%d\n", i_size );
2638 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2640 unsigned int i_sample;
2643 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2645 if( p_track->i_sample_size )
2647 MP4_Box_data_sample_soun_t *p_soun =
2648 p_track->p_sample->data.p_sample_soun;
2650 if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
2652 i_pos += ( p_track->i_sample -
2653 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2654 p_track->i_sample_size;
2658 /* we read chunk by chunk unless a blockalign is requested */
2659 if( p_track->fmt.audio.i_blockalign > 1 )
2660 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
2661 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2666 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2667 i_sample < p_track->i_sample; i_sample++ )
2669 i_pos += p_track->p_sample_size[i_sample];
2676 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2678 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2680 MP4_Box_data_sample_soun_t *p_soun;
2682 p_soun = p_track->p_sample->data.p_sample_soun;
2684 if( p_soun->i_qt_version == 1 )
2686 /* we read chunk by chunk unless a blockalign is requested */
2687 if( p_track->fmt.audio.i_blockalign > 1 )
2688 p_track->i_sample += p_soun->i_sample_per_packet;
2690 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
2692 else if( p_track->i_sample_size > 256 )
2694 /* We do that so we don't read too much data
2695 * (in this case we are likely dealing with compressed data) */
2696 p_track->i_sample += 1;
2701 p_track->i_sample += QT_V0_MAX_SAMPLES;
2702 if( p_track->i_sample >
2703 p_track->chunk[p_track->i_chunk].i_sample_first +
2704 p_track->chunk[p_track->i_chunk].i_sample_count )
2707 p_track->chunk[p_track->i_chunk].i_sample_first +
2708 p_track->chunk[p_track->i_chunk].i_sample_count;
2714 p_track->i_sample++;
2717 if( p_track->i_sample >= p_track->i_sample_count )
2718 return VLC_EGENERIC;
2720 /* Have we changed chunk ? */
2721 if( p_track->i_sample >=
2722 p_track->chunk[p_track->i_chunk].i_sample_first +
2723 p_track->chunk[p_track->i_chunk].i_sample_count )
2725 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2726 p_track->i_sample ) )
2728 msg_Warn( p_demux, "track[0x%x] will be disabled "
2729 "(cannot restart decoder)", p_track->i_track_ID );
2730 MP4_TrackUnselect( p_demux, p_track );
2731 return VLC_EGENERIC;
2735 /* Have we changed elst */
2736 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2738 demux_sys_t *p_sys = p_demux->p_sys;
2739 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2740 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2741 p_sys->i_timescale / (int64_t)1000000;
2743 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2744 i_mvt >= p_track->i_elst_time +
2745 elst->i_segment_duration[p_track->i_elst] )
2747 MP4_TrackSetELST( p_demux, p_track,
2748 MP4_TrackGetDTS( p_demux, p_track ) );
2755 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2758 demux_sys_t *p_sys = p_demux->p_sys;
2759 int i_elst_last = tk->i_elst;
2761 /* handle elst (find the correct one) */
2763 tk->i_elst_time = 0;
2764 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2766 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2767 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2769 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2771 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2773 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2777 tk->i_elst_time += i_dur;
2780 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2782 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2783 tk->i_elst = elst->i_entry_count - 1;
2784 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2787 if( elst->i_media_time[tk->i_elst] < 0 )
2790 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2793 if( i_elst_last != tk->i_elst )
2795 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2800 static const char *MP4_ConvertMacCode( uint16_t i_code )
2802 static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
2803 { "en", 0 }, { "fr", 1 }, { "de", 2 }, { "it", 3 }, { "nl", 4 },
2804 { "sv", 5 }, { "es", 6 }, { "da", 7 }, { "pt", 8 }, { "no", 9 },
2805 { "he", 10 }, { "ja", 11 }, { "ar", 12 }, { "fi", 13 }, { "el", 14 },
2806 { "is", 15 }, { "mt", 16 }, { "tr", 17 }, { "hr", 18 }, { "zh", 19 },
2807 { "ur", 20 }, { "hi", 21 }, { "th", 22 }, { "ko", 23 }, { "lt", 24 },
2808 { "pl", 25 }, { "hu", 26 }, { "et", 27 }, { "lv", 28 }, //{ "??", 29 },
2809 { "fo", 30 }, { "fa", 31 }, { "ru", 32 }, { "zh", 33 }, { "nl", 34 },
2810 { "ga", 35 }, { "sq", 36 }, { "ro", 37 }, { "cs", 38 }, { "sk", 39 },
2811 { "sl", 40 }, { "yi", 41 }, { "sr", 42 }, { "mk", 43 }, { "bg", 44 },
2812 { "uk", 45 }, { "be", 46 }, { "uz", 47 }, { "az", 48 }, { "kk", 48 },
2813 { "az", 50 }, { "hy", 51 }, { "ka", 52 }, { "mo", 53 }, { "ky", 54 },
2814 { "tg", 55 }, { "tk", 56 }, { "mn", 57 }, { "mn", 58 }, { "ps", 59 },
2815 { "ku", 60 }, { "ks", 61 }, { "sd", 62 }, { "bo", 63 }, { "ne", 64 },
2816 { "sa", 65 }, { "mr", 66 }, { "bn", 67 }, { "as", 68 }, { "gu", 69 },
2817 { "pa", 70 }, { "or", 71 }, { "ml", 72 }, { "kn", 73 }, { "ta", 74 },
2818 { "te", 75 }, { "si", 76 }, { "my", 77 }, { "km", 78 }, { "lo", 79 },
2819 { "vi", 80 }, { "id", 81 }, { "tl", 82 }, { "ms", 83 }, { "ms", 84 },
2820 { "am", 85 }, { "ti", 86 }, { "om", 87 }, { "so", 88 }, { "sw", 89 },
2821 { "rw", 90 }, { "rn", 91 }, { "ny", 92 }, { "mg", 93 }, { "eo", 94 },
2823 { "cy", 128 }, { "eu", 129 },
2824 { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
2825 { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
2826 { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
2827 { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
2832 for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
2834 if( p_cvt[i].i_code == i_code )
2835 return p_cvt[i].psz_iso639_1;