1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.50 2004/01/09 04:37:43 jlj 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>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
43 set_description( _("MP4 demuxer") );
44 set_capability( "demux", 242 );
45 set_callbacks( Open, Close );
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Demux ( input_thread_t * );
52 static int DemuxRef( input_thread_t *p_input )
56 static int Seek ( input_thread_t *, mtime_t );
57 static int Control ( input_thread_t *, int, va_list );
59 /*****************************************************************************
60 * Declaration of local function
61 *****************************************************************************/
62 static void MP4_TrackCreate ( input_thread_t *, track_data_mp4_t *, MP4_Box_t *);
63 static void MP4_TrackDestroy( input_thread_t *, track_data_mp4_t * );
65 static int MP4_TrackSelect ( input_thread_t *, track_data_mp4_t *, mtime_t );
66 static void MP4_TrackUnselect(input_thread_t *, track_data_mp4_t * );
68 static int MP4_TrackSeek ( input_thread_t *, track_data_mp4_t *, mtime_t );
70 static uint64_t MP4_GetTrackPos ( track_data_mp4_t * );
71 static int MP4_TrackSampleSize( track_data_mp4_t * );
72 static int MP4_TrackNextSample( input_thread_t *, track_data_mp4_t * );
75 if( p ) { free( p ); (p) = NULL;}
77 /*****************************************************************************
78 * Open: check file and initializes MP4 structures
79 *****************************************************************************/
80 static int Open( vlc_object_t * p_this )
82 input_thread_t *p_input = (input_thread_t *)p_this;
93 vlc_bool_t b_seekable;
95 /* a little test to see if it could be a mp4 */
96 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
98 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
101 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
114 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
119 stream_Control( p_input->s, STREAM_CAN_SEEK, &b_seekable );
122 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
126 /*Set exported functions */
127 p_input->pf_demux = Demux;
128 p_input->pf_demux_control = Control;
130 /* create our structure that will contains all data */
131 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
132 memset( p_sys, 0, sizeof( demux_sys_t ) );
134 /* Now load all boxes ( except raw data ) */
135 if( ( p_sys->p_root = MP4_BoxGetRoot( p_input ) ) == NULL )
137 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
141 MP4_BoxDumpStructure( p_input, p_sys->p_root );
143 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
145 switch( p_ftyp->data.p_ftyp->i_major_brand )
149 "ISO Media file (isom) version %d.",
150 p_ftyp->data.p_ftyp->i_minor_version );
154 "unrecognized major file specification (%4.4s).",
155 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
161 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
164 /* the file need to have one moov box */
165 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
167 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
171 msg_Err( p_input, "MP4 plugin discarded (no moov box)" );
174 /* we have a free box as a moov, rename it */
175 p_foov->i_type = FOURCC_moov;
178 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
180 playlist_t *p_playlist;
181 int i_count = MP4_BoxCount( p_rmra, "rmda" );
184 msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );
187 (playlist_t *)vlc_object_find( p_input,
192 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
193 for( i = 0; i < i_count; i++ )
195 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
199 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
203 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
205 msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
206 psz_ref, (char*)&i_ref_type );
208 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
210 if( strstr( psz_ref, "qt5gateQT" ) )
212 msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
215 if( !strncmp( psz_ref, "http://", 7 ) ||
216 !strncmp( psz_ref, "rtsp://", 7 ) )
218 msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
219 playlist_Add( p_playlist, psz_ref, psz_ref,
220 PLAYLIST_APPEND, PLAYLIST_END );
224 /* msg dbg relative ? */
225 char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
226 char *end = strrchr( p_input->psz_source, '/' );
230 int i_len = end + 1 - p_input->psz_source;
232 strncpy( psz_absolute, p_input->psz_source, i_len);
233 psz_absolute[i_len] = '\0';
237 strcpy( psz_absolute, "" );
239 strcat( psz_absolute, psz_ref );
240 msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
241 playlist_Add( p_playlist, psz_absolute, psz_absolute,
242 PLAYLIST_APPEND, PLAYLIST_END );
247 msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)", (char*)&p_rdrf->data.p_rdrf->i_ref_type );
250 vlc_object_release( p_playlist );
254 msg_Err( p_input, "can't find playlist" );
258 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
262 msg_Err( p_input, "cannot find /moov/mvhd" );
267 msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
268 p_input->pf_demux = DemuxRef;
274 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
275 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
278 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
280 msg_Err( p_input, "cannot find any /moov/trak" );
283 msg_Dbg( p_input, "find %d track%c",
285 p_sys->i_tracks ? 's':' ' );
287 /* create one program */
288 vlc_mutex_lock( &p_input->stream.stream_lock );
289 if( input_InitStream( p_input, 0 ) == -1)
291 vlc_mutex_unlock( &p_input->stream.stream_lock );
292 msg_Err( p_input, "cannot init stream" );
295 if( p_sys->i_duration/p_sys->i_timescale > 0 )
297 p_input->stream.i_mux_rate =
298 p_input->stream.p_selected_area->i_size / 50 /
299 ( p_sys->i_duration / p_sys->i_timescale );
303 p_input->stream.i_mux_rate = 0;
305 vlc_mutex_unlock( &p_input->stream.stream_lock );
308 /* allocate memory */
309 p_sys->track = calloc( p_sys->i_tracks, sizeof( track_data_mp4_t ) );
310 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( track_data_mp4_t ) );
312 /* now process each track and extract all usefull informations */
313 for( i = 0; i < p_sys->i_tracks; i++ )
315 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
316 MP4_TrackCreate( p_input, &p_sys->track[i], p_trak );
318 if( p_sys->track[i].b_ok )
321 switch( p_sys->track[i].fmt.i_cat )
334 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %s",
335 p_sys->track[i].i_track_ID,
337 p_sys->track[i].b_enable ? "enable":"disable",
338 p_sys->track[i].fmt.psz_language ? p_sys->track[i].fmt.psz_language : "undef" );
342 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_sys->track[i].i_track_ID );
351 MP4_BoxFree( p_input, p_sys->p_root );
357 /*****************************************************************************
358 * Demux: read packet and send them to decoders
359 *****************************************************************************
360 * TODO check for newly selected track (ie audio upt to now )
361 *****************************************************************************/
362 static int Demux( input_thread_t *p_input )
364 demux_sys_t *p_sys = p_input->p_demux_data;
365 unsigned int i_track;
368 unsigned int i_track_selected;
369 vlc_bool_t b_play_audio;
371 /* check for newly selected/unselected track */
372 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
374 #define track p_sys->track[i_track]
375 if( track.b_selected && track.i_sample >= track.i_sample_count )
377 msg_Warn( p_input, "track[0x%x] will be disabled", track.i_track_ID );
378 MP4_TrackUnselect( p_input, &track );
380 else if( track.b_ok )
383 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, track.p_es, &b );
385 if( track.b_selected && !b )
387 MP4_TrackUnselect( p_input, &track );
389 else if( !track.b_selected && b)
391 MP4_TrackSelect( p_input, &track, MP4_GetMoviePTS( p_sys ) );
394 if( track.b_selected )
402 if( i_track_selected <= 0 )
404 msg_Warn( p_input, "no track selected, exiting..." );
408 /* first wait for the good time to read a packet */
409 input_ClockManageRef( p_input,
410 p_input->stream.p_selected_program,
413 /* update pcr XXX in mpeg scale so in 90000 unit/s */
414 p_sys->i_pcr = MP4_GetMoviePTS( p_sys ) * 9 / 100;
416 /* we will read 100ms for each stream so ...*/
417 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
419 /* Check if we need to send the audio data to decoder */
420 b_play_audio = !p_input->stream.control.b_mute;
422 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
424 #define track p_sys->track[i_track]
427 MP4_GetTrackPTS( &track ) >= MP4_GetMoviePTS( p_sys ) )
432 while( MP4_GetTrackPTS( &track ) < MP4_GetMoviePTS( p_sys ) )
435 if( !b_play_audio && track.fmt.i_cat == AUDIO_ES )
437 if( MP4_TrackNextSample( p_input, &track ) )
447 if( stream_Seek( p_input->s, MP4_GetTrackPos( &track ) ) )
449 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
450 MP4_TrackUnselect( p_input, &track );
455 if( ( p_block = stream_Block( p_input->s,
456 MP4_TrackSampleSize( &track ) ) ) == NULL )
458 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
459 MP4_TrackUnselect( p_input, &track );
463 if( track.b_drms && track.p_drms )
465 drms_decrypt( track.p_drms,
466 (uint32_t *)p_block->p_buffer,
470 if( track.fmt.i_cat == VIDEO_ES )
472 /* FIXME sometime we can calculate PTS */
475 input_ClockGetTS( p_input,
476 p_input->stream.p_selected_program,
477 MP4_GetTrackPTS( &track ) * 9/100 );
483 input_ClockGetTS( p_input,
484 p_input->stream.p_selected_program,
485 MP4_GetTrackPTS( &track ) * 9/100 );
488 if( !track.b_drms || ( track.b_drms && track.p_drms ) )
490 es_out_Send( p_input->p_es_out, track.p_es, p_block );
493 if( MP4_TrackNextSample( p_input, &track ) )
504 /*****************************************************************************
505 * Seek: Got to i_date
506 ******************************************************************************/
507 static int Seek ( input_thread_t *p_input, mtime_t i_date )
509 demux_sys_t *p_sys = p_input->p_demux_data;
510 unsigned int i_track;
511 /* First update update global time */
512 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
513 p_sys->i_pcr = i_date* 9 / 100;
515 /* Now for each stream try to go to this time */
516 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
518 #define track p_sys->track[i_track]
519 if( track.b_ok && track.b_selected )
521 MP4_TrackSeek( p_input, &track, i_date );
528 /*****************************************************************************
530 *****************************************************************************/
531 static int Control ( input_thread_t *p_input, int i_query, va_list args )
533 demux_sys_t *p_sys = p_input->p_demux_data;
540 case DEMUX_GET_POSITION:
541 pf = (double*)va_arg( args, double * );
542 if( p_sys->i_duration > 0 )
544 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
552 case DEMUX_SET_POSITION:
553 f = (double)va_arg( args, double );
556 (double)p_sys->i_duration /
557 (double)p_sys->i_timescale );
558 return Seek( p_input, i64 );
561 pi64 = (int64_t*)va_arg( args, int64_t * );
562 *pi64 = (mtime_t)1000000 *
563 (mtime_t)p_sys->i_time /
564 (mtime_t)p_sys->i_timescale;
568 i64 = (int64_t)va_arg( args, int64_t );
569 return Seek( p_input, i64 );
571 case DEMUX_GET_LENGTH:
572 pi64 = (int64_t*)va_arg( args, int64_t * );
573 *pi64 = (mtime_t)1000000 *
574 (mtime_t)p_sys->i_duration /
575 (mtime_t)p_sys->i_timescale;
578 msg_Warn( p_input, "DEMUX_GET_FPS unimplemented !!" );
581 msg_Err( p_input, "control query unimplemented !!!" );
582 return demux_vaControlDefault( p_input, i_query, args );
586 /*****************************************************************************
587 * Close: frees unused data
588 *****************************************************************************/
589 static void Close ( vlc_object_t * p_this )
591 unsigned int i_track;
592 input_thread_t * p_input = (input_thread_t *)p_this;
593 demux_sys_t *p_sys = p_input->p_demux_data;
595 msg_Dbg( p_input, "freeing all memory" );
597 MP4_BoxFree( p_input, p_sys->p_root );
598 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
600 MP4_TrackDestroy( p_input, &p_sys->track[i_track] );
602 FREE( p_sys->track );
609 /****************************************************************************
610 * Local functions, specific to vlc
611 ****************************************************************************/
613 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
614 static int TrackCreateChunksIndex( input_thread_t *p_input,
615 track_data_mp4_t *p_demux_track )
617 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
620 unsigned int i_chunk;
621 unsigned int i_index, i_last;
623 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
624 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
625 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
627 return( VLC_EGENERIC );
630 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
631 if( !p_demux_track->i_chunk_count )
633 msg_Warn( p_input, "no chunk defined" );
634 return( VLC_EGENERIC );
636 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
637 sizeof( chunk_data_mp4_t ) );
639 /* first we read chunk offset */
640 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
642 p_demux_track->chunk[i_chunk].i_offset =
643 p_co64->data.p_co64->i_chunk_offset[i_chunk];
646 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
647 to be used for the sample XXX begin to 1
648 We construct it begining at the end */
649 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
650 i_index = p_stsc->data.p_stsc->i_entry_count;
653 msg_Warn( p_input, "cannot read chunk table or table empty" );
654 return( VLC_EGENERIC );
660 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
661 i_chunk < i_last; i_chunk++ )
663 p_demux_track->chunk[i_chunk].i_sample_description_index =
664 p_stsc->data.p_stsc->i_sample_description_index[i_index];
665 p_demux_track->chunk[i_chunk].i_sample_count =
666 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
668 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
671 p_demux_track->chunk[0].i_sample_first = 0;
672 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
674 p_demux_track->chunk[i_chunk].i_sample_first =
675 p_demux_track->chunk[i_chunk-1].i_sample_first +
676 p_demux_track->chunk[i_chunk-1].i_sample_count;
680 "track[Id 0x%x] read %d chunk",
681 p_demux_track->i_track_ID,
682 p_demux_track->i_chunk_count );
684 return( VLC_SUCCESS );
686 static int TrackCreateSamplesIndex( input_thread_t *p_input,
687 track_data_mp4_t *p_demux_track )
689 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
690 ctts make same mapping but for composition time,
691 not yet used and probably not usefull */
692 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
693 that uses a compressed form FIXME make them in libmp4
695 /* TODO use also stss and stsh table for seeking */
696 /* FIXME use edit table */
701 int64_t i_index_sample_used;
705 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
706 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
708 if( ( !p_stts )||( !p_stsz ) )
710 msg_Warn( p_input, "cannot read sample table" );
711 return( VLC_EGENERIC );
714 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
717 /* for sample size, there are 2 case */
718 if( p_stsz->data.p_stsz->i_sample_size )
720 /* 1: all sample have the same size, so no need to construct a table */
721 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
722 p_demux_track->p_sample_size = NULL;
726 /* 2: each sample can have a different size */
727 p_demux_track->i_sample_size = 0;
728 p_demux_track->p_sample_size =
729 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
731 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
733 p_demux_track->p_sample_size[i_sample] =
734 p_stsz->data.p_stsz->i_entry_size[i_sample];
737 /* we have extract all information from stsz,
740 /* if we don't want to waste too much memory, we can't expand
741 the box !, so each chunk will contain an "extract" of this table
745 i_index = 0; i_index_sample_used =0;
747 /* create and init last data for each chunk */
748 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
751 int64_t i_entry, i_sample_count, i;
753 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
754 /* count how many entries needed for this chunk
755 for p_sample_delta_dts and p_sample_count_dts */
757 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
760 while( i_sample_count > 0 )
762 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
765 i_sample_count += i_index_sample_used; /* don't count already used sample
772 p_demux_track->chunk[i_chunk].p_sample_count_dts =
773 calloc( i_entry, sizeof( uint32_t ) );
774 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
775 calloc( i_entry, sizeof( uint32_t ) );
778 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
779 for( i = 0; i < i_entry; i++ )
784 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
786 i_used = __MIN( i_rest, i_sample_count );
788 i_index_sample_used += i_used;
789 i_sample_count -= i_used;
791 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
793 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
794 p_stts->data.p_stts->i_sample_delta[i_index];
796 i_last_dts += i_used *
797 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
799 if( i_index_sample_used >=
800 p_stts->data.p_stts->i_sample_count[i_index] )
804 i_index_sample_used = 0;
811 "track[Id 0x%x] read %d samples length:"I64Fd"s",
812 p_demux_track->i_track_ID,
813 p_demux_track->i_sample_count,
814 i_last_dts / p_demux_track->i_timescale );
816 return( VLC_SUCCESS );
821 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
823 static int TrackCreateES ( input_thread_t *p_input,
824 track_data_mp4_t *p_track,
825 unsigned int i_chunk,
826 es_out_id_t **pp_es )
833 if( !p_track->chunk[i_chunk].i_sample_description_index )
836 "invalid SampleEntry index (track[Id 0x%x])",
837 p_track->i_track_ID );
841 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
842 p_track->chunk[i_chunk].i_sample_description_index - 1 );
844 if( !p_sample || !p_sample->data.p_data )
847 "cannot find SampleEntry (track[Id 0x%x])",
848 p_track->i_track_ID );
849 return( VLC_EGENERIC );
852 p_track->p_sample = p_sample;
854 if( p_track->i_sample_size == 1 )
856 MP4_Box_data_sample_soun_t *p_soun;
858 p_soun = p_sample->data.p_sample_soun;
860 if( p_soun->i_qt_version == 0 )
862 switch( p_sample->i_type )
864 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
865 p_soun->i_qt_version = 1;
866 p_soun->i_sample_per_packet = 64;
867 p_soun->i_bytes_per_packet = 34;
868 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
869 p_soun->i_bytes_per_sample = 2;
871 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
872 p_soun->i_qt_version = 1;
873 p_soun->i_sample_per_packet = 6;
874 p_soun->i_bytes_per_packet = 2;
875 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
876 p_soun->i_bytes_per_sample = 2;
878 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
879 p_soun->i_qt_version = 1;
880 p_soun->i_sample_per_packet = 12;
881 p_soun->i_bytes_per_packet = 2;
882 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
883 p_soun->i_bytes_per_sample = 2;
890 else if( p_soun->i_qt_version == 1 && p_soun->i_sample_per_packet <= 0 )
892 p_soun->i_qt_version = 0;
897 /* It's a little ugly but .. there are special cases */
898 switch( p_sample->i_type )
900 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
901 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
902 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
904 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
905 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
907 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
908 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
911 p_track->fmt.i_codec = p_sample->i_type;
915 /* now see if esds is present and if so create a data packet
916 with decoder_specific_info */
917 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
918 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
919 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
920 ( p_esds->data.p_esds )&&
923 /* First update information based on i_objectTypeIndication */
924 switch( p_decconfig->i_objectTypeIndication )
926 case( 0x20 ): /* MPEG4 VIDEO */
927 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
930 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
937 case( 0x65): /* MPEG2 video */
938 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
940 /* Theses are MPEG2-AAC */
941 case( 0x66): /* main profile */
942 case( 0x67): /* Low complexity profile */
943 case( 0x68): /* Scaleable Sampling rate profile */
944 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
946 /* true MPEG 2 audio */
948 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
950 case( 0x6a): /* MPEG1 video */
951 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
953 case( 0x6b): /* MPEG1 audio */
954 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
956 case( 0x6c ): /* jpeg */
957 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
960 /* Unknown entry, but don't touch i_fourcc */
962 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
963 p_decconfig->i_objectTypeIndication,
964 p_track->i_track_ID );
967 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
968 if( p_track->fmt.i_extra > 0 )
970 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
971 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
972 p_track->fmt.i_extra );
977 switch( p_sample->i_type )
979 /* qt decoder, send the complete chunk */
980 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
981 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
982 case VLC_FOURCC( 'V', 'P', '3', '1' ):
983 case VLC_FOURCC( '3', 'I', 'V', '1' ):
984 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
985 p_track->fmt.i_extra =
986 p_sample->data.p_sample_vide->i_qt_image_description;
987 if( p_track->fmt.i_extra > 0 )
989 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
990 memcpy( p_track->fmt.p_extra,
991 p_sample->data.p_sample_vide->p_qt_image_description,
992 p_track->fmt.i_extra);
995 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
996 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
997 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
998 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
999 p_track->fmt.i_extra =
1000 p_sample->data.p_sample_soun->i_qt_description;
1001 if( p_track->fmt.i_extra > 0 )
1003 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
1004 memcpy( p_track->fmt.p_extra,
1005 p_sample->data.p_sample_soun->p_qt_description,
1006 p_track->fmt.i_extra);
1016 /* some last initialisation */
1017 switch( p_track->fmt.i_cat )
1020 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1021 p_track->fmt.video.i_height = p_sample->data.p_sample_vide->i_height;
1023 /* fall on display size */
1024 if( p_track->fmt.video.i_width <= 0 )
1025 p_track->fmt.video.i_width = p_track->i_width;
1026 if( p_track->fmt.video.i_height <= 0 )
1027 p_track->fmt.video.i_height = p_track->i_height;
1029 /* Find out apect ratio from display size */
1030 if( p_track->i_width > 0 && p_track->i_height > 0 )
1031 p_track->fmt.video.i_aspect =
1032 VOUT_ASPECT_FACTOR * p_track->i_width / p_track->i_height;
1037 p_track->fmt.audio.i_channels =
1038 p_sample->data.p_sample_soun->i_channelcount;
1039 p_track->fmt.audio.i_rate =
1040 p_sample->data.p_sample_soun->i_sampleratehi;
1041 p_track->fmt.i_bitrate = p_sample->data.p_sample_soun->i_channelcount *
1042 p_sample->data.p_sample_soun->i_sampleratehi *
1043 p_sample->data.p_sample_soun->i_samplesize;
1044 p_track->fmt.audio.i_bitspersample =
1045 p_sample->data.p_sample_soun->i_samplesize;
1052 *pp_es = es_out_Add( p_input->p_es_out, &p_track->fmt );
1057 /* given a time it return sample/chunk */
1058 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1059 track_data_mp4_t *p_track,
1062 uint32_t *pi_sample )
1066 unsigned int i_sample;
1067 unsigned int i_chunk;
1070 /* convert absolute time to in timescale unit */
1071 i_start = i_start * (mtime_t)p_track->i_timescale / (mtime_t)1000000;
1073 /* FIXME see if it's needed to check p_track->i_chunk_count */
1074 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1076 return( VLC_EGENERIC );
1079 /* we start from sample 0/chunk 0, hope it won't take too much time */
1080 /* *** find good chunk *** */
1081 for( i_chunk = 0; ; i_chunk++ )
1083 if( i_chunk + 1 >= p_track->i_chunk_count )
1085 /* at the end and can't check if i_start in this chunk,
1086 it will be check while searching i_sample */
1087 i_chunk = p_track->i_chunk_count - 1;
1091 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1092 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1098 /* *** find sample in the chunk *** */
1099 i_sample = p_track->chunk[i_chunk].i_sample_first;
1100 i_dts = p_track->chunk[i_chunk].i_first_dts;
1101 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1104 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1105 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1108 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1109 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1111 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1116 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1120 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1125 if( i_sample >= p_track->i_sample_count )
1128 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1129 p_track->i_track_ID, i_chunk, i_sample );
1130 return( VLC_EGENERIC );
1134 /* *** Try to find nearest sync points *** */
1135 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1137 unsigned int i_index;
1139 "track[Id 0x%x] using Sync Sample Box (stss)",
1140 p_track->i_track_ID );
1141 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1143 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1147 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1149 p_stss->data.p_stss->i_sample_number[i_index-1] );
1150 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1151 /* new i_sample is less than old so i_chunk can only decreased */
1152 while( i_chunk > 0 &&
1153 i_sample < p_track->chunk[i_chunk].i_sample_first )
1160 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1162 p_stss->data.p_stss->i_sample_number[i_index] );
1163 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1164 /* new i_sample is more than old so i_chunk can only increased */
1165 while( i_chunk < p_track->i_chunk_count - 1 &&
1166 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1167 p_track->chunk[i_chunk].i_sample_count )
1179 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1180 p_track->i_track_ID );
1183 if( pi_chunk ) *pi_chunk = i_chunk;
1184 if( pi_sample ) *pi_sample = i_sample;
1185 return( VLC_SUCCESS );
1188 static int TrackGotoChunkSample( input_thread_t *p_input,
1189 track_data_mp4_t *p_track,
1190 unsigned int i_chunk,
1191 unsigned int i_sample )
1193 vlc_bool_t b_reselect = VLC_FALSE;
1195 /* now see if actual es is ok */
1196 if( p_track->i_chunk < 0 ||
1197 p_track->i_chunk >= p_track->i_chunk_count ||
1198 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1199 p_track->chunk[i_chunk].i_sample_description_index )
1201 msg_Warn( p_input, "Recreate ES" );
1203 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_track->p_es, &b_reselect );
1205 es_out_Del( p_input->p_es_out, p_track->p_es );
1207 p_track->p_es = NULL;
1209 if( TrackCreateES( p_input,
1213 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1214 p_track->i_track_ID );
1216 p_track->b_ok = VLC_FALSE;
1217 p_track->b_selected = VLC_FALSE;
1218 return VLC_EGENERIC;
1222 /* select again the new decoder */
1225 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES, p_track->p_es );
1228 p_track->i_chunk = i_chunk;
1229 p_track->i_sample = i_sample;
1231 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1234 /****************************************************************************
1236 ****************************************************************************
1237 * Parse track information and create all needed data to run a track
1238 * If it succeed b_ok is set to 1 else to 0
1239 ****************************************************************************/
1240 static void MP4_TrackCreate( input_thread_t *p_input,
1241 track_data_mp4_t *p_track,
1242 MP4_Box_t * p_box_trak )
1244 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1245 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1259 /* hint track unsuported */
1261 /* set default value (-> track unusable) */
1262 p_track->b_ok = VLC_FALSE;
1263 p_track->b_enable = VLC_FALSE;
1264 p_track->b_selected = VLC_FALSE;
1266 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1273 /* do we launch this track by default ? */
1275 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1277 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1278 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1279 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1281 if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1283 /* msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
1288 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1291 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1292 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1294 if( ( !p_mdhd )||( !p_hdlr ) )
1299 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1301 for( i = 0; i < 3; i++ )
1303 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1307 switch( p_hdlr->data.p_hdlr->i_handler_type )
1309 case( FOURCC_soun ):
1310 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1314 p_track->fmt.i_cat = AUDIO_ES;
1317 case( FOURCC_vide ):
1318 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1322 p_track->fmt.i_cat = VIDEO_ES;
1330 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1332 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1333 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1338 p_drms = MP4_BoxGet( p_track->p_stsd, "drms" );
1339 p_track->b_drms = p_drms != NULL;
1340 p_track->p_drms = p_track->b_drms ?
1341 p_drms->data.p_sample_soun->p_drms : NULL;
1344 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1346 p_track->fmt.psz_language = strdup( language );
1349 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1350 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1352 MP4_Box_t *p_sample;
1354 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1355 if( p_sample && p_sample->data.p_sample_soun)
1357 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1358 if( p_soun->i_qt_version == 0 &&
1359 p_track->i_timescale != p_soun->i_sampleratehi )
1362 "i_timescale ("I64Fu") != i_sampleratehi (%u) with "
1364 "Making both equal. (report any problem)",
1365 p_track->i_timescale, p_soun->i_sampleratehi );
1367 if( p_soun->i_sampleratehi )
1368 p_track->i_timescale = p_soun->i_sampleratehi;
1370 p_soun->i_sampleratehi = p_track->i_timescale;
1375 /* Create chunk index table and sample index table */
1376 if( TrackCreateChunksIndex( p_input,p_track ) ||
1377 TrackCreateSamplesIndex( p_input, p_track ) )
1379 return; /* cannot create chunks index */
1382 p_track->i_chunk = 0;
1383 p_track->i_sample = 0;
1386 if( TrackCreateES( p_input,
1387 p_track, p_track->i_chunk,
1390 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1391 p_track->i_track_ID );
1398 for( i = 0; i < p_track->i_chunk_count; i++ )
1400 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1405 p_track->b_ok = VLC_TRUE;
1408 /****************************************************************************
1410 ****************************************************************************
1411 * Destroy a track created by MP4_TrackCreate.
1412 ****************************************************************************/
1413 static void MP4_TrackDestroy( input_thread_t *p_input,
1414 track_data_mp4_t *p_track )
1416 unsigned int i_chunk;
1418 p_track->b_ok = VLC_FALSE;
1419 p_track->b_enable = VLC_FALSE;
1420 p_track->b_selected = VLC_FALSE;
1422 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1424 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1426 if( p_track->chunk )
1428 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1429 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1432 FREE( p_track->chunk );
1434 if( !p_track->i_sample_size )
1436 FREE( p_track->p_sample_size );
1440 static int MP4_TrackSelect ( input_thread_t *p_input,
1441 track_data_mp4_t *p_track,
1447 if( !p_track->b_ok )
1449 return( VLC_EGENERIC );
1452 if( p_track->b_selected )
1455 "track[Id 0x%x] already selected",
1456 p_track->i_track_ID );
1457 return( VLC_SUCCESS );
1460 if( TrackTimeToSampleChunk( p_input,
1462 &i_chunk, &i_sample ) )
1465 "cannot select track[Id 0x%x]",
1466 p_track->i_track_ID );
1467 return( VLC_EGENERIC );
1470 p_track->b_selected = VLC_TRUE;
1472 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1474 p_track->b_selected = VLC_FALSE;
1476 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1479 static void MP4_TrackUnselect(input_thread_t *p_input,
1480 track_data_mp4_t *p_track )
1482 if( !p_track->b_ok )
1487 if( !p_track->b_selected )
1490 "track[Id 0x%x] already unselected",
1491 p_track->i_track_ID );
1496 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_track->p_es, VLC_FALSE );
1499 p_track->b_selected = VLC_FALSE;
1502 static int MP4_TrackSeek ( input_thread_t *p_input,
1503 track_data_mp4_t *p_track,
1509 if( !p_track->b_ok )
1511 return( VLC_EGENERIC );
1514 if( TrackTimeToSampleChunk( p_input,
1516 &i_chunk, &i_sample ) )
1519 "cannot select track[Id 0x%x]",
1520 p_track->i_track_ID );
1521 return( VLC_EGENERIC );
1524 p_track->b_selected = VLC_TRUE;
1526 TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample );
1528 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1536 * 3 types: for audio
1539 #define QT_V0_MAX_SAMPLES 1500
1540 static int MP4_TrackSampleSize( track_data_mp4_t *p_track )
1543 MP4_Box_data_sample_soun_t *p_soun;
1545 if( p_track->i_sample_size == 0 )
1547 /* most simple case */
1548 return( p_track->p_sample_size[p_track->i_sample] );
1550 if( p_track->fmt.i_cat != AUDIO_ES )
1552 return( p_track->i_sample_size );
1555 if( p_track->i_sample_size != 1 )
1557 //msg_Warn( p_input, "SampleSize != 1" );
1558 return( p_track->i_sample_size );
1561 p_soun = p_track->p_sample->data.p_sample_soun;
1563 if( p_soun->i_qt_version == 1 )
1565 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1570 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1571 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1572 if( i_samples > QT_V0_MAX_SAMPLES )
1574 i_samples = QT_V0_MAX_SAMPLES;
1576 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1579 //fprintf( stderr, "size=%d\n", i_size );
1584 static uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
1586 unsigned int i_sample;
1590 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1592 if( p_track->i_sample_size )
1594 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1596 if( p_soun->i_qt_version == 0 )
1598 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1599 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1603 /* we read chunk by chunk */
1609 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1610 i_sample < p_track->i_sample; i_sample++ )
1612 i_pos += p_track->p_sample_size[i_sample];
1619 static int MP4_TrackNextSample( input_thread_t *p_input,
1620 track_data_mp4_t *p_track )
1623 if( p_track->fmt.i_cat == AUDIO_ES &&
1624 p_track->i_sample_size != 0 )
1626 MP4_Box_data_sample_soun_t *p_soun;
1628 p_soun = p_track->p_sample->data.p_sample_soun;
1630 if( p_soun->i_qt_version == 1 )
1632 /* chunk by chunk */
1634 p_track->chunk[p_track->i_chunk].i_sample_first +
1635 p_track->chunk[p_track->i_chunk].i_sample_count;
1640 p_track->i_sample += QT_V0_MAX_SAMPLES;
1641 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 )
1644 p_track->chunk[p_track->i_chunk].i_sample_first +
1645 p_track->chunk[p_track->i_chunk].i_sample_count;
1651 p_track->i_sample++;
1654 if( p_track->i_sample >= p_track->i_sample_count )
1656 /* we have reach end of the track so free decoder stuff */
1657 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1658 MP4_TrackUnselect( p_input, p_track );
1659 return( VLC_EGENERIC );
1662 /* Have we changed chunk ? */
1663 if( p_track->i_sample >=
1664 p_track->chunk[p_track->i_chunk].i_sample_first +
1665 p_track->chunk[p_track->i_chunk].i_sample_count )
1667 if( TrackGotoChunkSample( p_input,
1669 p_track->i_chunk + 1,
1670 p_track->i_sample ) )
1672 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1673 MP4_TrackUnselect( p_input, p_track );
1674 return( VLC_EGENERIC );
1678 return( VLC_SUCCESS );