1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.55 2004/01/18 22:00:00 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 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 static char *LanguageGetName( const char *psz_code );
133 #define FREE( p ) if( p ) { free( p ); (p) = NULL;}
135 /*****************************************************************************
136 * Open: check file and initializes MP4 structures
137 *****************************************************************************/
138 static int Open( vlc_object_t * p_this )
140 input_thread_t *p_input = (input_thread_t *)p_this;
151 vlc_bool_t b_seekable;
153 /* a little test to see if it could be a mp4 */
154 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
156 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
159 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
170 case VLC_FOURCC( 'p', 'n', 'o', 't' ):
173 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
178 stream_Control( p_input->s, STREAM_CAN_SEEK, &b_seekable );
181 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
185 /*Set exported functions */
186 p_input->pf_demux = Demux;
187 p_input->pf_demux_control = Control;
189 /* create our structure that will contains all data */
190 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
191 memset( p_sys, 0, sizeof( demux_sys_t ) );
193 /* Now load all boxes ( except raw data ) */
194 if( ( p_sys->p_root = MP4_BoxGetRoot( p_input ) ) == NULL )
196 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
200 MP4_BoxDumpStructure( p_input, p_sys->p_root );
202 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
204 switch( p_ftyp->data.p_ftyp->i_major_brand )
208 "ISO Media file (isom) version %d.",
209 p_ftyp->data.p_ftyp->i_minor_version );
213 "unrecognized major file specification (%4.4s).",
214 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
220 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
223 /* the file need to have one moov box */
224 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
226 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
230 msg_Err( p_input, "MP4 plugin discarded (no moov box)" );
233 /* we have a free box as a moov, rename it */
234 p_foov->i_type = FOURCC_moov;
237 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
239 playlist_t *p_playlist;
240 int i_count = MP4_BoxCount( p_rmra, "rmda" );
243 msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );
246 (playlist_t *)vlc_object_find( p_input,
251 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
252 for( i = 0; i < i_count; i++ )
254 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
258 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
262 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
264 msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
265 psz_ref, (char*)&i_ref_type );
267 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
269 if( strstr( psz_ref, "qt5gateQT" ) )
271 msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
274 if( !strncmp( psz_ref, "http://", 7 ) ||
275 !strncmp( psz_ref, "rtsp://", 7 ) )
277 msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
278 playlist_Add( p_playlist, psz_ref, psz_ref,
279 PLAYLIST_APPEND, PLAYLIST_END );
283 /* msg dbg relative ? */
284 char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
285 char *end = strrchr( p_input->psz_source, '/' );
289 int i_len = end + 1 - p_input->psz_source;
291 strncpy( psz_absolute, p_input->psz_source, i_len);
292 psz_absolute[i_len] = '\0';
296 strcpy( psz_absolute, "" );
298 strcat( psz_absolute, psz_ref );
299 msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
300 playlist_Add( p_playlist, psz_absolute, psz_absolute,
301 PLAYLIST_APPEND, PLAYLIST_END );
306 msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)",
307 (char*)&p_rdrf->data.p_rdrf->i_ref_type );
310 vlc_object_release( p_playlist );
314 msg_Err( p_input, "can't find playlist" );
318 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
322 msg_Err( p_input, "cannot find /moov/mvhd" );
327 msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
328 p_input->pf_demux = DemuxRef;
334 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
335 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
338 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
340 msg_Err( p_input, "cannot find any /moov/trak" );
343 msg_Dbg( p_input, "find %d track%c",
345 p_sys->i_tracks ? 's':' ' );
347 /* create one program */
348 vlc_mutex_lock( &p_input->stream.stream_lock );
349 if( input_InitStream( p_input, 0 ) == -1)
351 vlc_mutex_unlock( &p_input->stream.stream_lock );
352 msg_Err( p_input, "cannot init stream" );
355 if( p_sys->i_duration/p_sys->i_timescale > 0 )
357 p_input->stream.i_mux_rate =
358 p_input->stream.p_selected_area->i_size / 50 /
359 ( p_sys->i_duration / p_sys->i_timescale );
363 p_input->stream.i_mux_rate = 0;
365 vlc_mutex_unlock( &p_input->stream.stream_lock );
368 /* allocate memory */
369 p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
370 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
372 /* now process each track and extract all usefull informations */
373 for( i = 0; i < p_sys->i_tracks; i++ )
375 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
376 MP4_TrackCreate( p_input, &p_sys->track[i], p_trak );
378 if( p_sys->track[i].b_ok )
381 switch( p_sys->track[i].fmt.i_cat )
394 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %s",
395 p_sys->track[i].i_track_ID,
397 p_sys->track[i].b_enable ? "enable":"disable",
398 p_sys->track[i].fmt.psz_language ? p_sys->track[i].fmt.psz_language : "undef" );
402 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_sys->track[i].i_track_ID );
411 MP4_BoxFree( p_input, p_sys->p_root );
417 /*****************************************************************************
418 * Demux: read packet and send them to decoders
419 *****************************************************************************
420 * TODO check for newly selected track (ie audio upt to now )
421 *****************************************************************************/
422 static int Demux( input_thread_t *p_input )
424 demux_sys_t *p_sys = p_input->p_demux_data;
425 unsigned int i_track;
428 unsigned int i_track_selected;
429 vlc_bool_t b_play_audio;
431 /* check for newly selected/unselected track */
432 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
434 mp4_track_t *tk = &p_sys->track[i_track];
436 if( tk->b_selected && tk->i_sample >= tk->i_sample_count )
438 msg_Warn( p_input, "track[0x%x] will be disabled", tk->i_track_ID );
439 MP4_TrackUnselect( p_input, tk);
444 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
446 if( tk->b_selected && !b )
448 MP4_TrackUnselect( p_input, tk );
450 else if( !tk->b_selected && b)
452 MP4_TrackSelect( p_input, tk, MP4_GetMoviePTS( p_sys ) );
462 if( i_track_selected <= 0 )
464 msg_Warn( p_input, "no track selected, exiting..." );
468 /* first wait for the good time to read a packet */
469 input_ClockManageRef( p_input,
470 p_input->stream.p_selected_program,
473 /* update pcr XXX in mpeg scale so in 90000 unit/s */
474 p_sys->i_pcr = MP4_GetMoviePTS( p_sys ) * 9 / 100;
476 /* we will read 100ms for each stream so ...*/
477 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
479 /* Check if we need to send the audio data to decoder */
480 b_play_audio = !p_input->stream.control.b_mute;
482 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
484 mp4_track_t *tk = &p_sys->track[i_track];
486 if( !tk->b_ok || !tk->b_selected )
491 while( MP4_TrackGetPTS( p_input, tk ) < MP4_GetMoviePTS( p_sys ) )
494 msg_Dbg( p_input, "tk=%lld mv=%lld",
495 MP4_TrackGetPTS( p_input, tk ),
496 MP4_GetMoviePTS( p_sys ) );
499 if( MP4_TrackSampleSize( tk ) > 0 &&
500 ( b_play_audio || tk->fmt.i_cat != AUDIO_ES ) )
505 if( stream_Seek( p_input->s, MP4_TrackGetPos( tk ) ) )
507 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
508 MP4_TrackUnselect( p_input, tk );
513 if( ( p_block = stream_Block( p_input->s,
514 MP4_TrackSampleSize( tk ) ) ) == NULL )
516 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
517 MP4_TrackUnselect( p_input, tk );
521 if( tk->b_drms && tk->p_drms )
523 drms_decrypt( tk->p_drms,
524 (uint32_t*)p_block->p_buffer,
528 input_ClockGetTS( p_input,
529 p_input->stream.p_selected_program,
530 MP4_TrackGetPTS( p_input, tk ) * 9/100 );
532 p_block->i_pts = tk->fmt.i_cat == VIDEO_ES ? 0 : p_block->i_dts;
534 if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
536 es_out_Send( p_input->p_es_out, tk->p_es, p_block );
541 if( MP4_TrackNextSample( p_input, tk ) )
550 /*****************************************************************************
551 * Seek: Got to i_date
552 ******************************************************************************/
553 static int Seek ( input_thread_t *p_input, mtime_t i_date )
555 demux_sys_t *p_sys = p_input->p_demux_data;
556 unsigned int i_track;
558 /* First update update global time */
559 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
560 p_sys->i_pcr = i_date* 9 / 100;
562 /* Now for each stream try to go to this time */
563 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
565 mp4_track_t *tk = &p_sys->track[i_track];
567 if( tk->b_ok && tk->b_selected )
569 MP4_TrackSeek( p_input, tk, i_date );
575 /*****************************************************************************
577 *****************************************************************************/
578 static int Control ( input_thread_t *p_input, int i_query, va_list args )
580 demux_sys_t *p_sys = p_input->p_demux_data;
587 case DEMUX_GET_POSITION:
588 pf = (double*)va_arg( args, double * );
589 if( p_sys->i_duration > 0 )
591 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
599 case DEMUX_SET_POSITION:
600 f = (double)va_arg( args, double );
603 (double)p_sys->i_duration /
604 (double)p_sys->i_timescale );
605 return Seek( p_input, i64 );
608 pi64 = (int64_t*)va_arg( args, int64_t * );
609 *pi64 = (mtime_t)1000000 *
610 (mtime_t)p_sys->i_time /
611 (mtime_t)p_sys->i_timescale;
615 i64 = (int64_t)va_arg( args, int64_t );
616 return Seek( p_input, i64 );
618 case DEMUX_GET_LENGTH:
619 pi64 = (int64_t*)va_arg( args, int64_t * );
620 *pi64 = (mtime_t)1000000 *
621 (mtime_t)p_sys->i_duration /
622 (mtime_t)p_sys->i_timescale;
625 msg_Warn( p_input, "DEMUX_GET_FPS unimplemented !!" );
628 msg_Err( p_input, "control query unimplemented !!!" );
629 return demux_vaControlDefault( p_input, i_query, args );
633 /*****************************************************************************
634 * Close: frees unused data
635 *****************************************************************************/
636 static void Close ( vlc_object_t * p_this )
638 unsigned int i_track;
639 input_thread_t * p_input = (input_thread_t *)p_this;
640 demux_sys_t *p_sys = p_input->p_demux_data;
642 msg_Dbg( p_input, "freeing all memory" );
644 MP4_BoxFree( p_input, p_sys->p_root );
645 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
647 MP4_TrackDestroy( p_input, &p_sys->track[i_track] );
649 FREE( p_sys->track );
656 /****************************************************************************
657 * Local functions, specific to vlc
658 ****************************************************************************/
660 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
661 static int TrackCreateChunksIndex( input_thread_t *p_input,
662 mp4_track_t *p_demux_track )
664 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
667 unsigned int i_chunk;
668 unsigned int i_index, i_last;
670 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
671 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
672 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
674 return( VLC_EGENERIC );
677 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
678 if( !p_demux_track->i_chunk_count )
680 msg_Warn( p_input, "no chunk defined" );
681 return( VLC_EGENERIC );
683 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
684 sizeof( mp4_chunk_t ) );
686 /* first we read chunk offset */
687 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
689 p_demux_track->chunk[i_chunk].i_offset =
690 p_co64->data.p_co64->i_chunk_offset[i_chunk];
693 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
694 to be used for the sample XXX begin to 1
695 We construct it begining at the end */
696 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
697 i_index = p_stsc->data.p_stsc->i_entry_count;
700 msg_Warn( p_input, "cannot read chunk table or table empty" );
701 return( VLC_EGENERIC );
707 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
708 i_chunk < i_last; i_chunk++ )
710 p_demux_track->chunk[i_chunk].i_sample_description_index =
711 p_stsc->data.p_stsc->i_sample_description_index[i_index];
712 p_demux_track->chunk[i_chunk].i_sample_count =
713 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
715 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
718 p_demux_track->chunk[0].i_sample_first = 0;
719 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
721 p_demux_track->chunk[i_chunk].i_sample_first =
722 p_demux_track->chunk[i_chunk-1].i_sample_first +
723 p_demux_track->chunk[i_chunk-1].i_sample_count;
727 "track[Id 0x%x] read %d chunk",
728 p_demux_track->i_track_ID,
729 p_demux_track->i_chunk_count );
731 return( VLC_SUCCESS );
733 static int TrackCreateSamplesIndex( input_thread_t *p_input,
734 mp4_track_t *p_demux_track )
736 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
737 ctts make same mapping but for composition time,
738 not yet used and probably not usefull */
739 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
740 that uses a compressed form FIXME make them in libmp4
742 /* TODO use also stss and stsh table for seeking */
743 /* FIXME use edit table */
748 int64_t i_index_sample_used;
752 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
753 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
755 if( ( !p_stts )||( !p_stsz ) )
757 msg_Warn( p_input, "cannot read sample table" );
758 return( VLC_EGENERIC );
761 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
764 /* for sample size, there are 2 case */
765 if( p_stsz->data.p_stsz->i_sample_size )
767 /* 1: all sample have the same size, so no need to construct a table */
768 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
769 p_demux_track->p_sample_size = NULL;
773 /* 2: each sample can have a different size */
774 p_demux_track->i_sample_size = 0;
775 p_demux_track->p_sample_size =
776 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
778 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
780 p_demux_track->p_sample_size[i_sample] =
781 p_stsz->data.p_stsz->i_entry_size[i_sample];
784 /* we have extract all information from stsz,
787 /* if we don't want to waste too much memory, we can't expand
788 the box !, so each chunk will contain an "extract" of this table
792 i_index = 0; i_index_sample_used =0;
794 /* create and init last data for each chunk */
795 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
798 int64_t i_entry, i_sample_count, i;
800 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
801 /* count how many entries needed for this chunk
802 for p_sample_delta_dts and p_sample_count_dts */
804 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
807 while( i_sample_count > 0 )
809 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
812 i_sample_count += i_index_sample_used; /* don't count already used sample
819 p_demux_track->chunk[i_chunk].p_sample_count_dts =
820 calloc( i_entry, sizeof( uint32_t ) );
821 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
822 calloc( i_entry, sizeof( uint32_t ) );
825 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
826 for( i = 0; i < i_entry; i++ )
831 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
833 i_used = __MIN( i_rest, i_sample_count );
835 i_index_sample_used += i_used;
836 i_sample_count -= i_used;
838 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
840 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
841 p_stts->data.p_stts->i_sample_delta[i_index];
843 i_last_dts += i_used *
844 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
846 if( i_index_sample_used >=
847 p_stts->data.p_stts->i_sample_count[i_index] )
851 i_index_sample_used = 0;
858 "track[Id 0x%x] read %d samples length:"I64Fd"s",
859 p_demux_track->i_track_ID,
860 p_demux_track->i_sample_count,
861 i_last_dts / p_demux_track->i_timescale );
863 return( VLC_SUCCESS );
868 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
870 static int TrackCreateES ( input_thread_t *p_input,
871 mp4_track_t *p_track,
872 unsigned int i_chunk,
873 es_out_id_t **pp_es )
880 if( !p_track->chunk[i_chunk].i_sample_description_index )
883 "invalid SampleEntry index (track[Id 0x%x])",
884 p_track->i_track_ID );
888 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
889 p_track->chunk[i_chunk].i_sample_description_index - 1 );
891 if( !p_sample || !p_sample->data.p_data )
894 "cannot find SampleEntry (track[Id 0x%x])",
895 p_track->i_track_ID );
896 return( VLC_EGENERIC );
899 p_track->p_sample = p_sample;
901 if( p_track->i_sample_size == 1 )
903 MP4_Box_data_sample_soun_t *p_soun;
905 p_soun = p_sample->data.p_sample_soun;
907 if( p_soun->i_qt_version == 0 )
909 switch( p_sample->i_type )
911 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
912 p_soun->i_qt_version = 1;
913 p_soun->i_sample_per_packet = 64;
914 p_soun->i_bytes_per_packet = 34;
915 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
916 p_soun->i_bytes_per_sample = 2;
918 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
919 p_soun->i_qt_version = 1;
920 p_soun->i_sample_per_packet = 6;
921 p_soun->i_bytes_per_packet = 2;
922 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
923 p_soun->i_bytes_per_sample = 2;
925 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
926 p_soun->i_qt_version = 1;
927 p_soun->i_sample_per_packet = 12;
928 p_soun->i_bytes_per_packet = 2;
929 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
930 p_soun->i_bytes_per_sample = 2;
937 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
939 p_soun->i_qt_version = 0;
944 /* It's a little ugly but .. there are special cases */
945 switch( p_sample->i_type )
947 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
948 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
949 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
951 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
952 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
954 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
955 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
958 p_track->fmt.i_codec = p_sample->i_type;
962 /* now see if esds is present and if so create a data packet
963 with decoder_specific_info */
964 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
965 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
966 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
967 ( p_esds->data.p_esds )&&
970 /* First update information based on i_objectTypeIndication */
971 switch( p_decconfig->i_objectTypeIndication )
973 case( 0x20 ): /* MPEG4 VIDEO */
974 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
977 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
984 case( 0x65): /* MPEG2 video */
985 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
987 /* Theses are MPEG2-AAC */
988 case( 0x66): /* main profile */
989 case( 0x67): /* Low complexity profile */
990 case( 0x68): /* Scaleable Sampling rate profile */
991 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
993 /* true MPEG 2 audio */
995 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
997 case( 0x6a): /* MPEG1 video */
998 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1000 case( 0x6b): /* MPEG1 audio */
1001 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
1003 case( 0x6c ): /* jpeg */
1004 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
1007 /* Unknown entry, but don't touch i_fourcc */
1009 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1010 p_decconfig->i_objectTypeIndication,
1011 p_track->i_track_ID );
1014 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
1015 if( p_track->fmt.i_extra > 0 )
1017 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1018 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
1019 p_track->fmt.i_extra );
1024 switch( p_sample->i_type )
1026 /* qt decoder, send the complete chunk */
1027 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1028 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1029 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1030 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1031 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1032 p_track->fmt.i_extra =
1033 p_sample->data.p_sample_vide->i_qt_image_description;
1034 if( p_track->fmt.i_extra > 0 )
1036 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1037 memcpy( p_track->fmt.p_extra,
1038 p_sample->data.p_sample_vide->p_qt_image_description,
1039 p_track->fmt.i_extra);
1042 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1043 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1044 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1045 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1046 p_track->fmt.i_extra =
1047 p_sample->data.p_sample_soun->i_qt_description;
1048 if( p_track->fmt.i_extra > 0 )
1050 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1051 memcpy( p_track->fmt.p_extra,
1052 p_sample->data.p_sample_soun->p_qt_description,
1053 p_track->fmt.i_extra);
1063 /* some last initialisation */
1064 switch( p_track->fmt.i_cat )
1067 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1068 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1070 /* fall on display size */
1071 if( p_track->fmt.video.i_width <= 0 )
1072 p_track->fmt.video.i_width = p_track->i_width;
1073 if( p_track->fmt.video.i_height <= 0 )
1074 p_track->fmt.video.i_height = p_track->i_height;
1076 /* Find out apect ratio from display size */
1077 if( p_track->i_width > 0 && p_track->i_height > 0 )
1078 p_track->fmt.video.i_aspect =
1079 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1084 p_track->fmt.audio.i_channels =
1085 p_sample->data.p_sample_soun->i_channelcount;
1086 p_track->fmt.audio.i_rate =
1087 p_sample->data.p_sample_soun->i_sampleratehi;
1088 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1089 p_sample->data.p_sample_soun->i_sampleratehi *
1090 p_sample->data.p_sample_soun->i_samplesize;
1091 p_track->fmt.audio.i_bitspersample =
1092 p_sample->data.p_sample_soun->i_samplesize;
1099 *pp_es = es_out_Add( p_input->p_es_out, &p_track->fmt );
1104 /* given a time it return sample/chunk
1105 * it also update elst field of the track
1107 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1108 mp4_track_t *p_track,
1111 uint32_t *pi_sample )
1113 demux_sys_t *p_sys = p_input->p_demux_data;
1116 unsigned int i_sample;
1117 unsigned int i_chunk;
1120 /* FIXME see if it's needed to check p_track->i_chunk_count */
1121 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1123 return( VLC_EGENERIC );
1126 /* handle elst (find the correct one) */
1127 MP4_TrackSetELST( p_input, p_track, i_start );
1128 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1130 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1131 int64_t i_mvt= i_start * p_sys->i_timescale / (int64_t)1000000;
1133 /* now calculate i_start for this elst */
1135 i_start -= p_track->i_elst_time * (int64_t)1000000 / p_sys->i_timescale;
1143 /* to track time scale */
1144 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1145 /* add elst offset */
1146 if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
1147 elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
1148 elst->i_media_time[p_track->i_elst] > 0 )
1150 i_start += elst->i_media_time[p_track->i_elst];
1153 msg_Dbg( p_input, "elst (%d) gives "I64Fd"ms (movie)-> "I64Fd"ms (track)",
1155 i_mvt * 1000 / p_sys->i_timescale,
1156 i_start * 1000 / p_track->i_timescale );
1160 /* convert absolute time to in timescale unit */
1161 i_start = i_start * p_track->i_timescale / (int64_t)1000000;
1164 /* we start from sample 0/chunk 0, hope it won't take too much time */
1165 /* *** find good chunk *** */
1166 for( i_chunk = 0; ; i_chunk++ )
1168 if( i_chunk + 1 >= p_track->i_chunk_count )
1170 /* at the end and can't check if i_start in this chunk,
1171 it will be check while searching i_sample */
1172 i_chunk = p_track->i_chunk_count - 1;
1176 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1177 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1183 /* *** find sample in the chunk *** */
1184 i_sample = p_track->chunk[i_chunk].i_sample_first;
1185 i_dts = p_track->chunk[i_chunk].i_first_dts;
1186 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1189 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1190 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1193 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1194 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1196 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1201 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1205 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1210 if( i_sample >= p_track->i_sample_count )
1213 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1214 p_track->i_track_ID, i_chunk, i_sample );
1215 return( VLC_EGENERIC );
1219 /* *** Try to find nearest sync points *** */
1220 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1222 unsigned int i_index;
1224 "track[Id 0x%x] using Sync Sample Box (stss)",
1225 p_track->i_track_ID );
1226 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1228 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1232 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1234 p_stss->data.p_stss->i_sample_number[i_index-1] );
1235 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1236 /* new i_sample is less than old so i_chunk can only decreased */
1237 while( i_chunk > 0 &&
1238 i_sample < p_track->chunk[i_chunk].i_sample_first )
1245 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1247 p_stss->data.p_stss->i_sample_number[i_index] );
1248 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1249 /* new i_sample is more than old so i_chunk can only increased */
1250 while( i_chunk < p_track->i_chunk_count - 1 &&
1251 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1252 p_track->chunk[i_chunk].i_sample_count )
1264 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1265 p_track->i_track_ID );
1268 *pi_chunk = i_chunk;
1269 *pi_sample = i_sample;
1274 static int TrackGotoChunkSample( input_thread_t *p_input,
1275 mp4_track_t *p_track,
1276 unsigned int i_chunk,
1277 unsigned int i_sample )
1279 vlc_bool_t b_reselect = VLC_FALSE;
1281 /* now see if actual es is ok */
1282 if( p_track->i_chunk < 0 ||
1283 p_track->i_chunk >= p_track->i_chunk_count ||
1284 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1285 p_track->chunk[i_chunk].i_sample_description_index )
1287 msg_Warn( p_input, "Recreate ES" );
1289 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_track->p_es, &b_reselect );
1291 es_out_Del( p_input->p_es_out, p_track->p_es );
1293 p_track->p_es = NULL;
1295 if( TrackCreateES( p_input,
1299 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1300 p_track->i_track_ID );
1302 p_track->b_ok = VLC_FALSE;
1303 p_track->b_selected = VLC_FALSE;
1304 return VLC_EGENERIC;
1308 /* select again the new decoder */
1311 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES, p_track->p_es );
1314 p_track->i_chunk = i_chunk;
1315 p_track->i_sample = i_sample;
1317 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1320 /****************************************************************************
1322 ****************************************************************************
1323 * Parse track information and create all needed data to run a track
1324 * If it succeed b_ok is set to 1 else to 0
1325 ****************************************************************************/
1326 static void MP4_TrackCreate( input_thread_t *p_input,
1327 mp4_track_t *p_track,
1328 MP4_Box_t * p_box_trak )
1330 demux_sys_t *p_sys = p_input->p_demux_data;
1332 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1333 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1347 /* hint track unsuported */
1349 /* set default value (-> track unusable) */
1350 p_track->b_ok = VLC_FALSE;
1351 p_track->b_enable = VLC_FALSE;
1352 p_track->b_selected = VLC_FALSE;
1354 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1361 /* do we launch this track by default ? */
1363 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1365 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1366 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1367 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1371 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1374 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1375 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1377 if( ( !p_mdhd )||( !p_hdlr ) )
1382 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1384 for( i = 0; i < 3; i++ )
1386 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1390 switch( p_hdlr->data.p_hdlr->i_handler_type )
1392 case( FOURCC_soun ):
1393 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1397 p_track->fmt.i_cat = AUDIO_ES;
1400 case( FOURCC_vide ):
1401 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1405 p_track->fmt.i_cat = VIDEO_ES;
1412 p_track->i_elst = 0;
1413 p_track->i_elst_time = 0;
1414 if( ( p_track->p_elst = p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1416 MP4_Box_data_elst_t *elst = p_elst->data.p_elst;
1419 msg_Warn( p_input, "elst box found" );
1420 for( i = 0; i < elst->i_entry_count; i++ )
1422 msg_Dbg( p_input, " - [%d] duration="I64Fd"ms media time="I64Fd"ms) rate=%d.%d",
1424 elst->i_segment_duration[i] * 1000 / p_sys->i_timescale,
1425 elst->i_media_time[i] >= 0 ?
1426 elst->i_media_time[i] * 1000 / p_track->i_timescale : -1,
1427 elst->i_media_rate_integer[i],
1428 elst->i_media_rate_fraction[i] );
1435 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1437 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1438 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1443 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1444 p_track->b_drms = p_drms != NULL;
1445 p_track->p_drms = p_track->b_drms ?
1446 p_drms->data.p_sample_soun->p_drms : NULL;
1449 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1451 p_track->fmt.psz_language = LanguageGetName( language );
1454 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1455 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1457 MP4_Box_t *p_sample;
1459 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1460 if( p_sample && p_sample->data.p_sample_soun)
1462 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1463 if( p_soun->i_qt_version == 0 &&
1464 p_track->i_timescale != p_soun->i_sampleratehi )
1467 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1469 "Making both equal. (report any problem)",
1470 p_track->i_timescale, p_soun->i_sampleratehi );
1472 if( p_soun->i_sampleratehi )
1473 p_track->i_timescale = p_soun->i_sampleratehi;
1475 p_soun->i_sampleratehi = p_track->i_timescale;
1480 /* Create chunk index table and sample index table */
1481 if( TrackCreateChunksIndex( p_input,p_track ) ||
1482 TrackCreateSamplesIndex( p_input, p_track ) )
1484 return; /* cannot create chunks index */
1487 p_track->i_chunk = 0;
1488 p_track->i_sample = 0;
1491 if( TrackCreateES( p_input,
1492 p_track, p_track->i_chunk,
1495 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1496 p_track->i_track_ID );
1503 for( i = 0; i < p_track->i_chunk_count; i++ )
1505 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1510 p_track->b_ok = VLC_TRUE;
1513 /****************************************************************************
1515 ****************************************************************************
1516 * Destroy a track created by MP4_TrackCreate.
1517 ****************************************************************************/
1518 static void MP4_TrackDestroy( input_thread_t *p_input,
1519 mp4_track_t *p_track )
1521 unsigned int i_chunk;
1523 p_track->b_ok = VLC_FALSE;
1524 p_track->b_enable = VLC_FALSE;
1525 p_track->b_selected = VLC_FALSE;
1527 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1529 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1531 if( p_track->chunk )
1533 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1534 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1537 FREE( p_track->chunk );
1539 if( !p_track->i_sample_size )
1541 FREE( p_track->p_sample_size );
1545 static int MP4_TrackSelect ( input_thread_t *p_input,
1546 mp4_track_t *p_track,
1549 if( !p_track->b_ok )
1551 return VLC_EGENERIC;
1554 if( p_track->b_selected )
1557 "track[Id 0x%x] already selected",
1558 p_track->i_track_ID );
1562 return MP4_TrackSeek( p_input, p_track, i_start );
1565 static void MP4_TrackUnselect(input_thread_t *p_input,
1566 mp4_track_t *p_track )
1568 if( !p_track->b_ok )
1573 if( !p_track->b_selected )
1576 "track[Id 0x%x] already unselected",
1577 p_track->i_track_ID );
1582 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_track->p_es, VLC_FALSE );
1585 p_track->b_selected = VLC_FALSE;
1588 static int MP4_TrackSeek ( input_thread_t *p_input,
1589 mp4_track_t *p_track,
1595 if( !p_track->b_ok )
1597 return( VLC_EGENERIC );
1600 if( TrackTimeToSampleChunk( p_input,
1602 &i_chunk, &i_sample ) )
1605 "cannot select track[Id 0x%x]",
1606 p_track->i_track_ID );
1607 return( VLC_EGENERIC );
1610 p_track->b_selected = VLC_TRUE;
1612 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1614 p_track->b_selected = VLC_FALSE;
1616 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1621 * 3 types: for audio
1624 #define QT_V0_MAX_SAMPLES 1500
1625 static int MP4_TrackSampleSize( mp4_track_t *p_track )
1628 MP4_Box_data_sample_soun_t *p_soun;
1630 if( p_track->i_sample_size == 0 )
1632 /* most simple case */
1633 return( p_track->p_sample_size[p_track->i_sample] );
1635 if( p_track->fmt.i_cat != AUDIO_ES )
1637 return( p_track->i_sample_size );
1640 if( p_track->i_sample_size != 1 )
1642 //msg_Warn( p_input, "SampleSize != 1" );
1643 return( p_track->i_sample_size );
1646 p_soun = p_track->p_sample->data.p_sample_soun;
1648 if( p_soun->i_qt_version == 1 )
1650 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1655 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1656 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1657 if( i_samples > QT_V0_MAX_SAMPLES )
1659 i_samples = QT_V0_MAX_SAMPLES;
1661 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1664 //fprintf( stderr, "size=%d\n", i_size );
1669 static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
1671 unsigned int i_sample;
1675 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1677 if( p_track->i_sample_size )
1679 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1681 if( p_soun->i_qt_version == 0 )
1683 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1684 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1688 /* we read chunk by chunk */
1694 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1695 i_sample < p_track->i_sample; i_sample++ )
1697 i_pos += p_track->p_sample_size[i_sample];
1704 static int MP4_TrackNextSample( input_thread_t *p_input,
1705 mp4_track_t *p_track )
1708 if( p_track->fmt.i_cat == AUDIO_ES &&
1709 p_track->i_sample_size != 0 )
1711 MP4_Box_data_sample_soun_t *p_soun;
1713 p_soun = p_track->p_sample->data.p_sample_soun;
1715 if( p_soun->i_qt_version == 1 )
1717 /* chunk by chunk */
1719 p_track->chunk[p_track->i_chunk].i_sample_first +
1720 p_track->chunk[p_track->i_chunk].i_sample_count;
1725 p_track->i_sample += QT_V0_MAX_SAMPLES;
1726 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 )
1729 p_track->chunk[p_track->i_chunk].i_sample_first +
1730 p_track->chunk[p_track->i_chunk].i_sample_count;
1736 p_track->i_sample++;
1739 if( p_track->i_sample >= p_track->i_sample_count )
1741 /* we have reach end of the track so free decoder stuff */
1742 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1743 MP4_TrackUnselect( p_input, p_track );
1744 return VLC_EGENERIC;
1747 /* Have we changed chunk ? */
1748 if( p_track->i_sample >=
1749 p_track->chunk[p_track->i_chunk].i_sample_first +
1750 p_track->chunk[p_track->i_chunk].i_sample_count )
1752 if( TrackGotoChunkSample( p_input,
1754 p_track->i_chunk + 1,
1755 p_track->i_sample ) )
1757 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1758 MP4_TrackUnselect( p_input, p_track );
1759 return VLC_EGENERIC;
1763 /* Have we changed elst */
1764 if( p_track->p_elst && p_track->p_elst->data.p_elst->i_entry_count > 0 )
1766 demux_sys_t *p_sys = p_input->p_demux_data;
1767 MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;
1768 int64_t i_mvt = MP4_TrackGetPTS( p_input, p_track ) * p_sys->i_timescale / (int64_t)1000000;
1770 if( p_track->i_elst < elst->i_entry_count &&
1771 i_mvt >= p_track->i_elst_time + elst->i_segment_duration[p_track->i_elst] )
1773 MP4_TrackSetELST( p_input, p_track, MP4_TrackGetPTS( p_input, p_track ) );
1780 static void MP4_TrackSetELST( input_thread_t *p_input, mp4_track_t *tk, int64_t i_time )
1782 demux_sys_t *p_sys = p_input->p_demux_data;
1783 int i_elst_last = tk->i_elst;
1785 /* handle elst (find the correct one) */
1787 tk->i_elst_time = 0;
1788 if( tk->p_elst && tk->p_elst->data.p_elst->i_entry_count > 0 )
1790 MP4_Box_data_elst_t *elst = tk->p_elst->data.p_elst;
1791 int64_t i_mvt= i_time * p_sys->i_timescale / (int64_t)1000000;
1793 for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
1795 mtime_t i_dur = elst->i_segment_duration[tk->i_elst];
1797 if( tk->i_elst_time <= i_mvt && i_mvt < tk->i_elst_time + i_dur )
1801 tk->i_elst_time += i_dur;
1804 if( tk->i_elst >= elst->i_entry_count )
1806 /* msg_Dbg( p_input, "invalid number of entry in elst" ); */
1807 tk->i_elst = elst->i_entry_count - 1;
1808 tk->i_elst_time -= elst->i_segment_duration[tk->i_elst];
1811 if( elst->i_media_time[tk->i_elst] < 0 )
1814 tk->i_elst_time += elst->i_segment_duration[tk->i_elst];
1817 if( i_elst_last != tk->i_elst )
1819 msg_Warn( p_input, "elst old=%d new=%d", i_elst_last, tk->i_elst );
1823 static char *LanguageGetName( const char *psz_code )
1825 const iso639_lang_t *pl;
1827 pl = GetLang_2B( psz_code );
1828 if( !strcmp( pl->psz_iso639_1, "??" ) )
1830 pl = GetLang_2T( psz_code );
1833 if( !strcmp( pl->psz_iso639_1, "??" ) )
1835 return strdup( psz_code );
1838 if( *pl->psz_native_name )
1840 return strdup( pl->psz_native_name );
1842 return strdup( pl->psz_eng_name );