1 /*****************************************************************************
2 * mp4.c : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.c,v 1.35 2003/09/07 22:48:29 fenrir Exp $
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/input.h>
30 #include <vlc_playlist.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close ( vlc_object_t * );
42 set_description( _("MP4 demuxer") );
43 set_capability( "demux", 242 );
44 set_callbacks( Open, Close );
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Demux ( input_thread_t * );
52 static int DemuxRef( input_thread_t *p_input )
56 static int Seek ( input_thread_t *, mtime_t );
57 static int Control ( input_thread_t *, int, va_list );
59 /*****************************************************************************
60 * Declaration of local function
61 *****************************************************************************/
63 static void MP4_TrackCreate ( input_thread_t *, track_data_mp4_t *, MP4_Box_t *);
64 static void MP4_TrackDestroy( input_thread_t *, track_data_mp4_t * );
66 static int MP4_TrackSelect ( input_thread_t *, track_data_mp4_t *, mtime_t );
67 static void MP4_TrackUnselect(input_thread_t *, track_data_mp4_t * );
69 static int MP4_TrackSeek ( input_thread_t *, track_data_mp4_t *, mtime_t );
71 static uint64_t MP4_GetTrackPos ( track_data_mp4_t * );
72 static int MP4_TrackSampleSize( track_data_mp4_t * );
73 static int MP4_TrackNextSample( input_thread_t *, track_data_mp4_t * );
75 #define MP4_Set4BytesLE( p, dw ) \
76 *((uint8_t*)p) = ( (dw)&0xff ); \
77 *((uint8_t*)p+1) = ( ((dw)>> 8)&0xff ); \
78 *((uint8_t*)p+2) = ( ((dw)>>16)&0xff ); \
79 *((uint8_t*)p+3) = ( ((dw)>>24)&0xff )
81 #define MP4_Set2BytesLE( p, dw ) \
82 *((uint8_t*)p) = ( (dw)&0xff ); \
83 *((uint8_t*)p+1) = ( ((dw)>> 8)&0xff )
86 if( p ) { free( p ); (p) = NULL;}
88 /*****************************************************************************
89 * Open: check file and initializes MP4 structures
90 *****************************************************************************/
91 static int Open( vlc_object_t * p_this )
93 input_thread_t *p_input = (input_thread_t *)p_this;
108 /* a little test to see if it could be a mp4 */
109 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
111 msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
114 switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
127 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
131 if( !p_input->stream.b_seekable )
133 msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
137 /*Set exported functions */
138 p_input->pf_demux = Demux;
139 p_input->pf_demux_control = Control;
142 /* create our structure that will contains all data */
143 p_input->p_demux_data = p_demux = malloc( sizeof( demux_sys_t ) );
144 memset( p_demux, 0, sizeof( demux_sys_t ) );
146 /* Now load all boxes ( except raw data ) */
147 if( !MP4_BoxGetRoot( p_input, &p_demux->box_root ) )
149 msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
153 MP4_BoxDumpStructure( p_input, &p_demux->box_root );
155 if( ( p_ftyp = MP4_BoxGet( &p_demux->box_root, "/ftyp" ) ) )
157 switch( p_ftyp->data.p_ftyp->i_major_brand )
161 "ISO Media file (isom) version %d.",
162 p_ftyp->data.p_ftyp->i_minor_version );
166 "unrecognized major file specification (%4.4s).",
167 (char*)&p_ftyp->data.p_ftyp->i_major_brand );
173 msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
176 /* the file need to have one moov box */
177 if( MP4_BoxCount( &p_demux->box_root, "/moov" ) <= 0 )
179 MP4_Box_t *p_foov = MP4_BoxGet( &p_demux->box_root, "/foov" );
183 msg_Err( p_input, "MP4 plugin discarded (no moov box)" );
186 /* we have a free box as a moov, rename it */
187 p_foov->i_type = FOURCC_moov;
190 if( ( p_rmra = MP4_BoxGet( &p_demux->box_root, "/moov/rmra" ) ) )
192 playlist_t *p_playlist;
193 int i_count = MP4_BoxCount( p_rmra, "rmda" );
196 msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );
199 (playlist_t *)vlc_object_find( p_input,
204 //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
205 for( i = 0; i < i_count; i++ )
207 MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
211 if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
215 i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;
217 msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
218 psz_ref, (char*)&i_ref_type );
220 if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
222 if( strstr( psz_ref, "qt5gateQT" ) )
224 msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
227 if( !strncmp( psz_ref, "http://", 7 ) ||
228 !strncmp( psz_ref, "rtsp://", 7 ) )
230 msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
231 playlist_Add( p_playlist, psz_ref, 0, 0,
232 PLAYLIST_APPEND, PLAYLIST_END );
236 /* msg dbg relative ? */
237 char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
238 char *end = strrchr( p_input->psz_source, '/' );
242 int i_len = end + 1 - p_input->psz_source;
244 strncpy( psz_absolute, p_input->psz_source, i_len);
245 psz_absolute[i_len] = '\0';
249 strcpy( psz_absolute, "" );
251 strcat( psz_absolute, psz_ref );
252 msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
253 playlist_Add( p_playlist, psz_absolute, 0, 0,
254 PLAYLIST_APPEND, PLAYLIST_END );
259 msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)", (char*)&p_rdrf->data.p_rdrf->i_ref_type );
262 vlc_object_release( p_playlist );
266 msg_Err( p_input, "can't find playlist" );
270 if( !(p_mvhd = MP4_BoxGet( &p_demux->box_root, "/moov/mvhd" ) ) )
274 msg_Err( p_input, "cannot find /moov/mvhd" );
279 msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
280 p_input->pf_demux = DemuxRef;
286 p_demux->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
287 p_demux->i_duration = p_mvhd->data.p_mvhd->i_duration;
290 if( !( p_demux->i_tracks =
291 MP4_BoxCount( &p_demux->box_root, "/moov/trak" ) ) )
293 msg_Err( p_input, "cannot find any /moov/trak" );
296 msg_Dbg( p_input, "find %d track%c",
298 p_demux->i_tracks ? 's':' ' );
300 /* create one program */
301 vlc_mutex_lock( &p_input->stream.stream_lock );
302 if( input_InitStream( p_input, 0 ) == -1)
304 vlc_mutex_unlock( &p_input->stream.stream_lock );
305 msg_Err( p_input, "cannot init stream" );
308 /* Needed to create program _before_ MP4_TrackCreate */
309 if( input_AddProgram( p_input, 0, 0) == NULL )
311 vlc_mutex_unlock( &p_input->stream.stream_lock );
312 msg_Err( p_input, "cannot add program" );
315 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
316 if( p_demux->i_duration/p_demux->i_timescale > 0 )
318 p_input->stream.i_mux_rate =
319 p_input->stream.p_selected_area->i_size / 50 /
320 ( p_demux->i_duration / p_demux->i_timescale );
324 p_input->stream.i_mux_rate = 0;
326 vlc_mutex_unlock( &p_input->stream.stream_lock );
329 /* allocate memory */
330 p_demux->track = calloc( p_demux->i_tracks, sizeof( track_data_mp4_t ) );
332 /* now process each track and extract all usefull informations */
333 for( i = 0; i < p_demux->i_tracks; i++ )
335 p_trak = MP4_BoxGet( &p_demux->box_root, "/moov/trak[%d]", i );
336 MP4_TrackCreate( p_input, &p_demux->track[i], p_trak );
338 if( p_demux->track[i].b_ok )
341 switch( p_demux->track[i].i_cat )
354 msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %c%c%c",
355 p_demux->track[i].i_track_ID,
357 p_demux->track[i].b_enable ? "enable":"disable",
358 p_demux->track[i].i_language[0],
359 p_demux->track[i].i_language[1],
360 p_demux->track[i].i_language[2] );
364 msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_demux->track[i].i_track_ID );
369 for( i = 0, b_audio = VLC_FALSE; i < p_demux->i_tracks; i++ )
371 #define track p_demux->track[i]
372 /* start decoder for this track if enable by default*/
373 if( track.b_ok && track.b_enable &&
374 ( track.i_cat != AUDIO_ES || !b_audio ) )
376 if( !MP4_TrackSelect( p_input, &track, 0 ) )
378 if(track.i_cat == AUDIO_ES )
387 vlc_mutex_lock( &p_input->stream.stream_lock );
388 p_input->stream.p_selected_program->b_is_ok = 1;
389 vlc_mutex_unlock( &p_input->stream.stream_lock );
394 Close( VLC_OBJECT( p_input ) );
398 /*****************************************************************************
399 * Demux: read packet and send them to decoders
400 *****************************************************************************
401 * TODO check for newly selected track (ie audio upt to now )
402 *****************************************************************************/
403 static int Demux( input_thread_t *p_input )
405 demux_sys_t *p_demux = p_input->p_demux_data;
406 unsigned int i_track;
409 unsigned int i_track_selected;
411 vlc_bool_t b_play_audio;
413 /* check for newly selected/unselected track */
414 for( i_track = 0, i_track_selected = 0, b_video = VLC_FALSE;
415 i_track < p_demux->i_tracks; i_track++ )
417 #define track p_demux->track[i_track]
418 if( track.b_selected && track.i_sample >= track.i_sample_count )
420 msg_Warn( p_input, "track[0x%x] will be disabled", track.i_track_ID );
421 MP4_TrackUnselect( p_input, &track );
423 else if( track.b_ok )
425 if( track.b_selected && track.p_es->p_decoder_fifo == NULL )
427 MP4_TrackUnselect( p_input, &track );
429 else if( !track.b_selected && track.p_es->p_decoder_fifo != NULL )
431 MP4_TrackSelect( p_input, &track, MP4_GetMoviePTS( p_demux ) );
434 if( track.b_selected )
438 if( track.i_cat == VIDEO_ES )
447 if( i_track_selected <= 0 )
449 msg_Warn( p_input, "no track selected, exiting..." );
453 /* first wait for the good time to read a packet */
454 input_ClockManageRef( p_input,
455 p_input->stream.p_selected_program,
459 /* update pcr XXX in mpeg scale so in 90000 unit/s */
460 p_demux->i_pcr = MP4_GetMoviePTS( p_demux ) * 9 / 100;
463 /* we will read 100ms for each stream so ...*/
464 p_demux->i_time += __MAX( p_demux->i_timescale / 10 , 1 );
467 /* Check if we need to send the audio data to decoder */
468 b_play_audio = !p_input->stream.control.b_mute;
470 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
472 #define track p_demux->track[i_track]
475 MP4_GetTrackPTS( &track ) >= MP4_GetMoviePTS( p_demux ) )
479 while( MP4_GetTrackPTS( &track ) < MP4_GetMoviePTS( p_demux ) )
482 if( !b_play_audio && track.i_cat == AUDIO_ES )
484 if( MP4_TrackNextSample( p_input, &track ) )
494 data_packet_t *p_data;
497 /* caculate size and position for this sample */
498 i_size = MP4_TrackSampleSize( &track );
500 i_pos = MP4_GetTrackPos( &track );
502 //msg_Dbg( p_input, "stream %d size=%6d pos=%8lld", i_track, i_size, i_pos );
505 if( MP4_SeekAbsolute( p_input, i_pos ) )
507 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
508 MP4_TrackUnselect( p_input, &track );
513 /* now create a pes */
514 if( !(p_pes = input_NewPES( p_input->p_method_data ) ) )
518 /* and a data packet for the data */
519 if( !(p_data = input_NewPacket( p_input->p_method_data, i_size ) ) )
521 input_DeletePES( p_input->p_method_data, p_pes );
524 p_data->p_payload_end = p_data->p_payload_start + i_size;
526 /* initialisation of all the field */
527 p_pes->i_dts = p_pes->i_pts = 0;
528 p_pes->p_first = p_pes->p_last = p_data;
529 p_pes->i_nb_data = 1;
530 p_pes->i_pes_size = i_size;
533 if( MP4_ReadData( p_input, p_data->p_payload_start, i_size ) )
535 input_DeletePES( p_input->p_method_data, p_pes );
537 msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", track.i_track_ID );
538 MP4_TrackUnselect( p_input, &track );
544 p_pes->i_pts = input_ClockGetTS( p_input,
545 p_input->stream.p_selected_program,
546 MP4_GetTrackPTS( &track ) * 9/100);
548 if( track.p_es->p_decoder_fifo )
551 p_pes->i_rate = p_input->stream.control.i_rate;
552 input_DecodePES( track.p_es->p_decoder_fifo, p_pes );
556 input_DeletePES( p_input->p_method_data, p_pes );
559 if( MP4_TrackNextSample( p_input, &track ) )
570 /*****************************************************************************
571 * Seek: Got to i_date
572 ******************************************************************************/
573 static int Seek ( input_thread_t *p_input, mtime_t i_date )
575 demux_sys_t *p_demux = p_input->p_demux_data;
576 unsigned int i_track;
577 /* First update update global time */
578 p_demux->i_time = i_date * p_demux->i_timescale / 1000000;
579 p_demux->i_pcr = i_date* 9 / 100;
581 /* Now for each stream try to go to this time */
582 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
584 #define track p_demux->track[i_track]
585 if( track.b_ok && track.b_selected )
587 MP4_TrackSeek( p_input, &track, i_date );
594 /*****************************************************************************
596 *****************************************************************************/
597 static int Control ( input_thread_t *p_input, int i_query, va_list args )
599 demux_sys_t *p_sys = p_input->p_demux_data;
606 case DEMUX_GET_POSITION:
607 pf = (double*)va_arg( args, double * );
608 if( p_sys->i_duration > 0 )
610 *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
618 case DEMUX_SET_POSITION:
619 f = (double)va_arg( args, double );
622 (double)p_sys->i_duration /
623 (double)p_sys->i_timescale );
624 return Seek( p_input, i64 );
627 pi64 = (int64_t*)va_arg( args, int64_t * );
628 *pi64 = (mtime_t)1000000 *
629 (mtime_t)p_sys->i_time /
630 (mtime_t)p_sys->i_timescale;
634 i64 = (int64_t)va_arg( args, int64_t );
635 return Seek( p_input, i64 );
637 case DEMUX_GET_LENGTH:
638 pi64 = (int64_t*)va_arg( args, int64_t * );
639 *pi64 = (mtime_t)1000000 *
640 (mtime_t)p_sys->i_duration /
641 (mtime_t)p_sys->i_timescale;
645 msg_Err( p_input, "control query unimplemented !!!" );
646 return demux_vaControlDefault( p_input, i_query, args );
650 /*****************************************************************************
651 * Close: frees unused data
652 *****************************************************************************/
653 static void Close ( vlc_object_t * p_this )
655 unsigned int i_track;
656 input_thread_t * p_input = (input_thread_t *)p_this;
657 demux_sys_t *p_demux = p_input->p_demux_data;
659 msg_Dbg( p_input, "freeing all memory" );
660 MP4_BoxFree( p_input, &p_demux->box_root );
661 for( i_track = 0; i_track < p_demux->i_tracks; i_track++ )
663 MP4_TrackDestroy( p_input, &p_demux->track[i_track] );
665 FREE( p_demux->track );
667 FREE( p_input->p_demux_data );
672 /****************************************************************************
673 * Local functions, specific to vlc
674 ****************************************************************************/
676 /* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
677 static int TrackCreateChunksIndex( input_thread_t *p_input,
678 track_data_mp4_t *p_demux_track )
680 MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
683 unsigned int i_chunk;
684 unsigned int i_index, i_last;
686 if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
687 !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
688 ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
690 return( VLC_EGENERIC );
693 p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
694 if( !p_demux_track->i_chunk_count )
696 msg_Warn( p_input, "no chunk defined" );
697 return( VLC_EGENERIC );
699 p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
700 sizeof( chunk_data_mp4_t ) );
702 /* first we read chunk offset */
703 for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
705 p_demux_track->chunk[i_chunk].i_offset =
706 p_co64->data.p_co64->i_chunk_offset[i_chunk];
709 /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
710 to be used for the sample XXX begin to 1
711 We construct it begining at the end */
712 i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
713 i_index = p_stsc->data.p_stsc->i_entry_count;
716 msg_Warn( p_input, "cannot read chunk table or table empty" );
717 return( VLC_EGENERIC );
723 for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
724 i_chunk < i_last; i_chunk++ )
726 p_demux_track->chunk[i_chunk].i_sample_description_index =
727 p_stsc->data.p_stsc->i_sample_description_index[i_index];
728 p_demux_track->chunk[i_chunk].i_sample_count =
729 p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
731 i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
734 p_demux_track->chunk[0].i_sample_first = 0;
735 for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
737 p_demux_track->chunk[i_chunk].i_sample_first =
738 p_demux_track->chunk[i_chunk-1].i_sample_first +
739 p_demux_track->chunk[i_chunk-1].i_sample_count;
743 "track[Id 0x%x] read %d chunk",
744 p_demux_track->i_track_ID,
745 p_demux_track->i_chunk_count );
747 return( VLC_SUCCESS );
749 static int TrackCreateSamplesIndex( input_thread_t *p_input,
750 track_data_mp4_t *p_demux_track )
752 MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
753 ctts make same mapping but for composition time,
754 not yet used and probably not usefull */
755 MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
756 that uses a compressed form FIXME make them in libmp4
758 /* TODO use also stss and stsh table for seeking */
759 /* FIXME use edit table */
764 int64_t i_index_sample_used;
768 p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
769 p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
771 if( ( !p_stts )||( !p_stsz ) )
773 msg_Warn( p_input, "cannot read sample table" );
774 return( VLC_EGENERIC );
777 p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;
780 /* for sample size, there are 2 case */
781 if( p_stsz->data.p_stsz->i_sample_size )
783 /* 1: all sample have the same size, so no need to construct a table */
784 p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
785 p_demux_track->p_sample_size = NULL;
789 /* 2: each sample can have a different size */
790 p_demux_track->i_sample_size = 0;
791 p_demux_track->p_sample_size =
792 calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
794 for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
796 p_demux_track->p_sample_size[i_sample] =
797 p_stsz->data.p_stsz->i_entry_size[i_sample];
800 /* we have extract all information from stsz,
803 /* if we don't want to waste too much memory, we can't expand
804 the box !, so each chunk will contain an "extract" of this table
808 i_index = 0; i_index_sample_used =0;
810 /* create and init last data for each chunk */
811 for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
814 int64_t i_entry, i_sample_count, i;
816 p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
817 /* count how many entries needed for this chunk
818 for p_sample_delta_dts and p_sample_count_dts */
820 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
823 while( i_sample_count > 0 )
825 i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
828 i_sample_count += i_index_sample_used; /* don't count already used sample
835 p_demux_track->chunk[i_chunk].p_sample_count_dts =
836 calloc( i_entry, sizeof( uint32_t ) );
837 p_demux_track->chunk[i_chunk].p_sample_delta_dts =
838 calloc( i_entry, sizeof( uint32_t ) );
841 i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
842 for( i = 0; i < i_entry; i++ )
847 i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;
849 i_used = __MIN( i_rest, i_sample_count );
851 i_index_sample_used += i_used;
852 i_sample_count -= i_used;
854 p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;
856 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
857 p_stts->data.p_stts->i_sample_delta[i_index];
859 i_last_dts += i_used *
860 p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];
862 if( i_index_sample_used >=
863 p_stts->data.p_stts->i_sample_count[i_index] )
867 i_index_sample_used = 0;
874 "track[Id 0x%x] read %d samples length:"I64Fd"s",
875 p_demux_track->i_track_ID,
876 p_demux_track->i_sample_count,
877 i_last_dts / p_demux_track->i_timescale );
879 return( VLC_SUCCESS );
884 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
886 static int TrackCreateES ( input_thread_t *p_input,
887 track_data_mp4_t *p_track,
888 unsigned int i_chunk,
889 es_descriptor_t **pp_es,
890 pes_packet_t **pp_pes )
892 MP4_Box_t * p_sample;
896 unsigned int i_decoder_specific_info_len;
897 uint8_t * p_decoder_specific_info;
899 es_descriptor_t *p_es;
900 pes_packet_t *p_pes_init;
903 BITMAPINFOHEADER *p_bih;
908 if( !p_track->chunk[i_chunk].i_sample_description_index )
911 "invalid SampleEntry index (track[Id 0x%x])",
912 p_track->i_track_ID );
913 return( VLC_EGENERIC );
916 p_sample = MP4_BoxGet( p_track->p_stsd,
918 p_track->chunk[i_chunk].i_sample_description_index - 1 );
920 if( !p_sample || !p_sample->data.p_data )
923 "cannot find SampleEntry (track[Id 0x%x])",
924 p_track->i_track_ID );
925 return( VLC_EGENERIC );
928 p_track->p_sample = p_sample;
930 if( p_track->i_sample_size == 1 )
932 MP4_Box_data_sample_soun_t *p_soun;
934 p_soun = p_sample->data.p_sample_soun;
936 if( p_soun->i_qt_version == 0 )
938 switch( p_sample->i_type )
940 case VLC_FOURCC( 'i', 'm', 'a', '4' ):
941 p_soun->i_qt_version = 1;
942 p_soun->i_sample_per_packet = 64;
943 p_soun->i_bytes_per_packet = 34;
944 p_soun->i_bytes_per_frame = 34 * p_soun->i_channelcount;
945 p_soun->i_bytes_per_sample = 2;
947 case VLC_FOURCC( 'M', 'A', 'C', '3' ):
948 p_soun->i_qt_version = 1;
949 p_soun->i_sample_per_packet = 6;
950 p_soun->i_bytes_per_packet = 2;
951 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
952 p_soun->i_bytes_per_sample = 2;
954 case VLC_FOURCC( 'M', 'A', 'C', '6' ):
955 p_soun->i_qt_version = 1;
956 p_soun->i_sample_per_packet = 12;
957 p_soun->i_bytes_per_packet = 2;
958 p_soun->i_bytes_per_frame = 2 * p_soun->i_channelcount;
959 p_soun->i_bytes_per_sample = 2;
967 /* Initialise ES, first language as description */
968 for( i = 0; i < 3; i++ )
970 psz_lang[i] = p_track->i_language[i];
974 vlc_mutex_lock( &p_input->stream.stream_lock );
975 p_es = input_AddES( p_input, p_input->stream.p_selected_program,
976 p_track->i_track_ID, p_track->i_cat, psz_lang, 0 );
977 vlc_mutex_unlock( &p_input->stream.stream_lock );
979 p_es->i_stream_id = p_track->i_track_ID;
981 /* It's a little ugly but .. there are special cases */
982 switch( p_sample->i_type )
984 case( VLC_FOURCC( '.', 'm', 'p', '3' ) ):
985 case( VLC_FOURCC( 'm', 's', 0x00, 0x55 ) ):
986 p_es->i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'a' );
988 case( VLC_FOURCC( 'r', 'a', 'w', ' ' ) ):
989 p_es->i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
991 case( VLC_FOURCC( 's', '2', '6', '3' ) ):
992 p_es->i_fourcc = VLC_FOURCC( 'h', '2', '6', '3' );
995 p_es->i_fourcc = p_sample->i_type;
999 i_decoder_specific_info_len = 0;
1000 p_decoder_specific_info = NULL;
1003 /* now see if esds is present and if so create a data packet
1004 with decoder_specific_info */
1005 #define p_decconfig p_esds->data.p_esds->es_descriptor.p_decConfigDescr
1006 if( ( p_esds = MP4_BoxGet( p_sample, "esds" ) )&&
1007 ( p_esds->data.p_esds )&&
1010 /* First update information based on i_objectTypeIndication */
1011 switch( p_decconfig->i_objectTypeIndication )
1013 case( 0x20 ): /* MPEG4 VIDEO */
1014 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','v' );
1017 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1024 case( 0x65): /* MPEG2 video */
1025 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1027 /* Theses are MPEG2-AAC */
1028 case( 0x66): /* main profile */
1029 case( 0x67): /* Low complexity profile */
1030 case( 0x68): /* Scaleable Sampling rate profile */
1031 p_es->i_fourcc = VLC_FOURCC( 'm','p','4','a' );
1033 /* true MPEG 2 audio */
1035 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1037 case( 0x6a): /* MPEG1 video */
1038 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','v' );
1040 case( 0x6b): /* MPEG1 audio */
1041 p_es->i_fourcc = VLC_FOURCC( 'm','p','g','a' );
1043 case( 0x6c ): /* jpeg */
1044 p_es->i_fourcc = VLC_FOURCC( 'j','p','e','g' );
1047 /* Unknown entry, but don't touch i_fourcc */
1049 "unknown objectTypeIndication(0x%x) (Track[ID 0x%x])",
1050 p_decconfig->i_objectTypeIndication,
1051 p_track->i_track_ID );
1054 i_decoder_specific_info_len =
1055 p_decconfig->i_decoder_specific_info_len;
1056 p_decoder_specific_info =
1057 p_decconfig->p_decoder_specific_info;
1061 switch( p_sample->i_type )
1063 /* qt decoder, send the complete chunk */
1064 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1065 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
1066 case VLC_FOURCC( 'V', 'P', '3', '1' ):
1067 case VLC_FOURCC( '3', 'I', 'V', '1' ):
1068 case VLC_FOURCC( 'Z', 'y', 'G', 'o' ):
1069 i_decoder_specific_info_len = p_sample->data.p_sample_vide->i_qt_image_description;
1070 p_decoder_specific_info = p_sample->data.p_sample_vide->p_qt_image_description;
1072 case VLC_FOURCC( 'Q', 'D', 'M', 'C' ):
1073 case VLC_FOURCC( 'Q', 'D', 'M', '2' ):
1074 case VLC_FOURCC( 'Q', 'c', 'l', 'p' ):
1075 i_decoder_specific_info_len = p_sample->data.p_sample_soun->i_qt_description;
1076 p_decoder_specific_info = p_sample->data.p_sample_soun->p_qt_description;
1085 /* some last initialisation */
1086 /* XXX I create a bitmapinfoheader_t or
1087 waveformatex_t for each stream, up to now it's the best thing
1088 I've found but it could exist a better solution :) as something
1089 like adding some new fields in p_es ...
1091 XXX I don't set all values, only thoses that are interesting or known
1092 --> bitmapinfoheader_t : width and height
1093 --> waveformatex_t : channels, samplerate, bitspersample
1094 and at the end I add p_decoder_specific_info
1096 TODO set more values
1100 switch( p_track->i_cat )
1103 /* now create a bitmapinfoheader_t for decoder and
1104 add information found in p_esds */
1105 /* XXX XXX + 16 are for avoid segfault when ffmpeg access beyong the data */
1106 p_init = malloc( sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len + 16 );
1107 p_bih = (BITMAPINFOHEADER*)p_init;
1109 p_bih->biSize = sizeof( BITMAPINFOHEADER ) + i_decoder_specific_info_len;
1110 p_bih->biWidth = p_sample->data.p_sample_vide->i_width;
1111 p_bih->biHeight = p_sample->data.p_sample_vide->i_height;
1112 p_bih->biPlanes = 1; // FIXME
1113 p_bih->biBitCount = 0; // FIXME
1114 p_bih->biCompression = 0; // FIXME
1115 p_bih->biSizeImage = 0; // FIXME
1116 p_bih->biXPelsPerMeter = 0; // FIXME
1117 p_bih->biYPelsPerMeter = 0; // FIXME
1118 p_bih->biClrUsed = 0; // FIXME
1119 p_bih->biClrImportant = 0; // FIXME
1121 if( p_bih->biWidth == 0 )
1123 // fall on display size
1124 p_bih->biWidth = p_track->i_width;
1126 if( p_bih->biHeight == 0 )
1128 // fall on display size
1129 p_bih->biHeight = p_track->i_height;
1132 if( i_decoder_specific_info_len )
1134 data_packet_t *p_data;
1136 memcpy( p_init + sizeof( BITMAPINFOHEADER ),
1137 p_decoder_specific_info,
1138 i_decoder_specific_info_len);
1140 /* If stream is mpeg4 video we send specific_info,
1141 as it's needed to decode it (vol) */
1142 switch( p_es->i_fourcc )
1144 case VLC_FOURCC( 'm','p','4','v' ):
1145 case VLC_FOURCC( 'D','I','V','X' ):
1146 case VLC_FOURCC( 'd','i','v','x' ):
1147 p_pes_init = input_NewPES( p_input->p_method_data );
1148 p_data = input_NewPacket( p_input->p_method_data,
1149 i_decoder_specific_info_len);
1150 p_data->p_payload_end = p_data->p_payload_start + i_decoder_specific_info_len;
1152 memcpy( p_data->p_payload_start,
1153 p_decoder_specific_info,
1154 i_decoder_specific_info_len );
1155 p_pes_init->i_dts = p_pes_init->i_pts = 0;
1156 p_pes_init->p_first = p_pes_init->p_last = p_data;
1157 p_pes_init->i_nb_data = 1;
1158 p_pes_init->i_pes_size = i_decoder_specific_info_len;
1168 p_init = malloc( sizeof( WAVEFORMATEX ) + i_decoder_specific_info_len + 16 );
1169 p_wf = (WAVEFORMATEX*)p_init;
1171 p_wf->wFormatTag = 1;
1172 p_wf->nChannels = p_sample->data.p_sample_soun->i_channelcount;
1173 p_wf->nSamplesPerSec = p_sample->data.p_sample_soun->i_sampleratehi;
1174 p_wf->nAvgBytesPerSec = p_sample->data.p_sample_soun->i_channelcount *
1175 p_sample->data.p_sample_soun->i_sampleratehi *
1176 p_sample->data.p_sample_soun->i_samplesize / 8;
1177 p_wf->nBlockAlign = 0;
1178 p_wf->wBitsPerSample = p_sample->data.p_sample_soun->i_samplesize;
1179 p_wf->cbSize = i_decoder_specific_info_len;
1181 if( i_decoder_specific_info_len )
1183 memcpy( p_init + sizeof( WAVEFORMATEX ),
1184 p_decoder_specific_info,
1185 i_decoder_specific_info_len);
1194 if( p_es->i_cat == AUDIO_ES )
1196 p_es->p_bitmapinfoheader = NULL;
1197 p_es->p_waveformatex = (void*)p_init;
1199 else if( p_es->i_cat == VIDEO_ES )
1201 p_es->p_bitmapinfoheader = (void*)p_init;
1202 p_es->p_waveformatex = NULL;
1206 *pp_pes = p_pes_init;
1207 return( VLC_SUCCESS );
1210 /* given a time it return sample/chunk */
1211 static int TrackTimeToSampleChunk( input_thread_t *p_input,
1212 track_data_mp4_t *p_track,
1215 uint32_t *pi_sample )
1219 unsigned int i_sample;
1220 unsigned int i_chunk;
1223 /* convert absolute time to in timescale unit */
1224 i_start = i_start * (mtime_t)p_track->i_timescale / (mtime_t)1000000;
1226 /* FIXME see if it's needed to check p_track->i_chunk_count */
1227 if( !p_track->b_ok || p_track->i_chunk_count == 0 )
1229 return( VLC_EGENERIC );
1232 /* we start from sample 0/chunk 0, hope it won't take too much time */
1233 /* *** find good chunk *** */
1234 for( i_chunk = 0; ; i_chunk++ )
1236 if( i_chunk + 1 >= p_track->i_chunk_count )
1238 /* at the end and can't check if i_start in this chunk,
1239 it will be check while searching i_sample */
1240 i_chunk = p_track->i_chunk_count - 1;
1244 if( i_start >= p_track->chunk[i_chunk].i_first_dts &&
1245 i_start < p_track->chunk[i_chunk + 1].i_first_dts )
1251 /* *** find sample in the chunk *** */
1252 i_sample = p_track->chunk[i_chunk].i_sample_first;
1253 i_dts = p_track->chunk[i_chunk].i_first_dts;
1254 for( i_index = 0; i_sample < p_track->chunk[i_chunk].i_sample_count; )
1257 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1258 p_track->chunk[i_chunk].p_sample_delta_dts[i_index] < i_start )
1261 p_track->chunk[i_chunk].p_sample_count_dts[i_index] *
1262 p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1264 i_sample += p_track->chunk[i_chunk].p_sample_count_dts[i_index];
1269 if( p_track->chunk[i_chunk].p_sample_delta_dts[i_index] <= 0 )
1273 i_sample += ( i_start - i_dts ) / p_track->chunk[i_chunk].p_sample_delta_dts[i_index];
1278 if( i_sample >= p_track->i_sample_count )
1281 "track[Id 0x%x] will be disabled (seeking too far) chunk=%d sample=%d",
1282 p_track->i_track_ID, i_chunk, i_sample );
1283 return( VLC_EGENERIC );
1287 /* *** Try to find nearest sync points *** */
1288 if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
1290 unsigned int i_index;
1292 "track[Id 0x%x] using Sync Sample Box (stss)",
1293 p_track->i_track_ID );
1294 for( i_index = 0; i_index < p_stss->data.p_stss->i_entry_count; i_index++ )
1296 if( p_stss->data.p_stss->i_sample_number[i_index] >= i_sample )
1300 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1302 p_stss->data.p_stss->i_sample_number[i_index-1] );
1303 i_sample = p_stss->data.p_stss->i_sample_number[i_index-1];
1304 /* new i_sample is less than old so i_chunk can only decreased */
1305 while( i_chunk > 0 &&
1306 i_sample < p_track->chunk[i_chunk].i_sample_first )
1313 msg_Dbg( p_input, "stts gives %d --> %d (sample number)",
1315 p_stss->data.p_stss->i_sample_number[i_index] );
1316 i_sample = p_stss->data.p_stss->i_sample_number[i_index];
1317 /* new i_sample is more than old so i_chunk can only increased */
1318 while( i_chunk < p_track->i_chunk_count - 1 &&
1319 i_sample >= p_track->chunk[i_chunk].i_sample_first +
1320 p_track->chunk[i_chunk].i_sample_count )
1332 "track[Id 0x%x] does not provide Sync Sample Box (stss)",
1333 p_track->i_track_ID );
1336 if( pi_chunk ) *pi_chunk = i_chunk;
1337 if( pi_sample ) *pi_sample = i_sample;
1338 return( VLC_SUCCESS );
1341 static int TrackGotoChunkSample( input_thread_t *p_input,
1342 track_data_mp4_t *p_track,
1343 unsigned int i_chunk,
1344 unsigned int i_sample )
1346 /* now see if actual es is ok */
1347 if( p_track->i_chunk < 0 ||
1348 p_track->i_chunk >= p_track->i_chunk_count ||
1349 p_track->chunk[p_track->i_chunk].i_sample_description_index !=
1350 p_track->chunk[i_chunk].i_sample_description_index )
1352 msg_Warn( p_input, "Recreate ES" );
1354 /* no :( recreate es */
1355 vlc_mutex_lock( &p_input->stream.stream_lock );
1356 input_DelES( p_input, p_track->p_es );
1357 vlc_mutex_unlock( &p_input->stream.stream_lock );
1359 if( p_track->p_pes_init )
1361 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1364 if( TrackCreateES( p_input,
1367 &p_track->p_pes_init ) )
1369 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1370 p_track->i_track_ID );
1372 p_track->b_ok = VLC_FALSE;
1373 p_track->b_selected = VLC_FALSE;
1374 return( VLC_EGENERIC );
1378 /* select again the new decoder */
1379 if( p_track->b_selected && p_track->p_es && p_track->p_es->p_decoder_fifo == NULL )
1381 vlc_mutex_lock( &p_input->stream.stream_lock );
1382 input_SelectES( p_input, p_track->p_es );
1383 vlc_mutex_unlock( &p_input->stream.stream_lock );
1385 if( p_track->p_es->p_decoder_fifo )
1387 if( p_track->p_pes_init != NULL )
1389 p_track->p_pes_init->i_rate = p_input->stream.control.i_rate;
1391 input_DecodePES( p_track->p_es->p_decoder_fifo,
1392 p_track->p_pes_init );
1393 p_track->p_pes_init = NULL;
1395 p_track->b_selected = VLC_TRUE;
1399 msg_Dbg( p_input, "Argg cannot select this stream" );
1400 if( p_track->p_pes_init != NULL )
1402 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1403 p_track->p_pes_init = NULL;
1405 p_track->b_selected = VLC_FALSE;
1409 p_track->i_chunk = i_chunk;
1410 p_track->i_sample = i_sample;
1412 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1415 /****************************************************************************
1417 ****************************************************************************
1418 * Parse track information and create all needed data to run a track
1419 * If it succeed b_ok is set to 1 else to 0
1420 ****************************************************************************/
1421 static void MP4_TrackCreate( input_thread_t *p_input,
1422 track_data_mp4_t *p_track,
1423 MP4_Box_t * p_box_trak )
1427 MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
1428 MP4_Box_t *p_tref = MP4_BoxGet( p_box_trak, "tref" );
1437 /* hint track unsuported */
1439 /* set default value (-> track unusable) */
1440 p_track->b_ok = VLC_FALSE;
1441 p_track->b_enable = VLC_FALSE;
1442 p_track->b_selected = VLC_FALSE;
1444 p_track->i_cat = UNKNOWN_ES;
1451 /* do we launch this track by default ? */
1453 ( ( p_tkhd->data.p_tkhd->i_flags&MP4_TRACK_ENABLED ) != 0 );
1455 p_track->i_track_ID = p_tkhd->data.p_tkhd->i_track_ID;
1456 p_track->i_width = p_tkhd->data.p_tkhd->i_width / 65536;
1457 p_track->i_height = p_tkhd->data.p_tkhd->i_height / 65536;
1459 if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
1461 /* msg_Warn( p_input, "unhandled box: edts --> FIXME" ); */
1466 /* msg_Warn( p_input, "unhandled box: tref --> FIXME" ); */
1469 p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
1470 p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
1472 if( ( !p_mdhd )||( !p_hdlr ) )
1477 p_track->i_timescale = p_mdhd->data.p_mdhd->i_timescale;
1479 for( i = 0; i < 3; i++ )
1481 p_track->i_language[i] = p_mdhd->data.p_mdhd->i_language[i];
1483 p_mdhd->data.p_mdhd->i_language[3] = 0;
1485 switch( p_hdlr->data.p_hdlr->i_handler_type )
1487 case( FOURCC_soun ):
1488 if( !( p_smhd = MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) ) )
1492 p_track->i_cat = AUDIO_ES;
1495 case( FOURCC_vide ):
1496 if( !( p_vmhd = MP4_BoxGet( p_box_trak, "mdia/minf/vmhd" ) ) )
1500 p_track->i_cat = VIDEO_ES;
1508 p_dinf = MP4_BoxGet( p_minf, "dinf" );
1510 if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) )
1515 if( !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
1520 /* fxi i_timescale for AUDIO_ES with i_qt_version == 0 */
1521 if( p_track->i_cat == AUDIO_ES ) //&& p_track->i_sample_size == 1 )
1523 MP4_Box_t *p_sample;
1525 p_sample = MP4_BoxGet( p_track->p_stsd, "[0]" );
1526 if( p_sample && p_sample->data.p_sample_soun)
1528 MP4_Box_data_sample_soun_t *p_soun = p_sample->data.p_sample_soun;
1529 if( p_soun->i_qt_version == 0 && p_track->i_timescale != p_soun->i_sampleratehi )
1531 msg_Warn( p_input, "i_timescale != i_sampleratehi with qt_version == 0\nMaking both equal ? (report any problem)" );
1532 p_track->i_timescale = p_soun->i_sampleratehi;
1538 /* Create chunk index table */
1539 if( TrackCreateChunksIndex( p_input,p_track ) )
1541 return; /* cannot create chunks index */
1544 /* create sample index table needed for reading and seeking */
1545 if( TrackCreateSamplesIndex( p_input, p_track ) )
1547 return; /* cannot create samples index */
1550 p_track->i_chunk = 0;
1551 p_track->i_sample = 0;
1552 /* now create es but does not select it */
1553 /* XXX needed else vlc won't know this track exist */
1554 if( TrackCreateES( p_input,
1555 p_track, p_track->i_chunk,
1557 &p_track->p_pes_init ) )
1559 msg_Err( p_input, "cannot create es for track[Id 0x%x]",
1560 p_track->i_track_ID );
1567 for( i = 0; i < p_track->i_chunk_count; i++ )
1569 fprintf( stderr, "%-5d sample_count=%d pts=%lld\n", i, p_track->chunk[i].i_sample_count, p_track->chunk[i].i_first_dts );
1574 p_track->b_ok = VLC_TRUE;
1577 /****************************************************************************
1579 ****************************************************************************
1580 * Destroy a track created by MP4_TrackCreate.
1581 ****************************************************************************/
1582 static void MP4_TrackDestroy( input_thread_t *p_input,
1583 track_data_mp4_t *p_track )
1585 unsigned int i_chunk;
1587 p_track->b_ok = VLC_FALSE;
1588 p_track->b_enable = VLC_FALSE;
1589 p_track->b_selected = VLC_FALSE;
1591 p_track->i_cat = UNKNOWN_ES;
1593 if( p_track->p_pes_init )
1595 input_DeletePES( p_input->p_method_data, p_track->p_pes_init );
1598 for( i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
1600 if( p_track->chunk )
1602 FREE(p_track->chunk[i_chunk].p_sample_count_dts);
1603 FREE(p_track->chunk[i_chunk].p_sample_delta_dts );
1606 FREE( p_track->chunk );
1608 if( !p_track->i_sample_size )
1610 FREE( p_track->p_sample_size );
1614 static int MP4_TrackSelect ( input_thread_t *p_input,
1615 track_data_mp4_t *p_track,
1621 if( !p_track->b_ok )
1623 return( VLC_EGENERIC );
1626 if( p_track->b_selected )
1629 "track[Id 0x%x] already selected",
1630 p_track->i_track_ID );
1631 return( VLC_SUCCESS );
1634 if( TrackTimeToSampleChunk( p_input,
1636 &i_chunk, &i_sample ) )
1639 "cannot select track[Id 0x%x]",
1640 p_track->i_track_ID );
1641 return( VLC_EGENERIC );
1644 p_track->b_selected = VLC_TRUE;
1646 if( TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample ) )
1648 p_track->b_selected = VLC_FALSE;
1651 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1654 static void MP4_TrackUnselect(input_thread_t *p_input,
1655 track_data_mp4_t *p_track )
1657 if( !p_track->b_ok )
1662 if( !p_track->b_selected )
1665 "track[Id 0x%x] already unselected",
1666 p_track->i_track_ID );
1670 if( p_track->p_es->p_decoder_fifo )
1672 vlc_mutex_lock( &p_input->stream.stream_lock );
1673 input_UnselectES( p_input, p_track->p_es );
1674 vlc_mutex_unlock( &p_input->stream.stream_lock );
1677 p_track->b_selected = VLC_FALSE;
1680 static int MP4_TrackSeek ( input_thread_t *p_input,
1681 track_data_mp4_t *p_track,
1687 if( !p_track->b_ok )
1689 return( VLC_EGENERIC );
1692 if( TrackTimeToSampleChunk( p_input,
1694 &i_chunk, &i_sample ) )
1697 "cannot select track[Id 0x%x]",
1698 p_track->i_track_ID );
1699 return( VLC_EGENERIC );
1702 p_track->b_selected = VLC_TRUE;
1704 TrackGotoChunkSample( p_input, p_track, i_chunk, i_sample );
1706 return( p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC );
1714 * 3 types: for audio
1717 #define QT_V0_MAX_SAMPLES 1500
1718 static int MP4_TrackSampleSize( track_data_mp4_t *p_track )
1721 MP4_Box_data_sample_soun_t *p_soun;
1723 if( p_track->i_sample_size == 0 )
1725 /* most simple case */
1726 return( p_track->p_sample_size[p_track->i_sample] );
1728 if( p_track->i_cat != AUDIO_ES )
1730 return( p_track->i_sample_size );
1733 if( p_track->i_sample_size != 1 )
1735 //msg_Warn( p_input, "SampleSize != 1" );
1736 return( p_track->i_sample_size );
1739 p_soun = p_track->p_sample->data.p_sample_soun;
1741 if( p_soun->i_qt_version == 1 )
1743 i_size = p_track->chunk[p_track->i_chunk].i_sample_count / p_soun->i_sample_per_packet * p_soun->i_bytes_per_frame;
1748 int i_samples = p_track->chunk[p_track->i_chunk].i_sample_count -
1749 ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first );
1750 if( i_samples > QT_V0_MAX_SAMPLES )
1752 i_samples = QT_V0_MAX_SAMPLES;
1754 i_size = i_samples * p_soun->i_channelcount * p_soun->i_samplesize / 8;
1757 //fprintf( stderr, "size=%d\n", i_size );
1762 static uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
1764 unsigned int i_sample;
1768 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
1770 if( p_track->i_sample_size )
1772 MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
1774 if( p_soun->i_qt_version == 0 )
1776 i_pos += ( p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first ) *
1777 p_soun->i_channelcount * p_soun->i_samplesize / 8;
1781 /* we read chunk by chunk */
1787 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
1788 i_sample < p_track->i_sample; i_sample++ )
1790 i_pos += p_track->p_sample_size[i_sample];
1797 static int MP4_TrackNextSample( input_thread_t *p_input,
1798 track_data_mp4_t *p_track )
1801 if( p_track->i_cat == AUDIO_ES &&
1802 p_track->i_sample_size != 0 )
1804 MP4_Box_data_sample_soun_t *p_soun;
1806 p_soun = p_track->p_sample->data.p_sample_soun;
1808 if( p_soun->i_qt_version == 1 )
1810 /* chunk by chunk */
1812 p_track->chunk[p_track->i_chunk].i_sample_first +
1813 p_track->chunk[p_track->i_chunk].i_sample_count;
1818 p_track->i_sample += QT_V0_MAX_SAMPLES;
1819 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 )
1822 p_track->chunk[p_track->i_chunk].i_sample_first +
1823 p_track->chunk[p_track->i_chunk].i_sample_count;
1829 p_track->i_sample++;
1832 if( p_track->i_sample >= p_track->i_sample_count )
1834 /* we have reach end of the track so free decoder stuff */
1835 msg_Warn( p_input, "track[0x%x] will be disabled", p_track->i_track_ID );
1836 MP4_TrackUnselect( p_input, p_track );
1837 return( VLC_EGENERIC );
1840 /* Have we changed chunk ? */
1841 if( p_track->i_sample >=
1842 p_track->chunk[p_track->i_chunk].i_sample_first +
1843 p_track->chunk[p_track->i_chunk].i_sample_count )
1845 if( TrackGotoChunkSample( p_input,
1847 p_track->i_chunk + 1,
1848 p_track->i_sample ) )
1850 msg_Warn( p_input, "track[0x%x] will be disabled (cannot restart decoder)", p_track->i_track_ID );
1851 MP4_TrackUnselect( p_input, p_track );
1852 return( VLC_EGENERIC );
1856 return( VLC_SUCCESS );