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>
40 #include "id3genres.h" /* for ATOM_gnre */
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
49 set_category( CAT_INPUT )
50 set_subcategory( SUBCAT_INPUT_DEMUX )
51 set_description( N_("MP4 stream demuxer") )
52 set_shortname( N_("MP4") )
53 set_capability( "demux", 240 )
54 set_callbacks( Open, Close )
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Demux ( demux_t * );
61 static int DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
62 static int Seek ( demux_t *, mtime_t );
63 static int Control ( demux_t *, int, va_list );
65 /* Contain all information about a chunk */
68 uint64_t i_offset; /* absolute position of this chunk in the file */
69 uint32_t i_sample_description_index; /* index for SampleEntry to use */
70 uint32_t i_sample_count; /* how many samples in this chunk */
71 uint32_t i_sample_first; /* index of the first sample in this chunk */
73 /* now provide way to calculate pts, dts, and offset without too
74 much memory and with fast access */
76 /* with this we can calculate dts/pts without waste memory */
77 uint64_t i_first_dts; /* DTS of the first sample */
78 uint64_t i_last_dts; /* DTS of the last sample */
79 uint32_t *p_sample_count_dts;
80 uint32_t *p_sample_delta_dts; /* dts delta */
82 uint32_t *p_sample_count_pts;
83 int32_t *p_sample_offset_pts; /* pts-dts */
85 /* TODO if needed add pts
86 but quickly *add* support for edts and seeking */
90 /* Contain all needed information for read all track with vlc */
93 unsigned int i_track_ID;/* this should be unique */
95 int b_ok; /* The track is usable */
96 int b_enable; /* is the trak enable by default */
97 bool b_selected; /* is the trak being played */
98 bool b_chapter; /* True when used for chapter only */
105 /* display size only ! */
109 /* more internal data */
110 uint64_t i_timescale; /* time scale for this track only */
113 int i_elst; /* current elst */
114 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
115 MP4_Box_t *p_elst; /* elst (could be NULL) */
117 /* give the next sample to read, i_chunk is to find quickly where
118 the sample is located */
119 uint32_t i_sample; /* next sample to read */
120 uint32_t i_chunk; /* chunk where next sample is stored */
121 /* total count of chunk and sample */
122 uint32_t i_chunk_count;
123 uint32_t i_sample_count;
125 mp4_chunk_t *chunk; /* always defined for each chunk */
127 /* sample size, p_sample_size defined only if i_sample_size == 0
128 else i_sample_size is size for all sample */
129 uint32_t i_sample_size;
130 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
131 too much time to do sumations each time*/
133 MP4_Box_t *p_stbl; /* will contain all timing information */
134 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
135 MP4_Box_t *p_sample;/* point on actual sdsd */
144 MP4_Box_t *p_root; /* container for the whole file */
148 uint64_t i_time; /* time position of the presentation
149 * in movie timescale */
150 uint64_t i_timescale; /* movie time scale */
151 uint64_t i_duration; /* movie duration */
152 unsigned int i_tracks; /* number of tracks */
153 mp4_track_t *track; /* array of track */
154 float f_fps; /* number of frame per seconds */
157 MP4_Box_t *p_tref_chap;
160 input_title_t *p_title;
163 /*****************************************************************************
164 * Declaration of local function
165 *****************************************************************************/
166 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *, bool b_force_enable );
167 static void MP4_TrackDestroy( mp4_track_t * );
169 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
170 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
172 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
174 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
175 static int MP4_TrackSampleSize( mp4_track_t * );
176 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
177 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
179 static void MP4_UpdateSeekpoint( demux_t * );
180 static const char *MP4_ConvertMacCode( uint16_t );
182 /* Return time in s of a track */
183 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
185 #define chunk p_track->chunk[p_track->i_chunk]
187 unsigned int i_index = 0;
188 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
189 int64_t i_dts = chunk.i_first_dts;
191 while( i_sample > 0 )
193 if( i_sample > chunk.p_sample_count_dts[i_index] )
195 i_dts += chunk.p_sample_count_dts[i_index] *
196 chunk.p_sample_delta_dts[i_index];
197 i_sample -= chunk.p_sample_count_dts[i_index];
202 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
209 /* now handle elst */
210 if( p_track->p_elst )
212 demux_sys_t *p_sys = p_demux->p_sys;
213 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
215 /* convert to offset */
216 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
217 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
218 elst->i_media_time[p_track->i_elst] > 0 )
220 i_dts -= elst->i_media_time[p_track->i_elst];
223 /* add i_elst_time */
224 i_dts += p_track->i_elst_time * p_track->i_timescale /
227 if( i_dts < 0 ) i_dts = 0;
230 return INT64_C(1000000) * i_dts / p_track->i_timescale;
233 static inline int64_t MP4_TrackGetPTSDelta( mp4_track_t *p_track )
235 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
236 unsigned int i_index = 0;
237 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
239 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
242 for( i_index = 0;; i_index++ )
244 if( i_sample < ck->p_sample_count_pts[i_index] )
245 return ck->p_sample_offset_pts[i_index] * INT64_C(1000000) /
246 (int64_t)p_track->i_timescale;
248 i_sample -= ck->p_sample_count_pts[i_index];
252 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
254 return INT64_C(1000000) * p_sys->i_time / p_sys->i_timescale;
257 static void LoadChapter( demux_t *p_demux );
259 /*****************************************************************************
260 * Open: check file and initializes MP4 structures
261 *****************************************************************************/
262 static int Open( vlc_object_t * p_this )
264 demux_t *p_demux = (demux_t *)p_this;
267 const uint8_t *p_peek;
278 /* A little test to see if it could be a mp4 */
279 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
281 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
292 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
299 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
302 msg_Warn( p_demux, "MP4 plugin discarded (not fastseekable)" );
306 /*Set exported functions */
307 p_demux->pf_demux = Demux;
308 p_demux->pf_control = Control;
310 /* create our structure that will contains all data */
311 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
313 /* Now load all boxes ( except raw data ) */
314 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
316 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
320 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
322 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
324 switch( p_ftyp->data.p_ftyp->i_major_brand )
328 "ISO Media file (isom) version %d.",
329 p_ftyp->data.p_ftyp->i_minor_version );
332 case( VLC_FOURCC( '3', 'g', 'p', '5' ) ):
333 case( VLC_FOURCC( '3', 'g', 'p', '6' ) ):
334 case( VLC_FOURCC( '3', 'g', 'p', '7' ) ):
335 msg_Dbg( p_demux, "3GPP Media file Release: %c",
336 #ifdef WORDS_BIGENDIAN
337 p_ftyp->data.p_ftyp->i_major_brand
339 p_ftyp->data.p_ftyp->i_major_brand >> 24
343 case( VLC_FOURCC( 'q', 't', ' ', ' ') ):
344 msg_Dbg( p_demux, "Apple QuickTime file" );
346 case( VLC_FOURCC( 'i', 's', 'm', 'l') ):
347 msg_Dbg( p_demux, "PIFF (= isml = fMP4) 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_item = input_item_New( psz_ref, NULL );
442 input_item_CopyOptions( p_current, p_item );
443 input_item_node_AppendItem( p_subitems, p_item );
444 vlc_gc_decref( p_item );
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 )
496 /* Search the first chap reference (like quicktime) and
497 * check that at least 1 stream is enabled */
498 p_sys->p_tref_chap = NULL;
499 b_enabled_es = false;
500 for( i = 0; i < p_sys->i_tracks; i++ )
502 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
505 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
506 if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
509 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
510 if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
511 p_sys->p_tref_chap = p_chap;
514 /* now process each track and extract all useful information */
515 for( i = 0; i < p_sys->i_tracks; i++ )
517 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
518 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
520 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
523 switch( p_sys->track[i].fmt.i_cat )
532 psz_cat = "subtitle";
540 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
541 p_sys->track[i].i_track_ID, psz_cat,
542 p_sys->track[i].b_enable ? "enable":"disable",
543 p_sys->track[i].fmt.psz_language ?
544 p_sys->track[i].fmt.psz_language : "undef" );
546 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
548 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
549 p_sys->track[i].i_track_ID,
550 p_sys->track[i].fmt.psz_language ?
551 p_sys->track[i].fmt.psz_language : "undef" );
555 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
556 p_sys->track[i].i_track_ID );
561 LoadChapter( p_demux );
568 MP4_BoxFree( p_demux->s, p_sys->p_root );
574 /*****************************************************************************
575 * Demux: read packet and send them to decoders
576 *****************************************************************************
577 * TODO check for newly selected track (ie audio upt to now )
578 *****************************************************************************/
579 static int Demux( demux_t *p_demux )
581 demux_sys_t *p_sys = p_demux->p_sys;
582 unsigned int i_track;
585 unsigned int i_track_selected;
587 /* check for newly selected/unselected track */
588 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
591 mp4_track_t *tk = &p_sys->track[i_track];
594 if( !tk->b_ok || tk->b_chapter ||
595 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
600 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
602 if( tk->b_selected && !b )
604 MP4_TrackUnselect( p_demux, tk );
606 else if( !tk->b_selected && b)
608 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
617 if( i_track_selected <= 0 )
619 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
620 if( p_sys->i_timescale > 0 )
622 int64_t i_length = (mtime_t)1000000 *
623 (mtime_t)p_sys->i_duration /
624 (mtime_t)p_sys->i_timescale;
625 if( MP4_GetMoviePTS( p_sys ) >= i_length )
630 msg_Warn( p_demux, "no track selected, exiting..." );
635 MP4_UpdateSeekpoint( p_demux );
637 /* first wait for the good time to read a packet */
638 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
640 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
642 /* we will read 100ms for each stream so ...*/
643 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
645 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
647 mp4_track_t *tk = &p_sys->track[i_track];
649 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
652 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
655 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
656 MP4_TrackGetDTS( p_demux, tk ),
657 MP4_GetMoviePTS( p_sys ) );
660 if( MP4_TrackSampleSize( tk ) > 0 )
666 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
668 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
670 MP4_TrackUnselect( p_demux, tk );
676 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
678 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
680 MP4_TrackUnselect( p_demux, tk );
684 else if( tk->fmt.i_cat == SPU_ES )
686 if( tk->fmt.i_codec == VLC_CODEC_SUBT &&
687 p_block->i_buffer >= 2 )
689 size_t i_size = GetWBE( p_block->p_buffer );
691 if( i_size + 2 <= p_block->i_buffer )
694 /* remove the length field, and append a '\0' */
695 memmove( &p_block->p_buffer[0],
696 &p_block->p_buffer[2], i_size );
697 p_block->p_buffer[i_size] = '\0';
698 p_block->i_buffer = i_size + 1;
700 /* convert \r -> \n */
701 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
709 p_block->i_buffer = 0;
714 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
716 i_delta = MP4_TrackGetPTSDelta( tk );
718 p_block->i_pts = p_block->i_dts + i_delta;
719 else if( tk->fmt.i_cat != VIDEO_ES )
720 p_block->i_pts = p_block->i_dts;
722 p_block->i_pts = VLC_TS_INVALID;
724 es_out_Send( p_demux->out, tk->p_es, p_block );
728 if( MP4_TrackNextSample( p_demux, tk ) )
736 static void MP4_UpdateSeekpoint( demux_t *p_demux )
738 demux_sys_t *p_sys = p_demux->p_sys;
741 if( !p_sys->p_title )
743 i_time = MP4_GetMoviePTS( p_sys );
744 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
746 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
751 if( i != p_demux->info.i_seekpoint && i >= 0 )
753 p_demux->info.i_seekpoint = i;
754 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
757 /*****************************************************************************
759 ******************************************************************************/
760 static int Seek( demux_t *p_demux, mtime_t i_date )
762 demux_sys_t *p_sys = p_demux->p_sys;
763 unsigned int i_track;
765 /* First update update global time */
766 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
767 p_sys->i_pcr = i_date;
769 /* Now for each stream try to go to this time */
770 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
772 mp4_track_t *tk = &p_sys->track[i_track];
773 MP4_TrackSeek( p_demux, tk, i_date );
775 MP4_UpdateSeekpoint( p_demux );
777 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
782 /*****************************************************************************
784 *****************************************************************************/
785 static int Control( demux_t *p_demux, int i_query, va_list args )
787 demux_sys_t *p_sys = p_demux->p_sys;
794 case DEMUX_GET_POSITION:
795 pf = (double*)va_arg( args, double * );
796 if( p_sys->i_duration > 0 )
798 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
806 case DEMUX_SET_POSITION:
807 f = (double)va_arg( args, double );
808 if( p_sys->i_timescale > 0 )
810 i64 = (int64_t)( f * (double)1000000 *
811 (double)p_sys->i_duration /
812 (double)p_sys->i_timescale );
813 return Seek( p_demux, i64 );
815 else return VLC_SUCCESS;
818 pi64 = (int64_t*)va_arg( args, int64_t * );
819 if( p_sys->i_timescale > 0 )
821 *pi64 = (mtime_t)1000000 *
822 (mtime_t)p_sys->i_time /
823 (mtime_t)p_sys->i_timescale;
829 i64 = (int64_t)va_arg( args, int64_t );
830 return Seek( p_demux, i64 );
832 case DEMUX_GET_LENGTH:
833 pi64 = (int64_t*)va_arg( args, int64_t * );
834 if( p_sys->i_timescale > 0 )
836 *pi64 = (mtime_t)1000000 *
837 (mtime_t)p_sys->i_duration /
838 (mtime_t)p_sys->i_timescale;
844 pf = (double*)va_arg( args, double* );
850 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
851 MP4_Box_t *p_0xa9xxx;
853 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
856 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
863 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
864 p_0xa9xxx = p_0xa9xxx->p_next )
867 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
870 /* FIXME FIXME: should convert from whatever the character
871 * encoding of MP4 meta data is to UTF-8. */
872 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
873 if( psz_utf ) { EnsureUTF8( psz_utf ); \
874 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
876 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
877 switch( p_0xa9xxx->i_type )
879 case ATOM_0xa9nam: /* Full name */
880 SET( vlc_meta_SetTitle );
883 SET( vlc_meta_SetArtist );
886 SET( vlc_meta_SetArtist );
889 SET( vlc_meta_SetCopyright );
891 case ATOM_0xa9day: /* Creation Date */
892 SET( vlc_meta_SetDate );
894 case ATOM_0xa9des: /* Description */
895 SET( vlc_meta_SetDescription );
897 case ATOM_0xa9gen: /* Genre */
898 SET( vlc_meta_SetGenre );
902 if( p_0xa9xxx->data.p_gnre->i_genre <= NUM_GENRES )
903 vlc_meta_SetGenre( p_meta, ppsz_genres[p_0xa9xxx->data.p_gnre->i_genre - 1] );
906 case ATOM_0xa9alb: /* Album */
907 SET( vlc_meta_SetAlbum );
910 case ATOM_0xa9trk: /* Track */
911 SET( vlc_meta_SetTrackNum );
916 snprintf( psz_trck, sizeof( psz_trck ), "%i",
917 p_0xa9xxx->data.p_trkn->i_track_number );
918 vlc_meta_SetTrackNum( p_meta, psz_trck );
921 case ATOM_0xa9cmt: /* Commment */
922 SET( vlc_meta_SetDescription );
925 case ATOM_0xa9url: /* URL */
926 SET( vlc_meta_SetURL );
929 case ATOM_0xa9too: /* Encoder Tool */
930 case ATOM_0xa9enc: /* Encoded By */
931 SET( vlc_meta_SetEncodedBy );
938 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
940 { ATOM_0xa9wrt, N_("Writer") },
941 { ATOM_0xa9com, N_("Composer") },
942 { ATOM_0xa9prd, N_("Producer") },
943 { ATOM_0xa9inf, N_("Information") },
944 { ATOM_0xa9dir, N_("Director") },
945 { ATOM_0xa9dis, N_("Disclaimer") },
946 { ATOM_0xa9req, N_("Requirements") },
947 { ATOM_0xa9fmt, N_("Original Format") },
948 { ATOM_0xa9dsa, N_("Display Source As") },
949 { ATOM_0xa9hst, N_("Host Computer") },
950 { ATOM_0xa9prf, N_("Performers") },
951 { ATOM_0xa9ope, N_("Original Performer") },
952 { ATOM_0xa9src, N_("Providers Source Content") },
953 { ATOM_0xa9wrn, N_("Warning") },
954 { ATOM_0xa9swr, N_("Software") },
955 { ATOM_0xa9lyr, N_("Lyrics") },
956 { ATOM_0xa9mak, N_("Make") },
957 { ATOM_0xa9mod, N_("Model") },
958 { ATOM_0xa9PRD, N_("Product") },
959 { ATOM_0xa9grp, N_("Grouping") },
962 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
964 if( p_0xa9xxx->i_type == xa9typetoextrameta[i].xa9_type )
966 char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" );
969 EnsureUTF8( psz_utf );
970 vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
980 case DEMUX_GET_TITLE_INFO:
982 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
983 int *pi_int = (int*)va_arg( args, int* );
984 int *pi_title_offset = (int*)va_arg( args, int* );
985 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
987 if( !p_sys->p_title )
991 *ppp_title = malloc( sizeof( input_title_t**) );
992 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
993 *pi_title_offset = 0;
994 *pi_seekpoint_offset = 0;
997 case DEMUX_SET_TITLE:
999 const int i_title = (int)va_arg( args, int );
1000 if( !p_sys->p_title || i_title != 0 )
1001 return VLC_EGENERIC;
1004 case DEMUX_SET_SEEKPOINT:
1006 const int i_seekpoint = (int)va_arg( args, int );
1007 if( !p_sys->p_title )
1008 return VLC_EGENERIC;
1009 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
1012 case DEMUX_SET_NEXT_DEMUX_TIME:
1013 case DEMUX_SET_GROUP:
1014 case DEMUX_HAS_UNSUPPORTED_META:
1015 case DEMUX_GET_ATTACHMENTS:
1016 case DEMUX_GET_PTS_DELAY:
1017 case DEMUX_CAN_RECORD:
1018 return VLC_EGENERIC;
1021 msg_Warn( p_demux, "control query %u unimplemented", i_query );
1022 return VLC_EGENERIC;
1026 /*****************************************************************************
1027 * Close: frees unused data
1028 *****************************************************************************/
1029 static void Close ( vlc_object_t * p_this )
1031 demux_t * p_demux = (demux_t *)p_this;
1032 demux_sys_t *p_sys = p_demux->p_sys;
1033 unsigned int i_track;
1035 msg_Dbg( p_demux, "freeing all memory" );
1037 MP4_BoxFree( p_demux->s, p_sys->p_root );
1038 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1040 MP4_TrackDestroy( &p_sys->track[i_track] );
1042 FREENULL( p_sys->track );
1044 if( p_sys->p_title )
1045 vlc_input_title_Delete( p_sys->p_title );
1052 /****************************************************************************
1053 * Local functions, specific to vlc
1054 ****************************************************************************/
1056 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1058 demux_sys_t *p_sys = p_demux->p_sys;
1061 p_sys->p_title = vlc_input_title_New();
1062 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1064 seekpoint_t *s = vlc_seekpoint_New();
1066 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1067 EnsureUTF8( s->psz_name );
1068 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1069 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1072 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1074 demux_sys_t *p_sys = p_demux->p_sys;
1076 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1078 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1079 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( tk );
1080 const unsigned int i_size = MP4_TrackSampleSize( tk );
1082 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1085 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1086 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1090 seekpoint_t *s = vlc_seekpoint_New();
1092 s->psz_name = strndup( &p_buffer[2], i_len );
1093 EnsureUTF8( s->psz_name );
1095 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1097 if( !p_sys->p_title )
1098 p_sys->p_title = vlc_input_title_New();
1099 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1102 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1103 tk->chunk[tk->i_chunk].i_sample_count )
1107 static void LoadChapter( demux_t *p_demux )
1109 demux_sys_t *p_sys = p_demux->p_sys;
1112 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1114 LoadChapterGpac( p_demux, p_chpl );
1116 else if( p_sys->p_tref_chap )
1118 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1121 /* Load the first subtitle track like quicktime */
1122 for( i = 0; i < p_chap->i_entry_count; i++ )
1124 for( j = 0; j < p_sys->i_tracks; j++ )
1126 mp4_track_t *tk = &p_sys->track[j];
1127 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1128 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_SUBT )
1131 if( j < p_sys->i_tracks )
1133 LoadChapterApple( p_demux, &p_sys->track[j] );
1139 /* Add duration if titles are enabled */
1140 if( p_sys->p_title )
1142 p_sys->p_title->i_length = (uint64_t)1000000 *
1143 (uint64_t)p_sys->i_duration / (uint64_t)p_sys->i_timescale;
1147 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1148 static int TrackCreateChunksIndex( demux_t *p_demux,
1149 mp4_track_t *p_demux_track )
1151 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1154 unsigned int i_chunk;
1155 unsigned int i_index, i_last;
1157 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1158 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1159 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1161 return( VLC_EGENERIC );
1164 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1165 if( !p_demux_track->i_chunk_count )
1167 msg_Warn( p_demux, "no chunk defined" );
1168 return( VLC_EGENERIC );
1170 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1171 sizeof( mp4_chunk_t ) );
1172 if( p_demux_track->chunk == NULL )
1177 /* first we read chunk offset */
1178 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1180 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1182 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1184 ck->i_first_dts = 0;
1185 ck->p_sample_count_dts = NULL;
1186 ck->p_sample_delta_dts = NULL;
1187 ck->p_sample_count_pts = NULL;
1188 ck->p_sample_offset_pts = NULL;
1191 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1192 to be used for the sample XXX begin to 1
1193 We construct it begining at the end */
1194 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1195 i_index = p_stsc->data.p_stsc->i_entry_count;
1198 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1199 return( VLC_EGENERIC );
1204 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1205 i_chunk < i_last; i_chunk++ )
1207 if( i_chunk >= p_demux_track->i_chunk_count )
1209 msg_Warn( p_demux, "corrupted chunk table" );
1210 return VLC_EGENERIC;
1213 p_demux_track->chunk[i_chunk].i_sample_description_index =
1214 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1215 p_demux_track->chunk[i_chunk].i_sample_count =
1216 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1218 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1221 p_demux_track->chunk[0].i_sample_first = 0;
1222 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1224 p_demux_track->chunk[i_chunk].i_sample_first =
1225 p_demux_track->chunk[i_chunk-1].i_sample_first +
1226 p_demux_track->chunk[i_chunk-1].i_sample_count;
1229 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1230 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1235 static int TrackCreateSamplesIndex( demux_t *p_demux,
1236 mp4_track_t *p_demux_track )
1239 MP4_Box_data_stsz_t *stsz;
1240 MP4_Box_data_stts_t *stts;
1241 /* TODO use also stss and stsh table for seeking */
1242 /* FIXME use edit table */
1247 int64_t i_index_sample_used;
1252 * Gives the sample size for each samples. There is also a stz2 table
1253 * (compressed form) that we need to implement TODO */
1254 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1257 /* FIXME and stz2 */
1258 msg_Warn( p_demux, "cannot find STSZ box" );
1259 return VLC_EGENERIC;
1261 stsz = p_box->data.p_stsz;
1264 * Gives mapping between sample and decoding time
1266 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1269 msg_Warn( p_demux, "cannot find STTS box" );
1270 return VLC_EGENERIC;
1272 stts = p_box->data.p_stts;
1274 /* Use stsz table to create a sample number -> sample size table */
1275 p_demux_track->i_sample_count = stsz->i_sample_count;
1276 if( stsz->i_sample_size )
1278 /* 1: all sample have the same size, so no need to construct a table */
1279 p_demux_track->i_sample_size = stsz->i_sample_size;
1280 p_demux_track->p_sample_size = NULL;
1284 /* 2: each sample can have a different size */
1285 p_demux_track->i_sample_size = 0;
1286 p_demux_track->p_sample_size =
1287 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1288 if( p_demux_track->p_sample_size == NULL )
1291 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1293 p_demux_track->p_sample_size[i_sample] =
1294 stsz->i_entry_size[i_sample];
1298 /* Use stts table to create a sample number -> dts table.
1299 * XXX: if we don't want to waste too much memory, we can't expand
1300 * the box! so each chunk will contain an "extract" of this table
1301 * for fast research (problem with raw stream where a sample is sometime
1302 * just channels*bits_per_sample/8 */
1305 i_index = 0; i_index_sample_used = 0;
1306 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1308 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1309 int64_t i_entry, i_sample_count, i;
1311 /* save first dts */
1312 ck->i_first_dts = i_next_dts;
1313 ck->i_last_dts = i_next_dts;
1315 /* count how many entries are needed for this chunk
1316 * for p_sample_delta_dts and p_sample_count_dts */
1317 i_sample_count = ck->i_sample_count;
1320 while( i_sample_count > 0 )
1322 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1323 /* don't count already used sample in this entry */
1325 i_sample_count += i_index_sample_used;
1331 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1332 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1334 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1338 i_sample_count = ck->i_sample_count;
1339 for( i = 0; i < i_entry; i++ )
1344 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1346 i_used = __MIN( i_rest, i_sample_count );
1348 i_index_sample_used += i_used;
1349 i_sample_count -= i_used;
1350 i_next_dts += i_used * stts->i_sample_delta[i_index];
1352 ck->p_sample_count_dts[i] = i_used;
1353 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1355 ck->i_last_dts = i_next_dts - ck->p_sample_delta_dts[i];
1357 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1360 i_index_sample_used = 0;
1366 * Gives the delta between decoding time (dts) and composition table (pts)
1368 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1371 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1373 msg_Warn( p_demux, "CTTS table" );
1375 /* Create pts-dts table per chunk */
1376 i_index = 0; i_index_sample_used = 0;
1377 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1379 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1380 int64_t i_entry, i_sample_count, i;
1382 /* count how many entries are needed for this chunk
1383 * for p_sample_delta_dts and p_sample_count_dts */
1384 i_sample_count = ck->i_sample_count;
1387 while( i_sample_count > 0 )
1389 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1391 /* don't count already used sample in this entry */
1393 i_sample_count += i_index_sample_used;
1399 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1400 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1401 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1405 i_sample_count = ck->i_sample_count;
1406 for( i = 0; i < i_entry; i++ )
1411 i_rest = ctts->i_sample_count[i_index] -
1412 i_index_sample_used;
1414 i_used = __MIN( i_rest, i_sample_count );
1416 i_index_sample_used += i_used;
1417 i_sample_count -= i_used;
1419 ck->p_sample_count_pts[i] = i_used;
1420 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1422 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1425 i_index_sample_used = 0;
1431 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1432 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1433 i_next_dts / p_demux_track->i_timescale );
1439 * It computes the sample rate for a video track using the given sample
1442 static void TrackGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
1443 const mp4_track_t *p_track,
1444 unsigned i_sd_index,
1450 if( p_track->i_chunk_count <= 0 )
1454 const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
1455 while( p_chunk > &p_track->chunk[0] &&
1456 p_chunk[-1].i_sample_description_index == i_sd_index )
1461 uint64_t i_sample = 0;
1462 uint64_t i_first_dts = p_chunk->i_first_dts;
1463 uint64_t i_last_dts;
1466 i_sample += p_chunk->i_sample_count;
1467 i_last_dts = p_chunk->i_last_dts;
1470 while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
1471 p_chunk->i_sample_description_index == i_sd_index );
1473 if( i_sample > 1 && i_first_dts < i_last_dts )
1474 vlc_ureduce( pi_num, pi_den,
1475 ( i_sample - 1) * p_track->i_timescale,
1476 i_last_dts - i_first_dts,
1482 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1484 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1485 unsigned int i_chunk, es_out_id_t **pp_es )
1487 const unsigned i_sample_description_index =
1488 p_track->chunk[i_chunk].i_sample_description_index;
1489 MP4_Box_t *p_sample;
1498 if( !i_sample_description_index )
1500 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1501 p_track->i_track_ID );
1502 return VLC_EGENERIC;
1505 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1506 i_sample_description_index - 1 );
1509 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1511 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1512 p_track->i_track_ID );
1513 return VLC_EGENERIC;
1516 p_track->p_sample = p_sample;
1518 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
1520 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
1522 p_sample->i_type = p_frma->data.p_frma->i_type;
1525 p_enda = MP4_BoxGet( p_sample, "wave/enda" );
1527 p_enda = MP4_BoxGet( p_sample, "enda" );
1529 p_pasp = MP4_BoxGet( p_sample, "pasp" );
1531 if( p_track->fmt.i_cat == AUDIO_ES && ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1533 MP4_Box_data_sample_soun_t *p_soun;
1535 p_soun = p_sample->data.p_sample_soun;
1537 if( p_soun->i_qt_version == 0 )
1539 switch( p_sample->i_type )
1541 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1542 p_soun->i_qt_version = 1;
1543 p_soun->i_sample_per_packet = 64;
1544 p_soun->i_bytes_per_packet = 34;
1545 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1546 p_soun->i_bytes_per_sample = 2;
1548 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1549 p_soun->i_qt_version = 1;
1550 p_soun->i_sample_per_packet = 6;
1551 p_soun->i_bytes_per_packet = 2;
1552 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1553 p_soun->i_bytes_per_sample = 2;
1555 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1556 p_soun->i_qt_version = 1;
1557 p_soun->i_sample_per_packet = 12;
1558 p_soun->i_bytes_per_packet = 2;
1559 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1560 p_soun->i_bytes_per_sample = 2;
1562 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1563 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1564 p_soun->i_samplesize = 8;
1565 p_track->i_sample_size = p_soun->i_channelcount;
1567 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1568 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1569 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1570 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1571 /* What would be the fun if you could trust the .mov */
1572 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1579 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1581 p_soun->i_qt_version = 0;
1584 else if( p_track->fmt.i_cat == AUDIO_ES && p_sample->data.p_sample_soun->i_qt_version == 1 )
1586 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1588 switch( p_sample->i_type )
1590 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1591 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1593 if( p_track->i_sample_size > 1 )
1594 p_soun->i_qt_version = 0;
1597 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1598 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1599 case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
1600 p_soun->i_qt_version = 0;
1608 switch( p_track->fmt.i_cat )
1611 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1612 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1613 p_track->fmt.video.i_bits_per_pixel =
1614 p_sample->data.p_sample_vide->i_depth;
1616 /* fall on display size */
1617 if( p_track->fmt.video.i_width <= 0 )
1618 p_track->fmt.video.i_width = p_track->i_width;
1619 if( p_track->fmt.video.i_height <= 0 )
1620 p_track->fmt.video.i_height = p_track->i_height;
1622 /* Find out apect ratio from display size */
1623 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1624 /* Work-around buggy muxed files */
1625 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1627 p_track->fmt.video.i_sar_num = p_track->i_width * p_track->fmt.video.i_height;
1628 p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
1630 if( p_pasp && p_pasp->data.p_pasp->i_horizontal_spacing > 0 &&
1631 p_pasp->data.p_pasp->i_vertical_spacing > 0 )
1633 p_track->fmt.video.i_sar_num = p_pasp->data.p_pasp->i_horizontal_spacing;
1634 p_track->fmt.video.i_sar_den = p_pasp->data.p_pasp->i_vertical_spacing;
1637 /* Support for cropping (eg. in H263 files) */
1638 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1639 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1642 TrackGetESSampleRate( &p_track->fmt.video.i_frame_rate,
1643 &p_track->fmt.video.i_frame_rate_base,
1644 p_track, i_sample_description_index, i_chunk );
1645 p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
1646 (float)p_track->fmt.video.i_frame_rate_base;
1650 p_track->fmt.audio.i_channels =
1651 p_sample->data.p_sample_soun->i_channelcount;
1652 p_track->fmt.audio.i_rate =
1653 p_sample->data.p_sample_soun->i_sampleratehi;
1654 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1655 p_sample->data.p_sample_soun->i_sampleratehi *
1656 p_sample->data.p_sample_soun->i_samplesize;
1657 p_track->fmt.audio.i_bitspersample =
1658 p_sample->data.p_sample_soun->i_samplesize;
1660 if( p_track->i_sample_size != 0 &&
1661 p_sample->data.p_sample_soun->i_qt_version == 1 && p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1663 msg_Err( p_demux, "Invalid sample per packet value for qt_version 1" );
1664 return VLC_EGENERIC;
1673 /* It's a little ugly but .. there are special cases */
1674 switch( p_sample->i_type )
1676 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1677 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1679 p_track->fmt.i_codec = VLC_CODEC_MPGA;
1682 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1684 MP4_Box_t *p_dac3_box = MP4_BoxGet( p_sample, "dac3", 0 );
1686 p_track->fmt.i_codec = VLC_CODEC_A52;
1689 static const int pi_bitrate[] = {
1696 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
1697 p_track->fmt.audio.i_channels = 0;
1698 p_track->fmt.i_bitrate = 0;
1699 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
1700 p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
1701 p_track->fmt.audio.i_bitspersample = 0;
1705 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1707 p_track->fmt.i_codec = VLC_CODEC_EAC3;
1711 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1712 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1714 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1716 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1717 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1719 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1721 /* Buggy files workaround */
1722 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1723 p_sample->data.p_sample_soun->i_sampleratehi) )
1725 MP4_Box_data_sample_soun_t *p_soun =
1726 p_sample->data.p_sample_soun;
1728 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1729 "(%u), making both equal (report any problem).",
1730 p_track->i_timescale, p_soun->i_sampleratehi );
1732 if( p_soun->i_sampleratehi )
1733 p_track->i_timescale = p_soun->i_sampleratehi;
1735 p_soun->i_sampleratehi = p_track->i_timescale;
1740 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1741 p_track->fmt.i_codec = VLC_CODEC_H263;
1744 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1745 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1746 p_track->fmt.i_codec = VLC_CODEC_SUBT;
1747 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1748 /* FIXME UTF-8 doesn't work here ? */
1749 if( p_track->b_mac_encoding )
1750 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1752 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1755 case VLC_FOURCC('y','v','1','2'):
1756 p_track->fmt.i_codec = VLC_CODEC_YV12;
1758 case VLC_FOURCC('y','u','v','2'):
1759 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1762 case VLC_FOURCC('i','n','2','4'):
1763 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1764 VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
1766 case VLC_FOURCC('f','l','3','2'):
1767 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1768 VLC_CODEC_F32L : VLC_CODEC_F32B;
1770 case VLC_FOURCC('f','l','6','4'):
1771 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1772 VLC_CODEC_F64L : VLC_CODEC_F64B;
1774 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
1776 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1777 if( p_soun->i_qt_version == 2 &&
1778 p_soun->i_qt_description > 20 + 28 )
1785 static const struct {
1789 vlc_fourcc_t i_codec;
1791 { 0x01, 0x03, 32, VLC_CODEC_F32L },
1792 { 0x01, 0x03, 64, VLC_CODEC_F64L },
1793 { 0x01|0x02, 0x03, 32, VLC_CODEC_F32B },
1794 { 0x01|0x02, 0x03, 64, VLC_CODEC_F64B },
1796 { 0x00, 0x05, 8, VLC_CODEC_U8 },
1797 { 0x00| 0x04, 0x05, 8, VLC_CODEC_S8 },
1799 { 0x00, 0x07, 16, VLC_CODEC_U16L },
1800 { 0x00|0x02, 0x07, 16, VLC_CODEC_U16B },
1801 { 0x00 |0x04, 0x07, 16, VLC_CODEC_S16L },
1802 { 0x00|0x02|0x04, 0x07, 16, VLC_CODEC_S16B },
1804 { 0x00, 0x07, 24, VLC_CODEC_U24L },
1805 { 0x00|0x02, 0x07, 24, VLC_CODEC_U24B },
1806 { 0x00 |0x04, 0x07, 24, VLC_CODEC_S24L },
1807 { 0x00|0x02|0x04, 0x07, 24, VLC_CODEC_S24B },
1809 { 0x00, 0x07, 32, VLC_CODEC_U32L },
1810 { 0x00|0x02, 0x07, 32, VLC_CODEC_U32B },
1811 { 0x00 |0x04, 0x07, 32, VLC_CODEC_S32L },
1812 { 0x00|0x02|0x04, 0x07, 32, VLC_CODEC_S32B },
1816 uint32_t i_bits = GetDWBE(&p_soun->p_qt_description[20 + 20]);
1817 uint32_t i_flags = GetDWBE(&p_soun->p_qt_description[20 + 24]);
1819 for( int i = 0; p_formats[i].i_codec; i++ )
1821 if( p_formats[i].i_bits == i_bits &&
1822 (i_flags & p_formats[i].i_mask) == p_formats[i].i_flags )
1824 p_track->fmt.i_codec = p_formats[i].i_codec;
1825 p_track->fmt.audio.i_bitspersample = i_bits;
1826 p_track->fmt.audio.i_blockalign = p_soun->i_channelcount * i_bits / 8;
1827 p_track->i_sample_size = p_track->fmt.audio.i_blockalign;
1829 p_soun->i_qt_version = 0;
1837 p_track->fmt.i_codec = p_sample->i_type;
1841 /* now see if esds is present and if so create a data packet
1842 with decoder_specific_info */
1843 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1844 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1845 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1846 ( p_esds->data.p_esds )&&
1849 /* First update information based on i_objectTypeIndication */
1850 switch( p_decconfig->i_objectTypeIndication )
1852 case( 0x20 ): /* MPEG4 VIDEO */
1853 p_track->fmt.i_codec = VLC_CODEC_MP4V;
1855 case( 0x21 ): /* H.264 */
1856 p_track->fmt.i_codec = VLC_CODEC_H264;
1859 p_track->fmt.i_codec = VLC_CODEC_MP4A;
1860 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
1861 p_decconfig->p_decoder_specific_info[0] == 0xF8 &&
1862 (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
1864 p_track->fmt.i_codec = VLC_CODEC_ALS;
1872 case( 0x65): /* MPEG2 video */
1873 p_track->fmt.i_codec = VLC_CODEC_MPGV;
1875 /* Theses are MPEG2-AAC */
1876 case( 0x66): /* main profile */
1877 case( 0x67): /* Low complexity profile */
1878 case( 0x68): /* Scaleable Sampling rate profile */
1879 p_track->fmt.i_codec = VLC_CODEC_MP4A;
1881 /* True MPEG 2 audio */
1883 p_track->fmt.i_codec = VLC_CODEC_MPGA;
1885 case( 0x6a): /* MPEG1 video */
1886 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1888 case( 0x6b): /* MPEG1 audio */
1889 p_track->fmt.i_codec = VLC_CODEC_MPGA;
1891 case( 0x6c ): /* jpeg */
1892 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1894 case( 0x6d ): /* png */
1895 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
1897 case( 0x6e ): /* jpeg2000 */
1898 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
1900 case( 0xa3 ): /* vc1 */
1901 p_track->fmt.i_codec = VLC_FOURCC( 'W','V','C','1' );
1904 p_track->fmt.i_codec = VLC_CODEC_DIRAC;
1907 p_track->fmt.i_codec = VLC_CODEC_A52;
1910 p_track->fmt.i_codec = VLC_CODEC_EAC3;
1912 case( 0xa9 ): /* dts */
1913 case( 0xaa ): /* DTS-HD HRA */
1914 case( 0xab ): /* DTS-HD Master Audio */
1915 p_track->fmt.i_codec = VLC_CODEC_DTS;
1918 p_track->fmt.i_codec = VLC_CODEC_VORBIS;
1922 case( 0xe0 ): /* NeroDigital: dvd subs */
1923 if( p_track->fmt.i_cat == SPU_ES )
1925 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1926 if( p_track->i_width > 0 )
1927 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1928 if( p_track->i_height > 0 )
1929 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1932 case( 0xe1 ): /* QCelp for 3gp */
1933 if( p_track->fmt.i_cat == AUDIO_ES )
1935 p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
1941 /* Unknown entry, but don't touch i_fourcc */
1943 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1944 p_decconfig->i_objectTypeIndication,
1945 p_track->i_track_ID );
1948 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1949 if( p_track->fmt.i_extra > 0 )
1951 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1952 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1953 p_track->fmt.i_extra );
1958 switch( p_sample->i_type )
1960 /* qt decoder, send the complete chunk */
1961 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1962 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1963 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1964 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1965 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1966 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1967 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1968 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1969 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1970 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1971 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1972 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1973 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1975 /* qt decoder, send the complete chunk */
1976 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1977 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1978 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1979 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1980 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1981 p_track->fmt.i_extra =
1982 p_sample->data.p_sample_vide->i_qt_image_description;
1983 if( p_track->fmt.i_extra > 0 )
1985 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1986 memcpy( p_track->fmt.p_extra,
1987 p_sample->data.p_sample_vide->p_qt_image_description,
1988 p_track->fmt.i_extra);
1992 case VLC_CODEC_AMR_NB:
1993 p_track->fmt.audio.i_rate = 8000;
1994 case VLC_CODEC_AMR_WB:
1995 p_track->fmt.audio.i_rate = 16000;
1996 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1997 case VLC_CODEC_QDM2:
1998 case VLC_CODEC_ALAC:
1999 p_track->fmt.i_extra =
2000 p_sample->data.p_sample_soun->i_qt_description;
2001 if( p_track->fmt.i_extra > 0 )
2003 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
2004 memcpy( p_track->fmt.p_extra,
2005 p_sample->data.p_sample_soun->p_qt_description,
2006 p_track->fmt.i_extra);
2008 if( p_track->fmt.i_extra >= 56 && p_sample->i_type == VLC_CODEC_ALAC )
2010 p_track->fmt.audio.i_channels = *((uint8_t*)p_track->fmt.p_extra + 41);
2011 p_track->fmt.audio.i_rate = GetDWBE((uint8_t*)p_track->fmt.p_extra + 52);
2015 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
2016 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
2018 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
2022 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
2023 if( p_track->fmt.i_extra > 0 )
2025 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
2026 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
2027 p_track->fmt.i_extra );
2032 msg_Err( p_demux, "missing avcC" );
2037 case VLC_CODEC_ADPCM_MS:
2038 case VLC_CODEC_ADPCM_IMA_WAV:
2039 case VLC_CODEC_QCELP:
2040 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
2051 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2056 /* given a time it return sample/chunk
2057 * it also update elst field of the track
2059 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2060 int64_t i_start, uint32_t *pi_chunk,
2061 uint32_t *pi_sample )
2063 demux_sys_t *p_sys = p_demux->p_sys;
2064 MP4_Box_t *p_box_stss;
2066 unsigned int i_sample;
2067 unsigned int i_chunk;
2070 /* FIXME see if it's needed to check p_track->i_chunk_count */
2071 if( p_track->i_chunk_count == 0 )
2072 return( VLC_EGENERIC );
2074 /* handle elst (find the correct one) */
2075 MP4_TrackSetELST( p_demux, p_track, i_start );
2076 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2078 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2079 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
2081 /* now calculate i_start for this elst */
2083 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
2091 /* to track time scale */
2092 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2093 /* add elst offset */
2094 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2095 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2096 elst->i_media_time[p_track->i_elst] > 0 )
2098 i_start += elst->i_media_time[p_track->i_elst];
2101 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2102 "ms (track)", p_track->i_elst,
2103 i_mvt * 1000 / p_sys->i_timescale,
2104 i_start * 1000 / p_track->i_timescale );
2108 /* convert absolute time to in timescale unit */
2109 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2112 /* we start from sample 0/chunk 0, hope it won't take too much time */
2113 /* *** find good chunk *** */
2114 for( i_chunk = 0; ; i_chunk++ )
2116 if( i_chunk + 1 >= p_track->i_chunk_count )
2118 /* at the end and can't check if i_start in this chunk,
2119 it will be check while searching i_sample */
2120 i_chunk = p_track->i_chunk_count - 1;
2124 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2125 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
2131 /* *** find sample in the chunk *** */
2132 i_sample = p_track->chunk[i_chunk].i_sample_first;
2133 i_dts = p_track->chunk[i_chunk].i_first_dts;
2134 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2137 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2138 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2141 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2142 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2144 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2149 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2153 i_sample += ( i_start - i_dts ) /
2154 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2159 if( i_sample >= p_track->i_sample_count )
2161 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2162 "(seeking too far) chunk=%d sample=%d",
2163 p_track->i_track_ID, i_chunk, i_sample );
2164 return( VLC_EGENERIC );
2168 /* *** Try to find nearest sync points *** */
2169 if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2171 MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2172 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2173 p_track->i_track_ID );
2174 for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2176 if( i_index >= p_stss->i_entry_count - 1 ||
2177 i_sample < p_stss->i_sample_number[i_index+1] )
2179 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2180 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
2181 i_sample, i_sync_sample );
2183 if( i_sync_sample <= i_sample )
2185 while( i_chunk > 0 &&
2186 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2191 while( i_chunk < p_track->i_chunk_count - 1 &&
2192 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2193 p_track->chunk[i_chunk].i_sample_count )
2196 i_sample = i_sync_sample;
2203 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2204 "Sample Box (stss)", p_track->i_track_ID );
2207 *pi_chunk = i_chunk;
2208 *pi_sample = i_sample;
2213 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2214 unsigned int i_chunk, unsigned int i_sample )
2216 bool b_reselect = false;
2218 /* now see if actual es is ok */
2219 if( p_track->i_chunk >= p_track->i_chunk_count ||
2220 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2221 p_track->chunk[i_chunk].i_sample_description_index )
2223 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2224 p_track->i_track_ID );
2226 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2227 p_track->p_es, &b_reselect );
2229 es_out_Del( p_demux->out, p_track->p_es );
2231 p_track->p_es = NULL;
2233 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2235 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2236 p_track->i_track_ID );
2238 p_track->b_ok = false;
2239 p_track->b_selected = false;
2240 return VLC_EGENERIC;
2244 /* select again the new decoder */
2247 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2250 p_track->i_chunk = i_chunk;
2251 p_track->i_sample = i_sample;
2253 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2256 /****************************************************************************
2258 ****************************************************************************
2259 * Parse track information and create all needed data to run a track
2260 * If it succeed b_ok is set to 1 else to 0
2261 ****************************************************************************/
2262 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2263 MP4_Box_t *p_box_trak,
2264 bool b_force_enable )
2266 demux_sys_t *p_sys = p_demux->p_sys;
2268 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2269 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2282 /* hint track unsupported */
2284 /* set default value (-> track unusable) */
2285 p_track->b_ok = false;
2286 p_track->b_enable = false;
2287 p_track->b_selected = false;
2288 p_track->b_chapter = false;
2289 p_track->b_mac_encoding = false;
2291 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2298 /* do we launch this track by default ? */
2300 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2301 if( !p_track->b_enable )
2302 p_track->fmt.i_priority = -1;
2304 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2305 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2306 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2310 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2313 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2314 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2316 if( ( !p_mdhd )||( !p_hdlr ) )
2321 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2322 if( !p_track->i_timescale )
2325 if( p_mdhd->data.p_mdhd->i_language_code < 0x800 )
2327 /* We can convert i_language_code into iso 639 code,
2329 strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2330 p_track->b_mac_encoding = true;
2334 for( i = 0; i < 3; i++ )
2335 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2339 switch( p_hdlr->data.p_hdlr->i_handler_type )
2342 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2346 p_track->fmt.i_cat = AUDIO_ES;
2350 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2354 p_track->fmt.i_cat = VIDEO_ES;
2361 p_track->fmt.i_cat = SPU_ES;
2368 p_track->i_elst = 0;
2369 p_track->i_elst_time = 0;
2370 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2372 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2375 msg_Warn( p_demux, "elst box found" );
2376 for( i = 0; i < elst->i_entry_count; i++ )
2378 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2379 "ms) rate=%d.%d", i,
2380 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2381 elst->i_media_time[i] >= 0 ?
2382 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2384 elst->i_media_rate_integer[i],
2385 elst->i_media_rate_fraction[i] );
2392 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2394 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2395 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2401 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2403 p_track->fmt.psz_language = strdup( language );
2406 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2409 MP4_Box_t *p_box_iter;
2410 for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2411 p_box_iter = p_box_iter->p_next )
2413 switch( p_box_iter->i_type )
2416 p_track->fmt.psz_description =
2417 strdup( p_box_iter->data.p_0xa9xxx->psz_text );
2420 p_track->fmt.psz_description =
2421 strdup( p_box_iter->data.p_name->psz_text );
2427 /* Create chunk index table and sample index table */
2428 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2429 TrackCreateSamplesIndex( p_demux, p_track ) )
2431 return; /* cannot create chunks index */
2434 p_track->i_chunk = 0;
2435 p_track->i_sample = 0;
2437 /* Mark chapter only track */
2438 if( p_sys->p_tref_chap )
2440 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2443 for( i = 0; i < p_chap->i_entry_count; i++ )
2445 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2447 p_track->b_chapter = true;
2448 p_track->b_enable = false;
2455 if( b_force_enable &&
2456 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2458 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2459 p_track->i_track_ID );
2460 p_track->b_enable = true;
2461 p_track->fmt.i_priority = 0;
2464 p_track->p_es = NULL;
2465 if( TrackCreateES( p_demux,
2466 p_track, p_track->i_chunk,
2467 p_track->b_chapter ? NULL : &p_track->p_es ) )
2469 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2470 p_track->i_track_ID );
2473 p_track->b_ok = true;
2477 for( i = 0; i < p_track->i_chunk_count; i++ )
2479 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2480 i, p_track->chunk[i].i_sample_count,
2481 p_track->chunk[i].i_first_dts );
2488 /****************************************************************************
2490 ****************************************************************************
2491 * Destroy a track created by MP4_TrackCreate.
2492 ****************************************************************************/
2493 static void MP4_TrackDestroy( mp4_track_t *p_track )
2495 unsigned int i_chunk;
2497 p_track->b_ok = false;
2498 p_track->b_enable = false;
2499 p_track->b_selected = false;
2501 es_format_Clean( &p_track->fmt );
2503 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2505 if( p_track->chunk )
2507 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2508 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2510 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2511 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2514 FREENULL( p_track->chunk );
2516 if( !p_track->i_sample_size )
2518 FREENULL( p_track->p_sample_size );
2522 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2525 if( !p_track->b_ok || p_track->b_chapter )
2527 return VLC_EGENERIC;
2530 if( p_track->b_selected )
2532 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2533 p_track->i_track_ID );
2537 return MP4_TrackSeek( p_demux, p_track, i_start );
2540 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2542 if( !p_track->b_ok || p_track->b_chapter )
2547 if( !p_track->b_selected )
2549 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2550 p_track->i_track_ID );
2555 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2556 p_track->p_es, false );
2559 p_track->b_selected = false;
2562 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2568 if( !p_track->b_ok || p_track->b_chapter )
2569 return VLC_EGENERIC;
2571 p_track->b_selected = false;
2573 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2574 &i_chunk, &i_sample ) )
2576 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2577 p_track->i_track_ID );
2578 return VLC_EGENERIC;
2581 p_track->b_selected = true;
2583 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2584 p_track->b_selected = true;
2586 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2591 * 3 types: for audio
2594 #define QT_V0_MAX_SAMPLES 1024
2595 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2598 MP4_Box_data_sample_soun_t *p_soun;
2600 if( p_track->i_sample_size == 0 )
2602 /* most simple case */
2603 return p_track->p_sample_size[p_track->i_sample];
2605 if( p_track->fmt.i_cat != AUDIO_ES )
2607 return p_track->i_sample_size;
2610 p_soun = p_track->p_sample->data.p_sample_soun;
2612 if( p_soun->i_qt_version == 1 )
2614 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
2615 if( p_track->fmt.audio.i_blockalign > 1 )
2616 i_samples = p_soun->i_sample_per_packet;
2618 i_size = i_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2620 else if( p_track->i_sample_size > 256 )
2622 /* We do that so we don't read too much data
2623 * (in this case we are likely dealing with compressed data) */
2624 i_size = p_track->i_sample_size;
2628 /* Read a bunch of samples at once */
2629 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2630 ( p_track->i_sample -
2631 p_track->chunk[p_track->i_chunk].i_sample_first );
2633 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2634 i_size = i_samples * p_track->i_sample_size;
2637 //fprintf( stderr, "size=%d\n", i_size );
2641 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2643 unsigned int i_sample;
2646 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2648 if( p_track->i_sample_size )
2650 MP4_Box_data_sample_soun_t *p_soun =
2651 p_track->p_sample->data.p_sample_soun;
2653 if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
2655 i_pos += ( p_track->i_sample -
2656 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2657 p_track->i_sample_size;
2661 /* we read chunk by chunk unless a blockalign is requested */
2662 if( p_track->fmt.audio.i_blockalign > 1 )
2663 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
2664 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2669 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2670 i_sample < p_track->i_sample; i_sample++ )
2672 i_pos += p_track->p_sample_size[i_sample];
2679 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2681 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2683 MP4_Box_data_sample_soun_t *p_soun;
2685 p_soun = p_track->p_sample->data.p_sample_soun;
2687 if( p_soun->i_qt_version == 1 )
2689 /* we read chunk by chunk unless a blockalign is requested */
2690 if( p_track->fmt.audio.i_blockalign > 1 )
2691 p_track->i_sample += p_soun->i_sample_per_packet;
2693 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
2695 else if( p_track->i_sample_size > 256 )
2697 /* We do that so we don't read too much data
2698 * (in this case we are likely dealing with compressed data) */
2699 p_track->i_sample += 1;
2704 p_track->i_sample += QT_V0_MAX_SAMPLES;
2705 if( p_track->i_sample >
2706 p_track->chunk[p_track->i_chunk].i_sample_first +
2707 p_track->chunk[p_track->i_chunk].i_sample_count )
2710 p_track->chunk[p_track->i_chunk].i_sample_first +
2711 p_track->chunk[p_track->i_chunk].i_sample_count;
2717 p_track->i_sample++;
2720 if( p_track->i_sample >= p_track->i_sample_count )
2721 return VLC_EGENERIC;
2723 /* Have we changed chunk ? */
2724 if( p_track->i_sample >=
2725 p_track->chunk[p_track->i_chunk].i_sample_first +
2726 p_track->chunk[p_track->i_chunk].i_sample_count )
2728 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2729 p_track->i_sample ) )
2731 msg_Warn( p_demux, "track[0x%x] will be disabled "
2732 "(cannot restart decoder)", p_track->i_track_ID );
2733 MP4_TrackUnselect( p_demux, p_track );
2734 return VLC_EGENERIC;
2738 /* Have we changed elst */
2739 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2741 demux_sys_t *p_sys = p_demux->p_sys;
2742 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2743 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2744 p_sys->i_timescale / (int64_t)1000000;
2746 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2747 i_mvt >= p_track->i_elst_time +
2748 elst->i_segment_duration[p_track->i_elst] )
2750 MP4_TrackSetELST( p_demux, p_track,
2751 MP4_TrackGetDTS( p_demux, p_track ) );
2758 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2761 demux_sys_t *p_sys = p_demux->p_sys;
2762 int i_elst_last = tk->i_elst;
2764 /* handle elst (find the correct one) */
2766 tk->i_elst_time = 0;
2767 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2769 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2770 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2772 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2774 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2776 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2780 tk->i_elst_time += i_dur;
2783 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2785 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2786 tk->i_elst = elst->i_entry_count - 1;
2787 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2790 if( elst->i_media_time[tk->i_elst] < 0 )
2793 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2796 if( i_elst_last != tk->i_elst )
2798 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2803 static const char *MP4_ConvertMacCode( uint16_t i_code )
2805 static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
2806 { "en", 0 }, { "fr", 1 }, { "de", 2 }, { "it", 3 }, { "nl", 4 },
2807 { "sv", 5 }, { "es", 6 }, { "da", 7 }, { "pt", 8 }, { "no", 9 },
2808 { "he", 10 }, { "ja", 11 }, { "ar", 12 }, { "fi", 13 }, { "el", 14 },
2809 { "is", 15 }, { "mt", 16 }, { "tr", 17 }, { "hr", 18 }, { "zh", 19 },
2810 { "ur", 20 }, { "hi", 21 }, { "th", 22 }, { "ko", 23 }, { "lt", 24 },
2811 { "pl", 25 }, { "hu", 26 }, { "et", 27 }, { "lv", 28 }, //{ "??", 29 },
2812 { "fo", 30 }, { "fa", 31 }, { "ru", 32 }, { "zh", 33 }, { "nl", 34 },
2813 { "ga", 35 }, { "sq", 36 }, { "ro", 37 }, { "cs", 38 }, { "sk", 39 },
2814 { "sl", 40 }, { "yi", 41 }, { "sr", 42 }, { "mk", 43 }, { "bg", 44 },
2815 { "uk", 45 }, { "be", 46 }, { "uz", 47 }, { "az", 48 }, { "kk", 48 },
2816 { "az", 50 }, { "hy", 51 }, { "ka", 52 }, { "mo", 53 }, { "ky", 54 },
2817 { "tg", 55 }, { "tk", 56 }, { "mn", 57 }, { "mn", 58 }, { "ps", 59 },
2818 { "ku", 60 }, { "ks", 61 }, { "sd", 62 }, { "bo", 63 }, { "ne", 64 },
2819 { "sa", 65 }, { "mr", 66 }, { "bn", 67 }, { "as", 68 }, { "gu", 69 },
2820 { "pa", 70 }, { "or", 71 }, { "ml", 72 }, { "kn", 73 }, { "ta", 74 },
2821 { "te", 75 }, { "si", 76 }, { "my", 77 }, { "km", 78 }, { "lo", 79 },
2822 { "vi", 80 }, { "id", 81 }, { "tl", 82 }, { "ms", 83 }, { "ms", 84 },
2823 { "am", 85 }, { "ti", 86 }, { "om", 87 }, { "so", 88 }, { "sw", 89 },
2824 { "rw", 90 }, { "rn", 91 }, { "ny", 92 }, { "mg", 93 }, { "eo", 94 },
2826 { "cy", 128 }, { "eu", 129 },
2827 { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
2828 { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
2829 { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
2830 { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
2835 for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
2837 if( p_cvt[i].i_code == i_code )
2838 return p_cvt[i].psz_iso639_1;