1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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>
46 #define uint64_t int64_t
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
56 set_category( CAT_INPUT )
57 set_subcategory( SUBCAT_INPUT_DEMUX )
58 set_description( N_("MP4 stream demuxer") )
59 set_capability( "demux", 242 )
60 set_callbacks( Open, Close )
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Demux ( demux_t * );
67 static int DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
68 static int Seek ( demux_t *, mtime_t );
69 static int Control ( demux_t *, int, va_list );
71 /* Contain all information about a chunk */
74 uint64_t i_offset; /* absolute position of this chunk in the file */
75 uint32_t i_sample_description_index; /* index for SampleEntry to use */
76 uint32_t i_sample_count; /* how many samples in this chunk */
77 uint32_t i_sample_first; /* index of the first sample in this chunk */
79 /* now provide way to calculate pts, dts, and offset without to
80 much memory and with fast acces */
82 /* with this we can calculate dts/pts without waste memory */
84 uint32_t *p_sample_count_dts;
85 uint32_t *p_sample_delta_dts; /* dts delta */
87 uint32_t *p_sample_count_pts;
88 int32_t *p_sample_offset_pts; /* pts-dts */
90 /* TODO if needed add pts
91 but quickly *add* support for edts and seeking */
95 /* Contain all needed information for read all track with vlc */
98 unsigned int i_track_ID;/* this should be unique */
100 int b_ok; /* The track is usable */
101 int b_enable; /* is the trak enable by default */
102 bool b_selected; /* is the trak being played */
103 bool b_chapter; /* True when used for chapter only */
110 /* display size only ! */
114 /* more internal data */
115 uint64_t i_timescale; /* time scale for this track only */
118 int i_elst; /* current elst */
119 int64_t i_elst_time; /* current elst start time (in movie time scale)*/
120 MP4_Box_t *p_elst; /* elst (could be NULL) */
122 /* give the next sample to read, i_chunk is to find quickly where
123 the sample is located */
124 uint32_t i_sample; /* next sample to read */
125 uint32_t i_chunk; /* chunk where next sample is stored */
126 /* total count of chunk and sample */
127 uint32_t i_chunk_count;
128 uint32_t i_sample_count;
130 mp4_chunk_t *chunk; /* always defined for each chunk */
132 /* sample size, p_sample_size defined only if i_sample_size == 0
133 else i_sample_size is size for all sample */
134 uint32_t i_sample_size;
135 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
136 too much time to do sumations each time*/
138 MP4_Box_t *p_stbl; /* will contain all timing information */
139 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
140 MP4_Box_t *p_sample;/* point on actual sdsd */
151 MP4_Box_t *p_root; /* container for the whole file */
155 uint64_t i_time; /* time position of the presentation
156 * in movie timescale */
157 uint64_t i_timescale; /* movie time scale */
158 uint64_t i_duration; /* movie duration */
159 unsigned int i_tracks; /* number of tracks */
160 mp4_track_t *track; /* array of track */
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];
216 /* now handle elst */
217 if( p_track->p_elst )
219 demux_sys_t *p_sys = p_demux->p_sys;
220 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
222 /* convert to offset */
223 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
224 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
225 elst->i_media_time[p_track->i_elst] > 0 )
227 i_dts -= elst->i_media_time[p_track->i_elst];
230 /* add i_elst_time */
231 i_dts += p_track->i_elst_time * p_track->i_timescale /
234 if( i_dts < 0 ) i_dts = 0;
237 return INT64_C(1000000) * i_dts / p_track->i_timescale;
240 static inline int64_t MP4_TrackGetPTSDelta( mp4_track_t *p_track )
242 mp4_chunk_t *ck = &p_track->chunk[p_track->i_chunk];
243 unsigned int i_index = 0;
244 unsigned int i_sample = p_track->i_sample - ck->i_sample_first;
246 if( ck->p_sample_count_pts == NULL || ck->p_sample_offset_pts == NULL )
249 for( i_index = 0;; i_index++ )
251 if( i_sample < ck->p_sample_count_pts[i_index] )
252 return ck->p_sample_offset_pts[i_index] * INT64_C(1000000) /
253 (int64_t)p_track->i_timescale;
255 i_sample -= ck->p_sample_count_pts[i_index];
259 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
261 return INT64_C(1000000) * p_sys->i_time / p_sys->i_timescale;
264 static void LoadChapter( demux_t *p_demux );
266 /*****************************************************************************
267 * Open: check file and initializes MP4 structures
268 *****************************************************************************/
269 static int Open( vlc_object_t * p_this )
271 demux_t *p_demux = (demux_t *)p_this;
274 const uint8_t *p_peek;
285 /* A little test to see if it could be a mp4 */
286 if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
288 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
299 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
306 stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_seekable );
309 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
313 /*Set exported functions */
314 p_demux->pf_demux = Demux;
315 p_demux->pf_control = Control;
317 /* create our structure that will contains all data */
318 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
319 memset( p_sys, 0, sizeof( demux_sys_t ) );
321 /* Now load all boxes ( except raw data ) */
322 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
324 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
328 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
330 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
332 switch( p_ftyp->data.p_ftyp->i_major_brand )
336 "ISO Media file (isom) version %d.",
337 p_ftyp->data.p_ftyp->i_minor_version );
341 "unrecognized major file specification (%4.4s).",
342 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
348 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
351 /* the file need to have one moov box */
352 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
354 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
358 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
361 /* we have a free box as a moov, rename it */
362 p_foov->i_type = FOURCC_moov;
365 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
367 int i_count = MP4_BoxCount( p_rmra, "rmda" );
370 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
372 input_thread_t *p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
373 input_item_t *p_current = input_GetItem( p_input );
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_path );
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 psz_ref = psz_absolute;
418 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
419 input_item_t *p_input = input_item_NewExt( p_demux, psz_ref
420 , NULL, 0, NULL, -1 );
421 input_item_CopyOptions( p_current, p_input );
422 input_item_AddSubItem( p_current, p_input );
423 vlc_gc_decref( p_input );
427 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
428 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
432 vlc_object_release( p_input );
435 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
439 msg_Err( p_demux, "cannot find /moov/mvhd" );
444 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
445 p_demux->pf_demux = DemuxRef;
451 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
452 if( p_sys->i_timescale == 0 )
454 msg_Err( p_this, "bad timescale" );
457 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
460 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
462 msg_Err( p_demux, "cannot find any /moov/trak" );
465 msg_Dbg( p_demux, "found %d track%c",
467 p_sys->i_tracks ? 's':' ' );
469 /* allocate memory */
470 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
471 if( p_sys->track == NULL )
473 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
475 /* Search the first chap reference (like quicktime) and
476 * check that at least 1 stream is enabled */
477 p_sys->p_tref_chap = NULL;
478 b_enabled_es = false;
479 for( i = 0; i < p_sys->i_tracks; i++ )
481 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
484 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
485 if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
488 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
489 if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
490 p_sys->p_tref_chap = p_chap;
493 /* now process each track and extract all usefull information */
494 for( i = 0; i < p_sys->i_tracks; i++ )
496 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
497 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
499 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
502 switch( p_sys->track[i].fmt.i_cat )
511 psz_cat = "subtitle";
519 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
520 p_sys->track[i].i_track_ID, psz_cat,
521 p_sys->track[i].b_enable ? "enable":"disable",
522 p_sys->track[i].fmt.psz_language ?
523 p_sys->track[i].fmt.psz_language : "undef" );
525 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
527 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
528 p_sys->track[i].i_track_ID,
529 p_sys->track[i].fmt.psz_language ?
530 p_sys->track[i].fmt.psz_language : "undef" );
534 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
535 p_sys->track[i].i_track_ID );
540 LoadChapter( p_demux );
547 MP4_BoxFree( p_demux->s, p_sys->p_root );
553 /*****************************************************************************
554 * Demux: read packet and send them to decoders
555 *****************************************************************************
556 * TODO check for newly selected track (ie audio upt to now )
557 *****************************************************************************/
558 static int Demux( demux_t *p_demux )
560 demux_sys_t *p_sys = p_demux->p_sys;
561 unsigned int i_track;
564 unsigned int i_track_selected;
566 /* check for newly selected/unselected track */
567 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
570 mp4_track_t *tk = &p_sys->track[i_track];
573 if( !tk->b_ok || tk->b_chapter ||
574 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
579 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
581 if( tk->b_selected && !b )
583 MP4_TrackUnselect( p_demux, tk );
585 else if( !tk->b_selected && b)
587 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
596 if( i_track_selected <= 0 )
598 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
599 if( p_sys->i_timescale > 0 )
601 int64_t i_length = (mtime_t)1000000 *
602 (mtime_t)p_sys->i_duration /
603 (mtime_t)p_sys->i_timescale;
604 if( MP4_GetMoviePTS( p_sys ) >= i_length )
609 msg_Warn( p_demux, "no track selected, exiting..." );
614 MP4_UpdateSeekpoint( p_demux );
616 /* first wait for the good time to read a packet */
617 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
619 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
621 /* we will read 100ms for each stream so ...*/
622 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
624 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
626 mp4_track_t *tk = &p_sys->track[i_track];
628 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
631 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
634 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
635 MP4_TrackGetDTS( p_demux, tk ),
636 MP4_GetMoviePTS( p_sys ) );
639 if( MP4_TrackSampleSize( tk ) > 0 )
645 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
647 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
649 MP4_TrackUnselect( p_demux, tk );
655 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
657 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
659 MP4_TrackUnselect( p_demux, tk );
663 if( tk->b_drms && tk->p_drms )
668 drms_get_p_key( tk->p_drms, p_key );
670 for( int i_pos = tk->p_skcr->data.p_skcr->i_init; i_pos < p_block->i_buffer; )
672 int n = __MIN( tk->p_skcr->data.p_skcr->i_encr, p_block->i_buffer - i_pos );
673 drms_decrypt( tk->p_drms, (uint32_t*)&p_block->p_buffer[i_pos], n, p_key );
675 i_pos += __MIN( tk->p_skcr->data.p_skcr->i_decr, p_block->i_buffer - i_pos );
680 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
681 p_block->i_buffer, NULL );
684 else if( tk->fmt.i_cat == SPU_ES )
686 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
687 p_block->i_buffer >= 2 )
689 uint16_t i_size = GetWBE( p_block->p_buffer );
691 if( i_size + 2 <= p_block->i_buffer )
694 /* remove the length field, and append a '\0' */
695 memmove( &p_block->p_buffer[0],
696 &p_block->p_buffer[2], i_size );
697 p_block->p_buffer[i_size] = '\0';
698 p_block->i_buffer = i_size + 1;
700 /* convert \r -> \n */
701 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
709 p_block->i_buffer = 0;
714 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
716 i_delta = MP4_TrackGetPTSDelta( tk );
718 p_block->i_pts = p_block->i_dts + i_delta;
719 else if( tk->fmt.i_cat != VIDEO_ES )
720 p_block->i_pts = p_block->i_dts;
724 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
725 es_out_Send( p_demux->out, tk->p_es, p_block );
729 if( MP4_TrackNextSample( p_demux, tk ) )
737 static void MP4_UpdateSeekpoint( demux_t *p_demux )
739 demux_sys_t *p_sys = p_demux->p_sys;
742 if( !p_sys->p_title )
744 i_time = MP4_GetMoviePTS( p_sys );
745 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
747 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
752 if( i != p_demux->info.i_seekpoint && i >= 0 )
754 p_demux->info.i_seekpoint = i;
755 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
758 /*****************************************************************************
759 * Seek: Got to i_date
760 ******************************************************************************/
761 static int Seek( demux_t *p_demux, mtime_t i_date )
763 demux_sys_t *p_sys = p_demux->p_sys;
764 unsigned int i_track;
766 /* First update update global time */
767 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
768 p_sys->i_pcr = i_date;
770 /* Now for each stream try to go to this time */
771 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
773 mp4_track_t *tk = &p_sys->track[i_track];
774 MP4_TrackSeek( p_demux, tk, i_date );
776 MP4_UpdateSeekpoint( p_demux );
778 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
783 /*****************************************************************************
785 *****************************************************************************/
786 static int Control( demux_t *p_demux, int i_query, va_list args )
788 demux_sys_t *p_sys = p_demux->p_sys;
795 case DEMUX_GET_POSITION:
796 pf = (double*)va_arg( args, double * );
797 if( p_sys->i_duration > 0 )
799 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
807 case DEMUX_SET_POSITION:
808 f = (double)va_arg( args, double );
809 if( p_sys->i_timescale > 0 )
811 i64 = (int64_t)( f * (double)1000000 *
812 (double)p_sys->i_duration /
813 (double)p_sys->i_timescale );
814 return Seek( p_demux, i64 );
816 else return VLC_SUCCESS;
819 pi64 = (int64_t*)va_arg( args, int64_t * );
820 if( p_sys->i_timescale > 0 )
822 *pi64 = (mtime_t)1000000 *
823 (mtime_t)p_sys->i_time /
824 (mtime_t)p_sys->i_timescale;
830 i64 = (int64_t)va_arg( args, int64_t );
831 return Seek( p_demux, i64 );
833 case DEMUX_GET_LENGTH:
834 pi64 = (int64_t*)va_arg( args, int64_t * );
835 if( p_sys->i_timescale > 0 )
837 *pi64 = (mtime_t)1000000 *
838 (mtime_t)p_sys->i_duration /
839 (mtime_t)p_sys->i_timescale;
845 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
850 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
851 MP4_Box_t *p_0xa9xxx;
853 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
856 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
863 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
864 p_0xa9xxx = p_0xa9xxx->p_next )
867 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
870 /* FIXME FIXME: should convert from whatever the character
871 * encoding of MP4 meta data is to UTF-8. */
872 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
873 if( psz_utf ) { EnsureUTF8( psz_utf ); \
874 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
876 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
877 switch( p_0xa9xxx->i_type )
879 case FOURCC_0xa9nam: /* Full name */
880 SET( vlc_meta_SetTitle );
883 SET( vlc_meta_SetArtist );
886 SET( vlc_meta_SetArtist );
889 SET( vlc_meta_SetCopyright );
891 case FOURCC_0xa9day: /* Creation Date */
892 SET( vlc_meta_SetDate );
894 case FOURCC_0xa9des: /* Description */
895 SET( vlc_meta_SetDescription );
897 case FOURCC_0xa9gen: /* Genre */
898 SET( vlc_meta_SetGenre );
901 case FOURCC_0xa9alb: /* Album */
902 SET( vlc_meta_SetAlbum );
905 case FOURCC_0xa9trk: /* Track */
906 SET( vlc_meta_SetTracknum );
909 case FOURCC_0xa9cmt: /* Commment */
910 SET( vlc_meta_SetDescription );
913 case FOURCC_0xa9url: /* URL */
914 SET( vlc_meta_SetURL );
917 case FOURCC_0xa9enc: /* Encoded By */
918 SET( vlc_meta_SetEncodedBy );
922 case FOURCC_0xa9inf: /* Information */
923 case FOURCC_0xa9dir: /* Director */
924 case FOURCC_0xa9dis: /* Disclaimer */
925 case FOURCC_0xa9req: /* Requirements */
926 case FOURCC_0xa9fmt: /* Original Format */
927 case FOURCC_0xa9dsa: /* Display Source As */
928 case FOURCC_0xa9hst: /* Host Computer */
929 case FOURCC_0xa9prd: /* Producer */
930 case FOURCC_0xa9prf: /* Performers */
931 case FOURCC_0xa9ope: /* Original Performer */
932 case FOURCC_0xa9src: /* Providers Source Content */
933 case FOURCC_0xa9wrt: /* Writer */
934 case FOURCC_0xa9com: /* Composer */
935 case FOURCC_WLOC: /* Window Location */
936 /* TODO one day, but they aren't really meaningfull */
947 case DEMUX_GET_TITLE_INFO:
949 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
950 int *pi_int = (int*)va_arg( args, int* );
951 int *pi_title_offset = (int*)va_arg( args, int* );
952 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
954 if( !p_sys->p_title )
958 *ppp_title = malloc( sizeof( input_title_t**) );
959 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
960 *pi_title_offset = 0;
961 *pi_seekpoint_offset = 0;
964 case DEMUX_SET_TITLE:
966 const int i_title = (int)va_arg( args, int );
967 if( !p_sys->p_title || i_title != 0 )
971 case DEMUX_SET_SEEKPOINT:
973 const int i_seekpoint = (int)va_arg( args, int );
974 if( !p_sys->p_title )
976 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
979 case DEMUX_SET_NEXT_DEMUX_TIME:
980 case DEMUX_SET_GROUP:
981 case DEMUX_HAS_UNSUPPORTED_META:
982 case DEMUX_GET_ATTACHMENTS:
986 msg_Warn( p_demux, "control query %u unimplemented", i_query );
991 /*****************************************************************************
992 * Close: frees unused data
993 *****************************************************************************/
994 static void Close ( vlc_object_t * p_this )
996 demux_t * p_demux = (demux_t *)p_this;
997 demux_sys_t *p_sys = p_demux->p_sys;
998 unsigned int i_track;
1000 msg_Dbg( p_demux, "freeing all memory" );
1002 MP4_BoxFree( p_demux->s, p_sys->p_root );
1003 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1005 MP4_TrackDestroy( &p_sys->track[i_track] );
1007 FREENULL( p_sys->track );
1009 if( p_sys->p_title )
1010 vlc_input_title_Delete( p_sys->p_title );
1017 /****************************************************************************
1018 * Local functions, specific to vlc
1019 ****************************************************************************/
1021 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1023 demux_sys_t *p_sys = p_demux->p_sys;
1026 p_sys->p_title = vlc_input_title_New();
1027 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1029 seekpoint_t *s = vlc_seekpoint_New();
1031 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1032 EnsureUTF8( s->psz_name );
1033 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1034 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1037 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1039 demux_sys_t *p_sys = p_demux->p_sys;
1041 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1043 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1044 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( tk );
1045 const unsigned int i_size = MP4_TrackSampleSize( tk );
1047 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1050 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1051 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1055 seekpoint_t *s = vlc_seekpoint_New();
1057 s->psz_name = strndup( &p_buffer[2], i_len );
1058 EnsureUTF8( s->psz_name );
1060 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1062 if( !p_sys->p_title )
1063 p_sys->p_title = vlc_input_title_New();
1064 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1067 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1068 tk->chunk[tk->i_chunk].i_sample_count )
1072 static void LoadChapter( demux_t *p_demux )
1074 demux_sys_t *p_sys = p_demux->p_sys;
1077 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1079 LoadChapterGpac( p_demux, p_chpl );
1081 else if( p_sys->p_tref_chap )
1083 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1086 /* Load the first subtitle track like quicktime */
1087 for( i = 0; i < p_chap->i_entry_count; i++ )
1089 for( j = 0; j < p_sys->i_tracks; j++ )
1091 mp4_track_t *tk = &p_sys->track[j];
1092 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1093 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1096 if( j < p_sys->i_tracks )
1098 LoadChapterApple( p_demux, &p_sys->track[j] );
1105 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1106 static int TrackCreateChunksIndex( demux_t *p_demux,
1107 mp4_track_t *p_demux_track )
1109 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1112 unsigned int i_chunk;
1113 unsigned int i_index, i_last;
1115 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1116 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1117 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1119 return( VLC_EGENERIC );
1122 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1123 if( !p_demux_track->i_chunk_count )
1125 msg_Warn( p_demux, "no chunk defined" );
1126 return( VLC_EGENERIC );
1128 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1129 sizeof( mp4_chunk_t ) );
1130 if( p_demux_track->chunk == NULL )
1135 /* first we read chunk offset */
1136 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1138 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1140 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1142 ck->i_first_dts = 0;
1143 ck->p_sample_count_dts = NULL;
1144 ck->p_sample_delta_dts = NULL;
1145 ck->p_sample_count_pts = NULL;
1146 ck->p_sample_offset_pts = NULL;
1149 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1150 to be used for the sample XXX begin to 1
1151 We construct it begining at the end */
1152 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1153 i_index = p_stsc->data.p_stsc->i_entry_count;
1156 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1157 return( VLC_EGENERIC );
1162 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1163 i_chunk < i_last; i_chunk++ )
1165 if( i_chunk >= p_demux_track->i_chunk_count )
1167 msg_Warn( p_demux, "corrupted chunk table" );
1168 return VLC_EGENERIC;
1171 p_demux_track->chunk[i_chunk].i_sample_description_index =
1172 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1173 p_demux_track->chunk[i_chunk].i_sample_count =
1174 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1176 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1179 p_demux_track->chunk[0].i_sample_first = 0;
1180 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1182 p_demux_track->chunk[i_chunk].i_sample_first =
1183 p_demux_track->chunk[i_chunk-1].i_sample_first +
1184 p_demux_track->chunk[i_chunk-1].i_sample_count;
1187 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1188 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1193 static int TrackCreateSamplesIndex( demux_t *p_demux,
1194 mp4_track_t *p_demux_track )
1197 MP4_Box_data_stsz_t *stsz;
1198 MP4_Box_data_stts_t *stts;
1199 /* TODO use also stss and stsh table for seeking */
1200 /* FIXME use edit table */
1205 int64_t i_index_sample_used;
1210 * Gives the sample size for each samples. There is also a stz2 table
1211 * (compressed form) that we need to implement TODO */
1212 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1215 /* FIXME and stz2 */
1216 msg_Warn( p_demux, "cannot find STSZ box" );
1217 return VLC_EGENERIC;
1219 stsz = p_box->data.p_stsz;
1222 * Gives mapping between sample and decoding time
1224 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1227 msg_Warn( p_demux, "cannot find STTS box" );
1228 return VLC_EGENERIC;
1230 stts = p_box->data.p_stts;
1232 /* Use stsz table to create a sample number -> sample size table */
1233 p_demux_track->i_sample_count = stsz->i_sample_count;
1234 if( stsz->i_sample_size )
1236 /* 1: all sample have the same size, so no need to construct a table */
1237 p_demux_track->i_sample_size = stsz->i_sample_size;
1238 p_demux_track->p_sample_size = NULL;
1242 /* 2: each sample can have a different size */
1243 p_demux_track->i_sample_size = 0;
1244 p_demux_track->p_sample_size =
1245 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1246 if( p_demux_track->p_sample_size == NULL )
1249 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1251 p_demux_track->p_sample_size[i_sample] =
1252 stsz->i_entry_size[i_sample];
1256 /* Use stts table to create a sample number -> dts table.
1257 * XXX: if we don't want to waste too much memory, we can't expand
1258 * the box! so each chunk will contain an "extract" of this table
1259 * for fast research (problem with raw stream where a sample is sometime
1260 * just channels*bits_per_sample/8 */
1263 i_index = 0; i_index_sample_used = 0;
1264 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1266 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1267 int64_t i_entry, i_sample_count, i;
1270 ck->i_first_dts = i_last_dts;
1272 /* count how many entries are needed for this chunk
1273 * for p_sample_delta_dts and p_sample_count_dts */
1274 i_sample_count = ck->i_sample_count;
1277 while( i_sample_count > 0 )
1279 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1280 /* don't count already used sample in this entry */
1282 i_sample_count += i_index_sample_used;
1288 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1289 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1291 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1295 i_sample_count = ck->i_sample_count;
1296 for( i = 0; i < i_entry; i++ )
1301 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1303 i_used = __MIN( i_rest, i_sample_count );
1305 i_index_sample_used += i_used;
1306 i_sample_count -= i_used;
1308 ck->p_sample_count_dts[i] = i_used;
1309 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1311 i_last_dts += i_used * ck->p_sample_delta_dts[i];
1313 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1316 i_index_sample_used = 0;
1322 * Gives the delta between decoding time (dts) and composition table (pts)
1324 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1327 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1329 msg_Warn( p_demux, "CTTS table" );
1331 /* Create pts-dts table per chunk */
1332 i_index = 0; i_index_sample_used = 0;
1333 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1335 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1336 int64_t i_entry, i_sample_count, i;
1338 /* count how many entries are needed for this chunk
1339 * for p_sample_delta_dts and p_sample_count_dts */
1340 i_sample_count = ck->i_sample_count;
1343 while( i_sample_count > 0 )
1345 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1347 /* don't count already used sample in this entry */
1349 i_sample_count += i_index_sample_used;
1355 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1356 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1357 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1361 i_sample_count = ck->i_sample_count;
1362 for( i = 0; i < i_entry; i++ )
1367 i_rest = ctts->i_sample_count[i_index] -
1368 i_index_sample_used;
1370 i_used = __MIN( i_rest, i_sample_count );
1372 i_index_sample_used += i_used;
1373 i_sample_count -= i_used;
1375 ck->p_sample_count_pts[i] = i_used;
1376 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1378 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1381 i_index_sample_used = 0;
1387 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1388 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1389 i_last_dts / p_demux_track->i_timescale );
1396 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1398 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1399 unsigned int i_chunk, es_out_id_t **pp_es )
1401 MP4_Box_t *p_sample;
1409 if( !p_track->chunk[i_chunk].i_sample_description_index )
1411 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1412 p_track->i_track_ID );
1413 return VLC_EGENERIC;
1416 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1417 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1420 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1422 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1423 p_track->i_track_ID );
1424 return VLC_EGENERIC;
1427 p_track->p_sample = p_sample;
1429 if( ( p_frma = MP4_BoxGet( p_track->p_sample, "sinf/frma" ) ) )
1431 msg_Warn( p_demux, "Original Format Box: %4.4s", (char *)&p_frma->data.p_frma->i_type );
1433 p_sample->i_type = p_frma->data.p_frma->i_type;
1436 if( p_track->fmt.i_cat == AUDIO_ES && ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1438 MP4_Box_data_sample_soun_t *p_soun;
1440 p_soun = p_sample->data.p_sample_soun;
1442 if( p_soun->i_qt_version == 0 )
1444 switch( p_sample->i_type )
1446 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1447 p_soun->i_qt_version = 1;
1448 p_soun->i_sample_per_packet = 64;
1449 p_soun->i_bytes_per_packet = 34;
1450 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1451 p_soun->i_bytes_per_sample = 2;
1453 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1454 p_soun->i_qt_version = 1;
1455 p_soun->i_sample_per_packet = 6;
1456 p_soun->i_bytes_per_packet = 2;
1457 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1458 p_soun->i_bytes_per_sample = 2;
1460 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1461 p_soun->i_qt_version = 1;
1462 p_soun->i_sample_per_packet = 12;
1463 p_soun->i_bytes_per_packet = 2;
1464 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1465 p_soun->i_bytes_per_sample = 2;
1467 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1468 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1469 p_soun->i_samplesize = 8;
1471 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1472 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1473 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1474 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1475 /* What would be the fun if you could trust the .mov */
1476 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1483 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1485 p_soun->i_qt_version = 0;
1489 /* It's a little ugly but .. there are special cases */
1490 switch( p_sample->i_type )
1492 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1493 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1495 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1496 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1497 if( p_track->i_sample_size > 1 )
1498 p_soun->i_qt_version = 0;
1502 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1503 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1505 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1507 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1508 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1510 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1512 /* Buggy files workaround */
1513 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1514 p_sample->data.p_sample_soun->i_sampleratehi) )
1516 MP4_Box_data_sample_soun_t *p_soun =
1517 p_sample->data.p_sample_soun;
1519 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1520 "(%u), making both equal (report any problem).",
1521 p_track->i_timescale, p_soun->i_sampleratehi );
1523 if( p_soun->i_sampleratehi )
1524 p_track->i_timescale = p_soun->i_sampleratehi;
1526 p_soun->i_sampleratehi = p_track->i_timescale;
1531 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1532 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1535 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1536 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1537 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1538 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1539 /* FIXME UTF-8 doesn't work here ? */
1540 if( p_track->b_mac_encoding )
1541 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1543 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1546 case VLC_FOURCC('y','v','1','2'):
1547 p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1549 case VLC_FOURCC('y','u','v','2'):
1550 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1553 case VLC_FOURCC('i','n','2','4'):
1554 /* in in24/in32 there's enda-atom to tell it's little-endian (if present) */
1555 if( ( MP4_BoxGet( p_sample, "wave/enda" ) ) ||
1556 ( MP4_BoxGet( p_sample, "enda" ) ) )
1558 p_track->fmt.i_codec = VLC_FOURCC('4','2','n','i');
1560 p_track->fmt.i_codec = p_sample->i_type;
1565 p_track->fmt.i_codec = p_sample->i_type;
1569 /* now see if esds is present and if so create a data packet
1570 with decoder_specific_info */
1571 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1572 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1573 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1574 ( p_esds->data.p_esds )&&
1577 /* First update information based on i_objectTypeIndication */
1578 switch( p_decconfig->i_objectTypeIndication )
1580 case( 0x20 ): /* MPEG4 VIDEO */
1581 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1584 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1591 case( 0x65): /* MPEG2 video */
1592 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1594 /* Theses are MPEG2-AAC */
1595 case( 0x66): /* main profile */
1596 case( 0x67): /* Low complexity profile */
1597 case( 0x68): /* Scaleable Sampling rate profile */
1598 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1600 /* true MPEG 2 audio */
1602 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1604 case( 0x6a): /* MPEG1 video */
1605 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1607 case( 0x6b): /* MPEG1 audio */
1608 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1610 case( 0x6c ): /* jpeg */
1611 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1613 case( 0x6d ): /* png */
1614 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
1616 case( 0x6e ): /* jpeg200 */
1617 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
1619 case( 0xa3 ): /* vc1 */
1620 p_track->fmt.i_codec = VLC_FOURCC( 'W','V','C','1' );
1624 case( 0xe0 ): /* NeroDigital: dvd subs */
1625 if( p_track->fmt.i_cat == SPU_ES )
1627 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1628 if( p_track->i_width > 0 )
1629 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1630 if( p_track->i_height > 0 )
1631 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1634 case( 0xe1 ): /* QCelp for 3gp */
1635 if( p_track->fmt.i_cat == AUDIO_ES )
1637 p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
1643 /* Unknown entry, but don't touch i_fourcc */
1645 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1646 p_decconfig->i_objectTypeIndication,
1647 p_track->i_track_ID );
1650 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1651 if( p_track->fmt.i_extra > 0 )
1653 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1654 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1655 p_track->fmt.i_extra );
1660 switch( p_sample->i_type )
1662 /* qt decoder, send the complete chunk */
1663 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1664 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1665 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1666 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1667 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1668 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1669 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1670 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1671 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1672 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1673 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1674 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1675 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1677 /* qt decoder, send the complete chunk */
1678 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1679 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1680 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1681 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1682 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1683 p_track->fmt.i_extra =
1684 p_sample->data.p_sample_vide->i_qt_image_description;
1685 if( p_track->fmt.i_extra > 0 )
1687 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1688 memcpy( p_track->fmt.p_extra,
1689 p_sample->data.p_sample_vide->p_qt_image_description,
1690 p_track->fmt.i_extra);
1693 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1694 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1695 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1696 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1697 p_track->fmt.i_extra =
1698 p_sample->data.p_sample_soun->i_qt_description;
1699 if( p_track->fmt.i_extra > 0 )
1701 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1702 memcpy( p_track->fmt.p_extra,
1703 p_sample->data.p_sample_soun->p_qt_description,
1704 p_track->fmt.i_extra);
1708 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1709 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1711 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1715 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1716 if( p_track->fmt.i_extra > 0 )
1718 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1719 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1720 p_track->fmt.i_extra );
1725 msg_Err( p_demux, "missing avcC" );
1730 case VLC_FOURCC('m','s',0x00,0x02):
1731 case VLC_FOURCC('m','s',0x00,0x11):
1732 case VLC_FOURCC('Q','c','l','p'):
1733 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
1743 /* some last initialisation */
1744 switch( p_track->fmt.i_cat )
1747 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1748 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1749 p_track->fmt.video.i_bits_per_pixel =
1750 p_sample->data.p_sample_vide->i_depth;
1752 /* fall on display size */
1753 if( p_track->fmt.video.i_width <= 0 )
1754 p_track->fmt.video.i_width = p_track->i_width;
1755 if( p_track->fmt.video.i_height <= 0 )
1756 p_track->fmt.video.i_height = p_track->i_height;
1758 /* Find out apect ratio from display size */
1759 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1760 /* Work-around buggy muxed files */
1761 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1762 p_track->fmt.video.i_aspect =
1763 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1765 /* Support for cropping (eg. in H263 files) */
1766 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1767 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1770 p_track->fmt.video.i_frame_rate = p_track->i_timescale;
1771 p_track->fmt.video.i_frame_rate_base = 1;
1773 if( p_track->fmt.video.i_frame_rate &&
1774 (p_box = MP4_BoxGet( p_track->p_stbl, "stts" )) &&
1775 p_box->data.p_stts->i_entry_count >= 1 )
1777 p_track->fmt.video.i_frame_rate_base =
1778 p_box->data.p_stts->i_sample_delta[0];
1784 p_track->fmt.audio.i_channels =
1785 p_sample->data.p_sample_soun->i_channelcount;
1786 p_track->fmt.audio.i_rate =
1787 p_sample->data.p_sample_soun->i_sampleratehi;
1788 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1789 p_sample->data.p_sample_soun->i_sampleratehi *
1790 p_sample->data.p_sample_soun->i_samplesize;
1791 p_track->fmt.audio.i_bitspersample =
1792 p_sample->data.p_sample_soun->i_samplesize;
1794 if( p_track->i_sample_size != 0 &&
1795 p_sample->data.p_sample_soun->i_qt_version == 1 && p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1797 msg_Err( p_demux, "Invalid sample per packet value for qt_version 1" );
1798 return VLC_EGENERIC;
1807 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1812 /* given a time it return sample/chunk
1813 * it also update elst field of the track
1815 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1816 int64_t i_start, uint32_t *pi_chunk,
1817 uint32_t *pi_sample )
1819 demux_sys_t *p_sys = p_demux->p_sys;
1822 unsigned int i_sample;
1823 unsigned int i_chunk;
1826 /* FIXME see if it's needed to check p_track->i_chunk_count */
1827 if( p_track->i_chunk_count == 0 )
1828 return( VLC_EGENERIC );
1830 /* handle elst (find the correct one) */
1831 MP4_TrackSetELST( p_demux, p_track, i_start );
1832 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1834 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1835 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1837 /* now calculate i_start for this elst */
1839 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
1847 /* to track time scale */
1848 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1849 /* add elst offset */
1850 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1851 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1852 elst->i_media_time[p_track->i_elst] > 0 )
1854 i_start += elst->i_media_time[p_track->i_elst];
1857 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
1858 "ms (track)", p_track->i_elst,
1859 i_mvt * 1000 / p_sys->i_timescale,
1860 i_start * 1000 / p_track->i_timescale );
1864 /* convert absolute time to in timescale unit */
1865 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1868 /* we start from sample 0/chunk 0, hope it won't take too much time */
1869 /* *** find good chunk *** */
1870 for( i_chunk = 0; ; i_chunk++ )
1872 if( i_chunk + 1 >= p_track->i_chunk_count )
1874 /* at the end and can't check if i_start in this chunk,
1875 it will be check while searching i_sample */
1876 i_chunk = p_track->i_chunk_count - 1;
1880 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
1881 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1887 /* *** find sample in the chunk *** */
1888 i_sample = p_track->chunk[i_chunk].i_sample_first;
1889 i_dts = p_track->chunk[i_chunk].i_first_dts;
1890 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1893 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1894 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
1897 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1898 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1900 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1905 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1909 i_sample += ( i_start - i_dts ) /
1910 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1915 if( i_sample >= p_track->i_sample_count )
1917 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1918 "(seeking too far) chunk=%d sample=%d",
1919 p_track->i_track_ID, i_chunk, i_sample );
1920 return( VLC_EGENERIC );
1924 /* *** Try to find nearest sync points *** */
1925 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1927 unsigned int i_index;
1929 "track[Id 0x%x] using Sync Sample Box (stss)",
1930 p_track->i_track_ID );
1931 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1933 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1937 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1939 p_stss->data.p_stss->i_sample_number[i_index-1] );
1940 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1941 /* new i_sample is less than old so i_chunk can only decreased */
1942 while( i_chunk > 0 &&
1943 i_sample < p_track->chunk[i_chunk].i_sample_first )
1950 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1952 p_stss->data.p_stss->i_sample_number[i_index] );
1953 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1954 /* new i_sample is more than old so i_chunk can only increased */
1955 while( i_chunk < p_track->i_chunk_count - 1 &&
1956 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1957 p_track->chunk[i_chunk].i_sample_count )
1968 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1969 "Sample Box (stss)", p_track->i_track_ID );
1972 *pi_chunk = i_chunk;
1973 *pi_sample = i_sample;
1978 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1979 unsigned int i_chunk, unsigned int i_sample )
1981 bool b_reselect = false;
1983 /* now see if actual es is ok */
1984 if( p_track->i_chunk >= p_track->i_chunk_count ||
1985 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1986 p_track->chunk[i_chunk].i_sample_description_index )
1988 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1989 p_track->i_track_ID );
1991 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1992 p_track->p_es, &b_reselect );
1994 es_out_Del( p_demux->out, p_track->p_es );
1996 p_track->p_es = NULL;
1998 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
2000 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2001 p_track->i_track_ID );
2003 p_track->b_ok = false;
2004 p_track->b_selected = false;
2005 return VLC_EGENERIC;
2009 /* select again the new decoder */
2012 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
2015 p_track->i_chunk = i_chunk;
2016 p_track->i_sample = i_sample;
2018 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2021 /****************************************************************************
2023 ****************************************************************************
2024 * Parse track information and create all needed data to run a track
2025 * If it succeed b_ok is set to 1 else to 0
2026 ****************************************************************************/
2027 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2028 MP4_Box_t *p_box_trak,
2029 bool b_force_enable )
2031 demux_sys_t *p_sys = p_demux->p_sys;
2033 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2034 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2049 /* hint track unsupported */
2051 /* set default value (-> track unusable) */
2052 p_track->b_ok = false;
2053 p_track->b_enable = false;
2054 p_track->b_selected = false;
2055 p_track->b_chapter = false;
2056 p_track->b_mac_encoding = false;
2058 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2065 /* do we launch this track by default ? */
2067 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2068 if( !p_track->b_enable )
2069 p_track->fmt.i_priority = -1;
2071 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2072 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2073 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2077 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2080 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2081 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2083 if( ( !p_mdhd )||( !p_hdlr ) )
2088 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2089 if( !p_track->i_timescale )
2092 if( p_mdhd->data.p_mdhd->i_language_code < 0x800 )
2094 /* We can convert i_language_code into iso 639 code,
2096 strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2097 p_track->b_mac_encoding = true;
2101 for( i = 0; i < 3; i++ )
2102 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2106 switch( p_hdlr->data.p_hdlr->i_handler_type )
2108 case( FOURCC_soun ):
2109 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2113 p_track->fmt.i_cat = AUDIO_ES;
2116 case( FOURCC_vide ):
2117 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2121 p_track->fmt.i_cat = VIDEO_ES;
2124 case( FOURCC_text ):
2125 case( FOURCC_subp ):
2126 case( FOURCC_tx3g ):
2127 case( FOURCC_sbtl ):
2128 p_track->fmt.i_cat = SPU_ES;
2135 p_track->i_elst = 0;
2136 p_track->i_elst_time = 0;
2137 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2139 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2142 msg_Warn( p_demux, "elst box found" );
2143 for( i = 0; i < elst->i_entry_count; i++ )
2145 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2146 "ms) rate=%d.%d", i,
2147 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2148 elst->i_media_time[i] >= 0 ?
2149 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2151 elst->i_media_rate_integer[i],
2152 elst->i_media_rate_fraction[i] );
2159 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2161 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2162 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2167 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2168 p_track->b_drms = p_drms != NULL;
2169 p_track->p_drms = p_track->b_drms ?
2170 p_drms->data.p_sample_soun->p_drms : NULL;
2174 p_drms = MP4_BoxGet( p_track->p_stsd, "drmi" );
2175 p_track->b_drms = p_drms != NULL;
2176 p_track->p_drms = p_track->b_drms ?
2177 p_drms->data.p_sample_vide->p_drms : NULL;
2181 p_track->p_skcr = MP4_BoxGet( p_drms, "sinf/skcr" );
2184 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2186 p_track->fmt.psz_language = strdup( language );
2189 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2192 MP4_Box_t *p_0xa9xxx;
2193 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
2194 p_0xa9xxx = p_0xa9xxx->p_next )
2196 switch( p_0xa9xxx->i_type )
2198 case FOURCC_0xa9nam:
2199 p_track->fmt.psz_description =
2200 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
2206 /* Create chunk index table and sample index table */
2207 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2208 TrackCreateSamplesIndex( p_demux, p_track ) )
2210 return; /* cannot create chunks index */
2213 p_track->i_chunk = 0;
2214 p_track->i_sample = 0;
2216 /* Mark chapter only track */
2217 if( p_sys->p_tref_chap )
2219 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2222 for( i = 0; i < p_chap->i_entry_count; i++ )
2224 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2226 p_track->b_chapter = true;
2227 p_track->b_enable = false;
2234 if( b_force_enable &&
2235 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2237 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2238 p_track->i_track_ID );
2239 p_track->b_enable = true;
2240 p_track->fmt.i_priority = 0;
2243 p_track->p_es = NULL;
2244 if( TrackCreateES( p_demux,
2245 p_track, p_track->i_chunk,
2246 p_track->b_chapter ? NULL : &p_track->p_es ) )
2248 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2249 p_track->i_track_ID );
2252 p_track->b_ok = true;
2256 for( i = 0; i < p_track->i_chunk_count; i++ )
2258 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2259 i, p_track->chunk[i].i_sample_count,
2260 p_track->chunk[i].i_first_dts );
2267 /****************************************************************************
2269 ****************************************************************************
2270 * Destroy a track created by MP4_TrackCreate.
2271 ****************************************************************************/
2272 static void MP4_TrackDestroy( mp4_track_t *p_track )
2274 unsigned int i_chunk;
2276 p_track->b_ok = false;
2277 p_track->b_enable = false;
2278 p_track->b_selected = false;
2280 es_format_Clean( &p_track->fmt );
2282 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2284 if( p_track->chunk )
2286 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2287 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2289 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2290 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2293 FREENULL( p_track->chunk );
2295 if( !p_track->i_sample_size )
2297 FREENULL( p_track->p_sample_size );
2301 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2304 if( !p_track->b_ok || p_track->b_chapter )
2306 return VLC_EGENERIC;
2309 if( p_track->b_selected )
2311 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2312 p_track->i_track_ID );
2316 return MP4_TrackSeek( p_demux, p_track, i_start );
2319 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2321 if( !p_track->b_ok || p_track->b_chapter )
2326 if( !p_track->b_selected )
2328 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2329 p_track->i_track_ID );
2334 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2335 p_track->p_es, false );
2338 p_track->b_selected = false;
2341 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2347 if( !p_track->b_ok || p_track->b_chapter )
2348 return VLC_EGENERIC;
2350 p_track->b_selected = false;
2352 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2353 &i_chunk, &i_sample ) )
2355 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2356 p_track->i_track_ID );
2357 return VLC_EGENERIC;
2360 p_track->b_selected = true;
2362 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2363 p_track->b_selected = true;
2365 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2370 * 3 types: for audio
2373 #define QT_V0_MAX_SAMPLES 1024
2374 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2377 MP4_Box_data_sample_soun_t *p_soun;
2379 if( p_track->i_sample_size == 0 )
2381 /* most simple case */
2382 return p_track->p_sample_size[p_track->i_sample];
2384 if( p_track->fmt.i_cat != AUDIO_ES )
2386 return p_track->i_sample_size;
2389 p_soun = p_track->p_sample->data.p_sample_soun;
2391 if( p_soun->i_qt_version == 1 )
2393 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count;
2394 if( p_track->fmt.audio.i_blockalign > 1 )
2395 i_samples = p_soun->i_sample_per_packet;
2397 i_size = i_samples / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2399 else if( p_track->i_sample_size > 256 )
2401 /* We do that so we don't read too much data
2402 * (in this case we are likely dealing with compressed data) */
2403 i_size = p_track->i_sample_size;
2407 /* Read a bunch of samples at once */
2408 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2409 ( p_track->i_sample -
2410 p_track->chunk[p_track->i_chunk].i_sample_first );
2412 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2413 i_size = i_samples * p_track->i_sample_size;
2416 //fprintf( stderr, "size=%d\n", i_size );
2420 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2422 unsigned int i_sample;
2425 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2427 if( p_track->i_sample_size )
2429 MP4_Box_data_sample_soun_t *p_soun =
2430 p_track->p_sample->data.p_sample_soun;
2432 if( p_soun->i_qt_version == 0 )
2434 i_pos += ( p_track->i_sample -
2435 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2436 p_track->i_sample_size;
2440 /* we read chunk by chunk unless a blockalign is requested */
2441 if( p_track->fmt.audio.i_blockalign > 1 )
2442 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) /
2443 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2448 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2449 i_sample < p_track->i_sample; i_sample++ )
2451 i_pos += p_track->p_sample_size[i_sample];
2458 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2460 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2462 MP4_Box_data_sample_soun_t *p_soun;
2464 p_soun = p_track->p_sample->data.p_sample_soun;
2466 if( p_soun->i_qt_version == 1 )
2468 /* we read chunk by chunk unless a blockalign is requested */
2469 if( p_track->fmt.audio.i_blockalign > 1 )
2470 p_track->i_sample += p_soun->i_sample_per_packet;
2472 p_track->i_sample += p_track->chunk[p_track->i_chunk].i_sample_count;
2474 else if( p_track->i_sample_size > 256 )
2476 /* We do that so we don't read too much data
2477 * (in this case we are likely dealing with compressed data) */
2478 p_track->i_sample += 1;
2483 p_track->i_sample += QT_V0_MAX_SAMPLES;
2484 if( p_track->i_sample >
2485 p_track->chunk[p_track->i_chunk].i_sample_first +
2486 p_track->chunk[p_track->i_chunk].i_sample_count )
2489 p_track->chunk[p_track->i_chunk].i_sample_first +
2490 p_track->chunk[p_track->i_chunk].i_sample_count;
2496 p_track->i_sample++;
2499 if( p_track->i_sample >= p_track->i_sample_count )
2500 return VLC_EGENERIC;
2502 /* Have we changed chunk ? */
2503 if( p_track->i_sample >=
2504 p_track->chunk[p_track->i_chunk].i_sample_first +
2505 p_track->chunk[p_track->i_chunk].i_sample_count )
2507 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2508 p_track->i_sample ) )
2510 msg_Warn( p_demux, "track[0x%x] will be disabled "
2511 "(cannot restart decoder)", p_track->i_track_ID );
2512 MP4_TrackUnselect( p_demux, p_track );
2513 return VLC_EGENERIC;
2517 /* Have we changed elst */
2518 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2520 demux_sys_t *p_sys = p_demux->p_sys;
2521 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2522 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2523 p_sys->i_timescale / (int64_t)1000000;
2525 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2526 i_mvt >= p_track->i_elst_time +
2527 elst->i_segment_duration[p_track->i_elst] )
2529 MP4_TrackSetELST( p_demux, p_track,
2530 MP4_TrackGetDTS( p_demux, p_track ) );
2537 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2540 demux_sys_t *p_sys = p_demux->p_sys;
2541 int i_elst_last = tk->i_elst;
2543 /* handle elst (find the correct one) */
2545 tk->i_elst_time = 0;
2546 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2548 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2549 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2551 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2553 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2555 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2559 tk->i_elst_time += i_dur;
2562 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2564 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2565 tk->i_elst = elst->i_entry_count - 1;
2566 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2569 if( elst->i_media_time[tk->i_elst] < 0 )
2572 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2575 if( i_elst_last != tk->i_elst )
2577 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2582 static const char *MP4_ConvertMacCode( uint16_t i_code )
2584 static const struct { const char *psz_iso639_1; uint16_t i_code; } p_cvt[] = {
2585 { "en", 0 }, { "fr", 1 }, { "de", 2 }, { "it", 3 }, { "nl", 4 },
2586 { "sv", 5 }, { "es", 6 }, { "da", 7 }, { "pt", 8 }, { "no", 9 },
2587 { "he", 10 }, { "ja", 11 }, { "ar", 12 }, { "fi", 13 }, { "el", 14 },
2588 { "is", 15 }, { "mt", 16 }, { "tr", 17 }, { "hr", 18 }, { "zh", 19 },
2589 { "ur", 20 }, { "hi", 21 }, { "th", 22 }, { "ko", 23 }, { "lt", 24 },
2590 { "pl", 25 }, { "hu", 26 }, { "et", 27 }, { "lv", 28 }, //{ "??", 29 },
2591 { "fo", 30 }, { "fa", 31 }, { "ru", 32 }, { "zh", 33 }, { "nl", 34 },
2592 { "ga", 35 }, { "sq", 36 }, { "ro", 37 }, { "cs", 38 }, { "sk", 39 },
2593 { "sl", 40 }, { "yi", 41 }, { "sr", 42 }, { "mk", 43 }, { "bg", 44 },
2594 { "uk", 45 }, { "be", 46 }, { "uz", 47 }, { "az", 48 }, { "kk", 48 },
2595 { "az", 50 }, { "hy", 51 }, { "ka", 52 }, { "mo", 53 }, { "ky", 54 },
2596 { "tg", 55 }, { "tk", 56 }, { "mn", 57 }, { "mn", 58 }, { "ps", 59 },
2597 { "ku", 60 }, { "ks", 61 }, { "sd", 62 }, { "bo", 63 }, { "ne", 64 },
2598 { "sa", 65 }, { "mr", 66 }, { "bn", 67 }, { "as", 68 }, { "gu", 69 },
2599 { "pa", 70 }, { "or", 71 }, { "ml", 72 }, { "kn", 73 }, { "ta", 74 },
2600 { "te", 75 }, { "si", 76 }, { "my", 77 }, { "km", 78 }, { "lo", 79 },
2601 { "vi", 80 }, { "id", 81 }, { "tl", 82 }, { "ms", 83 }, { "ms", 84 },
2602 { "am", 85 }, { "ti", 86 }, { "om", 87 }, { "so", 88 }, { "sw", 89 },
2603 { "rw", 90 }, { "rn", 91 }, { "ny", 92 }, { "mg", 93 }, { "eo", 94 },
2605 { "cy", 128 }, { "eu", 129 },
2606 { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
2607 { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
2608 { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
2609 { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
2614 for( i = 0; p_cvt[i].psz_iso639_1 != NULL; i++ )
2616 if( p_cvt[i].i_code == i_code )
2617 return p_cvt[i].psz_iso639_1;