1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.2 2002/08/07 00:29:36 sam 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 #define MP4End(a) __MP4End(VLC_OBJECT(a))
46 /*****************************************************************************
48 *****************************************************************************/
50 set_description( "MP4 demuxer" );
51 set_capability( "demux", 242 );
52 set_callbacks( MP4Init, __MP4End );
55 /*****************************************************************************
56 * Declaration of local function
57 *****************************************************************************/
58 static void MP4_ParseTrack();
60 static int MP4_CreateChunksIndex();
61 static int MP4_CreateSamplesIndex();
63 static void MP4_StartDecoder();
64 static void MP4_StopDecoder();
66 static int MP4_ReadSample();
67 static int MP4_DecodeSample();
69 #define MP4_Set4BytesLE( p, dw ) \
70 *((u8*)p) = ( (dw)&0xff ); \
71 *((u8*)p+1) = ( ((dw)>> 8)&0xff ); \
72 *((u8*)p+2) = ( ((dw)>>16)&0xff ); \
73 *((u8*)p+3) = ( ((dw)>>24)&0xff )
75 #define MP4_Set2BytesLE( p, dw ) \
76 *((u8*)p) = ( (dw)&0xff ); \
77 *((u8*)p+1) = ( ((dw)>> 8)&0xff )
80 /*****************************************************************************
81 * MP4Init: check file and initializes MP4 structures
82 *****************************************************************************/
83 static int MP4Init( vlc_object_t * p_this )
85 input_thread_t *p_input = (input_thread_t *)p_this;
100 if( !p_input->stream.b_seekable )
102 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
106 /* Initialize access plug-in structures. */
107 if( p_input->i_mtu == 0 )
110 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
113 p_input->pf_demux = MP4Demux;
115 /* a little test to see if it could be a mp4 */
116 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
118 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
121 i_type = ( p_peek[4] ) + ( p_peek[5] << 8 ) +
122 ( p_peek[6] << 16 ) + ( p_peek[7] << 24);
129 case( FOURCC_udta ): /* should never match but ... */
132 case( FOURCC_wide ): /* not mp4 compliant but ... */
135 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
139 /* create our structure that will contains all data */
140 if( !( p_input->p_demux_data =
141 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
143 msg_Err( p_input, "out of memory" );
146 memset( p_demux, 0, sizeof( demux_sys_t ) );
147 p_input->p_demux_data = p_demux;
150 /* Now load all boxes ( except raw data ) */
151 if( !MP4_ReadBoxRoot( p_input, &p_demux->box_root ) )
153 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
157 MP4_DumpBoxStructure( p_input, &p_demux->box_root );
159 if( ( p_ftyp = MP4_FindBox( &p_demux->box_root, FOURCC_ftyp ) ) )
161 switch( p_ftyp->data.p_ftyp->i_major_brand )
165 "ISO Media file (isom) version %d.",
166 p_ftyp->data.p_ftyp->i_minor_version );
170 "Unrecognize major file specification (%c%c%c%c).",
171 p_ftyp->data.p_ftyp->i_major_brand&0xff,
172 ( p_ftyp->data.p_ftyp->i_major_brand >> 8)&0xff,
173 ( p_ftyp->data.p_ftyp->i_major_brand >> 16 )&0xff,
174 ( p_ftyp->data.p_ftyp->i_major_brand >> 24 )&0xff );
180 msg_Info( p_input, "File Type box missing(assume ISO Media file)" );
183 /* the file need to have one moov box */
184 if( !( p_moov = MP4_FindBox( &p_demux->box_root, FOURCC_moov ) ) )
186 msg_Warn( p_input, "MP4 plugin discarded (missing moov box)" );
191 if( MP4_CountBox( &p_demux->box_root, FOURCC_moov ) != 1 )
193 msg_Warn( p_input, "more than one \"moov\" box (continuying anyway)" );
196 if( !(p_mvhd = MP4_FindBox( p_moov, FOURCC_mvhd ) ) )
198 msg_Err( p_input, "cannot find \"mvhd\" box" );
204 p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
207 p_demux->i_tracks = MP4_CountBox( p_moov, FOURCC_trak );
208 msg_Dbg( p_input, "find %d track%c",
210 p_demux->i_tracks ? 's':' ' );
212 if( !( p_trak = MP4_FindBox( p_moov, FOURCC_trak ) ) )
214 msg_Err( p_input, "cannot find /moov/trak !" );
219 /* allocate memory */
220 p_demux->track = calloc( p_demux->i_tracks, sizeof( track_data_mp4_t ) );
222 /* now process each track and extract all usefull informations */
223 for( i = 0; i < p_demux->i_tracks; i++ )
225 MP4_ParseTrack( p_input, &p_demux->track[i], p_trak );
227 if( p_demux->track[i].b_ok )
230 switch( p_demux->track[i].i_cat )
243 msg_Dbg( p_input, "adding track(%d) %s (%s) language %c%c%c",
246 p_demux->track[i].b_enable ? "enable":"disable",
247 p_demux->track[i].i_language[0],
248 p_demux->track[i].i_language[1],
249 p_demux->track[i].i_language[2] );
253 msg_Dbg( p_input, "ignoring track(%d)", i );
256 p_trak = MP4_FindNextBox( p_trak );
259 /* create one program */
260 vlc_mutex_lock( &p_input->stream.stream_lock );
261 if( input_InitStream( p_input, 0 ) == -1)
263 vlc_mutex_unlock( &p_input->stream.stream_lock );
264 msg_Err( p_input, "cannot init stream" );
268 if( input_AddProgram( p_input, 0, 0) == NULL )
270 vlc_mutex_unlock( &p_input->stream.stream_lock );
271 msg_Err( p_input, "cannot add program" );
275 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
276 p_input->stream.i_mux_rate = 0 ; /* FIXME */
277 vlc_mutex_unlock( &p_input->stream.stream_lock );
280 for( i = 0; i < p_demux->i_tracks; i++ )
282 /* start decoder for this track if enable by default*/
283 if( p_demux->track[i].b_enable )
285 MP4_StartDecoder( p_input, &p_demux->track[i] );
289 vlc_mutex_lock( &p_input->stream.stream_lock );
290 p_input->stream.p_selected_program->b_is_ok = 1;
291 vlc_mutex_unlock( &p_input->stream.stream_lock );
297 /*****************************************************************************
298 * MP4Demux: read packet and send them to decoders
299 *****************************************************************************/
300 static int MP4Demux( input_thread_t *p_input )
302 demux_sys_t *p_demux = p_input->p_demux_data;
305 /* first wait for the good time to read a packet */
307 input_ClockManageRef( p_input,
308 p_input->stream.p_selected_program,
312 /* update pcr XXX in mpeg scale so in 90000 unit/s */
313 p_demux->i_pcr = MP4_GetMoviePTS( p_demux ) * 9 / 100;
316 /* we will read 100ms for each stream so ...*/
317 p_demux->i_time += __MAX( p_demux->i_timescale / 10 , 1 );
320 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
322 if( ( !p_demux->track[i_track].b_ok )||
323 ( !p_demux->track[i_track].p_es )||
324 ( !p_demux->track[i_track].p_es->p_decoder_fifo )||
325 ( MP4_GetTrackPTS( &p_demux->track[i_track] ) >=
326 MP4_GetMoviePTS( p_demux ) ) )
328 continue; /* no need to read something */
331 while( MP4_GetTrackPTS( &p_demux->track[i_track] ) <
332 MP4_GetMoviePTS( p_demux ) )
338 if( !MP4_ReadSample( p_input ,
339 &p_demux->track[i_track],
345 /* send it to decoder and update time of this track
346 it also launch a new decoder if needed */
347 MP4_DecodeSample( p_input ,
348 &p_demux->track[i_track],
354 /* now check if all tracks are finished or unhandled*/
356 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
358 if( ( p_demux->track[i_track].b_ok )&&
359 ( p_demux->track[i_track].i_sample < p_demux->track[i_track].i_sample_count )&&
360 ( p_demux->track[i_track].p_es )&&
361 ( p_demux->track[i_track].p_es->p_decoder_fifo ) )
367 return( 0 ); /* EOF */
370 /*****************************************************************************
371 * MP4End: frees unused data
372 *****************************************************************************/
373 static void __MP4End ( vlc_object_t * p_this )
376 if( p ) { free( p ); }
378 input_thread_t * p_input = (input_thread_t *)p_this;
379 demux_sys_t *p_demux = p_input->p_demux_data;
381 msg_Dbg( p_input, "Freeing all memory" );
382 MP4_FreeBox( p_input, &p_demux->box_root );
383 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
387 i_chunk < p_demux->track[i_track].i_chunk_count; i_chunk++ )
389 if( p_demux->track[i_track].chunk )
391 FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_count_dts);
392 FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_delta_dts );
396 if( !p_demux->track[i_track].i_sample_size )
398 FREE( p_demux->track[i_track].p_sample_size );
401 FREE( p_demux->track );
406 /****************************************************************************
407 * Local functions, specific to vlc
408 ****************************************************************************/
410 /****************************************************************************
411 * Parse track information and create all needed data to run a track
412 * If it succeed b_ok is set to 1 else to 0
413 ****************************************************************************/
414 static void MP4_ParseTrack( input_thread_t *p_input,
415 track_data_mp4_t *p_demux_track,
420 MP4_Box_t *p_tkhd = MP4_FindBox( p_trak, FOURCC_tkhd );
421 MP4_Box_t *p_tref = MP4_FindBox( p_trak, FOURCC_tref );
422 MP4_Box_t *p_edts = MP4_FindBox( p_trak, FOURCC_edts );
423 MP4_Box_t *p_mdia = MP4_FindBox( p_trak, FOURCC_mdia );
432 /* hint track unsuported */
434 /* by default, track isn't usable */
435 p_demux_track->b_ok = 0;
437 /* by default, we don't known the categorie */
438 p_demux_track->i_cat = UNKNOWN_ES;
440 if( ( !p_tkhd )||( !p_mdia ) )
445 /* do we launch this track by default ? */
446 p_demux_track->b_enable = ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
448 p_demux_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
449 p_demux_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
450 p_demux_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
454 // msg_Warn( p_input, "Unhandled box: edts --> FIXME" );
459 // msg_Warn( p_input, "Unhandled box: tref --> FIXME" );
462 p_mdhd = MP4_FindBox( p_mdia, FOURCC_mdhd );
463 p_hdlr = MP4_FindBox( p_mdia, FOURCC_hdlr );
464 p_minf = MP4_FindBox( p_mdia, FOURCC_minf );
466 if( ( !p_mdhd )||( !p_hdlr )||( !p_minf ) )
471 p_demux_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
473 for( i = 0; i < 3; i++ )
475 p_demux_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
478 switch( p_hdlr->data.p_hdlr->i_handler_type )
481 if( !( p_smhd = MP4_FindBox( p_minf, FOURCC_smhd ) ) )
485 p_demux_track->i_cat = AUDIO_ES;
489 if( !( p_vmhd = MP4_FindBox( p_minf, FOURCC_vmhd ) ) )
493 p_demux_track->i_cat = VIDEO_ES;
501 p_dinf = MP4_FindBox( p_minf, FOURCC_dinf );
503 if( !( p_demux_track->p_stbl = MP4_FindBox( p_minf, FOURCC_stbl ) ) )
508 if( !( p_demux_track->p_stsd = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsd ) ) )
513 /* Create chunk index table */
514 if( !MP4_CreateChunksIndex( p_input,p_demux_track ) )
516 return; /* cannot create chunks index */
519 /* create sample index table needed for reading and seeking */
520 if( !MP4_CreateSamplesIndex( p_input, p_demux_track ) )
522 return; /* cannot create samples index */
525 p_demux_track->b_ok = 1;
530 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
531 static int MP4_CreateChunksIndex( input_thread_t *p_input,
532 track_data_mp4_t *p_demux_track )
534 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
541 if( ( !(p_co64 = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stco ) )&&
542 !(p_co64 = MP4_FindBox( p_demux_track->p_stbl, FOURCC_co64 ) ) )||
543 ( !(p_stsc = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsc ) ) ))
548 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
549 if( !p_demux_track->i_chunk_count )
551 msg_Warn( p_input, "No chunk defined" );
554 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
555 sizeof( chunk_data_mp4_t ) );
557 /* first we read chunk offset */
558 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
560 p_demux_track->chunk[i_chunk].i_offset =
561 p_co64->data.p_co64->i_chunk_offset[i_chunk];
564 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
565 to be used for the sample XXX begin to 1
566 We construct it begining at the end */
567 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
568 i_index = p_stsc->data.p_stsc->i_entry_count;
571 msg_Warn( p_input, "cannot read chunk table or table empty" );
578 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
579 i_chunk < i_last; i_chunk++ )
581 p_demux_track->chunk[i_chunk].i_sample_description_index =
582 p_stsc->data.p_stsc->i_sample_description_index[i_index];
583 p_demux_track->chunk[i_chunk].i_sample_count =
584 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
586 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
589 p_demux_track->chunk[i_chunk].i_sample_first = 0;
590 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
592 p_demux_track->chunk[i_chunk].i_sample_first =
593 p_demux_track->chunk[i_chunk-1].i_sample_first +
594 p_demux_track->chunk[i_chunk-1].i_sample_count;
598 msg_Dbg( p_input, "read %d chunk", p_demux_track->i_chunk_count );
605 static int MP4_CreateSamplesIndex( input_thread_t *p_input,
606 track_data_mp4_t *p_demux_track )
608 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
609 ctts make same mapping but for composition time,
610 not yet used and probably not usefull */
611 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
612 that uses a compressed form FIXME make them in libmp4
614 /* TODO use also stss and stsh table for seeking */
615 /* FIXME use edit table */
620 int i_index_sample_used;
624 p_stts = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stts );
625 p_stsz = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsz ); /* FIXME and stz2 */
628 if( ( !p_stts )||( !p_stsz ) )
630 msg_Warn( p_input, "cannot read sample table" );
634 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
637 /* for sample size, there are 2 case */
638 if( p_stsz->data.p_stsz->i_sample_size )
640 /* 1: all sample have the same size, so no need to construct a table */
641 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
642 p_demux_track->p_sample_size = NULL;
646 /* 2: each sample can have a different size */
647 p_demux_track->i_sample_size = 0;
648 p_demux_track->p_sample_size =
649 calloc( p_demux_track->i_sample_count, sizeof( u32 ) );
651 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
653 p_demux_track->p_sample_size[i_sample] =
654 p_stsz->data.p_stsz->i_entry_size[i_sample];
657 /* we have extract all information from stsz,
660 /* if we don't want to waste too much memory, we can't expand
661 the box !, so each chunk will contain an "extract" of this table
665 i_index = 0; i_index_sample_used =0;
666 /* create and init last data for each chunk */
667 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
670 int i_entry, i_sample_count, i;
672 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
673 /* count how many entries needed for this chunk
674 for p_sample_delta_dts and p_sample_count_dts */
677 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
678 while( i_sample_count > 0 )
680 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
683 i_sample_count += i_index_sample_used; /* don't count already used sample
689 p_demux_track->chunk[i_chunk].p_sample_count_dts =
690 calloc( i_entry, sizeof( u32 ) );
691 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
692 calloc( i_entry, sizeof( u32 ) );
695 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
696 for( i = 0; i < i_entry; i++ )
701 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
703 i_used = __MIN( i_rest, i_sample_count );
705 i_index_sample_used += i_used;
707 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
709 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
710 p_stts->data.p_stts->i_sample_delta[i_index];
712 i_last_dts += i_used *
713 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
715 if( i_index_sample_used >=
716 p_stts->data.p_stts->i_sample_count[i_index] )
719 i_index_sample_used = 0;
725 msg_Dbg( p_input, "read %d samples", p_demux_track->i_sample_count );
730 static void MP4_StartDecoder( input_thread_t *p_input,
731 track_data_mp4_t *p_demux_track )
737 int i_decoder_specific_info_len;
738 u8 *p_decoder_specific_info;
745 if( (!p_demux_track->b_ok )||( p_demux_track->i_cat == UNKNOWN_ES ) )
750 msg_Dbg( p_input, "Starting decoder (track ID 0x%x)",
751 p_demux_track->i_track_ID );
753 /* launch decoder according in chunk we are */
754 i_chunk = p_demux_track->i_chunk;
756 if( !p_demux_track->chunk[i_chunk].i_sample_description_index )
759 "invalid SampleEntry index (track ID 0x%x)",
760 p_demux_track->i_track_ID );
764 p_sample = MP4_FindNbBox( p_demux_track->p_stsd,
765 p_demux_track->chunk[i_chunk].i_sample_description_index - 1);
767 if( ( !p_sample )||( !p_sample->data.p_data ) )
770 "cannot find SampleEntry (track ID 0x%x)",
771 p_demux_track->i_track_ID );
775 vlc_mutex_lock( &p_input->stream.stream_lock );
776 p_demux_track->p_es = input_AddES( p_input,
777 p_input->stream.p_selected_program,
778 p_demux_track->i_track_ID,
780 vlc_mutex_unlock( &p_input->stream.stream_lock );
781 /* Initialise ES, first language as description */
782 for( i = 0; i < 3; i++ )
784 p_demux_track->p_es->psz_desc[i] = p_demux_track->i_language[i];
786 p_demux_track->p_es->psz_desc[4] = 0;
788 p_demux_track->p_es->i_stream_id = p_demux_track->i_track_ID;
790 /* It's a little ugly but .. there are special cases */
791 switch( p_sample->i_type )
793 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
794 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
795 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
798 p_demux_track->p_es->i_fourcc = p_sample->i_type;
802 p_demux_track->p_es->i_cat = p_demux_track->i_cat;
804 i_decoder_specific_info_len = 0;
805 p_decoder_specific_info = NULL;
807 /* now see if esds is present and if so create a data packet
808 with decoder_specific_info */
809 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
810 if( ( p_esds = MP4_FindBox( p_sample, FOURCC_esds ) )&&
811 ( p_esds->data.p_esds )&&
814 /* First update information based on i_objectTypeIndication */
815 switch( p_decconfig->i_objectTypeIndication )
817 case( 0x20 ): /* MPEG4 VIDEO */
818 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
821 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
828 case( 0x65): /* MPEG2 video */
829 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
831 /* Theses are MPEG2-AAC (what is this codec ?) */
832 case( 0x66): /* main profile */
833 case( 0x67): /* Low complexity profile */
834 case( 0x68): /* Scaleable Sampling rate profile */
835 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
837 /* true MPEG 2 audio */
839 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
841 case( 0x6a): /* MPEG1 video */
842 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
844 case( 0x6b): /* MPEG1 audio */
845 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
847 case( 0x6c ): /* jpeg */
848 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
851 /* Unknown entry, but don't touch i_fourcc */
853 "objectTypeIndication(0x%x) unknow (Track ID 0x%x)",
854 p_decconfig->i_objectTypeIndication,
855 p_demux_track->i_track_ID );
858 i_decoder_specific_info_len =
859 p_decconfig->i_decoder_specific_info_len;
860 p_decoder_specific_info =
861 p_decconfig->p_decoder_specific_info;
866 /* some last initialisation */
867 /* XXX I create a bitmapinfoheader_t or
868 waveformatex_t for each stream, up to now it's the best thing
869 I've found but it could exist a better solution :) as something
870 like adding some new fields in p_es ...
872 XXX I don't set all values, only thoses that are interesting or known
873 --> bitmapinfoheader_t : width and height
874 --> waveformatex_t : channels, samplerate, bitspersample
875 and at the end I add p_decoder_specific_info
881 switch( p_demux_track->i_cat )
884 /* now create a bitmapinfoheader_t for decoder and
885 add information found in p_esds */
886 p_init = malloc( 40 + i_decoder_specific_info_len);
887 memset( p_init, 0, 40 + i_decoder_specific_info_len);
888 MP4_Set4BytesLE( p_init, 40 + i_decoder_specific_info_len );
889 if( p_sample->data.p_sample_vide->i_width )
891 MP4_Set4BytesLE( p_init + 4,
892 p_sample->data.p_sample_vide->i_width );
896 /* use display size */
897 MP4_Set4BytesLE( p_init + 4, p_demux_track->i_width );
899 if( p_sample->data.p_sample_vide->i_height )
901 MP4_Set4BytesLE( p_init + 8,
902 p_sample->data.p_sample_vide->i_height );
906 MP4_Set4BytesLE( p_init + 8, p_demux_track->i_height );
908 if( i_decoder_specific_info_len )
911 p_decoder_specific_info,
912 i_decoder_specific_info_len);
917 p_init = malloc( 18 + i_decoder_specific_info_len);
918 memset( p_init, 0, 18 + i_decoder_specific_info_len);
919 MP4_Set2BytesLE( p_init + 2, /* i_channel */
920 p_sample->data.p_sample_soun->i_channelcount );
921 MP4_Set4BytesLE( p_init + 4, /* samplepersec */
922 p_sample->data.p_sample_soun->i_sampleratehi );
923 MP4_Set4BytesLE( p_init + 8, /* avgbytespersec */
924 p_sample->data.p_sample_soun->i_channelcount *
925 p_sample->data.p_sample_soun->i_sampleratehi *
926 (p_sample->data.p_sample_soun->i_samplesize/8) );
927 MP4_Set2BytesLE( p_init + 14, /* bits/sample */
928 p_sample->data.p_sample_soun->i_samplesize );
930 MP4_Set2BytesLE( p_init + 16, /* i_size, specific info len*/
931 i_decoder_specific_info_len );
932 if( i_decoder_specific_info_len )
935 p_decoder_specific_info,
936 i_decoder_specific_info_len);
945 p_demux_track->p_es->p_demux_data = (es_sys_t *)p_init;
946 vlc_mutex_lock( &p_input->stream.stream_lock );
947 input_SelectES( p_input, p_demux_track->p_es );
948 vlc_mutex_unlock( &p_input->stream.stream_lock );
950 p_demux_track->b_ok = 1;
953 static void MP4_StopDecoder( input_thread_t *p_input,
954 track_data_mp4_t *p_demux_track )
956 msg_Dbg( p_input, "Stopping decoder (track ID 0x%x)",
957 p_demux_track->i_track_ID );
959 input_UnselectES( p_input, p_demux_track->p_es );
960 p_demux_track->p_es = NULL;
963 static int MP4_ReadSample( input_thread_t *p_input,
964 track_data_mp4_t *p_demux_track,
965 pes_packet_t **pp_pes )
970 data_packet_t *p_data;
973 /* this track have already reach the end */
974 if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
979 /* caculate size and position for this sample */
980 i_size = p_demux_track->i_sample_size ?
981 p_demux_track->i_sample_size :
982 p_demux_track->p_sample_size[p_demux_track->i_sample];
984 i_pos = MP4_GetTrackPos( p_demux_track );
987 if( ! MP4_SeekAbsolute( p_input, i_pos ) )
992 /* now create a pes */
993 if( !(*pp_pes = input_NewPES( p_input->p_method_data ) ) )
997 /* and a data packet for the data */
998 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
1000 input_DeletePES( p_input->p_method_data, *pp_pes );
1005 /* initialisation of all the field */
1007 (*pp_pes)->i_pts = MP4_GetTrackPTS( p_demux_track );
1008 (*pp_pes)->p_first = (*pp_pes)->p_last = p_data;
1009 (*pp_pes)->i_nb_data = 1;
1010 (*pp_pes)->i_pes_size = i_size;
1017 // msg_Dbg( p_input, "will read %d bytes", i_size );
1018 if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
1020 input_DeletePES( p_input->p_method_data, *pp_pes );
1021 input_DeletePacket( p_input->p_method_data, p_data );
1029 static int MP4_DecodeSample( input_thread_t *p_input,
1030 track_data_mp4_t *p_demux_track,
1031 pes_packet_t *p_pes )
1039 /* don't forget to convert in mpeg clock */
1040 /* FIXME correct ffmpeg to use dts instead of pts that it incorrect
1041 and, set it here ( and correct avi demux ) */
1043 p_pes->i_pts = input_ClockGetTS( p_input,
1044 p_input->stream.p_selected_program,
1045 p_pes->i_pts * 9/100);
1048 input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes );
1050 /* now update sample position */
1051 p_demux_track->i_sample++; /* easy ;) */
1052 if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
1054 /* we have reach end of the track so free decoder stuff */
1055 MP4_StopDecoder( p_input, p_demux_track );
1058 /* Have we changed chunk ? */
1059 if( p_demux_track->i_sample >=
1060 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_first +
1061 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_count )
1063 /* we haven't reached the end of the track, so see if we
1064 have to change the decoder for the next frame because
1065 i_sample_description_index have changed */
1067 p_demux_track->i_chunk++;
1068 if( p_demux_track->chunk[p_demux_track->i_chunk-1].i_sample_description_index
1069 != p_demux_track->chunk[p_demux_track->i_chunk].i_sample_description_index )
1073 "SampleEntry have changed, starting a new decoder" );
1074 MP4_StopDecoder( p_input, p_demux_track );
1075 MP4_StartDecoder( p_input, p_demux_track );