1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.8 2002/11/28 16:32:29 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" ) );
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;
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;
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 ); }
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++ )
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 );
445 if( !p_demux->track[i_track].i_sample_size )
447 FREE( p_demux->track[i_track].p_sample_size );
450 FREE( p_demux->track );
455 /****************************************************************************
456 * Local functions, specific to vlc
457 ****************************************************************************/
459 /****************************************************************************
460 * MP4_TrackSynchro : synchronize a track with movie time after seek or
461 * for newly selected track
462 *****************************************************************************
463 * TODO add support of Edit List (edts/elts) and Shadow Sync Sample(stsh)
464 ****************************************************************************/
465 static int MP4_TrackSynchro( input_thread_t *p_input, track_data_mp4_t *p_track )
467 demux_sys_t *p_demux = p_input->p_demux_data;
471 if( !p_track->b_ok ||
473 !p_track->p_es->p_decoder_fifo )
477 p_track->i_sample = 0;
478 i_chunk_last = p_track->i_chunk;
479 p_track->i_chunk = 0;
482 if( p_track->i_sample >= p_track->i_sample_count )
485 "track[Id 0x%x] will be disabled (seeking too far)",
486 p_track->i_track_ID );
487 MP4_StopDecoder( p_input, p_track );
490 if( MP4_GetTrackPTS( p_track ) >= MP4_GetMoviePTS( p_demux ) )
494 /* go one sample after */
496 if( p_track->i_sample >= p_track->chunk[p_track->i_chunk].i_sample_first +
497 p_track->chunk[p_track->i_chunk].i_sample_count )
503 if( p_track->i_sample >= p_track->i_sample_count )
508 /* *** Try to find nearest sync points *** */
509 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
513 "track[Id 0x%x] using Sync Sample Box (stss)",
514 p_track->i_track_ID );
515 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
517 if( p_stss->data.p_stss->i_sample_number[i_index] >= p_track->i_sample )
521 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
523 p_stss->data.p_stss->i_sample_number[i_index-1] );
524 p_track->i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
525 /* new track.i_sample is less than old so i_chunk can only decreased */
526 while( p_track->i_chunk > 0 &&
527 p_track->i_sample < p_track->chunk[p_track->i_chunk].i_sample_first )
534 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
536 p_stss->data.p_stss->i_sample_number[i_index-1] );
537 p_track->i_sample = p_stss->data.p_stss->i_sample_number[i_index];
538 /* new track.i_sample is more than old so i_chunk can only increased */
539 while( p_track->i_chunk < p_track->i_chunk_count - 1 &&
540 p_track->i_sample >= p_track->chunk[p_track->i_chunk].i_sample_first +
541 p_track->chunk[p_track->i_chunk].i_sample_count )
553 "track[Id 0x%x] doesn't provided Sync Sample Box (stss)",
554 p_track->i_track_ID );
557 /* *** If i_sample_description_index has changed restart decoder *** */
558 if( p_track->chunk[i_chunk_last].i_sample_description_index !=
559 p_track->chunk[p_track->i_chunk].i_sample_description_index )
562 "SampleEntry has changed, restarting decoder" );
563 MP4_StopDecoder( p_input, p_track );
564 MP4_StartDecoder( p_input, p_track );
569 /****************************************************************************
570 * Parse track information and create all needed data to run a track
571 * If it succeed b_ok is set to 1 else to 0
572 ****************************************************************************/
573 static void MP4_ParseTrack( input_thread_t *p_input,
574 track_data_mp4_t *p_demux_track,
579 MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
580 MP4_Box_t *p_tref = MP4_BoxGet( p_trak, "tref" );
589 /* hint track unsuported */
591 /* by default, track isn't usable */
592 p_demux_track->b_ok = 0;
594 /* by default, we don't known the categorie */
595 p_demux_track->i_cat = UNKNOWN_ES;
602 /* do we launch this track by default ? */
603 p_demux_track->b_enable =
604 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
606 p_demux_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
607 p_demux_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
608 p_demux_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
610 if( ( p_elst = MP4_BoxGet( p_trak, "edts/elst" ) ) )
612 /* msg_Warn( p_input, "Unhandled box: edts --> FIXME" ); */
617 /* msg_Warn( p_input, "Unhandled box: tref --> FIXME" ); */
620 p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
621 p_hdlr = MP4_BoxGet( p_trak, "mdia/hdlr" );
623 if( ( !p_mdhd )||( !p_hdlr ) )
628 p_demux_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
630 for( i = 0; i < 3; i++ )
632 p_demux_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
634 p_mdhd->data.p_mdhd->i_language[3] = 0;
636 switch( p_hdlr->data.p_hdlr->i_handler_type )
639 if( !( p_smhd = MP4_BoxGet( p_trak, "mdia/minf/smhd" ) ) )
643 p_demux_track->i_cat = AUDIO_ES;
647 if( !( p_vmhd = MP4_BoxGet( p_trak, "mdia/minf/vmhd" ) ) )
651 p_demux_track->i_cat = VIDEO_ES;
659 p_dinf = MP4_BoxGet( p_minf, "dinf" );
661 if( !( p_demux_track->p_stbl = MP4_BoxGet( p_trak,"mdia/minf/stbl" ) ) )
666 if( !( p_demux_track->p_stsd = MP4_BoxGet( p_trak,"mdia/minf/stbl/stsd") ) )
671 /* Create chunk index table */
672 if( !MP4_CreateChunksIndex( p_input,p_demux_track ) )
674 return; /* cannot create chunks index */
677 /* create sample index table needed for reading and seeking */
678 if( !MP4_CreateSamplesIndex( p_input, p_demux_track ) )
680 return; /* cannot create samples index */
683 p_demux_track->b_ok = 1;
688 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
689 static int MP4_CreateChunksIndex( input_thread_t *p_input,
690 track_data_mp4_t *p_demux_track )
692 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
699 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
700 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
701 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
706 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
707 if( !p_demux_track->i_chunk_count )
709 msg_Warn( p_input, "No chunk defined" );
712 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
713 sizeof( chunk_data_mp4_t ) );
715 /* first we read chunk offset */
716 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
718 p_demux_track->chunk[i_chunk].i_offset =
719 p_co64->data.p_co64->i_chunk_offset[i_chunk];
722 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
723 to be used for the sample XXX begin to 1
724 We construct it begining at the end */
725 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
726 i_index = p_stsc->data.p_stsc->i_entry_count;
729 msg_Warn( p_input, "cannot read chunk table or table empty" );
736 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
737 i_chunk < i_last; i_chunk++ )
739 p_demux_track->chunk[i_chunk].i_sample_description_index =
740 p_stsc->data.p_stsc->i_sample_description_index[i_index];
741 p_demux_track->chunk[i_chunk].i_sample_count =
742 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
744 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
747 p_demux_track->chunk[i_chunk].i_sample_first = 0;
748 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
750 p_demux_track->chunk[i_chunk].i_sample_first =
751 p_demux_track->chunk[i_chunk-1].i_sample_first +
752 p_demux_track->chunk[i_chunk-1].i_sample_count;
757 "track[Id 0x%x] read %d chunk",
758 p_demux_track->i_track_ID,
759 p_demux_track->i_chunk_count );
766 static int MP4_CreateSamplesIndex( input_thread_t *p_input,
767 track_data_mp4_t *p_demux_track )
769 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
770 ctts make same mapping but for composition time,
771 not yet used and probably not usefull */
772 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
773 that uses a compressed form FIXME make them in libmp4
775 /* TODO use also stss and stsh table for seeking */
776 /* FIXME use edit table */
781 int64_t i_index_sample_used;
785 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
786 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
789 if( ( !p_stts )||( !p_stsz ) )
791 msg_Warn( p_input, "cannot read sample table" );
795 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
798 /* for sample size, there are 2 case */
799 if( p_stsz->data.p_stsz->i_sample_size )
801 /* 1: all sample have the same size, so no need to construct a table */
802 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
803 p_demux_track->p_sample_size = NULL;
807 /* 2: each sample can have a different size */
808 p_demux_track->i_sample_size = 0;
809 p_demux_track->p_sample_size =
810 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
812 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
814 p_demux_track->p_sample_size[i_sample] =
815 p_stsz->data.p_stsz->i_entry_size[i_sample];
818 /* we have extract all information from stsz,
821 /* if we don't want to waste too much memory, we can't expand
822 the box !, so each chunk will contain an "extract" of this table
826 i_index = 0; i_index_sample_used =0;
828 /* create and init last data for each chunk */
829 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
832 int64_t i_entry, i_sample_count, i;
834 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
835 /* count how many entries needed for this chunk
836 for p_sample_delta_dts and p_sample_count_dts */
838 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
841 while( i_sample_count > 0 )
843 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
846 i_sample_count += i_index_sample_used; /* don't count already used sample
853 p_demux_track->chunk[i_chunk].p_sample_count_dts =
854 calloc( i_entry, sizeof( uint32_t ) );
855 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
856 calloc( i_entry, sizeof( uint32_t ) );
859 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
860 for( i = 0; i < i_entry; i++ )
865 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
867 i_used = __MIN( i_rest, i_sample_count );
869 i_index_sample_used += i_used;
870 i_sample_count -= i_used;
872 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
874 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
875 p_stts->data.p_stts->i_sample_delta[i_index];
877 i_last_dts += i_used *
878 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
880 if( i_index_sample_used >=
881 p_stts->data.p_stts->i_sample_count[i_index] )
885 i_index_sample_used = 0;
892 "track[Id 0x%x] read %d samples length:"I64Fd"s",
893 p_demux_track->i_track_ID,
894 p_demux_track->i_sample_count,
895 i_last_dts / p_demux_track->i_timescale );
900 static void MP4_StartDecoder( input_thread_t *p_input,
901 track_data_mp4_t *p_demux_track )
907 int i_decoder_specific_info_len;
908 uint8_t *p_decoder_specific_info;
911 BITMAPINFOHEADER *p_bih;
917 if( (!p_demux_track->b_ok )||( p_demux_track->i_cat == UNKNOWN_ES ) )
922 msg_Dbg( p_input, "Starting decoder for track[Id 0x%x]",
923 p_demux_track->i_track_ID );
925 /* launch decoder according in chunk we are */
926 i_chunk = p_demux_track->i_chunk;
928 if( !p_demux_track->chunk[i_chunk].i_sample_description_index )
931 "invalid SampleEntry index (track[Id 0x%x])",
932 p_demux_track->i_track_ID );
935 p_sample = MP4_BoxGet( p_demux_track->p_stsd,
937 p_demux_track->chunk[i_chunk].i_sample_description_index - 1 );
939 if( ( !p_sample )||( !p_sample->data.p_data ) )
942 "cannot find SampleEntry (track[Id 0x%x])",
943 p_demux_track->i_track_ID );
947 vlc_mutex_lock( &p_input->stream.stream_lock );
948 p_demux_track->p_es = input_AddES( p_input,
949 p_input->stream.p_selected_program,
950 p_demux_track->i_track_ID,
952 vlc_mutex_unlock( &p_input->stream.stream_lock );
953 /* Initialise ES, first language as description */
954 for( i = 0; i < 3; i++ )
956 p_demux_track->p_es->psz_desc[i] = p_demux_track->i_language[i];
958 p_demux_track->p_es->psz_desc[3] = '\0';
960 p_demux_track->p_es->i_stream_id = p_demux_track->i_track_ID;
962 /* It's a little ugly but .. there are special cases */
963 switch( p_sample->i_type )
965 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
966 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
967 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
970 p_demux_track->p_es->i_fourcc = p_sample->i_type;
974 p_demux_track->p_es->i_cat = p_demux_track->i_cat;
976 i_decoder_specific_info_len = 0;
977 p_decoder_specific_info = NULL;
979 /* now see if esds is present and if so create a data packet
980 with decoder_specific_info */
981 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
982 if( ( p_esds = MP4_BoxGet( p_sample, "esds" ) )&&
983 ( p_esds->data.p_esds )&&
986 /* First update information based on i_objectTypeIndication */
987 switch( p_decconfig->i_objectTypeIndication )
989 case( 0x20 ): /* MPEG4 VIDEO */
990 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
993 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1000 case( 0x65): /* MPEG2 video */
1001 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1003 /* Theses are MPEG2-AAC */
1004 case( 0x66): /* main profile */
1005 case( 0x67): /* Low complexity profile */
1006 case( 0x68): /* Scaleable Sampling rate profile */
1007 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1009 /* true MPEG 2 audio */
1011 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1013 case( 0x6a): /* MPEG1 video */
1014 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1016 case( 0x6b): /* MPEG1 audio */
1017 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1019 case( 0x6c ): /* jpeg */
1020 p_demux_track->p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
1023 /* Unknown entry, but don't touch i_fourcc */
1025 "objectTypeIndication(0x%x) unknow (Track[ID 0x%x])",
1026 p_decconfig->i_objectTypeIndication,
1027 p_demux_track->i_track_ID );
1030 i_decoder_specific_info_len =
1031 p_decconfig->i_decoder_specific_info_len;
1032 p_decoder_specific_info =
1033 p_decconfig->p_decoder_specific_info;
1038 /* some last initialisation */
1039 /* XXX I create a bitmapinfoheader_t or
1040 waveformatex_t for each stream, up to now it's the best thing
1041 I've found but it could exist a better solution :) as something
1042 like adding some new fields in p_es ...
1044 XXX I don't set all values, only thoses that are interesting or known
1045 --> bitmapinfoheader_t : width and height
1046 --> waveformatex_t : channels, samplerate, bitspersample
1047 and at the end I add p_decoder_specific_info
1049 TODO set more values
1053 switch( p_demux_track->i_cat )
1056 /* now create a bitmapinfoheader_t for decoder and
1057 add information found in p_esds */
1058 p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len );
1059 p_bih = (BITMAPINFOHEADER*)p_init;
1061 p_bih->biSize = sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len;
1062 p_bih->biWidth = p_sample->data.p_sample_vide->i_width;
1063 p_bih->biHeight = p_sample->data.p_sample_vide->i_height;
1064 p_bih->biPlanes = 1; // FIXME
1065 p_bih->biBitCount = 0; // FIXME
1066 p_bih->biCompression = 0; // FIXME
1067 p_bih->biSizeImage = 0; // FIXME
1068 p_bih->biXPelsPerMeter = 0; // FIXME
1069 p_bih->biYPelsPerMeter = 0; // FIXME
1070 p_bih->biClrUsed = 0; // FIXME
1071 p_bih->biClrImportant = 0; // FIXME
1073 if( p_bih->biWidth == 0 )
1075 // fall on display size
1076 p_bih->biWidth = p_demux_track->i_width;
1078 if( p_bih->biHeight == 0 )
1080 // fall on display size
1081 p_bih->biHeight = p_demux_track->i_height;
1084 if( i_decoder_specific_info_len )
1086 memcpy( p_init + sizeof( BITMAPINFOHEADER ),
1087 p_decoder_specific_info,
1088 i_decoder_specific_info_len);
1093 p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len);
1094 p_wf = (WAVEFORMATEX*)p_init;
1096 p_wf->wFormatTag = 0;
1097 p_wf->nChannels = p_sample->data.p_sample_soun->i_channelcount;
1098 p_wf->nSamplesPerSec = p_sample->data.p_sample_soun->i_sampleratehi;
1099 p_wf->nAvgBytesPerSec = p_sample->data.p_sample_soun->i_channelcount *
1100 p_sample->data.p_sample_soun->i_sampleratehi *
1101 p_sample->data.p_sample_soun->i_samplesize / 8;
1102 p_wf->nBlockAlign = 0;
1103 p_wf->wBitsPerSample = p_sample->data.p_sample_soun->i_samplesize;
1104 p_wf->cbSize = i_decoder_specific_info_len;
1106 if( i_decoder_specific_info_len )
1108 memcpy( p_init + sizeof( WAVEFORMATEX ),
1109 p_decoder_specific_info,
1110 i_decoder_specific_info_len);
1119 p_demux_track->p_es->p_demux_data = (es_sys_t *)p_init;
1120 vlc_mutex_lock( &p_input->stream.stream_lock );
1121 input_SelectES( p_input, p_demux_track->p_es );
1122 vlc_mutex_unlock( &p_input->stream.stream_lock );
1124 p_demux_track->b_ok = 1;
1125 p_demux_track->b_selected = 1;
1128 static void MP4_StopDecoder( input_thread_t *p_input,
1129 track_data_mp4_t *p_demux_track )
1131 msg_Dbg( p_input, "Stopping decoder (track[Id 0x%x])",
1132 p_demux_track->i_track_ID );
1134 input_UnselectES( p_input, p_demux_track->p_es );
1135 p_demux_track->p_es = NULL;
1137 p_demux_track->b_selected = 0;
1140 static int MP4_ReadSample( input_thread_t *p_input,
1141 track_data_mp4_t *p_demux_track,
1142 pes_packet_t **pp_pes )
1147 data_packet_t *p_data;
1150 /* this track have already reach the end */
1151 if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
1156 /* caculate size and position for this sample */
1157 i_size = p_demux_track->i_sample_size ?
1158 p_demux_track->i_sample_size :
1159 p_demux_track->p_sample_size[p_demux_track->i_sample];
1161 i_pos = MP4_GetTrackPos( p_demux_track );
1164 if( ! MP4_SeekAbsolute( p_input, i_pos ) )
1169 /* now create a pes */
1170 if( !(*pp_pes = input_NewPES( p_input->p_method_data ) ) )
1174 /* and a data packet for the data */
1175 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
1177 input_DeletePES( p_input->p_method_data, *pp_pes );
1182 /* initialisation of all the field */
1184 (*pp_pes)->i_pts = MP4_GetTrackPTS( p_demux_track );
1185 (*pp_pes)->p_first = (*pp_pes)->p_last = p_data;
1186 (*pp_pes)->i_nb_data = 1;
1187 (*pp_pes)->i_pes_size = i_size;
1194 /* msg_Dbg( p_input, "will read %d bytes", i_size ); */
1195 if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
1197 input_DeletePES( p_input->p_method_data, *pp_pes );
1198 input_DeletePacket( p_input->p_method_data, p_data );
1206 static int MP4_DecodeSample( input_thread_t *p_input,
1207 track_data_mp4_t *p_demux_track,
1208 pes_packet_t *p_pes )
1216 /* don't forget to convert in mpeg clock */
1217 /* FIXME correct ffmpeg to use dts instead of pts that it incorrect
1218 and, set it here ( and correct avi demux ) */
1220 p_pes->i_pts = input_ClockGetTS( p_input,
1221 p_input->stream.p_selected_program,
1222 p_pes->i_pts * 9/100);
1225 input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes );
1227 /* now update sample position */
1228 p_demux_track->i_sample++; /* easy ;) */
1229 if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
1231 /* we have reach end of the track so free decoder stuff */
1232 MP4_StopDecoder( p_input, p_demux_track );
1235 /* Have we changed chunk ? */
1236 if( p_demux_track->i_sample >=
1237 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_first +
1238 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_count )
1240 /* we haven't reached the end of the track, so see if we
1241 have to change the decoder for the next frame because
1242 i_sample_description_index has changed */
1244 p_demux_track->i_chunk++;
1245 if( p_demux_track->chunk[p_demux_track->i_chunk-1].i_sample_description_index
1246 != p_demux_track->chunk[p_demux_track->i_chunk].i_sample_description_index )
1250 "SampleEntry has changed, starting a new decoder" );
1251 MP4_StopDecoder( p_input, p_demux_track );
1252 MP4_StartDecoder( p_input, p_demux_track );