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>
35 #include <vlc_demux.h>
37 #include <vlc_charset.h>
38 #include <vlc_iso_lang.h>
40 #include <vlc_input.h>
44 #include "id3genres.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
53 set_category( CAT_INPUT )
54 set_subcategory( SUBCAT_INPUT_DEMUX )
55 set_description( N_("MP4 stream demuxer") )
56 set_shortname( N_("MP4") )
57 set_capability( "demux", 242 )
58 set_callbacks( Open, Close )
61 /*****************************************************************************
63 *****************************************************************************/
64 static int Demux ( demux_t * );
65 static int DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
66 static int Seek ( demux_t *, mtime_t );
67 static int Control ( demux_t *, int, va_list );
69 /* Contain all information about a chunk */
72 uint64_t i_offset; /* absolute position of this chunk in the file */
73 uint32_t i_sample_description_index; /* index for SampleEntry to use */
74 uint32_t i_sample_count; /* how many samples in this chunk */
75 uint32_t i_sample_first; /* index of the first sample in this chunk */
77 /* now provide way to calculate pts, dts, and offset without too
78 much memory and with fast access */
80 /* with this we can calculate dts/pts without waste memory */
81 uint64_t i_first_dts; /* DTS of the first sample */
82 uint64_t i_last_dts; /* DTS of the last sample */
83 uint32_t *p_sample_count_dts;
84 uint32_t *p_sample_delta_dts; /* dts delta */
86 uint32_t *p_sample_count_pts;
87 int32_t *p_sample_offset_pts; /* pts-dts */
89 /* TODO if needed add pts
90 but quickly *add* support for edts and seeking */
94 /* Contain all needed information for read all track with vlc */
97 unsigned int i_track_ID;/* this should be unique */
99 int b_ok; /* The track is usable */
100 int b_enable; /* is the trak enable by default */
101 bool b_selected; /* is the trak being played */
102 bool b_chapter; /* True when used for chapter only */
109 /* display size only ! */
113 /* more internal data */
114 uint64_t i_timescale; /* time scale for this track only */
117 int i_elst; /* current elst */
118 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
119 MP4_Box_t *p_elst; /* elst (could be NULL) */
121 /* give the next sample to read, i_chunk is to find quickly where
122 the sample is located */
123 uint32_t i_sample; /* next sample to read */
124 uint32_t i_chunk; /* chunk where next sample is stored */
125 /* total count of chunk and sample */
126 uint32_t i_chunk_count;
127 uint32_t i_sample_count;
129 mp4_chunk_t *chunk; /* always defined for each chunk */
131 /* sample size, p_sample_size defined only if i_sample_size == 0
132 else i_sample_size is size for all sample */
133 uint32_t i_sample_size;
134 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
135 too much time to do sumations each time*/
137 MP4_Box_t *p_stbl; /* will contain all timing information */
138 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
139 MP4_Box_t *p_sample;/* point on actual sdsd */
150 MP4_Box_t *p_root; /* container for the whole file */
154 uint64_t i_time; /* time position of the presentation
155 * in movie timescale */
156 uint64_t i_timescale; /* movie time scale */
157 uint64_t i_duration; /* movie duration */
158 unsigned int i_tracks; /* number of tracks */
159 mp4_track_t *track; /* array of track */
160 float f_fps; /* number of frame per seconds */
163 MP4_Box_t *p_tref_chap;
166 input_title_t *p_title;
169 /*****************************************************************************
170 * Declaration of local function
171 *****************************************************************************/
172 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *, bool b_force_enable );
173 static void MP4_TrackDestroy( mp4_track_t * );
175 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
176 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
178 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
180 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
181 static int MP4_TrackSampleSize( mp4_track_t * );
182 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
183 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
185 static void MP4_UpdateSeekpoint( demux_t * );
186 static const char *MP4_ConvertMacCode( uint16_t );
188 /* Return time in s of a track */
189 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
191 #define chunk p_track->chunk[p_track->i_chunk]
193 unsigned int i_index = 0;
194 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
195 int64_t i_dts = chunk.i_first_dts;
197 while( i_sample > 0 )
199 if( i_sample > chunk.p_sample_count_dts[i_index] )
201 i_dts += chunk.p_sample_count_dts[i_index] *
202 chunk.p_sample_delta_dts[i_index];
203 i_sample -= chunk.p_sample_count_dts[i_index];
208 i_dts += i_sample * chunk.p_sample_delta_dts[i_index];
215 /* now handle elst */
216 if( p_track->p_elst )
218 demux_sys_t *p_sys = p_demux->p_sys;
219 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
221 /* convert to offset */
222 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
223 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
224 elst->i_media_time[p_track->i_elst] > 0 )
226 i_dts -= elst->i_media_time[p_track->i_elst];
229 /* add i_elst_time */
230 i_dts += p_track->i_elst_time * p_track->i_timescale /
233 if( i_dts < 0 ) i_dts = 0;
236 return INT64_C(1000000) * i_dts / p_track->i_timescale;
239 static inline int64_t MP4_TrackGetPTSDelta( mp4_track_t *p_track )
241 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
242 unsigned int i_index = 0;
243 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
245 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
248 for( i_index = 0;; i_index++ )
250 if( i_sample < ck->p_sample_count_pts[i_index] )
251 return ck->p_sample_offset_pts[i_index] * INT64_C(1000000) /
252 (int64_t)p_track->i_timescale;
254 i_sample -= ck->p_sample_count_pts[i_index];
258 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
260 return INT64_C(1000000) * p_sys->i_time / p_sys->i_timescale;
263 static void LoadChapter( demux_t *p_demux );
265 /*****************************************************************************
266 * Open: check file and initializes MP4 structures
267 *****************************************************************************/
268 static int Open( vlc_object_t * p_this )
270 demux_t *p_demux = (demux_t *)p_this;
273 const uint8_t *p_peek;
284 /* A little test to see if it could be a mp4 */
285 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
287 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
298 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
305 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
308 msg_Warn( p_demux, "MP4 plugin discarded (not fastseekable)" );
312 /*Set exported functions */
313 p_demux->pf_demux = Demux;
314 p_demux->pf_control = Control;
316 /* create our structure that will contains all data */
317 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
319 /* Now load all boxes ( except raw data ) */
320 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
322 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
326 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
328 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
330 switch( p_ftyp->data.p_ftyp->i_major_brand )
334 "ISO Media file (isom) version %d.",
335 p_ftyp->data.p_ftyp->i_minor_version );
339 "unrecognized major file specification (%4.4s).",
340 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
346 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
349 /* the file need to have one moov box */
350 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
352 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
356 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
359 /* we have a free box as a moov, rename it */
360 p_foov->i_type = FOURCC_moov;
363 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
365 int i_count = MP4_BoxCount( p_rmra, "rmda" );
368 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
370 input_thread_t *p_input = demux_GetParentInput( p_demux );
371 input_item_t *p_current = input_GetItem( p_input );
373 input_item_node_t *p_subitems = input_item_node_Create( p_current );
375 for( i = 0; i < i_count; i++ )
377 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
381 if( !p_rdrf || !( psz_ref = strdup( p_rdrf->data.p_rdrf->psz_ref ) ) )
385 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
387 msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
388 psz_ref, (char*)&i_ref_type );
390 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
392 if( strstr( psz_ref, "qt5gateQT" ) )
394 msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
397 if( !strncmp( psz_ref, "http://", 7 ) ||
398 !strncmp( psz_ref, "rtsp://", 7 ) )
405 char *psz_path = strdup( p_demux->psz_location );
406 char *end = strrchr( psz_path, '/' );
407 if( end ) end[1] = '\0';
408 else *psz_path = '\0';
410 if( asprintf( &psz_absolute, "%s://%s%s",
411 p_demux->psz_access, psz_path, psz_ref ) < 0 )
415 vlc_object_release( p_input) ;
420 psz_ref = psz_absolute;
423 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
424 input_item_t *p_input = input_item_New( p_demux, psz_ref, NULL );
425 input_item_CopyOptions( p_current, p_input );
426 input_item_node_AppendItem( p_subitems, p_input );
427 vlc_gc_decref( p_input );
431 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
432 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
436 input_item_node_PostAndDelete( p_subitems );
437 vlc_object_release( p_input );
440 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
444 msg_Err( p_demux, "cannot find /moov/mvhd" );
449 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
450 p_demux->pf_demux = DemuxRef;
456 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
457 if( p_sys->i_timescale == 0 )
459 msg_Err( p_this, "bad timescale" );
462 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
465 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
467 msg_Err( p_demux, "cannot find any /moov/trak" );
470 msg_Dbg( p_demux, "found %d track%c",
472 p_sys->i_tracks ? 's':' ' );
474 /* allocate memory */
475 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
476 if( p_sys->track == NULL )
478 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
480 /* Search the first chap reference (like quicktime) and
481 * check that at least 1 stream is enabled */
482 p_sys->p_tref_chap = NULL;
483 b_enabled_es = false;
484 for( i = 0; i < p_sys->i_tracks; i++ )
486 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
489 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
490 if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
493 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
494 if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
495 p_sys->p_tref_chap = p_chap;
498 /* now process each track and extract all useful information */
499 for( i = 0; i < p_sys->i_tracks; i++ )
501 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
502 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
504 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
507 switch( p_sys->track[i].fmt.i_cat )
516 psz_cat = "subtitle";
524 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
525 p_sys->track[i].i_track_ID, psz_cat,
526 p_sys->track[i].b_enable ? "enable":"disable",
527 p_sys->track[i].fmt.psz_language ?
528 p_sys->track[i].fmt.psz_language : "undef" );
530 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
532 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
533 p_sys->track[i].i_track_ID,
534 p_sys->track[i].fmt.psz_language ?
535 p_sys->track[i].fmt.psz_language : "undef" );
539 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
540 p_sys->track[i].i_track_ID );
545 LoadChapter( p_demux );
552 MP4_BoxFree( p_demux->s, p_sys->p_root );
558 /*****************************************************************************
559 * Demux: read packet and send them to decoders
560 *****************************************************************************
561 * TODO check for newly selected track (ie audio upt to now )
562 *****************************************************************************/
563 static int Demux( demux_t *p_demux )
565 demux_sys_t *p_sys = p_demux->p_sys;
566 unsigned int i_track;
569 unsigned int i_track_selected;
571 /* check for newly selected/unselected track */
572 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
575 mp4_track_t *tk = &p_sys->track[i_track];
578 if( !tk->b_ok || tk->b_chapter ||
579 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
584 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
586 if( tk->b_selected && !b )
588 MP4_TrackUnselect( p_demux, tk );
590 else if( !tk->b_selected && b)
592 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
601 if( i_track_selected <= 0 )
603 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
604 if( p_sys->i_timescale > 0 )
606 int64_t i_length = (mtime_t)1000000 *
607 (mtime_t)p_sys->i_duration /
608 (mtime_t)p_sys->i_timescale;
609 if( MP4_GetMoviePTS( p_sys ) >= i_length )
614 msg_Warn( p_demux, "no track selected, exiting..." );
619 MP4_UpdateSeekpoint( p_demux );
621 /* first wait for the good time to read a packet */
622 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
624 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
626 /* we will read 100ms for each stream so ...*/
627 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
629 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
631 mp4_track_t *tk = &p_sys->track[i_track];
633 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
636 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
639 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
640 MP4_TrackGetDTS( p_demux, tk ),
641 MP4_GetMoviePTS( p_sys ) );
644 if( MP4_TrackSampleSize( tk ) > 0 )
650 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
652 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
654 MP4_TrackUnselect( p_demux, tk );
660 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
662 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
664 MP4_TrackUnselect( p_demux, tk );
668 if( tk->b_drms && tk->p_drms )
673 drms_get_p_key( tk->p_drms, p_key );
675 for( size_t i_pos = tk->p_skcr->data.p_skcr->i_init; i_pos < p_block->i_buffer; )
677 int n = __MIN( tk->p_skcr->data.p_skcr->i_encr, p_block->i_buffer - i_pos );
678 drms_decrypt( tk->p_drms, (uint32_t*)&p_block->p_buffer[i_pos], n, p_key );
680 i_pos += __MIN( tk->p_skcr->data.p_skcr->i_decr, p_block->i_buffer - i_pos );
685 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
686 p_block->i_buffer, NULL );
689 else if( tk->fmt.i_cat == SPU_ES )
691 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
692 p_block->i_buffer >= 2 )
694 uint16_t i_size = GetWBE( p_block->p_buffer );
696 if( i_size + 2 <= p_block->i_buffer )
699 /* remove the length field, and append a '\0' */
700 memmove( &p_block->p_buffer[0],
701 &p_block->p_buffer[2], i_size );
702 p_block->p_buffer[i_size] = '\0';
703 p_block->i_buffer = i_size + 1;
705 /* convert \r -> \n */
706 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
714 p_block->i_buffer = 0;
719 p_block->i_dts = VLC_TS_0 + MP4_TrackGetDTS( p_demux, tk );
721 i_delta = MP4_TrackGetPTSDelta( tk );
723 p_block->i_pts = p_block->i_dts + i_delta;
724 else if( tk->fmt.i_cat != VIDEO_ES )
725 p_block->i_pts = p_block->i_dts;
727 p_block->i_pts = VLC_TS_INVALID;
729 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
730 es_out_Send( p_demux->out, tk->p_es, p_block );
734 if( MP4_TrackNextSample( p_demux, tk ) )
742 static void MP4_UpdateSeekpoint( demux_t *p_demux )
744 demux_sys_t *p_sys = p_demux->p_sys;
747 if( !p_sys->p_title )
749 i_time = MP4_GetMoviePTS( p_sys );
750 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
752 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
757 if( i != p_demux->info.i_seekpoint && i >= 0 )
759 p_demux->info.i_seekpoint = i;
760 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
763 /*****************************************************************************
765 ******************************************************************************/
766 static int Seek( demux_t *p_demux, mtime_t i_date )
768 demux_sys_t *p_sys = p_demux->p_sys;
769 unsigned int i_track;
771 /* First update update global time */
772 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
773 p_sys->i_pcr = i_date;
775 /* Now for each stream try to go to this time */
776 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
778 mp4_track_t *tk = &p_sys->track[i_track];
779 MP4_TrackSeek( p_demux, tk, i_date );
781 MP4_UpdateSeekpoint( p_demux );
783 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
788 /*****************************************************************************
790 *****************************************************************************/
791 static int Control( demux_t *p_demux, int i_query, va_list args )
793 demux_sys_t *p_sys = p_demux->p_sys;
800 case DEMUX_GET_POSITION:
801 pf = (double*)va_arg( args, double * );
802 if( p_sys->i_duration > 0 )
804 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
812 case DEMUX_SET_POSITION:
813 f = (double)va_arg( args, double );
814 if( p_sys->i_timescale > 0 )
816 i64 = (int64_t)( f * (double)1000000 *
817 (double)p_sys->i_duration /
818 (double)p_sys->i_timescale );
819 return Seek( p_demux, i64 );
821 else return VLC_SUCCESS;
824 pi64 = (int64_t*)va_arg( args, int64_t * );
825 if( p_sys->i_timescale > 0 )
827 *pi64 = (mtime_t)1000000 *
828 (mtime_t)p_sys->i_time /
829 (mtime_t)p_sys->i_timescale;
835 i64 = (int64_t)va_arg( args, int64_t );
836 return Seek( p_demux, i64 );
838 case DEMUX_GET_LENGTH:
839 pi64 = (int64_t*)va_arg( args, int64_t * );
840 if( p_sys->i_timescale > 0 )
842 *pi64 = (mtime_t)1000000 *
843 (mtime_t)p_sys->i_duration /
844 (mtime_t)p_sys->i_timescale;
850 pf = (double*)va_arg( args, double* );
856 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
857 MP4_Box_t *p_0xa9xxx;
859 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
862 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
869 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
870 p_0xa9xxx = p_0xa9xxx->p_next )
873 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
876 /* FIXME FIXME: should convert from whatever the character
877 * encoding of MP4 meta data is to UTF-8. */
878 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
879 if( psz_utf ) { EnsureUTF8( psz_utf ); \
880 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
882 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
883 switch( p_0xa9xxx->i_type )
885 case FOURCC_0xa9nam: /* Full name */
886 SET( vlc_meta_SetTitle );
889 SET( vlc_meta_SetArtist );
892 SET( vlc_meta_SetArtist );
895 SET( vlc_meta_SetCopyright );
897 case FOURCC_0xa9day: /* Creation Date */
898 SET( vlc_meta_SetDate );
900 case FOURCC_0xa9des: /* Description */
901 SET( vlc_meta_SetDescription );
903 case FOURCC_0xa9gen: /* Genre */
904 SET( vlc_meta_SetGenre );
908 if( p_0xa9xxx->data.p_gnre->i_genre <= NUM_GENRES )
909 vlc_meta_SetGenre( p_meta, ppsz_genres[p_0xa9xxx->data.p_gnre->i_genre - 1] );
912 case FOURCC_0xa9alb: /* Album */
913 SET( vlc_meta_SetAlbum );
916 case FOURCC_0xa9trk: /* Track */
917 SET( vlc_meta_SetTrackNum );
922 snprintf( psz_trck, sizeof( psz_trck ), "%i",
923 p_0xa9xxx->data.p_trkn->i_track_number );
924 vlc_meta_SetTrackNum( p_meta, psz_trck );
927 case FOURCC_0xa9cmt: /* Commment */
928 SET( vlc_meta_SetDescription );
931 case FOURCC_0xa9url: /* URL */
932 SET( vlc_meta_SetURL );
935 case FOURCC_0xa9too: /* Encoder Tool */
936 case FOURCC_0xa9enc: /* Encoded By */
937 SET( vlc_meta_SetEncodedBy );
944 static const struct { uint32_t xa9_type; char metadata[25]; } xa9typetoextrameta[] =
946 { FOURCC_0xa9wrt, N_("Writer") },
947 { FOURCC_0xa9com, N_("Composr") },
948 { FOURCC_0xa9prd, N_("Producer") },
949 { FOURCC_0xa9inf, N_("Information") },
950 { FOURCC_0xa9dir, N_("Director") },
951 { FOURCC_0xa9dis, N_("Disclaimer") },
952 { FOURCC_0xa9req, N_("Requirements") },
953 { FOURCC_0xa9fmt, N_("Original Format") },
954 { FOURCC_0xa9dsa, N_("Display Source As") },
955 { FOURCC_0xa9hst, N_("Host Computer") },
956 { FOURCC_0xa9prf, N_("Performers") },
957 { FOURCC_0xa9ope, N_("Original Performer") },
958 { FOURCC_0xa9src, N_("Providers Source Content") },
959 { FOURCC_0xa9wrn, N_("Warning") },
960 { FOURCC_0xa9swr, N_("Software") },
961 { FOURCC_0xa9lyr, N_("Lyrics") },
962 { FOURCC_0xa9mak, N_("Make") },
963 { FOURCC_0xa9mod, N_("Model") },
964 { FOURCC_0xa9PRD, N_("Product") },
965 { FOURCC_0xa9grp, N_("Grouping") },
968 for( unsigned i = 0; xa9typetoextrameta[i].xa9_type; i++ )
970 if( p_0xa9xxx->i_type == xa9typetoextrameta[i].xa9_type )
972 char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" );
975 EnsureUTF8( psz_utf );
976 vlc_meta_AddExtra( p_meta, _(xa9typetoextrameta[i].metadata), psz_utf );
986 case DEMUX_GET_TITLE_INFO:
988 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
989 int *pi_int = (int*)va_arg( args, int* );
990 int *pi_title_offset = (int*)va_arg( args, int* );
991 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
993 if( !p_sys->p_title )
997 *ppp_title = malloc( sizeof( input_title_t**) );
998 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
999 *pi_title_offset = 0;
1000 *pi_seekpoint_offset = 0;
1003 case DEMUX_SET_TITLE:
1005 const int i_title = (int)va_arg( args, int );
1006 if( !p_sys->p_title || i_title != 0 )
1007 return VLC_EGENERIC;
1010 case DEMUX_SET_SEEKPOINT:
1012 const int i_seekpoint = (int)va_arg( args, int );
1013 if( !p_sys->p_title )
1014 return VLC_EGENERIC;
1015 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
1018 case DEMUX_SET_NEXT_DEMUX_TIME:
1019 case DEMUX_SET_GROUP:
1020 case DEMUX_HAS_UNSUPPORTED_META:
1021 case DEMUX_GET_ATTACHMENTS:
1022 return VLC_EGENERIC;
1025 msg_Warn( p_demux, "control query %u unimplemented", i_query );
1026 return VLC_EGENERIC;
1030 /*****************************************************************************
1031 * Close: frees unused data
1032 *****************************************************************************/
1033 static void Close ( vlc_object_t * p_this )
1035 demux_t * p_demux = (demux_t *)p_this;
1036 demux_sys_t *p_sys = p_demux->p_sys;
1037 unsigned int i_track;
1039 msg_Dbg( p_demux, "freeing all memory" );
1041 MP4_BoxFree( p_demux->s, p_sys->p_root );
1042 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1044 MP4_TrackDestroy( &p_sys->track[i_track] );
1046 FREENULL( p_sys->track );
1048 if( p_sys->p_title )
1049 vlc_input_title_Delete( p_sys->p_title );
1056 /****************************************************************************
1057 * Local functions, specific to vlc
1058 ****************************************************************************/
1060 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1062 demux_sys_t *p_sys = p_demux->p_sys;
1065 p_sys->p_title = vlc_input_title_New();
1066 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1068 seekpoint_t *s = vlc_seekpoint_New();
1070 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1071 EnsureUTF8( s->psz_name );
1072 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1073 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1076 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1078 demux_sys_t *p_sys = p_demux->p_sys;
1080 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1082 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1083 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( tk );
1084 const unsigned int i_size = MP4_TrackSampleSize( tk );
1086 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1089 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1090 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1094 seekpoint_t *s = vlc_seekpoint_New();
1096 s->psz_name = strndup( &p_buffer[2], i_len );
1097 EnsureUTF8( s->psz_name );
1099 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1101 if( !p_sys->p_title )
1102 p_sys->p_title = vlc_input_title_New();
1103 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1106 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1107 tk->chunk[tk->i_chunk].i_sample_count )
1111 static void LoadChapter( demux_t *p_demux )
1113 demux_sys_t *p_sys = p_demux->p_sys;
1116 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1118 LoadChapterGpac( p_demux, p_chpl );
1120 else if( p_sys->p_tref_chap )
1122 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1125 /* Load the first subtitle track like quicktime */
1126 for( i = 0; i < p_chap->i_entry_count; i++ )
1128 for( j = 0; j < p_sys->i_tracks; j++ )
1130 mp4_track_t *tk = &p_sys->track[j];
1131 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1132 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1135 if( j < p_sys->i_tracks )
1137 LoadChapterApple( p_demux, &p_sys->track[j] );
1144 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1145 static int TrackCreateChunksIndex( demux_t *p_demux,
1146 mp4_track_t *p_demux_track )
1148 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1151 unsigned int i_chunk;
1152 unsigned int i_index, i_last;
1154 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1155 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1156 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1158 return( VLC_EGENERIC );
1161 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1162 if( !p_demux_track->i_chunk_count )
1164 msg_Warn( p_demux, "no chunk defined" );
1165 return( VLC_EGENERIC );
1167 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1168 sizeof( mp4_chunk_t ) );
1169 if( p_demux_track->chunk == NULL )
1174 /* first we read chunk offset */
1175 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1177 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1179 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1181 ck->i_first_dts = 0;
1182 ck->p_sample_count_dts = NULL;
1183 ck->p_sample_delta_dts = NULL;
1184 ck->p_sample_count_pts = NULL;
1185 ck->p_sample_offset_pts = NULL;
1188 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1189 to be used for the sample XXX begin to 1
1190 We construct it begining at the end */
1191 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1192 i_index = p_stsc->data.p_stsc->i_entry_count;
1195 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1196 return( VLC_EGENERIC );
1201 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1202 i_chunk < i_last; i_chunk++ )
1204 if( i_chunk >= p_demux_track->i_chunk_count )
1206 msg_Warn( p_demux, "corrupted chunk table" );
1207 return VLC_EGENERIC;
1210 p_demux_track->chunk[i_chunk].i_sample_description_index =
1211 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1212 p_demux_track->chunk[i_chunk].i_sample_count =
1213 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1215 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1218 p_demux_track->chunk[0].i_sample_first = 0;
1219 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1221 p_demux_track->chunk[i_chunk].i_sample_first =
1222 p_demux_track->chunk[i_chunk-1].i_sample_first +
1223 p_demux_track->chunk[i_chunk-1].i_sample_count;
1226 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1227 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1232 static int TrackCreateSamplesIndex( demux_t *p_demux,
1233 mp4_track_t *p_demux_track )
1236 MP4_Box_data_stsz_t *stsz;
1237 MP4_Box_data_stts_t *stts;
1238 /* TODO use also stss and stsh table for seeking */
1239 /* FIXME use edit table */
1244 int64_t i_index_sample_used;
1249 * Gives the sample size for each samples. There is also a stz2 table
1250 * (compressed form) that we need to implement TODO */
1251 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1254 /* FIXME and stz2 */
1255 msg_Warn( p_demux, "cannot find STSZ box" );
1256 return VLC_EGENERIC;
1258 stsz = p_box->data.p_stsz;
1261 * Gives mapping between sample and decoding time
1263 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1266 msg_Warn( p_demux, "cannot find STTS box" );
1267 return VLC_EGENERIC;
1269 stts = p_box->data.p_stts;
1271 /* Use stsz table to create a sample number -> sample size table */
1272 p_demux_track->i_sample_count = stsz->i_sample_count;
1273 if( stsz->i_sample_size )
1275 /* 1: all sample have the same size, so no need to construct a table */
1276 p_demux_track->i_sample_size = stsz->i_sample_size;
1277 p_demux_track->p_sample_size = NULL;
1281 /* 2: each sample can have a different size */
1282 p_demux_track->i_sample_size = 0;
1283 p_demux_track->p_sample_size =
1284 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1285 if( p_demux_track->p_sample_size == NULL )
1288 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1290 p_demux_track->p_sample_size[i_sample] =
1291 stsz->i_entry_size[i_sample];
1295 /* Use stts table to create a sample number -> dts table.
1296 * XXX: if we don't want to waste too much memory, we can't expand
1297 * the box! so each chunk will contain an "extract" of this table
1298 * for fast research (problem with raw stream where a sample is sometime
1299 * just channels*bits_per_sample/8 */
1302 i_index = 0; i_index_sample_used = 0;
1303 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1305 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1306 int64_t i_entry, i_sample_count, i;
1308 /* save first dts */
1309 ck->i_first_dts = i_next_dts;
1310 ck->i_last_dts = i_next_dts;
1312 /* count how many entries are needed for this chunk
1313 * for p_sample_delta_dts and p_sample_count_dts */
1314 i_sample_count = ck->i_sample_count;
1317 while( i_sample_count > 0 )
1319 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1320 /* don't count already used sample in this entry */
1322 i_sample_count += i_index_sample_used;
1328 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1329 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1331 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1335 i_sample_count = ck->i_sample_count;
1336 for( i = 0; i < i_entry; i++ )
1341 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1343 i_used = __MIN( i_rest, i_sample_count );
1345 i_index_sample_used += i_used;
1346 i_sample_count -= i_used;
1347 i_next_dts += i_used * stts->i_sample_delta[i_index];
1349 ck->p_sample_count_dts[i] = i_used;
1350 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1352 ck->i_last_dts = i_next_dts - ck->p_sample_delta_dts[i];
1354 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1357 i_index_sample_used = 0;
1363 * Gives the delta between decoding time (dts) and composition table (pts)
1365 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1368 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1370 msg_Warn( p_demux, "CTTS table" );
1372 /* Create pts-dts table per chunk */
1373 i_index = 0; i_index_sample_used = 0;
1374 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1376 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1377 int64_t i_entry, i_sample_count, i;
1379 /* count how many entries are needed for this chunk
1380 * for p_sample_delta_dts and p_sample_count_dts */
1381 i_sample_count = ck->i_sample_count;
1384 while( i_sample_count > 0 )
1386 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1388 /* don't count already used sample in this entry */
1390 i_sample_count += i_index_sample_used;
1396 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1397 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1398 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1402 i_sample_count = ck->i_sample_count;
1403 for( i = 0; i < i_entry; i++ )
1408 i_rest = ctts->i_sample_count[i_index] -
1409 i_index_sample_used;
1411 i_used = __MIN( i_rest, i_sample_count );
1413 i_index_sample_used += i_used;
1414 i_sample_count -= i_used;
1416 ck->p_sample_count_pts[i] = i_used;
1417 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1419 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1422 i_index_sample_used = 0;
1428 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1429 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1430 i_next_dts / p_demux_track->i_timescale );
1436 * It computes the sample rate for a video track using the given sample
1439 static void TrackGetESSampleRate( unsigned *pi_num, unsigned *pi_den,
1440 const mp4_track_t *p_track,
1441 unsigned i_sd_index,
1447 if( p_track->i_chunk_count <= 0 )
1451 const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
1452 while( p_chunk > &p_track->chunk[0] &&
1453 p_chunk[-1].i_sample_description_index == i_sd_index )
1458 uint64_t i_sample = 0;
1459 uint64_t i_first_dts = p_chunk->i_first_dts;
1460 uint64_t i_last_dts;
1463 i_sample += p_chunk->i_sample_count;
1464 i_last_dts = p_chunk->i_last_dts;
1467 while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
1468 p_chunk->i_sample_description_index == i_sd_index );
1470 if( i_sample > 1 && i_first_dts < i_last_dts )
1471 vlc_ureduce( pi_num, pi_den,
1472 ( i_sample - 1) * p_track->i_timescale,
1473 i_last_dts - i_first_dts,
1479 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1481 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1482 unsigned int i_chunk, es_out_id_t **pp_es )
1484 const unsigned i_sample_description_index =
1485 p_track->chunk[i_chunk].i_sample_description_index;
1486 MP4_Box_t *p_sample;
1494 if( !i_sample_description_index )
1496 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1497 p_track->i_track_ID );
1498 return VLC_EGENERIC;
1501 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1502 i_sample_description_index - 1 );
1505 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1507 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1508 p_track->i_track_ID );
1509 return VLC_EGENERIC;
1512 p_track->p_sample = p_sample;
1514 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
1516 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
1518 p_sample->i_type = p_frma->data.p_frma->i_type;
1521 p_enda = MP4_BoxGet( p_sample, "wave/enda" );
1523 p_enda = MP4_BoxGet( p_sample, "enda" );
1525 if( p_track->fmt.i_cat == AUDIO_ES && ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1527 MP4_Box_data_sample_soun_t *p_soun;
1529 p_soun = p_sample->data.p_sample_soun;
1531 if( p_soun->i_qt_version == 0 )
1533 switch( p_sample->i_type )
1535 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1536 p_soun->i_qt_version = 1;
1537 p_soun->i_sample_per_packet = 64;
1538 p_soun->i_bytes_per_packet = 34;
1539 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1540 p_soun->i_bytes_per_sample = 2;
1542 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1543 p_soun->i_qt_version = 1;
1544 p_soun->i_sample_per_packet = 6;
1545 p_soun->i_bytes_per_packet = 2;
1546 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1547 p_soun->i_bytes_per_sample = 2;
1549 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1550 p_soun->i_qt_version = 1;
1551 p_soun->i_sample_per_packet = 12;
1552 p_soun->i_bytes_per_packet = 2;
1553 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1554 p_soun->i_bytes_per_sample = 2;
1556 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1557 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1558 p_soun->i_samplesize = 8;
1559 p_track->i_sample_size = p_soun->i_channelcount;
1561 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1562 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1563 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1564 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1565 /* What would be the fun if you could trust the .mov */
1566 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1573 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1575 p_soun->i_qt_version = 0;
1578 else if( p_track->fmt.i_cat == AUDIO_ES && p_sample->data.p_sample_soun->i_qt_version == 1 )
1580 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1582 switch( p_sample->i_type )
1584 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1585 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1587 if( p_track->i_sample_size > 1 )
1588 p_soun->i_qt_version = 0;
1591 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1592 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1593 case( VLC_FOURCC( 'm', 's', 0x20, 0x00 ) ):
1594 p_soun->i_qt_version = 0;
1602 switch( p_track->fmt.i_cat )
1605 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1606 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1607 p_track->fmt.video.i_bits_per_pixel =
1608 p_sample->data.p_sample_vide->i_depth;
1610 /* fall on display size */
1611 if( p_track->fmt.video.i_width <= 0 )
1612 p_track->fmt.video.i_width = p_track->i_width;
1613 if( p_track->fmt.video.i_height <= 0 )
1614 p_track->fmt.video.i_height = p_track->i_height;
1616 /* Find out apect ratio from display size */
1617 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1618 /* Work-around buggy muxed files */
1619 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1621 p_track->fmt.video.i_sar_num = p_track->i_width * p_track->fmt.video.i_height;
1622 p_track->fmt.video.i_sar_den = p_track->i_height * p_track->fmt.video.i_width;
1625 /* Support for cropping (eg. in H263 files) */
1626 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1627 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1630 TrackGetESSampleRate( &p_track->fmt.video.i_frame_rate,
1631 &p_track->fmt.video.i_frame_rate_base,
1632 p_track, i_sample_description_index, i_chunk );
1633 p_demux->p_sys->f_fps = (float)p_track->fmt.video.i_frame_rate /
1634 (float)p_track->fmt.video.i_frame_rate_base;
1638 p_track->fmt.audio.i_channels =
1639 p_sample->data.p_sample_soun->i_channelcount;
1640 p_track->fmt.audio.i_rate =
1641 p_sample->data.p_sample_soun->i_sampleratehi;
1642 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1643 p_sample->data.p_sample_soun->i_sampleratehi *
1644 p_sample->data.p_sample_soun->i_samplesize;
1645 p_track->fmt.audio.i_bitspersample =
1646 p_sample->data.p_sample_soun->i_samplesize;
1648 if( p_track->i_sample_size != 0 &&
1649 p_sample->data.p_sample_soun->i_qt_version == 1 && p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1651 msg_Err( p_demux, "Invalid sample per packet value for qt_version 1" );
1652 return VLC_EGENERIC;
1661 /* It's a little ugly but .. there are special cases */
1662 switch( p_sample->i_type )
1664 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1665 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1667 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1670 case( VLC_FOURCC( 'a', 'c', '-', '3' ) ):
1672 MP4_Box_t *p_dac3_box = MP4_BoxGet( p_sample, "dac3", 0 );
1674 p_track->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1677 static const int pi_bitrate[] = {
1684 MP4_Box_data_dac3_t *p_dac3 = p_dac3_box->data.p_dac3;
1685 p_track->fmt.audio.i_channels = 0;
1686 p_track->fmt.i_bitrate = 0;
1687 if( p_dac3->i_bitrate_code < sizeof(pi_bitrate)/sizeof(*pi_bitrate) )
1688 p_track->fmt.i_bitrate = pi_bitrate[p_dac3->i_bitrate_code] * 1000;
1689 p_track->fmt.audio.i_bitspersample = 0;
1693 case( VLC_FOURCC( 'e', 'c', '-', '3' ) ):
1695 p_track->fmt.i_codec = VLC_FOURCC( 'e', 'a', 'c', '3' );
1699 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1700 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1702 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1704 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1705 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1707 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1709 /* Buggy files workaround */
1710 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1711 p_sample->data.p_sample_soun->i_sampleratehi) )
1713 MP4_Box_data_sample_soun_t *p_soun =
1714 p_sample->data.p_sample_soun;
1716 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1717 "(%u), making both equal (report any problem).",
1718 p_track->i_timescale, p_soun->i_sampleratehi );
1720 if( p_soun->i_sampleratehi )
1721 p_track->i_timescale = p_soun->i_sampleratehi;
1723 p_soun->i_sampleratehi = p_track->i_timescale;
1728 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1729 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1732 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1733 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1734 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1735 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1736 /* FIXME UTF-8 doesn't work here ? */
1737 if( p_track->b_mac_encoding )
1738 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1740 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1743 case VLC_FOURCC('y','v','1','2'):
1744 p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1746 case VLC_FOURCC('y','u','v','2'):
1747 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1750 case VLC_FOURCC('i','n','2','4'):
1751 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1752 VLC_FOURCC('4','2','n','i') : VLC_FOURCC('i','n','2','4');
1754 case VLC_FOURCC('f','l','3','2'):
1755 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1756 VLC_CODEC_F32L : VLC_CODEC_F32B;
1758 case VLC_FOURCC('f','l','6','4'):
1759 p_track->fmt.i_codec = p_enda && p_enda->data.p_enda->i_little_endian == 1 ?
1760 VLC_CODEC_F64L : VLC_CODEC_F64B;
1764 p_track->fmt.i_codec = p_sample->i_type;
1768 /* now see if esds is present and if so create a data packet
1769 with decoder_specific_info */
1770 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1771 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1772 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1773 ( p_esds->data.p_esds )&&
1776 /* First update information based on i_objectTypeIndication */
1777 switch( p_decconfig->i_objectTypeIndication )
1779 case( 0x20 ): /* MPEG4 VIDEO */
1780 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1783 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1784 if( p_decconfig->i_decoder_specific_info_len >= 2 &&
1785 p_decconfig->p_decoder_specific_info[0] == 0xF8 &&
1786 (p_decconfig->p_decoder_specific_info[1]&0xE0) == 0x80 )
1788 p_track->fmt.i_codec = VLC_CODEC_ALS;
1796 case( 0x65): /* MPEG2 video */
1797 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1799 /* Theses are MPEG2-AAC */
1800 case( 0x66): /* main profile */
1801 case( 0x67): /* Low complexity profile */
1802 case( 0x68): /* Scaleable Sampling rate profile */
1803 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1805 /* true MPEG 2 audio */
1807 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1809 case( 0x6a): /* MPEG1 video */
1810 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1812 case( 0x6b): /* MPEG1 audio */
1813 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1815 case( 0x6c ): /* jpeg */
1816 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1818 case( 0x6d ): /* png */
1819 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
1821 case( 0x6e ): /* jpeg200 */
1822 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
1824 case( 0xa3 ): /* vc1 */
1825 p_track->fmt.i_codec = VLC_FOURCC( 'W','V','C','1' );
1829 case( 0xe0 ): /* NeroDigital: dvd subs */
1830 if( p_track->fmt.i_cat == SPU_ES )
1832 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1833 if( p_track->i_width > 0 )
1834 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1835 if( p_track->i_height > 0 )
1836 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1839 case( 0xe1 ): /* QCelp for 3gp */
1840 if( p_track->fmt.i_cat == AUDIO_ES )
1842 p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
1848 /* Unknown entry, but don't touch i_fourcc */
1850 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1851 p_decconfig->i_objectTypeIndication,
1852 p_track->i_track_ID );
1855 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1856 if( p_track->fmt.i_extra > 0 )
1858 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1859 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1860 p_track->fmt.i_extra );
1865 switch( p_sample->i_type )
1867 /* qt decoder, send the complete chunk */
1868 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1869 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1870 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1871 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1872 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1873 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1874 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1875 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1876 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1877 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1878 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1879 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1880 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1882 /* qt decoder, send the complete chunk */
1883 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1884 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1885 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1886 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1887 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1888 p_track->fmt.i_extra =
1889 p_sample->data.p_sample_vide->i_qt_image_description;
1890 if( p_track->fmt.i_extra > 0 )
1892 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1893 memcpy( p_track->fmt.p_extra,
1894 p_sample->data.p_sample_vide->p_qt_image_description,
1895 p_track->fmt.i_extra);
1898 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1899 p_track->fmt.audio.i_rate = 8000;
1900 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1901 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1902 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1903 p_track->fmt.i_extra =
1904 p_sample->data.p_sample_soun->i_qt_description;
1905 if( p_track->fmt.i_extra > 0 )
1907 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1908 memcpy( p_track->fmt.p_extra,
1909 p_sample->data.p_sample_soun->p_qt_description,
1910 p_track->fmt.i_extra);
1914 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1915 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1917 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1921 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1922 if( p_track->fmt.i_extra > 0 )
1924 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1925 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1926 p_track->fmt.i_extra );
1931 msg_Err( p_demux, "missing avcC" );
1936 case VLC_FOURCC('m','s',0x00,0x02):
1937 case VLC_FOURCC('m','s',0x00,0x11):
1938 case VLC_FOURCC('Q','c','l','p'):
1939 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
1950 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1955 /* given a time it return sample/chunk
1956 * it also update elst field of the track
1958 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1959 int64_t i_start, uint32_t *pi_chunk,
1960 uint32_t *pi_sample )
1962 demux_sys_t *p_sys = p_demux->p_sys;
1963 MP4_Box_t *p_box_stss;
1965 unsigned int i_sample;
1966 unsigned int i_chunk;
1969 /* FIXME see if it's needed to check p_track->i_chunk_count */
1970 if( p_track->i_chunk_count == 0 )
1971 return( VLC_EGENERIC );
1973 /* handle elst (find the correct one) */
1974 MP4_TrackSetELST( p_demux, p_track, i_start );
1975 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1977 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1978 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1980 /* now calculate i_start for this elst */
1982 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
1990 /* to track time scale */
1991 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1992 /* add elst offset */
1993 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1994 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1995 elst->i_media_time[p_track->i_elst] > 0 )
1997 i_start += elst->i_media_time[p_track->i_elst];
2000 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
2001 "ms (track)", p_track->i_elst,
2002 i_mvt * 1000 / p_sys->i_timescale,
2003 i_start * 1000 / p_track->i_timescale );
2007 /* convert absolute time to in timescale unit */
2008 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
2011 /* we start from sample 0/chunk 0, hope it won't take too much time */
2012 /* *** find good chunk *** */
2013 for( i_chunk = 0; ; i_chunk++ )
2015 if( i_chunk + 1 >= p_track->i_chunk_count )
2017 /* at the end and can't check if i_start in this chunk,
2018 it will be check while searching i_sample */
2019 i_chunk = p_track->i_chunk_count - 1;
2023 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
2024 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
2030 /* *** find sample in the chunk *** */
2031 i_sample = p_track->chunk[i_chunk].i_sample_first;
2032 i_dts = p_track->chunk[i_chunk].i_first_dts;
2033 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
2036 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2037 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
2040 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
2041 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2043 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
2048 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
2052 i_sample += ( i_start - i_dts ) /
2053 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
2058 if( i_sample >= p_track->i_sample_count )
2060 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
2061 "(seeking too far) chunk=%d sample=%d",
2062 p_track->i_track_ID, i_chunk, i_sample );
2063 return( VLC_EGENERIC );
2067 /* *** Try to find nearest sync points *** */
2068 if( ( p_box_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
2070 MP4_Box_data_stss_t *p_stss = p_box_stss->data.p_stss;
2071 msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
2072 p_track->i_track_ID );
2073 for( unsigned i_index = 0; i_index < p_stss->i_entry_count; i_index++ )
2075 if( i_index >= p_stss->i_entry_count - 1 ||
2076 i_sample < p_stss->i_sample_number[i_index+1] )
2078 unsigned i_sync_sample = p_stss->i_sample_number[i_index];
2079 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
2080 i_sample, i_sync_sample );
2082 if( i_sync_sample <= i_sample )
2084 while( i_chunk > 0 &&
2085 i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
2090 while( i_chunk < p_track->i_chunk_count - 1 &&
2091 i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
2092 p_track->chunk[i_chunk].i_sample_count )
2095 i_sample = i_sync_sample;
2102 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
2103 "Sample Box (stss)", p_track->i_track_ID );
2106 *pi_chunk = i_chunk;
2107 *pi_sample = i_sample;
2112 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
2113 unsigned int i_chunk, unsigned int i_sample )
2115 bool b_reselect = false;
2117 /* now see if actual es is ok */
2118 if( p_track->i_chunk >= p_track->i_chunk_count ||
2119 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
2120 p_track->chunk[i_chunk].i_sample_description_index )
2122 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
2123 p_track->i_track_ID );
2125 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
2126 p_track->p_es, &b_reselect );
2128 es_out_Del( p_demux->out, p_track->p_es );
2130 p_track->p_es = NULL;
2132 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2134 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2135 p_track->i_track_ID );
2137 p_track->b_ok = false;
2138 p_track->b_selected = false;
2139 return VLC_EGENERIC;
2143 /* select again the new decoder */
2146 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2149 p_track->i_chunk = i_chunk;
2150 p_track->i_sample = i_sample;
2152 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2155 /****************************************************************************
2157 ****************************************************************************
2158 * Parse track information and create all needed data to run a track
2159 * If it succeed b_ok is set to 1 else to 0
2160 ****************************************************************************/
2161 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2162 MP4_Box_t *p_box_trak,
2163 bool b_force_enable )
2165 demux_sys_t *p_sys = p_demux->p_sys;
2167 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2168 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2183 /* hint track unsupported */
2185 /* set default value (-> track unusable) */
2186 p_track->b_ok = false;
2187 p_track->b_enable = false;
2188 p_track->b_selected = false;
2189 p_track->b_chapter = false;
2190 p_track->b_mac_encoding = false;
2192 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2199 /* do we launch this track by default ? */
2201 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2202 if( !p_track->b_enable )
2203 p_track->fmt.i_priority = -1;
2205 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2206 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2207 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2211 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2214 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2215 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2217 if( ( !p_mdhd )||( !p_hdlr ) )
2222 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2223 if( !p_track->i_timescale )
2226 if( p_mdhd->data.p_mdhd->i_language_code < 0x800 )
2228 /* We can convert i_language_code into iso 639 code,
2230 strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2231 p_track->b_mac_encoding = true;
2235 for( i = 0; i < 3; i++ )
2236 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2240 switch( p_hdlr->data.p_hdlr->i_handler_type )
2242 case( FOURCC_soun ):
2243 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2247 p_track->fmt.i_cat = AUDIO_ES;
2250 case( FOURCC_vide ):
2251 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2255 p_track->fmt.i_cat = VIDEO_ES;
2258 case( FOURCC_text ):
2259 case( FOURCC_subp ):
2260 case( FOURCC_tx3g ):
2261 case( FOURCC_sbtl ):
2262 p_track->fmt.i_cat = SPU_ES;
2269 p_track->i_elst = 0;
2270 p_track->i_elst_time = 0;
2271 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2273 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2276 msg_Warn( p_demux, "elst box found" );
2277 for( i = 0; i < elst->i_entry_count; i++ )
2279 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2280 "ms) rate=%d.%d", i,
2281 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2282 elst->i_media_time[i] >= 0 ?
2283 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2285 elst->i_media_rate_integer[i],
2286 elst->i_media_rate_fraction[i] );
2293 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2295 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2296 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2301 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2302 p_track->b_drms = p_drms != NULL;
2303 p_track->p_drms = p_track->b_drms ?
2304 p_drms->data.p_sample_soun->p_drms : NULL;
2308 p_drms = MP4_BoxGet( p_track->p_stsd, "drmi" );
2309 p_track->b_drms = p_drms != NULL;
2310 p_track->p_drms = p_track->b_drms ?
2311 p_drms->data.p_sample_vide->p_drms : NULL;
2315 p_track->p_skcr = MP4_BoxGet( p_drms, "sinf/skcr" );
2318 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2320 p_track->fmt.psz_language = strdup( language );
2323 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2326 MP4_Box_t *p_0xa9xxx;
2327 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
2328 p_0xa9xxx = p_0xa9xxx->p_next )
2330 switch( p_0xa9xxx->i_type )
2332 case FOURCC_0xa9nam:
2333 p_track->fmt.psz_description =
2334 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
2340 /* Create chunk index table and sample index table */
2341 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2342 TrackCreateSamplesIndex( p_demux, p_track ) )
2344 return; /* cannot create chunks index */
2347 p_track->i_chunk = 0;
2348 p_track->i_sample = 0;
2350 /* Mark chapter only track */
2351 if( p_sys->p_tref_chap )
2353 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2356 for( i = 0; i < p_chap->i_entry_count; i++ )
2358 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2360 p_track->b_chapter = true;
2361 p_track->b_enable = false;
2368 if( b_force_enable &&
2369 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2371 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2372 p_track->i_track_ID );
2373 p_track->b_enable = true;
2374 p_track->fmt.i_priority = 0;
2377 p_track->p_es = NULL;
2378 if( TrackCreateES( p_demux,
2379 p_track, p_track->i_chunk,
2380 p_track->b_chapter ? NULL : &p_track->p_es ) )
2382 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2383 p_track->i_track_ID );
2386 p_track->b_ok = true;
2390 for( i = 0; i < p_track->i_chunk_count; i++ )
2392 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2393 i, p_track->chunk[i].i_sample_count,
2394 p_track->chunk[i].i_first_dts );
2401 /****************************************************************************
2403 ****************************************************************************
2404 * Destroy a track created by MP4_TrackCreate.
2405 ****************************************************************************/
2406 static void MP4_TrackDestroy( mp4_track_t *p_track )
2408 unsigned int i_chunk;
2410 p_track->b_ok = false;
2411 p_track->b_enable = false;
2412 p_track->b_selected = false;
2414 es_format_Clean( &p_track->fmt );
2416 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2418 if( p_track->chunk )
2420 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2421 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2423 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2424 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2427 FREENULL( p_track->chunk );
2429 if( !p_track->i_sample_size )
2431 FREENULL( p_track->p_sample_size );
2435 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2438 if( !p_track->b_ok || p_track->b_chapter )
2440 return VLC_EGENERIC;
2443 if( p_track->b_selected )
2445 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2446 p_track->i_track_ID );
2450 return MP4_TrackSeek( p_demux, p_track, i_start );
2453 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2455 if( !p_track->b_ok || p_track->b_chapter )
2460 if( !p_track->b_selected )
2462 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2463 p_track->i_track_ID );
2468 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2469 p_track->p_es, false );
2472 p_track->b_selected = false;
2475 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2481 if( !p_track->b_ok || p_track->b_chapter )
2482 return VLC_EGENERIC;
2484 p_track->b_selected = false;
2486 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2487 &i_chunk, &i_sample ) )
2489 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2490 p_track->i_track_ID );
2491 return VLC_EGENERIC;
2494 p_track->b_selected = true;
2496 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2497 p_track->b_selected = true;
2499 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2504 * 3 types: for audio
2507 #define QT_V0_MAX_SAMPLES 1024
2508 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2511 MP4_Box_data_sample_soun_t *p_soun;
2513 if( p_track->i_sample_size == 0 )
2515 /* most simple case */
2516 return p_track->p_sample_size[p_track->i_sample];
2518 if( p_track->fmt.i_cat != AUDIO_ES )
2520 return p_track->i_sample_size;
2523 p_soun = p_track->p_sample->data.p_sample_soun;
2525 if( p_soun->i_qt_version == 1 )
2527 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
2528 if( p_track->fmt.audio.i_blockalign > 1 )
2529 i_samples = p_soun->i_sample_per_packet;
2531 i_size = i_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2533 else if( p_track->i_sample_size > 256 )
2535 /* We do that so we don't read too much data
2536 * (in this case we are likely dealing with compressed data) */
2537 i_size = p_track->i_sample_size;
2541 /* Read a bunch of samples at once */
2542 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2543 ( p_track->i_sample -
2544 p_track->chunk[p_track->i_chunk].i_sample_first );
2546 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2547 i_size = i_samples * p_track->i_sample_size;
2550 //fprintf( stderr, "size=%d\n", i_size );
2554 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2556 unsigned int i_sample;
2559 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2561 if( p_track->i_sample_size )
2563 MP4_Box_data_sample_soun_t *p_soun =
2564 p_track->p_sample->data.p_sample_soun;
2566 if( p_soun->i_qt_version == 0 )
2568 i_pos += ( p_track->i_sample -
2569 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2570 p_track->i_sample_size;
2574 /* we read chunk by chunk unless a blockalign is requested */
2575 if( p_track->fmt.audio.i_blockalign > 1 )
2576 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
2577 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2582 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2583 i_sample < p_track->i_sample; i_sample++ )
2585 i_pos += p_track->p_sample_size[i_sample];
2592 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2594 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2596 MP4_Box_data_sample_soun_t *p_soun;
2598 p_soun = p_track->p_sample->data.p_sample_soun;
2600 if( p_soun->i_qt_version == 1 )
2602 /* we read chunk by chunk unless a blockalign is requested */
2603 if( p_track->fmt.audio.i_blockalign > 1 )
2604 p_track->i_sample += p_soun->i_sample_per_packet;
2606 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
2608 else if( p_track->i_sample_size > 256 )
2610 /* We do that so we don't read too much data
2611 * (in this case we are likely dealing with compressed data) */
2612 p_track->i_sample += 1;
2617 p_track->i_sample += QT_V0_MAX_SAMPLES;
2618 if( p_track->i_sample >
2619 p_track->chunk[p_track->i_chunk].i_sample_first +
2620 p_track->chunk[p_track->i_chunk].i_sample_count )
2623 p_track->chunk[p_track->i_chunk].i_sample_first +
2624 p_track->chunk[p_track->i_chunk].i_sample_count;
2630 p_track->i_sample++;
2633 if( p_track->i_sample >= p_track->i_sample_count )
2634 return VLC_EGENERIC;
2636 /* Have we changed chunk ? */
2637 if( p_track->i_sample >=
2638 p_track->chunk[p_track->i_chunk].i_sample_first +
2639 p_track->chunk[p_track->i_chunk].i_sample_count )
2641 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2642 p_track->i_sample ) )
2644 msg_Warn( p_demux, "track[0x%x] will be disabled "
2645 "(cannot restart decoder)", p_track->i_track_ID );
2646 MP4_TrackUnselect( p_demux, p_track );
2647 return VLC_EGENERIC;
2651 /* Have we changed elst */
2652 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2654 demux_sys_t *p_sys = p_demux->p_sys;
2655 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2656 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2657 p_sys->i_timescale / (int64_t)1000000;
2659 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2660 i_mvt >= p_track->i_elst_time +
2661 elst->i_segment_duration[p_track->i_elst] )
2663 MP4_TrackSetELST( p_demux, p_track,
2664 MP4_TrackGetDTS( p_demux, p_track ) );
2671 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2674 demux_sys_t *p_sys = p_demux->p_sys;
2675 int i_elst_last = tk->i_elst;
2677 /* handle elst (find the correct one) */
2679 tk->i_elst_time = 0;
2680 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2682 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2683 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2685 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2687 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2689 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2693 tk->i_elst_time += i_dur;
2696 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2698 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2699 tk->i_elst = elst->i_entry_count - 1;
2700 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2703 if( elst->i_media_time[tk->i_elst] < 0 )
2706 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2709 if( i_elst_last != tk->i_elst )
2711 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2716 static const char *MP4_ConvertMacCode( uint16_t i_code )
2718 static const struct { const char psz_iso639_1[3]; uint16_t i_code; } p_cvt[] = {
2719 { "en", 0 }, { "fr", 1 }, { "de", 2 }, { "it", 3 }, { "nl", 4 },
2720 { "sv", 5 }, { "es", 6 }, { "da", 7 }, { "pt", 8 }, { "no", 9 },
2721 { "he", 10 }, { "ja", 11 }, { "ar", 12 }, { "fi", 13 }, { "el", 14 },
2722 { "is", 15 }, { "mt", 16 }, { "tr", 17 }, { "hr", 18 }, { "zh", 19 },
2723 { "ur", 20 }, { "hi", 21 }, { "th", 22 }, { "ko", 23 }, { "lt", 24 },
2724 { "pl", 25 }, { "hu", 26 }, { "et", 27 }, { "lv", 28 }, //{ "??", 29 },
2725 { "fo", 30 }, { "fa", 31 }, { "ru", 32 }, { "zh", 33 }, { "nl", 34 },
2726 { "ga", 35 }, { "sq", 36 }, { "ro", 37 }, { "cs", 38 }, { "sk", 39 },
2727 { "sl", 40 }, { "yi", 41 }, { "sr", 42 }, { "mk", 43 }, { "bg", 44 },
2728 { "uk", 45 }, { "be", 46 }, { "uz", 47 }, { "az", 48 }, { "kk", 48 },
2729 { "az", 50 }, { "hy", 51 }, { "ka", 52 }, { "mo", 53 }, { "ky", 54 },
2730 { "tg", 55 }, { "tk", 56 }, { "mn", 57 }, { "mn", 58 }, { "ps", 59 },
2731 { "ku", 60 }, { "ks", 61 }, { "sd", 62 }, { "bo", 63 }, { "ne", 64 },
2732 { "sa", 65 }, { "mr", 66 }, { "bn", 67 }, { "as", 68 }, { "gu", 69 },
2733 { "pa", 70 }, { "or", 71 }, { "ml", 72 }, { "kn", 73 }, { "ta", 74 },
2734 { "te", 75 }, { "si", 76 }, { "my", 77 }, { "km", 78 }, { "lo", 79 },
2735 { "vi", 80 }, { "id", 81 }, { "tl", 82 }, { "ms", 83 }, { "ms", 84 },
2736 { "am", 85 }, { "ti", 86 }, { "om", 87 }, { "so", 88 }, { "sw", 89 },
2737 { "rw", 90 }, { "rn", 91 }, { "ny", 92 }, { "mg", 93 }, { "eo", 94 },
2739 { "cy", 128 }, { "eu", 129 },
2740 { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
2741 { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
2742 { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
2743 { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
2748 for( i = 0; *p_cvt[i].psz_iso639_1; i++ )
2750 if( p_cvt[i].i_code == i_code )
2751 return p_cvt[i].psz_iso639_1;