1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.19 2003/03/11 18:57:50 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() */
27 #include <string.h> /* strdup() */
29 #include <sys/types.h>
32 #include <vlc/input.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int MP4Init ( vlc_object_t * );
41 static void __MP4End ( vlc_object_t * );
42 static int MP4Demux ( input_thread_t * );
44 /* New input could have something like that... */
45 static int MP4Seek ( input_thread_t *, mtime_t );
47 #define MP4End(a) __MP4End(VLC_OBJECT(a))
49 /*****************************************************************************
51 *****************************************************************************/
53 set_description( _("MP4 demuxer") );
54 set_capability( "demux", 242 );
55 set_callbacks( MP4Init, __MP4End );
58 /*****************************************************************************
59 * Declaration of local function
60 *****************************************************************************/
62 static void MP4_TrackCreate ( input_thread_t *,
65 static void MP4_TrackDestroy( input_thread_t *,
68 static int MP4_TrackSelect ( input_thread_t *,
71 static void MP4_TrackUnselect(input_thread_t *,
74 static int MP4_TrackSeek ( input_thread_t *,
78 static uint64_t MP4_GetTrackPos( track_data_mp4_t * );
79 static int MP4_TrackSampleSize( track_data_mp4_t * );
80 static int MP4_TrackNextSample( input_thread_t *,
83 #define MP4_Set4BytesLE( p, dw ) \
84 *((uint8_t*)p) = ( (dw)&0xff ); \
85 *((uint8_t*)p+1) = ( ((dw)>> 8)&0xff ); \
86 *((uint8_t*)p+2) = ( ((dw)>>16)&0xff ); \
87 *((uint8_t*)p+3) = ( ((dw)>>24)&0xff )
89 #define MP4_Set2BytesLE( p, dw ) \
90 *((uint8_t*)p) = ( (dw)&0xff ); \
91 *((uint8_t*)p+1) = ( ((dw)>> 8)&0xff )
94 if( p ) { free( p ); (p) = NULL;}
96 /*****************************************************************************
97 * MP4Init: check file and initializes MP4 structures
98 *****************************************************************************/
99 static int MP4Init( vlc_object_t * p_this )
101 input_thread_t *p_input = (input_thread_t *)p_this;
104 demux_sys_t *p_demux;
114 if( !p_input->stream.b_seekable )
116 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
120 /* Initialize access plug-in structures. */
121 if( p_input->i_mtu == 0 )
124 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
127 p_input->pf_demux = MP4Demux;
129 /* a little test to see if it could be a mp4 */
130 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
132 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
137 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
149 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
153 /* create our structure that will contains all data */
154 if( !( p_input->p_demux_data =
155 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
157 msg_Err( p_input, "out of memory" );
160 memset( p_demux, 0, sizeof( demux_sys_t ) );
161 p_input->p_demux_data = p_demux;
164 /* Now load all boxes ( except raw data ) */
165 if( !MP4_BoxGetRoot( p_input, &p_demux->box_root ) )
167 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
171 MP4_BoxDumpStructure( p_input, &p_demux->box_root );
173 if( ( p_ftyp = MP4_BoxGet( &p_demux->box_root, "/ftyp" ) ) )
175 switch( p_ftyp->data.p_ftyp->i_major_brand )
179 "ISO Media file (isom) version %d.",
180 p_ftyp->data.p_ftyp->i_minor_version );
184 "unrecognized major file specification (%4.4s).",
185 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
191 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
194 /* the file need to have one moov box */
195 if( MP4_BoxCount( &p_demux->box_root, "/moov" ) != 1 )
198 "MP4 plugin discarded (%d moov boxes)",
199 MP4_BoxCount( &p_demux->box_root, "/moov" ) );
200 // MP4End( p_input );
204 if( !(p_mvhd = MP4_BoxGet( &p_demux->box_root, "/moov/mvhd" ) ) )
206 msg_Err( p_input, "cannot find /moov/mvhd" );
212 p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
213 p_demux->i_duration = p_mvhd->data.p_mvhd->i_duration;
216 if( !( p_demux->i_tracks =
217 MP4_BoxCount( &p_demux->box_root, "/moov/trak" ) ) )
219 msg_Err( p_input, "cannot find any /moov/trak" );
223 msg_Dbg( p_input, "find %d track%c",
225 p_demux->i_tracks ? 's':' ' );
227 /* create one program */
228 vlc_mutex_lock( &p_input->stream.stream_lock );
229 if( input_InitStream( p_input, 0 ) == -1)
231 vlc_mutex_unlock( &p_input->stream.stream_lock );
232 msg_Err( p_input, "cannot init stream" );
236 /* Needed to create program _before_ MP4_TrackCreate */
237 if( input_AddProgram( p_input, 0, 0) == NULL )
239 vlc_mutex_unlock( &p_input->stream.stream_lock );
240 msg_Err( p_input, "cannot add program" );
244 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
245 /* XXX beurk and beurk, see MP4Demux and MP4Seek */
246 if( p_demux->i_duration/p_demux->i_timescale > 0 )
248 p_input->stream.i_mux_rate =
249 p_input->stream.p_selected_area->i_size / 50 /
250 ( p_demux->i_duration / p_demux->i_timescale );
254 p_input->stream.i_mux_rate = 0;
256 vlc_mutex_unlock( &p_input->stream.stream_lock );
259 /* allocate memory */
260 p_demux->track = calloc( p_demux->i_tracks, sizeof( track_data_mp4_t ) );
262 /* now process each track and extract all usefull informations */
263 for( i = 0; i < p_demux->i_tracks; i++ )
265 p_trak = MP4_BoxGet( &p_demux->box_root, "/moov/trak[%d]", i );
266 MP4_TrackCreate( p_input, &p_demux->track[i], p_trak );
268 if( p_demux->track[i].b_ok )
271 switch( p_demux->track[i].i_cat )
284 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %c%c%c",
285 p_demux->track[i].i_track_ID,
287 p_demux->track[i].b_enable ? "enable":"disable",
288 p_demux->track[i].i_language[0],
289 p_demux->track[i].i_language[1],
290 p_demux->track[i].i_language[2] );
294 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_demux->track[i].i_track_ID );
299 for( i = 0; i < p_demux->i_tracks; i++ )
301 /* start decoder for this track if enable by default*/
302 if( p_demux->track[i].b_ok && p_demux->track[i].b_enable )
304 MP4_TrackSelect( p_input, &p_demux->track[i], 0 );
308 vlc_mutex_lock( &p_input->stream.stream_lock );
309 p_input->stream.p_selected_program->b_is_ok = 1;
310 vlc_mutex_unlock( &p_input->stream.stream_lock );
316 /*****************************************************************************
317 * MP4Demux: read packet and send them to decoders
318 *****************************************************************************
319 * TODO check for newly selected track (ie audio upt to now )
320 *****************************************************************************/
321 static int MP4Demux( input_thread_t *p_input )
323 demux_sys_t *p_demux = p_input->p_demux_data;
324 unsigned int i_track;
327 unsigned int i_track_selected;
329 vlc_bool_t b_play_audio;
331 /* check for newly selected/unselected track */
332 for( i_track = 0, i_track_selected = 0, b_video = VLC_FALSE;
333 i_track < p_demux->i_tracks; i_track++ )
335 #define track p_demux->track[i_track]
336 if( track.b_selected && track.i_sample >= track.i_sample_count )
338 msg_Warn( p_input, "track[0x%x] will be disabled", track.i_track_ID );
339 MP4_TrackUnselect( p_input, &track );
341 else if( track.b_ok )
343 if( track.b_selected && track.p_es->p_decoder_fifo == NULL )
345 MP4_TrackUnselect( p_input, &track );
347 else if( !track.b_selected && track.p_es->p_decoder_fifo != NULL )
349 MP4_TrackSelect( p_input, &track, MP4_GetMoviePTS( p_demux ) );
352 if( track.b_selected )
356 if( track.i_cat == VIDEO_ES )
365 if( i_track_selected <= 0 )
367 msg_Warn( p_input, "no track selected, exiting..." );
372 /* XXX beurk, beuRK and BEURK,
373 but only way I've found to detect seek from interface */
374 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
378 /* first wait for empty buffer, arbitrary time FIXME */
379 msleep( DEFAULT_PTS_DELAY );
380 /* *** calculate new date *** */
382 i_date = (mtime_t)1000000 *
383 (mtime_t)p_demux->i_duration /
384 (mtime_t)p_demux->i_timescale *
385 (mtime_t)MP4_TellAbsolute( p_input ) /
386 (mtime_t)p_input->stream.p_selected_area->i_size;
387 MP4Seek( p_input, i_date );
390 /* first wait for the good time to read a packet */
391 input_ClockManageRef( p_input,
392 p_input->stream.p_selected_program,
396 /* update pcr XXX in mpeg scale so in 90000 unit/s */
397 p_demux->i_pcr = MP4_GetMoviePTS( p_demux ) * 9 / 100;
400 /* we will read 100ms for each stream so ...*/
401 p_demux->i_time += __MAX( p_demux->i_timescale / 10 , 1 );
404 /* *** send audio data to decoder if rate == DEFAULT_RATE or no video *** */
405 vlc_mutex_lock( &p_input->stream.stream_lock );
406 if( p_input->stream.control.i_rate == DEFAULT_RATE || !b_video )
408 b_play_audio = VLC_TRUE;
412 b_play_audio = VLC_FALSE;
414 vlc_mutex_unlock( &p_input->stream.stream_lock );
416 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
418 #define track p_demux->track[i_track]
421 MP4_GetTrackPTS( &track ) >= MP4_GetMoviePTS( p_demux ) )
425 while( MP4_GetTrackPTS( &track ) < MP4_GetMoviePTS( p_demux ) )
428 if( !b_play_audio && track.i_cat == AUDIO_ES )
430 if( MP4_TrackNextSample( p_input, &track ) )
440 data_packet_t *p_data;
443 /* caculate size and position for this sample */
444 i_size = MP4_TrackSampleSize( &track );
446 i_pos = MP4_GetTrackPos( &track );
449 if( ! MP4_SeekAbsolute( p_input, i_pos ) )
451 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
452 MP4_TrackUnselect( p_input, &track );
457 /* now create a pes */
458 if( !(p_pes = input_NewPES( p_input->p_method_data ) ) )
462 /* and a data packet for the data */
463 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
465 input_DeletePES( p_input->p_method_data, p_pes );
468 /* initialisation of all the field */
469 p_pes->i_dts = p_pes->i_pts = 0;
470 p_pes->p_first = p_pes->p_last = p_data;
471 p_pes->i_nb_data = 1;
472 p_pes->i_pes_size = i_size;
475 if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
477 input_DeletePES( p_input->p_method_data, p_pes );
479 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
480 MP4_TrackUnselect( p_input, &track );
486 p_pes->i_pts = input_ClockGetTS( p_input,
487 p_input->stream.p_selected_program,
488 MP4_GetTrackPTS( &track ) * 9/100);
490 if( track.p_es->p_decoder_fifo )
492 input_DecodePES( track.p_es->p_decoder_fifo, p_pes );
496 input_DeletePES( p_input->p_method_data, p_pes );
499 if( MP4_TrackNextSample( p_input, &track ) )
510 /*****************************************************************************
511 * MP4Seek: Got to i_date
512 ******************************************************************************/
513 static int MP4Seek ( input_thread_t *p_input, mtime_t i_date )
515 demux_sys_t *p_demux = p_input->p_demux_data;
516 unsigned int i_track;
517 /* First update update global time */
518 p_demux->i_time = i_date * p_demux->i_timescale / 1000000;
519 p_demux->i_pcr = i_date* 9 / 100;
521 /* Now for each stream try to go to this time */
522 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
524 #define track p_demux->track[i_track]
525 if( track.b_ok && track.b_selected )
527 MP4_TrackSeek( p_input, &track, i_date );
534 /*****************************************************************************
535 * MP4End: frees unused data
536 *****************************************************************************/
537 static void __MP4End ( vlc_object_t * p_this )
539 unsigned int i_track;
540 input_thread_t * p_input = (input_thread_t *)p_this;
541 demux_sys_t *p_demux = p_input->p_demux_data;
543 msg_Dbg( p_input, "freeing all memory" );
544 MP4_BoxFree( p_input, &p_demux->box_root );
545 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
547 MP4_TrackDestroy( p_input, &p_demux->track[i_track] );
549 FREE( p_demux->track );
551 FREE( p_input->p_demux_data );
555 /****************************************************************************
556 * Local functions, specific to vlc
557 ****************************************************************************/
559 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
560 static int TrackCreateChunksIndex( input_thread_t *p_input,
561 track_data_mp4_t *p_demux_track )
563 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
566 unsigned int i_chunk;
567 unsigned int i_index, i_last;
569 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
570 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
571 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
573 return( VLC_EGENERIC );
576 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
577 if( !p_demux_track->i_chunk_count )
579 msg_Warn( p_input, "no chunk defined" );
580 return( VLC_EGENERIC );
582 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
583 sizeof( chunk_data_mp4_t ) );
585 /* first we read chunk offset */
586 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
588 p_demux_track->chunk[i_chunk].i_offset =
589 p_co64->data.p_co64->i_chunk_offset[i_chunk];
592 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
593 to be used for the sample XXX begin to 1
594 We construct it begining at the end */
595 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
596 i_index = p_stsc->data.p_stsc->i_entry_count;
599 msg_Warn( p_input, "cannot read chunk table or table empty" );
600 return( VLC_EGENERIC );
606 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
607 i_chunk < i_last; i_chunk++ )
609 p_demux_track->chunk[i_chunk].i_sample_description_index =
610 p_stsc->data.p_stsc->i_sample_description_index[i_index];
611 p_demux_track->chunk[i_chunk].i_sample_count =
612 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
614 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
617 p_demux_track->chunk[0].i_sample_first = 0;
618 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
620 p_demux_track->chunk[i_chunk].i_sample_first =
621 p_demux_track->chunk[i_chunk-1].i_sample_first +
622 p_demux_track->chunk[i_chunk-1].i_sample_count;
626 "track[Id 0x%x] read %d chunk",
627 p_demux_track->i_track_ID,
628 p_demux_track->i_chunk_count );
630 return( VLC_SUCCESS );
632 static int TrackCreateSamplesIndex( input_thread_t *p_input,
633 track_data_mp4_t *p_demux_track )
635 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
636 ctts make same mapping but for composition time,
637 not yet used and probably not usefull */
638 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
639 that uses a compressed form FIXME make them in libmp4
641 /* TODO use also stss and stsh table for seeking */
642 /* FIXME use edit table */
647 int64_t i_index_sample_used;
651 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
652 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
654 if( ( !p_stts )||( !p_stsz ) )
656 msg_Warn( p_input, "cannot read sample table" );
657 return( VLC_EGENERIC );
660 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
663 /* for sample size, there are 2 case */
664 if( p_stsz->data.p_stsz->i_sample_size )
666 /* 1: all sample have the same size, so no need to construct a table */
667 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
668 p_demux_track->p_sample_size = NULL;
672 /* 2: each sample can have a different size */
673 p_demux_track->i_sample_size = 0;
674 p_demux_track->p_sample_size =
675 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
677 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
679 p_demux_track->p_sample_size[i_sample] =
680 p_stsz->data.p_stsz->i_entry_size[i_sample];
683 /* we have extract all information from stsz,
686 /* if we don't want to waste too much memory, we can't expand
687 the box !, so each chunk will contain an "extract" of this table
691 i_index = 0; i_index_sample_used =0;
693 /* create and init last data for each chunk */
694 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
697 int64_t i_entry, i_sample_count, i;
699 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
700 /* count how many entries needed for this chunk
701 for p_sample_delta_dts and p_sample_count_dts */
703 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
706 while( i_sample_count > 0 )
708 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
711 i_sample_count += i_index_sample_used; /* don't count already used sample
718 p_demux_track->chunk[i_chunk].p_sample_count_dts =
719 calloc( i_entry, sizeof( uint32_t ) );
720 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
721 calloc( i_entry, sizeof( uint32_t ) );
724 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
725 for( i = 0; i < i_entry; i++ )
730 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
732 i_used = __MIN( i_rest, i_sample_count );
734 i_index_sample_used += i_used;
735 i_sample_count -= i_used;
737 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
739 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
740 p_stts->data.p_stts->i_sample_delta[i_index];
742 i_last_dts += i_used *
743 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
745 if( i_index_sample_used >=
746 p_stts->data.p_stts->i_sample_count[i_index] )
750 i_index_sample_used = 0;
757 "track[Id 0x%x] read %d samples length:"I64Fd"s",
758 p_demux_track->i_track_ID,
759 p_demux_track->i_sample_count,
760 i_last_dts / p_demux_track->i_timescale );
762 return( VLC_SUCCESS );
767 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
769 static int TrackCreateES ( input_thread_t *p_input,
770 track_data_mp4_t *p_track,
771 unsigned int i_chunk,
772 es_descriptor_t **pp_es,
773 pes_packet_t **pp_pes )
775 MP4_Box_t * p_sample;
778 unsigned int i_decoder_specific_info_len;
779 uint8_t * p_decoder_specific_info;
781 es_descriptor_t *p_es;
782 pes_packet_t *p_pes_init;
785 BITMAPINFOHEADER *p_bih;
790 if( !p_track->chunk[i_chunk].i_sample_description_index )
793 "invalid SampleEntry index (track[Id 0x%x])",
794 p_track->i_track_ID );
795 return( VLC_EGENERIC );
798 p_sample = MP4_BoxGet( p_track->p_stsd,
800 p_track->chunk[i_chunk].i_sample_description_index - 1 );
802 if( !p_sample || !p_sample->data.p_data )
805 "cannot find SampleEntry (track[Id 0x%x])",
806 p_track->i_track_ID );
807 return( VLC_EGENERIC );
810 p_track->p_sample = p_sample;
812 if( p_track->i_sample_size == 1 )
814 MP4_Box_data_sample_soun_t *p_soun;
816 p_soun = p_sample->data.p_sample_soun;
818 if( p_soun->i_qt_version == 0 )
820 switch( p_sample->i_type )
822 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
823 p_soun->i_qt_version = 1;
824 p_soun->i_sample_per_packet = 64;
825 p_soun->i_bytes_per_packet = 34;
826 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
827 p_soun->i_bytes_per_sample = 2;
836 vlc_mutex_lock( &p_input->stream.stream_lock );
837 p_es = input_AddES( p_input,
838 p_input->stream.p_selected_program,
841 vlc_mutex_unlock( &p_input->stream.stream_lock );
842 /* Initialise ES, first language as description */
843 for( i = 0; i < 3; i++ )
845 p_es->psz_desc[i] = p_track->i_language[i];
847 p_es->psz_desc[3] = '\0';
849 p_es->i_stream_id = p_track->i_track_ID;
851 /* It's a little ugly but .. there are special cases */
852 switch( p_sample->i_type )
854 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
855 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
856 p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
858 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
859 p_es->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
862 p_es->i_fourcc = p_sample->i_type;
866 p_es->i_cat = p_track->i_cat;
868 i_decoder_specific_info_len = 0;
869 p_decoder_specific_info = NULL;
872 /* now see if esds is present and if so create a data packet
873 with decoder_specific_info */
874 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
875 if( ( p_esds = MP4_BoxGet( p_sample, "esds" ) )&&
876 ( p_esds->data.p_esds )&&
879 /* First update information based on i_objectTypeIndication */
880 switch( p_decconfig->i_objectTypeIndication )
882 case( 0x20 ): /* MPEG4 VIDEO */
883 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
886 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
893 case( 0x65): /* MPEG2 video */
894 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
896 /* Theses are MPEG2-AAC */
897 case( 0x66): /* main profile */
898 case( 0x67): /* Low complexity profile */
899 case( 0x68): /* Scaleable Sampling rate profile */
900 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
902 /* true MPEG 2 audio */
904 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
906 case( 0x6a): /* MPEG1 video */
907 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
909 case( 0x6b): /* MPEG1 audio */
910 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
912 case( 0x6c ): /* jpeg */
913 p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
916 /* Unknown entry, but don't touch i_fourcc */
918 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
919 p_decconfig->i_objectTypeIndication,
920 p_track->i_track_ID );
923 i_decoder_specific_info_len =
924 p_decconfig->i_decoder_specific_info_len;
925 p_decoder_specific_info =
926 p_decconfig->p_decoder_specific_info;
931 /* some last initialisation */
932 /* XXX I create a bitmapinfoheader_t or
933 waveformatex_t for each stream, up to now it's the best thing
934 I've found but it could exist a better solution :) as something
935 like adding some new fields in p_es ...
937 XXX I don't set all values, only thoses that are interesting or known
938 --> bitmapinfoheader_t : width and height
939 --> waveformatex_t : channels, samplerate, bitspersample
940 and at the end I add p_decoder_specific_info
946 switch( p_track->i_cat )
949 /* now create a bitmapinfoheader_t for decoder and
950 add information found in p_esds */
951 /* XXX XXX + 16 are for avoid segfault when ffmpeg access beyong the data */
952 p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len + 16 );
953 p_bih = (BITMAPINFOHEADER*)p_init;
955 p_bih->biSize = sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len;
956 p_bih->biWidth = p_sample->data.p_sample_vide->i_width;
957 p_bih->biHeight = p_sample->data.p_sample_vide->i_height;
958 p_bih->biPlanes = 1; // FIXME
959 p_bih->biBitCount = 0; // FIXME
960 p_bih->biCompression = 0; // FIXME
961 p_bih->biSizeImage = 0; // FIXME
962 p_bih->biXPelsPerMeter = 0; // FIXME
963 p_bih->biYPelsPerMeter = 0; // FIXME
964 p_bih->biClrUsed = 0; // FIXME
965 p_bih->biClrImportant = 0; // FIXME
967 if( p_bih->biWidth == 0 )
969 // fall on display size
970 p_bih->biWidth = p_track->i_width;
972 if( p_bih->biHeight == 0 )
974 // fall on display size
975 p_bih->biHeight = p_track->i_height;
978 if( i_decoder_specific_info_len )
980 data_packet_t *p_data;
982 memcpy( p_init + sizeof( BITMAPINFOHEADER ),
983 p_decoder_specific_info,
984 i_decoder_specific_info_len);
986 /* If stream is mpeg4 video we send specific_info,
987 as it's needed to decode it (vol) */
988 switch( p_es->i_fourcc )
990 case VLC_FOURCC( 'm','p','4','v' ):
991 case VLC_FOURCC( 'D','I','V','X' ):
992 case VLC_FOURCC( 'd','i','v','x' ):
993 p_pes_init = input_NewPES( p_input->p_method_data );
994 p_data = input_NewPacket( p_input->p_method_data,
995 i_decoder_specific_info_len);
996 memcpy( p_data->p_payload_start,
997 p_decoder_specific_info,
998 i_decoder_specific_info_len );
999 p_pes_init->i_dts = p_pes_init->i_pts = 0;
1000 p_pes_init->p_first = p_pes_init->p_last = p_data;
1001 p_pes_init->i_nb_data = 1;
1002 p_pes_init->i_pes_size = i_decoder_specific_info_len;
1012 p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len + 16 );
1013 p_wf = (WAVEFORMATEX*)p_init;
1015 p_wf->wFormatTag = 1;
1016 p_wf->nChannels = p_sample->data.p_sample_soun->i_channelcount;
1017 p_wf->nSamplesPerSec = p_sample->data.p_sample_soun->i_sampleratehi;
1018 p_wf->nAvgBytesPerSec = p_sample->data.p_sample_soun->i_channelcount *
1019 p_sample->data.p_sample_soun->i_sampleratehi *
1020 p_sample->data.p_sample_soun->i_samplesize / 8;
1021 p_wf->nBlockAlign = 0;
1022 p_wf->wBitsPerSample = p_sample->data.p_sample_soun->i_samplesize;
1023 p_wf->cbSize = i_decoder_specific_info_len;
1025 if( i_decoder_specific_info_len )
1027 memcpy( p_init + sizeof( WAVEFORMATEX ),
1028 p_decoder_specific_info,
1029 i_decoder_specific_info_len);
1038 if( p_es->i_cat == AUDIO_ES )
1040 p_es->p_bitmapinfoheader = NULL;
1041 p_es->p_waveformatex = (void*)p_init;
1043 else if( p_es->i_cat == VIDEO_ES )
1045 p_es->p_bitmapinfoheader = (void*)p_init;
1046 p_es->p_waveformatex = NULL;
1050 *pp_pes = p_pes_init;
1051 return( VLC_SUCCESS );
1054 /* given a time it return sample/chunk */
1055 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1056 track_data_mp4_t *p_track,
1059 uint32_t *pi_sample )
1063 unsigned int i_sample;
1064 unsigned int i_chunk;
1067 /* convert absolute time to in timescale unit */
1068 i_start = i_start * (mtime_t)p_track->i_timescale / (mtime_t)1000000;
1070 /* FIXME see if it's needed to check p_track->i_chunk_count */
1071 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1073 return( VLC_EGENERIC );
1076 /* we start from sample 0/chunk 0, hope it won't take too much time */
1077 /* *** find good chunk *** */
1078 for( i_chunk = 0; ; i_chunk++ )
1080 if( i_chunk + 1 >= p_track->i_chunk_count )
1082 /* at the end and can't check if i_start in this chunk,
1083 it will be check while searching i_sample */
1084 i_chunk = p_track->i_chunk_count - 1;
1088 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1089 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1095 /* *** find sample in the chunk *** */
1096 i_sample = p_track->chunk[i_chunk].i_sample_first;
1097 i_dts = p_track->chunk[i_chunk].i_first_dts;
1098 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1101 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1102 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1105 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1106 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1108 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1113 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1117 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1122 if( i_sample >= p_track->i_sample_count )
1125 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1126 p_track->i_track_ID, i_chunk, i_sample );
1127 return( VLC_EGENERIC );
1131 /* *** Try to find nearest sync points *** */
1132 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1134 unsigned int i_index;
1136 "track[Id 0x%x] using Sync Sample Box (stss)",
1137 p_track->i_track_ID );
1138 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1140 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1144 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1146 p_stss->data.p_stss->i_sample_number[i_index-1] );
1147 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1148 /* new i_sample is less than old so i_chunk can only decreased */
1149 while( i_chunk > 0 &&
1150 i_sample < p_track->chunk[i_chunk].i_sample_first )
1157 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1159 p_stss->data.p_stss->i_sample_number[i_index-1] );
1160 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1161 /* new i_sample is more than old so i_chunk can only increased */
1162 while( i_chunk < p_track->i_chunk_count - 1 &&
1163 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1164 p_track->chunk[i_chunk].i_sample_count )
1176 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1177 p_track->i_track_ID );
1180 if( pi_chunk ) *pi_chunk = i_chunk;
1181 if( pi_sample ) *pi_sample = i_sample;
1182 return( VLC_SUCCESS );
1185 static int TrackGotoChunkSample( input_thread_t *p_input,
1186 track_data_mp4_t *p_track,
1187 unsigned int i_chunk,
1188 unsigned int i_sample )
1190 /* now see if actual es is ok */
1191 if( p_track->i_chunk < 0 ||
1192 p_track->i_chunk >= p_track->i_chunk_count ||
1193 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1194 p_track->chunk[i_chunk].i_sample_description_index )
1196 msg_Warn( p_input, "Recreate ES" );
1198 /* no :( recreate es */
1199 vlc_mutex_lock( &p_input->stream.stream_lock );
1200 input_DelES( p_input, p_track->p_es );
1201 vlc_mutex_unlock( &p_input->stream.stream_lock );
1203 if( p_track->p_pes_init )
1205 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1208 if( TrackCreateES( p_input,
1211 &p_track->p_pes_init ) )
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 */
1223 if( p_track->b_selected && p_track->p_es && p_track->p_es->p_decoder_fifo == NULL )
1225 vlc_mutex_lock( &p_input->stream.stream_lock );
1226 input_SelectES( p_input, p_track->p_es );
1227 vlc_mutex_unlock( &p_input->stream.stream_lock );
1229 if( p_track->p_es->p_decoder_fifo )
1231 if( p_track->p_pes_init != NULL )
1233 input_DecodePES( p_track->p_es->p_decoder_fifo, p_track->p_pes_init );
1234 p_track->p_pes_init = NULL;
1236 p_track->b_selected = VLC_TRUE;
1240 msg_Dbg( p_input, "Argg cannot select this stream" );
1241 if( p_track->p_pes_init != NULL )
1243 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1244 p_track->p_pes_init = NULL;
1246 p_track->b_selected = VLC_FALSE;
1250 p_track->i_chunk = i_chunk;
1251 p_track->i_sample = i_sample;
1253 return( VLC_SUCCESS );
1256 /****************************************************************************
1258 ****************************************************************************
1259 * Parse track information and create all needed data to run a track
1260 * If it succeed b_ok is set to 1 else to 0
1261 ****************************************************************************/
1262 static void MP4_TrackCreate( input_thread_t *p_input,
1263 track_data_mp4_t *p_track,
1264 MP4_Box_t * p_box_trak )
1268 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1269 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1278 /* hint track unsuported */
1280 /* set default value (-> track unusable) */
1281 p_track->b_ok = VLC_FALSE;
1282 p_track->b_enable = VLC_FALSE;
1283 p_track->b_selected = VLC_FALSE;
1285 p_track->i_cat = UNKNOWN_ES;
1292 /* do we launch this track by default ? */
1294 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1296 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1297 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1298 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1300 if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1302 /* msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
1307 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1310 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1311 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1313 if( ( !p_mdhd )||( !p_hdlr ) )
1318 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1320 for( i = 0; i < 3; i++ )
1322 p_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
1324 p_mdhd->data.p_mdhd->i_language[3] = 0;
1326 switch( p_hdlr->data.p_hdlr->i_handler_type )
1328 case( FOURCC_soun ):
1329 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1333 p_track->i_cat = AUDIO_ES;
1336 case( FOURCC_vide ):
1337 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1341 p_track->i_cat = VIDEO_ES;
1349 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1351 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) )
1356 if( !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1361 /* Create chunk index table */
1362 if( TrackCreateChunksIndex( p_input,p_track ) )
1364 return; /* cannot create chunks index */
1367 /* create sample index table needed for reading and seeking */
1368 if( TrackCreateSamplesIndex( p_input, p_track ) )
1370 return; /* cannot create samples index */
1373 p_track->i_chunk = 0;
1374 p_track->i_sample = 0;
1375 /* now create es but does not select it */
1376 /* XXX needed else vlc won't know this track exist */
1377 if( TrackCreateES( p_input,
1378 p_track, p_track->i_chunk,
1380 &p_track->p_pes_init ) )
1382 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1383 p_track->i_track_ID );
1390 for( i = 0; i < p_track->i_chunk_count; i++ )
1392 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1397 p_track->b_ok = VLC_TRUE;
1400 /****************************************************************************
1402 ****************************************************************************
1403 * Destroy a track created by MP4_TrackCreate.
1404 ****************************************************************************/
1405 static void MP4_TrackDestroy( input_thread_t *p_input,
1406 track_data_mp4_t *p_track )
1408 unsigned int i_chunk;
1410 p_track->b_ok = VLC_FALSE;
1411 p_track->b_enable = VLC_FALSE;
1412 p_track->b_selected = VLC_FALSE;
1414 p_track->i_cat = UNKNOWN_ES;
1416 if( p_track->p_pes_init )
1418 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1421 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1423 if( p_track->chunk )
1425 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1426 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1429 FREE( p_track->chunk );
1431 if( !p_track->i_sample_size )
1433 FREE( p_track->p_sample_size );
1437 static int MP4_TrackSelect ( input_thread_t *p_input,
1438 track_data_mp4_t *p_track,
1444 if( !p_track->b_ok )
1446 return( VLC_EGENERIC );
1449 if( p_track->b_selected )
1452 "track[Id 0x%x] already selected",
1453 p_track->i_track_ID );
1454 return( VLC_SUCCESS );
1457 if( TrackTimeToSampleChunk( p_input,
1459 &i_chunk, &i_sample ) )
1462 "cannot select track[Id 0x%x]",
1463 p_track->i_track_ID );
1464 return( VLC_EGENERIC );
1467 p_track->b_selected = VLC_TRUE;
1469 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1471 p_track->b_selected = VLC_FALSE;
1474 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1477 static void MP4_TrackUnselect(input_thread_t *p_input,
1478 track_data_mp4_t *p_track )
1480 if( !p_track->b_ok )
1485 if( !p_track->b_selected )
1488 "track[Id 0x%x] already unselected",
1489 p_track->i_track_ID );
1493 if( p_track->p_es->p_decoder_fifo )
1495 vlc_mutex_lock( &p_input->stream.stream_lock );
1496 input_UnselectES( p_input, p_track->p_es );
1497 vlc_mutex_unlock( &p_input->stream.stream_lock );
1500 p_track->b_selected = VLC_FALSE;
1503 static int MP4_TrackSeek ( input_thread_t *p_input,
1504 track_data_mp4_t *p_track,
1510 if( !p_track->b_ok )
1512 return( VLC_EGENERIC );
1515 if( TrackTimeToSampleChunk( p_input,
1517 &i_chunk, &i_sample ) )
1520 "cannot select track[Id 0x%x]",
1521 p_track->i_track_ID );
1522 return( VLC_EGENERIC );
1525 p_track->b_selected = VLC_TRUE;
1527 TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample );
1529 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1537 * 3 types: for audio
1541 static int MP4_TrackSampleSize( track_data_mp4_t *p_track )
1544 MP4_Box_data_sample_soun_t *p_soun;
1546 if( p_track->i_sample_size == 0 )
1548 /* most simple case */
1549 return( p_track->p_sample_size[p_track->i_sample] );
1551 if( p_track->i_cat != AUDIO_ES )
1553 return( p_track->i_sample_size );
1556 if( p_track->i_sample_size != 1 )
1558 //msg_Warn( p_input, "SampleSize != 1" );
1559 return( p_track->i_sample_size );
1562 p_soun = p_track->p_sample->data.p_sample_soun;
1564 if( p_soun->i_qt_version == 1 )
1566 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 //msg_Warn( p_input, "i_qt_version == 0" );
1573 i_size = p_track->chunk[p_track->i_chunk].i_sample_count * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1576 //fprintf( stderr, "size=%d\n", i_size );
1581 static uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
1583 unsigned int i_sample;
1587 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1589 if( p_track->i_sample_size )
1592 i_pos += ( p_track->i_sample -
1593 p_track->chunk[p_track->i_chunk].i_sample_first ) *
1594 MP4_TrackSampleSize( p_track );
1596 /* we read chunk by chunk */
1601 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1602 i_sample < p_track->i_sample; i_sample++ )
1604 i_pos += p_track->p_sample_size[i_sample];
1611 static int MP4_TrackNextSample( input_thread_t *p_input,
1612 track_data_mp4_t *p_track )
1615 if( p_track->i_cat == AUDIO_ES &&
1616 p_track->i_sample_size != 0 )
1618 MP4_Box_data_sample_soun_t *p_soun;
1620 p_soun = p_track->p_sample->data.p_sample_soun;
1622 if( p_soun->i_qt_version == 1 )
1624 /* chunk by chunk */
1626 p_track->chunk[p_track->i_chunk].i_sample_first +
1627 p_track->chunk[p_track->i_chunk].i_sample_count;
1633 p_track->chunk[p_track->i_chunk].i_sample_first +
1634 p_track->chunk[p_track->i_chunk].i_sample_count;
1639 p_track->i_sample++;
1642 if( p_track->i_sample >= p_track->i_sample_count )
1644 /* we have reach end of the track so free decoder stuff */
1645 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1646 MP4_TrackUnselect( p_input, p_track );
1647 return( VLC_EGENERIC );
1650 /* Have we changed chunk ? */
1651 if( p_track->i_sample >=
1652 p_track->chunk[p_track->i_chunk].i_sample_first +
1653 p_track->chunk[p_track->i_chunk].i_sample_count )
1655 if( TrackGotoChunkSample( p_input,
1657 p_track->i_chunk + 1,
1658 p_track->i_sample ) )
1660 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1661 MP4_TrackUnselect( p_input, p_track );
1662 return( VLC_EGENERIC );
1666 return( VLC_SUCCESS );