1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.5 2002/07/23 00:39:17 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 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] ) + ( p_peek[5] << 8 ) +
141 ( p_peek[6] << 16 ) + ( p_peek[7] << 24);
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 );
410 /* if( p_demux->track->p_data_init )
412 input_DeletePacket( p_input->p_method_data,
413 p_demux->track->p_data_init );
417 if( !p_demux->track[i_track].i_sample_size )
419 FREE( p_demux->track[i_track].p_sample_size );
422 FREE( p_demux->track );
427 /****************************************************************************
428 * Local functions, specific to vlc
429 ****************************************************************************/
431 /****************************************************************************
432 * Parse track information and create all needed data to run a track
433 * If it succeed b_ok is set to 1 else to 0
434 ****************************************************************************/
435 static void MP4_ParseTrack( input_thread_t *p_input,
436 track_data_mp4_t *p_demux_track,
441 MP4_Box_t *p_tkhd = MP4_FindBox( p_trak, FOURCC_tkhd );
442 MP4_Box_t *p_tref = MP4_FindBox( p_trak, FOURCC_tref );
443 MP4_Box_t *p_edts = MP4_FindBox( p_trak, FOURCC_edts );
444 MP4_Box_t *p_mdia = MP4_FindBox( p_trak, FOURCC_mdia );
453 /* hint track unsuported */
455 /* by default, track isn't usable */
456 p_demux_track->b_ok = 0;
458 /* by default, we don't known the categorie */
459 p_demux_track->i_cat = UNKNOWN_ES;
461 if( ( !p_tkhd )||( !p_mdia ) )
466 /* do we launch this track by default ? */
467 p_demux_track->b_enable = ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
469 p_demux_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
470 p_demux_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
471 p_demux_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
475 // msg_Warn( p_input, "Unhandled box: edts --> FIXME" );
480 // msg_Warn( p_input, "Unhandled box: tref --> FIXME" );
483 p_mdhd = MP4_FindBox( p_mdia, FOURCC_mdhd );
484 p_hdlr = MP4_FindBox( p_mdia, FOURCC_hdlr );
485 p_minf = MP4_FindBox( p_mdia, FOURCC_minf );
487 if( ( !p_mdhd )||( !p_hdlr )||( !p_minf ) )
492 p_demux_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
494 for( i = 0; i < 3; i++ )
496 p_demux_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
499 switch( p_hdlr->data.p_hdlr->i_handler_type )
502 if( !( p_smhd = MP4_FindBox( p_minf, FOURCC_smhd ) ) )
506 p_demux_track->i_cat = AUDIO_ES;
510 if( !( p_vmhd = MP4_FindBox( p_minf, FOURCC_vmhd ) ) )
514 p_demux_track->i_cat = VIDEO_ES;
522 p_dinf = MP4_FindBox( p_minf, FOURCC_dinf );
524 if( !( p_demux_track->p_stbl = MP4_FindBox( p_minf, FOURCC_stbl ) ) )
529 if( !( p_demux_track->p_stsd = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsd ) ) )
534 /* Create chunk index table */
535 if( !MP4_CreateChunksIndex( p_input,p_demux_track ) )
537 return; /* cannot create chunks index */
540 /* create sample index table needed for reading and seeking */
541 if( !MP4_CreateSamplesIndex( p_input, p_demux_track ) )
543 return; /* cannot create samples index */
546 p_demux_track->b_ok = 1;
551 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
552 static int MP4_CreateChunksIndex( input_thread_t *p_input,
553 track_data_mp4_t *p_demux_track )
555 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
562 if( ( !(p_co64 = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stco ) )&&
563 !(p_co64 = MP4_FindBox( p_demux_track->p_stbl, FOURCC_co64 ) ) )||
564 ( !(p_stsc = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsc ) ) ))
569 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
570 if( !p_demux_track->i_chunk_count )
572 msg_Warn( p_input, "No chunk defined" );
575 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
576 sizeof( chunk_data_mp4_t ) );
578 /* first we read chunk offset */
579 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
581 p_demux_track->chunk[i_chunk].i_offset =
582 p_co64->data.p_co64->i_chunk_offset[i_chunk];
585 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
586 to be used for the sample XXX begin to 1
587 We construct it begining at the end */
588 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
589 i_index = p_stsc->data.p_stsc->i_entry_count;
592 msg_Warn( p_input, "cannot read chunk table or table empty" );
599 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
600 i_chunk < i_last; i_chunk++ )
602 p_demux_track->chunk[i_chunk].i_sample_description_index =
603 p_stsc->data.p_stsc->i_sample_description_index[i_index];
604 p_demux_track->chunk[i_chunk].i_sample_count =
605 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
607 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
610 p_demux_track->chunk[i_chunk].i_sample_first = 0;
611 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
613 p_demux_track->chunk[i_chunk].i_sample_first =
614 p_demux_track->chunk[i_chunk-1].i_sample_first +
615 p_demux_track->chunk[i_chunk-1].i_sample_count;
619 msg_Dbg( p_input, "read %d chunk", p_demux_track->i_chunk_count );
626 static int MP4_CreateSamplesIndex( input_thread_t *p_input,
627 track_data_mp4_t *p_demux_track )
629 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
630 ctts make same mapping but for composition time,
631 not yet used and probably not usefull */
632 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
633 that uses a compressed form FIXME make them in libmp4
635 /* TODO use also stss and stsh table for seeking */
636 /* FIXME use edit table */
641 int i_index_sample_used;
645 p_stts = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stts );
646 p_stsz = MP4_FindBox( p_demux_track->p_stbl, FOURCC_stsz ); /* FIXME and stz2 */
649 if( ( !p_stts )||( !p_stsz ) )
651 msg_Warn( p_input, "cannot read sample table" );
655 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
658 /* for sample size, there are 2 case */
659 if( p_stsz->data.p_stsz->i_sample_size )
661 /* 1: all sample have the same size, so no need to construct a table */
662 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
663 p_demux_track->p_sample_size = NULL;
667 /* 2: each sample can have a different size */
668 p_demux_track->i_sample_size = 0;
669 p_demux_track->p_sample_size =
670 calloc( p_demux_track->i_sample_count, sizeof( u32 ) );
672 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
674 p_demux_track->p_sample_size[i_sample] =
675 p_stsz->data.p_stsz->i_entry_size[i_sample];
678 /* we have extract all information from stsz,
681 /* if we don't want to waste too much memory, we can't expand
682 the box !, so each chunk will contain an "extract" of this table
686 i_index = 0; i_index_sample_used =0;
687 /* create and init last data for each chunk */
688 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
691 int i_entry, i_sample_count, i;
693 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
694 /* count how many entries needed for this chunk
695 for p_sample_delta_dts and p_sample_count_dts */
698 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
699 while( i_sample_count > 0 )
701 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
704 i_sample_count += i_index_sample_used; /* don't count already used sample
710 p_demux_track->chunk[i_chunk].p_sample_count_dts =
711 calloc( i_entry, sizeof( u32 ) );
712 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
713 calloc( i_entry, sizeof( u32 ) );
716 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
717 for( i = 0; i < i_entry; i++ )
722 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
724 i_used = __MIN( i_rest, i_sample_count );
726 i_index_sample_used += i_used;
728 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
730 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
731 p_stts->data.p_stts->i_sample_delta[i_index];
733 i_last_dts += i_used *
734 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
736 if( i_index_sample_used >=
737 p_stts->data.p_stts->i_sample_count[i_index] )
740 i_index_sample_used = 0;
746 msg_Dbg( p_input, "read %d samples", p_demux_track->i_sample_count );
751 static void MP4_StartDecoder( input_thread_t *p_input,
752 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_fourcc = p_sample->i_type;
805 p_demux_track->p_es->i_cat = p_demux_track->i_cat;
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_decconfig && p_decconfig->i_decoder_specific_info_len )
813 data_packet_t *p_data;
814 int i_size = p_decconfig->i_decoder_specific_info_len;
816 /* data packet for the data */
817 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
822 memcpy( p_data->p_payload_start,
823 p_decconfig->p_decoder_specific_info,
825 p_demux_track->p_data_init = p_data;
829 /* some last initialisation */
830 switch( p_demux_track->i_cat )
833 /* now create a bitmapinfoheader_t for decoder */
834 p_bmih = malloc( 40 );
835 memset( p_bmih, 0, 40);
836 MP4_Set4BytesLE( p_bmih, 40 );
837 if( p_sample->data.p_sample_mp4v
838 && p_sample->data.p_sample_mp4v->i_width )
840 MP4_Set4BytesLE( p_bmih + 4,
841 p_sample->data.p_sample_mp4v->i_width );
845 /* use display size */
846 MP4_Set4BytesLE( p_bmih + 4, p_demux_track->i_width );
848 if( p_sample->data.p_sample_mp4v
849 && p_sample->data.p_sample_mp4v->i_height )
851 MP4_Set4BytesLE( p_bmih + 8,
852 p_sample->data.p_sample_mp4v->i_height );
856 MP4_Set4BytesLE( p_bmih + 8, p_demux_track->i_height );
859 p_demux_track->p_es->p_demux_data = p_bmih;
868 vlc_mutex_lock( &p_input->stream.stream_lock );
869 input_SelectES( p_input, p_demux_track->p_es );
870 vlc_mutex_unlock( &p_input->stream.stream_lock );
872 p_demux_track->b_ok = 1;
876 static void MP4_StopDecoder( input_thread_t *p_input,
877 track_data_mp4_t *p_demux_track )
879 msg_Dbg( p_input, "Stopping decoder (track ID 0x%x)",
880 p_demux_track->i_track_ID );
882 input_UnselectES( p_input, p_demux_track->p_es );
883 p_demux_track->p_es = NULL;
884 if( p_demux_track->p_data_init )
886 input_DeletePacket( p_input->p_method_data,
887 p_demux_track->p_data_init );
888 p_demux_track->p_data_init = NULL;
893 static int MP4_ReadSample( input_thread_t *p_input,
894 track_data_mp4_t *p_demux_track,
895 pes_packet_t **pp_pes )
900 data_packet_t *p_data;
903 /* this track have already reach the end */
904 if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
909 /* caculate size and position for this sample */
910 i_size = p_demux_track->i_sample_size ?
911 p_demux_track->i_sample_size : p_demux_track->p_sample_size[p_demux_track->i_sample];
913 i_pos = MP4_GetTrackPos( p_demux_track );
916 if( ! MP4_SeekAbsolute( p_input, i_pos ) )
921 /* now create a pes */
922 if( !(*pp_pes = input_NewPES( p_input->p_method_data ) ) )
926 /* and a data packet for the data */
927 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
929 input_DeletePES( p_input->p_method_data, *pp_pes );
934 /* initialisation of all the field */
936 (*pp_pes)->i_pts = MP4_GetTrackPTS( p_demux_track );
937 (*pp_pes)->p_first = (*pp_pes)->p_last = p_data;
938 (*pp_pes)->i_nb_data = 1;
939 (*pp_pes)->i_pes_size = i_size;
946 // msg_Dbg( p_input, "will read %d bytes", i_size );
947 if( !MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
949 input_DeletePES( p_input->p_method_data, *pp_pes );
950 input_DeletePacket( p_input->p_method_data, p_data );
958 static int MP4_DecodeSample( input_thread_t *p_input,
959 track_data_mp4_t *p_demux_track,
960 pes_packet_t *p_pes )
968 /* don't forget to convert in mpeg clock */
969 /* FIXME correct ffmpeg to use dts instead of pts that it incorrect
970 and, set it here ( and correct avi demux ) */
972 p_pes->i_pts = input_ClockGetTS( p_input,
973 p_input->stream.p_selected_program,
974 p_pes->i_pts * 9/100);
977 if( p_demux_track->p_data_init )
979 pes_packet_t *p_pes_init;
980 /* create a pes packet containing decoder initialisation
981 with the one we will send to decoder */
982 if( !(p_pes_init = input_NewPES( p_input->p_method_data ) ) )
984 msg_Err( p_input, "out of memory" );
988 p_pes_init->p_first =
989 p_pes_init->p_last = p_demux_track->p_data_init;
991 p_pes_init->i_pes_size = p_demux_track->p_data_init->p_payload_end -
992 p_demux_track->p_data_init->p_payload_start;
993 p_pes_init->i_nb_data = 1;
995 input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes_init );
996 p_demux_track->p_data_init = NULL;
999 input_DecodePES( p_demux_track->p_es->p_decoder_fifo, p_pes );
1001 /* now update sample position */
1002 p_demux_track->i_sample++; /* easy ;) */
1003 if( p_demux_track->i_sample >= p_demux_track->i_sample_count )
1005 /* we have reach end of the track so free decoder stuff */
1006 MP4_StopDecoder( p_input, p_demux_track );
1009 /* Have we changed chunk ? */
1010 if( p_demux_track->i_sample >=
1011 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_first +
1012 p_demux_track->chunk[p_demux_track->i_chunk].i_sample_count )
1014 /* we haven't reached the end of the track, so see if we
1015 have to change the decoder for the next frame because
1016 i_sample_description_index have changed */
1018 p_demux_track->i_chunk++;
1019 if( p_demux_track->chunk[p_demux_track->i_chunk-1].i_sample_description_index
1020 != p_demux_track->chunk[p_demux_track->i_chunk].i_sample_description_index )
1023 msg_Err( p_input, "I need to change the decoder but not yet implemented" );