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 */
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 */
162 MP4_Box_t *p_tref_chap;
165 input_title_t *p_title;
168 /*****************************************************************************
169 * Declaration of local function
170 *****************************************************************************/
171 static void MP4_TrackCreate ( demux_t *, mp4_track_t *, MP4_Box_t *, bool b_force_enable );
172 static void MP4_TrackDestroy( mp4_track_t * );
174 static int MP4_TrackSelect ( demux_t *, mp4_track_t *, mtime_t );
175 static void MP4_TrackUnselect(demux_t *, mp4_track_t * );
177 static int MP4_TrackSeek ( demux_t *, mp4_track_t *, mtime_t );
179 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
180 static int MP4_TrackSampleSize( mp4_track_t * );
181 static int MP4_TrackNextSample( demux_t *, mp4_track_t * );
182 static void MP4_TrackSetELST( demux_t *, mp4_track_t *, int64_t );
184 static void MP4_UpdateSeekpoint( demux_t * );
185 static const char *MP4_ConvertMacCode( uint16_t );
187 /* Return time in s of a track */
188 static inline int64_t MP4_TrackGetDTS( demux_t *p_demux, mp4_track_t *p_track )
190 #define chunk p_track->chunk[p_track->i_chunk]
192 unsigned int i_index = 0;
193 unsigned int i_sample = p_track->i_sample - chunk.i_sample_first;
194 int64_t i_dts = chunk.i_first_dts;
196 while( i_sample > 0 )
198 if( i_sample > chunk.p_sample_count_dts[i_index] )
200 i_dts += chunk.p_sample_count_dts[i_index] *
201 chunk.p_sample_delta_dts[i_index];
202 i_sample -= chunk.p_sample_count_dts[i_index];
207 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_SEEK, &b_seekable );
308 msg_Warn( p_demux, "MP4 plugin discarded (unseekable)" );
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 = malloc( sizeof( demux_sys_t ) );
318 memset( p_sys, 0, sizeof( demux_sys_t ) );
320 /* Now load all boxes ( except raw data ) */
321 if( ( p_sys->p_root = MP4_BoxGetRoot( p_demux->s ) ) == NULL )
323 msg_Warn( p_demux, "MP4 plugin discarded (not a valid file)" );
327 MP4_BoxDumpStructure( p_demux->s, p_sys->p_root );
329 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
331 switch( p_ftyp->data.p_ftyp->i_major_brand )
335 "ISO Media file (isom) version %d.",
336 p_ftyp->data.p_ftyp->i_minor_version );
340 "unrecognized major file specification (%4.4s).",
341 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
347 msg_Dbg( p_demux, "file type box missing (assuming ISO Media file)" );
350 /* the file need to have one moov box */
351 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
353 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
357 msg_Err( p_demux, "MP4 plugin discarded (no moov box)" );
360 /* we have a free box as a moov, rename it */
361 p_foov->i_type = FOURCC_moov;
364 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
366 int i_count = MP4_BoxCount( p_rmra, "rmda" );
369 msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
371 input_thread_t * p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
372 input_item_t * p_current = input_GetItem( p_input );
373 p_current->i_type = ITEM_TYPE_PLAYLIST;
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 input_item_t *p_input;
419 msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
420 p_input = input_item_NewExt( p_demux, psz_ref, NULL,
422 input_item_CopyOptions( p_current, p_input );
423 input_item_AddSubItem( p_current, p_input );
424 vlc_gc_decref( p_input );
428 msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
429 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
433 vlc_object_release( p_input );
436 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
440 msg_Err( p_demux, "cannot find /moov/mvhd" );
445 msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
446 p_demux->pf_demux = DemuxRef;
452 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
453 if( p_sys->i_timescale == 0 )
455 msg_Err( p_this, "bad timescale" );
458 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
461 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
463 msg_Err( p_demux, "cannot find any /moov/trak" );
466 msg_Dbg( p_demux, "found %d track%c",
468 p_sys->i_tracks ? 's':' ' );
470 /* allocate memory */
471 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
472 if( p_sys->track == NULL )
474 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
476 /* Search the first chap reference (like quicktime) and
477 * check that at least 1 stream is enabled */
478 p_sys->p_tref_chap = NULL;
479 b_enabled_es = false;
480 for( i = 0; i < p_sys->i_tracks; i++ )
482 MP4_Box_t *p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
485 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
486 if( p_tkhd && (p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED) )
489 MP4_Box_t *p_chap = MP4_BoxGet( p_trak, "tref/chap", i );
490 if( p_chap && p_chap->data.p_tref_generic->i_entry_count > 0 && !p_sys->p_tref_chap )
491 p_sys->p_tref_chap = p_chap;
494 /* now process each track and extract all usefull information */
495 for( i = 0; i < p_sys->i_tracks; i++ )
497 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
498 MP4_TrackCreate( p_demux, &p_sys->track[i], p_trak, !b_enabled_es );
500 if( p_sys->track[i].b_ok && !p_sys->track[i].b_chapter )
503 switch( p_sys->track[i].fmt.i_cat )
512 psz_cat = "subtitle";
520 msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
521 p_sys->track[i].i_track_ID, psz_cat,
522 p_sys->track[i].b_enable ? "enable":"disable",
523 p_sys->track[i].fmt.psz_language ?
524 p_sys->track[i].fmt.psz_language : "undef" );
526 else if( p_sys->track[i].b_ok && p_sys->track[i].b_chapter )
528 msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
529 p_sys->track[i].i_track_ID,
530 p_sys->track[i].fmt.psz_language ?
531 p_sys->track[i].fmt.psz_language : "undef" );
535 msg_Dbg( p_demux, "ignoring track[Id 0x%x]",
536 p_sys->track[i].i_track_ID );
541 LoadChapter( p_demux );
548 MP4_BoxFree( p_demux->s, p_sys->p_root );
554 /*****************************************************************************
555 * Demux: read packet and send them to decoders
556 *****************************************************************************
557 * TODO check for newly selected track (ie audio upt to now )
558 *****************************************************************************/
559 static int Demux( demux_t *p_demux )
561 demux_sys_t *p_sys = p_demux->p_sys;
562 unsigned int i_track;
565 unsigned int i_track_selected;
567 /* check for newly selected/unselected track */
568 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks;
571 mp4_track_t *tk = &p_sys->track[i_track];
574 if( !tk->b_ok || tk->b_chapter ||
575 ( tk->b_selected && tk->i_sample >= tk->i_sample_count ) )
580 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
582 if( tk->b_selected && !b )
584 MP4_TrackUnselect( p_demux, tk );
586 else if( !tk->b_selected && b)
588 MP4_TrackSelect( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
597 if( i_track_selected <= 0 )
599 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
600 if( p_sys->i_timescale > 0 )
602 int64_t i_length = (mtime_t)1000000 *
603 (mtime_t)p_sys->i_duration /
604 (mtime_t)p_sys->i_timescale;
605 if( MP4_GetMoviePTS( p_sys ) >= i_length )
610 msg_Warn( p_demux, "no track selected, exiting..." );
615 MP4_UpdateSeekpoint( p_demux );
617 /* first wait for the good time to read a packet */
618 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr + 1 );
620 p_sys->i_pcr = MP4_GetMoviePTS( p_sys );
622 /* we will read 100ms for each stream so ...*/
623 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
625 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
627 mp4_track_t *tk = &p_sys->track[i_track];
629 if( !tk->b_ok || tk->b_chapter || !tk->b_selected || tk->i_sample >= tk->i_sample_count )
632 while( MP4_TrackGetDTS( p_demux, tk ) < MP4_GetMoviePTS( p_sys ) )
635 msg_Dbg( p_demux, "tk(%i)=%lld mv=%lld", i_track,
636 MP4_TrackGetDTS( p_demux, tk ),
637 MP4_GetMoviePTS( p_sys ) );
640 if( MP4_TrackSampleSize( tk ) > 0 )
646 if( stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
648 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
650 MP4_TrackUnselect( p_demux, tk );
656 stream_Block( p_demux->s, MP4_TrackSampleSize(tk) )) )
658 msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)",
660 MP4_TrackUnselect( p_demux, tk );
664 if( tk->b_drms && tk->p_drms )
666 drms_decrypt( tk->p_drms, (uint32_t*)p_block->p_buffer,
669 else if( tk->fmt.i_cat == SPU_ES )
671 if( tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
672 p_block->i_buffer >= 2 )
674 uint16_t i_size = GetWBE( p_block->p_buffer );
676 if( i_size + 2 <= p_block->i_buffer )
679 /* remove the length field, and append a '\0' */
680 memmove( &p_block->p_buffer[0],
681 &p_block->p_buffer[2], i_size );
682 p_block->p_buffer[i_size] = '\0';
683 p_block->i_buffer = i_size + 1;
685 /* convert \r -> \n */
686 while( ( p = strchr((char *) p_block->p_buffer, '\r' ) ) )
694 p_block->i_buffer = 0;
699 p_block->i_dts = MP4_TrackGetDTS( p_demux, tk ) + 1;
701 i_delta = MP4_TrackGetPTSDelta( tk );
703 p_block->i_pts = p_block->i_dts + i_delta;
704 else if( tk->fmt.i_cat != VIDEO_ES )
705 p_block->i_pts = p_block->i_dts;
709 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
710 es_out_Send( p_demux->out, tk->p_es, p_block );
714 if( MP4_TrackNextSample( p_demux, tk ) )
722 static void MP4_UpdateSeekpoint( demux_t *p_demux )
724 demux_sys_t *p_sys = p_demux->p_sys;
727 if( !p_sys->p_title )
729 i_time = MP4_GetMoviePTS( p_sys );
730 for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
732 if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
737 if( i != p_demux->info.i_seekpoint && i >= 0 )
739 p_demux->info.i_seekpoint = i;
740 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
743 /*****************************************************************************
744 * Seek: Got to i_date
745 ******************************************************************************/
746 static int Seek( demux_t *p_demux, mtime_t i_date )
748 demux_sys_t *p_sys = p_demux->p_sys;
749 unsigned int i_track;
751 /* First update update global time */
752 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
753 p_sys->i_pcr = i_date;
755 /* Now for each stream try to go to this time */
756 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
758 mp4_track_t *tk = &p_sys->track[i_track];
759 MP4_TrackSeek( p_demux, tk, i_date );
761 MP4_UpdateSeekpoint( p_demux );
763 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
768 /*****************************************************************************
770 *****************************************************************************/
771 static int Control( demux_t *p_demux, int i_query, va_list args )
773 demux_sys_t *p_sys = p_demux->p_sys;
780 case DEMUX_GET_POSITION:
781 pf = (double*)va_arg( args, double * );
782 if( p_sys->i_duration > 0 )
784 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
792 case DEMUX_SET_POSITION:
793 f = (double)va_arg( args, double );
794 if( p_sys->i_timescale > 0 )
796 i64 = (int64_t)( f * (double)1000000 *
797 (double)p_sys->i_duration /
798 (double)p_sys->i_timescale );
799 return Seek( p_demux, i64 );
801 else return VLC_SUCCESS;
804 pi64 = (int64_t*)va_arg( args, int64_t * );
805 if( p_sys->i_timescale > 0 )
807 *pi64 = (mtime_t)1000000 *
808 (mtime_t)p_sys->i_time /
809 (mtime_t)p_sys->i_timescale;
815 i64 = (int64_t)va_arg( args, int64_t );
816 return Seek( p_demux, i64 );
818 case DEMUX_GET_LENGTH:
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_duration /
824 (mtime_t)p_sys->i_timescale;
830 msg_Warn( p_demux, "DEMUX_GET_FPS unimplemented !!" );
835 vlc_meta_t *p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t*);
836 MP4_Box_t *p_0xa9xxx;
838 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta/meta/ilst" );
841 p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
848 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
849 p_0xa9xxx = p_0xa9xxx->p_next )
852 if( !p_0xa9xxx || !p_0xa9xxx->data.p_0xa9xxx )
855 /* FIXME FIXME: should convert from whatever the character
856 * encoding of MP4 meta data is to UTF-8. */
857 #define SET(fct) do { char *psz_utf = strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text ? p_0xa9xxx->data.p_0xa9xxx->psz_text : "" ); \
858 if( psz_utf ) { EnsureUTF8( psz_utf ); \
859 fct( p_meta, psz_utf ); free( psz_utf ); } } while(0)
861 /* XXX Becarefull p_udta can have box that are not 0xa9xx */
862 switch( p_0xa9xxx->i_type )
864 case FOURCC_0xa9nam: /* Full name */
865 SET( vlc_meta_SetTitle );
868 SET( vlc_meta_SetArtist );
871 SET( vlc_meta_SetArtist );
874 SET( vlc_meta_SetCopyright );
876 case FOURCC_0xa9day: /* Creation Date */
877 SET( vlc_meta_SetDate );
879 case FOURCC_0xa9des: /* Description */
880 SET( vlc_meta_SetDescription );
882 case FOURCC_0xa9gen: /* Genre */
883 SET( vlc_meta_SetGenre );
886 case FOURCC_0xa9alb: /* Album */
887 SET( vlc_meta_SetAlbum );
890 case FOURCC_0xa9trk: /* Track */
891 SET( vlc_meta_SetTracknum );
894 case FOURCC_0xa9cmt: /* Commment */
895 SET( vlc_meta_SetDescription );
898 case FOURCC_0xa9url: /* URL */
899 SET( vlc_meta_SetURL );
902 case FOURCC_0xa9enc: /* Encoded By */
903 SET( vlc_meta_SetEncodedBy );
907 case FOURCC_0xa9inf: /* Information */
908 case FOURCC_0xa9dir: /* Director */
909 case FOURCC_0xa9dis: /* Disclaimer */
910 case FOURCC_0xa9req: /* Requirements */
911 case FOURCC_0xa9fmt: /* Original Format */
912 case FOURCC_0xa9dsa: /* Display Source As */
913 case FOURCC_0xa9hst: /* Host Computer */
914 case FOURCC_0xa9prd: /* Producer */
915 case FOURCC_0xa9prf: /* Performers */
916 case FOURCC_0xa9ope: /* Original Performer */
917 case FOURCC_0xa9src: /* Providers Source Content */
918 case FOURCC_0xa9wrt: /* Writer */
919 case FOURCC_0xa9com: /* Composer */
920 case FOURCC_WLOC: /* Window Location */
921 /* TODO one day, but they aren't really meaningfull */
932 case DEMUX_GET_TITLE_INFO:
934 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
935 int *pi_int = (int*)va_arg( args, int* );
936 int *pi_title_offset = (int*)va_arg( args, int* );
937 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
939 if( !p_sys->p_title )
943 *ppp_title = malloc( sizeof( input_title_t**) );
944 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->p_title );
945 *pi_title_offset = 0;
946 *pi_seekpoint_offset = 0;
949 case DEMUX_SET_TITLE:
951 const int i_title = (int)va_arg( args, int );
952 if( !p_sys->p_title || i_title != 0 )
956 case DEMUX_SET_SEEKPOINT:
958 const int i_seekpoint = (int)va_arg( args, int );
959 if( !p_sys->p_title )
961 return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset );
964 case DEMUX_SET_NEXT_DEMUX_TIME:
965 case DEMUX_SET_GROUP:
966 case DEMUX_HAS_UNSUPPORTED_META:
967 case DEMUX_GET_ATTACHMENTS:
971 msg_Warn( p_demux, "control query %u unimplemented", i_query );
976 /*****************************************************************************
977 * Close: frees unused data
978 *****************************************************************************/
979 static void Close ( vlc_object_t * p_this )
981 demux_t * p_demux = (demux_t *)p_this;
982 demux_sys_t *p_sys = p_demux->p_sys;
983 unsigned int i_track;
985 msg_Dbg( p_demux, "freeing all memory" );
987 MP4_BoxFree( p_demux->s, p_sys->p_root );
988 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
990 MP4_TrackDestroy( &p_sys->track[i_track] );
992 FREENULL( p_sys->track );
995 vlc_input_title_Delete( p_sys->p_title );
1002 /****************************************************************************
1003 * Local functions, specific to vlc
1004 ****************************************************************************/
1006 static void LoadChapterGpac( demux_t *p_demux, MP4_Box_t *p_chpl )
1008 demux_sys_t *p_sys = p_demux->p_sys;
1011 p_sys->p_title = vlc_input_title_New();
1012 for( i = 0; i < p_chpl->data.p_chpl->i_chapter; i++ )
1014 seekpoint_t *s = vlc_seekpoint_New();
1016 s->psz_name = strdup( p_chpl->data.p_chpl->chapter[i].psz_name );
1017 EnsureUTF8( s->psz_name );
1018 s->i_time_offset = p_chpl->data.p_chpl->chapter[i].i_start / 10;
1019 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1022 static void LoadChapterApple( demux_t *p_demux, mp4_track_t *tk )
1024 demux_sys_t *p_sys = p_demux->p_sys;
1026 for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
1028 const int64_t i_dts = MP4_TrackGetDTS( p_demux, tk );
1029 const int64_t i_pts_delta = MP4_TrackGetPTSDelta( tk );
1030 const unsigned int i_size = MP4_TrackSampleSize( tk );
1032 if( i_size > 0 && !stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
1035 const int i_read = stream_Read( p_demux->s, p_buffer, __MIN( sizeof(p_buffer), i_size ) );
1036 const int i_len = __MIN( GetWBE(p_buffer), i_read-2 );
1040 seekpoint_t *s = vlc_seekpoint_New();
1042 s->psz_name = strndup( &p_buffer[2], i_len );
1043 EnsureUTF8( s->psz_name );
1045 s->i_time_offset = i_dts + __MAX( i_pts_delta, 0 );
1047 if( !p_sys->p_title )
1048 p_sys->p_title = vlc_input_title_New();
1049 TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
1052 if( tk->i_sample+1 >= tk->chunk[tk->i_chunk].i_sample_first +
1053 tk->chunk[tk->i_chunk].i_sample_count )
1057 static void LoadChapter( demux_t *p_demux )
1059 demux_sys_t *p_sys = p_demux->p_sys;
1062 if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) && p_chpl->data.p_chpl->i_chapter > 0 )
1064 LoadChapterGpac( p_demux, p_chpl );
1066 else if( p_sys->p_tref_chap )
1068 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
1071 /* Load the first subtitle track like quicktime */
1072 for( i = 0; i < p_chap->i_entry_count; i++ )
1074 for( j = 0; j < p_sys->i_tracks; j++ )
1076 mp4_track_t *tk = &p_sys->track[j];
1077 if( tk->b_ok && tk->i_track_ID == p_chap->i_track_ID[i] &&
1078 tk->fmt.i_cat == SPU_ES && tk->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1081 if( j < p_sys->i_tracks )
1083 LoadChapterApple( p_demux, &p_sys->track[j] );
1090 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
1091 static int TrackCreateChunksIndex( demux_t *p_demux,
1092 mp4_track_t *p_demux_track )
1094 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
1097 unsigned int i_chunk;
1098 unsigned int i_index, i_last;
1100 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
1101 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
1102 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
1104 return( VLC_EGENERIC );
1107 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
1108 if( !p_demux_track->i_chunk_count )
1110 msg_Warn( p_demux, "no chunk defined" );
1111 return( VLC_EGENERIC );
1113 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
1114 sizeof( mp4_chunk_t ) );
1115 if( p_demux_track->chunk == NULL )
1120 /* first we read chunk offset */
1121 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1123 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1125 ck->i_offset = p_co64->data.p_co64->i_chunk_offset[i_chunk];
1127 ck->i_first_dts = 0;
1128 ck->p_sample_count_dts = NULL;
1129 ck->p_sample_delta_dts = NULL;
1130 ck->p_sample_count_pts = NULL;
1131 ck->p_sample_offset_pts = NULL;
1134 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
1135 to be used for the sample XXX begin to 1
1136 We construct it begining at the end */
1137 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
1138 i_index = p_stsc->data.p_stsc->i_entry_count;
1141 msg_Warn( p_demux, "cannot read chunk table or table empty" );
1142 return( VLC_EGENERIC );
1147 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1148 i_chunk < i_last; i_chunk++ )
1150 if( i_chunk >= p_demux_track->i_chunk_count )
1152 msg_Warn( p_demux, "corrupted chunk table" );
1153 return VLC_EGENERIC;
1156 p_demux_track->chunk[i_chunk].i_sample_description_index =
1157 p_stsc->data.p_stsc->i_sample_description_index[i_index];
1158 p_demux_track->chunk[i_chunk].i_sample_count =
1159 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
1161 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
1164 p_demux_track->chunk[0].i_sample_first = 0;
1165 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1167 p_demux_track->chunk[i_chunk].i_sample_first =
1168 p_demux_track->chunk[i_chunk-1].i_sample_first +
1169 p_demux_track->chunk[i_chunk-1].i_sample_count;
1172 msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
1173 p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
1178 static int TrackCreateSamplesIndex( demux_t *p_demux,
1179 mp4_track_t *p_demux_track )
1182 MP4_Box_data_stsz_t *stsz;
1183 MP4_Box_data_stts_t *stts;
1184 /* TODO use also stss and stsh table for seeking */
1185 /* FIXME use edit table */
1190 int64_t i_index_sample_used;
1195 * Gives the sample size for each samples. There is also a stz2 table
1196 * (compressed form) that we need to implement TODO */
1197 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
1200 /* FIXME and stz2 */
1201 msg_Warn( p_demux, "cannot find STSZ box" );
1202 return VLC_EGENERIC;
1204 stsz = p_box->data.p_stsz;
1207 * Gives mapping between sample and decoding time
1209 p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
1212 msg_Warn( p_demux, "cannot find STTS box" );
1213 return VLC_EGENERIC;
1215 stts = p_box->data.p_stts;
1217 /* Use stsz table to create a sample number -> sample size table */
1218 p_demux_track->i_sample_count = stsz->i_sample_count;
1219 if( stsz->i_sample_size )
1221 /* 1: all sample have the same size, so no need to construct a table */
1222 p_demux_track->i_sample_size = stsz->i_sample_size;
1223 p_demux_track->p_sample_size = NULL;
1227 /* 2: each sample can have a different size */
1228 p_demux_track->i_sample_size = 0;
1229 p_demux_track->p_sample_size =
1230 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
1231 if( p_demux_track->p_sample_size == NULL )
1234 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
1236 p_demux_track->p_sample_size[i_sample] =
1237 stsz->i_entry_size[i_sample];
1241 /* Use stts table to create a sample number -> dts table.
1242 * XXX: if we don't want to waste too much memory, we can't expand
1243 * the box! so each chunk will contain an "extract" of this table
1244 * for fast research (problem with raw stream where a sample is sometime
1245 * just channels*bits_per_sample/8 */
1248 i_index = 0; i_index_sample_used = 0;
1249 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1251 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1252 int64_t i_entry, i_sample_count, i;
1255 ck->i_first_dts = i_last_dts;
1257 /* count how many entries are needed for this chunk
1258 * for p_sample_delta_dts and p_sample_count_dts */
1259 i_sample_count = ck->i_sample_count;
1262 while( i_sample_count > 0 )
1264 i_sample_count -= stts->i_sample_count[i_index+i_entry];
1265 /* don't count already used sample in this entry */
1267 i_sample_count += i_index_sample_used;
1273 ck->p_sample_count_dts = calloc( i_entry, sizeof( uint32_t ) );
1274 ck->p_sample_delta_dts = calloc( i_entry, sizeof( uint32_t ) );
1276 if( !ck->p_sample_count_dts || !ck->p_sample_delta_dts )
1280 i_sample_count = ck->i_sample_count;
1281 for( i = 0; i < i_entry; i++ )
1286 i_rest = stts->i_sample_count[i_index] - i_index_sample_used;
1288 i_used = __MIN( i_rest, i_sample_count );
1290 i_index_sample_used += i_used;
1291 i_sample_count -= i_used;
1293 ck->p_sample_count_dts[i] = i_used;
1294 ck->p_sample_delta_dts[i] = stts->i_sample_delta[i_index];
1296 i_last_dts += i_used * ck->p_sample_delta_dts[i];
1298 if( i_index_sample_used >= stts->i_sample_count[i_index] )
1301 i_index_sample_used = 0;
1307 * Gives the delta between decoding time (dts) and composition table (pts)
1309 p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
1312 MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
1314 msg_Warn( p_demux, "CTTS table" );
1316 /* Create pts-dts table per chunk */
1317 i_index = 0; i_index_sample_used = 0;
1318 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
1320 mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
1321 int64_t i_entry, i_sample_count, i;
1323 /* count how many entries are needed for this chunk
1324 * for p_sample_delta_dts and p_sample_count_dts */
1325 i_sample_count = ck->i_sample_count;
1328 while( i_sample_count > 0 )
1330 i_sample_count -= ctts->i_sample_count[i_index+i_entry];
1332 /* don't count already used sample in this entry */
1334 i_sample_count += i_index_sample_used;
1340 ck->p_sample_count_pts = calloc( i_entry, sizeof( uint32_t ) );
1341 ck->p_sample_offset_pts = calloc( i_entry, sizeof( int32_t ) );
1342 if( !ck->p_sample_count_pts || !ck->p_sample_offset_pts )
1346 i_sample_count = ck->i_sample_count;
1347 for( i = 0; i < i_entry; i++ )
1352 i_rest = ctts->i_sample_count[i_index] -
1353 i_index_sample_used;
1355 i_used = __MIN( i_rest, i_sample_count );
1357 i_index_sample_used += i_used;
1358 i_sample_count -= i_used;
1360 ck->p_sample_count_pts[i] = i_used;
1361 ck->p_sample_offset_pts[i] = ctts->i_sample_offset[i_index];
1363 if( i_index_sample_used >= ctts->i_sample_count[i_index] )
1366 i_index_sample_used = 0;
1372 msg_Dbg( p_demux, "track[Id 0x%x] read %d samples length:%"PRId64"s",
1373 p_demux_track->i_track_ID, p_demux_track->i_sample_count,
1374 i_last_dts / p_demux_track->i_timescale );
1381 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
1383 static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
1384 unsigned int i_chunk, es_out_id_t **pp_es )
1386 MP4_Box_t *p_sample;
1393 if( !p_track->chunk[i_chunk].i_sample_description_index )
1395 msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
1396 p_track->i_track_ID );
1397 return VLC_EGENERIC;
1400 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
1401 p_track->chunk[i_chunk].i_sample_description_index - 1 );
1404 ( !p_sample->data.p_data && p_track->fmt.i_cat != SPU_ES ) )
1406 msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
1407 p_track->i_track_ID );
1408 return VLC_EGENERIC;
1411 p_track->p_sample = p_sample;
1413 if( p_track->fmt.i_cat == AUDIO_ES && ( p_track->i_sample_size == 1 || p_track->i_sample_size == 2 ) )
1415 MP4_Box_data_sample_soun_t *p_soun;
1417 p_soun = p_sample->data.p_sample_soun;
1419 if( p_soun->i_qt_version == 0 )
1421 switch( p_sample->i_type )
1423 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
1424 p_soun->i_qt_version = 1;
1425 p_soun->i_sample_per_packet = 64;
1426 p_soun->i_bytes_per_packet = 34;
1427 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
1428 p_soun->i_bytes_per_sample = 2;
1430 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
1431 p_soun->i_qt_version = 1;
1432 p_soun->i_sample_per_packet = 6;
1433 p_soun->i_bytes_per_packet = 2;
1434 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1435 p_soun->i_bytes_per_sample = 2;
1437 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
1438 p_soun->i_qt_version = 1;
1439 p_soun->i_sample_per_packet = 12;
1440 p_soun->i_bytes_per_packet = 2;
1441 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1442 p_soun->i_bytes_per_sample = 2;
1444 case VLC_FOURCC( 'a', 'l', 'a', 'w' ):
1445 case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
1446 p_soun->i_samplesize = 8;
1448 case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
1449 case VLC_FOURCC( 'r', 'a', 'w', ' ' ):
1450 case VLC_FOURCC( 't', 'w', 'o', 's' ):
1451 case VLC_FOURCC( 's', 'o', 'w', 't' ):
1452 /* What would be the fun if you could trust the .mov */
1453 p_track->i_sample_size = ((p_soun->i_samplesize+7)/8) * p_soun->i_channelcount;
1460 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1462 p_soun->i_qt_version = 0;
1466 /* It's a little ugly but .. there are special cases */
1467 switch( p_sample->i_type )
1469 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1470 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1472 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1473 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1474 if( p_track->i_sample_size > 1 )
1475 p_soun->i_qt_version = 0;
1479 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1480 case( VLC_FOURCC( 'N', 'O', 'N', 'E' ) ):
1482 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1484 if(p_soun && (p_soun->i_samplesize+7)/8 == 1 )
1485 p_track->fmt.i_codec = VLC_FOURCC( 'u', '8', ' ', ' ' );
1487 p_track->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1489 /* Buggy files workaround */
1490 if( p_sample->data.p_sample_soun && (p_track->i_timescale !=
1491 p_sample->data.p_sample_soun->i_sampleratehi) )
1493 MP4_Box_data_sample_soun_t *p_soun =
1494 p_sample->data.p_sample_soun;
1496 msg_Warn( p_demux, "i_timescale (%"PRIu64") != i_sampleratehi "
1497 "(%u), making both equal (report any problem).",
1498 p_track->i_timescale, p_soun->i_sampleratehi );
1500 if( p_soun->i_sampleratehi )
1501 p_track->i_timescale = p_soun->i_sampleratehi;
1503 p_soun->i_sampleratehi = p_track->i_timescale;
1508 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1509 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1512 case( VLC_FOURCC( 't', 'e', 'x', 't' ) ):
1513 case( VLC_FOURCC( 't', 'x', '3', 'g' ) ):
1514 p_track->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1515 /* FIXME: Not true, could be UTF-16 with a Byte Order Mark (0xfeff) */
1516 /* FIXME UTF-8 doesn't work here ? */
1517 if( p_track->b_mac_encoding )
1518 p_track->fmt.subs.psz_encoding = strdup( "MAC" );
1520 p_track->fmt.subs.psz_encoding = strdup( "UTF-8" );
1523 case VLC_FOURCC('y','v','1','2'):
1524 p_track->fmt.i_codec = VLC_FOURCC('Y','V','1','2');
1526 case VLC_FOURCC('y','u','v','2'):
1527 p_track->fmt.i_codec = VLC_FOURCC('Y','U','Y','2');
1530 case VLC_FOURCC('i','n','2','4'):
1531 /* in in24/in32 there's enda-atom to tell it's little-endian (if present) */
1532 if( ( MP4_BoxGet( p_sample, "wave/enda" ) ) ||
1533 ( MP4_BoxGet( p_sample, "enda" ) ) )
1535 p_track->fmt.i_codec = VLC_FOURCC('4','2','n','i');
1537 p_track->fmt.i_codec = p_sample->i_type;
1542 p_track->fmt.i_codec = p_sample->i_type;
1546 /* now see if esds is present and if so create a data packet
1547 with decoder_specific_info */
1548 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1549 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1550 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1551 ( p_esds->data.p_esds )&&
1554 /* First update information based on i_objectTypeIndication */
1555 switch( p_decconfig->i_objectTypeIndication )
1557 case( 0x20 ): /* MPEG4 VIDEO */
1558 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1561 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1568 case( 0x65): /* MPEG2 video */
1569 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1571 /* Theses are MPEG2-AAC */
1572 case( 0x66): /* main profile */
1573 case( 0x67): /* Low complexity profile */
1574 case( 0x68): /* Scaleable Sampling rate profile */
1575 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1577 /* true MPEG 2 audio */
1579 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1581 case( 0x6a): /* MPEG1 video */
1582 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1584 case( 0x6b): /* MPEG1 audio */
1585 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1587 case( 0x6c ): /* jpeg */
1588 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1590 case( 0x6d ): /* png */
1591 p_track->fmt.i_codec = VLC_FOURCC( 'p','n','g',' ' );
1593 case( 0x6e ): /* jpeg200 */
1594 p_track->fmt.i_codec = VLC_FOURCC( 'M','J','2','C' );
1596 case( 0xa3 ): /* vc1 */
1597 p_track->fmt.i_codec = VLC_FOURCC( 'W','V','C','1' );
1601 case( 0xe0 ): /* NeroDigital: dvd subs */
1602 if( p_track->fmt.i_cat == SPU_ES )
1604 p_track->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1605 if( p_track->i_width > 0 )
1606 p_track->fmt.subs.spu.i_original_frame_width = p_track->i_width;
1607 if( p_track->i_height > 0 )
1608 p_track->fmt.subs.spu.i_original_frame_height = p_track->i_height;
1611 case( 0xe1 ): /* QCelp for 3gp */
1612 if( p_track->fmt.i_cat == AUDIO_ES )
1614 p_track->fmt.i_codec = VLC_FOURCC( 'Q','c','l','p' );
1620 /* Unknown entry, but don't touch i_fourcc */
1622 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1623 p_decconfig->i_objectTypeIndication,
1624 p_track->i_track_ID );
1627 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1628 if( p_track->fmt.i_extra > 0 )
1630 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1631 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1632 p_track->fmt.i_extra );
1637 switch( p_sample->i_type )
1639 /* qt decoder, send the complete chunk */
1640 case VLC_FOURCC ('h', 'd', 'v', '1'): // HDV 720p30
1641 case VLC_FOURCC ('h', 'd', 'v', '2'): // HDV 1080i60
1642 case VLC_FOURCC ('h', 'd', 'v', '3'): // HDV 1080i50
1643 case VLC_FOURCC ('h', 'd', 'v', '5'): // HDV 720p25
1644 case VLC_FOURCC ('m', 'x', '5', 'n'): // MPEG2 IMX NTSC 525/60 50mb/s produced by FCP
1645 case VLC_FOURCC ('m', 'x', '5', 'p'): // MPEG2 IMX PAL 625/60 50mb/s produced by FCP
1646 case VLC_FOURCC ('m', 'x', '4', 'n'): // MPEG2 IMX NTSC 525/60 40mb/s produced by FCP
1647 case VLC_FOURCC ('m', 'x', '4', 'p'): // MPEG2 IMX PAL 625/60 40mb/s produced by FCP
1648 case VLC_FOURCC ('m', 'x', '3', 'n'): // MPEG2 IMX NTSC 525/60 30mb/s produced by FCP
1649 case VLC_FOURCC ('m', 'x', '3', 'p'): // MPEG2 IMX PAL 625/50 30mb/s produced by FCP
1650 case VLC_FOURCC ('x', 'd', 'v', '2'): // XDCAM HD 1080i60
1651 case VLC_FOURCC ('A', 'V', 'm', 'p'): // AVID IMX PAL
1652 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1654 /* qt decoder, send the complete chunk */
1655 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1656 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1657 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1658 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1659 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1660 p_track->fmt.i_extra =
1661 p_sample->data.p_sample_vide->i_qt_image_description;
1662 if( p_track->fmt.i_extra > 0 )
1664 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1665 memcpy( p_track->fmt.p_extra,
1666 p_sample->data.p_sample_vide->p_qt_image_description,
1667 p_track->fmt.i_extra);
1670 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1671 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1672 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1673 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1674 case VLC_FOURCC( 'a', 'l', 'a', 'c' ):
1675 p_track->fmt.i_extra =
1676 p_sample->data.p_sample_soun->i_qt_description;
1677 if( p_track->fmt.i_extra > 0 )
1679 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1680 memcpy( p_track->fmt.p_extra,
1681 p_sample->data.p_sample_soun->p_qt_description,
1682 p_track->fmt.i_extra);
1686 /* avc1: send avcC (h264 without annexe B, ie without start code)*/
1687 case VLC_FOURCC( 'a', 'v', 'c', '1' ):
1689 MP4_Box_t *p_avcC = MP4_BoxGet( p_sample, "avcC" );
1693 p_track->fmt.i_extra = p_avcC->data.p_avcC->i_avcC;
1694 if( p_track->fmt.i_extra > 0 )
1696 p_track->fmt.p_extra = malloc( p_avcC->data.p_avcC->i_avcC );
1697 memcpy( p_track->fmt.p_extra, p_avcC->data.p_avcC->p_avcC,
1698 p_track->fmt.i_extra );
1703 msg_Err( p_demux, "missing avcC" );
1708 case VLC_FOURCC('m','s',0x00,0x02):
1709 case VLC_FOURCC('m','s',0x00,0x11):
1710 p_track->fmt.audio.i_blockalign = p_sample->data.p_sample_soun->i_bytes_per_frame;
1720 /* some last initialisation */
1721 switch( p_track->fmt.i_cat )
1724 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1725 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1726 p_track->fmt.video.i_bits_per_pixel =
1727 p_sample->data.p_sample_vide->i_depth;
1729 /* fall on display size */
1730 if( p_track->fmt.video.i_width <= 0 )
1731 p_track->fmt.video.i_width = p_track->i_width;
1732 if( p_track->fmt.video.i_height <= 0 )
1733 p_track->fmt.video.i_height = p_track->i_height;
1735 /* Find out apect ratio from display size */
1736 if( p_track->i_width > 0 && p_track->i_height > 0 &&
1737 /* Work-around buggy muxed files */
1738 p_sample->data.p_sample_vide->i_width != p_track->i_width )
1739 p_track->fmt.video.i_aspect =
1740 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1742 /* Support for cropping (eg. in H263 files) */
1743 p_track->fmt.video.i_visible_width = p_track->fmt.video.i_width;
1744 p_track->fmt.video.i_visible_height = p_track->fmt.video.i_height;
1747 p_track->fmt.video.i_frame_rate = p_track->i_timescale;
1748 p_track->fmt.video.i_frame_rate_base = 1;
1750 if( p_track->fmt.video.i_frame_rate &&
1751 (p_box = MP4_BoxGet( p_track->p_stbl, "stts" )) &&
1752 p_box->data.p_stts->i_entry_count >= 1 )
1754 p_track->fmt.video.i_frame_rate_base =
1755 p_box->data.p_stts->i_sample_delta[0];
1761 p_track->fmt.audio.i_channels =
1762 p_sample->data.p_sample_soun->i_channelcount;
1763 p_track->fmt.audio.i_rate =
1764 p_sample->data.p_sample_soun->i_sampleratehi;
1765 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1766 p_sample->data.p_sample_soun->i_sampleratehi *
1767 p_sample->data.p_sample_soun->i_samplesize;
1768 p_track->fmt.audio.i_bitspersample =
1769 p_sample->data.p_sample_soun->i_samplesize;
1771 if( p_track->i_sample_size != 0 &&
1772 p_sample->data.p_sample_soun->i_qt_version == 1 && p_sample->data.p_sample_soun->i_sample_per_packet <= 0 )
1774 msg_Err( p_demux, "Invalid sample per packet value for qt_version 1" );
1775 return VLC_EGENERIC;
1784 *pp_es = es_out_Add( p_demux->out, &p_track->fmt );
1789 /* given a time it return sample/chunk
1790 * it also update elst field of the track
1792 static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
1793 int64_t i_start, uint32_t *pi_chunk,
1794 uint32_t *pi_sample )
1796 demux_sys_t *p_sys = p_demux->p_sys;
1799 unsigned int i_sample;
1800 unsigned int i_chunk;
1803 /* FIXME see if it's needed to check p_track->i_chunk_count */
1804 if( p_track->i_chunk_count == 0 )
1805 return( VLC_EGENERIC );
1807 /* handle elst (find the correct one) */
1808 MP4_TrackSetELST( p_demux, p_track, i_start );
1809 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1811 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1812 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1814 /* now calculate i_start for this elst */
1816 i_start -= p_track->i_elst_time * INT64_C(1000000) / p_sys->i_timescale;
1824 /* to track time scale */
1825 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1826 /* add elst offset */
1827 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1828 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1829 elst->i_media_time[p_track->i_elst] > 0 )
1831 i_start += elst->i_media_time[p_track->i_elst];
1834 msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
1835 "ms (track)", p_track->i_elst,
1836 i_mvt * 1000 / p_sys->i_timescale,
1837 i_start * 1000 / p_track->i_timescale );
1841 /* convert absolute time to in timescale unit */
1842 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1845 /* we start from sample 0/chunk 0, hope it won't take too much time */
1846 /* *** find good chunk *** */
1847 for( i_chunk = 0; ; i_chunk++ )
1849 if( i_chunk + 1 >= p_track->i_chunk_count )
1851 /* at the end and can't check if i_start in this chunk,
1852 it will be check while searching i_sample */
1853 i_chunk = p_track->i_chunk_count - 1;
1857 if( (uint64_t)i_start >= p_track->chunk[i_chunk].i_first_dts &&
1858 (uint64_t)i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1864 /* *** find sample in the chunk *** */
1865 i_sample = p_track->chunk[i_chunk].i_sample_first;
1866 i_dts = p_track->chunk[i_chunk].i_first_dts;
1867 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1870 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1871 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < (uint64_t)i_start )
1874 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1875 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1877 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1882 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1886 i_sample += ( i_start - i_dts ) /
1887 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1892 if( i_sample >= p_track->i_sample_count )
1894 msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
1895 "(seeking too far) chunk=%d sample=%d",
1896 p_track->i_track_ID, i_chunk, i_sample );
1897 return( VLC_EGENERIC );
1901 /* *** Try to find nearest sync points *** */
1902 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1904 unsigned int i_index;
1906 "track[Id 0x%x] using Sync Sample Box (stss)",
1907 p_track->i_track_ID );
1908 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1910 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1914 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1916 p_stss->data.p_stss->i_sample_number[i_index-1] );
1917 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1918 /* new i_sample is less than old so i_chunk can only decreased */
1919 while( i_chunk > 0 &&
1920 i_sample < p_track->chunk[i_chunk].i_sample_first )
1927 msg_Dbg( p_demux, "stts gives %d --> %d (sample number)",
1929 p_stss->data.p_stss->i_sample_number[i_index] );
1930 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1931 /* new i_sample is more than old so i_chunk can only increased */
1932 while( i_chunk < p_track->i_chunk_count - 1 &&
1933 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1934 p_track->chunk[i_chunk].i_sample_count )
1945 msg_Dbg( p_demux, "track[Id 0x%x] does not provide Sync "
1946 "Sample Box (stss)", p_track->i_track_ID );
1949 *pi_chunk = i_chunk;
1950 *pi_sample = i_sample;
1955 static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
1956 unsigned int i_chunk, unsigned int i_sample )
1958 bool b_reselect = false;
1960 /* now see if actual es is ok */
1961 if( (p_track->i_chunk >= p_track->i_chunk_count - 1) ||
1962 (p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1963 p_track->chunk[i_chunk].i_sample_description_index) )
1965 msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
1966 p_track->i_track_ID );
1968 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1969 p_track->p_es, &b_reselect );
1971 es_out_Del( p_demux->out, p_track->p_es );
1973 p_track->p_es = NULL;
1975 if( TrackCreateES( p_demux, p_track, i_chunk, &p_track->p_es ) )
1977 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
1978 p_track->i_track_ID );
1980 p_track->b_ok = false;
1981 p_track->b_selected = false;
1982 return VLC_EGENERIC;
1986 /* select again the new decoder */
1989 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
1992 p_track->i_chunk = i_chunk;
1993 p_track->i_sample = i_sample;
1995 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1998 /****************************************************************************
2000 ****************************************************************************
2001 * Parse track information and create all needed data to run a track
2002 * If it succeed b_ok is set to 1 else to 0
2003 ****************************************************************************/
2004 static void MP4_TrackCreate( demux_t *p_demux, mp4_track_t *p_track,
2005 MP4_Box_t *p_box_trak,
2006 bool b_force_enable )
2008 demux_sys_t *p_sys = p_demux->p_sys;
2010 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
2011 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
2026 /* hint track unsupported */
2028 /* set default value (-> track unusable) */
2029 p_track->b_ok = false;
2030 p_track->b_enable = false;
2031 p_track->b_selected = false;
2032 p_track->b_chapter = false;
2033 p_track->b_mac_encoding = false;
2035 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
2042 /* do we launch this track by default ? */
2044 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
2045 if( !p_track->b_enable )
2046 p_track->fmt.i_priority = -1;
2048 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
2049 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
2050 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
2054 /* msg_Warn( p_demux, "unhandled box: tref --> FIXME" ); */
2057 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
2058 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
2060 if( ( !p_mdhd )||( !p_hdlr ) )
2065 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
2066 if( !p_track->i_timescale )
2069 if( p_mdhd->data.p_mdhd->i_language_code < 0x800 )
2071 /* We can convert i_language_code into iso 639 code,
2073 strcpy( language, MP4_ConvertMacCode( p_mdhd->data.p_mdhd->i_language_code ) );
2074 p_track->b_mac_encoding = true;
2078 for( i = 0; i < 3; i++ )
2079 language[i] = p_mdhd->data.p_mdhd->i_language[i];
2083 switch( p_hdlr->data.p_hdlr->i_handler_type )
2085 case( FOURCC_soun ):
2086 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
2090 p_track->fmt.i_cat = AUDIO_ES;
2093 case( FOURCC_vide ):
2094 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
2098 p_track->fmt.i_cat = VIDEO_ES;
2101 case( FOURCC_text ):
2102 case( FOURCC_subp ):
2103 case( FOURCC_tx3g ):
2104 p_track->fmt.i_cat = SPU_ES;
2111 p_track->i_elst = 0;
2112 p_track->i_elst_time = 0;
2113 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
2115 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
2118 msg_Warn( p_demux, "elst box found" );
2119 for( i = 0; i < elst->i_entry_count; i++ )
2121 msg_Dbg( p_demux, " - [%d] duration=%"PRId64"ms media time=%"PRId64
2122 "ms) rate=%d.%d", i,
2123 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
2124 elst->i_media_time[i] >= 0 ?
2125 (int64_t)(elst->i_media_time[i] * 1000 / p_track->i_timescale) :
2127 elst->i_media_rate_integer[i],
2128 elst->i_media_rate_fraction[i] );
2135 p_dinf = MP4_BoxGet( p_minf, "dinf" );
2137 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
2138 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
2143 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
2144 p_track->b_drms = p_drms != NULL;
2145 p_track->p_drms = p_track->b_drms ?
2146 p_drms->data.p_sample_soun->p_drms : NULL;
2149 if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
2151 p_track->fmt.psz_language = strdup( language );
2154 p_udta = MP4_BoxGet( p_box_trak, "udta" );
2157 MP4_Box_t *p_0xa9xxx;
2158 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
2159 p_0xa9xxx = p_0xa9xxx->p_next )
2161 switch( p_0xa9xxx->i_type )
2163 case FOURCC_0xa9nam:
2164 p_track->fmt.psz_description =
2165 strdup( p_0xa9xxx->data.p_0xa9xxx->psz_text );
2171 /* Create chunk index table and sample index table */
2172 if( TrackCreateChunksIndex( p_demux,p_track ) ||
2173 TrackCreateSamplesIndex( p_demux, p_track ) )
2175 return; /* cannot create chunks index */
2178 p_track->i_chunk = 0;
2179 p_track->i_sample = 0;
2181 /* Mark chapter only track */
2182 if( p_sys->p_tref_chap )
2184 MP4_Box_data_tref_generic_t *p_chap = p_sys->p_tref_chap->data.p_tref_generic;
2187 for( i = 0; i < p_chap->i_entry_count; i++ )
2189 if( p_track->i_track_ID == p_chap->i_track_ID[i] )
2191 p_track->b_chapter = true;
2192 p_track->b_enable = false;
2199 if( b_force_enable &&
2200 ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
2202 msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
2203 p_track->i_track_ID );
2204 p_track->b_enable = true;
2205 p_track->fmt.i_priority = 0;
2208 p_track->p_es = NULL;
2209 if( TrackCreateES( p_demux,
2210 p_track, p_track->i_chunk,
2211 p_track->b_chapter ? NULL : &p_track->p_es ) )
2213 msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
2214 p_track->i_track_ID );
2217 p_track->b_ok = true;
2221 for( i = 0; i < p_track->i_chunk_count; i++ )
2223 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n",
2224 i, p_track->chunk[i].i_sample_count,
2225 p_track->chunk[i].i_first_dts );
2232 /****************************************************************************
2234 ****************************************************************************
2235 * Destroy a track created by MP4_TrackCreate.
2236 ****************************************************************************/
2237 static void MP4_TrackDestroy( mp4_track_t *p_track )
2239 unsigned int i_chunk;
2241 p_track->b_ok = false;
2242 p_track->b_enable = false;
2243 p_track->b_selected = false;
2245 es_format_Clean( &p_track->fmt );
2247 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
2249 if( p_track->chunk )
2251 FREENULL(p_track->chunk[i_chunk].p_sample_count_dts);
2252 FREENULL(p_track->chunk[i_chunk].p_sample_delta_dts );
2254 FREENULL(p_track->chunk[i_chunk].p_sample_count_pts);
2255 FREENULL(p_track->chunk[i_chunk].p_sample_offset_pts );
2258 FREENULL( p_track->chunk );
2260 if( !p_track->i_sample_size )
2262 FREENULL( p_track->p_sample_size );
2266 static int MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track,
2269 if( !p_track->b_ok || p_track->b_chapter )
2271 return VLC_EGENERIC;
2274 if( p_track->b_selected )
2276 msg_Warn( p_demux, "track[Id 0x%x] already selected",
2277 p_track->i_track_ID );
2281 return MP4_TrackSeek( p_demux, p_track, i_start );
2284 static void MP4_TrackUnselect( demux_t *p_demux, mp4_track_t *p_track )
2286 if( !p_track->b_ok || p_track->b_chapter )
2291 if( !p_track->b_selected )
2293 msg_Warn( p_demux, "track[Id 0x%x] already unselected",
2294 p_track->i_track_ID );
2299 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
2300 p_track->p_es, false );
2303 p_track->b_selected = false;
2306 static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
2312 if( !p_track->b_ok || p_track->b_chapter )
2313 return VLC_EGENERIC;
2315 p_track->b_selected = false;
2317 if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
2318 &i_chunk, &i_sample ) )
2320 msg_Warn( p_demux, "cannot select track[Id 0x%x]",
2321 p_track->i_track_ID );
2322 return VLC_EGENERIC;
2325 p_track->b_selected = true;
2327 if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
2328 p_track->b_selected = true;
2330 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
2335 * 3 types: for audio
2338 #define QT_V0_MAX_SAMPLES 1024
2339 static int MP4_TrackSampleSize( mp4_track_t *p_track )
2342 MP4_Box_data_sample_soun_t *p_soun;
2344 if( p_track->i_sample_size == 0 )
2346 /* most simple case */
2347 return p_track->p_sample_size[p_track->i_sample];
2349 if( p_track->fmt.i_cat != AUDIO_ES )
2351 return p_track->i_sample_size;
2354 p_soun = p_track->p_sample->data.p_sample_soun;
2356 if( p_soun->i_qt_version == 1 )
2358 i_size = p_track->chunk[p_track->i_chunk].i_sample_count /
2359 p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
2361 else if( p_track->i_sample_size > 256 )
2363 /* We do that so we don't read too much data
2364 * (in this case we are likely dealing with compressed data) */
2365 i_size = p_track->i_sample_size;
2369 /* Read a bunch of samples at once */
2370 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
2371 ( p_track->i_sample -
2372 p_track->chunk[p_track->i_chunk].i_sample_first );
2374 i_samples = __MIN( QT_V0_MAX_SAMPLES, i_samples );
2375 i_size = i_samples * p_track->i_sample_size;
2378 //fprintf( stderr, "size=%d\n", i_size );
2382 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
2384 unsigned int i_sample;
2387 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
2389 if( p_track->i_sample_size )
2391 MP4_Box_data_sample_soun_t *p_soun =
2392 p_track->p_sample->data.p_sample_soun;
2394 if( p_soun->i_qt_version == 0 )
2396 i_pos += ( p_track->i_sample -
2397 p_track->chunk[p_track->i_chunk].i_sample_first ) *
2398 p_track->i_sample_size;
2402 /* we read chunk by chunk */
2408 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
2409 i_sample < p_track->i_sample; i_sample++ )
2411 i_pos += p_track->p_sample_size[i_sample];
2418 static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track )
2420 if( p_track->fmt.i_cat == AUDIO_ES && p_track->i_sample_size != 0 )
2422 MP4_Box_data_sample_soun_t *p_soun;
2424 p_soun = p_track->p_sample->data.p_sample_soun;
2426 if( p_soun->i_qt_version == 1 )
2428 /* chunk by chunk */
2430 p_track->chunk[p_track->i_chunk].i_sample_first +
2431 p_track->chunk[p_track->i_chunk].i_sample_count;
2433 else if( p_track->i_sample_size > 256 )
2435 /* We do that so we don't read too much data
2436 * (in this case we are likely dealing with compressed data) */
2437 p_track->i_sample += 1;
2442 p_track->i_sample += QT_V0_MAX_SAMPLES;
2443 if( p_track->i_sample >
2444 p_track->chunk[p_track->i_chunk].i_sample_first +
2445 p_track->chunk[p_track->i_chunk].i_sample_count )
2448 p_track->chunk[p_track->i_chunk].i_sample_first +
2449 p_track->chunk[p_track->i_chunk].i_sample_count;
2455 p_track->i_sample++;
2458 if( p_track->i_sample >= p_track->i_sample_count )
2459 return VLC_EGENERIC;
2461 /* Have we changed chunk ? */
2462 if( p_track->i_sample >=
2463 p_track->chunk[p_track->i_chunk].i_sample_first +
2464 p_track->chunk[p_track->i_chunk].i_sample_count )
2466 if( TrackGotoChunkSample( p_demux, p_track, p_track->i_chunk + 1,
2467 p_track->i_sample ) )
2469 msg_Warn( p_demux, "track[0x%x] will be disabled "
2470 "(cannot restart decoder)", p_track->i_track_ID );
2471 MP4_TrackUnselect( p_demux, p_track );
2472 return VLC_EGENERIC;
2476 /* Have we changed elst */
2477 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
2479 demux_sys_t *p_sys = p_demux->p_sys;
2480 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
2481 uint64_t i_mvt = MP4_TrackGetDTS( p_demux, p_track ) *
2482 p_sys->i_timescale / (int64_t)1000000;
2484 if( (unsigned int)p_track->i_elst < elst->i_entry_count &&
2485 i_mvt >= p_track->i_elst_time +
2486 elst->i_segment_duration[p_track->i_elst] )
2488 MP4_TrackSetELST( p_demux, p_track,
2489 MP4_TrackGetDTS( p_demux, p_track ) );
2496 static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
2499 demux_sys_t *p_sys = p_demux->p_sys;
2500 int i_elst_last = tk->i_elst;
2502 /* handle elst (find the correct one) */
2504 tk->i_elst_time = 0;
2505 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
2507 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
2508 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
2510 for( tk->i_elst = 0; (unsigned int)tk->i_elst < elst->i_entry_count; tk->i_elst++ )
2512 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
2514 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
2518 tk->i_elst_time += i_dur;
2521 if( (unsigned int)tk->i_elst >= elst->i_entry_count )
2523 /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
2524 tk->i_elst = elst->i_entry_count - 1;
2525 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
2528 if( elst->i_media_time[tk->i_elst] < 0 )
2531 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
2534 if( i_elst_last != tk->i_elst )
2536 msg_Warn( p_demux, "elst old=%d new=%d", i_elst_last, tk->i_elst );
2541 static const char *MP4_ConvertMacCode( uint16_t i_code )
2543 static const struct { const char *psz_iso639_1; uint16_t i_code; } p_cvt[] = {
2544 { "en", 0 }, { "fr", 1 }, { "de", 2 }, { "it", 3 }, { "nl", 4 },
2545 { "sv", 5 }, { "es", 6 }, { "da", 7 }, { "pt", 8 }, { "no", 9 },
2546 { "he", 10 }, { "ja", 11 }, { "ar", 12 }, { "fi", 13 }, { "el", 14 },
2547 { "is", 15 }, { "mt", 16 }, { "tr", 17 }, { "hr", 18 }, { "zh", 19 },
2548 { "ur", 20 }, { "hi", 21 }, { "th", 22 }, { "ko", 23 }, { "lt", 24 },
2549 { "pl", 25 }, { "hu", 26 }, { "et", 27 }, { "lv", 28 }, //{ "??", 29 },
2550 { "fo", 30 }, { "fa", 31 }, { "ru", 32 }, { "zh", 33 }, { "nl", 34 },
2551 { "ga", 35 }, { "sq", 36 }, { "ro", 37 }, { "cs", 38 }, { "sk", 39 },
2552 { "sl", 40 }, { "yi", 41 }, { "sr", 42 }, { "mk", 43 }, { "bg", 44 },
2553 { "uk", 45 }, { "be", 46 }, { "uz", 47 }, { "az", 48 }, { "kk", 48 },
2554 { "az", 50 }, { "hy", 51 }, { "ka", 52 }, { "mo", 53 }, { "ky", 54 },
2555 { "tg", 55 }, { "tk", 56 }, { "mn", 57 }, { "mn", 58 }, { "ps", 59 },
2556 { "ku", 60 }, { "ks", 61 }, { "sd", 62 }, { "bo", 63 }, { "ne", 64 },
2557 { "sa", 65 }, { "mr", 66 }, { "bn", 67 }, { "as", 68 }, { "gu", 69 },
2558 { "pa", 70 }, { "or", 71 }, { "ml", 72 }, { "kn", 73 }, { "ta", 74 },
2559 { "te", 75 }, { "si", 76 }, { "my", 77 }, { "km", 78 }, { "lo", 79 },
2560 { "vi", 80 }, { "id", 81 }, { "tl", 82 }, { "ms", 83 }, { "ms", 84 },
2561 { "am", 85 }, { "ti", 86 }, { "om", 87 }, { "so", 88 }, { "sw", 89 },
2562 { "rw", 90 }, { "rn", 91 }, { "ny", 92 }, { "mg", 93 }, { "eo", 94 },
2564 { "cy", 128 }, { "eu", 129 },
2565 { "ca", 130 }, { "la", 131 }, { "qu", 132 }, { "gn", 133 }, { "ay", 134 },
2566 { "tt", 135 }, { "ug", 136 }, { "dz", 137 }, { "jv", 138 }, { "su", 139 },
2567 { "gl", 140 }, { "af", 141 }, { "br", 142 }, { "iu", 143 }, { "gd", 144 },
2568 { "gv", 145 }, { "ga", 146 }, { "to", 147 }, { "el", 148 },
2573 for( i = 0; p_cvt[i].psz_iso639_1 != NULL; i++ )
2575 if( p_cvt[i].i_code == i_code )
2576 return p_cvt[i].psz_iso639_1;