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 );
67 MP4_Box_t *p_root; /* container for the whole file */
71 uint64_t i_time; /* time position of the presentation
72 * in movie timescale */
73 uint64_t i_timescale; /* movie time scale */
74 uint64_t i_duration; /* movie duration */
75 unsigned int i_tracks; /* number of tracks */
76 mp4_track_t *track; /* array of track */
77 float f_fps; /* number of frame per seconds */
80 MP4_Box_t *p_tref_chap;
83 input_title_t *p_title;
86 /*****************************************************************************
87 * Declaration of local function
88 *****************************************************************************/
89 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *, bool b_force_enable );
90 static void MP4_TrackDestroy( mp4_track_t * );
92 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
93 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
95 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
97 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
98 static int MP4_TrackSampleSize( mp4_track_t * );
99 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
100 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
102 static void MP4_UpdateSeekpoint( demux_t * );
103 static const char *MP4_ConvertMacCode( uint16_t );
105 /* Return time in s of a track */
106 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
108 #define chunk p_track->chunk[p_track->i_chunk]
110 unsigned int i_index = 0;
111 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
112 int64_t i_dts = chunk.i_first_dts;
114 while( i_sample > 0 )
116 if( i_sample > chunk.p_sample_count_dts[i_index] )
118 i_dts += chunk.p_sample_count_dts[i_index] *
119 chunk.p_sample_delta_dts[i_index];
120 i_sample -= chunk.p_sample_count_dts[i_index];
125 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
132 /* now handle elst */
133 if( p_track->p_elst )
135 demux_sys_t *p_sys = p_demux->p_sys;
136 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
138 /* convert to offset */
139 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
140 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
141 elst->i_media_time[p_track->i_elst] > 0 )
143 i_dts -= elst->i_media_time[p_track->i_elst];
146 /* add i_elst_time */
147 i_dts += p_track->i_elst_time * p_track->i_timescale /
150 if( i_dts < 0 ) i_dts = 0;
153 return INT64_C(1000000) * i_dts / p_track->i_timescale;
156 static inline int64_t MP4_TrackGetPTSDelta( mp4_track_t *p_track )
158 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
159 unsigned int i_index = 0;
160 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
162 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
165 for( i_index = 0;; i_index++ )
167 if( i_sample < ck->p_sample_count_pts[i_index] )
168 return ck->p_sample_offset_pts[i_index] * INT64_C(1000000) /
169 (int64_t)p_track->i_timescale;
171 i_sample -= ck->p_sample_count_pts[i_index];
175 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
177 return INT64_C(1000000) * p_sys->i_time / p_sys->i_timescale;
180 static void LoadChapter( demux_t *p_demux );
182 /*****************************************************************************
183 * Open: check file and initializes MP4 structures
184 *****************************************************************************/
185 static int Open( vlc_object_t * p_this )
187 demux_t *p_demux = (demux_t *)p_this;
190 const uint8_t *p_peek;
201 /* A little test to see if it could be a mp4 */
202 if( stream_Peek( p_demux->s, &p_peek, 11 ) < 11 ) return VLC_EGENERIC;
204 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
214 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
217 /* We don't yet support f4v, but avformat does. */
218 if( p_peek[8] == 'f' && p_peek[9] == '4' && p_peek[10] == 'v' )
226 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
229 msg_Warn( p_demux, "MP4 plugin discarded (not fastseekable)" );
233 /*Set exported functions */
234 p_demux->pf_demux = Demux;
235 p_demux->pf_control = Control;
237 /* create our structure that will contains all data */
238 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
240 /* Now load all boxes ( except raw data ) */
241 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
243 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
247 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
249 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
251 switch( p_ftyp->data.p_ftyp->i_major_brand )
255 "ISO Media file (isom) version %d.",
256 p_ftyp->data.p_ftyp->i_minor_version );
259 case( VLC_FOURCC( '3', 'g', 'p', '5' ) ):
260 case( VLC_FOURCC( '3', 'g', 'p', '6' ) ):
261 case( VLC_FOURCC( '3', 'g', 'p', '7' ) ):
262 msg_Dbg( p_demux, "3GPP Media file Release: %c",
263 #ifdef WORDS_BIGENDIAN
264 p_ftyp->data.p_ftyp->i_major_brand
266 p_ftyp->data.p_ftyp->i_major_brand >> 24
270 case( VLC_FOURCC( 'q', 't', ' ', ' ') ):
271 msg_Dbg( p_demux, "Apple QuickTime file" );
273 case( VLC_FOURCC( 'i', 's', 'm', 'l') ):
274 msg_Dbg( p_demux, "PIFF (= isml = fMP4) file" );
278 "unrecognized major file specification (%4.4s).",
279 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
285 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
288 /* the file need to have one moov box */
289 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
291 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
295 /* search also for moof box used by smoothstreaming */
296 p_foov = MP4_BoxGet( p_sys->p_root, "/moof" );
299 msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
303 /* we have a free box as a moov, rename it */
304 p_foov->i_type = ATOM_moov;
307 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
309 int i_count = MP4_BoxCount( p_rmra, "rmda" );
312 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
314 input_thread_t *p_input = demux_GetParentInput( p_demux );
315 input_item_t *p_current = input_GetItem( p_input );
317 input_item_node_t *p_subitems = input_item_node_Create( p_current );
319 for( i = 0; i < i_count; i++ )
321 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
325 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
329 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
331 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
332 psz_ref, (char*)&i_ref_type );
334 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
336 if( strstr( psz_ref, "qt5gateQT" ) )
338 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
341 if( !strncmp( psz_ref, "http://", 7 ) ||
342 !strncmp( psz_ref, "rtsp://", 7 ) )
349 char *psz_path = strdup( p_demux->psz_location );
350 char *end = strrchr( psz_path, '/' );
351 if( end ) end[1] = '\0';
352 else *psz_path = '\0';
354 if( asprintf( &psz_absolute, "%s://%s%s",
355 p_demux->psz_access, psz_path, psz_ref ) < 0 )
359 vlc_object_release( p_input) ;
364 psz_ref = psz_absolute;
367 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
368 input_item_t *p_item = input_item_New( psz_ref, NULL );
369 input_item_CopyOptions( p_current, p_item );
370 input_item_node_AppendItem( p_subitems, p_item );
371 vlc_gc_decref( p_item );
375 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
376 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
380 input_item_node_PostAndDelete( p_subitems );
381 vlc_object_release( p_input );
384 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
388 msg_Err( p_demux, "cannot find /moov/mvhd" );
393 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
394 p_demux->pf_demux = DemuxRef;
400 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
401 if( p_sys->i_timescale == 0 )
403 msg_Err( p_this, "bad timescale" );
406 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
409 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
411 msg_Err( p_demux, "cannot find any /moov/trak" );
414 msg_Dbg( p_demux, "found %d track%c",
416 p_sys->i_tracks ? 's':' ' );
418 /* allocate memory */
419 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
420 if( p_sys->track == NULL )
423 /* Search the first chap reference (like quicktime) and
424 * check that at least 1 stream is enabled */
425 p_sys->p_tref_chap = NULL;
426 b_enabled_es = false;
427 for( i = 0; i < p_sys->i_tracks; i++ )
429 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
432 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
433 if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
436 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
437 if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
438 p_sys->p_tref_chap = p_chap;
441 /* now process each track and extract all useful information */
442 for( i = 0; i < p_sys->i_tracks; i++ )
444 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
445 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
447 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
450 switch( p_sys->track[i].fmt.i_cat )
459 psz_cat = "subtitle";
467 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
468 p_sys->track[i].i_track_ID, psz_cat,
469 p_sys->track[i].b_enable ? "enable":"disable",
470 p_sys->track[i].fmt.psz_language ?
471 p_sys->track[i].fmt.psz_language : "undef" );
473 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
475 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
476 p_sys->track[i].i_track_ID,
477 p_sys->track[i].fmt.psz_language ?
478 p_sys->track[i].fmt.psz_language : "undef" );
482 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
483 p_sys->track[i].i_track_ID );
488 LoadChapter( p_demux );
495 MP4_BoxFree( p_demux->s, p_sys->p_root );
501 /*****************************************************************************
502 * Demux: read packet and send them to decoders
503 *****************************************************************************
504 * TODO check for newly selected track (ie audio upt to now )
505 *****************************************************************************/
506 static int Demux( demux_t *p_demux )
508 demux_sys_t *p_sys = p_demux->p_sys;
509 unsigned int i_track;
512 unsigned int i_track_selected;
514 /* check for newly selected/unselected track */
515 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
518 mp4_track_t *tk = &p_sys->track[i_track];
521 if( !tk->b_ok || tk->b_chapter ||
522 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
527 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
529 if( tk->b_selected && !b )
531 MP4_TrackUnselect( p_demux, tk );
533 else if( !tk->b_selected && b)
535 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
544 if( i_track_selected <= 0 )
546 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
547 if( p_sys->i_timescale > 0 )
549 int64_t i_length = (mtime_t)1000000 *
550 (mtime_t)p_sys->i_duration /
551 (mtime_t)p_sys->i_timescale;
552 if( MP4_GetMoviePTS( p_sys ) >= i_length )
557 msg_Warn( p_demux, "no track selected, exiting..." );
562 MP4_UpdateSeekpoint( p_demux );
564 /* first wait for the good time to read a packet */
565 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
567 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
569 /* we will read 100ms for each stream so ...*/
570 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
572 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
574 mp4_track_t *tk = &p_sys->track[i_track];
576 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
579 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
582 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
583 MP4_TrackGetDTS( p_demux, tk ),
584 MP4_GetMoviePTS( p_sys ) );
587 if( MP4_TrackSampleSize( tk ) > 0 )
593 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
595 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
597 MP4_TrackUnselect( p_demux, tk );
603 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
605 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
607 MP4_TrackUnselect( p_demux, tk );
611 else if( tk->fmt.i_cat == SPU_ES )
613 if( tk->fmt.i_codec == VLC_CODEC_SUBT &&
614 p_block->i_buffer >= 2 )
616 size_t i_size = GetWBE( p_block->p_buffer );
618 if( i_size + 2 <= p_block->i_buffer )
621 /* remove the length field, and append a '\0' */
622 memmove( &p_block->p_buffer[0],
623 &p_block->p_buffer[2], i_size );
624 p_block->p_buffer[i_size] = '\0';
625 p_block->i_buffer = i_size + 1;
627 /* convert \r -> \n */
628 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
636 p_block->i_buffer = 0;
641 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
643 i_delta = MP4_TrackGetPTSDelta( tk );
645 p_block->i_pts = p_block->i_dts + i_delta;
646 else if( tk->fmt.i_cat != VIDEO_ES )
647 p_block->i_pts = p_block->i_dts;
649 p_block->i_pts = VLC_TS_INVALID;
651 es_out_Send( p_demux->out, tk->p_es, p_block );
655 if( MP4_TrackNextSample( p_demux, tk ) )
663 static void MP4_UpdateSeekpoint( demux_t *p_demux )
665 demux_sys_t *p_sys = p_demux->p_sys;
668 if( !p_sys->p_title )
670 i_time = MP4_GetMoviePTS( p_sys );
671 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
673 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
678 if( i != p_demux->info.i_seekpoint && i >= 0 )
680 p_demux->info.i_seekpoint = i;
681 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
684 /*****************************************************************************
686 ******************************************************************************/
687 static int Seek( demux_t *p_demux, mtime_t i_date )
689 demux_sys_t *p_sys = p_demux->p_sys;
690 unsigned int i_track;
692 /* First update global time */
693 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
694 p_sys->i_pcr = i_date;
696 /* Now for each stream try to go to this time */
697 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
699 mp4_track_t *tk = &p_sys->track[i_track];
700 MP4_TrackSeek( p_demux, tk, i_date );
702 MP4_UpdateSeekpoint( p_demux );
704 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
709 /*****************************************************************************
711 *****************************************************************************/
712 static int Control( demux_t *p_demux, int i_query, va_list args )
714 demux_sys_t *p_sys = p_demux->p_sys;
721 case DEMUX_GET_POSITION:
722 pf = (double*)va_arg( args, double * );
723 if( p_sys->i_duration > 0 )
725 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
733 case DEMUX_SET_POSITION:
734 f = (double)va_arg( args, double );
735 if( p_sys->i_timescale > 0 )
737 i64 = (int64_t)( f * (double)1000000 *
738 (double)p_sys->i_duration /
739 (double)p_sys->i_timescale );
740 return Seek( p_demux, i64 );
742 else return VLC_SUCCESS;
745 pi64 = (int64_t*)va_arg( args, int64_t * );
746 if( p_sys->i_timescale > 0 )
748 *pi64 = (mtime_t)1000000 *
749 (mtime_t)p_sys->i_time /
750 (mtime_t)p_sys->i_timescale;
756 i64 = (int64_t)va_arg( args, int64_t );
757 return Seek( p_demux, i64 );
759 case DEMUX_GET_LENGTH:
760 pi64 = (int64_t*)va_arg( args, int64_t * );
761 if( p_sys->i_timescale > 0 )
763 *pi64 = (mtime_t)1000000 *
764 (mtime_t)p_sys->i_duration /
765 (mtime_t)p_sys->i_timescale;
771 pf = (double*)va_arg( args, double* );
777 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
778 MP4_Box_t *p_0xa9xxx;
780 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
783 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
790 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
791 p_0xa9xxx = p_0xa9xxx->p_next )
794 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
797 /* FIXME FIXME: should convert from whatever the character
798 * encoding of MP4 meta data is to UTF-8. */
799 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
800 if( psz_utf ) { EnsureUTF8( psz_utf ); \
801 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
803 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
804 switch( p_0xa9xxx->i_type )
806 case ATOM_0xa9nam: /* Full name */
807 SET( vlc_meta_SetTitle );
810 SET( vlc_meta_SetArtist );
813 SET( vlc_meta_SetArtist );
816 SET( vlc_meta_SetCopyright );
818 case ATOM_0xa9day: /* Creation Date */
819 SET( vlc_meta_SetDate );
821 case ATOM_0xa9des: /* Description */
822 SET( vlc_meta_SetDescription );
824 case ATOM_0xa9gen: /* Genre */
825 SET( vlc_meta_SetGenre );
829 if( p_0xa9xxx->data.p_gnre->i_genre <= NUM_GENRES )
830 vlc_meta_SetGenre( p_meta, ppsz_genres[p_0xa9xxx->data.p_gnre->i_genre - 1] );
833 case ATOM_0xa9alb: /* Album */
834 SET( vlc_meta_SetAlbum );
837 case ATOM_0xa9trk: /* Track */
838 SET( vlc_meta_SetTrackNum );
843 snprintf( psz_trck, sizeof( psz_trck ), "%i",
844 p_0xa9xxx->data.p_trkn->i_track_number );
845 vlc_meta_SetTrackNum( p_meta, psz_trck );
846 snprintf( psz_trck, sizeof( psz_trck ), "%i",
847 p_0xa9xxx->data.p_trkn->i_track_total );
848 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, psz_trck );
851 case ATOM_0xa9cmt: /* Commment */
852 SET( vlc_meta_SetDescription );
855 case ATOM_0xa9url: /* URL */
856 SET( vlc_meta_SetURL );
859 case ATOM_0xa9too: /* Encoder Tool */
860 case ATOM_0xa9enc: /* Encoded By */
861 SET( vlc_meta_SetEncodedBy );
865 SET( vlc_meta_SetPublisher );
872 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
874 { ATOM_0xa9wrt, N_("Writer") },
875 { ATOM_0xa9com, N_("Composer") },
876 { ATOM_0xa9prd, N_("Producer") },
877 { ATOM_0xa9inf, N_("Information") },
878 { ATOM_0xa9dir, N_("Director") },
879 { ATOM_0xa9dis, N_("Disclaimer") },
880 { ATOM_0xa9req, N_("Requirements") },
881 { ATOM_0xa9fmt, N_("Original Format") },
882 { ATOM_0xa9dsa, N_("Display Source As") },
883 { ATOM_0xa9hst, N_("Host Computer") },
884 { ATOM_0xa9prf, N_("Performers") },
885 { ATOM_0xa9ope, N_("Original Performer") },
886 { ATOM_0xa9src, N_("Providers Source Content") },
887 { ATOM_0xa9wrn, N_("Warning") },
888 { ATOM_0xa9swr, N_("Software") },
889 { ATOM_0xa9lyr, N_("Lyrics") },
890 { ATOM_0xa9mak, N_("Record Company") },
891 { ATOM_0xa9mod, N_("Model") },
892 { ATOM_0xa9PRD, N_("Product") },
893 { ATOM_0xa9grp, N_("Grouping") },
894 { ATOM_0xa9gen, N_("Genre") },
895 { ATOM_0xa9st3, N_("Sub-Title") },
896 { ATOM_0xa9arg, N_("Arranger") },
897 { ATOM_0xa9ard, N_("Art Director") },
898 { ATOM_0xa9cak, N_("Copyright Acknowledgement") },
899 { ATOM_0xa9con, N_("Conductor") },
900 { ATOM_0xa9des, N_("Song Description") },
901 { ATOM_0xa9lnt, N_("Liner Notes") },
902 { ATOM_0xa9phg, N_("Phonogram Rights") },
903 { ATOM_0xa9pub, N_("Publisher") },
904 { ATOM_0xa9sne, N_("Sound Engineer") },
905 { ATOM_0xa9sol, N_("Soloist") },
906 { ATOM_0xa9thx, N_("Thanks") },
907 { ATOM_0xa9xpd, N_("Executive Producer") },
910 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
912 if( p_0xa9xxx->i_type == xa9typetoextrameta[i].xa9_type )
914 char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" );
917 EnsureUTF8( psz_utf );
918 vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
928 case DEMUX_GET_TITLE_INFO:
930 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
931 int *pi_int = (int*)va_arg( args, int* );
932 int *pi_title_offset = (int*)va_arg( args, int* );
933 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
935 if( !p_sys->p_title )
939 *ppp_title = malloc( sizeof( input_title_t**) );
940 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
941 *pi_title_offset = 0;
942 *pi_seekpoint_offset = 0;
945 case DEMUX_SET_TITLE:
947 const int i_title = (int)va_arg( args, int );
948 if( !p_sys->p_title || i_title != 0 )
952 case DEMUX_SET_SEEKPOINT:
954 const int i_seekpoint = (int)va_arg( args, int );
955 if( !p_sys->p_title )
957 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
960 case DEMUX_SET_NEXT_DEMUX_TIME:
961 case DEMUX_SET_GROUP:
962 case DEMUX_HAS_UNSUPPORTED_META:
963 case DEMUX_GET_ATTACHMENTS:
964 case DEMUX_GET_PTS_DELAY:
965 case DEMUX_CAN_RECORD:
969 msg_Warn( p_demux, "control query %u unimplemented", i_query );
974 /*****************************************************************************
975 * Close: frees unused data
976 *****************************************************************************/
977 static void Close ( vlc_object_t * p_this )
979 demux_t * p_demux = (demux_t *)p_this;
980 demux_sys_t *p_sys = p_demux->p_sys;
981 unsigned int i_track;
983 msg_Dbg( p_demux, "freeing all memory" );
985 MP4_BoxFree( p_demux->s, p_sys->p_root );
986 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
988 MP4_TrackDestroy( &p_sys->track[i_track] );
990 FREENULL( p_sys->track );
993 vlc_input_title_Delete( p_sys->p_title );
1000 /****************************************************************************
1001 * Local functions, specific to vlc
1002 ****************************************************************************/
1004 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1006 demux_sys_t *p_sys = p_demux->p_sys;
1009 p_sys->p_title = vlc_input_title_New();
1010 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1012 seekpoint_t *s = vlc_seekpoint_New();
1014 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1015 EnsureUTF8( s->psz_name );
1016 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1017 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1020 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1022 demux_sys_t *p_sys = p_demux->p_sys;
1024 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1026 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1027 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( tk );
1028 const unsigned int i_size = MP4_TrackSampleSize( tk );
1030 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1033 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1034 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1038 seekpoint_t *s = vlc_seekpoint_New();
1040 s->psz_name = strndup( &p_buffer[2], i_len );
1041 EnsureUTF8( s->psz_name );
1043 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1045 if( !p_sys->p_title )
1046 p_sys->p_title = vlc_input_title_New();
1047 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1050 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1051 tk->chunk[tk->i_chunk].i_sample_count )
1055 static void LoadChapter( demux_t *p_demux )
1057 demux_sys_t *p_sys = p_demux->p_sys;
1060 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1062 LoadChapterGpac( p_demux, p_chpl );
1064 else if( p_sys->p_tref_chap )
1066 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1069 /* Load the first subtitle track like quicktime */
1070 for( i = 0; i < p_chap->i_entry_count; i++ )
1072 for( j = 0; j < p_sys->i_tracks; j++ )
1074 mp4_track_t *tk = &p_sys->track[j];
1075 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1076 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_CODEC_SUBT )
1079 if( j < p_sys->i_tracks )
1081 LoadChapterApple( p_demux, &p_sys->track[j] );
1087 /* Add duration if titles are enabled */
1088 if( p_sys->p_title )
1090 p_sys->p_title->i_length = (uint64_t)1000000 *
1091 (uint64_t)p_sys->i_duration / (uint64_t)p_sys->i_timescale;
1095 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1096 static int TrackCreateChunksIndex( demux_t *p_demux,
1097 mp4_track_t *p_demux_track )
1099 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1102 unsigned int i_chunk;
1103 unsigned int i_index, i_last;
1105 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1106 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1107 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1109 return( VLC_EGENERIC );
1112 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1113 if( !p_demux_track->i_chunk_count )
1115 msg_Warn( p_demux, "no chunk defined" );
1116 return( VLC_EGENERIC );
1118 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1119 sizeof( mp4_chunk_t ) );
1120 if( p_demux_track->chunk == NULL )
1125 /* first we read chunk offset */
1126 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1128 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1130 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1132 ck->i_first_dts = 0;
1133 ck->p_sample_count_dts = NULL;
1134 ck->p_sample_delta_dts = NULL;
1135 ck->p_sample_count_pts = NULL;
1136 ck->p_sample_offset_pts = NULL;
1139 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1140 to be used for the sample XXX begin to 1
1141 We construct it begining at the end */
1142 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1143 i_index = p_stsc->data.p_stsc->i_entry_count;
1146 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1147 return( VLC_EGENERIC );
1152 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1153 i_chunk < i_last; i_chunk++ )
1155 if( i_chunk >= p_demux_track->i_chunk_count )
1157 msg_Warn( p_demux, "corrupted chunk table" );
1158 return VLC_EGENERIC;
1161 p_demux_track->chunk[i_chunk].i_sample_description_index =
1162 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1163 p_demux_track->chunk[i_chunk].i_sample_count =
1164 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1166 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1169 p_demux_track->chunk[0].i_sample_first = 0;
1170 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1172 p_demux_track->chunk[i_chunk].i_sample_first =
1173 p_demux_track->chunk[i_chunk-1].i_sample_first +
1174 p_demux_track->chunk[i_chunk-1].i_sample_count;
1177 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1178 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1183 static int TrackCreateSamplesIndex( demux_t *p_demux,
1184 mp4_track_t *p_demux_track )
1187 MP4_Box_data_stsz_t *stsz;
1188 MP4_Box_data_stts_t *stts;
1189 /* TODO use also stss and stsh table for seeking */
1190 /* FIXME use edit table */
1195 int64_t i_index_sample_used;
1200 * Gives the sample size for each samples. There is also a stz2 table
1201 * (compressed form) that we need to implement TODO */
1202 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1205 /* FIXME and stz2 */
1206 msg_Warn( p_demux, "cannot find STSZ box" );
1207 return VLC_EGENERIC;
1209 stsz = p_box->data.p_stsz;
1212 * Gives mapping between sample and decoding time
1214 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1217 msg_Warn( p_demux, "cannot find STTS box" );
1218 return VLC_EGENERIC;
1220 stts = p_box->data.p_stts;
1222 /* Use stsz table to create a sample number -> sample size table */
1223 p_demux_track->i_sample_count = stsz->i_sample_count;
1224 if( stsz->i_sample_size )
1226 /* 1: all sample have the same size, so no need to construct a table */
1227 p_demux_track->i_sample_size = stsz->i_sample_size;
1228 p_demux_track->p_sample_size = NULL;
1232 /* 2: each sample can have a different size */
1233 p_demux_track->i_sample_size = 0;
1234 p_demux_track->p_sample_size =
1235 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1236 if( p_demux_track->p_sample_size == NULL )
1239 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1241 p_demux_track->p_sample_size[i_sample] =
1242 stsz->i_entry_size[i_sample];
1246 /* Use stts table to create a sample number -> dts table.
1247 * XXX: if we don't want to waste too much memory, we can't expand
1248 * the box! so each chunk will contain an "extract" of this table
1249 * for fast research (problem with raw stream where a sample is sometime
1250 * just channels*bits_per_sample/8 */
1253 i_index = 0; i_index_sample_used = 0;
1254 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1256 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1257 int64_t i_entry, i_sample_count, i;
1259 /* save first dts */
1260 ck->i_first_dts = i_next_dts;
1261 ck->i_last_dts = i_next_dts;
1263 /* count how many entries are needed for this chunk
1264 * for p_sample_delta_dts and p_sample_count_dts */
1265 i_sample_count = ck->i_sample_count;
1268 while( i_sample_count > 0 )
1270 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1271 /* don't count already used sample in this entry */
1273 i_sample_count += i_index_sample_used;
1279 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1280 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1282 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1286 i_sample_count = ck->i_sample_count;
1287 for( i = 0; i < i_entry; i++ )
1292 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1294 i_used = __MIN( i_rest, i_sample_count );
1296 i_index_sample_used += i_used;
1297 i_sample_count -= i_used;
1298 i_next_dts += i_used * stts->i_sample_delta[i_index];
1300 ck->p_sample_count_dts[i] = i_used;
1301 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1303 ck->i_last_dts = i_next_dts - ck->p_sample_delta_dts[i];
1305 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1308 i_index_sample_used = 0;
1314 * Gives the delta between decoding time (dts) and composition table (pts)
1316 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1319 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1321 msg_Warn( p_demux, "CTTS table" );
1323 /* Create pts-dts table per chunk */
1324 i_index = 0; i_index_sample_used = 0;
1325 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1327 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1328 int64_t i_entry, i_sample_count, i;
1330 /* count how many entries are needed for this chunk
1331 * for p_sample_delta_dts and p_sample_count_dts */
1332 i_sample_count = ck->i_sample_count;
1335 while( i_sample_count > 0 )
1337 i_sample_count -= ctts->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_pts = calloc( i_entry, sizeof( uint32_t ) );
1348 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1349 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1353 i_sample_count = ck->i_sample_count;
1354 for( i = 0; i < i_entry; i++ )
1359 i_rest = ctts->i_sample_count[i_index] -
1360 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;
1367 ck->p_sample_count_pts[i] = i_used;
1368 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1370 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1373 i_index_sample_used = 0;
1379 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1380 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1381 i_next_dts / p_demux_track->i_timescale );
1387 * It computes the sample rate for a video track using the given sample
1390 static void TrackGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
1391 const mp4_track_t *p_track,
1392 unsigned i_sd_index,
1398 if( p_track->i_chunk_count <= 0 )
1402 const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
1403 while( p_chunk > &p_track->chunk[0] &&
1404 p_chunk[-1].i_sample_description_index == i_sd_index )
1409 uint64_t i_sample = 0;
1410 uint64_t i_first_dts = p_chunk->i_first_dts;
1411 uint64_t i_last_dts;
1414 i_sample += p_chunk->i_sample_count;
1415 i_last_dts = p_chunk->i_last_dts;
1418 while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
1419 p_chunk->i_sample_description_index == i_sd_index );
1421 if( i_sample > 1 && i_first_dts < i_last_dts )
1422 vlc_ureduce( pi_num, pi_den,
1423 ( i_sample - 1) * p_track->i_timescale,
1424 i_last_dts - i_first_dts,
1430 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1432 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1433 unsigned int i_chunk, es_out_id_t **pp_es )
1435 const unsigned i_sample_description_index =
1436 p_track->chunk[i_chunk].i_sample_description_index;
1437 MP4_Box_t *p_sample;
1446 if( !i_sample_description_index )
1448 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1449 p_track->i_track_ID );
1450 return VLC_EGENERIC;
1453 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1454 i_sample_description_index - 1 );
1457 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1459 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1460 p_track->i_track_ID );
1461 return VLC_EGENERIC;
1464 p_track->p_sample = p_sample;
1466 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
1468 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
1470 p_sample->i_type = p_frma->data.p_frma->i_type;
1473 p_enda = MP4_BoxGet( p_sample, "wave/enda" );
1475 p_enda = MP4_BoxGet( p_sample, "enda" );
1477 p_pasp = MP4_BoxGet( p_sample, "pasp" );
1480 switch( p_track->fmt.i_cat )
1483 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1484 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1485 p_track->fmt.video.i_bits_per_pixel =
1486 p_sample->data.p_sample_vide->i_depth;
1488 /* fall on display size */
1489 if( p_track->fmt.video.i_width <= 0 )
1490 p_track->fmt.video.i_width = p_track->i_width;
1491 if( p_track->fmt.video.i_height <= 0 )
1492 p_track->fmt.video.i_height = p_track->i_height;
1494 /* Find out apect ratio from display size */
1495 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1496 /* Work-around buggy muxed files */
1497 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1499 p_track->fmt.video.i_sar_num = p_track->i_width * p_track->fmt.video.i_height;
1500 p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
1502 if( p_pasp && p_pasp->data.p_pasp->i_horizontal_spacing > 0 &&
1503 p_pasp->data.p_pasp->i_vertical_spacing > 0 )
1505 p_track->fmt.video.i_sar_num = p_pasp->data.p_pasp->i_horizontal_spacing;
1506 p_track->fmt.video.i_sar_den = p_pasp->data.p_pasp->i_vertical_spacing;
1509 /* Support for cropping (eg. in H263 files) */
1510 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1511 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1514 TrackGetESSampleRate( &p_track->fmt.video.i_frame_rate,
1515 &p_track->fmt.video.i_frame_rate_base,
1516 p_track, i_sample_description_index, i_chunk );
1517 p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
1518 (float)p_track->fmt.video.i_frame_rate_base;
1521 switch( (int)p_track->f_rotation ) {
1523 p_track->fmt.video.orientation = ORIENT_ROTATED_90;
1526 p_track->fmt.video.orientation = ORIENT_ROTATED_180;
1529 p_track->fmt.video.orientation = ORIENT_ROTATED_270;
1536 p_track->fmt.audio.i_channels =
1537 p_sample->data.p_sample_soun->i_channelcount;
1538 p_track->fmt.audio.i_rate =
1539 p_sample->data.p_sample_soun->i_sampleratehi;
1540 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1541 p_sample->data.p_sample_soun->i_sampleratehi *
1542 p_sample->data.p_sample_soun->i_samplesize;
1543 p_track->fmt.audio.i_bitspersample =
1544 p_sample->data.p_sample_soun->i_samplesize;
1546 if( ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1548 MP4_Box_data_sample_soun_t *p_soun;
1549 p_soun = p_sample->data.p_sample_soun;
1551 if( p_soun->i_qt_version == 0 )
1553 switch( p_sample->i_type )
1555 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1556 p_soun->i_qt_version = 1;
1557 p_soun->i_sample_per_packet = 64;
1558 p_soun->i_bytes_per_packet = 34;
1559 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1560 p_soun->i_bytes_per_sample = 2;
1562 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1563 p_soun->i_qt_version = 1;
1564 p_soun->i_sample_per_packet = 6;
1565 p_soun->i_bytes_per_packet = 2;
1566 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1567 p_soun->i_bytes_per_sample = 2;
1569 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1570 p_soun->i_qt_version = 1;
1571 p_soun->i_sample_per_packet = 12;
1572 p_soun->i_bytes_per_packet = 2;
1573 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1574 p_soun->i_bytes_per_sample = 2;
1576 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1577 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1578 p_soun->i_samplesize = 8;
1579 p_track->i_sample_size = p_soun->i_channelcount;
1581 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1582 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1583 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1584 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1585 /* What would be the fun if you could trust the .mov */
1586 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1593 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1595 p_soun->i_qt_version = 0;
1598 else if( p_sample->data.p_sample_soun->i_qt_version == 1 )
1600 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1602 switch( p_sample->i_type )
1604 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1605 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1607 if( p_track->i_sample_size > 1 )
1608 p_soun->i_qt_version = 0;
1611 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1612 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1613 case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
1614 p_soun->i_qt_version = 0;
1621 if( p_track->i_sample_size != 0 &&
1622 p_sample->data.p_sample_soun->i_qt_version == 1 &&
1623 p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1625 msg_Err( p_demux, "Invalid sample per packet value for qt_version 1. Broken muxer!" );
1626 p_sample->data.p_sample_soun->i_qt_version = 0;
1635 /* It's a little ugly but .. there are special cases */
1636 switch( p_sample->i_type )
1638 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1639 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1641 p_track->fmt.i_codec = VLC_CODEC_MPGA;
1644 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1646 MP4_Box_t *p_dac3_box = MP4_BoxGet( p_sample, "dac3", 0 );
1648 p_track->fmt.i_codec = VLC_CODEC_A52;
1651 static const int pi_bitrate[] = {
1658 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
1659 p_track->fmt.audio.i_channels = 0;
1660 p_track->fmt.i_bitrate = 0;
1661 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
1662 p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
1663 p_track->fmt.audio.i_bitspersample = 0;
1667 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1669 p_track->fmt.i_codec = VLC_CODEC_EAC3;
1673 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1674 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1676 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1678 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1679 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1681 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1683 /* Buggy files workaround */
1684 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1685 p_sample->data.p_sample_soun->i_sampleratehi) )
1687 MP4_Box_data_sample_soun_t *p_soun =
1688 p_sample->data.p_sample_soun;
1690 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1691 "(%u), making both equal (report any problem).",
1692 p_track->i_timescale, p_soun->i_sampleratehi );
1694 if( p_soun->i_sampleratehi )
1695 p_track->i_timescale = p_soun->i_sampleratehi;
1697 p_soun->i_sampleratehi = p_track->i_timescale;
1702 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1703 p_track->fmt.i_codec = VLC_CODEC_H263;
1706 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1707 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1708 p_track->fmt.i_codec = VLC_CODEC_SUBT;
1709 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1710 /* FIXME UTF-8 doesn't work here ? */
1711 if( p_track->b_mac_encoding )
1712 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1714 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1717 case VLC_FOURCC('y','v','1','2'):
1718 p_track->fmt.i_codec = VLC_CODEC_YV12;
1720 case VLC_FOURCC('y','u','v','2'):
1721 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1724 case VLC_FOURCC('i','n','2','4'):
1725 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1726 VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
1728 case VLC_FOURCC('f','l','3','2'):
1729 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1730 VLC_CODEC_F32L : VLC_CODEC_F32B;
1732 case VLC_FOURCC('f','l','6','4'):
1733 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1734 VLC_CODEC_F64L : VLC_CODEC_F64B;
1736 case VLC_FOURCC( 'l', 'p', 'c', 'm' ):
1738 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1739 if( p_soun->i_qt_version == 2 &&
1740 p_soun->i_qt_description > 20 + 28 )
1747 static const struct {
1751 vlc_fourcc_t i_codec;
1753 { 0x01, 0x03, 32, VLC_CODEC_F32L },
1754 { 0x01, 0x03, 64, VLC_CODEC_F64L },
1755 { 0x01|0x02, 0x03, 32, VLC_CODEC_F32B },
1756 { 0x01|0x02, 0x03, 64, VLC_CODEC_F64B },
1758 { 0x00, 0x05, 8, VLC_CODEC_U8 },
1759 { 0x00| 0x04, 0x05, 8, VLC_CODEC_S8 },
1761 { 0x00, 0x07, 16, VLC_CODEC_U16L },
1762 { 0x00|0x02, 0x07, 16, VLC_CODEC_U16B },
1763 { 0x00 |0x04, 0x07, 16, VLC_CODEC_S16L },
1764 { 0x00|0x02|0x04, 0x07, 16, VLC_CODEC_S16B },
1766 { 0x00, 0x07, 24, VLC_CODEC_U24L },
1767 { 0x00|0x02, 0x07, 24, VLC_CODEC_U24B },
1768 { 0x00 |0x04, 0x07, 24, VLC_CODEC_S24L },
1769 { 0x00|0x02|0x04, 0x07, 24, VLC_CODEC_S24B },
1771 { 0x00, 0x07, 32, VLC_CODEC_U32L },
1772 { 0x00|0x02, 0x07, 32, VLC_CODEC_U32B },
1773 { 0x00 |0x04, 0x07, 32, VLC_CODEC_S32L },
1774 { 0x00|0x02|0x04, 0x07, 32, VLC_CODEC_S32B },
1778 uint32_t i_bits = GetDWBE(&p_soun->p_qt_description[20 + 20]);
1779 uint32_t i_flags = GetDWBE(&p_soun->p_qt_description[20 + 24]);
1781 for( int i = 0; p_formats[i].i_codec; i++ )
1783 if( p_formats[i].i_bits == i_bits &&
1784 (i_flags & p_formats[i].i_mask) == p_formats[i].i_flags )
1786 p_track->fmt.i_codec = p_formats[i].i_codec;
1787 p_track->fmt.audio.i_bitspersample = i_bits;
1788 p_track->fmt.audio.i_blockalign = p_soun->i_channelcount * i_bits / 8;
1789 p_track->i_sample_size = p_track->fmt.audio.i_blockalign;
1791 p_soun->i_qt_version = 0;
1799 p_track->fmt.i_codec = p_sample->i_type;
1803 /* now see if esds is present and if so create a data packet
1804 with decoder_specific_info */
1805 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1806 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1807 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1808 ( p_esds->data.p_esds )&&
1811 /* First update information based on i_objectTypeIndication */
1812 switch( p_decconfig->i_objectTypeIndication )
1814 case( 0x20 ): /* MPEG4 VIDEO */
1815 p_track->fmt.i_codec = VLC_CODEC_MP4V;
1817 case( 0x21 ): /* H.264 */
1818 p_track->fmt.i_codec = VLC_CODEC_H264;
1821 p_track->fmt.i_codec = VLC_CODEC_MP4A;
1822 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
1823 p_decconfig->p_decoder_specific_info[0] == 0xF8 &&
1824 (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
1826 p_track->fmt.i_codec = VLC_CODEC_ALS;
1834 case( 0x65): /* MPEG2 video */
1835 p_track->fmt.i_codec = VLC_CODEC_MPGV;
1837 /* Theses are MPEG2-AAC */
1838 case( 0x66): /* main profile */
1839 case( 0x67): /* Low complexity profile */
1840 case( 0x68): /* Scaleable Sampling rate profile */
1841 p_track->fmt.i_codec = VLC_CODEC_MP4A;
1843 /* True MPEG 2 audio */
1845 p_track->fmt.i_codec = VLC_CODEC_MPGA;
1847 case( 0x6a): /* MPEG1 video */
1848 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1850 case( 0x6b): /* MPEG1 audio */
1851 p_track->fmt.i_codec = VLC_CODEC_MPGA;
1853 case( 0x6c ): /* jpeg */
1854 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1856 case( 0x6d ): /* png */
1857 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
1859 case( 0x6e ): /* jpeg2000 */
1860 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
1862 case( 0xa3 ): /* vc1 */
1863 p_track->fmt.i_codec = VLC_CODEC_VC1;
1866 p_track->fmt.i_codec = VLC_CODEC_DIRAC;
1869 p_track->fmt.i_codec = VLC_CODEC_A52;
1872 p_track->fmt.i_codec = VLC_CODEC_EAC3;
1874 case( 0xa9 ): /* dts */
1875 case( 0xaa ): /* DTS-HD HRA */
1876 case( 0xab ): /* DTS-HD Master Audio */
1877 p_track->fmt.i_codec = VLC_CODEC_DTS;
1880 p_track->fmt.i_codec = VLC_CODEC_VORBIS;
1884 case( 0xe0 ): /* NeroDigital: dvd subs */
1885 if( p_track->fmt.i_cat == SPU_ES )
1887 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1888 if( p_track->i_width > 0 )
1889 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1890 if( p_track->i_height > 0 )
1891 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1894 case( 0xe1 ): /* QCelp for 3gp */
1895 if( p_track->fmt.i_cat == AUDIO_ES )
1897 p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
1903 /* Unknown entry, but don't touch i_fourcc */
1905 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1906 p_decconfig->i_objectTypeIndication,
1907 p_track->i_track_ID );
1910 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1911 if( p_track->fmt.i_extra > 0 )
1913 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1914 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1915 p_track->fmt.i_extra );
1920 switch( p_sample->i_type )
1922 /* qt decoder, send the complete chunk */
1923 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1924 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1925 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1926 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1927 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1928 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1929 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1930 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1931 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1932 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1933 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1934 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1935 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1937 /* qt decoder, send the complete chunk */
1938 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1939 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1940 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1941 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1942 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1943 p_track->fmt.i_extra =
1944 p_sample->data.p_sample_vide->i_qt_image_description;
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,
1949 p_sample->data.p_sample_vide->p_qt_image_description,
1950 p_track->fmt.i_extra);
1954 case VLC_CODEC_AMR_NB:
1955 p_track->fmt.audio.i_rate = 8000;
1957 case VLC_CODEC_AMR_WB:
1958 p_track->fmt.audio.i_rate = 16000;
1960 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1961 case VLC_CODEC_QDM2:
1962 case VLC_CODEC_ALAC:
1963 p_track->fmt.i_extra =
1964 p_sample->data.p_sample_soun->i_qt_description;
1965 if( p_track->fmt.i_extra > 0 )
1967 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1968 memcpy( p_track->fmt.p_extra,
1969 p_sample->data.p_sample_soun->p_qt_description,
1970 p_track->fmt.i_extra);
1972 if( p_track->fmt.i_extra >= 56 && p_sample->i_type == VLC_CODEC_ALAC )
1974 p_track->fmt.audio.i_channels = *((uint8_t*)p_track->fmt.p_extra + 41);
1975 p_track->fmt.audio.i_rate = GetDWBE((uint8_t*)p_track->fmt.p_extra + 52);
1979 case VLC_FOURCC( 'v', 'c', '-', '1' ):
1981 MP4_Box_t *p_dvc1 = MP4_BoxGet( p_sample, "dvc1" );
1984 p_track->fmt.i_extra = p_dvc1->data.p_dvc1->i_vc1;
1985 if( p_track->fmt.i_extra > 0 )
1987 p_track->fmt.p_extra = malloc( p_dvc1->data.p_dvc1->i_vc1 );
1988 memcpy( p_track->fmt.p_extra, p_dvc1->data.p_dvc1->p_vc1,
1989 p_track->fmt.i_extra );
1994 msg_Err( p_demux, "missing dvc1" );
1999 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
2000 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
2002 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
2006 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
2007 if( p_track->fmt.i_extra > 0 )
2009 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
2010 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
2011 p_track->fmt.i_extra );
2016 msg_Err( p_demux, "missing avcC" );
2021 case VLC_CODEC_ADPCM_MS:
2022 case VLC_CODEC_ADPCM_IMA_WAV:
2023 case VLC_CODEC_QCELP:
2024 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
2028 msg_Dbg( p_demux, "Unrecognized FourCC %4.4s", (char *)&p_sample->i_type );
2036 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
2041 /* given a time it return sample/chunk
2042 * it also update elst field of the track
2044 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
2045 int64_t i_start, uint32_t *pi_chunk,
2046 uint32_t *pi_sample )
2048 demux_sys_t *p_sys = p_demux->p_sys;
2049 MP4_Box_t *p_box_stss;
2051 unsigned int i_sample;
2052 unsigned int i_chunk;
2055 /* FIXME see if it's needed to check p_track->i_chunk_count */
2056 if( p_track->i_chunk_count == 0 )
2057 return( VLC_EGENERIC );
2059 /* handle elst (find the correct one) */
2060 MP4_TrackSetELST( p_demux, p_track, i_start );
2061 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2063 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2064 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
2066 /* now calculate i_start for this elst */
2068 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
2076 /* to track time scale */
2077 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2078 /* add elst offset */
2079 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
2080 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
2081 elst->i_media_time[p_track->i_elst] > 0 )
2083 i_start += elst->i_media_time[p_track->i_elst];
2086 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2087 "ms (track)", p_track->i_elst,
2088 i_mvt * 1000 / p_sys->i_timescale,
2089 i_start * 1000 / p_track->i_timescale );
2093 /* convert absolute time to in timescale unit */
2094 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2097 /* we start from sample 0/chunk 0, hope it won't take too much time */
2098 /* *** find good chunk *** */
2099 for( i_chunk = 0; ; i_chunk++ )
2101 if( i_chunk + 1 >= p_track->i_chunk_count )
2103 /* at the end and can't check if i_start in this chunk,
2104 it will be check while searching i_sample */
2105 i_chunk = p_track->i_chunk_count - 1;
2109 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2110 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
2116 /* *** find sample in the chunk *** */
2117 i_sample = p_track->chunk[i_chunk].i_sample_first;
2118 i_dts = p_track->chunk[i_chunk].i_first_dts;
2119 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2122 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2123 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2126 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2127 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2129 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2134 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2138 i_sample += ( i_start - i_dts ) /
2139 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2144 if( i_sample >= p_track->i_sample_count )
2146 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2147 "(seeking too far) chunk=%d sample=%d",
2148 p_track->i_track_ID, i_chunk, i_sample );
2149 return( VLC_EGENERIC );
2153 /* *** Try to find nearest sync points *** */
2154 if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2156 MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2157 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2158 p_track->i_track_ID );
2159 for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2161 if( i_index >= p_stss->i_entry_count - 1 ||
2162 i_sample < p_stss->i_sample_number[i_index+1] )
2164 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2165 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
2166 i_sample, i_sync_sample );
2168 if( i_sync_sample <= i_sample )
2170 while( i_chunk > 0 &&
2171 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2176 while( i_chunk < p_track->i_chunk_count - 1 &&
2177 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2178 p_track->chunk[i_chunk].i_sample_count )
2181 i_sample = i_sync_sample;
2188 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2189 "Sample Box (stss)", p_track->i_track_ID );
2192 *pi_chunk = i_chunk;
2193 *pi_sample = i_sample;
2198 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2199 unsigned int i_chunk, unsigned int i_sample )
2201 bool b_reselect = false;
2203 /* now see if actual es is ok */
2204 if( p_track->i_chunk >= p_track->i_chunk_count ||
2205 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2206 p_track->chunk[i_chunk].i_sample_description_index )
2208 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2209 p_track->i_track_ID );
2211 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2212 p_track->p_es, &b_reselect );
2214 es_out_Del( p_demux->out, p_track->p_es );
2216 p_track->p_es = NULL;
2218 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2220 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2221 p_track->i_track_ID );
2223 p_track->b_ok = false;
2224 p_track->b_selected = false;
2225 return VLC_EGENERIC;
2229 /* select again the new decoder */
2232 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2235 p_track->i_chunk = i_chunk;
2236 p_track->i_sample = i_sample;
2238 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2241 /****************************************************************************
2243 ****************************************************************************
2244 * Parse track information and create all needed data to run a track
2245 * If it succeed b_ok is set to 1 else to 0
2246 ****************************************************************************/
2247 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2248 MP4_Box_t *p_box_trak,
2249 bool b_force_enable )
2251 demux_sys_t *p_sys = p_demux->p_sys;
2253 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2254 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2266 /* hint track unsupported */
2268 /* set default value (-> track unusable) */
2269 p_track->b_ok = false;
2270 p_track->b_enable = false;
2271 p_track->b_selected = false;
2272 p_track->b_chapter = false;
2273 p_track->b_mac_encoding = false;
2275 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2282 /* do we launch this track by default ? */
2284 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2285 if( !p_track->b_enable )
2286 p_track->fmt.i_priority = -1;
2288 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2290 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2291 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2292 p_track->f_rotation = p_tkhd->data.p_tkhd->f_rotation;
2296 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2299 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2300 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2302 if( ( !p_mdhd )||( !p_hdlr ) )
2307 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2308 if( !p_track->i_timescale )
2311 if( p_mdhd->data.p_mdhd->i_language_code < 0x800 )
2313 /* We can convert i_language_code into iso 639 code,
2315 strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2316 p_track->b_mac_encoding = true;
2320 for( unsigned i = 0; i < 3; i++ )
2321 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2325 switch( p_hdlr->data.p_hdlr->i_handler_type )
2328 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2332 p_track->fmt.i_cat = AUDIO_ES;
2336 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2340 p_track->fmt.i_cat = VIDEO_ES;
2347 p_track->fmt.i_cat = SPU_ES;
2354 p_track->i_elst = 0;
2355 p_track->i_elst_time = 0;
2356 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2358 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2361 msg_Warn( p_demux, "elst box found" );
2362 for( i = 0; i < elst->i_entry_count; i++ )
2364 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2365 "ms) rate=%d.%d", i,
2366 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2367 elst->i_media_time[i] >= 0 ?
2368 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2370 elst->i_media_rate_integer[i],
2371 elst->i_media_rate_fraction[i] );
2378 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2380 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2381 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2387 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2389 p_track->fmt.psz_language = strdup( language );
2392 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2395 MP4_Box_t *p_box_iter;
2396 for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
2397 p_box_iter = p_box_iter->p_next )
2399 switch( p_box_iter->i_type )
2402 p_track->fmt.psz_description =
2403 strdup( p_box_iter->data.p_0xa9xxx->psz_text );
2406 p_track->fmt.psz_description =
2407 strdup( p_box_iter->data.p_name->psz_text );
2413 /* Create chunk index table and sample index table */
2414 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2415 TrackCreateSamplesIndex( p_demux, p_track ) )
2417 return; /* cannot create chunks index */
2420 p_track->i_chunk = 0;
2421 p_track->i_sample = 0;
2423 /* Mark chapter only track */
2424 if( p_sys->p_tref_chap )
2426 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2429 for( i = 0; i < p_chap->i_entry_count; i++ )
2431 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2433 p_track->b_chapter = true;
2434 p_track->b_enable = false;
2441 if( b_force_enable &&
2442 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2444 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2445 p_track->i_track_ID );
2446 p_track->b_enable = true;
2447 p_track->fmt.i_priority = 0;
2450 p_track->p_es = NULL;
2451 if( TrackCreateES( p_demux,
2452 p_track, p_track->i_chunk,
2453 p_track->b_chapter ? NULL : &p_track->p_es ) )
2455 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2456 p_track->i_track_ID );
2459 p_track->b_ok = true;
2463 for( i = 0; i < p_track->i_chunk_count; i++ )
2465 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2466 i, p_track->chunk[i].i_sample_count,
2467 p_track->chunk[i].i_first_dts );
2474 /****************************************************************************
2476 ****************************************************************************
2477 * Destroy a track created by MP4_TrackCreate.
2478 ****************************************************************************/
2479 static void MP4_TrackDestroy( mp4_track_t *p_track )
2481 unsigned int i_chunk;
2483 p_track->b_ok = false;
2484 p_track->b_enable = false;
2485 p_track->b_selected = false;
2487 es_format_Clean( &p_track->fmt );
2489 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2491 if( p_track->chunk )
2493 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2494 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2496 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2497 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2500 FREENULL( p_track->chunk );
2502 if( !p_track->i_sample_size )
2504 FREENULL( p_track->p_sample_size );
2508 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2511 if( !p_track->b_ok || p_track->b_chapter )
2513 return VLC_EGENERIC;
2516 if( p_track->b_selected )
2518 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2519 p_track->i_track_ID );
2523 return MP4_TrackSeek( p_demux, p_track, i_start );
2526 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2528 if( !p_track->b_ok || p_track->b_chapter )
2533 if( !p_track->b_selected )
2535 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2536 p_track->i_track_ID );
2541 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2542 p_track->p_es, false );
2545 p_track->b_selected = false;
2548 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2554 if( !p_track->b_ok || p_track->b_chapter )
2555 return VLC_EGENERIC;
2557 p_track->b_selected = false;
2559 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2560 &i_chunk, &i_sample ) )
2562 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2563 p_track->i_track_ID );
2564 return VLC_EGENERIC;
2567 p_track->b_selected = true;
2569 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2570 p_track->b_selected = true;
2572 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2577 * 3 types: for audio
2580 #define QT_V0_MAX_SAMPLES 1024
2581 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2584 MP4_Box_data_sample_soun_t *p_soun;
2586 if( p_track->i_sample_size == 0 )
2588 /* most simple case */
2589 return p_track->p_sample_size[p_track->i_sample];
2591 if( p_track->fmt.i_cat != AUDIO_ES )
2593 return p_track->i_sample_size;
2596 p_soun = p_track->p_sample->data.p_sample_soun;
2598 if( p_soun->i_qt_version == 1 )
2600 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
2601 if( p_track->fmt.audio.i_blockalign > 1 )
2602 i_samples = p_soun->i_sample_per_packet;
2604 i_size = i_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2606 else if( p_track->i_sample_size > 256 )
2608 /* We do that so we don't read too much data
2609 * (in this case we are likely dealing with compressed data) */
2610 i_size = p_track->i_sample_size;
2614 /* Read a bunch of samples at once */
2615 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2616 ( p_track->i_sample -
2617 p_track->chunk[p_track->i_chunk].i_sample_first );
2619 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2620 i_size = i_samples * p_track->i_sample_size;
2623 //fprintf( stderr, "size=%d\n", i_size );
2627 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2629 unsigned int i_sample;
2632 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2634 if( p_track->i_sample_size )
2636 MP4_Box_data_sample_soun_t *p_soun =
2637 p_track->p_sample->data.p_sample_soun;
2639 if( p_track->fmt.i_cat != AUDIO_ES || p_soun->i_qt_version == 0 )
2641 i_pos += ( p_track->i_sample -
2642 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2643 p_track->i_sample_size;
2647 /* we read chunk by chunk unless a blockalign is requested */
2648 if( p_track->fmt.audio.i_blockalign > 1 )
2649 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
2650 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2655 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2656 i_sample < p_track->i_sample; i_sample++ )
2658 i_pos += p_track->p_sample_size[i_sample];
2665 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2667 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2669 MP4_Box_data_sample_soun_t *p_soun;
2671 p_soun = p_track->p_sample->data.p_sample_soun;
2673 if( p_soun->i_qt_version == 1 )
2675 /* we read chunk by chunk unless a blockalign is requested */
2676 if( p_track->fmt.audio.i_blockalign > 1 )
2677 p_track->i_sample += p_soun->i_sample_per_packet;
2679 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
2681 else if( p_track->i_sample_size > 256 )
2683 /* We do that so we don't read too much data
2684 * (in this case we are likely dealing with compressed data) */
2685 p_track->i_sample += 1;
2690 p_track->i_sample += QT_V0_MAX_SAMPLES;
2691 if( p_track->i_sample >
2692 p_track->chunk[p_track->i_chunk].i_sample_first +
2693 p_track->chunk[p_track->i_chunk].i_sample_count )
2696 p_track->chunk[p_track->i_chunk].i_sample_first +
2697 p_track->chunk[p_track->i_chunk].i_sample_count;
2703 p_track->i_sample++;
2706 if( p_track->i_sample >= p_track->i_sample_count )
2707 return VLC_EGENERIC;
2709 /* Have we changed chunk ? */
2710 if( p_track->i_sample >=
2711 p_track->chunk[p_track->i_chunk].i_sample_first +
2712 p_track->chunk[p_track->i_chunk].i_sample_count )
2714 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2715 p_track->i_sample ) )
2717 msg_Warn( p_demux, "track[0x%x] will be disabled "
2718 "(cannot restart decoder)", p_track->i_track_ID );
2719 MP4_TrackUnselect( p_demux, p_track );
2720 return VLC_EGENERIC;
2724 /* Have we changed elst */
2725 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2727 demux_sys_t *p_sys = p_demux->p_sys;
2728 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2729 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2730 p_sys->i_timescale / (int64_t)1000000;
2732 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2733 i_mvt >= p_track->i_elst_time +
2734 elst->i_segment_duration[p_track->i_elst] )
2736 MP4_TrackSetELST( p_demux, p_track,
2737 MP4_TrackGetDTS( p_demux, p_track ) );
2744 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2747 demux_sys_t *p_sys = p_demux->p_sys;
2748 int i_elst_last = tk->i_elst;
2750 /* handle elst (find the correct one) */
2752 tk->i_elst_time = 0;
2753 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2755 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2756 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2758 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2760 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2762 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2766 tk->i_elst_time += i_dur;
2769 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2771 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2772 tk->i_elst = elst->i_entry_count - 1;
2773 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2776 if( elst->i_media_time[tk->i_elst] < 0 )
2779 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2782 if( i_elst_last != tk->i_elst )
2784 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2789 static const char *MP4_ConvertMacCode( uint16_t i_code )
2791 static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
2792 { "en", 0 }, { "fr", 1 }, { "de", 2 }, { "it", 3 }, { "nl", 4 },
2793 { "sv", 5 }, { "es", 6 }, { "da", 7 }, { "pt", 8 }, { "no", 9 },
2794 { "he", 10 }, { "ja", 11 }, { "ar", 12 }, { "fi", 13 }, { "el", 14 },
2795 { "is", 15 }, { "mt", 16 }, { "tr", 17 }, { "hr", 18 }, { "zh", 19 },
2796 { "ur", 20 }, { "hi", 21 }, { "th", 22 }, { "ko", 23 }, { "lt", 24 },
2797 { "pl", 25 }, { "hu", 26 }, { "et", 27 }, { "lv", 28 }, //{ "??", 29 },
2798 { "fo", 30 }, { "fa", 31 }, { "ru", 32 }, { "zh", 33 }, { "nl", 34 },
2799 { "ga", 35 }, { "sq", 36 }, { "ro", 37 }, { "cs", 38 }, { "sk", 39 },
2800 { "sl", 40 }, { "yi", 41 }, { "sr", 42 }, { "mk", 43 }, { "bg", 44 },
2801 { "uk", 45 }, { "be", 46 }, { "uz", 47 }, { "az", 48 }, { "kk", 48 },
2802 { "az", 50 }, { "hy", 51 }, { "ka", 52 }, { "mo", 53 }, { "ky", 54 },
2803 { "tg", 55 }, { "tk", 56 }, { "mn", 57 }, { "mn", 58 }, { "ps", 59 },
2804 { "ku", 60 }, { "ks", 61 }, { "sd", 62 }, { "bo", 63 }, { "ne", 64 },
2805 { "sa", 65 }, { "mr", 66 }, { "bn", 67 }, { "as", 68 }, { "gu", 69 },
2806 { "pa", 70 }, { "or", 71 }, { "ml", 72 }, { "kn", 73 }, { "ta", 74 },
2807 { "te", 75 }, { "si", 76 }, { "my", 77 }, { "km", 78 }, { "lo", 79 },
2808 { "vi", 80 }, { "id", 81 }, { "tl", 82 }, { "ms", 83 }, { "ms", 84 },
2809 { "am", 85 }, { "ti", 86 }, { "om", 87 }, { "so", 88 }, { "sw", 89 },
2810 { "rw", 90 }, { "rn", 91 }, { "ny", 92 }, { "mg", 93 }, { "eo", 94 },
2812 { "cy", 128 }, { "eu", 129 },
2813 { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
2814 { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
2815 { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
2816 { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
2821 for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
2823 if( p_cvt[i].i_code == i_code )
2824 return p_cvt[i].psz_iso639_1;