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;
1514 if( !i_sample_description_index )
1516 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1517 p_track->i_track_ID );
1518 return VLC_EGENERIC;
1521 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1522 i_sample_description_index - 1 );
1525 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1527 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1528 p_track->i_track_ID );
1529 return VLC_EGENERIC;
1532 p_track->p_sample = p_sample;
1534 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
1536 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
1538 p_sample->i_type = p_frma->data.p_frma->i_type;
1541 p_enda = MP4_BoxGet( p_sample, "wave/enda" );
1543 p_enda = MP4_BoxGet( p_sample, "enda" );
1545 p_pasp = MP4_BoxGet( p_sample, "pasp" );
1547 if( p_track->fmt.i_cat == AUDIO_ES && ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1549 MP4_Box_data_sample_soun_t *p_soun;
1551 p_soun = p_sample->data.p_sample_soun;
1553 if( p_soun->i_qt_version == 0 )
1555 switch( p_sample->i_type )
1557 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1558 p_soun->i_qt_version = 1;
1559 p_soun->i_sample_per_packet = 64;
1560 p_soun->i_bytes_per_packet = 34;
1561 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1562 p_soun->i_bytes_per_sample = 2;
1564 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1565 p_soun->i_qt_version = 1;
1566 p_soun->i_sample_per_packet = 6;
1567 p_soun->i_bytes_per_packet = 2;
1568 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1569 p_soun->i_bytes_per_sample = 2;
1571 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1572 p_soun->i_qt_version = 1;
1573 p_soun->i_sample_per_packet = 12;
1574 p_soun->i_bytes_per_packet = 2;
1575 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1576 p_soun->i_bytes_per_sample = 2;
1578 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1579 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1580 p_soun->i_samplesize = 8;
1581 p_track->i_sample_size = p_soun->i_channelcount;
1583 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1584 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1585 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1586 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1587 /* What would be the fun if you could trust the .mov */
1588 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1595 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1597 p_soun->i_qt_version = 0;
1600 else if( p_track->fmt.i_cat == AUDIO_ES && p_sample->data.p_sample_soun->i_qt_version == 1 )
1602 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1604 switch( p_sample->i_type )
1606 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1607 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1609 if( p_track->i_sample_size > 1 )
1610 p_soun->i_qt_version = 0;
1613 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1614 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1615 case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
1616 p_soun->i_qt_version = 0;
1624 switch( p_track->fmt.i_cat )
1627 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1628 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1629 p_track->fmt.video.i_bits_per_pixel =
1630 p_sample->data.p_sample_vide->i_depth;
1632 /* fall on display size */
1633 if( p_track->fmt.video.i_width <= 0 )
1634 p_track->fmt.video.i_width = p_track->i_width;
1635 if( p_track->fmt.video.i_height <= 0 )
1636 p_track->fmt.video.i_height = p_track->i_height;
1638 /* Find out apect ratio from display size */
1639 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1640 /* Work-around buggy muxed files */
1641 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1643 p_track->fmt.video.i_sar_num = p_track->i_width * p_track->fmt.video.i_height;
1644 p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
1646 if( p_pasp && p_pasp->data.p_pasp->i_horizontal_spacing > 0 &&
1647 p_pasp->data.p_pasp->i_vertical_spacing > 0 )
1649 p_track->fmt.video.i_sar_num = p_pasp->data.p_pasp->i_horizontal_spacing;
1650 p_track->fmt.video.i_sar_den = p_pasp->data.p_pasp->i_vertical_spacing;
1653 /* Support for cropping (eg. in H263 files) */
1654 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1655 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1658 TrackGetESSampleRate( &p_track->fmt.video.i_frame_rate,
1659 &p_track->fmt.video.i_frame_rate_base,
1660 p_track, i_sample_description_index, i_chunk );
1661 p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
1662 (float)p_track->fmt.video.i_frame_rate_base;
1666 p_track->fmt.audio.i_channels =
1667 p_sample->data.p_sample_soun->i_channelcount;
1668 p_track->fmt.audio.i_rate =
1669 p_sample->data.p_sample_soun->i_sampleratehi;
1670 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1671 p_sample->data.p_sample_soun->i_sampleratehi *
1672 p_sample->data.p_sample_soun->i_samplesize;
1673 p_track->fmt.audio.i_bitspersample =
1674 p_sample->data.p_sample_soun->i_samplesize;
1676 if( p_track->i_sample_size != 0 &&
1677 p_sample->data.p_sample_soun->i_qt_version == 1 && p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1679 msg_Err( p_demux, "Invalid sample per packet value for qt_version 1" );
1680 return VLC_EGENERIC;
1689 /* It's a little ugly but .. there are special cases */
1690 switch( p_sample->i_type )
1692 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1693 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1695 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1698 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1700 MP4_Box_t *p_dac3_box = MP4_BoxGet( p_sample, "dac3", 0 );
1702 p_track->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1705 static const int pi_bitrate[] = {
1712 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
1713 p_track->fmt.audio.i_channels = 0;
1714 p_track->fmt.i_bitrate = 0;
1715 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
1716 p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
1717 p_track->fmt.audio.i_bitspersample = 0;
1721 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1723 p_track->fmt.i_codec = VLC_FOURCC( 'e', 'a', 'c', '3' );
1727 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1728 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1730 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1732 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1733 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1735 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1737 /* Buggy files workaround */
1738 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1739 p_sample->data.p_sample_soun->i_sampleratehi) )
1741 MP4_Box_data_sample_soun_t *p_soun =
1742 p_sample->data.p_sample_soun;
1744 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1745 "(%u), making both equal (report any problem).",
1746 p_track->i_timescale, p_soun->i_sampleratehi );
1748 if( p_soun->i_sampleratehi )
1749 p_track->i_timescale = p_soun->i_sampleratehi;
1751 p_soun->i_sampleratehi = p_track->i_timescale;
1756 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1757 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1760 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1761 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1762 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1763 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1764 /* FIXME UTF-8 doesn't work here ? */
1765 if( p_track->b_mac_encoding )
1766 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1768 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1771 case VLC_FOURCC('y','v','1','2'):
1772 p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1774 case VLC_FOURCC('y','u','v','2'):
1775 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1778 case VLC_FOURCC('i','n','2','4'):
1779 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1780 VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
1782 case VLC_FOURCC('f','l','3','2'):
1783 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1784 VLC_CODEC_F32L : VLC_CODEC_F32B;
1786 case VLC_FOURCC('f','l','6','4'):
1787 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1788 VLC_CODEC_F64L : VLC_CODEC_F64B;
1790 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
1792 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1793 if( p_soun->i_qt_version == 2 &&
1794 p_soun->i_qt_description > 20 + 28 )
1801 static const struct {
1805 vlc_fourcc_t i_codec;
1807 { 0x01, 0x03, 32, VLC_CODEC_F32L },
1808 { 0x01, 0x03, 64, VLC_CODEC_F64L },
1809 { 0x01|0x02, 0x03, 32, VLC_CODEC_F32B },
1810 { 0x01|0x02, 0x03, 64, VLC_CODEC_F64B },
1812 { 0x00, 0x05, 8, VLC_CODEC_U8 },
1813 { 0x00| 0x04, 0x05, 8, VLC_CODEC_S8 },
1815 { 0x00, 0x07, 16, VLC_CODEC_U16L },
1816 { 0x00|0x02, 0x07, 16, VLC_CODEC_U16B },
1817 { 0x00 |0x04, 0x07, 16, VLC_CODEC_S16L },
1818 { 0x00|0x02|0x04, 0x07, 16, VLC_CODEC_S16B },
1820 { 0x00, 0x07, 24, VLC_CODEC_U24L },
1821 { 0x00|0x02, 0x07, 24, VLC_CODEC_U24B },
1822 { 0x00 |0x04, 0x07, 24, VLC_CODEC_S24L },
1823 { 0x00|0x02|0x04, 0x07, 24, VLC_CODEC_S24B },
1825 { 0x00, 0x07, 32, VLC_CODEC_U32L },
1826 { 0x00|0x02, 0x07, 32, VLC_CODEC_U32B },
1827 { 0x00 |0x04, 0x07, 32, VLC_CODEC_S32L },
1828 { 0x00|0x02|0x04, 0x07, 32, VLC_CODEC_S32B },
1832 uint32_t i_bits = GetDWBE(&p_soun->p_qt_description[20 + 20]);
1833 uint32_t i_flags = GetDWBE(&p_soun->p_qt_description[20 + 24]);
1835 for( int i = 0; p_formats[i].i_codec; i++ )
1837 if( p_formats[i].i_bits == i_bits &&
1838 (i_flags & p_formats[i].i_mask) == p_formats[i].i_flags )
1840 p_track->fmt.i_codec = p_formats[i].i_codec;
1841 p_track->fmt.audio.i_bitspersample = i_bits;
1842 p_track->fmt.audio.i_blockalign = p_soun->i_channelcount * i_bits / 8;
1843 p_track->i_sample_size = p_track->fmt.audio.i_blockalign;
1845 p_soun->i_qt_version = 0;
1853 p_track->fmt.i_codec = p_sample->i_type;
1857 /* now see if esds is present and if so create a data packet
1858 with decoder_specific_info */
1859 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1860 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1861 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1862 ( p_esds->data.p_esds )&&
1865 /* First update information based on i_objectTypeIndication */
1866 switch( p_decconfig->i_objectTypeIndication )
1868 case( 0x20 ): /* MPEG4 VIDEO */
1869 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1872 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1873 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
1874 p_decconfig->p_decoder_specific_info[0] == 0xF8 &&
1875 (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
1877 p_track->fmt.i_codec = VLC_CODEC_ALS;
1885 case( 0x65): /* MPEG2 video */
1886 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1888 /* Theses are MPEG2-AAC */
1889 case( 0x66): /* main profile */
1890 case( 0x67): /* Low complexity profile */
1891 case( 0x68): /* Scaleable Sampling rate profile */
1892 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1894 /* true MPEG 2 audio */
1896 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1898 case( 0x6a): /* MPEG1 video */
1899 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1901 case( 0x6b): /* MPEG1 audio */
1902 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1904 case( 0x6c ): /* jpeg */
1905 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1907 case( 0x6d ): /* png */
1908 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
1910 case( 0x6e ): /* jpeg200 */
1911 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
1913 case( 0xa3 ): /* vc1 */
1914 p_track->fmt.i_codec = VLC_FOURCC( 'W','V','C','1' );
1918 case( 0xe0 ): /* NeroDigital: dvd subs */
1919 if( p_track->fmt.i_cat == SPU_ES )
1921 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1922 if( p_track->i_width > 0 )
1923 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1924 if( p_track->i_height > 0 )
1925 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1928 case( 0xe1 ): /* QCelp for 3gp */
1929 if( p_track->fmt.i_cat == AUDIO_ES )
1931 p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
1937 /* Unknown entry, but don't touch i_fourcc */
1939 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1940 p_decconfig->i_objectTypeIndication,
1941 p_track->i_track_ID );
1944 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1945 if( p_track->fmt.i_extra > 0 )
1947 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1948 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1949 p_track->fmt.i_extra );
1954 switch( p_sample->i_type )
1956 /* qt decoder, send the complete chunk */
1957 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1958 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1959 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1960 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1961 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1962 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1963 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1964 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1965 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1966 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1967 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1968 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1969 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1971 /* qt decoder, send the complete chunk */
1972 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1973 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1974 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1975 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1976 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1977 p_track->fmt.i_extra =
1978 p_sample->data.p_sample_vide->i_qt_image_description;
1979 if( p_track->fmt.i_extra > 0 )
1981 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1982 memcpy( p_track->fmt.p_extra,
1983 p_sample->data.p_sample_vide->p_qt_image_description,
1984 p_track->fmt.i_extra);
1987 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1988 p_track->fmt.audio.i_rate = 8000;
1989 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1990 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1991 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1992 p_track->fmt.i_extra =
1993 p_sample->data.p_sample_soun->i_qt_description;
1994 if( p_track->fmt.i_extra > 0 )
1996 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1997 memcpy( p_track->fmt.p_extra,
1998 p_sample->data.p_sample_soun->p_qt_description,
1999 p_track->fmt.i_extra);
2003 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
2004 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
2006 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
2010 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
2011 if( p_track->fmt.i_extra > 0 )
2013 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
2014 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
2015 p_track->fmt.i_extra );
2020 msg_Err( p_demux, "missing avcC" );
2025 case VLC_FOURCC('m','s',0x00,0x02):
2026 case VLC_FOURCC('m','s',0x00,0x11):
2027 case VLC_FOURCC('Q','c','l','p'):
2028 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
2039 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2044 /* given a time it return sample/chunk
2045 * it also update elst field of the track
2047 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2048 int64_t i_start, uint32_t *pi_chunk,
2049 uint32_t *pi_sample )
2051 demux_sys_t *p_sys = p_demux->p_sys;
2052 MP4_Box_t *p_box_stss;
2054 unsigned int i_sample;
2055 unsigned int i_chunk;
2058 /* FIXME see if it's needed to check p_track->i_chunk_count */
2059 if( p_track->i_chunk_count == 0 )
2060 return( VLC_EGENERIC );
2062 /* handle elst (find the correct one) */
2063 MP4_TrackSetELST( p_demux, p_track, i_start );
2064 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2066 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2067 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
2069 /* now calculate i_start for this elst */
2071 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
2079 /* to track time scale */
2080 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2081 /* add elst offset */
2082 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2083 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2084 elst->i_media_time[p_track->i_elst] > 0 )
2086 i_start += elst->i_media_time[p_track->i_elst];
2089 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2090 "ms (track)", p_track->i_elst,
2091 i_mvt * 1000 / p_sys->i_timescale,
2092 i_start * 1000 / p_track->i_timescale );
2096 /* convert absolute time to in timescale unit */
2097 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2100 /* we start from sample 0/chunk 0, hope it won't take too much time */
2101 /* *** find good chunk *** */
2102 for( i_chunk = 0; ; i_chunk++ )
2104 if( i_chunk + 1 >= p_track->i_chunk_count )
2106 /* at the end and can't check if i_start in this chunk,
2107 it will be check while searching i_sample */
2108 i_chunk = p_track->i_chunk_count - 1;
2112 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2113 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
2119 /* *** find sample in the chunk *** */
2120 i_sample = p_track->chunk[i_chunk].i_sample_first;
2121 i_dts = p_track->chunk[i_chunk].i_first_dts;
2122 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2125 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2126 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2129 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2130 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2132 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2137 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2141 i_sample += ( i_start - i_dts ) /
2142 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2147 if( i_sample >= p_track->i_sample_count )
2149 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2150 "(seeking too far) chunk=%d sample=%d",
2151 p_track->i_track_ID, i_chunk, i_sample );
2152 return( VLC_EGENERIC );
2156 /* *** Try to find nearest sync points *** */
2157 if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2159 MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2160 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2161 p_track->i_track_ID );
2162 for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2164 if( i_index >= p_stss->i_entry_count - 1 ||
2165 i_sample < p_stss->i_sample_number[i_index+1] )
2167 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2168 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
2169 i_sample, i_sync_sample );
2171 if( i_sync_sample <= i_sample )
2173 while( i_chunk > 0 &&
2174 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2179 while( i_chunk < p_track->i_chunk_count - 1 &&
2180 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2181 p_track->chunk[i_chunk].i_sample_count )
2184 i_sample = i_sync_sample;
2191 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2192 "Sample Box (stss)", p_track->i_track_ID );
2195 *pi_chunk = i_chunk;
2196 *pi_sample = i_sample;
2201 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2202 unsigned int i_chunk, unsigned int i_sample )
2204 bool b_reselect = false;
2206 /* now see if actual es is ok */
2207 if( p_track->i_chunk >= p_track->i_chunk_count ||
2208 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2209 p_track->chunk[i_chunk].i_sample_description_index )
2211 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2212 p_track->i_track_ID );
2214 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2215 p_track->p_es, &b_reselect );
2217 es_out_Del( p_demux->out, p_track->p_es );
2219 p_track->p_es = NULL;
2221 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2223 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2224 p_track->i_track_ID );
2226 p_track->b_ok = false;
2227 p_track->b_selected = false;
2228 return VLC_EGENERIC;
2232 /* select again the new decoder */
2235 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2238 p_track->i_chunk = i_chunk;
2239 p_track->i_sample = i_sample;
2241 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2244 /****************************************************************************
2246 ****************************************************************************
2247 * Parse track information and create all needed data to run a track
2248 * If it succeed b_ok is set to 1 else to 0
2249 ****************************************************************************/
2250 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2251 MP4_Box_t *p_box_trak,
2252 bool b_force_enable )
2254 demux_sys_t *p_sys = p_demux->p_sys;
2256 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2257 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2272 /* hint track unsupported */
2274 /* set default value (-> track unusable) */
2275 p_track->b_ok = false;
2276 p_track->b_enable = false;
2277 p_track->b_selected = false;
2278 p_track->b_chapter = false;
2279 p_track->b_mac_encoding = false;
2281 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2288 /* do we launch this track by default ? */
2290 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2291 if( !p_track->b_enable )
2292 p_track->fmt.i_priority = -1;
2294 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2295 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2296 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2300 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2303 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2304 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2306 if( ( !p_mdhd )||( !p_hdlr ) )
2311 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2312 if( !p_track->i_timescale )
2315 if( p_mdhd->data.p_mdhd->i_language_code < 0x800 )
2317 /* We can convert i_language_code into iso 639 code,
2319 strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2320 p_track->b_mac_encoding = true;
2324 for( i = 0; i < 3; i++ )
2325 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2329 switch( p_hdlr->data.p_hdlr->i_handler_type )
2332 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2336 p_track->fmt.i_cat = AUDIO_ES;
2340 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2344 p_track->fmt.i_cat = VIDEO_ES;
2351 p_track->fmt.i_cat = SPU_ES;
2358 p_track->i_elst = 0;
2359 p_track->i_elst_time = 0;
2360 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2362 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2365 msg_Warn( p_demux, "elst box found" );
2366 for( i = 0; i < elst->i_entry_count; i++ )
2368 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2369 "ms) rate=%d.%d", i,
2370 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2371 elst->i_media_time[i] >= 0 ?
2372 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2374 elst->i_media_rate_integer[i],
2375 elst->i_media_rate_fraction[i] );
2382 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2384 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2385 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2390 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2391 p_track->b_drms = p_drms != NULL;
2392 p_track->p_drms = p_track->b_drms ?
2393 p_drms->data.p_sample_soun->p_drms : NULL;
2397 p_drms = MP4_BoxGet( p_track->p_stsd, "drmi" );
2398 p_track->b_drms = p_drms != NULL;
2399 p_track->p_drms = p_track->b_drms ?
2400 p_drms->data.p_sample_vide->p_drms : NULL;
2404 p_track->p_skcr = MP4_BoxGet( p_drms, "sinf/skcr" );
2407 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2409 p_track->fmt.psz_language = strdup( language );
2412 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2415 MP4_Box_t *p_box_iter;
2416 for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2417 p_box_iter = p_box_iter->p_next )
2419 switch( p_box_iter->i_type )
2422 p_track->fmt.psz_description =
2423 strdup( p_box_iter->data.p_0xa9xxx->psz_text );
2426 p_track->fmt.psz_description =
2427 strdup( p_box_iter->data.p_name->psz_text );
2433 /* Create chunk index table and sample index table */
2434 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2435 TrackCreateSamplesIndex( p_demux, p_track ) )
2437 return; /* cannot create chunks index */
2440 p_track->i_chunk = 0;
2441 p_track->i_sample = 0;
2443 /* Mark chapter only track */
2444 if( p_sys->p_tref_chap )
2446 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2449 for( i = 0; i < p_chap->i_entry_count; i++ )
2451 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2453 p_track->b_chapter = true;
2454 p_track->b_enable = false;
2461 if( b_force_enable &&
2462 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2464 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2465 p_track->i_track_ID );
2466 p_track->b_enable = true;
2467 p_track->fmt.i_priority = 0;
2470 p_track->p_es = NULL;
2471 if( TrackCreateES( p_demux,
2472 p_track, p_track->i_chunk,
2473 p_track->b_chapter ? NULL : &p_track->p_es ) )
2475 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2476 p_track->i_track_ID );
2479 p_track->b_ok = true;
2483 for( i = 0; i < p_track->i_chunk_count; i++ )
2485 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2486 i, p_track->chunk[i].i_sample_count,
2487 p_track->chunk[i].i_first_dts );
2494 /****************************************************************************
2496 ****************************************************************************
2497 * Destroy a track created by MP4_TrackCreate.
2498 ****************************************************************************/
2499 static void MP4_TrackDestroy( mp4_track_t *p_track )
2501 unsigned int i_chunk;
2503 p_track->b_ok = false;
2504 p_track->b_enable = false;
2505 p_track->b_selected = false;
2507 es_format_Clean( &p_track->fmt );
2509 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2511 if( p_track->chunk )
2513 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2514 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2516 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2517 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2520 FREENULL( p_track->chunk );
2522 if( !p_track->i_sample_size )
2524 FREENULL( p_track->p_sample_size );
2528 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2531 if( !p_track->b_ok || p_track->b_chapter )
2533 return VLC_EGENERIC;
2536 if( p_track->b_selected )
2538 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2539 p_track->i_track_ID );
2543 return MP4_TrackSeek( p_demux, p_track, i_start );
2546 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2548 if( !p_track->b_ok || p_track->b_chapter )
2553 if( !p_track->b_selected )
2555 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2556 p_track->i_track_ID );
2561 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2562 p_track->p_es, false );
2565 p_track->b_selected = false;
2568 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2574 if( !p_track->b_ok || p_track->b_chapter )
2575 return VLC_EGENERIC;
2577 p_track->b_selected = false;
2579 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2580 &i_chunk, &i_sample ) )
2582 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2583 p_track->i_track_ID );
2584 return VLC_EGENERIC;
2587 p_track->b_selected = true;
2589 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2590 p_track->b_selected = true;
2592 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2597 * 3 types: for audio
2600 #define QT_V0_MAX_SAMPLES 1024
2601 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2604 MP4_Box_data_sample_soun_t *p_soun;
2606 if( p_track->i_sample_size == 0 )
2608 /* most simple case */
2609 return p_track->p_sample_size[p_track->i_sample];
2611 if( p_track->fmt.i_cat != AUDIO_ES )
2613 return p_track->i_sample_size;
2616 p_soun = p_track->p_sample->data.p_sample_soun;
2618 if( p_soun->i_qt_version == 1 )
2620 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
2621 if( p_track->fmt.audio.i_blockalign > 1 )
2622 i_samples = p_soun->i_sample_per_packet;
2624 i_size = i_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2626 else if( p_track->i_sample_size > 256 )
2628 /* We do that so we don't read too much data
2629 * (in this case we are likely dealing with compressed data) */
2630 i_size = p_track->i_sample_size;
2634 /* Read a bunch of samples at once */
2635 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2636 ( p_track->i_sample -
2637 p_track->chunk[p_track->i_chunk].i_sample_first );
2639 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2640 i_size = i_samples * p_track->i_sample_size;
2643 //fprintf( stderr, "size=%d\n", i_size );
2647 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2649 unsigned int i_sample;
2652 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2654 if( p_track->i_sample_size )
2656 MP4_Box_data_sample_soun_t *p_soun =
2657 p_track->p_sample->data.p_sample_soun;
2659 if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
2661 i_pos += ( p_track->i_sample -
2662 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2663 p_track->i_sample_size;
2667 /* we read chunk by chunk unless a blockalign is requested */
2668 if( p_track->fmt.audio.i_blockalign > 1 )
2669 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
2670 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2675 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2676 i_sample < p_track->i_sample; i_sample++ )
2678 i_pos += p_track->p_sample_size[i_sample];
2685 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2687 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2689 MP4_Box_data_sample_soun_t *p_soun;
2691 p_soun = p_track->p_sample->data.p_sample_soun;
2693 if( p_soun->i_qt_version == 1 )
2695 /* we read chunk by chunk unless a blockalign is requested */
2696 if( p_track->fmt.audio.i_blockalign > 1 )
2697 p_track->i_sample += p_soun->i_sample_per_packet;
2699 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
2701 else if( p_track->i_sample_size > 256 )
2703 /* We do that so we don't read too much data
2704 * (in this case we are likely dealing with compressed data) */
2705 p_track->i_sample += 1;
2710 p_track->i_sample += QT_V0_MAX_SAMPLES;
2711 if( p_track->i_sample >
2712 p_track->chunk[p_track->i_chunk].i_sample_first +
2713 p_track->chunk[p_track->i_chunk].i_sample_count )
2716 p_track->chunk[p_track->i_chunk].i_sample_first +
2717 p_track->chunk[p_track->i_chunk].i_sample_count;
2723 p_track->i_sample++;
2726 if( p_track->i_sample >= p_track->i_sample_count )
2727 return VLC_EGENERIC;
2729 /* Have we changed chunk ? */
2730 if( p_track->i_sample >=
2731 p_track->chunk[p_track->i_chunk].i_sample_first +
2732 p_track->chunk[p_track->i_chunk].i_sample_count )
2734 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2735 p_track->i_sample ) )
2737 msg_Warn( p_demux, "track[0x%x] will be disabled "
2738 "(cannot restart decoder)", p_track->i_track_ID );
2739 MP4_TrackUnselect( p_demux, p_track );
2740 return VLC_EGENERIC;
2744 /* Have we changed elst */
2745 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2747 demux_sys_t *p_sys = p_demux->p_sys;
2748 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2749 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2750 p_sys->i_timescale / (int64_t)1000000;
2752 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2753 i_mvt >= p_track->i_elst_time +
2754 elst->i_segment_duration[p_track->i_elst] )
2756 MP4_TrackSetELST( p_demux, p_track,
2757 MP4_TrackGetDTS( p_demux, p_track ) );
2764 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2767 demux_sys_t *p_sys = p_demux->p_sys;
2768 int i_elst_last = tk->i_elst;
2770 /* handle elst (find the correct one) */
2772 tk->i_elst_time = 0;
2773 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2775 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2776 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2778 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2780 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2782 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2786 tk->i_elst_time += i_dur;
2789 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2791 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2792 tk->i_elst = elst->i_entry_count - 1;
2793 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2796 if( elst->i_media_time[tk->i_elst] < 0 )
2799 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2802 if( i_elst_last != tk->i_elst )
2804 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2809 static const char *MP4_ConvertMacCode( uint16_t i_code )
2811 static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
2812 { "en", 0 }, { "fr", 1 }, { "de", 2 }, { "it", 3 }, { "nl", 4 },
2813 { "sv", 5 }, { "es", 6 }, { "da", 7 }, { "pt", 8 }, { "no", 9 },
2814 { "he", 10 }, { "ja", 11 }, { "ar", 12 }, { "fi", 13 }, { "el", 14 },
2815 { "is", 15 }, { "mt", 16 }, { "tr", 17 }, { "hr", 18 }, { "zh", 19 },
2816 { "ur", 20 }, { "hi", 21 }, { "th", 22 }, { "ko", 23 }, { "lt", 24 },
2817 { "pl", 25 }, { "hu", 26 }, { "et", 27 }, { "lv", 28 }, //{ "??", 29 },
2818 { "fo", 30 }, { "fa", 31 }, { "ru", 32 }, { "zh", 33 }, { "nl", 34 },
2819 { "ga", 35 }, { "sq", 36 }, { "ro", 37 }, { "cs", 38 }, { "sk", 39 },
2820 { "sl", 40 }, { "yi", 41 }, { "sr", 42 }, { "mk", 43 }, { "bg", 44 },
2821 { "uk", 45 }, { "be", 46 }, { "uz", 47 }, { "az", 48 }, { "kk", 48 },
2822 { "az", 50 }, { "hy", 51 }, { "ka", 52 }, { "mo", 53 }, { "ky", 54 },
2823 { "tg", 55 }, { "tk", 56 }, { "mn", 57 }, { "mn", 58 }, { "ps", 59 },
2824 { "ku", 60 }, { "ks", 61 }, { "sd", 62 }, { "bo", 63 }, { "ne", 64 },
2825 { "sa", 65 }, { "mr", 66 }, { "bn", 67 }, { "as", 68 }, { "gu", 69 },
2826 { "pa", 70 }, { "or", 71 }, { "ml", 72 }, { "kn", 73 }, { "ta", 74 },
2827 { "te", 75 }, { "si", 76 }, { "my", 77 }, { "km", 78 }, { "lo", 79 },
2828 { "vi", 80 }, { "id", 81 }, { "tl", 82 }, { "ms", 83 }, { "ms", 84 },
2829 { "am", 85 }, { "ti", 86 }, { "om", 87 }, { "so", 88 }, { "sw", 89 },
2830 { "rw", 90 }, { "rn", 91 }, { "ny", 92 }, { "mg", 93 }, { "eo", 94 },
2832 { "cy", 128 }, { "eu", 129 },
2833 { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
2834 { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
2835 { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
2836 { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
2841 for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
2843 if( p_cvt[i].i_code == i_code )
2844 return p_cvt[i].psz_iso639_1;