1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.27 2003/04/30 21:45:52 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] ) )
158 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
159 return( VLC_EGENERIC );
162 /* create our structure that will contains all data */
163 if( !( p_input->p_demux_data =
164 p_demux = malloc( sizeof( demux_sys_t ) ) ) )
166 msg_Err( p_input, "out of memory" );
167 return( VLC_EGENERIC );
169 memset( p_demux, 0, sizeof( demux_sys_t ) );
170 p_input->p_demux_data = p_demux;
173 /* Now load all boxes ( except raw data ) */
174 if( !MP4_BoxGetRoot( p_input, &p_demux->box_root ) )
176 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
177 return( VLC_EGENERIC );
180 MP4_BoxDumpStructure( p_input, &p_demux->box_root );
182 if( ( p_ftyp = MP4_BoxGet( &p_demux->box_root, "/ftyp" ) ) )
184 switch( p_ftyp->data.p_ftyp->i_major_brand )
188 "ISO Media file (isom) version %d.",
189 p_ftyp->data.p_ftyp->i_minor_version );
193 "unrecognized major file specification (%4.4s).",
194 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
200 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
203 /* the file need to have one moov box */
204 if( MP4_BoxCount( &p_demux->box_root, "/moov" ) != 1 )
207 "MP4 plugin discarded (%d moov boxes)",
208 MP4_BoxCount( &p_demux->box_root, "/moov" ) );
209 // MP4End( p_input );
213 if( ( p_rmra = MP4_BoxGet( &p_demux->box_root, "/moov/rmra" ) ) )
215 playlist_t *p_playlist;
216 int i_count = MP4_BoxCount( p_rmra, "rmda" );
219 msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );
222 (playlist_t *)vlc_object_find( p_input,
227 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
228 for( i = 0; i < i_count; i++ )
230 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
234 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
238 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
240 msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
241 psz_ref, (char*)&i_ref_type );
243 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
245 if( strstr( psz_ref, "qt5gateQT" ) )
247 msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
250 if( !strncmp( psz_ref, "http://", 7 ) ||
251 !strncmp( psz_ref, "rtsp://", 7 ) )
253 msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
254 playlist_Add( p_playlist, psz_ref, PLAYLIST_APPEND, PLAYLIST_END );
258 /* msg dbg relative ? */
259 char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
260 char *end = strrchr( p_input->psz_source, '/' );
264 int i_len = end + 1 - p_input->psz_source;
266 strncpy( psz_absolute, p_input->psz_source, i_len);
267 psz_absolute[i_len] = '\0';
271 strcpy( psz_absolute, "" );
273 strcat( psz_absolute, psz_ref );
274 msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
275 playlist_Add( p_playlist, psz_absolute, PLAYLIST_APPEND, PLAYLIST_END );
280 msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)", (char*)&p_rdrf->data.p_rdrf->i_ref_type );
283 vlc_object_release( p_playlist );
287 msg_Err( p_input, "can't find playlist" );
291 if( !(p_mvhd = MP4_BoxGet( &p_demux->box_root, "/moov/mvhd" ) ) )
295 msg_Err( p_input, "cannot find /moov/mvhd" );
301 msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
302 p_input->pf_demux = MP4DemuxRef;
308 p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
309 p_demux->i_duration = p_mvhd->data.p_mvhd->i_duration;
312 if( !( p_demux->i_tracks =
313 MP4_BoxCount( &p_demux->box_root, "/moov/trak" ) ) )
315 msg_Err( p_input, "cannot find any /moov/trak" );
317 return( VLC_EGENERIC );
319 msg_Dbg( p_input, "find %d track%c",
321 p_demux->i_tracks ? 's':' ' );
323 /* create one program */
324 vlc_mutex_lock( &p_input->stream.stream_lock );
325 if( input_InitStream( p_input, 0 ) == -1)
327 vlc_mutex_unlock( &p_input->stream.stream_lock );
328 msg_Err( p_input, "cannot init stream" );
330 return( VLC_EGENERIC );
332 /* Needed to create program _before_ MP4_TrackCreate */
333 if( input_AddProgram( p_input, 0, 0) == NULL )
335 vlc_mutex_unlock( &p_input->stream.stream_lock );
336 msg_Err( p_input, "cannot add program" );
338 return( VLC_EGENERIC );
340 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
341 /* XXX beurk and beurk, see MP4Demux and MP4Seek */
342 if( p_demux->i_duration/p_demux->i_timescale > 0 )
344 p_input->stream.i_mux_rate =
345 p_input->stream.p_selected_area->i_size / 50 /
346 ( p_demux->i_duration / p_demux->i_timescale );
350 p_input->stream.i_mux_rate = 0;
352 vlc_mutex_unlock( &p_input->stream.stream_lock );
355 /* allocate memory */
356 p_demux->track = calloc( p_demux->i_tracks, sizeof( track_data_mp4_t ) );
358 /* now process each track and extract all usefull informations */
359 for( i = 0; i < p_demux->i_tracks; i++ )
361 p_trak = MP4_BoxGet( &p_demux->box_root, "/moov/trak[%d]", i );
362 MP4_TrackCreate( p_input, &p_demux->track[i], p_trak );
364 if( p_demux->track[i].b_ok )
367 switch( p_demux->track[i].i_cat )
380 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %c%c%c",
381 p_demux->track[i].i_track_ID,
383 p_demux->track[i].b_enable ? "enable":"disable",
384 p_demux->track[i].i_language[0],
385 p_demux->track[i].i_language[1],
386 p_demux->track[i].i_language[2] );
390 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_demux->track[i].i_track_ID );
395 for( i = 0, b_audio = VLC_FALSE; i < p_demux->i_tracks; i++ )
397 #define track p_demux->track[i]
398 /* start decoder for this track if enable by default*/
399 if( track.b_ok && track.b_enable &&
400 ( track.i_cat != AUDIO_ES || !b_audio ) )
402 if( !MP4_TrackSelect( p_input, &track, 0 ) )
404 if(track.i_cat == AUDIO_ES )
413 vlc_mutex_lock( &p_input->stream.stream_lock );
414 p_input->stream.p_selected_program->b_is_ok = 1;
415 vlc_mutex_unlock( &p_input->stream.stream_lock );
417 return( VLC_SUCCESS );
420 /*****************************************************************************
421 * MP4Demux: read packet and send them to decoders
422 *****************************************************************************
423 * TODO check for newly selected track (ie audio upt to now )
424 *****************************************************************************/
425 static int MP4Demux( input_thread_t *p_input )
427 demux_sys_t *p_demux = p_input->p_demux_data;
428 unsigned int i_track;
431 unsigned int i_track_selected;
433 vlc_bool_t b_play_audio;
435 /* check for newly selected/unselected track */
436 for( i_track = 0, i_track_selected = 0, b_video = VLC_FALSE;
437 i_track < p_demux->i_tracks; i_track++ )
439 #define track p_demux->track[i_track]
440 if( track.b_selected && track.i_sample >= track.i_sample_count )
442 msg_Warn( p_input, "track[0x%x] will be disabled", track.i_track_ID );
443 MP4_TrackUnselect( p_input, &track );
445 else if( track.b_ok )
447 if( track.b_selected && track.p_es->p_decoder_fifo == NULL )
449 MP4_TrackUnselect( p_input, &track );
451 else if( !track.b_selected && track.p_es->p_decoder_fifo != NULL )
453 MP4_TrackSelect( p_input, &track, MP4_GetMoviePTS( p_demux ) );
456 if( track.b_selected )
460 if( track.i_cat == VIDEO_ES )
469 if( i_track_selected <= 0 )
471 msg_Warn( p_input, "no track selected, exiting..." );
476 /* XXX beurk, beuRK and BEURK,
477 but only way I've found to detect seek from interface */
478 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
482 /* first wait for empty buffer, arbitrary time FIXME */
483 msleep( DEFAULT_PTS_DELAY );
484 /* *** calculate new date *** */
486 i_date = (mtime_t)1000000 *
487 (mtime_t)p_demux->i_duration /
488 (mtime_t)p_demux->i_timescale *
489 (mtime_t)MP4_TellAbsolute( p_input ) /
490 (mtime_t)p_input->stream.p_selected_area->i_size;
491 MP4Seek( p_input, i_date );
494 /* first wait for the good time to read a packet */
495 input_ClockManageRef( p_input,
496 p_input->stream.p_selected_program,
500 /* update pcr XXX in mpeg scale so in 90000 unit/s */
501 p_demux->i_pcr = MP4_GetMoviePTS( p_demux ) * 9 / 100;
504 /* we will read 100ms for each stream so ...*/
505 p_demux->i_time += __MAX( p_demux->i_timescale / 10 , 1 );
508 /* *** send audio data to decoder if rate == DEFAULT_RATE or no video *** */
509 vlc_mutex_lock( &p_input->stream.stream_lock );
510 if( p_input->stream.control.i_rate == DEFAULT_RATE || !b_video )
512 b_play_audio = VLC_TRUE;
516 b_play_audio = VLC_FALSE;
518 vlc_mutex_unlock( &p_input->stream.stream_lock );
520 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
522 #define track p_demux->track[i_track]
525 MP4_GetTrackPTS( &track ) >= MP4_GetMoviePTS( p_demux ) )
529 while( MP4_GetTrackPTS( &track ) < MP4_GetMoviePTS( p_demux ) )
532 if( !b_play_audio && track.i_cat == AUDIO_ES )
534 if( MP4_TrackNextSample( p_input, &track ) )
544 data_packet_t *p_data;
547 /* caculate size and position for this sample */
548 i_size = MP4_TrackSampleSize( &track );
550 i_pos = MP4_GetTrackPos( &track );
552 //msg_Dbg( p_input, "stream %d size=%6d pos=%8lld", i_track, i_size, i_pos );
555 if( MP4_SeekAbsolute( p_input, i_pos ) )
557 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
558 MP4_TrackUnselect( p_input, &track );
563 /* now create a pes */
564 if( !(p_pes = input_NewPES( p_input->p_method_data ) ) )
568 /* and a data packet for the data */
569 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
571 input_DeletePES( p_input->p_method_data, p_pes );
574 p_data->p_payload_end = p_data->p_payload_start + i_size;
576 /* initialisation of all the field */
577 p_pes->i_dts = p_pes->i_pts = 0;
578 p_pes->p_first = p_pes->p_last = p_data;
579 p_pes->i_nb_data = 1;
580 p_pes->i_pes_size = i_size;
583 if( MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
585 input_DeletePES( p_input->p_method_data, p_pes );
587 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
588 MP4_TrackUnselect( p_input, &track );
594 p_pes->i_pts = input_ClockGetTS( p_input,
595 p_input->stream.p_selected_program,
596 MP4_GetTrackPTS( &track ) * 9/100);
598 if( track.p_es->p_decoder_fifo )
600 input_DecodePES( track.p_es->p_decoder_fifo, p_pes );
604 input_DeletePES( p_input->p_method_data, p_pes );
607 if( MP4_TrackNextSample( p_input, &track ) )
618 /*****************************************************************************
619 * MP4Seek: Got to i_date
620 ******************************************************************************/
621 static int MP4Seek ( input_thread_t *p_input, mtime_t i_date )
623 demux_sys_t *p_demux = p_input->p_demux_data;
624 unsigned int i_track;
625 /* First update update global time */
626 p_demux->i_time = i_date * p_demux->i_timescale / 1000000;
627 p_demux->i_pcr = i_date* 9 / 100;
629 /* Now for each stream try to go to this time */
630 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
632 #define track p_demux->track[i_track]
633 if( track.b_ok && track.b_selected )
635 MP4_TrackSeek( p_input, &track, i_date );
642 /*****************************************************************************
643 * MP4End: frees unused data
644 *****************************************************************************/
645 static void __MP4End ( vlc_object_t * p_this )
647 unsigned int i_track;
648 input_thread_t * p_input = (input_thread_t *)p_this;
649 demux_sys_t *p_demux = p_input->p_demux_data;
651 msg_Dbg( p_input, "freeing all memory" );
652 MP4_BoxFree( p_input, &p_demux->box_root );
653 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
655 MP4_TrackDestroy( p_input, &p_demux->track[i_track] );
657 FREE( p_demux->track );
659 FREE( p_input->p_demux_data );
663 /****************************************************************************
664 * Local functions, specific to vlc
665 ****************************************************************************/
667 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
668 static int TrackCreateChunksIndex( input_thread_t *p_input,
669 track_data_mp4_t *p_demux_track )
671 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
674 unsigned int i_chunk;
675 unsigned int i_index, i_last;
677 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
678 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
679 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
681 return( VLC_EGENERIC );
684 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
685 if( !p_demux_track->i_chunk_count )
687 msg_Warn( p_input, "no chunk defined" );
688 return( VLC_EGENERIC );
690 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
691 sizeof( chunk_data_mp4_t ) );
693 /* first we read chunk offset */
694 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
696 p_demux_track->chunk[i_chunk].i_offset =
697 p_co64->data.p_co64->i_chunk_offset[i_chunk];
700 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
701 to be used for the sample XXX begin to 1
702 We construct it begining at the end */
703 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
704 i_index = p_stsc->data.p_stsc->i_entry_count;
707 msg_Warn( p_input, "cannot read chunk table or table empty" );
708 return( VLC_EGENERIC );
714 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
715 i_chunk < i_last; i_chunk++ )
717 p_demux_track->chunk[i_chunk].i_sample_description_index =
718 p_stsc->data.p_stsc->i_sample_description_index[i_index];
719 p_demux_track->chunk[i_chunk].i_sample_count =
720 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
722 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
725 p_demux_track->chunk[0].i_sample_first = 0;
726 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
728 p_demux_track->chunk[i_chunk].i_sample_first =
729 p_demux_track->chunk[i_chunk-1].i_sample_first +
730 p_demux_track->chunk[i_chunk-1].i_sample_count;
734 "track[Id 0x%x] read %d chunk",
735 p_demux_track->i_track_ID,
736 p_demux_track->i_chunk_count );
738 return( VLC_SUCCESS );
740 static int TrackCreateSamplesIndex( input_thread_t *p_input,
741 track_data_mp4_t *p_demux_track )
743 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
744 ctts make same mapping but for composition time,
745 not yet used and probably not usefull */
746 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
747 that uses a compressed form FIXME make them in libmp4
749 /* TODO use also stss and stsh table for seeking */
750 /* FIXME use edit table */
755 int64_t i_index_sample_used;
759 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
760 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
762 if( ( !p_stts )||( !p_stsz ) )
764 msg_Warn( p_input, "cannot read sample table" );
765 return( VLC_EGENERIC );
768 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
771 /* for sample size, there are 2 case */
772 if( p_stsz->data.p_stsz->i_sample_size )
774 /* 1: all sample have the same size, so no need to construct a table */
775 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
776 p_demux_track->p_sample_size = NULL;
780 /* 2: each sample can have a different size */
781 p_demux_track->i_sample_size = 0;
782 p_demux_track->p_sample_size =
783 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
785 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
787 p_demux_track->p_sample_size[i_sample] =
788 p_stsz->data.p_stsz->i_entry_size[i_sample];
791 /* we have extract all information from stsz,
794 /* if we don't want to waste too much memory, we can't expand
795 the box !, so each chunk will contain an "extract" of this table
799 i_index = 0; i_index_sample_used =0;
801 /* create and init last data for each chunk */
802 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
805 int64_t i_entry, i_sample_count, i;
807 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
808 /* count how many entries needed for this chunk
809 for p_sample_delta_dts and p_sample_count_dts */
811 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
814 while( i_sample_count > 0 )
816 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
819 i_sample_count += i_index_sample_used; /* don't count already used sample
826 p_demux_track->chunk[i_chunk].p_sample_count_dts =
827 calloc( i_entry, sizeof( uint32_t ) );
828 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
829 calloc( i_entry, sizeof( uint32_t ) );
832 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
833 for( i = 0; i < i_entry; i++ )
838 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
840 i_used = __MIN( i_rest, i_sample_count );
842 i_index_sample_used += i_used;
843 i_sample_count -= i_used;
845 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
847 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
848 p_stts->data.p_stts->i_sample_delta[i_index];
850 i_last_dts += i_used *
851 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
853 if( i_index_sample_used >=
854 p_stts->data.p_stts->i_sample_count[i_index] )
858 i_index_sample_used = 0;
865 "track[Id 0x%x] read %d samples length:"I64Fd"s",
866 p_demux_track->i_track_ID,
867 p_demux_track->i_sample_count,
868 i_last_dts / p_demux_track->i_timescale );
870 return( VLC_SUCCESS );
875 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
877 static int TrackCreateES ( input_thread_t *p_input,
878 track_data_mp4_t *p_track,
879 unsigned int i_chunk,
880 es_descriptor_t **pp_es,
881 pes_packet_t **pp_pes )
883 MP4_Box_t * p_sample;
886 unsigned int i_decoder_specific_info_len;
887 uint8_t * p_decoder_specific_info;
889 es_descriptor_t *p_es;
890 pes_packet_t *p_pes_init;
893 BITMAPINFOHEADER *p_bih;
898 if( !p_track->chunk[i_chunk].i_sample_description_index )
901 "invalid SampleEntry index (track[Id 0x%x])",
902 p_track->i_track_ID );
903 return( VLC_EGENERIC );
906 p_sample = MP4_BoxGet( p_track->p_stsd,
908 p_track->chunk[i_chunk].i_sample_description_index - 1 );
910 if( !p_sample || !p_sample->data.p_data )
913 "cannot find SampleEntry (track[Id 0x%x])",
914 p_track->i_track_ID );
915 return( VLC_EGENERIC );
918 p_track->p_sample = p_sample;
920 if( p_track->i_sample_size == 1 )
922 MP4_Box_data_sample_soun_t *p_soun;
924 p_soun = p_sample->data.p_sample_soun;
926 if( p_soun->i_qt_version == 0 )
928 switch( p_sample->i_type )
930 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
931 p_soun->i_qt_version = 1;
932 p_soun->i_sample_per_packet = 64;
933 p_soun->i_bytes_per_packet = 34;
934 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
935 p_soun->i_bytes_per_sample = 2;
944 vlc_mutex_lock( &p_input->stream.stream_lock );
945 p_es = input_AddES( p_input,
946 p_input->stream.p_selected_program,
949 vlc_mutex_unlock( &p_input->stream.stream_lock );
950 /* Initialise ES, first language as description */
951 for( i = 0; i < 3; i++ )
953 p_es->psz_desc[i] = p_track->i_language[i];
955 p_es->psz_desc[3] = '\0';
957 p_es->i_stream_id = p_track->i_track_ID;
959 /* It's a little ugly but .. there are special cases */
960 switch( p_sample->i_type )
962 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
963 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
964 p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
966 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
967 p_es->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
970 p_es->i_fourcc = p_sample->i_type;
974 p_es->i_cat = p_track->i_cat;
976 i_decoder_specific_info_len = 0;
977 p_decoder_specific_info = NULL;
980 /* now see if esds is present and if so create a data packet
981 with decoder_specific_info */
982 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
983 if( ( p_esds = MP4_BoxGet( p_sample, "esds" ) )&&
984 ( p_esds->data.p_esds )&&
987 /* First update information based on i_objectTypeIndication */
988 switch( p_decconfig->i_objectTypeIndication )
990 case( 0x20 ): /* MPEG4 VIDEO */
991 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
994 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1001 case( 0x65): /* MPEG2 video */
1002 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1004 /* Theses are MPEG2-AAC */
1005 case( 0x66): /* main profile */
1006 case( 0x67): /* Low complexity profile */
1007 case( 0x68): /* Scaleable Sampling rate profile */
1008 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1010 /* true MPEG 2 audio */
1012 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1014 case( 0x6a): /* MPEG1 video */
1015 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1017 case( 0x6b): /* MPEG1 audio */
1018 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1020 case( 0x6c ): /* jpeg */
1021 p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
1024 /* Unknown entry, but don't touch i_fourcc */
1026 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1027 p_decconfig->i_objectTypeIndication,
1028 p_track->i_track_ID );
1031 i_decoder_specific_info_len =
1032 p_decconfig->i_decoder_specific_info_len;
1033 p_decoder_specific_info =
1034 p_decconfig->p_decoder_specific_info;
1038 switch( p_sample->i_type )
1040 /* qt decoder, send the complete chunk */
1041 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1042 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1043 i_decoder_specific_info_len = p_sample->data.p_sample_vide->i_qt_image_description;
1044 p_decoder_specific_info = p_sample->data.p_sample_vide->p_qt_image_description;
1046 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1047 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1048 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1049 i_decoder_specific_info_len = p_sample->data.p_sample_soun->i_qt_description;
1050 p_decoder_specific_info = p_sample->data.p_sample_soun->p_qt_description;
1059 /* some last initialisation */
1060 /* XXX I create a bitmapinfoheader_t or
1061 waveformatex_t for each stream, up to now it's the best thing
1062 I've found but it could exist a better solution :) as something
1063 like adding some new fields in p_es ...
1065 XXX I don't set all values, only thoses that are interesting or known
1066 --> bitmapinfoheader_t : width and height
1067 --> waveformatex_t : channels, samplerate, bitspersample
1068 and at the end I add p_decoder_specific_info
1070 TODO set more values
1074 switch( p_track->i_cat )
1077 /* now create a bitmapinfoheader_t for decoder and
1078 add information found in p_esds */
1079 /* XXX XXX + 16 are for avoid segfault when ffmpeg access beyong the data */
1080 p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len + 16 );
1081 p_bih = (BITMAPINFOHEADER*)p_init;
1083 p_bih->biSize = sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len;
1084 p_bih->biWidth = p_sample->data.p_sample_vide->i_width;
1085 p_bih->biHeight = p_sample->data.p_sample_vide->i_height;
1086 p_bih->biPlanes = 1; // FIXME
1087 p_bih->biBitCount = 0; // FIXME
1088 p_bih->biCompression = 0; // FIXME
1089 p_bih->biSizeImage = 0; // FIXME
1090 p_bih->biXPelsPerMeter = 0; // FIXME
1091 p_bih->biYPelsPerMeter = 0; // FIXME
1092 p_bih->biClrUsed = 0; // FIXME
1093 p_bih->biClrImportant = 0; // FIXME
1095 if( p_bih->biWidth == 0 )
1097 // fall on display size
1098 p_bih->biWidth = p_track->i_width;
1100 if( p_bih->biHeight == 0 )
1102 // fall on display size
1103 p_bih->biHeight = p_track->i_height;
1106 if( i_decoder_specific_info_len )
1108 data_packet_t *p_data;
1110 memcpy( p_init + sizeof( BITMAPINFOHEADER ),
1111 p_decoder_specific_info,
1112 i_decoder_specific_info_len);
1114 /* If stream is mpeg4 video we send specific_info,
1115 as it's needed to decode it (vol) */
1116 switch( p_es->i_fourcc )
1118 case VLC_FOURCC( 'm','p','4','v' ):
1119 case VLC_FOURCC( 'D','I','V','X' ):
1120 case VLC_FOURCC( 'd','i','v','x' ):
1121 p_pes_init = input_NewPES( p_input->p_method_data );
1122 p_data = input_NewPacket( p_input->p_method_data,
1123 i_decoder_specific_info_len);
1124 p_data->p_payload_end = p_data->p_payload_start + i_decoder_specific_info_len;
1126 memcpy( p_data->p_payload_start,
1127 p_decoder_specific_info,
1128 i_decoder_specific_info_len );
1129 p_pes_init->i_dts = p_pes_init->i_pts = 0;
1130 p_pes_init->p_first = p_pes_init->p_last = p_data;
1131 p_pes_init->i_nb_data = 1;
1132 p_pes_init->i_pes_size = i_decoder_specific_info_len;
1142 p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len + 16 );
1143 p_wf = (WAVEFORMATEX*)p_init;
1145 p_wf->wFormatTag = 1;
1146 p_wf->nChannels = p_sample->data.p_sample_soun->i_channelcount;
1147 p_wf->nSamplesPerSec = p_sample->data.p_sample_soun->i_sampleratehi;
1148 p_wf->nAvgBytesPerSec = p_sample->data.p_sample_soun->i_channelcount *
1149 p_sample->data.p_sample_soun->i_sampleratehi *
1150 p_sample->data.p_sample_soun->i_samplesize / 8;
1151 p_wf->nBlockAlign = 0;
1152 p_wf->wBitsPerSample = p_sample->data.p_sample_soun->i_samplesize;
1153 p_wf->cbSize = i_decoder_specific_info_len;
1155 if( i_decoder_specific_info_len )
1157 memcpy( p_init + sizeof( WAVEFORMATEX ),
1158 p_decoder_specific_info,
1159 i_decoder_specific_info_len);
1168 if( p_es->i_cat == AUDIO_ES )
1170 p_es->p_bitmapinfoheader = NULL;
1171 p_es->p_waveformatex = (void*)p_init;
1173 else if( p_es->i_cat == VIDEO_ES )
1175 p_es->p_bitmapinfoheader = (void*)p_init;
1176 p_es->p_waveformatex = NULL;
1180 *pp_pes = p_pes_init;
1181 return( VLC_SUCCESS );
1184 /* given a time it return sample/chunk */
1185 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1186 track_data_mp4_t *p_track,
1189 uint32_t *pi_sample )
1193 unsigned int i_sample;
1194 unsigned int i_chunk;
1197 /* convert absolute time to in timescale unit */
1198 i_start = i_start * (mtime_t)p_track->i_timescale / (mtime_t)1000000;
1200 /* FIXME see if it's needed to check p_track->i_chunk_count */
1201 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1203 return( VLC_EGENERIC );
1206 /* we start from sample 0/chunk 0, hope it won't take too much time */
1207 /* *** find good chunk *** */
1208 for( i_chunk = 0; ; i_chunk++ )
1210 if( i_chunk + 1 >= p_track->i_chunk_count )
1212 /* at the end and can't check if i_start in this chunk,
1213 it will be check while searching i_sample */
1214 i_chunk = p_track->i_chunk_count - 1;
1218 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1219 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1225 /* *** find sample in the chunk *** */
1226 i_sample = p_track->chunk[i_chunk].i_sample_first;
1227 i_dts = p_track->chunk[i_chunk].i_first_dts;
1228 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1231 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1232 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1235 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1236 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1238 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1243 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1247 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1252 if( i_sample >= p_track->i_sample_count )
1255 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1256 p_track->i_track_ID, i_chunk, i_sample );
1257 return( VLC_EGENERIC );
1261 /* *** Try to find nearest sync points *** */
1262 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1264 unsigned int i_index;
1266 "track[Id 0x%x] using Sync Sample Box (stss)",
1267 p_track->i_track_ID );
1268 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1270 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1274 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1276 p_stss->data.p_stss->i_sample_number[i_index-1] );
1277 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1278 /* new i_sample is less than old so i_chunk can only decreased */
1279 while( i_chunk > 0 &&
1280 i_sample < p_track->chunk[i_chunk].i_sample_first )
1287 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1289 p_stss->data.p_stss->i_sample_number[i_index] );
1290 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1291 /* new i_sample is more than old so i_chunk can only increased */
1292 while( i_chunk < p_track->i_chunk_count - 1 &&
1293 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1294 p_track->chunk[i_chunk].i_sample_count )
1306 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1307 p_track->i_track_ID );
1310 if( pi_chunk ) *pi_chunk = i_chunk;
1311 if( pi_sample ) *pi_sample = i_sample;
1312 return( VLC_SUCCESS );
1315 static int TrackGotoChunkSample( input_thread_t *p_input,
1316 track_data_mp4_t *p_track,
1317 unsigned int i_chunk,
1318 unsigned int i_sample )
1320 /* now see if actual es is ok */
1321 if( p_track->i_chunk < 0 ||
1322 p_track->i_chunk >= p_track->i_chunk_count ||
1323 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1324 p_track->chunk[i_chunk].i_sample_description_index )
1326 msg_Warn( p_input, "Recreate ES" );
1328 /* no :( recreate es */
1329 vlc_mutex_lock( &p_input->stream.stream_lock );
1330 input_DelES( p_input, p_track->p_es );
1331 vlc_mutex_unlock( &p_input->stream.stream_lock );
1333 if( p_track->p_pes_init )
1335 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1338 if( TrackCreateES( p_input,
1341 &p_track->p_pes_init ) )
1343 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1344 p_track->i_track_ID );
1346 p_track->b_ok = VLC_FALSE;
1347 p_track->b_selected = VLC_FALSE;
1348 return( VLC_EGENERIC );
1352 /* select again the new decoder */
1353 if( p_track->b_selected && p_track->p_es && p_track->p_es->p_decoder_fifo == NULL )
1355 vlc_mutex_lock( &p_input->stream.stream_lock );
1356 input_SelectES( p_input, p_track->p_es );
1357 vlc_mutex_unlock( &p_input->stream.stream_lock );
1359 if( p_track->p_es->p_decoder_fifo )
1361 if( p_track->p_pes_init != NULL )
1363 input_DecodePES( p_track->p_es->p_decoder_fifo, p_track->p_pes_init );
1364 p_track->p_pes_init = NULL;
1366 p_track->b_selected = VLC_TRUE;
1370 msg_Dbg( p_input, "Argg cannot select this stream" );
1371 if( p_track->p_pes_init != NULL )
1373 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1374 p_track->p_pes_init = NULL;
1376 p_track->b_selected = VLC_FALSE;
1380 p_track->i_chunk = i_chunk;
1381 p_track->i_sample = i_sample;
1383 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1386 /****************************************************************************
1388 ****************************************************************************
1389 * Parse track information and create all needed data to run a track
1390 * If it succeed b_ok is set to 1 else to 0
1391 ****************************************************************************/
1392 static void MP4_TrackCreate( input_thread_t *p_input,
1393 track_data_mp4_t *p_track,
1394 MP4_Box_t * p_box_trak )
1398 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1399 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1408 /* hint track unsuported */
1410 /* set default value (-> track unusable) */
1411 p_track->b_ok = VLC_FALSE;
1412 p_track->b_enable = VLC_FALSE;
1413 p_track->b_selected = VLC_FALSE;
1415 p_track->i_cat = UNKNOWN_ES;
1422 /* do we launch this track by default ? */
1424 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1426 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1427 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1428 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1430 if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1432 /* msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
1437 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1440 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1441 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1443 if( ( !p_mdhd )||( !p_hdlr ) )
1448 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1450 for( i = 0; i < 3; i++ )
1452 p_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
1454 p_mdhd->data.p_mdhd->i_language[3] = 0;
1456 switch( p_hdlr->data.p_hdlr->i_handler_type )
1458 case( FOURCC_soun ):
1459 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1463 p_track->i_cat = AUDIO_ES;
1466 case( FOURCC_vide ):
1467 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1471 p_track->i_cat = VIDEO_ES;
1479 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1481 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) )
1486 if( !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1491 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1492 if( p_track->i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1494 MP4_Box_t *p_sample;
1496 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1499 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1500 if( p_soun->i_qt_version == 0 && p_track->i_timescale != p_soun->i_sampleratehi )
1502 msg_Warn( p_input, "i_timescale != i_sampleratehi with qt_version == 0\nMaking both equal ? (report any problem)" );
1503 p_track->i_timescale = p_soun->i_sampleratehi;
1509 /* Create chunk index table */
1510 if( TrackCreateChunksIndex( p_input,p_track ) )
1512 return; /* cannot create chunks index */
1515 /* create sample index table needed for reading and seeking */
1516 if( TrackCreateSamplesIndex( p_input, p_track ) )
1518 return; /* cannot create samples index */
1521 p_track->i_chunk = 0;
1522 p_track->i_sample = 0;
1523 /* now create es but does not select it */
1524 /* XXX needed else vlc won't know this track exist */
1525 if( TrackCreateES( p_input,
1526 p_track, p_track->i_chunk,
1528 &p_track->p_pes_init ) )
1530 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1531 p_track->i_track_ID );
1538 for( i = 0; i < p_track->i_chunk_count; i++ )
1540 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1545 p_track->b_ok = VLC_TRUE;
1548 /****************************************************************************
1550 ****************************************************************************
1551 * Destroy a track created by MP4_TrackCreate.
1552 ****************************************************************************/
1553 static void MP4_TrackDestroy( input_thread_t *p_input,
1554 track_data_mp4_t *p_track )
1556 unsigned int i_chunk;
1558 p_track->b_ok = VLC_FALSE;
1559 p_track->b_enable = VLC_FALSE;
1560 p_track->b_selected = VLC_FALSE;
1562 p_track->i_cat = UNKNOWN_ES;
1564 if( p_track->p_pes_init )
1566 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1569 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1571 if( p_track->chunk )
1573 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1574 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1577 FREE( p_track->chunk );
1579 if( !p_track->i_sample_size )
1581 FREE( p_track->p_sample_size );
1585 static int MP4_TrackSelect ( input_thread_t *p_input,
1586 track_data_mp4_t *p_track,
1592 if( !p_track->b_ok )
1594 return( VLC_EGENERIC );
1597 if( p_track->b_selected )
1600 "track[Id 0x%x] already selected",
1601 p_track->i_track_ID );
1602 return( VLC_SUCCESS );
1605 if( TrackTimeToSampleChunk( p_input,
1607 &i_chunk, &i_sample ) )
1610 "cannot select track[Id 0x%x]",
1611 p_track->i_track_ID );
1612 return( VLC_EGENERIC );
1615 p_track->b_selected = VLC_TRUE;
1617 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1619 p_track->b_selected = VLC_FALSE;
1622 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1625 static void MP4_TrackUnselect(input_thread_t *p_input,
1626 track_data_mp4_t *p_track )
1628 if( !p_track->b_ok )
1633 if( !p_track->b_selected )
1636 "track[Id 0x%x] already unselected",
1637 p_track->i_track_ID );
1641 if( p_track->p_es->p_decoder_fifo )
1643 vlc_mutex_lock( &p_input->stream.stream_lock );
1644 input_UnselectES( p_input, p_track->p_es );
1645 vlc_mutex_unlock( &p_input->stream.stream_lock );
1648 p_track->b_selected = VLC_FALSE;
1651 static int MP4_TrackSeek ( input_thread_t *p_input,
1652 track_data_mp4_t *p_track,
1658 if( !p_track->b_ok )
1660 return( VLC_EGENERIC );
1663 if( TrackTimeToSampleChunk( p_input,
1665 &i_chunk, &i_sample ) )
1668 "cannot select track[Id 0x%x]",
1669 p_track->i_track_ID );
1670 return( VLC_EGENERIC );
1673 p_track->b_selected = VLC_TRUE;
1675 TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample );
1677 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1685 * 3 types: for audio
1688 #define QT_V0_MAX_SAMPLES 1500
1689 static int MP4_TrackSampleSize( track_data_mp4_t *p_track )
1692 MP4_Box_data_sample_soun_t *p_soun;
1694 if( p_track->i_sample_size == 0 )
1696 /* most simple case */
1697 return( p_track->p_sample_size[p_track->i_sample] );
1699 if( p_track->i_cat != AUDIO_ES )
1701 return( p_track->i_sample_size );
1704 if( p_track->i_sample_size != 1 )
1706 //msg_Warn( p_input, "SampleSize != 1" );
1707 return( p_track->i_sample_size );
1710 p_soun = p_track->p_sample->data.p_sample_soun;
1712 if( p_soun->i_qt_version == 1 )
1714 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1719 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1720 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1721 if( i_samples > QT_V0_MAX_SAMPLES )
1723 i_samples = QT_V0_MAX_SAMPLES;
1725 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1728 //fprintf( stderr, "size=%d\n", i_size );
1733 static uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
1735 unsigned int i_sample;
1739 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1741 if( p_track->i_sample_size )
1743 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1745 if( p_soun->i_qt_version == 0 )
1747 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1748 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1752 /* we read chunk by chunk */
1758 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1759 i_sample < p_track->i_sample; i_sample++ )
1761 i_pos += p_track->p_sample_size[i_sample];
1768 static int MP4_TrackNextSample( input_thread_t *p_input,
1769 track_data_mp4_t *p_track )
1772 if( p_track->i_cat == AUDIO_ES &&
1773 p_track->i_sample_size != 0 )
1775 MP4_Box_data_sample_soun_t *p_soun;
1777 p_soun = p_track->p_sample->data.p_sample_soun;
1779 if( p_soun->i_qt_version == 1 )
1781 /* chunk by chunk */
1783 p_track->chunk[p_track->i_chunk].i_sample_first +
1784 p_track->chunk[p_track->i_chunk].i_sample_count;
1789 p_track->i_sample += QT_V0_MAX_SAMPLES;
1790 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 )
1793 p_track->chunk[p_track->i_chunk].i_sample_first +
1794 p_track->chunk[p_track->i_chunk].i_sample_count;
1800 p_track->i_sample++;
1803 if( p_track->i_sample >= p_track->i_sample_count )
1805 /* we have reach end of the track so free decoder stuff */
1806 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1807 MP4_TrackUnselect( p_input, p_track );
1808 return( VLC_EGENERIC );
1811 /* Have we changed chunk ? */
1812 if( p_track->i_sample >=
1813 p_track->chunk[p_track->i_chunk].i_sample_first +
1814 p_track->chunk[p_track->i_chunk].i_sample_count )
1816 if( TrackGotoChunkSample( p_input,
1818 p_track->i_chunk + 1,
1819 p_track->i_sample ) )
1821 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1822 MP4_TrackUnselect( p_input, p_track );
1823 return( VLC_EGENERIC );
1827 return( VLC_SUCCESS );