1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.43 2003/11/27 12:32:51 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>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
42 set_description( _("MP4 demuxer") );
43 set_capability( "demux", 242 );
44 set_callbacks( Open, Close );
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Demux ( input_thread_t * );
51 static int DemuxRef( input_thread_t *p_input )
55 static int Seek ( input_thread_t *, mtime_t );
56 static int Control ( input_thread_t *, int, va_list );
58 /*****************************************************************************
59 * Declaration of local function
60 *****************************************************************************/
61 static void MP4_TrackCreate ( input_thread_t *, track_data_mp4_t *, MP4_Box_t *);
62 static void MP4_TrackDestroy( input_thread_t *, track_data_mp4_t * );
64 static int MP4_TrackSelect ( input_thread_t *, track_data_mp4_t *, mtime_t );
65 static void MP4_TrackUnselect(input_thread_t *, track_data_mp4_t * );
67 static int MP4_TrackSeek ( input_thread_t *, track_data_mp4_t *, mtime_t );
69 static uint64_t MP4_GetTrackPos ( track_data_mp4_t * );
70 static int MP4_TrackSampleSize( track_data_mp4_t * );
71 static int MP4_TrackNextSample( input_thread_t *, track_data_mp4_t * );
74 if( p ) { free( p ); (p) = NULL;}
76 /*****************************************************************************
77 * Open: check file and initializes MP4 structures
78 *****************************************************************************/
79 static int Open( vlc_object_t * p_this )
81 input_thread_t *p_input = (input_thread_t *)p_this;
92 vlc_bool_t b_seekable;
94 /* a little test to see if it could be a mp4 */
95 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
97 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
100 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
113 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
118 stream_Control( p_input->s, STREAM_CAN_SEEK, &b_seekable );
121 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
125 /*Set exported functions */
126 p_input->pf_demux = Demux;
127 p_input->pf_demux_control = Control;
129 /* create our structure that will contains all data */
130 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
131 memset( p_sys, 0, sizeof( demux_sys_t ) );
133 /* Now load all boxes ( except raw data ) */
134 if( ( p_sys->p_root = MP4_BoxGetRoot( p_input ) ) == NULL )
136 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
140 MP4_BoxDumpStructure( p_input, p_sys->p_root );
142 if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
144 switch( p_ftyp->data.p_ftyp->i_major_brand )
148 "ISO Media file (isom) version %d.",
149 p_ftyp->data.p_ftyp->i_minor_version );
153 "unrecognized major file specification (%4.4s).",
154 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
160 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
163 /* the file need to have one moov box */
164 if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
166 MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
170 msg_Err( p_input, "MP4 plugin discarded (no moov box)" );
173 /* we have a free box as a moov, rename it */
174 p_foov->i_type = FOURCC_moov;
177 if( ( p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" ) ) )
179 playlist_t *p_playlist;
180 int i_count = MP4_BoxCount( p_rmra, "rmda" );
183 msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );
186 (playlist_t *)vlc_object_find( p_input,
191 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
192 for( i = 0; i < i_count; i++ )
194 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
198 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
202 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
204 msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
205 psz_ref, (char*)&i_ref_type );
207 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
209 if( strstr( psz_ref, "qt5gateQT" ) )
211 msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
214 if( !strncmp( psz_ref, "http://", 7 ) ||
215 !strncmp( psz_ref, "rtsp://", 7 ) )
217 msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
218 playlist_Add( p_playlist, psz_ref, 0, 0,
219 PLAYLIST_APPEND, PLAYLIST_END );
223 /* msg dbg relative ? */
224 char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
225 char *end = strrchr( p_input->psz_source, '/' );
229 int i_len = end + 1 - p_input->psz_source;
231 strncpy( psz_absolute, p_input->psz_source, i_len);
232 psz_absolute[i_len] = '\0';
236 strcpy( psz_absolute, "" );
238 strcat( psz_absolute, psz_ref );
239 msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
240 playlist_Add( p_playlist, psz_absolute, 0, 0,
241 PLAYLIST_APPEND, PLAYLIST_END );
246 msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)", (char*)&p_rdrf->data.p_rdrf->i_ref_type );
249 vlc_object_release( p_playlist );
253 msg_Err( p_input, "can't find playlist" );
257 if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
261 msg_Err( p_input, "cannot find /moov/mvhd" );
266 msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
267 p_input->pf_demux = DemuxRef;
273 p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
274 p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
277 if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
279 msg_Err( p_input, "cannot find any /moov/trak" );
282 msg_Dbg( p_input, "find %d track%c",
284 p_sys->i_tracks ? 's':' ' );
286 /* create one program */
287 vlc_mutex_lock( &p_input->stream.stream_lock );
288 if( input_InitStream( p_input, 0 ) == -1)
290 vlc_mutex_unlock( &p_input->stream.stream_lock );
291 msg_Err( p_input, "cannot init stream" );
294 if( p_sys->i_duration/p_sys->i_timescale > 0 )
296 p_input->stream.i_mux_rate =
297 p_input->stream.p_selected_area->i_size / 50 /
298 ( p_sys->i_duration / p_sys->i_timescale );
302 p_input->stream.i_mux_rate = 0;
304 vlc_mutex_unlock( &p_input->stream.stream_lock );
307 /* allocate memory */
308 p_sys->track = calloc( p_sys->i_tracks, sizeof( track_data_mp4_t ) );
309 memset( p_sys->track, 0, p_sys->i_tracks * sizeof( track_data_mp4_t ) );
311 /* now process each track and extract all usefull informations */
312 for( i = 0; i < p_sys->i_tracks; i++ )
314 p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
315 MP4_TrackCreate( p_input, &p_sys->track[i], p_trak );
317 if( p_sys->track[i].b_ok )
320 switch( p_sys->track[i].fmt.i_cat )
333 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %s",
334 p_sys->track[i].i_track_ID,
336 p_sys->track[i].b_enable ? "enable":"disable",
337 p_sys->track[i].fmt.psz_language ? p_sys->track[i].fmt.psz_language : "undef" );
341 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_sys->track[i].i_track_ID );
350 MP4_BoxFree( p_input, p_sys->p_root );
356 /*****************************************************************************
357 * Demux: read packet and send them to decoders
358 *****************************************************************************
359 * TODO check for newly selected track (ie audio upt to now )
360 *****************************************************************************/
361 static int Demux( input_thread_t *p_input )
363 demux_sys_t *p_sys = p_input->p_demux_data;
364 unsigned int i_track;
367 unsigned int i_track_selected;
368 vlc_bool_t b_play_audio;
370 /* check for newly selected/unselected track */
371 for( i_track = 0, i_track_selected = 0; i_track < p_sys->i_tracks; i_track++ )
373 #define track p_sys->track[i_track]
374 if( track.b_selected && track.i_sample >= track.i_sample_count )
376 msg_Warn( p_input, "track[0x%x] will be disabled", track.i_track_ID );
377 MP4_TrackUnselect( p_input, &track );
379 else if( track.b_ok )
382 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, track.p_es, &b );
384 if( track.b_selected && !b )
386 MP4_TrackUnselect( p_input, &track );
388 else if( !track.b_selected && b)
390 MP4_TrackSelect( p_input, &track, MP4_GetMoviePTS( p_sys ) );
393 if( track.b_selected )
401 if( i_track_selected <= 0 )
403 msg_Warn( p_input, "no track selected, exiting..." );
407 /* first wait for the good time to read a packet */
408 input_ClockManageRef( p_input,
409 p_input->stream.p_selected_program,
412 /* update pcr XXX in mpeg scale so in 90000 unit/s */
413 p_sys->i_pcr = MP4_GetMoviePTS( p_sys ) * 9 / 100;
415 /* we will read 100ms for each stream so ...*/
416 p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
418 /* Check if we need to send the audio data to decoder */
419 b_play_audio = !p_input->stream.control.b_mute;
421 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
423 #define track p_sys->track[i_track]
426 MP4_GetTrackPTS( &track ) >= MP4_GetMoviePTS( p_sys ) )
431 while( MP4_GetTrackPTS( &track ) < MP4_GetMoviePTS( p_sys ) )
434 if( !b_play_audio && track.fmt.i_cat == AUDIO_ES )
436 if( MP4_TrackNextSample( p_input, &track ) )
446 if( stream_Seek( p_input->s, MP4_GetTrackPos( &track ) ) )
448 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
449 MP4_TrackUnselect( p_input, &track );
454 if( ( p_block = stream_Block( p_input->s,
455 MP4_TrackSampleSize( &track ) ) ) == NULL )
457 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
458 MP4_TrackUnselect( p_input, &track );
462 if( track.fmt.i_cat == VIDEO_ES )
464 /* FIXME sometime we can calculate PTS */
467 input_ClockGetTS( p_input,
468 p_input->stream.p_selected_program,
469 MP4_GetTrackPTS( &track ) * 9/100 );
475 input_ClockGetTS( p_input,
476 p_input->stream.p_selected_program,
477 MP4_GetTrackPTS( &track ) * 9/100 );
480 es_out_Send( p_input->p_es_out, track.p_es, p_block );
482 if( MP4_TrackNextSample( p_input, &track ) )
493 /*****************************************************************************
494 * Seek: Got to i_date
495 ******************************************************************************/
496 static int Seek ( input_thread_t *p_input, mtime_t i_date )
498 demux_sys_t *p_sys = p_input->p_demux_data;
499 unsigned int i_track;
500 /* First update update global time */
501 p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
502 p_sys->i_pcr = i_date* 9 / 100;
504 /* Now for each stream try to go to this time */
505 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
507 #define track p_sys->track[i_track]
508 if( track.b_ok && track.b_selected )
510 MP4_TrackSeek( p_input, &track, i_date );
517 /*****************************************************************************
519 *****************************************************************************/
520 static int Control ( input_thread_t *p_input, int i_query, va_list args )
522 demux_sys_t *p_sys = p_input->p_demux_data;
529 case DEMUX_GET_POSITION:
530 pf = (double*)va_arg( args, double * );
531 if( p_sys->i_duration > 0 )
533 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
541 case DEMUX_SET_POSITION:
542 f = (double)va_arg( args, double );
545 (double)p_sys->i_duration /
546 (double)p_sys->i_timescale );
547 return Seek( p_input, i64 );
550 pi64 = (int64_t*)va_arg( args, int64_t * );
551 *pi64 = (mtime_t)1000000 *
552 (mtime_t)p_sys->i_time /
553 (mtime_t)p_sys->i_timescale;
557 i64 = (int64_t)va_arg( args, int64_t );
558 return Seek( p_input, i64 );
560 case DEMUX_GET_LENGTH:
561 pi64 = (int64_t*)va_arg( args, int64_t * );
562 *pi64 = (mtime_t)1000000 *
563 (mtime_t)p_sys->i_duration /
564 (mtime_t)p_sys->i_timescale;
567 msg_Warn( p_input, "DEMUX_GET_FPS unimplemented !!" );
570 msg_Err( p_input, "control query unimplemented !!!" );
571 return demux_vaControlDefault( p_input, i_query, args );
575 /*****************************************************************************
576 * Close: frees unused data
577 *****************************************************************************/
578 static void Close ( vlc_object_t * p_this )
580 unsigned int i_track;
581 input_thread_t * p_input = (input_thread_t *)p_this;
582 demux_sys_t *p_sys = p_input->p_demux_data;
584 msg_Dbg( p_input, "freeing all memory" );
586 MP4_BoxFree( p_input, p_sys->p_root );
587 for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
589 MP4_TrackDestroy( p_input, &p_sys->track[i_track] );
591 FREE( p_sys->track );
598 /****************************************************************************
599 * Local functions, specific to vlc
600 ****************************************************************************/
602 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
603 static int TrackCreateChunksIndex( input_thread_t *p_input,
604 track_data_mp4_t *p_demux_track )
606 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
609 unsigned int i_chunk;
610 unsigned int i_index, i_last;
612 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
613 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
614 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
616 return( VLC_EGENERIC );
619 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
620 if( !p_demux_track->i_chunk_count )
622 msg_Warn( p_input, "no chunk defined" );
623 return( VLC_EGENERIC );
625 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
626 sizeof( chunk_data_mp4_t ) );
628 /* first we read chunk offset */
629 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
631 p_demux_track->chunk[i_chunk].i_offset =
632 p_co64->data.p_co64->i_chunk_offset[i_chunk];
635 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
636 to be used for the sample XXX begin to 1
637 We construct it begining at the end */
638 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
639 i_index = p_stsc->data.p_stsc->i_entry_count;
642 msg_Warn( p_input, "cannot read chunk table or table empty" );
643 return( VLC_EGENERIC );
649 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
650 i_chunk < i_last; i_chunk++ )
652 p_demux_track->chunk[i_chunk].i_sample_description_index =
653 p_stsc->data.p_stsc->i_sample_description_index[i_index];
654 p_demux_track->chunk[i_chunk].i_sample_count =
655 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
657 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
660 p_demux_track->chunk[0].i_sample_first = 0;
661 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
663 p_demux_track->chunk[i_chunk].i_sample_first =
664 p_demux_track->chunk[i_chunk-1].i_sample_first +
665 p_demux_track->chunk[i_chunk-1].i_sample_count;
669 "track[Id 0x%x] read %d chunk",
670 p_demux_track->i_track_ID,
671 p_demux_track->i_chunk_count );
673 return( VLC_SUCCESS );
675 static int TrackCreateSamplesIndex( input_thread_t *p_input,
676 track_data_mp4_t *p_demux_track )
678 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
679 ctts make same mapping but for composition time,
680 not yet used and probably not usefull */
681 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
682 that uses a compressed form FIXME make them in libmp4
684 /* TODO use also stss and stsh table for seeking */
685 /* FIXME use edit table */
690 int64_t i_index_sample_used;
694 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
695 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
697 if( ( !p_stts )||( !p_stsz ) )
699 msg_Warn( p_input, "cannot read sample table" );
700 return( VLC_EGENERIC );
703 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
706 /* for sample size, there are 2 case */
707 if( p_stsz->data.p_stsz->i_sample_size )
709 /* 1: all sample have the same size, so no need to construct a table */
710 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
711 p_demux_track->p_sample_size = NULL;
715 /* 2: each sample can have a different size */
716 p_demux_track->i_sample_size = 0;
717 p_demux_track->p_sample_size =
718 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
720 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
722 p_demux_track->p_sample_size[i_sample] =
723 p_stsz->data.p_stsz->i_entry_size[i_sample];
726 /* we have extract all information from stsz,
729 /* if we don't want to waste too much memory, we can't expand
730 the box !, so each chunk will contain an "extract" of this table
734 i_index = 0; i_index_sample_used =0;
736 /* create and init last data for each chunk */
737 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
740 int64_t i_entry, i_sample_count, i;
742 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
743 /* count how many entries needed for this chunk
744 for p_sample_delta_dts and p_sample_count_dts */
746 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
749 while( i_sample_count > 0 )
751 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
754 i_sample_count += i_index_sample_used; /* don't count already used sample
761 p_demux_track->chunk[i_chunk].p_sample_count_dts =
762 calloc( i_entry, sizeof( uint32_t ) );
763 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
764 calloc( i_entry, sizeof( uint32_t ) );
767 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
768 for( i = 0; i < i_entry; i++ )
773 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
775 i_used = __MIN( i_rest, i_sample_count );
777 i_index_sample_used += i_used;
778 i_sample_count -= i_used;
780 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
782 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
783 p_stts->data.p_stts->i_sample_delta[i_index];
785 i_last_dts += i_used *
786 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
788 if( i_index_sample_used >=
789 p_stts->data.p_stts->i_sample_count[i_index] )
793 i_index_sample_used = 0;
800 "track[Id 0x%x] read %d samples length:"I64Fd"s",
801 p_demux_track->i_track_ID,
802 p_demux_track->i_sample_count,
803 i_last_dts / p_demux_track->i_timescale );
805 return( VLC_SUCCESS );
810 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
812 static int TrackCreateES ( input_thread_t *p_input,
813 track_data_mp4_t *p_track,
814 unsigned int i_chunk,
815 es_out_id_t **pp_es )
822 if( !p_track->chunk[i_chunk].i_sample_description_index )
825 "invalid SampleEntry index (track[Id 0x%x])",
826 p_track->i_track_ID );
830 p_sample = MP4_BoxGet( p_track->p_stsd, "[%d]",
831 p_track->chunk[i_chunk].i_sample_description_index - 1 );
833 if( !p_sample || !p_sample->data.p_data )
836 "cannot find SampleEntry (track[Id 0x%x])",
837 p_track->i_track_ID );
838 return( VLC_EGENERIC );
841 p_track->p_sample = p_sample;
843 if( p_track->i_sample_size == 1 )
845 MP4_Box_data_sample_soun_t *p_soun;
847 p_soun = p_sample->data.p_sample_soun;
849 if( p_soun->i_qt_version == 0 )
851 switch( p_sample->i_type )
853 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
854 p_soun->i_qt_version = 1;
855 p_soun->i_sample_per_packet = 64;
856 p_soun->i_bytes_per_packet = 34;
857 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
858 p_soun->i_bytes_per_sample = 2;
860 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
861 p_soun->i_qt_version = 1;
862 p_soun->i_sample_per_packet = 6;
863 p_soun->i_bytes_per_packet = 2;
864 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
865 p_soun->i_bytes_per_sample = 2;
867 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
868 p_soun->i_qt_version = 1;
869 p_soun->i_sample_per_packet = 12;
870 p_soun->i_bytes_per_packet = 2;
871 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
872 p_soun->i_bytes_per_sample = 2;
880 /* It's a little ugly but .. there are special cases */
881 switch( p_sample->i_type )
883 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
884 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
885 p_track->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
887 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
888 p_track->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
890 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
891 p_track->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
894 p_track->fmt.i_codec = p_sample->i_type;
898 /* now see if esds is present and if so create a data packet
899 with decoder_specific_info */
900 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
901 if( ( ( p_esds = MP4_BoxGet( p_sample, "esds" ) ) ||
902 ( p_esds = MP4_BoxGet( p_sample, "wave/esds" ) ) )&&
903 ( p_esds->data.p_esds )&&
906 /* First update information based on i_objectTypeIndication */
907 switch( p_decconfig->i_objectTypeIndication )
909 case( 0x20 ): /* MPEG4 VIDEO */
910 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','v' );
913 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
920 case( 0x65): /* MPEG2 video */
921 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
923 /* Theses are MPEG2-AAC */
924 case( 0x66): /* main profile */
925 case( 0x67): /* Low complexity profile */
926 case( 0x68): /* Scaleable Sampling rate profile */
927 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','4','a' );
929 /* true MPEG 2 audio */
931 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
933 case( 0x6a): /* MPEG1 video */
934 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
936 case( 0x6b): /* MPEG1 audio */
937 p_track->fmt.i_codec = VLC_FOURCC( 'm','p','g','a' );
939 case( 0x6c ): /* jpeg */
940 p_track->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
943 /* Unknown entry, but don't touch i_fourcc */
945 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
946 p_decconfig->i_objectTypeIndication,
947 p_track->i_track_ID );
950 p_track->fmt.i_extra = p_decconfig->i_decoder_specific_info_len;
951 if( p_track->fmt.i_extra > 0 )
953 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
954 memcpy( p_track->fmt.p_extra, p_decconfig->p_decoder_specific_info,
955 p_track->fmt.i_extra );
960 switch( p_sample->i_type )
962 /* qt decoder, send the complete chunk */
963 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
964 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
965 case VLC_FOURCC( 'V', 'P', '3', '1' ):
966 case VLC_FOURCC( '3', 'I', 'V', '1' ):
967 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
968 p_track->fmt.i_extra =
969 p_sample->data.p_sample_vide->i_qt_image_description;
970 if( p_track->fmt.i_extra > 0 )
972 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
973 memcpy( p_track->fmt.p_extra,
974 p_sample->data.p_sample_vide->p_qt_image_description,
975 p_track->fmt.i_extra);
978 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
979 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
980 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
981 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
982 p_track->fmt.i_extra =
983 p_sample->data.p_sample_soun->i_qt_description;
984 if( p_track->fmt.i_extra > 0 )
986 p_track->fmt.p_extra = malloc( p_track->fmt.i_extra );
987 memcpy( p_track->fmt.p_extra,
988 p_sample->data.p_sample_soun->p_qt_description,
989 p_track->fmt.i_extra);
999 /* some last initialisation */
1000 /* XXX I create a bitmapinfoheader_t or
1001 waveformatex_t for each stream, up to now it's the best thing
1002 I've found but it could exist a better solution :) as something
1003 like adding some new fields in p_es ...
1005 XXX I don't set all values, only thoses that are interesting or known
1006 --> bitmapinfoheader_t : width and height
1007 --> waveformatex_t : channels, samplerate, bitspersample
1008 and at the end I add p_decoder_specific_info
1010 TODO set more values
1014 switch( p_track->fmt.i_cat )
1017 p_track->fmt.video.i_width = p_sample->data.p_sample_vide->i_width;
1018 p_track->fmt.video.i_height= p_sample->data.p_sample_vide->i_height;
1020 /* fall on display size */
1021 if( p_track->fmt.video.i_width <= 0 )
1023 p_track->fmt.video.i_width = p_track->i_width;
1025 if( p_track->fmt.video.i_height <= 0 )
1027 p_track->fmt.video.i_height = p_track->i_height;
1032 p_track->fmt.audio.i_channels = p_sample->data.p_sample_soun->i_channelcount;
1033 p_track->fmt.audio.i_rate = p_sample->data.p_sample_soun->i_sampleratehi;
1034 p_track->fmt.i_bitrate =p_sample->data.p_sample_soun->i_channelcount *
1035 p_sample->data.p_sample_soun->i_sampleratehi *
1036 p_sample->data.p_sample_soun->i_samplesize;
1037 p_track->fmt.audio.i_bitspersample = p_sample->data.p_sample_soun->i_samplesize;
1043 *pp_es = es_out_Add( p_input->p_es_out, &p_track->fmt );
1048 /* given a time it return sample/chunk */
1049 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1050 track_data_mp4_t *p_track,
1053 uint32_t *pi_sample )
1057 unsigned int i_sample;
1058 unsigned int i_chunk;
1061 /* convert absolute time to in timescale unit */
1062 i_start = i_start * (mtime_t)p_track->i_timescale / (mtime_t)1000000;
1064 /* FIXME see if it's needed to check p_track->i_chunk_count */
1065 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1067 return( VLC_EGENERIC );
1070 /* we start from sample 0/chunk 0, hope it won't take too much time */
1071 /* *** find good chunk *** */
1072 for( i_chunk = 0; ; i_chunk++ )
1074 if( i_chunk + 1 >= p_track->i_chunk_count )
1076 /* at the end and can't check if i_start in this chunk,
1077 it will be check while searching i_sample */
1078 i_chunk = p_track->i_chunk_count - 1;
1082 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1083 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1089 /* *** find sample in the chunk *** */
1090 i_sample = p_track->chunk[i_chunk].i_sample_first;
1091 i_dts = p_track->chunk[i_chunk].i_first_dts;
1092 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1095 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1096 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1099 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1100 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1102 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1107 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1111 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1116 if( i_sample >= p_track->i_sample_count )
1119 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1120 p_track->i_track_ID, i_chunk, i_sample );
1121 return( VLC_EGENERIC );
1125 /* *** Try to find nearest sync points *** */
1126 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1128 unsigned int i_index;
1130 "track[Id 0x%x] using Sync Sample Box (stss)",
1131 p_track->i_track_ID );
1132 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1134 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1138 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1140 p_stss->data.p_stss->i_sample_number[i_index-1] );
1141 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1142 /* new i_sample is less than old so i_chunk can only decreased */
1143 while( i_chunk > 0 &&
1144 i_sample < p_track->chunk[i_chunk].i_sample_first )
1151 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1153 p_stss->data.p_stss->i_sample_number[i_index] );
1154 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1155 /* new i_sample is more than old so i_chunk can only increased */
1156 while( i_chunk < p_track->i_chunk_count - 1 &&
1157 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1158 p_track->chunk[i_chunk].i_sample_count )
1170 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1171 p_track->i_track_ID );
1174 if( pi_chunk ) *pi_chunk = i_chunk;
1175 if( pi_sample ) *pi_sample = i_sample;
1176 return( VLC_SUCCESS );
1179 static int TrackGotoChunkSample( input_thread_t *p_input,
1180 track_data_mp4_t *p_track,
1181 unsigned int i_chunk,
1182 unsigned int i_sample )
1184 vlc_bool_t b_reselect = VLC_FALSE;
1186 /* now see if actual es is ok */
1187 if( p_track->i_chunk < 0 ||
1188 p_track->i_chunk >= p_track->i_chunk_count ||
1189 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1190 p_track->chunk[i_chunk].i_sample_description_index )
1192 msg_Warn( p_input, "Recreate ES" );
1194 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, p_track->p_es, &b_reselect );
1196 es_out_Del( p_input->p_es_out, p_track->p_es );
1198 p_track->p_es = NULL;
1200 if( TrackCreateES( p_input,
1204 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1205 p_track->i_track_ID );
1207 p_track->b_ok = VLC_FALSE;
1208 p_track->b_selected = VLC_FALSE;
1209 return VLC_EGENERIC;
1213 /* select again the new decoder */
1216 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES, p_track->p_es );
1219 p_track->i_chunk = i_chunk;
1220 p_track->i_sample = i_sample;
1222 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1225 /****************************************************************************
1227 ****************************************************************************
1228 * Parse track information and create all needed data to run a track
1229 * If it succeed b_ok is set to 1 else to 0
1230 ****************************************************************************/
1231 static void MP4_TrackCreate( input_thread_t *p_input,
1232 track_data_mp4_t *p_track,
1233 MP4_Box_t * p_box_trak )
1235 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1236 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1248 /* hint track unsuported */
1250 /* set default value (-> track unusable) */
1251 p_track->b_ok = VLC_FALSE;
1252 p_track->b_enable = VLC_FALSE;
1253 p_track->b_selected = VLC_FALSE;
1255 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1262 /* do we launch this track by default ? */
1264 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1266 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1267 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1268 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1270 if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1272 /* msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
1277 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1280 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1281 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1283 if( ( !p_mdhd )||( !p_hdlr ) )
1288 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1290 for( i = 0; i < 3; i++ )
1292 language[i] = p_mdhd->data.p_mdhd->i_language[i];
1296 switch( p_hdlr->data.p_hdlr->i_handler_type )
1298 case( FOURCC_soun ):
1299 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1303 p_track->fmt.i_cat = AUDIO_ES;
1306 case( FOURCC_vide ):
1307 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1311 p_track->fmt.i_cat = VIDEO_ES;
1319 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1321 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
1322 !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1328 if( strcmp( language, "```" ) && strcmp( language, "und" ) )
1330 p_track->fmt.psz_language = strdup( language );
1333 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1334 if( p_track->fmt.i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1336 MP4_Box_t *p_sample;
1338 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1339 if( p_sample && p_sample->data.p_sample_soun)
1341 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1342 if( p_soun->i_qt_version == 0 && p_track->i_timescale != p_soun->i_sampleratehi )
1344 msg_Warn( p_input, "i_timescale != i_sampleratehi with qt_version == 0\nMaking both equal ? (report any problem)" );
1345 p_track->i_timescale = p_soun->i_sampleratehi;
1350 /* Create chunk index table and sample index table */
1351 if( TrackCreateChunksIndex( p_input,p_track ) ||
1352 TrackCreateSamplesIndex( p_input, p_track ) )
1354 return; /* cannot create chunks index */
1357 p_track->i_chunk = 0;
1358 p_track->i_sample = 0;
1361 if( TrackCreateES( p_input,
1362 p_track, p_track->i_chunk,
1365 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1366 p_track->i_track_ID );
1373 for( i = 0; i < p_track->i_chunk_count; i++ )
1375 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1380 p_track->b_ok = VLC_TRUE;
1383 /****************************************************************************
1385 ****************************************************************************
1386 * Destroy a track created by MP4_TrackCreate.
1387 ****************************************************************************/
1388 static void MP4_TrackDestroy( input_thread_t *p_input,
1389 track_data_mp4_t *p_track )
1391 unsigned int i_chunk;
1393 p_track->b_ok = VLC_FALSE;
1394 p_track->b_enable = VLC_FALSE;
1395 p_track->b_selected = VLC_FALSE;
1397 es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
1399 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1401 if( p_track->chunk )
1403 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1404 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1407 FREE( p_track->chunk );
1409 if( !p_track->i_sample_size )
1411 FREE( p_track->p_sample_size );
1415 static int MP4_TrackSelect ( input_thread_t *p_input,
1416 track_data_mp4_t *p_track,
1422 if( !p_track->b_ok )
1424 return( VLC_EGENERIC );
1427 if( p_track->b_selected )
1430 "track[Id 0x%x] already selected",
1431 p_track->i_track_ID );
1432 return( VLC_SUCCESS );
1435 if( TrackTimeToSampleChunk( p_input,
1437 &i_chunk, &i_sample ) )
1440 "cannot select track[Id 0x%x]",
1441 p_track->i_track_ID );
1442 return( VLC_EGENERIC );
1445 p_track->b_selected = VLC_TRUE;
1447 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1449 p_track->b_selected = VLC_FALSE;
1451 return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
1454 static void MP4_TrackUnselect(input_thread_t *p_input,
1455 track_data_mp4_t *p_track )
1457 if( !p_track->b_ok )
1462 if( !p_track->b_selected )
1465 "track[Id 0x%x] already unselected",
1466 p_track->i_track_ID );
1471 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_track->p_es, VLC_FALSE );
1474 p_track->b_selected = VLC_FALSE;
1477 static int MP4_TrackSeek ( input_thread_t *p_input,
1478 track_data_mp4_t *p_track,
1484 if( !p_track->b_ok )
1486 return( VLC_EGENERIC );
1489 if( TrackTimeToSampleChunk( p_input,
1491 &i_chunk, &i_sample ) )
1494 "cannot select track[Id 0x%x]",
1495 p_track->i_track_ID );
1496 return( VLC_EGENERIC );
1499 p_track->b_selected = VLC_TRUE;
1501 TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample );
1503 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1511 * 3 types: for audio
1514 #define QT_V0_MAX_SAMPLES 1500
1515 static int MP4_TrackSampleSize( track_data_mp4_t *p_track )
1518 MP4_Box_data_sample_soun_t *p_soun;
1520 if( p_track->i_sample_size == 0 )
1522 /* most simple case */
1523 return( p_track->p_sample_size[p_track->i_sample] );
1525 if( p_track->fmt.i_cat != AUDIO_ES )
1527 return( p_track->i_sample_size );
1530 if( p_track->i_sample_size != 1 )
1532 //msg_Warn( p_input, "SampleSize != 1" );
1533 return( p_track->i_sample_size );
1536 p_soun = p_track->p_sample->data.p_sample_soun;
1538 if( p_soun->i_qt_version == 1 )
1540 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1545 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1546 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1547 if( i_samples > QT_V0_MAX_SAMPLES )
1549 i_samples = QT_V0_MAX_SAMPLES;
1551 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1554 //fprintf( stderr, "size=%d\n", i_size );
1559 static uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
1561 unsigned int i_sample;
1565 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1567 if( p_track->i_sample_size )
1569 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1571 if( p_soun->i_qt_version == 0 )
1573 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1574 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1578 /* we read chunk by chunk */
1584 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1585 i_sample < p_track->i_sample; i_sample++ )
1587 i_pos += p_track->p_sample_size[i_sample];
1594 static int MP4_TrackNextSample( input_thread_t *p_input,
1595 track_data_mp4_t *p_track )
1598 if( p_track->fmt.i_cat == AUDIO_ES &&
1599 p_track->i_sample_size != 0 )
1601 MP4_Box_data_sample_soun_t *p_soun;
1603 p_soun = p_track->p_sample->data.p_sample_soun;
1605 if( p_soun->i_qt_version == 1 )
1607 /* chunk by chunk */
1609 p_track->chunk[p_track->i_chunk].i_sample_first +
1610 p_track->chunk[p_track->i_chunk].i_sample_count;
1615 p_track->i_sample += QT_V0_MAX_SAMPLES;
1616 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 )
1619 p_track->chunk[p_track->i_chunk].i_sample_first +
1620 p_track->chunk[p_track->i_chunk].i_sample_count;
1626 p_track->i_sample++;
1629 if( p_track->i_sample >= p_track->i_sample_count )
1631 /* we have reach end of the track so free decoder stuff */
1632 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1633 MP4_TrackUnselect( p_input, p_track );
1634 return( VLC_EGENERIC );
1637 /* Have we changed chunk ? */
1638 if( p_track->i_sample >=
1639 p_track->chunk[p_track->i_chunk].i_sample_first +
1640 p_track->chunk[p_track->i_chunk].i_sample_count )
1642 if( TrackGotoChunkSample( p_input,
1644 p_track->i_chunk + 1,
1645 p_track->i_sample ) )
1647 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1648 MP4_TrackUnselect( p_input, p_track );
1649 return( VLC_EGENERIC );
1653 return( VLC_SUCCESS );