1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.15 2003/01/25 17:57:36 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 int MP4_TrackSynchro( input_thread_t *p_input, track_data_mp4_t *p_track );
64 static void MP4_ParseTrack();
66 static int MP4_CreateChunksIndex();
67 static int MP4_CreateSamplesIndex();
69 static void MP4_StartDecoder();
70 static void MP4_StopDecoder();
72 static int MP4_ReadSample();
73 static int MP4_DecodeSample();
75 #define MP4_Set4BytesLE( p, dw ) \
76 *((uint8_t*)p) = ( (dw)&0xff ); \
77 *((uint8_t*)p+1) = ( ((dw)>> 8)&0xff ); \
78 *((uint8_t*)p+2) = ( ((dw)>>16)&0xff ); \
79 *((uint8_t*)p+3) = ( ((dw)>>24)&0xff )
81 #define MP4_Set2BytesLE( p, dw ) \
82 *((uint8_t*)p) = ( (dw)&0xff ); \
83 *((uint8_t*)p+1) = ( ((dw)>> 8)&0xff )
86 /*****************************************************************************
87 * MP4Init: check file and initializes MP4 structures
88 *****************************************************************************/
89 static int MP4Init( vlc_object_t * p_this )
91 input_thread_t *p_input = (input_thread_t *)p_this;
104 if( !p_input->stream.b_seekable )
106 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
110 /* Initialize access plug-in structures. */
111 if( p_input->i_mtu == 0 )
114 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
117 p_input->pf_demux = MP4Demux;
119 /* a little test to see if it could be a mp4 */
120 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
122 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
127 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
139 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
143 /* create our structure that will contains all data */
144 if( !( p_input->p_demux_data =
145 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
147 msg_Err( p_input, "out of memory" );
150 memset( p_demux, 0, sizeof( demux_sys_t ) );
151 p_input->p_demux_data = p_demux;
154 /* Now load all boxes ( except raw data ) */
155 if( !MP4_BoxGetRoot( p_input, &p_demux->box_root ) )
157 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
161 MP4_BoxDumpStructure( p_input, &p_demux->box_root );
163 if( ( p_ftyp = MP4_BoxGet( &p_demux->box_root, "/ftyp" ) ) )
165 switch( p_ftyp->data.p_ftyp->i_major_brand )
169 "ISO Media file (isom) version %d.",
170 p_ftyp->data.p_ftyp->i_minor_version );
174 "Unrecognize major file specification (%c%c%c%c).",
175 p_ftyp->data.p_ftyp->i_major_brand&0xff,
176 ( p_ftyp->data.p_ftyp->i_major_brand >> 8)&0xff,
177 ( p_ftyp->data.p_ftyp->i_major_brand >> 16 )&0xff,
178 ( p_ftyp->data.p_ftyp->i_major_brand >> 24 )&0xff );
184 msg_Info( p_input, "File Type box missing(assume ISO Media file)" );
187 /* the file need to have one moov box */
188 if( MP4_BoxCount( &p_demux->box_root, "/moov" ) != 1 )
191 "MP4 plugin discarded (%d moov box)",
192 MP4_BoxCount( &p_demux->box_root, "/moov" ) );
193 // MP4End( p_input );
197 if( !(p_mvhd = MP4_BoxGet( &p_demux->box_root, "/moov/mvhd" ) ) )
199 msg_Err( p_input, "cannot find /moov/mvhd !" );
205 p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
206 p_demux->i_duration = p_mvhd->data.p_mvhd->i_duration;
209 if( !( p_demux->i_tracks =
210 MP4_BoxCount( &p_demux->box_root, "/moov/trak" ) ) )
212 msg_Err( p_input, "cannot find any /moov/trak !" );
216 msg_Dbg( p_input, "find %d track%c",
218 p_demux->i_tracks ? 's':' ' );
220 /* allocate memory */
221 p_demux->track = calloc( p_demux->i_tracks, sizeof( track_data_mp4_t ) );
223 /* now process each track and extract all usefull informations */
224 for( i = 0; i < p_demux->i_tracks; i++ )
226 p_trak = MP4_BoxGet( &p_demux->box_root, "/moov/trak[%d]", i );
227 MP4_ParseTrack( p_input, &p_demux->track[i], p_trak );
229 if( p_demux->track[i].b_ok )
232 switch( p_demux->track[i].i_cat )
245 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %c%c%c",
246 p_demux->track[i].i_track_ID,
248 p_demux->track[i].b_enable ? "enable":"disable",
249 p_demux->track[i].i_language[0],
250 p_demux->track[i].i_language[1],
251 p_demux->track[i].i_language[2] );
255 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_demux->track[i].i_track_ID );
260 /* create one program */
261 vlc_mutex_lock( &p_input->stream.stream_lock );
262 if( input_InitStream( p_input, 0 ) == -1)
264 vlc_mutex_unlock( &p_input->stream.stream_lock );
265 msg_Err( p_input, "cannot init stream" );
269 if( input_AddProgram( p_input, 0, 0) == NULL )
271 vlc_mutex_unlock( &p_input->stream.stream_lock );
272 msg_Err( p_input, "cannot add program" );
276 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
277 /* XXX beurk and beurk, see MP4Demux and MP4Seek */
278 if( p_demux->i_duration/p_demux->i_timescale > 0 )
280 p_input->stream.i_mux_rate =
281 p_input->stream.p_selected_area->i_size / 50 /
282 ( p_demux->i_duration / p_demux->i_timescale );
286 p_input->stream.i_mux_rate = 0;
288 vlc_mutex_unlock( &p_input->stream.stream_lock );
291 for( i = 0; i < p_demux->i_tracks; i++ )
293 /* start decoder for this track if enable by default*/
294 if( p_demux->track[i].b_enable )
296 MP4_StartDecoder( p_input, &p_demux->track[i] );
300 vlc_mutex_lock( &p_input->stream.stream_lock );
301 p_input->stream.p_selected_program->b_is_ok = 1;
302 vlc_mutex_unlock( &p_input->stream.stream_lock );
308 /*****************************************************************************
309 * MP4Demux: read packet and send them to decoders
310 *****************************************************************************
311 * TODO check for newly selected track (ie audio upt to now )
312 *****************************************************************************/
313 static int MP4Demux( input_thread_t *p_input )
315 demux_sys_t *p_demux = p_input->p_demux_data;
316 unsigned int i_track;
318 /* XXX beurk, beuRK and BEURK,
319 but only way I've found to detect seek from interface */
320 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
324 /* first wait for empty buffer, arbitrary time FIXME */
325 msleep( DEFAULT_PTS_DELAY );
326 /* *** calculate new date *** */
328 i_date = (mtime_t)1000000 *
329 (mtime_t)p_demux->i_duration /
330 (mtime_t)p_demux->i_timescale *
331 (mtime_t)MP4_TellAbsolute( p_input ) /
332 (mtime_t)p_input->stream.p_selected_area->i_size;
333 MP4Seek( p_input, i_date );
336 /* first wait for the good time to read a packet */
337 input_ClockManageRef( p_input,
338 p_input->stream.p_selected_program,
342 /* update pcr XXX in mpeg scale so in 90000 unit/s */
343 p_demux->i_pcr = MP4_GetMoviePTS( p_demux ) * 9 / 100;
346 /* we will read 100ms for each stream so ...*/
347 p_demux->i_time += __MAX( p_demux->i_timescale / 10 , 1 );
350 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
352 if( ( !p_demux->track[i_track].b_ok )||
353 ( !p_demux->track[i_track].p_es )||
354 ( !p_demux->track[i_track].p_es->p_decoder_fifo )||
355 ( MP4_GetTrackPTS( &p_demux->track[i_track] ) >=
356 MP4_GetMoviePTS( p_demux ) ) )
358 continue; /* no need to read something */
360 while( MP4_GetTrackPTS( &p_demux->track[i_track] ) <
361 MP4_GetMoviePTS( p_demux ) )
367 if( !MP4_ReadSample( p_input ,
368 &p_demux->track[i_track],
374 /* send it to decoder and update time of this track
375 it also launch a new decoder if needed */
376 MP4_DecodeSample( p_input ,
377 &p_demux->track[i_track],
383 /* now check if all tracks are finished or unhandled*/
385 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
387 if( ( p_demux->track[i_track].b_ok )&&
388 ( p_demux->track[i_track].i_sample < p_demux->track[i_track].i_sample_count )&&
389 ( p_demux->track[i_track].p_es )&&
390 ( p_demux->track[i_track].p_es->p_decoder_fifo ) )
396 return( 0 ); /* EOF */
398 /*****************************************************************************
399 * MP4Seek: Got to i_date
400 ******************************************************************************/
401 static int MP4Seek ( input_thread_t *p_input, mtime_t i_date )
403 demux_sys_t *p_demux = p_input->p_demux_data;
404 unsigned int i_track;
406 /* First update update global time */
407 p_demux->i_time = i_date * p_demux->i_timescale / 1000000;
408 p_demux->i_pcr = __MAX( MP4_GetMoviePTS( p_demux ) - DEFAULT_PTS_DELAY,
411 /* Now for each stream try to go to this time */
412 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
414 MP4_TrackSynchro( p_input, &p_demux->track[i_track] );
419 /*****************************************************************************
420 * MP4End: frees unused data
421 *****************************************************************************/
422 static void __MP4End ( vlc_object_t * p_this )
425 if( p ) { free( p ); }
426 unsigned int i_track;
427 input_thread_t * p_input = (input_thread_t *)p_this;
428 demux_sys_t *p_demux = p_input->p_demux_data;
430 msg_Dbg( p_input, "Freeing all memory" );
431 MP4_BoxFree( p_input, &p_demux->box_root );
432 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
434 unsigned int i_chunk;
436 i_chunk < p_demux->track[i_track].i_chunk_count; i_chunk++ )
438 if( p_demux->track[i_track].chunk )
440 FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_count_dts);
441 FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_delta_dts );
444 FREE( p_demux->track[i_track].chunk );
446 if( !p_demux->track[i_track].i_sample_size )
448 FREE( p_demux->track[i_track].p_sample_size );
451 FREE( p_demux->track );
453 FREE( p_input->p_demux_data );
458 /****************************************************************************
459 * Local functions, specific to vlc
460 ****************************************************************************/
462 /****************************************************************************
463 * MP4_TrackSynchro : synchronize a track with movie time after seek or
464 * for newly selected track
465 *****************************************************************************
466 * TODO add support of Edit List (edts/elts) and Shadow Sync Sample(stsh)
467 ****************************************************************************/
468 static int MP4_TrackSynchro( input_thread_t *p_input, track_data_mp4_t *p_track )
470 demux_sys_t *p_demux = p_input->p_demux_data;
471 unsigned int i_chunk_last;
474 if( !p_track->b_ok ||
476 !p_track->p_es->p_decoder_fifo )
480 p_track->i_sample = 0;
481 i_chunk_last = p_track->i_chunk;
482 p_track->i_chunk = 0;
485 if( p_track->i_sample >= p_track->i_sample_count )
488 "track[Id 0x%x] will be disabled (seeking too far)",
489 p_track->i_track_ID );
490 MP4_StopDecoder( p_input, p_track );
493 if( MP4_GetTrackPTS( p_track ) >= MP4_GetMoviePTS( p_demux ) )
497 /* go one sample after */
499 if( p_track->i_sample >= p_track->chunk[p_track->i_chunk].i_sample_first +
500 p_track->chunk[p_track->i_chunk].i_sample_count )
506 if( p_track->i_sample >= p_track->i_sample_count )
511 /* *** Try to find nearest sync points *** */
512 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
514 unsigned int i_index;
516 "track[Id 0x%x] using Sync Sample Box (stss)",
517 p_track->i_track_ID );
518 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
520 if( p_stss->data.p_stss->i_sample_number[i_index] >= p_track->i_sample )
524 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
526 p_stss->data.p_stss->i_sample_number[i_index-1] );
527 p_track->i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
528 /* new track.i_sample is less than old so i_chunk can only decreased */
529 while( p_track->i_chunk > 0 &&
530 p_track->i_sample < p_track->chunk[p_track->i_chunk].i_sample_first )
537 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
539 p_stss->data.p_stss->i_sample_number[i_index-1] );
540 p_track->i_sample = p_stss->data.p_stss->i_sample_number[i_index];
541 /* new track.i_sample is more than old so i_chunk can only increased */
542 while( p_track->i_chunk < p_track->i_chunk_count - 1 &&
543 p_track->i_sample >= p_track->chunk[p_track->i_chunk].i_sample_first +
544 p_track->chunk[p_track->i_chunk].i_sample_count )
556 "track[Id 0x%x] doesn't provided Sync Sample Box (stss)",
557 p_track->i_track_ID );
560 /* *** If i_sample_description_index has changed restart decoder *** */
561 if( p_track->chunk[i_chunk_last].i_sample_description_index !=
562 p_track->chunk[p_track->i_chunk].i_sample_description_index )
565 "SampleEntry has changed, restarting decoder" );
566 MP4_StopDecoder( p_input, p_track );
567 MP4_StartDecoder( p_input, p_track );
572 /****************************************************************************
573 * Parse track information and create all needed data to run a track
574 * If it succeed b_ok is set to 1 else to 0
575 ****************************************************************************/
576 static void MP4_ParseTrack( input_thread_t *p_input,
577 track_data_mp4_t *p_demux_track,
582 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
583 MP4_Box_t *p_tref = MP4_BoxGet( p_trak, "tref" );
592 /* hint track unsuported */
594 /* by default, track isn't usable */
595 p_demux_track->b_ok = 0;
597 /* by default, we don't known the categorie */
598 p_demux_track->i_cat = UNKNOWN_ES;
605 /* do we launch this track by default ? */
606 p_demux_track->b_enable =
607 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
609 p_demux_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
610 p_demux_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
611 p_demux_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
613 if( ( p_elst = MP4_BoxGet( p_trak, "edts/elst" ) ) )
615 /* msg_Warn( p_input, "Unhandled box: edts --> FIXME" ); */
620 /* msg_Warn( p_input, "Unhandled box: tref --> FIXME" ); */
623 p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
624 p_hdlr = MP4_BoxGet( p_trak, "mdia/hdlr" );
626 if( ( !p_mdhd )||( !p_hdlr ) )
631 p_demux_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
633 for( i = 0; i < 3; i++ )
635 p_demux_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
637 p_mdhd->data.p_mdhd->i_language[3] = 0;
639 switch( p_hdlr->data.p_hdlr->i_handler_type )
642 if( !( p_smhd = MP4_BoxGet( p_trak, "mdia/minf/smhd" ) ) )
646 p_demux_track->i_cat = AUDIO_ES;
650 if( !( p_vmhd = MP4_BoxGet( p_trak, "mdia/minf/vmhd" ) ) )
654 p_demux_track->i_cat = VIDEO_ES;
662 p_dinf = MP4_BoxGet( p_minf, "dinf" );
664 if( !( p_demux_track->p_stbl = MP4_BoxGet( p_trak,"mdia/minf/stbl" ) ) )
669 if( !( p_demux_track->p_stsd = MP4_BoxGet( p_trak,"mdia/minf/stbl/stsd") ) )
674 /* Create chunk index table */
675 if( !MP4_CreateChunksIndex( p_input,p_demux_track ) )
677 return; /* cannot create chunks index */
680 /* create sample index table needed for reading and seeking */
681 if( !MP4_CreateSamplesIndex( p_input, p_demux_track ) )
683 return; /* cannot create samples index */
686 p_demux_track->b_ok = 1;
691 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
692 static int MP4_CreateChunksIndex( input_thread_t *p_input,
693 track_data_mp4_t *p_demux_track )
695 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
698 unsigned int i_chunk;
699 unsigned int i_index, i_last;
702 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
703 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
704 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
709 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
710 if( !p_demux_track->i_chunk_count )
712 msg_Warn( p_input, "No chunk defined" );
715 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
716 sizeof( chunk_data_mp4_t ) );
718 /* first we read chunk offset */
719 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
721 p_demux_track->chunk[i_chunk].i_offset =
722 p_co64->data.p_co64->i_chunk_offset[i_chunk];
725 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
726 to be used for the sample XXX begin to 1
727 We construct it begining at the end */
728 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
729 i_index = p_stsc->data.p_stsc->i_entry_count;
732 msg_Warn( p_input, "cannot read chunk table or table empty" );
739 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
740 i_chunk < i_last; i_chunk++ )
742 p_demux_track->chunk[i_chunk].i_sample_description_index =
743 p_stsc->data.p_stsc->i_sample_description_index[i_index];
744 p_demux_track->chunk[i_chunk].i_sample_count =
745 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
747 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
750 p_demux_track->chunk[i_chunk].i_sample_first = 0;
751 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
753 p_demux_track->chunk[i_chunk].i_sample_first =
754 p_demux_track->chunk[i_chunk-1].i_sample_first +
755 p_demux_track->chunk[i_chunk-1].i_sample_count;
760 "track[Id 0x%x] read %d chunk",
761 p_demux_track->i_track_ID,
762 p_demux_track->i_chunk_count );
769 static int MP4_CreateSamplesIndex( input_thread_t *p_input,
770 track_data_mp4_t *p_demux_track )
772 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
773 ctts make same mapping but for composition time,
774 not yet used and probably not usefull */
775 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
776 that uses a compressed form FIXME make them in libmp4
778 /* TODO use also stss and stsh table for seeking */
779 /* FIXME use edit table */
784 int64_t i_index_sample_used;
788 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
789 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
792 if( ( !p_stts )||( !p_stsz ) )
794 msg_Warn( p_input, "cannot read sample table" );
798 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
801 /* for sample size, there are 2 case */
802 if( p_stsz->data.p_stsz->i_sample_size )
804 /* 1: all sample have the same size, so no need to construct a table */
805 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
806 p_demux_track->p_sample_size = NULL;
810 /* 2: each sample can have a different size */
811 p_demux_track->i_sample_size = 0;
812 p_demux_track->p_sample_size =
813 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
815 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
817 p_demux_track->p_sample_size[i_sample] =
818 p_stsz->data.p_stsz->i_entry_size[i_sample];
821 /* we have extract all information from stsz,
824 /* if we don't want to waste too much memory, we can't expand
825 the box !, so each chunk will contain an "extract" of this table
829 i_index = 0; i_index_sample_used =0;
831 /* create and init last data for each chunk */
832 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
835 int64_t i_entry, i_sample_count, i;
837 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
838 /* count how many entries needed for this chunk
839 for p_sample_delta_dts and p_sample_count_dts */
841 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
844 while( i_sample_count > 0 )
846 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
849 i_sample_count += i_index_sample_used; /* don't count already used sample
856 p_demux_track->chunk[i_chunk].p_sample_count_dts =
857 calloc( i_entry, sizeof( uint32_t ) );
858 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
859 calloc( i_entry, sizeof( uint32_t ) );
862 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
863 for( i = 0; i < i_entry; i++ )
868 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
870 i_used = __MIN( i_rest, i_sample_count );
872 i_index_sample_used += i_used;
873 i_sample_count -= i_used;
875 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
877 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
878 p_stts->data.p_stts->i_sample_delta[i_index];
880 i_last_dts += i_used *
881 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
883 if( i_index_sample_used >=
884 p_stts->data.p_stts->i_sample_count[i_index] )
888 i_index_sample_used = 0;
895 "track[Id 0x%x] read %d samples length:"I64Fd"s",
896 p_demux_track->i_track_ID,
897 p_demux_track->i_sample_count,
898 i_last_dts / p_demux_track->i_timescale );
903 static void MP4_StartDecoder( input_thread_t *p_input,
904 track_data_mp4_t *p_demux_track )
906 MP4_Box_t * p_sample;
908 unsigned int i_chunk;
910 unsigned int i_decoder_specific_info_len;
911 uint8_t * p_decoder_specific_info;
912 pes_packet_t *p_pes_init;
915 BITMAPINFOHEADER *p_bih;
921 if( (!p_demux_track->b_ok )||( p_demux_track->i_cat == UNKNOWN_ES ) )
926 msg_Dbg( p_input, "Starting decoder for track[Id 0x%x]",
927 p_demux_track->i_track_ID );
929 /* launch decoder according in chunk we are */
930 i_chunk = p_demux_track->i_chunk;
932 if( !p_demux_track->chunk[i_chunk].i_sample_description_index )
935 "invalid SampleEntry index (track[Id 0x%x])",
936 p_demux_track->i_track_ID );
939 p_sample = MP4_BoxGet( p_demux_track->p_stsd,
941 p_demux_track->chunk[i_chunk].i_sample_description_index - 1 );
943 if( ( !p_sample )||( !p_sample->data.p_data ) )
946 "cannot find SampleEntry (track[Id 0x%x])",
947 p_demux_track->i_track_ID );
951 vlc_mutex_lock( &p_input->stream.stream_lock );
952 p_demux_track->p_es = input_AddES( p_input,
953 p_input->stream.p_selected_program,
954 p_demux_track->i_track_ID,
956 vlc_mutex_unlock( &p_input->stream.stream_lock );
957 /* Initialise ES, first language as description */
958 for( i = 0; i < 3; i++ )
960 p_demux_track->p_es->psz_desc[i] = p_demux_track->i_language[i];
962 p_demux_track->p_es->psz_desc[3] = '\0';
964 p_demux_track->p_es->i_stream_id = p_demux_track->i_track_ID;
966 /* It's a little ugly but .. there are special cases */
967 switch( p_sample->i_type )
969 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
970 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
971 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
974 p_demux_track->p_es->i_fourcc = p_sample->i_type;
978 p_demux_track->p_es->i_cat = p_demux_track->i_cat;
980 i_decoder_specific_info_len = 0;
981 p_decoder_specific_info = NULL;
984 /* now see if esds is present and if so create a data packet
985 with decoder_specific_info */
986 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
987 if( ( p_esds = MP4_BoxGet( p_sample, "esds" ) )&&
988 ( p_esds->data.p_esds )&&
991 /* First update information based on i_objectTypeIndication */
992 switch( p_decconfig->i_objectTypeIndication )
994 case( 0x20 ): /* MPEG4 VIDEO */
995 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
998 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1005 case( 0x65): /* MPEG2 video */
1006 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1008 /* Theses are MPEG2-AAC */
1009 case( 0x66): /* main profile */
1010 case( 0x67): /* Low complexity profile */
1011 case( 0x68): /* Scaleable Sampling rate profile */
1012 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1014 /* true MPEG 2 audio */
1016 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1018 case( 0x6a): /* MPEG1 video */
1019 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1021 case( 0x6b): /* MPEG1 audio */
1022 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1024 case( 0x6c ): /* jpeg */
1025 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
1028 /* Unknown entry, but don't touch i_fourcc */
1030 "objectTypeIndication(0x%x) unknow (Track[ID 0x%x])",
1031 p_decconfig->i_objectTypeIndication,
1032 p_demux_track->i_track_ID );
1035 i_decoder_specific_info_len =
1036 p_decconfig->i_decoder_specific_info_len;
1037 p_decoder_specific_info =
1038 p_decconfig->p_decoder_specific_info;
1043 /* some last initialisation */
1044 /* XXX I create a bitmapinfoheader_t or
1045 waveformatex_t for each stream, up to now it's the best thing
1046 I've found but it could exist a better solution :) as something
1047 like adding some new fields in p_es ...
1049 XXX I don't set all values, only thoses that are interesting or known
1050 --> bitmapinfoheader_t : width and height
1051 --> waveformatex_t : channels, samplerate, bitspersample
1052 and at the end I add p_decoder_specific_info
1054 TODO set more values
1058 switch( p_demux_track->i_cat )
1061 /* now create a bitmapinfoheader_t for decoder and
1062 add information found in p_esds */
1063 /* XXX XXX + 16 are for avoid segfault when ffmpeg access beyong the data */
1064 p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len + 16 );
1065 p_bih = (BITMAPINFOHEADER*)p_init;
1067 p_bih->biSize = sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len;
1068 p_bih->biWidth = p_sample->data.p_sample_vide->i_width;
1069 p_bih->biHeight = p_sample->data.p_sample_vide->i_height;
1070 p_bih->biPlanes = 1; // FIXME
1071 p_bih->biBitCount = 0; // FIXME
1072 p_bih->biCompression = 0; // FIXME
1073 p_bih->biSizeImage = 0; // FIXME
1074 p_bih->biXPelsPerMeter = 0; // FIXME
1075 p_bih->biYPelsPerMeter = 0; // FIXME
1076 p_bih->biClrUsed = 0; // FIXME
1077 p_bih->biClrImportant = 0; // FIXME
1079 if( p_bih->biWidth == 0 )
1081 // fall on display size
1082 p_bih->biWidth = p_demux_track->i_width;
1084 if( p_bih->biHeight == 0 )
1086 // fall on display size
1087 p_bih->biHeight = p_demux_track->i_height;
1090 if( i_decoder_specific_info_len )
1092 data_packet_t *p_data;
1094 memcpy( p_init + sizeof( BITMAPINFOHEADER ),
1095 p_decoder_specific_info,
1096 i_decoder_specific_info_len);
1098 /* If stream is mpeg4 video we send specific_info,
1099 as it's needed to decode it (vol) */
1100 switch( p_demux_track->p_es->i_fourcc )
1102 case VLC_FOURCC( 'm','p','4','v' ):
1103 case VLC_FOURCC( 'D','I','V','X' ):
1104 case VLC_FOURCC( 'd','i','v','x' ):
1105 p_pes_init = input_NewPES( p_input->p_method_data );
1106 p_data = input_NewPacket( p_input->p_method_data,
1107 i_decoder_specific_info_len);
1108 memcpy( p_data->p_payload_start,
1109 p_decoder_specific_info,
1110 i_decoder_specific_info_len );
1111 p_pes_init->i_dts = p_pes_init->i_pts = 0;
1112 p_pes_init->p_first = p_pes_init->p_last = p_data;
1113 p_pes_init->i_nb_data = 1;
1114 p_pes_init->i_pes_size = i_decoder_specific_info_len;
1124 p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len + 16 );
1125 p_wf = (WAVEFORMATEX*)p_init;
1127 p_wf->wFormatTag = 0;
1128 p_wf->nChannels = p_sample->data.p_sample_soun->i_channelcount;
1129 p_wf->nSamplesPerSec = p_sample->data.p_sample_soun->i_sampleratehi;
1130 p_wf->nAvgBytesPerSec = p_sample->data.p_sample_soun->i_channelcount *
1131 p_sample->data.p_sample_soun->i_sampleratehi *
1132 p_sample->data.p_sample_soun->i_samplesize / 8;
1133 p_wf->nBlockAlign = 0;
1134 p_wf->wBitsPerSample = p_sample->data.p_sample_soun->i_samplesize;
1135 p_wf->cbSize = i_decoder_specific_info_len;
1137 if( i_decoder_specific_info_len )
1139 memcpy( p_init + sizeof( WAVEFORMATEX ),
1140 p_decoder_specific_info,
1141 i_decoder_specific_info_len);
1150 if( p_demux_track->i_cat == AUDIO_ES )
1152 p_demux_track->p_es->p_waveformatex = (void*)p_init;
1154 else if( p_demux_track->i_cat == VIDEO_ES )
1156 p_demux_track->p_es->p_bitmapinfoheader = (void*)p_init;
1158 vlc_mutex_lock( &p_input->stream.stream_lock );
1159 input_SelectES( p_input, p_demux_track->p_es );
1160 vlc_mutex_unlock( &p_input->stream.stream_lock );
1162 if( !p_demux_track->p_es->p_decoder_fifo )
1164 msg_Warn( p_input, "cannot start decoder" );
1167 input_DeletePES( p_input->p_method_data, p_pes_init );
1172 if( p_pes_init != NULL )
1174 input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes_init );
1176 p_demux_track->b_ok = 1;
1177 p_demux_track->b_selected = 1;
1180 static void MP4_StopDecoder( input_thread_t *p_input,
1181 track_data_mp4_t *p_demux_track )
1183 msg_Dbg( p_input, "Stopping decoder (track[Id 0x%x])",
1184 p_demux_track->i_track_ID );
1186 input_UnselectES( p_input, p_demux_track->p_es );
1187 p_demux_track->p_es = NULL;
1189 p_demux_track->b_selected = 0;
1192 static int MP4_ReadSample( input_thread_t *p_input,
1193 track_data_mp4_t *p_demux_track,
1194 pes_packet_t **pp_pes )
1199 data_packet_t *p_data;
1202 /* this track have already reach the end */
1203 if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
1208 /* caculate size and position for this sample */
1209 i_size = p_demux_track->i_sample_size ?
1210 p_demux_track->i_sample_size :
1211 p_demux_track->p_sample_size[p_demux_track->i_sample];
1213 i_pos = MP4_GetTrackPos( p_demux_track );
1216 if( ! MP4_SeekAbsolute( p_input, i_pos ) )
1221 /* now create a pes */
1222 if( !(*pp_pes = input_NewPES( p_input->p_method_data ) ) )
1226 /* and a data packet for the data */
1227 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
1229 input_DeletePES( p_input->p_method_data, *pp_pes );
1234 /* initialisation of all the field */
1236 (*pp_pes)->i_pts = MP4_GetTrackPTS( p_demux_track );
1237 (*pp_pes)->p_first = (*pp_pes)->p_last = p_data;
1238 (*pp_pes)->i_nb_data = 1;
1239 (*pp_pes)->i_pes_size = i_size;
1246 /* msg_Dbg( p_input, "will read %d bytes", i_size ); */
1247 if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
1249 input_DeletePES( p_input->p_method_data, *pp_pes );
1257 static int MP4_DecodeSample( input_thread_t *p_input,
1258 track_data_mp4_t *p_demux_track,
1259 pes_packet_t *p_pes )
1267 /* don't forget to convert in mpeg clock */
1268 /* FIXME correct ffmpeg to use dts instead of pts that it incorrect
1269 and, set it here ( and correct avi demux ) */
1271 p_pes->i_pts = input_ClockGetTS( p_input,
1272 p_input->stream.p_selected_program,
1273 p_pes->i_pts * 9/100);
1276 input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes );
1278 /* now update sample position */
1279 p_demux_track->i_sample++; /* easy ;) */
1280 if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
1282 /* we have reach end of the track so free decoder stuff */
1283 MP4_StopDecoder( p_input, p_demux_track );
1286 /* Have we changed chunk ? */
1287 if( p_demux_track->i_sample >=
1288 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_first +
1289 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_count )
1291 /* we haven't reached the end of the track, so see if we
1292 have to change the decoder for the next frame because
1293 i_sample_description_index has changed */
1295 p_demux_track->i_chunk++;
1296 if( p_demux_track->chunk[p_demux_track->i_chunk-1].i_sample_description_index
1297 != p_demux_track->chunk[p_demux_track->i_chunk].i_sample_description_index )
1301 "SampleEntry has changed, starting a new decoder" );
1302 MP4_StopDecoder( p_input, p_demux_track );
1303 MP4_StartDecoder( p_input, p_demux_track );