1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
45 set_description( _("MP4 stream demuxer") );
46 set_capability( "demux", 242 );
47 set_callbacks( Open, Close );
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Demux ( input_thread_t * );
54 static int DemuxRef( input_thread_t *p_input )
58 static int Seek ( input_thread_t *, mtime_t );
59 static int Control ( input_thread_t *, int, va_list );
61 /*****************************************************************************
62 * Declaration of local function
63 *****************************************************************************/
64 static void MP4_TrackCreate ( input_thread_t *, mp4_track_t *, MP4_Box_t *);
65 static void MP4_TrackDestroy( input_thread_t *, mp4_track_t * );
67 static int MP4_TrackSelect ( input_thread_t *, mp4_track_t *, mtime_t );
68 static void MP4_TrackUnselect(input_thread_t *, mp4_track_t * );
70 static int MP4_TrackSeek ( input_thread_t *, mp4_track_t *, mtime_t );
72 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
73 static int MP4_TrackSampleSize( mp4_track_t * );
74 static int MP4_TrackNextSample( input_thread_t *, mp4_track_t * );
75 static void MP4_TrackSetELST( input_thread_t *, mp4_track_t *, int64_t );
77 /* Return time in µs of a track */
78 static inline int64_t MP4_TrackGetPTS( input_thread_t *p_input, mp4_track_t *p_track )
80 unsigned int i_sample;
84 i_sample = p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first;
85 i_dts = p_track->chunk[p_track->i_chunk].i_first_dts;
89 if( i_sample > p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] )
91 i_dts += p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] *
92 p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
93 i_sample -= p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index];
99 p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
105 /* now handle elst */
106 if( p_track->p_elst )
108 demux_sys_t *p_sys = p_input->p_demux_data;
109 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
111 /* convert to offset */
112 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
113 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
114 elst->i_media_time[p_track->i_elst] > 0 )
116 i_dts -= elst->i_media_time[p_track->i_elst];
119 /* add i_elst_time */
120 i_dts += p_track->i_elst_time * p_track->i_timescale / p_sys->i_timescale;
122 if( i_dts < 0 ) i_dts = 0;
125 return (int64_t)1000000 * i_dts / p_track->i_timescale;
127 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
129 return (int64_t)1000000 * p_sys->i_time / p_sys->i_timescale;
132 #define FREE( p ) if( p ) { free( p ); (p) = NULL;}
134 /*****************************************************************************
135 * Open: check file and initializes MP4 structures
136 *****************************************************************************/
137 static int Open( vlc_object_t * p_this )
139 input_thread_t *p_input = (input_thread_t *)p_this;
150 vlc_bool_t b_seekable;
152 /* a little test to see if it could be a mp4 */
153 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
155 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
158 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
169 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
172 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
177 stream_Control( p_input->s, STREAM_CAN_SEEK, &b_seekable );
180 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
184 /*Set exported functions */
185 p_input->pf_demux = Demux;
186 p_input->pf_demux_control = Control;
188 /* create our structure that will contains all data */
189 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
190 memset( p_sys, 0, sizeof( demux_sys_t ) );
192 /* Now load all boxes ( except raw data ) */
193 if( ( p_sys->p_root = MP4_BoxGetRoot( p_input ) ) == NULL )
195 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
199 MP4_BoxDumpStructure( p_input, p_sys->p_root );
201 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
203 switch( p_ftyp->data.p_ftyp->i_major_brand )
207 "ISO Media file (isom) version %d.",
208 p_ftyp->data.p_ftyp->i_minor_version );
212 "unrecognized major file specification (%4.4s).",
213 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
219 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
222 /* the file need to have one moov box */
223 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
225 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
229 msg_Err( p_input, "MP4 plugin discarded (no moov box)" );
232 /* we have a free box as a moov, rename it */
233 p_foov->i_type = FOURCC_moov;
236 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
238 playlist_t *p_playlist;
239 int i_count = MP4_BoxCount( p_rmra, "rmda" );
242 msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );
245 (playlist_t *)vlc_object_find( p_input,
250 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
251 for( i = 0; i < i_count; i++ )
253 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
257 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
261 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
263 msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
264 psz_ref, (char*)&i_ref_type );
266 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
268 if( strstr( psz_ref, "qt5gateQT" ) )
270 msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
273 if( !strncmp( psz_ref, "http://", 7 ) ||
274 !strncmp( psz_ref, "rtsp://", 7 ) )
276 msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
277 playlist_Add( p_playlist, psz_ref, psz_ref,
278 PLAYLIST_APPEND, PLAYLIST_END );
282 /* msg dbg relative ? */
283 char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
284 char *end = strrchr( p_input->psz_source, '/' );
288 int i_len = end + 1 - p_input->psz_source;
290 strncpy( psz_absolute, p_input->psz_source, i_len);
291 psz_absolute[i_len] = '\0';
295 strcpy( psz_absolute, "" );
297 strcat( psz_absolute, psz_ref );
298 msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
299 playlist_Add( p_playlist, psz_absolute, psz_absolute,
300 PLAYLIST_APPEND, PLAYLIST_END );
305 msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)",
306 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
309 vlc_object_release( p_playlist );
313 msg_Err( p_input, "can't find playlist" );
317 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
321 msg_Err( p_input, "cannot find /moov/mvhd" );
326 msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
327 p_input->pf_demux = DemuxRef;
333 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
334 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
337 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
339 msg_Err( p_input, "cannot find any /moov/trak" );
342 msg_Dbg( p_input, "find %d track%c",
344 p_sys->i_tracks ? 's':' ' );
346 /* create one program */
347 vlc_mutex_lock( &p_input->stream.stream_lock );
348 if( input_InitStream( p_input, 0 ) == -1)
350 vlc_mutex_unlock( &p_input->stream.stream_lock );
351 msg_Err( p_input, "cannot init stream" );
354 if( p_sys->i_duration/p_sys->i_timescale > 0 )
356 p_input->stream.i_mux_rate =
357 p_input->stream.p_selected_area->i_size / 50 /
358 ( p_sys->i_duration / p_sys->i_timescale );
362 p_input->stream.i_mux_rate = 0;
364 vlc_mutex_unlock( &p_input->stream.stream_lock );
367 /* allocate memory */
368 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
369 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
371 /* now process each track and extract all usefull informations */
372 for( i = 0; i < p_sys->i_tracks; i++ )
374 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
375 MP4_TrackCreate( p_input, &p_sys->track[i], p_trak );
377 if( p_sys->track[i].b_ok )
380 switch( p_sys->track[i].fmt.i_cat )
393 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %s",
394 p_sys->track[i].i_track_ID,
396 p_sys->track[i].b_enable ? "enable":"disable",
397 p_sys->track[i].fmt.psz_language ? p_sys->track[i].fmt.psz_language : "undef" );
401 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_sys->track[i].i_track_ID );
410 MP4_BoxFree( p_input, p_sys->p_root );
416 /*****************************************************************************
417 * Demux: read packet and send them to decoders
418 *****************************************************************************
419 * TODO check for newly selected track (ie audio upt to now )
420 *****************************************************************************/
421 static int Demux( input_thread_t *p_input )
423 demux_sys_t *p_sys = p_input->p_demux_data;
424 unsigned int i_track;
427 unsigned int i_track_selected;
428 vlc_bool_t b_play_audio;
430 /* check for newly selected/unselected track */
431 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
433 mp4_track_t *tk = &p_sys->track[i_track];
435 if( tk->b_selected && tk->i_sample >= tk->i_sample_count )
437 msg_Warn( p_input, "track[0x%x] will be disabled", tk->i_track_ID );
438 MP4_TrackUnselect( p_input, tk);
443 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
445 if( tk->b_selected && !b )
447 MP4_TrackUnselect( p_input, tk );
449 else if( !tk->b_selected && b)
451 MP4_TrackSelect( p_input, tk, MP4_GetMoviePTS( p_sys ) );
461 if( i_track_selected <= 0 )
463 msg_Warn( p_input, "no track selected, exiting..." );
467 /* first wait for the good time to read a packet */
468 input_ClockManageRef( p_input,
469 p_input->stream.p_selected_program,
472 /* update pcr XXX in mpeg scale so in 90000 unit/s */
473 p_sys->i_pcr = MP4_GetMoviePTS( p_sys ) * 9 / 100;
475 /* we will read 100ms for each stream so ...*/
476 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
478 /* Check if we need to send the audio data to decoder */
479 b_play_audio = !p_input->stream.control.b_mute;
481 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
483 mp4_track_t *tk = &p_sys->track[i_track];
485 if( !tk->b_ok || !tk->b_selected )
490 while( MP4_TrackGetPTS( p_input, tk ) < MP4_GetMoviePTS( p_sys ) )
493 msg_Dbg( p_input, "tk=%lld mv=%lld",
494 MP4_TrackGetPTS( p_input, tk ),
495 MP4_GetMoviePTS( p_sys ) );
498 if( MP4_TrackSampleSize( tk ) > 0 &&
499 ( b_play_audio || tk->fmt.i_cat != AUDIO_ES ) )
504 if( stream_Seek( p_input->s, MP4_TrackGetPos( tk ) ) )
506 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
507 MP4_TrackUnselect( p_input, tk );
512 if( ( p_block = stream_Block( p_input->s,
513 MP4_TrackSampleSize( tk ) ) ) == NULL )
515 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
516 MP4_TrackUnselect( p_input, tk );
520 if( tk->b_drms && tk->p_drms )
522 drms_decrypt( tk->p_drms,
523 (uint32_t*)p_block->p_buffer,
527 input_ClockGetTS( p_input,
528 p_input->stream.p_selected_program,
529 MP4_TrackGetPTS( p_input, tk ) * 9/100 );
531 p_block->i_pts = tk->fmt.i_cat == VIDEO_ES ? 0 : p_block->i_dts;
533 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
535 es_out_Send( p_input->p_es_out, tk->p_es, p_block );
540 if( MP4_TrackNextSample( p_input, tk ) )
549 /*****************************************************************************
550 * Seek: Got to i_date
551 ******************************************************************************/
552 static int Seek ( input_thread_t *p_input, mtime_t i_date )
554 demux_sys_t *p_sys = p_input->p_demux_data;
555 unsigned int i_track;
557 /* First update update global time */
558 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
559 p_sys->i_pcr = i_date* 9 / 100;
561 /* Now for each stream try to go to this time */
562 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
564 mp4_track_t *tk = &p_sys->track[i_track];
566 if( tk->b_ok && tk->b_selected )
568 MP4_TrackSeek( p_input, tk, i_date );
574 /*****************************************************************************
576 *****************************************************************************/
577 static int Control( input_thread_t *p_input, int i_query, va_list args )
579 demux_sys_t *p_sys = p_input->p_demux_data;
586 case DEMUX_GET_POSITION:
587 pf = (double*)va_arg( args, double * );
588 if( p_sys->i_duration > 0 )
590 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
598 case DEMUX_SET_POSITION:
599 f = (double)va_arg( args, double );
600 if( p_sys->i_timescale > 0 )
602 i64 = (int64_t)( f * (double)1000000 *
603 (double)p_sys->i_duration /
604 (double)p_sys->i_timescale );
605 return Seek( p_input, i64 );
607 else return VLC_SUCCESS;
610 pi64 = (int64_t*)va_arg( args, int64_t * );
611 if( p_sys->i_timescale > 0 )
613 *pi64 = (mtime_t)1000000 *
614 (mtime_t)p_sys->i_time /
615 (mtime_t)p_sys->i_timescale;
621 i64 = (int64_t)va_arg( args, int64_t );
622 return Seek( p_input, i64 );
624 case DEMUX_GET_LENGTH:
625 pi64 = (int64_t*)va_arg( args, int64_t * );
626 if( p_sys->i_timescale > 0 )
628 *pi64 = (mtime_t)1000000 *
629 (mtime_t)p_sys->i_duration /
630 (mtime_t)p_sys->i_timescale;
636 msg_Warn( p_input, "DEMUX_GET_FPS unimplemented !!" );
641 vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
643 MP4_Box_t *p_udta = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
644 MP4_Box_t *p_0xa9xxx;
649 *pp_meta = meta = vlc_meta_New();
650 for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
651 p_0xa9xxx = p_0xa9xxx->p_next )
653 switch( p_0xa9xxx->i_type )
656 vlc_meta_Add( meta, VLC_META_TITLE,
657 p_0xa9xxx->data.p_0xa9xxx->psz_text );
660 vlc_meta_Add( meta, VLC_META_AUTHOR,
661 p_0xa9xxx->data.p_0xa9xxx->psz_text );
664 vlc_meta_Add( meta, VLC_META_ARTIST,
665 p_0xa9xxx->data.p_0xa9xxx->psz_text );
668 vlc_meta_Add( meta, VLC_META_COPYRIGHT,
669 p_0xa9xxx->data.p_0xa9xxx->psz_text );
672 vlc_meta_Add( meta, VLC_META_DATE,
673 p_0xa9xxx->data.p_0xa9xxx->psz_text );
676 vlc_meta_Add( meta, VLC_META_DESCRIPTION,
677 p_0xa9xxx->data.p_0xa9xxx->psz_text );
689 /* TODO one day, but they aren't really meaningfull */
700 msg_Err( p_input, "control query unimplemented !!!" );
701 return demux_vaControlDefault( p_input, i_query, args );
705 /*****************************************************************************
706 * Close: frees unused data
707 *****************************************************************************/
708 static void Close ( vlc_object_t * p_this )
710 unsigned int i_track;
711 input_thread_t * p_input = (input_thread_t *)p_this;
712 demux_sys_t *p_sys = p_input->p_demux_data;
714 msg_Dbg( p_input, "freeing all memory" );
716 MP4_BoxFree( p_input, p_sys->p_root );
717 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
719 MP4_TrackDestroy( p_input, &p_sys->track[i_track] );
721 FREE( p_sys->track );
728 /****************************************************************************
729 * Local functions, specific to vlc
730 ****************************************************************************/
732 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
733 static int TrackCreateChunksIndex( input_thread_t *p_input,
734 mp4_track_t *p_demux_track )
736 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
739 unsigned int i_chunk;
740 unsigned int i_index, i_last;
742 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
743 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
744 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
746 return( VLC_EGENERIC );
749 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
750 if( !p_demux_track->i_chunk_count )
752 msg_Warn( p_input, "no chunk defined" );
753 return( VLC_EGENERIC );
755 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
756 sizeof( mp4_chunk_t ) );
758 /* first we read chunk offset */
759 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
761 p_demux_track->chunk[i_chunk].i_offset =
762 p_co64->data.p_co64->i_chunk_offset[i_chunk];
765 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
766 to be used for the sample XXX begin to 1
767 We construct it begining at the end */
768 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
769 i_index = p_stsc->data.p_stsc->i_entry_count;
772 msg_Warn( p_input, "cannot read chunk table or table empty" );
773 return( VLC_EGENERIC );
779 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
780 i_chunk < i_last; i_chunk++ )
782 p_demux_track->chunk[i_chunk].i_sample_description_index =
783 p_stsc->data.p_stsc->i_sample_description_index[i_index];
784 p_demux_track->chunk[i_chunk].i_sample_count =
785 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
787 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
790 p_demux_track->chunk[0].i_sample_first = 0;
791 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
793 p_demux_track->chunk[i_chunk].i_sample_first =
794 p_demux_track->chunk[i_chunk-1].i_sample_first +
795 p_demux_track->chunk[i_chunk-1].i_sample_count;
799 "track[Id 0x%x] read %d chunk",
800 p_demux_track->i_track_ID,
801 p_demux_track->i_chunk_count );
803 return( VLC_SUCCESS );
805 static int TrackCreateSamplesIndex( input_thread_t *p_input,
806 mp4_track_t *p_demux_track )
808 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
809 ctts make same mapping but for composition time,
810 not yet used and probably not usefull */
811 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
812 that uses a compressed form FIXME make them in libmp4
814 /* TODO use also stss and stsh table for seeking */
815 /* FIXME use edit table */
820 int64_t i_index_sample_used;
824 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
825 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
827 if( ( !p_stts )||( !p_stsz ) )
829 msg_Warn( p_input, "cannot read sample table" );
830 return( VLC_EGENERIC );
833 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
836 /* for sample size, there are 2 case */
837 if( p_stsz->data.p_stsz->i_sample_size )
839 /* 1: all sample have the same size, so no need to construct a table */
840 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
841 p_demux_track->p_sample_size = NULL;
845 /* 2: each sample can have a different size */
846 p_demux_track->i_sample_size = 0;
847 p_demux_track->p_sample_size =
848 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
850 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
852 p_demux_track->p_sample_size[i_sample] =
853 p_stsz->data.p_stsz->i_entry_size[i_sample];
856 /* we have extract all information from stsz,
859 /* if we don't want to waste too much memory, we can't expand
860 the box !, so each chunk will contain an "extract" of this table
864 i_index = 0; i_index_sample_used =0;
866 /* create and init last data for each chunk */
867 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
870 int64_t i_entry, i_sample_count, i;
872 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
873 /* count how many entries needed for this chunk
874 for p_sample_delta_dts and p_sample_count_dts */
876 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
879 while( i_sample_count > 0 )
881 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
884 i_sample_count += i_index_sample_used; /* don't count already used sample
891 p_demux_track->chunk[i_chunk].p_sample_count_dts =
892 calloc( i_entry, sizeof( uint32_t ) );
893 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
894 calloc( i_entry, sizeof( uint32_t ) );
897 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
898 for( i = 0; i < i_entry; i++ )
903 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
905 i_used = __MIN( i_rest, i_sample_count );
907 i_index_sample_used += i_used;
908 i_sample_count -= i_used;
910 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
912 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
913 p_stts->data.p_stts->i_sample_delta[i_index];
915 i_last_dts += i_used *
916 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
918 if( i_index_sample_used >=
919 p_stts->data.p_stts->i_sample_count[i_index] )
923 i_index_sample_used = 0;
930 "track[Id 0x%x] read %d samples length:"I64Fd"s",
931 p_demux_track->i_track_ID,
932 p_demux_track->i_sample_count,
933 i_last_dts / p_demux_track->i_timescale );
935 return( VLC_SUCCESS );
940 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
942 static int TrackCreateES ( input_thread_t *p_input,
943 mp4_track_t *p_track,
944 unsigned int i_chunk,
945 es_out_id_t **pp_es )
952 if( !p_track->chunk[i_chunk].i_sample_description_index )
955 "invalid SampleEntry index (track[Id 0x%x])",
956 p_track->i_track_ID );
960 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
961 p_track->chunk[i_chunk].i_sample_description_index - 1 );
963 if( !p_sample || !p_sample->data.p_data )
966 "cannot find SampleEntry (track[Id 0x%x])",
967 p_track->i_track_ID );
968 return( VLC_EGENERIC );
971 p_track->p_sample = p_sample;
973 if( p_track->i_sample_size == 1 )
975 MP4_Box_data_sample_soun_t *p_soun;
977 p_soun = p_sample->data.p_sample_soun;
979 if( p_soun->i_qt_version == 0 )
981 switch( p_sample->i_type )
983 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
984 p_soun->i_qt_version = 1;
985 p_soun->i_sample_per_packet = 64;
986 p_soun->i_bytes_per_packet = 34;
987 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
988 p_soun->i_bytes_per_sample = 2;
990 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
991 p_soun->i_qt_version = 1;
992 p_soun->i_sample_per_packet = 6;
993 p_soun->i_bytes_per_packet = 2;
994 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
995 p_soun->i_bytes_per_sample = 2;
997 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
998 p_soun->i_qt_version = 1;
999 p_soun->i_sample_per_packet = 12;
1000 p_soun->i_bytes_per_packet = 2;
1001 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
1002 p_soun->i_bytes_per_sample = 2;
1009 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
1011 p_soun->i_qt_version = 0;
1016 /* It's a little ugly but .. there are special cases */
1017 switch( p_sample->i_type )
1019 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
1020 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
1021 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1023 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
1024 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1026 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
1027 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
1030 p_track->fmt.i_codec = p_sample->i_type;
1034 /* now see if esds is present and if so create a data packet
1035 with decoder_specific_info */
1036 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1037 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
1038 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
1039 ( p_esds->data.p_esds )&&
1042 /* First update information based on i_objectTypeIndication */
1043 switch( p_decconfig->i_objectTypeIndication )
1045 case( 0x20 ): /* MPEG4 VIDEO */
1046 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
1049 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1056 case( 0x65): /* MPEG2 video */
1057 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1059 /* Theses are MPEG2-AAC */
1060 case( 0x66): /* main profile */
1061 case( 0x67): /* Low complexity profile */
1062 case( 0x68): /* Scaleable Sampling rate profile */
1063 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1065 /* true MPEG 2 audio */
1067 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1069 case( 0x6a): /* MPEG1 video */
1070 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1072 case( 0x6b): /* MPEG1 audio */
1073 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1075 case( 0x6c ): /* jpeg */
1076 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1079 /* Unknown entry, but don't touch i_fourcc */
1081 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1082 p_decconfig->i_objectTypeIndication,
1083 p_track->i_track_ID );
1086 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1087 if( p_track->fmt.i_extra > 0 )
1089 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1090 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1091 p_track->fmt.i_extra );
1096 switch( p_sample->i_type )
1098 /* qt decoder, send the complete chunk */
1099 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1100 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1101 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1102 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1103 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1104 p_track->fmt.i_extra =
1105 p_sample->data.p_sample_vide->i_qt_image_description;
1106 if( p_track->fmt.i_extra > 0 )
1108 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1109 memcpy( p_track->fmt.p_extra,
1110 p_sample->data.p_sample_vide->p_qt_image_description,
1111 p_track->fmt.i_extra);
1114 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1115 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1116 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1117 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1118 p_track->fmt.i_extra =
1119 p_sample->data.p_sample_soun->i_qt_description;
1120 if( p_track->fmt.i_extra > 0 )
1122 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1123 memcpy( p_track->fmt.p_extra,
1124 p_sample->data.p_sample_soun->p_qt_description,
1125 p_track->fmt.i_extra);
1135 /* some last initialisation */
1136 switch( p_track->fmt.i_cat )
1139 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1140 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1142 /* fall on display size */
1143 if( p_track->fmt.video.i_width <= 0 )
1144 p_track->fmt.video.i_width = p_track->i_width;
1145 if( p_track->fmt.video.i_height <= 0 )
1146 p_track->fmt.video.i_height = p_track->i_height;
1148 /* Find out apect ratio from display size */
1149 if( p_track->i_width > 0 && p_track->i_height > 0 )
1150 p_track->fmt.video.i_aspect =
1151 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1156 p_track->fmt.audio.i_channels =
1157 p_sample->data.p_sample_soun->i_channelcount;
1158 p_track->fmt.audio.i_rate =
1159 p_sample->data.p_sample_soun->i_sampleratehi;
1160 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1161 p_sample->data.p_sample_soun->i_sampleratehi *
1162 p_sample->data.p_sample_soun->i_samplesize;
1163 p_track->fmt.audio.i_bitspersample =
1164 p_sample->data.p_sample_soun->i_samplesize;
1171 *pp_es = es_out_Add( p_input->p_es_out, &p_track->fmt );
1176 /* given a time it return sample/chunk
1177 * it also update elst field of the track
1179 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1180 mp4_track_t *p_track,
1183 uint32_t *pi_sample )
1185 demux_sys_t *p_sys = p_input->p_demux_data;
1188 unsigned int i_sample;
1189 unsigned int i_chunk;
1192 /* FIXME see if it's needed to check p_track->i_chunk_count */
1193 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1195 return( VLC_EGENERIC );
1198 /* handle elst (find the correct one) */
1199 MP4_TrackSetELST( p_input, p_track, i_start );
1200 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1202 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1203 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1205 /* now calculate i_start for this elst */
1207 i_start -= p_track->i_elst_time * (int64_t)1000000 / p_sys->i_timescale;
1215 /* to track time scale */
1216 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1217 /* add elst offset */
1218 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1219 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1220 elst->i_media_time[p_track->i_elst] > 0 )
1222 i_start += elst->i_media_time[p_track->i_elst];
1225 msg_Dbg( p_input, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd"ms (track)",
1227 i_mvt * 1000 / p_sys->i_timescale,
1228 i_start * 1000 / p_track->i_timescale );
1232 /* convert absolute time to in timescale unit */
1233 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1236 /* we start from sample 0/chunk 0, hope it won't take too much time */
1237 /* *** find good chunk *** */
1238 for( i_chunk = 0; ; i_chunk++ )
1240 if( i_chunk + 1 >= p_track->i_chunk_count )
1242 /* at the end and can't check if i_start in this chunk,
1243 it will be check while searching i_sample */
1244 i_chunk = p_track->i_chunk_count - 1;
1248 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1249 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1255 /* *** find sample in the chunk *** */
1256 i_sample = p_track->chunk[i_chunk].i_sample_first;
1257 i_dts = p_track->chunk[i_chunk].i_first_dts;
1258 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1261 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1262 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1265 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1266 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1268 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1273 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1277 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1282 if( i_sample >= p_track->i_sample_count )
1285 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1286 p_track->i_track_ID, i_chunk, i_sample );
1287 return( VLC_EGENERIC );
1291 /* *** Try to find nearest sync points *** */
1292 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1294 unsigned int i_index;
1296 "track[Id 0x%x] using Sync Sample Box (stss)",
1297 p_track->i_track_ID );
1298 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1300 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1304 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1306 p_stss->data.p_stss->i_sample_number[i_index-1] );
1307 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1308 /* new i_sample is less than old so i_chunk can only decreased */
1309 while( i_chunk > 0 &&
1310 i_sample < p_track->chunk[i_chunk].i_sample_first )
1317 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1319 p_stss->data.p_stss->i_sample_number[i_index] );
1320 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1321 /* new i_sample is more than old so i_chunk can only increased */
1322 while( i_chunk < p_track->i_chunk_count - 1 &&
1323 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1324 p_track->chunk[i_chunk].i_sample_count )
1336 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1337 p_track->i_track_ID );
1340 *pi_chunk = i_chunk;
1341 *pi_sample = i_sample;
1346 static int TrackGotoChunkSample( input_thread_t *p_input,
1347 mp4_track_t *p_track,
1348 unsigned int i_chunk,
1349 unsigned int i_sample )
1351 vlc_bool_t b_reselect = VLC_FALSE;
1353 /* now see if actual es is ok */
1354 if( p_track->i_chunk < 0 ||
1355 p_track->i_chunk >= p_track->i_chunk_count ||
1356 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1357 p_track->chunk[i_chunk].i_sample_description_index )
1359 msg_Warn( p_input, "recreate ES" );
1361 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_track->p_es, &b_reselect );
1363 es_out_Del( p_input->p_es_out, p_track->p_es );
1365 p_track->p_es = NULL;
1367 if( TrackCreateES( p_input,
1371 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1372 p_track->i_track_ID );
1374 p_track->b_ok = VLC_FALSE;
1375 p_track->b_selected = VLC_FALSE;
1376 return VLC_EGENERIC;
1380 /* select again the new decoder */
1383 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES, p_track->p_es );
1386 p_track->i_chunk = i_chunk;
1387 p_track->i_sample = i_sample;
1389 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1392 /****************************************************************************
1394 ****************************************************************************
1395 * Parse track information and create all needed data to run a track
1396 * If it succeed b_ok is set to 1 else to 0
1397 ****************************************************************************/
1398 static void MP4_TrackCreate( input_thread_t *p_input,
1399 mp4_track_t *p_track,
1400 MP4_Box_t * p_box_trak )
1402 demux_sys_t *p_sys = p_input->p_demux_data;
1404 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1405 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1419 /* hint track unsuported */
1421 /* set default value (-> track unusable) */
1422 p_track->b_ok = VLC_FALSE;
1423 p_track->b_enable = VLC_FALSE;
1424 p_track->b_selected = VLC_FALSE;
1426 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1433 /* do we launch this track by default ? */
1435 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1437 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1438 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1439 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1443 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1446 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1447 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1449 if( ( !p_mdhd )||( !p_hdlr ) )
1454 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1456 for( i = 0; i < 3; i++ )
1458 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1462 switch( p_hdlr->data.p_hdlr->i_handler_type )
1464 case( FOURCC_soun ):
1465 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1469 p_track->fmt.i_cat = AUDIO_ES;
1472 case( FOURCC_vide ):
1473 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1477 p_track->fmt.i_cat = VIDEO_ES;
1484 p_track->i_elst = 0;
1485 p_track->i_elst_time = 0;
1486 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1488 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1491 msg_Warn( p_input, "elst box found" );
1492 for( i = 0; i < elst->i_entry_count; i++ )
1494 msg_Dbg( p_input, " - [%d] duration="I64Fd"ms media time="I64Fd"ms) rate=%d.%d",
1496 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1497 elst->i_media_time[i] >= 0 ?
1498 elst->i_media_time[i] * 1000 / p_track->i_timescale : -1,
1499 elst->i_media_rate_integer[i],
1500 elst->i_media_rate_fraction[i] );
1507 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1509 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1510 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1515 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1516 p_track->b_drms = p_drms != NULL;
1517 p_track->p_drms = p_track->b_drms ?
1518 p_drms->data.p_sample_soun->p_drms : NULL;
1521 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1523 p_track->fmt.psz_language = strdup( language );
1526 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1527 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1529 MP4_Box_t *p_sample;
1531 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1532 if( p_sample && p_sample->data.p_sample_soun)
1534 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1535 if( p_soun->i_qt_version == 0 &&
1536 p_track->i_timescale != p_soun->i_sampleratehi )
1539 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1541 "Making both equal. (report any problem)",
1542 p_track->i_timescale, p_soun->i_sampleratehi );
1544 if( p_soun->i_sampleratehi )
1545 p_track->i_timescale = p_soun->i_sampleratehi;
1547 p_soun->i_sampleratehi = p_track->i_timescale;
1552 /* Create chunk index table and sample index table */
1553 if( TrackCreateChunksIndex( p_input,p_track ) ||
1554 TrackCreateSamplesIndex( p_input, p_track ) )
1556 return; /* cannot create chunks index */
1559 p_track->i_chunk = 0;
1560 p_track->i_sample = 0;
1563 if( TrackCreateES( p_input,
1564 p_track, p_track->i_chunk,
1567 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1568 p_track->i_track_ID );
1575 for( i = 0; i < p_track->i_chunk_count; i++ )
1577 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1582 p_track->b_ok = VLC_TRUE;
1585 /****************************************************************************
1587 ****************************************************************************
1588 * Destroy a track created by MP4_TrackCreate.
1589 ****************************************************************************/
1590 static void MP4_TrackDestroy( input_thread_t *p_input,
1591 mp4_track_t *p_track )
1593 unsigned int i_chunk;
1595 p_track->b_ok = VLC_FALSE;
1596 p_track->b_enable = VLC_FALSE;
1597 p_track->b_selected = VLC_FALSE;
1599 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1601 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1603 if( p_track->chunk )
1605 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1606 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1609 FREE( p_track->chunk );
1611 if( !p_track->i_sample_size )
1613 FREE( p_track->p_sample_size );
1617 static int MP4_TrackSelect ( input_thread_t *p_input,
1618 mp4_track_t *p_track,
1621 if( !p_track->b_ok )
1623 return VLC_EGENERIC;
1626 if( p_track->b_selected )
1629 "track[Id 0x%x] already selected",
1630 p_track->i_track_ID );
1634 return MP4_TrackSeek( p_input, p_track, i_start );
1637 static void MP4_TrackUnselect(input_thread_t *p_input,
1638 mp4_track_t *p_track )
1640 if( !p_track->b_ok )
1645 if( !p_track->b_selected )
1648 "track[Id 0x%x] already unselected",
1649 p_track->i_track_ID );
1654 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_track->p_es, VLC_FALSE );
1657 p_track->b_selected = VLC_FALSE;
1660 static int MP4_TrackSeek ( input_thread_t *p_input,
1661 mp4_track_t *p_track,
1667 if( !p_track->b_ok )
1669 return( VLC_EGENERIC );
1672 if( TrackTimeToSampleChunk( p_input,
1674 &i_chunk, &i_sample ) )
1677 "cannot select track[Id 0x%x]",
1678 p_track->i_track_ID );
1679 return( VLC_EGENERIC );
1682 p_track->b_selected = VLC_TRUE;
1684 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1686 p_track->b_selected = VLC_FALSE;
1688 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1693 * 3 types: for audio
1696 #define QT_V0_MAX_SAMPLES 1500
1697 static int MP4_TrackSampleSize( mp4_track_t *p_track )
1700 MP4_Box_data_sample_soun_t *p_soun;
1702 if( p_track->i_sample_size == 0 )
1704 /* most simple case */
1705 return( p_track->p_sample_size[p_track->i_sample] );
1707 if( p_track->fmt.i_cat != AUDIO_ES )
1709 return( p_track->i_sample_size );
1712 if( p_track->i_sample_size != 1 )
1714 //msg_Warn( p_input, "SampleSize != 1" );
1715 return( p_track->i_sample_size );
1718 p_soun = p_track->p_sample->data.p_sample_soun;
1720 if( p_soun->i_qt_version == 1 )
1722 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1727 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1728 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1729 if( i_samples > QT_V0_MAX_SAMPLES )
1731 i_samples = QT_V0_MAX_SAMPLES;
1733 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1736 //fprintf( stderr, "size=%d\n", i_size );
1741 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
1743 unsigned int i_sample;
1747 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1749 if( p_track->i_sample_size )
1751 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1753 if( p_soun->i_qt_version == 0 )
1755 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1756 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1760 /* we read chunk by chunk */
1766 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1767 i_sample < p_track->i_sample; i_sample++ )
1769 i_pos += p_track->p_sample_size[i_sample];
1776 static int MP4_TrackNextSample( input_thread_t *p_input,
1777 mp4_track_t *p_track )
1780 if( p_track->fmt.i_cat == AUDIO_ES &&
1781 p_track->i_sample_size != 0 )
1783 MP4_Box_data_sample_soun_t *p_soun;
1785 p_soun = p_track->p_sample->data.p_sample_soun;
1787 if( p_soun->i_qt_version == 1 )
1789 /* chunk by chunk */
1791 p_track->chunk[p_track->i_chunk].i_sample_first +
1792 p_track->chunk[p_track->i_chunk].i_sample_count;
1797 p_track->i_sample += QT_V0_MAX_SAMPLES;
1798 if( p_track->i_sample > p_track->chunk[p_track->i_chunk].i_sample_first + p_track->chunk[p_track->i_chunk].i_sample_count )
1801 p_track->chunk[p_track->i_chunk].i_sample_first +
1802 p_track->chunk[p_track->i_chunk].i_sample_count;
1808 p_track->i_sample++;
1811 if( p_track->i_sample >= p_track->i_sample_count )
1813 /* we have reach end of the track so free decoder stuff */
1814 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1815 MP4_TrackUnselect( p_input, p_track );
1816 return VLC_EGENERIC;
1819 /* Have we changed chunk ? */
1820 if( p_track->i_sample >=
1821 p_track->chunk[p_track->i_chunk].i_sample_first +
1822 p_track->chunk[p_track->i_chunk].i_sample_count )
1824 if( TrackGotoChunkSample( p_input,
1826 p_track->i_chunk + 1,
1827 p_track->i_sample ) )
1829 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1830 MP4_TrackUnselect( p_input, p_track );
1831 return VLC_EGENERIC;
1835 /* Have we changed elst */
1836 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1838 demux_sys_t *p_sys = p_input->p_demux_data;
1839 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1840 int64_t i_mvt = MP4_TrackGetPTS( p_input, p_track ) * p_sys->i_timescale / (int64_t)1000000;
1842 if( p_track->i_elst < elst->i_entry_count &&
1843 i_mvt >= p_track->i_elst_time + elst->i_segment_duration[p_track->i_elst] )
1845 MP4_TrackSetELST( p_input, p_track, MP4_TrackGetPTS( p_input, p_track ) );
1852 static void MP4_TrackSetELST( input_thread_t *p_input, mp4_track_t *tk, int64_t i_time )
1854 demux_sys_t *p_sys = p_input->p_demux_data;
1855 int i_elst_last = tk->i_elst;
1857 /* handle elst (find the correct one) */
1859 tk->i_elst_time = 0;
1860 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
1862 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
1863 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
1865 for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
1867 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
1869 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
1873 tk->i_elst_time += i_dur;
1876 if( tk->i_elst >= elst->i_entry_count )
1878 /* msg_Dbg( p_input, "invalid number of entry in elst" ); */
1879 tk->i_elst = elst->i_entry_count - 1;
1880 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
1883 if( elst->i_media_time[tk->i_elst] < 0 )
1886 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
1889 if( i_elst_last != tk->i_elst )
1891 msg_Warn( p_input, "elst old=%d new=%d", i_elst_last, tk->i_elst );