1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
5 * $Id: mp4.c,v 1.58 2004/01/30 14:27:48 fenrir Exp $
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>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
44 set_description( _("MP4 stream demuxer") );
45 set_capability( "demux", 242 );
46 set_callbacks( Open, Close );
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Demux ( input_thread_t * );
53 static int DemuxRef( input_thread_t *p_input )
57 static int Seek ( input_thread_t *, mtime_t );
58 static int Control ( input_thread_t *, int, va_list );
60 /*****************************************************************************
61 * Declaration of local function
62 *****************************************************************************/
63 static void MP4_TrackCreate ( input_thread_t *, mp4_track_t *, MP4_Box_t *);
64 static void MP4_TrackDestroy( input_thread_t *, mp4_track_t * );
66 static int MP4_TrackSelect ( input_thread_t *, mp4_track_t *, mtime_t );
67 static void MP4_TrackUnselect(input_thread_t *, mp4_track_t * );
69 static int MP4_TrackSeek ( input_thread_t *, mp4_track_t *, mtime_t );
71 static uint64_t MP4_TrackGetPos ( mp4_track_t * );
72 static int MP4_TrackSampleSize( mp4_track_t * );
73 static int MP4_TrackNextSample( input_thread_t *, mp4_track_t * );
74 static void MP4_TrackSetELST( input_thread_t *, mp4_track_t *, int64_t );
76 /* Return time in µs of a track */
77 static inline int64_t MP4_TrackGetPTS( input_thread_t *p_input, mp4_track_t *p_track )
79 unsigned int i_sample;
83 i_sample = p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first;
84 i_dts = p_track->chunk[p_track->i_chunk].i_first_dts;
88 if( i_sample > p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] )
90 i_dts += p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] *
91 p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
92 i_sample -= p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index];
98 p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
104 /* now handle elst */
105 if( p_track->p_elst )
107 demux_sys_t *p_sys = p_input->p_demux_data;
108 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
110 /* convert to offset */
111 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
112 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
113 elst->i_media_time[p_track->i_elst] > 0 )
115 i_dts -= elst->i_media_time[p_track->i_elst];
118 /* add i_elst_time */
119 i_dts += p_track->i_elst_time * p_track->i_timescale / p_sys->i_timescale;
121 if( i_dts < 0 ) i_dts = 0;
124 return (int64_t)1000000 * i_dts / p_track->i_timescale;
126 static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
128 return (int64_t)1000000 * p_sys->i_time / p_sys->i_timescale;
131 #define FREE( p ) if( p ) { free( p ); (p) = NULL;}
133 /*****************************************************************************
134 * Open: check file and initializes MP4 structures
135 *****************************************************************************/
136 static int Open( vlc_object_t * p_this )
138 input_thread_t *p_input = (input_thread_t *)p_this;
149 vlc_bool_t b_seekable;
151 /* a little test to see if it could be a mp4 */
152 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
154 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
157 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
168 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
171 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
176 stream_Control( p_input->s, STREAM_CAN_SEEK, &b_seekable );
179 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
183 /*Set exported functions */
184 p_input->pf_demux = Demux;
185 p_input->pf_demux_control = Control;
187 /* create our structure that will contains all data */
188 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
189 memset( p_sys, 0, sizeof( demux_sys_t ) );
191 /* Now load all boxes ( except raw data ) */
192 if( ( p_sys->p_root = MP4_BoxGetRoot( p_input ) ) == NULL )
194 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
198 MP4_BoxDumpStructure( p_input, p_sys->p_root );
200 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
202 switch( p_ftyp->data.p_ftyp->i_major_brand )
206 "ISO Media file (isom) version %d.",
207 p_ftyp->data.p_ftyp->i_minor_version );
211 "unrecognized major file specification (%4.4s).",
212 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
218 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
221 /* the file need to have one moov box */
222 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
224 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
228 msg_Err( p_input, "MP4 plugin discarded (no moov box)" );
231 /* we have a free box as a moov, rename it */
232 p_foov->i_type = FOURCC_moov;
235 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
237 playlist_t *p_playlist;
238 int i_count = MP4_BoxCount( p_rmra, "rmda" );
241 msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );
244 (playlist_t *)vlc_object_find( p_input,
249 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
250 for( i = 0; i < i_count; i++ )
252 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
256 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
260 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
262 msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
263 psz_ref, (char*)&i_ref_type );
265 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
267 if( strstr( psz_ref, "qt5gateQT" ) )
269 msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
272 if( !strncmp( psz_ref, "http://", 7 ) ||
273 !strncmp( psz_ref, "rtsp://", 7 ) )
275 msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
276 playlist_Add( p_playlist, psz_ref, psz_ref,
277 PLAYLIST_APPEND, PLAYLIST_END );
281 /* msg dbg relative ? */
282 char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
283 char *end = strrchr( p_input->psz_source, '/' );
287 int i_len = end + 1 - p_input->psz_source;
289 strncpy( psz_absolute, p_input->psz_source, i_len);
290 psz_absolute[i_len] = '\0';
294 strcpy( psz_absolute, "" );
296 strcat( psz_absolute, psz_ref );
297 msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
298 playlist_Add( p_playlist, psz_absolute, psz_absolute,
299 PLAYLIST_APPEND, PLAYLIST_END );
304 msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)",
305 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
308 vlc_object_release( p_playlist );
312 msg_Err( p_input, "can't find playlist" );
316 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
320 msg_Err( p_input, "cannot find /moov/mvhd" );
325 msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
326 p_input->pf_demux = DemuxRef;
332 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
333 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
336 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
338 msg_Err( p_input, "cannot find any /moov/trak" );
341 msg_Dbg( p_input, "find %d track%c",
343 p_sys->i_tracks ? 's':' ' );
345 /* create one program */
346 vlc_mutex_lock( &p_input->stream.stream_lock );
347 if( input_InitStream( p_input, 0 ) == -1)
349 vlc_mutex_unlock( &p_input->stream.stream_lock );
350 msg_Err( p_input, "cannot init stream" );
353 if( p_sys->i_duration/p_sys->i_timescale > 0 )
355 p_input->stream.i_mux_rate =
356 p_input->stream.p_selected_area->i_size / 50 /
357 ( p_sys->i_duration / p_sys->i_timescale );
361 p_input->stream.i_mux_rate = 0;
363 vlc_mutex_unlock( &p_input->stream.stream_lock );
366 /* allocate memory */
367 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
368 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
370 /* now process each track and extract all usefull informations */
371 for( i = 0; i < p_sys->i_tracks; i++ )
373 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
374 MP4_TrackCreate( p_input, &p_sys->track[i], p_trak );
376 if( p_sys->track[i].b_ok )
379 switch( p_sys->track[i].fmt.i_cat )
392 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %s",
393 p_sys->track[i].i_track_ID,
395 p_sys->track[i].b_enable ? "enable":"disable",
396 p_sys->track[i].fmt.psz_language ? p_sys->track[i].fmt.psz_language : "undef" );
400 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_sys->track[i].i_track_ID );
409 MP4_BoxFree( p_input, p_sys->p_root );
415 /*****************************************************************************
416 * Demux: read packet and send them to decoders
417 *****************************************************************************
418 * TODO check for newly selected track (ie audio upt to now )
419 *****************************************************************************/
420 static int Demux( input_thread_t *p_input )
422 demux_sys_t *p_sys = p_input->p_demux_data;
423 unsigned int i_track;
426 unsigned int i_track_selected;
427 vlc_bool_t b_play_audio;
429 /* check for newly selected/unselected track */
430 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
432 mp4_track_t *tk = &p_sys->track[i_track];
434 if( tk->b_selected && tk->i_sample >= tk->i_sample_count )
436 msg_Warn( p_input, "track[0x%x] will be disabled", tk->i_track_ID );
437 MP4_TrackUnselect( p_input, tk);
442 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
444 if( tk->b_selected && !b )
446 MP4_TrackUnselect( p_input, tk );
448 else if( !tk->b_selected && b)
450 MP4_TrackSelect( p_input, tk, MP4_GetMoviePTS( p_sys ) );
460 if( i_track_selected <= 0 )
462 msg_Warn( p_input, "no track selected, exiting..." );
466 /* first wait for the good time to read a packet */
467 input_ClockManageRef( p_input,
468 p_input->stream.p_selected_program,
471 /* update pcr XXX in mpeg scale so in 90000 unit/s */
472 p_sys->i_pcr = MP4_GetMoviePTS( p_sys ) * 9 / 100;
474 /* we will read 100ms for each stream so ...*/
475 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
477 /* Check if we need to send the audio data to decoder */
478 b_play_audio = !p_input->stream.control.b_mute;
480 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
482 mp4_track_t *tk = &p_sys->track[i_track];
484 if( !tk->b_ok || !tk->b_selected )
489 while( MP4_TrackGetPTS( p_input, tk ) < MP4_GetMoviePTS( p_sys ) )
492 msg_Dbg( p_input, "tk=%lld mv=%lld",
493 MP4_TrackGetPTS( p_input, tk ),
494 MP4_GetMoviePTS( p_sys ) );
497 if( MP4_TrackSampleSize( tk ) > 0 &&
498 ( b_play_audio || tk->fmt.i_cat != AUDIO_ES ) )
503 if( stream_Seek( p_input->s, MP4_TrackGetPos( tk ) ) )
505 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
506 MP4_TrackUnselect( p_input, tk );
511 if( ( p_block = stream_Block( p_input->s,
512 MP4_TrackSampleSize( tk ) ) ) == NULL )
514 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
515 MP4_TrackUnselect( p_input, tk );
519 if( tk->b_drms && tk->p_drms )
521 drms_decrypt( tk->p_drms,
522 (uint32_t*)p_block->p_buffer,
526 input_ClockGetTS( p_input,
527 p_input->stream.p_selected_program,
528 MP4_TrackGetPTS( p_input, tk ) * 9/100 );
530 p_block->i_pts = tk->fmt.i_cat == VIDEO_ES ? 0 : p_block->i_dts;
532 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
534 es_out_Send( p_input->p_es_out, tk->p_es, p_block );
539 if( MP4_TrackNextSample( p_input, tk ) )
548 /*****************************************************************************
549 * Seek: Got to i_date
550 ******************************************************************************/
551 static int Seek ( input_thread_t *p_input, mtime_t i_date )
553 demux_sys_t *p_sys = p_input->p_demux_data;
554 unsigned int i_track;
556 /* First update update global time */
557 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
558 p_sys->i_pcr = i_date* 9 / 100;
560 /* Now for each stream try to go to this time */
561 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
563 mp4_track_t *tk = &p_sys->track[i_track];
565 if( tk->b_ok && tk->b_selected )
567 MP4_TrackSeek( p_input, tk, i_date );
573 /*****************************************************************************
575 *****************************************************************************/
576 static int Control( input_thread_t *p_input, int i_query, va_list args )
578 demux_sys_t *p_sys = p_input->p_demux_data;
585 case DEMUX_GET_POSITION:
586 pf = (double*)va_arg( args, double * );
587 if( p_sys->i_duration > 0 )
589 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
597 case DEMUX_SET_POSITION:
598 f = (double)va_arg( args, double );
599 if( p_sys->i_timescale > 0 )
601 i64 = (int64_t)( f * (double)1000000 *
602 (double)p_sys->i_duration /
603 (double)p_sys->i_timescale );
604 return Seek( p_input, i64 );
606 else return VLC_SUCCESS;
609 pi64 = (int64_t*)va_arg( args, int64_t * );
610 if( p_sys->i_timescale > 0 )
612 *pi64 = (mtime_t)1000000 *
613 (mtime_t)p_sys->i_time /
614 (mtime_t)p_sys->i_timescale;
620 i64 = (int64_t)va_arg( args, int64_t );
621 return Seek( p_input, i64 );
623 case DEMUX_GET_LENGTH:
624 pi64 = (int64_t*)va_arg( args, int64_t * );
625 if( p_sys->i_timescale > 0 )
627 *pi64 = (mtime_t)1000000 *
628 (mtime_t)p_sys->i_duration /
629 (mtime_t)p_sys->i_timescale;
635 msg_Warn( p_input, "DEMUX_GET_FPS unimplemented !!" );
639 msg_Err( p_input, "control query unimplemented !!!" );
640 return demux_vaControlDefault( p_input, i_query, args );
644 /*****************************************************************************
645 * Close: frees unused data
646 *****************************************************************************/
647 static void Close ( vlc_object_t * p_this )
649 unsigned int i_track;
650 input_thread_t * p_input = (input_thread_t *)p_this;
651 demux_sys_t *p_sys = p_input->p_demux_data;
653 msg_Dbg( p_input, "freeing all memory" );
655 MP4_BoxFree( p_input, p_sys->p_root );
656 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
658 MP4_TrackDestroy( p_input, &p_sys->track[i_track] );
660 FREE( p_sys->track );
667 /****************************************************************************
668 * Local functions, specific to vlc
669 ****************************************************************************/
671 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
672 static int TrackCreateChunksIndex( input_thread_t *p_input,
673 mp4_track_t *p_demux_track )
675 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
678 unsigned int i_chunk;
679 unsigned int i_index, i_last;
681 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
682 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
683 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
685 return( VLC_EGENERIC );
688 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
689 if( !p_demux_track->i_chunk_count )
691 msg_Warn( p_input, "no chunk defined" );
692 return( VLC_EGENERIC );
694 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
695 sizeof( mp4_chunk_t ) );
697 /* first we read chunk offset */
698 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
700 p_demux_track->chunk[i_chunk].i_offset =
701 p_co64->data.p_co64->i_chunk_offset[i_chunk];
704 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
705 to be used for the sample XXX begin to 1
706 We construct it begining at the end */
707 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
708 i_index = p_stsc->data.p_stsc->i_entry_count;
711 msg_Warn( p_input, "cannot read chunk table or table empty" );
712 return( VLC_EGENERIC );
718 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
719 i_chunk < i_last; i_chunk++ )
721 p_demux_track->chunk[i_chunk].i_sample_description_index =
722 p_stsc->data.p_stsc->i_sample_description_index[i_index];
723 p_demux_track->chunk[i_chunk].i_sample_count =
724 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
726 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
729 p_demux_track->chunk[0].i_sample_first = 0;
730 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
732 p_demux_track->chunk[i_chunk].i_sample_first =
733 p_demux_track->chunk[i_chunk-1].i_sample_first +
734 p_demux_track->chunk[i_chunk-1].i_sample_count;
738 "track[Id 0x%x] read %d chunk",
739 p_demux_track->i_track_ID,
740 p_demux_track->i_chunk_count );
742 return( VLC_SUCCESS );
744 static int TrackCreateSamplesIndex( input_thread_t *p_input,
745 mp4_track_t *p_demux_track )
747 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
748 ctts make same mapping but for composition time,
749 not yet used and probably not usefull */
750 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
751 that uses a compressed form FIXME make them in libmp4
753 /* TODO use also stss and stsh table for seeking */
754 /* FIXME use edit table */
759 int64_t i_index_sample_used;
763 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
764 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
766 if( ( !p_stts )||( !p_stsz ) )
768 msg_Warn( p_input, "cannot read sample table" );
769 return( VLC_EGENERIC );
772 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
775 /* for sample size, there are 2 case */
776 if( p_stsz->data.p_stsz->i_sample_size )
778 /* 1: all sample have the same size, so no need to construct a table */
779 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
780 p_demux_track->p_sample_size = NULL;
784 /* 2: each sample can have a different size */
785 p_demux_track->i_sample_size = 0;
786 p_demux_track->p_sample_size =
787 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
789 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
791 p_demux_track->p_sample_size[i_sample] =
792 p_stsz->data.p_stsz->i_entry_size[i_sample];
795 /* we have extract all information from stsz,
798 /* if we don't want to waste too much memory, we can't expand
799 the box !, so each chunk will contain an "extract" of this table
803 i_index = 0; i_index_sample_used =0;
805 /* create and init last data for each chunk */
806 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
809 int64_t i_entry, i_sample_count, i;
811 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
812 /* count how many entries needed for this chunk
813 for p_sample_delta_dts and p_sample_count_dts */
815 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
818 while( i_sample_count > 0 )
820 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
823 i_sample_count += i_index_sample_used; /* don't count already used sample
830 p_demux_track->chunk[i_chunk].p_sample_count_dts =
831 calloc( i_entry, sizeof( uint32_t ) );
832 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
833 calloc( i_entry, sizeof( uint32_t ) );
836 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
837 for( i = 0; i < i_entry; i++ )
842 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
844 i_used = __MIN( i_rest, i_sample_count );
846 i_index_sample_used += i_used;
847 i_sample_count -= i_used;
849 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
851 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
852 p_stts->data.p_stts->i_sample_delta[i_index];
854 i_last_dts += i_used *
855 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
857 if( i_index_sample_used >=
858 p_stts->data.p_stts->i_sample_count[i_index] )
862 i_index_sample_used = 0;
869 "track[Id 0x%x] read %d samples length:"I64Fd"s",
870 p_demux_track->i_track_ID,
871 p_demux_track->i_sample_count,
872 i_last_dts / p_demux_track->i_timescale );
874 return( VLC_SUCCESS );
879 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
881 static int TrackCreateES ( input_thread_t *p_input,
882 mp4_track_t *p_track,
883 unsigned int i_chunk,
884 es_out_id_t **pp_es )
891 if( !p_track->chunk[i_chunk].i_sample_description_index )
894 "invalid SampleEntry index (track[Id 0x%x])",
895 p_track->i_track_ID );
899 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
900 p_track->chunk[i_chunk].i_sample_description_index - 1 );
902 if( !p_sample || !p_sample->data.p_data )
905 "cannot find SampleEntry (track[Id 0x%x])",
906 p_track->i_track_ID );
907 return( VLC_EGENERIC );
910 p_track->p_sample = p_sample;
912 if( p_track->i_sample_size == 1 )
914 MP4_Box_data_sample_soun_t *p_soun;
916 p_soun = p_sample->data.p_sample_soun;
918 if( p_soun->i_qt_version == 0 )
920 switch( p_sample->i_type )
922 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
923 p_soun->i_qt_version = 1;
924 p_soun->i_sample_per_packet = 64;
925 p_soun->i_bytes_per_packet = 34;
926 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
927 p_soun->i_bytes_per_sample = 2;
929 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
930 p_soun->i_qt_version = 1;
931 p_soun->i_sample_per_packet = 6;
932 p_soun->i_bytes_per_packet = 2;
933 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
934 p_soun->i_bytes_per_sample = 2;
936 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
937 p_soun->i_qt_version = 1;
938 p_soun->i_sample_per_packet = 12;
939 p_soun->i_bytes_per_packet = 2;
940 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
941 p_soun->i_bytes_per_sample = 2;
948 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
950 p_soun->i_qt_version = 0;
955 /* It's a little ugly but .. there are special cases */
956 switch( p_sample->i_type )
958 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
959 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
960 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
962 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
963 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
965 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
966 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
969 p_track->fmt.i_codec = p_sample->i_type;
973 /* now see if esds is present and if so create a data packet
974 with decoder_specific_info */
975 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
976 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
977 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
978 ( p_esds->data.p_esds )&&
981 /* First update information based on i_objectTypeIndication */
982 switch( p_decconfig->i_objectTypeIndication )
984 case( 0x20 ): /* MPEG4 VIDEO */
985 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
988 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
995 case( 0x65): /* MPEG2 video */
996 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
998 /* Theses are MPEG2-AAC */
999 case( 0x66): /* main profile */
1000 case( 0x67): /* Low complexity profile */
1001 case( 0x68): /* Scaleable Sampling rate profile */
1002 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
1004 /* true MPEG 2 audio */
1006 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1008 case( 0x6a): /* MPEG1 video */
1009 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1011 case( 0x6b): /* MPEG1 audio */
1012 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1014 case( 0x6c ): /* jpeg */
1015 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1018 /* Unknown entry, but don't touch i_fourcc */
1020 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1021 p_decconfig->i_objectTypeIndication,
1022 p_track->i_track_ID );
1025 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1026 if( p_track->fmt.i_extra > 0 )
1028 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1029 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1030 p_track->fmt.i_extra );
1035 switch( p_sample->i_type )
1037 /* qt decoder, send the complete chunk */
1038 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1039 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1040 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1041 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1042 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1043 p_track->fmt.i_extra =
1044 p_sample->data.p_sample_vide->i_qt_image_description;
1045 if( p_track->fmt.i_extra > 0 )
1047 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1048 memcpy( p_track->fmt.p_extra,
1049 p_sample->data.p_sample_vide->p_qt_image_description,
1050 p_track->fmt.i_extra);
1053 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1054 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1055 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1056 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1057 p_track->fmt.i_extra =
1058 p_sample->data.p_sample_soun->i_qt_description;
1059 if( p_track->fmt.i_extra > 0 )
1061 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1062 memcpy( p_track->fmt.p_extra,
1063 p_sample->data.p_sample_soun->p_qt_description,
1064 p_track->fmt.i_extra);
1074 /* some last initialisation */
1075 switch( p_track->fmt.i_cat )
1078 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1079 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1081 /* fall on display size */
1082 if( p_track->fmt.video.i_width <= 0 )
1083 p_track->fmt.video.i_width = p_track->i_width;
1084 if( p_track->fmt.video.i_height <= 0 )
1085 p_track->fmt.video.i_height = p_track->i_height;
1087 /* Find out apect ratio from display size */
1088 if( p_track->i_width > 0 && p_track->i_height > 0 )
1089 p_track->fmt.video.i_aspect =
1090 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1095 p_track->fmt.audio.i_channels =
1096 p_sample->data.p_sample_soun->i_channelcount;
1097 p_track->fmt.audio.i_rate =
1098 p_sample->data.p_sample_soun->i_sampleratehi;
1099 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1100 p_sample->data.p_sample_soun->i_sampleratehi *
1101 p_sample->data.p_sample_soun->i_samplesize;
1102 p_track->fmt.audio.i_bitspersample =
1103 p_sample->data.p_sample_soun->i_samplesize;
1110 *pp_es = es_out_Add( p_input->p_es_out, &p_track->fmt );
1115 /* given a time it return sample/chunk
1116 * it also update elst field of the track
1118 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1119 mp4_track_t *p_track,
1122 uint32_t *pi_sample )
1124 demux_sys_t *p_sys = p_input->p_demux_data;
1127 unsigned int i_sample;
1128 unsigned int i_chunk;
1131 /* FIXME see if it's needed to check p_track->i_chunk_count */
1132 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1134 return( VLC_EGENERIC );
1137 /* handle elst (find the correct one) */
1138 MP4_TrackSetELST( p_input, p_track, i_start );
1139 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1141 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1142 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1144 /* now calculate i_start for this elst */
1146 i_start -= p_track->i_elst_time * (int64_t)1000000 / p_sys->i_timescale;
1154 /* to track time scale */
1155 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1156 /* add elst offset */
1157 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1158 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1159 elst->i_media_time[p_track->i_elst] > 0 )
1161 i_start += elst->i_media_time[p_track->i_elst];
1164 msg_Dbg( p_input, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd"ms (track)",
1166 i_mvt * 1000 / p_sys->i_timescale,
1167 i_start * 1000 / p_track->i_timescale );
1171 /* convert absolute time to in timescale unit */
1172 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1175 /* we start from sample 0/chunk 0, hope it won't take too much time */
1176 /* *** find good chunk *** */
1177 for( i_chunk = 0; ; i_chunk++ )
1179 if( i_chunk + 1 >= p_track->i_chunk_count )
1181 /* at the end and can't check if i_start in this chunk,
1182 it will be check while searching i_sample */
1183 i_chunk = p_track->i_chunk_count - 1;
1187 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1188 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1194 /* *** find sample in the chunk *** */
1195 i_sample = p_track->chunk[i_chunk].i_sample_first;
1196 i_dts = p_track->chunk[i_chunk].i_first_dts;
1197 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1200 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1201 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1204 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1205 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1207 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1212 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1216 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1221 if( i_sample >= p_track->i_sample_count )
1224 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1225 p_track->i_track_ID, i_chunk, i_sample );
1226 return( VLC_EGENERIC );
1230 /* *** Try to find nearest sync points *** */
1231 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1233 unsigned int i_index;
1235 "track[Id 0x%x] using Sync Sample Box (stss)",
1236 p_track->i_track_ID );
1237 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1239 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1243 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1245 p_stss->data.p_stss->i_sample_number[i_index-1] );
1246 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1247 /* new i_sample is less than old so i_chunk can only decreased */
1248 while( i_chunk > 0 &&
1249 i_sample < p_track->chunk[i_chunk].i_sample_first )
1256 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1258 p_stss->data.p_stss->i_sample_number[i_index] );
1259 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1260 /* new i_sample is more than old so i_chunk can only increased */
1261 while( i_chunk < p_track->i_chunk_count - 1 &&
1262 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1263 p_track->chunk[i_chunk].i_sample_count )
1275 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1276 p_track->i_track_ID );
1279 *pi_chunk = i_chunk;
1280 *pi_sample = i_sample;
1285 static int TrackGotoChunkSample( input_thread_t *p_input,
1286 mp4_track_t *p_track,
1287 unsigned int i_chunk,
1288 unsigned int i_sample )
1290 vlc_bool_t b_reselect = VLC_FALSE;
1292 /* now see if actual es is ok */
1293 if( p_track->i_chunk < 0 ||
1294 p_track->i_chunk >= p_track->i_chunk_count ||
1295 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1296 p_track->chunk[i_chunk].i_sample_description_index )
1298 msg_Warn( p_input, "recreate ES" );
1300 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_track->p_es, &b_reselect );
1302 es_out_Del( p_input->p_es_out, p_track->p_es );
1304 p_track->p_es = NULL;
1306 if( TrackCreateES( p_input,
1310 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1311 p_track->i_track_ID );
1313 p_track->b_ok = VLC_FALSE;
1314 p_track->b_selected = VLC_FALSE;
1315 return VLC_EGENERIC;
1319 /* select again the new decoder */
1322 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES, p_track->p_es );
1325 p_track->i_chunk = i_chunk;
1326 p_track->i_sample = i_sample;
1328 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1331 /****************************************************************************
1333 ****************************************************************************
1334 * Parse track information and create all needed data to run a track
1335 * If it succeed b_ok is set to 1 else to 0
1336 ****************************************************************************/
1337 static void MP4_TrackCreate( input_thread_t *p_input,
1338 mp4_track_t *p_track,
1339 MP4_Box_t * p_box_trak )
1341 demux_sys_t *p_sys = p_input->p_demux_data;
1343 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1344 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1358 /* hint track unsuported */
1360 /* set default value (-> track unusable) */
1361 p_track->b_ok = VLC_FALSE;
1362 p_track->b_enable = VLC_FALSE;
1363 p_track->b_selected = VLC_FALSE;
1365 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1372 /* do we launch this track by default ? */
1374 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1376 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1377 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1378 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1382 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1385 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1386 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1388 if( ( !p_mdhd )||( !p_hdlr ) )
1393 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1395 for( i = 0; i < 3; i++ )
1397 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1401 switch( p_hdlr->data.p_hdlr->i_handler_type )
1403 case( FOURCC_soun ):
1404 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1408 p_track->fmt.i_cat = AUDIO_ES;
1411 case( FOURCC_vide ):
1412 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1416 p_track->fmt.i_cat = VIDEO_ES;
1423 p_track->i_elst = 0;
1424 p_track->i_elst_time = 0;
1425 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1427 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1430 msg_Warn( p_input, "elst box found" );
1431 for( i = 0; i < elst->i_entry_count; i++ )
1433 msg_Dbg( p_input, " - [%d] duration="I64Fd"ms media time="I64Fd"ms) rate=%d.%d",
1435 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1436 elst->i_media_time[i] >= 0 ?
1437 elst->i_media_time[i] * 1000 / p_track->i_timescale : -1,
1438 elst->i_media_rate_integer[i],
1439 elst->i_media_rate_fraction[i] );
1446 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1448 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1449 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1454 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1455 p_track->b_drms = p_drms != NULL;
1456 p_track->p_drms = p_track->b_drms ?
1457 p_drms->data.p_sample_soun->p_drms : NULL;
1460 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1462 p_track->fmt.psz_language = strdup( language );
1465 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1466 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1468 MP4_Box_t *p_sample;
1470 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1471 if( p_sample && p_sample->data.p_sample_soun)
1473 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1474 if( p_soun->i_qt_version == 0 &&
1475 p_track->i_timescale != p_soun->i_sampleratehi )
1478 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1480 "Making both equal. (report any problem)",
1481 p_track->i_timescale, p_soun->i_sampleratehi );
1483 if( p_soun->i_sampleratehi )
1484 p_track->i_timescale = p_soun->i_sampleratehi;
1486 p_soun->i_sampleratehi = p_track->i_timescale;
1491 /* Create chunk index table and sample index table */
1492 if( TrackCreateChunksIndex( p_input,p_track ) ||
1493 TrackCreateSamplesIndex( p_input, p_track ) )
1495 return; /* cannot create chunks index */
1498 p_track->i_chunk = 0;
1499 p_track->i_sample = 0;
1502 if( TrackCreateES( p_input,
1503 p_track, p_track->i_chunk,
1506 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1507 p_track->i_track_ID );
1514 for( i = 0; i < p_track->i_chunk_count; i++ )
1516 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1521 p_track->b_ok = VLC_TRUE;
1524 /****************************************************************************
1526 ****************************************************************************
1527 * Destroy a track created by MP4_TrackCreate.
1528 ****************************************************************************/
1529 static void MP4_TrackDestroy( input_thread_t *p_input,
1530 mp4_track_t *p_track )
1532 unsigned int i_chunk;
1534 p_track->b_ok = VLC_FALSE;
1535 p_track->b_enable = VLC_FALSE;
1536 p_track->b_selected = VLC_FALSE;
1538 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1540 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1542 if( p_track->chunk )
1544 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1545 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1548 FREE( p_track->chunk );
1550 if( !p_track->i_sample_size )
1552 FREE( p_track->p_sample_size );
1556 static int MP4_TrackSelect ( input_thread_t *p_input,
1557 mp4_track_t *p_track,
1560 if( !p_track->b_ok )
1562 return VLC_EGENERIC;
1565 if( p_track->b_selected )
1568 "track[Id 0x%x] already selected",
1569 p_track->i_track_ID );
1573 return MP4_TrackSeek( p_input, p_track, i_start );
1576 static void MP4_TrackUnselect(input_thread_t *p_input,
1577 mp4_track_t *p_track )
1579 if( !p_track->b_ok )
1584 if( !p_track->b_selected )
1587 "track[Id 0x%x] already unselected",
1588 p_track->i_track_ID );
1593 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_track->p_es, VLC_FALSE );
1596 p_track->b_selected = VLC_FALSE;
1599 static int MP4_TrackSeek ( input_thread_t *p_input,
1600 mp4_track_t *p_track,
1606 if( !p_track->b_ok )
1608 return( VLC_EGENERIC );
1611 if( TrackTimeToSampleChunk( p_input,
1613 &i_chunk, &i_sample ) )
1616 "cannot select track[Id 0x%x]",
1617 p_track->i_track_ID );
1618 return( VLC_EGENERIC );
1621 p_track->b_selected = VLC_TRUE;
1623 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1625 p_track->b_selected = VLC_FALSE;
1627 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1632 * 3 types: for audio
1635 #define QT_V0_MAX_SAMPLES 1500
1636 static int MP4_TrackSampleSize( mp4_track_t *p_track )
1639 MP4_Box_data_sample_soun_t *p_soun;
1641 if( p_track->i_sample_size == 0 )
1643 /* most simple case */
1644 return( p_track->p_sample_size[p_track->i_sample] );
1646 if( p_track->fmt.i_cat != AUDIO_ES )
1648 return( p_track->i_sample_size );
1651 if( p_track->i_sample_size != 1 )
1653 //msg_Warn( p_input, "SampleSize != 1" );
1654 return( p_track->i_sample_size );
1657 p_soun = p_track->p_sample->data.p_sample_soun;
1659 if( p_soun->i_qt_version == 1 )
1661 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1666 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1667 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1668 if( i_samples > QT_V0_MAX_SAMPLES )
1670 i_samples = QT_V0_MAX_SAMPLES;
1672 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1675 //fprintf( stderr, "size=%d\n", i_size );
1680 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
1682 unsigned int i_sample;
1686 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1688 if( p_track->i_sample_size )
1690 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1692 if( p_soun->i_qt_version == 0 )
1694 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1695 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1699 /* we read chunk by chunk */
1705 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1706 i_sample < p_track->i_sample; i_sample++ )
1708 i_pos += p_track->p_sample_size[i_sample];
1715 static int MP4_TrackNextSample( input_thread_t *p_input,
1716 mp4_track_t *p_track )
1719 if( p_track->fmt.i_cat == AUDIO_ES &&
1720 p_track->i_sample_size != 0 )
1722 MP4_Box_data_sample_soun_t *p_soun;
1724 p_soun = p_track->p_sample->data.p_sample_soun;
1726 if( p_soun->i_qt_version == 1 )
1728 /* chunk by chunk */
1730 p_track->chunk[p_track->i_chunk].i_sample_first +
1731 p_track->chunk[p_track->i_chunk].i_sample_count;
1736 p_track->i_sample += QT_V0_MAX_SAMPLES;
1737 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 )
1740 p_track->chunk[p_track->i_chunk].i_sample_first +
1741 p_track->chunk[p_track->i_chunk].i_sample_count;
1747 p_track->i_sample++;
1750 if( p_track->i_sample >= p_track->i_sample_count )
1752 /* we have reach end of the track so free decoder stuff */
1753 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1754 MP4_TrackUnselect( p_input, p_track );
1755 return VLC_EGENERIC;
1758 /* Have we changed chunk ? */
1759 if( p_track->i_sample >=
1760 p_track->chunk[p_track->i_chunk].i_sample_first +
1761 p_track->chunk[p_track->i_chunk].i_sample_count )
1763 if( TrackGotoChunkSample( p_input,
1765 p_track->i_chunk + 1,
1766 p_track->i_sample ) )
1768 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1769 MP4_TrackUnselect( p_input, p_track );
1770 return VLC_EGENERIC;
1774 /* Have we changed elst */
1775 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1777 demux_sys_t *p_sys = p_input->p_demux_data;
1778 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1779 int64_t i_mvt = MP4_TrackGetPTS( p_input, p_track ) * p_sys->i_timescale / (int64_t)1000000;
1781 if( p_track->i_elst < elst->i_entry_count &&
1782 i_mvt >= p_track->i_elst_time + elst->i_segment_duration[p_track->i_elst] )
1784 MP4_TrackSetELST( p_input, p_track, MP4_TrackGetPTS( p_input, p_track ) );
1791 static void MP4_TrackSetELST( input_thread_t *p_input, mp4_track_t *tk, int64_t i_time )
1793 demux_sys_t *p_sys = p_input->p_demux_data;
1794 int i_elst_last = tk->i_elst;
1796 /* handle elst (find the correct one) */
1798 tk->i_elst_time = 0;
1799 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
1801 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
1802 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
1804 for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
1806 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
1808 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
1812 tk->i_elst_time += i_dur;
1815 if( tk->i_elst >= elst->i_entry_count )
1817 /* msg_Dbg( p_input, "invalid number of entry in elst" ); */
1818 tk->i_elst = elst->i_entry_count - 1;
1819 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
1822 if( elst->i_media_time[tk->i_elst] < 0 )
1825 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
1828 if( i_elst_last != tk->i_elst )
1830 msg_Warn( p_input, "elst old=%d new=%d", i_elst_last, tk->i_elst );