1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.1 2002/07/17 21:37:27 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 void input_getfunctions( function_list_t * );
41 static int MP4Demux ( input_thread_t * );
42 static int MP4Init ( input_thread_t * );
43 static void MP4End ( input_thread_t * );
45 /*****************************************************************************
46 * Build configuration tree.
47 *****************************************************************************/
52 SET_DESCRIPTION( "MP4 file input" )
53 ADD_CAPABILITY( DEMUX, 242 )
57 input_getfunctions( &p_module->p_functions->demux );
60 MODULE_DEACTIVATE_START
61 MODULE_DEACTIVATE_STOP
63 /*****************************************************************************
64 * Functions exported as capabilities. They are declared as static so that
65 * we don't pollute the namespace too much.
66 *****************************************************************************/
67 static void input_getfunctions( function_list_t * p_function_list )
69 #define input p_function_list->functions.demux
70 input.pf_init = MP4Init;
71 input.pf_end = MP4End;
72 input.pf_demux = MP4Demux;
73 input.pf_rewind = NULL;
77 /*****************************************************************************
78 * Declaration of local function
79 *****************************************************************************/
80 static void MP4_ParseTrack();
82 static int MP4_CreateChunksIndex();
83 static int MP4_CreateSamplesIndex();
85 static void MP4_StartDecoder();
86 static void MP4_StopDecoder();
88 static int MP4_ReadSample();
89 static int MP4_DecodeSample();
91 #define MP4_Set4BytesLE( p, dw ) \
92 *((u8*)p) = ( dw&0xff ); \
93 *((u8*)p+1) = ( ((dw)>> 8)&0xff ); \
94 *((u8*)p+2) = ( ((dw)>>16)&0xff ); \
95 *((u8*)p+3) = ( ((dw)>>24)&0xff )
97 #define MP4_Set2BytesLE( p, dw ) \
98 *((u8*)p) = ( (dw)&0xff ); \
99 *((u8*)p+1) = ( ((dw)>> 8)&0xff )
102 /*****************************************************************************
103 * MP4Init: check file and initializes MP4 structures
104 *****************************************************************************/
105 static int MP4Init( input_thread_t *p_input )
110 demux_data_mp4_t *p_demux;
121 if( !p_input->stream.b_seekable )
123 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
127 /* Initialize access plug-in structures. */
128 if( p_input->i_mtu == 0 )
131 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
134 /* a little test to see if it could be a mp4 */
135 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
137 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
140 i_type = ( p_peek[4] << 24 ) + ( p_peek[5] << 16 ) +
141 ( p_peek[6] << 8 ) + ( p_peek[7] );
148 case( FOURCC_udta ): /* should never match but ... */
151 case( FOURCC_wide ): /* not mp4 compliant but ... */
154 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
158 /* create our structure that will contains all data */
159 if( !( p_input->p_demux_data =
160 p_demux = malloc( sizeof( demux_data_mp4_t ) ) ) )
162 msg_Err( p_input, "out of memory" );
165 memset( p_demux, 0, sizeof( demux_data_mp4_t ) );
166 p_input->p_demux_data = p_demux;
169 /* Now load all boxes ( except raw data ) */
170 if( !MP4_ReadBoxRoot( p_input, &p_demux->box_root ) )
172 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
176 MP4_DumpBoxStructure( p_input, &p_demux->box_root );
178 if( ( p_ftyp = MP4_FindBox( &p_demux->box_root, FOURCC_ftyp ) ) )
180 switch( p_ftyp->data.p_ftyp->i_major_brand )
184 "ISO Media file (isom) version %d.",
185 p_ftyp->data.p_ftyp->i_minor_version );
189 "Unrecognize major file specification." );
195 msg_Info( p_input, "File Type box missing(assume ISO Media file)" );
198 /* the file need to have one moov box */
199 if( !( p_moov = MP4_FindBox( &p_demux->box_root, FOURCC_moov ) ) )
201 msg_Warn( p_input, "MP4 plugin discarded (missing moov box)" );
206 if( MP4_CountBox( &p_demux->box_root, FOURCC_moov ) != 1 )
208 msg_Warn( p_input, "more than one \"moov\" box (continuying anyway)" );
211 if( !(p_mvhd = MP4_FindBox( p_moov, FOURCC_mvhd ) ) )
213 msg_Err( p_input, "cannot find \"mvhd\" box" );
219 p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
222 p_demux->i_tracks = MP4_CountBox( p_moov, FOURCC_trak );
223 msg_Dbg( p_input, "find %d track%c",
225 p_demux->i_tracks ? 's':' ' );
227 if( !( p_trak = MP4_FindBox( p_moov, FOURCC_trak ) ) )
229 msg_Err( p_input, "cannot find /moov/trak !" );
234 /* allocate memory */
235 p_demux->track = calloc( p_demux->i_tracks, sizeof( track_data_mp4_t ) );
237 /* now process each track and extract all usefull informations */
238 for( i = 0; i < p_demux->i_tracks; i++ )
240 MP4_ParseTrack( p_input, &p_demux->track[i], p_trak );
242 if( p_demux->track[i].b_ok )
245 switch( p_demux->track[i].i_cat )
258 msg_Dbg( p_input, "adding track(%d) %s (%s) language %c%c%c",
261 p_demux->track[i].b_enable ? "enable":"disable",
262 p_demux->track[i].i_language[0],
263 p_demux->track[i].i_language[1],
264 p_demux->track[i].i_language[2] );
268 msg_Dbg( p_input, "ignoring track(%d)", i );
271 p_trak = MP4_FindNextBox( p_trak );
274 /* create one program */
275 vlc_mutex_lock( &p_input->stream.stream_lock );
276 if( input_InitStream( p_input, 0 ) == -1)
278 vlc_mutex_unlock( &p_input->stream.stream_lock );
279 msg_Err( p_input, "cannot init stream" );
283 if( input_AddProgram( p_input, 0, 0) == NULL )
285 vlc_mutex_unlock( &p_input->stream.stream_lock );
286 msg_Err( p_input, "cannot add program" );
290 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
291 p_input->stream.i_mux_rate = 0 ; /* FIXME */
292 vlc_mutex_unlock( &p_input->stream.stream_lock );
295 for( i = 0; i < p_demux->i_tracks; i++ )
297 /* start decoder for this track if enable by default*/
298 if( p_demux->track[i].b_enable )
300 MP4_StartDecoder( p_input, &p_demux->track[i] );
304 vlc_mutex_lock( &p_input->stream.stream_lock );
305 p_input->stream.p_selected_program->b_is_ok = 1;
306 vlc_mutex_unlock( &p_input->stream.stream_lock );
312 /*****************************************************************************
313 * MP4Demux: read packet and send them to decoders
314 *****************************************************************************/
315 static int MP4Demux( input_thread_t *p_input )
317 demux_data_mp4_t *p_demux = p_input->p_demux_data;
320 /* first wait for the good time to read a packet */
322 input_ClockManageRef( p_input,
323 p_input->stream.p_selected_program,
327 /* update pcr XXX in mpeg scale so in 90000 unit/s */
328 p_demux->i_pcr = MP4_GetMoviePTS( p_demux ) * 9 / 100;
331 /* we will read 100ms for each stream so ...*/
332 p_demux->i_time += __MAX( p_demux->i_timescale / 10 , 1 );
335 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
337 if( ( !p_demux->track[i_track].b_ok )||
338 ( !p_demux->track[i_track].p_es )||
339 ( !p_demux->track[i_track].p_es->p_decoder_fifo )||
340 ( MP4_GetTrackPTS( &p_demux->track[i_track] ) >=
341 MP4_GetMoviePTS( p_demux ) ) )
343 continue; /* no need to read something */
346 while( MP4_GetTrackPTS( &p_demux->track[i_track] ) <
347 MP4_GetMoviePTS( p_demux ) )
353 if( !MP4_ReadSample( p_input ,
354 &p_demux->track[i_track],
360 /* send it to decoder and update time of this track
361 it also launch a new decoder if needed */
362 MP4_DecodeSample( p_input ,
363 &p_demux->track[i_track],
369 /* now check if all tracks are finished or unhandled*/
371 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
373 if( ( p_demux->track[i_track].b_ok )&&
374 ( p_demux->track[i_track].i_sample < p_demux->track[i_track].i_sample_count )&&
375 ( p_demux->track[i_track].p_es )&&
376 ( p_demux->track[i_track].p_es->p_decoder_fifo ) )
382 return( 0 ); /* EOF */
385 /*****************************************************************************
386 * MP4End: frees unused data
387 *****************************************************************************/
388 static void MP4End( input_thread_t *p_input )
391 if( p ) { free( p ); }
393 demux_data_mp4_t *p_demux = p_input->p_demux_data;
395 msg_Dbg( p_input, "Freeing all memory" );
396 MP4_FreeBox( p_input, &p_demux->box_root );
397 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
401 i_chunk < p_demux->track[i_track].i_chunk_count; i_chunk++ )
403 if( p_demux->track[i_track].chunk )
405 FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_count_dts);
406 FREE(p_demux->track[i_track].chunk[i_chunk].p_sample_delta_dts );
409 if( p_demux->track->p_data_init )
411 input_DeletePacket( p_input->p_method_data,
412 p_demux->track->p_data_init );
414 if( !p_demux->track[i_track].i_sample_size )
416 FREE( p_demux->track[i_track].p_sample_size );
419 FREE( p_demux->track );
424 /****************************************************************************
425 * Local functions, specific to vlc
426 ****************************************************************************/
428 /****************************************************************************
429 * Parse track information and create all needed data to run a track
430 * If it succeed b_ok is set to 1 else to 0
431 ****************************************************************************/
432 static void MP4_ParseTrack( input_thread_t *p_input,
433 track_data_mp4_t *p_demux_track,
438 MP4_Box_t *p_tkhd = MP4_FindBox( p_trak, FOURCC_tkhd );
439 MP4_Box_t *p_tref = MP4_FindBox( p_trak, FOURCC_tref );
440 MP4_Box_t *p_edts = MP4_FindBox( p_trak, FOURCC_edts );
441 MP4_Box_t *p_mdia = MP4_FindBox( p_trak, FOURCC_mdia );
450 /* hint track unsuported */
452 /* by default, track isn't usable */
453 p_demux_track->b_ok = 0;
455 /* by default, we don't known the categorie */
456 p_demux_track->i_cat = UNKNOWN_ES;
458 if( ( !p_tkhd )||( !p_mdia ) )
463 /* do we launch this track by default ? */
464 p_demux_track->b_enable = ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
466 p_demux_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
467 p_demux_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
468 p_demux_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
472 // msg_Warn( p_input, "Unhandled box: edts --> FIXME" );
477 // msg_Warn( p_input, "Unhandled box: tref --> FIXME" );
480 p_mdhd = MP4_FindBox( p_mdia, FOURCC_mdhd );
481 p_hdlr = MP4_FindBox( p_mdia, FOURCC_hdlr );
482 p_minf = MP4_FindBox( p_mdia, FOURCC_minf );
484 if( ( !p_mdhd )||( !p_hdlr )||( !p_minf ) )
489 p_demux_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
491 for( i = 0; i < 3; i++ )
493 p_demux_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
496 switch( p_hdlr->data.p_hdlr->i_handler_type )
499 if( !( p_smhd = MP4_FindBox( p_minf, FOURCC_smhd ) ) )
503 p_demux_track->i_cat = AUDIO_ES;
507 if( !( p_vmhd = MP4_FindBox( p_minf, FOURCC_vmhd ) ) )
511 p_demux_track->i_cat = VIDEO_ES;
519 p_dinf = MP4_FindBox( p_minf, FOURCC_dinf );
521 if( !( p_demux_track->p_stbl = MP4_FindBox( p_minf, FOURCC_stbl ) ) )
526 if( !( p_demux_track->p_stsd = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsd ) ) )
531 /* Create chunk index table */
532 if( !MP4_CreateChunksIndex( p_input,p_demux_track ) )
534 return; /* cannot create chunks index */
537 /* create sample index table needed for reading and seeking */
538 if( !MP4_CreateSamplesIndex( p_input, p_demux_track ) )
540 return; /* cannot create samples index */
543 p_demux_track->b_ok = 1;
548 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
549 static int MP4_CreateChunksIndex( input_thread_t *p_input,
550 track_data_mp4_t *p_demux_track )
552 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
559 if( ( !(p_co64 = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stco ) )&&
560 !(p_co64 = MP4_FindBox( p_demux_track->p_stbl, FOURCC_co64 ) ) )||
561 ( !(p_stsc = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsc ) ) ))
566 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
567 if( !p_demux_track->i_chunk_count )
569 msg_Warn( p_input, "No chunk defined" );
572 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
573 sizeof( chunk_data_mp4_t ) );
575 /* first we read chunk offset */
576 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
578 p_demux_track->chunk[i_chunk].i_offset =
579 p_co64->data.p_co64->i_chunk_offset[i_chunk];
582 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
583 to be used for the sample XXX begin to 1
584 We construct it begining at the end */
585 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
586 i_index = p_stsc->data.p_stsc->i_entry_count;
589 msg_Warn( p_input, "cannot read chunk table or table empty" );
596 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
597 i_chunk < i_last; i_chunk++ )
599 p_demux_track->chunk[i_chunk].i_sample_description_index =
600 p_stsc->data.p_stsc->i_sample_description_index[i_index];
601 p_demux_track->chunk[i_chunk].i_sample_count =
602 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
604 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
607 p_demux_track->chunk[i_chunk].i_sample_first = 0;
608 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
610 p_demux_track->chunk[i_chunk].i_sample_first =
611 p_demux_track->chunk[i_chunk-1].i_sample_first +
612 p_demux_track->chunk[i_chunk-1].i_sample_count;
616 msg_Dbg( p_input, "read %d chunk", p_demux_track->i_chunk_count );
623 static int MP4_CreateSamplesIndex( input_thread_t *p_input,
624 track_data_mp4_t *p_demux_track )
626 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
627 ctts make same mapping but for composition time,
628 not yet used and probably not usefull */
629 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
630 that uses a compressed form FIXME make them in libmp4
632 /* TODO use also stss and stsh table for seeking */
633 /* FIXME use edit table */
638 int i_index_sample_used;
642 p_stts = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stts );
643 p_stsz = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsz ); /* FIXME and stz2 */
646 if( ( !p_stts )||( !p_stsz ) )
648 msg_Warn( p_input, "cannot read sample table" );
652 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
655 /* for sample size, there are 2 case */
656 if( p_stsz->data.p_stsz->i_sample_size )
658 /* 1: all sample have the same size, so no need to construct a table */
659 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
660 p_demux_track->p_sample_size = NULL;
664 /* 2: each sample can have a different size */
665 p_demux_track->i_sample_size = 0;
666 p_demux_track->p_sample_size =
667 calloc( p_demux_track->i_sample_count, sizeof( u32 ) );
669 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
671 p_demux_track->p_sample_size[i_sample] =
672 p_stsz->data.p_stsz->i_entry_size[i_sample];
675 /* we have extract all information from stsz,
678 /* if we don't want to waste too much memory, we can't expand
679 the box !, so each chunk will contain an "extract" of this table
683 i_index = 0; i_index_sample_used =0;
684 /* create and init last data for each chunk */
685 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
688 int i_entry, i_sample_count, i;
690 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
691 /* count how many entries needed for this chunk
692 for p_sample_delta_dts and p_sample_count_dts */
695 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
696 while( i_sample_count > 0 )
698 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
701 i_sample_count += i_index_sample_used; /* don't count already used sample
707 p_demux_track->chunk[i_chunk].p_sample_count_dts =
708 calloc( i_entry, sizeof( u32 ) );
709 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
710 calloc( i_entry, sizeof( u32 ) );
713 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
714 for( i = 0; i < i_entry; i++ )
719 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
721 i_used = __MIN( i_rest, i_sample_count );
723 i_index_sample_used += i_used;
725 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
727 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
728 p_stts->data.p_stts->i_sample_delta[i_index];
730 i_last_dts += i_used *
731 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
733 if( i_index_sample_used >=
734 p_stts->data.p_stts->i_sample_count[i_index] )
737 i_index_sample_used = 0;
743 msg_Dbg( p_input, "read %d samples", p_demux_track->i_sample_count );
748 static void MP4_StartDecoder( input_thread_t *p_input,
749 track_data_mp4_t *p_demux_track )
762 if( (!p_demux_track->b_ok )||( p_demux_track->i_cat == UNKNOWN_ES ) )
767 msg_Dbg( p_input, "Starting decoder (track ID 0x%x)",
768 p_demux_track->i_track_ID );
770 /* launch decoder according in chunk we are */
771 i_chunk = p_demux_track->i_chunk;
773 if( !p_demux_track->chunk[i_chunk].i_sample_description_index )
775 msg_Warn( p_input, "invalid SampleEntry index for this track i_cat %d" );
779 p_sample = MP4_FindNbBox( p_demux_track->p_stsd,
780 p_demux_track->chunk[i_chunk].i_sample_description_index - 1);
784 msg_Warn( p_input, "cannot find SampleEntry for this track" );
789 vlc_mutex_lock( &p_input->stream.stream_lock );
790 p_demux_track->p_es = input_AddES( p_input,
791 p_input->stream.p_selected_program,
792 p_demux_track->i_track_ID,
794 vlc_mutex_unlock( &p_input->stream.stream_lock );
795 /* Initialise ES, first language as description */
796 for( i = 0; i < 3; i++ )
798 p_demux_track->p_es->psz_desc[i] = p_demux_track->i_language[i];
800 p_demux_track->p_es->psz_desc[4] = 0;
802 p_demux_track->p_es->i_stream_id = p_demux_track->i_track_ID;
804 p_demux_track->p_es->i_type = UNKNOWN_ES;
805 p_demux_track->p_es->i_cat = p_demux_track->i_cat;
807 /* search for the codec */
808 if( !MP4_GetCodec( p_sample->i_type, &i_codec, &psz_name ) )
810 msg_Warn( p_input, "%s (%c%c%c%c) unsupported",
812 (p_sample->i_type >> 24)&0xff,
813 (p_sample->i_type >> 16)&0xff,
814 (p_sample->i_type >> 8)&0xff,
815 (p_sample->i_type )&0xff);
816 p_demux_track->b_ok = 0;
821 p_demux_track->p_es->i_type = i_codec;
822 msg_Info( p_input, "%s supported", psz_name );
825 switch( p_demux_track->i_cat )
828 p_demux_track->p_es->b_audio = 0;
830 /* now create a bitmapinfoheader_t for decoder */
831 p_bmih = malloc( 40 );
832 memset( p_bmih, 0, 40);
833 MP4_Set4BytesLE( p_bmih, 40 );
834 if( p_sample->data.p_sample_mp4v->i_width )
836 MP4_Set4BytesLE( p_bmih + 4, p_sample->data.p_sample_mp4v->i_width );
840 /* use display size */
841 MP4_Set4BytesLE( p_bmih + 4, p_demux_track->i_width );
843 if( p_sample->data.p_sample_mp4v->i_height )
845 MP4_Set4BytesLE( p_bmih + 8, p_sample->data.p_sample_mp4v->i_height );
849 MP4_Set4BytesLE( p_bmih + 8, p_demux_track->i_height );
852 p_demux_track->p_es->p_demux_data = p_bmih;
855 p_demux_track->p_es->b_audio = 1;
861 vlc_mutex_lock( &p_input->stream.stream_lock );
862 input_SelectES( p_input, p_demux_track->p_es );
863 vlc_mutex_unlock( &p_input->stream.stream_lock );
866 p_demux_track->b_ok = 1;
868 /* now see if esds is present and i so create a data packet
869 with decoder_specific_info */
870 if( ( p_esds = MP4_FindBox( p_sample, FOURCC_esds ) )&&
871 ( p_esds->data.p_esds->es_descriptor.p_decConfigDescr )&&
872 ( p_esds->data.p_esds->es_descriptor.p_decConfigDescr->i_decoder_specific_info_len ) )
874 data_packet_t *p_data;
875 int i_size = p_esds->data.p_esds->es_descriptor.p_decConfigDescr->i_decoder_specific_info_len;
877 /* data packet for the data */
878 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
883 /* initialisation of all the field */
884 memcpy( p_data->p_payload_start,
885 p_esds->data.p_esds->es_descriptor.p_decConfigDescr->p_decoder_specific_info,
887 p_demux_track->p_data_init = p_data;
895 static void MP4_StopDecoder( input_thread_t *p_input,
896 track_data_mp4_t *p_demux_track )
898 msg_Dbg( p_input, "Stopping decoder (track ID 0x%x)",
899 p_demux_track->i_track_ID );
901 input_UnselectES( p_input, p_demux_track->p_es );
902 p_demux_track->p_es = NULL;
903 if( p_demux_track->p_data_init )
905 input_DeletePacket( p_input->p_method_data,
906 p_demux_track->p_data_init );
907 p_demux_track->p_data_init = NULL;
912 static int MP4_ReadSample( input_thread_t *p_input,
913 track_data_mp4_t *p_demux_track,
914 pes_packet_t **pp_pes )
919 data_packet_t *p_data;
922 /* this track have already reach the end */
923 if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
928 /* caculate size and position for this sample */
929 i_size = p_demux_track->i_sample_size ?
930 p_demux_track->i_sample_size : p_demux_track->p_sample_size[p_demux_track->i_sample];
932 i_pos = MP4_GetTrackPos( p_demux_track );
935 if( ! MP4_SeekAbsolute( p_input, i_pos ) )
940 /* now create a pes */
941 if( !(*pp_pes = input_NewPES( p_input->p_method_data ) ) )
945 /* and a data packet for the data */
946 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
948 input_DeletePES( p_input->p_method_data, *pp_pes );
953 /* initialisation of all the field */
955 (*pp_pes)->i_pts = MP4_GetTrackPTS( p_demux_track );
956 (*pp_pes)->p_first = (*pp_pes)->p_last = p_data;
957 (*pp_pes)->i_nb_data = 1;
958 (*pp_pes)->i_pes_size = i_size;
965 // msg_Dbg( p_input, "will read %d bytes", i_size );
966 if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
968 input_DeletePES( p_input->p_method_data, *pp_pes );
969 input_DeletePacket( p_input->p_method_data, p_data );
977 static int MP4_DecodeSample( input_thread_t *p_input,
978 track_data_mp4_t *p_demux_track,
979 pes_packet_t *p_pes )
987 /* don't forget to convert in mpeg clock */
988 /* FIXME correct ffmpeg to use dts instead of pts that it incorrect
989 and, set it here ( and correct avi demux ) */
991 p_pes->i_pts = input_ClockGetTS( p_input,
992 p_input->stream.p_selected_program,
993 p_pes->i_pts * 9/100);
996 if( p_demux_track->p_data_init )
998 pes_packet_t *p_pes_init;
999 /* create a pes packet containing decoder initialisation
1000 with the one we will send to decoder */
1001 if( !(p_pes_init = input_NewPES( p_input->p_method_data ) ) )
1003 msg_Err( p_input, "out of memory" );
1007 p_pes_init->p_first =
1008 p_pes_init->p_last = p_demux_track->p_data_init;
1010 p_pes_init->i_pes_size = p_demux_track->p_data_init->p_payload_end -
1011 p_demux_track->p_data_init->p_payload_start;
1012 p_pes_init->i_nb_data = 1;
1014 input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes_init );
1015 p_demux_track->p_data_init = NULL;
1018 input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes );
1020 /* now update sample position */
1021 p_demux_track->i_sample++; /* easy ;) */
1022 if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
1024 /* we have reach end of the track so free decoder stuff */
1025 MP4_StopDecoder( p_input, p_demux_track );
1028 /* Have we changed chunk ? */
1029 if( p_demux_track->i_sample >=
1030 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_first +
1031 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_count )
1033 /* we haven't reached the end of the track, so see if we
1034 have to change the decoder for the next frame because
1035 i_sample_description_index have changed */
1037 p_demux_track->i_chunk++;
1038 if( p_demux_track->chunk[p_demux_track->i_chunk-1].i_sample_description_index
1039 != p_demux_track->chunk[p_demux_track->i_chunk].i_sample_description_index )
1042 msg_Err( p_input, "I need to change the decoder but not yet implemented" );