1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.31 2003/05/09 19:29:57 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>
33 #include <vlc_playlist.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int MP4Init ( vlc_object_t * );
42 static void __MP4End ( vlc_object_t * );
43 static int MP4Demux ( input_thread_t * );
45 static int MP4DemuxRef( input_thread_t *p_input )
50 /* New input could have something like that... */
51 static int MP4Seek ( input_thread_t *, mtime_t );
53 #define MP4End(a) __MP4End(VLC_OBJECT(a))
55 /*****************************************************************************
57 *****************************************************************************/
59 set_description( _("MP4 demuxer") );
60 set_capability( "demux", 242 );
61 set_callbacks( MP4Init, __MP4End );
64 /*****************************************************************************
65 * Declaration of local function
66 *****************************************************************************/
68 static void MP4_TrackCreate ( input_thread_t *,
71 static void MP4_TrackDestroy( input_thread_t *,
74 static int MP4_TrackSelect ( input_thread_t *,
77 static void MP4_TrackUnselect(input_thread_t *,
80 static int MP4_TrackSeek ( input_thread_t *,
84 static uint64_t MP4_GetTrackPos( track_data_mp4_t * );
85 static int MP4_TrackSampleSize( track_data_mp4_t * );
86 static int MP4_TrackNextSample( input_thread_t *,
89 #define MP4_Set4BytesLE( p, dw ) \
90 *((uint8_t*)p) = ( (dw)&0xff ); \
91 *((uint8_t*)p+1) = ( ((dw)>> 8)&0xff ); \
92 *((uint8_t*)p+2) = ( ((dw)>>16)&0xff ); \
93 *((uint8_t*)p+3) = ( ((dw)>>24)&0xff )
95 #define MP4_Set2BytesLE( p, dw ) \
96 *((uint8_t*)p) = ( (dw)&0xff ); \
97 *((uint8_t*)p+1) = ( ((dw)>> 8)&0xff )
100 if( p ) { free( p ); (p) = NULL;}
102 /*****************************************************************************
103 * MP4Init: check file and initializes MP4 structures
104 *****************************************************************************/
105 static int MP4Init( vlc_object_t * p_this )
107 input_thread_t *p_input = (input_thread_t *)p_this;
110 demux_sys_t *p_demux;
123 if( !p_input->stream.b_seekable )
125 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
126 return( VLC_EGENERIC );
129 /* Initialize access plug-in structures. */
130 if( p_input->i_mtu == 0 )
133 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
136 p_input->pf_demux = MP4Demux;
138 /* a little test to see if it could be a mp4 */
139 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
141 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
142 return( VLC_EGENERIC );
146 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
159 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
160 return( VLC_EGENERIC );
163 /* create our structure that will contains all data */
164 if( !( p_input->p_demux_data =
165 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
167 msg_Err( p_input, "out of memory" );
168 return( VLC_EGENERIC );
170 memset( p_demux, 0, sizeof( demux_sys_t ) );
171 p_input->p_demux_data = p_demux;
174 /* Now load all boxes ( except raw data ) */
175 if( !MP4_BoxGetRoot( p_input, &p_demux->box_root ) )
177 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
178 return( VLC_EGENERIC );
181 MP4_BoxDumpStructure( p_input, &p_demux->box_root );
183 if( ( p_ftyp = MP4_BoxGet( &p_demux->box_root, "/ftyp" ) ) )
185 switch( p_ftyp->data.p_ftyp->i_major_brand )
189 "ISO Media file (isom) version %d.",
190 p_ftyp->data.p_ftyp->i_minor_version );
194 "unrecognized major file specification (%4.4s).",
195 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
201 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
204 /* the file need to have one moov box */
205 if( MP4_BoxCount( &p_demux->box_root, "/moov" ) <= 0 )
207 MP4_Box_t *p_foov = MP4_BoxGet( &p_demux->box_root, "/foov" );
211 msg_Err( p_input, "MP4 plugin discarded (no moov box)" );
213 return( VLC_EGENERIC );
215 /* we have a free box as a moov, rename it */
216 p_foov->i_type = FOURCC_moov;
219 if( ( p_rmra = MP4_BoxGet( &p_demux->box_root, "/moov/rmra" ) ) )
221 playlist_t *p_playlist;
222 int i_count = MP4_BoxCount( p_rmra, "rmda" );
225 msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );
228 (playlist_t *)vlc_object_find( p_input,
233 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
234 for( i = 0; i < i_count; i++ )
236 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
240 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
244 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
246 msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
247 psz_ref, (char*)&i_ref_type );
249 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
251 if( strstr( psz_ref, "qt5gateQT" ) )
253 msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
256 if( !strncmp( psz_ref, "http://", 7 ) ||
257 !strncmp( psz_ref, "rtsp://", 7 ) )
259 msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
260 playlist_Add( p_playlist, psz_ref, PLAYLIST_APPEND, PLAYLIST_END );
264 /* msg dbg relative ? */
265 char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
266 char *end = strrchr( p_input->psz_source, '/' );
270 int i_len = end + 1 - p_input->psz_source;
272 strncpy( psz_absolute, p_input->psz_source, i_len);
273 psz_absolute[i_len] = '\0';
277 strcpy( psz_absolute, "" );
279 strcat( psz_absolute, psz_ref );
280 msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
281 playlist_Add( p_playlist, psz_absolute, PLAYLIST_APPEND, PLAYLIST_END );
286 msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)", (char*)&p_rdrf->data.p_rdrf->i_ref_type );
289 vlc_object_release( p_playlist );
293 msg_Err( p_input, "can't find playlist" );
297 if( !(p_mvhd = MP4_BoxGet( &p_demux->box_root, "/moov/mvhd" ) ) )
301 msg_Err( p_input, "cannot find /moov/mvhd" );
307 msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
308 p_input->pf_demux = MP4DemuxRef;
314 p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
315 p_demux->i_duration = p_mvhd->data.p_mvhd->i_duration;
318 if( !( p_demux->i_tracks =
319 MP4_BoxCount( &p_demux->box_root, "/moov/trak" ) ) )
321 msg_Err( p_input, "cannot find any /moov/trak" );
323 return( VLC_EGENERIC );
325 msg_Dbg( p_input, "find %d track%c",
327 p_demux->i_tracks ? 's':' ' );
329 /* create one program */
330 vlc_mutex_lock( &p_input->stream.stream_lock );
331 if( input_InitStream( p_input, 0 ) == -1)
333 vlc_mutex_unlock( &p_input->stream.stream_lock );
334 msg_Err( p_input, "cannot init stream" );
336 return( VLC_EGENERIC );
338 /* Needed to create program _before_ MP4_TrackCreate */
339 if( input_AddProgram( p_input, 0, 0) == NULL )
341 vlc_mutex_unlock( &p_input->stream.stream_lock );
342 msg_Err( p_input, "cannot add program" );
344 return( VLC_EGENERIC );
346 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
347 /* XXX beurk and beurk, see MP4Demux and MP4Seek */
348 if( p_demux->i_duration/p_demux->i_timescale > 0 )
350 p_input->stream.i_mux_rate =
351 p_input->stream.p_selected_area->i_size / 50 /
352 ( p_demux->i_duration / p_demux->i_timescale );
356 p_input->stream.i_mux_rate = 0;
358 vlc_mutex_unlock( &p_input->stream.stream_lock );
361 /* allocate memory */
362 p_demux->track = calloc( p_demux->i_tracks, sizeof( track_data_mp4_t ) );
364 /* now process each track and extract all usefull informations */
365 for( i = 0; i < p_demux->i_tracks; i++ )
367 p_trak = MP4_BoxGet( &p_demux->box_root, "/moov/trak[%d]", i );
368 MP4_TrackCreate( p_input, &p_demux->track[i], p_trak );
370 if( p_demux->track[i].b_ok )
373 switch( p_demux->track[i].i_cat )
386 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %c%c%c",
387 p_demux->track[i].i_track_ID,
389 p_demux->track[i].b_enable ? "enable":"disable",
390 p_demux->track[i].i_language[0],
391 p_demux->track[i].i_language[1],
392 p_demux->track[i].i_language[2] );
396 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_demux->track[i].i_track_ID );
401 for( i = 0, b_audio = VLC_FALSE; i < p_demux->i_tracks; i++ )
403 #define track p_demux->track[i]
404 /* start decoder for this track if enable by default*/
405 if( track.b_ok && track.b_enable &&
406 ( track.i_cat != AUDIO_ES || !b_audio ) )
408 if( !MP4_TrackSelect( p_input, &track, 0 ) )
410 if(track.i_cat == AUDIO_ES )
419 vlc_mutex_lock( &p_input->stream.stream_lock );
420 p_input->stream.p_selected_program->b_is_ok = 1;
421 vlc_mutex_unlock( &p_input->stream.stream_lock );
423 return( VLC_SUCCESS );
426 /*****************************************************************************
427 * MP4Demux: read packet and send them to decoders
428 *****************************************************************************
429 * TODO check for newly selected track (ie audio upt to now )
430 *****************************************************************************/
431 static int MP4Demux( input_thread_t *p_input )
433 demux_sys_t *p_demux = p_input->p_demux_data;
434 unsigned int i_track;
437 unsigned int i_track_selected;
439 vlc_bool_t b_play_audio;
441 /* check for newly selected/unselected track */
442 for( i_track = 0, i_track_selected = 0, b_video = VLC_FALSE;
443 i_track < p_demux->i_tracks; i_track++ )
445 #define track p_demux->track[i_track]
446 if( track.b_selected && track.i_sample >= track.i_sample_count )
448 msg_Warn( p_input, "track[0x%x] will be disabled", track.i_track_ID );
449 MP4_TrackUnselect( p_input, &track );
451 else if( track.b_ok )
453 if( track.b_selected && track.p_es->p_decoder_fifo == NULL )
455 MP4_TrackUnselect( p_input, &track );
457 else if( !track.b_selected && track.p_es->p_decoder_fifo != NULL )
459 MP4_TrackSelect( p_input, &track, MP4_GetMoviePTS( p_demux ) );
462 if( track.b_selected )
466 if( track.i_cat == VIDEO_ES )
475 if( i_track_selected <= 0 )
477 msg_Warn( p_input, "no track selected, exiting..." );
482 /* XXX beurk, beuRK and BEURK,
483 but only way I've found to detect seek from interface */
484 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
488 /* first wait for empty buffer, arbitrary time FIXME */
489 msleep( DEFAULT_PTS_DELAY );
490 /* *** calculate new date *** */
492 i_date = (mtime_t)1000000 *
493 (mtime_t)p_demux->i_duration /
494 (mtime_t)p_demux->i_timescale *
495 (mtime_t)MP4_TellAbsolute( p_input ) /
496 (mtime_t)p_input->stream.p_selected_area->i_size;
497 MP4Seek( p_input, i_date );
500 /* first wait for the good time to read a packet */
501 input_ClockManageRef( p_input,
502 p_input->stream.p_selected_program,
506 /* update pcr XXX in mpeg scale so in 90000 unit/s */
507 p_demux->i_pcr = MP4_GetMoviePTS( p_demux ) * 9 / 100;
510 /* we will read 100ms for each stream so ...*/
511 p_demux->i_time += __MAX( p_demux->i_timescale / 10 , 1 );
514 /* *** send audio data to decoder if rate == DEFAULT_RATE or no video *** */
515 vlc_mutex_lock( &p_input->stream.stream_lock );
516 if( p_input->stream.control.i_rate == DEFAULT_RATE || !b_video )
518 b_play_audio = VLC_TRUE;
522 b_play_audio = VLC_FALSE;
524 vlc_mutex_unlock( &p_input->stream.stream_lock );
526 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
528 #define track p_demux->track[i_track]
531 MP4_GetTrackPTS( &track ) >= MP4_GetMoviePTS( p_demux ) )
535 while( MP4_GetTrackPTS( &track ) < MP4_GetMoviePTS( p_demux ) )
538 if( !b_play_audio && track.i_cat == AUDIO_ES )
540 if( MP4_TrackNextSample( p_input, &track ) )
550 data_packet_t *p_data;
553 /* caculate size and position for this sample */
554 i_size = MP4_TrackSampleSize( &track );
556 i_pos = MP4_GetTrackPos( &track );
558 //msg_Dbg( p_input, "stream %d size=%6d pos=%8lld", i_track, i_size, i_pos );
561 if( MP4_SeekAbsolute( p_input, i_pos ) )
563 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
564 MP4_TrackUnselect( p_input, &track );
569 /* now create a pes */
570 if( !(p_pes = input_NewPES( p_input->p_method_data ) ) )
574 /* and a data packet for the data */
575 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
577 input_DeletePES( p_input->p_method_data, p_pes );
580 p_data->p_payload_end = p_data->p_payload_start + i_size;
582 /* initialisation of all the field */
583 p_pes->i_dts = p_pes->i_pts = 0;
584 p_pes->p_first = p_pes->p_last = p_data;
585 p_pes->i_nb_data = 1;
586 p_pes->i_pes_size = i_size;
589 if( MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
591 input_DeletePES( p_input->p_method_data, p_pes );
593 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
594 MP4_TrackUnselect( p_input, &track );
600 p_pes->i_pts = input_ClockGetTS( p_input,
601 p_input->stream.p_selected_program,
602 MP4_GetTrackPTS( &track ) * 9/100);
604 if( track.p_es->p_decoder_fifo )
606 input_DecodePES( track.p_es->p_decoder_fifo, p_pes );
610 input_DeletePES( p_input->p_method_data, p_pes );
613 if( MP4_TrackNextSample( p_input, &track ) )
624 /*****************************************************************************
625 * MP4Seek: Got to i_date
626 ******************************************************************************/
627 static int MP4Seek ( input_thread_t *p_input, mtime_t i_date )
629 demux_sys_t *p_demux = p_input->p_demux_data;
630 unsigned int i_track;
631 /* First update update global time */
632 p_demux->i_time = i_date * p_demux->i_timescale / 1000000;
633 p_demux->i_pcr = i_date* 9 / 100;
635 /* Now for each stream try to go to this time */
636 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
638 #define track p_demux->track[i_track]
639 if( track.b_ok && track.b_selected )
641 MP4_TrackSeek( p_input, &track, i_date );
648 /*****************************************************************************
649 * MP4End: frees unused data
650 *****************************************************************************/
651 static void __MP4End ( vlc_object_t * p_this )
653 unsigned int i_track;
654 input_thread_t * p_input = (input_thread_t *)p_this;
655 demux_sys_t *p_demux = p_input->p_demux_data;
657 msg_Dbg( p_input, "freeing all memory" );
658 MP4_BoxFree( p_input, &p_demux->box_root );
659 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
661 MP4_TrackDestroy( p_input, &p_demux->track[i_track] );
663 FREE( p_demux->track );
665 FREE( p_input->p_demux_data );
669 /****************************************************************************
670 * Local functions, specific to vlc
671 ****************************************************************************/
673 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
674 static int TrackCreateChunksIndex( input_thread_t *p_input,
675 track_data_mp4_t *p_demux_track )
677 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
680 unsigned int i_chunk;
681 unsigned int i_index, i_last;
683 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
684 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
685 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
687 return( VLC_EGENERIC );
690 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
691 if( !p_demux_track->i_chunk_count )
693 msg_Warn( p_input, "no chunk defined" );
694 return( VLC_EGENERIC );
696 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
697 sizeof( chunk_data_mp4_t ) );
699 /* first we read chunk offset */
700 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
702 p_demux_track->chunk[i_chunk].i_offset =
703 p_co64->data.p_co64->i_chunk_offset[i_chunk];
706 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
707 to be used for the sample XXX begin to 1
708 We construct it begining at the end */
709 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
710 i_index = p_stsc->data.p_stsc->i_entry_count;
713 msg_Warn( p_input, "cannot read chunk table or table empty" );
714 return( VLC_EGENERIC );
720 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
721 i_chunk < i_last; i_chunk++ )
723 p_demux_track->chunk[i_chunk].i_sample_description_index =
724 p_stsc->data.p_stsc->i_sample_description_index[i_index];
725 p_demux_track->chunk[i_chunk].i_sample_count =
726 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
728 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
731 p_demux_track->chunk[0].i_sample_first = 0;
732 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
734 p_demux_track->chunk[i_chunk].i_sample_first =
735 p_demux_track->chunk[i_chunk-1].i_sample_first +
736 p_demux_track->chunk[i_chunk-1].i_sample_count;
740 "track[Id 0x%x] read %d chunk",
741 p_demux_track->i_track_ID,
742 p_demux_track->i_chunk_count );
744 return( VLC_SUCCESS );
746 static int TrackCreateSamplesIndex( input_thread_t *p_input,
747 track_data_mp4_t *p_demux_track )
749 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
750 ctts make same mapping but for composition time,
751 not yet used and probably not usefull */
752 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
753 that uses a compressed form FIXME make them in libmp4
755 /* TODO use also stss and stsh table for seeking */
756 /* FIXME use edit table */
761 int64_t i_index_sample_used;
765 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
766 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
768 if( ( !p_stts )||( !p_stsz ) )
770 msg_Warn( p_input, "cannot read sample table" );
771 return( VLC_EGENERIC );
774 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
777 /* for sample size, there are 2 case */
778 if( p_stsz->data.p_stsz->i_sample_size )
780 /* 1: all sample have the same size, so no need to construct a table */
781 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
782 p_demux_track->p_sample_size = NULL;
786 /* 2: each sample can have a different size */
787 p_demux_track->i_sample_size = 0;
788 p_demux_track->p_sample_size =
789 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
791 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
793 p_demux_track->p_sample_size[i_sample] =
794 p_stsz->data.p_stsz->i_entry_size[i_sample];
797 /* we have extract all information from stsz,
800 /* if we don't want to waste too much memory, we can't expand
801 the box !, so each chunk will contain an "extract" of this table
805 i_index = 0; i_index_sample_used =0;
807 /* create and init last data for each chunk */
808 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
811 int64_t i_entry, i_sample_count, i;
813 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
814 /* count how many entries needed for this chunk
815 for p_sample_delta_dts and p_sample_count_dts */
817 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
820 while( i_sample_count > 0 )
822 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
825 i_sample_count += i_index_sample_used; /* don't count already used sample
832 p_demux_track->chunk[i_chunk].p_sample_count_dts =
833 calloc( i_entry, sizeof( uint32_t ) );
834 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
835 calloc( i_entry, sizeof( uint32_t ) );
838 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
839 for( i = 0; i < i_entry; i++ )
844 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
846 i_used = __MIN( i_rest, i_sample_count );
848 i_index_sample_used += i_used;
849 i_sample_count -= i_used;
851 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
853 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
854 p_stts->data.p_stts->i_sample_delta[i_index];
856 i_last_dts += i_used *
857 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
859 if( i_index_sample_used >=
860 p_stts->data.p_stts->i_sample_count[i_index] )
864 i_index_sample_used = 0;
871 "track[Id 0x%x] read %d samples length:"I64Fd"s",
872 p_demux_track->i_track_ID,
873 p_demux_track->i_sample_count,
874 i_last_dts / p_demux_track->i_timescale );
876 return( VLC_SUCCESS );
881 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
883 static int TrackCreateES ( input_thread_t *p_input,
884 track_data_mp4_t *p_track,
885 unsigned int i_chunk,
886 es_descriptor_t **pp_es,
887 pes_packet_t **pp_pes )
889 MP4_Box_t * p_sample;
893 unsigned int i_decoder_specific_info_len;
894 uint8_t * p_decoder_specific_info;
896 es_descriptor_t *p_es;
897 pes_packet_t *p_pes_init;
900 BITMAPINFOHEADER *p_bih;
905 if( !p_track->chunk[i_chunk].i_sample_description_index )
908 "invalid SampleEntry index (track[Id 0x%x])",
909 p_track->i_track_ID );
910 return( VLC_EGENERIC );
913 p_sample = MP4_BoxGet( p_track->p_stsd,
915 p_track->chunk[i_chunk].i_sample_description_index - 1 );
917 if( !p_sample || !p_sample->data.p_data )
920 "cannot find SampleEntry (track[Id 0x%x])",
921 p_track->i_track_ID );
922 return( VLC_EGENERIC );
925 p_track->p_sample = p_sample;
927 if( p_track->i_sample_size == 1 )
929 MP4_Box_data_sample_soun_t *p_soun;
931 p_soun = p_sample->data.p_sample_soun;
933 if( p_soun->i_qt_version == 0 )
935 switch( p_sample->i_type )
937 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
938 p_soun->i_qt_version = 1;
939 p_soun->i_sample_per_packet = 64;
940 p_soun->i_bytes_per_packet = 34;
941 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
942 p_soun->i_bytes_per_sample = 2;
944 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
945 p_soun->i_qt_version = 1;
946 p_soun->i_sample_per_packet = 6;
947 p_soun->i_bytes_per_packet = 2;
948 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
949 p_soun->i_bytes_per_sample = 2;
951 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
952 p_soun->i_qt_version = 1;
953 p_soun->i_sample_per_packet = 12;
954 p_soun->i_bytes_per_packet = 2;
955 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
956 p_soun->i_bytes_per_sample = 2;
964 /* Initialise ES, first language as description */
965 for( i = 0; i < 3; i++ )
967 psz_lang[i] = p_track->i_language[i];
971 vlc_mutex_lock( &p_input->stream.stream_lock );
972 p_es = input_AddES( p_input, p_input->stream.p_selected_program,
973 p_track->i_track_ID, p_track->i_cat, psz_lang, 0 );
974 vlc_mutex_unlock( &p_input->stream.stream_lock );
976 p_es->i_stream_id = p_track->i_track_ID;
978 /* It's a little ugly but .. there are special cases */
979 switch( p_sample->i_type )
981 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
982 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
983 p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
985 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
986 p_es->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
988 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
989 p_es->i_fourcc = VLC_FOURCC( 'h', '2', '6', '3' );
992 p_es->i_fourcc = p_sample->i_type;
996 i_decoder_specific_info_len = 0;
997 p_decoder_specific_info = NULL;
1000 /* now see if esds is present and if so create a data packet
1001 with decoder_specific_info */
1002 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1003 if( ( p_esds = MP4_BoxGet( p_sample, "esds" ) )&&
1004 ( p_esds->data.p_esds )&&
1007 /* First update information based on i_objectTypeIndication */
1008 switch( p_decconfig->i_objectTypeIndication )
1010 case( 0x20 ): /* MPEG4 VIDEO */
1011 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
1014 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1021 case( 0x65): /* MPEG2 video */
1022 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1024 /* Theses are MPEG2-AAC */
1025 case( 0x66): /* main profile */
1026 case( 0x67): /* Low complexity profile */
1027 case( 0x68): /* Scaleable Sampling rate profile */
1028 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1030 /* true MPEG 2 audio */
1032 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1034 case( 0x6a): /* MPEG1 video */
1035 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1037 case( 0x6b): /* MPEG1 audio */
1038 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1040 case( 0x6c ): /* jpeg */
1041 p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
1044 /* Unknown entry, but don't touch i_fourcc */
1046 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1047 p_decconfig->i_objectTypeIndication,
1048 p_track->i_track_ID );
1051 i_decoder_specific_info_len =
1052 p_decconfig->i_decoder_specific_info_len;
1053 p_decoder_specific_info =
1054 p_decconfig->p_decoder_specific_info;
1058 switch( p_sample->i_type )
1060 /* qt decoder, send the complete chunk */
1061 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1062 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1063 i_decoder_specific_info_len = p_sample->data.p_sample_vide->i_qt_image_description;
1064 p_decoder_specific_info = p_sample->data.p_sample_vide->p_qt_image_description;
1066 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1067 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1068 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1069 i_decoder_specific_info_len = p_sample->data.p_sample_soun->i_qt_description;
1070 p_decoder_specific_info = p_sample->data.p_sample_soun->p_qt_description;
1079 /* some last initialisation */
1080 /* XXX I create a bitmapinfoheader_t or
1081 waveformatex_t for each stream, up to now it's the best thing
1082 I've found but it could exist a better solution :) as something
1083 like adding some new fields in p_es ...
1085 XXX I don't set all values, only thoses that are interesting or known
1086 --> bitmapinfoheader_t : width and height
1087 --> waveformatex_t : channels, samplerate, bitspersample
1088 and at the end I add p_decoder_specific_info
1090 TODO set more values
1094 switch( p_track->i_cat )
1097 /* now create a bitmapinfoheader_t for decoder and
1098 add information found in p_esds */
1099 /* XXX XXX + 16 are for avoid segfault when ffmpeg access beyong the data */
1100 p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len + 16 );
1101 p_bih = (BITMAPINFOHEADER*)p_init;
1103 p_bih->biSize = sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len;
1104 p_bih->biWidth = p_sample->data.p_sample_vide->i_width;
1105 p_bih->biHeight = p_sample->data.p_sample_vide->i_height;
1106 p_bih->biPlanes = 1; // FIXME
1107 p_bih->biBitCount = 0; // FIXME
1108 p_bih->biCompression = 0; // FIXME
1109 p_bih->biSizeImage = 0; // FIXME
1110 p_bih->biXPelsPerMeter = 0; // FIXME
1111 p_bih->biYPelsPerMeter = 0; // FIXME
1112 p_bih->biClrUsed = 0; // FIXME
1113 p_bih->biClrImportant = 0; // FIXME
1115 if( p_bih->biWidth == 0 )
1117 // fall on display size
1118 p_bih->biWidth = p_track->i_width;
1120 if( p_bih->biHeight == 0 )
1122 // fall on display size
1123 p_bih->biHeight = p_track->i_height;
1126 if( i_decoder_specific_info_len )
1128 data_packet_t *p_data;
1130 memcpy( p_init + sizeof( BITMAPINFOHEADER ),
1131 p_decoder_specific_info,
1132 i_decoder_specific_info_len);
1134 /* If stream is mpeg4 video we send specific_info,
1135 as it's needed to decode it (vol) */
1136 switch( p_es->i_fourcc )
1138 case VLC_FOURCC( 'm','p','4','v' ):
1139 case VLC_FOURCC( 'D','I','V','X' ):
1140 case VLC_FOURCC( 'd','i','v','x' ):
1141 p_pes_init = input_NewPES( p_input->p_method_data );
1142 p_data = input_NewPacket( p_input->p_method_data,
1143 i_decoder_specific_info_len);
1144 p_data->p_payload_end = p_data->p_payload_start + i_decoder_specific_info_len;
1146 memcpy( p_data->p_payload_start,
1147 p_decoder_specific_info,
1148 i_decoder_specific_info_len );
1149 p_pes_init->i_dts = p_pes_init->i_pts = 0;
1150 p_pes_init->p_first = p_pes_init->p_last = p_data;
1151 p_pes_init->i_nb_data = 1;
1152 p_pes_init->i_pes_size = i_decoder_specific_info_len;
1162 p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len + 16 );
1163 p_wf = (WAVEFORMATEX*)p_init;
1165 p_wf->wFormatTag = 1;
1166 p_wf->nChannels = p_sample->data.p_sample_soun->i_channelcount;
1167 p_wf->nSamplesPerSec = p_sample->data.p_sample_soun->i_sampleratehi;
1168 p_wf->nAvgBytesPerSec = p_sample->data.p_sample_soun->i_channelcount *
1169 p_sample->data.p_sample_soun->i_sampleratehi *
1170 p_sample->data.p_sample_soun->i_samplesize / 8;
1171 p_wf->nBlockAlign = 0;
1172 p_wf->wBitsPerSample = p_sample->data.p_sample_soun->i_samplesize;
1173 p_wf->cbSize = i_decoder_specific_info_len;
1175 if( i_decoder_specific_info_len )
1177 memcpy( p_init + sizeof( WAVEFORMATEX ),
1178 p_decoder_specific_info,
1179 i_decoder_specific_info_len);
1188 if( p_es->i_cat == AUDIO_ES )
1190 p_es->p_bitmapinfoheader = NULL;
1191 p_es->p_waveformatex = (void*)p_init;
1193 else if( p_es->i_cat == VIDEO_ES )
1195 p_es->p_bitmapinfoheader = (void*)p_init;
1196 p_es->p_waveformatex = NULL;
1200 *pp_pes = p_pes_init;
1201 return( VLC_SUCCESS );
1204 /* given a time it return sample/chunk */
1205 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1206 track_data_mp4_t *p_track,
1209 uint32_t *pi_sample )
1213 unsigned int i_sample;
1214 unsigned int i_chunk;
1217 /* convert absolute time to in timescale unit */
1218 i_start = i_start * (mtime_t)p_track->i_timescale / (mtime_t)1000000;
1220 /* FIXME see if it's needed to check p_track->i_chunk_count */
1221 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1223 return( VLC_EGENERIC );
1226 /* we start from sample 0/chunk 0, hope it won't take too much time */
1227 /* *** find good chunk *** */
1228 for( i_chunk = 0; ; i_chunk++ )
1230 if( i_chunk + 1 >= p_track->i_chunk_count )
1232 /* at the end and can't check if i_start in this chunk,
1233 it will be check while searching i_sample */
1234 i_chunk = p_track->i_chunk_count - 1;
1238 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1239 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1245 /* *** find sample in the chunk *** */
1246 i_sample = p_track->chunk[i_chunk].i_sample_first;
1247 i_dts = p_track->chunk[i_chunk].i_first_dts;
1248 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1251 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1252 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1255 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1256 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1258 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1263 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1267 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1272 if( i_sample >= p_track->i_sample_count )
1275 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1276 p_track->i_track_ID, i_chunk, i_sample );
1277 return( VLC_EGENERIC );
1281 /* *** Try to find nearest sync points *** */
1282 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1284 unsigned int i_index;
1286 "track[Id 0x%x] using Sync Sample Box (stss)",
1287 p_track->i_track_ID );
1288 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1290 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1294 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1296 p_stss->data.p_stss->i_sample_number[i_index-1] );
1297 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1298 /* new i_sample is less than old so i_chunk can only decreased */
1299 while( i_chunk > 0 &&
1300 i_sample < p_track->chunk[i_chunk].i_sample_first )
1307 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1309 p_stss->data.p_stss->i_sample_number[i_index] );
1310 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1311 /* new i_sample is more than old so i_chunk can only increased */
1312 while( i_chunk < p_track->i_chunk_count - 1 &&
1313 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1314 p_track->chunk[i_chunk].i_sample_count )
1326 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1327 p_track->i_track_ID );
1330 if( pi_chunk ) *pi_chunk = i_chunk;
1331 if( pi_sample ) *pi_sample = i_sample;
1332 return( VLC_SUCCESS );
1335 static int TrackGotoChunkSample( input_thread_t *p_input,
1336 track_data_mp4_t *p_track,
1337 unsigned int i_chunk,
1338 unsigned int i_sample )
1340 /* now see if actual es is ok */
1341 if( p_track->i_chunk < 0 ||
1342 p_track->i_chunk >= p_track->i_chunk_count ||
1343 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1344 p_track->chunk[i_chunk].i_sample_description_index )
1346 msg_Warn( p_input, "Recreate ES" );
1348 /* no :( recreate es */
1349 vlc_mutex_lock( &p_input->stream.stream_lock );
1350 input_DelES( p_input, p_track->p_es );
1351 vlc_mutex_unlock( &p_input->stream.stream_lock );
1353 if( p_track->p_pes_init )
1355 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1358 if( TrackCreateES( p_input,
1361 &p_track->p_pes_init ) )
1363 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1364 p_track->i_track_ID );
1366 p_track->b_ok = VLC_FALSE;
1367 p_track->b_selected = VLC_FALSE;
1368 return( VLC_EGENERIC );
1372 /* select again the new decoder */
1373 if( p_track->b_selected && p_track->p_es && p_track->p_es->p_decoder_fifo == NULL )
1375 vlc_mutex_lock( &p_input->stream.stream_lock );
1376 input_SelectES( p_input, p_track->p_es );
1377 vlc_mutex_unlock( &p_input->stream.stream_lock );
1379 if( p_track->p_es->p_decoder_fifo )
1381 if( p_track->p_pes_init != NULL )
1383 input_DecodePES( p_track->p_es->p_decoder_fifo, p_track->p_pes_init );
1384 p_track->p_pes_init = NULL;
1386 p_track->b_selected = VLC_TRUE;
1390 msg_Dbg( p_input, "Argg cannot select this stream" );
1391 if( p_track->p_pes_init != NULL )
1393 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1394 p_track->p_pes_init = NULL;
1396 p_track->b_selected = VLC_FALSE;
1400 p_track->i_chunk = i_chunk;
1401 p_track->i_sample = i_sample;
1403 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1406 /****************************************************************************
1408 ****************************************************************************
1409 * Parse track information and create all needed data to run a track
1410 * If it succeed b_ok is set to 1 else to 0
1411 ****************************************************************************/
1412 static void MP4_TrackCreate( input_thread_t *p_input,
1413 track_data_mp4_t *p_track,
1414 MP4_Box_t * p_box_trak )
1418 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1419 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1428 /* hint track unsuported */
1430 /* set default value (-> track unusable) */
1431 p_track->b_ok = VLC_FALSE;
1432 p_track->b_enable = VLC_FALSE;
1433 p_track->b_selected = VLC_FALSE;
1435 p_track->i_cat = UNKNOWN_ES;
1442 /* do we launch this track by default ? */
1444 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1446 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1447 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1448 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1450 if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1452 /* msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
1457 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1460 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1461 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1463 if( ( !p_mdhd )||( !p_hdlr ) )
1468 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1470 for( i = 0; i < 3; i++ )
1472 p_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
1474 p_mdhd->data.p_mdhd->i_language[3] = 0;
1476 switch( p_hdlr->data.p_hdlr->i_handler_type )
1478 case( FOURCC_soun ):
1479 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1483 p_track->i_cat = AUDIO_ES;
1486 case( FOURCC_vide ):
1487 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1491 p_track->i_cat = VIDEO_ES;
1499 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1501 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) )
1506 if( !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1511 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1512 if( p_track->i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1514 MP4_Box_t *p_sample;
1516 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1517 if( p_sample && p_sample->data.p_sample_soun)
1519 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1520 if( p_soun->i_qt_version == 0 && p_track->i_timescale != p_soun->i_sampleratehi )
1522 msg_Warn( p_input, "i_timescale != i_sampleratehi with qt_version == 0\nMaking both equal ? (report any problem)" );
1523 p_track->i_timescale = p_soun->i_sampleratehi;
1529 /* Create chunk index table */
1530 if( TrackCreateChunksIndex( p_input,p_track ) )
1532 return; /* cannot create chunks index */
1535 /* create sample index table needed for reading and seeking */
1536 if( TrackCreateSamplesIndex( p_input, p_track ) )
1538 return; /* cannot create samples index */
1541 p_track->i_chunk = 0;
1542 p_track->i_sample = 0;
1543 /* now create es but does not select it */
1544 /* XXX needed else vlc won't know this track exist */
1545 if( TrackCreateES( p_input,
1546 p_track, p_track->i_chunk,
1548 &p_track->p_pes_init ) )
1550 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1551 p_track->i_track_ID );
1558 for( i = 0; i < p_track->i_chunk_count; i++ )
1560 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1565 p_track->b_ok = VLC_TRUE;
1568 /****************************************************************************
1570 ****************************************************************************
1571 * Destroy a track created by MP4_TrackCreate.
1572 ****************************************************************************/
1573 static void MP4_TrackDestroy( input_thread_t *p_input,
1574 track_data_mp4_t *p_track )
1576 unsigned int i_chunk;
1578 p_track->b_ok = VLC_FALSE;
1579 p_track->b_enable = VLC_FALSE;
1580 p_track->b_selected = VLC_FALSE;
1582 p_track->i_cat = UNKNOWN_ES;
1584 if( p_track->p_pes_init )
1586 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1589 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1591 if( p_track->chunk )
1593 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1594 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1597 FREE( p_track->chunk );
1599 if( !p_track->i_sample_size )
1601 FREE( p_track->p_sample_size );
1605 static int MP4_TrackSelect ( input_thread_t *p_input,
1606 track_data_mp4_t *p_track,
1612 if( !p_track->b_ok )
1614 return( VLC_EGENERIC );
1617 if( p_track->b_selected )
1620 "track[Id 0x%x] already selected",
1621 p_track->i_track_ID );
1622 return( VLC_SUCCESS );
1625 if( TrackTimeToSampleChunk( p_input,
1627 &i_chunk, &i_sample ) )
1630 "cannot select track[Id 0x%x]",
1631 p_track->i_track_ID );
1632 return( VLC_EGENERIC );
1635 p_track->b_selected = VLC_TRUE;
1637 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1639 p_track->b_selected = VLC_FALSE;
1642 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1645 static void MP4_TrackUnselect(input_thread_t *p_input,
1646 track_data_mp4_t *p_track )
1648 if( !p_track->b_ok )
1653 if( !p_track->b_selected )
1656 "track[Id 0x%x] already unselected",
1657 p_track->i_track_ID );
1661 if( p_track->p_es->p_decoder_fifo )
1663 vlc_mutex_lock( &p_input->stream.stream_lock );
1664 input_UnselectES( p_input, p_track->p_es );
1665 vlc_mutex_unlock( &p_input->stream.stream_lock );
1668 p_track->b_selected = VLC_FALSE;
1671 static int MP4_TrackSeek ( input_thread_t *p_input,
1672 track_data_mp4_t *p_track,
1678 if( !p_track->b_ok )
1680 return( VLC_EGENERIC );
1683 if( TrackTimeToSampleChunk( p_input,
1685 &i_chunk, &i_sample ) )
1688 "cannot select track[Id 0x%x]",
1689 p_track->i_track_ID );
1690 return( VLC_EGENERIC );
1693 p_track->b_selected = VLC_TRUE;
1695 TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample );
1697 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1705 * 3 types: for audio
1708 #define QT_V0_MAX_SAMPLES 1500
1709 static int MP4_TrackSampleSize( track_data_mp4_t *p_track )
1712 MP4_Box_data_sample_soun_t *p_soun;
1714 if( p_track->i_sample_size == 0 )
1716 /* most simple case */
1717 return( p_track->p_sample_size[p_track->i_sample] );
1719 if( p_track->i_cat != AUDIO_ES )
1721 return( p_track->i_sample_size );
1724 if( p_track->i_sample_size != 1 )
1726 //msg_Warn( p_input, "SampleSize != 1" );
1727 return( p_track->i_sample_size );
1730 p_soun = p_track->p_sample->data.p_sample_soun;
1732 if( p_soun->i_qt_version == 1 )
1734 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1739 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1740 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1741 if( i_samples > QT_V0_MAX_SAMPLES )
1743 i_samples = QT_V0_MAX_SAMPLES;
1745 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1748 //fprintf( stderr, "size=%d\n", i_size );
1753 static uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
1755 unsigned int i_sample;
1759 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1761 if( p_track->i_sample_size )
1763 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1765 if( p_soun->i_qt_version == 0 )
1767 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1768 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1772 /* we read chunk by chunk */
1778 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1779 i_sample < p_track->i_sample; i_sample++ )
1781 i_pos += p_track->p_sample_size[i_sample];
1788 static int MP4_TrackNextSample( input_thread_t *p_input,
1789 track_data_mp4_t *p_track )
1792 if( p_track->i_cat == AUDIO_ES &&
1793 p_track->i_sample_size != 0 )
1795 MP4_Box_data_sample_soun_t *p_soun;
1797 p_soun = p_track->p_sample->data.p_sample_soun;
1799 if( p_soun->i_qt_version == 1 )
1801 /* chunk by chunk */
1803 p_track->chunk[p_track->i_chunk].i_sample_first +
1804 p_track->chunk[p_track->i_chunk].i_sample_count;
1809 p_track->i_sample += QT_V0_MAX_SAMPLES;
1810 if( p_track->i_sample > p_track->chunk[p_track->i_chunk].i_sample_first + p_track->chunk[p_track->i_chunk].i_sample_count )
1813 p_track->chunk[p_track->i_chunk].i_sample_first +
1814 p_track->chunk[p_track->i_chunk].i_sample_count;
1820 p_track->i_sample++;
1823 if( p_track->i_sample >= p_track->i_sample_count )
1825 /* we have reach end of the track so free decoder stuff */
1826 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1827 MP4_TrackUnselect( p_input, p_track );
1828 return( VLC_EGENERIC );
1831 /* Have we changed chunk ? */
1832 if( p_track->i_sample >=
1833 p_track->chunk[p_track->i_chunk].i_sample_first +
1834 p_track->chunk[p_track->i_chunk].i_sample_count )
1836 if( TrackGotoChunkSample( p_input,
1838 p_track->i_chunk + 1,
1839 p_track->i_sample ) )
1841 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1842 MP4_TrackUnselect( p_input, p_track );
1843 return( VLC_EGENERIC );
1847 return( VLC_SUCCESS );